diff options
author | Tor Andersson <tor@ccxvii.net> | 2025-03-28 20:46:01 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2025-03-28 20:46:01 +0100 |
commit | ddf0b64e149e6548e5abf1326ea986dae26a2c27 (patch) | |
tree | 5ec25bbcae023acc96989b64bb07cbee2bacb1f9 | |
parent | 7c26e81c096bf5fdbe183de4a1c32870b0fb43cf (diff) | |
download | land-and-freedom-ddf0b64e149e6548e5abf1326ea986dae26a2c27.tar.gz |
Use separate untriggered array to delay placing blank markers.
-rw-r--r-- | rules.js | 36 | ||||
-rw-r--r-- | rules.ts | 51 | ||||
-rw-r--r-- | types.d.ts | 1 |
3 files changed, 55 insertions, 33 deletions
@@ -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; @@ -335,7 +335,7 @@ const engine_functions: Record<string, Function> = { 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<string,bo [], ], triggered_track_effects: [], + untriggered_track_effects: [], log: [], undo: [], used_medallions: [], @@ -2066,7 +2067,7 @@ states.player_turn = { function remove_blank_marker(b: number) { 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}`); } @@ -3152,21 +3153,18 @@ function move_track_to(track_id: number, new_value: number) { 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) - ) + !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<T>(array: T[], index: number, item: T) { // return false; // } -// function set_add<T>(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<T>(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<T>(set: T[], item: T) { // let a = 0; @@ -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; |