Advertisement
KDLPro

Switch Code v2.0

Mar 17th, 2021
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.18 KB | None | 0 0
  1. CheckPlayerMoveTypeMatchups:
  2. ; Check how well the moves you've already used
  3. ; fare against the enemy's Pokemon. Used to
  4. ; score a potential switch.
  5. push hl
  6. push de
  7. push bc
  8. ld a, BASE_AI_SWITCH_SCORE
  9. ld [wEnemyAISwitchScore], a
  10. ld hl, wPlayerUsedMoves
  11. ld a, [hl]
  12. and a
  13. jr z, .unknown_moves
  14.  
  15. ld d, NUM_MOVES
  16. ld e, 0
  17. .loop
  18. ld a, [hli]
  19. and a
  20. jr z, .exit
  21. push hl
  22. dec a
  23. ld hl, Moves + MOVE_POWER
  24. call GetMoveAttr
  25. and a
  26. jr z, .next
  27.  
  28. inc hl
  29. call GetMoveByte
  30. ld hl, wEnemyMonType
  31. call CheckTypeMatchup
  32. ld a, [wTypeMatchup]
  33. cp EFFECTIVE + 1 ; 1.0 + 0.1
  34. jr nc, .super_effective
  35. and a
  36. jr z, .next
  37. cp EFFECTIVE ; 1.0
  38. jr nc, .neutral
  39.  
  40. ; not very effective
  41. ld a, e
  42. cp 1 ; 0.1
  43. jr nc, .next
  44. ld e, 1
  45. jr .next
  46.  
  47. .neutral
  48. ld e, 2
  49. jr .next
  50.  
  51. .super_effective
  52. call .doubledown
  53. pop hl
  54. jr .done
  55.  
  56. .next
  57. pop hl
  58. dec d
  59. jr nz, .loop
  60.  
  61. .exit
  62. ld a, e
  63. cp 2
  64. jr z, .done
  65. call .IncreaseScore
  66. ld a, e
  67. and a
  68. jr nz, .done
  69. call .IncreaseScore
  70. jr .done
  71.  
  72. .unknown_moves
  73. ld a, [wBattleMonType1]
  74. ld b, a
  75. ld hl, wEnemyMonType1
  76. call CheckTypeMatchup
  77. ld a, [wTypeMatchup]
  78. cp EFFECTIVE + 1 ; 1.0 + 0.1
  79. jr c, .ok
  80. call .DecreaseScore
  81. .ok
  82. ld a, [wBattleMonType2]
  83. cp b
  84. jr z, .ok2
  85. call CheckTypeMatchup
  86. ld a, [wTypeMatchup]
  87. cp EFFECTIVE + 1 ; 1.0 + 0.1
  88. jr c, .ok2
  89. call .DecreaseScore
  90. .ok2
  91. ld a, [wBattleMonType1]
  92. ld b, a
  93. ld hl, wEnemyMonType2
  94. ld a, [wEnemyMonType1]
  95. ld d, b
  96. ld a, [wEnemyMonType2]
  97. cp d
  98. jr z, .done
  99. call CheckTypeMatchup
  100. ld a, [wTypeMatchup]
  101. cp EFFECTIVE + 1 ; 1.0 + 0.1
  102. jr c, .ok3
  103. call .DecreaseScore
  104. .ok3
  105. ld a, [wBattleMonType2]
  106. cp b
  107. jr z, .done
  108. call CheckTypeMatchup
  109. ld a, [wTypeMatchup]
  110. cp EFFECTIVE + 1 ; 1.0 + 0.1
  111. jr c, .done
  112. call .DecreaseScore
  113. .done
  114. call .CheckEnemyMoveMatchups
  115. pop bc
  116. pop de
  117. pop hl
  118. ret
  119.  
  120. .CheckEnemyMoveMatchups:
  121. ld de, wEnemyMonMoves
  122. ld b, NUM_MOVES + 1
  123. ld c, 0
  124.  
  125. ld a, [wTypeMatchup]
  126. push af
  127. .loop2
  128. dec b
  129. jr z, .exit2
  130.  
  131. ld a, [de]
  132. and a
  133. jr z, .exit2
  134.  
  135. inc de
  136. dec a
  137. ld hl, Moves + MOVE_POWER
  138. call GetMoveAttr
  139. and a
  140. jr z, .loop2
  141.  
  142. inc hl
  143. call GetMoveByte
  144. ld hl, wBattleMonType1
  145. call CheckTypeMatchup
  146.  
  147. ld a, [wTypeMatchup]
  148. ; immune
  149. and a
  150. jr z, .loop2
  151.  
  152. ; not very effective
  153. inc c
  154. cp EFFECTIVE
  155. jr c, .loop2
  156.  
  157. ; neutral
  158. inc c
  159. inc c
  160. inc c
  161. inc c
  162. inc c
  163. cp EFFECTIVE
  164. jr z, .loop2
  165.  
  166. ; super effective
  167. ld c, 100
  168. call .IncreaseScore
  169. jr .loop2
  170.  
  171. .exit2
  172. pop af
  173. ld [wTypeMatchup], a
  174.  
  175. ld a, c
  176. and a
  177. jr z, .doubledown ; double down
  178. cp 5
  179. jr c, .DecreaseScore ; down
  180. cp 100
  181. ret c
  182. jr .IncreaseScore ; up
  183.  
  184. .doubledown
  185. call .DecreaseScore
  186. .DecreaseScore:
  187. ld a, [wEnemyAISwitchScore]
  188. dec a
  189. ld [wEnemyAISwitchScore], a
  190. ret
  191.  
  192. .IncreaseScore:
  193. ld a, [wEnemyAISwitchScore]
  194. inc a
  195. ld [wEnemyAISwitchScore], a
  196. ret
  197.  
  198. CheckAbleToSwitch:
  199. xor a
  200. ld [wEnemySwitchMonParam], a
  201. call FindAliveEnemyMons
  202. ret c
  203.  
  204. ld hl, TrainerClassAttributes + TRNATTR_AI_ITEM_SWITCH
  205.  
  206. ld a, [wTrainerClass]
  207. dec a
  208. ld bc, NUM_TRAINER_ATTRIBUTES
  209. call AddNTimes
  210.  
  211. ld a, BANK(TrainerClassAttributes)
  212. call GetFarByte
  213. bit SWITCH_OFTEN_F, a
  214. jr nz, .checkstat
  215. bit SWITCH_SOMETIMES_F, a
  216. jr nz, .checkstat
  217. jr .checkperish
  218. .checkstat
  219. ; Checks if Evasion is greater than 0
  220. ld a, [wEnemyEvaLevel]
  221. cp BASE_STAT_LEVEL + 1
  222. ret nc
  223. ; Checks if Accuracy is below -1
  224. ld a, [wEnemyAccLevel]
  225. cp BASE_STAT_LEVEL - 1
  226. jr c, .rollswitch
  227. ld hl, wPlayerStatLevels
  228. ld c, NUM_LEVEL_STATS - 1
  229. ld b, 0
  230. ld e, 0
  231. ; Check player's stat buffs
  232. .checkplayerbuff
  233. dec c
  234. jr z, .checkpt2
  235. ld a, [hli]
  236. cp BASE_STAT_LEVEL
  237. jr nc, .checkplayerbuff2
  238. jr .checkplayerbuff
  239. .checkplayerbuff2
  240. sub a, BASE_STAT_LEVEL
  241. ; b holds the stat buffs
  242. add b
  243. ld b, a
  244. jr .checkplayerbuff
  245. ; Check AI's stat buffs
  246. .checkpt2
  247. ld hl, wEnemyStatLevels
  248. ld c, 7
  249. .checkenemybuff
  250. dec c
  251. jr z, .cont_check
  252. ld a, [hli]
  253. cp BASE_STAT_LEVEL
  254. jr nc, .checkenemybuff2
  255. jr .checkenemybuff
  256. .checkenemybuff2
  257. sub a, BASE_STAT_LEVEL
  258. ; e holds the stat buffs
  259. add e
  260. ld e, a
  261. jr .checkenemybuff
  262. .cont_check
  263. ; Checks if AI has no boosts
  264. ld a, e
  265. cp 0
  266. jr c, .cont_check_2
  267. jr z, .cont_check_2
  268. ; Check if player is has 2 stat buffs
  269. ld a, b
  270. cp 2
  271. ret nc
  272. ; Otherwise, roll to check other clauses or not
  273. call Random
  274. cp 20 percent
  275. ret c
  276. jr .checkperish
  277. .cont_check_2
  278. ; Checks if non-spd stat (because of Curse) is below -2
  279. ld a, [wEnemyAtkLevel]
  280. cp BASE_STAT_LEVEL - 2
  281. jr c, .rollswitch
  282. ld a, [wEnemyDefLevel]
  283. cp BASE_STAT_LEVEL - 2
  284. jr c, .rollswitch
  285. ld a, [wEnemySAtkLevel]
  286. cp BASE_STAT_LEVEL - 2
  287. jr c, .rollswitch
  288. ld a, [wEnemySDefLevel]
  289. cp BASE_STAT_LEVEL - 2
  290. jr c, .rollswitch
  291. jr .checkperish
  292. .rollswitch
  293. call Random
  294. cp 65 percent
  295. jr c, .switch
  296. jr .checkperish
  297.  
  298. .checkperish
  299. ld a, [wEnemySubStatus1]
  300. bit SUBSTATUS_PERISH, a
  301. jr z, .no_perish
  302.  
  303. ld a, [wEnemyPerishCount]
  304. cp 1
  305. jr nz, .no_perish
  306.  
  307. ; Try to switch
  308. .switch
  309. call FindAliveEnemyMons
  310. call FindEnemyMonsWithAtLeastQuarterMaxHP
  311. call FindEnemyMonsThatResistPlayer
  312. call FindAliveEnemyMonsWithASuperEffectiveMove
  313.  
  314. ld a, e
  315. cp 2
  316. jr nz, .not_2
  317.  
  318. ld a, [wEnemyAISwitchScore]
  319. add $30 ; maximum chance
  320. ld [wEnemySwitchMonParam], a
  321. ret
  322.  
  323. .not_2
  324. call FindAliveEnemyMons
  325. sla c
  326. sla c
  327. ld b, $ff
  328.  
  329. .loop1
  330. inc b
  331. sla c
  332. jr nc, .loop1
  333.  
  334. ld a, b
  335. add $30 ; maximum chance
  336. ld [wEnemySwitchMonParam], a
  337. ret
  338.  
  339. .no_perish
  340. call CheckPlayerMoveTypeMatchups
  341. ld a, [wEnemyAISwitchScore]
  342. cp 11
  343. ret nc
  344.  
  345. ld a, [wLastPlayerCounterMove]
  346. and a
  347. jr z, .no_last_counter_move
  348.  
  349. call FindEnemyMonsImmuneToLastCounterMove
  350. ld a, [wEnemyAISwitchScore]
  351. and a
  352. jr z, .no_last_counter_move
  353.  
  354. ld c, a
  355. call FindEnemyMonsWithASuperEffectiveMove
  356. ld a, [wEnemyAISwitchScore]
  357. cp $ff
  358. ret z
  359.  
  360. ld b, a
  361. ld a, e
  362. cp 2
  363. jr z, .not_2_again
  364.  
  365. call CheckPlayerMoveTypeMatchups
  366. ld a, [wEnemyAISwitchScore]
  367. cp 11
  368. ret nc
  369.  
  370. ld a, b
  371. add $20
  372. ld [wEnemySwitchMonParam], a
  373. ret
  374.  
  375. .not_2_again
  376. ld c, $10
  377. call CheckPlayerMoveTypeMatchups
  378. ld a, [wEnemyAISwitchScore]
  379. cp 10
  380. jr nc, .okay
  381. ld c, $20
  382.  
  383. .okay
  384. ld a, b
  385. add c
  386. ld [wEnemySwitchMonParam], a
  387. ret
  388.  
  389. ; Smart switching
  390. .smartswitch
  391. call Random
  392. cp 80 percent
  393. jr c, .no_switch
  394.  
  395. ld a, e
  396. cp 2
  397. jr nz, .not_2
  398.  
  399. ld a, [wEnemyAISwitchScore]
  400. add $20
  401. ld [wEnemySwitchMonParam], a
  402. ret
  403.  
  404. .no_switch
  405. ld a, e
  406. cp $2
  407. ret nz
  408.  
  409. .no_last_counter_move
  410. call CheckPlayerMoveTypeMatchups
  411. ld a, [wEnemyAISwitchScore]
  412. cp 11
  413. ret nc
  414.  
  415. ld hl, TrainerClassAttributes + TRNATTR_AI_ITEM_SWITCH
  416.  
  417. ld a, [wTrainerClass]
  418. dec a
  419. ld bc, NUM_TRAINER_ATTRIBUTES
  420. call AddNTimes
  421.  
  422. ld a, BANK(TrainerClassAttributes)
  423. call GetFarByte
  424. bit SWITCH_OFTEN_F, a
  425. jr nz, .smartcheck
  426. ret
  427.  
  428. ; Randomize switch check only for SWITCH_OFTEN AI
  429. .smartcheck:
  430. call Random
  431. cp 35 percent
  432. jr c, .algorithm2
  433. ; Algorithm 1
  434. call FindAliveEnemyMons
  435. call FindEnemyMonsWithAtLeastQuarterMaxHP
  436. call FindEnemyMonsThatResistPlayer
  437. call FindAliveEnemyMonsWithASuperEffectiveMove
  438. jp .smartswitch
  439.  
  440. .algorithm2
  441. call FindAliveEnemyMons
  442. call FindEnemyMonsWithAtLeastQuarterMaxHP
  443. call FindEnemyMonsThatResistPlayer
  444. jp .smartswitch
  445.  
  446. FindAliveEnemyMons:
  447. ld a, [wOTPartyCount]
  448. cp 2
  449. jr c, .only_one
  450.  
  451. ld d, a
  452. ld e, 0
  453. ld b, 1 << (PARTY_LENGTH - 1)
  454. ld c, 0
  455. ld hl, wOTPartyMon1HP
  456.  
  457. .loop
  458. ld a, [wCurOTMon]
  459. cp e
  460. jr z, .next
  461.  
  462. push bc
  463. ld b, [hl]
  464. inc hl
  465. ld a, [hld]
  466. or b
  467. pop bc
  468. jr z, .next
  469.  
  470. ld a, c
  471. or b
  472. ld c, a
  473.  
  474. .next
  475. srl b
  476. push bc
  477. ld bc, PARTYMON_STRUCT_LENGTH
  478. add hl, bc
  479. pop bc
  480. inc e
  481. dec d
  482. jr nz, .loop
  483.  
  484. ld a, c
  485. and a
  486. jr nz, .more_than_one
  487.  
  488. .only_one
  489. scf
  490. ret
  491.  
  492. .more_than_one
  493. and a
  494. ret
  495.  
  496. FindEnemyMonsImmuneToLastCounterMove:
  497. ld hl, wOTPartyMon1
  498. ld a, [wOTPartyCount]
  499. ld b, a
  500. ld c, 1 << (PARTY_LENGTH - 1)
  501. ld d, 0
  502. xor a
  503. ld [wEnemyAISwitchScore], a
  504.  
  505. .loop
  506. ld a, [wCurOTMon]
  507. cp d
  508. push hl
  509. jr z, .next
  510.  
  511. push hl
  512. push bc
  513.  
  514. ; If the Pokemon has at least 1 HP...
  515. ld bc, MON_HP
  516. add hl, bc
  517. pop bc
  518. ld a, [hli]
  519. or [hl]
  520. pop hl
  521. jr z, .next
  522.  
  523. ld a, [hl]
  524. ld [wCurSpecies], a
  525. call GetBaseData
  526.  
  527. ; the player's last move is damaging...
  528. ld a, [wLastPlayerCounterMove]
  529. dec a
  530. ld hl, Moves + MOVE_POWER
  531. call GetMoveAttr
  532. and a
  533. jr z, .next
  534.  
  535. ; and the Pokemon is immune to it...
  536. inc hl
  537. call GetMoveByte
  538. ld hl, wBaseType
  539. call CheckTypeMatchup
  540. ld a, [wTypeMatchup]
  541. and a
  542. jr nz, .next
  543.  
  544. ; ... encourage that Pokemon.
  545. ld a, [wEnemyAISwitchScore]
  546. or c
  547. ld [wEnemyAISwitchScore], a
  548. .next
  549. pop hl
  550. dec b
  551. ret z
  552.  
  553. push bc
  554. ld bc, PARTYMON_STRUCT_LENGTH
  555. add hl, bc
  556. pop bc
  557.  
  558. inc d
  559. srl c
  560. jr .loop
  561.  
  562. FindAliveEnemyMonsWithASuperEffectiveMove:
  563. push bc
  564. ld a, [wOTPartyCount]
  565. ld e, a
  566. ld hl, wOTPartyMon1HP
  567. ld b, 1 << (PARTY_LENGTH - 1)
  568. ld c, 0
  569. .loop
  570. ld a, [hli]
  571. or [hl]
  572. jr z, .next
  573.  
  574. ld a, b
  575. or c
  576. ld c, a
  577.  
  578. .next
  579. srl b
  580. push bc
  581. ld bc, wPartyMon2HP - (wPartyMon1HP + 1)
  582. add hl, bc
  583. pop bc
  584. dec e
  585. jr nz, .loop
  586.  
  587. ld a, c
  588. pop bc
  589.  
  590. and c
  591. ld c, a
  592. ; fallthrough
  593.  
  594. FindEnemyMonsWithASuperEffectiveMove:
  595. ld a, -1
  596. ld [wEnemyAISwitchScore], a
  597. ld hl, wOTPartyMon1Moves
  598. ld b, 1 << (PARTY_LENGTH - 1)
  599. ld d, 0
  600. ld e, 0
  601. .loop
  602. ld a, b
  603. and c
  604. jr z, .next
  605.  
  606. push hl
  607. push bc
  608. ; for move on mon:
  609. ld b, NUM_MOVES
  610. ld c, 0
  611. .loop3
  612. ; if move is None: break
  613. ld a, [hli]
  614. and a
  615. push hl
  616. jr z, .break3
  617.  
  618. ; if move has no power: continue
  619. dec a
  620. ld hl, Moves + MOVE_POWER
  621. call GetMoveAttr
  622. and a
  623. jr z, .nope
  624.  
  625. ; check type matchups
  626. inc hl
  627. call GetMoveByte
  628. ld hl, wBattleMonType1
  629. call CheckTypeMatchup
  630.  
  631. ; if immune or not very effective: continue
  632. ld a, [wTypeMatchup]
  633. cp 10
  634. jr c, .nope
  635.  
  636. ; if neutral: load 1 and continue
  637. ld e, 1
  638. cp EFFECTIVE + 1
  639. jr c, .nope
  640.  
  641. ; if super-effective: load 2 and break
  642. ld e, 2
  643. jr .break3
  644.  
  645. .nope
  646. pop hl
  647. dec b
  648. jr nz, .loop3
  649.  
  650. jr .done
  651.  
  652. .break3
  653. pop hl
  654. .done
  655. ld a, e
  656. pop bc
  657. pop hl
  658. cp 2
  659. jr z, .done2 ; at least one move is super-effective
  660. cp 1
  661. jr nz, .next ; no move does more than half damage
  662.  
  663. ; encourage this pokemon
  664. ld a, d
  665. or b
  666. ld d, a
  667. jr .next ; such a long jump
  668.  
  669. .next
  670. ; next pokemon?
  671. push bc
  672. ld bc, PARTYMON_STRUCT_LENGTH
  673. add hl, bc
  674. pop bc
  675. srl b
  676. jr nc, .loop
  677.  
  678. ; if no pokemon has a super-effective move: return
  679. ld a, d
  680. ld b, a
  681. and a
  682. ret z
  683.  
  684. .done2
  685. ; convert the bit flag to an int and return
  686. push bc
  687. sla b
  688. sla b
  689. ld c, $ff
  690. .loop2
  691. inc c
  692. sla b
  693. jr nc, .loop2
  694.  
  695. ld a, c
  696. ld [wEnemyAISwitchScore], a
  697. pop bc
  698. ret
  699.  
  700. FindEnemyMonsThatResistPlayer:
  701. push bc
  702. ld hl, wOTPartySpecies
  703. ld b, 1 << (PARTY_LENGTH - 1)
  704. ld c, 0
  705.  
  706. .loop
  707. ld a, [hli]
  708. cp $ff
  709. jr z, .done
  710.  
  711. push hl
  712. ld [wCurSpecies], a
  713. call GetBaseData
  714. ld a, [wLastPlayerCounterMove]
  715. and a
  716. jr z, .skip_move
  717.  
  718. dec a
  719. ld hl, Moves + MOVE_POWER
  720. call GetMoveAttr
  721. and a
  722. jr z, .skip_move
  723.  
  724. inc hl
  725. call GetMoveByte
  726. jr .check_type
  727.  
  728. .skip_move
  729. ld a, [wBattleMonType1]
  730. ld hl, wBaseType
  731. call CheckTypeMatchup
  732. ld a, [wTypeMatchup]
  733. cp 10 + 1
  734. jr nc, .dont_choose_mon
  735. ld a, [wBattleMonType2]
  736.  
  737. .check_type
  738. ld hl, wBaseType
  739. call CheckTypeMatchup
  740. ld a, [wTypeMatchup]
  741. cp EFFECTIVE + 1
  742. jr nc, .dont_choose_mon
  743.  
  744. ld a, b
  745. or c
  746. ld c, a
  747.  
  748. .dont_choose_mon
  749. srl b
  750. pop hl
  751. jr .loop
  752.  
  753. .done
  754. ld a, c
  755. pop bc
  756. and c
  757. ld c, a
  758. ret
  759.  
  760. FindEnemyMonsWithAtLeastQuarterMaxHP:
  761. push bc
  762. ld de, wOTPartySpecies
  763. ld b, 1 << (PARTY_LENGTH - 1)
  764. ld c, 0
  765. ld hl, wOTPartyMon1HP
  766.  
  767. .loop
  768. ld a, [de]
  769. inc de
  770. cp $ff
  771. jr z, .done
  772.  
  773. push hl
  774. push bc
  775. ld b, [hl]
  776. inc hl
  777. ld c, [hl]
  778. inc hl
  779. inc hl
  780. ; hl = MaxHP + 1
  781. ; bc = [CurHP] * 4
  782. srl c
  783. rl b
  784. srl c
  785. rl b
  786. ; if bc >= [hl], encourage
  787. ld a, [hld]
  788. cp c
  789. ld a, [hl]
  790. sbc b
  791. pop bc
  792. jr nc, .next
  793.  
  794. ld a, b
  795. or c
  796. ld c, a
  797.  
  798. .next
  799. srl b
  800. pop hl
  801. push bc
  802. ld bc, PARTYMON_STRUCT_LENGTH
  803. add hl, bc
  804. pop bc
  805. jr .loop
  806.  
  807. .done
  808. ld a, c
  809. pop bc
  810. and c
  811. ld c, a
  812. ret
  813.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement