• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] 12-Bit Integer Synchronization

Status
Not open for further replies.
Level 8
Joined
Jun 28, 2008
Messages
356
I tried to synchronize variables with gamecache, but that caused me problems and didn't even work. Maybe I didn't do it right. So instead I developed something else:

JASS:
//******************************************************************************
//
//      12-Bit Integer Synchronization System
//      by TriEdgeAI
//
//      unsigned int12: 0 to 4096 
//      signed   int12: -2048 to 2047 
//
//      Why 12-bit only? Blame Blizzard. Uses selections.
//      Local RandomInts make crashes, even tho everything works.  
//
//******************************************************************************

globals

    unit array xbit
    boolexpr sync_extract_cb
    integer sync_num
    group sync_xbits
    
endglobals

function sync_extract_callback takes nothing returns boolean

    set sync_num = sync_num + GetUnitUserData(GetFilterUnit())
    return false
    
endfunction

function sync_init takes nothing returns nothing

    local integer i = 1
    local integer j = 0   
    local texttag tt = null
    
    set sync_extract_cb = Condition(function sync_extract_callback)
    set sync_xbits = CreateGroup()
    
    loop
        
        set xbit[j] = CreateUnit(Player(14), 'xbit', -400 + j * 100, 0, 0)
        call SetUnitUserData(xbit[j], i)
        set tt = CreateTextTag()
        call SetTextTagText(tt, I2S(i), 0.023)
        call SetTextTagPos(tt, -400 + j * 100, 0, 100)
        set i = i * 2
        set j = j + 1
        exitwhen i == 4096 
        
    endloop
    
    set tt = null
    
endfunction

function sync_mark takes player p, integer int12 returns nothing

    if(GetLocalPlayer() == p) then
        
        set int12 = int12 + 2048
        
        call ClearSelection()
        
        if(int12 >= 2048) then
            call SelectUnit(xbit[11], true)
            set int12 = int12 - 2048
        endif
        
        if(int12 >= 1024) then
            call SelectUnit(xbit[10], true)
            set int12 = int12 - 1024
        endif
        
        if(int12 >= 512) then
            call SelectUnit(xbit[9], true)
            set int12 = int12 - 512
        endif
        
        if(int12 >= 256) then
            call SelectUnit(xbit[8], true)
            set int12 = int12 - 256
        endif
        
        if(int12 >= 128) then
            call SelectUnit(xbit[7], true)
            set int12 = int12 - 128
        endif
        
        if(int12 >= 64) then
            call SelectUnit(xbit[6], true)
            set int12 = int12 - 64
        endif
        
        if(int12 >= 32) then
            call SelectUnit(xbit[5], true)
            set int12 = int12 - 32
        endif
        
        if(int12 >= 16) then
            call SelectUnit(xbit[4], true)
            set int12 = int12 - 16
        endif
        
        if(int12 >= 8) then
            call SelectUnit(xbit[3], true)
            set int12 = int12 - 8
        endif
        
        if(int12 >= 4) then
            call SetUnitOwner(xbit[11], p, false)
            call SelectUnit(xbit[2], true)
            set int12 = int12 - 4
        endif
        
        if(int12 >= 2) then
            call SetUnitOwner(xbit[11], p, false)
            call SelectUnit(xbit[1], true)
            set int12 = int12 - 2
        endif
        
        if(int12 >= 1) then
            call SetUnitOwner(xbit[11], p, false)
            call SelectUnit(xbit[0], true)
        endif
    
    endif

endfunction

function sync_acquire takes player p returns nothing

    call SetUnitOwner(xbit[0], p, false)
    call SetUnitOwner(xbit[1], p, false)
    call SetUnitOwner(xbit[2], p, false)
    call SetUnitOwner(xbit[3], p, false)
    call SetUnitOwner(xbit[4], p, false)
    call SetUnitOwner(xbit[5], p, false)
    call SetUnitOwner(xbit[6], p, false)
    call SetUnitOwner(xbit[7], p, false)
    call SetUnitOwner(xbit[8], p, false)
    call SetUnitOwner(xbit[9], p, false)
    call SetUnitOwner(xbit[10], p, false)
    call SetUnitOwner(xbit[11], p, false)
    
endfunction

function sync_extract takes player p returns integer

    
    set sync_num = -2048
    call SyncSelections()
    call GroupEnumUnitsSelected(sync_xbits, p, sync_extract_cb)
    return sync_num
    
endfunction

And an example use:
JASS:
globals

    integer array async //This is gonna be asynced

endglobals

function Trig_Initiailization_Actions takes nothing returns nothing

    local integer i = 0

    call sync_init() //Prepare the system for use
    
    if(GetLocalPlayer() == Player(0)) then
        set async[0] = 666
        set async[1] = 1337
        set async[2] = -2048
        set async[3] = 2047
        set async[4] = 1134
    endif
    
    set i = 0
    loop
        call BJDebugMsg("[" + I2S(i) + "] = " + I2S(async[i])) //Everyone should see 0, Player Red should see the number
        set i = i + 1
        exitwhen i == 5
    endloop
    
    call sync_acquire(Player(0)) //Prepare for submitting numbers to sync from Player Red
    
    set i = 0
    loop
        call sync_mark(Player(0), async[i]) //Submit a number
        set async[i] = sync_extract(Player(0)) //Synchronize the number
        call BJDebugMsg("[" + I2S(i) + "] = " + I2S(async[i])) //Everyone should see the random number
        set i = i + 1
        exitwhen i == 5
    endloop
    
    call FogEnable(false) //Disable fog
    call FogMaskEnable(false) //Murder a terrorist
    
endfunction

function InitTrig_Initiailization takes nothing returns nothing

    set gg_trg_Initiailization = CreateTrigger()
    call TriggerAddAction(gg_trg_Initiailization, function Trig_Initiailization_Actions)
    
endfunction

I was inspired by this:

http://www.hiveworkshop.com/forums/latest-updates-news-101/new-warcraft-iii-security-exploit-184310/

More specificly preloading data from the host's PC and then syncing it to all the clients. Anyways, I would be happy if someone shows me a way to do syncing with GameCache or a better way that actually works. :| Thanks. Critics on the alternative above are also welcome.
 
Level 8
Joined
Jun 28, 2008
Messages
356
Status
Not open for further replies.
Top