Skip to navigation

Version analysis of Main game loop (Part 1 of 6)

This code appears in the following versions (click to see it in the source code):

Code variations between these versions are shown below.

Name: Main game loop (Part 1 of 6) Type: Subroutine Category: Main loop

Code variation 1 of 8A variation in the comments only

Tap on a block to expand it, and tap it again to revert.

Summary: Spawn a trader (a peaceful Cobra Mk III)
Summary: Spawn a trader (a Cobra Mk III, Python, Boa or Anaconda)
Deep dive: Program flow of the main game loop Ship data blocks Aggression and hostility in ship tactics
This is part of the main game loop. This is where the core loop of the game lives, and it's in two parts. The shorter loop (just parts 5 and 6) is iterated when we are docked, while the entire loop from part 1 to 6 iterates if we are in space. This section covers the following:

Code variation 2 of 8A variation in the comments only

Tap on a block to expand it, and tap it again to revert.

* Spawn a trader, i.e. a Cobra Mk III with AI disabled, a 50% chance of it having an E.C.M., a speed between 16 and 31, a random aggression level and a gentle clockwise roll We call this from within the main loop, with A set to a random number.
* Spawn a trader, i.e. a Cobra Mk III, Python, Boa or Anaconda, with a 50% chance of it having an E.C.M., a 50% chance of it docking, a random aggression level, a speed between 16 and 31, and a gentle clockwise roll We call this from within the main loop.
.MTT4

Code variation 3 of 8Specific to an individual platform

This variation is blank in the Cassette and Electron versions.

JSR DORND \ Set A and X to random numbers
 LSR A                  \ Clear bit 7 of our random number in A and set the C
                        \ flag to bit 0 of A, which is random

 STA INWK+32            \ Store this in the ship's AI flag, so this ship does
                        \ not have AI

 STA INWK+29            \ Store A in the ship's roll counter, giving it a
                        \ clockwise roll (as bit 7 is clear), and a 1 in 127
                        \ chance of it having no damping

 ROL INWK+31            \ This instruction would appear to set bit 0 of the
                        \ ship's missile count randomly (as the C flag was set),
                        \ giving the ship either no missiles or one missile
                        \
                        \ However, INWK+31 is overwritten in the call to the
                        \ NWSHP routine below, where it is set to the number of
                        \ missiles from the ship blueprint, and the value of the
                        \ C flag is not used, so this instruction actually has
                        \ no effect
                        \
                        \ Interestingly, the original source code for the NWSPS
                        \ routine also has an instruction that sets INWK+31 and
                        \ which gets overwritten when it falls through into
                        \ NWSHP, but in this case the instruction is commented
                        \ out in the source. Perhaps the original version of
                        \ NWSHP didn't set the missile count and instead relied
                        \ on the calling code to set it, and when the authors
                        \ changed it, they commented out the INWK+31 instruction
                        \ in NWSPS and forgot about this one. Who knows?

Code variation 4 of 8Minor and very low-impact

Tap on a block to expand it, and tap it again to revert.

AND #31 \ Set the ship speed to our random number, set to a ORA #16 \ minimum of 16 and a maximum of 31 STA INWK+27
AND #15 \ Set the ship speed to our random number, set to a ORA #16 \ minimum of 16 and a maximum of 31 STA INWK+27

Code variation 5 of 8Related to an enhanced feature

Traders in the enhanced versions can spawn in docking mode, in which case they mind their own business.

This variation is blank in the Cassette and Electron versions.

JSR DORND \ Set A and X to random numbers, plus the C flag BMI nodo \ If A is negative (50% chance), jump to nodo to skip \ the following \ If we get here then we are going to spawn a ship that \ is minding its own business and trying to dock LDA INWK+32 \ Set bits 6 and 7 of A, so the ship has AI (bit 7) and ORA #%11000000 \ an aggression level of at least 32 out of 63 (this STA INWK+32 \ makes the ship more likely to turn towards its target, \ which in this case is the space station, as we are \ about to set the ship flags so it is docking) LDX #%00010000 \ Set bit 4 of the ship's NEWB flags, to indicate that STX NEWB \ this ship is docking .nodo

Code variation 6 of 8Related to an enhanced feature

Traders in the enhanced versions can be one of the following: Cobra Mk III, Python, Boa or Anaconda (in the cassette and Electron versions, they are always Cobras).

Tap on a block to expand it, and tap it again to revert.

LDA #CYL \ Add a new Cobra Mk III to the local bubble and fall JSR NWSHP \ through into the main game loop again
AND #2 \ This reduces A to a random value of either 0 or 2 ADC #CYL \ Set A = A + C + #CYL \ \ where A is 0 or 2 and C is 0 or 1, so this gives us a \ ship type from the following: Cobra Mk III, Python, \ Boa or Anaconda

Code variation 7 of 8Related to an advanced feature

The advanced versions have rock hermits, which are classed as junk but can release ships if attacked.

This variation is blank in the Cassette, Disc (flight) and Electron versions.

CMP #HER \ If A is now the ship type of a rock hermit, jump to BEQ TT100 \ TT100 to skip the following instruction

Code variation 8 of 8Specific to an individual platform

This variation is blank in the Cassette and Electron versions.

JSR NWSHP \ Add a new ship of type A to the local bubble and fall \ through into the main game loop again