wesnoth-wiki-changes
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-wiki-changes] PseudoRandom


From: wiki
Subject: [Wesnoth-wiki-changes] PseudoRandom
Date: Tue, 16 Nov 2004 19:43 +0100

UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
IP: 216.20.1.218
URI: http://wesnoth.slack.it/?PseudoRandom
 - - - - -
||Pseudo Random Number Generation||

This is a replacement for {RANDOM} (and random= in set_variable)
in multiplayer, where "true" random numbers cause Out of Sync errors (as of 
0.8.7)

Instead of {RANDOM 0..155}, use {PSEUDORANDOM 156}. This is the only way to use 
it.
You can use {VARIABLE_OP random add X} afterwards to get a higher range.

 
################################################################################################
 ################Pseudo-Random Number Generation Engine For 
Multiplayer##########################
 
################################################################################################

 #Don't use this, it's for internal use by other macros
 #define GETNEXTNUMBER

 #multiplying xrand by 421
 {VARIABLE i 0}
 {VARIABLE_OP temp_factor to_variable xrand}
 [while]
 [variable]
 name=i
 less_than=420
 [/variable]
 [do]
 {VARIABLE_OP xrand add $temp_factor}
 {VARIABLE_OP i add 1}
 [/do]
 [/while]

 {VARIABLE_OP xrand add 2311}
 [while]
 [variable]
 name=xrand
 greater_than=32767
 [/variable]
 [do]
 {VARIABLE_OP xrand add -32768}
 [/do]
 [/while]
 #enddef

 [event]
 name=turn 2
        {VARIABLE xrand 0}
        [store_unit]
        [filter]
        [/filter]
        variable=units
        [/store_unit]
        {FOREACH units i}
                {VARIABLE_OP temp to_variable units[i].hitpoints}
                {VARIABLE_OP xrand add $temp}
        {NEXT i}
 #randomize the number away from low values a bit
 {GETNEXTNUMBER}
 {GETNEXTNUMBER}
 {GETNEXTNUMBER}
 {GETNEXTNUMBER}
        {CLEAR_VARIABLE temp}
        {CLEAR_VARIABLE units}
 [/event]

 #Instead of {RANDOM 0..155}, use {PSEUDORANDOM 156}. This is the only way to 
use it.
 #You can use {VARIABLE_OP random add X} afterwards to get a higher range.
 #define PSEUDORANDOM NUMBER
 {GETNEXTNUMBER}
 {VARIABLE_OP random to_variable xrand}

 {VARIABLE_OP temp format ({NUMBER})}
 [if]
 [variable]
 name=temp
 less_than=33
 [/variable]
        [then]
        #making temp equal {NUMBER}x1024
        {VARIABLE i 0}
        {VARIABLE temp 0}
        [while]
        [variable]
        name=i
        less_than={NUMBER}
        [/variable]
        [do]
        {VARIABLE_OP temp add 1024}
        {VARIABLE_OP i add 1}
        [/do]
        [/while]
        
        {VARIABLE_OP temp2 to_variable temp}
        {VARIABLE_OP temp2 multiply -1}
        [while]
                [variable]
                name=temp
                less_than=$random
                [/variable]
                        [do]
                        {VARIABLE_OP random add $temp2}
                        [/do]
        [/while]
        [/then]
 [/if]

 [while]
 [or]
 [variable]
 name=random
 greater_than={NUMBER}
 [/variable]
 [/or]
 [or]
 [variable]
 name=random
 equals={NUMBER}
 [/variable]
 [/or]
 [do]
 {VARIABLE_OP random add -{NUMBER}}
 [/do]
 [/while]
 #enddef


||See Also||

* UsefulWMLFragments






reply via email to

[Prev in Thread] Current Thread [Next in Thread]