Skip to navigation

Elite on the BBC Micro and NES

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

[Acorn Electron version]

Name: Main flight loop (Part 1 of 16) [Show more] Type: Subroutine Category: Main loop Summary: Seed the random number generator Deep dive: Program flow of the main game loop Generating random numbers
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: * DEATH calls via M% * Main game loop (Part 2 of 6) calls via M%

The main flight loop covers most of the flight-specific aspects of Elite. This section covers the following: * Seed the random number generator * Update the sound channel's duration counter to ensure sounds are allocated a minimum duration (unless they are stopped by a higher priority sound)
Other entry points: M% The entry point for the main flight loop
.M% LDA K% \ We want to seed the random number generator with a \ pretty random number, so fetch the contents of K%, \ which is the x_lo coordinate of the planet. This value \ will be fairly unpredictable, so it's a pretty good \ candidate STA RAND \ Store the seed in the first byte of the four-byte \ random number seed that's stored in RAND \ The following processes each sound channel to ensure \ that sounds last for their minimum duration (see the \ SFX variable for more details) LDA #0 \ Set A = 0 so we can use it for resetting the sound \ channel's duration and priority values below LDX #1 \ Set X as a sound channel counter, starting with \ channel 1 and then doing channel 0 .SFXL DEC SFXDU,X \ Decrement this sound channel's SFXDU duration value BPL P%+8 \ If the duration is still positive, skip the following \ two instructions STA SFXDU,X \ The duration just reached zero, so the sound on this STA SFXPR,X \ channel has reached the end of its minimum duration, \ so we zero the channel's SFXDU duration and SFXPR \ priority values so any new sounds that need to be made \ will be made regardless of priority DEX \ Decrement the sound channel BPL SFXL \ Loop back to process the next sound channel until we \ have done both