library OrderQueue initializer init
globals
group oq_queuedUnits = CreateGroup() //to reduce lag, only check for order queues for units in this group
endglobals
struct Order
integer orderId
Order next = -1
integer orderType = 0
//0 = no target
//1 = point target
//2 = widget target
real x //point target
real y //point target
widget w //order target
static method create takes integer orderId, real x, real y, widget target returns Order
local Order o = Order.allocate()
set o.orderId = orderId
if x != 0 or y != 0 then
set o.orderType = 1
elseif target != null then
set o.orderType = 2
endif
set o.x = x
set o.y = y
set o.w = target
return o
endmethod
method Issue takes unit subject returns nothing
if .orderType == 0 then
call IssueImmediateOrderById(subject,.orderId)
elseif .orderType == 1 then
call IssuePointOrderById(subject,.orderId,.x,.y)
else
call IssueTargetOrderById(subject,.orderId,.w)
endif
endmethod
endstruct
struct UnitData
Order first = -1
Order last = -1 //for faster allocation of new orders
static method create takes unit attach returns UnitData
local UnitData dat = UnitData.allocate()
call SetUnitUserData(attach,dat)
return dat
endmethod
method QueueOrder takes integer orderId, real x, real y, widget target returns nothing
local Order o = Order.create(orderId,x,y,target)
if .first == -1 then
set .first = o
else
set .last.next = o
endif
set .last = o
endmethod
method QueueOrderStringNoTarget takes string orderString returns nothing
call .QueueOrder(OrderId(orderString),0,0,null)
endmethod
method QueueOrderStringWidgetTarget takes string orderString, widget target returns nothing
call .QueueOrder(OrderId(orderString),0,0,target)
endmethod
method QueueOrderStringPointTarget takes string orderString, real x, real y returns nothing
call .QueueOrder(OrderId(orderString),x,y,null)
endmethod
method QueueOrderNoTarget takes integer orderId returns nothing
call .QueueOrder(orderId,0,0,null)
endmethod
method QueueOrderWidgetTarget takes integer orderId, widget target returns nothing
call .QueueOrder(orderId,0,0,target)
endmethod
method QueueOrderPointTarget takes integer orderId, real x, real y returns nothing
call .QueueOrder(orderId,x,y,null)
endmethod
endstruct
private function CheckOrders_Child takes nothing returns nothing
local unit u = GetEnumUnit()
local UnitData dat = GetUnitUserData(u)
local Order o
if GetUnitCurrentOrder(u) == 0 then //I think the null order is 0... or is it "stop"?
set o = dat.first
call o.Issue(u)
set dat.first = o.next
call o.destroy()
endif
set u = null
endfunction
private function CheckOrders takes nothing returns nothing
call ForGroup(oq_queuedUnits,function CheckOrders_Child)
endfunction
private function init takes nothing returns nothing
call TimerStart(CreateTimer(),.25,true,function CheckOrders)//change interval as you will; the higher, the less lag, the lower, the more precise.
endfunction
endlibrary