From a7e31dd87fbf41bae4a5e8d082e7e410bc19f826 Mon Sep 17 00:00:00 2001 From: Idain Date: Thu, 10 Aug 2023 13:29:03 -0400 Subject: [PATCH] Document fix about AI item pointer overflowing (#1066) --- docs/bugs_and_glitches.md | 28 ++++++++++++++++++++++++++++ engine/battle/ai/items.asm | 1 + 2 files changed, 29 insertions(+) diff --git a/docs/bugs_and_glitches.md b/docs/bugs_and_glitches.md index 6ccc1ffe915..b14568bdba8 100644 --- a/docs/bugs_and_glitches.md +++ b/docs/bugs_and_glitches.md @@ -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 value as an item](#ai-might-use-its-base-reward-value-as-an-item) - [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) @@ -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 value as an item + +In the `AI_TryItem` routine, an item pointer is set to `wEnemyTrainerItem1` and then increments to `wEnemyTrainerItem2` to see if either of the AI's items are in the `AI_Items` list. However, if the AI has used its first item (or its first one is `ITEM_NONE`) and hasn't used its second item, the item pointer can increment from `wEnemyTrainerItem2` to `wEnemyTrainerBaseReward`. If the value at this address then matches an item in the `AI_Items` list, the AI could mistakenly use it. + +**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 value as an item (see docs/bugs_and_glitches.md) +- 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 diff --git a/engine/battle/ai/items.asm b/engine/battle/ai/items.asm index 7e5c5ef5912..3c0adb5f281 100644 --- a/engine/battle/ai/items.asm +++ b/engine/battle/ai/items.asm @@ -175,6 +175,7 @@ AI_TryItem: ld b, h ld c, l ld hl, AI_Items +; BUG: AI might use its base reward value as an item (see docs/bugs_and_glitches.md) ld de, wEnemyTrainerItem1 .loop ld a, [hl]