• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Trigger] How to prevent unit from damaging itself when using "make unit damage area" function?

Status
Not open for further replies.
Level 6
Joined
Sep 5, 2007
Messages
264
Desyncs, is what you call it when a player is forcefully disconnected from the game due to their Wc3 being out of "sync" with the host's Wc3.

Razorbrain gave you a valid solution.
Here's my one:

JASS:
function Filter_AlwaysTrue takes nothing returns boolean
    return true
endfunction

function DistanceBetweenPointsXY takes real x, real y, real targ_x, real targ_y returns real
    local real dx = targ_x - x
    local real dy = targ_y - y
    return SquareRoot(dx * dx + dy * dy)
endfunction

function DamageArea takes unit attacker, real x, real y, real radius, real damage, real self, real friend, attacktype a_type, damagetype d_type returns nothing
    local group ug = CreateGroup()
    local unit u
    local player player_owner = GetOwningPlayer(attacker)

    local real damage_percent
    local real dist

    local real targ_x
    local real targ_y

    call GroupEnumUnitsInRange(ug, x, y, radius, Condition(function Filter_AlwaysTrue))

    loop
        set u = FirstOfGroup(ug)
        exitwhen u == null

        if (attacker == u) then
            if (self > 0) then
                // Reaction to Self
                set targ_x = GetUnitX(u)
                set targ_y = GetUnitY(u)
                set dist = DistanceBetweenPointsXY(x,y,targ_x,targ_y)
                if (dist < 1) then
                    set damage_percent = damage
                else
                    set damage_percent = (dist / radius) * damage
                endif
                call UnitDamageTarget(attacker, u, damage_percent * self, true, true, a_type, d_type, WEAPON_TYPE_WHOKNOWS)
            endif
        elseif (IsUnitEnemy(u, player_owner)) then
        // Reaction to Enemies
            set targ_x = GetUnitX(u)
            set targ_y = GetUnitY(u)
            set dist = DistanceBetweenPointsXY(x,y,targ_x,targ_y)
            if (dist < 1) then
                set damage_percent = damage
            else
                set damage_percent = (dist / radius) * damage
            endif
            call UnitDamageTarget(attacker, u, damage_percent, true, true, a_type, d_type, WEAPON_TYPE_WHOKNOWS)
        elseif (friend > 0) then
        // Reaction to Friendlies
            set targ_x = GetUnitX(u)
            set targ_y = GetUnitY(u)
            set dist = DistanceBetweenPointsXY(x,y,targ_x,targ_y)
            if (dist < 1) then
                set damage_percent = damage
            else
                set damage_percent = (dist / radius) * damage
            endif
            call UnitDamageTarget(attacker, u, damage_percent * friend, true, true, a_type, d_type, WEAPON_TYPE_WHOKNOWS)
        endif

        call GroupRemoveUnit(ug, u)
        set u = null
    endloop

    set player_owner = null

    call DestroyGroup(ug)
    set ug = null
endfunction

This function allows you to define how much damage (factor of 0-1) that friendlies (or yourself) get. It also damages less the further away from the center that each target is... I can create another one that does 100% damage to any targets within the radius, just ask me and I do it.

As far as I can tell, it is 100% leak free... :grin:
Hope this is what you're after. :thumbs_up:
 
Status
Not open for further replies.
Top