evos_moves.asm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. ; try to evolve the mon in [wWhichPokemon]
  2. TryEvolvingMon:
  3. ld hl, wCanEvolveFlags
  4. xor a
  5. ld [hl], a
  6. ld a, [wWhichPokemon]
  7. ld c, a
  8. ld b, FLAG_SET
  9. call Evolution_FlagAction
  10. ; this is only called after battle
  11. ; it is supposed to do level up evolutions, though there is a bug that allows item evolutions to occur
  12. EvolutionAfterBattle:
  13. ld a, [hTilesetType]
  14. push af
  15. xor a
  16. ld [wEvolutionOccurred], a
  17. dec a
  18. ld [wWhichPokemon], a
  19. push hl
  20. push bc
  21. push de
  22. ld hl, wPartyCount
  23. push hl
  24. Evolution_PartyMonLoop: ; loop over party mons
  25. ld hl, wWhichPokemon
  26. inc [hl]
  27. pop hl
  28. inc hl
  29. ld a, [hl]
  30. cp $ff ; have we reached the end of the party?
  31. jp z, .done
  32. ld [wEvoOldSpecies], a
  33. push hl
  34. ld a, [wWhichPokemon]
  35. ld c, a
  36. ld hl, wCanEvolveFlags
  37. ld b, FLAG_TEST
  38. call Evolution_FlagAction
  39. ld a, c
  40. and a ; is the mon's bit set?
  41. jp z, Evolution_PartyMonLoop ; if not, go to the next mon
  42. ld a, [wEvoOldSpecies]
  43. dec a
  44. ld b, 0
  45. ld hl, EvosMovesPointerTable
  46. add a
  47. rl b
  48. ld c, a
  49. add hl, bc
  50. ld a, [hli]
  51. ld h, [hl]
  52. ld l, a
  53. push hl
  54. ld a, [wcf91]
  55. push af
  56. xor a ; PLAYER_PARTY_DATA
  57. ld [wMonDataLocation], a
  58. call LoadMonData
  59. pop af
  60. ld [wcf91], a
  61. pop hl
  62. .evoEntryLoop ; loop over evolution entries
  63. ld a, [hli]
  64. and a ; have we reached the end of the evolution data?
  65. jr z, Evolution_PartyMonLoop
  66. ld b, a ; evolution type
  67. cp EV_TRADE
  68. jr z, .checkTradeEvo
  69. ; not trade evolution
  70. ld a, [wLinkState]
  71. cp LINK_STATE_TRADING
  72. jr z, Evolution_PartyMonLoop ; if trading, go the next mon
  73. ld a, b
  74. cp EV_ITEM
  75. jr z, .checkItemEvo
  76. ld a, [wForceEvolution]
  77. and a
  78. jr nz, Evolution_PartyMonLoop
  79. ld a, b
  80. cp EV_LEVEL
  81. jr z, .checkLevel
  82. .checkTradeEvo
  83. ld a, [wLinkState]
  84. cp LINK_STATE_TRADING
  85. jp nz, .nextEvoEntry1 ; if not trading, go to the next evolution entry
  86. ld a, [hli] ; level requirement
  87. ld b, a
  88. ld a, [wLoadedMonLevel]
  89. cp b ; is the mon's level greater than the evolution requirement?
  90. jp c, Evolution_PartyMonLoop ; if so, go the next mon
  91. jr .doEvolution
  92. .checkItemEvo
  93. ld a, [hli]
  94. ld b, a ; evolution item
  95. ld a, [wcf91] ; this is supposed to be the last item used, but it is also used to hold species numbers
  96. cp b ; was the evolution item in this entry used?
  97. jp nz, .nextEvoEntry1 ; if not, go to the next evolution entry
  98. .checkLevel
  99. ld a, [hli] ; level requirement
  100. ld b, a
  101. ld a, [wLoadedMonLevel]
  102. cp b ; is the mon's level greater than the evolution requirement?
  103. jp c, .nextEvoEntry2 ; if so, go the next evolution entry
  104. .doEvolution
  105. ld [wCurEnemyLVL], a
  106. ld a, 1
  107. ld [wEvolutionOccurred], a
  108. push hl
  109. ld a, [hl]
  110. ld [wEvoNewSpecies], a
  111. ld a, [wWhichPokemon]
  112. ld hl, wPartyMonNicks
  113. call GetPartyMonName
  114. call CopyStringToCF4B
  115. ld hl, IsEvolvingText
  116. call PrintText
  117. ld c, 50
  118. call DelayFrames
  119. xor a
  120. ld [H_AUTOBGTRANSFERENABLED], a
  121. coord hl, 0, 0
  122. lb bc, 12, 20
  123. call ClearScreenArea
  124. ld a, $1
  125. ld [H_AUTOBGTRANSFERENABLED], a
  126. ld a, $ff
  127. ld [wUpdateSpritesEnabled], a
  128. call ClearSprites
  129. callab EvolveMon
  130. jp c, CancelledEvolution
  131. ld hl, EvolvedText
  132. call PrintText
  133. pop hl
  134. ld a, [hl]
  135. ld [wd0b5], a
  136. ld [wLoadedMonSpecies], a
  137. ld [wEvoNewSpecies], a
  138. ld a, MONSTER_NAME
  139. ld [wNameListType], a
  140. ld a, BANK(TrainerNames) ; bank is not used for monster names
  141. ld [wPredefBank], a
  142. call GetName
  143. push hl
  144. ld hl, IntoText
  145. call PrintText_NoCreatingTextBox
  146. ld a, SFX_GET_ITEM_2
  147. call PlaySoundWaitForCurrent
  148. call WaitForSoundToFinish
  149. ld c, 40
  150. call DelayFrames
  151. call ClearScreen
  152. call RenameEvolvedMon
  153. ld a, [wd11e]
  154. push af
  155. ld a, [wd0b5]
  156. ld [wd11e], a
  157. predef IndexToPokedex
  158. ld a, [wd11e]
  159. dec a
  160. ld hl, BaseStats
  161. ld bc, MonBaseStatsEnd - MonBaseStats
  162. call AddNTimes
  163. ld de, wMonHeader
  164. call CopyData
  165. ld a, [wd0b5]
  166. ld [wMonHIndex], a
  167. pop af
  168. ld [wd11e], a
  169. ld hl, wLoadedMonHPExp - 1
  170. ld de, wLoadedMonStats
  171. ld b, $1
  172. call CalcStats
  173. ld a, [wWhichPokemon]
  174. ld hl, wPartyMon1
  175. ld bc, wPartyMon2 - wPartyMon1
  176. call AddNTimes
  177. ld e, l
  178. ld d, h
  179. push hl
  180. push bc
  181. ld bc, wPartyMon1MaxHP - wPartyMon1
  182. add hl, bc
  183. ld a, [hli]
  184. ld b, a
  185. ld c, [hl]
  186. ld hl, wLoadedMonMaxHP + 1
  187. ld a, [hld]
  188. sub c
  189. ld c, a
  190. ld a, [hl]
  191. sbc b
  192. ld b, a
  193. ld hl, wLoadedMonHP + 1
  194. ld a, [hl]
  195. add c
  196. ld [hld], a
  197. ld a, [hl]
  198. adc b
  199. ld [hl], a
  200. dec hl
  201. pop bc
  202. call CopyData
  203. ld a, [wd0b5]
  204. ld [wd11e], a
  205. xor a
  206. ld [wMonDataLocation], a
  207. call LearnMoveFromLevelUp
  208. pop hl
  209. predef SetPartyMonTypes
  210. ld a, [wIsInBattle]
  211. and a
  212. call z, Evolution_ReloadTilesetTilePatterns
  213. predef IndexToPokedex
  214. ld a, [wd11e]
  215. dec a
  216. ld c, a
  217. ld b, FLAG_SET
  218. ld hl, wPokedexOwned
  219. push bc
  220. call Evolution_FlagAction
  221. pop bc
  222. ld hl, wPokedexSeen
  223. call Evolution_FlagAction
  224. pop de
  225. pop hl
  226. ld a, [wLoadedMonSpecies]
  227. ld [hl], a
  228. push hl
  229. ld l, e
  230. ld h, d
  231. jr .nextEvoEntry2
  232. .nextEvoEntry1
  233. inc hl
  234. .nextEvoEntry2
  235. inc hl
  236. jp .evoEntryLoop
  237. .done
  238. pop de
  239. pop bc
  240. pop hl
  241. pop af
  242. ld [hTilesetType], a
  243. ld a, [wLinkState]
  244. cp LINK_STATE_TRADING
  245. ret z
  246. ld a, [wIsInBattle]
  247. and a
  248. ret nz
  249. ld a, [wEvolutionOccurred]
  250. and a
  251. call nz, PlayDefaultMusic
  252. ret
  253. RenameEvolvedMon:
  254. ; Renames the mon to its new, evolved form's standard name unless it had a
  255. ; nickname, in which case the nickname is kept.
  256. ld a, [wd0b5]
  257. push af
  258. ld a, [wMonHIndex]
  259. ld [wd0b5], a
  260. call GetName
  261. pop af
  262. ld [wd0b5], a
  263. ld hl, wcd6d
  264. ld de, wcf4b
  265. .compareNamesLoop
  266. ld a, [de]
  267. inc de
  268. cp [hl]
  269. inc hl
  270. ret nz
  271. cp "@"
  272. jr nz, .compareNamesLoop
  273. ld a, [wWhichPokemon]
  274. ld bc, NAME_LENGTH
  275. ld hl, wPartyMonNicks
  276. call AddNTimes
  277. push hl
  278. call GetName
  279. ld hl, wcd6d
  280. pop de
  281. jp CopyData
  282. CancelledEvolution:
  283. ld hl, StoppedEvolvingText
  284. call PrintText
  285. call ClearScreen
  286. pop hl
  287. call Evolution_ReloadTilesetTilePatterns
  288. jp Evolution_PartyMonLoop
  289. EvolvedText:
  290. TX_FAR _EvolvedText
  291. db "@"
  292. IntoText:
  293. TX_FAR _IntoText
  294. db "@"
  295. StoppedEvolvingText:
  296. TX_FAR _StoppedEvolvingText
  297. db "@"
  298. IsEvolvingText:
  299. TX_FAR _IsEvolvingText
  300. db "@"
  301. Evolution_ReloadTilesetTilePatterns:
  302. ld a, [wLinkState]
  303. cp LINK_STATE_TRADING
  304. ret z
  305. jp ReloadTilesetTilePatterns
  306. LearnMoveFromLevelUp:
  307. ld hl, EvosMovesPointerTable
  308. ld a, [wd11e] ; species
  309. ld [wcf91], a
  310. dec a
  311. ld bc, 0
  312. ld hl, EvosMovesPointerTable
  313. add a
  314. rl b
  315. ld c, a
  316. add hl, bc
  317. ld a, [hli]
  318. ld h, [hl]
  319. ld l, a
  320. .skipEvolutionDataLoop ; loop to skip past the evolution data, which comes before the move data
  321. ld a, [hli]
  322. and a ; have we reached the end of the evolution data?
  323. jr nz, .skipEvolutionDataLoop ; if not, jump back up
  324. .learnSetLoop ; loop over the learn set until we reach a move that is learnt at the current level or the end of the list
  325. ld a, [hli]
  326. and a ; have we reached the end of the learn set?
  327. jr z, .done ; if we've reached the end of the learn set, jump
  328. ld b, a ; level the move is learnt at
  329. ld a, [wCurEnemyLVL]
  330. cp b ; is the move learnt at the mon's current level?
  331. ld a, [hli] ; move ID
  332. jr nz, .learnSetLoop
  333. ld d, a ; ID of move to learn
  334. ld a, [wMonDataLocation]
  335. and a
  336. jr nz, .next
  337. ; If [wMonDataLocation] is 0 (PLAYER_PARTY_DATA), get the address of the mon's
  338. ; current moves in party data. Every call to this function sets
  339. ; [wMonDataLocation] to 0 because other data locations are not supported.
  340. ; If it is not 0, this function will not work properly.
  341. ld hl, wPartyMon1Moves
  342. ld a, [wWhichPokemon]
  343. ld bc, wPartyMon2 - wPartyMon1
  344. call AddNTimes
  345. .next
  346. ld b, NUM_MOVES
  347. .checkCurrentMovesLoop ; check if the move to learn is already known
  348. ld a, [hli]
  349. cp d
  350. jr z, .done ; if already known, jump
  351. dec b
  352. jr nz, .checkCurrentMovesLoop
  353. ld a, d
  354. ld [wMoveNum], a
  355. ld [wd11e], a
  356. call GetMoveName
  357. call CopyStringToCF4B
  358. predef LearnMove
  359. .done
  360. ld a, [wcf91]
  361. ld [wd11e], a
  362. ret
  363. ; writes the moves a mon has at level [wCurEnemyLVL] to [de]
  364. ; move slots are being filled up sequentially and shifted if all slots are full
  365. WriteMonMoves:
  366. call GetPredefRegisters
  367. push hl
  368. push de
  369. push bc
  370. ld hl, EvosMovesPointerTable
  371. ld b, 0
  372. ld a, [wcf91] ; cur mon ID
  373. dec a
  374. add a
  375. rl b
  376. ld c, a
  377. add hl, bc
  378. ld a, [hli]
  379. ld h, [hl]
  380. ld l, a
  381. .skipEvoEntriesLoop
  382. ld a, [hli]
  383. and a
  384. jr nz, .skipEvoEntriesLoop
  385. jr .firstMove
  386. .nextMove
  387. pop de
  388. .nextMove2
  389. inc hl
  390. .firstMove
  391. ld a, [hli] ; read level of next move in learnset
  392. and a
  393. jp z, .done ; end of list
  394. ld b, a
  395. ld a, [wCurEnemyLVL]
  396. cp b
  397. jp c, .done ; mon level < move level (assumption: learnset is sorted by level)
  398. ld a, [wLearningMovesFromDayCare]
  399. and a
  400. jr z, .skipMinLevelCheck
  401. ld a, [wDayCareStartLevel]
  402. cp b
  403. jr nc, .nextMove2 ; min level >= move level
  404. .skipMinLevelCheck
  405. ; check if the move is already known
  406. push de
  407. ld c, NUM_MOVES
  408. .alreadyKnowsCheckLoop
  409. ld a, [de]
  410. inc de
  411. cp [hl]
  412. jr z, .nextMove
  413. dec c
  414. jr nz, .alreadyKnowsCheckLoop
  415. ; try to find an empty move slot
  416. pop de
  417. push de
  418. ld c, NUM_MOVES
  419. .findEmptySlotLoop
  420. ld a, [de]
  421. and a
  422. jr z, .writeMoveToSlot2
  423. inc de
  424. dec c
  425. jr nz, .findEmptySlotLoop
  426. ; no empty move slots found
  427. pop de
  428. push de
  429. push hl
  430. ld h, d
  431. ld l, e
  432. call WriteMonMoves_ShiftMoveData ; shift all moves one up (deleting move 1)
  433. ld a, [wLearningMovesFromDayCare]
  434. and a
  435. jr z, .writeMoveToSlot
  436. ; shift PP as well if learning moves from day care
  437. push de
  438. ld bc, wPartyMon1PP - (wPartyMon1Moves + 3)
  439. add hl, bc
  440. ld d, h
  441. ld e, l
  442. call WriteMonMoves_ShiftMoveData ; shift all move PP data one up
  443. pop de
  444. .writeMoveToSlot
  445. pop hl
  446. .writeMoveToSlot2
  447. ld a, [hl]
  448. ld [de], a
  449. ld a, [wLearningMovesFromDayCare]
  450. and a
  451. jr z, .nextMove
  452. ; write move PP value if learning moves from day care
  453. push hl
  454. ld a, [hl]
  455. ld hl, wPartyMon1PP - wPartyMon1Moves
  456. add hl, de
  457. push hl
  458. dec a
  459. ld hl, Moves
  460. ld bc, MoveEnd - Moves
  461. call AddNTimes
  462. ld de, wBuffer
  463. ld a, BANK(Moves)
  464. call FarCopyData
  465. ld a, [wBuffer + 5]
  466. pop hl
  467. ld [hl], a
  468. pop hl
  469. jr .nextMove
  470. .done
  471. pop bc
  472. pop de
  473. pop hl
  474. ret
  475. ; shifts all move data one up (freeing 4th move slot)
  476. WriteMonMoves_ShiftMoveData:
  477. ld c, NUM_MOVES - 1
  478. .loop
  479. inc de
  480. ld a, [de]
  481. ld [hli], a
  482. dec c
  483. jr nz, .loop
  484. ret
  485. Evolution_FlagAction:
  486. predef_jump FlagActionPredef
  487. INCLUDE "data/evos_moves.asm"