Prev: DFCD Up: Map Next: E0B0
E039: Check if Maroc or room item is within room boundaries
Used by the routines at CB00, CB5B, CBA8, D04B, D12E and E8EE.
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 E8EE
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
E039 LD B,$00 B register unused in this routine
E03B LD A,($EB48) Graphic's horizontal position on screen (in half-character/4-pixel steps)
E03E LD D,A
E03F LD HL,($EB4C) 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
E042 LD A,($EAFA) Vertical screen position/alignment
E045 LD C,A Store in C register
E046 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)
E047 SUB C Subtract vertical position on screen
E048 LD C,A Store the difference in the C register
E049 CP $30
E04B JR C,$E067 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:
E04D CP $80 Jump if >= 48 but < 128 as it means vertical room position is < 42. Item is past the BOTTOM screen boundary
E04F JR C,$E05D
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
E051 LD C,$00 The calculated difference is < 0, the minimum is 0 to set the difference to this
E053 LD A,H Vertical movement speed/direction
E054 CP $80 For the next calculations we need the vertical movement value to be a positive number
E056 JR C,$E067
E058 NEG If > 128 it's a negative value so set it to a positive figure
E05A LD H,A
E05B JR $E067
Vertical position is below 42 (from E04F) 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
E05D LD C,$30 The calculated difference is > 48.
Max allowed value is 48 so set the difference to this
E05F LD A,H Vertical movement speed/direction
E060 CP $80 If > 128 (no carry), leave value as is
E062 JR NC,$E067
E064 NEG If < 128 (carry), reverse the value to make it negative
E066 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:
E067 LD A,C Get earlier calculated value - this is the top area boundary minus screen vertical position
E068 RRA Divide by 2
E069 AND $7F (Reset bit 7 from any earlier carry)
E06B LD C,A C register now contains half the difference (0-24)
LEFT WALL BOUNDARY CHECK
E06C LD A,($EAFB) 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
E06F SUB C Subtract the vertical difference (halved in previous calculation to account for the side wall 'slope')
E070 SUB D Subtract the horizontal co-ordinate (in half-character/4-pixel steps)
E071 CP $20 If <32, we've passed the side wall on the left - bounce the item/Maroc back off the wall (to the right)
E073 JR NC,$E08E 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
E075 LD A,L Horizontal speed/direction
E076 CP $80
E078 JR C,$E07D If < 128 (carry), retain the positive value
E07A NEG If >= 128 (no carry), reverse the value to make it positive
E07C 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
E07D LD A,H Check vertical speed/direction
E07E CP $80 If < 128, Maroc is moving downwards - this is fine for the sloping wall, so can jump to end of this routine
E080 JR C,$E0AC
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:
E082 SRA A Speed = speed/2
E084 SRA A Speed = speed/4
E086 NEG Make speed into a positive value (item/Maroc will be moving right after bouncing off left wall)
E088 ADD A,L Add to horizontal speed/direction
E089 LD L,A And store
E08A LD H,$00 Set vertical speed to 0
E08C JR $E0AC
RIGHT WALL BOUNDARY CHECK
No carry jump from a bit earlier - E073
E08E LD A,($EAFC) 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
E091 ADD A,C Add the vertical difference (halved in earlier calculation to account for the side wall 'slope')
E092 SUB D Add the horizontal co-ordinate (in half-character/4-pixel steps)
E093 CP $E0 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)
E095 JR C,$E0AC 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
E097 LD A,L Horizontal speed/direction
E098 CP $80
E09A JR NC,$E09F If >=128 128 (no carry), retain the negative value
E09C NEG If <128 (carry), reverse the value to make it negative
E09E 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
E09F LD A,H Check vertical speed/direction
E0A0 CP $80 If < 128, Maroc is moving downwards - this is fine for the sloping wall, so can jump to end of this routine
E0A2 JR C,$E0AC
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:
E0A4 SRA A Speed = speed/2
E0A6 SRA A Speed = speed/4
E0A8 ADD A,L Add to horizontal speed/direction (though this is a negative figure so will essentially be subtracted)
E0A9 LD L,A ...and re-store horizontal speed/direction
E0AA LD H,$00 Set vertical movement direction/speed to 0
All room boundary checks complete
E0AC LD ($EB4C),HL Adjusted horizontal/vertical speed/direction re-stored
E0AF RET
Prev: DFCD Up: Map Next: E0B0