Hi, I would like to preface this by saying while I believe my conclusion is correct, I am not experienced in assembly code, apologies if I am mistaken.
I believe the cart accurate damage calculation in this case is:
Code: (floor(floor(((floor((level * 2) / 5) + 2) * basePower * attack) / defense) / 50) * critModifier + 2) * typeEffectiveness * rollWhich is what Honko has given as the calc formula.
I am using
https://github.com/pret/pokegold and
https://github.com/pret/pokered as the sources for the cartridge code.
The damage calc code for GS is found under pret/pokegold/master/engine/battle/effect_commands.asm
The damage calc for RB is found under pret/pokered/master/engine/battle/core.asm
The damage calculation formula is virtually the same between RB and GS, the key differences are how critical hits are handled and the inclusion of items in GS. GS also has some minor code to prevent the overflowing of levels.
This is the shared code between Gen 1 and Gen 2: (Starting from line 4435 in RB and line 2962 in GS)
A lot of Code Code: ; Don't calculate damage for moves that don't do any.
ld a, d ; base power
and a
ret z
.skipbp
xor a
ld hl, hDividend
ldi [hl], a
ldi [hl], a
ld [hl], a
; Multiply level by 2
ld a, e ; level
add a
jr nc, .nc
push af
ld a, 1
ld [hl], a
pop af
.nc
inc hl
ldi [hl], a
; Divide by 5
ld a, 5
ldd [hl], a
push bc
ld b, 4
call Divide
pop bc
; Add 2
inc [hl]
inc [hl]
inc hl ; multiplier
; Multiply by attack base power
ld [hl], d
call Multiply
; Multiply by attack stat
ld [hl], b
call Multiply
; Divide by defender's defense stat
ld [hl], c
ld b, 4
call Divide
; Divide by 50
ld [hl], 50
ld b, 4
call Divide
; Update wCurDamage.
; Capped at MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE: 999 - 2 = 997.
ld hl, wDamage
ld b, [hl]
ldh a, [hQuotient + 3]
add b
ldh [hQuotient + 3], a
jr nc, .dont_cap_1
ldh a, [hQuotient + 2]
inc a
ldh [hQuotient + 2], a
and a
jr z, .cap
.dont_cap_1
ldh a, [hQuotient]
ld b, a
ldh a, [hQuotient + 1]
or a
jr nz, .cap
ldh a, [hQuotient + 2]
cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_2
cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) + 1
jr nc, .cap
ldh a, [hQuotient + 3]
cp LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr nc, .cap
.dont_cap_2
inc hl
ldh a, [hQuotient + 3]
ld b, [hl]
add b
ld [hld], a
ldh a, [hQuotient + 2]
ld b, [hl]
adc b
ld [hl], a
jr c, .cap
ld a, [hl]
cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_3
cp HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1) + 1
jr nc, .cap
inc hl
ld a, [hld]
cp LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE + 1)
jr c, .dont_cap_3
.cap
ld a, HIGH(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE)
ld [hli], a
ld a, LOW(MAX_NEUTRAL_DAMAGE - MIN_NEUTRAL_DAMAGE)
ld [hld], a
.dont_cap_3
; Add back MIN_NEUTRAL_DAMAGE (capping at 999).
inc hl
ld a, [hl]
add MIN_NEUTRAL_DAMAGE
ld [hld], a
jr nc, .dont_floor
inc [hl]
.dont_floor
; Returns nz and nc.
ld a, 1
and a
ret
This essentially covers the (floor(floor(((floor((level * 2) / 5) + 2) * basePower * attack) / defense) / 50) + 2) section of the damage function.
Critical hit modifiers for RB are handled on line 4237 of pret/pokered/master/engine/battle/core.asm and is as follows:
Code: ld a, [wCriticalHitOrOHKO]
and a ; check for critical hit
jr z, .done
sla e ; double level if it was a critical hitThis occurs before the damage calculation code is executed and doubles the level 'e' by shifting the binary value one digit to the left. (SLA = Shift left arithmetic)
Critical hit modifiers for GS are handled on line 3151 of pret/pokegold/master/engine/battle/effect_commands.asm as:
Code: .CriticalMultiplier:
ld a, [wCriticalHit]
and a
ret z
; x2
ldh a, [hQuotient + 3]
sla a
ldh [hQuotient + 3], a
ldh a, [hQuotient + 2]
rl a
ldh [hQuotient + 2], aThis .CriticalMultiplier function is called on line 3061, after the /50 operation but before the +2 of the damage capping operations and doubles the running total damage value 'a'. The doubling of the level variable 'e' to my knowledge does not occur in the GS code.
What gives me cause for concern, and makes me think I could be incorrect is that the function for determining the damage boosts received from items (i.e. Magnet, Charcoal) is called immediately before the CriticialMultiplier function. Line 3021:
Code: ; Item boosts
call GetUserItem
ld a, b
and a
jr z, .DoneItem
ld hl, TypeBoostItems
.NextItem:
ld a, [hli]
cp -1
jr z, .DoneItem
; Item effect
cp b
ld a, [hli]
jr nz, .NextItem
; Type
ld b, a
ld a, BATTLE_VARS_MOVE_TYPE
call GetBattleVar
cp b
jr nz, .DoneItem
; * 100 + item effect amount
ld a, c
add 100
ldh [hMultiplier], a
call Multiply
; / 100
ld a, 100
ldh [hDivisor], a
ld b, 4
call DivideThis code finds what item the user is holding, checks its effect, compares if the move selected benefits from the boosting item, then applies an item multiplier to 'a'.
This leads me to believe that the true damage formula is:
Code: (floor(floor(((floor((level * 2) / 5) + 2) * basePower * attack) / defense) / 50) * critModifier * Item boost + 2) * ModifiersNot that it made any difference in the SPL case, but I would like someone to confirm this as it is a minor nerf to the type boosting items, dropping damage by 1 or 2 HP.
I can say with some degree of certainty that in GSC games the PS! calc inaccurately doubles the user's level when calculating critical hits instead of applying the multiplier later.
Please let me know if you spot any errors in this analysis.