Prev: C7EA Up: Map Next: C89F
C837: Adjust room item's position based on the speed and direction of Maroc and the item
Used by the routine at C7EA.
Re-calculate the item's screen position based on the combined speed directions of Maroc (or the room) and the item
Firstly check if the graphic is animated, or has a left/right (multi-)facing directions. If so, first deal with the graphics animation frame:
C837 LD A,($EB4F) Get the Graphic properties byte (again)
C83A AND $C0 Check the top 2 bits (6 & 7).
Bit 6 set = graphic has left/right facing versions
Bit 7 = graphic is animated
C83C JR Z,$C841 If neither is set, there's nothing to change animation-wise, so skip the following CALL instruction
C83E CALL $E336 If either of the bits are set, handle the animation frame(s) for this graphic
The next routine calculates the item's adjusted room position based on the trajectory of Maroc and the trajectory of the item. Calculations include:
  • Converting speed values, which are stored as 4 x pixel values (e.g. a speed of 32 would cause the the item to move 32/4 = 8 pixels )
  • Using overflow/offset bytes (e.g. at EACC) to calculate pixel fractions/overflows
  • Converting horizontal 4-pixel/half-character horizontal positions to pixel positions
Calculate item's new HORIZONTAL speed/position based on a combination of the item's speed and Maroc's speed
C841 LD A,($EB4C) Item's horizontal movement speed (this will be non-zero for moving things such as creatures)
C844 LD C,A
C845 LD A,($EB04) Maroc's horizontal movement speed (reverse of the above)
C848 ADD A,C Add them together to get the combined speed of the moving item
C849 LD L,A ...and store in the L register
C84A RLA Rotate into carry bit
C84B SBC A,A A register is now 0 or 255 (-1)
C84C LD H,A Put 0 or 255 in H register - the ADD HL,HL statements will now add or subtract the offset depending on whether Maroc is moving left or right
The speed needs to be added to the high byte holding the item's horizontal (4-pixel) position.
The following ADD instructions make this adjustment - e.g. a speed of 32 would be shifted into bit 1 (2) of the high byte - this would mean a result of 2 (x 4-pixel/half-character steps, or 8 pixels).
C84D ADD HL,HL x2
C84E ADD HL,HL x4
C84F ADD HL,HL x8
C850 ADD HL,HL x16
C851 EX DE,HL Swap into DE register pair
C852 LD HL,($EB47) Graphic horizontal position within current room, in half character (4 pixel) steps.
C855 ADD HL,DE Add to speed to get the new position
C856 LD ($EB47),HL ...and re-store the adjusted horizontal position
C859 ADD HL,HL Value in HL is 4-pixel steps, convert to pixels by multiplying by 4 - first x2
C85A SBC A,A Put the carry into the A register as this will act as an overflow to the calculated horizontal pixel position
C85B ADD HL,HL x4
C85C RLA Repeat with any carry - shift into A register
C85D LD ($EACC),A Store the horizontal pixel position overflow...
C860 LD A,H
C861 LD ($EACB),A ...and the new horizontal pixel position
If the graphic has moved on-screen, make sure it's erased at its old position before it gets redrawn at its new one:
C864 LD B,A Store new horizontal pixel position in B register
C865 LD A,($EAD3) Get the original horizontal pixel position
C868 CP B Compare it with the new horizontal pixel position
C869 JR Z,$C873 Has the graphic moved?
Graphic has moved on-screen
C86B LD A,($EB4E) Get the graphics properties byte
C86E OR $04 Set bit 2 (4) - this indicates the graphic will need erasing (before it's redrawn)
C870 LD ($EB4E),A ...and re-store
Calculate item's new VERTICAL speed/position based on a combination of the item's speed and Maroc's speed
C873 LD A,($EB4D) Item's vertical movement speed
C876 LD D,A
C877 LD A,($EB05) Maroc's vertical movement speed (reverse of the above)
C87A ADD A,D Add them together to get the effective speed of the moving item
C87B LD L,A ...and store in the L register
C87C RLA Rotate into carry bit
C87D SBC A,A A register is now 0 or 255 (-1)
C87E LD H,A Put 0 or 255 in H register - the ADD HL,HL statements will now add or subtract the offset amount depending on whether Maroc is moving up or down
Next, a similar calculation to the horizontal position, where the speed value is shifted into the high byte of HL to provide the offset pixel value for the item's vertical screen position.
Vertical position is held in pixels (rather than half-characters like the horizontal position), so 6 straight shifts to the left (x64) will calculate the new vertical pixel position from the speed.
C87F ADD HL,HL x2
C880 ADD HL,HL x4
C881 ADD HL,HL x8
C882 ADD HL,HL x16
C883 LD A,($EB4A) Graphic's vertical pixel position within current room
C886 LD ($EAD2),A Copy into item working data buffer
C889 ADD HL,HL x32
C88A ADD HL,HL x64
C88B EX DE,HL Swap into DE register pair
C88C LD HL,($EB49) Graphic vertical pixel position within current room
C88F LD A,H High byte - contains 'actual' pixel position
C890 ADD HL,DE Add to speed to get the new position
C891 LD ($EB49),HL ...and restore the new vertical pixel position
If the graphic has moved on-screen, make sure it's erased at its old position before it gets redrawn at its new one:
C894 CP H Compare old with new vertical position
C895 JR Z,$C89F Has the graphic moved?
Graphic has moved on-screen
C897 LD A,($EB4E) Get the graphics properties byte
C89A OR $04 Set bit 2 (4) - this indicates the graphic will need erasing (before it's redrawn)
C89C LD ($EB4E),A ...and re-store.
Prev: C7EA Up: Map Next: C89F