summaryrefslogtreecommitdiff
path: root/rules.js
diff options
context:
space:
mode:
authoriainp5 <iain.pearce.ip@gmail.com>2024-10-25 18:13:20 +0100
committeriainp5 <iain.pearce.ip@gmail.com>2024-10-25 18:13:20 +0100
commit5bc3b0140d5e59f0c8d987d2d3d5299c420673ee (patch)
tree7474d9466d1ac307a262b4375ddc7ba78415368b /rules.js
parente85e39c90fd3d6b375a4ce48d3347531b75f5f56 (diff)
download1989-dawn-of-freedom-5bc3b0140d5e59f0c8d987d2d3d5299c420673ee.tar.gz
Removed Math.random
Diffstat (limited to 'rules.js')
-rw-r--r--rules.js98
1 files changed, 57 insertions, 41 deletions
diff --git a/rules.js b/rules.js
index d0d86f7..877c4d8 100644
--- a/rules.js
+++ b/rules.js
@@ -721,7 +721,7 @@ states.finish_add_infl = {
// Generate actions for valid spaces
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id)
+ gen_action_space(space_id)
}
},
space(space) {
@@ -749,7 +749,7 @@ states.finish_support_check_prep = {
} else {
view.prompt = `Select a space. ${pluralize(game.available_ops, 'support check')} remaining.`
for (let space_id of game.valid_spaces) {
- space(space_id)
+ gen_action_space(space_id)
}
}
},
@@ -825,7 +825,7 @@ states.add_influence = {
// Generate actions for valid spaces
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
}
},
@@ -1014,7 +1014,7 @@ states.support_check_prep = {
view.prompt = `Select a space. ${pluralize(game.available_ops, 'support check')} remaining.`
for (let space_id of game.valid_spaces) {
- space(space_id)
+ gen_action_space(space_id)
}
}
},
@@ -1248,10 +1248,10 @@ states.the_crowd_turns_against_ceausescu_infl = {
return;
}
- view.prompt = `Add SPs: ${game.vm_available_ops} remaining`
- for (let space of game.valid_spaces) {
- gen_action_space(space)
- }
+ view.prompt = `Add SPs: ${game.vm_available_ops} remaining`
+ for (let space of game.valid_spaces) {
+ gen_action_space(space)
+ }
},
space(space) {
vm_do_add_infl(space)
@@ -1496,7 +1496,7 @@ states.power_struggle = {
}
},
roll () {
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
if (roll >= power_cards[game.played_power_card].value) {
log('Initiative roll successful')
@@ -1623,7 +1623,7 @@ states.support_loss ={
},
roll () {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
let rally_win = 0
let petition_win = 0
log(`Roll: D${roll}`)
@@ -1709,7 +1709,7 @@ states.vp_roll = {
}
},
roll () {
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
let rally_win = 0
let petition_win = 0
@@ -2064,7 +2064,7 @@ states.general_strike = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
logi(`+${game.available_ops} ops`)
@@ -2454,7 +2454,7 @@ function do_sc(space) {
// Continue with Support Check Logic
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
/*
@@ -2927,7 +2927,7 @@ function check_com_control(space_id) {
}
function do_tst_attempt() {
- let roll = Math.floor(Math.random() * 6) + 1;
+ let roll = roll_d6();
log(`Roll: D${roll}`);
roll += game.available_ops
@@ -3944,6 +3944,18 @@ function change_player() {
game.active = DEM
}
+function random(range) {
+ // An MLCG using integer arithmetic with doubles.
+ // https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf
+ // m = 2**35 − 31
+ return (game.seed = game.seed * 200105 % 34359738337) % range
+}
+
+function roll_d6() {
+ return random(6) + 1
+}
+
+
function find_space_index(name_unique) {
return spaces.findIndex(space => space && space.name_unique === name_unique)
}
@@ -4002,7 +4014,7 @@ function draw_card(deck) {
deck.push(...game.strategy_discard)
game.strategy_discard = []
}
- const randomIndex = Math.floor(Math.random() * deck.length)
+ const randomIndex = Math.floor(random(deck.length))
//console.log('card chosen:', randomIndex)
//console.log('game.strategy_deck after', game.strategy_deck)
return deck.splice(randomIndex, 1)[0];
@@ -4072,7 +4084,7 @@ function remove_from_discard(card) {
function discard_card(hand) {
//let find_card
- let card = Math.floor(Math.random()*hand.length)
+ let card = Math.floor(random(hand.length))
let discarded_card = hand.splice(card, 1)[0]
if (game.is_pwr_struggle) {
if (numberless_cards.includes(discarded_card)) {
@@ -6217,7 +6229,7 @@ states.vm_add_infl = {
view.prompt = `${clean_name(cards[this_card()].name)}: add ${pluralize(game.vm_available_ops,'SP')}${event_prompt()}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id)
+ gen_action_space(space_id)
}
} else {
get_end_infl_prompt()
@@ -6270,7 +6282,7 @@ states.vm_add_infl_free = {
view.prompt = `${clean_name(cards[this_card()].name)}: add ${game.vm_available_ops} SPs to ${event_prompt()}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} else {
get_end_infl_prompt()
@@ -6368,7 +6380,7 @@ states.vm_add_limited_infl = {
view.prompt = `${clean_name(cards[this_card()].name)}: add ${pluralize(game.vm_available_ops,'SP')} to ${event_prompt()}.`
}
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} /*else {
view.prompt = `${clean_name(cards[this_card()].name)}. Add SPs: done.`
@@ -6419,7 +6431,7 @@ states.vm_remove_infl = {
view.prompt = `${clean_name(cards[this_card()].name)}: remove ${pluralize(game.vm_available_ops,'SP')}${event_prompt()}.`
}
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
},
space(space) {
@@ -6459,7 +6471,7 @@ states.vm_remove_x_infl = {
view.prompt = `${clean_name(cards[this_card()].name)}: remove ${pluralize(game.vm_available_ops,'SP')} from ${event_prompt()}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} /*else {
if (game.vm_infl_to_do) {
@@ -6499,7 +6511,7 @@ states.vm_remove_limited_infl = {
view.prompt = `${clean_name(cards[this_card()].name)}: remove ${pluralize(game.vm_available_ops,'SP')}${event_prompt()}, no more than ${game.vm_max_infl} per space.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} else if (game.valid_spaces.length === 0 && game.vm_available_ops > 0) {
view.prompt = `${clean_name(cards[this_card()].name)}: no further SP to remove.`
@@ -6546,7 +6558,7 @@ states.vm_remove_all_infl = {
} else if (game.vm_available_ops > 0) {
view.prompt = `${clean_name(cards[this_card()].name)}: remove all SPs from ${event_prompt()}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} /*else {
view.prompt = `${clean_name(cards[this_card()].name)}. Remove SPs: done.`
@@ -6582,7 +6594,7 @@ states.vm_support_check_prep = {
//}
for (let space_id of game.valid_spaces) {
if (!space_id) continue
- space(space_id);
+ gen_action_space(space_id);
}
}
},
@@ -6630,7 +6642,7 @@ states.vm_ceh_support_check_prep = {
view.prompt = `Select a space. ${pluralize(game.vm_available_ops, 'support check')} remaining.`
for (let space_id of game.valid_spaces) {
- space(space_id)
+ gen_action_space(space_id)
}
}
},
@@ -6709,7 +6721,7 @@ states.vm_1_support_check_prep = {
for (let space_id of game.valid_spaces) {
if (!space_id) continue
- space(space_id);
+ gen_action_space(space_id);
}
}
},
@@ -6778,7 +6790,7 @@ states.vm_adamec = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
let worker_spaces = spaces.filter(space => space && space.country === 'Czechoslovakia' && space.socio === 4 && check_dem_control(space.space_id)).length
if (worker_spaces > 0) {
@@ -6986,7 +6998,7 @@ states.vm_dash_for_the_west = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
let com_control = check_presence('East_Germany').com_spaces
@@ -7090,8 +7102,12 @@ states.vm_deutsche_marks_confirm = {
gen_action('done')
},
done() {
- next_player()
- game.state = 'vm_deutsche_marks'
+ if (is_auto_resolve(game.vm_event) || switch_events.includes(game.vm_event)) {
+ goto_vm(game.vm_event)
+ } else {
+ next_player()
+ game.state = 'vm_deutsche_marks'
+ }
}
}
@@ -7736,7 +7752,7 @@ states.vm_switch_infl = {
/*if (game.vm_available_ops > 0 ) {*/
view.prompt = `${clean_name(cards[game.played_card].name)}: ${event_prompt()}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
} /*else {
view.prompt = 'Influence replaced.'
@@ -7774,7 +7790,7 @@ states.vm_malta_summit = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
if (game.stability > 0) {
log(`+${game.stability} from USSR Stability Track`)
@@ -7818,7 +7834,7 @@ states.vm_modrow = {
},
roll(){
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
let dem_spaces = spaces.filter(space => space && space.country === 'East_Germany' && check_dem_control(space.space_id)).length
if (roll > dem_spaces) {
log(`Roll: D${roll}`)
@@ -7848,7 +7864,7 @@ states.vm_nepotism = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
if (roll < 3) {
log(`Roll: D${roll} adds 4 SPs`)
game.vm_available_ops = 4}
@@ -7952,7 +7968,7 @@ states.vm_nomenklatura_remove = {
view.prompt = 'Nomenklatura: remove all Democratic SPs from Elite spaces.'
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
}
},
@@ -8117,7 +8133,7 @@ states.vm_shock_therapy = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
let worker_farmer = 0
for (let space of spaces) {
if (space && space.country === game.vm_active_country && check_com_control(space.space_id) && (space.socio === 3 || space.socio === 4)) {
@@ -8385,7 +8401,7 @@ states.vm_the_wall_must_go = {
log_h3(`Round ${attempt+1}`)
}
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
if (game.active === DEM) {
let controlled_spaces = spaces.filter(space => space && space.country === 'East_Germany' && check_dem_control(space.space_id)).length
@@ -8588,7 +8604,7 @@ states.vm_workers_revolt = {
}
view.prompt = 'Workers Revolt: select a Worker Space in a country your opponent has power.'
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id)
+ gen_action_space(space_id)
}
},
pass() {
@@ -8620,7 +8636,7 @@ states.vm_workers_revolt_finish = {
},
roll() {
clear_undo()
- let roll = Math.floor(Math.random() * 6) + 1
+ let roll = roll_d6()
log(`Roll: D${roll}`)
let adj = count_adj(game.selected_space)
if (game.active === DEM) {
@@ -8704,7 +8720,7 @@ states.vm_tst_4 = {
view.prompt = `Tiananmen Square Track award: remove ${pluralize(game.vm_available_ops,'SP')}.`
for (let space_id of game.valid_spaces) {
- gen_action_space(space_id);
+ gen_action_space(space_id);
}
},
space(space) {
@@ -8737,7 +8753,7 @@ states.vm_tst_6 = {
view.prompt = 'Tiananmen Square Track award: you have a free 2 Ops support check.'
for (let space_id of game.valid_spaces) {
if (space_id) {
- space(space_id);
+ gen_action_space(space_id);
}
}
//}