Prev: 57293 Up: Map Next: 57520
57401: Check if Maroc or room item is within room boundaries
Used by the routines at 51968, 52059, 52136, 53323, 53550 and 59630.
Input
E Screen vertical alignment/displacement value - calculated vertical height boundary based on screen viewport position
For Maroc's basic room position, this is between 42 (Maroc at his lowest position in the room) and 90 (Maroc at his highest point in the room)
Set to 90 for Maroc's checks at 59630
Room boundary checks. If Maroc or an item hits a wall/boundary of a room, he bounces off it, in the opposite direction to his current movement.
Maroc bounces off the back wall Maroc bounces off the bottom of the play area
This boundary check routine applies to all items, such as missiles and thrown objects.
a missile spell bouncing off a room's side wall an object bouncing off a room's side wall
Any objects hitting the side walls will bounce off sideways, away from the wall.
avalon room boundary layout
Avalon's room boundary layout - original sketch (courtesy of Steve Turner). Click the image for a larger version
57401 LD B,0 B register unused in this routine
57403 LD A,(60232) Graphic's horizontal position on screen (in half-character/4-pixel steps)
57406 LD D,A
57407 LD HL,(60236) H register = object's vertical speed/direction ( > 128 = negative = up, < 128 = positive = down )
L register = object's horizontal movement speed/direction ( > 128 = negative = left, < 128 = positive = right)
ROOM TOP/BOTTOM BOUNDARY CHECKS
57410 LD A,(60154) Vertical screen position/alignment
57413 LD C,A Store in C register
57414 LD A,E E register has been calculated before the start of this routine
Contains vertical screen displacement boundary to check against (as room can scroll up/down)
Based on Maroc's position in the room, from 42 (lowest point of room) to 90 (highest point of room)
57415 SUB C Subtract vertical position on screen
57416 LD C,A Store the difference in the C register
57417 CP 48
57419 JR C,57447 If the difference is between 0 and 48 (carry triggered) the graphic is within the top/bottom vertical screen boundary
Difference is too great (> 48). This means that Maroc or the item is passing either the TOP or BOTTOM of the vertical screen boundary allowed for it, so check which one:
57421 CP 128 Jump if >= 48 but < 128 as it means vertical room position is < 42. Item is past the BOTTOM screen boundary
57423 JR C,57437
Vertical position is above boundary value which means item is past the TOP of the screen room boundary
  1. Correct the overspill so the difference is set to the max difference allowed (0)
  2. Ensure vertical movement value is a positive figure for the next calculation
57425 LD C,0 The calculated difference is < 0, the minimum is 0 to set the difference to this
57427 LD A,H Vertical movement speed/direction
57428 CP 128 For the next calculations we need the vertical movement value to be a positive number
57430 JR C,57447
57432 NEG If > 128 it's a negative value so set it to a positive figure
57434 LD H,A
57435 JR 57447
Vertical position is below 42 (from 57423) meaning he's past the BOTTOM of the screen room boundary.
  1. Correct the overspill so the difference is set to the max difference allowed (48)
  2. Ensure Maroc's vertical movement value is NEGATIVE for the next calculation
57437 LD C,48 The calculated difference is > 48.
Max allowed value is 48 so set the difference to this
57439 LD A,H Vertical movement speed/direction
57440 CP 128 If > 128 (no carry), leave value as is
57442 JR NC,57447
57444 NEG If < 128 (carry), reverse the value to make it negative
57446 LD H,A
ROOM LEFT/RIGHT BOUNDARY CHECKS
At this point:
  • H register contains the item's/Maroc's (positive) vertical speed
  • C register contains the screen position offset, between 0 and 48.
