Prev: 51178 Up: Map Next: 51359
51255: Adjust room item's position based on the speed and direction of Maroc and the item
Used by the routine at 51178.
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:
51255 LD A,(60239) Get the Graphic properties byte (again)
51258 AND 192 Check the top 2 bits (6 & 7).
Bit 6 set = graphic has left/right facing versions
Bit 7 = graphic is animated
51260 JR Z,51265 If neither is set, there's nothing to change animation-wise, so skip the following CALL instruction
51262 CALL 58166 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 60108) 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
51265 LD A,(60236) Item's horizontal movement speed (this will be non-zero for moving things such as creatures)
51268 LD C,A
51269 LD A,(60164) Maroc's horizontal movement speed (reverse of the above)
51272 ADD A,C Add them together to get the combined speed of the moving item
51273 LD L,A ...and store in the L register
51274 RLA Rotate into carry bit
51275 SBC A,A A register is now 0 or 255 (-1)
51276 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).
51277 ADD HL,HL x2
51278 ADD HL,HL x4
51279 ADD HL,HL x8
51280 ADD HL,HL x16
51281 EX DE,HL Swap into DE register pair
51282 LD HL,(60231) Graphic horizontal position within current room, in half character (4 pixel) steps.
51285 ADD HL,DE Add to speed to get the new position
51286 LD (60231),HL ...and re-store the adjusted horizontal position
51289 ADD HL,HL Value in HL is 4-pixel steps, convert to pixels by multiplying by 4 - first x2
51290 SBC A,A Put the carry into the A register as this will act as an overflow to the calculated horizontal pixel position
51291 ADD HL,HL x4
51292 RLA Repeat with any carry - shift into A register
51293 LD (60108),A Store the horizontal pixel position overflow...
51296 LD A,H
51297 LD (60107),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:
51300 LD B,A Store new horizontal pixel position in B register
51301 LD A,(60115) Get the original horizontal pixel position
51304 CP B Compare it with the new horizontal pixel position
51305 JR Z,51315 Has the graphic moved?
Graphic has moved on-screen
51307 LD A,(60238) Get the graphics properties byte
51310 OR 4 Set bit 2 (4) - this indicates the graphic will need erasing (before it's redrawn)
51312 LD (60238),A ...and re-store
Calculate item's new VERTICAL speed/position based on a combination of the item's speed and Maroc's speed
51315 LD A,(60237) Item's vertical movement speed
51318 LD D,A
51319 LD A,(60165) Maroc's vertical movement speed (reverse of the above)
51322 ADD A,D Add them together to get the effective speed of the moving item
51323 LD L,A ...and store in the L register
51324 RLA Rotate into carry bit
51325 SBC A,A A register is now 0 or 255 (-1)
51326 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.
51327 ADD HL,HL x2
51328 ADD HL,HL x4
51329 ADD HL,HL x8
51330 ADD HL,HL x16
51331 LD A,(60234) Graphic's vertical pixel position within current room
51334 LD (60114),A Copy into item working data buffer
51337 ADD HL,HL x32
51338 ADD HL,HL x64
51339 EX DE,HL Swap into DE register pair
51340 LD HL,(60233) Graphic vertical pixel position within current room
51343 LD A,H High byte - contains 'actual' pixel position
51344 ADD HL,DE Add to speed to get the new position
51345 LD (60233),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:
51348 CP H Compare old with new vertical position
51349 JR Z,51359 Has the graphic moved?
Graphic has moved on-screen
51351 LD A,(60238) Get the graphics properties byte
51354 OR 4 Set bit 2 (4) - this indicates the graphic will need erasing (before it's redrawn)
51356 LD (60238),A ...and re-store.
Prev: 51178 Up: Map Next: 51359