Skip to navigation


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

[NES version, Bank 0]

Name: Main flight loop (Part 14 of 16) [Show more] Type: Subroutine Category: Main loop Summary: Spawn a space station if we are close enough to the planet Deep dive: Program flow of the main game loop Scheduling tasks with the main loop counter Ship data blocks The space station safe zone
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: * Spawn a space station if we are close enough to the planet (every 32 iterations of the main loop)
LDA MJ ; If we are in witchspace, jump down to MA23S to skip BNE MA23S ; the following, as there are no space stations in ; witchspace LDA MCNT ; Fetch the main loop counter and calculate MCNT mod 32, AND #31 ; jumping to MA93 if it is on-zero (so the following BNE MA93 ; code only runs every 32 iterations of the main loop) LDA SSPR ; If we are inside the space station safe zone, jump to BNE MA23S ; MA23S to skip the following, as we already have a ; space station and don't need another TAY ; Set Y = A = 0 (A is 0 as we didn't branch with the ; previous BNE instruction) JSR MAS2 ; Call MAS2 to calculate the largest distance to the BNE MA23S ; planet in any of the three axes, and if it's ; non-zero, jump to MA23S to skip the following, as we ; are too far from the planet to bump into a space ; station ; We now want to spawn a space station, so first we ; need to set up a ship data block for the station in ; INWK that we can then pass to NWSPS to add a new ; station to our bubble of universe. We do this by ; copying the planet data block from K% to INWK so we ; can work on it, but we only need the first 29 bytes, ; as we don't need to worry about bytes #29 to #35 ; for planets (as they don't have rotation counters, ; AI, explosions, missiles or energy levels) LDX #28 ; So we set a counter in X to copy 29 bytes from K%+0 ; to K%+28 .MAL4 LDA K%,X ; Load the X-th byte of K% and store in the X-th byte STA INWK,X ; of the INWK workspace DEX ; Decrement the loop counter BPL MAL4 ; Loop back for the next byte until we have copied the ; first 28 bytes of K% to INWK JSR SpawnSpaceStation ; If we are close enough, add a new space station to our ; local bubble of universe BCS MA23S ; If we spawned the space station, jump to MA23S to skip ; the following ; We didn't manage to spawn the space station, so now ; we rotate the planet around the x-axis and then the ; z-axis, and then have another go ; ; We rotate the planet by swapping the orientation ; vectors as follows (in parallel): ; ; * Set nosev = roofv ; ; * Set roofv = sidev ; ; * Set sidev = nosev ; ; First we need to set up a ship data block for the ; station in INWK again, which we do by copying the ; first nine bytes from the planet data block in K% to ; INWK (we only need the coordinates and the orientation ; vectors for the calculation, and we'll do the latter ; in a minute) LDX #8 ; Set a counter in X to copy 9 bytes from K%+0 to K%+8 .main44 LDA K%,X ; Load the X-th byte of K% and store in the X-th byte STA INWK,X ; of the INWK workspace DEX ; Decrement the loop counter BPL main44 ; Loop back for the next byte until we have copied the ; all 9 bytes of coordinate data from K% to INWK ; Next we rotate the orientation vectors LDX #5 ; Set a counter in X to swap each of the six bytes of ; the orientation vectors (the comments below are for ; when X = 0, in which we swap the x_lo bytes) .main45 LDY INWK+9,X ; Set Y to nosev_x_lo LDA INWK+15,X ; Set nosev_x_lo = roofv_x_lo STA INWK+9,X LDA INWK+21,X ; Set roofv_x_lo = sidev_x_lo STA INWK+15,X STY INWK+21,X ; Set sidev_x_lo = Y = nosev_x_lo DEX ; Decrement the loop counter BPL main45 ; Loop back until we have swapped all six bytes of each ; orientation vector JSR SpawnSpaceStation ; Now we check to see if we are close enough to the ; rotated planet, and if so, add a new space station to ; our local bubble of universe .MA23S JMP MA23 ; Jump to MA23 to skip the following planet and sun ; altitude checks