# Main loop: Main flight loop (Part 15 of 16)

## [Acorn Electron version]

```       Name: Main flight loop (Part 15 of 16)                        [Show more]
Type: Subroutine
Category: Main loop
Summary: Perform altitude checks with the planet
Deep dive: Program flow of the main game loop
Scheduling tasks with the main loop counter
Context: See this subroutine in context in the source code
Variations: See code variations for this subroutine in the different versions
References: No direct references to this subroutine in this source file

The main flight loop covers most of the flight-specific aspects of Elite. This
section covers the following:

* Perform an altitude check with the planet (every 32 iterations of the main
loop, on iteration 10 of each 32)

.MA22

LDA MCNT               \ Fetch the main loop counter and calculate MCNT mod 32,
AND #31                \ which tells us the position of this loop in each block
\ of 32 iterations

.MA93

CMP #10                \ If this is the tenth iteration in this block of 32,
BNE MA29               \ do the following, otherwise jump to MA29 to skip the
\ planet altitude check

LDA #50                \ If our energy bank status in ENERGY is >= 50, skip
CMP ENERGY             \ printing the following message (so the message is
BCC P%+6               \ only shown if our energy is low)

ASL A                  \ Print recursive token 100 ("ENERGY LOW{beep}") as an
JSR MESS               \ in-flight message

LDY #&FF               \ Set our altitude in ALTIT to &FF, the maximum
STY ALTIT

INY                    \ Set Y = 0

JSR m                  \ Call m to calculate the maximum distance to the
\ planet in any of the three axes, returned in A

BNE MA23               \ If A > 0 then we are a fair distance away from the
\ planet in at least one axis, so jump to MA23 to skip
\ the rest of the altitude check

JSR MAS3               \ Set A = x_hi^2 + y_hi^2 + z_hi^2, so using Pythagoras
\ we now know that A now contains the square of the
\ distance between our ship (at the origin) and the
\ centre of the planet at (x_hi, y_hi, z_hi)

BCS MA23               \ If the C flag was set by MAS3, then the result
\ overflowed (was greater than &FF) and we are still a
\ fair distance from the planet, so jump to MA23 as we
\ haven't crashed into the planet

SBC #36                \ Subtract 36 from x_hi^2 + y_hi^2 + z_hi^2
\
\ When we do the 3D Pythagoras calculation, we only use
\ the high bytes of the coordinates, so that's x_hi,
\ y_hi and z_hi and
\
\ The planet radius is (0 96 0), as defined in the
\ PLANET routine, so the high byte is 96
\
\ When we square the coordinates above and add them,
\ the result gets divided by 256 (otherwise the result
\ wouldn't fit into one byte), so if we do the same for
\ the planet's radius, we get:
\
\   96 * 96 / 256 = 36
\
\ So for the planet, the equivalent figure to test the
\ sum of the _hi bytes against is 36, so A now contains
\ the high byte of our altitude above the planet
\ surface, squared

BCC MA28               \ If A < 0 then jump to MA28 as we have crashed into
\ the planet

STA R                  \ We are getting close to the planet, so we need to
JSR LL5                \ work out how close. We know from the above that A
\ contains our altitude squared, so we store A in R
\ and call LL5 to calculate:
\
\   Q = SQRT(R Q) = SQRT(A Q)
\
\ Interestingly, Q doesn't appear to be set to 0 for
\ this calculation, so presumably this doesn't make a
\ difference

LDA Q                  \ Store the result in ALTIT, our altitude
STA ALTIT

BNE MA23               \ If our altitude is non-zero then we haven't crashed,