From 0c673db43e6a6872ffc3ecf4db42b168ad375c70 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 18 Nov 2022 00:58:36 +0100 Subject: Add initial implementation of UI and rules. --- data.js | 97 +++++++++++ play.html | 398 ++++++++++++++++++++++++++++++++++++++++++ play.js | 366 +++++++++++++++++++++++++++++++++++++++ rules.js | 583 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1444 insertions(+) create mode 100644 data.js create mode 100644 play.html create mode 100644 play.js create mode 100644 rules.js diff --git a/data.js b/data.js new file mode 100644 index 0000000..499b190 --- /dev/null +++ b/data.js @@ -0,0 +1,97 @@ +"use strict" + +const data = { + + cards: [ + null, + { side: "Versailles", ops: 1, name: "Jules Decatel" }, + { side: "Versailles", ops: 1, name: "The Murder of Vincenzini" }, + { side: "Versailles", ops: 1, name: "Brassardiers" }, + { side: "Versailles", ops: 1, name: "Jules Ferry" }, + { side: "Versailles", ops: 1, name: "Le Figaro" }, + { side: "Versailles", ops: 1, name: "Général Louis Valentin" }, + { side: "Versailles", ops: 1, name: "Général Espivent" }, + { side: "Versailles", ops: 1, name: "Les Amis de l'Ordre" }, + { side: "Versailles", ops: 2, name: "Socialist Newspaper Ban" }, + { side: "Versailles", ops: 2, name: "Fortification of Mont-Valérien" }, + { side: "Versailles", ops: 2, name: "Adolphe Thiers" }, + { side: "Versailles", ops: 2, name: "Otto von Bismarck" }, + { side: "Versailles", ops: 2, name: "Général Ernest de Cissey" }, + { side: "Versailles", ops: 2, name: "Colonel de Lochner" }, + { side: "Versailles", ops: 2, name: "Jules Favre" }, + { side: "Versailles", ops: 2, name: "Hostage Decree" }, + { side: "Versailles", ops: 4, name: "Maréchal Macmahon" }, + { side: "Commune", ops: 1, name: "Paule Minck" }, + { side: "Commune", ops: 1, name: "Walery Wroblewski" }, + { side: "Commune", ops: 1, name: "Banque de France" }, + { side: "Commune", ops: 1, name: "Le Réveil" }, + { side: "Commune", ops: 1, name: "Execution of Generals" }, + { side: "Commune", ops: 1, name: "Les Cantinières" }, + { side: "Commune", ops: 1, name: "Eugène Protot" }, + { side: "Commune", ops: 1, name: "Paul Cluseret" }, + { side: "Commune", ops: 2, name: "Gaston Crémieux" }, + { side: "Commune", ops: 2, name: "Luise Michel" }, + { side: "Commune", ops: 2, name: "Jaroslav Dombrowski" }, + { side: "Commune", ops: 2, name: "Raoul Rigault" }, + { side: "Commune", ops: 2, name: "Karl Marx" }, + { side: "Commune", ops: 2, name: "Blanquists" }, + { side: "Commune", ops: 2, name: "Général Lullier" }, + { side: "Commune", ops: 2, name: "Jules Vallès" }, + { side: "Commune", ops: 4, name: "Charles Delescluze" }, + { side: "Neutral", ops: 3, name: "Conciliation" }, + { side: "Neutral", ops: 3, name: "Georges Clemenceau" }, + { side: "Neutral", ops: 3, name: "Archbishop Georges Darboy" }, + { side: "Neutral", ops: 3, name: "Victor Hugo" }, + { side: "Neutral", ops: 3, name: "Léon Gambetta" }, + { side: "Neutral", ops: 3, name: "Elihu Washburne" }, + { side: "Neutral", ops: 3, name: "Freemason Parade" }, + { side: "Objective", ops: 0, name: "Paris Cannons", region: "Butte Montmartre" }, + { side: "Objective", ops: 0, name: "Aux Barricades!", region: "Buttes-Aux-Cailles" }, + { side: "Objective", ops: 0, name: "Commune's Stronghold", region: "Père Lachaise" }, + { side: "Objective", ops: 0, name: "Fighting in Issy Village", region: "Fort d'Issy" }, + { side: "Objective", ops: 0, name: "Battle of Mont-Valérien", region: "Mont-Valérien" }, + { side: "Objective", ops: 0, name: "Raid on Château de Vincennes", region: "Château de Vincennes" }, + { side: "Objective", ops: 0, name: "Revolution in the Press", region: "Press" }, + { side: "Objective", ops: 0, name: "Pius IX", region: "Catholic Church" }, + { side: "Objective", ops: 0, name: "Socialist International", region: "Social Movements" }, + { side: "Objective", ops: 0, name: "Royalists Dissension", region: "Royalists" }, + { side: "Objective", ops: 0, name: "Rise of Republicanism", region: "Republicans" }, + { side: "Objective", ops: 0, name: "Legitimacy", region: "National Assembly" }, + ], + + space_names: [ + "Red Cube Pool", + "Red Crisis Track Start", + "Red Crisis Track Escalation", + "Red Crisis Track Tension", + "Red Crisis Track Final Crisis", + "Red Bonus Cubes 1", + "Red Bonus Cubes 2", + "Red Bonus Cubes 3", + "Blue Cube Pool", + "Blue Crisis Track Start", + "Blue Crisis Track Escalation", + "Blue Crisis Track Tension", + "Blue Crisis Track Final Crisis", + "Blue Bonus Cubes 1", + "Blue Bonus Cubes 2", + "Blue Bonus Cubes 3", + "Royalists", + "National Assembly", + "Republicans", + "Catholic Church", + "Press", + "Social Movements", + "Mont-Valérien", + "Butte Montmartre", + "Fort d'Issy", + "Butte-Aux-Cailles", + "Père Lachaise", + "Château de Vincennes", + "Prussian Occupied Territory", + "Versailles HQ", + ], +} + +if (typeof module !== 'undefined') + module.exports = data diff --git a/play.html b/play.html new file mode 100644 index 0000000..fce9c2c --- /dev/null +++ b/play.html @@ -0,0 +1,398 @@ + + + + + + +RED FLAG OVER PARIS + + + + + + + + + + +
+ + + +
+
+ +
+
+
+
+
+
+ + + +
+ +
+
+
+
+
+
+
+
+
+
+
+
+ +
+ +
+ + + + diff --git a/play.js b/play.js new file mode 100644 index 0000000..c3abff4 --- /dev/null +++ b/play.js @@ -0,0 +1,366 @@ +"use strict" + +const space_count = data.space_names.length + +let layout = [] +let space_layout_cube = [] +let space_layout_disc = [] + +let ui = { + cards: [ null ], + cubes: [], + discs: [], + spaces: [], + red_momentum: document.getElementById("red_momentum"), + blue_momentum: document.getElementById("blue_momentum"), + political_vp: document.getElementById("political_vp"), + military_vp: document.getElementById("military_vp"), + round_marker: document.getElementById("round_marker"), +} + +// :r !python3 tools/genboxes.py +const boxes = { + "Royalists": [318,1711,506,505], + "Republicans": [1960,1712,504,504], + "Catholic Church": [318,3146,505,505], + "Social Movements": [1960,3146,506,505], + "Fort d'Issy": [3374,3172,506,506], + "Butte-Aux-Cailles": [4153,2642,505,505], + "Père Lachaise": [4824,2364,506,505], + "Château de Vincennes": [5369,2599,504,507], + "Press": [1176,2908,447,448], + "National Assembly": [1168,1984,447,447], + "Butte Montmartre": [4208,1842,447,444], + "Mont-Valérien": [2868,2028,447,448], + "Versailles HQ": [2646,3398,404,380], + "Prussian Occupied Territory": [5190,1413,718,346], + "Red Crisis Track Start": [3928,244,361,448], + "Red Crisis Track Escalation": [4289,244,334,448], + "Red Crisis Track Tension": [4623,244,332,448], + "Red Crisis Track Final Crisis": [4955,244,332,448], + "Blue Crisis Track Start": [1728,244,354,446], + "Blue Crisis Track Escalation": [1394,244,334,446], + "Blue Crisis Track Tension": [1061,244,333,446], + "Blue Crisis Track Final Crisis": [728,244,333,446], + "Blue Objective Card": [479,3983,1088,218], + "Red Objective Card": [4454,3984,1088,218], + "Red Cube Pool": [4498,790,791,219], + "Blue Cube Pool": [720,790,791,219], + "Red Bonus Cubes 1": [4289,89,334,155], + "Red Bonus Cubes 2": [4623,89,332,155], + "Red Bonus Cubes 3": [4955,89,332,155], + "Blue Bonus Cubes 1": [1394,89,334,155], + "Blue Bonus Cubes 2": [1061,89,333,155], + "Blue Bonus Cubes 3": [728,89,333,155], +} + +function is_card_action(action, card) { + if (view.actions && view.actions[action] && view.actions[action].includes(card)) + return true + return false +} + +function is_cube_action(i) { + if (view.actions && view.actions.cube && view.actions.cube.includes(i)) + return true + return false +} + +function is_disc_action(i) { + if (view.actions && view.actions.disc && view.actions.disc.includes(i)) + return true + return false +} + +function is_space_action(i) { + if (view.actions && view.actions.space && view.actions.space.includes(i)) + return true + return false +} + +function on_blur(evt) { + document.getElementById("status").textContent = "" +} + +function on_focus_space(evt) { + document.getElementById("status").textContent = evt.target.my_name +} + +function on_click_space(evt) { + if (evt.button === 0) { + if (send_action('space', evt.target.my_space)) + evt.stopPropagation() + } +} + +function on_click_cube(evt) { + if (evt.button === 0) { + if (send_action('cube', evt.target.my_cube)) + evt.stopPropagation() + } +} + +function on_click_disc(evt) { + if (evt.button === 0) { + if (send_action('disc', evt.target.my_disc)) + evt.stopPropagation() + } +} + +function build_user_interface() { + let elt + + for (let c = 1; c <= 41 + 12; ++c) { + elt = ui.cards[c] = document.createElement("div") + elt.className = `card card_${c}` + elt.my_card = c + elt.addEventListener("click", on_click_card) + } + + for (let i = 0; i < 36; ++i) { + elt = ui.cubes[i] = document.createElement("div") + if (i < 18) + elt.className = "piece cube red" + else + elt.className = "piece cube blue" + elt.my_cube = i + elt.addEventListener("mousedown", on_click_cube) + document.getElementById("pieces").appendChild(elt) + } + + for (let i = 0; i < 4; ++i) { + elt = ui.discs[i] = document.createElement("div") + if (i < 2) + elt.className = "piece disc red" + else + elt.className = "piece disc blue" + elt.my_disc = i + elt.addEventListener("mousedown", on_click_disc) + } + + for (let i = 0; i < space_count; ++i) { + let name = data.space_names[i] + let r = boxes[name] + elt = ui.spaces[i] = document.createElement("div") + elt.className = "space" + elt.my_space = i + elt.my_name = name + elt.addEventListener("mousedown", on_click_space) + elt.addEventListener("mouseenter", on_focus_space) + elt.addEventListener("mouseleave", on_blur) + let bw = 8 + let x = Math.round(r[0] / 4) + let y = Math.round(r[1] / 4) + let w = Math.round(r[2] / 4) + let h = Math.round(r[3] / 4) + elt.style.top = y + "px" + elt.style.left = x + "px" + elt.style.width = (w - bw * 2) + "px" + elt.style.height = (h - bw * 2) + "px" + space_layout_cube[i] = { x: x + Math.round(w/2), y: y + Math.round(h*1/2) } + space_layout_disc[i] = { x: x + w, y: y + h } + document.getElementById("spaces").appendChild(elt) + } +} + +function layout_cubes(list, xorig, yorig) { + const dx = 20 + const dy = 11 + if (list.length > 0) { + let ncol = Math.round(Math.sqrt(list.length)) + let nrow = Math.ceil(list.length / ncol) + function place_cube(row, col, e, z) { + let x = xorig - (row * dx - col * dx) - 18 + (nrow-ncol) * 6 + let y = yorig - (row * dy + col * dy) - 28 + (nrow-1) * 8 + e.style.left = x + "px" + e.style.top = y + "px" + e.style.zIndex = z + } + let z = 50 + let i = 0 + for (let row = 0; row < nrow; ++row) + for (let col = 0; col < ncol && i < list.length; ++col) + place_cube(row, col, list[list.length-(++i)], z--) + } +} + +function layout_disc(s, disc) { + if (s > 0) + disc.classList.remove("hide") + else + disc.classList.add("hide") + disc.style.left = (space_layout_disc[s].x - 50 - 12) + "px" + disc.style.top = (space_layout_disc[s].y - 20 - 8) + "px" +} + +function on_focus_card_tip(card_number) { + document.getElementById("tooltip").className = "card card_" + card_number +} + +function on_blur_card_tip() { + document.getElementById("tooltip").classList = "card hide" +} + +function sub_card_name(match, p1, offset, string) { + let c = p1 | 0 + let n = data.cards[c].name + return `${n}` +} + +function on_log(text) { + let p = document.createElement("div") + + if (text.match(/^>/)) { + text = text.substring(1) + p.className = 'i' + } + + text = text.replace(/&/g, "&") + text = text.replace(//g, ">") + text = text.replace(/#(\d+)/g, sub_card_name) + + if (text.match(/^\.h1/)) { + text = text.substring(4) + p.className = 'h1' + } + + if (text.match(/^\.h2/)) { + text = text.substring(4) + if (text === 'Commune') + p.className = 'h2 commune' + else if (text === 'Versailles') + p.className = 'h2 versailles' + else + p.className = 'h2' + } + + p.innerHTML = text + return p +} + +function on_update() { + if (view.active_card) + document.getElementById("active_card").className = `card card_${view.active_card}` + else + document.getElementById("active_card").className = `card card_strategy_back` + + if (view.initiative === "Commune") + document.getElementById("commune_info").textContent = "\u2756" + else + document.getElementById("commune_info").textContent = "" + if (view.initiative === "Versailles") + document.getElementById("versailles_info").textContent = "\u2756" + else + document.getElementById("versailles_info").textContent = "" + + ui.round_marker.className = `piece pawn round${view.round}` + ui.red_momentum.className = `piece cylinder red m${view.red_momentum}` + ui.blue_momentum.className = `piece cylinder blue m${view.blue_momentum}` + ui.military_vp.className = `piece cylinder purple vp${5+view.military_vp}` + ui.political_vp.className = `piece cylinder orange vp${5+view.political_vp}` + + document.getElementById("hand").replaceChildren() + if (view.objective) + document.getElementById("hand").appendChild(ui.cards[view.objective]) + if (view.hand) + for (let c of view.hand) + document.getElementById("hand").appendChild(ui.cards[c]) + + for (let i = 0; i < space_count; ++i) + layout[i] = [] + for (let i = 0; i < 36; ++i) { + layout[view.cubes[i]].push(ui.cubes[i]) + ui.cubes[i].classList.toggle("action", is_cube_action(i)) + } + for (let i = 0; i < space_count; ++i) { + layout_cubes(layout[i], space_layout_cube[i].x, space_layout_cube[i].y) + ui.spaces[i].classList.toggle("action", is_space_action(i)) + } + for (let i = 0; i < 4; ++i) { + layout_disc(view.discs[i], ui.discs[i]) + ui.discs[i].classList.toggle("action", is_cube_action(i)) + } + + for (let i = 1; i < ui.cards.length; ++i) { + ui.cards[i].classList.toggle("action", is_card_action('card', i)) + } + + action_button("commune", "Commune") + action_button("versailles", "Versailles") + action_button("undo", "Undo") +} + +/* CARD ACTION MENU */ + +let current_popup_card = 0 + +function show_popup_menu(evt, list) { + document.querySelectorAll("#popup div").forEach(e => e.classList.remove('enabled')) + for (let item of list) { + let e = document.getElementById("menu_" + item) + e.classList.add('enabled') + } + let popup = document.getElementById("popup") + popup.style.display = 'block' + popup.style.left = (evt.clientX-50) + "px" + popup.style.top = (evt.clientY-12) + "px" + ui.cards[current_popup_card].classList.add("selected") +} + +function hide_popup_menu() { + let popup = document.getElementById("popup") + popup.style.display = 'none' + if (current_popup_card) { + ui.cards[current_popup_card].classList.remove("selected") + current_popup_card = 0 + } +} + +function on_card_event() { + if (send_action('card_event', current_popup_card)) + hide_popup_menu() +} + +function on_card_ops() { + if (send_action('card_ops', current_popup_card)) + hide_popup_menu() +} + +function on_card_use_discarded() { + if (send_action('card_use_discarded', current_popup_card)) + hide_popup_menu() +} + +function on_card_advance_momentum() { + if (send_action('card_advance_momentum', current_popup_card)) + hide_popup_menu() +} + +function on_click_card(evt) { + if (evt.button === 0) { + if (view.actions) { + let card = evt.target.my_card + if (is_card_action('card', card)) { + send_action('card', card) + } else { + let menu = [] + if (is_card_action('card_event', card)) + menu.push('card_event') + if (is_card_action('card_ops', card)) + menu.push('card_ops') + if (is_card_action('card_use_discarded', card)) + menu.push('card_use_discarded') + if (is_card_action('card_advance_momentum', card)) + menu.push('card_advance_momentum') + if (menu.length > 0) { + current_popup_card = card + show_popup_menu(evt, menu) + } + } + } + } +} + +build_user_interface() +scroll_with_middle_mouse("main") diff --git a/rules.js b/rules.js new file mode 100644 index 0000000..51d2753 --- /dev/null +++ b/rules.js @@ -0,0 +1,583 @@ +"use strict" + +const data = require("./data") + +const RED_CUBE_POOL = 0 +const RED_CRISIS_TRACK = [1, 2, 3, 4] +const RED_BONUS_CUBES = [ 5, 6, 7 ] +const BLUE_CUBE_POOL = 8 +const BLUE_CRISIS_TRACK = [9, 10, 11, 12] +const BLUE_BONUS_CUBES = [ 13, 14, 15 ] +const S_ROYALISTS = 16 +const S_NATIONAL_ASSEMBLY = 17 +const S_REPUBLICANS = 18 +const S_CATHOLIC_CHURCH = 19 +const S_PRESS = 20 +const S_SOCIAL_MOVEMENTS = 21 +const S_MONT_VALERIEN = 22 +const S_BUTTE_MONTMARTRE = 23 +const S_FORT_D_ISSY = 24 +const S_BUTTE_AUX_CAILLES = 25 +const S_PERE_LACHAISE = 26 +const S_CHATEAU_DE_VINCENNES = 27 +const S_PRUSSIAN_OCCUPIED_TERRITORY = 28 +const S_VERSAILLES_HQ = 29 + +var game, view + +var states = {} + +exports.scenarios = [ "Standard" ] + +exports.roles = [ "Commune", "Versailles" ] + +exports.action = function (state, current, action, arg) { + game = state + if (action in states[game.state]) { + states[game.state][action](arg, current) + } else { + if (action === "undo" && game.undo && game.undo.length > 0) + pop_undo() + else + throw new Error("Invalid action: " + action) + } + return game +} + +exports.resign = function (state, current) { + game = state + if (game.state !== "game_over") { + log_br() + log(`${current} resigned.`) + game.state = "game_over" + game.active = null + game.state = "game_over" + game.result = (current === "Commune" ? "Versailles" : "Commune") + game.victory = current + " resigned." + } + return game +} + +exports.is_checkpoint = function (a, b) { + return a.round !== b.round +} + +exports.view = function(state, current) { + game = state + + view = { + log: game.log, + prompt: null, + actions: null, + + round: game.round, + initiative: game.initiative, + political_vp: game.political_vp, + military_vp: game.military_vp, + + red_hand: game.red_hand.length, + blue_hand: game.blue_hand.length, + red_momentum: game.red_momentum, + blue_momentum: game.blue_momentum, + + discs: game.discs, + cubes: game.cubes, + + active_card: game.active_card, + hand: 0, + objective: 0 + } + + if (current === "Commune") { + view.hand = game.red_hand + view.objective = game.red_objective + } + if (current === "Versailles") { + view.hand = game.blue_hand + view.objective = game.blue_objective + } + + if (game.state === "game_over") { + view.prompt = game.victory + } else if (current === "Observer" || (game.active !== current && game.active !== "Both")) { + if (states[game.state]) { + let inactive = states[game.state].inactive || game.state + view.prompt = `Waiting for ${game.active} to ${inactive}...` + } else { + view.prompt = "Unknown state: " + game.state + } + } else { + view.actions = {} + if (states[game.state]) + states[game.state].prompt(current) + else + view.prompt = "Unknown state: " + game.state + if (view.actions.undo === undefined) { + if (game.undo && game.undo.length > 0) + view.actions.undo = 1 + else + view.actions.undo = 0 + } + } + + return view +} + +exports.setup = function (seed, scenario, options) { + game = { + seed: seed, + log: [], + undo: [], + active: "Both", + state: "choose_objective_card", + + round: 1, + initiative: null, + political_vp: 0, + military_vp: 0, + red_momentum: 0, + blue_momentum: 0, + + strategy_deck: [], + objective_deck: [], + + red_hand: [ 34 ], + red_objective: 0, + blue_hand: [ 17 ], + blue_objective: 0, + + cubes: [ + // red cubes + RED_CRISIS_TRACK[0], + RED_CRISIS_TRACK[0], + RED_CRISIS_TRACK[0], + RED_CRISIS_TRACK[1], + RED_CRISIS_TRACK[1], + RED_CRISIS_TRACK[2], + RED_CRISIS_TRACK[2], + RED_CRISIS_TRACK[3], + RED_CRISIS_TRACK[3], + RED_BONUS_CUBES[0], + RED_BONUS_CUBES[0], + RED_BONUS_CUBES[1], + RED_BONUS_CUBES[1], + RED_BONUS_CUBES[2], + RED_BONUS_CUBES[2], + S_PRESS, + S_SOCIAL_MOVEMENTS, + S_PERE_LACHAISE, + + // blue cubes + BLUE_CRISIS_TRACK[0], + BLUE_CRISIS_TRACK[1], + BLUE_CRISIS_TRACK[1], + BLUE_CRISIS_TRACK[2], + BLUE_CRISIS_TRACK[3], + BLUE_CRISIS_TRACK[3], + BLUE_BONUS_CUBES[0], + BLUE_BONUS_CUBES[1], + BLUE_BONUS_CUBES[2], + BLUE_BONUS_CUBES[2], + BLUE_CUBE_POOL, + BLUE_CUBE_POOL, + BLUE_CUBE_POOL, + BLUE_CUBE_POOL, + BLUE_CUBE_POOL, + BLUE_CUBE_POOL, + S_ROYALISTS, + S_PRESS, + ], + + discs: [ 0, 0, 0, 0 ], + + active_card: 0, + count: 0, + } + + log_h1("Red Flag Over Paris") + log_h1("Round 1") + + for (let i = 1; i <= 41; ++i) + if (i !== 17 && i !== 34) + game.strategy_deck.push(i) + + for (let i = 42; i <= 53; ++i) + game.objective_deck.push(i) + + shuffle(game.strategy_deck) + shuffle(game.objective_deck) + + for (let i = 0; i < 4; ++i) { + game.red_hand.push(game.strategy_deck.pop()) + game.blue_hand.push(game.strategy_deck.pop()) + } + + for (let i = 0; i < 2; ++i) { + game.red_hand.push(game.objective_deck.pop()) + game.blue_hand.push(game.objective_deck.pop()) + } + + return game +} + +// === GAME STATES === + +function is_objective_card(c) { + return c >= 42 && c <= 53 +} + +function is_strategy_card(c) { + return !is_objective_card(c) && c !== 17 && c !== 34 +} + +function enemy_player() { + if (game.active === "Commune") + return "Versailles" + return "Commune" +} + +function player_hand(current) { + if (current === "Commune") + return game.red_hand + return game.blue_hand +} + +function can_play_event(c) { + let side = data.cards[c].side + if (side === game.active || side === "Neutral") + return true + return false +} + +function can_advance_momentum() { + if (game.active === "Commune") + return game.red_momentum < 3 + return game.blue_momentum < 3 +} + +states.choose_objective_card = { + inactive: "choose an objective card", + prompt(current) { + view.prompt = "Choose an Objective card." + for (let c of player_hand(current)) { + if (is_objective_card(c)) { + gen_action("card", c) + } + } + }, + card(c, current) { + if (current === "Commune") { + game.red_objective = c + game.red_hand = game.red_hand.filter(c => !is_objective_card(c)) + } else { + game.blue_objective = c + game.blue_hand = game.blue_hand.filter(c => !is_objective_card(c)) + } + if (game.red_objective > 0 && game.blue_objective > 0) + goto_initiative_phase() + else if (game.red_objective > 0) + game.active = "Versailles" + else if (game.blue_objective > 0) + game.active = "Commune" + else + game.active = "Both" + }, +} + +function goto_initiative_phase() { + game.active = "Commune" + game.state = "initiative_phase" +} + +states.initiative_phase = { + inactive: "decide player order", + prompt() { + view.prompt = "Decide player order." + view.actions.commune = 1 + view.actions.versailles = 1 + }, + commune() { + log("Initiative: Commune") + game.initiative = "Commune" + game.active = game.initiative + goto_strategy_phase() + }, + versailles() { + log("Initiative: Versailles") + game.initiative = "Versailles" + game.active = game.initiative + goto_strategy_phase() + }, +} + +function goto_strategy_phase() { + clear_undo() + log_h2(game.active) + game.state = "strategy_phase" +} + +function resume_strategy_phase() { + game.active = enemy_player() + goto_strategy_phase() +} + +states.strategy_phase = { + inactive: "play a card", + prompt() { + view.prompt = "Play a card." + for (let c of player_hand(game.active)) { + console.log(game.active, "hand", c) + if (is_strategy_card(c)) { + if (can_play_event(c)) + gen_action("card_event", c) + gen_action("card_ops", c) + if (game.active_card > 0 && can_play_event(game.active_card)) + gen_action("card_use_discarded", c) + if (can_advance_momentum()) + gen_action("card_advance_momentum", c) + } + } + }, + card_event(c) { + push_undo() + log(`Played #${c} for event.`) + array_remove_item(player_hand(game.active), c) + game.active_card = c + goto_play_event() + }, + card_ops(c) { + push_undo() + log(`Played #${c} for ${data.cards[c].ops} ops.`) + array_remove_item(player_hand(game.active), c) + game.active_card = c + game.count = data.cards[c].ops + game.state = "operations" + }, + card_advance_momentum(c) { + push_undo() + log(`Played #${c} to advance momentum.`) + array_remove_item(player_hand(game.active), c) + game.active_card = c + if (game.active === "Commune") + game.red_momentum += 1 + else + game.blue_momentum += 1 + // TODO: momentum trigger + resume_strategy_phase() + }, + card_use_discarded(c) { + push_undo() + log(`Discarded #${c} to play #${game.active_card}.`) + let old_c = game.active_card + array_remove_item(player_hand(game.active), c) + game.active_card = c + goto_play_event(old_c) + }, +} + +function goto_play_event(c) { + switch (c) { + // TODO + } + resume_strategy_phase() +} + +// === COMMON LIBRARY === + +function gen_action(action, argument) { + if (argument !== undefined) { + if (!(action in view.actions)) + view.actions[action] = [] + set_add(view.actions[action], argument) + } else { + view.actions[action] = 1 + } +} + +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 shuffle(list) { + for (let i = list.length - 1; i > 0; --i) { + let j = random(i + 1) + let tmp = list[j] + list[j] = list[i] + list[i] = tmp + } +} + +// remove item at index (faster than splice) +function array_remove(array, index) { + let n = array.length + for (let i = index + 1; i < n; ++i) + array[i - 1] = array[i] + array.length = n - 1 + return array +} + +// insert item at index (faster than splice) +function array_insert(array, index, item) { + for (let i = array.length; i > index; --i) + array[i] = array[i - 1] + array[index] = item + return array +} + +function array_remove_item(array, item) { + let i = array.indexOf(item) + if (i >= 0) + array_remove(array, i) +} + +function set_clear(set) { + set.length = 0 +} + +function set_has(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return true + } + return false +} + +function set_add(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return set + } + return array_insert(set, a, item) +} + +function set_delete(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return array_remove(set, m) + } + return set +} + +function set_toggle(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return array_remove(set, m) + } + return array_insert(set, a, item) +} + + +// Fast deep copy for objects without cycles +function object_copy(original) { + if (Array.isArray(original)) { + let n = original.length + let copy = new Array(n) + for (let i = 0; i < n; ++i) { + let v = original[i] + if (typeof v === "object" && v !== null) + copy[i] = object_copy(v) + else + copy[i] = v + } + return copy + } else { + let copy = {} + for (let i in original) { + let v = original[i] + if (typeof v === "object" && v !== null) + copy[i] = object_copy(v) + else + copy[i] = v + } + return copy + } +} + +function clear_undo() { + if (game.undo.length > 0) + game.undo = [] +} + +function push_undo() { + let copy = {} + for (let k in game) { + let v = game[k] + if (k === "undo") + continue + else if (k === "log") + v = v.length + else if (typeof v === "object" && v !== null) + v = object_copy(v) + copy[k] = v + } + game.undo.push(copy) +} + +function pop_undo() { + let save_log = game.log + let save_undo = game.undo + game = save_undo.pop() + save_log.length = game.log + game.log = save_log + game.undo = save_undo +} + +function log(msg) { + game.log.push(msg) +} + +function log_br() { + if (game.log.length > 0 && game.log[game.log.length-1] !== "") + game.log.push("") +} + +function logi(msg) { + game.log.push(">" + msg) +} + +function log_h1(msg) { + log_br() + log(".h1 " + msg) + log_br() +} + +function log_h2(msg) { + log_br() + log(".h2 " + msg) + log_br() +} -- cgit v1.2.3