• 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.

How to compare Angles relative to unit facing?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
Unit A is facing X. Unit B casts a spell on unit A facing Y. How do I detect if the spell is coming from the side of unit A?



The purple sides are front/back of unit A. A direct hit is the light-purple horizontal line and the damage should get higher and higher the closer the angle correspond to that line and smaller and smaller the further away (after the orange parameter).
 
Last edited:
Create in map header:
JASS:
function GetRealOpposite takes real r returns real
    return -r
endfunction

function AlwaysPos takes real r returns real
    if r < 0. then
        set r = -r
    endif
    return r
endfunction

JASS:
//locals
local real r1 = Angle X
local real r2 = Angle X + 90
local real r3 = Angle X - 90
local real r4 = Angle Y
local location p1 = Unit A Pos
local location p2 = Unit B Pos
local real r5 = Angle Between p1 and p2

if AlwaysPos(r2 - r5) < 45. then
    set r6 = AlwaysPos(r2 - r5)
    set r7 = r2
endif
if AlwaysPos(r3 - r5) < 45. then
    set r6 = AlwaysPos(r3 - r5)
    set r7 = r3
endif

set damage = damage * (1. + GetRealOpposite((r7 * 0.01)))

//remove leaks

At 45 degrees away from the horizontal purple line this will deal a bonus of 0% damage, increasing to 45% damage when closest to it.
Of course, either you do this calculation before dealing damage, or in a DDS if it's a buff checker/etc. spell.
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
JASS:
function Trig_Ram_Conditions takes nothing returns boolean
    if ( not ( GetSpellAbilityId() == 'Aslo' ) ) then
        return false
    endif
    return true
endfunction

function Trig_Ram_Actions takes nothing returns nothing
    local unit x = GetSpellTargetUnit()
    local unit y = GetTriggerUnit()
    local real r1 = GetUnitFacing(x)
    local real r2 = r1 + 90
    local real r3 = r1 - 90
    local real r4 = GetUnitFacing(y)
    local location p1 = GetUnitLoc(x)
    local location p1 = GetUnitLoc(y)
    local real r5 = AngleBetweenPoints(p1, p2)
    local real r6
    local real r7
    local real damage

    if AlwaysPos(r2 - r5) < 45. then
        set r6 = AlwaysPos(r3 - r5)
        set r7 = r3
    endif


    if AlwaysPos(r3 - r5) < 45. then
        set r6 = AlwaysPos(r3 - r5)
        set r7 = r3
    endif

    set damage = damage * (1. + GetRealOpposite((r7 * 0.01)))
    call DisplayTextToForce( GetPlayersAll(), R2S(damage) )

endfunction

//===========================================================================
function InitTrig_Ram takes nothing returns nothing
    set local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( t, Condition( function Trig_Ram_Conditions ) )
    call TriggerAddAction( t, function Trig_Ram_Actions )
    set t = null
endfunction
I suck at this XD
 
JASS:
function Trig_Ram_Actions takes nothing returns boolean
    local unit x = GetSpellTargetUnit()
    local unit y = GetTriggerUnit()
    local real r1 = GetUnitFacing(x)
    local real r2 = r1 + 90
    local real r3 = r1 - 90
    local real r4 = GetUnitFacing(y)
    local location p1 = GetUnitLoc(x)
    local location p1 = GetUnitLoc(y)
    local real r5 = AngleBetweenPoints(p1, p2)
    local real r6
    local real r7
    local real damage
    local integer b = GetUnitAbilityLevel(GetTriggerUnit(), GetSpellAbilityId())
    if GetSpellAbilityId() == 'Aslo' then
        if AlwaysPos(r2 - r5) < 45. then
            set r6 = AlwaysPos(r3 - r5)
            set r7 = r3
        endif
        if AlwaysPos(r3 - r5) < 45. then
            set r6 = AlwaysPos(r3 - r5)
            set r7 = r3
        endif
        //set damage = some amount
        set damage = (40. * b)
        //this line below only increases damage
        set damage = damage * (1. + GetRealOpposite((r7 * 0.01)))
        call DisplayTextToForce( GetPlayersAll(), R2S(damage) )
        call RemoveLocation(p1)
        call RemoveLocation(p2)
    endif
    return false
endfunction

//===========================================================================
function InitTrig_Ram takes nothing returns nothing
    set local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( t, Condition( function Trig_Ram_Actions ) )
    set t = null
endfunction

Always use AddCondition over AddAction as it does not create a new thread. Remember to return false at the end of the func, and 'returns boolean'.

Also do not create a separate condition function since it can be done in If/Then/Else. Doing otherwise nullifies the first sentence^.
 
Status
Not open for further replies.
Top