summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2024-05-18 00:13:06 +0200
committerTor Andersson <tor@ccxvii.net>2024-08-21 00:28:20 +0200
commitce0a313d66852b7d8c228cf992d260b665c51ad8 (patch)
treee088f3c9608af5c1ce81029cba3e1e5bbcab305c
parent79bad8cdc623f9277507361e95f00bf60e1c229d (diff)
downloadwashingtons-war-ce0a313d66852b7d8c228cf992d260b665c51ad8.tar.gz
dynamic colony control
-rw-r--r--rules.js116
1 files changed, 46 insertions, 70 deletions
diff --git a/rules.js b/rules.js
index ae93fb7..c23b1f4 100644
--- a/rules.js
+++ b/rules.js
@@ -5,6 +5,8 @@
// TODO: bit flags for regulars, european_war, french alliance triggered
+/* DATA */
+
const data = require("./data")
const CARDS = data.cards
@@ -59,7 +61,6 @@ const WASHINGTON = data.general_index["Washington"]
const CARLETON = data.general_index["Carleton"]
const HOWE = data.general_index["Howe"]
-const NOWHERE = -1
const BOSTON = data.space_index["Boston"]
const CHARLESTON = data.space_index["Charleston"]
const FALMOUTH = data.space_index["Falmouth"]
@@ -79,7 +80,7 @@ const CONTINENTAL_CONGRESS_DISPERSED = data.space_index["Continental Congress Di
const BRITISH_REINFORCEMENTS = data.space_index["British Leader Reinforcements"]
const AMERICAN_REINFORCEMENTS = data.space_index["American Leader Reinforcements"]
const FRENCH_REINFORCEMENTS = data.space_index["French Reinforcements"]
-const ELIMINATED = data.spaces.length
+const NOWHERE = data.spaces.length
const THE_13_COLONIES = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
const SOUTH_OF_WINTER_ATTRITION_LINE = [ 11, 12, 13 ]
@@ -94,21 +95,16 @@ const general_count = data.generals.length
const space_count = 66
const all_spaces = new Array(space_count).fill(0).map((_,i)=>i)
-// 66 * 2 / 32 = 5 words for all PC data
-// cu stacks
-
const ENEMY = { [P_AMERICA]: P_BRITAIN, [P_BRITAIN]: P_AMERICA }
-exports.scenarios = [ "Standard" ]
-
-exports.roles = [ P_BRITAIN, P_AMERICA ]
-
let states = {}
let events = {}
let game
let view
+/* SETUP */
+
function setup_game(seed) {
game = {
seed: seed,
@@ -147,9 +143,6 @@ function setup_game(seed) {
// transient state
moved: [],
mcu: [],
-
- // TODO: compute on the fly
- control: [],
}
set_space_pc(QUEBEC, PC_BRITISH)
@@ -361,11 +354,11 @@ function allowed_to_place_american_pc() {
}
function is_british_militia(space) {
- return game.control[SPACES[space].colony] === PC_BRITISH
+ return get_colony_control(SPACES[space].colony) === PC_BRITISH
}
function is_american_militia(space) {
- return game.control[SPACES[space].colony] === PC_AMERICAN
+ return get_colony_control(SPACES[space].colony) === PC_AMERICAN
}
function is_american_winter_offensive() {
@@ -456,22 +449,20 @@ function flip_pc(space) {
set_space_pc(space, PC_BRITISH)
}
-function update_colony_control() {
- for (let c = 0; c <= 13; ++c) {
- let control = 0
- for (let space of COLONIES[c]) {
- if (has_british_pc(space))
- --control
- else if (has_american_pc(space))
- ++control
- }
- if (control < 0)
- game.control[c] = PC_BRITISH
- else if (control > 0)
- game.control[c] = PC_AMERICAN
- else
- game.control[c] = PC_NONE
+function get_colony_control(c) {
+ let control = 0
+ for (let space of COLONIES[c]) {
+ let pc = get_space_pc(space)
+ if (pc === PC_BRITISH)
+ --control
+ else if (pc === PC_AMERICAN)
+ ++control
}
+ if (control < 0)
+ return PC_BRITISH
+ if (control > 0)
+ return PC_AMERICAN
+ return PC_NONE
}
/* CU */
@@ -736,7 +727,7 @@ function move_general(who, where) {
}
function capture_washington() {
- set_general_location(WASHINGTON, ELIMINATED)
+ set_general_location(WASHINGTON, NOWHERE)
if (!game.french_alliance_triggered) {
game.french_alliance -= 3
@@ -770,9 +761,9 @@ function capture_enemy_general(where) {
}
function remove_benedict_arnold() {
- if (!is_general_at_location(ARNOLD, ELIMINATED)) {
+ if (!is_general_at_location(ARNOLD, NOWHERE)) {
log("Removed Arnold from the game!")
- set_general_location(ARNOLD, ELIMINATED)
+ set_general_location(ARNOLD, NOWHERE)
}
}
@@ -956,32 +947,6 @@ function disperse_continental_congress() {
/* MOVE GENERATORS */
-function gen_action(action, argument) {
- if (!view.actions)
- view.actions = {}
- if (argument !== undefined) {
- if (!(action in view.actions))
- view.actions[action] = [ argument ]
- else
- view.actions[action].push(argument)
- } else {
- view.actions[action] = 1
- }
-}
-
-function gen_action_undo() {
- if (!view.actions)
- view.actions = {}
- if (game.undo && game.undo.length > 0)
- view.actions.undo = 1
- else
- view.actions.undo = 0
-}
-
-function gen_pass() {
- gen_action("pass")
-}
-
function gen_remove_british_pc_from(list_of_colonies) {
for (let colony of list_of_colonies) {
for (let space of COLONIES[colony]) {
@@ -3497,11 +3462,9 @@ states.european_war = {
}
function norths_government_falls() {
- update_colony_control()
-
let n_american = 0
for (let c = 0; c <= 13; ++c)
- if (game.control[c] === PC_AMERICAN)
+ if (get_colony_control(c) === PC_AMERICAN)
++n_american
if (n_american >= 7)
@@ -3543,6 +3506,20 @@ states.game_over = {
/* CLIENT/SERVER COMMS */
+function gen_action(action, argument) {
+ if (argument === undefined) {
+ view.actions.action = 1
+ } else {
+ if (!(action in view.actions))
+ view.actions[action] = []
+ set_add(view.actions[action], argument)
+ }
+}
+
+exports.scenarios = [ "Standard" ]
+
+exports.roles = [ P_BRITAIN, P_AMERICA ]
+
exports.setup = function (seed, scenario, options) {
setup_game(seed)
return game
@@ -3550,6 +3527,9 @@ exports.setup = function (seed, scenario, options) {
exports.action = function (state, current, action, arg) {
game = state
+
+ Object.seal(game) // don't allow adding properties!
+
// TODO: check against action list
if (current === game.active) {
let S = states[game.state]
@@ -3563,14 +3543,9 @@ exports.action = function (state, current, action, arg) {
}
}
- update_colony_control()
-
return game
}
-function list_actions() {
-}
-
exports.view = function (state, current) {
game = state
@@ -3587,7 +3562,6 @@ exports.view = function (state, current) {
loc: state.loc,
spc: state.spc,
- control: state.control,
a_cards: state.a_hand.length,
b_cards: state.b_hand.length,
a_queue: state.a_queue,
@@ -3610,7 +3584,10 @@ exports.view = function (state, current) {
if (current === state.active) {
view.actions = {}
states[game.state].prompt()
- gen_action_undo()
+ if (game.undo && game.undo.length > 0)
+ view.actions.undo = 1
+ else
+ view.actions.undo = 0
} else {
let inactive = states[game.state].inactive
if (typeof inactive !== "string")
@@ -3621,8 +3598,7 @@ exports.view = function (state, current) {
return view
}
-
-// === COMMON LIBRARY ===
+/* COMMON LIBRARY */
function log(s) {
game.log.push(s)