Skip to navigation

Elite on the BBC Micro and NES

Charts: TT23

[NES version, Bank 0]

Name: TT23 [Show more] Type: Subroutine Category: Charts Summary: Show the Short-range Chart
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * RedrawCurrentView calls TT23 * TT102 calls TT23 * TT114 calls TT23
.TT23 LDA #0 ; Set systemsOnChart = 0 so we can use it to count the STA systemsOnChart ; systems as we add them to the chart LDA #199 ; Set the number of pixel rows in the space view to 199, STA Yx2M1 ; to cover the size of the chart part of the Short-range ; Chart view LDA #$9C ; Clear the screen and set the view type in QQ11 to $9C JSR TT66 ; (Short-range Chart) LDX languageIndex ; Move the text cursor to the correct column for the LDA xShortRange,X ; Short-range Chart title in the chosen language STA XC LDA #190 ; Print recursive token 30 ("SHORT RANGE CHART") on the JSR NLIN3 ; top row JSR SetScreenForUpdate ; Get the screen ready for updating by hiding all ; sprites, after fading the screen to black if we are ; changing view JSR TT14 ; Call TT14 to draw a circle with crosshairs at the ; current system's galactic coordinates JSR TT103 ; Draw small crosshairs at coordinates (QQ9, QQ10), ; i.e. at the selected system JSR TT81 ; Set the seeds in QQ15 to those of system 0 in the ; current galaxy (i.e. copy the seeds from QQ21 to QQ15) LDA #0 ; Set A = 0, which we'll use below to zero out the INWK ; workspace STA XX20 ; We're about to start working our way through each of ; the galaxy's systems, so set up a counter in XX20 for ; each system, starting at 0 and looping through to 255 LDX #24 ; First, though, we need to zero out the 25 bytes at ; INWK so we can use them to work out which systems have ; room for a label, so set a counter in X for 25 bytes .EE3 STA INWK,X ; Set the X-th byte of INWK to zero DEX ; Decrement the counter BPL EE3 ; Loop back to EE3 for the next byte until we've zeroed ; all 25 bytes ; We now loop through every single system in the galaxy ; and check the distance from the current system whose ; coordinates are in (QQ0, QQ1). We get the galactic ; coordinates of each system from the system's seeds, ; like this: ; ; x = s1_hi (which is stored in QQ15+3) ; y = s0_hi (which is stored in QQ15+1) ; ; so the following loops through each system in the ; galaxy in turn and calculates the distance between ; (QQ0, QQ1) and (s1_hi, s0_hi) to find the closest one .TT182 SETUP_PPU_FOR_ICON_BAR ; If the PPU has started drawing the icon bar, configure ; the PPU to use nametable 0 and pattern table 0 LDA QQ15+3 ; Set A = s1_hi - QQ0, the horizontal distance between SEC ; (s1_hi, s0_hi) and (QQ0, QQ1) SBC QQ0 BCS TT184 ; If a borrow didn't occur, i.e. s1_hi >= QQ0, then the ; result is positive, so jump to TT184 and skip the ; following two instructions EOR #$FF ; Otherwise negate the result in A, so A is always ADC #1 ; positive (i.e. A = |s1_hi - QQ0|) .TT184 CMP #20 ; If the horizontal distance in A is >= 20, then this BCS TT187 ; system is too far away from the current system to ; appear in the Short-range Chart, so jump to TT187 to ; move on to the next system LDA QQ15+1 ; Set A = s0_hi - QQ1, the vertical distance between SEC ; (s1_hi, s0_hi) and (QQ0, QQ1) SBC QQ1 BCS TT186 ; If a borrow didn't occur, i.e. s0_hi >= QQ1, then the ; result is positive, so jump to TT186 and skip the ; following two instructions EOR #$FF ; Otherwise negate the result in A, so A is always ADC #1 ; positive (i.e. A = |s0_hi - QQ1|) .TT186 CMP #38 ; If the vertical distance in A is >= 38, then this BCS TT187 ; system is too far away from the current system to ; appear in the Short-range Chart, so jump to TT187 to ; move on to the next system ; This system should be shown on the Short-range Chart, ; so now we need to work out where the label should go, ; and set up the various variables we need to draw the ; system's filled circle on the chart LDA QQ15+3 ; Set A = s1_hi - QQ0, the horizontal distance between SEC ; this system and the current system, where |A| < 20. SBC QQ0 ; Let's call this the x-delta, as it's the horizontal ; difference between the current system at the centre of ; the chart, and this system (and this time we keep the ; sign of A, so it can be negative if it's to the left ; of the chart's centre, or positive if it's to the ; right) ASL A ; Set XX12 = 104 + x-delta * 4 ASL A ; ADC #104 ; 104 is the x-coordinate of the centre of the chart, STA XX12 ; so this sets XX12 to the centre 104 +/- 76, the pixel ; x-coordinate of this system LSR A ; Move the text cursor to column x-delta / 2 + 1 LSR A ; which will be in the range 1-10 LSR A CLC ADC #1 STA XC LDA QQ15+1 ; Set A = s0_hi - QQ1, the vertical distance between SEC ; this system and the current system, where |A| < 38. SBC QQ1 ; Let's call this the y-delta, as it's the vertical ; difference between the current system at the centre of ; the chart, and this system (and this time we keep the ; sign of A, so it can be negative if it's above the ; chart's centre, or positive if it's below) ASL A ; Set K4 = 90 + y-delta * 2 ADC #90 ; STA K4 ; 90 is the y-coordinate of the centre of the chart, ; so this sets K4 to the centre 90 +/- 74, the pixel ; y-coordinate of this system LSR A ; Set Y = K4 / 8, so Y contains the number of the text LSR A ; row that contains this system LSR A TAY ; Now to see if there is room for this system's label. ; Ideally we would print the system name on the same ; text row as the system, but we only want to print one ; label per row, to prevent overlap, so now we check ; this system's row, and if that's already occupied, ; the row above, and if that's already occupied, the ; row below... and if that's already occupied, we give ; up and don't print a label for this system LDX INWK,Y ; If the value in INWK+Y is 0 (i.e. the text row BEQ EE4 ; containing this system does not already have another ; system's label on it), jump to EE4 to store this ; system's label on this row INY ; If the value in INWK+Y+1 is 0 (i.e. the text row below LDX INWK,Y ; the one containing this system does not already have BEQ EE4 ; another system's label on it), jump to EE4 to store ; this system's label on this row DEY ; If the value in INWK+Y-1 is 0 (i.e. the text row above DEY ; the one containing this system does not already have LDX INWK,Y ; another system's label on it), fall through into to BNE ee1 ; EE4 to store this system's label on this row, ; otherwise jump to ee1 to skip printing a label for ; this system (as there simply isn't room) .EE4 TYA ; Now to print the label, so move the text cursor to row STA YC ; Y (which contains the row where we can print this ; system's label) CPY #3 ; If Y < 3, then the system would clash with the chart BCC TT187 ; title, so jump to TT187 to skip showing the system LDA #$FF ; Store $FF in INWK+Y, to denote that this row is now STA INWK,Y ; occupied so we don't try to print another system's ; label on this row LDA #%10000000 ; Set bit 7 of QQ17 to switch to Sentence Case STA QQ17 JSR cpl ; Call cpl to print out the system name for the seeds ; in QQ15 (which now contains the seeds for the current ; system) .ee1 LDA #0 ; Now to plot the star, so set the high bytes of K, K3 STA K3+1 ; and K4 to 0 STA K4+1 STA K+1 LDA XX12 ; Set the low byte of K3 to XX12, the pixel x-coordinate STA K3 ; of this system LDA QQ15+5 ; Fetch s2_hi for this system from QQ15+5, extract bit 0 AND #1 ; and add 2 to get the size of the star, which we store ADC #2 ; in K. This will be either 2, 3 or 4, depending on the STA K ; value of bit 0, and whether the C flag is set (which ; will vary depending on what happens in the above call ; to cpl). Incidentally, the planet's average radius ; also uses s2_hi, bits 0-3 to be precise, but that ; doesn't mean the two sizes affect each other ; We now have the following: ; ; K(1 0) = radius of star (2, 3 or 4) ; ; K3(1 0) = pixel x-coordinate of system ; ; K4(1 0) = pixel y-coordinate of system ; ; which we can now pass to the DrawChartSystem routine ; to draw a system on the Short-range Chart JSR DrawChartSystem ; Draw the system on the chart .TT187 JSR TT20 ; We want to move on to the next system, so call TT20 ; to twist the three 16-bit seeds in QQ15 INC XX20 ; Increment the counter BEQ P%+5 ; If X = 0 then we have done all 256 systems, so skip ; the next instruction to return from the subroutine JMP TT182 ; Otherwise jump back up to TT182 to process the next ; system LDA #143 ; Set the number of pixel rows in the space view to 143, STA Yx2M1 ; so the screen height is correctly set for the ; Long-range Chart in case we switch to it using the ; icon in the icon bar (which toggles between the two ; charts) JMP UpdateView ; Update the view, returning from the subroutine using ; a tail call