BBC Micro Elite

# Main loop: Main flight loop (Part 14 of 16) (6502SP version)

```       Name: Main flight loop (Part 14 of 16)                  [View in context]
Type: Subroutine
Category: Main loop
Summary: Spawn a space station if we are close enough to the planet

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

* Spawn a space station if we are close enough to the planet (every 32
iterations of the main loop)

LDA MJ                 \ If we are in witchspace, jump down to MA23S to skip
BNE MA23S              \ the following, as there are no space stations in
\ witchspace

LDA MCNT               \ Fetch the main loop counter and calculate MCNT mod 32,
AND #31                \ jumping to MA93 if it is on-zero (so the following
BNE MA93               \ code only runs every 32 iterations of the main loop

LDA SSPR               \ If we are inside the space station safe zone, jump to
BNE MA23S              \ MA23S to skip the following, as we already have a
\ space station and don't need another

TAY                    \ Set Y = A = 0 (A is 0 as we didn't branch with the
\ previous BNE instruction)

JSR MAS2               \ Call MAS2 to calculate the largest distance to the
BNE MA23S              \ planet in any of the three axes, and if it's
\ are too far from the planet to bump into a space
\ station

\ We now want to spawn a space station, so first we
\ need to set up a ship data block for the station in
\ INWK that we can then pass to NWSPS to add a new
\ station to our bubble of universe. We do this by
\ copying the planet data block from K% to INWK so we
\ can work on it, but we only need the first 29 bytes,
\ as we don't need to worry about bytes #29 to #35
\ for planets (as they don't have rotation counters,
\ AI, explosions, missiles, a ship line heap or energy
\ levels)

LDX #28                \ So we set a counter in X to copy 29 bytes from K%+0
\ to K%+28

.MAL4

LDA K%,X               \ Load the X-th byte of K% and store in the X-th byte
STA INWK,X             \ of the INWK workspace

DEX                    \ Decrement the loop counter

BPL MAL4               \ Loop back for the next byte until we have copied the
\ first 28 bytes of K% to INWK

\ We now check the distance from our ship (at the
\ origin) towards the planet's surface, by adding the
\ planet's nosev vector to the planet's centre at
\ (x, y, z) and checking our distance to the end
\ point along the relevant axis

INX                    \ Set X = 0 (as we ended the above loop with X as &FF)

LDY #9                 \ Call MAS1 with X = 0, Y = 9 to do the following:
JSR MAS1               \
\   (x_sign x_hi x_lo) += (nosev_x_hi nosev_x_lo) * 2
\
\   A = |x_hi|

BNE MA23S              \ If A > 0, jump to MA23S to skip the following, as we
\ are too far from the planet in the x-direction to
\ bump into a space station

LDX #3                 \ Call MAS1 with X = 3, Y = 11 to do the following:
LDY #11                \
JSR MAS1               \   (y_sign y_hi y_lo) += (nosev_y_hi nosev_y_lo) * 2
\
\   A = |y_hi|

BNE MA23S              \ If A > 0, jump to MA23S to skip the following, as we
\ are too far from the planet in the y-direction to
\ bump into a space station

LDX #6                 \ Call MAS1 with X = 6, Y = 13 to do the following:
LDY #13                \
JSR MAS1               \   (z_sign z_hi z_lo) += (nosev_z_hi nosev_z_lo) * 2
\
\   A = |z_hi|

BNE MA23S              \ If A > 0, jump to MA23S to skip the following, as we
\ are too far from the planet in the z-direction to
\ bump into a space station

LDA #192               \ Call FAROF2 to compare x_hi, y_hi and z_hi with 192,
JSR FAROF2             \ which will set the C flag if all three are < 192, or
\ clear the C flag if any of them are >= 192

BCC MA23S              \ Jump to MA23S if any one of x_hi, y_hi or z_hi are
\ >= 192 (i.e. they must all be < 192 for us to be near
\ enough to the planet to bump into a space station)

JSR WPLS               \ Call WPLS to remove the sun from the screen, as we
\ can't have both the sun and the space station at the
\ same time

JSR NWSPS              \ Add a new space station to our local bubble of
\ universe

.MA23S

JMP MA23               \ Jump to MA23 to skip the following planet and sun
\ altitude checks
```