Skip to navigation


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

[NES version, Bank 0]

Name: Main game loop (Part 2 of 6) [Show more] Type: Subroutine Category: Main loop Summary: Call the main flight loop, and potentially spawn a trader, an asteroid, or a cargo canister Deep dive: Program flow of the main game loop Ship data blocks Fixing ship positions
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * Main game loop (Part 6 of 6) calls via TT100

This section covers the following: * Call M% to do the main flight loop * Potentially spawn a trader, asteroid or cargo canister
Other entry points: TT100 The entry point for the start of the main game loop, which calls the main flight loop and the moves into the spawning routine
.TT100 LDA nmiTimerLo ; Store the low byte of the NMI timer (which will be STA RAND ; pretty random) in the RAND seed LDA K%+6 ; Store the z_lo coordinate for the planet (which will STA RAND+1 ; be pretty random) in the RAND+1 seed LDA soundVibrato ; Store the soundVibrato variable (which will be pretty STA RAND+3 ; random) in the RAND+3 seed LDA QQ12 ; Fetch the docked flag from QQ12 into A BEQ game2 ; If we are docked, jump down to MLOOP to skip all the JMP MLOOP ; the flight and spawning code in the top part of the ; main loop .game2 JSR M% ; Call M% to iterate through the main flight loop DEC MCNT ; Decrement the main loop counter in MCNT BEQ P%+5 ; If the counter has reached zero, which it will do ; every 256 main loops, skip the next JMP instruction ; (or to put it another way, if the counter hasn't ; reached zero, jump down to MLOOP, skipping all the ; following checks) .ytq JMP MLOOP ; Jump down to MLOOP to do some end-of-loop tidying and ; restart the main loop ; We only get here once every 256 iterations of the ; main loop. If we aren't in witchspace and don't ; already have 3 or more asteroids in our local bubble, ; then this section has a 13% chance of spawning ; something benign (the other 87% of the time we jump ; down to consider spawning cops, pirates and bounty ; hunters) ; ; If we are in that 13%, then 50% of the time this will ; be a Cobra Mk III trader, and the other 50% of the ; time it will either be an asteroid (98.5% chance) or, ; very rarely, a cargo canister (1.5% chance) LDA MJ ; If we are in witchspace (in which case MJ > 0) or ORA demoInProgress ; demoInProgress > 0 (in which case we are playing the BNE ytq ; demo), jump down to MLOOP (via ytq above) JSR DORND ; Set A and X to random numbers CMP #40 ; If A >= 40 (85% chance), jump down to MTT1 to skip BCS MTT1 ; the spawning of an asteroid or cargo canister and ; potentially spawn something else LDA JUNK ; If we already have 3 or more bits of junk in the local CMP #3 ; bubble, jump down to MTT1 to skip the following and BCS MTT1 ; potentially spawn something else JSR ZINF ; Call ZINF to reset the INWK ship workspace LDA #38 ; Set z_hi = 38 (far away) STA INWK+7 JSR DORND ; Set A, X and C flag to random numbers STA INWK ; Set x_lo = random STX INWK+3 ; Set y_lo = random ; ; Note that because we use the value of X returned by ; DORND, and X contains the value of A returned by the ; previous call to DORND, this does not set the new ship ; to a totally random location AND #%10000000 ; Set x_sign = bit 7 of x_lo STA INWK+2 TXA ; Set y_sign = bit 7 of y_lo AND #%10000000 STA INWK+5 ROL INWK+1 ; Set bit 1 of x_hi to the C flag, which is random, so ROL INWK+1 ; this randomly moves us off-centre by 512 (as if x_hi ; is %00000010, then (x_hi x_lo) is 512 + x_lo) JSR DORND ; Set A, X and V flag to random numbers AND #%00110000 ; If either of bits 4 and 5 are set (75% chance), skip BNE P%+5 ; the following instruction JMP MTT4 ; Jump up to MTT4 to spawn a trader (25% chance) ORA #%01101111 ; Take the random number in A and set bits 0-3 and 5-6, STA INWK+29 ; so the result has a 50% chance of being positive or ; negative, and a 50% chance of bits 0-6 being 127. ; Storing this number in the roll counter therefore ; gives our new ship a fast roll speed with a 50% ; chance of having no damping, plus a 50% chance of ; rolling clockwise or anti-clockwise LDA SSPR ; If we are inside the space station safe zone, jump BNE MLOOPS ; down to MLOOPS to stop spawning TXA ; Set A to the random X we set above, which we haven't BCS MTT2 ; used yet, and if the C flag is set (50% chance) jump ; down to MTT2 to skip the following AND #31 ; Set the ship speed to our random number, reduced to ORA #16 ; the range 16 to 31 STA INWK+27 BCC MTT3 ; Jump down to MTT3, skipping the following (this BCC ; is effectively a JMP as we know the C flag is clear, ; having passed through the BCS above) .MTT2 ORA #%01111111 ; Set bits 0-6 of A to 127, leaving bit 7 as random, so STA INWK+30 ; storing this number in the pitch counter means we have ; full pitch with no damping, with a 50% chance of ; pitching up or down .MTT3 JSR DORND ; Set A and X to random numbers CMP #252 ; If random A < 252 (98.8% of the time), jump to thongs BCC thongs ; to skip the following LDA #HER ; Set A to #HER so we spawn a rock hermit 1.2% of the ; time STA INWK+32 ; Set byte #32 to %00001111 to give the rock hermit an ; E.C.M. BNE whips ; Jump to whips (this BNE is effectively a JMP as A will ; never be zero) .thongs CMP #10 ; If random A >= 10 (96% of the time), set the C flag AND #1 ; Reduce A to a random number that's 0 or 1 ADC #OIL ; Set A = #OIL + A + C, so there's a tiny chance of us ; spawning a cargo canister (#OIL) and an even chance of ; us spawning either a boulder (#OIL + 1) or an asteroid ; (#OIL + 2) .whips JSR NWSHP ; Add our new asteroid or canister to the universe