library GetRallyUnitEx initializer Init
/*
function GetUnitRallyUnitEx takes unit building returns unit
*/
globals
private hashtable hash = InitHashtable()
endglobals
private function AllyFilter takes unit a, unit b returns boolean
return IsUnitType(a, UNIT_TYPE_STRUCTURE) and (IsUnitEnemy(a, GetOwningPlayer(b)) or GetOwningPlayer(b) == Player(PLAYER_NEUTRAL_PASSIVE))
endfunction
private function IsStructureTower takes unit a, unit b returns boolean
return IssueTargetOrder(a, "attack", b) and IssueImmediateOrder(a, "stop")
endfunction
private function OrderFilter takes integer orderId returns boolean
return orderId == OrderId("setrally") or orderId == OrderId("smart")
endfunction
private function onOrder takes nothing returns boolean
local unit building = GetTriggerUnit()
local unit target = GetOrderTargetUnit()
local integer id
if OrderFilter(GetIssuedOrderId()) and not IsStructureTower(building, target) then
set id = GetHandleId(building)
if AllyFilter(building, target) then
// building wants a non-ally as rally unit
if HaveSavedHandle(hash, id, 0) then
call RemoveSavedHandle(hash, id, 0)
endif
call SaveUnitHandle(hash, id, 0, target)
else
// building wants an ally as rally unit so we clean table
if HaveSavedHandle(hash, id, 0) then
call RemoveSavedHandle(hash, id, 0)
endif
endif
endif
set building = null
set target = null
return false
endfunction
function GetUnitRallyUnitEx takes unit building returns unit
local integer id
if GetUnitRallyUnit(building) != null then
return GetUnitRallyUnit(building)
else
set id = GetHandleId(building)
if HaveSavedHandle(hash, id, 0) then
return LoadUnitHandle(hash, id, 0)
endif
endif
return null
endfunction
private function onDeath takes nothing returns boolean
if HaveSavedHandle(hash, GetHandleId(GetTriggerUnit()), 0) then
call RemoveSavedHandle(hash, GetHandleId(GetTriggerUnit()), 0)
endif
return false
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
call TriggerAddCondition(t, Condition(function onOrder))
set t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH )
call TriggerAddCondition(t, Condition(function onDeath))
endfunction
endlibrary