Prev: AF7D Up: Map Next: B6A9
B66C: Random Number
Input
A Maximum value, clamped to bits 0-5
Output
A Random value in that range
Random_Number B66C LD H,A Set the high bound from A.
B66D LD L,$00 Set the low bound to 00.
This entry point is used by the routines at Run_Other_Phase, Initialise_Object_Slots, Clear_Work_Buffers and Update_Sprite_Slots.
Use the range already in HL.
Random_Number_Range B66F LD A,L Fetch the low bound.
B670 AND %00111111 Keep only bits 0-5.
B672 LD L,A Store the low bound.
B673 LD A,H Fetch the high bound.
B674 AND %00111111 Keep only bits 0-5.
B676 CP L Jump to Random_Number_Setup if the high bound is at least the low bound.
B677 JR NC,Random_Number_Setup
B679 LD H,L Copy the low bound to H.
B67A LD L,A Store the smaller value as the low bound.
Random_Number_Setup B67B EX DE,HL Copy the range to DE.
B67C INC D Increment the high bound by one.
B67D LD HL,($B6A9) Fetch *Random_Seed.
B680 LD H,$02 Force the pointer into ROM.
B682 LD A,R Add the R register to the low byte.
B684 ADD A,L
B685 LD L,A
B686 PUSH IX Stash IX on the stack.
B688 LD IX,$B6A9 Point IX at Random_Seed.
Pick a value in range.
Random_Number_Pick B68C LD A,(HL) Mix the ROM byte with *Random_Seed.
B68D XOR (IX+$00)
B690 AND %00111111 Keep only bits 0-5.
B692 INC L Advance the ROM pointer.
B693 CP E Jump to Random_Number_Pick if the value is less than the low bound.
B694 JR C,Random_Number_Pick
B696 CP D Jump to Random_Number_Pick if the value is not less than the high bound.
B697 JR NC,Random_Number_Pick
B699 LD ($B6A9),HL Write the ROM pointer to *Random_Seed.
B69C POP IX Restore IX from the stack.
B69E PUSH AF Stash the random value on the stack.
B69F LD A,($5C6B) Restart if *ATTR_T is not 0D.
B6A2 CP $0D
B6A4 JP NZ,$0000
B6A7 POP AF Restore the random value from the stack.
B6A8 RET Return.
Prev: AF7D Up: Map Next: B6A9