Skip to navigation

Elite on the BBC Micro and NES

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

[NES version, Bank 0]

Name: Main flight loop (Part 2 of 16) [Show more] Type: Subroutine Category: Main loop Summary: Calculate the alpha and beta angles from the current pitch and roll of our ship Deep dive: Program flow of the main game loop Pitching and rolling
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: * Calculate the alpha and beta angles from the current pitch and roll Here we take the current rate of pitch and roll, as set by the controller, and convert them into alpha and beta angles that we can use in the matrix functions to rotate space around our ship. The alpha angle covers roll, while the beta angle covers pitch (there is no yaw in this version of Elite). The angles are in radians, which allows us to use the small angle approximation when moving objects in the sky (see the MVEIT routine for more on this). Also, the signs of the two angles are stored separately, in both the sign and the flipped sign, as this makes calculations easier.
; We now check to see if we are allowed to make an ; in-system jump, and set the allowInSystemJump flag ; accordingly LDA auto ; If auto is zero, then the docking computer is not BEQ main2 ; activated, so jump to main2 to move on to the next ; check CLC ; The docking computer is activated, so clear the C flag ; to put into bit 7 of allowInSystemJump, so the ; Fast-forward button is enabled (so we can use it to ; speed up docking) BCC main5 ; Jump to main5 to update allowInSystemJump (this BCC is ; effectively a JMP as we know the C flag is clear) .main2 LDA MJ ; If MJ is zero then we are not in witchspace, so jump BEQ main3 ; to main3 to move on to the next check SEC ; We are in witchspace, so set the C flag to put into ; bit 7 of allowInSystemJump, so in-system jumps are not ; allowed BCS main5 ; Jump to main5 to update allowInSystemJump (this BCS is ; effectively a JMP as we know the C flag is set) .main3 ; If we get here then the docking computer is not ; activated and we are not in witchspace LDA allowInSystemJump ; If bit 7 of allowInSystemJump is clear then jump to BPL main4 ; to main4 to check whether we are too close to the ; planet or sun to make an in-system jump LDA #176 ; Set A = 176 to use as the distance to check in the ; following call to CheckJumpSafety ; ; The default distance checks use A = 128, so this makes ; the distance checks more stringent, as bit 7 of ; allowInSystemJump is set, so we are need to be further ; away from the danger to make a safe jump JSR CheckJumpSafety+2 ; Check whether we are far enough away from the planet ; and sun to be able to do an in-system (fast-forward) ; jump, returning the result in the C flag (clear means ; we can jump, set means we can't), using the distance ; in A JMP main5 ; Jump to main5 to update allowInSystemJump with the ; result of the distance check .main4 JSR CheckJumpSafety ; Check whether we are far enough away from the planet ; and sun to be able to do an in-system (fast-forward) ; jump, returning the result in the C flag (clear means ; we can jump, set means we can't) .main5 ROR allowInSystemJump ; Rotate the C flag into bit 7 of allowInSystemJump, so ; in-system jumps are enabled or disabled accordingly LDX JSTX ; Set X to the current rate of roll in JSTX LDY numberOfPilots ; Set Y to the configured number of pilots, so the ; following checks controller 1 when only one pilot ; is playing, or controller 2 when two pilots are ; playing LDA controller1Left,Y ; If any of the left or right buttons are being pressed ORA controller1Right,Y ; on the controller that's responsible for steering, ORA KY3 ; jump to main6 to skip the following code so we do not ORA KY4 ; apply roll damping BMI main6 LDA #16 ; Apply damping to the roll rate (if enabled), so the JSR cntr ; roll rate in X creeps towards the centre by 16 .main6 ; The roll rate in JSTX increases if we press the right ; button (and the RL indicator on the dashboard goes to ; the right) ; ; This rolls our ship to the right (clockwise), but we ; actually implement this by rolling everything else ; to the left (anti-clockwise), so a positive roll rate ; in JSTX translates to a negative roll angle alpha TXA ; Set A and Y to the roll rate but with the sign bit EOR #%10000000 ; flipped (i.e. set them to the sign we want for alpha) TAY AND #%10000000 ; Extract the flipped sign of the roll rate and store STA ALP2 ; in ALP2 (so ALP2 contains the sign of the roll angle ; alpha) STX JSTX ; Update JSTX with the damped value that's still in X EOR #%10000000 ; Extract the correct sign of the roll rate and store STA ALP2+1 ; in ALP2+1 (so ALP2+1 contains the flipped sign of the ; roll angle alpha) TYA ; Set A to the roll rate but with the sign bit flipped BPL P%+7 ; If the value of A is positive, skip the following ; three instructions EOR #%11111111 ; A is negative, so change the sign of A using two's CLC ; complement so that A is now positive and contains ADC #1 ; the absolute value of the roll rate, i.e. |JSTX| LSR A ; Divide the (positive) roll rate in A by 4 LSR A STA ALP1 ; Store A in ALP1, so we now have: ; ; ALP1 = |JSTX| / 8 if |JSTX| < 32 ; ; ALP1 = |JSTX| / 4 if |JSTX| >= 32 ; ; This means that at lower roll rates, the roll angle is ; reduced closer to zero than at higher roll rates, ; which gives us finer control over the ship's roll at ; lower roll rates ; ; Because JSTX is in the range -127 to +127, ALP1 is ; in the range 0 to 31 ORA ALP2 ; Store A in ALPHA, but with the sign set to ALP2 (so STA ALPHA ; ALPHA has a different sign to the actual roll rate) LDX JSTY ; Set X to the current rate of pitch in JSTY LDY numberOfPilots ; Set Y to the configured number of pilots, so the ; following checks controller 1 when only one pilot ; is playing, or controller 2 when two pilots are ; playing LDA controller1Up,Y ; If any of the up or down buttons are being pressed ORA controller1Down,Y ; on the controller that's responsible for steering, ORA KY5 ; jump to main6 to skip the following code so we do ORA KY6 ; not apply pitch damping BMI main7 LDA #12 ; Apply damping to the pitch rate (if enabled), so the JSR cntr ; pitch rate in X creeps towards the centre by 12 .main7 TXA ; Set A and Y to the pitch rate but with the sign bit EOR #%10000000 ; flipped TAY AND #%10000000 ; Extract the flipped sign of the pitch rate into A STX JSTY ; Update JSTY with the damped value that's still in X STA BET2+1 ; Store the flipped sign of the pitch rate in BET2+1 EOR #%10000000 ; Extract the correct sign of the pitch rate and store STA BET2 ; it in BET2 TYA ; Set A to the pitch rate but with the sign bit flipped BPL P%+4 ; If the value of A is positive, skip the following ; instruction EOR #%11111111 ; A is negative, so flip the bits ADC #1 ; Add 1 to the (positive) pitch rate, so the maximum ; value is now up to 128 (rather than 127) LSR A ; Divide the (positive) pitch rate in A by 8 LSR A LSR A STA BET1 ; Store A in BET1, so we now have: ; ; BET1 = |JSTY| / 32 if |JSTY| < 48 ; ; BET1 = |JSTY| / 16 if |JSTY| >= 48 ; ; This means that at lower pitch rates, the pitch angle ; is reduced closer to zero than at higher pitch rates, ; which gives us finer control over the ship's pitch at ; lower pitch rates ; ; Because JSTY is in the range -131 to +131, BET1 is in ; the range 0 to 8 ORA BET2 ; Store A in BETA, but with the sign set to BET2 (so STA BETA ; BETA has the same sign as the actual pitch rate)