shithub: pokecrystal

Download patch

ref: c85ba78b778a0e35c2fe7f97ed4e8fee1b3c7036
parent: 9dcdad5e60fbc10ba81ae127743c78a6b00d3dd5
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Mon Oct 26 18:24:38 EDT 2020

Use more assertions to ensure correct code and data

--- a/audio/sfx.asm
+++ b/audio/sfx.asm
@@ -21,7 +21,8 @@
 Sfx_ReadText:
 Sfx_ReadText2:
 	channel_count 1
-	channel 5, Sfx_ReadText_Ch5 ; aka Sfx_ReadText2_Ch5
+	channel 5, Sfx_ReadText_Ch5
+	assert Sfx_ReadText_Ch5 == Sfx_ReadText2_Ch5
 
 Sfx_Poison:
 	channel_count 1
@@ -486,10 +487,14 @@
 Sfx_LevelUp:
 Sfx_DexFanfare5079:
 	channel_count 4
-	channel 5, Sfx_LevelUp_Ch5 ; aka Sfx_DexFanfare5079_Ch5
-	channel 6, Sfx_LevelUp_Ch6 ; aka Sfx_DexFanfare5079_Ch6
-	channel 7, Sfx_LevelUp_Ch7 ; aka Sfx_DexFanfare5079_Ch7
-	channel 8, Sfx_LevelUp_Ch8 ; aka Sfx_DexFanfare5079_Ch8
+	channel 5, Sfx_LevelUp_Ch5
+	channel 6, Sfx_LevelUp_Ch6
+	channel 7, Sfx_LevelUp_Ch7
+	channel 8, Sfx_LevelUp_Ch8
+	assert Sfx_LevelUp_Ch5 == Sfx_DexFanfare5079_Ch5
+	assert Sfx_LevelUp_Ch6 == Sfx_DexFanfare5079_Ch6
+	assert Sfx_LevelUp_Ch7 == Sfx_DexFanfare5079_Ch7
+	assert Sfx_LevelUp_Ch8 == Sfx_DexFanfare5079_Ch8
 
 Sfx_LevelUp_Ch5:
 Sfx_DexFanfare5079_Ch5:
--- a/audio/sfx_pointers.asm
+++ b/audio/sfx_pointers.asm
@@ -144,7 +144,7 @@
 	dba Sfx_MilkDrink
 	dba Sfx_Present
 	dba Sfx_MorningSun
-	dba Sfx_LevelUp ; aka Sfx_DexFanfare5079
+	dba Sfx_LevelUp
 	dba Sfx_KeyItem
 	dba Sfx_Fanfare2
 	dba Sfx_RegisterPhoneNumber
--- a/engine/battle/core.asm
+++ b/engine/battle/core.asm
@@ -7781,7 +7781,8 @@
 	jr .finish
 
 .angry
-	dec hl ; wSafariMonAngerCount
+	dec hl
+	assert wSafariMonEating - 1 == wSafariMonAngerCount
 	ld a, [hl]
 	and a
 	ret z
--- a/engine/battle_anims/anim_commands.asm
+++ b/engine/battle_anims/anim_commands.asm
@@ -245,7 +245,8 @@
 	ld c, NUM_SPRITE_OAM_STRUCTS
 .loop
 	ld a, [hl]
-	and $ff ^ (PALETTE_MASK | VRAM_BANK_1) ; PAL_BATTLE_OB_ENEMY (0)
+	and $ff ^ (PALETTE_MASK | VRAM_BANK_1) ; zeros out the palette bits
+	assert PAL_BATTLE_OB_ENEMY == 0
 	ld [hli], a
 rept SPRITEOAMSTRUCT_LENGTH - 1
 	inc hl
--- a/engine/events/bug_contest/judging.asm
+++ b/engine/events/bug_contest/judging.asm
@@ -68,7 +68,8 @@
 
 LoadContestantName:
 ; If a = 1, get your name.
-	dec a ; BUG_CONTEST_PLAYER
+	assert BUG_CONTEST_PLAYER == 1
+	dec a
 	jr z, .player
 ; Find the pointer for the trainer class of the Bug Catching Contestant whose ID is in a.
 	ld c, a
--- a/engine/gfx/load_pics.asm
+++ b/engine/gfx/load_pics.asm
@@ -208,7 +208,8 @@
 	push de
 
 	; These are assumed to be at the same address in their respective banks.
-	ld hl, PokemonPicPointers ; UnownPicPointers
+	assert PokemonPicPointers == UnownPicPointers
+	ld hl, PokemonPicPointers
 	ld a, b
 	ld d, BANK(PokemonPicPointers)
 	cp UNOWN
--- a/engine/overworld/map_object_action.asm
+++ b/engine/overworld/map_object_action.asm
@@ -273,7 +273,8 @@
 	and 2
 	ld a, FACING_BOULDER_DUST_1
 	jr z, .ok
-	inc a ; FACING_BOULDER_DUST_2
+	inc a
+	assert FACING_BOULDER_DUST_1 + 1 == FACING_BOULDER_DUST_2
 .ok
 	ld [hl], a
 	ret
--- a/engine/overworld/warp_connection.asm
+++ b/engine/overworld/warp_connection.asm
@@ -175,7 +175,8 @@
 ; MOUNT_MOON_SQUARE and TIN_TOWER_ROOF are outdoor maps within indoor maps.
 ; Dig and Escape Rope should not take you to them.
 	ld a, [wPrevMapGroup]
