Prev: 6483 Up: Map Next: 64C7
6498: Randomise Cup Positions
Used by the routine at NewGame.
Gives every cup on the map a new Y position from 20 to 9F putting back any cups which have been collected. The numbers come from the ROM, read from a random address in the first 4000 bytes (the low byte of the address is whatever is in E).
RandomiseCups 6498 LD BC,$7CD6 Point BC at the end of the map (Sprite_Car_1, one byte past the end of MapPoints).
649B LD A,R Fetch a random number (from the Z80 refresh register).
Limit the number to 0-63.
649D AND %00111111 Keep only bits 0-5.
649F LD D,A Use it as the high byte of a random ROM address, held in D.
64A0 LD HL,$0000 HL=0000.
64A3 ADD HL,DE Add DE to HL then swap them over (this leaves DE unchanged).
64A4 EX DE,HL
64A5 LD HL,$7813 HL=7813 (the sprite ID of the first map entry).
Check the sprite ID of each map entry.
RandomiseCups_Loop 64A8 LD A,(HL) Fetch the sprite ID of the map entry.
64A9 CP $20 If this is a cup (A is 20) then jump to HandlerSpriteCup.
64AB JR Z,HandlerSpriteCup
64AD CP $60 If this is not a collected cup (A is not 60) then jump to RandomiseCups_Next.
64AF JR NZ,RandomiseCups_Next
64B1 LD (HL),$20 Put the cup back by writing 20 as the sprite ID.
Write a new random Y position.
HandlerSpriteCup 64B3 DEC HL Point HL at the Y position of the cup.
64B4 LD A,(DE) Fetch a random byte from the ROM.
64B5 INC DE Move on to the next ROM address.
64B6 AND %01111111 Keep only bits 0-6.
64B8 ADD A,$20 Add 20 so that the Y position is between 20 and 9F.
64BA LD (HL),A Store the new Y position of the cup.
64BB INC HL Point HL back at the sprite ID of the map entry.
Move on to the sprite ID of the next map entry, and check if the end of the map has been reached.
RandomiseCups_Next 64BC INC HL Increment HL by three.
64BD INC HL
64BE INC HL
64BF PUSH HL Stash the map pointer on the stack.
64C0 AND A Reset the carry flag.
64C1 SBC HL,BC Work out how far the map pointer is from the end of the map.
64C3 POP HL Restore the map pointer from the stack.
64C4 JR C,RandomiseCups_Loop If the map pointer is before the end of the map then jump to RandomiseCups_Loop.
64C6 RET Return.
Prev: 6483 Up: Map Next: 64C7