shithub: pokecrystal

Download patch

ref: 7307fc8dce605823fbdf8e5ec8530d7aaf675cfa
parent: 99df17d57173cb82abc668714727c5dada6aac73
author: Rangi <[email protected]>
date: Thu Apr 5 07:44:02 EDT 2018

Use constants for bit/set/res more

--- a/audio/engine.asm
+++ b/audio/engine.asm
@@ -1081,7 +1081,7 @@
 	ret z
 	; are we in a sfx channel?
 	ld a, [wCurChannel]
-	bit 2, a ; sfx
+	bit NOISE_CHAN_F, a
 	jr nz, .next
 	; is ch8 on? (noise)
 	ld hl, wChannel8Flags
@@ -1330,7 +1330,7 @@
 	call SetNoteDuration
 	; check current channel
 	ld a, [wCurChannel]
-	bit 2, a ; are we in a sfx channel?
+	bit NOISE_CHAN_F, a
 	jr nz, .sfx
 	ld hl, wChannel8Flags
 	bit SOUND_CHANNEL_ON, [hl] ; is ch8 on? (noise)
--- a/constants/audio_constants.asm
+++ b/constants/audio_constants.asm
@@ -31,6 +31,8 @@
 	const CHAN8 ; 7
 NUM_CHANNELS EQU const_value
 
+NOISE_CHAN_F EQU 2 ; bit set in CHAN5-CHAN7
+
 ; Flags1
 	const_def
 	const SOUND_CHANNEL_ON ; 0
--- a/constants/battle_constants.asm
+++ b/constants/battle_constants.asm
@@ -157,7 +157,7 @@
 	const FRZ
 	const PAR
 
-ALL_STATUS EQU (1 << PSN) + (1 << BRN) + (1 << FRZ) + (1 << PAR) + SLP
+ALL_STATUS EQU (1 << PSN) | (1 << BRN) | (1 << FRZ) | (1 << PAR) | SLP
 
 ; wPlayerSubStatus1 or wEnemySubStatus1 bit flags
 	enum_start 7, -1
@@ -248,3 +248,7 @@
 	const WIN
 	const LOSE
 	const DRAW
+
+BATTLERESULT_CAUGHT_CELEBI EQU 6
+BATTLERESULT_BOX_FULL EQU 7
+BATTLERESULT_BITMASK EQU (1 << BATTLERESULT_CAUGHT_CELEBI) | (1 << BATTLERESULT_BOX_FULL)
--- a/docs/bugs_and_glitches.md
+++ b/docs/bugs_and_glitches.md
@@ -949,18 +949,18 @@
 	add 3
 	ld hl, wEnemyMonLevel
 	cp [hl]
-	jr nc, .okay
-	set 0, e
-.okay
+	jr nc, .not_stronger
+	set TRANS_STRONGER_F, e
+.not_stronger
 	ld a, [wEnvironment]
 	cp CAVE
-	jr z, .okay2
+	jr z, .cave
 	cp ENVIRONMENT_5
-	jr z, .okay2
+	jr z, .cave
 	cp DUNGEON
-	jr z, .okay2
-	set 1, e
-.okay2
+	jr z, .cave
+	set TRANS_NO_CAVE_F, e
+.cave
 	ld hl, .StartingPoints
 	add hl, de
 	ld a, [hl]
@@ -969,8 +969,11 @@
 ; 8c38f (23:438f)
 
 .StartingPoints: ; 8c38f
