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:19:39 +0100
commit03227f80a89508b0e709a41b4a99e34f75777144 (patch)
tree320eca90a8b8cbd56ae5b54e9959bb2cfb19fd79
parentbe7cf9dbd78b409dd7fa31bf7d3c4112e4a1dc04 (diff)
downloadcrusader-rex-03227f80a89508b0e709a41b4a99e34f75777144.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 345ef39..68efba2 100644
--- a/rules.js
+++ b/rules.js
@@ -77,6 +77,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);
@@ -201,7 +205,7 @@ function gen_action(view, action, argument) {
}
function roll_d6() {
- return Math.floor(Math.random() * 6) + 1;
+ return random(6) + 1;
}
function shuffle_deck() {
@@ -214,7 +218,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);
}
@@ -228,7 +232,7 @@ function select_random_block(where) {
list.push(b);
if (list.length === 0)
return null;
- return list[Math.floor(Math.random() * list.length)];
+ return list[random(list.length)];
}
function select_random_enemy_block(where) {
@@ -238,7 +242,7 @@ function select_random_enemy_block(where) {
list.push(b);
if (list.length === 0)
return null;
- return list[Math.floor(Math.random() * list.length)];
+ return list[random(list.length)];
}
function block_plural(who) {
@@ -3677,8 +3681,9 @@ exports.ready = function (scenario, players) {
return players.length === 2;
}
-exports.setup = function (scenario) {
+exports.setup = function (seed, scenario) {
game = {
+ seed: seed,
s_hand: [],
f_hand: [],
s_card: 0,