Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/pokemon/experience.asm
1271 views
1
; calculates the level a mon should be based on its current exp
2
CalcLevelFromExperience::
3
ld a, [wLoadedMonSpecies]
4
ld [wCurSpecies], 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
ldh a, [hExperience + 2]
14
ld c, a
15
ld a, [hld]
16
sub c
17
ldh a, [hExperience + 1]
18
ld c, a
19
ld a, [hld]
20
sbc c
21
ldh 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
30
; calculates the amount of experience needed for level d
31
CalcExperience::
32
ld a, [wMonHGrowthRate]
33
add a
34
add a
35
ld c, a
36
ld b, 0
37
ld hl, GrowthRateTable
38
add hl, bc
39
call CalcDSquared
40
ld a, d
41
ldh [hMultiplier], a
42
call Multiply
43
ld a, [hl]
44
and $f0
45
swap a
46
ldh [hMultiplier], a
47
call Multiply
48
ld a, [hli]
49
and $f
50
ldh [hDivisor], a
51
ld b, $4
52
call Divide
53
ldh a, [hQuotient + 1]
54
push af
55
ldh a, [hQuotient + 2]
56
push af
57
ldh a, [hQuotient + 3]
58
push af
59
call CalcDSquared
60
ld a, [hl]
61
and $7f
62
ldh [hMultiplier], a
63
call Multiply
64
ldh a, [hProduct + 1]
65
push af
66
ldh a, [hProduct + 2]
67
push af
68
ldh a, [hProduct + 3]
69
push af
70
ld a, [hli]
71
push af
72
xor a
73
ldh [hMultiplicand], a
74
ldh [hMultiplicand + 1], a
75
ld a, d
76
ldh [hMultiplicand + 2], a
77
ld a, [hli]
78
ldh [hMultiplier], a
79
call Multiply
80
ld b, [hl]
81
ldh a, [hProduct + 3]
82
sub b
83
ldh [hProduct + 3], a
84
ld b, $0
85
ldh a, [hProduct + 2]
86
sbc b
87
ldh [hProduct + 2], a
88
ldh a, [hProduct + 1]
89
sbc b
90
ldh [hProduct + 1], a
91
; The difference of the linear term and the constant term consists of 3 bytes
92
; starting at hProduct + 1. Below, hExperience (an alias of that address) will
93
; be used instead for the further work of adding or subtracting the squared
94
; term and adding the cubed term.
95
pop af
96
and $80
97
jr nz, .subtractSquaredTerm ; check sign
98
pop bc
99
ldh a, [hExperience + 2]
100
add b
101
ldh [hExperience + 2], a
102
pop bc
103
ldh a, [hExperience + 1]
104
adc b
105
ldh [hExperience + 1], a
106
pop bc
107
ldh a, [hExperience]
108
adc b
109
ldh [hExperience], a
110
jr .addCubedTerm
111
.subtractSquaredTerm
112
pop bc
113
ldh a, [hExperience + 2]
114
sub b
115
ldh [hExperience + 2], a
116
pop bc
117
ldh a, [hExperience + 1]
118
sbc b
119
ldh [hExperience + 1], a
120
pop bc
121
ldh a, [hExperience]
122
sbc b
123
ldh [hExperience], a
124
.addCubedTerm
125
pop bc
126
ldh a, [hExperience + 2]
127
add b
128
ldh [hExperience + 2], a
129
pop bc
130
ldh a, [hExperience + 1]
131
adc b
132
ldh [hExperience + 1], a
133
pop bc
134
ldh a, [hExperience]
135
adc b
136
ldh [hExperience], a
137
ret
138
139
; calculates d*d
140
CalcDSquared:
141
xor a
142
ldh [hMultiplicand], a
143
ldh [hMultiplicand + 1], a
144
ld a, d
145
ldh [hMultiplicand + 2], a
146
ldh [hMultiplier], a
147
jp Multiply
148
149
INCLUDE "data/growth_rates.asm"
150
151