Skip to navigation

Elite on the BBC Micro and NES

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

[NES version, Bank 0]

Name: Main flight loop (Part 15 of 16) [Show more] Type: Subroutine Category: Main loop Summary: Perform altitude checks with the planet and sun and process fuel scooping if appropriate Deep dive: Program flow of the main game loop Scheduling tasks with the main loop counter The NES combat demo The Trumbles mission
Context: See this subroutine in context in the source code 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 16 iterations of the main loop, on iterations 10 and 20 of each 32) * Perform an altitude check with the sun and process fuel scooping (every 32 iterations of the main loop, on iteration 20 of each 32)
.MA22 LDA MJ ; If we are in witchspace, jump down to MA23S to skip BNE MA23S ; the following, as there are no planets or suns to ; bump into in witchspace .MA93 LDA demoInProgress ; If the demo is not in progress, jump to main46 to skip BEQ main46 ; the following ; If we get here then the demo is in progress, so now we ; check to see if we have destroyed all the demo ships LDA JUNK ; Set Y to the number of pieces of space junk (in JUNK) CLC ; plus the number of missiles (in MANY+1) ADC MANY+1 TAY LDA FRIN+2,Y ; There are Y non-ship items in the bubble, so if slot BNE main46 ; Y+2 is not empty (given that the first two slots are ; the planet and sun), then this means there is at least ; one ship in the bubble along with the junk and ; missiles, so jump to main46 to skip the following as ; we haven't yet destroyed all the ships in the combat ; practice demo LDA #1 ; If we get here then we have destroyed all the ships in JMP ShowScrollText_b6 ; the demo, so jump to ShowScrollText with A = 1 to show ; the results of combat practice, returning from the ; subroutine using a tail call .main46 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 CMP #10 ; If this is the tenth or twentieth iteration in this BEQ main47 ; block of 32, do the following, otherwise jump to MA29 CMP #20 ; to skip the planet altitude check and move on to the BNE MA29 ; sun distance check .main47 LDA #80 ; If our energy bank status in ENERGY is >= 80, skip CMP ENERGY ; printing the following message (so the message is BCC main48 ; only shown if our energy is low) LDA #100 ; Print recursive token 100 ("ENERGY LOW{beep}") as an JSR MESS ; in-flight message LDY #7 ; Call the NOISE routine with Y = 7 to make a beep to JSR NOISE ; indicate low energy .main48 JSR CheckAltitude ; Perform an altitude check with the planet, ending the ; game if we hit the ground JMP MA23 ; Jump to MA23 to skip to the next section .MA28 JMP DEATH ; If we get here then we just crashed into the planet ; or got too close to the sun, so jump to DEATH to start ; the funeral preparations and return from the main ; flight loop using a tail call .MA29 CMP #15 ; If this is the 15th iteration in this block of 32, BNE MA33 ; do the following, otherwise jump to MA33 to skip the ; docking computer manoeuvring LDA auto ; If auto is zero, then the docking computer is not BEQ MA23 ; activated, so jump to MA23 to skip to the next ; section LDA #123 ; Set A = 123 and jump down to MA34 to print token 123 BNE MA34 ; ("DOCKING COMPUTERS ON") as an in-flight message .MA33 AND #15 ; If this is the 6th iteration in this block of 16, CMP #6 ; do the following, otherwise jump to MA23 to skip the BNE MA23 ; sun altitude check LDA #30 ; Set CABTMP to 30, the cabin temperature in deep space STA CABTMP ; (i.e. one notch on the dashboard bar) LDA SSPR ; If we are inside the space station safe zone, jump to BNE MA23 ; MA23 to skip the following, as we can't have both the ; sun and space station at the same time, so we clearly ; can't be flying near the sun LDY #NIK% ; Set Y to NIK%+4, which is the offset in K% for the ; sun's data block, as the second block at K% is ; reserved for the sun (or space station) JSR MAS2 ; Call MAS2 to calculate the largest distance to the BNE MA23 ; sun in any of the three axes, and if it's non-zero, ; jump to MA23 to skip the following, as we are too far ; from the sun for scooping or temperature changes 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 ; heart of the sun at (x_hi, y_hi, z_hi) EOR #%11111111 ; Invert A, so A is now small if we are far from the ; sun and large if we are close to the sun, in the ; range 0 = far away to $FF = extremely close, ouch, ; hot, hot, hot! ADC #30 ; Add the minimum cabin temperature of 30, so we get ; one of the following: ; ; * If the C flag is clear, A contains the cabin ; temperature, ranging from 30 to 255, that's hotter ; the closer we are to the sun ; ; * If the C flag is set, the addition has rolled over ; and the cabin temperature is over 255 STA CABTMP ; Store the updated cabin temperature BCS MA28 ; If the C flag is set then jump to MA28 to die, as ; our temperature is off the scale CMP #224 ; If the cabin temperature < 224 then jump to MA23 to BCC MA23 ; skip fuel scooping, as we aren't close enough CMP #240 ; If the cabin temperature < 240 then jump to nokilltr BCC nokilltr ; as the heat isn't high enough to kill Trumbles LDA TRIBBLE+1 ; If TRIBBLE(1 0) = 0 then there are no Trumbles in the ORA TRIBBLE ; hold, so jump to nokilltr to skip the following BEQ nokilltr LSR TRIBBLE+1 ; Halve the number of Trumbles in TRIBBLE(1 0) as the ROR TRIBBLE ; cabin temperature is high enough to kill them off ; (this will eventually bring the number down to zero) LDY #31 ; Call the NOISE routine with Y = 31 to make the sound JSR NOISE ; of Trumbles being killed off by the heat of the sun .nokilltr LDA BST ; If we don't have fuel scoops fitted, jump to MA23 to BEQ MA23 ; skip fuel scooping, as we can't scoop without fuel ; scoops LDA DELT4+1 ; We are now successfully fuel scooping, so it's time BEQ MA23 ; to work out how much fuel we're scooping. Fetch the ; high byte of DELT4, which contains our current speed ; divided by 4, and if it is zero, jump to BA23 to skip ; skip fuel scooping, as we can't scoop fuel if we are ; not moving LSR A ; If we are moving, halve A to get our current speed ; divided by 8 (so it's now a value between 1 and 5, as ; our speed is normally between 1 and 40). This gives ; us the amount of fuel that's being scooped in A, so ; the faster we go, the more fuel we scoop, and because ; the fuel levels are stored as 10 * the fuel in light ; years, that means we just scooped between 0.1 and 0.5 ; light years of free fuel ADC QQ14 ; Set A = A + the current fuel level * 10 (from QQ14) CMP #70 ; If A > 70 then set A = 70 (as 70 is the maximum fuel BCC P%+4 ; level, or 7.0 light years) LDA #70 STA QQ14 ; Store the updated fuel level in QQ14 BCS MA23 ; If A >= 70, jump to BA23 to skip fuel scooping, as the ; fuel tanks are already full JSR MakeScoopSound ; Make the sound of the fuel scoops working JSR SetSelectionFlags ; Set the selected system flags for the new system and ; update the icon bar if required LDA #160 ; Set A to token 160 ("FUEL SCOOPS ON") .MA34 JSR MESS ; Print the token in A as an in-flight message