Prev: 90DB Up: Map Next: 9190
917D: Randomise a fight value
Adds a random amount to the value in B, using 9BFD with a range of 10, and returns the result in A, clamped to 0-255.
The random amount C is a signed number between -10 and +10, but it is heavily biased towards positive values: only about 1 in 25 adjustments is negative, and +1 to +5 are twice as likely as +6 to +10 (see 9BFD).
BUG: the clamping is wrong for negative adjustments. After ADD A,B the code treats the carry flag as 'overflow'. When C is negative (say -3, which is $FD) and B is at least 3, the addition B+$FD always produces a carry, even though the true result (B-3) is perfectly valid. The code then sees that C is negative and returns 0 instead of B-3. So whenever the adjustment is negative, the value collapses to 0. When this happens to a defence value, the next blow is almost certainly fatal; when it happens to an attack value, the blow is wasted. This bug is the reason a bare-handed Bilbo occasionally kills Thorin, and one reason for the game's famous sudden deaths.
Input
B Value to randomise
Output
A Randomised value (0-255)
917D PUSH BC
917E LD B,A B = the value to randomise
917F LD A,$0A A random number from -10 to +10...
9181 CALL $9BFD
9184 LD C,A ...in C
9185 ADD A,B A = value + random number
9186 JR NC,$918E No carry: that is the answer
9188 XOR A Carry: assume it overflowed; A = 0...
9189 BIT 7,C ...is the random number negative?
918B JR NZ,$918E Yes: return 0. (But adding a negative number always sets the carry, so every negative adjustment gives 0: the bug)
918D DEC A Positive: the value really overflowed, so return 255
918E POP BC
918F RET
Prev: 90DB Up: Map Next: 9190