diff options
author | Tor Andersson <tor@ccxvii.net> | 2025-03-20 11:57:26 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2025-03-20 12:15:56 +0100 |
commit | 6d986f19f52f5c67f3ff55f417c26b3ed053f379 (patch) | |
tree | 6880af25558c74fedc55760656e8f20ce48240e9 | |
parent | b8238aacafd983b0fd0c2bad4f5a497ad924b753 (diff) | |
download | land-and-freedom-6d986f19f52f5c67f3ff55f417c26b3ed053f379.tar.gz |
Fix swap_card_tableau_hand use of selected_cards array.
When we changed to keep the currently selected card in the hand while
executing it, this function stopped working correctly.
-rw-r--r-- | rules.js | 41 | ||||
-rw-r--r-- | rules.ts | 69 |
2 files changed, 60 insertions, 50 deletions
@@ -1857,22 +1857,27 @@ states.swap_card_tableau_hand = { gen_spend_hero_points(); view.prompt = 'Swap a card in your tableau with a card in your hand.'; const faction = get_active_faction(); + const selected_cards = game.selected_cards[faction]; + const hand = game.hands[faction]; + const tableau = game.tableaus[faction]; gen_action('skip'); - if (game.tableaus[faction].length === 0) { + if (tableau.length === 0) { view.prompt = 'No card in your tableau to swap.'; return; } - if (game.hands[faction].length === 0) { + if (hand.length === 1) { view.prompt = 'No card in your hand to swap.'; 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 (selected_cards.length === 1) { + for (const c of hand) { + if (selected_cards[0] !== c) { + 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]) { + if (selected_cards.length === 2) { + for (const c of tableau) { gen_action_card(c); } } @@ -1884,22 +1889,20 @@ states.swap_card_tableau_hand = { 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; + if (selected_cards.length === 3) { 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] = []; + array_remove_item(hand, selected_cards[1]); + array_remove_item(tableau, selected_cards[2]); + hand.push(selected_cards[2]); + tableau.push(selected_cards[1]); + game.selected_cards[faction].length = 1; resolve_active_and_proceed(); } }, skip() { const faction = get_active_faction(); - game.selected_cards[faction] = []; + game.selected_cards[faction].length = 1; resolve_active_and_proceed(); }, }; @@ -3026,6 +3029,12 @@ function object_copy(original) { return copy; } } +function array_remove_item(array, item) { + let n = array.length; + for (let i = 0; i < n; ++i) + if (array[i] === item) + return array_remove(array, i); +} function array_remove(array, index) { let n = array.length; for (let i = index + 1; i < n; ++i) @@ -2260,36 +2260,42 @@ states.spend_hero_points = { }, }; +/* + selected_cards[faction] is always length 1 when executing an event (before and after this state). + selected_cards[faction][0]: currently executing card. + selected_cards[faction][1]: card from hand to put into tableau. + selected_cards[faction][2]: card from tableau to put into hand. + Use the length of selected_cards[faction] to figure out where we are. +*/ states.swap_card_tableau_hand = { inactive: 'swap cards', prompt() { gen_spend_hero_points(); view.prompt = 'Swap a card in your tableau with a card in your hand.'; const faction = get_active_faction(); + const selected_cards = game.selected_cards[faction]; + const hand = game.hands[faction]; + const tableau = game.tableaus[faction]; gen_action('skip'); - if (game.tableaus[faction].length === 0) { + if (tableau.length === 0) { view.prompt = 'No card in your tableau to swap.'; return; } - if (game.hands[faction].length === 0) { + // hand always includes the executing event card! + if (hand.length === 1) { view.prompt = 'No card in your hand to swap.'; 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 (selected_cards.length === 1) { + for (const c of hand) { + // do not swap currently executing card! + if (selected_cards[0] !== c) { + 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]) { + if (selected_cards.length === 2) { + for (const c of tableau) { gen_action_card(c); } } @@ -2300,33 +2306,21 @@ states.swap_card_tableau_hand = { 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; - + if (selected_cards.length === 3) { 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] = []; + array_remove_item(hand, selected_cards[1]); + array_remove_item(tableau, selected_cards[2]); + hand.push(selected_cards[2]); + tableau.push(selected_cards[1]); + game.selected_cards[faction].length = 1; resolve_active_and_proceed(); } }, skip() { const faction = get_active_faction(); - game.selected_cards[faction] = []; + game.selected_cards[faction].length = 1; resolve_active_and_proceed(); }, }; @@ -3776,6 +3770,13 @@ function object_copy(original) { // Array remove and insert (faster than splice) +function array_remove_item<T>(array: T[], item: T) { + let n = array.length + for (let i = 0; i < n; ++i) + if (array[i] === item) + return array_remove(array, i) +} + function array_remove<T>(array: T[], index: number) { let n = array.length; for (let i = index + 1; i < n; ++i) array[i - 1] = array[i]; |