Snippet ShieldUnit

emjlr3

Change can be a good thing
Staff member
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
Staff member
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
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), 'hpea', 0, 0, 0)
        local unit foo = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        local unit angryman = CreateUnit(Player(0), 'Hpal', 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
Staff member
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
Staff member
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
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
Staff member
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
Staff member
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 :)
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
 
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
Staff member
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
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
Staff member
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
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™
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
Staff member
updated - should be...actually good this time!
 

Azlier

Old World Ghost
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
Staff member
BUMP, unless there exists any objections, i will aprove
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    They aren't cutting my responsibilities or interjecting in my management abilities or anything, it's just stupid shit that comes up. And they don't exactly have people lining up for my job, there's not that many people applying for the positions I hire
  • Varine Varine:
    Eh, whatever. Thanks for listening guys
  • jonas jonas:
    Sure :) Let us know how it ends
  • Varine Varine:
    All of these things will end happily, they're just stressful. And I still lack many good friends that I can go to, and the ones I can are preoccupied with similar things. Thus general chit chat, cuz for some reason TH and Ghan and Tom all actively keep it up.
  • Varine Varine:
    Just gotta keep Miss Mazie up through the week until her shock wears off and she realizes that she still has family all around her, and bossman will do whatever he's going to do and I'll respond appropriately when it happens. Thank you all for the support, I do very much appreciate everyone being here for me through the years
    +3
  • vypur85 vypur85:
    Best of luck Varine!
  • vypur85 vypur85:
    I just gotten myself an offer to work in China. The pay quadruples my current one. Damn.... Not really ready to start a new life there in China.
  • The Helper The Helper:
    I have heard that they pay pretty good to English teachers in China - you would be an expat
  • jonas jonas:
    Cool, what kind of job?
  • Accname Accname:
    I would be careful with jobs in China. They can be hit and miss depending on where in China you go. Places like hong kong / Shengzen / Beijing can be neat. Other places not so much.
  • Accname Accname:
    I would recommend searching for some first person experiences for the city you got the offer in. Especially now when the political situation in China is deteriorating.
  • jonas jonas:
    Accname, long time no see
  • jonas jonas:
    What have you been up to
  • tom_mai78101 tom_mai78101:
    Hey Accname, welcome back.
  • Accname Accname:
    Not much. Working in the Renewable Energy Sector as an IT Consultant. Its okay, but I think I preferred working at the university. It was more relaxed and you met all kinds of crazy people there.
  • vypur85 vypur85:
    I gotten a teaching position for Biology in a college in Wuhan (yes, there)... I suppose it should be fine there (I hope). Many of my ex colleagues are teaching in China as well currently (none in Wuhan though)
  • vypur85 vypur85:
    And I signed the contract already. I guess there's no turning back....
  • jonas jonas:
    @Accname how many hours do you work? I heard in some sectors IT consultants rack up insane hours
  • jonas jonas:
    @vypur85 sounds nice, have fun : )
  • Accname Accname:
    I am supposed to work 40 hrs a week, but I can work more if I like and I will be paid for those hours (as long as I don't go too far, there are laws and company policies, etc)
  • Accname Accname:
    In practice its basically work as much as you like, as long as the job gets done in time.
  • jonas jonas:
    Haha, my job is like that as well... that usually means I have a few 70-80 hours weeks a year, and lots of 20 hours weeks...
  • jonas jonas:
    a few weeks ago, one of my friends basically said "jonas, I received an invitation to submit something to conference X but I'm too lazy to do it and also the conference isn't advanced enough for my high level of research*, why don't you write something? Oh by the way, the deadline is in two weeks. Enjoy!" so I got two 80 hour weeks out of that kind offer. (*of course he didn't say those parts, but it's a better story this way)
  • jonas jonas:
    now I'll have next week off to make up for overtime :p and I'll play some good old gothic 2
  • The Helper The Helper:
    Hope you are enjoying that gothic 2~

    Members online

    Affiliates

    Hive Workshop NUON Dome
    Top