summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2021-10-23 23:15:45 +0200
committerTor Andersson <tor@ccxvii.net>2023-02-18 12:12:42 +0100
commitd8a1378e8bfd1915ab4386b63130d16c3acdfec7 (patch)
treed560ffa9d90c237818a208ffddd7f99209f4355b
parent0365abbb564b5207e5eea8eb0e78fcb2e1e9dd02 (diff)
downloadshores-of-tripoli-d8a1378e8bfd1915ab4386b63130d16c3acdfec7.tar.gz
Add PRNG seed to game state.
Log all game actions to a table so they can be replayed.
-rw-r--r--rules.js13
1 files 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,