Skip to content

Commit

Permalink
Implement backup items, used to reverse opponent Thief usage
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrIQ committed Dec 10, 2024
1 parent 1e87ff9 commit 8238b17
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 104 deletions.
119 changes: 63 additions & 56 deletions engine/battle/core.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; Core components of the battle engine.

DoBattle:
call BackupBattleItems
xor a
ld [wBattleParticipantsNotFainted], a
ld [wBattleParticipantsIncludingFainted], a
Expand Down Expand Up @@ -152,7 +153,7 @@ SafariBattleTurn:
ret c
ld a, [wBattleEnded]
and a
ret nz
ret nz
jr .loop

CheckSafariBattleOver:
Expand Down Expand Up @@ -415,37 +416,21 @@ HandleBerserkGene:

.player
call SetPlayerTurn
ld de, wPartyMon1Item
ld a, [wCurBattleMon]
ld b, a
jr .go

.enemy
call SetEnemyTurn
ld de, wOTPartyMon1Item
ld a, [wCurOTMon]
ld b, a
; fallthrough

.go
push de
push bc
callfar GetUserItem
ld a, [hl]
ld [wNamedObjectIndex], a
sub BERSERK_GENE
pop bc
pop de
cp BERSERK_GENE
ret nz

ld [hl], a
call ConsumeUserItem

ld h, d
ld l, e
ld a, b
call GetPartyLocation
xor a
ld [hl], a
ld a, BATTLE_VARS_SUBSTATUS3
call GetBattleVarAddr
push af
Expand Down Expand Up @@ -1505,28 +1490,53 @@ HandleLeppaberry:
callfar GetUserItem
ld a, [hl]
ld [wNamedObjectIndex], a
xor a
ld [hl], a
call ConsumeUserItem
call GetItemName
call SwitchTurnCore
call ItemRecoveryAnim
call SwitchTurnCore
ld hl, BattleText_UserRecoveredPPUsing
jp StdBattleTextbox

ConsumeUserItem:
; Consume (use up) the user's held item.
call GetPartymonItem
ldh a, [hBattleTurn]
and a
jr z, .consume_item
ld a, [wBattleMode]
dec a
jr z, .skip_consumption
call GetOTPartymonItem
push af
call nz, GetOTPartymonItem

.consume_item
; Consume the battler's held item.
xor a
ld [bc], a
pop af

jr z, .check_backup_item

; Opponent wildmons do not have a party struct.
ld a, [wBattleMode]
sub TRAINER_BATTLE
ret nz
ld [hl], a
ret

.skip_consumption
call GetItemName
call SwitchTurnCore
call ItemRecoveryAnim
call SwitchTurnCore
ld hl, BattleText_UserRecoveredPPUsing
jp StdBattleTextbox
.check_backup_item
; We might also want to update the backup item struct.
call GetBackupItemAddr
ld a, [hl]
push hl
push af
call GetPartymonItem
pop af
sub [hl]
ld [hl], NO_ITEM
pop hl

; If the items don't match, do nothing. This can happen if the foe steals
; item X, player steals item Y, player uses item Y.
ret nz
ld [hl], a
ret

HandleFutureSight:
ldh a, [hSerialConnectionStatus]
Expand Down Expand Up @@ -4616,24 +4626,9 @@ UseConfusionHealingItem:
call ItemRecoveryAnim
ld hl, BattleText_ItemHealedConfusion
call StdBattleTextbox
ldh a, [hBattleTurn]
and a
jr nz, .do_partymon
call GetOTPartymonItem
xor a
ld [bc], a
ld a, [wBattleMode]
dec a
ret z
ld [hl], $0
ret

.do_partymon
call GetPartymonItem
xor a
ld [bc], a
ld [hl], a
ret
call SwitchTurnCore
call ConsumeUserItem
jp SwitchTurnCore

