shithub: pokecrystal

Download patch

ref: a1498e63e27d651ef85fdfc7bcc255d67fb04d57
parent: 66d84ff021bf22e607b766867a1a6ce7ce9855ed
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Fri Jun 17 19:27:53 EDT 2022

Rename search.asm and search2.asm

--- a/docs/bugs_and_glitches.md
+++ b/docs/bugs_and_glitches.md
@@ -2354,7 +2354,7 @@
 
 This bug can allow you to talk to Eusine in Celadon City and encounter Ho-Oh with only traded legendary beasts.
 
-**Fix:** Edit `CheckOwnMon` in [engine/pokemon/search.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokemon/search.asm):
+**Fix:** Edit `CheckOwnMon` in [engine/pokemon/search_owned.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokemon/search_owned.asm):
 
 ```diff
  ; check OT
@@ -2386,7 +2386,7 @@
 
 This bug can prevent you from talking to Eusine in Celadon City or encountering Ho-Oh when a caught legendary beast is in the Day-Care.
 
-**Fix:** Edit `CheckOwnMonAnywhere` in [engine/pokemon/search.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokemon/search.asm):
+**Fix:** Edit `CheckOwnMonAnywhere` in [engine/pokemon/search_owned.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokemon/search_owned.asm):
 
 ```diff
  	; If there are no monsters in the party,
