From ddf0b64e149e6548e5abf1326ea986dae26a2c27 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 28 Mar 2025 20:46:01 +0100 Subject: Use separate untriggered array to delay placing blank markers. --- rules.js | 36 ++++++++++++++++++++++++++++-------- rules.ts | 51 ++++++++++++++++++++++++++------------------------- types.d.ts | 1 + 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/rules.js b/rules.js index d792ea0..3709972 100644 --- a/rules.js +++ b/rules.js @@ -206,7 +206,7 @@ const engine_functions = { start_year, resolve_fascist_test, resolve_final_bid, - log_trigger, + place_blank_marker, card1_event2, card3_event2, card10_event2, @@ -456,6 +456,7 @@ function setup(seed, _scenario, options) { [], ], triggered_track_effects: [], + untriggered_track_effects: [], log: [], undo: [], used_medallions: [], @@ -1692,7 +1693,7 @@ states.player_turn = { function remove_blank_marker(b) { const track_id = Math.floor(b / 11); const space_id = b % 11; - array_remove_item(game.triggered_track_effects, b); + set_delete(game.triggered_track_effects, b); logi(`Removed Blank from ${get_track_name(track_id)} ${space_id}`); } states.remove_blank_marker = { @@ -2573,15 +2574,16 @@ function move_track_to(track_id, new_value) { : make_list(new_value, current_value - 1); triggered_spaces.forEach((space_id) => { const trigger = tracks[track_id].triggers[space_id]; + const blank = get_blank_marker_id(track_id, 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)); - } + !game.triggered_track_effects.includes(blank) && + !game.untriggered_track_effects.includes(blank)) { + set_delete(game.triggered_track_effects, blank); + set_add(game.untriggered_track_effects, blank); const node = resolve_effect(trigger, tracks[track_id].action); if (node !== null) { insert_after_active_node(node); - insert_after_active_node(create_function_node('log_trigger', [track_id, space_id])); + insert_after_active_node(create_function_node('place_blank_marker', [track_id, space_id])); } } }); @@ -2888,9 +2890,12 @@ function log_header(msg, prefix) { log(`#${prefix} ${msg}`); log_br(); } -function log_trigger(args) { +function place_blank_marker(args) { let [track_id, space_id] = args; + let blank = get_blank_marker_id(track_id, space_id); log(`Trigger ${get_track_name(track_id)} ${space_id}:`); + set_delete(game.untriggered_track_effects, blank); + set_add(game.triggered_track_effects, blank); resolve_active_and_proceed(); } function logi(msg) { @@ -3190,6 +3195,21 @@ function array_insert(array, index, item) { array[i] = array[i - 1]; array[index] = item; } +function set_add(set, item) { + 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, item) { let a = 0; let b = set.length - 1; diff --git a/rules.ts b/rules.ts index 30272b1..3d0603a 100644 --- a/rules.ts +++ b/rules.ts @@ -335,7 +335,7 @@ const engine_functions: Record = { start_year, resolve_fascist_test, resolve_final_bid, - log_trigger, + place_blank_marker, // Unique card effects card1_event2, card3_event2, @@ -652,6 +652,7 @@ export function setup(seed: number, _scenario: string, options: Record { const trigger = tracks[track_id].triggers[space_id]; + const blank = get_blank_marker_id(track_id, space_id); if ( trigger !== null && - !game.triggered_track_effects.includes( - get_blank_marker_id(track_id, space_id) - ) + !game.triggered_track_effects.includes(blank) && + !game.untriggered_track_effects.includes(blank) ) { - if (space_id !== 0) { - game.triggered_track_effects.push( - get_blank_marker_id(track_id, space_id) - ); - } + set_delete(game.triggered_track_effects, blank); + set_add(game.untriggered_track_effects, blank); const node = resolve_effect(trigger, tracks[track_id].action); if (node !== null) { insert_after_active_node(node); - insert_after_active_node(create_function_node('log_trigger', [track_id, space_id])); + insert_after_active_node(create_function_node('place_blank_marker', [track_id, space_id])); } } }); @@ -3580,9 +3578,12 @@ function log_header(msg: string, prefix: string | number) { log_br(); } -function log_trigger(args) { +function place_blank_marker(args) { let [ track_id, space_id ] = args; + let blank = get_blank_marker_id(track_id, space_id); log(`Trigger ${get_track_name(track_id)} ${space_id}:`); + set_delete(game.untriggered_track_effects, blank); + set_add(game.triggered_track_effects, blank); resolve_active_and_proceed(); } @@ -3970,19 +3971,19 @@ function array_insert(array: T[], index: number, item: T) { // 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_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; diff --git a/types.d.ts b/types.d.ts index 743804b..0b901d5 100644 --- a/types.d.ts +++ b/types.d.ts @@ -75,6 +75,7 @@ export interface Game { tracks: number[]; trash: CardId[][]; triggered_track_effects: number[]; + untriggered_track_effects: number[]; used_medallions: number[]; glory_current_year?: boolean[] | null; fascist: 0 | 1 | 2; -- cgit v1.2.3