Snippet ShieldUnit

emjlr3

Change can be a good thing
Reaction score
395
Shield damage taken on a unit:

Why?
A unit taking damage can be detected through a trigger. This trigger fires the moment before the target takes the damage. As such, adding said damage back to the targets health, if they are at maximum hp, does not actually block the damage (or some decrement thereof). Since the unit will be at max hp when the damage is actually done, the portion between the difference in the current hp and max hp and the damage done will be taken as damage after the life is added to the unit.

i.e. - Units hp is 75, units max hp is 100, unit takes 50 damage. Add 50 hp to unit, unit now has 100 hp, unit then takes 50 damage, unit is left with 50 hp, not the 75 they should have.

This is undesirable.​

Solution?
Add the health back after the damage is done, in some optimal fashion.​

Issues?
If the damage done is greater then the maximum health the unit has, no amount of added hp before or after the damage will keep it alive. In those cases, you need to actually increase the max hp of the unit temporarily. This snippet does not account for such cases.​

Snippet?

JASS:
library ShieldUnit initializer Init

globals
    private timer Tim=CreateTimer()
    private unit array U
    private real array Heal
    private integer I = 0
endglobals

private function Restore takes nothing returns nothing
    loop
        exitwhen I==0
        call SetWidgetLife(U<i>,Heal<i>)
        set I=I-1
    endloop
endfunction
function ShieldUnit takes unit u, real r, real dam returns boolean 
    if r&lt;=0. or GetUnitState(u,UNIT_STATE_MAX_LIFE)&lt;dam+.405 or (GetWidgetLife(u)+r)-dam&lt;.405 then
        return false
    debug elseif r&gt;dam then
        debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0.,0.,5.,GetUnitName(u)+&quot; is being shielded for more damage then it took.&quot;)
    endif    
    
    set I = I + 1
    set U<i> = u
    set Heal<i> = GetWidgetLife(u)-(dam-r)
    call SetWidgetLife(u,GetUnitState(u,UNIT_STATE_MAX_LIFE))
    
    call ResumeTimer(Tim)
    return true
endfunction

private function Init takes nothing returns nothing
    call TimerStart(Tim,0.0,false,function Restore)
endfunction

endlibrary</i></i></i></i>



Use?
When you want to shield some damage from a unit:
JASS:
call ShieldUnit(unit to shield,damage to shield, damage taken on event)

Insert this snippet into any trigger you desire.
Returns a boolean as to whether the unit was shielded or not.​
 

Romek

Super Moderator
Reaction score
963
The entire issue with the wrong amount being shielded or the kill if more damage is done than the units Max HP can be easily fixed by adding an item ability which gives +99999 health. :)

JASS:
//
    elseif GetWidgetLife(u)&lt;=dam then
        set U = u
        set Dam = (dam+GetWidgetLife(u))-GetUnitState(u,UNIT_STATE_MAX_LIFE)
        call SetWidgetLife(u,GetUnitState(u,UNIT_STATE_MAX_LIFE))
        call TimerStart(T,0.0,false,function Restore)
    else
        set U = u
        set Dam = dam
        call TimerStart(T,0.0,false,function Restore)
    endif

Could be:
JASS:
//
    elseif GetWidgetLife(u)&lt;=dam then
        set dam = (dam+GetWidgetLife(u))-GetUnitState(u,UNIT_STATE_MAX_LIFE)
        call SetWidgetLife(u,GetUnitState(u,UNIT_STATE_MAX_LIFE))
    endif
    set U = u
    set Dam = dam
    call TimerStart(T,0.0,false,function Restore)
 

Artificial

