library ArhowksNetwork initializer init requires Table, Ascii, GetDummy //everything is little endian
globals
private integer A0 = 'A#*0'
private integer A1 = 'A#*1'
private integer A2 = 'A#*2'
private integer A3 = 'A#*3'
private integer AF = 'A#*/'
private integer A4 = 'A#*4'
private integer array ABILS
private string O0 = "A"
private string O1 = "B"
private string O2 = "C"
private string O3 = "D"
private string OF = "T"
private string O4 = "E"
private string array ORDERS
private integer B_ALL = 'A%%7'
private string B_AK = "W"
private string B_LV = "E"
private string B_WXYZ = "R"
private string B_NUM = "Q"
private string BIG_AK = "T"
private string BIG_LV = "Y"
private string BIG_WXYZ = "U"
private string array oCache
private string array bCache
// private integer cacheIndex = 0
private boolean Syncing = false
private unit dummyUnit
private Table timerData
endglobals
function ClearUnit takes unit u returns nothing
local location l = GetUnitLoc(u)
call SetUnitPositionLoc(u, l)
call RemoveLocation(l)
set l = null
endfunction
struct BinaryDecode extends array
implement Alloc
integer val
public method isFinished takes nothing returns boolean
return val == 0
endmethod
public method next takes nothing returns integer
local integer one = ModuloInteger(val, 4)
set val = val / 4
return one
endmethod
public static method create takes integer input returns thistype
local thistype this = thistype.allocate()
set this.val = input
return this
endmethod
endstruct
struct BinaryStream
integer val
integer res
integer bakedRes
integer count
static integer DEFAULT_RES = 3
public method add takes integer i returns nothing
set val = val + (i * R2I(Pow(bakedRes, count)))
set count = count + 1
endmethod
public method get takes nothing returns integer
return val
endmethod
public method flush takes nothing returns nothing
set val = 0
set count = 0
endmethod
public method destroy takes nothing returns nothing
call deallocate()
endmethod
public static method create takes integer res returns thistype
local thistype this = thistype.allocate()
set val = 0
set this.res = res
if res == 2 then
set bakedRes = 4
else
set bakedRes = R2I(Pow(2, res))
endif
set count = 0
return this
endmethod
endstruct
struct UnitData extends array
implement Alloc
integer typee
integer value
public method recieve takes integer abilId returns nothing
if typee == 0 then
// call FMsg("iqu")
call IntegerPacket(value).recieve(abilId)
elseif typee == 1 then
call IntegerQueuePacket(value).recieve(abilId)
else
call StringQueuePacket(value).recieve(abilId)
endif
endmethod
public method destroy takes nothing returns nothing
call deallocate()
endmethod
public static method create takes integer typ, integer thisv returns thistype
local thistype this = thistype.allocate()
set this.typee = typ
set this.value = thisv
return this
endmethod
endstruct
struct IntegerPacket extends array
implement Alloc
integer val
BinaryStream incoming
UnitData unitData
boolean fin
integer value
player p
unit u
method getValue takes nothing returns integer
return value
endmethod
method destroy takes nothing returns nothing
if not fin then
call Network_RemoveDummy(u)
call unitData.destroy()
set value = incoming.get()
call incoming.destroy()
endif
call deallocate()
endmethod
method wait takes boolean b returns boolean
if b then
call TriggerSyncStart()
call TriggerSyncReady()
endif
return fin
endmethod
method recieve takes integer i returns nothing
local integer l = 0
if i == AF then
call Network_RemoveDummy(u)
call unitData.destroy()
set value = incoming.get()
call incoming.destroy()
set fin = true
else
set l = 0
loop
if ABILS[l] == i then
call incoming.add(l)
exitwhen true
endif
set l = l + 1
exitwhen l == 16
endloop
endif
endmethod
static thistype tem
static trigger broadc = null
private static method broad2 takes nothing returns nothing
local integer i
local thistype this = tem
local BinaryDecode stream = BinaryDecode.create(val)
set u= Network_GetDummy(p)
call UnitAddAbility(u, A0)
call UnitAddAbility(u, A1)
call UnitAddAbility(u, A2)
call UnitAddAbility(u, A3)
call UnitAddAbility(u, AF)
call SetUnitUserData(u, unitData)
if GetLocalPlayer() == p then
call ClearSelection( )
call SelectUnit( u, true)
endif
// call TriggerSyncStart()
// call TriggerSyncReady()
if not Syncing then
set Syncing = true
call SyncSelections()
set Syncing = false
else
call TriggerSyncStart()
call TriggerSyncReady()
endif
if GetLocalPlayer() == p then
if stream.isFinished() then
call ForceUIKey(ORDERS[0])
endif
loop
exitwhen stream.isFinished()
call ForceUIKey(ORDERS[stream.next()])
endloop
call ForceUIKey(OF)
endif
call stream.deallocate()
endmethod
public method broadcast takes nothing returns nothing
set tem = this
call TriggerExecute(broadc)
endmethod
public static method create takes integer value, player playerFor returns thistype
local thistype this = thistype.allocate()
set val = value
set incoming = BinaryStream.create(0)
set unitData = UnitData.create(0, this)
set p = playerFor
set u = null
if broadc == null then
set broadc = CreateTrigger()
call TriggerAddAction(broadc, function thistype.broad2)
endif
set fin = false
return this
endmethod
endstruct
struct IntegerQueuePacket extends array
implement Alloc
BinaryStream incoming
UnitData unitData
Table incomingQueue
integer incomingCount
integer incomingQueuePos
integer popPos
boolean recievingCount
Table Queue
integer count
boolean fin
player p
unit u
method pop takes nothing returns integer
set popPos = popPos + 1
return incomingQueue[popPos-1]
endmethod
method getCount takes nothing returns integer
return incomingCount
endmethod
method outOfData takes nothing returns boolean
return popPos == incomingCount
endmethod
method add takes integer value returns nothing
set Queue[count] = value
set count = count + 1
endmethod
method destroy takes nothing returns nothing
call Queue.destroy()
call incoming.destroy()
call unitData.destroy()
set p = null
call incomingQueue.destroy()
if not fin then
call Network_RemoveDummy(u)
endif
call deallocate()
endmethod
method wait takes boolean b returns boolean
if b then
call TriggerSyncStart()
call TriggerSyncReady()
endif
return fin
endmethod
method recieve takes integer i returns nothing
local integer l = 0
if fin then
return
endif
// call BJDebugMsg("RECIEVED")
// call BJDebugMsg("RECIEVED ORDER : " + I2S(i))
if i == AF then
if recievingCount then
// call BJDebugMsg("RECIEVD CCUNT")
set incomingCount = incoming.get()
set recievingCount = false
if incomingCount == 0 then
call Network_RemoveDummy(u)
set fin = true
endif
else
//if incomingQueuePos = 100 then
// call BJDebugMsg("count : " + I2S(incomingCount))
//endif
set incomingQueue[incomingQueuePos] = incoming.get()
set incomingQueuePos = incomingQueuePos + 1
endif
call incoming.flush()
if incomingQueuePos == incomingCount then
call Network_RemoveDummy(u)
set fin = true
endif
else
set l = 0
loop
if ABILS[l] == i then
call incoming.add(l)
exitwhen true
endif
set l = l + 1
exitwhen l == 16
endloop
endif
call ClearUnit(u)
endmethod
static integer tr_i
static trigger t = null
private method broadcastint takes integer i returns nothing
local BinaryDecode stream = BinaryDecode.create(i)
local integer next
if GetLocalPlayer() == p then
if stream.isFinished() then
// call BJDebugMsg("FINIHSED ORDER : " + ORDERS[0])
call ForceUIKey(ORDERS[0])
else
loop
exitwhen stream.isFinished()
set next = stream.next()
// call BJDebugMsg("NEXT ORDER : " + ORDERS[next])
call ForceUIKey(ORDERS[next])
endloop
endif
call ForceUIKey( OF )
endif
call stream.deallocate()
endmethod
public method broadcast takes nothing returns nothing
local integer i
set recievingCount = true
set u = Network_GetDummy(p)
call UnitAddAbility(u, B_ALL)
//call PauseUnit(u, true)
//call PauseUnit(u, false)
call SetUnitUserData(u, unitData)
call TriggerSyncStart()
call TriggerSyncReady()
if GetLocalPlayer() == p then
call ForceUICancel()
endif
if GetLocalPlayer() == p then
call ClearSelection()
call SelectUnit(u, true)
endif
// call BJDebugMsg("1")
if not Syncing then
set Syncing = true
call SyncSelections()
set Syncing = false
call TriggerSyncStart()
call TriggerSyncReady()
else
call TriggerSyncStart()
call TriggerSyncReady()
endif
if GetLocalPlayer() == p then
call ForceUICancel()
endif
call UnitRemoveAbility(u, B_ALL)
call UnitAddAbility(u, A0)
call UnitAddAbility(u, A1)
call UnitAddAbility(u, A2)
call UnitAddAbility(u, A3)
call UnitAddAbility(u, AF)
call TriggerSyncStart()
call TriggerSyncReady()
call broadcastint(count)
set i = 0
loop
exitwhen i == count
// call BJDebugMsg("BROADCASTING INT : " + I2S(Queue[i]))
call broadcastint(Queue[i])
set i = i + 1
endloop
// call BJDebugMsg("3")
endmethod
public static method create takes player playerFor returns thistype
local thistype this = thistype.allocate()
set incoming = BinaryStream.create(2)
set p = playerFor
set u = null
set Queue = Table.create()
set popPos = 0
set incomingCount = 0
set incomingQueuePos = 0
set incomingQueue = Table.create()
set count = 0
set unitData = UnitData.create(1, this)
set fin = false
return this
endmethod
endstruct
struct StringQueuePacket extends array
implement Alloc
player player
unit u
UnitData unitData
Table stringData
Table bTable
Table oTable
string packet
integer sDataIndex
integer sCacheIndex
integer packetIndex
integer cacheIndex
public method operator [] takes integer i returns string
return stringData.string[i]
endmethod
public method wait takes boolean pause returns boolean
if pause then
call TriggerSyncStart()
call TriggerSyncReady()
endif
if packetIndex >= sCacheIndex and u != null then
call RemoveUnit(u)
set u = null
endif
return packetIndex >= sCacheIndex
endmethod
public method recieve takes integer i returns nothing
local string name = GetObjectName(i)
// call BJDebugMsg("GOT")
if name != "invalid" then
// call BJDebugMsg("not invalid")
if name == "/" then
// call BJDebugMsg("PACKET : " + packet)
// call BJDebugMsg(
set stringData.string[sDataIndex] = packet
set sDataIndex = sDataIndex + 1
set packet = ""
else
// call BJDebugMsg("PACKET : " + packet)
if name == "space" then
set packet = packet + " "
else
set packet = packet + name
endif
endif
endif
set packetIndex = packetIndex + 1
if packetIndex > sCacheIndex then
call Network_RemoveDummy(u)
set u = null
endif
endmethod
public method broadcast takes nothing returns nothing
local integer i = 0
set sCacheIndex = cacheIndex
set packet = ""
set u= Network_GetDummy(player)
call UnitRemoveAbility(u, A0)
call UnitRemoveAbility(u, A1)
call UnitRemoveAbility(u, A2)
call UnitRemoveAbility(u, A3)
call UnitRemoveAbility(u, AF)
call UnitAddAbility(u,B_ALL)
call SetUnitLifeBJ(u, 1)
call SetUnitUserData(u, unitData)
call TriggerSyncStart()
call TriggerSyncReady()
if GetLocalPlayer() == player then
call ClearSelection( )
call SelectUnit( u, true)
endif
if not Syncing then
set Syncing = true
call SyncSelections()
set Syncing = false
call TriggerSyncStart()
call TriggerSyncReady()
else
call TriggerSyncStart()
call TriggerSyncReady()
endif
call ForceUIKey("Q")
// call BJDebugMsg("cacheINdex : " + I2S(cacheIndex))
// call BJDebugMsg("broadcast broadcast broadcast")
loop
exitwhen i == cacheIndex
if GetRandomInt(0,10) == 1 then
call PrintTextConditional(GetLocalPlayer() == player, bCache[i])
endif
if GetLocalPlayer() == player then
// call BJDebugMsg("book cache : " + bCache[i])
call ForceUIKey(bTable.string[i])
call ForceUIKey(oTable.string[i])
call ForceUICancel()
endif
set i = i + 1
endloop
endmethod
public method add takes string orderKey returns nothing
local integer asc = S2A(orderKey)
if asc >= 'a' and asc <= 'k' then
set bTable.string[cacheIndex] = B_AK
set oTable.string[cacheIndex] = orderKey
elseif asc >= 'l' and asc <= 'v' then
set bTable.string[cacheIndex] = B_LV
set oTable.string[cacheIndex] = orderKey
elseif asc >= 'w' and asc <= 'z' then
set bTable.string[cacheIndex] = B_WXYZ
set oTable.string[cacheIndex] = orderKey
elseif asc == '/' then
set bTable.string[cacheIndex] = B_WXYZ
set oTable.string[cacheIndex] = "T"
elseif asc == ' ' then
set bTable.string[cacheIndex] = B_WXYZ
set oTable.string[cacheIndex] = "B"
elseif asc >= '0' and asc <= '9' then
set oTable.string[cacheIndex] = Ascii2Char(asc + 17)
set bTable.string[cacheIndex] = B_NUM
elseif asc >= 'A' and asc <= 'K' then
set bTable.string[cacheIndex] = BIG_AK
set oTable.string[cacheIndex] = orderKey
elseif asc >= 'L' and asc <= 'V' then
set bTable.string[cacheIndex] = BIG_LV
set oTable.string[cacheIndex] = orderKey
elseif asc >= 'W' and asc <= 'Z' then
set bTable.string[cacheIndex] = BIG_WXYZ
set oTable.string[cacheIndex] = orderKey
else
set oTable.string[cacheIndex] = "A"
set bTable.string[cacheIndex] = B_WXYZ
endif
set cacheIndex = cacheIndex + 1
endmethod
public method destroy takes nothing returns nothing
set cacheIndex = 0
call unitData.destroy()
call stringData.destroy()
call oTable.destroy()
call bTable.destroy()
// if packetIndex >= sCacheIndex then
call Network_RemoveDummy(u)
set u = null
//endif
call deallocate()
endmethod
public static method create takes player p returns thistype
local thistype this = thistype.allocate()
set player = p
set unitData = UnitData.create(2, this)
set stringData = Table.create()
set oTable = Table.create()
set bTable = Table.create()
set cacheIndex = 0
return this
endmethod
endstruct
struct StringPacket extends array
implement Alloc
player p
Table data
integer length
boolean hasRecievedLength
boolean hasRecievedData
IntegerQueuePacket lengthPacket
StringQueuePacket dataPacket
integer dataIndex
public method pop takes nothing returns string
set dataIndex = dataIndex + 1
return dataPacket[dataIndex - 1]
endmethod
public method operator [] takes integer i returns string
return dataPacket[i]
endmethod
private method broadcastData takes nothing returns nothing
local integer i = length
local integer ib = 0
local string bc
local string add
local boolean b = GetLocalPlayer() == p
local integer len
set dataPacket = StringQueuePacket.create(p)
loop
exitwhen ib == dataIndex
set bc = data.string[ib]
set i = 0
set len = lengthPacket.pop()
loop
exitwhen len == 0
set len = len - 1
if b then
set add = SubString(bc, 0, 1)
set bc = SubString(bc, 1, StringLength(bc))
else
set add = ""
endif
call dataPacket.add(add)
endloop
call dataPacket.add("/")
set ib = ib + 1
endloop
//call dataPacket.add("/")
call lengthPacket.destroy()
call dataPacket.broadcast()
endmethod
private method compileData takes nothing returns nothing
call data.flush()
set dataIndex = 0
/*local integer i = length
local integer da
local boolean b = false
call data.flush()
set dataIndex = 0
loop
set da = dataPacket.pop()
exitwhen da == ' #^Q' or (b and da == 0)
set b = da == 0
if da == ' #^Z' then
set dataIndex = dataIndex + 1
else
set data.string[dataIndex] = data.string[dataIndex] + A2S(da)
endif
endloop
set dataIndex = 0
call dataPacket.destroy()*/
endmethod
public method wait takes boolean pause returns boolean
if not hasRecievedLength then
if lengthPacket.wait(false) then
// call BJDebugMsg("length packet")
call TriggerSyncStart()
call TriggerSyncReady()
set hasRecievedLength = true
call broadcastData()
set dataIndex = 0
endif
elseif not hasRecievedData then
// call BJDebugMsg("wating data")
if dataPacket.wait(false) then
set hasRecievedData = true
call compileData()
endif
endif
if pause then
call TriggerSyncStart()
call TriggerSyncReady()
endif
return hasRecievedData
endmethod
integer compDataLength
public method broadcast takes nothing returns nothing
call lengthPacket.broadcast()
endmethod
public method add takes string s returns nothing
call lengthPacket.add(StringLength(s))
set data.string[dataIndex] = s
set dataIndex = dataIndex + 1
endmethod
public method destroy takes nothing returns nothing
call data.destroy()
call lengthPacket.destroy()
call dataPacket.destroy()
call deallocate()
endmethod
public static method create takes player pFor returns thistype
local thistype this = thistype.allocate()
set compDataLength = 0
set p = pFor
set data = Table.create()
set lengthPacket = IntegerQueuePacket.create(pFor)
return this
endmethod
endstruct
private function onAbil takes nothing returns nothing
// call BJDebugMsg("RECIEVED ORDER : " + I2S(GetSpellAbilityId()))
if GetUnitTypeId(GetTriggerUnit()) == 'h000' or GetUnitTypeId(GetTriggerUnit()) == Network_GetDummyId() then
call UnitData(GetUnitUserData(GetTriggerUnit())).recieve(GetSpellAbilityId())
endif
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_CAST )
call TriggerAddAction( t, function onAbil )
set t = null
set dummyUnit = Network_GetDummy(Player(0))
call UnitAddAbility(dummyUnit, B_ALL)
call Network_RemoveDummy(dummyUnit)
// call IntegerPacket.create(15,Player(0)).broadcast()
set ORDERS[0] = O0
set ORDERS[1] = O1
set ORDERS[2] = O2
set ORDERS[3] = O3
set ABILS[0] = A0
set ABILS[1] = A1
set ABILS[2] = A2
set ABILS[3] = A3
set timerData = Table.create()
endfunction
endlibrary
//old StringPacket, binary
/*struct StringPacket extends array
implement Alloc
player p
Table data
integer length
boolean hasRecievedLength
boolean hasRecievedData
IntegerQueuePacket lengthPacket
IntegerQueuePacket dataPacket
integer dataIndex
public method pop takes nothing returns string
set dataIndex = dataIndex + 1
return data.string[dataIndex - 1]
endmethod
private method broadcastData takes nothing returns nothing
local integer i = length
local integer ib = 0
local string bc
local integer add = 0
local boolean b = GetLocalPlayer() == p
local integer len
set dataPacket = IntegerQueuePacket.create(p)
loop
exitwhen ib == dataIndex
set bc = data.string[ib]
set i = 0
set len = (lengthPacket.pop() - 1)/ 4 + 1
loop
exitwhen len == 0
set len = len - 1
if b then
set add = S2A(SubString(bc, 0, 4))
set bc = SubString(bc, 4, StringLength(bc))
else
set add = 0
endif
call dataPacket.add(add)
endloop
call dataPacket.add(' #^Z')
set ib = ib + 1
endloop
call dataPacket.add(' #^Q')
call lengthPacket.destroy()
call dataPacket.broadcast()
endmethod
private method compileData takes nothing returns nothing
local integer i = length
local integer da
local boolean b = false
call data.flush()
set dataIndex = 0
loop
set da = dataPacket.pop()
exitwhen da == ' #^Q' or (b and da == 0)
set b = da == 0
if da == ' #^Z' then
set dataIndex = dataIndex + 1
else
set data.string[dataIndex] = data.string[dataIndex] + A2S(da)
endif
endloop
set dataIndex = 0
call dataPacket.destroy()
endmethod
public method destroy takes nothing returns nothing
set p = null
call deallocate()
endmethod
public method wait takes boolean pause returns boolean
if not hasRecievedLength then
if lengthPacket.wait(false) then
set hasRecievedLength = true
call broadcastData()
set dataIndex = 0
endif
elseif not hasRecievedData then
if dataPacket.wait(false) then
set hasRecievedData = true
call compileData()
endif
endif
if pause then
call TriggerSyncStart()
call TriggerSyncReady()
endif
return hasRecievedData
endmethod
integer compDataLength
public method broadcast takes nothing returns nothing
call lengthPacket.broadcast()
endmethod
public method add takes string s returns nothing
call lengthPacket.add(StringLength(s))
set data.string[dataIndex] = s
set dataIndex = dataIndex + 1
endmethod
public static method create takes player pFor returns thistype
local thistype this = thistype.allocate()
set compDataLength = 0
set p = pFor
set data = Table.create()
set lengthPacket = IntegerQueuePacket.create(p)
return this
endmethod
endstruct*/