trainer_ai.asm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. ; creates a set of moves that may be used and returns its address in hl
  2. ; unused slots are filled with 0, all used slots may be chosen with equal probability
  3. AIEnemyTrainerChooseMoves:
  4. ld a, $a
  5. ld hl, wBuffer ; init temporary move selection array. Only the moves with the lowest numbers are chosen in the end
  6. ld [hli], a ; move 1
  7. ld [hli], a ; move 2
  8. ld [hli], a ; move 3
  9. ld [hl], a ; move 4
  10. ld a, [wEnemyDisabledMove] ; forbid disabled move (if any)
  11. swap a
  12. and $f
  13. jr z, .noMoveDisabled
  14. ld hl, wBuffer
  15. dec a
  16. ld c, a
  17. ld b, $0
  18. add hl, bc ; advance pointer to forbidden move
  19. ld [hl], $50 ; forbid (highly discourage) disabled move
  20. .noMoveDisabled
  21. ld hl, TrainerClassMoveChoiceModifications
  22. ld a, [wTrainerClass]
  23. ld b, a
  24. .loopTrainerClasses
  25. dec b
  26. jr z, .readTrainerClassData
  27. .loopTrainerClassData
  28. ld a, [hli]
  29. and a
  30. jr nz, .loopTrainerClassData
  31. jr .loopTrainerClasses
  32. .readTrainerClassData
  33. ld a, [hl]
  34. and a
  35. jp z, .useOriginalMoveSet
  36. push hl
  37. .nextMoveChoiceModification
  38. pop hl
  39. ld a, [hli]
  40. and a
  41. jr z, .loopFindMinimumEntries
  42. push hl
  43. ld hl, AIMoveChoiceModificationFunctionPointers
  44. dec a
  45. add a
  46. ld c, a
  47. ld b, 0
  48. add hl, bc ; skip to pointer
  49. ld a, [hli] ; read pointer into hl
  50. ld h, [hl]
  51. ld l, a
  52. ld de, .nextMoveChoiceModification ; set return address
  53. push de
  54. jp hl ; execute modification function
  55. .loopFindMinimumEntries ; all entries will be decremented sequentially until one of them is zero
  56. ld hl, wBuffer ; temp move selection array
  57. ld de, wEnemyMonMoves ; enemy moves
  58. ld c, NUM_MOVES
  59. .loopDecrementEntries
  60. ld a, [de]
  61. inc de
  62. and a
  63. jr z, .loopFindMinimumEntries
  64. dec [hl]
  65. jr z, .minimumEntriesFound
  66. inc hl
  67. dec c
  68. jr z, .loopFindMinimumEntries
  69. jr .loopDecrementEntries
  70. .minimumEntriesFound
  71. ld a, c
  72. .loopUndoPartialIteration ; undo last (partial) loop iteration
  73. inc [hl]
  74. dec hl
  75. inc a
  76. cp NUM_MOVES + 1
  77. jr nz, .loopUndoPartialIteration
  78. ld hl, wBuffer ; temp move selection array
  79. ld de, wEnemyMonMoves ; enemy moves
  80. ld c, NUM_MOVES
  81. .filterMinimalEntries ; all minimal entries now have value 1. All other slots will be disabled (move set to 0)
  82. ld a, [de]
  83. and a
  84. jr nz, .moveExisting
  85. ld [hl], a
  86. .moveExisting
  87. ld a, [hl]
  88. dec a
  89. jr z, .slotWithMinimalValue
  90. xor a
  91. ld [hli], a ; disable move slot
  92. jr .next
  93. .slotWithMinimalValue
  94. ld a, [de]
  95. ld [hli], a ; enable move slot
  96. .next
  97. inc de
  98. dec c
  99. jr nz, .filterMinimalEntries
  100. ld hl, wBuffer ; use created temporary array as move set
  101. ret
  102. .useOriginalMoveSet
  103. ld hl, wEnemyMonMoves ; use original move set
  104. ret
  105. AIMoveChoiceModificationFunctionPointers:
  106. dw AIMoveChoiceModification1
  107. dw AIMoveChoiceModification2
  108. dw AIMoveChoiceModification3
  109. dw AIMoveChoiceModification4 ; unused, does nothing
  110. ; discourages moves that cause no damage but only a status ailment if player's mon already has one
  111. AIMoveChoiceModification1:
  112. ld a, [wBattleMonStatus]
  113. and a
  114. ret z ; return if no status ailment on player's mon
  115. ld hl, wBuffer - 1 ; temp move selection array (-1 byte offset)
  116. ld de, wEnemyMonMoves ; enemy moves
  117. ld b, NUM_MOVES + 1
  118. .nextMove
  119. dec b
  120. ret z ; processed all 4 moves
  121. inc hl
  122. ld a, [de]
  123. and a
  124. ret z ; no more moves in move set
  125. inc de
  126. call ReadMove
  127. ld a, [wEnemyMovePower]
  128. and a
  129. jr nz, .nextMove
  130. ld a, [wEnemyMoveEffect]
  131. push hl
  132. push de
  133. push bc
  134. ld hl, StatusAilmentMoveEffects
  135. ld de, $0001
  136. call IsInArray
  137. pop bc
  138. pop de
  139. pop hl
  140. jr nc, .nextMove
  141. ld a, [hl]
  142. add $5 ; heavily discourage move
  143. ld [hl], a
  144. jr .nextMove
  145. StatusAilmentMoveEffects:
  146. db $01 ; unused sleep effect
  147. db SLEEP_EFFECT
  148. db POISON_EFFECT
  149. db PARALYZE_EFFECT
  150. db $FF
  151. ; slightly encourage moves with specific effects.
  152. ; in particular, stat-modifying moves and other move effects
  153. ; that fall in-between
  154. AIMoveChoiceModification2:
  155. ld a, [wAILayer2Encouragement]
  156. cp $1
  157. ret nz
  158. ld hl, wBuffer - 1 ; temp move selection array (-1 byte offset)
  159. ld de, wEnemyMonMoves ; enemy moves
  160. ld b, NUM_MOVES + 1
  161. .nextMove
  162. dec b
  163. ret z ; processed all 4 moves
  164. inc hl
  165. ld a, [de]
  166. and a
  167. ret z ; no more moves in move set
  168. inc de
  169. call ReadMove
  170. ld a, [wEnemyMoveEffect]
  171. cp ATTACK_UP1_EFFECT
  172. jr c, .nextMove
  173. cp BIDE_EFFECT
  174. jr c, .preferMove
  175. cp ATTACK_UP2_EFFECT
  176. jr c, .nextMove
  177. cp POISON_EFFECT
  178. jr c, .preferMove
  179. jr .nextMove
  180. .preferMove
  181. dec [hl] ; slightly encourage this move
  182. jr .nextMove
  183. ; encourages moves that are effective against the player's mon (even if non-damaging).
  184. ; discourage damaging moves that are ineffective or not very effective against the player's mon,
  185. ; unless there's no damaging move that deals at least neutral damage
  186. AIMoveChoiceModification3:
  187. ld hl, wBuffer - 1 ; temp move selection array (-1 byte offset)
  188. ld de, wEnemyMonMoves ; enemy moves
  189. ld b, NUM_MOVES + 1
  190. .nextMove
  191. dec b
  192. ret z ; processed all 4 moves
  193. inc hl
  194. ld a, [de]
  195. and a
  196. ret z ; no more moves in move set
  197. inc de
  198. call ReadMove
  199. push hl
  200. push bc
  201. push de
  202. callab AIGetTypeEffectiveness
  203. pop de
  204. pop bc
  205. pop hl
  206. ld a, [wTypeEffectiveness]
  207. cp $10
  208. jr z, .nextMove
  209. jr c, .notEffectiveMove
  210. dec [hl] ; slightly encourage this move
  211. jr .nextMove
  212. .notEffectiveMove ; discourages non-effective moves if better moves are available
  213. push hl
  214. push de
  215. push bc
  216. ld a, [wEnemyMoveType]
  217. ld d, a
  218. ld hl, wEnemyMonMoves ; enemy moves
  219. ld b, NUM_MOVES + 1
  220. ld c, $0
  221. .loopMoves
  222. dec b
  223. jr z, .done
  224. ld a, [hli]
  225. and a
  226. jr z, .done
  227. call ReadMove
  228. ld a, [wEnemyMoveEffect]
  229. cp SUPER_FANG_EFFECT
  230. jr z, .betterMoveFound ; Super Fang is considered to be a better move
  231. cp SPECIAL_DAMAGE_EFFECT
  232. jr z, .betterMoveFound ; any special damage moves are considered to be better moves
  233. cp FLY_EFFECT
  234. jr z, .betterMoveFound ; Fly is considered to be a better move
  235. ld a, [wEnemyMoveType]
  236. cp d
  237. jr z, .loopMoves
  238. ld a, [wEnemyMovePower]
  239. and a
  240. jr nz, .betterMoveFound ; damaging moves of a different type are considered to be better moves
  241. jr .loopMoves
  242. .betterMoveFound
  243. ld c, a
  244. .done
  245. ld a, c
  246. pop bc
  247. pop de
  248. pop hl
  249. and a
  250. jr z, .nextMove
  251. inc [hl] ; slightly discourage this move
  252. jr .nextMove
  253. AIMoveChoiceModification4:
  254. ret
  255. ReadMove:
  256. push hl
  257. push de
  258. push bc
  259. dec a
  260. ld hl, Moves
  261. ld bc, MoveEnd - Moves
  262. call AddNTimes
  263. ld de, wEnemyMoveNum
  264. call CopyData
  265. pop bc
  266. pop de
  267. pop hl
  268. ret
  269. ; move choice modification methods that are applied for each trainer class
  270. ; 0 is sentinel value
  271. TrainerClassMoveChoiceModifications:
  272. db 0 ; YOUNGSTER
  273. db 1,0 ; BUG CATCHER
  274. db 1,0 ; LASS
  275. db 1,3,0 ; SAILOR
  276. db 1,0 ; JR_TRAINER_M
  277. db 1,0 ; JR_TRAINER_F
  278. db 1,2,3,0; POKEMANIAC
  279. db 1,2,0 ; SUPER_NERD
  280. db 1,0 ; HIKER
  281. db 1,0 ; BIKER
  282. db 1,3,0 ; BURGLAR
  283. db 1,0 ; ENGINEER
  284. db 1,2,0 ; JUGGLER_X
  285. db 1,3,0 ; FISHER
  286. db 1,3,0 ; SWIMMER
  287. db 0 ; CUE_BALL
  288. db 1,0 ; GAMBLER
  289. db 1,3,0 ; BEAUTY
  290. db 1,2,0 ; PSYCHIC_TR
  291. db 1,3,0 ; ROCKER
  292. db 1,0 ; JUGGLER
  293. db 1,0 ; TAMER
  294. db 1,0 ; BIRD_KEEPER
  295. db 1,0 ; BLACKBELT
  296. db 1,0 ; SONY1
  297. db 1,3,0 ; PROF_OAK
  298. db 1,2,0 ; CHIEF
  299. db 1,2,0 ; SCIENTIST
  300. db 1,3,0 ; GIOVANNI
  301. db 1,0 ; ROCKET
  302. db 1,3,0 ; COOLTRAINER_M
  303. db 1,3,0 ; COOLTRAINER_F
  304. db 1,0 ; BRUNO
  305. db 1,0 ; BROCK
  306. db 1,3,0 ; MISTY
  307. db 1,3,0 ; LT_SURGE
  308. db 1,3,0 ; ERIKA
  309. db 1,3,0 ; KOGA
  310. db 1,3,0 ; BLAINE
  311. db 1,3,0 ; SABRINA
  312. db 1,2,0 ; GENTLEMAN
  313. db 1,3,0 ; SONY2
  314. db 1,3,0 ; SONY3
  315. db 1,2,3,0; LORELEI
  316. db 1,0 ; CHANNELER
  317. db 1,0 ; AGATHA
  318. db 1,3,0 ; LANCE
  319. INCLUDE "engine/battle/trainer_pic_money_pointers.asm"
  320. INCLUDE "text/trainer_names.asm"
  321. INCLUDE "engine/battle/bank_e_misc.asm"
  322. INCLUDE "engine/battle/read_trainer_party.asm"
  323. INCLUDE "data/trainer_moves.asm"
  324. INCLUDE "data/trainer_parties.asm"
  325. TrainerAI:
  326. and a
  327. ld a, [wIsInBattle]
  328. dec a
  329. ret z ; if not a trainer, we're done here
  330. ld a, [wLinkState]
  331. cp LINK_STATE_BATTLING
  332. ret z
  333. ld a, [wTrainerClass] ; what trainer class is this?
  334. dec a
  335. ld c, a
  336. ld b, 0
  337. ld hl, TrainerAIPointers
  338. add hl, bc
  339. add hl, bc
  340. add hl, bc
  341. ld a, [wAICount]
  342. and a
  343. ret z ; if no AI uses left, we're done here
  344. inc hl
  345. inc a
  346. jr nz, .getpointer
  347. dec hl
  348. ld a, [hli]
  349. ld [wAICount], a
  350. .getpointer
  351. ld a, [hli]
  352. ld h, [hl]
  353. ld l, a
  354. call Random
  355. jp hl
  356. TrainerAIPointers:
  357. ; one entry per trainer class
  358. ; first byte, number of times (per Pokémon) it can occur
  359. ; next two bytes, pointer to AI subroutine for trainer class
  360. dbw 3,GenericAI
  361. dbw 3,GenericAI
  362. dbw 3,GenericAI
  363. dbw 3,GenericAI
  364. dbw 3,GenericAI
  365. dbw 3,GenericAI
  366. dbw 3,GenericAI
  367. dbw 3,GenericAI
  368. dbw 3,GenericAI
  369. dbw 3,GenericAI
  370. dbw 3,GenericAI
  371. dbw 3,GenericAI
  372. dbw 3,JugglerAI ; juggler_x
  373. dbw 3,GenericAI
  374. dbw 3,GenericAI
  375. dbw 3,GenericAI
  376. dbw 3,GenericAI
  377. dbw 3,GenericAI
  378. dbw 3,GenericAI
  379. dbw 3,GenericAI
  380. dbw 3,JugglerAI ; juggler
  381. dbw 3,GenericAI
  382. dbw 3,GenericAI
  383. dbw 2,BlackbeltAI ; blackbelt
  384. dbw 3,GenericAI
  385. dbw 3,GenericAI
  386. dbw 1,GenericAI ; chief
  387. dbw 3,GenericAI
  388. dbw 1,GiovanniAI ; giovanni
  389. dbw 3,GenericAI
  390. dbw 2,CooltrainerMAI ; cooltrainerm
  391. dbw 1,CooltrainerFAI ; cooltrainerf
  392. dbw 2,BrunoAI ; bruno
  393. dbw 5,BrockAI ; brock
  394. dbw 1,MistyAI ; misty
  395. dbw 1,LtSurgeAI ; surge
  396. dbw 1,ErikaAI ; erika
  397. dbw 2,KogaAI ; koga
  398. dbw 2,BlaineAI ; blaine
  399. dbw 1,SabrinaAI ; sabrina
  400. dbw 3,GenericAI
  401. dbw 1,Sony2AI ; sony2
  402. dbw 1,Sony3AI ; sony3
  403. dbw 2,LoreleiAI ; lorelei
  404. dbw 3,GenericAI
  405. dbw 2,AgathaAI ; agatha
  406. dbw 1,LanceAI ; lance
  407. JugglerAI:
  408. cp $40
  409. ret nc
  410. jp AISwitchIfEnoughMons
  411. BlackbeltAI:
  412. cp $20
  413. ret nc
  414. jp AIUseXAttack
  415. GiovanniAI:
  416. cp $40
  417. ret nc
  418. jp AIUseGuardSpec
  419. CooltrainerMAI:
  420. cp $40
  421. ret nc
  422. jp AIUseXAttack
  423. CooltrainerFAI:
  424. cp $40
  425. ld a, $A
  426. call AICheckIfHPBelowFraction
  427. jp c, AIUseHyperPotion
  428. ld a, 5
  429. call AICheckIfHPBelowFraction
  430. ret nc
  431. jp AISwitchIfEnoughMons
  432. BrockAI:
  433. ; if his active monster has a status condition, use a full heal
  434. ld a, [wEnemyMonStatus]
  435. and a
  436. ret z
  437. jp AIUseFullHeal
  438. MistyAI:
  439. cp $40
  440. ret nc
  441. jp AIUseXDefend
  442. LtSurgeAI:
  443. cp $40
  444. ret nc
  445. jp AIUseXSpeed
  446. ErikaAI:
  447. cp $80
  448. ret nc
  449. ld a, $A
  450. call AICheckIfHPBelowFraction
  451. ret nc
  452. jp AIUseSuperPotion
  453. KogaAI:
  454. cp $40
  455. ret nc
  456. jp AIUseXAttack
  457. BlaineAI:
  458. cp $40
  459. ret nc
  460. jp AIUseSuperPotion
  461. SabrinaAI:
  462. cp $40
  463. ret nc
  464. ld a, $A
  465. call AICheckIfHPBelowFraction
  466. ret nc
  467. jp AIUseHyperPotion
  468. Sony2AI:
  469. cp $20
  470. ret nc
  471. ld a, 5
  472. call AICheckIfHPBelowFraction
  473. ret nc
  474. jp AIUsePotion
  475. Sony3AI:
  476. cp $20
  477. ret nc
  478. ld a, 5
  479. call AICheckIfHPBelowFraction
  480. ret nc
  481. jp AIUseFullRestore
  482. LoreleiAI:
  483. cp $80
  484. ret nc
  485. ld a, 5
  486. call AICheckIfHPBelowFraction
  487. ret nc
  488. jp AIUseSuperPotion
  489. BrunoAI:
  490. cp $40
  491. ret nc
  492. jp AIUseXDefend
  493. AgathaAI:
  494. cp $14
  495. jp c, AISwitchIfEnoughMons
  496. cp $80
  497. ret nc
  498. ld a, 4
  499. call AICheckIfHPBelowFraction
  500. ret nc
  501. jp AIUseSuperPotion
  502. LanceAI:
  503. cp $80
  504. ret nc
  505. ld a, 5
  506. call AICheckIfHPBelowFraction
  507. ret nc
  508. jp AIUseHyperPotion
  509. GenericAI:
  510. and a ; clear carry
  511. ret
  512. ; end of individual trainer AI routines
  513. DecrementAICount:
  514. ld hl, wAICount
  515. dec [hl]
  516. scf
  517. ret
  518. AIPlayRestoringSFX:
  519. ld a, SFX_HEAL_AILMENT
  520. jp PlaySoundWaitForCurrent
  521. AIUseFullRestore:
  522. call AICureStatus
  523. ld a, FULL_RESTORE
  524. ld [wAIItem], a
  525. ld de, wHPBarOldHP
  526. ld hl, wEnemyMonHP + 1
  527. ld a, [hld]
  528. ld [de], a
  529. inc de
  530. ld a, [hl]
  531. ld [de], a
  532. inc de
  533. ld hl, wEnemyMonMaxHP + 1
  534. ld a, [hld]
  535. ld [de], a
  536. inc de
  537. ld [wHPBarMaxHP], a
  538. ld [wEnemyMonHP + 1], a
  539. ld a, [hl]
  540. ld [de], a
  541. ld [wHPBarMaxHP+1], a
  542. ld [wEnemyMonHP], a
  543. jr AIPrintItemUseAndUpdateHPBar
  544. AIUsePotion:
  545. ; enemy trainer heals his monster with a potion
  546. ld a, POTION
  547. ld b, 20
  548. jr AIRecoverHP
  549. AIUseSuperPotion:
  550. ; enemy trainer heals his monster with a super potion
  551. ld a, SUPER_POTION
  552. ld b, 50
  553. jr AIRecoverHP
  554. AIUseHyperPotion:
  555. ; enemy trainer heals his monster with a hyper potion
  556. ld a, HYPER_POTION
  557. ld b, 200
  558. ; fallthrough
  559. AIRecoverHP:
  560. ; heal b HP and print "trainer used $(a) on pokemon!"
  561. ld [wAIItem], a
  562. ld hl, wEnemyMonHP + 1
  563. ld a, [hl]
  564. ld [wHPBarOldHP], a
  565. add b
  566. ld [hld], a
  567. ld [wHPBarNewHP], a
  568. ld a, [hl]
  569. ld [wHPBarOldHP+1], a
  570. ld [wHPBarNewHP+1], a
  571. jr nc, .next
  572. inc a
  573. ld [hl], a
  574. ld [wHPBarNewHP+1], a
  575. .next
  576. inc hl
  577. ld a, [hld]
  578. ld b, a
  579. ld de, wEnemyMonMaxHP + 1
  580. ld a, [de]
  581. dec de
  582. ld [wHPBarMaxHP], a
  583. sub b
  584. ld a, [hli]
  585. ld b, a
  586. ld a, [de]
  587. ld [wHPBarMaxHP+1], a
  588. sbc b
  589. jr nc, AIPrintItemUseAndUpdateHPBar
  590. inc de
  591. ld a, [de]
  592. dec de
  593. ld [hld], a
  594. ld [wHPBarNewHP], a
  595. ld a, [de]
  596. ld [hl], a
  597. ld [wHPBarNewHP+1], a
  598. ; fallthrough
  599. AIPrintItemUseAndUpdateHPBar:
  600. call AIPrintItemUse_
  601. coord hl, 2, 2
  602. xor a
  603. ld [wHPBarType], a
  604. predef UpdateHPBar2
  605. jp DecrementAICount
  606. AISwitchIfEnoughMons:
  607. ; enemy trainer switches if there are 3 or more unfainted mons in party
  608. ld a, [wEnemyPartyCount]
  609. ld c, a
  610. ld hl, wEnemyMon1HP
  611. ld d, 0 ; keep count of unfainted monsters
  612. ; count how many monsters haven't fainted yet
  613. .loop
  614. ld a, [hli]
  615. ld b, a
  616. ld a, [hld]
  617. or b
  618. jr z, .Fainted ; has monster fainted?
  619. inc d
  620. .Fainted
  621. push bc
  622. ld bc, wEnemyMon2 - wEnemyMon1
  623. add hl, bc
  624. pop bc
  625. dec c
  626. jr nz, .loop
  627. ld a, d ; how many available monsters are there?
  628. cp 2 ; don't bother if only 1 or 2
  629. jp nc, SwitchEnemyMon
  630. and a
  631. ret
  632. SwitchEnemyMon:
  633. ; prepare to withdraw the active monster: copy hp, number, and status to roster
  634. ld a, [wEnemyMonPartyPos]
  635. ld hl, wEnemyMon1HP
  636. ld bc, wEnemyMon2 - wEnemyMon1
  637. call AddNTimes
  638. ld d, h
  639. ld e, l
  640. ld hl, wEnemyMonHP
  641. ld bc, 4
  642. call CopyData
  643. ld hl, AIBattleWithdrawText
  644. call PrintText
  645. ; This wFirstMonsNotOutYet variable is abused to prevent the player from
  646. ; switching in a new mon in response to this switch.
  647. ld a, 1
  648. ld [wFirstMonsNotOutYet], a
  649. callab EnemySendOut
  650. xor a
  651. ld [wFirstMonsNotOutYet], a
  652. ld a, [wLinkState]
  653. cp LINK_STATE_BATTLING
  654. ret z
  655. scf
  656. ret
  657. AIBattleWithdrawText:
  658. TX_FAR _AIBattleWithdrawText
  659. db "@"
  660. AIUseFullHeal:
  661. call AIPlayRestoringSFX
  662. call AICureStatus
  663. ld a, FULL_HEAL
  664. jp AIPrintItemUse
  665. AICureStatus:
  666. ; cures the status of enemy's active pokemon
  667. ld a, [wEnemyMonPartyPos]
  668. ld hl, wEnemyMon1Status
  669. ld bc, wEnemyMon2 - wEnemyMon1
  670. call AddNTimes
  671. xor a
  672. ld [hl], a ; clear status in enemy team roster
  673. ld [wEnemyMonStatus], a ; clear status of active enemy
  674. ld hl, wEnemyBattleStatus3
  675. res 0, [hl]
  676. ret
  677. AIUseXAccuracy: ; unused
  678. call AIPlayRestoringSFX
  679. ld hl, wEnemyBattleStatus2
  680. set 0, [hl]
  681. ld a, X_ACCURACY
  682. jp AIPrintItemUse
  683. AIUseGuardSpec:
  684. call AIPlayRestoringSFX
  685. ld hl, wEnemyBattleStatus2
  686. set 1, [hl]
  687. ld a, GUARD_SPEC
  688. jp AIPrintItemUse
  689. AIUseDireHit: ; unused
  690. call AIPlayRestoringSFX
  691. ld hl, wEnemyBattleStatus2
  692. set 2, [hl]
  693. ld a, DIRE_HIT
  694. jp AIPrintItemUse
  695. AICheckIfHPBelowFraction:
  696. ; return carry if enemy trainer's current HP is below 1 / a of the maximum
  697. ld [H_DIVISOR], a
  698. ld hl, wEnemyMonMaxHP
  699. ld a, [hli]
  700. ld [H_DIVIDEND], a
  701. ld a, [hl]
  702. ld [H_DIVIDEND + 1], a
  703. ld b, 2
  704. call Divide
  705. ld a, [H_QUOTIENT + 3]
  706. ld c, a
  707. ld a, [H_QUOTIENT + 2]
  708. ld b, a
  709. ld hl, wEnemyMonHP + 1
  710. ld a, [hld]
  711. ld e, a
  712. ld a, [hl]
  713. ld d, a
  714. ld a, d
  715. sub b
  716. ret nz
  717. ld a, e
  718. sub c
  719. ret
  720. AIUseXAttack:
  721. ld b, $A
  722. ld a, X_ATTACK
  723. jr AIIncreaseStat
  724. AIUseXDefend:
  725. ld b, $B
  726. ld a, X_DEFEND
  727. jr AIIncreaseStat
  728. AIUseXSpeed:
  729. ld b, $C
  730. ld a, X_SPEED
  731. jr AIIncreaseStat
  732. AIUseXSpecial:
  733. ld b, $D
  734. ld a, X_SPECIAL
  735. ; fallthrough
  736. AIIncreaseStat:
  737. ld [wAIItem], a
  738. push bc
  739. call AIPrintItemUse_
  740. pop bc
  741. ld hl, wEnemyMoveEffect
  742. ld a, [hld]
  743. push af
  744. ld a, [hl]
  745. push af
  746. push hl
  747. ld a, ANIM_AF
  748. ld [hli], a
  749. ld [hl], b
  750. callab StatModifierUpEffect
  751. pop hl
  752. pop af
  753. ld [hli], a
  754. pop af
  755. ld [hl], a
  756. jp DecrementAICount
  757. AIPrintItemUse:
  758. ld [wAIItem], a
  759. call AIPrintItemUse_
  760. jp DecrementAICount
  761. AIPrintItemUse_:
  762. ; print "x used [wAIItem] on z!"
  763. ld a, [wAIItem]
  764. ld [wd11e], a
  765. call GetItemName
  766. ld hl, AIBattleUseItemText
  767. jp PrintText
  768. AIBattleUseItemText:
  769. TX_FAR _AIBattleUseItemText
  770. db "@"