summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2021-10-23 23:15:45 +0200
committerTor Andersson <tor@ccxvii.net>2022-11-16 19:08:56 +0100
commit23ca24db3e0a84066bc14af0841f0ba87d5141d5 (patch)
tree12cc4bb1bf62feae935ce52ab26399b2b592099d
parent2ad31c79096b21ff9501f3997e56d54600757678 (diff)
downloadjulius-caesar-23ca24db3e0a84066bc14af0841f0ba87d5141d5.tar.gz
Add PRNG seed to game state.
Log all game actions to a table so they can be replayed.
-rw-r--r--rules.js15
1 files changed, 10 insertions, 5 deletions
diff --git a/rules.js b/rules.js
index bab18a1..867c432 100644
--- a/rules.js
+++ b/rules.js
@@ -35,6 +35,10 @@ const RESERVE_MARK = "";
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);
@@ -171,7 +175,7 @@ function edge_id(A, B) {
}
function roll_d6() {
- return Math.floor(Math.random() * 6) + 1;
+ return random(6) + 1;
}
function reset_deck() {
@@ -184,7 +188,7 @@ function reset_deck() {
function deal_cards(deck, n) {
let hand = [];
for (let i = 0; i < n; ++i) {
- let c = Math.floor(Math.random() * deck.length);
+ let c = random(deck.length);
hand.push(deck[c]);
deck.splice(c, 1);
}
@@ -1138,7 +1142,7 @@ states.jupiter = {
for (let x in BLOCKS)
if (game.location[x] === where)
list.push(x);
- let i = Math.floor(Math.random() * list.length);
+ let i = random(list.length);
jupiter_block(list[i]);
},
secret: function (args) {
@@ -1151,7 +1155,7 @@ states.jupiter = {
for (let b in BLOCKS)
if (game.location[b] === where && BLOCKS[b].owner === owner)
list.push(b);
- let i = Math.floor(Math.random() * list.length);
+ let i = random(list.length);
jupiter_block(list[i]);
}
},
@@ -2376,8 +2380,9 @@ exports.ready = function (scenario, players) {
return players.length === 2;
}
-exports.setup = function (scenario) {
+exports.setup = function (seed, scenario) {
game = {
+ seed: seed,
tournament: (scenario === "Tournament"),
c_hand: [],
p_hand: [],