- Joined
- Feb 6, 2014
- Messages
- 2,466
JASS:
library SyncNibble /*
SyncNibble v1.00
by Flux
SyncNibble allows you to synchronize a nibble (4 bits) which is represented
as an integer between 0 and 15. If you want to synchronize negative integers,
use 2's complement or use the most significant bit as a sign bit.
******************************* API: *********************************
- Sync.start(player source, integer nibble)
Start synchronizing a 4-bit integer of player source to all other players
Returns false upon improper use.
- Sync.wait()
Pause current execution of the code until synchronization is finished.
- Sync.nibble
Contains the synchronized 4-bit integer after Sync.wait()
If Sync.nibble == -1, it means synchronization failed.
*/
globals
//Important DUMMY_ID needed by the System
private constant integer DUMMY_ID = 'sync'
//Time between each check attempt
private constant real REFRESH_RATE = 0.05
//If the number of retry check attempt reaches MAX_RETRY, the synchronization
//will count as a failed and will display FailedMessage.
private constant integer MAX_RETRY = 20
endglobals
//Configure the displayed message upon a failed synchronization attempt
private function FailedMessage takes nothing returns nothing
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "|cffffcc00[SyncNibble]:|r Failed to synchronize due to poor network condition!")
endfunction
struct Sync extends array
static integer nibble
private static unit array dummy
private static player source
private static integer retry
private static boolean free = true
private static timer t = CreateTimer()
static method wait takes nothing returns nothing
loop
exitwhen free
call TriggerSleepAction(0.00)
endloop
endmethod
private static method check takes nothing returns nothing
local integer i = 0
local boolean b = true
loop
exitwhen IsUnitSelected(dummy[i], source)
if i > 15 then
set retry = retry + 1
if retry == MAX_RETRY then
set thistype.nibble = -1
set free = true
call PauseTimer(t)
if GetLocalPlayer() == source then
call ClearSelection()
endif
call FailedMessage()
endif
return
endif
set i = i + 1
endloop
set thistype.nibble = i
set free = true
call PauseTimer(t)
if GetLocalPlayer() == source then
call ClearSelection()
endif
endmethod
static method start takes player p, integer whichNibble returns boolean
local integer i = 0
if free then
if whichNibble >= 0 and whichNibble < 16 then
set free = false
loop
exitwhen i > 15
call SetUnitOwner(dummy[i], p, false)
set i = i + 1
endloop
if GetLocalPlayer() == p then
call ClearSelection()
call SelectUnit(dummy[whichNibble], true)
endif
set source = p
set retry = 0
call TimerStart(t, REFRESH_RATE, true, function thistype.check)
return true
debug else
debug call BJDebugMsg("|cffffcc00[SyncNibble]:|r Invalid nibble!")
endif
debug else
debug call BJDebugMsg("|cffffcc00[SyncNibble]:|r Busy")
endif
return false
endmethod
private static method onInit takes nothing returns nothing
local integer i = 0
loop
exitwhen i > 15
set dummy[i] = CreateUnit(Player(14), DUMMY_ID, 0, 0, 0)
call UnitRemoveAbility(dummy[i], 'Amov')
set i = i + 1
endloop
endmethod
endstruct
endlibrary
JASS:
library Tester initializer OnInit uses SyncNibble
private function BeginTest takes nothing returns nothing
local integer i1 = GetRandomInt(0, 15)
local integer i2 = GetRandomInt(0, 15)
local integer nibble
call BJDebugMsg("|cffffcc00**********Test about to begin***********|r")
//Make sure they have different values
loop
exitwhen i2 != i1
set i2 = GetRandomInt(0, 15)
endloop
//Assign a different value to each player
if GetLocalPlayer() == Player(0) then
set nibble = i1
elseif GetLocalPlayer() == Player(1) then
set nibble = i2
endif
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "your local integer is " + I2S(nibble))
//Display who is the source
if GetTriggerPlayer() == Player(0) then
call BJDebugMsg("About to sync from Player(0)")
elseif GetTriggerPlayer() == Player(1) then
call BJDebugMsg("About to sync from Player(1)")
endif
call BJDebugMsg("Synchronizing the nibble")
if Sync.start(GetTriggerPlayer(), nibble) then
call BJDebugMsg("Synchronization Starting...")
call Sync.wait()
call BJDebugMsg("Synchronization Finished")
else
call BJDebugMsg("Synchronization Failed")
endif
call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "your synchronized integer is " + I2S(Sync.nibble))
call BJDebugMsg("|cffffcc00*********************************************|r")
endfunction
private function OnInit takes nothing returns nothing
local trigger t = CreateTrigger()
local string s = "test"
call TriggerRegisterPlayerChatEvent(t, Player(0), s, true)
call TriggerRegisterPlayerChatEvent(t, Player(1), s, true)
call TriggerAddAction(t, function BeginTest)
//Test Messages
call TriggerSleepAction(0.1)
call BJDebugMsg("Type '" + s + "' to sync an integer")
endfunction
endlibrary
My last submission before I go inactive :/