'use strict'; import { CardId, Effect, EngineNode, EventCard, FactionId, Front, FrontId, FunctionNode, Game, Icon, LeafNode, Player, PlayerCard, SeqNode, States, View, } from './types'; import data, { ANARCHIST, COMMUNIST, MODERATE, ANARCHISTS_ID, COMMUNISTS_ID, MODERATES_ID, // LIBERTY, // COLLECTIVIZATION, // GOVERNMENT, // SOVIET_SUPPORT, // FOREIGN_AID, // MORALE_BONUS, // TEAMWORK_BONUS, // OFF, LIBERTY, CLOSEST_TO_DEFEAT, CLOSEST_TO_VICTORY, COLLECTIVIZATION, GOVERNMENT, INITIATIVE_PLAYER, SOVIET_SUPPORT, FOREIGN_AID, ON, PLAYER_WITH_MOST_HERO_POINTS, SELF, ANY, TEAMWORK_BONUS, MORALE_BONUS, OFF, VICTORY, DEFEAT, FRONTS, create_effect, AWAY_FROM_CENTER, TOWARDS_CENTER, ARCHIVES_MEDALLION_ID, INTELLIGENCE_MEDALLION_ID, // VOLUNTEERS_MEDALLION_ID, ORGANIZATION_MEDALLION_ID, STRATEGY_MEDALLION_ID, PROPAGANDA_MEDALLION_ID, VOLUNTEERS_MEDALLION_ID, ALL_PLAYERS, OTHER_PLAYERS, MADRID, SOUTHERN, COMMUNIST_EXTRA_HERO_POINT, LIBERTY_OR_COLLECTIVIZATION, ANARCHIST_EXTRA_HERO_POINT, ARAGON, FASCIST_ID, // StaticData, // PLAYER_WITH_MOST_HERO_POINTS, } from './data'; // interface State { // inactive: string; // prompt: () => void; // } const OBSERVER = 'Observer'; const states = {} as States; let game = {} as Game; // = null var view = {} as View; // = null // export const ANARCHIST = 'Anarchist' as Player; // export const COMMUNIST = 'Communist' as Player; // export const MODERATE = 'Moderate' as Player; const role_ids: FactionId[] = [ANARCHISTS_ID, COMMUNISTS_ID, MODERATES_ID]; const faction_player_map: Record = { a: ANARCHIST, c: COMMUNIST, m: MODERATE, }; const player_faction_map: Record = { [ANARCHIST]: ANARCHISTS_ID, [COMMUNIST]: COMMUNISTS_ID, [MODERATE]: MODERATES_ID, }; const front_names: Record = { a: 'Aragon Front', m: 'Madrid Front', n: 'Northern Front', s: 'Southern Front', d: 'the Front closest to Defeat', v: 'the Front closest to Victory', }; const bonus_names: string[] = ['Morale Bonus', 'Teamwork Bonus']; const { cards, medallions, // fronts, tracks, } = data; const bonuses = [MORALE_BONUS, TEAMWORK_BONUS]; const faction_cards = { [ANARCHISTS_ID]: make_list(37, 54) as CardId[], [COMMUNISTS_ID]: make_list(19, 36) as CardId[], [MODERATES_ID]: make_list(1, 18) as CardId[], }; const fascist_decks = { 1: make_list(55, 72) as CardId[], 2: make_list(73, 90) as CardId[], 3: make_list(91, 108) as CardId[], }; export const scenarios = ['Standard']; export const roles: Player[] = [ANARCHIST, COMMUNIST, MODERATE]; function gen_action(action: string, argument?: number | string) { if (argument === undefined) { view.actions![action] = 1; } else { if (!(action in view.actions)) view.actions[action] = []; view.actions[action].push(argument); } } function gen_action_blank_marker(marker_id: number) { gen_action('blank_marker', marker_id); } function gen_action_bonus(bonus_id: number) { gen_action('bonus', bonus_id); } function gen_action_card(card_id: CardId) { gen_action('card', card_id); } function gen_action_front(front_id: string) { gen_action('front', front_id); } function gen_action_medallion(medallion_id: number) { gen_action('medallion', medallion_id); } function gen_action_standee(track_id: number) { gen_action('standee', track_id); } function gen_spend_hero_points() { const faction = get_active_faction(); const can_spend_hp = game.faction_turn === faction && game.hero_points[faction] > 0; if (can_spend_hp) { gen_action('spend_hp'); } } // function gen_action_space(space) { // gen_action('space', space); // } // function gen_action_piece(piece) { // gen_action('piece', piece); // } // function gen_action_card(card) { // gen_action('card', card); // } export function action( state: Game, player: Player, action: string, arg: unknown ) { if (action !== 'undo') { state.undo = push_undo(); } game = state; let S = states[game.state]; if (action in S) S[action](arg, player); else if (action === 'undo' && game.undo && game.undo.length > 0) pop_undo(); else throw new Error('Invalid action: ' + action); return game; } // #region ENGINE const leaf_node = 'l'; const seq_node = 's'; const function_node = 'f'; const resolved = 1; function create_leaf_node( state: string, faction: FactionId | 'None', args?: any ): LeafNode { return { t: leaf_node, s: state, p: faction, a: args, r: 0, }; } function create_function_node(func_name: string, args?: any): FunctionNode { return { t: function_node, f: func_name, a: args, r: 0, }; } function create_seq_node(children: EngineNode[]): SeqNode { return { t: seq_node, c: children, }; } // Use to force confirm question if possible to undo function checkpoint() { if (game.undo.length > 0) { insert_after_active_node( create_leaf_node('confirm_turn', get_active_faction()) ); } resolve_active_and_proceed(); } function setup_bag_of_glory() { game.engine = [ create_leaf_node('add_glory', game.initiative), create_function_node('end_of_turn'), ]; next(); } function setup_choose_card() { const player_order = get_player_order(); game.engine = player_order.map((faction_id) => create_leaf_node('choose_card', faction_id) ); game.engine.push(create_function_node('setup_player_turn')); next(); } function setup_final_bid() { log_h1('Final Bid'); const player_order = get_player_order(); game.engine = player_order.map((faction_id) => create_leaf_node('choose_final_bid', faction_id) ); game.engine.push(create_function_node('checkpoint')); game.engine.push(create_function_node('resolve_final_bid')); game.engine.push(create_function_node('setup_choose_card')); next(); } function setup_player_turn() { const next_faction = game.first_player === null ? get_player_order()[0] : get_next_faction(get_active_faction()); if (game.first_player === null) { game.first_player = next_faction; } // const player_order = get_player_order(); // game.engine = player_order.map((faction_id) => // create_seq_node([ // create_function_node('start_of_player_turn', { f: faction_id }), // create_leaf_node('player_turn', faction_id), // ]) // ); game.engine = [ create_function_node('start_of_player_turn', { f: next_faction }), create_leaf_node('player_turn', next_faction), create_function_node('end_of_player_turn', { f: next_faction }), ]; // game.engine.push(create_function_node('resolve_fascist_test')); // game.engine.push(create_function_node('setup_bag_of_glory')); next(); } function end_of_player_turn() { if (get_next_faction(get_active_faction()) === game.first_player) { game.engine = [ create_function_node('resolve_fascist_test'), create_function_node('setup_bag_of_glory'), ]; } else { game.engine = [create_function_node('setup_player_turn')]; } next(); } function start_of_player_turn() { const args = get_active_node_args(); const player = faction_player_map[args.f]; log_h2(player, player); game.faction_turn = args.f; resolve_active_and_proceed(); } const engine_functions: Record = { check_activate_icon, checkpoint, end_of_player_turn, end_of_turn, setup_bag_of_glory, setup_choose_card, setup_final_bid, setup_player_turn, start_of_player_turn, start_year, resolve_fascist_test, resolve_final_bid, // Unique card effects card1_event2, card3_event2, card10_event2, card16_event2, card17_event3, card20_event3, card22_event3, card23_event1, card26_event1, card29_event2, card35_event2, card42_event3, card45_event2, card46_event3, card50_event2, card53_event2, card54_event1, setup_return_card_from_trash, }; function get_active( engine: EngineNode[] ): { parent: EngineNode[]; node: FunctionNode | LeafNode } | null { for (let i of engine) { if ((i.t === leaf_node || i.t === function_node) && i.r !== resolved) { return { parent: engine, node: i }; } if (i.t === seq_node) { const next_child = get_active(i.c); if (next_child !== null) { return next_child; } } } return null; } function get_active_node( engine: EngineNode[] = game.engine ): FunctionNode | LeafNode | null { const a = get_active(engine); return a === null ? null : a.node; } function get_active_node_args(): T { const node = get_active_node(game.engine); if (node.t === leaf_node || node.t === function_node) { return node.a; } return null; } function update_active_node_args(args: T) { const node = get_active_node(game.engine); if (node.t === leaf_node || node.t === function_node) { node.a = { ...node.a, ...args, }; } } function insert_before_or_after_active_node( node: EngineNode, position: 'before' | 'after', engine: EngineNode[] = game.engine ) { const a = get_active(engine); if (a === null) { return; } const i = a.parent.indexOf(a.node); if (i >= 0) { array_insert(a.parent, i + (position == 'after' ? 1 : 0), node); } } function insert_after_active_node( node: EngineNode, engine: EngineNode[] = game.engine ) { insert_before_or_after_active_node(node, 'after', engine); } function insert_before_active_node( node: EngineNode, engine: EngineNode[] = game.engine ) { insert_before_or_after_active_node(node, 'before', engine); } function next() { const node = get_active_node(game.engine); if (node.t === function_node && engine_functions[node.f]) { const args = node.a; if (args) { engine_functions[node.f](args); } else { engine_functions[node.f](); } } else if (node.t === 'l') { game.state = node.s; // Control switches to another player and player can undo // so ask to confirm turn const current_active = game.active; const next_active = faction_player_map[node.p]; if (next_active !== current_active && game.undo.length > 0) { insert_before_active_node( create_leaf_node('confirm_turn', get_active_faction()) ); game.state = 'confirm_turn'; return; } game.active = next_active; } } function resolve_active_node(checkpoint = false) { const next_node = get_active_node(game.engine); if (next_node !== null) { next_node.r = resolved; } if (checkpoint) { clear_undo(); } } function resolve_active_and_proceed(checkpoint = false) { resolve_active_node(checkpoint); next(); } // #endregion // #region VIEW export { game_view as view }; function game_view(state: Game, current: Player | 'Observer') { game = state; const faction: FactionId | null = current === OBSERVER ? null : player_faction_map[current]; view = { engine: game.engine, // TODO: remove log: game.log, prompt: null, bag_of_glory: game.bag_of_glory, bonuses: game.bonuses, current, current_events: game.current_events, fronts: game.fronts, glory: game.glory, hand: faction === null ? [] : game.hands[faction], hero_points: game.hero_points, initiative: game.initiative, medallions: game.medallions, played_card: game.played_card, player_order: current === OBSERVER ? game.player_order : get_player_order(faction).map((id) => faction_player_map[id]), selectable_cards: game.selectable_cards, selected_cards: current === OBSERVER ? [] : game.selected_cards[faction], tableaus: game.tableaus, tracks: game.tracks, triggered_track_effects: game.triggered_track_effects, used_medallions: game.used_medallions, year: game.year, }; if (game.state === 'game_over') { view.prompt = game.victory; } else if (current !== game.active) { let inactive = states[game.state].inactive || game.state; view.prompt = `Waiting for ${game.active} to ${inactive}.`; } else { view.actions = {}; states[game.state].prompt(); if (game.undo && game.undo.length > 0) view.actions.undo = 1; else view.actions.undo = 0; } return view; } // #endregion // #region SETUP export function setup(seed: number, _scenario: string, _options: unknown) { // game.seed = seed; game = { seed: seed, state: null, active: ANARCHIST, active_abilities: [], bag_of_glory: [ANARCHISTS_ID, COMMUNISTS_ID, MODERATES_ID], blank_markers: [[], [], [], [], []], bonuses: [ON, ON], current_events: [], discard: { a: [], c: [], m: [], f: [], }, engine: [], faction_turn: null, fronts: { a: { value: -2, contributions: [], status: null, }, m: { value: -2, contributions: [], status: null, }, n: { value: -2, contributions: [], status: null, }, s: { value: -2, contributions: [], status: null, }, }, glory: [], first_player: null, hands: { a: [], c: [], m: [], }, hero_points: { a: 2, c: 2, m: 0, pool: 14, }, initiative: MODERATES_ID, medallions: { a: [], c: [], m: [], pool: [], }, played_card: null, player_order: [MODERATE], selectable_cards: [], selected_cards: { a: [], c: [], m: [], }, tableaus: { a: [], c: [], m: [], }, tracks: [5, 5, 6, 3, 3], trash: { a: [], c: [], m: [], }, triggered_track_effects: [], log: [], undo: [], used_medallions: [], top_of_events_deck: null, turn: 0, year: 0, }; // Randomly choose second player game.player_order.push(roles[random(2)]); // Remaining role is 3rd player game.player_order.push( game.player_order[1] === ANARCHIST ? COMMUNIST : ANARCHIST ); draw_medallions(); start_year(); return game; } function draw_hand_cards(faction_id: FactionId, count: number) { const deck = list_deck(faction_id); if (game.medallions[faction_id].includes(INTELLIGENCE_MEDALLION_ID)) { count++; } const drawn_cards = count; // Draw all remaining cards if (deck.length < count) { count = count - deck.length; game.hands[faction_id] = game.hands[faction_id].concat(deck); game.discard[faction_id] = []; } const log = drawn_cards === 1 ? `${get_player(faction_id)} draws 1 card` : `${get_player(faction_id)} draws ${drawn_cards} cards`; logi(log); for (let i = 0; i < count; i++) { const deck = list_deck(faction_id); game.hands[faction_id].push(draw_card(deck)); } } // #endregion function start_year() { game.year++; game.turn = 1; game.current_events = []; role_ids.forEach((role) => { draw_hand_cards(role, 5); }); start_turn(); } function start_turn() { log_h1(`Year ${game.year} - Turn ${game.turn}`); const cardId = draw_fascist_card(); game.current_events.push(cardId); const card = cards[cardId] as EventCard; log_h2('Fascist Event', 'fascist'); log(card.title); game.engine = card.effects.map((effect) => resolve_effect(effect)); if (game.year === 3 && game.turn === 4) { game.engine.push(create_function_node('setup_final_bid')); } else { game.engine.push(create_function_node('setup_choose_card')); } next(); // game.state = 'resolve_event'; // game.active = faction_player_map[game.initiative]; // game.state_data = { // current_effect: 0, // }; } // region STATES states.activate_icon = { inactive: 'activate an icon', prompt() { gen_spend_hero_points(); view.prompt = 'Choose an icon to activate'; const c = cards[game.played_card] as PlayerCard; for (const i of c.icons) { gen_action(i); } }, spend_hp() { resolve_spend_hp(); }, add_to_front() { insert_after_active_node( create_leaf_node('add_to_front', get_active_faction(), { t: ANY, v: get_icon_count_in_tableau('add_to_front'), }) ); resolve_active_and_proceed(); }, collectivization() { const count = get_icon_count_in_tableau('collectivization'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(COLLECTIVIZATION, count); } else { move_track(COLLECTIVIZATION, count); } resolve_active_and_proceed(); }, d_collectivization() { const count = -1 * get_icon_count_in_tableau('d_collectivization'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(COLLECTIVIZATION, count); } else { move_track(COLLECTIVIZATION, count); } resolve_active_and_proceed(); }, d_foreign_aid() { const count = -1 * get_icon_count_in_tableau('d_foreign_aid'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(FOREIGN_AID, count); } else { move_track(FOREIGN_AID, count); } resolve_active_and_proceed(); }, d_government() { const count = -1 * get_icon_count_in_tableau('d_government'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(GOVERNMENT, count); } else { move_track(GOVERNMENT, count); } resolve_active_and_proceed(); }, d_liberty() { const count = -1 * get_icon_count_in_tableau('d_liberty'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(LIBERTY, count); } else { move_track(LIBERTY, count); } resolve_active_and_proceed(); }, d_soviet_support() { const count = -1 * get_icon_count_in_tableau('d_soviet_support'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(SOVIET_SUPPORT, count); } else { move_track(SOVIET_SUPPORT, count); } resolve_active_and_proceed(); }, draw_card() { draw_hand_cards( get_active_faction(), get_icon_count_in_tableau('draw_card') ); resolve_active_and_proceed(); }, foreign_aid() { const count = get_icon_count_in_tableau('foreign_aid'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(FOREIGN_AID, count); } else { move_track(FOREIGN_AID, count); } resolve_active_and_proceed(); }, government() { const direction = game.active === COMMUNIST ? -1 : 1; const count = direction * get_icon_count_in_tableau('government'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(GOVERNMENT, count); } else { move_track(GOVERNMENT, count); } resolve_active_and_proceed(); }, government_to_center() { const direction = game.tracks[GOVERNMENT] >= 6 ? -1 : 1; move_track( GOVERNMENT, direction * get_icon_count_in_tableau('government_to_center') ); resolve_active_and_proceed(); }, liberty() { const count = get_icon_count_in_tableau('liberty'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(LIBERTY, count); } else { move_track(LIBERTY, count); } resolve_active_and_proceed(); }, soviet_support() { const count = get_icon_count_in_tableau('soviet_support'); if (can_use_medallion(ORGANIZATION_MEDALLION_ID)) { insert_use_organization_medallion_node(SOVIET_SUPPORT, count); } else { move_track(SOVIET_SUPPORT, count); } resolve_active_and_proceed(); }, teamwork_on() { game.bonuses[TEAMWORK_BONUS] = ON; resolve_active_and_proceed(); }, }; states.add_card_to_tableau = { inactive: 'add a card to their tableau', prompt() { gen_spend_hero_points(); view.prompt = 'Choose a card to add to your tableau'; const faction = get_active_faction(); for (const c of game.hands[faction]) { gen_action_card(c); } if (game.hands[faction].length === 0) { gen_action('skip'); } }, spend_hp() { resolve_spend_hp(); }, card(c: CardId) { const faction_id = get_active_faction(); const card = cards[c]; array_remove(game.hands[faction_id], game.hands[faction_id].indexOf(c)); game.tableaus[faction_id].push(c); logi( `${faction_player_map[faction_id]} adds ${card.title} to their tableau` ); resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.add_glory = { inactive: 'add tokens to the Bag of Glory', prompt() { gen_spend_hero_points(); view.prompt = 'Add tokens to the Bag of Glory'; gen_action('add_glory'); }, spend_hp() { resolve_spend_hp(); }, add_glory() { let number = 1; if (game.turn === 4) { number++; } add_glory(get_active_faction(), number); resolve_active_and_proceed(); }, }; states.add_to_front = { inactive: 'add strength to a Front', prompt() { gen_spend_hero_points(); const args = get_active_node_args(); const possible_fronts = get_fronts_to_add_to(args.t); view.prompt = possible_fronts.length === 1 ? `Add strength to ${front_names[possible_fronts[0]]}` : 'Add strength to a Front'; for (let f of possible_fronts) { gen_action_front(f); } }, spend_hp() { resolve_spend_hp(); }, front(f: FrontId) { const value = get_active_node_args().v; update_front(f, value, get_active_faction()); resolve_active_and_proceed(); }, }; states.attack_front = { inactive: 'attack a Front', prompt() { gen_spend_hero_points(); const { t: target, n } = get_active_node_args(); let fronts: Array = []; if (target === ANY) { fronts = get_fronts_to_add_to(ANY, n); } else if (target === 'd' || target === 'v') { fronts = get_fronts_closest_to(target); } else if (game.fronts[target].status === DEFEAT) { fronts = get_fronts_closest_to('d'); } else if (game.fronts[target].status === VICTORY) { fronts = get_fronts_to_add_to(ANY); } else { fronts.push(target); } view.prompt = fronts.length === 1 ? `Attack ${front_names[fronts[0]]}` : 'Attack a front'; fronts.forEach((id) => gen_action('front', id)); }, spend_hp() { resolve_spend_hp(); }, front(f: FrontId) { const node = get_active_node(); const value = node.a.v; update_front(f, value); resolve_active_and_proceed(); }, }; states.break_tie_final_bid = { inactive: 'break tie for Final Bid', prompt() { view.prompt = 'Choose the winner of the Final Bid'; const { winners } = get_active_node_args(); for (const f of winners) { gen_action(faction_player_map[f]); } }, Anarchist() { win_final_bid(ANARCHISTS_ID); resolve_active_and_proceed(); }, Communist() { win_final_bid(COMMUNISTS_ID); resolve_active_and_proceed(); }, Moderate() { win_final_bid(MODERATES_ID); resolve_active_and_proceed(); }, }; states.break_tie_winner = { inactive: 'break tie for winner of the game', prompt() { view.prompt = 'Choose the winner of the game'; const { winners } = get_active_node_args(); for (const f of winners) { gen_action(faction_player_map[f]); } }, Anarchist() { const { glory } = get_active_node_args(); win_game(ANARCHIST, glory); resolve_active_and_proceed(); }, Communist() { const { glory } = get_active_node_args(); win_game(COMMUNIST, glory); resolve_active_and_proceed(); }, Moderate() { const { glory } = get_active_node_args(); win_game(MODERATE, glory); resolve_active_and_proceed(); }, }; states.choose_area_ap = { inactive: 'choose area to use Action Points', prompt() { gen_spend_hero_points(); view.prompt = 'Choose area of the board to affect'; for (const track of tracks) { gen_action_standee(track.id); } const fronts = get_fronts_to_add_to(ANY); for (const front of fronts) { gen_action_front(front); } for (const bonus of bonuses) { if (game.bonuses[bonus] === OFF) { gen_action_bonus(bonus); } } }, spend_hp() { resolve_spend_hp(); }, bonus(b: number) { // Turn on bonus update_bonus(b, ON); const s: number = get_active_node_args().strength; // Check if other bonus can be activated const other_bonus = b === TEAMWORK_BONUS ? MORALE_BONUS : TEAMWORK_BONUS; if (s > 1 && game.bonuses[other_bonus] === OFF) { insert_after_active_node( resolve_effect(create_effect('bonus', other_bonus, ON)) ); } resolve_active_and_proceed(); }, front(f: FrontId) { const s: number = get_active_node_args().strength; update_front(f, s, get_active_faction()); resolve_active_and_proceed(); }, standee(track_id: number) { insert_after_active_node({ t: leaf_node, p: get_active_faction(), s: 'move_track_up_or_down', a: { track_id, strength: get_active_node()?.a.strength, }, }); resolve_active_and_proceed(); }, }; states.change_bonus = { inactive: 'select Bonus', prompt() { gen_spend_hero_points(); const args = get_active_node_args(); if ( (args.v === ON && game.bonuses[TEAMWORK_BONUS] === ON && game.bonuses[MORALE_BONUS] === ON) || (args.v === OFF && game.bonuses[args.t] === OFF) ) { gen_action('skip'); } if (args.t === ANY && args.v === ON) { view.prompt = 'Turn on a Bonus'; for (const bonus of bonuses) { if (game.bonuses[bonus] === OFF) { gen_action_bonus(bonus); } } } else { view.prompt = `Turn ${args.v === OFF ? 'off' : 'on'} ${ bonus_names[args.t] }`; gen_action_bonus(args.t); } }, spend_hp() { resolve_spend_hp(); }, bonus(b: number) { const value = get_active_node_args().v; update_bonus(b, value); resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.choose_card = { inactive: 'choose a card', prompt() { gen_spend_hero_points(); view.prompt = 'Choose a card to play this turn'; const faction = get_active_faction(); const hand = game.hands[faction]; for (let c of hand) { if (!game.selected_cards[faction].includes(c)) { gen_action_card(c); } } }, spend_hp() { resolve_spend_hp(); }, card(c: CardId) { const faction = get_active_faction(); game.selected_cards[faction].push(c); resolve_active_and_proceed(); }, }; states.choose_final_bid = { inactive: 'choose Final Bid', prompt() { view.prompt = 'Choose a card to add to the Final Bid'; const faction = get_active_faction(); for (let c of game.hands[faction]) { if (!game.selected_cards[faction].includes(c)) { gen_action_card(c); } } gen_action('done'); }, card(c: CardId) { const faction = get_active_faction(); game.selected_cards[faction].push(c); const number_selected = game.selected_cards[faction].length; const number_hand = game.hands[faction].length; if ( number_selected === 3 || (number_hand < 4 && number_selected === number_hand - 1) ) { resolve_active_and_proceed(); } else { next(); } }, done() { resolve_active_and_proceed(true); }, }; states.choose_medallion = { inactive: 'choose a medallion', prompt() { gen_spend_hero_points(); view.prompt = 'Choose a medallion'; for (let m of game.medallions.pool) { gen_action_medallion(m); } if (!game.medallions.pool.some((m) => m !== null)) { gen_action('skip'); } }, spend_hp() { resolve_spend_hp(); }, medallion(m: number) { const faction = get_active_faction(); const medallion = medallions[m]; logi(`${faction_player_map[faction]} earns ${medallion.name}`); const index = game.medallions.pool.indexOf(m); game.medallions.pool[index] = null; switch (m) { case 0: add_glory(faction, 1, true); break; case 1: gain_hero_points(faction, 7); break; case 2: insert_after_active_node(create_leaf_node('choose_card', faction)); break; default: game.medallions[faction].push(m); } resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.confirm_turn = { inactive: 'confirm their turn', prompt() { view.prompt = 'Confirm your actions or undo'; gen_action('confirm'); }, confirm() { resolve_active_and_proceed(true); }, }; states.draw_card = { inactive: 'draw a card', prompt() { gen_spend_hero_points(); const { v } = get_active_node_args(); view.prompt = v === 1 ? 'Draw a card' : `Draw ${v} cards`; gen_action(v === 1 ? 'draw_card' : 'draw_cards'); }, spend_hp() { resolve_spend_hp(); }, draw_card() { const { v } = get_active_node_args(); draw_hand_cards(get_active_faction(), v); resolve_active_and_proceed(); }, draw_cards() { const { v } = get_active_node_args(); draw_hand_cards(get_active_faction(), v); resolve_active_and_proceed(); }, }; states.end_of_year_discard = { inactive: 'discard cards from hand and tableau', prompt() { view.prompt = 'Discard a card'; const faction_id = get_active_faction(); const hand = game.hands[faction_id]; if (hand.length > get_hand_limit(faction_id)) { for (let c of hand) gen_action_card(c); } const tableau = game.tableaus[faction_id]; if (tableau.length > game.year) { for (let c of tableau) gen_action_card(c); } }, card(c: CardId) { const faction_id = get_active_faction(); if (game.hands[faction_id].includes(c)) { game.hands[faction_id] = game.hands[faction_id].filter((id) => id !== c); } else if (game.tableaus[faction_id].includes(c)) { game.tableaus[faction_id] = game.tableaus[faction_id].filter( (id) => id !== c ); } game.discard[faction_id].push(c); if ( game.hands[faction_id].length > get_hand_limit(faction_id) || game.tableaus[faction_id].length > game.year ) { // More cards to discard so resolve same state again next(); } else { log(`${faction_player_map[faction_id]} discards cards`); resolve_active_and_proceed(); } }, }; states.hero_points = { inactive: 'gain Hero Points', prompt() { gen_spend_hero_points(); const value = get_active_node_args().v; if (value < 0) { view.prompt = value < -1 ? `Lose ${Math.abs(value)} Hero Points` : 'Lose 1 Hero Point'; gen_action('lose_hp'); return; } if (game.hero_points.pool > 0) { view.prompt = value > 1 ? `Gain ${value} Hero Points` : 'Gain 1 Hero Point'; gen_action('gain_hp'); } else { view.prompt = 'No Hero Points available in pool. You must skip'; gen_action('skip'); } }, spend_hp() { resolve_spend_hp(); }, gain_hp() { const value = get_active_node_args().v; gain_hero_points(get_active_faction(), value); resolve_active_and_proceed(); }, lose_hp() { const value = get_active_node_args().v; lose_hero_points(get_active_faction(), value); resolve_active_and_proceed(); }, skip() { resolve_active_and_proceed(); }, }; states.game_over = { get inactive() { return game.victory; }, prompt() { view.prompt = game.victory; }, }; function resolve_player_with_most_hero_points(faction: FactionId) { const value = get_active_node_args().v; if (value < 0) { lose_hero_points(faction, value); } else { gain_hero_points(faction, value); } resolve_active_and_proceed(); } states.select_player_with_most_hero_points = { inactive: 'choose a Player', prompt() { gen_spend_hero_points(); const { v } = get_active_node_args(); view.prompt = v < 0 ? 'Choose player to lose Hero Points' : 'Choose player to gain Hero Points'; const factions = get_factions_with_most_hero_poins(); for (let faction_id of factions) { gen_action(faction_player_map[faction_id]); } }, spend_hp() { resolve_spend_hp(); }, Anarchist() { resolve_player_with_most_hero_points(ANARCHISTS_ID); }, Communist() { resolve_player_with_most_hero_points(COMMUNISTS_ID); }, Moderate() { resolve_player_with_most_hero_points(MODERATES_ID); }, }; states.move_track = { inactive: 'move a Track', prompt() { gen_spend_hero_points(); const node = get_active_node(); const track = node.a.t; const value = node.a.v; const name = track === LIBERTY_OR_COLLECTIVIZATION ? 'Liberty OR Collectivization' : tracks[track].name; view.prompt = `Move ${name} ${value > 0 ? 'up' : 'down'}`; if (track === GOVERNMENT && value === TOWARDS_CENTER) { view.prompt = `Move ${name} towards center`; } else if (track === GOVERNMENT && value === AWAY_FROM_CENTER) { view.prompt = `Move ${name} away from center`; } if (track === LIBERTY_OR_COLLECTIVIZATION) { gen_action_standee(LIBERTY); gen_action_standee(COLLECTIVIZATION); } else { gen_action_standee(track); } }, spend_hp() { resolve_spend_hp(); }, standee(s: number) { const node = get_active_node(); let value = node.a.v; if ( s === GOVERNMENT && (value === TOWARDS_CENTER || value === AWAY_FROM_CENTER) ) { const direction = get_government_track_direction(value); // Value equals direction because away / towards always moves 1 step value = direction; } move_track(s, value); resolve_active_and_proceed(); }, }; states.move_track_up_or_down = { inactive: 'move a track', prompt() { gen_spend_hero_points(); const node = get_active_node(); view.prompt = `Move ${get_track_name(node.a.track_id)} up or down`; gen_action('up'); gen_action('down'); }, spend_hp() { resolve_spend_hp(); }, down() { const node = get_active_node(); move_track(node.a.track_id, -1 * node.a.strength); resolve_active_and_proceed(); }, up() { const node = get_active_node(); move_track(node.a.track_id, node.a.strength); resolve_active_and_proceed(); }, }; states.peek_fascist_cards = { inactive: 'peek at Fascist cards', prompt() { gen_spend_hero_points(); view.prompt = 'Choose one card to return to the top of the deck'; for (const c of game.selectable_cards) { gen_action_card(c); } }, spend_hp() { resolve_spend_hp(); }, card(c: CardId) { game.top_of_events_deck = c; for (const ec of game.selectable_cards) { if (ec !== c) { game.discard.f.push(ec); } } game.selectable_cards = []; resolve_active_and_proceed(); }, }; function resolve_spend_hp() { // insert spend hero points node before current node // so it will return to current node after resolving insert_before_active_node( create_leaf_node('spend_hero_points', get_active_faction()) ); log('Spends Hero Points'); next(); } states.player_turn = { inactive: 'play their turn', prompt() { gen_spend_hero_points(); const faction_id = get_active_faction(); const can_spend_hp = game.faction_turn === faction_id && game.hero_points[faction_id] > 0; const can_play_card = game.selected_cards[faction_id].length > 0; view.prompt = 'Play a card or spend Hero points'; if (!(can_play_card || can_spend_hp)) { view.prompt = 'End turn'; } else if (!can_play_card && can_spend_hp) { view.prompt = 'Spend Hero Points or end turn'; } else if (can_play_card && !can_spend_hp) { view.prompt = 'Play a card'; } // const card = game.cards_in_play[faction_id]; if (can_play_card) { // gen_action_card(); gen_action('play_for_ap'); gen_action('play_for_event'); } else { gen_action('done'); } }, spend_hp() { resolve_spend_hp(); }, done() { game.faction_turn = null; game.played_card = null; resolve_active_and_proceed(true); }, play_for_ap() { const faction = get_active_faction(); const { strength } = play_card(faction, 'play_for_ap'); insert_before_active_node( create_seq_node([ create_leaf_node('choose_area_ap', faction, { strength, }), create_function_node('check_activate_icon'), ]) ); next(); }, play_for_event() { const faction = get_active_faction(); const { effects } = play_card(faction, 'play_for_event'); insert_before_active_node(create_effects_node(effects)); next(); }, }; states.remove_blank_marker = { inactive: 'remove a Blank marker', prompt() { gen_spend_hero_points(); view.prompt = 'Remove a Blank marker'; for (const b of game.triggered_track_effects) { gen_action_blank_marker(b); } }, spend_hp() { resolve_spend_hp(); }, blank_marker(b: number) { const faction = get_active_faction(); pay_hero_points(faction, 1); const track_id = Math.floor(b / 11); const space_id = b % 11; logi( `${ faction_player_map[faction] } removes a Blank marker from space ${space_id} of ${get_track_name( track_id )}` ); game.triggered_track_effects = game.triggered_track_effects.filter( (id) => id !== b ); game.used_medallions.push(ARCHIVES_MEDALLION_ID); resolve_active_and_proceed(); }, }; /** * Event cards 6 and 39 * 6: remove 1 attack from up to three fronts * 16: Move up to 2 Attacks from a Front to another Front * 39: remove up to 3 attacks from a Front; place 2 back(-2) */ states.remove_attack_from_fronts = { inactive: 'remove attacks', prompt() { gen_spend_hero_points(); const { f, v: card_id } = get_active_node_args(); view.prompt = card_id === 6 ? 'Choose a Front to remove an attack from' : 'Choose a Front to remove attacks from'; const front_data = f ?? {}; let is_front_with_attacks = false; FRONTS.forEach((id) => { if (game.fronts[id].value >= 0 || game.fronts[id].status !== null) { return; } if (card_id === 6 && front_data[id]) { return; } is_front_with_attacks = true; gen_action_front(id); }); if (!is_front_with_attacks) { view.prompt = 'No valid Front to choose. You must skip'; gen_action('skip'); } }, spend_hp() { resolve_spend_hp(); }, front(id: FrontId) { const { f, v: card_id } = get_active_node_args(); const removed_value = card_id === 6 ? 1 : Math.min(3, Math.abs(game.fronts[id].value)); update_front(id, removed_value); const fronts = f ?? {}; fronts[id] = removed_value; update_active_node_args({ f: fronts }); if (card_id === 6 && Object.keys(fronts).length === 3) { resolve_active_and_proceed(); } else if (card_id === 39 || card_id === 16) { insert_after_active_node( create_leaf_node('attack_front', get_active_faction(), { t: ANY, v: card_id === 39 ? -2 : -1 * removed_value, n: card_id === 16 ? id : undefined, }) ); resolve_active_and_proceed(); } }, skip() { const { f, v: card_id } = get_active_node_args(); const values: number[] = Object.values(f ?? {}); if (card_id === 39 && values.length > 0) { insert_after_active_node( create_leaf_node('attack_front', get_active_faction(), { t: ANY, v: -2, }) ); } resolve_active_and_proceed(); }, }; states.return_card = { inactive: 'return a card to their hand', prompt() { gen_spend_hero_points(); view.prompt = 'Choose a card to return to your hand'; if (game.selectable_cards.length === 0) { view.prompt = 'No card in trash to return. You must skip'; gen_action('skip'); } for (const c of game.selectable_cards) { gen_action_card(c); } }, spend_hp() { resolve_spend_hp(); }, card(c: CardId) { const faction = get_active_faction(); array_remove(game.trash[faction], game.trash[faction].indexOf(c)); game.hands[faction].push(c); logi(`${faction_player_map[faction]} returns a card to their hand`); game.selectable_cards = []; resolve_active_and_proceed(); }, skip() { game.selectable_cards = []; resolve_active_and_proceed(); }, }; states.spend_hero_points = { inactive: 'spend Hero points', prompt() { view.prompt = 'Spend your Hero points'; gen_action('done'); const faction = get_active_faction(); const hero_points = game.hero_points[get_active_faction()]; if (hero_points === 0) { return; } gen_action('draw_card'); if (can_use_medallion(ARCHIVES_MEDALLION_ID, faction)) { gen_action('remove_blank_marker'); } if (can_use_medallion(VOLUNTEERS_MEDALLION_ID, faction)) { gen_action('add_to_front'); } if (hero_points < 2) { return; } gen_action_standee(FOREIGN_AID); gen_action_standee(SOVIET_SUPPORT); for (const bonus of bonuses) { if (game.bonuses[bonus] === OFF) { gen_action_bonus(bonus); } } if (hero_points < 3) { return; } gen_action_standee(COLLECTIVIZATION); gen_action_standee(LIBERTY); if (hero_points < 4) { return; } gen_action_standee(GOVERNMENT); }, add_to_front() { const faction = get_active_faction(); pay_hero_points(faction, 1); insert_after_active_node( create_seq_node([ create_leaf_node('add_to_front', faction, { t: ANY, v: 1, }), create_leaf_node('spend_hero_points', faction), ]) ); resolve_active_and_proceed(); }, done() { resolve_active_and_proceed(); }, bonus(b: number) { update_bonus(b, ON); pay_hero_points(get_active_faction(), 2); }, draw_card() { const faction = get_active_faction(); pay_hero_points(faction, 1); draw_hand_cards(faction, 1); }, remove_blank_marker() { const faction = get_active_faction(); if (game.used_medallions) { game.used_medallions.push(ARCHIVES_MEDALLION_ID); } else { game.used_medallions = [ARCHIVES_MEDALLION_ID]; } insert_after_active_node( create_seq_node([ create_leaf_node('remove_blank_marker', faction), create_leaf_node('spend_hero_points', faction), ]) ); resolve_active_and_proceed(); }, standee(track_id: number) { let amount = 2; if (track_id === LIBERTY || track_id === COLLECTIVIZATION) { amount = 3; } else if (track_id === GOVERNMENT) { amount = 4; } const faction = get_active_faction(); pay_hero_points(faction, amount); insert_after_active_node( create_seq_node([ create_leaf_node('move_track_up_or_down', faction, { track_id, strength: 1, }), create_leaf_node('spend_hero_points', faction), ]) ); resolve_active_and_proceed(); }, }; states.swap_card_tableau_hand = { inactive: 'swap cards', prompt() { gen_spend_hero_points(); view.prompt = 'Choose a card in your tableau and a card in your hand to swap'; const faction = get_active_faction(); gen_action('skip'); if (game.tableaus[faction].length === 0) { view.prompt = 'No card in your tableau to swap. You must skip'; return; } if (game.hands[faction].length === 0) { view.prompt = 'No card in your hand to swap. You must skip'; return; } if ( !game.selected_cards[faction].some((card_id) => game.hands[faction].includes(card_id) ) ) { for (const c of game.hands[faction]) { gen_action_card(c); } } if ( !game.selected_cards[faction].some((card_id) => game.tableaus[faction].includes(card_id) ) ) { for (const c of game.tableaus[faction]) { gen_action_card(c); } } }, spend_hp() { resolve_spend_hp(); }, card(c: CardId) { const faction = get_active_faction(); const selected_cards = game.selected_cards[faction]; selected_cards.push(c); if (selected_cards.length === 2) { const hand_card_index = selected_cards.findIndex((card_id) => game.hands[faction].includes(card_id) ); const tableau_card_index = hand_card_index === 0 ? 1 : 0; const hand = game.hands[faction]; const tableau = game.tableaus[faction]; array_remove(hand, hand.indexOf(selected_cards[hand_card_index])); array_remove( tableau, tableau.indexOf(selected_cards[tableau_card_index]) ); hand.push(selected_cards[tableau_card_index]); tableau.push(selected_cards[hand_card_index]); game.selected_cards[faction] = []; resolve_active_and_proceed(); } }, skip() { const faction = get_active_faction(); game.selected_cards[faction] = []; resolve_active_and_proceed(); }, }; function resolve_take_hero_points(faction: FactionId) { const { v } = get_active_node_args(); const amount = Math.min(v, game.hero_points[faction]); lose_hero_points(faction, amount); gain_hero_points(get_active_faction(), amount); resolve_active_and_proceed(); } states.take_hero_points = { inactive: 'take Hero Points', prompt() { gen_spend_hero_points(); const { v } = get_active_node_args(); view.prompt = v === 1 ? 'Choose a player to take a Hero Point from' : `Choose a player to take ${v} Hero Points from`; const active_faction = get_active_faction(); for (const faction of role_ids) { if (faction !== active_faction) { gen_action(faction_player_map[faction]); } } }, spend_hp() { resolve_spend_hp(); }, Anarchist() { resolve_take_hero_points(ANARCHISTS_ID); }, Communist() { resolve_take_hero_points(COMMUNISTS_ID); }, Moderate() { resolve_take_hero_points(MODERATES_ID); }, }; states.use_organization_medallion = { inactive: 'use Organization Medallion', prompt() { gen_spend_hero_points(); view.prompt = 'Use Organization Medallion?'; gen_action('yes'); gen_action('no'); }, spend_hp() { resolve_spend_hp(); }, yes() { const faction = get_active_faction(); pay_hero_points(faction, 1); game.used_medallions.push(ORGANIZATION_MEDALLION_ID); let { t, v } = get_active_node_args(); if (v > 0) { v++; } else { v--; } move_track(t, v); resolve_active_and_proceed(); }, no() { const { t, v } = get_active_node_args(); move_track(t, v); resolve_active_and_proceed(); }, }; states.use_strategy_medallion = { inactive: 'use Strategy Medallion', prompt() { gen_spend_hero_points(); view.prompt = 'Use Strategy Medallion?'; gen_action('yes'); gen_action('no'); }, spend_hp() { resolve_spend_hp(); }, yes() { game.used_medallions.push(STRATEGY_MEDALLION_ID); const { f } = get_active_node_args(); const faction = get_active_faction(); update_front(f, 1, faction); resolve_active_and_proceed(); }, no() { resolve_active_and_proceed(); }, }; // #endrregion // #region card effects function card1_event2() { const value = game.tracks[FOREIGN_AID] >= 6 ? 3 : 2; insert_after_active_node( resolve_effect(create_effect('track', FOREIGN_AID, value)) ); resolve_active_and_proceed(); } function card3_event2() { const value = game.tracks[FOREIGN_AID] >= 8 ? 2 : 1; insert_after_active_node( resolve_effect(create_effect('track', GOVERNMENT, value)) ); resolve_active_and_proceed(); } function card10_event2() { if (game.tracks[FOREIGN_AID] >= 6) { resolve_effect(create_effect('draw_card', SELF, 2)); } resolve_active_and_proceed(); } function card16_event2() { const value = game.tracks[GOVERNMENT] >= 6 ? 4 : 3; insert_after_active_node( resolve_effect(create_effect('track', FOREIGN_AID, value)) ); resolve_active_and_proceed(); } function card17_event3() { const value = game.tracks[GOVERNMENT] >= 6 ? -4 : -3; insert_after_active_node( resolve_effect(create_effect('track', COLLECTIVIZATION, value)) ); resolve_active_and_proceed(); } function card20_event3() { const value = game.tracks[SOVIET_SUPPORT] >= 6 ? 2 : 1; insert_after_active_node( create_seq_node([ resolve_effect(create_effect('front', MADRID, value)), resolve_effect(create_effect('front', SOUTHERN, value)), ]) ); resolve_active_and_proceed(); } function card22_event3() { const value = game.tracks[SOVIET_SUPPORT] >= 8 ? -3 : -3; insert_after_active_node( resolve_effect(create_effect('track', GOVERNMENT, value)) ); resolve_active_and_proceed(); } function card23_event1() { const value = game.tracks[SOVIET_SUPPORT] >= 6 ? 4 : 3; insert_after_active_node(resolve_effect(create_effect('front', ANY, value))); resolve_active_and_proceed(); } function card26_event1() { game.active_abilities.push(COMMUNIST_EXTRA_HERO_POINT); resolve_active_and_proceed(); } function card29_event2() { const value = game.tracks[GOVERNMENT] <= 5 ? -3 : -2; insert_after_active_node( resolve_effect(create_effect('track', LIBERTY, value)) ); resolve_active_and_proceed(); } function card35_event2() { const value = game.tracks[GOVERNMENT] <= 5 ? 2 : 1; insert_after_active_node( resolve_effect(create_effect('track', SOVIET_SUPPORT, value)) ); resolve_active_and_proceed(); } function card42_event3() { game.active_abilities.push(ANARCHIST_EXTRA_HERO_POINT); resolve_active_and_proceed(); } function card45_event2() { if (game.tracks[LIBERTY] >= 6) { insert_after_active_node( resolve_effect(create_effect('track', COLLECTIVIZATION, 1)) ); } resolve_active_and_proceed(); } function card46_event3() { for (let i = 0; i < 3; ++i) { game.selectable_cards.push(draw_fascist_card()); } resolve_active_and_proceed(); } function card50_event2() { const value = game.tracks[COLLECTIVIZATION] >= 8 ? 3 : 2; insert_after_active_node( resolve_effect(create_effect('front', ARAGON, value)) ); resolve_active_and_proceed(); } function card53_event2() { const value = game.tracks[LIBERTY] >= 8 ? 3 : 2; insert_after_active_node(resolve_effect(create_effect('front', ANY, value))); resolve_active_and_proceed(); } function card54_event1() { const value = game.tracks[COLLECTIVIZATION] >= 8 ? 3 : 2; insert_after_active_node( resolve_effect(create_effect('track', LIBERTY, value)) ); resolve_active_and_proceed(); } function setup_return_card_from_trash() { const faction = get_active_faction(); game.selectable_cards = game.trash[faction].filter( (c) => c !== game.played_card ); resolve_active_and_proceed(); } // #endregion // #region GAME FUNCTIONS function add_glory( faction: FactionId, amount: number, indent: boolean = false ) { for (let i = 0; i < amount; ++i) { game.bag_of_glory.push(get_active_faction()); } let text = amount === 1 ? `${faction_player_map[faction]} adds 1 token to the Bag of Glory` : `${faction_player_map[faction]} adds ${amount} tokens to the Bag of Glory`; if (indent) { logi(text); } else { log_h3(text); } } // Check if Morale bonus is on so player can activate icon function check_activate_icon() { if (game.bonuses[MORALE_BONUS] === ON) { insert_after_active_node( create_leaf_node('activate_icon', get_active_faction()) ); } resolve_active_and_proceed(); } function check_initiative() { let initiative: FactionId; if (game.tracks[LIBERTY] >= 6 && game.tracks[COLLECTIVIZATION] >= 6) { initiative = ANARCHISTS_ID; } else if (game.tracks[GOVERNMENT] <= 5) { initiative = COMMUNISTS_ID; } else { initiative = MODERATES_ID; } if (game.initiative === initiative) { return; } game.initiative = initiative; logi(`${faction_player_map[initiative]} claims the Initiative`); } function war_is_won() { let won_fronts = 0; for (const f of FRONTS) { if (game.fronts[f].value >= 1) { won_fronts++; } } return won_fronts >= 3; } function determine_winner() { const glory = { [ANARCHISTS_ID]: 0, [COMMUNISTS_ID]: 0, [MODERATES_ID]: 0, }; for (const g of game.glory) { glory[g]++; } let highest_glory = 0; let winners = []; for (let f of role_ids) { if (glory[f] === highest_glory) { winners.push(f); } else if (glory[f] > highest_glory) { highest_glory = glory[f]; winners = [f]; } } if (winners.length === 1) { win_game(faction_player_map[winners[0]], highest_glory); } else { insert_after_active_node( create_leaf_node('break_tie_winner', game.initiative, { winners, glory: highest_glory, }) ); } resolve_active_and_proceed(); } function end_of_turn() { Object.keys(game.fronts).forEach((front_id) => { game.fronts[front_id].contributions = []; }); game.active_abilities = []; game.used_medallions = []; if (game.turn === 4) { end_of_year(); } else { game.turn++; start_turn(); } } function end_of_year() { if (game.year === 3) { log_h1('End of the game'); const is_won = war_is_won(); if (is_won) { log('The war is won!'); } else { game_over('None', 'The war is lost. All Players lose the game!'); resolve_active_and_proceed(); return; } } const glory_to_draw = [0, 1, 2, 5]; const glory_this_year: Record = { a: false, c: false, m: false, }; for (let i = 0; i < glory_to_draw[game.year]; ++i) { const index = random(game.bag_of_glory.length); const faction = game.bag_of_glory[index]; game.glory.push(faction); glory_this_year[faction] = true; array_remove(game.bag_of_glory, index); } if (game.year === 3) { // end of game determine_winner(); return; } const players_to_gain_hero_points = role_ids.filter( (f) => !glory_this_year[f] ); gain_hero_points_in_player_order(players_to_gain_hero_points, game.year); // Setup card discarding game.engine = get_player_order().map((f) => create_leaf_node('end_of_year_discard', f) ); game.engine.push(create_function_node('checkpoint')); game.engine.push(create_function_node('start_year')); // New deck is used for next year so clear top card // if it was set game.top_of_events_deck = null; next(); } function gain_hero_points_in_player_order(factions: FactionId[], value) { for (const f of get_player_order()) { if (factions.includes(f)) { gain_hero_points(f, value); } } } function gain_hero_points( faction_id: FactionId, value: number, skip_abilities = false // Used to prevent gaining of extra hero points when taking them from another player ) { if (game.hero_points.pool === 0) { return; } if ( !skip_abilities && ((faction_id === ANARCHISTS_ID && (game.active_abilities || []).includes(ANARCHIST_EXTRA_HERO_POINT)) || (faction_id === COMMUNISTS_ID && (game.active_abilities || []).includes(COMMUNIST_EXTRA_HERO_POINT))) ) { value++; } const gain = Math.min(game.hero_points.pool, value); game.hero_points.pool -= gain; game.hero_points[faction_id] += gain; logi( `${get_player(faction_id)} +${gain} ${ gain === 1 ? 'Hero Point' : 'Hero Points' }` ); } function game_over(result: Player | 'None', victory: string) { insert_after_active_node(create_leaf_node('game_over', 'None')); // game.state = 'game_over'; // game.active = 'None'; game.result = result; game.victory = victory; game.undo = []; log_br(); log(game.victory); } function get_hand_limit(faction: FactionId) { let hand_limit = game.year; if (game.medallions[faction].includes(INTELLIGENCE_MEDALLION_ID)) { hand_limit++; } return hand_limit; } function play_card( faction: FactionId, type: 'play_for_event' | 'play_for_ap' ): PlayerCard { const index = game.selected_cards[faction].length - 1; const card_id = game.selected_cards[faction][index]; const card = cards[card_id]; game.played_card = card_id; log_h3( `${game.active} plays ${card.title} for the ${ type === 'play_for_event' ? 'Event' : 'Action Points' }` ); array_remove(game.hands[faction], game.hands[faction].indexOf(card_id)); array_remove(game.selected_cards[faction], index); if (type === 'play_for_event') { game.trash[faction].push(card_id); } else { game.tableaus[faction].push(card_id); } return card as PlayerCard; } function resolve_fascist_test() { log_h2('Fascist Test', 'fascist'); const test = get_current_event().test; const status = game.fronts[test.front].status; const test_passed = status === VICTORY || (status !== DEFEAT && game.fronts[test.front].value >= test.value); if (test_passed) { log('The Test is passed'); for (const faction of get_player_order()) { let hero_points_gain = game.fronts[test.front].contributions.includes( faction ) ? 2 : 0; if (can_use_medallion(PROPAGANDA_MEDALLION_ID, faction)) { hero_points_gain += 2; } if (hero_points_gain > 0) { gain_hero_points(faction, hero_points_gain); } } } else { log('The Test is failed'); } const effect = test_passed ? test.pass : test.fail; const node = resolve_effect(effect); if (node !== null) { insert_after_active_node(node); } resolve_active_and_proceed(); } function resolve_final_bid() { let highest_bid = 0; let winners: FactionId[] = []; for (const f of get_player_order()) { let player_bid = 0; for (const c of game.selected_cards[f]) { player_bid += (cards[c] as PlayerCard).strength; } log(`${faction_player_map[f]} bids ${player_bid}`); if (player_bid === highest_bid) { winners.push(f); } else if (player_bid > highest_bid) { highest_bid = player_bid; winners = [f]; } game.hands[f] = game.hands[f].filter( (c) => !game.selected_cards[f].includes(c) ); game.discard[f].concat(game.selected_cards[f]); game.selected_cards[f] = []; } if (winners.length === 1) { win_final_bid(winners[0]); } else { insert_after_active_node( create_leaf_node('break_tie_final_bid', game.initiative, { winners }) ); } resolve_active_and_proceed(); } function get_fronts_to_add_to(target: string, not: FrontId[] = []): FrontId[] { if (target === CLOSEST_TO_DEFEAT || target === CLOSEST_TO_VICTORY) { return get_fronts_closest_to(target); } else if (target === ANY) { return FRONTS.filter( (id) => game.fronts[id].status === null && !not.includes(id) ); } else { return [target as FrontId]; } } function get_max_value_for_track(track_id: number) { switch (track_id) { case LIBERTY: const max_lib = game.tracks[COLLECTIVIZATION] >= 8 ? 10 : 7; return Math.max(max_lib, game.tracks[LIBERTY]); case GOVERNMENT: const max_gov = game.tracks[FOREIGN_AID] >= 8 ? 10 : 7; return Math.max(max_gov, game.tracks[GOVERNMENT]); case COLLECTIVIZATION: case SOVIET_SUPPORT: case FOREIGN_AID: default: return 10; } } function get_min_value_for_track(track_id: number) { switch (track_id) { case GOVERNMENT: const min_gov = game.tracks[SOVIET_SUPPORT] >= 8 ? 4 : 1; return Math.min(min_gov, game.tracks[GOVERNMENT]); case LIBERTY: case COLLECTIVIZATION: case SOVIET_SUPPORT: case FOREIGN_AID: default: return 0; } } // TOWARDS_CENTER = 10; // AWAY_FROM_CENTER = 11; function get_government_track_direction(direction: 10 | 11): -1 | 1 { const value = game.tracks[GOVERNMENT]; if ( (direction === TOWARDS_CENTER && value >= 6) || (direction === AWAY_FROM_CENTER && value <= 5) ) { return -1; } else { return 1; } } function move_track(track_id: number, change: number) { const current_value = game.tracks[track_id]; let new_value = current_value + change; new_value = Math.max(new_value, get_min_value_for_track(track_id)); new_value = Math.min(new_value, get_max_value_for_track(track_id)); game.tracks[track_id] = new_value; logi(`${get_track_name(track_id)} to ${new_value}`); check_initiative(); const triggered_spaces = change > 0 ? make_list(current_value + 1, new_value).reverse() : make_list(new_value, current_value - 1); triggered_spaces.forEach((space_id) => { const trigger = tracks[track_id].triggers[space_id]; if ( trigger !== null && !game.triggered_track_effects.includes( get_blank_marker_id(track_id, space_id) ) ) { if (space_id !== 0) { game.triggered_track_effects.push( get_blank_marker_id(track_id, space_id) ); } const node = resolve_effect(trigger); if (node !== null) { insert_after_active_node(node); } } }); } function pay_hero_points(faction: FactionId, amount: number) { game.hero_points[faction] -= amount; game.hero_points.pool += amount; } function can_use_medallion(medallion_id: number, faction?: FactionId) { faction = faction === undefined ? get_active_faction() : faction; const can_use = game.medallions[faction].includes(medallion_id) && !game.used_medallions.includes(medallion_id); if (medallion_id === ORGANIZATION_MEDALLION_ID) { return can_use && game.hero_points[faction] > 0; } else { return can_use; } } function insert_use_organization_medallion_node( track_id: number, value: number ) { const faction = get_active_faction(); insert_after_active_node( create_leaf_node('use_organization_medallion', faction, { t: track_id, v: value, }) ); } function update_bonus(bonus_id: number, status: number) { if (game.bonuses[bonus_id] === status) { return; } game.bonuses[bonus_id] = status; logi(`${bonus_names[bonus_id]} ${status === ON ? 'on' : 'off'}`); } function update_front( // f: string, front_id: FrontId, change: number, faction_id: FactionId | null = null ) { // Check teamwork bonus const player_token_on_front = faction_id !== null && game.fronts[front_id].contributions.includes(faction_id); if ( game.bonuses[TEAMWORK_BONUS] === ON && change > 0 && faction_id !== null && !player_token_on_front && game.fronts[front_id].contributions.length > 0 ) { change += 1; } const value_before = game.fronts[front_id].value; game.fronts[front_id].value += change; logi(`${front_names[front_id]}: ${change > 0 ? '+' : ''}${change}`); if ( faction_id !== null && value_before <= 0 && game.fronts[front_id].value > 0 ) { gain_hero_points(faction_id, 1); } // Add token to front if player contributed if ( faction_id !== null && !game.fronts[front_id].contributions.includes(faction_id) ) { game.fronts[front_id].contributions.push(faction_id); } if ( change > 0 && faction_id !== undefined && game.fronts[front_id].value < 10 && can_use_medallion(STRATEGY_MEDALLION_ID) ) { insert_after_active_node( create_leaf_node('use_strategy_medallion', get_active_faction(), { f: front_id, }) ); } // Check victory / defeat on a front if (game.fronts[front_id].value >= 10) { victory_on_a_front(front_id); } else if (game.fronts[front_id].value <= -10) { defeat_on_a_front(front_id); } } function defeat_on_a_front(front_id: FrontId) { game.fronts[front_id].status = DEFEAT; log('Defeat on ' + get_front_name(front_id)); // Check game end if (front_id === 'm' || get_defeated_front_count() == 2) { game_over('None', 'All players lose the game!'); return; } insert_after_active_node( create_effects_node([ create_effect('bonus', MORALE_BONUS, OFF), create_effect('track', COLLECTIVIZATION, -1), create_effect('track', SOVIET_SUPPORT, -1), create_effect('track', FOREIGN_AID, -1), ]) ); } function victory_on_a_front(front_id: FrontId) { game.fronts[front_id].status = VICTORY; log('Victory on ' + get_front_name(front_id)); gain_hero_points_in_player_order(game.fronts[front_id].contributions, 3); } function create_effects_node(effects: Effect[]): EngineNode { const nodes = effects.reduce((accrued: EngineNode[], current: Effect) => { const node = resolve_effect(current); if (node !== null) { accrued.push(node); } return accrued; }, []); return create_seq_node(nodes); } function get_faction_to_resolve_effect(effect: Effect): FactionId { if (!effect.faction) { return get_active_faction(); } if (effect.faction === INITIATIVE_PLAYER) { return game.initiative; } return effect.faction; } const effect_type_state_map: Record = { add_card_to_tableau: 'add_card_to_tableau', attack: 'attack_front', bonus: 'change_bonus', front: 'add_to_front', medallion: 'choose_medallion', remove_blank_marker: 'remove_blank_marker', return_card: 'return_card', swap_card_tableau_hand: 'swap_card_tableau_hand', take_hero_points: 'take_hero_points', track: 'move_track', }; function resolve_effect( effect: Effect // faction: FactionId = get_active_faction() // ): EngineNode { const args = { t: effect.target, v: effect.value, }; const faction = get_faction_to_resolve_effect(effect); if (effect.type === 'function') { return create_function_node(effect.target as string); } if (effect.type === 'state') { return create_leaf_node(effect.target as string, faction, { v: effect.value, }); } // Default cases where effect type is mapped to a state let state = effect_type_state_map[effect.type]; if (state !== undefined) { return create_leaf_node(state, faction, args); } // Specific mapping based on target const strategies = [ { condition: effect.type === 'hero_points' && effect.target === PLAYER_WITH_MOST_HERO_POINTS, resolve: () => { return create_leaf_node( 'select_player_with_most_hero_points', faction, args ); }, }, { condition: effect.type === 'hero_points' && effect.target === ALL_PLAYERS, resolve: () => { return create_seq_node( get_player_order().map((faction) => create_leaf_node('hero_points', faction, { v: effect.value, }) ) ); }, }, { condition: effect.type === 'hero_points' && effect.target === SELF, resolve: () => { return create_leaf_node('hero_points', faction, args); }, }, { condition: effect.type === 'hero_points' && role_ids.includes(effect.target as FactionId), resolve: () => { return create_leaf_node( 'hero_points', effect.target as FactionId, args ); }, }, { condition: effect.type === 'hero_points' && effect.target === INITIATIVE_PLAYER, resolve: () => { return create_leaf_node('hero_points', game.initiative, args); }, }, { condition: effect.type === 'draw_card' && effect.target === SELF, resolve: () => { return create_leaf_node('draw_card', faction, args); }, }, { condition: effect.type === 'draw_card' && effect.target === INITIATIVE_PLAYER, resolve: () => { return create_leaf_node('draw_card', game.initiative, args); }, }, { condition: effect.type === 'draw_card' && role_ids.includes(effect.target as FactionId), resolve: () => { return create_leaf_node('draw_card', effect.target as FactionId, args); }, }, { condition: effect.type === 'draw_card' && effect.target === ALL_PLAYERS, resolve: () => { return create_seq_node( get_player_order(get_active_faction()).map((faction) => create_leaf_node('draw_card', faction, { v: effect.value, }) ) ); }, }, { condition: effect.type === 'draw_card' && effect.target === OTHER_PLAYERS, resolve: () => { const leaf_nodes = get_player_order(get_active_faction()).map( (faction) => create_leaf_node('draw_card', faction, { v: effect.value, }) ); array_remove(leaf_nodes, 0); return create_seq_node(leaf_nodes); }, }, { condition: effect.type === 'play_card', resolve: () => { return create_seq_node([ create_leaf_node('choose_card', faction), create_leaf_node('player_turn', faction), ]); }, }, ]; const strategy = strategies.find((strategy) => strategy.condition); if (strategy) { return strategy.resolve(); } else { console.log('----UNRESOLVED EFFECT----', effect); throw new Error('Unresolved effect'); } } function win_final_bid(faction_id: FactionId) { log_br(); log(`${faction_player_map[faction_id]} wins the Final Bid`); game.glory.push(faction_id); } function win_game(player: Player, glory: number) { game_over(player, `${player} wins the game with a total of ${glory} Glory!`); } // #endregion // #region CARDS // function draw_faction_card(faction: Player): CardId { // return draw_faction_cards(faction, 1)[0]; // } // function draw_faction_cards(faction: Player, count: number = 1): CardId[] { // const drawnCards = []; // } function draw_card(deck: CardId[]): CardId { clear_undo(); let i = random(deck.length); let c = deck[i] as CardId; set_delete(deck, c); return c; } function draw_fascist_card(): CardId { if (game.top_of_events_deck !== null) { const card_id = game.top_of_events_deck; game.top_of_events_deck = null; return card_id; } return draw_card(list_deck(FASCIST_ID)); } function lose_hero_points(faction: FactionId, value: number) { const points_lost = Math.min(game.hero_points[faction], Math.abs(value)); game.hero_points.pool += points_lost; game.hero_points[faction] -= points_lost; if (points_lost === 0) { return; } if (points_lost === 1) { log(`${get_player(faction)}: -1 Hero Point`); } else { log(`${get_player(faction)}: -${points_lost} Hero Points`); } } // #endregion // #region FRONTS function get_fronts_closest_to(target: 'd' | 'v'): FrontId[] { const values = Object.values(game.fronts).reduce( (accrued: number[], current: Front) => { if (current.status === null) { accrued.push(current.value); } return accrued; }, [] ); const targetValue = target === 'd' ? Math.min(...values) : Math.max(...values); return Object.keys(game.fronts).filter( (frontId) => game.fronts[frontId].value === targetValue ) as FrontId[]; } // #endregion // #region LOGGING function log_br() { if (game.log.length > 0 && game.log[game.log.length - 1] !== '') game.log.push(''); } function log(msg: string) { game.log.push(msg); } // function logevent(cap: Card) { // game.log.push(`E${cap}.`) // } // function logcap(cap: Card) { // game.log.push(`C${cap}.`) // } function logi(msg: string) { log('>' + msg); } // function logii(msg: string) { // log('>>' + msg); // } function log_h1(msg: string) { log_br(); log('.h1 ' + msg); log_br(); } function log_h2(msg: string, player?: Player | 'fascist') { log_br(); log(`.h2${player ? `.${player}` : ''} ${msg}`); log_br(); } // function log_h2_active(msg: string) { // log_br(); // log('.h2 ' + msg); // log_br(); // } // function log_h2_common(msg: string) { // log_br(); // log('.h2 ' + msg); // log_br(); // } function log_h3(msg: string) { log_br(); log('.h3 ' + msg); } // function log_h4(msg: string) { // log_br(); // log('.h4 ' + msg); // } // #endregion LOGGING // #region UTILITY function get_active_faction(): FactionId { return player_faction_map[game.active]; } function get_blank_marker_id(track_id: number, space_id: number) { return track_id * 11 + space_id; } function get_front_name(id: FrontId | 'd' | 'v') { return front_names[id]; } function get_current_event(): EventCard { return cards[ game.current_events[game.current_events.length - 1] ] as EventCard; } function get_defeated_front_count() { let count = 0; for (const front_id of FRONTS) { if (game.fronts[front_id].status === DEFEAT) { count++; } } return count; } function get_icon_count_in_tableau( icon: Icon, faction: FactionId = get_active_faction() ) { let count = 0; for (const c of game.tableaus[faction]) { const card = cards[c] as PlayerCard; for (const i of card.icons) { if (i === icon) { ++count; } } } return count; } function get_player(faction_id: FactionId) { return faction_player_map[faction_id]; } function get_player_order(first_player = game.initiative): FactionId[] { const order = []; let faction = first_player; for (let i = 0; i < 3; ++i) { order.push(faction); faction = game.year === 2 ? get_previous_faction(faction) : get_next_faction(faction); } return order; } function get_previous_faction(faction_id: FactionId): FactionId { const index = game.player_order.indexOf(faction_player_map[faction_id]); if (index === 0) { return player_faction_map[game.player_order[2]]; } return player_faction_map[game.player_order[index - 1]]; } function get_next_faction(faction_id: FactionId): FactionId { console.log('get_next', faction_id) const index = game.player_order.indexOf(faction_player_map[faction_id]); console.log('index', index); if (index === 2) { return player_faction_map[game.player_order[0]]; } return player_faction_map[game.player_order[index + 1]]; } function get_factions_with_most_hero_poins(): FactionId[] { let most_hero_points = null; let faction_ids = []; Object.entries(game.hero_points).forEach(([id, value]) => { if (id === 'pool') { return; } if (most_hero_points === null || value > most_hero_points) { most_hero_points = value; faction_ids = [id]; } else if (most_hero_points === value) { faction_ids.push(id); } }); return faction_ids; } function get_track_name(track_id: number): string { return tracks[track_id].name; } function make_list(first: number, last: number): number[] { let list = []; for (let i = first; i <= last; i++) list.push(i); return list; } function list_deck(id: FactionId | 'f') { const deck = []; const card_list: CardId[] = id === FASCIST_ID ? fascist_decks[game.year] : faction_cards[id]; card_list.forEach((card) => { if ( game.discard[id].includes(card) || game.selectable_cards.includes(card) ) { return; } if (id === FASCIST_ID && game.current_events.includes(card)) { return; } else if ( id !== FASCIST_ID && (game.hands[id].includes(card) || game.discard[id].includes(card) || game.tableaus[id].includes(card) || game.trash[id].includes(card)) ) { return; } deck.push(card); }); return deck; } function draw_medallions() { const medallion_ids = make_list(0, 8) as number[]; for (let m = 0; m < 5; ++m) { let i = random(medallion_ids.length); let r = medallion_ids[i] as CardId; set_delete(medallion_ids, r); game.medallions.pool.push(r); } } // #endregion // #region COMMON LIBRARY function clear_undo() { if (game.undo) { game.undo.length = 0; } } function push_undo() { if (game.undo) { let copy = {} as Game; 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); } return game.undo; } function pop_undo() { if (game.undo) { let save_log = game.log; let save_undo = game.undo; game = save_undo.pop(); (save_log as string[]).length = game.log as unknown as number; game.log = save_log; game.undo = save_undo; } next(); } function random(range: number): number { // 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; } // 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; } } // Array remove and insert (faster than splice) function array_remove(array: T[], index: number) { let n = array.length; for (let i = index + 1; i < n; ++i) array[i - 1] = array[i]; array.length = n - 1; } function array_insert(array: T[], index: number, item: T) { for (let i = array.length; i > index; --i) array[i] = array[i - 1]; array[index] = item; } // function array_remove_pair(array: T[], index: number) { // let n = array.length; // for (let i = index + 2; i < n; ++i) array[i - 2] = array[i]; // array.length = n - 2; // } // function array_insert_pair( // array: (K | V)[], // index: number, // key: K, // value: V // ) { // for (let i = array.length; i > index; i -= 2) { // array[i] = array[i - 2]; // array[i + 1] = array[i - 1]; // } // array[index] = key; // array[index + 1] = value; // } // Set as plain sorted array // function set_clear(set: T[]) { // // eslint-disable-line @typescript-eslint/no-unused-vars // set.length = 0; // } // function set_has(set: T[], item: T) { // let a = 0; // let b = set.length - 1; // while (a <= b) { // const m = (a + b) >> 1; // const 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: T[], item: T) { // // eslint-disable-line @typescript-eslint/no-unused-vars // let a = 0; // let b = set.length - 1; // while (a <= b) { // const m = (a + b) >> 1; // const 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: T[], item: T) { // 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 { // array_remove(set, m); // return; // } // } // } function set_delete(set: T[], item: T) { // eslint-disable-line @typescript-eslint/no-unused-vars let a = 0; let b = set.length - 1; while (a <= b) { const m = (a + b) >> 1; const x = set[m]; if (item < x) b = m - 1; else if (item > x) a = m + 1; else return array_remove(set, m); } return set; } // #endregion