-	db 1,  9
-	db 16, 24
+; entries correspond to TRANS_* constants
+	db BATTLETRANSITION_CAVE
+	db BATTLETRANSITION_CAVE_STRONGER
+	db BATTLETRANSITION_NO_CAVE
+	db BATTLETRANSITION_NO_CAVE_STRONGER
 ; 8c393
 ```
 
--- a/engine/battle/battle_transition.asm
+++ b/engine/battle/battle_transition.asm
@@ -1,3 +1,11 @@
+; BattleTransitionJumptable.Jumptable indexes
+BATTLETRANSITION_CAVE             EQU $01
+BATTLETRANSITION_CAVE_STRONGER    EQU $09
+BATTLETRANSITION_NO_CAVE          EQU $10
+BATTLETRANSITION_NO_CAVE_STRONGER EQU $18
+BATTLETRANSITION_FINISH           EQU $20
+BATTLETRANSITION_END              EQU $80
+
 DoBattleTransition: ; 8c20f
 	call .InitGFX
 	ld a, [rBGP]
@@ -14,7 +22,7 @@
 
 .loop
 	ld a, [wJumptableIndex]
-	bit 7, a
+	bit 7, a ; BATTLETRANSITION_END?
 	jr nz, .done
 	call BattleTransitionJumptable
 	call DelayFrame
@@ -145,13 +153,13 @@
 
 
 BattleTransitionJumptable: ; 8c314
-	jumptable .dw, wJumptableIndex
+	jumptable .Jumptable, wJumptableIndex
 ; 8c323
 
-.dw ; 8c323 (23:4323)
+.Jumptable ; 8c323 (23:4323)
 	dw StartTrainerBattle_DetermineWhichAnimation ; 00
 
-	; Animation 1: cave
+	; BATTLETRANSITION_CAVE
 	dw StartTrainerBattle_LoadPokeBallGraphics ; 01
 	dw StartTrainerBattle_SetUpBGMap ; 02
 	dw StartTrainerBattle_Flash ; 03
@@ -161,7 +169,7 @@
 	dw StartTrainerBattle_SetUpForWavyOutro ; 07
 	dw StartTrainerBattle_SineWave ; 08
 
-	; Animation 2: cave, stronger
+	; BATTLETRANSITION_CAVE_STRONGER
 	dw StartTrainerBattle_LoadPokeBallGraphics ; 09
 	dw StartTrainerBattle_SetUpBGMap ; 0a
 	dw StartTrainerBattle_Flash ; 0b
@@ -171,7 +179,7 @@
 	; There is no setup for this one
 	dw StartTrainerBattle_ZoomToBlack ; 0f
 
-	; Animation 3: no cave
+	; BATTLETRANSITION_NO_CAVE
 	dw StartTrainerBattle_LoadPokeBallGraphics ; 10
 	dw StartTrainerBattle_SetUpBGMap ; 11
 	dw StartTrainerBattle_Flash ; 12
@@ -181,7 +189,7 @@
 	dw StartTrainerBattle_SetUpForSpinOutro ; 16
 	dw StartTrainerBattle_SpinToBlack ; 17
 
-	; Animation 4: no cave, stronger
+	; BATTLETRANSITION_NO_CAVE_STRONGER
 	dw StartTrainerBattle_LoadPokeBallGraphics ; 18
 	dw StartTrainerBattle_SetUpBGMap ; 19
 	dw StartTrainerBattle_Flash ; 1a
@@ -191,10 +199,20 @@
 	dw StartTrainerBattle_SetUpForRandomScatterOutro ; 1e
 	dw StartTrainerBattle_SpeckleToBlack ; 1f
 
-	; All animations jump to here.
+	; BATTLETRANSITION_FINISH
 	dw StartTrainerBattle_Finish ; 20
 
+; transition animations
+	const_def
+	const TRANS_CAVE
+	const TRANS_CAVE_STRONGER
+	const TRANS_NO_CAVE
+	const TRANS_NO_CAVE_STRONGER
 
+; transition animation bits
+TRANS_STRONGER_F EQU 0 ; bit set in TRANS_CAVE_STRONGER and TRANS_NO_CAVE_STRONGER
+TRANS_NO_CAVE_F EQU 1 ; bit set in TRANS_NO_CAVE and TRANS_NO_CAVE_STRONGER
+
 StartTrainerBattle_DetermineWhichAnimation: ; 8c365 (23:4365)
 ; The screen flashes a different number of times depending on the level of
 ; your lead Pokemon relative to the opponent's.
@@ -205,18 +223,18 @@
 	add 3
 	ld hl, wEnemyMonLevel
 	cp [hl]
-	jr nc, .okay
-	set 0, e
-.okay
+	jr nc, .not_stronger
+	set TRANS_STRONGER_F, e
+.not_stronger
 	ld a, [wEnvironment]
 	cp CAVE
-	jr z, .okay2
+	jr z, .cave
 	cp ENVIRONMENT_5
-	jr z, .okay2
+	jr z, .cave
 	cp DUNGEON
-	jr z, .okay2
-	set 1, e
-.okay2
+	jr z, .cave
+	set TRANS_NO_CAVE_F, e
+.cave
 	ld hl, .StartingPoints
 	add hl, de
 	ld a, [hl]
@@ -225,13 +243,16 @@
 ; 8c38f (23:438f)
 
 .StartingPoints: ; 8c38f
-	db 1,  9
-	db 16, 24
+; entries correspond to TRANS_* constants
+	db BATTLETRANSITION_CAVE
+	db BATTLETRANSITION_CAVE_STRONGER
+	db BATTLETRANSITION_NO_CAVE
+	db BATTLETRANSITION_NO_CAVE_STRONGER
 ; 8c393
 
 StartTrainerBattle_Finish: ; 8c393 (23:4393)
 	call ClearSprites
-	ld a, $80
+	ld a, BATTLETRANSITION_END
 	ld [wJumptableIndex], a
 	ret
 
@@ -322,7 +343,7 @@
 	ret
 
 .end
-	ld a, $20
+	ld a, BATTLETRANSITION_FINISH
 	ld [wJumptableIndex], a
 	ret
 
@@ -394,7 +415,7 @@
 	call DelayFrame
 	xor a
 	ld [hBGMapMode], a
-	ld a, $20
+	ld a, BATTLETRANSITION_FINISH
 	ld [wJumptableIndex], a
 	ret
 ; 8c490 (23:4490)
@@ -406,6 +427,10 @@
 	const LOWER_LEFT
 	const LOWER_RIGHT
 
+; quadrant bits
+RIGHT_QUADRANT_F EQU 0 ; bit set in UPPER_RIGHT and LOWER_RIGHT
+LOWER_QUADRANT_F EQU 1 ; bit set in LOWER_LEFT and LOWER_RIGHT
+
 .spintable ; 8c490
 spintable_entry: MACRO
 	db \1
@@ -451,7 +476,7 @@
 .loop1
 	ld [hl], $ff
 	ld a, [wcf65]
-	bit 0, a
+	bit RIGHT_QUADRANT_F, a
 	jr z, .leftside
 	inc hl
 	jr .okay1
@@ -462,7 +487,7 @@
 	jr nz, .loop1
 	pop hl
 	ld a, [wcf65]
-	bit 1, a
+	bit LOWER_QUADRANT_F, a
 	ld bc, SCREEN_WIDTH
 	jr z, .upper
 	ld bc, -SCREEN_WIDTH
@@ -477,7 +502,7 @@
 	ld c, a
 .loop2
 	ld a, [wcf65]
-	bit 0, a
+	bit RIGHT_QUADRANT_F, a
 	jr z, .leftside2
 	dec hl
 	jr .okay2
@@ -530,7 +555,7 @@
 	call DelayFrame
 	xor a
 	ld [hBGMapMode], a
-	ld a, $20
+	ld a, BATTLETRANSITION_FINISH
 	ld [wJumptableIndex], a
 	ret
 
@@ -778,7 +803,7 @@
 	jr .loop
 
 .done
-	ld a, $20
+	ld a, BATTLETRANSITION_FINISH
 	ld [wJumptableIndex], a
 	ret
 ; 8c792 (23:4792)
--- a/engine/battle/core.asm
+++ b/engine/battle/core.asm
@@ -119,8 +119,8 @@
 WildFled_EnemyFled_LinkBattleCanceled: ; 3c0e5
 	call Call_LoadTempTileMapToTileMap
 	ld a, [wBattleResult]
-	and $c0
-	add $2
+	and BATTLERESULT_BITMASK
+	add DRAW
 	ld [wBattleResult], a
 	ld a, [wLinkMode]
 	and a
@@ -128,8 +128,8 @@
 	jr z, .print_text
 
 	ld a, [wBattleResult]
-	and $c0
-	ld [wBattleResult], a
+	and BATTLERESULT_BITMASK
+	ld [wBattleResult], a ; WIN
 	ld hl, BattleText_EnemyFled
 	call CheckMobileBattleError
 	jr nc, .print_text
@@ -575,8 +575,8 @@
 	and a
 	jr nz, .contest_not_over
 	ld a, [wBattleResult]
-	and $c0
-	add $2
+	and BATTLERESULT_BITMASK
+	add DRAW
 	ld [wBattleResult], a
 	scf
 	ret
@@ -2199,8 +2199,8 @@
 	call EmptyBattleTextBox
 	call LoadTileMapToTempTileMap
 	ld a, [wBattleResult]
-	and $c0
-	ld [wBattleResult], a
+	and BATTLERESULT_BITMASK
+	ld [wBattleResult], a ; WIN
 	call IsAnyMonHoldingExpShare
 	jr z, .skip_exp
 	ld hl, wEnemyMonBaseStats
@@ -2745,8 +2745,8 @@
 	ld [wCurPartyMon], a
 	callfar ChangeHappiness
 	ld a, [wBattleResult]
-	and %11000000
-	add $1
+	and BATTLERESULT_BITMASK
+	add LOSE
 	ld [wBattleResult], a
 	ld a, [wWhichMonFaintedFirst]
 	and a
@@ -3050,8 +3050,8 @@
 	jr nz, .not_tied
 	ld hl, TiedAgainstText
 	ld a, [wBattleResult]
-	and $c0
-	add 2
+	and BATTLERESULT_BITMASK
+	add DRAW
 	ld [wBattleResult], a
 	jr .text
 
@@ -3928,11 +3928,11 @@
 	cp BATTLEACTION_FORFEIT
 	ld a, DRAW
 	jr z, .fled
-	dec a
+	dec a ; LOSE
 .fled
 	ld b, a
 	ld a, [wBattleResult]
-	and $c0
+	and BATTLERESULT_BITMASK
 	add b
 	ld [wBattleResult], a
 	call StopDangerSound
@@ -5180,8 +5180,8 @@
 	xor a
 	ld [wWildMon], a
 	ld a, [wBattleResult]
-	and $c0
-	ld [wBattleResult], a
+	and BATTLERESULT_BITMASK
+	ld [wBattleResult], a ; WIN
 	call ClearWindowData
 	call SetPalettes
 	scf
@@ -8607,9 +8607,10 @@
 .proceed
 	ld a, [wBattleResult]
 	and $f
-	cp $1
-	jr c, .victory
-	jr z, .loss
+	cp LOSE
+	jr c, .victory ; WIN
+	jr z, .loss ; LOSE
+	; DRAW
 	farcall StubbedTrainerRankings_ColosseumDraws
 	ld de, .Draw
 	jr .store_result
@@ -8841,7 +8842,7 @@
 	jr nz, .not_roaming
 	ld a, [wBattleResult]
 	and $f
-	jr z, .caught_or_defeated_roam_mon
+	jr z, .caught_or_defeated_roam_mon ; WIN
 	call GetRoamMonHP
 	ld a, [wEnemyMonHP + 1]
 	ld [hl], a
@@ -9001,11 +9002,12 @@
 .StoreResult: ; 3faa0
 	ld a, [wBattleResult]
 	and $f
-	cp $1
+	cp LOSE
 	ld bc, sLinkBattleWins + 1 - sLinkBattleResults
-	jr c, .okay
+	jr c, .okay ; WIN
 	ld bc, sLinkBattleLosses + 1 - sLinkBattleResults
-	jr z, .okay
+	jr z, .okay ; LOSE
+	; DRAW
 	ld bc, sLinkBattleDraws + 1 - sLinkBattleResults
 .okay
 	add hl, bc
--- a/engine/battle/effect_commands.asm
+++ b/engine/battle/effect_commands.asm
@@ -5390,8 +5390,8 @@
 
 SetBattleDraw: ; 36804
 	ld a, [wBattleResult]
-	and $c0
-	or $2
+	and BATTLERESULT_BITMASK
+	or DRAW
 	ld [wBattleResult], a
 	ret
 
--- a/engine/battle/link_result.asm
+++ b/engine/battle/link_result.asm
@@ -41,13 +41,13 @@
 .victory
 	ld a, [wBattleResult]
 	and $f0
-	ld [wBattleResult], a
+	ld [wBattleResult], a ; WIN
 	ret
 
 .defeat
 	ld a, [wBattleResult]
 	and $f0
-	add $1
+	add LOSE
 	ld [wBattleResult], a
 	ret
 
@@ -54,7 +54,7 @@
 .drawn
 	ld a, [wBattleResult]
 	and $f0
-	add $2
+	add DRAW
 	ld [wBattleResult], a
 	ret
 
@@ -123,16 +123,16 @@
 	jr nz, .finish ; we have a pokemon that's neither fainted nor at full health
 	ld hl, wOTPartyMon1HP
 	call .CheckFaintedOrFullHealth
-	ld e, $1
+	ld e, $1 ; victory
 	ret
 
 .finish
 	ld hl, wOTPartyMon1HP
 	call .CheckFaintedOrFullHealth
-	ld e, $0
+	ld e, $0 ; drawn
 	ret nz ; we both have pokemon that are neither fainted nor at full health
-	ld e, $2
-	ld a, $1
+	ld e, $2 ; defeat
+	ld a, $1 ; not drawn
 	and a
 	ret
 
--- a/engine/events/battle_tower/battle_tower.asm
+++ b/engine/events/battle_tower/battle_tower.asm
@@ -254,7 +254,7 @@
 	farcall HealParty
 	ld a, [wBattleResult]
 	ld [wScriptVar], a
-	and a
+	and a ; WIN?
 	jr nz, .lost
 	ld a, BANK(sNrOfBeatenBattleTowerTrainers)
 	call GetSRAMBank
--- a/engine/events/bug_contest/display_stats.asm
+++ b/engine/events/bug_contest/display_stats.asm
@@ -7,7 +7,7 @@
 	ld hl, wOptions
 	ld a, [hl]
 	push af
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 
 	hlcoord 0, 0
 	ld b, 4
--- a/engine/events/celebi.asm
+++ b/engine/events/celebi.asm
@@ -325,15 +325,15 @@
 
 CheckCaughtCelebi: ; 49bf9
 	ld a, [wBattleResult]
-	bit 6, a
+	bit BATTLERESULT_CAUGHT_CELEBI, a
 	jr z, .false
-	ld a, $1
+	ld a, TRUE
 	ld [wScriptVar], a
 	jr .done
 
 
 .false
-	xor a
+	xor a ; FALSE
 	ld [wScriptVar], a
 
 .done
--- a/engine/games/card_flip.asm
+++ b/engine/games/card_flip.asm
@@ -12,7 +12,7 @@
 
 _CardFlip: ; e00ee (38:40ee)
 	ld hl, wOptions
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 	call ClearBGPalettes
 	call ClearTileMap
 	call ClearSprites
--- a/engine/items/item_effects.asm
+++ b/engine/items/item_effects.asm
@@ -537,7 +537,7 @@
 	cp BATTLETYPE_CELEBI
 	jr nz, .not_celebi
 	ld hl, wBattleResult
-	set 6, [hl]
+	set BATTLERESULT_CAUGHT_CELEBI, [hl]
 .not_celebi
 
 	ld a, [wPartyCount]
@@ -615,7 +615,7 @@
 	cp MONS_PER_BOX
 	jr nz, .BoxNotFullYet
 	ld hl, wBattleResult
-	set 7, [hl]
+	set BATTLERESULT_BOX_FULL, [hl]
 .BoxNotFullYet:
 	ld a, [wCurItem]
 	cp FRIEND_BALL
@@ -2196,8 +2196,8 @@
 	inc a
 	ld [wForcedSwitch], a
 	ld a, [wBattleResult]
-	and $c0
-	or $2
+	and BATTLERESULT_BITMASK
+	or DRAW
 	ld [wBattleResult], a
 	jp UseItemText
 
--- a/engine/movie/trade_animation.asm
+++ b/engine/movie/trade_animation.asm
@@ -126,7 +126,7 @@
 	ld hl, wOptions
 	ld a, [hl]
 	push af
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 	call .TradeAnimLayout
 	ld a, [wcf66]
 	and a
--- a/engine/overworld/scripting.asm
+++ b/engine/overworld/scripting.asm
@@ -1372,7 +1372,7 @@
 	call BufferScreen
 	predef StartBattle
 	ld a, [wBattleResult]
-	and $3f
+	and $ff ^ BATTLERESULT_BITMASK
 	ld [wScriptVar], a
 	ret
 
@@ -1391,10 +1391,10 @@
 
 	ld hl, wBattleScriptFlags
 	ld d, [hl]
-	ld [hl], $0
+	ld [hl], 0
 	ld a, [wBattleResult]
-	and $3f
-	cp $1
+	and $ff ^ BATTLERESULT_BITMASK
+	cp LOSE
 	jr nz, .notblackedout
 	ld b, BANK(Script_BattleWhiteout)
 	ld hl, Script_BattleWhiteout
@@ -1408,7 +1408,7 @@
 
 .was_wild
 	ld a, [wBattleResult]
-	bit 7, a
+	bit BATTLERESULT_BOX_FULL, a
 	jr z, .done
 	ld b, BANK(Script_SpecialBillCall)
 	ld de, Script_SpecialBillCall
--- a/engine/overworld/variables.asm
+++ b/engine/overworld/variables.asm
@@ -146,6 +146,6 @@
 
 .BattleResult: ; 80728
 	ld a, [wBattleResult]
-	and $3f
+	and $ff ^ BATTLERESULT_BITMASK
 	jp .loadstringbuffer2
 ; 80730
--- a/engine/pokemon/bills_pc.asm
+++ b/engine/pokemon/bills_pc.asm
@@ -2,7 +2,7 @@
 	ld hl, wOptions
 	ld a, [hl]
 	push af
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 	ld a, [wVramState]
 	push af
 	xor a
@@ -264,7 +264,7 @@
 	ld hl, wOptions
 	ld a, [hl]
 	push af
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 	ld a, [wVramState]
 	push af
 	xor a
@@ -507,7 +507,7 @@
 	ld hl, wOptions
 	ld a, [hl]
 	push af
-	set 4, [hl]
+	set NO_TEXT_SCROLL, [hl]
 	ld a, [wVramState]
 	push af
 	xor a
--- a/engine/pokemon/party_menu.asm
+++ b/engine/pokemon/party_menu.asm
@@ -778,7 +778,7 @@
 .gotstring ; 504be
 	ld a, [wOptions]
 	push af
-	set 4, a ; disable text delay
+	set NO_TEXT_SCROLL, a
 	ld [wOptions], a
 	hlcoord 1, 16 ; Coord
 	call PlaceString
--- a/home/trainers.asm
+++ b/home/trainers.asm
@@ -247,7 +247,7 @@
 .canlose
 	ld a, [wBattleResult]
 	ld hl, wWinTextPointer
-	and $f
+	and $f ; WIN?
 	jr z, .ok
 	ld hl, wLossTextPointer
 
--- a/mobile/mobile_5c.asm
+++ b/mobile/mobile_5c.asm
@@ -106,7 +106,7 @@
 	call GetSRAMBank
 	ld hl, $a894
 	ld a, [wBattleResult]
-	and a
+	and a ; WIN?
 	jr nz, .asm_170c15
 	inc [hl]
 
--- a/wram.asm
+++ b/wram.asm
@@ -1504,8 +1504,11 @@
 
 	ds 1
 
-wcfbe:: ; SGB flags?
-; bit 7
+wcfbe:: ; cfbe
+; bits 4, 6, or 7 can be used to disable joypad input
+; bit 4
+; bit 6: mon fainted?
+; bit 7: SGB flag?
 	db
 
 	ds 1
@@ -1893,7 +1896,11 @@
 ;        flickers when climbing waterfall
 	db
 
-wBattleResult:: db ; d0ee
+wBattleResult:: ; d0ee
+; WIN, LOSE, or DRAW
+; bit 6: caught celebi
+; bit 7: box full
+	db
 wUsingItemWithSelect:: db ; d0ef
 
 UNION ; d0f0