Aviator on the BBC Micro

# Flight model: ProcessLanding (Part 6 of 7)

```       Name: ProcessLanding (Part 6 of 7)                            [Show more]
Type: Subroutine
Category: Flight model
Summary: If we are not taxiing, process the landing
Deep dive: Take-offs and landings
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

This part processes the touchdown part of the landing, bouncing the plane and
crashing if we are coming down too fast.

.clan10

\ When we jump here from part 3, we know that:
\
\   * We are not on the ground
\
\   * yLandingGear >= yPlaneLo
\
\   * A = yLandingGear
\
\ The second condition means that the plane is closer to
\ the ground than the distance between the plane and the
\ lowest part of the plane, so we have touched down and
\ the bottom part of the plane is now below ground level
\ (which we now need to fix)

SEC                    \ Set A = (yLandingGear - yPlaneLo) / 2
SBC yPlaneLo           \
LSR A                  \ so A contains half the distance that the wheels would
\ be below the ground if we didn't fix this

CLC                    \ Set yPlaneLo = yLandingGear + A
STA yPlaneLo           \ so the plane bounces up by the distance in A, which is
\ greater, the further "below" the ground the wheels are

LDA yVelocityTop       \ If the top byte of the plane's vertical velocity is
BPL clan11             \ positive, skip the following as the plane is already
\ travelling upwards

SEC                    \ Negate the plane's vertical velocity, so the plane
LDA #0                 \ bounces up by the same speed as it was originally
SBC yVelocityHi        \ heading towards the ground
STA yVelocityHi
LDA #0
SBC yVelocityTop

.clan11

STA yVelocityTop       \ Update the top byte of the plane's vertical velocity
\ (this instruction should really be before the clan11
\ label, as it has no effect if we jump straight here)

\ We now do various velocity checks to make sure we are
\ not coming down to fast (if we are, we crash)

LSR A                  \ Shift bit 0 of the top byte of the plane's vertical
\ velocity into the C flag

BNE clan13             \ If the above shift left any set bits in A, then the
\ top byte of the plane's vertical velocity is >= 2,
\ so yVelocity >= 512, so jump to clan13 to crash the
\ plane, as the we are coming down far too fast to land

LDA yVelocityHi        \ Set A = (yVelocityTop yVelocityHi) / 2
ROR A                  \       = yVelocity / 2
\
\ by shifting the low byte right by one place and
\ shifting the C flag into the top bit

STA R                  \ Set R = A
\       = yVelocity / 2

LDX ucStatus           \ If ucStatus is non-zero, then the undercarriage

\ If we get here then we are descending slowly enough
\ to make the landing, but the undercarriage is up, so
\ the propellor will smash into the ground

DEX                    \ Set propellorStatus = 255 to denote that the propellor
STX propellorStatus    \ is broken, so we can't turn the engine on again

CMP #160               \ If A < 160, i.e. yVelocity < 320, jump to clan14 to
BCC clan14             \ turn the engine off before continuing the landing
\ checks

.clan12

CMP #110               \ If A < 110, i.e. yVelocity < 220, jump to clan15 to
BCC clan15             \ continue the landing checks

.clan13

\ If we get here then at least one of the following is
\ true:
\
\   * yVelocity >= 512
\
\   * The undercarriage is up and yVelocity >= 320
\
\   * The undercarriage is down and yVelocity >= 220
\
\   * The undercarriage is down, we are not landing on
\     the runway, and yVelocity >= 160
\
\ In all these cases we are coming down too fast for the
\ relevant conditions, so the landing has failed
\
\ The above means that if the undercarriage is up, we
\ can successfully land at higher vertical speeds then
\ when the undercarriage is down, though in this case
\ both the propellor and engine get destroyed

JMP Crash              \ Crash the plane and return from the subroutine using a
\ tail call

.clan14

LDA #0                 \ If we get here then the undercarriage is up and we are
JSR SetEngine          \ making a high-speed landing, so turn off the engine

.clan15

\ If we get here then we are descending slowly enough
\ to make the landing

JSR CheckPlaneOnRunway \ Check whether the plane is over the runway

BCC clan16             \ If the plane is on the runway, then jump to clan16

LDA R                  \ We are not landing on the runway, so set the vertical
STA yVelocityHi        \ velocity to R, which halves the vertical velocity

LDX ucStatus           \ If ucStatus is zero, then the undercarriage is up, so
BEQ clan16             \ skip the following

\ If we get here then the undercarriage is down

CMP #80                \ If A < 80, i.e. yVelocity < 160, jump to clan17 to
BCC clan17             \ continue the landing checks without making the sound
\ of a touchdown

BCS clan13             \ Jump to clan13 to crash the plane, as we are
\ descending too fast for an emergency landing with the
\ undercarriage down

.clan16

\ If we get here then either we are landing on the
\ runway, or we are making an emergency landing with
\ the undercarriage up - in either case, we make a sound
\ (though we don't make a sound if this is an emergency
\ landing with the undercarriage down)

LDA #26                \ Make sound #26, the sound of us making contact with
JSR MakeSound          \ the ground while landing

JSR ResetEngineSound   \ Reset the pitch of the engine sound
```