• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Unit is attacked - earlier detection needed

Status
Not open for further replies.
Level 19
Joined
Apr 25, 2006
Messages
1,309
So what I would want to do is to give units a miss chance depending in the distance between the attacking and attacked unit. Problem is that if I use event unit is attacked then I cast a miss spell to the attacking unit the miss ability takes effect after the attack has been made though it should affect the attack. For example if unit has 100% miss chance from last spell and then it attacks a unit near him and I would give it 0% miss chance the first attack is a miss. So how can I detect a unit that is attacking a target earlier than unit is attacked event.
 
Level 9
Joined
Feb 14, 2009
Messages
316
I doubt such detection exsists. Alternatively, you can use a trigger to instantly heal the damaged unit (similar to DotA's Faceless Void skill). Thus you will need the Unit is attacked event.

Here are the functions
JASS:
function DelayedDamageTimer takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local unit damagereceiver = GetHandleUnit(t, "Unit")
    call SetUnitState(damagereceiver, UNIT_STATE_LIFE, GetUnitState (damagereceiver, UNIT_STATE_LIFE) + GetHandleReal(t, "Damage"))
endfunction


function DelayedDamage takes unit damagereceiver,real damage returns nothing
    local timer t=CreateTimer()
    call SetHandleReal(t, "Damage", damage)
    call SetHandleHandle(t, "Unit", damagereceiver)
    call TimerStart(t,0.,false,function DelayedDamageTimer)
endfunction


function DelayedDamageCheck takes unit damagereceiver,real damage returns nothing
    local real maxlife=GetUnitState(damagereceiver, UNIT_STATE_MAX_LIFE)
    local real life=GetUnitState(damagereceiver, UNIT_STATE_LIFE)
    if damage > (maxlife - life)then
        if damage >= life then
            call SetUnitState(damagereceiver, UNIT_STATE_LIFE, maxlife)
            call DelayedDamage(damagereceiver, damage - (maxlife - life))
        else
            call DelayedDamage(damagereceiver, damage)
        endif
    else
        call SetUnitState(damagereceiver, UNIT_STATE_LIFE, GetUnitState(damagereceiver, UNIT_STATE_LIFE) + damage)
    endif
endfunction
 
Level 19
Joined
Apr 25, 2006
Messages
1,309
uuh I use only gui but I know how to do this heal thing. one more thing about this. if the attacked unit actually dies due to getting too much damage I guess I need to revive it then instantly? but now there is a new problem. if I use takes damage for the attack detection which one happens first: the unit takes damage event or the unit dies event? well I can check this out too but if unit dies event happens first I guess I will be in some trouble since I have another trigger using unit dies event.
 
Level 9
Joined
Feb 14, 2009
Messages
316
There are several cases:

Unit has 600/1000 hp and takes 300 dmg. In that case it is healed after the damage
Unit has 100/1000 hp and takes 300 dmg. In that case it is healed before the damage
Unit has 1000/1000 hp and takes 300 dmg. In that case it is healed after the damage
Unit has 1000/1000 hp and takes 1500 dmg. In that case it is added 1500 max life and then is damaged. Same as if the unit has 600/1000 hp -> add 1500 to max and current hp and then damage it. Don't forget to remove the additional hp after the damage.

Use this and don't mess up with reviving. The funtions I entered are not complete but at least you will know the mechanism.
 
Status
Not open for further replies.
Top