pokered/engine/experience.asm

150 lines
2.7 KiB
NASM
Raw Normal View History

2014-08-09 05:39:13 +00:00
; calculates the level a mon should be based on its current exp
2016-06-12 00:24:04 +00:00
CalcLevelFromExperience:
2015-02-08 02:37:40 +00:00
ld a, [wLoadedMonSpecies]
ld [wd0b5], a
2014-05-22 22:13:20 +00:00
call GetMonHeader
2014-08-09 05:39:13 +00:00
ld d, $1 ; init level to 1
.loop
inc d ; increment level
2014-05-22 22:13:20 +00:00
call CalcExperience
push hl
2015-02-08 02:37:40 +00:00
ld hl, wLoadedMonExp + 2 ; current exp
2014-08-09 05:39:13 +00:00
; compare exp needed for level d with current exp
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 2]
2014-05-22 22:13:20 +00:00
ld c, a
ld a, [hld]
sub c
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 1]
2014-05-22 22:13:20 +00:00
ld c, a
ld a, [hld]
sbc c
2015-07-19 03:49:52 +00:00
ld a, [hExperience]
2014-05-22 22:13:20 +00:00
ld c, a
ld a, [hl]
sbc c
pop hl
2014-08-09 05:39:13 +00:00
jr nc, .loop ; if exp needed for level d is not greater than exp, try the next level
dec d ; since the exp was too high on the last loop iteration, go back to the previous value and return
2014-05-22 22:13:20 +00:00
ret
; calculates the amount of experience needed for level d
2016-06-12 00:24:04 +00:00
CalcExperience:
2015-08-31 02:38:41 +00:00
ld a, [wMonHGrowthRate]
2014-05-22 22:13:20 +00:00
add a
add a
ld c, a
2015-07-19 03:49:52 +00:00
ld b, 0
2014-05-22 22:13:20 +00:00
ld hl, GrowthRateTable
add hl, bc
call CalcDSquared
ld a, d
ld [H_MULTIPLIER], a
2014-05-22 22:13:20 +00:00
call Multiply
ld a, [hl]
and $f0
swap a
ld [H_MULTIPLIER], a
2014-05-22 22:13:20 +00:00
call Multiply
ld a, [hli]
and $f
ld [H_DIVISOR], a
2014-05-22 22:13:20 +00:00
ld b, $4
call Divide
2015-07-19 03:49:52 +00:00
ld a, [H_QUOTIENT + 1]
2014-05-22 22:13:20 +00:00
push af
2015-07-19 03:49:52 +00:00
ld a, [H_QUOTIENT + 2]
2014-05-22 22:13:20 +00:00
push af
2015-07-19 03:49:52 +00:00
ld a, [H_QUOTIENT + 3]
2014-05-22 22:13:20 +00:00
push af
call CalcDSquared
ld a, [hl]
and $7f
ld [H_MULTIPLIER], a
2014-05-22 22:13:20 +00:00
call Multiply
2015-07-19 03:49:52 +00:00
ld a, [H_PRODUCT + 1]
2014-05-22 22:13:20 +00:00
push af
2015-07-19 03:49:52 +00:00
ld a, [H_PRODUCT + 2]
2014-05-22 22:13:20 +00:00
push af
2015-07-19 03:49:52 +00:00
ld a, [H_PRODUCT + 3]
2014-05-22 22:13:20 +00:00
push af
ld a, [hli]
push af
xor a
ld [H_MULTIPLICAND], a
2015-07-19 03:49:52 +00:00
ld [H_MULTIPLICAND + 1], a
2014-05-22 22:13:20 +00:00
ld a, d
2015-07-19 03:49:52 +00:00
ld [H_MULTIPLICAND + 2], a
2014-05-22 22:13:20 +00:00
ld a, [hli]
ld [H_MULTIPLIER], a
call Multiply
ld b, [hl]
2015-07-19 03:49:52 +00:00
ld a, [H_PRODUCT + 3]
2014-05-22 22:13:20 +00:00
sub b
2015-07-19 03:49:52 +00:00
ld [H_PRODUCT + 3], a
2014-05-22 22:13:20 +00:00
ld b, $0
2015-07-19 03:49:52 +00:00
ld a, [H_PRODUCT + 2]
2014-05-22 22:13:20 +00:00
sbc b
2015-07-19 03:49:52 +00:00
ld [H_PRODUCT + 2], a
ld a, [H_PRODUCT + 1]
2014-05-22 22:13:20 +00:00
sbc b
2015-07-19 03:49:52 +00:00
ld [H_PRODUCT + 1], a
; The difference of the linear term and the constant term consists of 3 bytes
; starting at H_PRODUCT + 1. Below, hExperience (an alias of that address) will
; be used instead for the further work of adding or subtracting the squared
; term and adding the cubed term.
2014-05-22 22:13:20 +00:00
pop af
and $80
jr nz, .subtractSquaredTerm ; check sign
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 2]
2014-05-22 22:13:20 +00:00
add b
2015-07-19 03:49:52 +00:00
ld [hExperience + 2], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 1]
2014-05-22 22:13:20 +00:00
adc b
2015-07-19 03:49:52 +00:00
ld [hExperience + 1], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience]
2014-05-22 22:13:20 +00:00
adc b
2015-07-19 03:49:52 +00:00
ld [hExperience], a
2014-05-22 22:13:20 +00:00
jr .addCubedTerm
.subtractSquaredTerm
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 2]
2014-05-22 22:13:20 +00:00
sub b
2015-07-19 03:49:52 +00:00
ld [hExperience + 2], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 1]
2014-05-22 22:13:20 +00:00
sbc b
2015-07-19 03:49:52 +00:00
ld [hExperience + 1], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience]
2014-05-22 22:13:20 +00:00
sbc b
2015-07-19 03:49:52 +00:00
ld [hExperience], a
2014-05-22 22:13:20 +00:00
.addCubedTerm
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 2]
2014-05-22 22:13:20 +00:00
add b
2015-07-19 03:49:52 +00:00
ld [hExperience + 2], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience + 1]
2014-05-22 22:13:20 +00:00
adc b
2015-07-19 03:49:52 +00:00
ld [hExperience + 1], a
2014-05-22 22:13:20 +00:00
pop bc
2015-07-19 03:49:52 +00:00
ld a, [hExperience]
2014-05-22 22:13:20 +00:00
adc b
2015-07-19 03:49:52 +00:00
ld [hExperience], a
2014-05-22 22:13:20 +00:00
ret
; calculates d*d
2016-06-12 00:24:04 +00:00
CalcDSquared:
2014-05-22 22:13:20 +00:00
xor a
2015-07-19 03:49:52 +00:00
ld [H_MULTIPLICAND], a
ld [H_MULTIPLICAND + 1], a
2014-05-22 22:13:20 +00:00
ld a, d
2015-07-19 03:49:52 +00:00
ld [H_MULTIPLICAND + 2], a
ld [H_MULTIPLIER], a
2014-05-22 22:13:20 +00:00
jp Multiply
INCLUDE "data/growth_rates.asm"