library TS initializer init uses DamageEvent, TimerUtils, Table
globals
private Table chk
private TableArray tb
private TableArray atk
private integer DATA
private integer Counter = 0
endglobals
private function UnitAlive takes unit u returns boolean
return not IsUnitType(u, UNIT_TYPE_DEAD)
endfunction
private function Grp takes nothing returns nothing
local unit u = GetEnumUnit()
if chk.has(GetHandleId(u)) then
set Counter = Counter + 1
endif
set u = null
endfunction
private function GrpRemove takes nothing returns nothing
call chk.remove(GetHandleId(GetEnumUnit()))
endfunction
private function Dam takes nothing returns nothing
local integer id = GetHandleId(source)
local TS this = tb[1][id] //refering to this in TS struct
if chk.has(id) then
set Counter = 0
call ForGroup(this.g, function Grp)
if Counter==this.index then
call GroupClear(this.grpCheck)
call ForGroup(this.g, function GrpRemove)
endif
call IssuePointOrder(source, "move", tb[2].real[id], tb[3].real[id] )
set this.checker = true
endif
endfunction
private function init takes nothing returns nothing
set chk = Table.create()
set tb = TableArray[0x3000]
set atk = TableArray[0x3000]
call AddDamageHandler(function Dam)
endfunction
struct TS
unit source
integer counter
integer index
boolean checker
group g
group grpCheck
private static method looper takes nothing returns nothing
local timer t = GetExpiredTimer()
local thistype this = GetTimerData(t)
local unit u
local integer id
if UnitAlive(.source) then
if .checker then
set .counter = 0
loop
set .counter = .counter + 1
set u = atk[.counter].unit[this]
if UnitAlive(u) then
set id = GetHandleId(u)
if not chk.has(id) and not IsUnitInGroup(u, .grpCheck) then
set .checker = false
set chk[id] = 0
call GroupAddUnit(.grpCheck, u)
call BJDebugMsg(I2S(.counter)+" turn is "+GetUnitName(u)+" attacks "+GetUnitName(.source))
//Issue attack or spell or something as long as it deals damage
call IssueTargetOrder(u, "attack", .source)
exitwhen true
endif
set u = null
endif
exitwhen .counter==.index
endloop
endif
else
call DestroyGroup(.g)
call DestroyGroup(.grpCheck)
set .g = null
set .grpCheck = null
set .source = null
call .destroy()
call ReleaseTimer(t)
endif
set t = null
endmethod
static method register takes unit u returns thistype
local thistype this = allocate()
set .source = u
set .checker = true
set .counter = 0
set .index = 0
set .g = CreateGroup()
set .grpCheck = CreateGroup()
call TimerStart(NewTimerEx(this), 1.0, true, function thistype.looper)
return this
endmethod
method addAttacker takes unit u returns nothing
local integer id = GetHandleId(u)
set .index = .index + 1
set atk[.index].unit[this] = u
set tb[0].unit[id] = .source
set tb[1][id] = this
set tb[2].real[id] = GetUnitX(u)
set tb[3].real[id] = GetUnitY(u)
set tb[4].real[id] = GetUnitFacing(u)
call GroupAddUnit(.g, u)
endmethod
endstruct
endlibrary