Aviator on the BBC Micro

# Visibility: ProcessHorizonLine

```       Name: ProcessHorizonLine                                      [Show more]
Type: Subroutine
Category: Visibility
Summary: Calculate coordinates for the horizon's start and end points
Deep dive: Visibility checks
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ProcessLine (Part 1 of 7) calls ProcessHorizonLine

Arguments:

L                    The point ID of the horizon line's start point

M                    The point ID of the horizon line's end point

.ProcessHorizonLine

LDX M                  \ Set the end point to (0, 0, 0)
JSR SetPointToOrigin

LDX L                  \ Set X = the point ID of the horizon line's start point

JSR SetPointToOrigin   \ Set the start point to (0, 0, 0)

LDA #&F0               \ Set the x-coordinate of the horizon line's start point
STA xPointHi,X         \ to &F000 by setting the high byte

STX GG                 \ Set GG = the point ID of the horizon line's start
\ point

\ We now do the following loop twice, once for the
\ horizon's start point, and again for the end point

.phor1

BIT xRotationHi        \ If bit 7 of xRotation is set, jump to phor3
BMI phor3

BVS phor4              \ If bit 6 of xRotation is set (so bit 7 is clear

.phor2

\ We get here if one of the following is true:
\
\   * Bit 7 of zRotationHi is clear and bit 6 is clear
\   * Bit 7 of zRotationHi is set and bit 6 is set
\
\ either of which means the plane is the correct way up

LDA #40                \ Set A = 40 to set as point's z-coordinate, so we
\ draw the horizon in front of the plane (which is the
\ direction we are looking)

BNE phor5              \ Jump to phor5 to set this as the point's z-coordinate
\ (this BNE is effectively a JMP as A is never zero)

.phor3

BVS phor2              \ If bit 6 of xRotation is set (so both bits 6 and 7

.phor4

\ We get here if one of the following is true:
\
\   * Bit 7 of zRotationHi is clear and bit 6 is set
\   * Bit 7 of zRotationHi is set and bit 6 is clear
\
\ either of which means the plane is upside down

LDA #216               \ Set A = -40 to use as the point's z-coordinate, so
\ we draw the horizon behind the plane (which is the
\ direction we are looking)

.phor5

STA zPointHi,X         \ Set the z-coordinate of the horizon line's start (or
\ end) point to A

LDA #%10000000         \ Set bit 7 of point X's status byte, to indicate that
ORA pointStatus,X      \ the point's coordinates and visibility have been
STA pointStatus,X      \ calculated

LDA #27                \ Set the matrix number so the call to SetPointCoords
STA matrixNumber       \ uses matrix 4 in the calculation, so it rotates the
\ point by the roll and pitch angles, but not the yaw,
\ as rotating a horizon by a yaw angle has no effect

JSR SetPointCoords     \ Calculate the coordinates for point GG, which will
\ be the start or end point of the horizon

CPX M                  \ If we just calculated the coordinates for the horizon
BEQ phor6              \ line's end point, then we have now done both points,