From 3aec639f39c1d035a5a2aa6f12e139d70e58c9fa Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 23 Oct 2021 23:15:45 +0200 Subject: Add PRNG seed to game state. Log all game actions to a table so they can be replayed. --- rules.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/rules.js b/rules.js index 337f982..5d507df 100644 --- a/rules.js +++ b/rules.js @@ -37,6 +37,10 @@ let states = {}; let game = null; +function random(n) { + return Math.floor(((game.seed = game.seed * 48271 % 0x7fffffff) / 0x7fffffff) * n); +} + function log(...args) { let s = Array.from(args).join(" "); game.log.push(s); @@ -144,7 +148,7 @@ function gen_action(view, action, argument) { } function roll_d6() { - return Math.floor(Math.random() * 6) + 1; + return random(6) + 1; } function shuffle_deck() { @@ -157,7 +161,7 @@ function shuffle_deck() { function deal_cards(deck, n) { let hand = []; for (let i = 0; i < n; ++i) { - let k = Math.floor(Math.random() * deck.length); + let k = random(deck.length); hand.push(deck[k]); deck.splice(k, 1); } @@ -631,7 +635,7 @@ function draw_from_bag(bag, exclude_list) { if (game.location[b] === bag) list.push(b); } - return list[Math.floor(Math.random() * list.length)]; + return list[random(list.length)]; } function deploy_english(count) { @@ -640,7 +644,7 @@ function deploy_english(count) { if (game.location[b] === E_BAG) list.push(b); for (let i = 0; i < count; ++i) { - let x = Math.floor(Math.random() * list.length); + let x = random(list.length); let b = list[x]; list.splice(x,1); game.location[b] = ENGLAND; @@ -2827,8 +2831,9 @@ exports.ready = function (scenario, players) { return players.length === 2; } -exports.setup = function (scenario) { +exports.setup = function (seed, scenario) { game = { + seed: seed, attacker: {}, border_limit: {}, last_used: {}, -- cgit v1.2.3