# Flight model: CheckPlaneOnRunway

```       Name: CheckPlaneOnRunway                                      [Show more]
Type: Subroutine
Category: Flight model
Summary: Check whether the plane is over the runway
Deep dive: Take-offs and landings
On-ground calculations
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyFlightModel (Part 5 of 7) calls CheckPlaneOnRunway
* ProcessLanding (Part 1 of 7) calls CheckPlaneOnRunway
* ProcessLanding (Part 6 of 7) calls CheckPlaneOnRunway

The runway runs north-south, and the runway's anchor point is in the southwest
corner of the runway rectangle. The plane is on the runway if:

* The x-axis distance between the plane and the runway's anchor point is
less than 256 (so the runway is 256 wide, and this checks that the plane
is not to the side of the runway)

* The z-axis distance between the plane and the runway's anchor point is
positive, and less than 24 * 256 (so the runway is 24 * 256 long, and
this checks that the plane is not too far to the south or the north of the
runway)

In other words, the runway is a long, thin strip with a north-south alignment
with the strip being 24 times longer than it is wide, and with the anchor
point at the southwest corner.

Returns:

C flag               Determines whether the plane is over the runway:

* Clear if the plane is over the runway

* Set if the plane is not over the runway

.CheckPlaneOnRunway

LDA xPlaneLo           \ Set A to the high byte of the following:
SEC                    \
SBC xObjectLo+1        \   (xPlaneHi xPlaneLo) - (xObjectHi xObjectLo)
LDA xPlaneHi           \
SBC xObjectHi+1        \ for object ID 1, which is the runway, so this
\ calculates the distance in the x-axis between the
\ plane and the runway's anchor point

BNE crun1              \ If the high byte is non-zero, then the plane is more
\ crun1 to return from the subroutine with a negative
\ result

\ If we get here then the plane is over the runway in
\ the x-axis, so now we check the z-axis

LDA zPlaneLo           \ Set A to the high byte of the following:
SEC                    \
SBC zObjectLo+1        \   (zPlaneHi zPlaneLo) - (zObjectHi zObjectLo)
LDA zPlaneHi           \
SBC zObjectHi+1        \ for object ID 1, which is the runway, so this
\ calculates the distance in the z-axis between the
\ plane and the runway's anchor point

BMI crun1              \ If the result is negative then the plane is south of
\ the anchor point, so it can't be on the runway, so
\ negative result

CMP #24                \ Finally, if the high byte is less than 24, then this
\ will clear the C flag, otherwise it will set the C
\ flag

\ By this point, the C flag is only clear if:
\
\   * The distance between the plane and the runway's
\     anchor point in the x-axis is < 256
\
\   * The distance between the plane and the runway's
\     anchor point in the z-axis is < 24 * 256

RTS                    \ Return from the subroutine

.crun1

SEC                    \ Set the C flag to indicate that the plane is not over
\ the runway

RTS                    \ Return from the subroutine
```