add_mon.asm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. _AddPartyMon:
  2. ; Adds a new mon to the player's or enemy's party.
  3. ; [wMonDataLocation] is used in an unusual way in this function.
  4. ; If the lower nybble is 0, the mon is added to the player's party, else the enemy's.
  5. ; If the entire value is 0, then the player is allowed to name the mon.
  6. ld de, wPartyCount
  7. ld a, [wMonDataLocation]
  8. and $f
  9. jr z, .next
  10. ld de, wEnemyPartyCount
  11. .next
  12. ld a, [de]
  13. inc a
  14. cp PARTY_LENGTH + 1
  15. ret nc ; return if the party is already full
  16. ld [de], a
  17. ld a, [de]
  18. ld [hNewPartyLength], a
  19. add e
  20. ld e, a
  21. jr nc, .noCarry
  22. inc d
  23. .noCarry
  24. ld a, [wcf91]
  25. ld [de], a ; write species of new mon in party list
  26. inc de
  27. ld a, $ff ; terminator
  28. ld [de], a
  29. ld hl, wPartyMonOT
  30. ld a, [wMonDataLocation]
  31. and $f
  32. jr z, .next2
  33. ld hl, wEnemyMonOT
  34. .next2
  35. ld a, [hNewPartyLength]
  36. dec a
  37. call SkipFixedLengthTextEntries
  38. ld d, h
  39. ld e, l
  40. ld hl, wPlayerName
  41. ld bc, NAME_LENGTH
  42. call CopyData
  43. ld a, [wMonDataLocation]
  44. and a
  45. jr nz, .skipNaming
  46. ld hl, wPartyMonNicks
  47. ld a, [hNewPartyLength]
  48. dec a
  49. call SkipFixedLengthTextEntries
  50. ld a, NAME_MON_SCREEN
  51. ld [wNamingScreenType], a
  52. predef AskName
  53. .skipNaming
  54. ld hl, wPartyMons
  55. ld a, [wMonDataLocation]
  56. and $f
  57. jr z, .next3
  58. ld hl, wEnemyMons
  59. .next3
  60. ld a, [hNewPartyLength]
  61. dec a
  62. ld bc, wPartyMon2 - wPartyMon1
  63. call AddNTimes
  64. ld e, l
  65. ld d, h
  66. push hl
  67. ld a, [wcf91]
  68. ld [wd0b5], a
  69. call GetMonHeader
  70. ld hl, wMonHeader
  71. ld a, [hli]
  72. ld [de], a ; species
  73. inc de
  74. pop hl
  75. push hl
  76. ld a, [wMonDataLocation]
  77. and $f
  78. ld a, $98 ; set enemy trainer mon IVs to fixed average values
  79. ld b, $88
  80. jr nz, .next4
  81. ; If the mon is being added to the player's party, update the pokedex.
  82. ld a, [wcf91]
  83. ld [wd11e], a
  84. push de
  85. predef IndexToPokedex
  86. pop de
  87. ld a, [wd11e]
  88. dec a
  89. ld c, a
  90. ld b, FLAG_TEST
  91. ld hl, wPokedexOwned
  92. call FlagAction
  93. ld a, c ; whether the mon was already flagged as owned
  94. ld [wUnusedD153], a ; not read
  95. ld a, [wd11e]
  96. dec a
  97. ld c, a
  98. ld b, FLAG_SET
  99. push bc
  100. call FlagAction
  101. pop bc
  102. ld hl, wPokedexSeen
  103. call FlagAction
  104. pop hl
  105. push hl
  106. ld a, [wIsInBattle]
  107. and a ; is this a wild mon caught in battle?
  108. jr nz, .copyEnemyMonData
  109. ; Not wild.
  110. call Random ; generate random IVs
  111. ld b, a
  112. call Random
  113. .next4
  114. push bc
  115. ld bc, wPartyMon1DVs - wPartyMon1
  116. add hl, bc
  117. pop bc
  118. ld [hli], a
  119. ld [hl], b ; write IVs
  120. ld bc, (wPartyMon1HPExp - 1) - (wPartyMon1DVs + 1)
  121. add hl, bc
  122. ld a, 1
  123. ld c, a
  124. xor a
  125. ld b, a
  126. call CalcStat ; calc HP stat (set cur Hp to max HP)
  127. ld a, [H_MULTIPLICAND+1]
  128. ld [de], a
  129. inc de
  130. ld a, [H_MULTIPLICAND+2]
  131. ld [de], a
  132. inc de
  133. xor a
  134. ld [de], a ; box level
  135. inc de
  136. ld [de], a ; status ailments
  137. inc de
  138. jr .copyMonTypesAndMoves
  139. .copyEnemyMonData
  140. ld bc, wEnemyMon1DVs - wEnemyMon1
  141. add hl, bc
  142. ld a, [wEnemyMonDVs] ; copy IVs from cur enemy mon
  143. ld [hli], a
  144. ld a, [wEnemyMonDVs + 1]
  145. ld [hl], a
  146. ld a, [wEnemyMonHP] ; copy HP from cur enemy mon
  147. ld [de], a
  148. inc de
  149. ld a, [wEnemyMonHP+1]
  150. ld [de], a
  151. inc de
  152. xor a
  153. ld [de], a ; box level
  154. inc de
  155. ld a, [wEnemyMonStatus] ; copy status ailments from cur enemy mon
  156. ld [de], a
  157. inc de
  158. .copyMonTypesAndMoves
  159. ld hl, wMonHTypes
  160. ld a, [hli] ; type 1
  161. ld [de], a
  162. inc de
  163. ld a, [hli] ; type 2
  164. ld [de], a
  165. inc de
  166. ld a, [hli] ; catch rate (held item in gen 2)
  167. ld [de], a
  168. ld hl, wMonHMoves
  169. ld a, [hli]
  170. inc de
  171. push de
  172. ld [de], a
  173. ld a, [hli]
  174. inc de
  175. ld [de], a
  176. ld a, [hli]
  177. inc de
  178. ld [de], a
  179. ld a, [hli]
  180. inc de
  181. ld [de], a
  182. push de
  183. dec de
  184. dec de
  185. dec de
  186. xor a
  187. ld [wLearningMovesFromDayCare], a
  188. predef WriteMonMoves
  189. pop de
  190. ld a, [wPlayerID] ; set trainer ID to player ID
  191. inc de
  192. ld [de], a
  193. ld a, [wPlayerID + 1]
  194. inc de
  195. ld [de], a
  196. push de
  197. ld a, [wCurEnemyLVL]
  198. ld d, a
  199. callab CalcExperience
  200. pop de
  201. inc de
  202. ld a, [hExperience] ; write experience
  203. ld [de], a
  204. inc de
  205. ld a, [hExperience + 1]
  206. ld [de], a
  207. inc de
  208. ld a, [hExperience + 2]
  209. ld [de], a
  210. xor a
  211. ld b, NUM_STATS * 2
  212. .writeEVsLoop ; set all EVs to 0
  213. inc de
  214. ld [de], a
  215. dec b
  216. jr nz, .writeEVsLoop
  217. inc de
  218. inc de
  219. pop hl
  220. call AddPartyMon_WriteMovePP
  221. inc de
  222. ld a, [wCurEnemyLVL]
  223. ld [de], a
  224. inc de
  225. ld a, [wIsInBattle]
  226. dec a
  227. jr nz, .calcFreshStats
  228. ld hl, wEnemyMonMaxHP
  229. ld bc, $a
  230. call CopyData ; copy stats of cur enemy mon
  231. pop hl
  232. jr .done
  233. .calcFreshStats
  234. pop hl
  235. ld bc, wPartyMon1HPExp - 1 - wPartyMon1
  236. add hl, bc
  237. ld b, $0
  238. call CalcStats ; calculate fresh set of stats
  239. .done
  240. scf
  241. ret
  242. LoadMovePPs:
  243. call GetPredefRegisters
  244. ; fallthrough
  245. AddPartyMon_WriteMovePP:
  246. ld b, NUM_MOVES
  247. .pploop
  248. ld a, [hli] ; read move ID
  249. and a
  250. jr z, .empty
  251. dec a
  252. push hl
  253. push de
  254. push bc
  255. ld hl, Moves
  256. ld bc, MoveEnd - Moves
  257. call AddNTimes
  258. ld de, wcd6d
  259. ld a, BANK(Moves)
  260. call FarCopyData
  261. pop bc
  262. pop de
  263. pop hl
  264. ld a, [wcd6d + 5] ; PP is byte 5 of move data
  265. .empty
  266. inc de
  267. ld [de], a
  268. dec b
  269. jr nz, .pploop ; there are still moves to read
  270. ret
  271. ; adds enemy mon [wcf91] (at position [wWhichPokemon] in enemy list) to own party
  272. ; used in the cable club trade center
  273. _AddEnemyMonToPlayerParty:
  274. ld hl, wPartyCount
  275. ld a, [hl]
  276. cp PARTY_LENGTH
  277. scf
  278. ret z ; party full, return failure
  279. inc a
  280. ld [hl], a ; add 1 to party members
  281. ld c, a
  282. ld b, $0
  283. add hl, bc
  284. ld a, [wcf91]
  285. ld [hli], a ; add mon as last list entry
  286. ld [hl], $ff ; write new sentinel
  287. ld hl, wPartyMons
  288. ld a, [wPartyCount]
  289. dec a
  290. ld bc, wPartyMon2 - wPartyMon1
  291. call AddNTimes
  292. ld e, l
  293. ld d, h
  294. ld hl, wLoadedMon
  295. call CopyData ; write new mon's data (from wLoadedMon)
  296. ld hl, wPartyMonOT
  297. ld a, [wPartyCount]
  298. dec a
  299. call SkipFixedLengthTextEntries
  300. ld d, h
  301. ld e, l
  302. ld hl, wEnemyMonOT
  303. ld a, [wWhichPokemon]
  304. call SkipFixedLengthTextEntries
  305. ld bc, NAME_LENGTH
  306. call CopyData ; write new mon's OT name (from an enemy mon)
  307. ld hl, wPartyMonNicks
  308. ld a, [wPartyCount]
  309. dec a
  310. call SkipFixedLengthTextEntries
  311. ld d, h
  312. ld e, l
  313. ld hl, wEnemyMonNicks
  314. ld a, [wWhichPokemon]
  315. call SkipFixedLengthTextEntries
  316. ld bc, NAME_LENGTH
  317. call CopyData ; write new mon's nickname (from an enemy mon)
  318. ld a, [wcf91]
  319. ld [wd11e], a
  320. predef IndexToPokedex
  321. ld a, [wd11e]
  322. dec a
  323. ld c, a
  324. ld b, FLAG_SET
  325. ld hl, wPokedexOwned
  326. push bc
  327. call FlagAction ; add to owned pokemon
  328. pop bc
  329. ld hl, wPokedexSeen
  330. call FlagAction ; add to seen pokemon
  331. and a
  332. ret ; return success
  333. _MoveMon:
  334. ld a, [wMoveMonType]
  335. and a ; BOX_TO_PARTY
  336. jr z, .checkPartyMonSlots
  337. cp DAYCARE_TO_PARTY
  338. jr z, .checkPartyMonSlots
  339. cp PARTY_TO_DAYCARE
  340. ld hl, wDayCareMon
  341. jr z, .findMonDataSrc
  342. ; else it's PARTY_TO_BOX
  343. ld hl, wNumInBox
  344. ld a, [hl]
  345. cp MONS_PER_BOX
  346. jr nz, .partyOrBoxNotFull
  347. jr .boxFull
  348. .checkPartyMonSlots
  349. ld hl, wPartyCount
  350. ld a, [hl]
  351. cp PARTY_LENGTH
  352. jr nz, .partyOrBoxNotFull
  353. .boxFull
  354. scf
  355. ret
  356. .partyOrBoxNotFull
  357. inc a
  358. ld [hl], a ; increment number of mons in party/box
  359. ld c, a
  360. ld b, 0
  361. add hl, bc
  362. ld a, [wMoveMonType]
  363. cp DAYCARE_TO_PARTY
  364. ld a, [wDayCareMon]
  365. jr z, .copySpecies
  366. ld a, [wcf91]
  367. .copySpecies
  368. ld [hli], a ; write new mon ID
  369. ld [hl], $ff ; write new sentinel
  370. .findMonDataDest
  371. ld a, [wMoveMonType]
  372. dec a
  373. ld hl, wPartyMons
  374. ld bc, wPartyMon2 - wPartyMon1 ; $2c
  375. ld a, [wPartyCount]
  376. jr nz, .addMonOffset
  377. ; if it's PARTY_TO_BOX
  378. ld hl, wBoxMons
  379. ld bc, wBoxMon2 - wBoxMon1 ; $21
  380. ld a, [wNumInBox]
  381. .addMonOffset
  382. dec a
  383. call AddNTimes
  384. .findMonDataSrc
  385. push hl
  386. ld e, l
  387. ld d, h
  388. ld a, [wMoveMonType]
  389. and a
  390. ld hl, wBoxMons
  391. ld bc, wBoxMon2 - wBoxMon1 ; $21
  392. jr z, .addMonOffset2
  393. cp DAYCARE_TO_PARTY
  394. ld hl, wDayCareMon
  395. jr z, .copyMonData
  396. ld hl, wPartyMons
  397. ld bc, wPartyMon2 - wPartyMon1 ; $2c
  398. .addMonOffset2
  399. ld a, [wWhichPokemon]
  400. call AddNTimes
  401. .copyMonData
  402. push hl
  403. push de
  404. ld bc, wBoxMon2 - wBoxMon1
  405. call CopyData
  406. pop de
  407. pop hl
  408. ld a, [wMoveMonType]
  409. and a ; BOX_TO_PARTY
  410. jr z, .findOTdest
  411. cp DAYCARE_TO_PARTY
  412. jr z, .findOTdest
  413. ld bc, wBoxMon2 - wBoxMon1
  414. add hl, bc
  415. ld a, [hl] ; hl = Level
  416. inc de
  417. inc de
  418. inc de
  419. ld [de], a ; de = BoxLevel
  420. .findOTdest
  421. ld a, [wMoveMonType]
  422. cp PARTY_TO_DAYCARE
  423. ld de, wDayCareMonOT
  424. jr z, .findOTsrc
  425. dec a
  426. ld hl, wPartyMonOT
  427. ld a, [wPartyCount]
  428. jr nz, .addOToffset
  429. ld hl, wBoxMonOT
  430. ld a, [wNumInBox]
  431. .addOToffset
  432. dec a
  433. call SkipFixedLengthTextEntries
  434. ld d, h
  435. ld e, l
  436. .findOTsrc
  437. ld hl, wBoxMonOT
  438. ld a, [wMoveMonType]
  439. and a
  440. jr z, .addOToffset2
  441. ld hl, wDayCareMonOT
  442. cp DAYCARE_TO_PARTY
  443. jr z, .copyOT
  444. ld hl, wPartyMonOT
  445. .addOToffset2
  446. ld a, [wWhichPokemon]
  447. call SkipFixedLengthTextEntries
  448. .copyOT
  449. ld bc, NAME_LENGTH
  450. call CopyData
  451. ld a, [wMoveMonType]
  452. .findNickDest
  453. cp PARTY_TO_DAYCARE
  454. ld de, wDayCareMonName
  455. jr z, .findNickSrc
  456. dec a
  457. ld hl, wPartyMonNicks
  458. ld a, [wPartyCount]
  459. jr nz, .addNickOffset
  460. ld hl, wBoxMonNicks
  461. ld a, [wNumInBox]
  462. .addNickOffset
  463. dec a
  464. call SkipFixedLengthTextEntries
  465. ld d, h
  466. ld e, l
  467. .findNickSrc
  468. ld hl, wBoxMonNicks
  469. ld a, [wMoveMonType]
  470. and a
  471. jr z, .addNickOffset2
  472. ld hl, wDayCareMonName
  473. cp DAYCARE_TO_PARTY
  474. jr z, .copyNick
  475. ld hl, wPartyMonNicks
  476. .addNickOffset2
  477. ld a, [wWhichPokemon]
  478. call SkipFixedLengthTextEntries
  479. .copyNick
  480. ld bc, NAME_LENGTH
  481. call CopyData
  482. pop hl
  483. ld a, [wMoveMonType]
  484. cp PARTY_TO_BOX
  485. jr z, .done
  486. cp PARTY_TO_DAYCARE
  487. jr z, .done
  488. push hl
  489. srl a
  490. add $2
  491. ld [wMonDataLocation], a
  492. call LoadMonData
  493. callba CalcLevelFromExperience
  494. ld a, d
  495. ld [wCurEnemyLVL], a
  496. pop hl
  497. ld bc, wBoxMon2 - wBoxMon1
  498. add hl, bc
  499. ld [hli], a
  500. ld d, h
  501. ld e, l
  502. ld bc, -18
  503. add hl, bc
  504. ld b, $1
  505. call CalcStats
  506. .done
  507. and a
  508. ret