HandleStatBoostingHeldItems:
; The effects handled here are not used in-game.
Expand Down Expand Up @@ -4689,9 +4684,7 @@ HandleStatBoostingHeldItems:
ld a, [wFailedMessage]
and a
ret nz
xor a
ld [bc], a
ld [de], a
call ConsumeUserItem
call GetItemName
ld hl, BattleText_UsersStringBuffer1Activated
call StdBattleTextbox
Expand Down Expand Up @@ -9314,6 +9307,20 @@ InitBattleDisplay:
call GetTrainerBackpic
jp CopyBackpic

MobileTextBorder::
; For mobile link battles only.
ld a, [wLinkMode]
cp LINK_MOBILE
ret c

; Draw a cell phone icon at the
; top right corner of the border.
hlcoord 19, 12
ld [hl], $5e ; top
hlcoord 19, 13
ld [hl], $5f ; bottom
ret

GetTrainerBackpic:
; Load the player character's backpic (6x6) into VRAM starting from vTiles2 tile $31.

Expand Down Expand Up @@ -9546,4 +9553,4 @@ GetWeatherImage:
db $88, $1c ; y/x - bottom right
db $88, $14 ; y/x - bottom left
db $80, $1c ; y/x - top right
db $80, $14 ; y/x - top left
db $80, $14 ; y/x - top left
27 changes: 20 additions & 7 deletions engine/battle/move_effects/thief.asm
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ BattleCommand_Thief:
and a
jr z, .stealenemyitem

ld a, [wBattleMode]
dec a
ret z

.stealenemyitem
call .enemyitem
xor a
Expand All @@ -46,6 +42,12 @@ BattleCommand_Thief:
ld a, [wNamedObjectIndex]
ld [hl], a
ld [de], a

; If you steal from a wildmon, you do so permanently.
ld a, [wBattleMode]
dec a
ld b, [hl]
call z, SetBackupItem
jr .stole

.enemy
Expand Down Expand Up @@ -80,9 +82,7 @@ BattleCommand_Thief:
and a
ret nz

; If the enemy steals your item,
; it's gone for good if you don't get it back.

; If a foe steals an item, it is returned after battle.
call .playeritem
xor a
ld [hl], a
Expand Down Expand Up @@ -117,3 +117,16 @@ BattleCommand_Thief:
.cant
or 1
ret

SetBackupItem::
; If backup is empty, replace with b if our turn (even in trainer battles)
ldh a, [hBattleTurn]
and a
ret nz

call GetBackupItemAddr
ld a, [hl]
and a
ret nz
ld [hl], b
ret
20 changes: 19 additions & 1 deletion engine/items/item_effects.asm
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,34 @@ PokeBallEffect:

ld a, [wBattleType]
cp BATTLETYPE_TUTORIAL
jr z, .room_in_party
jr z, .backup_item_done

ld a, [wPartyCount]
cp PARTY_LENGTH
jr nz, .room_in_party

newfarcall NewStorageBoxPointer
jp c, Ball_BoxIsFullMessage
jr .backup_item_done

.room_in_party
; a == wPartyCount, used to insert backup item
ld c, a

; Don't mess with item backup struct if we are in a bug contest
ld a, [wBattleType]
cp BATTLETYPE_CONTEST
jr z, .backup_item_done

; Copy wildmon's item to item backup struct in case we catch
ld hl, wPartyBackupItems
ld b, 0
add hl, bc
ld a, [wEnemyMonItem]
ld [hl], a
jr .room_in_party

.backup_item_done
xor a
ld [wWildMon], a
ld a, [wBattleType]
Expand Down
1 change: 1 addition & 0 deletions engine/overworld/scripting.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ Script_catchtutorial:
jp Script_reloadmap

Script_reloadmapafterbattle:
call RestoreBattleItems
ld hl, wBattleScriptFlags
ld d, [hl]
ld [hl], 0
Expand Down
Loading

0 comments on commit 8238b17

Please sign in to comment.