Prev: 9C9F Up: Map Next: 9CE8
9CA8: Random: Scale The Roll
Scales the random roll by the limit passed in B, advances the random seed at RandomCounter, and returns the scaled roll in A.
CalculateRandom 9CA8 PUSH IX Keep the limit passed in.
9CAA PUSH BC
9CAB LD C,A
9CAC SLA A A*=02.
9CAE JR NC,CalculateRandom_KeepTheDoubledLimit Jump to CalculateRandom_KeepTheDoubledLimit if there's no carry over.
9CB0 LD A,$FF Else, set A=FF.
CalculateRandom_KeepTheDoubledLimit 9CB2 LD B,A Keep the doubled limit in B.
CalculateRandom_Loop 9CB3 LD IX,$B712 Increase the LSB of the RandomCounter by one.
9CB7 INC (IX+$01)
9CBA JR NZ,CalculateRandom_UseTheRandomCounterAsTheRecord Jump to CalculateRandom_UseTheRandomCounterAsTheRecord if the low byte of the random seed did not roll over, skipping the high byte.
9CBC INC (IX+$00) Increase the MSB of the RandomCounter by one.
CalculateRandom_UseTheRandomCounterAsTheRecord 9CBF LD IX,($B712) Use the random counter as the record being walked.
9CC3 LD A,($B70E) Read the random seed.
9CC6 ADC A,(IX+$00) Add the carry onto the random seed.
9CC9 ADD IX,DE Step on through the random counter.
9CCB XOR (IX+$01) Fold byte 1 of this record into it.
9CCE PUSH HL Compare RandomSeed against A.
9CCF LD HL,$B70E
9CD2 CP (HL)
9CD3 POP HL If the result of the comparison was zero, jump back to CalculateRandom_Loop to try again.
9CD4 JR Z,CalculateRandom_Loop
9CD6 LD ($B70E),A Write A to RandomSeed.
CalculateRandom_HalveTheSeed 9CD9 CP B Jump to CalculateRandom_TakeTheResultOffTheRandomSeed if the random seed is below the doubled limit in B.
9CDA JR C,CalculateRandom_TakeTheResultOffTheRandomSeed
9CDC JR Z,CalculateRandom_TakeTheResultOffTheRandomSeed Jump to CalculateRandom_TakeTheResultOffTheRandomSeed if the random seed equals it.
9CDE SRL A Halve the random seed.
9CE0 JP CalculateRandom_HalveTheSeed Jump back to CalculateRandom_HalveTheSeed to repeat this search.
CalculateRandom_TakeTheResultOffTheRandomSeed 9CE3 SUB C Take the result off the random seed.
9CE4 POP BC Restore BC and IX from the stack.
9CE5 POP IX
9CE7 RET Return.
View the equivalent code in;
Prev: 9C9F Up: Map Next: 9CE8