Prev: CA8E Up: Map Next: CB00
CAB4: Door opening or closing
Used by the routine at C8DC.
Input
C Door properties byte at EB4E
If bit 6 (64) of the door's properties byte is set, it means the door is in the process of opening or closing.
This routine determines which way the door is swinging based on its wall position and direction (opening vs closing). From this, the graphics frame address pointer is calculated.
CAB4 LD A,($EB50) Graphic frame offset, based on the door's wall position and open/closed status, calculated at C3AB
CAB7 LD B,A Store in B register
Determine if graphic pointer offset is negative or positive (e.g. closing vs opening)
CAB8 RLA Rotate bits left
CAB9 SBC A,A Subtract-with-carry means A register now contains 0 (positive) or 255 (negative)
CABA ADD A,A
CABB INC A
CABC LD E,A E register will now contain either 1 (positive) or 255 (negative)
CABD RLA We need a value for a 2-byte address pointer, so do the same for the high byte
CABE SBC A,A
CABF LD D,A D will either contain 0 - for positive - or 255 (-1) - for negative
DE now contains an offset of 1 or -1 to indicate which way to move the graphics address pointer, depending on whether the door is opening or closing.
CAC0 LD A,E Retrieve the original frame offset
CAC1 ADD A,B Add (or subtract if negative) the +1/-1 offset
CAC2 LD B,A
CAC3 AND $0F For the frame address pointer offset, we're only interested in the lowest 4 bits (0-3) as we just need to figure out a small offset to the graphics address pointer
CAC5 JR Z,$CAE1 If it's now zero, the door is now fully opened or closed so we can skip the rest of the instructions in this block
CAC7 LD H,A Otherwise, copy the offset to the H register
CAC8 LD A,($EB4F) This is the pre-graphics properties data byte - for doors it's used to identify the number of animation frames to open or close this particular door (calculated at C3AB)
CACB AND $0F Again, we only really need bits 0-3 here
CACD CP H Check calculated frame offset against the current frame offset
CACE JR C,$CAE1 If calculated offset is now < stored byte, the door is fully opened or closed
CAD0 LD A,B Otherwise, store as the new current frame value offset
CAD1 LD ($EB50),A
CAD4 LD HL,($EB51) Graphics address pointer to current door frame graphic at 97C3 - doors start at 97D3
CAD7 ADD HL,DE
CAD8 ADD HL,DE Graphics addresses are 2 bytes long, so add the frame offset (-1 or +1) twice
CAD9 LD ($EB51),HL ...and re-store the address pointer
CADC LD A,C Retrieve door properties byte (from EB4E)
CADD OR $04 Set bit 2 (4) to indicate existing door graphic will need to be erased
CADF JR $CAF3
Door is now fully closed or open.
Play a 'crunch' sound effect to indicate the door has fully opened or closed.
CAE1 LD A,$10
CAE3 LD ($EB56),A Sound effect duration counter for the 'crunch' that happens when a door is fully opened or closed
CAE6 LD A,($EB50) Graphic frame number offset
CAE9 XOR $E0 This XOR sets the frame offset by setting its top 3 bits from positive to negative or vice versa, reversing the frame animations for next time (closed to open, open to closed)
CAEB LD ($EB50),A Re-store the updated frame offset value
CAEE LD A,C Retrieve door properties byte (from EB4E)
CAEF AND $0F Keep bits 0-3
CAF1 XOR $08 Then toggle bit 3 which indicates whether the door is open or closed:
  • If the door was open, it's now closed
  • If the door was closed, it's now open
This entry point is used by the routines at C99B, CA65 and CA8E.
CAF3 LD ($EB4E),A Re-store the graphic properties byte, amended based on the various door checks and animations
This entry point is used by the routines at C99B, C9F6 and CA8E.
CAF6 JP $D27D Jump to the next routine.
The jump to these instructions is made from C8F7 (skipping all the door checks) if Maroc is moving through a doorway.
CAF9 LD A,($EB4E) Door properties byte
CAFC ADD A,$80 If bit 7 is set (which it will be) Maroc has just gone through a doorway. Add 128 to reset this bit (back to 0) ready to move him to the next room.
CAFE JR $CAF3
Prev: CA8E Up: Map Next: CB00