Deterministic scripting

For AI and campaign script related discussions and questions
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Deterministic scripting

Post by NoQ »

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?
Reg312
Regular
Regular
Posts: 681
Joined: 25 Mar 2011, 18:36

Re: Deterministic scripting

Post by Reg312 »

NoQ 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?
Math.random used in scavfact.js
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Deterministic scripting

Post by NoQ »

scavfact.js is only run on host, like any AI script.
Reg312
Regular
Regular
Posts: 681
Joined: 25 Mar 2011, 18:36

Re: Deterministic scripting

Post by Reg312 »

NoQ wrote:scavfact.js is only run on host, like any AI script.
well i think you able to use same seed for random function on all clients.. not sure hot it works in js
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Deterministic scripting

Post by NoQ »

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).
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

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
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Deterministic scripting

Post by NoQ »

Then host could cheat too easily (adding buildings and droids for himself, setting power, enabling technologies).
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

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.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Cyp
Evitcani
Evitcani
Posts: 784
Joined: 17 Jan 2010, 23:35

Re: Deterministic scripting

Post by Cyp »

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.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

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.
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Deterministic scripting

Post by Per »

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.
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

I assume we continue to use Math.random() as usual?

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
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

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
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: Deterministic scripting

Post by aubergine »

"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?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Per
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 3780
Joined: 03 Aug 2006, 19:39

Re: Deterministic scripting

Post by Per »

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.
Post Reply