Is there any way of increasing the performance of this trigger?:
JASS:
function IsInRect takes real x1, real y1,real x2, real y2,real x3, real y3,real x4, real y4,real px, real py returns boolean
local real d1 = SquareRoot((x1-px) * (x1-px) + (y1-py) * (y1-py))
local real d2 = SquareRoot((x2-px) * (x2-px) + (y2-py) * (y2-py))
local real d3 = SquareRoot((x3-px) * (x3-px) + (y3-py) * (y3-py))
local real d4 = SquareRoot((x4-px) * (x4-px) + (y4-py) * (y4-py))
local real s1 = SquareRoot((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2))
local real s2 = SquareRoot((x2-x3) * (x2-x3) + (y2-y3) * (y2-y3))
local real s3 = SquareRoot((x3-x4) * (x3-x4) + (y3-y4) * (y3-y4))
local real s4 = SquareRoot((x4-x1) * (x4-x1) + (y4-y1) * (y4-y1))
local real cos1 = (d1*d1+d2*d2-s1*s1)/ (2 * (d1*d2))
local real cos2 = (d2*d2+d3*d3-s2*s2)/ (2 * (d2*d3))
local real cos3 = (d3*d3+d4*d4-s3*s3)/ (2 * (d3*d4))
local real cos4 = (d4*d4+d1*d1-s4*s4)/ (2 * (d4*d1))
local real output = cos1+cos2+cos3+cos4
return output < 0
endfunction
function checkIfUnitsOnBridge takes nothing returns nothing
local group g = CreateGroup()
local unit u
local unit cliffDummy = gg_unit_h000_0012
local location cliffDummyLoc = GetUnitLoc(cliffDummy)
local real uZ
local real uX
local real uY
local real cliffZ = GetLocationZ(cliffDummyLoc)
local real heightDifference
call RemoveLocation(cliffDummyLoc)
set cliffDummyLoc = null
set cliffDummy = null
call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, null)
loop
set u = FirstOfGroup(g)
exitwhen u == null
if IsInRect(udg_CornerX1Rect[2], udg_CornerY1Rect[2], udg_CornerX2Rect[2], udg_CornerY2Rect[2], udg_CornerX3Rect[2], udg_CornerY3Rect[2], udg_CornerX4Rect[2], udg_CornerY4Rect[2], GetUnitX(u), GetUnitY(u)) or IsInRect(udg_CornerX1Rect[3], udg_CornerY1Rect[3], udg_CornerX2Rect[3], udg_CornerY2Rect[3], udg_CornerX3Rect[3], udg_CornerY3Rect[3], udg_CornerX4Rect[3], udg_CornerY4Rect[3], GetUnitX(u), GetUnitY(u)) then
if GetUnitUserData(u) == null then
set udg_unit_user_data = udg_unit_user_data + 1
call SetUnitUserData(u, udg_unit_user_data)
endif
if GetUnitUserData(u) != null and (udg_is_on_bridge_unit[GetUnitUserData(u)] == 2) then
set udg_is_on_bridge_unit[GetUnitUserData(u)] = 1
call UnitAddAbility(u, 'Amrf')
call SetUnitFlyHeight(u, 0.00, 0.00)
call UnitRemoveAbility(u, 'Amrf')
endif
if GetUnitUserData(u) != null and (udg_is_on_bridge_unit[GetUnitUserData(u)] == 0) then
set udg_is_on_bridge_unit[GetUnitUserData(u)] = 1
endif
endif
if IsInRect(udg_CornerX1Rect[1], udg_CornerY1Rect[1], udg_CornerX2Rect[1], udg_CornerY2Rect[1], udg_CornerX3Rect[1], udg_CornerY3Rect[1], udg_CornerX4Rect[1], udg_CornerY4Rect[1], GetUnitX(u), GetUnitY(u)) and udg_is_on_bridge_unit[GetUnitUserData(u)] == 1 or udg_is_on_bridge_unit[GetUnitUserData(u)] == 2 then
set udg_is_on_bridge_unit[GetUnitUserData(u)] = 2
call UnitAddAbility(u, 'Amrf')
set udg_uLoc[GetUnitUserData(u)] = GetUnitLoc(u)
set uZ = GetLocationZ(udg_uLoc[GetUnitUserData(u)])
set heightDifference = cliffZ-uZ+40
call SetUnitFlyHeight(u, heightDifference, 0.00)
call UnitRemoveAbility(u, 'Amrf')
endif
if (not(IsInRect(udg_CornerX1Rect[1], udg_CornerY1Rect[1], udg_CornerX2Rect[1], udg_CornerY2Rect[1], udg_CornerX3Rect[1], udg_CornerY3Rect[1], udg_CornerX4Rect[1], udg_CornerY4Rect[1], GetUnitX(u), GetUnitY(u)))) and udg_is_on_bridge_unit[GetUnitUserData(u)] == 2 then
set uX = GetLocationX(udg_uLoc[GetUnitUserData(u)])
set uY = GetLocationY(udg_uLoc[GetUnitUserData(u)])
call SetUnitX(u, uX)
call SetUnitY(u, uY)
endif
if (not(IsInRect(udg_CornerX1Rect[1], udg_CornerY1Rect[1], udg_CornerX2Rect[1], udg_CornerY2Rect[1], udg_CornerX3Rect[1], udg_CornerY3Rect[1], udg_CornerX4Rect[1], udg_CornerY4Rect[1], GetUnitX(u), GetUnitY(u)))) and (not(IsInRect(udg_CornerX1Rect[2], udg_CornerY1Rect[2], udg_CornerX2Rect[2], udg_CornerY2Rect[2], udg_CornerX3Rect[2], udg_CornerY3Rect[2], udg_CornerX4Rect[2], udg_CornerY4Rect[2], GetUnitX(u), GetUnitY(u)))) and (not(IsInRect(udg_CornerX1Rect[3], udg_CornerY1Rect[3], udg_CornerX2Rect[3], udg_CornerY2Rect[3], udg_CornerX3Rect[3], udg_CornerY3Rect[3], udg_CornerX4Rect[3], udg_CornerY4Rect[3], GetUnitX(u), GetUnitY(u)))) and udg_is_on_bridge_unit[GetUnitUserData(u)] == 1 then
call RemoveLocation(udg_uLoc[GetUnitUserData(u)])
set udg_uLoc[GetUnitUserData(u)] = null
set udg_is_on_bridge_unit[GetUnitUserData(u)] = 0
endif
call GroupRemoveUnit(g, u)
endloop
set g = null
endfunction
function InitTrig_Bridges takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEvent( t, 0.03, true )
call TriggerAddAction( t, function checkIfUnitsOnBridge )
set t = null
endfunction
Last edited: