From d8a1378e8bfd1915ab4386b63130d16c3acdfec7 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 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/rules.js b/rules.js index e6dd4e8..18c1367 100644 --- a/rules.js +++ b/rules.js @@ -261,6 +261,10 @@ const 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); @@ -367,7 +371,7 @@ function gen_action(view, action, argument) { } function roll_d6() { - return Math.floor(Math.random() * 6) + 1; + return random(6) + 1; } function reshuffle_discard(draw, discard) { @@ -377,14 +381,14 @@ function reshuffle_discard(draw, discard) { function draw_cards(hand, draw, n) { for (let i = 0; i < n; ++i) { - let c = Math.floor(Math.random() * draw.length); + let c = random(draw.length); hand.push(draw[c]); draw.splice(c, 1); } } function discard_random_card(hand, discard) { - let i = Math.floor(Math.random() * hand.length); + let i = random(hand.length); let c = hand[i]; discard.push(c); hand.splice(i, 1); @@ -2758,8 +2762,9 @@ exports.ready = function (scenario, players) { return players.length === 2; } -exports.setup = function (scenario) { +exports.setup = function (seed, scenario) { game = { + seed: seed, state: null, year: 1801, season: 0, -- cgit v1.2.3