Prev: 51359 Up: Map Next: 51551
51420: Determine if creatures will enter the current room
Used by the routine at 51359.
Creatures will follow Maroc into a room after a short delay. Maroc can:
  • Lose his pursuers if he exits the room before they appear
  • Move away so the door is off-screen. Creatures won't enter the room off-screen, though they may appear once the door is back in view.
Creatures may also wander in from other adjacent rooms. The frequency of this happening is much lower - there's a longer timer counter for this event.
This is the first routine run when handling DOOR items (item number 2), in the room item check at 51359.
51420 LD A,(60238) Get item properties byte (for the door)
51423 LD C,A
51424 AND 1 Bit 1 indicates if the item is on-screen (0 = no, 1 = yes)
51426 JP Z,53885 If the door isn't on-screen, nothing more to do or check in this routine
51429 LD A,C If the bit is set, the door is on-screen. Retrieve item properties byte
51430 AND 240 Filter out bits 0-3
51432 CP 0 If any of bits 4-7 are set, there's already something already happening with this door that needs dealing with
51434 JP Z,51450 No bits set - skip next few checks
51437 CP 32
51439 JP Z,51854 Bit 5 (32) set = Maroc or a creature has touched a door, so it needs to be opened/closed
51442 CP 64 Bit 6 (64) set = means the door is currently opening or closing
51444 JP Z,51892
51447 JP 51961 Bit 7 (128) set = Maroc has just gone through this doorway
Creatures will follow Maroc into other rooms. After Maroc enters a room, a check is made every 16 game cycles to see if there are any creatures in the connecting room.
This gives Maroc a short time to quickly move room again before the creature enters. Doing so lets him successfully escape his pursuers.
51450 LD A,(60074) Game cycle counter/timer
51453 AND 15 Check bits 0-3 (values = 0-15)
51455 JP NZ,51611 If anything other than 0, skip this check
Every 16 frames/game cycles:
51458 LD A,(60230) Door being checked - 0 = Back wall, 1 = Right wall, 2 = Left wall
51461 LD C,A
51462 LD A,(60084) Connecting door (in table at 26512) that Maroc entered the current room from
51465 CP C
51466 JR Z,51493 If Maroc entered through this door, check if there were any creatures in his previous room
Maroc didn't enter through this door. However, there's still a chance that any creatures in the adjacent connected room may wander in.
A random number is generated and compared against the delay counter at 60341. There will be a much greater delay before this event occurs (a monster wandering in, rather than pursuing).
Additionally, creatures will not wander into the room if there is already another creature in the room.
51468 CALL 56070 Generate pseudo-random number
51471 AND 127 Filter out bit 7 (just keep random values 0-127)
51473 LD C,A
51474 LD A,(60341) Timer indicating how long before a creature is likely to wander in from an adjacent room
51477 DEC A Decrement the counter
51478 LD (60341),A ...re-store it...
51481 CP C Compare it with the randomly generated value
51482 JP NC,51611 If timer is (still) >= than the random value, a creature won't wander into the room
51485 LD A,(60343) Timer is less than the random value. Get the number of creatures currently in the room
51488 CP 0
51490 JP NZ,51611 If there are ANY creatures already in the room, don't let any more enter
First, check if we've reached the maximum creatures-per-room (3)
51493 LD A,(60342) Get number of creatures currently in room
51496 CP 3
51498 JR NC,51611 If there are already 3, skip the whole check as there can't be any more
Use the connecting door table at 26512 to check which rooms are adjacent to this one
51500 LD HL,(60228) Address pointer to connecting door table at 26512
51503 LD C,(HL) First byte in set contains the room number for this door
51504 LD A,(60087) Current room number
51507 CP C Is it the same as the first byte of the connecting door set?
51508 JR NZ,51517
51510 INC HL If so, the door is in this room - move to next byte
51511 LD C,(HL) This byte holds the connecting room for this door
51512 LD A,223
51514 CP C
51515 JR C,51611 If the connecting room is >=224, the door leads to a tunnel. Nothing can enter from that direction so skip out of the routine
Check for any creature data in the adjacent room
51517 DEC C C register will be part of an offset (BC register pair) to add to an address pointer for the room creature table at 27532. Room numbers start at 1, but it needs to start at 0 for the offset, so reduce the value by 1
51518 LD B,0 Reset B register (ready for offset to be added)
51520 LD HL,(56048) Address pointer at start of room creature data at 27532
51523 ADD HL,BC Add the offset to get to the right creature data in this adjacent room
51524 LD A,(HL) Get the room creature data (all stored in 1 single byte)
51525 LD C,A
51526 CP 0
51528 JR Z,51611 If there are no creatures in this room, there aren't any creatures who might come through this door
There IS a creature (or creatures) in the adjacent room that this door connects to:
51530 LD A,(60238) Get door properties byte
51533 LD D,A Temp store in D register
51534 AND 240 Clear bits 0-3
51536 CP 0 Any of bits 4-7 set?
This check has already been made at 51430, so the jump in the next instruction shouldn't be triggered.
51538 JR NZ,51611 If so, jump out of routine here
51540 LD A,D Retrieve door properties byte
51541 AND 8 Check bit 3 - if set, indicates door is open, otherwise closed
51543 JR NZ,51551 If door is open, skip ahead to the next routine
51545 LD A,D Retrieve door properties byte
51546 ADD A,64 A creature is entering the room and the door is closed.
Open the door by setting bit 6 (if set this indicates the door is now opening/closing)
51548 LD (60238),A ...and re-store
The next routine determines the type of creature that will enter the room.
Prev: 51359 Up: Map Next: 51551