Deterministic scripting
-
NoQ
- Special

- Posts: 6226
- Joined: 24 Dec 2009, 11:35
- Location: /var/zone
Deterministic scripting
As long as rules.js is run on all clients ... Can Math.random() be used in rules.js? Can floating point calculations be used in rules.js at all? Maybe it's worth adding a multiplayer-safe random() API function?
Maps | Tower Defense | NullBot AI | More NullBot AI | Scavs | More Scavs | Tilesets | Walkthrough | JSCam
-
Reg312
- Regular

- Posts: 681
- Joined: 25 Mar 2011, 18:36
Re: Deterministic scripting
Math.random used in scavfact.jsNoQ wrote:As long as rules.js is run on all clients ... Can Math.random() be used in rules.js? Can floating point calculations be used in rules.js at all? Maybe it's worth adding a multiplayer-safe random() API function?
-
NoQ
- Special

- Posts: 6226
- Joined: 24 Dec 2009, 11:35
- Location: /var/zone
Re: Deterministic scripting
scavfact.js is only run on host, like any AI script.
Maps | Tower Defense | NullBot AI | More NullBot AI | Scavs | More Scavs | Tilesets | Walkthrough | JSCam
-
Reg312
- Regular

- Posts: 681
- Joined: 25 Mar 2011, 18:36
Re: Deterministic scripting
well i think you able to use same seed for random function on all clients.. not sure hot it works in jsNoQ wrote:scavfact.js is only run on host, like any AI script.
-
NoQ
- Special

- Posts: 6226
- Joined: 24 Dec 2009, 11:35
- Location: /var/zone
Re: Deterministic scripting
1. this way it becomes dull (imagine trees growing exactly similarly each game, or oil drums placed in the same spots), unless we have an API support to synchronize random seed across clients.
2. it is still uncertain whether floating-point operations will have equal results on all platforms (since Math.random() returns a float).
2. it is still uncertain whether floating-point operations will have equal results on all platforms (since Math.random() returns a float).
Maps | Tower Defense | NullBot AI | More NullBot AI | Scavs | More Scavs | Tilesets | Walkthrough | JSCam
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
What if only the rules.js associated with player 0 (always human game host) did that stuff and had some way to update the remote clients on its actions?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
NoQ
- Special

- Posts: 6226
- Joined: 24 Dec 2009, 11:35
- Location: /var/zone
Re: Deterministic scripting
Then host could cheat too easily (adding buildings and droids for himself, setting power, enabling technologies).
Maps | Tower Defense | NullBot AI | More NullBot AI | Scavs | More Scavs | Tilesets | Walkthrough | JSCam
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
Fair point.
Maybe use something derived form game time as the random seed?
And, because humans in the game will obviously affect things, eg. by destroying stuff, causing AI to have to react to that destruction, resulting in AIs planned actions being affected and thus different outcomes, the human players are the real random seed.
Maybe use something derived form game time as the random seed?
And, because humans in the game will obviously affect things, eg. by destroying stuff, causing AI to have to react to that destruction, resulting in AIs planned actions being affected and thus different outcomes, the human players are the real random seed.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
Cyp
- Evitcani

- Posts: 784
- Joined: 17 Jan 2010, 23:35
Re: Deterministic scripting
The gameRand() function can be used to generate synchronised random numbers. It's important to call it on all clients at exactly the same game time. Not sure if there's a way to call it from Javascript, though. The seed should be different each game.
Would be good if scripts could be deterministic, don't think they would be if using floating-point, if at all.
Would be good if scripts could be deterministic, don't think they would be if using floating-point, if at all.
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
In WZScript I found the following:
int random(range)
Return a random number between 0 and range - 1.
randomiseSeed()
Generate a new random seed for the random number generator.
Maybe port those to JS? I assume there is some synch in place for those WZScript functions already, so hopefully not to difficult to expose them to JS.
int random(range)
Return a random number between 0 and range - 1.
randomiseSeed()
Generate a new random seed for the random number generator.
Maybe port those to JS? I assume there is some synch in place for those WZScript functions already, so hopefully not to difficult to expose them to JS.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
Per
- Warzone 2100 Team Member

- Posts: 3780
- Joined: 03 Aug 2006, 19:39
Re: Deterministic scripting
Added new function syncRandom(limit) to generate sync-safe random numbers. Must only be called from scripts that actually run on all peers. Also I suspect that scripts will be unlikely to be sync safe if they involve any kind of math in synced rules decisions, since I believe javascript numbers tend to be floats.
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
I assume we continue to use Math.random() as usual?
EDIT: just realised we use the new function instead. *brain fail moment*
EDIT: just realised we use the new function instead. *brain fail moment*
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
Will it be safe if we Math.round() / Math.ceil() / Math.floor() ?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
aubergine
- Professional

- Posts: 3462
- Joined: 10 Oct 2010, 00:58
Re: Deterministic scripting
"game frame" = 100ms?
Also, how do we ensure all peers run the code in the same fame frame?
For example, we don't have direct control over when timers will trigger, my understanding is that they will trigger as close to the desired time as possible?
Also, how do we ensure all peers run the code in the same fame frame?
For example, we don't have direct control over when timers will trigger, my understanding is that they will trigger as close to the desired time as possible?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
-- https://warzone.atlassian.net/wiki/display/GO
-
Per
- Warzone 2100 Team Member

- Posts: 3780
- Joined: 03 Aug 2006, 19:39
Re: Deterministic scripting
I doubt that rounding control will make floats safe, but I don't know for sure. (The only thing that would make it entirely safe would be if we managed to compile the javascript engine without floating point support, somehow. I don't know if that is possible.)
Each game frame is 100ms in game time. A game second has 10 frames, always. A game second may last shorter or longer than a real second, depending on lag and game speed setting.
The same code runs on all peers, so triggers will trigger at the same time on each. Script writers do not have to think about that part.
Each game frame is 100ms in game time. A game second has 10 frames, always. A game second may last shorter or longer than a real second, depending on lag and game speed setting.
The same code runs on all peers, so triggers will trigger at the same time on each. Script writers do not have to think about that part.