Skip to content

Commit

Permalink
Remove Some Extraneous call/jp's in the Home Bank
Browse files Browse the repository at this point in the history
  • Loading branch information
Sha0den committed Jun 3, 2024
1 parent b16b83b commit 7ee531a
Show file tree
Hide file tree
Showing 23 changed files with 333 additions and 390 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@


## Code Optimization
- **June 3, 2024:** 22 Files Changed
- Rearrange some functions in the home bank to replace some jp's with jr's or fallthroughs
- Eliminate remaining home bank tail calls (replacing a call ret with a fallthrough/jr/jp)
- *Intentially ignored BankpopROM tail calls (that function can't be jumped to)*

<br/>

- **[May 29, 2024](https://github.com/Sha0den/poketcg_v2/commit/d9cbaa4bd90be37a382faa9cd81c903b1f92d66f):** 35 Files Changed
- Refactor code to minimize use of unconditional jr's
- Other minor optimizations, most of which involve jumps
Expand Down Expand Up @@ -168,7 +175,7 @@


## Miscellaneous Changes
- **June 2, 2024:** 16 Files Changed
- **[June 2, 2024](https://github.com/Sha0den/poketcg_v2/commit/b16b83b296ee35aa3d05b7066ed8c649343e0879):** 16 Files Changed
- Update the Glossary (both the overall display and the actual text information)
- Increase the size of the font tile section in vram when viewing the Glossary from a duel
- Create many new fullwidth and halfwidth font characters, plus 2 new text box symbols
Expand Down
2 changes: 1 addition & 1 deletion src/engine/overworld/scripting.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ ScriptCommand_SetDefaultSong:

ScriptCommand_PlaySong:
ld a, c
call ScriptPlaySong
call PlaySong
jp IncreaseScriptPointerBy2

ScriptCommand_PlaySFX:
Expand Down
3 changes: 1 addition & 2 deletions src/home/card_collection.asm
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ CreateTempCardCollection::
call .AddDeckCards
ld de, sDeck4Name
call .AddDeckCards
call DisableSRAM
ret
jp DisableSRAM

; adds the cards from a deck to wTempCardCollection given de = sDeck*Name
.AddDeckCards:
Expand Down
3 changes: 1 addition & 2 deletions src/home/card_data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,4 @@ LoadCardGfx::
dec b
jr nz, .copy_card_palette
pop af
call BankswitchROM
ret
jp BankswitchROM
6 changes: 2 additions & 4 deletions src/home/clear_sram.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ ValidateSRAM::
call RestartSRAM
scf
call InitSaveDataAndSetUppercase
call DisableSRAM
ret
jp DisableSRAM
.check_sequence
ld hl, s0a000
ld a, [hli]
Expand All @@ -37,8 +36,7 @@ ValidateSRAM::
call RestartSRAM
or a
call InitSaveDataAndSetUppercase
call DisableSRAM
ret
jp DisableSRAM

; zero all SRAM banks and set s0a000 to $04, $21, $05
RestartSRAM::
Expand Down
194 changes: 91 additions & 103 deletions src/home/duel.asm
Original file line number Diff line number Diff line change
Expand Up @@ -989,11 +989,10 @@ EmptyPlayAreaSlot::

; shift play area Pokemon of both players to the first available play area (arena + benchx) slots
ShiftAllPokemonToFirstPlayAreaSlots::
call ShiftTurnPokemonToFirstPlayAreaSlots
call SwapTurn
call ShiftTurnPokemonToFirstPlayAreaSlots
call SwapTurn
ret
; fallthrough

; shift play area Pokemon of the turn holder to the first available play area (arena + benchx) slots
ShiftTurnPokemonToFirstPlayAreaSlots::
Expand Down Expand Up @@ -1249,8 +1248,7 @@ ProcessPlayedPokemonCard::
call DisplayUsePokemonPowerScreen
ldtx hl, UnableDueToToxicGasText
call DrawWideTextBox_WaitForInput
call ExchangeRNG
ret
jp ExchangeRNG

.use_pokemon_power
ld hl, wLoadedAttackEffectCommands
Expand Down Expand Up @@ -1281,8 +1279,7 @@ ProcessPlayedPokemonCard::
call ExchangeRNG
call Func_7415
ld a, EFFECTCMDTYPE_PKMN_POWER_TRIGGER
call TryExecuteEffectCommandFunction
ret
jp TryExecuteEffectCommandFunction

; copies, given a card identified by register a (card ID):
; - e into wSelectedAttack and d into hTempCardIndex_ff9f
Expand Down Expand Up @@ -1372,6 +1369,39 @@ UpdateArenaCardIDsAndClearTwoTurnDuelVars::
bank1call ClearNonTurnTemporaryDuelvars_CopyStatus
ret

; use Pokemon Power
UsePokemonPower::
call Func_7415
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_2
call TryExecuteEffectCommandFunction
jr c, DisplayUsePokemonPowerScreen_WaitForInput
ld a, EFFECTCMDTYPE_REQUIRE_SELECTION
call TryExecuteEffectCommandFunction
jr c, ReturnCarry
ld a, OPPACTION_USE_PKMN_POWER
call SetOppAction_SerialSendDuelData
call ExchangeRNG
ld a, OPPACTION_EXECUTE_PKMN_POWER_EFFECT
call SetOppAction_SerialSendDuelData
ld a, EFFECTCMDTYPE_BEFORE_DAMAGE
call TryExecuteEffectCommandFunction
ld a, OPPACTION_DUEL_MAIN_SCENE
jp SetOppAction_SerialSendDuelData

DisplayUsePokemonPowerScreen_WaitForInput::
push hl
call DisplayUsePokemonPowerScreen
pop hl
; fallthrough

DrawWideTextBox_WaitForInput_ReturnCarry::
call DrawWideTextBox_WaitForInput
; fallthrough

ReturnCarry::
scf
ret

; Use an attack (from DuelMenu_Attack) or a Pokemon Power (from DuelMenu_PkmnPower)
UseAttackOrPokemonPower::
ld a, [wSelectedAttack]
Expand All @@ -1382,16 +1412,16 @@ UseAttackOrPokemonPower::
ld [wPlayerAttackingCardID], a
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
jp z, UsePokemonPower
jr z, UsePokemonPower
call UpdateArenaCardIDsAndClearTwoTurnDuelVars
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_1
call TryExecuteEffectCommandFunction
jp c, DrawWideTextBox_WaitForInput_ReturnCarry
jr c, DrawWideTextBox_WaitForInput_ReturnCarry
call CheckSmokescreenSubstatus
jr c, .sand_attack_smokescreen
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_2
call TryExecuteEffectCommandFunction
jp c, ReturnCarry
jr c, ReturnCarry
call SendAttackDataToLinkOpponent
jr .next
.sand_attack_smokescreen
Expand All @@ -1400,7 +1430,7 @@ UseAttackOrPokemonPower::
jp c, ClearNonTurnTemporaryDuelvars_ResetCarry
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_2
call TryExecuteEffectCommandFunction
jp c, ReturnCarry
jr c, ReturnCarry
.next
ld a, OPPACTION_USE_ATTACK
call SetOppAction_SerialSendDuelData
Expand Down Expand Up @@ -1474,20 +1504,6 @@ HandleAfterDamageEffects::
or a
ret

DisplayUsePokemonPowerScreen_WaitForInput::
push hl
call DisplayUsePokemonPowerScreen
pop hl
; fallthrough

DrawWideTextBox_WaitForInput_ReturnCarry::
call DrawWideTextBox_WaitForInput
; fallthrough

ReturnCarry::
scf
ret

ClearNonTurnTemporaryDuelvars_ResetCarry::
bank1call ClearNonTurnTemporaryDuelvars
or a
Expand All @@ -1511,26 +1527,6 @@ HandleConfusionDamageToSelf::
or a
ret

; use Pokemon Power
UsePokemonPower::
call Func_7415
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_2
call TryExecuteEffectCommandFunction
jr c, DisplayUsePokemonPowerScreen_WaitForInput
ld a, EFFECTCMDTYPE_REQUIRE_SELECTION
call TryExecuteEffectCommandFunction
jr c, ReturnCarry
ld a, OPPACTION_USE_PKMN_POWER
call SetOppAction_SerialSendDuelData
call ExchangeRNG
ld a, OPPACTION_EXECUTE_PKMN_POWER_EFFECT
call SetOppAction_SerialSendDuelData
ld a, EFFECTCMDTYPE_BEFORE_DAMAGE
call TryExecuteEffectCommandFunction
ld a, OPPACTION_DUEL_MAIN_SCENE
call SetOppAction_SerialSendDuelData
ret

; called by UseAttackOrPokemonPower (on an attack only)
; in a link duel, it's used to send the other game data about the
; attack being in use, triggering a call to OppAction_BeginUseAttack in the receiver
Expand Down Expand Up @@ -1771,10 +1767,22 @@ ApplyDamageModifiers_DamageToTarget::
call ApplyAttachedDefender
call HandleDamageReduction
bit 7, d
jr z, .no_underflow
jr z, SwapTurn ; no underflow
ld de, 0
.no_underflow
call SwapTurn
; fallthrough

; returns [hWhoseTurn] <-- ([hWhoseTurn] ^ $1)
; As a side effect, this also returns a duelist variable in a similar manner to
; GetNonTurnDuelistVariable, but this function appears to be
; only called to swap the turn value.
SwapTurn::
push af
push hl
call GetNonTurnDuelistVariable
ld a, h
ldh [hWhoseTurn], a
pop hl
pop af
ret

; convert a color to its equivalent WR_* (weakness/resistance) value
Expand Down Expand Up @@ -2074,8 +2082,7 @@ PrintPokemonsAttackText::
ld a, [wLoadedAttackName + 1]
ld [hli], a
ldtx hl, PokemonsAttackText
call DrawWideTextBox_PrintText
ret
jp DrawWideTextBox_PrintText

Func_1bb4::
call FinishQueuedAnimations
Expand All @@ -2085,8 +2092,7 @@ Func_1bb4::
ldh [hTempPlayAreaLocation_ff9d], a
call PrintFailedEffectText
call WaitForWideTextBoxInput
call ExchangeRNG
ret
jp ExchangeRNG

; prints one of the ThereWasNoEffectFrom*Text if wEffectFailed contains EFFECT_FAILED_NO_EFFECT,
; and prints WasUnsuccessfulText if wEffectFailed contains EFFECT_FAILED_UNSUCCESSFUL
Expand Down Expand Up @@ -2130,7 +2136,40 @@ GetPlayAreaCardRetreatCost::
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer1_FromDeckIndex
call GetLoadedCard1RetreatCost
; fallthrough

; return, in a, the retreat cost of the card in wLoadedCard1,
; adjusting for any Dodrio's Retreat Aid Pkmn Power that is active.
GetLoadedCard1RetreatCost::
ld c, 0
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
.check_bench_loop
ld a, [hli]
cp -1
jr z, .no_more_bench
call GetCardIDFromDeckIndex
ld a, e
cp DODRIO
jr nz, .not_dodrio
inc c
.not_dodrio
jr .check_bench_loop
.no_more_bench
ld a, c
or a
jr nz, .dodrio_found
.muk_found
ld a, [wLoadedCard1RetreatCost] ; return regular retreat cost
ret
.dodrio_found
ld a, MUK
call CountPokemonIDInBothPlayAreas
jr c, .muk_found
ld a, [wLoadedCard1RetreatCost]
sub c ; apply Retreat Aid for each Pkmn Power-capable Dodrio
ret nc
xor a
ret

; calculate damage and max HP of card at PLAY_AREA_* in e.
Expand Down Expand Up @@ -2193,57 +2232,6 @@ CheckLoadedAttackFlag::
pop hl
ret

; returns [hWhoseTurn] <-- ([hWhoseTurn] ^ $1)
; As a side effect, this also returns a duelist variable in a similar manner to
; GetNonTurnDuelistVariable, but this function appears to be
; only called to swap the turn value.
SwapTurn::
push af
push hl
call GetNonTurnDuelistVariable
ld a, h
ldh [hWhoseTurn], a
pop hl
pop af
ret

; copy the TX_END-terminated player's name from sPlayerName to de
CopyPlayerName::
call EnableSRAM
ld hl, sPlayerName
.loop
ld a, [hli]
ld [de], a
inc de
or a ; TX_END
jr nz, .loop
dec de
call DisableSRAM
ret

; copy the opponent's name to de
; if text ID at wOpponentName is non-0, copy it from there
; else, if text at wc500 is non-0, copy if from there
; else, copy Player2Text
CopyOpponentName::
ld hl, wOpponentName
ld a, [hli]
or [hl]
jr z, .special_name
ld a, [hld]
ld l, [hl]
ld h, a
jp CopyText
.special_name
ld hl, wNameBuffer
ld a, [hl]
or a
jr z, .print_player2
jr CopyPlayerName.loop
.print_player2
ldtx hl, Player2Text
jp CopyText

; returns carry if the card in a is a Basic Pokemon
CheckDeckIndexForBasicPokemon::
call LoadCardDataToBuffer2_FromDeckIndex
Expand Down
Loading

0 comments on commit 7ee531a

Please sign in to comment.