diff --git a/src/constants/card_data_constants.asm b/src/constants/card_data_constants.asm index beba938..67e8260 100644 --- a/src/constants/card_data_constants.asm +++ b/src/constants/card_data_constants.asm @@ -7,6 +7,9 @@ DEF CARD_NOT_OWNED_F EQU 7 DEF CARD_NOT_OWNED EQU 1 << CARD_NOT_OWNED_F DEF CARD_COUNT_MASK EQU $7f +DEF CARD_COUNT_FROM_BUILT_DECKS EQU CARD_NOT_OWNED ; $80 +; on the other hand, counting all owned cards uses $0 and $ff inconsistently + ; sDeck* and generic deck constants DEF NUM_DECKS EQU 4 DEF DECK_NAME_SIZE EQU 24 @@ -17,7 +20,10 @@ DEF DECK_STRUCT_SIZE EQU DECK_NAME_SIZE + DECK_SIZE_BYTES DEF DECK_COMPRESSED_SIZE EQU ((DECK_SIZE + 7) / 8 + 1) * 8 DEF DECK_COMPRESSED_STRUCT_SIZE EQU DECK_NAME_SIZE + DECK_COMPRESSED_SIZE DEF DECK_CONFIG_BUFFER_SIZE EQU 80 +DEF DECK_TEMP_BUFFER_SIZE EQU 128 ; as DECK_COMPRESSED_STRUCT_SIZE + 32, DECK_SIZE_BYTES + 8, etc. +DEF DECK_BIG_TEMP_BUFFER_SIZE EQU 256 DEF MAX_NUM_SAME_NAME_CARDS EQU 4 +DEF MAX_NUM_SUB_ENERGY_CARDS EQU 9 DEF MAX_UNNAMED_DECK_NUM EQU 999 ; card data offsets (data/cards.asm and card_data_struct) diff --git a/src/constants/menu_constants.asm b/src/constants/menu_constants.asm index 80165d7..f0b4925 100644 --- a/src/constants/menu_constants.asm +++ b/src/constants/menu_constants.asm @@ -64,6 +64,13 @@ const MINICOMMENU_MAILBOX ; 1 const MINICOMMENU_CARD_ALBUM ; 2 +; deck save machine options + const_def + const DECKSAVEMACHINEMENU_SAVE ; 0 + const DECKSAVEMACHINEMENU_DELETE ; 1 + const DECKSAVEMACHINEMENU_BUILD ; 2 + const DECKSAVEMACHINEMENU_CANCEL ; 3 + ; mailbox options const_def const MAILBOXMENU_READ ; 0 @@ -99,6 +106,10 @@ const POPUPMENU_CARD_DUNGEON_QUEEN ; $9 const POPUPMENU_CARD_DUNGEON_PAWN ; $a +; for menu items, PlaySFXConfirmOrCancel, etc. +DEF MENU_CANCEL EQU -1 +DEF MENU_CONFIRM EQU 1 ; != -1, but uses 1 most of the time + ; filter types for CardTypeFilters ; used to categorise the different cards ; i.e. in the deck building screen @@ -121,6 +132,7 @@ DEF FILTER_ONLY_ENERGY EQU FILTER_ENERGY >> 4 ; 2, or any number > 1 DEF NUM_DECK_CONFIRMATION_VISIBLE_CARDS EQU 7 DEF NUM_FILTERED_LIST_VISIBLE_CARDS EQU 6 +DEF NUM_DECK_STATUS_LIST_VISIBLE_CARDS EQU 5 DEF NUM_DECK_SAVE_MACHINE_SLOTS EQU 50 DEF NUM_DECK_MACHINE_VISIBLE_DECKS EQU 5 diff --git a/src/constants/sfx_constants.asm b/src/constants/sfx_constants.asm index d334d20..0655e90 100644 --- a/src/constants/sfx_constants.asm +++ b/src/constants/sfx_constants.asm @@ -162,7 +162,3 @@ const SFX_GHOST_MASTER_DISAPPEAR ; $a0 DEF NUM_SFX EQU const_value - -; PlaySFXConfirmOrCancel args -DEF MENU_CANCEL EQU -1 -DEF MENU_CONFIRM EQU 1 ; != -1, but uses 1 most of the time diff --git a/src/engine/bank02.asm b/src/engine/bank02.asm index bb78577..5ce0fc7 100644 --- a/src/engine/bank02.asm +++ b/src/engine/bank02.asm @@ -1532,6 +1532,8 @@ DeckSelectScreenTextItems: textitem 12, 16, CancelDeckText textitems_end +; for X = [wCurDeck] + 1, +; return hl = sDeckX GetSRAMPointerToCurDeck: ld a, [wCurDeck] ld h, a @@ -1543,6 +1545,8 @@ GetSRAMPointerToCurDeck: pop de ret +; for X = [wCurDeck] + 1, +; return hl = sDeckXCards GetSRAMPointerToCurDeckCards: push af ld a, [wCurDeck] @@ -1689,7 +1693,7 @@ PlaySFXConfirmOrCancel: pop af ret -Func_9337: +DecrementDeckCardsInCollection_CopyDeckFromSRAM: push hl ld d, h ld e, l @@ -1749,7 +1753,7 @@ AddGiftCenterDeckCardsToCollection: push hl push de push bc - ld a, ALL_DECKS + ld a, $ff ; all owned cards call CreateCardCollectionListWithDeckCards pop bc pop de @@ -3585,10 +3589,10 @@ InitializeScrollMenuParameters: ret DeckMachineSelectionParams: - scrollmenu_params 1, 2, 2, 0, 5, SYM_CURSOR_R, SYM_SPACE, NULL -; 0x9eb5 + scrollmenu_params 1, 2, 2, 0, NUM_DECK_MACHINE_VISIBLE_DECKS, SYM_CURSOR_R, SYM_SPACE, NULL -SECTION "Bank 2@5ebe", ROMX[$5ebe], BANK[$2] +MenuParams_9eb5: + scrollmenu_params 1, 2, 2, 0, 4, SYM_CURSOR_R, SYM_SPACE, NULL HandleCardSelectionInput: xor a ; FALSE @@ -4336,7 +4340,7 @@ HandleDeckConfirmationMenu: ld a, MENU_CONFIRM call PlaySFXConfirmOrCancel ld a, [wCurScrollMenuItem] - ld [wced7], a + ld [wd11e], a ; set wOwnedCardsCountList as current card list ; and show card page screen @@ -4913,7 +4917,7 @@ GetCardTypeIconPalette: ; uses deck builder ui HandleGiftCenterSendCardsScreen: ld hl, wCurDeckCards - ld a, $81 + ld a, DECK_TEMP_BUFFER_SIZE + 1 call ClearNBytesFromHL ld a, $ff ld [wCurDeck], a @@ -5004,7 +5008,7 @@ HandleGiftCenterSendCardsMenu: call InitializeScrollMenuParameters ld hl, wCurDeckCards ld de, wTempSavedDeckCards - ld b, $82 + ld b, DECK_TEMP_BUFFER_SIZE + 2 call CopyBBytesFromHLToDE_Bank02 call PrintCardsToSendToPlayerText call Func_b81d @@ -5031,7 +5035,7 @@ Func_a6ef: ld [wScrollMenuScrollOffset], a ld hl, wCurDeckCards ld de, wTempSavedDeckCards - ld b, $82 + ld b, DECK_TEMP_BUFFER_SIZE + 2 call CopyBBytesFromHLToDE_Bank02 call EmptyScreenAndDrawTextBox call Func_b81d @@ -5039,7 +5043,7 @@ Func_a6ef: HandleBlackBoxSendCardsScreen: ld hl, wCurDeckCards - ld a, $81 + ld a, DECK_TEMP_BUFFER_SIZE + 1 call ClearNBytesFromHL ld a, $ff ld [wCurDeck], a @@ -5268,7 +5272,7 @@ PrintFilteredCardSelectionList: add hl, bc ld a, [hl] push af - ld a, ALL_DECKS + ld a, $ff ; all owned cards call CreateCardCollectionListWithDeckCards ld a, TRUE ld [wd121], a @@ -5289,16 +5293,16 @@ PrintFilteredCardSelectionList: ret ; creates a card collection list in wTempCardCollection -; if a is $80, only include cards that are in the -; built decks, otherwise include all owned cards +; if a = CARD_COUNT_FROM_BUILT_DECKS, only include cards used in the built decks +; otherwise include all owned cards CreateCardCollectionListWithDeckCards: - cp $80 + cp CARD_COUNT_FROM_BUILT_DECKS jr nz, .copy_card_collection ld hl, wTempCardCollection - xor a + xor a ; aka $100 bytes call ClearNBytesFromHL ld hl, wTempCardCollection + $100 - xor a + xor a ; aka $100 bytes call ClearNBytesFromHL ld a, ALL_DECKS ld [hffbf], a @@ -5311,11 +5315,11 @@ CreateCardCollectionListWithDeckCards: call EnableSRAM ld hl, sCardCollection ld de, wTempCardCollection - ld b, $00 ; aka $100 bytes + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank02 ld hl, sCardCollection + $100 ld de, wTempCardCollection + $100 - ld b, $00 ; aka $100 bytes + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank02 call DisableSRAM .deck_1 @@ -5546,7 +5550,7 @@ PrintPlayersCardsText: ret PrintTotalNumberOfCardsInCollection: - ld a, ALL_DECKS + ld a, $ff ; all owned cards call CreateCardCollectionListWithDeckCards ; count all the cards in collection @@ -5556,7 +5560,7 @@ PrintTotalNumberOfCardsInCollection: .loop_all_cards ld a, [bc] inc bc - and $7f + and CARD_COUNT_MASK push de ld d, $00 ld e, a @@ -7132,14 +7136,18 @@ PrinterMenu: .PrinterQualityMenuParams scrollmenu_params 5, 16, 0, 2, 5, SYM_CURSOR_R, SYM_SPACE, NULL -Func_b57c: +; unlike tcg1's HandleDeckMissingCardsList, +; it's now a multipurpose screen +HandleDeckStatusCardList: push de ld de, wCurDeckName call CopyListFromHLToDE pop de ld hl, wCurDeckCards call CopyDeckFromSRAM -.asm_b58a + ; fallthrough + +HandleDeckStatusCardList_Execute: ld a, NUM_FILTERS ld hl, wCardFilterCounts call ClearNBytesFromHL @@ -7147,10 +7155,10 @@ Func_b57c: ld [wTotalCardCount], a ld hl, wCardFilterCounts ld [hl], a - call Func_b5b1 + call _HandleDeckStatusCardList ret -Func_b59f: +HandleDeckStatusCardList_InSRAM: push de ld de, wCurDeckName call CopyListFromHLToDEInSRAM @@ -7158,30 +7166,32 @@ Func_b59f: ld de, wCurDeckCards ld b, $80 call CopyBBytesFromHLToDE_Bank02 - jr Func_b57c.asm_b58a + jr HandleDeckStatusCardList_Execute -Func_b5b1: +_HandleDeckStatusCardList: call SortCurDeckCardsByID call CreateCurDeckUniqueCardList xor a ld [wScrollMenuScrollOffset], a -.asm_b5bb - ld hl, $7621 +.init_params + ld hl, .menu_params call InitializeScrollMenuParameters ld a, [wNumUniqueCards] ld [wNumCardListEntries], a - cp $05 - jr c, .asm_b5cd - ld a, $05 -.asm_b5cd + cp NUM_DECK_STATUS_LIST_VISIBLE_CARDS + jr c, .no_cap + ld a, NUM_DECK_STATUS_LIST_VISIBLE_CARDS +.no_cap ld [wNumMenuItems], a ld [wNumVisibleCardListEntries], a - call Func_b649 - ld hl, wd38a + call .PrintTitleAndList + ld hl, wCardConfirmationText ld a, [hli] ld h, [hl] ld l, a call DrawWideTextBox_PrintText + +; set scroll func ld hl, PrintConfirmationCardList ld d, h ld a, l @@ -7190,38 +7200,57 @@ Func_b5b1: ld [hl], d xor a ld [wd119], a -.asm_b5ed + +.loop_input call DoFrame call HandleScrollListInput - jr c, .asm_b619 + jr c, .selection_made call HandleJumpListInput - jr c, .asm_b5ed + jr c, .loop_input ldh a, [hDPadHeld] and PAD_START - jr z, .asm_b5ed -.asm_b600 + jr z, .loop_input + +; using wUniqueDeckCardList +.open_card_page ld a, MENU_CONFIRM call PlaySFXConfirmOrCancel ld a, [wTempCardTypeFilter] - ld [wced7], a + ld [wd11e], a ld de, wUniqueDeckCardList ld hl, wCurCardListPtr ld [hl], e inc hl ld [hl], d call OpenCardPageFromCardList - jr .asm_b5bb -.asm_b619 + jr .init_params + +.selection_made ld a, [hCurMenuItem] - cp $ff + cp MENU_CANCEL ret z - jr .asm_b600 -; 0xb621 + jr .open_card_page -SECTION "Bank 2@7649", ROMX[$7649], BANK[$2] +.menu_params + scrollmenu_params 0, 3, 2, 0, NUM_DECK_STATUS_LIST_VISIBLE_CARDS, SYM_CURSOR_R, SYM_SPACE, NULL -Func_b649: - call Func_b659 +.ScrollFunc_TCG1: + ld hl, hffbb + ld [hl], $01 + call .PrintDeckIndexAndName + lb de, 1, 14 + call InitTextPrinting + ld hl, wCardConfirmationText + ld a, [hli] + ld h, [hl] + ld l, a + call ProcessTextFromID + ld hl, hffbb + ld [hl], $00 + jp PrintConfirmationCardList + +.PrintTitleAndList: + call .ClearScreenAndPrintDeckTitle lb de, 3, 3 ld hl, wCardListCoords ld [hl], e @@ -7230,16 +7259,16 @@ Func_b649: call PrintConfirmationCardList ret -Func_b659: +.ClearScreenAndPrintDeckTitle: call EmptyScreenAndLoadFontDuelAndHandCardsIcons - call Func_b663 + call .PrintDeckIndexAndName call EnableLCD ret -Func_b663: +.PrintDeckIndexAndName: ld a, [wCurDeckName] or a - ret z + ret z ; not a valid deck lb de, 0, 1 call InitTextPrinting ld a, [wCurDeck] @@ -7251,6 +7280,7 @@ Func_b663: ld [hl], TX_END ld hl, wDefaultText call ProcessText + ld hl, wCurDeckName ld de, wDefaultText call CopyListFromHLToDE @@ -7862,8 +7892,8 @@ DeckDiagnosisResult: pop af ret -Func_baec: - farcall Func_3ada1 +HandleDeckSaveMachineMenu: + farcall _HandleDeckSaveMachineMenu ret HandleAutoDeckMenu: diff --git a/src/engine/bank07.asm b/src/engine/bank07.asm index 129dbab..824428a 100644 --- a/src/engine/bank07.asm +++ b/src/engine/bank07.asm @@ -5675,7 +5675,7 @@ CallMinicomMenuFunction: MinicomDeckSaveMachine: farcall ClearSpriteAnimsAndSetInitialGraphicsConfiguration - farcall Func_baec + farcall HandleDeckSaveMachineMenu ret MinicomCardAlbum: diff --git a/src/engine/bank0a.asm b/src/engine/bank0a.asm index dd9ddbb..d3fa76d 100644 --- a/src/engine/bank0a.asm +++ b/src/engine/bank0a.asm @@ -3419,14 +3419,14 @@ Func_2bbbb: ld e, [hl] inc hl ld d, [hl] - ld hl, wc000 + ld hl, wDeckToBuild xor a - ld b, $82 + ld b, DECK_TEMP_BUFFER_SIZE + 2 .asm_2bbc8 ld [hli], a dec b jr nz, .asm_2bbc8 - ld hl, wc000 + ld hl, wDeckToBuild .asm_2bbcf ld a, [de] inc de @@ -3447,9 +3447,9 @@ Func_2bbbb: jr .asm_2bbcf .asm_2bbe2 pop hl - ld bc, $18 + ld bc, DECK_NAME_SIZE add hl, bc - ld de, wc000 + ld de, wDeckToBuild farcall SwitchToWRAM2 bank1call SaveDeckCards farcall SwitchToWRAM1 @@ -3600,10 +3600,10 @@ _HandleAutoDeckMenu: ld a, [wScrollMenuScrollOffset] ld [wd54a], a ld b, a - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] ld [wd54b], a add b - ld hl, wd49f + ld hl, wNumCardsNeededToBuildDeckMachineDecks ld c, a ld b, $00 add hl, bc @@ -3651,7 +3651,7 @@ _HandleAutoDeckMenu: Func_2bd48: ld a, $08 - ld hl, wd49f + ld hl, wNumCardsNeededToBuildDeckMachineDecks farcall ClearNBytesFromHL ld a, $08 ld hl, wd4b4 @@ -3696,7 +3696,7 @@ Func_2bd7f: ret Func_2bd92: - ld hl, wd49f + ld hl, wNumCardsNeededToBuildDeckMachineDecks push bc ld a, b inc a diff --git a/src/engine/bank0e.asm b/src/engine/bank0e.asm index 55accb8..13ab07a 100644 --- a/src/engine/bank0e.asm +++ b/src/engine/bank0e.asm @@ -4848,7 +4848,7 @@ UpdateBoosterPackMenuArrows: call WriteByteToBGMap0 ret -Func_3ada1: +_HandleDeckSaveMachineMenu: xor a ld [wScrollMenuScrollOffset], a ldtx de, DeckSaveMachineText @@ -4859,8 +4859,9 @@ Func_3ada1: call ClearScreenAndDrawDeckMachineScreen ld a, NUM_DECK_SAVE_MACHINE_SLOTS ld [wNumDeckMachineEntries], a + xor a -.asm_3adb7 +.wait_input ld hl, DeckMachineSelectionParams farcall InitializeScrollMenuParameters call DrawListScrollArrows @@ -4870,82 +4871,102 @@ Func_3ada1: ldtx de, PleaseSelectDeckText call InitDeckMachineDrawingParams call HandleDeckMachineSelection - jr c, .asm_3adb7 - cp $ff + jr c, .wait_input + cp MENU_CANCEL ret z +; get deck index ld b, a ld a, [wScrollMenuScrollOffset] add b ld [wSelectedDeckMachineEntry], a + farcall ResetCheckMenuCursorPositionAndBlink call DrawWideTextBox - ld hl, $6e7b + ld hl, .text_items call PlaceTextItems -.asm_3aded: +.wait_input_submenu call DoFrame farcall HandleCheckMenuInput - jp nc, .asm_3aded - cp $ff - jr nz, .asm_3ae01 + jp nc, .wait_input_submenu + cp MENU_CANCEL + jr nz, .submenu_option_selected +; return from submenu ld a, [wTempScrollMenuItem] - jp .asm_3adb7 -.asm_3ae01 + jp .wait_input + +.submenu_option_selected ld a, [wCheckMenuCursorYPosition] sla a ld hl, wCheckMenuCursorXPosition add [hl] or a - jr nz, .asm_3ae33 + jr nz, .next_submenu_1 + +; save call CheckIfSelectedDeckMachineEntryIsEmpty - jr nc, .asm_3ae1d - call Func_3b2db + jr nc, .overwrite + call SaveDeckInDeckSaveMachine ld a, [wTempScrollMenuItem] - jp c, .asm_3adb7 - jr .asm_3ae65 -.asm_3ae1d + jp c, .wait_input + jr .return_to_list + +.overwrite ldtx hl, DeleteSavedDeckPromptText call YesOrNoMenuWithText ld a, [wTempScrollMenuItem] - jr c, .asm_3adb7 - call Func_3b2db + jr c, .wait_input + call SaveDeckInDeckSaveMachine ld a, [wTempScrollMenuItem] - jp c, .asm_3adb7 - jr .asm_3ae65 -.asm_3ae33 - cp $01 - jr nz, .asm_3ae53 + jp c, .wait_input + jr .return_to_list + +.next_submenu_1 + cp DECKSAVEMACHINEMENU_DELETE + jr nz, .next_submenu_2 + +; delete call CheckIfSelectedDeckMachineEntryIsEmpty - jr c, .asm_3ae47 - call Func_3b3fa + jr c, .is_empty + call TryDeleteSavedDeck ld a, [wTempScrollMenuItem] - jp c, .asm_3adb7 - jr .asm_3ae65 -.asm_3ae47 + jp c, .wait_input + jr .return_to_list + +.is_empty ldtx hl, NoDecksSavedToMachineText call DrawWideTextBox_WaitForInput ld a, [wTempScrollMenuItem] - jp .asm_3adb7 -.asm_3ae53 - cp $02 - jr nz, .asm_3ae7a + jp .wait_input + +.next_submenu_2 + cp DECKSAVEMACHINEMENU_BUILD + jr nz, .cancel + +; build call CheckIfSelectedDeckMachineEntryIsEmpty - jr c, .asm_3ae47 - call Func_3b4eb + jr c, .is_empty + call TryBuildDeckMachineDeck ld a, [wTempScrollMenuItem] - jp nc, .asm_3adb7 -.asm_3ae65 + jp nc, .wait_input + +.return_to_list ld a, [wTempScrollMenuScrollOffset] ld [wScrollMenuScrollOffset], a call ClearScreenAndDrawDeckMachineScreen call DrawListScrollArrows call PrintNumSavedDecks ld a, [wTempScrollMenuItem] - jp .asm_3adb7 -.asm_3ae7a - ret -; 0x3ae7b + jp .wait_input -SECTION "Bank e@6e8c", ROMX[$6e8c], BANK[$e] +.cancel + ret + +.text_items + textitem 2, 14, SaveDeckToMachineText ; DECKSAVEMACHINEMENU_SAVE + textitem 12, 14, DeleteDeckFromMachineText ; DECKSAVEMACHINEMENU_DELETE + textitem 2, 16, BuildDeckText ; DECKSAVEMACHINEMENU_BUILD + textitem 12, 16, CancelDeckText ; DECKSAVEMACHINEMENU_CANCEL + textitems_end ; sets the number of cursor positions for deck machine menu, ; sets the text ID to show given by de @@ -4968,7 +4989,7 @@ InitDeckMachineDrawingParams: ld [wd119], a ret - ; handles player input inside the Deck Machine screen +; handles player input inside the Deck Machine screen ; the Start button opens up the deck confirmation menu ; and returns carry ; otherwise, returns no carry and selection made in a @@ -4996,23 +5017,23 @@ HandleDeckMachineSelection: or $80 ld [wCurDeck], a - ; get pointer to selected deck cards - ; and if it's an empty deck, jump to start +; get pointer to selected deck cards +; and if it's an empty deck, jump to start ld a, c - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr push hl farcall CheckIfDeckHasCards pop hl jr c, .start - -; show deck confirmation screen with deck cards -; and return carry set push hl ld bc, DECK_NAME_SIZE add hl, bc ld d, h ld e, l pop hl + +; show deck confirmation screen with deck cards +; and return carry set ld a, MENU_CONFIRM farcall PlaySFXConfirmOrCancel farcall OpenDeckConfirmationMenu @@ -5074,8 +5095,8 @@ HandleDeckMachineSelection: ld [wScrollMenuScrollOffset], a cp c jr z, .set_carry - ; play SFX if jump was made - ; and update UI + +; play SFX on jump and update UI ld a, SFX_CURSOR call PlaySFX call DrawDeckMachineScreen @@ -5088,7 +5109,7 @@ HandleDeckMachineSelection: ; entry selected in the Deck Machine menu is empty CheckIfSelectedDeckMachineEntryIsEmpty: ld a, [wSelectedDeckMachineEntry] - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr farcall CheckIfDeckHasCards ret @@ -5097,7 +5118,7 @@ ClearScreenAndDrawDeckMachineScreen: ld [wTileMapFill], a call ZeroObjectPositions call EmptyScreen - ld a, $01 + ld a, TRUE ld [wVBlankOAMCopyToggle], a call LoadSymbolsFont call LoadDuelCardSymbolTiles @@ -5142,8 +5163,11 @@ CopyListFromHLToDE_Bank0e: inc de jr .loop -; a = deck index in wMachineDeckPtrs -Func_3afb8: +; return hl = wMachineDeckPtrs[a] +; if that points at wram (not at sram, etc.), +; also load its data to wSelectedMachineDeck and wBackup3DeckToBuild, +; and overwrite hl = wSelectedMachineDeck +GetAndLoadSelectedMachineDeckPtr: push bc push de add a ; *2 @@ -5160,27 +5184,27 @@ Func_3afb8: call SwitchToWRAM2 push hl - ld hl, wc000 - ld de, w2d58e - ld b, $80 + ld hl, wDeckToBuild + ld de, wBackup3DeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e pop hl - ld de, wc000 - ld b, $80 + ld de, wDeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - ld hl, wc000 - ld de, wd4c8 - ld b, $80 + ld hl, wDeckToBuild + ld de, wSelectedMachineDeck + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM2 - ld hl, w2d58e - ld de, wc000 - ld b, $80 + ld hl, wBackup3DeckToBuild + ld de, wDeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - ld hl, wd4c8 + ld hl, wSelectedMachineDeck .done pop de pop bc @@ -5231,9 +5255,10 @@ PrintVisibleDeckMachineEntries: inc e inc e jr .loop -; 0x3b043 -SECTION "Bank e@7048", ROMX[$7048], BANK[$e] +UpdateDeckMachineScrollArrowsAndEntries: + call DrawListScrollArrows + jr PrintVisibleDeckMachineEntries DrawDeckMachineScreen: call DrawListScrollArrows @@ -5312,10 +5337,10 @@ PrintDeckMachineEntry: ; get the deck corresponding to input index ; and append its name to wDefaultText push af - call Func_3afb8 - inc d - inc d + call GetAndLoadSelectedMachineDeckPtr +REPT 3 ; indent x inc d +ENDR push de farcall AppendDeckName pop de @@ -5330,53 +5355,52 @@ PrintDeckMachineEntry: ld d, 12 inc e call InitTextPrinting - ld hl, .text + ld hl, .spaces call ProcessText scf ret +; b = deck index .valid_deck push de push bc - ld a, $0 ; no decks dismantled + ld a, 0 ; no decks dismantled call CheckIfCanBuildSavedDeck pop bc ld hl, wDefaultText jr c, .cannot_build - ; deck can be built +; can build xor a - ld [wd49b], a - ld hl, wd49f + ld [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks], a + ld hl, wNumCardsNeededToBuildDeckMachineDecks ld d, $00 ld e, b sla e ; *2 add hl, de ld [hl], a - ld [wd49e], a - ld hl, wd49f + ld [wNumCardsNeededToBuildSelectedDeckMissingInCardCollection], a + ld hl, wNumCardsNeededToBuildDeckMachineDecks ld d, $00 ld e, b sla e inc e ; *2 + 1 add hl, de ld [hl], a - ldfw de, "○" ; can build - jp .asm_3b18e + ldfw de, "○" ; "can build" symbol + jp .padding .cannot_build - ; deck cannot be built ldfw de, " " call Func_22ca - ; figure out how many cards are being - ; used on the other decks +; figure out how many cards are being used on the other decks push bc - call .CountCardsNeededToBuildInBuiltDecks - ld [wd49b], a + call CountCardsNeededToBuildInBuiltDecks + ld [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks], a pop bc - ld hl, wd49f + ld hl, wNumCardsNeededToBuildDeckMachineDecks ld d, $00 ld e, b sla e ; *2 @@ -5384,11 +5408,11 @@ PrintDeckMachineEntry: ld [hl], a push bc - ld a, ALL_DECKS - call .CountCardsNeededToBuildInCardCollection - ld [wd49e], a + ld a, $ff ; all owned cards + call CountCardsNeededToBuildInCardCollection + ld [wNumCardsNeededToBuildSelectedDeckMissingInCardCollection], a pop bc - ld hl, wd49f + ld hl, wNumCardsNeededToBuildDeckMachineDecks ld d, $00 ld e, b sla e @@ -5402,68 +5426,71 @@ PrintDeckMachineEntry: ld d, 12 inc e call InitTextPrinting - ld hl, .text + ld hl, .spaces call ProcessText pop de pop af push de or a - jr z, .need_dismantle + jr z, .need_dismantle ; can build by dismantling - ; players doesn't own all necessary cards +; necessary cards missing in collection pop de push de inc e - ld d, $10 + ld d, 16 call InitTextPrinting - ldfw de, "×" + ldfw de, "×" ; "missing" symbol call Func_22ca - ld a, [wd49e] + ld a, [wNumCardsNeededToBuildSelectedDeckMissingInCardCollection] ld hl, wDefaultText farcall ConvertToNumericalDigits ld [hl], TX_END ld hl, wDefaultText call ProcessText +; necessary cards used in built decks .need_dismantle pop de - ld a, [wd49b] + ld a, [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks] or a - jr z, .asm_3b18c + jr z, .printed_shortfall inc e ld d, 12 call InitTextPrinting - ldfw de, "※" + ldfw de, "※" ; REF_MARK, "used" symbol call Func_22ca - ld a, [wd49b] + ld a, [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks] ld hl, wDefaultText farcall ConvertToNumericalDigits ld [hl], TX_END ld hl, wDefaultText call ProcessText -.asm_3b18c + +.printed_shortfall or a ret -.asm_3b18e +; clear the card-shortfall text area +.padding call Func_22ca pop de ld d, 12 inc e call InitTextPrinting - ld hl, .text + ld hl, .spaces call ProcessText or a ret -.text +.spaces REPT 7 db "" ENDR done ; de = card ID -.GetCardCountInScratchCardCollection: +GetCardCountInScratchCardCollection: call SwitchToWRAM2 push hl ld hl, wScratchCardCollection @@ -5474,7 +5501,7 @@ ENDR ret ; de = card ID -.DecrementCardCountInScratchCardCollection: +DecrementCardCountInScratchCardCollection: call SwitchToWRAM2 push hl ld hl, wScratchCardCollection @@ -5485,34 +5512,31 @@ ENDR ret ; b = saved deck index -.CountCardsNeededToBuildInBuiltDecks: +; return a = total number of cards required but already used in built decks +CountCardsNeededToBuildInBuiltDecks: push bc - ld a, $80 ; only cards that are in built decks - farcall CreateCardCollectionListWithDeckCards - ; copy to wScratchCardCollection +; set: +; wTempCardCollection = all owned cards +; wScratchCardCollection = all cards used in built decks + ld a, CARD_COUNT_FROM_BUILT_DECKS + farcall CreateCardCollectionListWithDeckCards call SwitchToWRAM2 ld hl, wTempCardCollection ld de, wScratchCardCollection - ld b, 0 ; $100 bytes + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e ld hl, wTempCardCollection + $100 ld de, wScratchCardCollection + $100 - ld b, 0 ; $100 bytes + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - - xor a ; all owned cards + xor a farcall CreateCardCollectionListWithDeckCards + pop bc - - ; here, wTempCardCollection holds all cards that - ; the player owns, including in and out of decks - ; wScratchCardCollection holds all cards that - ; are in built decks only - ld a, b - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr ld bc, DECK_NAME_SIZE add hl, bc ld d, h @@ -5524,7 +5548,7 @@ ENDR lb bc, DECK_SIZE + 1, 0 .loop_deck dec b - jr z, .got_count_1 + jr z, .got_count_already_in_use ld e, [hl] inc hl ld d, [hl] @@ -5540,43 +5564,46 @@ ENDR pop hl jr .loop_deck .zero_count - call .GetCardCountInScratchCardCollection + call GetCardCountInScratchCardCollection or a jr z, .next_card - call .DecrementCardCountInScratchCardCollection + call DecrementCardCountInScratchCardCollection inc c .next_card pop hl jr .loop_deck -.got_count_1 - ld a, c ; total number of cards found in built decks + +.got_count_already_in_use + ld a, c ret ; b = saved deck index -.CountCardsNeededToBuildInCardCollection: +; return a = total number of cards required but missing +CountCardsNeededToBuildInCardCollection: push bc farcall CreateCardCollectionListWithDeckCards pop bc ld a, b - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr ld bc, DECK_NAME_SIZE add hl, bc ld d, h ld e, l ld hl, wTempSavedDeckCards farcall CopyDeckFromSRAM + ld hl, wTempSavedDeckCards lb bc, DECK_SIZE + 1, 0 .loop_collection dec b - jr z, .got_count_2 + jr z, .got_count_missing ld e, [hl] inc hl ld d, [hl] inc hl push hl - ld hl, wc000 + ld hl, wTempCardCollection add hl, de ld a, [hl] and CARD_COUNT_MASK @@ -5590,7 +5617,8 @@ ENDR inc c pop hl jr .loop_collection -.got_count_2 + +.got_count_missing ld a, c ret @@ -5635,46 +5663,72 @@ PrintNumSavedDecks: ld hl, wDefaultText call ProcessText ret -; 0x3b2af -SECTION "Bank e@72db", ROMX[$72db], BANK[$e] +; unused in tcg1 +PrintDeckIndexPerNumSavedDecks: + ld a, [wCurScrollMenuItem] + ld b, a + ld a, [wScrollMenuScrollOffset] + add b + inc a + ld hl, wDefaultText + farcall ConvertToNumericalDigits + ld a, TX_SYMBOL + ld [hli], a + ld a, SYM_SLASH + ld [hli], a + ld a, [wNumSavedDecks] + farcall ConvertToNumericalDigits + ld [hl], TX_END + lb de, 14, 1 + call InitTextPrinting + ld hl, wDefaultText + call ProcessText + ret -Func_3b2db: - ld a, $ff +; for the selected slot (wSelectedDeckMachineEntry), +; handle decks screen menu to choose a deck to save there +; set carry if successfully saved +SaveDeckInDeckSaveMachine: + ld a, ALL_DECKS farcall DrawDecksScreen xor a -.asm_3b2e2 - ld hl, $735e +.wait_input + ld hl, DecksScreenMenuParams call InitializeMenuParameters ldtx hl, ChooseDeckToSaveToMachineText call DrawWideTextBox_PrintText -.asm_3b2ee +.wait_submenu_input call DoFrame farcall HandleStartButtonInDeckSelectionMenu - jr c, .asm_3b2e2 + jr c, .wait_input call HandleMenuInput - jp nc, .asm_3b2ee + jp nc, .wait_submenu_input ldh a, [hCurScrollMenuItem] - cp $ff + cp MENU_CANCEL ret z ld [wCurDeck], a farcall CheckIfCurDeckIsEmpty - jp nc, Func_3b315 + jp nc, .SaveDeckInSelectedEntry ; can be jr farcall PrintThereIsNoDeckHereText ld a, [wCurDeck] - jr .asm_3b2e2 + jr .wait_input -Func_3b315: +; copy sDeckX (X = [wCurDeck]) data +; to sSavedDeckY (Y = [wSelectedDeckMachineEntry]), +; update screen ui, and set carry +.SaveDeckInSelectedEntry: farcall GetSRAMPointerToCurDeck push hl call GetSelectedSavedDeckPtr ld d, h ld e, l pop hl - ld b, $60 + ld b, DECK_COMPRESSED_STRUCT_SIZE call EnableSRAM call CopyBBytesFromHLToDE_Bank0e call DisableSRAM + call ClearScreenAndDrawDeckMachineScreen call DrawListScrollArrows call PrintNumSavedDecks @@ -5693,15 +5747,15 @@ Func_3b315: call DrawWideTextBox_WaitForInput scf ret -; 0x3b35e -SECTION "Bank e@7366", ROMX[$7366], BANK[$e] +DecksScreenMenuParams: + menu_params 1, 2, 3, NUM_DECKS, SYM_CURSOR_R, SYM_SPACE, NULL GetSelectedSavedDeckPtr: push af push de ld a, [wSelectedDeckMachineEntry] - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr pop de pop af ret @@ -5716,7 +5770,7 @@ CheckIfCanBuildSavedDeck: farcall CreateCardCollectionListWithDeckCards pop bc ld a, b - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr ld bc, DECK_NAME_SIZE add hl, bc call CheckIfHasEnoughCardsToBuildDeck @@ -5724,11 +5778,11 @@ CheckIfCanBuildSavedDeck: SwitchToWRAM1: push af - ld a, [wce4c] - cp $01 + ld a, [wTempBankWRAM] + cp BANK("WRAM1") jr z, .skip - ld a, $01 - ld [wce4c], a + ld a, BANK("WRAM1") + ld [wTempBankWRAM], a ldh [rWBK], a .skip pop af @@ -5736,11 +5790,11 @@ SwitchToWRAM1: SwitchToWRAM2: push af - ld a, [wce4c] - cp $02 + ld a, [wTempBankWRAM] + cp BANK("WRAM2") jr z, .skip - ld a, $02 - ld [wce4c], a + ld a, BANK("WRAM2") + ld [wTempBankWRAM], a ldh [rWBK], a .skip pop af @@ -5785,48 +5839,58 @@ CheckIfHasEnoughCardsToBuildDeck: or a ret -Func_3b3d1: - ld hl, sDeck1Name +; return a = DECK_*_F of the first empty slot +; set carry otherwise +FindFirstEmptyDeckSlot: + ld hl, sDeck1 ld a, [hl] or a - jr nz, .asm_3b3da - xor a + jr nz, .check_deck_2 + xor a ; DECK_1_F ret -.asm_3b3da - ld hl, sDeck2Name + +.check_deck_2 + ld hl, sDeck2 ld a, [hl] or a - jr nz, .asm_3b3e4 - ld a, $01 + jr nz, .check_deck_3 + ld a, DECK_2_F ret -.asm_3b3e4 - ld hl, sDeck3Name + +.check_deck_3 + ld hl, sDeck3 ld a, [hl] or a - jr nz, .asm_3b3ee - ld a, $02 + jr nz, .check_deck_4 + ld a, DECK_3_F ret -.asm_3b3ee - ld hl, sDeck4Name + +.check_deck_4 + ld hl, sDeck4 ld a, [hl] or a - jr nz, .asm_3b3f8 - ld a, $03 + jr nz, .no_empty + ld a, DECK_4_F ret -.asm_3b3f8 + +.no_empty scf ret -Func_3b3fa: +; for N = selected saved deck [wSelectedDeckMachineEntry], +; provide delete prompt +; if yes, clear sSavedDeckN +; if no, return a = menu item pos with carry +TryDeleteSavedDeck: ldtx hl, ConfirmDeletePromptText call YesOrNoMenuWithText - jr c, .asm_3b426 + jr c, .no call GetSelectedSavedDeckPtr push hl call EnableSRAM farcall CopyDeckName pop hl - ld a, $60 + ld a, DECK_COMPRESSED_STRUCT_SIZE farcall ClearNBytesFromHL call DisableSRAM xor a @@ -5836,8 +5900,9 @@ Func_3b3fa: call DrawWideTextBox_WaitForInput or a ret -.asm_3b426 - ld a, [wTempCardTypeFilter] + +.no + ld a, [wCurScrollMenuItem] scf ret @@ -5872,57 +5937,62 @@ DrawListScrollArrows: call WriteByteToBGMap0 ret -Func_3b45f: +; decks screen menu to make space for new deck to build +HandleDismantleDeckToMakeSpace: ldtx hl, YouMayOnlyCarry4DecksText call DrawWideTextBox_WaitForInput - ld a, $ff + ld a, ALL_DECKS farcall DrawDecksScreen xor a -.asm_3b46c - ld hl, $735e +.init_menu_params + ld hl, DecksScreenMenuParams call InitializeMenuParameters ldtx hl, ChooseDeckToDismantleText call DrawWideTextBox_PrintText -.asm_3b478 +.loop_input call DoFrame farcall HandleStartButtonInDeckSelectionMenu - jr c, .asm_3b46c + jr c, .init_menu_params call HandleMenuInput - jp nc, .asm_3b478 + jp nc, .loop_input ; can be jr ldh a, [hCurScrollMenuItem] - cp $ff - jr nz, .asm_3b48f + cp MENU_CANCEL + jr nz, .selected_deck scf ret -.asm_3b48f + +.selected_deck ld [wCurDeck], a ldtx hl, DeckBuildingDismantlePromptText call YesOrNoMenuWithText - jr nc, .asm_3b49f + jr nc, .dismantle ld a, [wCurDeck] - jr .asm_3b46c -.asm_3b49f + jr .init_menu_params + +.dismantle farcall GetSRAMPointerToCurDeck push hl - ld de, wd47e + ld de, wDismantledDeckName call EnableSRAM call CopyListFromHLToDE_Bank0e pop hl push hl - ld bc, $18 + ld bc, DECK_NAME_SIZE add hl, bc farcall AddDeckToCollection pop hl - ld a, $60 + ld a, DECK_COMPRESSED_STRUCT_SIZE farcall ClearNBytesFromHL call DisableSRAM - ld a, $ff + +; redraw decks screen + ld a, ALL_DECKS farcall DrawDecksScreen ld a, [wCurDeck] - ld hl, $735e + ld hl, DecksScreenMenuParams call InitializeMenuParameters call DrawCursor2 - ld hl, wd47e + ld hl, wDismantledDeckName farcall CopyDeckName xor a ld [wTxRam2], a @@ -5932,84 +6002,99 @@ Func_3b45f: ld a, [wCurDeck] ret -Func_3b4eb: +TryBuildDeckMachineDeck: call SwitchToWRAM2 xor a - ld [w2d280], a + ld [wNumRemainingBasicEnergyCardsForSubbedDeck], a call SwitchToWRAM1 ld a, [wSelectedDeckMachineEntry] - ld hl, wd49f - sla a + ld hl, wNumCardsNeededToBuildDeckMachineDecks + sla a ; *2 ld b, $00 ld c, a add hl, bc ld a, [hli] - ld [wd49b], a + ld [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks], a ld a, [hl] - ld [wd49e], a + ld [wNumCardsNeededToBuildSelectedDeckMissingInCardCollection], a or a - jr nz, .asm_3b526 - ld a, [wd49b] + jr nz, .not_own_all_cards_needed + ld a, [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks] or a - jr z, .asm_3b53a + jr z, .build_deck + +; can build by dismantling deck(s) ldtx hl, CannotBuildMustDismantleText call DrawWideTextBox_WaitForInput - call Func_3b9d6 - call Func_3b5f1 - jr nc, .asm_3b53a - call Func_3b661 - jr nc, .asm_3b53a + call ShowUsedCardListFromBuiltDecks + call .DismantleDecksNeededToBuild + jr nc, .build_deck + call .TryBuildSubbedDeck + jr nc, .build_deck ret -.asm_3b526 + +.not_own_all_cards_needed ldtx hl, YouDoNotOwnAllCardsNeededToBuildThisDeckText call DrawWideTextBox_WaitForInput - call Func_3b92b - ld a, [wd49b] + call ShowMissingCardList + ld a, [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks] or a - call nz, Func_3b9d6 - call Func_3b661 + call nz, ShowUsedCardListFromBuiltDecks + call .TryBuildSubbedDeck ret c -.asm_3b53a +; fallthrough + +.build_deck call EnableSRAM - call Func_3b3d1 + call FindFirstEmptyDeckSlot call DisableSRAM - jr nc, .asm_3b54c - call Func_3b45f - jr nc, .asm_3b54c + jr nc, .got_deck_slot + call HandleDismantleDeckToMakeSpace + jr nc, .got_deck_slot scf ret -.asm_3b54c - ld [wd496], a + +.got_deck_slot + ld [wDeckSlotForNewDeck], a ld a, [wSelectedDeckMachineEntry] - call Func_3afb8 - ld de, wc000 - ld b, $60 + call GetAndLoadSelectedMachineDeckPtr + +; copy deck to buffer + ld de, wDeckToBuild + ld b, DECK_COMPRESSED_STRUCT_SIZE call EnableSRAM call CopyBBytesFromHLToDE_Bank0e + call SwitchToWRAM2 - ld a, [w2d280] + ld a, [wNumRemainingBasicEnergyCardsForSubbedDeck] or a - call nz, Func_3b7fc + call nz, SubInBasicEnergyInCurDeck + +; remove needed cards from collection call SwitchToWRAM1 - ld hl, $c018 - farcall Func_9337 - ld a, [wd496] + ld hl, wDeckToBuild + DECK_NAME_SIZE + farcall DecrementDeckCardsInCollection_CopyDeckFromSRAM + +; copy deck cards from buffer to selected deck slot + ld a, [wDeckSlotForNewDeck] ld l, a - ld h, $60 + ld h, DECK_COMPRESSED_STRUCT_SIZE call HtimesL - ld bc, sDeck1Name + ld bc, sBuiltDecks add hl, bc ld d, h ld e, l - ld hl, wc000 - ld b, $60 + ld hl, wDeckToBuild + ld b, DECK_COMPRESSED_STRUCT_SIZE call CopyBBytesFromHLToDE_Bank0e call DisableSRAM - ld a, $ff + +; draw decks screen + ld a, ALL_DECKS farcall DrawDecksScreen - ld a, [wd496] + ld a, [wDeckSlotForNewDeck] ld [wCurDeck], a - ld hl, $735e + ld hl, DecksScreenMenuParams call InitializeMenuParameters call DrawCursor2 farcall GetSRAMPointerToCurDeck @@ -6022,138 +6107,151 @@ Func_3b4eb: ldtx hl, BuiltDeckText call DrawWideTextBox_WaitForInput call SwitchToWRAM2 - ld a, [w2d280] + ld a, [wNumRemainingBasicEnergyCardsForSubbedDeck] or a call SwitchToWRAM1 - jr z, .asm_3b5ef + jr z, .done + +; built subbed deck call SwitchToWRAM2 - ld hl, w2d38e - ld de, wc000 - ld b, $80 + ld hl, wBackup2DeckToBuild + ld de, wDeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 ldtx bc, BuiltSubbedDeckWithTheseCardsText - ld hl, wd38a + ld hl, wCardConfirmationText ld a, c ld [hli], a ld a, b ld [hl], a call GetSelectedSavedDeckPtr - ld de, wc000 - farcall Func_b59f -.asm_3b5ef + ld de, wDeckToBuild + farcall HandleDeckStatusCardList_InSRAM + +.done scf ret -Func_3b5f1: - call Func_3bcd6 +.DismantleDecksNeededToBuild: + call CheckWhichDecksToDismantleToBuildSavedDeck farcall DrawDecksScreen ldtx hl, DismantleTheseDecksPromptText call YesOrNoMenuWithText - jr nc, .asm_3b601 + jr nc, .dismantle_yes ret -.asm_3b601 +.dismantle_yes call EnableSRAM - ld a, [wd49a] - bit 0, a - jr z, .asm_3b610 - ld a, $00 - call Func_3b646 -.asm_3b610 - ld a, [wd49a] - bit 1, a - jr z, .asm_3b61c - ld a, $01 - call Func_3b646 -.asm_3b61c - ld a, [wd49a] - bit 2, a - jr z, .asm_3b628 - ld a, $02 - call Func_3b646 -.asm_3b628 - ld a, [wd49a] - bit 3, a - jr z, .asm_3b634 - ld a, $03 - call Func_3b646 -.asm_3b634 +; dismantle deck 1 + ld a, [wDecksToBeDismantled] + bit DECK_1_F, a + jr z, .dismantle_deck_2 + ld a, DECK_1_F + call .DismantleDeck +.dismantle_deck_2 + ld a, [wDecksToBeDismantled] + bit DECK_2_F, a + jr z, .dismantle_deck_3 + ld a, DECK_2_F + call .DismantleDeck +.dismantle_deck_3 + ld a, [wDecksToBeDismantled] + bit DECK_3_F, a + jr z, .dismantle_deck_4 + ld a, DECK_3_F + call .DismantleDeck +.dismantle_deck_4 + ld a, [wDecksToBeDismantled] + bit DECK_4_F, a + jr z, .done_dismantling + ld a, DECK_4_F + call .DismantleDeck + +.done_dismantling call DisableSRAM - ld a, [wd49a] + ld a, [wDecksToBeDismantled] farcall DrawDecksScreen ldtx hl, DismantledTheseDecksText call DrawWideTextBox_WaitForInput or a ret -Func_3b646: +; a = DECK_*_F +.DismantleDeck: ld l, a - ld h, $60 + ld h, DECK_COMPRESSED_STRUCT_SIZE call HtimesL - ld bc, sDeck1Name + ld bc, sBuiltDecks add hl, bc push hl - ld bc, $18 + ld bc, DECK_NAME_SIZE add hl, bc farcall AddDeckToCollection pop hl - ld a, $60 + ld a, DECK_COMPRESSED_STRUCT_SIZE farcall ClearNBytesFromHL ret -Func_3b661: +.TryBuildSubbedDeck: ldtx hl, MaySubInEnergyCardsToBuildThisDeckText call DrawWideTextBox_WaitForInput ldtx hl, BuildSubbedDeckPromptText call YesOrNoMenuWithText ret c - ld a, [wd49b] - ld hl, wd49e + + ld a, [wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks] + ld hl, wNumCardsNeededToBuildSelectedDeckMissingInCardCollection add [hl] - cp $0a - jr c, .asm_3b681 + cp MAX_NUM_SUB_ENERGY_CARDS + 1 + jr c, .check_basic_energy_to_sub_in ldtx hl, CannotBuildLackingTooManyCardsText call DrawWideTextBox_WaitForInput scf ret -.asm_3b681 + +.check_basic_energy_to_sub_in push af - call Func_3b6c4 - call Func_3b75a + call OmitMissingCardsFromDeckAndBackup + call GetSumOfRemainingBasicEnergyCards pop bc - jr c, .asm_3b696 - cp b - jr nc, .asm_3b696 + jr c, .check_basic_pkmn_before_sub ; has 256+ energy cards + cp b ; required sub count + jr nc, .check_basic_pkmn_before_sub ldtx hl, CannotBuildLackingEnergyCardsText call DrawWideTextBox_WaitForInput scf ret -.asm_3b696 + +.check_basic_pkmn_before_sub call SwitchToWRAM2 - ld hl, w2d200 - ld de, wc000 - ld b, $80 + ld hl, wBackup1DeckToBuild + ld de, wDeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - ld hl, wc000 + ld hl, wDeckToBuild ld de, wCurDeckCards - ld b, $80 + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e farcall CheckIfThereAreAnyBasicCardsInDeck - jr c, .asm_3b6c0 + jr c, .prepare_sub ldtx hl, CannotBuildLackingBasicPokemonText call DrawWideTextBox_WaitForInput scf ret -.asm_3b6c0 - call Func_3b79b + +.prepare_sub + call SortRemainingBasicEnergyCardsDescending ret -Func_3b6c4: +; list available cards for the target deck +; and load them into wDeckToBuild and wBackup1DeckToBuild, +; skipping all missing cards +OmitMissingCardsFromDeckAndBackup: ld a, [wSelectedDeckMachineEntry] ld [wCurDeck], a call GetSelectedSavedDeckPtr - ld de, $18 + ld de, DECK_NAME_SIZE add hl, de ld d, h ld e, l @@ -6161,13 +6259,16 @@ Func_3b6c4: farcall CopyDeckFromSRAM farcall SortCurDeckCardsByID farcall CreateCurDeckUniqueCardList + +; set wTempCardCollection = all owned cards xor a farcall CreateCardCollectionListWithDeckCards - ld hl, $0 -.asm_3b6ea + + ld hl, 0 +.loop_deck_configuration push hl ld l, h - ld h, $00 + ld h, 0 ld de, wUniqueDeckCardList add hl, de ld e, [hl] @@ -6176,19 +6277,20 @@ Func_3b6c4: pop hl inc h inc h - call Func_3b91d - jr c, .asm_3b720 + call CheckIfCardIDIsZero_Bank0e + jr c, .got_list push bc push de push hl ld hl, wCurDeckCards - call Func_3b746 + call .GetCardCountAvailable pop hl pop de pop bc - jr nc, .asm_3b6ea + jr nc, .loop_deck_configuration + ld c, a -.asm_3b70c +.loop_append push hl push de ld h, $00 @@ -6202,187 +6304,228 @@ Func_3b6c4: inc l inc l dec c - jr nz, .asm_3b70c - jr .asm_3b6ea -.asm_3b720 + jr nz, .loop_append + jr .loop_deck_configuration + +.got_list +; append null terminator ld h, $00 ld de, wTempCardList add hl, de xor a ld [hli], a ld [hl], a +; copy deck data ld hl, wTempCardList - ld de, wc000 - ld b, $80 + ld de, wDeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM2 - ld hl, wc000 - ld de, w2d200 - ld b, $80 + ld hl, wDeckToBuild + ld de, wBackup1DeckToBuild + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 ret -Func_3b746: - call Func_3b9ba - ld hl, wc000 +; hl = deck cards (wCurDeckCards) +; de = card id +; if owned count > 0, return a = min( (required count), (owned count) ) with carry +; if owned count = 0, return a = 0 (nc) +.GetCardCountAvailable: + call GetCardCountFromDeck + ld hl, wTempCardCollection ; all owned cards add hl, de ld a, [hl] - and $7f + and CARD_COUNT_MASK or a ret z + cp b - jr nc, .asm_3b757 + jr nc, .enough +; not enough but nonzero scf ret -.asm_3b757 +.enough ld a, b scf ret -Func_3b75a: +; for x[t] = {basic energy cards remaining after building wBackup1DeckToBuild}, +; calc sum(x) from grass to psychic with overflow check: +; if sum(x[0..k]) > 255 at t = k, return +; a = sum(x[0..k]) % 256 (with carry), +; b = sum(x[0..k-1]) +; otherwise return a = b = sum(x) +GetSumOfRemainingBasicEnergyCards: +; set wTempCardCollection = all owned cards xor a farcall CreateCardCollectionListWithDeckCards + call SwitchToWRAM2 - ld hl, w2d200 -.asm_3b765 + ld hl, wBackup1DeckToBuild +.loop_deck_cards ld e, [hl] inc hl ld d, [hl] inc hl - call Func_3b91d - jr c, .asm_3b77c + call CheckIfCardIDIsZero_Bank0e + jr c, .load_count push hl - ld hl, wc000 + ld hl, wTempCardCollection add hl, de ld a, [hl] - and $7f - jr z, .asm_3b779 + and CARD_COUNT_MASK + jr z, .next_card dec [hl] -.asm_3b779 +.next_card pop hl - jr .asm_3b765 -.asm_3b77c - ld hl, wc000 + 1 - ld de, w2d280 - ld b, $06 + jr .loop_deck_cards + +.load_count + ld hl, wTempCardCollection + GRASS_ENERGY + ld de, wNumRemainingBasicEnergyCardsForSubbedDeck + ld b, NUM_COLORED_TYPES call CopyBBytesFromHLToDE_Bank0e - ld hl, w2d280 - ld bc, $6 -.asm_3b78d + + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck + lb bc, 0, NUM_COLORED_TYPES +.loop_sum ld a, [hli] add b - jr c, .asm_3b797 + jr c, .done ld b, a dec c - jr nz, .asm_3b78d + jr nz, .loop_sum ld b, a or a -.asm_3b797 +.done call SwitchToWRAM1 ret -Func_3b79b: +; sort wNumRemainingBasicEnergyCardsForSubbedDeck in descending order +; and map its index array to wIndicesRemainingBasicEnergyCardsForSubbedDeck +SortRemainingBasicEnergyCardsDescending: call SwitchToWRAM2 ld a, $ff - ld [w2d286], a + ld [wNumRemainingBasicEnergyCardsForSubbedDeck + NUM_COLORED_TYPES], a xor a - ld hl, w2d287 - ld [hli], a + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck + ld [hli], a ; GRASS_ENERGY - GRASS_ENERGY inc a - ld [hli], a + ld [hli], a ; FIRE_ENERGY - GRASS_ENERGY inc a - ld [hli], a + ld [hli], a ; WATER_ENERGY - GRASS_ENERGY inc a - ld [hli], a + ld [hli], a ; LIGHTNING_ENERGY - GRASS_ENERGY inc a - ld [hli], a + ld [hli], a ; FIGHTING_ENERGY - GRASS_ENERGY inc a - ld [hl], a - ld de, $0 -.asm_3b7b5 - ld hl, w2d280 + ld [hl], a ; PSYCHIC_ENERGY - GRASS_ENERGY + + ld de, 0 +.loop_swap + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck add hl, de - call Func_3b7e4 + call .GetMaxNumAndIndex ld a, e add c ld c, a - ld hl, w2d280 + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck add hl, de ld a, [hl] ld [hl], b - ld b, $00 - ld hl, w2d280 + ld b, 0 + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck add hl, bc ld [hl], a - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, de ld a, [hl] push hl - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, bc ld c, [hl] ld [hl], a pop hl ld [hl], c inc e - ld a, $06 + ld a, NUM_COLORED_TYPES cp e - jr nz, .asm_3b7b5 + jr nz, .loop_swap call SwitchToWRAM1 ret -Func_3b7e4: +; from the current index and onwards, +; return b = max count, c = energy index of max count +.GetMaxNumAndIndex: push de - ld e, $00 - ld bc, $0 -.asm_3b7ea + ld e, 0 + lb bc, 0, 0 +.loop_energy ld a, [hli] cp $ff - jr z, .asm_3b7f9 - and $7f + jr z, .got_max + and CARD_COUNT_MASK cp b - jr c, .asm_3b7f6 + jr c, .next_energy ld b, a ld e, c -.asm_3b7f6 +.next_energy inc c - jr .asm_3b7ea -.asm_3b7f9 + jr .loop_energy +.got_max ld c, e pop de ret -Func_3b7fc: +; fill in missing card slots in the deck with remaining basic energy cards +; (already guaranteed of enough amount of replacements at this point), +; starting with the type of the most remaining +; bug: +; in the first iteration, skip type(s) of +; ( (*_ENERGY used in the deck) + 1) +; e.g. +; when the deck misses 9 cards +; and the remaining energy count is 50 fighting, 40 grass, 30 fire, …, +; if the deck has any lightning (LIGHTNING_ENERGY + 1 = FIGHTING_ENERGY), +; sub in 9 grass +; otherwise sub in 9 fighting +SubInBasicEnergyInCurDeck: call SwitchToWRAM2 - ld hl, wc000 - ld de, w2d28e - ld b, $00 + ld hl, wDeckToBuild + ld de, wBigBackupDeckToBuild + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - call Func_3b6c4 - call Func_3b75a - call Func_3b79b + call OmitMissingCardsFromDeckAndBackup + call GetSumOfRemainingBasicEnergyCards + call SortRemainingBasicEnergyCardsDescending call SwitchToWRAM2 - ld hl, w2d28e - ld de, wc000 - ld b, $00 + ld hl, wBigBackupDeckToBuild + ld de, wDeckToBuild + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e + +; decompress wDeckToBuild deck config call SwitchToWRAM1 - ld de, $c018 + ld de, wDeckToBuild + DECK_NAME_SIZE ld hl, wTempSavedDeckCards farcall CopyDeckFromSRAM ld hl, wTempSavedDeckCards - ld de, $c018 - ld b, $80 + ld de, wDeckToBuild + DECK_NAME_SIZE + ld b, DECK_TEMP_BUFFER_SIZE call CopyBBytesFromHLToDE_Bank0e + +; copy wBackup1DeckToBuild (missing cards omitted) to wScratchCardCollection +; set b = card count call SwitchToWRAM2 - ld a, $80 + ld a, DECK_TEMP_BUFFER_SIZE ld hl, wScratchCardCollection farcall ClearNBytesFromHL - ld b, $00 - ld de, w2d200 -.asm_3b84d + ld b, 0 + ld de, wBackup1DeckToBuild +.loop_copy_and_count ld a, [de] inc de ld [hli], a @@ -6394,53 +6537,61 @@ Func_3b7fc: inc b ld e, c ld d, a - call Func_3b91d + call CheckIfCardIDIsZero_Bank0e pop de - jr nc, .asm_3b84d + jr nc, .loop_copy_and_count dec b dec hl dec hl + push hl ld a, $ff - ld [w2d28d], a - ld de, $0 -.asm_3b86a + ld [wIndicesRemainingBasicEnergyCardsForSubbedDeck + NUM_COLORED_TYPES], a + +; first iteration +; bug: skip all *_ENERGY of ( (*_ENERGY used in the deck) + 1) + ld de, 0 +.loop_energy push hl - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, de ld a, [hl] cp $ff - jr z, .asm_3b882 - call Func_3b8c0 + jr z, .second_iteration + call .CheckEnergyInDeck pop hl - jr c, .asm_3b87f - call Func_3b8da - jr c, .asm_3b899 -.asm_3b87f + jr c, .next_energy + call .SubInCurBasicEnergy + jr c, .list_energy +.next_energy inc e - jr .asm_3b86a -.asm_3b882 + jr .loop_energy + +; ensure completion, with no skip logic +.second_iteration pop hl - ld de, $0 -.asm_3b886 + ld de, 0 +.loop_energy_2 push hl - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, de ld a, [hl] cp $ff pop hl - jr z, .asm_3b899 - call Func_3b8da - jr c, .asm_3b899 + jr z, .list_energy + call .SubInCurBasicEnergy + jr c, .list_energy inc e - jr .asm_3b886 -.asm_3b899 - ld a, $80 - ld hl, w2d38e + jr .loop_energy_2 + +; list all energy subs at wBackup2DeckToBuild to display it later +.list_energy + ld a, DECK_TEMP_BUFFER_SIZE + ld hl, wBackup2DeckToBuild farcall ClearNBytesFromHL pop hl - ld bc, w2d38e -.asm_3b8a6 + ld bc, wBackup2DeckToBuild +.loop_list ld a, [hli] ld [bc], a inc bc @@ -6449,113 +6600,126 @@ Func_3b7fc: ld [bc], a inc bc ld d, a - call Func_3b91d - jr nc, .asm_3b8a6 - ld hl, $c018 + call CheckIfCardIDIsZero_Bank0e + jr nc, .loop_list + +; save result + ld hl, wDeckToBuild + DECK_NAME_SIZE ld de, wScratchCardCollection bank1call SaveDeckCards call SwitchToWRAM1 ret -Func_3b8c0: +; a = basic energy index [0, 5] +; bug: set carry if wDeckToBuild contains a card of id = a +; *_ENERGY card id is [1, 6], which is a mismatch +.CheckEnergyInDeck: push de - ld hl, $c018 -.asm_3b8c4 + ld hl, wDeckToBuild + DECK_NAME_SIZE +.loop_deck_cards ld e, [hl] inc hl ld d, [hl] inc hl - call Func_3b91d + call CheckIfCardIDIsZero_Bank0e ccf - jr nc, .asm_3b8d8 + jr nc, .checked_deck_cards cp e - jr nz, .asm_3b8c4 + jr nz, .loop_deck_cards ld e, a ld a, d or a ld a, e - jr nz, .asm_3b8c4 + jr nz, .loop_deck_cards scf -.asm_3b8d8 +.checked_deck_cards pop de ret -Func_3b8da: +; append current basic energy (e = offset) +; to the end of card list (wScratchCardCollection) +; if the deck is completed, set carry +; and overwrite the index in wIndicesRemainingBasicEnergyCardsForSubbedDeck +; with the final remaining count (!?) +.SubInCurBasicEnergy: push hl - ld hl, w2d280 + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck add hl, de ld a, [hl] - and $7f - jr z, .asm_3b90d + and CARD_COUNT_MASK + jr z, .exit_append ld c, a pop hl push de push hl - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, de ld e, [hl] - inc e + inc e ; convert index to *_ENERGY card id ld d, $00 pop hl -.asm_3b8f1 +.loop_append ld [hl], e inc hl ld [hl], d inc hl inc b dec c - jr z, .asm_3b900 - ld a, $3c + jr z, .depleted + ld a, DECK_SIZE cp b - jr z, .asm_3b910 - jr .asm_3b8f1 -.asm_3b900 - ld a, $3c + jr z, .done_append + jr .loop_append + +.depleted + ld a, DECK_SIZE cp b - jr z, .asm_3b910 + jr z, .done_append pop de push hl - ld hl, w2d280 + ld hl, wNumRemainingBasicEnergyCardsForSubbedDeck add hl, de xor a ld [hl], a -.asm_3b90d +.exit_append pop hl or a ret -.asm_3b910 + +.done_append xor a ld [hli], a ld [hl], a pop de push hl - ld hl, w2d287 + ld hl, wIndicesRemainingBasicEnergyCardsForSubbedDeck add hl, de ld [hl], c pop hl scf ret -Func_3b91d: +; return carry if de (card id) = 0 +CheckIfCardIDIsZero_Bank0e: push af xor a cp d - jr nz, .asm_3b928 + jr nz, .false cp e - jr nz, .asm_3b928 + jr nz, .false pop af scf ret -.asm_3b928 +.false pop af or a ret -Func_3b92b: +ShowMissingCardList: ld a, [wSelectedDeckMachineEntry] ld [wCurDeck], a call GetSelectedSavedDeckPtr - ld de, $18 + ld de, DECK_NAME_SIZE add hl, de ld d, h ld e, l @@ -6563,10 +6727,13 @@ Func_3b92b: farcall CopyDeckFromSRAM farcall SortCurDeckCardsByID farcall CreateCurDeckUniqueCardList + +; set wTempCardCollection = all owned cards ld a, $ff farcall CreateCardCollectionListWithDeckCards - ld hl, $0 -.asm_3b952 + + ld hl, 0 +.loop_deck_configuration push hl ld l, h ld h, $00 @@ -6578,19 +6745,21 @@ Func_3b92b: pop hl inc h inc h - call Func_3b91d - jr c, .asm_3b988 + call CheckIfCardIDIsZero_Bank0e + jr c, .got_list push bc push de push hl ld hl, wCurDeckCards - call Func_3b9a6 + call .GetCardCountMissing pop hl pop de pop bc - jr nc, .asm_3b952 + jr nc, .loop_deck_configuration + +; list at wTempCardList cards missing ld c, a -.asm_3b974 +.loop_append push hl push de ld h, $00 @@ -6604,73 +6773,84 @@ Func_3b92b: inc l inc l dec c - jr nz, .asm_3b974 - jr .asm_3b952 -.asm_3b988 + jr nz, .loop_append + jr .loop_deck_configuration + +.got_list +; append null terminator ld h, $00 ld de, wTempCardList add hl, de xor a ld [hli], a ld [hl], a +; display list ldtx bc, LackTheseCardsToBuildThisDeckText - ld hl, wd38a + ld hl, wCardConfirmationText ld a, c ld [hli], a ld a, b ld [hl], a call GetSelectedSavedDeckPtr ld de, wTempCardList - farcall Func_b59f + farcall HandleDeckStatusCardList_InSRAM ret -Func_3b9a6: - call Func_3b9ba - ld hl, wc000 +; hl = deck cards (wCurDeckCards) +; de = card id +; if (required count) > (owned count), return a = (shortage count) with carry +; otherwise clear carry +.GetCardCountMissing: + call GetCardCountFromDeck + ld hl, wTempCardCollection ; all owned cards add hl, de ld a, [hl] - and $7f + and CARD_COUNT_MASK cp b - jr c, .asm_3b9b5 + jr c, .not_enough +; enough or a ret -.asm_3b9b5 +.not_enough ld e, a ld a, b sub e scf ret -Func_3b9ba: +; hl = deck cards (wCurDeckCards) +; de = card id +; return b = count of the card in hl +GetCardCountFromDeck: push de - ld b, $00 -.asm_3b9bd + ld b, 0 +.loop_list push de ld e, [hl] inc hl ld d, [hl] inc hl - call Func_3b91d + call CheckIfCardIDIsZero_Bank0e ld a, e ld c, d pop de - jr c, .asm_3b9d4 + jr c, .done cp e - jr nz, .asm_3b9bd + jr nz, .loop_list ld a, c cp d - jr nz, .asm_3b9bd + jr nz, .loop_list inc b - jr .asm_3b9bd -.asm_3b9d4 + jr .loop_list +.done pop de ret -Func_3b9d6: +ShowUsedCardListFromBuiltDecks: ld a, [wSelectedDeckMachineEntry] ld [wCurDeck], a call GetSelectedSavedDeckPtr - ld de, $18 + ld de, DECK_NAME_SIZE add hl, de ld d, h ld e, l @@ -6678,22 +6858,27 @@ Func_3b9d6: farcall CopyDeckFromSRAM farcall SortCurDeckCardsByID farcall CreateCurDeckUniqueCardList + +; set: +; wTempCardCollection = all cards used in built decks +; wScratchCardCollection = all owned cards xor a farcall CreateCardCollectionListWithDeckCards call SwitchToWRAM2 - ld hl, wc000 + ld hl, wTempCardCollection ld de, wScratchCardCollection - ld b, $00 + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e - ld hl, wc000 + $100 + ld hl, wTempCardCollection + $100 ld de, wScratchCardCollection + $100 - ld b, $00 + ld b, 0 ; aka $100 bytes call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 - ld a, $80 + ld a, CARD_COUNT_FROM_BUILT_DECKS farcall CreateCardCollectionListWithDeckCards - ld hl, $0 -.asm_3ba1e + + ld hl, 0 +.loop_deck_configuration push hl ld l, h ld h, $00 @@ -6705,19 +6890,21 @@ Func_3b9d6: pop hl inc h inc h - call Func_3b91d - jr c, .asm_3ba54 + call CheckIfCardIDIsZero_Bank0e + jr c, .got_list push bc push de push hl ld hl, wCurDeckCards - call Func_3ba7d + call .GetCardCountNeededToTakeFromBuiltDecks pop hl pop de pop bc - jr nc, .asm_3ba1e + jr nc, .loop_deck_configuration + +; list at wTempCardList cards needed from built decks ld c, a -.asm_3ba40 +.loop_append push hl push de ld h, $00 @@ -6731,55 +6918,65 @@ Func_3b9d6: inc l inc l dec c - jr nz, .asm_3ba40 - jr .asm_3ba1e -.asm_3ba54 + jr nz, .loop_append + jr .loop_deck_configuration + +.got_list +; append null terminator ld h, $00 ld de, wTempCardList add hl, de xor a ld [hli], a ld [hl], a - ld a, [wd49e] +; set up text + ld a, [wNumCardsNeededToBuildSelectedDeckMissingInCardCollection] or a - jr nz, .asm_3ba68 + jr nz, .also_not_enough_in_card_collection ldtx bc, UsingTheseCardsInOtherDecksText - jr .asm_3ba6b -.asm_3ba68 + jr .display_list +.also_not_enough_in_card_collection ldtx bc, UsingTheseCardsTooInOtherDecksText -.asm_3ba6b - ld hl, wd38a +.display_list + ld hl, wCardConfirmationText ld a, c ld [hli], a ld a, b ld [hl], a call GetSelectedSavedDeckPtr ld de, wTempCardList - farcall Func_b59f + farcall HandleDeckStatusCardList_InSRAM ret -Func_3ba7d: - call Func_3b9ba - call PrintDeckMachineEntry.GetCardCountInScratchCardCollection +; hl = deck cards (wCurDeckCards) +; de = card id +; if that card isn't enough and is used in built decks, +; return a = min( (card count used in built decks), (shortage count) ) with carry +; otherwise clear carry +.GetCardCountNeededToTakeFromBuiltDecks: + call GetCardCountFromDeck + call GetCardCountInScratchCardCollection ld c, a ld a, b sub c - jr z, .asm_3ba9a - jr c, .asm_3ba9a +; a = (shortage count) = (card count needed) - (owned count) + jr z, .no_cards_to_take + jr c, .no_cards_to_take +; not enough, check built decks ld b, a - ld hl, wc000 + ld hl, wTempCardCollection ; all cards used in built decks add hl, de ld a, [hl] - and $7f - jr z, .asm_3ba9a + and CARD_COUNT_MASK + jr z, .no_cards_to_take cp b - jr c, .asm_3ba98 + jr c, .got_count_to_take ld a, b -.asm_3ba98 +.got_count_to_take scf ret -.asm_3ba9a - or a +.no_cards_to_take + or a ; clear carry ret _PrinterMenu_DeckConfiguration: @@ -6832,6 +7029,7 @@ _PrinterMenu_DeckConfiguration: ld [wCurScrollMenuItem], a jp .start_selection +; auto deck machines Func_3bb09: ld a, [wd548] or a @@ -6857,13 +7055,13 @@ Func_3bb09: call Func_3bc95 xor a .asm_3bb30 - ld hl, $5eb5 + ld hl, MenuParams_9eb5 farcall InitializeScrollMenuParameters ldtx hl, PleaseSelectDeckText call DrawWideTextBox_PrintText ld a, [wNumDeckMachineEntries] ld [wNumMenuItems], a - ld a, $01 + ld a, TRUE ld [wUnableToScrollDown], a xor a ld [wd119], a @@ -6878,7 +7076,7 @@ Func_3bb09: ld a, [wScrollMenuScrollOffset] ld [wTempScrollMenuScrollOffset], a ld b, a - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] ld [wTempScrollMenuItem], a add b ld c, a @@ -6886,7 +7084,7 @@ Func_3bb09: or $80 ld [wCurDeck], a ld a, c - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr push hl farcall CheckIfDeckHasCards pop hl @@ -6904,13 +7102,13 @@ Func_3bb09: ld [wScrollMenuScrollOffset], a call Func_3bc95 ld a, [wTempScrollMenuItem] - ld [wTempCardTypeFilter], a + ld [wCurScrollMenuItem], a jp .asm_3bb30 .asm_3bba2 call HandleScrollMenu.draw_visible_cursor ld a, [wScrollMenuScrollOffset] ld [wTempScrollMenuScrollOffset], a - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] ld [wTempScrollMenuItem], a ld a, [hCurMenuItem] cp $ff @@ -6936,11 +7134,11 @@ Func_3bb09: .asm_3bbe6 ld a, [wCheckMenuCursorYPosition] sla a - ld hl, wCurSongBankBackup + ld hl, wCheckMenuCursorXPosition add [hl] or a jr nz, .asm_3bc0a - call Func_3b4eb + call TryBuildDeckMachineDeck ld a, [wTempScrollMenuItem] jp nc, .asm_3bb30 ld a, [wTempScrollMenuScrollOffset] @@ -6957,7 +7155,7 @@ Func_3bb09: ld a, [wScrollMenuScrollOffset] ld [wTempScrollMenuScrollOffset], a ld b, a - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] ld [wTempScrollMenuItem], a add b ld c, a @@ -6967,14 +7165,14 @@ Func_3bb09: ld b, $00 ld hl, wd4b4 add hl, bc - ld bc, wd38a + ld bc, wCardConfirmationText ld a, [hli] ld [bc], a inc bc ld a, [hl] ld [bc], a pop af - call Func_3afb8 + call GetAndLoadSelectedMachineDeckPtr push hl farcall CheckIfDeckHasCards pop hl @@ -6982,12 +7180,12 @@ Func_3bb09: ld a, MENU_CONFIRM farcall PlaySFXConfirmOrCancel push hl - ld de, $18 + ld de, DECK_NAME_SIZE add hl, de ld d, h ld e, l pop hl - farcall Func_b57c + farcall HandleDeckStatusCardList ld a, [wTempScrollMenuScrollOffset] ld [wScrollMenuScrollOffset], a call Func_3bc95 @@ -7024,61 +7222,72 @@ Func_3bc95: call EnableLCD ret -Func_3bcd6: +; to build wSelectedDeckMachineEntry, +; try out all combinations of dismantling decks +; if none of the combinations work, return carry set +; otherwise, return a = DECK_* flags to be dismantled +; and load it to wDecksToBeDismantled +CheckWhichDecksToDismantleToBuildSavedDeck: xor a - ld [wd49a], a - ld a, $01 -.asm_3bcdc - call Func_3bd28 + ld [wDecksToBeDismantled], a + +; single deck + ld a, DECK_1 +.loop_single_built_decks + call .CheckIfCanBuild ret nc - sla a - cp $10 - jr nz, .asm_3bcdc - ld a, $03 - call Func_3bd28 + sla a ; next deck + cp 1 << NUM_DECKS + jr nz, .loop_single_built_decks + +; two-deck combinations + ld a, DECK_1 | DECK_2 + call .CheckIfCanBuild ret nc - ld a, $05 - call Func_3bd28 + ld a, DECK_1 | DECK_3 + call .CheckIfCanBuild ret nc - ld a, $09 - call Func_3bd28 + ld a, DECK_1 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $06 - call Func_3bd28 + ld a, DECK_2 | DECK_3 + call .CheckIfCanBuild ret nc - ld a, $0a - call Func_3bd28 + ld a, DECK_2 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $0c - call Func_3bd28 + ld a, DECK_3 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $07 - call Func_3bd28 +; three-deck combinations + ld a, DECK_1 | DECK_2 | DECK_3 + call .CheckIfCanBuild ret nc - ld a, $0b - call Func_3bd28 + ld a, DECK_1 | DECK_2 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $0d - call Func_3bd28 + ld a, DECK_1 | DECK_3 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $0e - call Func_3bd28 + ld a, DECK_2 | DECK_3 | DECK_4 + call .CheckIfCanBuild ret nc - ld a, $ff - call Func_3bd28 +; all + ld a, ALL_DECKS + call .CheckIfCanBuild ret -Func_3bd28: +.CheckIfCanBuild: push af ld hl, wSelectedDeckMachineEntry ld b, [hl] call CheckIfCanBuildSavedDeck - jr c, .asm_3bd38 + jr c, .cannot_build pop af - ld [wd49a], a + ld [wDecksToBeDismantled], a or a ret -.asm_3bd38 +.cannot_build pop af scf ret @@ -7207,7 +7416,7 @@ ReceiveDeckConfigurationMenu: ldtx hl, DeleteSavedDeckPromptText call YesOrNoMenuWithText jr nc, .save - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] jr .start_selection .save @@ -7227,7 +7436,7 @@ ReceiveDeckConfigurationMenu: call DisableSRAM call SaveGame call ClearScreenAndDrawDeckMachineScreen - ld a, [wTempCardTypeFilter] + ld a, [wCurScrollMenuItem] ld hl, DeckMachineSelectionParams farcall InitializeScrollMenuParameters call DrawListScrollArrows @@ -7253,14 +7462,14 @@ ReceiveDeckConfigurationMenu: SaveDeckDataToWRAM2: push de - ld de, wc000 + ld de, wDeckToBuild call CopyListFromHLToDE_Bank0e pop de - ld hl, wc000 + DECK_NAME_SIZE + ld hl, wDeckToBuild + DECK_NAME_SIZE bank1call SaveDeckCards call SwitchToWRAM2 - ld hl, wc000 - ld de, w2d28e + ld hl, wDeckToBuild + ld de, wBigBackupDeckToBuild ld b, DECK_COMPRESSED_STRUCT_SIZE call CopyBBytesFromHLToDE_Bank0e call SwitchToWRAM1 @@ -7307,7 +7516,7 @@ OpenDeckSaveMachineFromDeckBuilding: call GetSelectedSavedDeckPtr ld d, h ld e, l - ld hl, w2d28e + ld hl, wBigBackupDeckToBuild ld b, DECK_COMPRESSED_STRUCT_SIZE call EnableSRAM call SwitchToWRAM2 diff --git a/src/home/menus.asm b/src/home/menus.asm index b9a1457..ce25772 100644 --- a/src/home/menus.asm +++ b/src/home/menus.asm @@ -138,14 +138,14 @@ HandleMenuInput:: ; B button pressed ld a, [wCurMenuItem] ld e, a - ld a, $ff + ld a, MENU_CANCEL ldh [hCurScrollMenuItem], a call PlayOpenOrExitScreenSFX scf ret -; plays an "open screen" sound (SFX_CONFIRM) if [hCurScrollMenuItem] != 0xff -; plays an "exit screen" sound (SFX_CANCEL) if [hCurScrollMenuItem] == 0xff +; if [hCurScrollMenuItem] = MENU_CANCEL (exit), play SFX_CANCEL, +; otherwise (open) SFX_CONFIRM PlayOpenOrExitScreenSFX:: push af ldh a, [hCurScrollMenuItem] diff --git a/src/wram.asm b/src/wram.asm index 72596d4..d19e657 100644 --- a/src/wram.asm +++ b/src/wram.asm @@ -44,6 +44,12 @@ NEXTU wCardPopCandidateList:: ; c000 ds CARD_COLLECTION_SIZE +NEXTU + +; buffer to store a target deck +wDeckToBuild:: ; c000 + ds DECK_BIG_TEMP_BUFFER_SIZE + ENDU SECTION "WRAM0 Duels 1", WRAM0 @@ -1334,7 +1340,7 @@ wCardPopRecordType:: ; ce45 ds $6 ; padding to align to $20 -wce4c:: ; ce4c +wTempBankWRAM:: ; ce4c ds $1 wWRAMBank:: ; ce4d @@ -2007,7 +2013,8 @@ wTempFilteredCardListNumCursorPositions:: ; d11c wd11d:: ; d11d ds $1 -wced7:: ; d11e +; tcg1: wced7 +wd11e:: ; d11e ds $1 wCardListVisibleOffsetBackup:: ; d11f @@ -2032,7 +2039,7 @@ wTempCardList:: ; d122 ; holds cards for the current deck wCurDeckCards:: ; d1c4 - ds $a2 + ds 2 * (DECK_CONFIG_BUFFER_SIZE + 1) ; list of all the different cards in a deck configuration wUniqueDeckCardList:: ; d266 @@ -2103,7 +2110,7 @@ wCurCardListPtr:: ; d388 ds $1 -wd38a:: ; d38a +wCardConfirmationText:: ; d38a ds $2 ds $2 @@ -2210,10 +2217,11 @@ wTempScrollMenuScrollOffset:: ; d47c wSelectedDeckMachineEntry:: ; d47d ds $1 -wd47e:: ; d47e - ds $18 +wDismantledDeckName:: ; d47e + ds DECK_NAME_SIZE -wd496:: ; d496 +; which deck slot to be used to build a new deck +wDeckSlotForNewDeck:: ; d496 ds $1 wDeckMachineTitleText:: ; d497 @@ -2222,10 +2230,10 @@ wDeckMachineTitleText:: ; d497 wNumDeckMachineEntries:: ; d499 ds $1 -wd49a:: ; d49a +wDecksToBeDismantled:: ; d49a ds $1 -wd49b:: ; d49b +wNumCardsNeededToBuildSelectedDeckUsedInBuiltDecks:: ; d49b ds $1 ; text ID to print in the text box when @@ -2233,9 +2241,17 @@ wd49b:: ; d49b wDeckMachineText:: ; d49c ds $2 -wd49e:: ; d49e +wNumCardsNeededToBuildSelectedDeckMissingInCardCollection:: ; d49e ds $1 +UNION + +; cards used in built decks, cards missing +wNumCardsNeededToBuildDeckMachineDecks:: ; d49f + ds (1 + 1) * NUM_DECK_SAVE_MACHINE_SLOTS + +NEXTU + wd49f:: ; d49f ds $1 @@ -2249,8 +2265,10 @@ wd4b4:: ; d4b4 ds $c -wd4c8:: ; d4c8 - ds $80 +wSelectedMachineDeck:: ; d4c8 + ds DECK_TEMP_BUFFER_SIZE + +ENDU wd548:: ; d548 ds $2 @@ -3597,32 +3615,32 @@ SECTION "WRAM2", WRAMX wScratchCardCollection:: ; d000 ds CARD_COLLECTION_SIZE -w2d200:: ; d200 - ds $80 +wBackup1DeckToBuild:: ; d200 + ds DECK_TEMP_BUFFER_SIZE -w2d280:: ; d280 - ds $6 +; $ff-terminated array of basic energy amount available to sub in, +; first in card constant order (not in type-flag order), +; then sorted in descending order when processed +; also a subbed deck flag after the sort +wNumRemainingBasicEnergyCardsForSubbedDeck:: ; d280 + ds NUM_COLORED_TYPES + 1 -w2d286:: ; d286 - ds $1 +; $ff-terminated index array [0, 5] +; mapped when wNumRemainingBasicEnergyCardsForSubbedDeck is sorted +wIndicesRemainingBasicEnergyCardsForSubbedDeck:: ; d287 + ds NUM_COLORED_TYPES + 1 -w2d287:: ; d287 - ds $6 +wBigBackupDeckToBuild:: ; d28e + ds DECK_BIG_TEMP_BUFFER_SIZE -w2d28d:: ; d28d - ds $1 - -w2d28e:: ; d28e - ds $100 - -w2d38e:: ; d38e - ds $80 +wBackup2DeckToBuild:: ; d38e + ds DECK_TEMP_BUFFER_SIZE wAutoDecks:: ; d40e ds DECK_COMPRESSED_STRUCT_SIZE * NUM_AUTO_DECK_MACHINE_SLOTS -w2d58e:: ; d58e - ds $80 +wBackup3DeckToBuild:: ; d58e + ds DECK_TEMP_BUFFER_SIZE SECTION "WRAM3", WRAMX