• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] More Spell Optimization

Status
Not open for further replies.
Level 9
Joined
Apr 23, 2011
Messages
460
I'll try to make this one of my last (there will be probably more) posts for just looking over my code to make sure I'm doing things right. Any comments? Suggestions?

JASS:
library ManaStrikes initializer init uses RegisterPlayerUnitEvent
    globals
        private constant real DAMAGEBONUS     = .05
        private constant real DRAINAMOUNT     = .025
        private constant integer PROBPROC     = 5
        private constant integer SPELL_ID     = 'MaSt'
    endglobals
    
    scope ManaStrikes
        private function damage takes unit caster, unit target, integer mana returns nothing
            local real damageDealt = mana * DAMAGEBONUS
            local real manaDrained = mana * DRAINAMOUNT
            call UnitDamageTarget(caster, target, damageDealt, true, true, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_WHOKNOWS)
            call SetUnitState(caster, UNIT_STATE_MANA, GetUnitState(caster, UNIT_STATE_MANA) - manaDrained)
        endfunction
        
        private function spell takes nothing returns nothing
            local unit caster = GetAttacker()
            local unit target = GetTriggerUnit()
            local integer mana = R2I(GetUnitState(caster, UNIT_STATE_MANA))
            local integer probable = GetUnitAbilityLevel(caster, SPELL_ID) * PROBPROC
            local integer random = GetRandomInt(1, 100)
            if random <= probable then 
                call damage(caster, target, mana)
            endif
            set caster = null
            set target = null
        endfunction
        
        function castMS takes nothing returns nothing
            local unit attacker = GetAttacker()
            if GetUnitAbilityLevel(attacker, SPELL_ID) >= 1 and not IsUnitAlly(attacker, GetTriggerPlayer()) then
                call spell()
            endif
            set attacker = null
        endfunction
        
    endscope
    
    private function init takes nothing returns nothing
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ATTACKED, function castMS)
    endfunction
endlibrary
 
Last edited:
Level 14
Joined
Nov 18, 2007
Messages
1,084
Not really so much to improve.

  • You could substitute Set/GetUnitState for UNIT_STATE_LIFE with Set/GetWidgetLife. Better yet, use UnitDamageTarget so the caster can receive credit.
  • The mana variable in spell isn't necessary. This also goes for the mana parameter of damage since you're just getting it from the caster.
  • Null attacker in castMS.
  • Can replace GetOwningPlayer(GetTriggerUnit()) with GetTriggerPlayer()
  • Use a damage detection system instead of EVENT_PLAYER_UNIT_ATTACKED since it's more reliable and can't be abused.
 
Level 9
Joined
Apr 23, 2011
Messages
460
Not really so much to improve.

  • You could substitute Set/GetUnitState for UNIT_STATE_LIFE with Set/GetWidgetLife. Better yet, use UnitDamageTarget so the caster can receive credit.
  • The mana variable in spell isn't necessary. This also goes for the mana parameter of damage since you're just getting it from the caster.
  • Null attacker in castMS.
  • Can replace GetOwningPlayer(GetTriggerUnit()) with GetTriggerPlayer()
  • Use a damage detection system instead of EVENT_PLAYER_UNIT_ATTACKED since it's more reliable and can't be abused.

1. Done.

2. I don't understand, I used the mana variable so that I only have to get the mana once, and can pass that value to the damage function with ease.

3. Done.

4. Done.

4. I don't know how to use one, nor which one to use. Suggestions?
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
2. I don't understand, I used the mana variable so that I only have to get the mana once, and can pass that value to the damage function with ease.
You don't use "mana" at all in the spell function except to pass it to the damage function.

4. I don't know how to use one, nor which one to use. Suggestions?
You could try Bribe's GUI damage engine (which is really just Jass code in a GUI trigger) or DamageEvent
 
Level 6
Joined
Oct 23, 2011
Messages
182
maybe this? removed a lot of variables

JASS:
library ManaStrikes initializer init uses RegisterPlayerUnitEvent

    globals
        private constant real DAMAGE_BONUS  = .05
        private constant real DRAIN_AMOUNT  = .025
        private constant integer PROB_PROC  = 5
        private constant integer SPELL_ID   = 'MaSt'
    endglobals
    
    scope ManaStrikes
        
        private function spell takes unit caster returns nothing
            local real mana
            
            if GetRandomInt(1, 100) <= GetUnitAbilityLevel(caster, SPELL_ID) * PROB_PROC then   
                set mana = GetUnitState(caster, UNIT_STATE_MANA)
                
                call UnitDamageTarget(caster, GetTriggerUnit(), mana * DAMAGE_BONUS, true, true, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, null)
                call SetUnitState(caster, UNIT_STATE_MANA, mana * (1 - DRAIN_AMOUNT))
            endif
        endfunction
        
        function castMS takes nothing returns nothing
            local unit attacker = GetAttacker()
            
            if GetUnitAbilityLevel(attacker, SPELL_ID) >= 1 and not IsUnitAlly(attacker, GetTriggerPlayer()) then
                call spell(attacker)
            endif
            
            set attacker = null
        endfunction
    endscope
    
    private function init takes nothing returns nothing
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ATTACKED, function castMS)
    endfunction
    
endlibrary
 
Last edited:
Status
Not open for further replies.
Top