-
Notifications
You must be signed in to change notification settings - Fork 806
/
experience.asm
163 lines (156 loc) · 2.69 KB
/
experience.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
CalcLevel:
ld a, [wTempMonSpecies]
ld [wCurSpecies], a
call GetBaseData
ld d, 1
.next_level
inc d
ld a, d
cp LOW(MAX_LEVEL + 1)
jr z, .got_level
call CalcExpAtLevel
push hl
ld hl, wTempMonExp + 2
ldh a, [hProduct + 3]
ld c, a
ld a, [hld]
sub c
ldh a, [hProduct + 2]
ld c, a
ld a, [hld]
sbc c
ldh a, [hProduct + 1]
ld c, a
ld a, [hl]
sbc c
pop hl
jr nc, .next_level
.got_level
dec d
ret
CalcExpAtLevel:
; (a/b)*n**3 + c*n**2 + d*n - e
; BUG: Experience underflow for level 1 Pokémon with Medium-Slow growth rate (see docs/bugs_and_glitches.md)
ld a, [wBaseGrowthRate]
add a
add a
ld c, a
ld b, 0
ld hl, GrowthRates
add hl, bc
; Cube the level
call .LevelSquared
ld a, d
ldh [hMultiplier], a
call Multiply
; Multiply by a
ld a, [hl]
and $f0
swap a
ldh [hMultiplier], a
call Multiply
; Divide by b
ld a, [hli]
and $f
ldh [hDivisor], a
ld b, 4
call Divide
; Push the cubic term to the stack
ldh a, [hQuotient + 1]
push af
ldh a, [hQuotient + 2]
push af
ldh a, [hQuotient + 3]
push af
; Square the level and multiply by the lower 7 bits of c
call .LevelSquared
ld a, [hl]
and $7f
ldh [hMultiplier], a
call Multiply
; Push the absolute value of the quadratic term to the stack
ldh a, [hProduct + 1]
push af
ldh a, [hProduct + 2]
push af
ldh a, [hProduct + 3]
push af
ld a, [hli]
push af
; Multiply the level by d
xor a
ldh [hMultiplicand + 0], a
ldh [hMultiplicand + 1], a
ld a, d
ldh [hMultiplicand + 2], a
ld a, [hli]
ldh [hMultiplier], a
call Multiply
; Subtract e
ld b, [hl]
ldh a, [hProduct + 3]
sub b
ldh [hMultiplicand + 2], a
ld b, 0
ldh a, [hProduct + 2]
sbc b
ldh [hMultiplicand + 1], a
ldh a, [hProduct + 1]
sbc b
ldh [hMultiplicand], a
; If bit 7 of c is set, c is negative; otherwise, it's positive
pop af
and $80
jr nz, .subtract
; Add c*n**2 to (d*n - e)
pop bc
ldh a, [hProduct + 3]
add b
ldh [hMultiplicand + 2], a
pop bc
ldh a, [hProduct + 2]
adc b
ldh [hMultiplicand + 1], a
pop bc
ldh a, [hProduct + 1]
adc b
ldh [hMultiplicand], a
jr .done_quadratic
.subtract
; Subtract c*n**2 from (d*n - e)
pop bc
ldh a, [hProduct + 3]
sub b
ldh [hMultiplicand + 2], a
pop bc
ldh a, [hProduct + 2]
sbc b
ldh [hMultiplicand + 1], a
pop bc
ldh a, [hProduct + 1]
sbc b
ldh [hMultiplicand], a
.done_quadratic
; Add (a/b)*n**3 to (d*n - e +/- c*n**2)
pop bc
ldh a, [hProduct + 3]
add b
ldh [hMultiplicand + 2], a
pop bc
ldh a, [hProduct + 2]
adc b
ldh [hMultiplicand + 1], a
pop bc
ldh a, [hProduct + 1]
adc b
ldh [hMultiplicand], a
ret
.LevelSquared:
xor a
ldh [hMultiplicand + 0], a
ldh [hMultiplicand + 1], a
ld a, d
ldh [hMultiplicand + 2], a
ldh [hMultiplier], a
jp Multiply
INCLUDE "data/growth_rates.asm"