scope Backstab initalizer InitBackstab
globals
constant integer NPLAYERS = 4 //Number of players in game
constant player array INDEXEDPLAYERS //Make this array of all players who are currently in-game
constant integer SPELLID = 'A001' //Important with single quotes since they mark the integer conversion
constant real PRECISION = 30.0 //The degrees amount of precision
endglobals
//Static function because null boolexpr leaks
function returnNull takes nothing returns boolexpr
return null
endfunction
//Credit: [url]http://www.wc3c.net/showthread.php?p=1103463[/url]
function IsPointInCone takes real x1, real y1, real x2, real y2, real facingAngle, real angle returns boolean
return Cos(Atan2(y2-y1,x2-x1)-facingAngle) > Cos(angle)
endfunction
function BackstabActions takes nothing returns nothing
if BackstabConditions() then
call BjDebugMsg("Backstab has been succesfully casted!")
else
call BjDebugMsg("You must be behind your enemy when trying to backstab someone!")
endif
endfunction
function BackstabConditions takes nothing returns boolean
local unit caster = GetTriggerUnit()
local unit target = GetSpellTargetUnit()
local real casterFacing = GetUnitFacing(caster)
local real targetFacing = GetUnitFacing(target)
local real difFacing = RAbsBj(casterFacing-targetFacing)
local real x1 = GetWidgetX(caster)
local real y1 = GetWidgetY(caster)
local real x2 = GetWidgetX(target)
local real y2 = GetWidgetY(target)
local boolean castBackstab = false
//IsPointInCone checks if the caster is behind the target
//difFacing is for allowing attacks that are near 180 degrees
if IsPointInCone(x1, y1, x2, y2, casterFacing, PRECISION) and difFacing < PRECISION then
set castBackstab = true
endif
set caster = null
set target = null
return castBackstab
endfunction
function IsAbilityBackstab takes nothing returns boolean
if GetSpellAbilityId() == SPELLID then
return true
endif
return false
endfunction
function InitBackstab takes nothing returns nothing
local trigger backstabTrg = CreateTrigger()
local integer i = 0
//Loop through indexed players and add the event that a unit starts the effect of an ability
for i = 0 to NPLAYERS
call TriggerRegisterPlayerUnitEvent(backstabTrg, INDEXEDPLAYERS(i), EVENT_PLAYER_UNIT_SPELL_EFFECT, returnNull())
endfor
//Check the condition that the ability is backstab
call TriggerAddCondition(backstabTrg, Condition(function IsAbilityBackstab))
//Do the actions
call TriggerAddAction(backstabTrg, function BackstabActions)
endfunction
endscope