Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document fix about AI item pointer overflowing #1066

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/bugs_and_glitches.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Fixes in the [multi-player battle engine](#multi-player-battle-engine) category
- [AI makes a false assumption about `CheckTypeMatchup`](#ai-makes-a-false-assumption-about-checktypematchup)
- [AI use of Full Heal or Full Restore does not cure Nightmare status](#ai-use-of-full-heal-or-full-restore-does-not-cure-nightmare-status)
- [AI use of Full Heal does not cure confusion status](#ai-use-of-full-heal-does-not-cure-confusion-status)
- [AI might use its base reward's value as an item](#ai-might-use-its-base-rewards-value-as-an-item)
Rangi42 marked this conversation as resolved.
Show resolved Hide resolved
- [Wild Pokémon can always Teleport regardless of level difference](#wild-pok%C3%A9mon-can-always-teleport-regardless-of-level-difference)
- [`RIVAL2` has lower DVs than `RIVAL1`](#rival2-has-lower-dvs-than-rival1)
- [`HELD_CATCH_CHANCE` has no effect](#held_catch_chance-has-no-effect)
Expand Down Expand Up @@ -1455,6 +1456,33 @@ Pryce's dialog ("That BADGE will raise the SPECIAL stats of POKéMON.") implies
ret
```

### AI might use its base reward's value as an item
Rangi42 marked this conversation as resolved.
Show resolved Hide resolved

In the `AI_TryItem` routine, the code checks both `wEnemyTrainerItem1` and `wEnemyTrainerItem2` to see if the AI's items match any in the `AI_Items` list. However, if the AI has used its first item (or it's set to `ITEM_NONE`) and doesn't use its second item, the item pointer can overflow to the subsequent address, `wEnemyTrainerBaseReward`. If this address's value matches an item in the `AI_Items` list, the AI could mistakenly use it.
Rangi42 marked this conversation as resolved.
Show resolved Hide resolved

**Fix:** Edit `AI_TryItem` in [engine/battle/ai/items.asm](https://github.com/pret/pokecrystal/blob/master/engine/battle/ai/items.asm):

```diff
AI_TryItem:
...
ld a, [wTrainerClass]
dec a
ld hl, TrainerClassAttributes + TRNATTR_AI_ITEM_SWITCH
ld bc, NUM_TRAINER_ATTRIBUTES
call AddNTimes
ld b, h
ld c, l
ld hl, AI_Items
-; BUG: AI might use its base reward's value as an item (see docs/bugs_and_glitches.md)
Rangi42 marked this conversation as resolved.
Show resolved Hide resolved
- ld de, wEnemyTrainerItem1
.loop
+ ld de, wEnemyTrainerItem1
ld a, [hl]
and a
inc a
ret z
```


### Wild Pokémon can always Teleport regardless of level difference

Expand Down
1 change: 1 addition & 0 deletions engine/battle/ai/items.asm
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ AI_TryItem:
ld b, h
ld c, l
ld hl, AI_Items
; BUG: AI might use its base reward's value as an item (see docs/bugs_and_glitches.md)
Rangi42 marked this conversation as resolved.
Show resolved Hide resolved
ld de, wEnemyTrainerItem1
.loop
ld a, [hl]
Expand Down