Uses 35 bytes of free space at C2/6780(Should be safe) and assumes a headerless rom.
Fixed.

a couple other issues with it (i haven't fully digested the code, so correct me if i'm wrong):
1) capping damage at 65535 before any of the modifications in the function that can further reduce damage (i.e. Genji Glove and Offering) means that when we're done with this function, we won't necessarily have made full use of our potential 0-65535 raw damage range. this can result in problems of too-low damage against high-Defense targets.
suppose we: cap a huge damage value of 100000 to 65535, reduce it to ~24575 since the attacker has both Genji Glove and Offering. the target has a high Defense of 192, so we reduce that damage by another 3/4, giving us ~6143 final damage (before randomization, anyway).
if we had waited until the END of this physical damage function to cap the 100000, we'd get ~37500 damage. then after Defense modifications, it'd become 9375. that's
far away from the 6143.
now, you're capping at 32767, which means everything i've said above with the 65535 cap can only wind up worse in accuracy.
this is why my-and-Lenophis' patch in progress keeps the running damage at 24-bits for as much of the physical damage function as possible, waiting until the end of the math to cap. yes, the code is big and ugly. :/ but arguably worth it.
2) let's look at your C2/6790 (ignore the different addresses, i Tracer dumped it):
- Code: Select all
00/8010: 29 00 FF AND #$FF00
00/8013: D0 09 BNE $801E
00/8015: A5 E8 LDA $E8
00/8017: 6D B0 11 ADC $11B0
00/801A: 8D B0 11 STA $11B0
00/801D: 60 RTS
presumably, you don't want the final damage to exceed 16 bits. so if it already has before the addition of $11B0, you branch and cap it rather than adding in $11B0. ah, but what if the running value is
just shy of exceeding 16 bits, and adding $11B0 makes it overflow? your code doesn't account for that.
here's an example.
---- skip to next dotted lines if you don't like sorta abstract stuff. -----
when multiplying multi-byte values, i like to pretend they're multi
-digit decimal values to simplify things so i can check it on paper. in this case, we start with a 16-bit (2 byte) Attack and an 8-bit (1 byte) Level, and our running product is 24-bits (3 bytes). so let's use example input numbers of 2 digits and 1 digit in size, producing a 3-digit product.
let 2-digit Attack = 28
let 1-digit Level = 6
3-digit $e8 - $ea = 28 * 6 = 168
bottom digit(8) * 6 = 48
$11b0 = 4
3-digit $e8 - $ea = 16 * 6 = 96
96 + $11B0 = 96 + 4 = 100 (uh oh!)
-------------------
ok, an example involving actual bytes that screws C2/6790 are Attack of 1784 and Level of 97. now, one obviously can't get Attack that high without hacking Level past 99 somehow. i'm not trying to strawman you into submission here... just saying that it'd be worth another overflow check in the end of the function to make it more robust.