The side walls of the room slope towards the bottom of the screen, meaning the left/right room boundary changes depending on the vertical position.
The angle of the side walls is not 45 degrees - it's half this angle. So halve the vertical offset/difference by 2:
57447 LD A,C Get earlier calculated value - this is the top area boundary minus screen vertical position
57448 RRA Divide by 2
57449 AND 127 (Reset bit 7 from any earlier carry)
57451 LD C,A C register now contains half the difference (0-24)
LEFT WALL BOUNDARY CHECK
57452 LD A,(60155) This is the LEFT (vertical) corner of the room - horizontal position, in half character squares (4 pixels) in the room, from the left of the room's playing area.
It's at this point that the left side wall starts to slope down to the left hand side of the screen
57455 SUB C Subtract the vertical difference (halved in previous calculation to account for the side wall 'slope')
57456 SUB D Subtract the horizontal co-ordinate (in half-character/4-pixel steps)
57457 CP 32 If <32, we've passed the side wall on the left - bounce the item/Maroc back off the wall (to the right)
57459 JR NC,57486 If >=32 (no carry), we're fine as far as the left room boundary is concerned, so jump out here for a right-wall boundary check
Item/Maroc has hit the LEFT of the room boundary.
Ensure the horizontal movement speed value is positive, so the item/Maroc is moving right
57461 LD A,L Horizontal speed/direction
57462 CP 128
57464 JR C,57469 If < 128 (carry), retain the positive value
57466 NEG If >= 128 (no carry), reverse the value to make it positive
57468 LD L,A
Check vertical movement:
  • Maroc and other items can hit the side walls by moving upwards into them, even if they're not moving left/right
  • If this happens, the item/Maroc bounces off the wall horizontally (rather than vertically)
  • See the small diagram at the bottom of the original 80s scanned illustration earlier on this page, to see this
57469 LD A,H Check vertical speed/direction
57470 CP 128 If < 128, Maroc is moving downwards - this is fine for the sloping wall, so can jump to end of this routine
57472 JR C,57516
Item/Maroc is moving UPWARDS into the LEFT room boundary.
Vertical speed will be transferred to horizontal speed (wall bounce)
However, the horizontal bounce is lessened (to preserve the effect of a 'diagonal bounce' by first dividing the speed by 4:
57474 SRA A Speed = speed/2
57476 SRA A Speed = speed/4
57478 NEG Make speed into a positive value (item/Maroc will be moving right after bouncing off left wall)
57480 ADD A,L Add to horizontal speed/direction
57481 LD L,A And store
57482 LD H,0 Set vertical speed to 0
57484 JR 57516
RIGHT WALL BOUNDARY CHECK
No carry jump from a bit earlier - 57459
57486 LD A,(60156) This is the RIGHT (vertical) corner of the room - horizontal position, in half character squares (4 pixels) in the room, from the left of the room's playing area.
It's at this point that the right side wall starts to slope down to the right hand side of the screen
57489 ADD A,C Add the vertical difference (halved in earlier calculation to account for the side wall 'slope')
57490 SUB D Add the horizontal co-ordinate (in half-character/4-pixel steps)
57491 CP 224 If value is >=224 (no carry), we've passed the side wall on the right - bounce the item/Maroc back off the wall (to the left)
57493 JR C,57516 If (carry) value is <=224 (equivalent of -32), we're fine as far as the right room boundary is concerned, so jump out here to the end of the routine
Item/Maroc has hit the RIGHT of the room boundary.
Ensure the horizontal movement speed value is negative, so the item/Maroc is now moving left
57495 LD A,L Horizontal speed/direction
57496 CP 128
57498 JR NC,57503 If >=128 128 (no carry), retain the negative value
57500 NEG If <128 (carry), reverse the value to make it negative
57502 LD L,A
Check vertical movement:
  • Maroc and other items can hit the side walls by moving upwards into them, even if they're not moving left/right
  • If this happens, the item/Maroc bounces off the wall horizontally (rather than vertically)
  • See the small diagram at the bottom of the original 80s scanned illustration earlier on this page, to see this
57503 LD A,H Check vertical speed/direction
57504 CP 128 If < 128, Maroc is moving downwards - this is fine for the sloping wall, so can jump to end of this routine
57506 JR C,57516
Item/Maroc is moving UPWARDS into the RIGHT room boundary.
Vertical speed will be transferred to horizontal speed (wall bounce)
However, the horizontal bounce is lessened (to preserve the effect of a 'diagonal bounce' by first dividing the speed by 4:
57508 SRA A Speed = speed/2
57510 SRA A Speed = speed/4
57512 ADD A,L Add to horizontal speed/direction (though this is a negative figure so will essentially be subtracted)
57513 LD L,A ...and re-store horizontal speed/direction
57514 LD H,0 Set vertical movement direction/speed to 0
All room boundary checks complete
57516 LD (60236),HL Adjusted horizontal/vertical speed/direction re-stored
57519 RET
Prev: 57293 Up: Map Next: 57520