[JASS] Increasing the damage according to distance between source and target?

Status
Not open for further replies.
Level 8
Joined
May 12, 2018
Messages
122
I want to amplify the taking damage of Target Unit As it stands far from Spirit Tower.
I learned that the computation speed of the Locations are slow, so I tried to solve this with x/y coordinate values, but I'm not sure about the exact calculation formula.
here's my test code.
JASS:
scope UAScourgeBaseDefensiveA initializer init
    private function Act takes nothing returns nothing
        local unit source = udg_DamageEventSource
        local unit target = udg_DamageEventTarget
        local unit locust
        local real x0 = GetUnitX(source)
        local real y0 = GetUnitY(source)
        local real x1 = GetUnitX(target)
        local real y1 = GetUnitY(target)
        local real angle = Atan2(y1 - y0, x1 - x0)
        local real xOffset = x0 + (x1 - x0) * Cos(angle)
        local real yOffset = y0 + (y1 - y0) * Sin(angle)
        local real dx = xOffset - x0
        local real dy = yOffset - y0
        local real tx = 0
        local real ty = 0
        set tx = x0 + dx
        set ty = y0 + dy
        
        if tx >= 100 and tx < 200 then
            set locust = CreateUnit(Player(0), 'hfoo', x1,y1,1)
        elseif ty >= 100 and ty < 200 then
            set locust = CreateUnit(Player(0), 'hfoo', x1,y1,1)
        elseif tx >= 200 and tx < 300 then
            set locust = CreateUnit(Player(0), 'hrfl', x1,y1,1)
        elseif ty >= 200 and ty < 300 then
            set locust = CreateUnit(Player(0), 'hrfl', x1,y1,1)
        endif

        call KillUnit(locust)
        
        set locust = null
        set source = null
        set target = null
    endfunction
    
    private function Cond takes nothing returns boolean
        return GetUnitTypeId(udg_DamageEventSource) == 'uzg1' and GetPlayerTechCount(GetOwningPlayer(udg_DamageEventSource), T_SCOURGEBASE_DEFENSIVE_A, true) > 0 and not IsUnitIllusion(udg_DamageEventSource)
    endfunction
    
    private function init takes nothing returns nothing
        local trigger t = CreateTrigger()
        
        call TriggerRegisterVariableEvent( t, "udg_PreDamageEvent", LESS_THAN, 0 )
        call TriggerAddCondition( t, Condition( function Cond ) )
        call TriggerAddAction( t, function Act )
        
        set t = null
    endfunction
endscope
I wrote create and kill unit for checking distances but this is not working.
 

Uncle

Warcraft Moderator
Level 68
Joined
Aug 10, 2018
Messages
7,164
I can see issues with your Actions but I would also double check to see if the Actions are even running in the first place.

Then you can use these functions to get the distance and angle between the coordinates:
vJASS:
function GetDistanceBetweenCoords takes real x1, real y1, real x2, real y2 returns real
    local real dx = x2 - x1
    local real dy = y2 - y1
    return SquareRoot(dx * dx + dy * dy)
endfunction

function GetAngleBetweenCoords takes real x1, real y1, real x2, real y2 returns real
    return Atan2(y2 - y1, x2 - x1)
endfunction
Distance will be a single Real value based on the coordinates of the two objects:
vJASS:
   private function Act takes nothing returns nothing
        local unit source = udg_DamageEventSource
        local unit target = udg_DamageEventTarget
        local unit locust
        local real x1 = GetUnitX(source)
        local real y1 = GetUnitY(source)
        local real x2 = GetUnitX(target)
        local real y2 = GetUnitY(target)
        local real angle = GetAngleBetweenCoords(x1, y1, x2, y2)
        local real distance = GetDistanceBetweenCords(x1, y1, x2, y2)
 
        // Create a footman if it's > 600 units away, rifleman at 400-600 units away, knight at < 400 units away
        if distance > 600 then
            set locust = CreateUnit(Player(0), 'hfoo', x2, y2, 270)
        elseif distance >= 400 then
            set locust = CreateUnit(Player(0), 'hrfl', x2, y2, 270)
        else
            set locust = CreateUnit(Player(0), 'hkni', x2, y2, 270)
        endif

        call KillUnit(locust)
 
        set locust = null
        set source = null
        set target = null
    endfunction
 
Last edited:
Status
Not open for further replies.
Top