package GuardDevilUnbreakable import DamageType import StructuredDD import ClosureTimers import Players import Invulnerable

constant int ID_GUARD_DEVIL         = 'h03B'
constant int ID_GUARD_DEVIL_GREATER = 'h03C'
constant int ID_ABILITY_UNBREAKABLE = 'A01W'
constant int ID_ABILITY_UNBREAKA_CD = 'A01X'
constant int ID_INVULNERABLE        = 'Avul'

constant real RADIUS   = 600.
constant real DURATION =   1.
constant real COOLDOWN =  60.

constant string SHIELD_FX = "Abilities\\Spells\\Human\\DivineShield\\DivineShieldTarget.mdl"

group grp = CreateGroup()

function isGuardDevil(unit u) returns bool
    return u.getTypeId() == ID_GUARD_DEVIL or u.getTypeId() == ID_GUARD_DEVIL_GREATER
end

function findUnbreakable(unit u) returns unit
    unit guard = null
    unit iter

    grp.enumUnitsInRange(vec2(u.getX(), u.getY()), RADIUS)
    loop
        iter = FirstOfGroup(grp)
        exitwhen iter == null

        if iter.isAlive() and isGuardDevil(iter) and iter.hasAbility(ID_ABILITY_UNBREAKABLE)
            guard = iter
        end

        grp.removeUnit(iter)
    end

    return guard
end

function h()
    unit tU     = GetTriggerUnit()
    real damage = GetEventDamage()

    if tU.getOwner().getId() < Players.COUNT and DamageType.get() == DamageType.ATTACK and (tU.getHP() - damage < 1.) and not tU.hasAbility(ID_INVULNERABLE)
        unit unbreakableSource = findUnbreakable(tU)

        if unbreakableSource != null
            Invulnerable.apply(tU, DURATION)
            unbreakableSource.removeAbility(ID_ABILITY_UNBREAKABLE)
            unbreakableSource.addAbility(   ID_ABILITY_UNBREAKA_CD)
            effect shield = tU.addEffect(SHIELD_FX, "origin")

            doAfter(DURATION, () -> begin
                shield.destr()
            end)

            doAfter(COOLDOWN, () -> begin
                unbreakableSource.addAbility(   ID_ABILITY_UNBREAKABLE)
                unbreakableSource.removeAbility(ID_ABILITY_UNBREAKA_CD)
            end)
        end
    end
end

init
    StructuredDD.addHandler(function h)
end

goto line:
Compare with:
text copy window edit this code post new code