shithub: pokecrystal

Download patch

ref: 9cd59db9aeaa26db321cc99719785a592edcca60
parent: ceb747c622cd90e9796515e917f2a0e00ebd7764
parent: 576cbf5b3e4ae7cd377dfeb0d75dca54b44518bc
author: Rangi <[email protected]>
date: Sat Apr 18 14:38:54 EDT 2020

Merge pull request #713 from Rangi42/master

Miscellaneous fixes, including one for the Pokédex design flaw by ax6

--- a/docs/bugs_and_glitches.md
+++ b/docs/bugs_and_glitches.md
@@ -78,6 +78,7 @@
 - [`LoadSpriteGFX` does not limit the capacity of `UsedSprites`](#loadspritegfx-does-not-limit-the-capacity-of-usedsprites)
 - [`ChooseWildEncounter` doesn't really validate the wild Pokémon species](#choosewildencounter-doesnt-really-validate-the-wild-pokémon-species)
 - [`TryObjectEvent` arbitrary code execution](#tryobjectevent-arbitrary-code-execution)
+- [`ReadObjectEvents` overflows into `wObjectMasks`](#readobjectevents-overflows-into-wobjectmasks)
 - [`ClearWRAM` only clears WRAM bank 1](#clearwram-only-clears-wram-bank-1)
 - [`BattleAnimCmd_ClearObjs` only clears the first 6⅔ objects](#battleanimcmd_clearobjs-only-clears-the-first-6-objects)
 
@@ -341,7 +342,7 @@
  	call GetBattleVarAddr
  	push af
  	set SUBSTATUS_CONFUSED, [hl]
-+	ld a, [hBattleTurn]
++	ldh a, [hBattleTurn]
 +	and a
 +	ld hl, wEnemyConfuseCount
 +	jr z, .set_confuse_count
@@ -349,7 +350,7 @@
 +.set_confuse_count
 +	call BattleRandom
 +	and %11
-+	add a, 2
++	add 2
 +	ld [hl], a
  	ld a, BATTLE_VARS_MOVE_ANIM
  	call GetBattleVarAddr
@@ -2142,6 +2143,39 @@
 -	; pop bc
  	xor a
  	ret
+```
+
+
+## `ReadObjectEvents` overflows into `wObjectMasks`
+
+**Fix:** Edit `ReadObjectEvents` in [home/map.asm](https://github.com/pret/pokecrystal/blob/master/home/map.asm):
+
+```diff
+-; get NUM_OBJECTS - [wCurMapObjectEventCount]
++; get NUM_OBJECTS - [wCurMapObjectEventCount] - 1
+ 	ld a, [wCurMapObjectEventCount]
+ 	ld c, a
+-	ld a, NUM_OBJECTS ; - 1
++	ld a, NUM_OBJECTS - 1
+ 	sub c
+ 	jr z, .skip
+-	; jr c, .skip
++	jr c, .skip
+ 
+ 	; could have done "inc hl" instead
+ 	ld bc, 1
+ 	add hl, bc
+-; Fill the remaining sprite IDs and y coords with 0 and -1, respectively.
+-; Bleeds into wObjectMasks due to a bug.  Uncomment the above code to fix.
+ 	ld bc, MAPOBJECT_LENGTH
+ .loop
+ 	ld [hl],  0
+ 	inc hl
+ 	ld [hl], -1
+ 	dec hl
+ 	add hl, bc
+ 	dec a
+ 	jr nz, .loop
 ```
 
 
--- a/docs/design_flaws.md
+++ b/docs/design_flaws.md
@@ -513,7 +513,124 @@
 	db BANK("Pokedex Entries 193-251")
 ```
 
-**Fix:** Use `dba` instead of `dw` in `PokedexDataPointerTable`. Then edit [home.asm](https://github.com/pret/pokecrystal/blob/master/home.asm) to contain a single copy of the `PokedexDataPointerTable` lookup code, updated to work with 3-byte `dba` entries and get the bank from the first entry byte. Delete the three separate lookup routines and use the new one (placed in [home.asm](https://github.com/pret/pokecrystal/blob/master/home.asm) so it can be called from any bank.)
+**Fix:**
+
+Use `dba` instead of `dw` in `PokedexDataPointerTable`.
+
+Delete `GetPokedexEntryBank` and `PokedexShow_GetDexEntryBank`. You can also delete `NUM_DEX_ENTRY_BANKS` from [constants/pokemon_data_constants.asm](https://github.com/pret/pokecrystal/blob/master/constants/pokemon_data_constants.asm).
+
+Edit [engine/pokedex/pokedex_2.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokedex/pokedex_2.asm):
+
+```diff
+ GetDexEntryPointer:
+ ; return dex entry pointer b:de
+ 	push hl
+ 	ld hl, PokedexDataPointerTable
+ 	ld a, b
+ 	dec a
+ 	ld d, 0
+ 	ld e, a
+ 	add hl, de
+ 	add hl, de
+-	ld e, [hl]
+-	inc hl
+-	ld d, [hl]
+-	push de
+-	rlca
+-	rlca
+-	maskbits NUM_DEX_ENTRY_BANKS
+-	ld hl, .PokedexEntryBanks
+-	ld d, 0
+-	ld e, a
+-	add hl, de
+-	ld b, [hl]
+-	pop de
++	add hl, de
++	; b = bank
++	ld a, [hli]
++	ld b, a
++	; de = address
++	ld a, [hli]
++	ld e, a
++	ld d, [hl]
+ 	pop hl
+ 	ret
+-
+-.PokedexEntryBanks:
+-	db BANK("Pokedex Entries 001-064")
+-	db BANK("Pokedex Entries 065-128")
+-	db BANK("Pokedex Entries 129-192")
+-	db BANK("Pokedex Entries 193-251")
+```
+
+Edit [engine/items/item_effects.asm](https://github.com/pret/pokecrystal/blob/master/engine/items/item_effects.asm):
+
+```diff
+ HeavyBallMultiplier:
+ ; subtract 20 from catch rate if weight < 102.4 kg
+ ; else add 0 to catch rate if weight < 204.8 kg
+ ; else add 20 to catch rate if weight < 307.2 kg
+ ; else add 30 to catch rate if weight < 409.6 kg
+ ; else add 40 to catch rate (never happens)
+ 	ld a, [wEnemyMonSpecies]
+ 	ld hl, PokedexDataPointerTable
+ 	dec a
+ 	ld e, a
+ 	ld d, 0
+ 	add hl, de
+ 	add hl, de
++	add hl, de
++	; d = bank, hl = address
++	ld a, BANK(PokedexDataPointerTable)
++	call GetFarByte
++	push af
++	inc hl
+ 	ld a, BANK(PokedexDataPointerTable)
+ 	call GetFarHalfword
++	pop de
+ 
+ .SkipText:
+-	call GetPokedexEntryBank
++	ld a, d
+ 	call GetFarByte
+ 	inc hl
+ 	cp "@"
+ 	jr nz, .SkipText
+ 
+-	call GetPokedexEntryBank
++	ld a, d
+ 	push bc
+ 	inc hl
+ 	inc hl
+ 	call GetFarHalfword
+```
+
+And edit [engine/pokegear/radio.asm](https://github.com/pret/pokecrystal/blob/master/engine/pokegear/radio.asm):
+
+```diff
+ PokedexShow2:
+ 	ld a, [wCurPartySpecies]
+ 	dec a
+ 	ld hl, PokedexDataPointerTable
+ 	ld c, a
+ 	ld b, 0
+ 	add hl, bc
+ 	add hl, bc
++	add hl, bc
++	b = bank
++	ld a, BANK(PokedexDataPointerTable)
++	call GetFarByte
++	ld b, a
++	inc hl
++	; hl = address
+ 	ld a, BANK(PokedexDataPointerTable)
+ 	call GetFarHalfword
+-	call PokedexShow_GetDexEntryBank
++	ld a, b
+ 	push af
+ 	push hl
+ 	call CopyDexEntryPart1
+```
 
 
 ## Identical sine wave code and data is repeated five times
--- a/engine/events/bug_contest/judging.asm
+++ b/engine/events/bug_contest/judging.asm
@@ -126,7 +126,7 @@
 
 BugContest_GetPlayersResult:
 	ld hl, wBugContestThirdPlaceWinnerID
-	ld de, - BUG_CONTESTANT_SIZE
+	ld de, -BUG_CONTESTANT_SIZE
 	ld b, 3 ; 3rd, 2nd, or 1st
 .loop
 	ld a, [hl]
--- a/engine/games/unown_puzzle.asm
+++ b/engine/games/unown_puzzle.asm
@@ -90,7 +90,7 @@
 	ret
 
 InitUnownPuzzlePiecePositions:
-	ld c,  1
+	ld c, 1
 	ld b, 16
 .load_loop
 	call Random
--- a/engine/gfx/sprites.asm
+++ b/engine/gfx/sprites.asm
@@ -318,7 +318,7 @@
 	ld hl, wCurSpriteOAMFlags
 	bit OAM_Y_FLIP, [hl]
 	jr z, .ok
-	; 8 - a
+	; -8 - a
 	add $8
 	xor $ff
 	inc a
@@ -333,7 +333,7 @@
 	ld hl, wCurSpriteOAMFlags
 	bit OAM_X_FLIP, [hl]
 	jr z, .ok
-	; 8 - a
+	; -8 - a
 	add $8
 	xor $ff
 	inc a
--- a/engine/overworld/map_objects.asm
+++ b/engine/overworld/map_objects.asm
@@ -2318,7 +2318,7 @@
 	jr c, .ok3
 	sub BG_MAP_WIDTH
 .ok3
-	ldh [hUsedSpriteIndex], a
+	ldh [hCurSpriteXCoord], a
 	ld a, [wPlayerBGMapOffsetY]
 	ld e, a
 	ld hl, OBJECT_SPRITE_Y_OFFSET
@@ -2347,7 +2347,7 @@
 	jr c, .ok6
 	sub BG_MAP_HEIGHT
 .ok6
-	ldh [hUsedSpriteTile], a
+	ldh [hCurSpriteYCoord], a
 	ld hl, OBJECT_PALETTE
 	add hl, bc
 	bit BIG_OBJECT_F, [hl]
@@ -2360,11 +2360,11 @@
 	ld e, a
 .ok7
 	ld a, d
-	ldh [hFFBF], a
+	ldh [hCurSpriteXPixel], a
 .loop
-	ldh a, [hFFBF]
+	ldh a, [hCurSpriteXPixel]
 	ld d, a
-	ldh a, [hUsedSpriteTile]
+	ldh a, [hCurSpriteYCoord]
 	add e
 	dec a
 	cp SCREEN_HEIGHT
@@ -2371,7 +2371,7 @@
 	jr nc, .ok9
 	ld b, a
 .next
-	ldh a, [hUsedSpriteIndex]
+	ldh a, [hCurSpriteXCoord]
 	add d
 	dec a
 	cp SCREEN_WIDTH
@@ -2856,7 +2856,7 @@
 	add hl, bc
 	ld a, [hl]
 	and $ff ^ (1 << 7)
-	ldh [hFFC1], a
+	ldh [hCurSpriteTile], a
 	xor a
 	bit 7, [hl]
 	jr nz, .skip1
@@ -2885,7 +2885,7 @@
 	jr z, .skip4
 	or PRIORITY
 .skip4
-	ldh [hFFC2], a
+	ldh [hCurSpriteOAMFlags], a
 	ld hl, OBJECT_SPRITE_X
 	add hl, bc
 	ld a, [hl]
@@ -2896,7 +2896,7 @@
 	ld e, a
 	ld a, [wPlayerBGMapOffsetX]
 	add e
-	ldh [hFFBF], a
+	ldh [hCurSpriteXPixel], a
 	ld hl, OBJECT_SPRITE_Y
 	add hl, bc
 	ld a, [hl]
@@ -2907,7 +2907,7 @@
 	ld e, a
 	ld a, [wPlayerBGMapOffsetY]
 	add e
-	ldh [hFFC0], a
+	ldh [hCurSpriteYPixel], a
 	ld hl, OBJECT_FACING_STEP
 	add hl, bc
 	ld a, [hl]
@@ -2932,12 +2932,12 @@
 	cp LOW(wVirtualOAMEnd)
 	jr nc, .full
 .addsprite
-	ldh a, [hFFC0]
+	ldh a, [hCurSpriteYPixel]
 	add [hl]
 	inc hl
 	ld [bc], a ; y
 	inc c
-	ldh a, [hFFBF]
+	ldh a, [hCurSpriteXPixel]
 	add [hl]
 	inc hl
 	ld [bc], a ; x
@@ -2944,7 +2944,7 @@
 	inc c
 	ld e, [hl]
 	inc hl
-	ldh a, [hFFC1]
+	ldh a, [hCurSpriteTile]
 	bit ABSOLUTE_TILE_ID_F, e
 	jr z, .nope1
 	xor a
@@ -2956,7 +2956,7 @@
 	ld a, e
 	bit RELATIVE_ATTRIBUTES_F, a
 	jr z, .nope2
-	ldh a, [hFFC2]
+	ldh a, [hCurSpriteOAMFlags]
 	or e
 .nope2
 	and OBP_NUM | X_FLIP | Y_FLIP | PRIORITY
--- a/home/map.asm
+++ b/home/map.asm
@@ -596,8 +596,7 @@
 	ld bc, 1
 	add hl, bc
 ; Fill the remaining sprite IDs and y coords with 0 and -1, respectively.
-; Bleeds into wObjectMasks due to a bug.  Uncomment the above subtraction
-; to fix.
+; Bleeds into wObjectMasks due to a bug.  Uncomment the above code to fix.
 	ld bc, MAPOBJECT_LENGTH
 .loop
 	ld [hl],  0
--- a/hram.asm
+++ b/hram.asm
@@ -95,12 +95,17 @@
 hMGStatusFlags:: db ; ffbc
 ENDU ; ffbd
 
+UNION
 hUsedSpriteIndex:: db ; ffbd
 hUsedSpriteTile::  db ; ffbe
-hFFBF::            db ; ffbf
-hFFC0::            db ; ffc0
-hFFC1::            db ; ffc1
-hFFC2::            db ; ffc2
+NEXTU
+hCurSpriteXCoord::   db ; ffbd
+hCurSpriteYCoord::   db ; ffbe
+hCurSpriteXPixel::   db ; ffbf
+hCurSpriteYPixel::   db ; ffc0
+hCurSpriteTile::     db ; ffc1
+hCurSpriteOAMFlags:: db ; ffc2
+ENDU
 
 UNION ; ffc3
 hMoneyTemp:: ds 3 ; ffc3