Prev: 94DD Up: Map Next: 9546
94F5: Randomise Doors
Used by the routine at StartGame.
Called when a game starts. For each of the doors from RoomItemEntities onwards, a byte from the ROM (from an address that depends on FRAMES and CounterLow) is used as a random number. If it is less than 70 and the sprite ID of both halves of the door is 01 then they are both changed to 22 or if it is 02 then to 20. Both of those are doors that open and close (see HandlerDoorOpen and HandlerDoorClosed). The doorway is then closed by setting bit 3 of the attribute of both halves (see CloseDoorway).
RandomiseDoors 94F5 LD A,($5E12) Fetch the counter from CounterLow and keep the low byte of the ROM address in L.
94F8 LD L,A
94F9 LD A,($5C78) Fetch FRAMES.
94FC AND %00001111 Keep only bits 0-3.
94FE OR %00010000 Set bit 4.
9500 LD H,A Keep the high byte of the ROM address in H.
9501 EXX Switch to the shadow registers.
9502 LD HL,$EEE0 HL'=EEE0 (the first half of the first door).
9505 LD DE,$EEE8 Point DE' at the second half of the first door at RoomItemEntities_Door_1_Second_Half.
9508 LD BC,$0010 BC'=0010 (the distance between the doors).
RandomiseDoors_Loop 950B EXX Switch to the main registers.
950C LD A,(HL) Fetch the next random byte from the ROM.
950D INC HL Move HL on to the next byte.
950E EXX Switch to the shadow registers.
950F CP $70 Jump to RandomiseDoors_Next if the byte is 70 or more.
9511 JR NC,RandomiseDoors_Next
9513 LD A,(DE) Fetch the sprite ID of the second half of the door.
9514 CP (HL) Jump to RandomiseDoors_Next if it is different to the first half.
9515 JR NZ,RandomiseDoors_Next
9517 CP $01 Jump to RandomiseDoors_Close if it is 01.
9519 JR Z,RandomiseDoors_Close
951B CP $02 Jump to RandomiseDoors_Close_Alternate if it is 02.
951D JR Z,RandomiseDoors_Close_Alternate
RandomiseDoors_Next 951F ADD HL,BC Move HL on to the next door.
9520 EX DE,HL Switch DE and HL.
9521 ADD HL,BC Move HL on to the next door.
9522 RET C Return if there are no more doors.
9523 EX DE,HL Switch DE and HL back again.
9524 JR RandomiseDoors_Loop Jump back to RandomiseDoors_Loop to carry on with the loop through the doors.
RandomiseDoors_Close 9526 LD A,$22 A=22 (the new sprite ID of the closed door).
RandomiseDoors_Change 9528 LD (DE),A Update the sprite ID of the second half.
9529 LD (HL),A Update the sprite ID of the first half.
952A PUSH DE Stash DE on the stack.
952B INC DE Move DE on to the attribute of the second half.
952C INC DE
952D INC DE
952E INC DE
952F INC DE
9530 LD A,(DE) Fetch the attribute.
9531 OR %00001000 Set bit 3, to close the doorway.
9533 LD (DE),A Write it back.
9534 POP DE Restore DE from the stack.
9535 PUSH HL Stash HL on the stack.
9536 INC HL Move HL on to the attribute of the first half.
9537 INC HL
9538 INC HL
9539 INC HL
953A INC HL
953B LD A,(HL) Fetch the attribute.
953C OR %00001000 Set bit 3, to close the doorway.
953E LD (HL),A Write it back.
953F POP HL Restore HL from the stack.
9540 JR RandomiseDoors_Next Jump back to RandomiseDoors_Next to move on to the next door.
RandomiseDoors_Close_Alternate 9542 LD A,$20 A=20 (the new sprite ID of the closed door).
9544 JR RandomiseDoors_Change Jump to RandomiseDoors_Change to change the door.
Prev: 94DD Up: Map Next: 9546