Prev: CB96 Up: Map Next: CC27
CBA8: Determine movement of room items/creatures that move around the screen
Used by the routines at CB00 and CB5B.
moving object - mouse
Not defined in the room creature table (6B8C), but there are certain room items defined in the room object item sets at 736F that can move around the screen, for example:
  • Mouse (which Maroc can pick up)
  • Animated sabre (also collectable)
  • Spiders
  • Bats
  • Scorpions
This routine determines if the item/creature in the room item set can move around the screen.
If so, it determines its movement using the movement pattern byte stored at EB8D. Some creatures home in on Maroc, others move more haphazardly.
CBA8 LD A,($EB8D) First byte from object state data set
CBAB AND $07 Keep bits 0-2
CBAD LD C,A Store in C register
CBAE CP $00 Does this item move (any of bits 0-2 are set)?
CBB0 JP Z,$CC27 If not, skip the next section
This item moves:
CBB3 LD A,($EB4E) Get the item properties byte
CBB6 AND $FD Reset bit 2 to indicate that this item is not hidden - e.g. it might be a scorpion released from a bottle, or the mouse appearing from the mouse hole
CBB8 LD ($EB4E),A ...and re-store
CBBB LD A,($EAAA) Get game counter/timer
CBBE AND $1F Keep bits 0-4 (values 0-31)
CBC0 JP Z,$CC1F To stop the item/creature's movement/speed getting out of hand, every 32nd frame, stop the creature/item by jumping out here and setting its vertical & horizontal movement speed to zero.
CBC3 CP $07 Check if counter is < 7
CBC5 JR C,$CC0F If so, jump out (overall this will be around 1 in 4 times) - don't adjust this creature's current direction/speed/trajectory
Game counter between 7 and 31 (will be ~3 times in every 4)
CBC7 LD A,C Graphic movement data for graphics from C register (EB8D)
CBC8 AND $01 Check bit 1 - if set, this indicates that the creature has a random element to its movement
CBCA JR Z,$CBEC If not set, skip the next routine, which randomly adjusts the creature's speed/direction
CREATURES WITH A RANDOM ELEMENT TO THEIR MOVEMENT - randomly adjust this item's vertical and horizontal movement speed
CBCC CALL $DB06 Generate pseudo-random number
CBCF LD B,A Temp store in B register
CBD0 AND $0F Keep bits 0-3 (values 0-15)
CBD2 SUB $07 Subtract 7
CBD4 LD D,A Store in D register - will be a number between -7 and +8
CBD5 LD A,($EB4C) Item's horizontal movement speed
CBD8 ADD A,D Add (or subtract if negative) the random value to current speed
CBD9 LD ($EB4C),A ...and re-store altered horizontal movement speed
CBDC LD A,B Retrieve pseudo-random number
CBDD RRA
CBDE RRA
CBDF RRA Divide by 8
CBE0 AND $0F Keep bits 0-3 (values 0-15)
CBE2 SUB $07 Subtract 7
CBE4 LD D,A Store in D register - will be a number between -7 and +8
CBE5 LD A,($EB4D) Item's vertical movement speed
CBE8 ADD A,D Add (or subtract if negative) the random value to current speed
CBE9 LD ($EB4D),A ...and re-store altered horizontal movement speed
Bit 1 (2) of the item's movement pattern byte indicates whether this creature/item will home in on Maroc.
CBEC LD A,C Retrieve movement pattern byte from C register
CBED AND $02 Check bit 1 (2)
CBEF JR Z,$CC0F If set, this item/creature will try to home in on Maroc's position. If not (e.g. the mouse) skip the next set of instructions.
CREATURES THAT HOME IN ON MAROC'S POSITION:
Calculate HORIZONTAL movement direction
CBF1 LD A,($EB48) Get the creature/item's horizontal position
CBF4 LD C,A
CBF5 LD A,($EAC0) Maroc's horizontal screen position
CBF8 SUB C Subtract the item's horizontal position
CBF9 RLA Rotating the bits left...
CBFA SBC A,A ...and then using sub with carry will result in 0 or 255 (-1) - this indicates whether the difference is positive (Maroc is to the right) or negative (Maroc is to the left)
CBFB XOR $07 This XOR subsequently produces a value of either +7 or -7
CBFD LD ($EB4C),A ...Store this as the item/creature's horizontal movement speed
Calculate VERTICAL movement direction
CC00 LD A,($EB4A) Get the creature/item's vertical position
CC03 LD C,A
CC04 LD A,($EAC1) Maroc's vertical (Y-axis) screen position
CC07 SUB C Subtract the item's vertical position
CC08 RLA Rotating the bits left...
CC09 SBC A,A ...and then using sub with carry will result in 0 or 255 (-1) - this indicates whether the difference is positive (Maroc is below the creature/item) or negative (Maroc is above the creature/item)
CC0A XOR $07 This XOR subsequently produces a value of either +7 or -7
CC0C LD ($EB4D),A ...Store this as the item/creature's vertical movement speed
Check if item/creature is within wall boundaries (room wall collision check)
  • If bit 2 (4) of the movement pattern byte is NOT set, run a standard room boundary collision check
  • If this bit IS set, skip the check. This is to allow certain room creature 'items' - mainly BATS - to move above into the areas above the normal back and side wall boundaries.
CC0F LD A,($EB8D) Retrieve movement pattern byte
CC12 AND $04 Check if bit 2 is set
CC14 JR NZ,$CC27 If so, skip to the next routine
CC16 LD A,($EB4A) Graphic vertical position in room
CC19 LD E,A
CC1A CALL $E039 Run the standard wall collision check, which keeps the creature/item within the room's boundary
CC1D JR $CC27
Jump from CBC0 every 32nd game cycle. To reduce overly-erratic movement of creatures or items, this routine stops the item/creature moving. It also makes creature movement looks slightly more natural as they pause their movement occasionally.
CC1F LD A,$00 Set the item/creature's vertical and horizontal movement speed to zero
CC21 LD ($EB4D),A
CC24 LD ($EB4C),A
Prev: CB96 Up: Map Next: CC27