Aviator on the BBC Micro

# Visibility: ProcessRunwayLine (Part 2 of 5)

```       Name: ProcessRunwayLine (Part 2 of 5)                         [Show more]
Type: Subroutine
Category: Visibility
Summary: Calculate coordinates and visibility for the runway outline
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

.prun3

LDA #0                 \ Set the matrix number so the call to SetPointCoords
STA matrixNumber       \ and SetObjPointCoords use matrix 1 in the calculation,
\ which will rotate the point by the plane's pitch, roll
\ and yaw angles,  transforming it from the outside
\ world's frame of reference to the plane's frame of
\ reference

LDA #1                 \ Set objectId = 1 to denote the runway object
STA objectId

JSR SetObjectCoords    \ Calculate the runway object's coordinates and
\ visibility, updating the object's status byte with the
\ results and setting point 217 to the object's anchor
\ point

BPL prun9              \ If bit 7 of the runway object's updated status byte is
\ clear, then the runway object is not visible, so jump
\ to prun9 to set the line's visibility accordingly

\ The runway object is visible, so now we work out the
\ coordinates of the runway outline, which is a
\ rectangle whose corners are points 1 to 4

LDA #0                 \ We start by zeroing the (xTemp1 yTemp1 zTemp1) vector,
\ which is stored in six bytes to give us three 16-bit
\ coordinate values (from xTemp1Lo to zTemp1Hi), so
\ first we set A = 0 to use as our zero

LDX #5                 \ Set a counter in X to count the six bytes

.prun4

STA xTemp1Lo,X         \ Zero the X-th byte of the six-byte coordinate block
\ between xTemp1Lo and zTemp1Hi

DEX                    \ Decrement the loop counter

BPL prun4              \ Loop back until we have zeroed all six bytes

\ We now calculate the coordinates for points 1 to 4,
\ starting with point 1

LDY #217               \ Set Y so the call to AddTempToPoint adds point 217 to
\ the xTemp1 vector

STY objectAnchorPoint  \ The anchor point of the runway is point 217, so set
\ objectAnchorPoint to this point for the call to
\ SetObjPointCoords

LDX #1                 \ Set X so the call to AddTempToPoint stores the result
\ in point ID 1

JSR AddTempToPoint     \ Add point 217 to the (xTemp1 yTemp1 zTemp1) point and
\ store the result in (xPoint, yPoint, zPoint) for
\ point 1
\
\ This simply sets point ID 1 to the object's anchor
\ point, as we just zeroed (xTemp1 yTemp1 zTemp1), so
\ point1 of the runway outline is now calculated

LDA #2                 \ Calculate the coordinates for object point 2 with
STA GG                 \ anchor point 217, so point 2 is now calculated
JSR SetObjPointCoords  \
\ This also sets (xTemp1 yTemp1 zTemp1) to the vector
\ from the anchor point to point 2, which we now copy
\ into xTemp2 by copying the following values:
\
\   xTemp1Lo, yTemp1Lo, zTemp1Lo
\   xTemp1Hi, yTemp1Hi, zTemp1Hi
\
\ into the following locations:
\
\   xTemp2Hi,  yTemp2Hi,  zTemp2Hi
\   xTemp2Top, yTemp2Top, zTemp2Top
\
\ so (xTemp2Top xTemp2Hi) etc. contain the vector from
\ the anchor point to the point 2

LDX #5                 \ Set a counter for six bytes

.prun5

LDA xTemp1Lo,X         \ Copy the X-th byte of xTemp1Lo to the X-th byte of
STA xTemp2Hi,X         \ xTemp2Hi

DEX                    \ Decrement the loop counter

BPL prun5              \ Loop back until we have copied all six bytes

\ By now we have coordinates for points 1 and 2, plus
\ the vector from the anchor point to point 2 (which
\ is the same as the vector from point 1 to point 2,
\ as point 1 is the runway's anchor point)

LDA #4                 \ Calculate the coordinates for object point 4 with
STA GG                 \ anchor point 217, so point 4 is now calculated
JSR SetObjPointCoords  \
\ This also sets (xTemp1 yTemp1 zTemp1) to the vector
\ from the anchor point (point 1) to point 4

\ By now we have coordinates for points 1, 2 and 4, and
\ we could just call SetObjPointCoords for point 3, but
\ we can save a bit of time as the runway outline is a
\ rectangle, so we already have all the data we need to
\ calculate the coordinates for point 3
\
\ The runway outline looks like this:
\
\      2         3
\       +-------+
\       |   :   |
\       |   :   |
\       |   :   |
\       |   :   |
\       |   :   |
\       +-------+
\      1         4
\
\ We know the vector from point 1 to point 4 - it's in
\ the (xTemp1 yTemp1 zTemp1) vector - and we know the
\ coordinates of point 2, so we can calculate the
\ coordinates of point 3 by simply adding the vector to
\ the coordinates of point 2, which is what we do next

LDY #2                 \ Set point 3's coordinates to point 2's coordinates +
LDX #3                 \ (xTemp1 yTemp1 zTemp1), so point 3 is now calculated

LDA showLine           \ If showLine is zero, then all four coordinates fit
BEQ prun6              \ within the boundaries of the world, so jump to prun6
\ to keep going

LDA #%01000000         \ At least one of the calculations above overflowed, so
STA objectStatus+1     \ at leat one coordinate is outside the boundaries of
\ the world, so set bit 6 of the status byte for the
\ runway object to indicate that we have now calculated
\ the runway object's coordinates and visibility, and
\ clear bit 7 to indicate that the runway is not visible

RTS                    \ Return from the subroutine

.prun6

LDX #4                 \ Set the status byte for points 1 to 4 to indicate
JSR SetPointVisibility \ that their coordinates and visibility have been
\ calculated
```