Flight model: ProcessLanding (Part 1 of 7)

```       Name: ProcessLanding (Part 1 of 7)                            [Show more]
Type: Subroutine
Category: Flight model
Summary: If this is an emergency landing, make the landing a bumpy one
Deep dive: Take-offs and landings
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ApplyFlightModel (Part 6 of 7) calls ProcessLanding

This part checks whether the plane is on the ground but not on the runway, and
if this is the case, it implements a bumpy ride by randomly changing the
plane's height and roll, with larger changes at bigger speeds.

Arguments:

L                    The current value of onGround:

* 0 = we are not on the ground

* 1 = we are on the ground

.ProcessLanding

LDA L                  \ Fetch the current value of onGround

BEQ clan1              \ If onGround is zero then we are not on the ground, so

JSR CheckPlaneOnRunway \ Check whether the plane is over the runway

BCC clan1              \ If the plane is on the runway, then jump to clan1 to
\ skip the bumpy ride

\ If we get here then we are on the ground but we aren't
\ on the runway, so this must be an emergency landing
\ outside the airport, and we apply a suitably bumpy
\ ride, depending on the plane's velocity

ASL landingStatus      \ Shift landingStatus left and set bit 0

LDX #&EE               \ Set X so the call to ApplyBumpyRide updates the yPlane
\ variable, so it makes the plane bump up by a random
\ amount

CLC                    \ Clear the C flag so the call to ApplyBumpyRide
\ adds a random amount to yPlane (rather than taking it
\ away)

LDY #8                 \ Set the scale factor in Y so the random amount we bump
\ the plane up by is in the range 0 to zVelocityP >> 9,
\ so if we try to land at 100 mph, when zVelocityP is
\ (9 64) = 2368, then this would bump the plane up
\ by a random value in the range 0 to 4 (as 2368 >> 9
\ is 4)

JSR ApplyBumpyRide     \ Bump the plane up by a random height in the range 0 to
\ zVelocityP >> 9, so the bump height is higher with
\ higher forward velocity (so the faster we are trying
\ to land, the more the plane bounces up when it hits
\ the runway)

LDX #&EC               \ Set X so the call to ApplyBumpyRide updates the
\ zRotation variable, so it makes the plane roll by a
\ random amount

LDY #4                 \ Set the scale factor in Y so the random amount we roll
\ the plane by is in the range 0 to zVelocityP >> 5,
\ so if we try to land at 100 mph, when zVelocityP is
\ (9 64) = 2368, then this would roll the plane by a
\ random value in the range 0 to 74 (as 2368 >> 5 is 74)

LDA VIA+&64            \ Read the 6522 User VIA T1C-L timer 1 low-order
\ counter (SHEILA &64), which decrements one million
\ times a second and will therefore be pretty random

LSR A                  \ Set the C flag randomly, so the call to ApplyBumpyRide
\ randomly adds or subtracts the random roll amount

JSR ApplyBumpyRide     \ Roll the plane up by a random angle in the range 0 to
\ zVelocityP >> 5, in a random direction, so the roll
\ amount is higher with higher forward velocity (so the
\ faster we are trying to land, the more the plane rolls
\ when it hits the runway)
```