pokered/engine/battle/experience.asm

373 lines
7.2 KiB
NASM
Raw Normal View History

2016-06-12 00:24:04 +00:00
GainExperience:
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
2015-07-20 03:45:34 +00:00
ld b, FLAG_TEST
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
ld c, NUM_STATS
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
2016-06-12 04:30:05 +00:00
jr z, .statExpDone
2014-05-22 22:13:20 +00:00
inc de
inc de
2014-08-09 05:39:13 +00:00
jr .gainStatExpLoop
2016-06-12 04:30:05 +00:00
.statExpDone
2014-05-22 22:13:20 +00:00
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
ld hl, wPartyMon1OTID - (wPartyMon1DVs - 1)
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
2015-08-13 05:14:31 +00:00
ld a, 0
2014-08-09 05:39:13 +00:00
jr z, .next
.tradedMon
call BoostExp ; traded mon exp boost
2015-08-13 05:14:31 +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
2015-08-31 02:38:41 +00:00
ld a, [wIsInBattle]
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]
2015-08-13 05:14:31 +00:00
ld [wExpAmountGained + 1], 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]
2015-08-13 05:14:31 +00:00
ld [wExpAmountGained], 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
2015-07-16 03:04:58 +00:00
xor a ; PLAYER_PARTY_DATA
ld [wMonDataLocation], 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
2015-08-31 02:38:41 +00:00
ld a, [wCurEnemyLVL]
2014-05-22 22:13:20 +00:00
push af
push hl
ld a, d
2015-08-31 02:38:41 +00:00
ld [wCurEnemyLVL], 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, 1 + NUM_STATS * 2 ; size of stats
2014-05-22 22:13:20 +00:00
call CopyData
pop hl
2015-08-31 02:38:41 +00:00
ld a, [wPlayerBattleStatus3]
2014-08-09 05:39:13 +00:00
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
ld bc, 1 + NUM_STATS * 2
2014-05-22 22:13:20 +00:00
call CopyData
2014-08-09 05:39:13 +00:00
.recalcStatChanges
2015-08-07 12:20:37 +00:00
xor a ; battle mon
ld [wCalculateWhoseStats], 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
2015-07-16 03:04:58 +00:00
xor a ; PLAYER_PARTY_DATA
ld [wMonDataLocation], a
2014-05-22 22:13:20 +00:00
call LoadMonData
ld d, $1
callab PrintStatsBox
call WaitForTextScrollButtonPress
call LoadScreenTilesFromBuffer1
2015-07-16 03:04:58 +00:00
xor a ; PLAYER_PARTY_DATA
ld [wMonDataLocation], a
ld a, [wd0b5]
ld [wd11e], a
2014-08-09 05:39:13 +00:00
predef LearnMoveFromLevelUp
2015-07-19 03:49:52 +00:00
ld hl, wCanEvolveFlags
2014-08-09 05:39:13 +00:00
ld a, [wWhichPokemon]
2014-05-22 22:13:20 +00:00
ld c, a
2015-07-20 03:45:34 +00:00
ld b, FLAG_SET
predef FlagActionPredef
2014-05-22 22:13:20 +00:00
pop hl
pop af
2015-08-31 02:38:41 +00:00
ld [wCurEnemyLVL], 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
2015-07-20 03:45:34 +00:00
ld b, FLAG_SET
2014-05-22 22:13:20 +00:00
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
2016-06-12 00:24:04 +00:00
DivideExpDataByNumMonsGainingExp:
2014-08-09 05:39:13 +00:00
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
ld c, wEnemyMonBaseExp + 1 - wEnemyMonBaseStats
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
2016-06-12 00:24:04 +00:00
BoostExp:
2014-08-09 05:39:13 +00:00
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
2016-06-12 00:24:04 +00:00
GainedText:
2014-05-22 22:13:20 +00:00
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
2016-06-12 00:24:04 +00:00
WithExpAllText:
2014-05-22 22:13:20 +00:00
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
2016-06-12 00:24:04 +00:00
BoostedText:
2014-05-22 22:13:20 +00:00
TX_FAR _BoostedText
2016-06-12 00:24:04 +00:00
ExpPointsText:
2014-05-22 22:13:20 +00:00
TX_FAR _ExpPointsText
db "@"
2016-06-12 00:24:04 +00:00
GrewLevelText:
2014-05-22 22:13:20 +00:00
TX_FAR _GrewLevelText
2016-07-18 06:17:03 +00:00
TX_SFX_LEVEL_UP
2014-05-22 22:13:20 +00:00
db "@"