Skip to navigation

Elite on the BBC Micro and NES

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

[BBC Master version]

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 Variations: See code variations for this subroutine in the different versions References: This subroutine is called as follows: * Main game loop (Part 1 of 6) calls via TT100 * Main game loop (Part 6 of 6) calls via TT100 * me2 calls via me3

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 me3 Used by me2 to jump back into the main game loop after printing an in-flight message
.TT100 JSR M% \ Call M% to iterate through the main flight loop DEC DLY \ Decrement the delay counter in DLY, so any in-flight \ messages get removed once the counter reaches zero BEQ me2 \ If DLY is now 0, jump to me2 to remove any in-flight \ message from the space view, and once done, return to \ me3 below, skipping the following two instructions BPL me3 \ If DLY is positive, jump to me3 to skip the next \ instruction INC DLY \ If we get here, DLY is negative, so we have gone too \ and need to increment DLY back to 0 .me3 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 following a mis-jump, skip the BNE ytq \ following by jumping down to MLOOP (via ytq above) JSR DORND \ Set A and X to random numbers CMP #35 \ If A >= 35 (87% 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. See the deep dive on \ "Fixing ship positions" for details 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 BVS MTT4 \ If V flag is set (50% chance), jump up to MTT4 to \ spawn a trader 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 MTT1 \ down to MTT1 to skip the following and potentially \ spawn something else 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, set to a ORA #16 \ minimum of 16 and a maximum of 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