experience.asm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ; calculates the level a mon should be based on its current exp
  2. CalcLevelFromExperience:
  3. ld a, [wLoadedMonSpecies]
  4. ld [wd0b5], a
  5. call GetMonHeader
  6. ld d, $1 ; init level to 1
  7. .loop
  8. inc d ; increment level
  9. call CalcExperience
  10. push hl
  11. ld hl, wLoadedMonExp + 2 ; current exp
  12. ; compare exp needed for level d with current exp
  13. ld a, [hExperience + 2]
  14. ld c, a
  15. ld a, [hld]
  16. sub c
  17. ld a, [hExperience + 1]
  18. ld c, a
  19. ld a, [hld]
  20. sbc c
  21. ld a, [hExperience]
  22. ld c, a
  23. ld a, [hl]
  24. sbc c
  25. pop hl
  26. jr nc, .loop ; if exp needed for level d is not greater than exp, try the next level
  27. dec d ; since the exp was too high on the last loop iteration, go back to the previous value and return
  28. ret
  29. ; calculates the amount of experience needed for level d
  30. CalcExperience:
  31. ld a, [wMonHGrowthRate]
  32. add a
  33. add a
  34. ld c, a
  35. ld b, 0
  36. ld hl, GrowthRateTable
  37. add hl, bc
  38. call CalcDSquared
  39. ld a, d
  40. ld [H_MULTIPLIER], a
  41. call Multiply
  42. ld a, [hl]
  43. and $f0
  44. swap a
  45. ld [H_MULTIPLIER], a
  46. call Multiply
  47. ld a, [hli]
  48. and $f
  49. ld [H_DIVISOR], a
  50. ld b, $4
  51. call Divide
  52. ld a, [H_QUOTIENT + 1]
  53. push af
  54. ld a, [H_QUOTIENT + 2]
  55. push af
  56. ld a, [H_QUOTIENT + 3]
  57. push af
  58. call CalcDSquared
  59. ld a, [hl]
  60. and $7f
  61. ld [H_MULTIPLIER], a
  62. call Multiply
  63. ld a, [H_PRODUCT + 1]
  64. push af
  65. ld a, [H_PRODUCT + 2]
  66. push af
  67. ld a, [H_PRODUCT + 3]
  68. push af
  69. ld a, [hli]
  70. push af
  71. xor a
  72. ld [H_MULTIPLICAND], a
  73. ld [H_MULTIPLICAND + 1], a
  74. ld a, d
  75. ld [H_MULTIPLICAND + 2], a
  76. ld a, [hli]
  77. ld [H_MULTIPLIER], a
  78. call Multiply
  79. ld b, [hl]
  80. ld a, [H_PRODUCT + 3]
  81. sub b
  82. ld [H_PRODUCT + 3], a
  83. ld b, $0
  84. ld a, [H_PRODUCT + 2]
  85. sbc b
  86. ld [H_PRODUCT + 2], a
  87. ld a, [H_PRODUCT + 1]
  88. sbc b
  89. ld [H_PRODUCT + 1], a
  90. ; The difference of the linear term and the constant term consists of 3 bytes
  91. ; starting at H_PRODUCT + 1. Below, hExperience (an alias of that address) will
  92. ; be used instead for the further work of adding or subtracting the squared
  93. ; term and adding the cubed term.
  94. pop af
  95. and $80
  96. jr nz, .subtractSquaredTerm ; check sign
  97. pop bc
  98. ld a, [hExperience + 2]
  99. add b
  100. ld [hExperience + 2], a
  101. pop bc
  102. ld a, [hExperience + 1]
  103. adc b
  104. ld [hExperience + 1], a
  105. pop bc
  106. ld a, [hExperience]
  107. adc b
  108. ld [hExperience], a
  109. jr .addCubedTerm
  110. .subtractSquaredTerm
  111. pop bc
  112. ld a, [hExperience + 2]
  113. sub b
  114. ld [hExperience + 2], a
  115. pop bc
  116. ld a, [hExperience + 1]
  117. sbc b
  118. ld [hExperience + 1], a
  119. pop bc
  120. ld a, [hExperience]
  121. sbc b
  122. ld [hExperience], a
  123. .addCubedTerm
  124. pop bc
  125. ld a, [hExperience + 2]
  126. add b
  127. ld [hExperience + 2], a
  128. pop bc
  129. ld a, [hExperience + 1]
  130. adc b
  131. ld [hExperience + 1], a
  132. pop bc
  133. ld a, [hExperience]
  134. adc b
  135. ld [hExperience], a
  136. ret
  137. ; calculates d*d
  138. CalcDSquared:
  139. xor a
  140. ld [H_MULTIPLICAND], a
  141. ld [H_MULTIPLICAND + 1], a
  142. ld a, d
  143. ld [H_MULTIPLICAND + 2], a
  144. ld [H_MULTIPLIER], a
  145. jp Multiply
  146. ; each entry has the following scheme:
  147. ; %AAAABBBB %SCCCCCCC %DDDDDDDD %EEEEEEEE
  148. ; resulting in
  149. ; (a*n^3)/b + sign*c*n^2 + d*n - e
  150. ; where sign = -1 <=> S=1
  151. GrowthRateTable:
  152. db $11,$00,$00,$00 ; medium fast n^3
  153. db $34,$0A,$00,$1E ; (unused?) 3/4 n^3 + 10 n^2 - 30
  154. db $34,$14,$00,$46 ; (unused?) 3/4 n^3 + 20 n^2 - 70
  155. db $65,$8F,$64,$8C ; medium slow: 6/5 n^3 - 15 n^2 + 100 n - 140
  156. db $45,$00,$00,$00 ; fast: 4/5 n^3
  157. db $54,$00,$00,$00 ; slow: 5/4 n^3