Prev: D2A4 Up: Map Next: D2ED
D2AD: Scale The Roll
Used by the routine at GetRandomNumber.
Mixes the seed at A037 with the counter at A039 and scales A down to the limit in C.
ScaleTheRoll D2AD PUSH IX Keep the record and the count, copy the roll into C, and go to ScaleTheRoll_Advance if bit 7 is clear.
D2AF PUSH BC
D2B0 LD C,A
D2B1 SLA A
D2B3 JR NC,ScaleTheRoll_Advance
D2B5 LD A,$FF Take FF for the count.
ScaleTheRoll_Advance D2B7 LD B,A Use the value in A as the count.
ScaleTheRoll_Count D2B8 LD IX,$A039 Jump to ScaleTheRoll_ObjectAttributeFlag if the count has not reached zero.
D2BC INC (IX+$01)
D2BF JR NZ,ScaleTheRoll_ObjectAttributeFlag
D2C1 INC (IX+$00) Add one to the high byte of the counter at A039.
ScaleTheRoll_ObjectAttributeFlag D2C4 LD IX,($A039) Point IX at A039 and read the random seed (A037).
D2C8 LD A,($A037)
D2CB ADC A,(IX+$00) Add the seed byte.
D2CE ADD IX,DE Add the offset to the seed pointer, mix in the next seed byte and keep the pointer.
D2D0 XOR (IX+$01)
D2D3 PUSH HL
D2D4 LD HL,$A037 Use the random seed (A037).
D2D7 CP (HL) Jump back to ScaleTheRoll_Count if the mixed seed matches the seed at A037.
D2D8 POP HL
D2D9 JR Z,ScaleTheRoll_Count
D2DB LD ($A037),A Record the random seed.
ScaleTheRoll_Loop D2DE CP B Jump ahead to ScaleTheRoll_Continue if the first value is below the second.
D2DF JR C,ScaleTheRoll_Continue
D2E1 JR Z,ScaleTheRoll_Continue Go to ScaleTheRoll_Continue if the first value equals the second.
D2E3 SRL A Shift the roll down and test it again from ScaleTheRoll_Loop.
D2E5 JP ScaleTheRoll_Loop
Return.
ScaleTheRoll_Continue D2E8 SUB C Take away the other byte.
D2E9 POP BC Restore the saved count and the saved character record from the stack.
D2EA POP IX
D2EC RET Return.
View the equivalent code in;
Prev: D2A4 Up: Map Next: D2ED