Skip to navigation


Main loop: Main game loop (Part 5 of 6)

[NES version, Bank 0]

Name: Main game loop (Part 5 of 6) [Show more] Type: Subroutine Category: Main loop Summary: Cool down lasers, make calls to update the dashboard Deep dive: Program flow of the main game loop The dashboard indicators The Trumbles mission
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * Main game loop (Part 1 of 6) calls via MLOOP * Main game loop (Part 2 of 6) calls via MLOOP * Main game loop (Part 3 of 6) calls via MLOOP * Main game loop (Part 4 of 6) calls via MLOOP * PlayDemo calls via MLOOP

This is the first half of the minimal game loop, which we iterate when we are docked. This section covers the following: * Cool down lasers * Make calls to update the dashboard
Other entry points: MLOOP The entry point for the main game loop. This entry point comes after the call to the main flight loop and spawning routines, so it marks the start of the main game loop for when we are docked (as we don't need to call the main flight loop or spawning routines if we aren't in space)
.MLOOP LDX #$FF ; Set the stack pointer to $01FF, which is the standard TXS ; location for the 6502 stack, so this instruction ; effectively resets the stack LDX GNTMP ; If the laser temperature in GNTMP is non-zero, BEQ EE20 ; decrement it (i.e. cool it down a bit) DEC GNTMP .EE20 LDX LASCT ; Set X to the value of LASCT, the laser pulse count BEQ NOLASCT ; If X = 0 then jump to NOLASCT to skip reducing LASCT, ; as it can't be reduced any further DEX ; Decrement the value of LASCT in X BEQ P%+3 ; If X = 0, skip the next instruction DEX ; Decrement the value of LASCT in X again STX LASCT ; Store the decremented value of X in LASCT, so LASCT ; gets reduced by 2, but not into negative territory .NOLASCT LDA QQ11 ; If this is a space view, skip the following two BEQ P%+7 ; instructions (i.e. jump to JSR TT17 below) LDY #4 ; Wait for 4/50 of a second (0.08 seconds), to slow the JSR DELAY ; main loop down a bit LDA TRIBBLE+1 ; If the high byte of TRIBBLE(1 0), the number of BEQ nobabies ; Trumbles in the hold, is zero, jump to nobabies to ; skip the following ; We have a lot of Trumbles in the hold, so let's see if ; any of them are breeding (note that Trumbles always ; breed when we jump into a new system in the SOLAR ; routine, but when we have lots of them, they also ; breed here in the main flight loop) JSR DORND ; Set A and X to random numbers CMP #220 ; If A >= 220 then set the C flag (14% chance) LDA TRIBBLE ; Add the C flag to TRIBBLE(1 0), starting with the low ADC #0 ; bytes STA TRIBBLE BCC nobabies ; And then the high bytes INC TRIBBLE+1 ; ; So there is a 14% chance of a Trumble being born BPL nobabies ; If the high byte of TRIBBLE(1 0) is now $80, then DEC TRIBBLE+1 ; decrement it back to $7F, so the number of Trumbles ; never goes above $7FFF (32767) .nobabies LDA TRIBBLE+1 ; If the high byte of TRIBBLE(1 0), the number of BEQ NOSQUEEK ; Trumbles in the hold, is zero, jump to NOSQUEEK to ; skip the following ; We have a lot of Trumbles in the hold, so they are ; probably making a bit of a noise LDY CABTMP ; If the cabin temperature is >= 224 then skip the next CPY #224 ; two LSR A instructions and leave the value of A as a BCS P%+4 ; high value, so the chances of the Trumbles making a ; noise in hot temperatures is greater (specifically, ; this is the temperature at which the fuel scoops start ; working) LSR A ; Set A = A / 2 LSR A STA T ; Set T = A, which will be higher with more Trumbles and ; higher temperatures JSR DORND ; Set A and X to random numbers CMP T ; If A >= T then jump to NOSQUEEK to skip making any BCS NOSQUEEK ; noise, so there is a higher chance of Trumbles making ; noise when there are lots of them or the cabin ; temperature is hot enough for the fuel scoops to work AND #3 ; Set Y to our random number reduced to the range 0 to 3 TAY LDA trumbleSounds,Y ; Set Y to the Y-th sound effect from the trumbleSounds TAY ; table, so there's a 75% change of Y being set to 5, ; and a 25% chance of Y being set to 6 JSR NOISE ; Call the NOISE routine to make the sound of the ; Trumbles in Y, which will be one of 5 or 6, with 5 ; more likely than 6 .NOSQUEEK LDA allowInSystemJump ; Set A to the value of allowInSystemJump, which ; determines whether we are allowed to perform an ; in-system jump (which is the same as saying whether ; the fast-forward button is enabled) LDX QQ22+1 ; Fetch into X the number that's shown on-screen during ; the hyperspace countdown BEQ game5 ; If the counter is zero then we are not counting down ; to hyperspace, so jump to game5 to skip the next ; instruction ORA #%10000000 ; Set bit 7 of A to prevent in-system jumps, as there ; is a hyperspace countdown in progress .game5 LDX demoInProgress ; If the demo is not in progress, jump to game6 to skip BEQ game6 ; the following AND #%01111111 ; Clear bit 7 of A to enable the fast-forward button, as ; this is the combat demo and the fast-forward button ; lets us skip the rest of the demo .game6 STA allowInSystemJump ; Store the updated value of A in allowInSystemJump AND #%11000000 ; If bits 6 and 7 of allowInSystemJump are both clear BEQ game7 ; then in-system jumps are allowed, so jump to game7 ; to leave allowInSystemJump alone CMP #%11000000 ; If bits 6 and 7 of allowInSystemJump are both set then BEQ game7 ; in-system jumps are not allowed, so jump to game7 to ; leave allowInSystemJump alone CMP #%10000000 ; If bit 7 of allowInSystemJump is set but bit 6 isn't, ; then this sets the C flag, otherwise it clears the C ; flag ROR A ; This updates allowInSystemJump as follows: STA allowInSystemJump ; ; * %10xxxxxx rotates to %11xxxxxx ; ; * %01xxxxxx rotates to %00xxxxxx ; ; In other words, when bit 7 of the allowInSystemJump ; flag is set, then that condition will spread to both ; bit 6 and 7, but if only bit 6 is set, then the flag ; will clear at this point in the main game loop ; ; So once bit 7 is cleared, in-system jumps will be ; allowed within an iteration of the main loop, assuming ; no more preventative conditions appear JSR UpdateIconBar_b3 ; Update the icon bar to hide or show the in-system jump ; icon bar button, according to the new value of the ; allowInSystemJump flag .game7 JSR TT17 ; Scan the key logger for the directional pad buttons, ; returning the cursor's delta values in X and Y and ; the button pressed in A