Aviator on the BBC Micro

# Flight model: ApplyFlightModel (Part 3 of 7)

```       Name: ApplyFlightModel (Part 3 of 7)                          [Show more]
Type: Subroutine
Category: Flight model
Summary: If we are near to an exploding alien, apply turbulence
Deep dive: The flight model
Explosions and turbulence
Context: See this subroutine in context in the source code
References: No direct references to this subroutine in this source file

This part does the following:

* If we are near an exploding alien, calculate the amount of turbulence and
apply it to the (xControlsSc, yControlsSc, zControlsSc) vector, as
follows:

* Set the level of turbulence in the range 0 to 1920, to be inversely
proportional to the distance from the explosion (so it is 0 if we are
far away, 1920 if we are very close).

* We randomly add or subtract this amount from each axis (with each axis
being individually signed randomly), to give:

xControlsSc = xControlsSc +/- turbulence / 2

yControlsSc = yControlsSc +/- turbulence / 2

zControlsSc = zControlsSc +/- turbulence / 2

LDA hitTimer           \ If the hit timer is zero, then there are no exploding
BEQ fmod3              \ aliens, so jump to fmod3 to skip this part

\ We now apply turbulence to the plane by applying a
\ random amount to xControlsSc, yControlsSc and
\ zControlsSc, with the random amount being positive or
\ negative, and with a larger magnitude the closer we
\ are to the alien

LDX #&6A               \ Set X so in the following loop, the call to AddScaled
\ updates xControlsSc, yControlsSc and zControlsSc, one
\ on each iteration of the loop
\
\ The comments below are for xControlsSc

.fmod2

JSR NextRandomNumber   \ Set A to point to the next item in the randomNumbers
\ list

TAY                    \ Set P to the next random number from the list
LDA randomNumbers+1,Y
STA P

LDA distanceFromHit    \ If distanceFromHit >= 16, jump to fmod3 to skip the
CMP #16                \ following, as we are too far from the exploding alien
BCS fmod3              \ to feel the effects

EOR #&0F               \ As A < 16, this flips the value of A so instead of
\ being in the range 0 to 15, it's reversed to the
\ range 15 to 0 - so A is now 15 if we are really close
\ to the exploding alien, or 0 if we are further away

ASL A                  \ Double the value of A, so it's in the range 0 to 30,
\ with the number being higher the closer we are to the
\ exploding alien

LSR P                  \ We set P to a random number above, so this sets the C
\ or subtracts the amount of turbulence in A

LDY #1                 \ Set the scale factor in Y so we add or subtract
\ (A 0) >> 2, which is in the range 0 to 1920, as
\ (30 0) >> 2 is 1920

JSR AddScaled          \ Set xControlsSc = xControlsSc +/- (A 0) >> 2
\
\ So this changes xControlsSc by an amount in the range
\ 0 to 1920, with a higher amount the closer we are to
\ the exploding alien, with the sign being random (i.e.
\ it is random whether we add or subtract the amount,
\ but the amount itself is not random)

INX                    \ Increment the value of X so the next iteration updates
\ the next of xControlsSc, yControlsSc and zControlsSc

CPX #&6D               \ Loop back until we have applied turbulence to all
BNE fmod2              \ three axes
```