-	cp GROUP_MOUNT_MOON_SQUARE ; aka GROUP_TIN_TOWER_ROOF
+	cp GROUP_MOUNT_MOON_SQUARE
+	assert GROUP_MOUNT_MOON_SQUARE == GROUP_TIN_TOWER_ROOF
 	jr nz, .not_mt_moon_or_tin_tower
 	ld a, [wPrevMapNumber]
 	cp MAP_MOUNT_MOON_SQUARE
--- a/engine/pokegear/radio.asm
+++ b/engine/pokegear/radio.asm
@@ -334,6 +334,7 @@
 	; so no need for a retry loop
 	call Random
 	maskbits NUM_OAKS_POKEMON_TALK_ADVERBS
+	assert_power_of_2 NUM_OAKS_POKEMON_TALK_ADVERBS
 	ld e, a
 	ld d, 0
 	ld hl, .Adverbs
@@ -433,6 +434,7 @@
 	; so no need for a retry loop
 	call Random
 	maskbits NUM_OAKS_POKEMON_TALK_ADJECTIVES
+	assert_power_of_2 NUM_OAKS_POKEMON_TALK_ADJECTIVES
 	ld e, a
 	ld d, 0
 	ld hl, .Adjectives
@@ -1130,6 +1132,7 @@
 	; so no need for a retry loop
 	call Random
 	maskbits NUM_PNP_PEOPLE_ADJECTIVES
+	assert_power_of_2 NUM_PNP_PEOPLE_ADJECTIVES
 	ld e, a
 	ld d, 0
 	ld hl, .Adjectives
@@ -1263,6 +1266,7 @@
 	; so no need for a retry loop
 	call Random
 	maskbits NUM_PNP_PLACES_ADJECTIVES
+	assert_power_of_2 NUM_PNP_PLACES_ADJECTIVES
 	ld e, a
 	ld d, 0
 	ld hl, .Adjectives
--- a/gfx/trade/mobile_border.pal
+++ b/gfx/trade/mobile_border.pal
@@ -1,9 +1,9 @@
-	RGB 31, 31, 63
+	dw $ffff ; RGB 31, 31, 63
 	RGB 00, 00, 00
 	RGB 00, 00, 00
 	RGB 00, 00, 00
 
-	RGB 31, 31, 63
+	dw $ffff ; RGB 31, 31, 63
 	RGB 00, 00, 00
 	RGB 00, 00, 00
 	RGB 00, 00, 00
--- a/macros/code.asm
+++ b/macros/code.asm
@@ -38,6 +38,7 @@
 ; 	maskbits 26
 ; 	cp 26
 ; 	jr nc, .loop
+	assert 0 < (\1) && (\1) <= $100, "bitmask must be 8-bit"
 x = 1
 rept 8
 if x + 1 < (\1)
--- a/macros/data.asm
+++ b/macros/data.asm
@@ -25,6 +25,10 @@
 ; e.g. 1 out_of 2 == 50 percent + 1 == $80
 out_of EQUS "* $100 /"
 
+assert_power_of_2: MACRO
+	assert (\1) & ((\1) - 1) == 0, "\1 must be a power of 2"
+ENDM
+
 ; Constant data (db, dw, dl) macros
 
 dwb: MACRO
--- a/macros/gfx.asm
+++ b/macros/gfx.asm
@@ -1,5 +1,13 @@
+assert_valid_rgb: MACRO
+rept _NARG
+	assert 0 <= (\1) && (\1) <= 31, "RGB channel must be 0-31"
+	shift
+endr
+ENDM
+
 RGB: MACRO
 rept _NARG / 3
+	assert_valid_rgb \1, \2, \3
 	dw palred (\1) + palgreen (\2) + palblue (\3)
 	shift 3
 endr
--- a/macros/scripts/audio.asm
+++ b/macros/scripts/audio.asm
@@ -1,8 +1,12 @@
 channel_count: MACRO
+	assert 0 < (\1) && (\1) <= NUM_MUSIC_CHANS, \
+		"channel_count must be 1-{d:NUM_MUSIC_CHANS}"
 _num_channels = \1 - 1
 ENDM
 
 channel: MACRO
+	assert 0 < (\1) && (\1) <= NUM_CHANNELS, \
+		"channel id must be 1-{d:NUM_CHANNELS}"
 	dn (_num_channels << 2), \1 - 1 ; channel id
 	dw \2 ; address
 _num_channels = 0
@@ -46,6 +50,7 @@
 
 	const octave_cmd ; $d0
 octave: MACRO
+	assert 0 < (\1) && (\1) < 8, "octave must be 1-8"
 	db octave_cmd | 8 - (\1) ; octave
 ENDM
 
--- a/macros/scripts/battle_anims.asm
+++ b/macros/scripts/battle_anims.asm
@@ -1,7 +1,5 @@
 anim_wait: MACRO
-if \1 >= $d0
-	fail "anim_wait argument must be less than $d0."
-endc
+	assert (\1) < $d0, "anim_wait argument must be less than $d0"
 	db \1
 ENDM
 
--- a/macros/scripts/maps.asm
+++ b/macros/scripts/maps.asm
@@ -1,5 +1,7 @@
 map_id: MACRO
 ;\1: map id
+	assert DEF(GROUP_\1) && DEF(MAP_\1), \
+		"Missing 'map_const \1' in constants/map_constants.asm"
 	db GROUP_\1, MAP_\1
 ENDM