jojobear13 / shinpokered

Mostly-vanilla hack of Pokémon Red/Blue focused on bugfixes and trainer ai
209 stars 42 forks source link

Bug: Trainers facing down have view capped to 3 tiles #273

Closed Mord5 closed 12 months ago

Mord5 commented 1 year ago

What emulator and/or hardware are you are using? bgb 1.5.9.w64

What version of this project are you using? built from source master_dev commit https://github.com/jojobear13/shinpokered/commit/43b63b6f6eb93674af1580741b3686096a868e10

What is the problem you encountered? Any trainer with a defined view range of 4 cells that faces downwards actually only has a view range of 3 cells. Can be seen with the southernmost Biker on Route 14:

image
Route14TrainerHeader6:
    dbEventFlagBit EVENT_BEAT_ROUTE_14_TRAINER_6
    db ($4 << 4) ; trainer's view range
    dwEventFlagAddress EVENT_BEAT_ROUTE_14_TRAINER_6
    dw Route14BattleText7 ; TextBeforeBattle
    dw Route14AfterBattleText7 ; TextAfterBattle
    dw Route14EndBattleText7 ; TextEndBattle
    dw Route14EndBattleText7 ; TextEndBattle
jojobear13 commented 1 year ago

Verified that this is a Gamefreak original.

image

jojobear13 commented 1 year ago

I tracked down the cause. There is a bug in TrainerEngage.linedUpX, let me explain.

Sprites are offset by 4 pixels towards the top of the screen for perspective.

Top of the player's hat is always Y=$3C pixels from the top of the screen.

Vertical distance is always calculated between the top of the player's head and the top of the NPC's head.

When the NPC is 4 spaces above the player, the top of its head is off of the screen. The top of the screen is Y=0, so this means the top of the NPC's head underflows by 4 pixels to Y=$FC.

This results in the distance being calculated wrong: $3C - $FC = $C0 instead of the expected $40.

capture 1 capture 2

This bug affects TrainerWalkUpToPlayer.facingDown as well.

See 78368126 for the fix. Easiest solution is to undo the 4-pixel offset under conditions that are affected by the underflow.