pokered/engine/battle/experience.asm

373 lines
7.2 KiB
NASM
Raw Normal View History

2014-05-22 22:13:20 +00:00
GainExperience: ; 5524f (15:524f)
2015-02-07 10:43:08 +00:00
ld a, [wLinkState]
cp LINK_STATE_BATTLING
2014-08-09 05:39:13 +00:00
ret z ; return if link battle
call DivideExpDataByNumMonsGainingExp
ld hl, wPartyMon1
2014-05-22 22:13:20 +00:00
xor a
ld [wWhichPokemon], a
2014-08-09 05:39:13 +00:00
.partyMonLoop ; loop over each mon and add gained exp
2014-05-22 22:13:20 +00:00
inc hl
ld a, [hli]
2014-08-09 05:39:13 +00:00
or [hl] ; is mon's HP 0?
jp z, .nextMon ; if so, go to next mon
2014-05-22 22:13:20 +00:00
push hl
2014-08-09 05:39:13 +00:00
ld hl, wPartyGainExpFlags
ld a, [wWhichPokemon]
2014-05-22 22:13:20 +00:00
ld c, a
ld b, $2
predef FlagActionPredef
2014-05-22 22:13:20 +00:00
ld a, c
2014-08-09 05:39:13 +00:00
and a ; is mon's gain exp flag set?
2014-05-22 22:13:20 +00:00
pop hl
2014-08-09 05:39:13 +00:00
jp z, .nextMon ; if mon's gain exp flag not set, go to next mon
ld de, (wPartyMon1HPExp + 1) - (wPartyMon1HP + 1)
2014-05-22 22:13:20 +00:00
add hl, de
ld d, h
ld e, l
2014-08-09 05:39:13 +00:00
ld hl, wEnemyMonBaseStats
2014-05-22 22:13:20 +00:00
ld c, $5
2014-08-09 05:39:13 +00:00
.gainStatExpLoop
2014-05-22 22:13:20 +00:00
ld a, [hli]
2014-08-09 05:39:13 +00:00
ld b, a ; enemy mon base stat
ld a, [de] ; stat exp
add b ; add enemy mon base state to stat exp
2014-05-22 22:13:20 +00:00
ld [de], a
2014-08-09 05:39:13 +00:00
jr nc, .nextBaseStat
; if there was a carry, increment the upper byte
2014-05-22 22:13:20 +00:00
dec de
ld a, [de]
inc a
2014-08-09 05:39:13 +00:00
jr z, .maxStatExp ; jump if the value overflowed
2014-05-22 22:13:20 +00:00
ld [de], a
inc de
2014-08-09 05:39:13 +00:00
jr .nextBaseStat
.maxStatExp ; if the upper byte also overflowed, then we have hit the max stat exp
2014-05-22 22:13:20 +00:00
ld a, $ff
ld [de], a
inc de
ld [de], a
2014-08-09 05:39:13 +00:00
.nextBaseStat
2014-05-22 22:13:20 +00:00
dec c
jr z, .asm_552a1
inc de
inc de
2014-08-09 05:39:13 +00:00
jr .gainStatExpLoop
2014-05-22 22:13:20 +00:00
.asm_552a1
xor a
ld [H_MULTIPLICAND], a
ld [H_MULTIPLICAND + 1], a
2014-08-09 05:39:13 +00:00
ld a, [wEnemyMonBaseExp]
ld [H_MULTIPLICAND + 2], a
ld a, [wEnemyMonLevel]
ld [H_MULTIPLIER], a
2014-05-22 22:13:20 +00:00
call Multiply
ld a, 7
ld [H_DIVISOR], a
ld b, 4
2014-05-22 22:13:20 +00:00
call Divide
2014-08-09 05:39:13 +00:00
ld hl, -((wPartyMon1HPExp + 1) - wPartyMon1OTID + 4 * 2)
2014-05-22 22:13:20 +00:00
add hl, de
2014-08-09 05:39:13 +00:00
ld b, [hl] ; party mon OTID
2014-05-22 22:13:20 +00:00
inc hl
ld a, [wPlayerID]
2014-05-22 22:13:20 +00:00
cp b
2014-08-09 05:39:13 +00:00
jr nz, .tradedMon
2014-05-22 22:13:20 +00:00
ld b, [hl]
ld a, [wPlayerID + 1]
2014-05-22 22:13:20 +00:00
cp b
ld a, $0
2014-08-09 05:39:13 +00:00
jr z, .next
.tradedMon
call BoostExp ; traded mon exp boost
2014-05-22 22:13:20 +00:00
ld a, $1
2014-08-09 05:39:13 +00:00
.next
2015-03-31 18:28:42 +00:00
ld [wGainBoostedExp], a
ld a, [W_ISINBATTLE]
2014-08-09 05:39:13 +00:00
dec a ; is it a trainer battle?
call nz, BoostExp ; if so, boost exp
2014-05-22 22:13:20 +00:00
inc hl
inc hl
inc hl
2014-08-09 05:39:13 +00:00
; add the gained exp to the party mon's exp
2014-05-22 22:13:20 +00:00
ld b, [hl]
2014-08-09 05:39:13 +00:00
ld a, [H_QUOTIENT + 3]
ld [wcf4c], a
2014-05-22 22:13:20 +00:00
add b
ld [hld], a
ld b, [hl]
2014-08-09 05:39:13 +00:00
ld a, [H_QUOTIENT + 2]
ld [wcf4b], a
2014-05-22 22:13:20 +00:00
adc b
ld [hl], a
2014-08-09 05:39:13 +00:00
jr nc, .noCarry
2014-05-22 22:13:20 +00:00
dec hl
inc [hl]
inc hl
2014-08-09 05:39:13 +00:00
.noCarry
; calculate exp for the mon at max level, and cap the exp at that value
2014-05-22 22:13:20 +00:00
inc hl
push hl
ld a, [wWhichPokemon]
2014-05-22 22:13:20 +00:00
ld c, a
ld b, 0
ld hl, wPartySpecies
2014-05-22 22:13:20 +00:00
add hl, bc
2014-08-09 05:39:13 +00:00
ld a, [hl] ; species
ld [wd0b5], a
2014-05-22 22:13:20 +00:00
call GetMonHeader
2014-06-10 21:06:30 +00:00
ld d, MAX_LEVEL
2014-08-09 05:39:13 +00:00
callab CalcExperience ; get max exp
; compare max exp with current exp
ld a, [$ff96]
2014-05-22 22:13:20 +00:00
ld b, a
ld a, [$ff97]
ld c, a
ld a, [$ff98]
ld d, a
pop hl
ld a, [hld]
sub d
ld a, [hld]
sbc c
ld a, [hl]
sbc b
2014-08-09 05:39:13 +00:00
jr c, .next2
; the mon's exp is greater than the max exp, so overwrite it with the max exp
2014-05-22 22:13:20 +00:00
ld a, b
ld [hli], a
ld a, c
ld [hli], a
ld a, d
ld [hld], a
dec hl
2014-08-09 05:39:13 +00:00
.next2
2014-05-22 22:13:20 +00:00
push hl
ld a, [wWhichPokemon]
ld hl, wPartyMonNicks
2014-05-22 22:13:20 +00:00
call GetPartyMonName
ld hl, GainedText
call PrintText
2014-08-09 05:39:13 +00:00
xor a ; party mon data
ld [wcc49], a
2014-05-22 22:13:20 +00:00
call LoadMonData
pop hl
2014-08-09 05:39:13 +00:00
ld bc, wPartyMon1Level - wPartyMon1Exp
2014-05-22 22:13:20 +00:00
add hl, bc
push hl
2014-08-09 05:39:13 +00:00
callba CalcLevelFromExperience
2014-05-22 22:13:20 +00:00
pop hl
2014-08-09 05:39:13 +00:00
ld a, [hl] ; current level
2014-05-22 22:13:20 +00:00
cp d
2014-08-09 05:39:13 +00:00
jp z, .nextMon ; if level didn't change, go to next mon
ld a, [W_CURENEMYLVL]
2014-05-22 22:13:20 +00:00
push af
push hl
ld a, d
ld [W_CURENEMYLVL], a
2014-05-22 22:13:20 +00:00
ld [hl], a
2014-08-09 05:39:13 +00:00
ld bc, wPartyMon1Species - wPartyMon1Level
2014-05-22 22:13:20 +00:00
add hl, bc
2014-08-09 05:39:13 +00:00
ld a, [hl] ; species
ld [wd0b5], a
ld [wd11e], a
2014-05-22 22:13:20 +00:00
call GetMonHeader
2014-08-09 05:39:13 +00:00
ld bc, (wPartyMon1MaxHP + 1) - wPartyMon1Species
2014-05-22 22:13:20 +00:00
add hl, bc
push hl
ld a, [hld]
ld c, a
ld b, [hl]
2014-08-09 05:39:13 +00:00
push bc ; push max HP (from before levelling up)
2014-05-22 22:13:20 +00:00
ld d, h
ld e, l
2014-08-09 05:39:13 +00:00
ld bc, (wPartyMon1HPExp - 1) - wPartyMon1MaxHP
2014-05-22 22:13:20 +00:00
add hl, bc
2014-08-09 05:39:13 +00:00
ld b, $1 ; consider stat exp when calculating stats
2014-05-22 22:13:20 +00:00
call CalcStats
2014-08-09 05:39:13 +00:00
pop bc ; pop max HP (from before levelling up)
2014-05-22 22:13:20 +00:00
pop hl
ld a, [hld]
sub c
ld c, a
ld a, [hl]
sbc b
2014-08-09 05:39:13 +00:00
ld b, a ; bc = difference between old max HP and new max HP after levelling
ld de, (wPartyMon1HP + 1) - wPartyMon1MaxHP
2014-05-22 22:13:20 +00:00
add hl, de
2014-08-09 05:39:13 +00:00
; add to the current HP the amount of max HP gained when levelling
ld a, [hl] ; wPartyMon1HP + 1
2014-05-22 22:13:20 +00:00
add c
ld [hld], a
2014-08-09 05:39:13 +00:00
ld a, [hl] ; wPartyMon1HP + 1
2014-05-22 22:13:20 +00:00
adc b
2014-08-09 05:39:13 +00:00
ld [hl], a ; wPartyMon1HP
ld a, [wPlayerMonNumber]
2014-05-22 22:13:20 +00:00
ld b, a
ld a, [wWhichPokemon]
2014-08-09 05:39:13 +00:00
cp b ; is the current mon in battle?
jr nz, .printGrewLevelText
; current mon is in battle
ld de, wBattleMonHP
2014-08-09 05:39:13 +00:00
; copy party mon HP to battle mon HP
2014-05-22 22:13:20 +00:00
ld a, [hli]
ld [de], a
inc de
ld a, [hl]
ld [de], a
2014-08-09 05:39:13 +00:00
; copy other stats from party mon to battle mon
ld bc, wPartyMon1Level - (wPartyMon1HP + 1)
2014-05-22 22:13:20 +00:00
add hl, bc
push hl
2014-08-09 05:39:13 +00:00
ld de, wBattleMonLevel
ld bc, $b ; size of stats
2014-05-22 22:13:20 +00:00
call CopyData
pop hl
2014-08-09 05:39:13 +00:00
ld a, [W_PLAYERBATTSTATUS3]
bit 3, a ; is the mon transformed?
jr nz, .recalcStatChanges
2015-02-07 10:43:08 +00:00
; the mon is not transformed, so update the unmodified stats
ld de, wPlayerMonUnmodifiedLevel
2014-05-22 22:13:20 +00:00
ld bc, $b
call CopyData
2014-08-09 05:39:13 +00:00
.recalcStatChanges
2014-05-22 22:13:20 +00:00
xor a
ld [wd11e], a
2014-08-09 05:39:13 +00:00
callab CalculateModifiedStats
callab ApplyBurnAndParalysisPenaltiesToPlayer
callab ApplyBadgeStatBoosts
callab DrawPlayerHUDAndHPBar
callab PrintEmptyString
2014-05-22 22:13:20 +00:00
call SaveScreenTilesToBuffer1
2014-08-09 05:39:13 +00:00
.printGrewLevelText
2014-05-22 22:13:20 +00:00
ld hl, GrewLevelText
call PrintText
2014-08-09 05:39:13 +00:00
xor a ; party mon data
ld [wcc49], a
2014-05-22 22:13:20 +00:00
call LoadMonData
ld d, $1
callab PrintStatsBox
call WaitForTextScrollButtonPress
call LoadScreenTilesFromBuffer1
xor a
ld [wcc49], a
ld a, [wd0b5]
ld [wd11e], a
2014-08-09 05:39:13 +00:00
predef LearnMoveFromLevelUp
ld hl, wccd3
2014-08-09 05:39:13 +00:00
ld a, [wWhichPokemon]
2014-05-22 22:13:20 +00:00
ld c, a
ld b, $1
predef FlagActionPredef
2014-05-22 22:13:20 +00:00
pop hl
pop af
2014-08-09 05:39:13 +00:00
ld [W_CURENEMYLVL], a
2014-05-22 22:13:20 +00:00
2014-08-09 05:39:13 +00:00
.nextMon
ld a, [wPartyCount]
2014-05-22 22:13:20 +00:00
ld b, a
2014-08-09 05:39:13 +00:00
ld a, [wWhichPokemon]
2014-05-22 22:13:20 +00:00
inc a
cp b
2014-08-09 05:39:13 +00:00
jr z, .done
ld [wWhichPokemon], a
ld bc, wPartyMon2 - wPartyMon1
ld hl, wPartyMon1
2014-05-22 22:13:20 +00:00
call AddNTimes
2014-08-09 05:39:13 +00:00
jp .partyMonLoop
.done
ld hl, wPartyGainExpFlags
2014-05-22 22:13:20 +00:00
xor a
2014-08-09 05:39:13 +00:00
ld [hl], a ; clear gain exp flags
ld a, [wPlayerMonNumber]
2014-05-22 22:13:20 +00:00
ld c, a
ld b, $1
push bc
2014-08-09 05:39:13 +00:00
predef FlagActionPredef ; set the gain exp flag for the mon that is currently out
ld hl, wPartyFoughtCurrentEnemyFlags
2014-05-22 22:13:20 +00:00
xor a
ld [hl], a
pop bc
2014-08-09 05:39:13 +00:00
predef_jump FlagActionPredef ; set the fought current enemy flag for the mon that is currently out
2014-05-22 22:13:20 +00:00
2014-08-09 05:39:13 +00:00
; divide enemy base stats, catch rate, and base exp by the number of mons gaining exp
DivideExpDataByNumMonsGainingExp: ; 5546c (15:546c)
ld a, [wPartyGainExpFlags]
2014-05-22 22:13:20 +00:00
ld b, a
xor a
ld c, $8
ld d, $0
2014-08-09 05:39:13 +00:00
.countSetBitsLoop ; loop to count set bits in wPartyGainExpFlags
2014-05-22 22:13:20 +00:00
xor a
srl b
adc d
ld d, a
dec c
2014-08-09 05:39:13 +00:00
jr nz, .countSetBitsLoop
2014-05-22 22:13:20 +00:00
cp $2
2014-08-09 05:39:13 +00:00
ret c ; return if only one mon is gaining exp
ld [wd11e], a ; store number of mons gaining exp
ld hl, wEnemyMonBaseStats
2014-05-22 22:13:20 +00:00
ld c, $7
2014-08-09 05:39:13 +00:00
.divideLoop
2014-05-22 22:13:20 +00:00
xor a
2014-08-09 05:39:13 +00:00
ld [H_DIVIDEND], a
2014-05-22 22:13:20 +00:00
ld a, [hl]
2014-08-09 05:39:13 +00:00
ld [H_DIVIDEND + 1], a
ld a, [wd11e]
2014-08-09 05:39:13 +00:00
ld [H_DIVISOR], a
2014-05-22 22:13:20 +00:00
ld b, $2
2014-08-09 05:39:13 +00:00
call Divide ; divide value by number of mons gaining exp
ld a, [H_QUOTIENT + 3]
2014-05-22 22:13:20 +00:00
ld [hli], a
dec c
2014-08-09 05:39:13 +00:00
jr nz, .divideLoop
2014-05-22 22:13:20 +00:00
ret
2014-08-09 05:39:13 +00:00
; multiplies exp by 1.5
BoostExp: ; 5549f (15:549f)
ld a, [H_QUOTIENT + 2]
2014-05-22 22:13:20 +00:00
ld b, a
2014-08-09 05:39:13 +00:00
ld a, [H_QUOTIENT + 3]
2014-05-22 22:13:20 +00:00
ld c, a
srl b
rr c
add c
2014-08-09 05:39:13 +00:00
ld [H_QUOTIENT + 3], a
ld a, [H_QUOTIENT + 2]
2014-05-22 22:13:20 +00:00
adc b
2014-08-09 05:39:13 +00:00
ld [H_QUOTIENT + 2], a
2014-05-22 22:13:20 +00:00
ret
GainedText: ; 554b2 (15:54b2)
TX_FAR _GainedText
2015-07-03 19:58:50 +00:00
TX_ASM
2015-03-31 18:28:42 +00:00
ld a, [wBoostExpByExpAll]
2014-05-22 22:13:20 +00:00
ld hl, WithExpAllText
and a
ret nz
ld hl, ExpPointsText
2015-03-31 18:28:42 +00:00
ld a, [wGainBoostedExp]
2014-05-22 22:13:20 +00:00
and a
ret z
ld hl, BoostedText
ret
WithExpAllText: ; 554cb (15:54cb)
TX_FAR _WithExpAllText
2015-07-03 19:58:50 +00:00
TX_ASM
2014-05-22 22:13:20 +00:00
ld hl, ExpPointsText
ret
BoostedText: ; 554d4 (15:54d4)
TX_FAR _BoostedText
ExpPointsText: ; 554d8 (15:54d8)
TX_FAR _ExpPointsText
db "@"
GrewLevelText: ; 554dd (15:54dd)
TX_FAR _GrewLevelText
db $0b
db "@"