Prev: 40095 Up: Map Next: 40168
40104: 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 40104 PUSH IX Keep the limit passed in.
40106 PUSH BC
40107 LD C,A
40108 SLA A A*=2.
40110 JR NC,CalculateRandom_KeepTheDoubledLimit Jump to CalculateRandom_KeepTheDoubledLimit if there's no carry over.
40112 LD A,255 Else, set A=255.
CalculateRandom_KeepTheDoubledLimit 40114 LD B,A Keep the doubled limit in B.
CalculateRandom_Loop 40115 LD IX,46866 Increase the LSB of the RandomCounter by one.
40119 INC (IX+1)
40122 JR NZ,CalculateRandom_UseTheRandomCounterAsTheRecord Jump to CalculateRandom_UseTheRandomCounterAsTheRecord if the low byte of the random seed did not roll over, skipping the high byte.
40124 INC (IX+0) Increase the MSB of the RandomCounter by one.
CalculateRandom_UseTheRandomCounterAsTheRecord 40127 LD IX,(46866) Use the random counter as the record being walked.
40131 LD A,(46862) Read the random seed.
40134 ADC A,(IX+0) Add the carry onto the random seed.
40137 ADD IX,DE Step on through the random counter.
40139 XOR (IX+1) Fold byte 1 of this record into it.
40142 PUSH HL Compare RandomSeed against A.
40143 LD HL,46862
40146 CP (HL)
40147 POP HL If the result of the comparison was zero, jump back to CalculateRandom_Loop to try again.
40148 JR Z,CalculateRandom_Loop
40150 LD (46862),A Write A to RandomSeed.
CalculateRandom_HalveTheSeed 40153 CP B Jump to CalculateRandom_TakeTheResultOffTheRandomSeed if the random seed is below the doubled limit in B.
40154 JR C,CalculateRandom_TakeTheResultOffTheRandomSeed
40156 JR Z,CalculateRandom_TakeTheResultOffTheRandomSeed Jump to CalculateRandom_TakeTheResultOffTheRandomSeed if the random seed equals it.
40158 SRL A Halve the random seed.
40160 JP CalculateRandom_HalveTheSeed Jump back to CalculateRandom_HalveTheSeed to repeat this search.
CalculateRandom_TakeTheResultOffTheRandomSeed 40163 SUB C Take the result off the random seed.
40164 POP BC Restore BC and IX from the stack.
40165 POP IX
40167 RET Return.
View the equivalent code in;
Prev: 40095 Up: Map Next: 40168