--- a/engine/pokemon/search.asm
+++ /dev/null
@@ -1,277 +1,0 @@
-BeastsCheck:
-; Check if the player owns all three legendary beasts.
-; They must exist in either party or PC, and have the player's OT and ID.
-; Return the result in wScriptVar.
-
-	ld a, RAIKOU
-	ld [wScriptVar], a
-	call CheckOwnMonAnywhere
-	jr nc, .notexist
-
-	ld a, ENTEI
-	ld [wScriptVar], a
-	call CheckOwnMonAnywhere
-	jr nc, .notexist
-
-	ld a, SUICUNE
-	ld [wScriptVar], a
-	call CheckOwnMonAnywhere
-	jr nc, .notexist
-
-	; they exist
-	ld a, 1
-	ld [wScriptVar], a
-	ret
-
-.notexist
-	xor a
-	ld [wScriptVar], a
-	ret
-
-MonCheck:
-; Check if the player owns any Pokémon of the species in wScriptVar.
-; Return the result in wScriptVar.
-
-	call CheckOwnMonAnywhere
-	jr c, .exists
-
-	; doesn't exist
-	xor a
-	ld [wScriptVar], a
-	ret
-
-.exists
-	ld a, 1
-	ld [wScriptVar], a
-	ret
-
-CheckOwnMonAnywhere:
-; Check if the player owns any monsters of the species in wScriptVar.
-; It must exist in either party or PC, and have the player's OT and ID.
-
-	; If there are no monsters in the party,
-	; the player must not own any yet.
-
-	ld a, [wPartyCount]
-	and a
-	ret z
-
-	ld d, a
-	ld e, 0
-	ld hl, wPartyMon1Species
-	ld bc, wPartyMonOTs
-
-	; Run CheckOwnMon on each Pokémon in the party.
-
-.partymon
-	call CheckOwnMon
-	ret c
-
-	push bc
-	ld bc, PARTYMON_STRUCT_LENGTH
-	add hl, bc
-	pop bc
-	call UpdateOTPointer
-	dec d
-	jr nz, .partymon
-
-	; Run CheckOwnMon on each Pokémon in the PC.
-
-	ld a, BANK(sBoxCount)
-	call OpenSRAM
-	ld a, [sBoxCount]
-	and a
-	jr z, .boxes
-
-	ld d, a
-	ld hl, sBoxMon1Species
-	ld bc, sBoxMonOTs
-.openboxmon
-	call CheckOwnMon
-	jr nc, .loop
-
-	call CloseSRAM
-	ret
-
-.loop
-	push bc
-	ld bc, BOXMON_STRUCT_LENGTH
-	add hl, bc
-	pop bc
-	call UpdateOTPointer
-	dec d
-	jr nz, .openboxmon
-
-	; Run CheckOwnMon on each monster in the other 13 PC boxes.
-
-.boxes
-	call CloseSRAM
-
-	ld c, 0
-.box
-	; Don't search the current box again.
-	ld a, [wCurBox]
-	and $f
-	cp c
-	jr z, .loopbox
-
-	; Load the box.
-
-	ld hl, SearchBoxAddressTable
-	ld b, 0
-	add hl, bc
-	add hl, bc
-	add hl, bc
-	ld a, [hli]
-	call OpenSRAM
-	ld a, [hli]
-	ld h, [hl]
-	ld l, a
-
-	; Number of monsters in the box
-
-	ld a, [hl]
-	and a
-	jr z, .loopbox
-
-	push bc
-
-	push hl
-	ld de, sBoxMons - sBoxCount
-	add hl, de
-	ld d, h
-	ld e, l
-	pop hl
-	push de
-	ld de, sBoxMonOTs - sBoxCount
-	add hl, de
-	ld b, h
-	ld c, l
-	pop hl
-
-	ld d, a
-
-.boxmon
-	call CheckOwnMon
-	jr nc, .loopboxmon
-
-	pop bc
-	call CloseSRAM
-	ret
-
-.loopboxmon
-	push bc
-	ld bc, BOXMON_STRUCT_LENGTH
-	add hl, bc
-	pop bc
-	call UpdateOTPointer
-	dec d
-	jr nz, .boxmon
-	pop bc
-
-.loopbox
-	inc c
-	ld a, c
-	cp NUM_BOXES
-	jr c, .box
-
-	call CloseSRAM
-	and a
-	ret
-
-CheckOwnMon:
-; Check if a Pokémon belongs to the player and is of a specific species.
-; We compare the species we are looking for in [wScriptVar] to the species
-; we have in [hl].
-
-; inputs:
-; hl, pointer to PartyMonNSpecies
-; bc, pointer to PartyMonNOT
-; wScriptVar should contain the species we're looking for
-
-; outputs:
-; sets carry if monster matches species, ID, and OT name.
-
-	push bc
-	push hl
-	push de
-	ld d, b
-	ld e, c
-
-	; check species
-
-	ld a, [wScriptVar]
-	ld b, [hl]
-	cp b
-	jr nz, .notfound
-
-	; check ID number
-
-	ld bc, MON_ID
-	add hl, bc
-	ld a, [wPlayerID]
-	cp [hl]
-	jr nz, .notfound
-	inc hl
-	ld a, [wPlayerID + 1]
-	cp [hl]
-	jr nz, .notfound
-
-	; check OT
-
-	ld hl, wPlayerName
-
-rept NAME_LENGTH_JAPANESE - 2 ; should be PLAYER_NAME_LENGTH - 2
-	ld a, [de]
-	cp [hl]
-	jr nz, .notfound
-	cp "@"
-	jr z, .found
-	inc hl
-	inc de
-endr
-
-	ld a, [de]
-	cp [hl]
-	jr z, .found
-
-.notfound
-	pop de
-	pop hl
-	pop bc
-	and a
-	ret
-
-.found
-	pop de
-	pop hl
-	pop bc
-	scf
-	ret
-
-SearchBoxAddressTable:
-	table_width 3, SearchBoxAddressTable
-	dba sBox1
-	dba sBox2
-	dba sBox3
-	dba sBox4
-	dba sBox5
-	dba sBox6
-	dba sBox7
-	dba sBox8
-	dba sBox9
-	dba sBox10
-	dba sBox11
-	dba sBox12
-	dba sBox13
-	dba sBox14
-	assert_table_length NUM_BOXES
-
-UpdateOTPointer:
-	push hl
-	ld hl, NAME_LENGTH
-	add hl, bc
-	ld b, h
-	ld c, l
-	pop hl
-	ret
--- a/engine/pokemon/search2.asm
+++ /dev/null
@@ -1,134 +1,0 @@
-_FindPartyMonAboveLevel:
-	ld hl, wPartyMon1Level
-	call FindAboveLevel
-	ret
-
-_FindPartyMonAtLeastThatHappy:
-	ld hl, wPartyMon1Happiness
-	call FindAtLeastThatHappy
-	ret
-
-_FindPartyMonThatSpecies:
-	ld hl, wPartyMon1Species
-	jp FindThatSpecies
-
-_FindPartyMonThatSpeciesYourTrainerID:
-	ld hl, wPartyMon1Species
-	call FindThatSpecies
-	ret z
-	ld a, c
-	ld hl, wPartyMon1ID
-	ld bc, PARTYMON_STRUCT_LENGTH
-	call AddNTimes
-	ld a, [wPlayerID]
-	cp [hl]
-	jr nz, .nope
-	inc hl
-	ld a, [wPlayerID + 1]
-	cp [hl]
-	jr nz, .nope
-	ld a, $1
-	and a
-	ret
-
-.nope
-	xor a
-	ret
-
-FindAtLeastThatHappy:
-; Sets the bits for the Pokemon that have a happiness greater than or equal to b.
-; The lowest bits are used.  Sets z if no Pokemon in your party is at least that happy.
-	ld c, $0
-	ld a, [wPartyCount]
-	ld d, a
-.loop
-	ld a, d
-	dec a
-	push hl
-	push bc
-	ld bc, PARTYMON_STRUCT_LENGTH
-	call AddNTimes
-	pop bc
-	ld a, b
-	cp [hl]
-	pop hl
-	jr z, .greater_equal
-	jr nc, .lower
-
-.greater_equal
-	ld a, c
-	or $1
-	ld c, a
-
-.lower
-	sla c
-	dec d
-	jr nz, .loop
-	call RetroactivelyIgnoreEggs
-	ld a, c
-	and a
-	ret
-
-FindAboveLevel:
-	ld c, $0
-	ld a, [wPartyCount]
-	ld d, a
-.loop
-	ld a, d
-	dec a
-	push hl
-	push bc
-	ld bc, PARTYMON_STRUCT_LENGTH
-	call AddNTimes
-	pop bc
-	ld a, b
-	cp [hl]
-	pop hl
-	jr c, .greater
-	ld a, c
-	or $1
-	ld c, a
-
-.greater
-	sla c
-	dec d
-	jr nz, .loop
-	call RetroactivelyIgnoreEggs
-	ld a, c
-	and a
-	ret
-
-FindThatSpecies:
-; Find species b in your party.
-; If you have no Pokemon, returns c = -1 and z.
-; If that species is in your party, returns its location in c, and nz.
-; Otherwise, returns z.
-	ld c, -1
-	ld hl, wPartySpecies
-.loop
-	ld a, [hli]
-	cp -1
-	ret z
-	inc c
-	cp b
-	jr nz, .loop
-	ld a, $1
-	and a
-	ret
-
-RetroactivelyIgnoreEggs:
-	ld e, %11111110
-	ld hl, wPartySpecies
-.loop
-	ld a, [hli]
-	cp -1
-	ret z
-	cp EGG
-	jr nz, .skip_notegg
-	ld a, c
-	and e
-	ld c, a
-
-.skip_notegg
-	rlc e
-	jr .loop
--- /dev/null
+++ b/engine/pokemon/search_owned.asm
@@ -1,0 +1,277 @@
+BeastsCheck:
+; Check if the player owns all three legendary beasts.
+; They must exist in either party or PC, and have the player's OT and ID.
+; Return the result in wScriptVar.
+
+	ld a, RAIKOU
+	ld [wScriptVar], a
+	call CheckOwnMonAnywhere
+	jr nc, .notexist
+
+	ld a, ENTEI
+	ld [wScriptVar], a
+	call CheckOwnMonAnywhere
+	jr nc, .notexist
+
+	ld a, SUICUNE
+	ld [wScriptVar], a
+	call CheckOwnMonAnywhere
+	jr nc, .notexist
+
+	; they exist
+	ld a, 1
+	ld [wScriptVar], a
+	ret
+
+.notexist
+	xor a
+	ld [wScriptVar], a
+	ret
+
+MonCheck:
+; Check if the player owns any Pokémon of the species in wScriptVar.
+; Return the result in wScriptVar.
+
+	call CheckOwnMonAnywhere
+	jr c, .exists
+
+	; doesn't exist
+	xor a
+	ld [wScriptVar], a
+	ret
+
+.exists
+	ld a, 1
+	ld [wScriptVar], a
+	ret
+
+CheckOwnMonAnywhere:
+; Check if the player owns any monsters of the species in wScriptVar.
+; It must exist in either party or PC, and have the player's OT and ID.
+
+	; If there are no monsters in the party,
+	; the player must not own any yet.
+
+	ld a, [wPartyCount]
+	and a
+	ret z
+
+	ld d, a
+	ld e, 0
+	ld hl, wPartyMon1Species
+	ld bc, wPartyMonOTs
+
+	; Run CheckOwnMon on each Pokémon in the party.
+
+.partymon
+	call CheckOwnMon
+	ret c
+
+	push bc
+	ld bc, PARTYMON_STRUCT_LENGTH
+	add hl, bc
+	pop bc
+	call UpdateOTPointer
+	dec d
+	jr nz, .partymon
+
+	; Run CheckOwnMon on each Pokémon in the PC.
+
+	ld a, BANK(sBoxCount)
+	call OpenSRAM
+	ld a, [sBoxCount]
+	and a
+	jr z, .boxes
+
+	ld d, a
+	ld hl, sBoxMon1Species
+	ld bc, sBoxMonOTs
+.openboxmon
+	call CheckOwnMon
+	jr nc, .loop
+
+	call CloseSRAM
+	ret
+
+.loop
+	push bc
+	ld bc, BOXMON_STRUCT_LENGTH
+	add hl, bc
+	pop bc
+	call UpdateOTPointer
+	dec d
+	jr nz, .openboxmon
+
+	; Run CheckOwnMon on each monster in the other 13 PC boxes.
+
+.boxes
+	call CloseSRAM
+
+	ld c, 0
+.box
+	; Don't search the current box again.
+	ld a, [wCurBox]
+	and $f
+	cp c
+	jr z, .loopbox
+
+	; Load the box.
+
+	ld hl, SearchBoxAddressTable
+	ld b, 0
+	add hl, bc
+	add hl, bc
+	add hl, bc
+	ld a, [hli]
+	call OpenSRAM
+	ld a, [hli]
+	ld h, [hl]
+	ld l, a
+
+	; Number of monsters in the box
+
+	ld a, [hl]
+	and a
+	jr z, .loopbox
+
+	push bc
+
+	push hl
+	ld de, sBoxMons - sBoxCount
+	add hl, de
+	ld d, h
+	ld e, l
+	pop hl
+	push de
+	ld de, sBoxMonOTs - sBoxCount
+	add hl, de
+	ld b, h
+	ld c, l
+	pop hl
+
+	ld d, a
+
+.boxmon
+	call CheckOwnMon
+	jr nc, .loopboxmon
+
+	pop bc
+	call CloseSRAM
+	ret
+
+.loopboxmon
+	push bc
+	ld bc, BOXMON_STRUCT_LENGTH
+	add hl, bc
+	pop bc
+	call UpdateOTPointer
+	dec d
+	jr nz, .boxmon
+	pop bc
+
+.loopbox
+	inc c
+	ld a, c
+	cp NUM_BOXES
+	jr c, .box
+
+	call CloseSRAM
+	and a
+	ret
+
+CheckOwnMon:
+; Check if a Pokémon belongs to the player and is of a specific species.
+; We compare the species we are looking for in [wScriptVar] to the species
+; we have in [hl].
+
+; inputs:
+; hl, pointer to PartyMonNSpecies
+; bc, pointer to PartyMonNOT
+; wScriptVar should contain the species we're looking for
+
+; outputs:
+; sets carry if monster matches species, ID, and OT name.
+
+	push bc
+	push hl
+	push de
+	ld d, b
+	ld e, c
+
+	; check species
+
+	ld a, [wScriptVar]
+	ld b, [hl]
+	cp b
+	jr nz, .notfound
+
+	; check ID number
+
+	ld bc, MON_ID
+	add hl, bc
+	ld a, [wPlayerID]
+	cp [hl]
+	jr nz, .notfound
+	inc hl
+	ld a, [wPlayerID + 1]
+	cp [hl]
+	jr nz, .notfound
+
+	; check OT
+
+	ld hl, wPlayerName
+
+rept NAME_LENGTH_JAPANESE - 2 ; should be PLAYER_NAME_LENGTH - 2
+	ld a, [de]
+	cp [hl]
+	jr nz, .notfound
+	cp "@"
+	jr z, .found
+	inc hl
+	inc de
+endr
+
+	ld a, [de]
+	cp [hl]
+	jr z, .found
+
+.notfound
+	pop de
+	pop hl
+	pop bc
+	and a
+	ret
+
+.found
+	pop de
+	pop hl
+	pop bc
+	scf
+	ret
+
+SearchBoxAddressTable:
+	table_width 3, SearchBoxAddressTable
+	dba sBox1
+	dba sBox2
+	dba sBox3
+	dba sBox4
+	dba sBox5
+	dba sBox6
+	dba sBox7
+	dba sBox8
+	dba sBox9
+	dba sBox10
+	dba sBox11
+	dba sBox12
+	dba sBox13
+	dba sBox14
+	assert_table_length NUM_BOXES
+
+UpdateOTPointer:
+	push hl
+	ld hl, NAME_LENGTH
+	add hl, bc
+	ld b, h
+	ld c, l
+	pop hl
+	ret
--- /dev/null
+++ b/engine/pokemon/search_party.asm
@@ -1,0 +1,134 @@
+_FindPartyMonAboveLevel:
+	ld hl, wPartyMon1Level
+	call FindAboveLevel
+	ret
+
+_FindPartyMonAtLeastThatHappy:
+	ld hl, wPartyMon1Happiness
+	call FindAtLeastThatHappy
+	ret
+
+_FindPartyMonThatSpecies:
+	ld hl, wPartyMon1Species
+	jp FindThatSpecies
+
+_FindPartyMonThatSpeciesYourTrainerID:
+	ld hl, wPartyMon1Species
+	call FindThatSpecies
+	ret z
+	ld a, c
+	ld hl, wPartyMon1ID
+	ld bc, PARTYMON_STRUCT_LENGTH
+	call AddNTimes
+	ld a, [wPlayerID]
+	cp [hl]
+	jr nz, .nope
+	inc hl
+	ld a, [wPlayerID + 1]
+	cp [hl]
+	jr nz, .nope
+	ld a, $1
+	and a
+	ret
+
+.nope
+	xor a
+	ret
+
+FindAtLeastThatHappy:
+; Sets the bits for the Pokemon that have a happiness greater than or equal to b.
+; The lowest bits are used.  Sets z if no Pokemon in your party is at least that happy.
+	ld c, $0
+	ld a, [wPartyCount]
+	ld d, a
+.loop
+	ld a, d
+	dec a
+	push hl
+	push bc
+	ld bc, PARTYMON_STRUCT_LENGTH
+	call AddNTimes
+	pop bc
+	ld a, b
+	cp [hl]
+	pop hl
+	jr z, .greater_equal
+	jr nc, .lower
+
+.greater_equal
+	ld a, c
+	or $1
+	ld c, a
+
+.lower
+	sla c
+	dec d
+	jr nz, .loop
+	call RetroactivelyIgnoreEggs
+	ld a, c
+	and a
+	ret
+
+FindAboveLevel:
+	ld c, $0
+	ld a, [wPartyCount]
+	ld d, a
+.loop
+	ld a, d
+	dec a
+	push hl
+	push bc
+	ld bc, PARTYMON_STRUCT_LENGTH
+	call AddNTimes
+	pop bc
+	ld a, b
+	cp [hl]
+	pop hl
+	jr c, .greater
+	ld a, c
+	or $1
+	ld c, a
+
+.greater
+	sla c
+	dec d
+	jr nz, .loop
+	call RetroactivelyIgnoreEggs
+	ld a, c
+	and a
+	ret
+
+FindThatSpecies:
+; Find species b in your party.
+; If you have no Pokemon, returns c = -1 and z.
+; If that species is in your party, returns its location in c, and nz.
+; Otherwise, returns z.
+	ld c, -1
+	ld hl, wPartySpecies
+.loop
+	ld a, [hli]
+	cp -1
+	ret z
+	inc c
+	cp b
+	jr nz, .loop
+	ld a, $1
+	and a
+	ret
+
+RetroactivelyIgnoreEggs:
+	ld e, %11111110
+	ld hl, wPartySpecies
+.loop
+	ld a, [hli]
+	cp -1
+	ret z
+	cp EGG
+	jr nz, .skip_notegg
+	ld a, c
+	and e
+	ld c, a
+
+.skip_notegg
+	rlc e
+	jr .loop
--- a/main.asm
+++ b/main.asm
@@ -188,7 +188,7 @@
 INCLUDE "engine/events/celebi.asm"
 INCLUDE "engine/menus/main_menu.asm"
 INCLUDE "mobile/mobile_menu.asm"
-INCLUDE "engine/pokemon/search.asm"
+INCLUDE "engine/pokemon/search_owned.asm"
 INCLUDE "mobile/mobile_12_2.asm"
 INCLUDE "engine/events/buena_menu.asm"
 
@@ -217,7 +217,7 @@
 INCLUDE "engine/events/pokerus/check_pokerus.asm"
 INCLUDE "engine/events/lucky_number.asm"
 INCLUDE "engine/pokemon/caught_data.asm"
-INCLUDE "engine/pokemon/search2.asm"
+INCLUDE "engine/pokemon/search_party.asm"
 INCLUDE "engine/pokemon/stats_screen.asm"
 INCLUDE "engine/events/catch_tutorial.asm"
 INCLUDE "engine/movie/evolution_animation.asm"