Without Intelligence
Reaction score
326
JASS:
scope Test initializer Init
    
    private function Act takes nothing returns nothing
        call ShieldUnit(GetTriggerUnit(), 50.)
    endfunction
    
    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local unit pea = CreateUnit(Player(0), &#039;hpea&#039;, 0, 0, 0)
        local unit foo = CreateUnit(Player(0), &#039;hfoo&#039;, 0, 0, 0)
        local unit angryman = CreateUnit(Player(0), &#039;Hpal&#039;, 0, 0, 0)
        
        call TriggerRegisterAnyUnitDamaged(t) // Or w/e function you wanna use for this...
        call TriggerAddAction(t, function Act)
        
        call UnitDamageTarget(angryman, pea, 50., false, false, null, null, null)
        call UnitDamageTarget(angryman, foo, 50., false, false, null, null, null)
    endfunction
    
endscope

Only shields the footman from taking damage. Is the snippet not quite MUI, or did I just happen to fail with my testing? ^__^

Okay, might seem like a silly example, but I think the same would happen with a spell that damages two units at the same time, and both of those units have an ability (eg. "Save mi friend!" ^_^) that blocks (some of) the damage with the help of this snippet. :>
 

emjlr3

Change can be a good thing
Reaction score
395
you both are correct

The entire issue with the wrong amount being shielded or the kill if more damage is done than the units Max HP can be easily fixed by adding an item ability which gives +99999 health.

yea, seems sort of silly to require all this when that works just as well in all cases, but its here nonetheless

Only shields the footman from taking damage.

looks like this will require attachment after all - was hoping to avoid that, but from that example its obvious this can bug in such cases

**Updated with bugless version
 

Romek

Super Moderator
Reaction score
963
You could do this without timerutils. Use the a struct array and a single timer.
Would work just as well, but system-less. :)
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
No requirements != desirable. We aren't in the stone age, where we had to import all our systems to the custom script section. Importing a library is a simple copy-paste procedure.

Also, I don't see why everybody is so in love with timer stacks...
 

emjlr3

Change can be a good thing
Reaction score
395
he meant a struct stack - i can imagine how id do it, but for some reason I have a hunch there may be overlaps - might have to give it a try though
 

emjlr3

Change can be a good thing
Reaction score
395
currently, say a unit has 50 hp, is taking 75 damage, and you want to shield 20 of it - the unit will not die from the damage, but will then be set to a hp level below 0

not good

I am going to have to update this to take an argument for damage to account for such situations
 

13lade619

is now a game developer :)
Reaction score
400
is this the proper way to use it?

and function call should be like SU_ShieldUnit() right?
JASS:
scope SU

globals
    private unit array U
    private real array Dam
endglobals

private function H2I takes handle h returns integer
    return h
    return 0
endfunction

private function Restore takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer id = H2I(t)-0x100000

    call SetWidgetLife(U[id],GetWidgetLife(U[id])+Dam[id])
    call ReleaseTimer(t)
endfunction

public function ShieldUnit takes unit u, real dam returns boolean
    local timer t
    local integer id

    if GetUnitState(u,UNIT_STATE_MAX_LIFE)&lt;=dam then
        return false
    elseif GetWidgetLife(u)&lt;=dam then
        set dam = (dam+GetWidgetLife(u))-GetUnitState(u,UNIT_STATE_MAX_LIFE)
        call SetWidgetLife(u,GetUnitState(u,UNIT_STATE_MAX_LIFE))
    endif
    set t = NewTimer()
    set id = H2I(t)-0x100000
    set U[id] = u
    set Dam[id] = dam
    call TimerStart(t,0.0,false,function Restore)

    return true
endfunction

endscope
 

wraithseeker

Tired.
Reaction score
122
I agree with Jesus4Lyf on the H2I use.

Why? said:
It's already 2009 and why aren't you using a indexing system or add wrappers to the H2I stuff so they can easily configure it with their own indexing system.
 

emjlr3

Change can be a good thing
Reaction score
395
Why does this use H2I?
Why does this use attachment?

Just have a list of units to process instead?

thats not a half bad idea - iam going to upload the current version that I have, which works as intended - and make a new one w/o attachment
 

Builder Bob

Live free or don't
Reaction score
249
Isn't it a bit weird that this shield function can actually heal units as well?

I'd check for (GetTriggerEventId() == EVENT_UNIT_DAMAGED) and make sure the damage reduction couldn't go over GetEventDamage().

There's also no need to have unit as an argument to the function, as only the unit taking damage (GetTriggerUnit()) is a valid candidate for the shielding.
 

emjlr3

Change can be a good thing
Reaction score
395
I explained that a few posts up (I need to take a damage argument as well) - and if someone uses IDDS, TriggerUnit will fail (along with GetEventDamage), so I have my reasons

I have not had a chance to update this thread yet
 

Builder Bob

Live free or don't
Reaction score
249
I explained that a few posts up (I need to take a damage argument as well) - and if someone uses IDDS, TriggerUnit will fail (along with GetEventDamage), so I have my reasons

Ok, I didn't know there was any other way to deal damage. I don't know what IDDS is anyway.
 

Jesus4Lyf

Good Idea™
Reaction score
397
thats not a half bad idea - iam going to upload the current version that I have, which works as intended - and make a new one w/o attachment
http://www.thehelper.net/forums/showthread.php?t=127826
Builder Bob's ExecuteWait snippet should do exactly what you require in terms of this no-attachment thing. And after he implements my suggestions, it should be EXTREMELY efficient at it. :)

Just posting it here to save you time re-crafting this yourself. You'll be able to copy/paste implement it if you like too, so you won't even need to require his snippet. But that's up to you. Requiring it is naturally more efficient if the resource can be shared within a map. :)
 

emjlr3

Change can be a good thing
Reaction score
395
updated - should be...actually good this time!
 

Azlier

Old World Ghost
Reaction score
461
It looks shady to me, but only one thing I will recommend you change in my infinite laziness is:
JASS:
if r&lt;=0. or GetUnitState(u,UNIT_STATE_MAX_LIFE)&lt;dam+.405 or (GetWidgetLife(u)+r)-dam&lt;.405 then
    return false
elseif r&gt;dam then
    debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0.,0.,5.,GetUnitName(u)+&quot; is being shielded for more damage then it took.&quot;)
endif


You can put 'debug' before the elseif, so that never happens pointlessly during gameplay.
 

emjlr3

Change can be a good thing
Reaction score
395
BUMP, unless there exists any objections, i will aprove
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top