Advertisement
_PoY

Gen 4 - Why the AI items' choice is broken

Jan 28th, 2024 (edited)
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 KB | None | 0 0
  1. Gen 4 - Why the AI items' choice is broken
  2. ----------
  3.  
  4. Presentation :
  5. This document explains how the AI chooses items during fights in all Gen 4 Pokémon games (Diamond, Pearl, Platinum, HeartGold, SoulSilver). It originated from personal observations in Pokémon Diamond, was documented with the help of pret's dissassembly project `pokeplatinum`, and the logic was observed/refined by experienced runners/routers such as `Alw0` and `March` for Pokémon Platinum, and `Gimmy` and `decsy` for HeartGold/SoulSilver. The HeartGold/SoulSilver Manipless leaderboard runs on speedrun.com lead to pinpointing differences with Diamond/Pearl/Platinum that were raised by `decsy`.
  6.  
  7.  
  8. Summary : Item selection ...
  9. 1) ... is deterministic [all games].
  10. 2) ... is only affected by the trainer's list of items, the enemy and the number of Pokémon still alive in the trainer's party [all games].
  11. 3) ... can discard several items for a single item usage [only in Diamond/Pearl/Platinum].
  12. 4) ... of a healing item can occur when the enemy has high HP (for example, even above 50% HP left) [only observable in Diamond/Pearl].
  13.  
  14. Each point is detailled in its respective section below. Criteria for each item are provided at the very bottom of this document.
  15.  
  16.  
  17. ---
  18. Updates history :
  19.  
  20. January 29th, 2024.
  21. - More precise behaviour in HeartGold/SoulSilver.
  22. - Simplified wording.
  23.  
  24. First publication : January 28th, 2024.
  25.  
  26.  
  27. ---
  28. Sources :
  29. - pokeplatinum - `TrainerAI_ShouldUseItem` : https://github.com/pret/pokeplatinum/blob/0cc483b6c482da85291d93984bbce4577429a69c/src/overlay014/trainer_ai.c#L4164
  30. - own testings in Diamond (especially Mt. Coronet Cyrus, Volkner, Cynthia)
  31. - PSR community's observations (thanks again to `Alw0`, `March`, `Gimmy` and `decsy`)
  32. - HeartGold/SoulSilver leaderboard runs (only the ones used in this document, many more were examined) :
  33. • JTMagicman's : https://www.speedrun.com/pkmnhgssext/runs/y42eql3m
  34. • Visigo's : https://www.speedrun.com/pkmnhgssext/runs/yd142kqy
  35. • tluo5458's : https://www.speedrun.com/pkmnhgssext/runs/z1o400jm
  36. • frozenflygone's : https://www.speedrun.com/pkmnhgssext/runs/y2do605z
  37.  
  38.  
  39. ---
  40. Terminology used :
  41. - enemy = the Pokémon currently battling on the AI side, facing the player
  42. - trainer = the AI trainer
  43. - healing item = item that heals HP
  44.  
  45.  
  46. ---
  47. 1) Item selection is deterministic
  48.  
  49. The main routine that calculates whether a trainer should use an item is `TrainerAI_ShouldUseItem` (terminology as of Jan 28th, 2024 in the pokeplatinum repository, linked in the sources above).
  50.  
  51. The item selection does not feature any random call.
  52. However, it follows the `TrainerAI_ShouldSwitch` routine - which decides whether to switch enemy or not - which has some random calls if the conditions for a switch are met.
  53.  
  54. In short : aside from a switch, the item selection will always give the same result. This result depends on the variables described in the next section.
  55.  
  56.  
  57. ---
  58. 2) Item selection is only affected by the trainer's list of items, the enemy and the number of Pokémon still alive in the trainer's party.
  59.  
  60. Only three groups of variables matter for the choice of the item to be used. These are :
  61. - the trainer's list of items (its order matters), and which items have already been "used" ;
  62. - the current HP, the maximum HP and the status condition of the enemy ;
  63. - the number of Pokémon still alive in the trainer's party.
  64.  
  65. The general layout is the same across all games, but the details seem to vary between Diamond/Pearl/Platinum on the one hand, and HeartGold/SoulSilver on the other hand.
  66.  
  67.  
  68. 2.1) Diamond/Pearl/Platinum
  69.  
  70. The first item can be used on any enemy.
  71.  
  72. Any subsequent item can be used only if the number of enemy alive in the trainer's party is low enough. More precisely :
  73. - the two last Pokémon alive can use all items in the list.
  74. - the third-to-last Pokémon alive can use all items BUT the last one.
  75. - the fourth-to-last Pokémon alive can use all items BUT the last two ones.
  76. - the fifth-to-last and sixth-to last Pokémon alive can only use the first item.
  77. If any of the lists above is empty, the list of examined items consists of the sole first item.
  78.  
  79. Example : Cynthia has 4 items in total and 5 (or 6) Pokémon alive.
  80. => she will only check the first item in her list.
  81. Example : Cynthia has 4 items and 4 Pokémon alive.
  82. => she will check her first two items.
  83. Example : Cynthia has 4 items and 1 (or 2) Pokémon alive.
  84. => she will check all her items.
  85.  
  86. The layout for the item selection is as follows :
  87. (a) The game loops through this examined list in order, and for each item in it, checks whether or not to use the item.
  88.  
  89. (b) In this loop, once an examined item has met some criteria for being used, it is temporarily set to be the next item to be used.
  90.  
  91. (c) If any more items are examined afterwards in that loop, the last item in the examined list will be the one to be used REGARDLESS of its criteria being met. Furthermore, all the items in the examined list will be discarded, as if they were already used.
  92.  
  93. Precise examples are provided in sections 3 and 4.
  94.  
  95.  
  96. > Technical reason why the quirks happen in Diamond/Pearl/Platinum
  97.  
  98. Let :
  99. - `aliveMons` be the number of Pokémon still alive in the trainer's party.
  100. - `itemCount` be the total number of items in the trainer's list of items at the start of the fight.
  101.  
  102. In the `TrainerAI_ShouldUseItem` routine, the game loops through each item in order. Once an item has met its criteria for being used, the flag `result` is set to `TRUE`, enabling the used item to be set. However, the game proceeds with the loop and does not reset this flag. Thus it examines all the following items in the list (no further than index `itemCount - aliveMons + 1`, which corresponds to the cases explained at the start of this section). Each iteration overwrites the item to be used with the currently examined item and deletes the examined item from the trainer's list of items by setting its corresponding slot to 0 (i.e. `NO_ITEM`).
  103.  
  104.  
  105. 2.2) HeartGold/SoulSilver
  106.  
  107. The same general logic seems to apply to HGSS, apart from a few changes. Let's summarize :
  108. i. The first item can still be used on all Pokémon.
  109.  
  110. ii. The pool of examined items seems shifted : only the last alive Pokémon can have access to all items, the second-to-last alive Pokémon can have access to all items BUT the last one, etc. (all Pokémon still have access to the first item in the list, like in Diamond/Pearl/Platinum).
  111.  
  112. Example : Red's 6st Pokémon (i.e. the last Pokémon alive) can trigger all four items, his 5th Pokémon (i.e. the second-to-last Pokémon alive) can only trigger the first three items, his 4th only the first two items, and his 1st/2nd/3rd only the first item.
  113.  
  114. iii. The game doesn't discard several items per item usage anymore. As soon as an item's criteria are met, this item will be used by the trainer and only this item will be discarded from the items' list.
  115.  
  116.  
  117.  
  118. > Observations backing up these claims (all in Red fight) :
  119. i. In JTMagicman's run (https://www.speedrun.com/pkmnhgssext/runs/y42eql3m) : a Full Restore is used on Pikachu (sent first), then is put back into heal range. Red doesn't trigger a Full Restore.
  120. => Pokémon 1 can only be healed once.
  121.  
  122. ii. • In Visigo's run (https://www.speedrun.com/pkmnhgssext/runs/yd142kqy) : a Full Restore is used on Pikachu (sent first), then Venusaur (sent third) is put into heal range without Red using a Full Restore.
  123. => using an item on Pokémon 1 disables items on Pokémon 3. From JTMagicman's example above, and by interpolation, it also disables items on Pokémon 2.
  124.  
  125. • In tluo5458's run (https://www.speedrun.com/pkmnhgssext/runs/z1o400jm) : a Full Restore is used on Blastoise (sent second), then two more on Venusaur (sent fifth). Venusaur is put once more in heal range, yet Red doesn't trigger a Full Restore.
  126. => despite having four items, Red doesn't use his fourth item on Pokémon 5.
  127.  
  128.  
  129. iii. In frozenflygone's run (3rd fight, https://www.speedrun.com/pkmnhgssext/runs/y2do605z) : a Full Restore is used on Blastoise (sent second), one on Lapras (sent 4th), then two more on Venusaur (sent 6th).
  130. => If multiple items were discarded at once, Venusaur would only have one Full Restore available (which is proven wrong by this example).
  131.  
  132.  
  133. ---
  134. 3) [only in Diamond/Pearl/Platinum] Item selection can discard several items for a single item usage.
  135.  
  136. This is a consequence of points (b) and (c) of section 2.1. As soon as an item can be used, all the following ones will be discarded (up to the upper limit based on the total number of items and the number of Pokémon still alive). As an example, for all the following trainers, if an item is triggered on their SECOND-TO-LAST/LAST Pokémon, no more items will be available (even though only one item has been used in total) and the item effectively used will be the one provided between parenthesis :
  137.  
  138. > Diamond/Pearl :
  139. - all gym leaders : Roark (Potion)
  140. Gardenia (Super Potion)
  141. Maylene (Hyper Potion)
  142. Wake (Super Potion)
  143. Fantina (Hyper Potion)
  144. Byron (Full Restore)
  145. Candice (Full Restore)
  146. Volkner (Full Restore)
  147. - Veilstone Cyrus (Super Potion)
  148. - Mt. Coronet Cyrus (Full Restore)
  149. - all Elite Four members (Full Restore)
  150. - Cynthia (Full Restore)
  151.  
  152. > Platinum :
  153. - all gym leaders : Roark (Potion/Full Restore)
  154. except Maylene Gardenia (Super Potion/Full Restore)
  155. (rounds 1/2) Fantina (Super Potion/Full Restore)
  156. Wake (Hyper Potion/Full Restore)
  157. Byron (Full Restore)
  158. Candice (Full Restore)
  159. Volkner (Full Restore)
  160. - Veilstone & Distorsion World Cyrus (Full Restore)
  161. - all Elite Four members (Full Restore)
  162. - Cynthia (Full Restore)
  163. - Survival Area Barry (Full Restore)
  164.  
  165.  
  166. Another side effect occurs when the first item left in the list is always the one to trigger the item selection. This leads to only one item maximum to be used per Pokémon. This is exactly what happens when a trainer has a list of items where all are triggered by the same criteria (for instance, if all items are the same). All trainers from the list above are concerned.
  167.  
  168. Example : Roark has 3 Pokémon (Geodude/Onix/Cranidos in this order most of the times) and 2 Potions.
  169. - if a Potion is used on Geodude, then the other Potion can't be used on Geodude. However, the Potion can be used either on Onix or Cranidos.
  170. - if a Potion is used on Onix, the other Potion CAN'T be used on either Onix or Cranidos.
  171.  
  172.  
  173. ---
  174. 4) [only observable in Diamond/Pearl] Item selection of a healing item can occur when the enemy has high HP (for example, even above 50% HP left)
  175.  
  176. Another consequence of points (b) and (c) of section 2.1. Let's detail an existing use case : Diamond/Pearl Mt. Coronet Cyrus fight.
  177. His items are Super Potion and Full Restore in this order. Let's assume he has 1 or 2 Pokémon alive, thus he is checking all his items every turn, and Gyarados is on the field. Let's deal 51 damage to Gyarados, this is exactly enough to trigger the Super Potion criteria (more details below). As explained, the Super Potion will be temporarily set as the item to be used and will be discarded from the items to be used later. But the game will proceed in examining the second item, and set it as the one to be used instead, discarding it as well.
  178.  
  179. As a summary, Diamond/Pearl Mt. Coronet Cyrus fight works like this :
  180. - Only Super Potion on Pokemon 1 and 2, only Full Restore on Pokémon 3 and 4.
  181. - If Super Potion was used on Pokémon 1 or 2, Full Restore will trigger on Pokémon 3 and 4 when cur < (max/4).
  182. - If Super Potion is still available on Pokémon 3 or 4, Full Restore will trigger after 51 damage dealt (and Cyrus won't have any more healing items).
  183.  
  184. And that's how Cyrus can trigger a Full Restore despite Gyarados having 66% HP left (100/151).
  185.  
  186.  
  187. ---
  188. Bonus) Items' criteria
  189.  
  190. Partner AI can't use any items.
  191.  
  192. Don't use any items if the enemy is under Embargo.
  193.  
  194. For the following item categories, these are the conditions for triggering an item selection (cur = current enemy HP, max = max enemy HP, heal = the maximum amount of HP an item restores, all divisions are rounded down) :
  195. - Full Restore : cur < (max/4)
  196. - HP restoring items : cur < (max/4) or (max - cur) > heal
  197. - Status : statused
  198. - Battle item : enemy has been on the field for at least a turn
  199.  
  200. Note that the second condition for HP restoring items explains why a Super Potion isn't triggered if the enemy has undertaken exactly 50 damage (assuming the condition `cur < (max/4)` is not met).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement