diff options
Diffstat (limited to 'rules.ts')
-rw-r--r-- | rules.ts | 69 |
1 files changed, 35 insertions, 34 deletions
@@ -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]; |