```       Name: UpdateRadarBlip                                         [Show more]
Type: Subroutine
Category: Dashboard
Summary: Update a blip on the radar (runway or alien)
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* MainLoop (Part 5 of 15) calls UpdateRadarBlip

This routine calculates the screen coordinates for a blip on the radar, which
it then passes to DrawRadarBlip to update the blip.

Arguments:

Y                    The item to update on the radar:

* 1 = update the runway

* 33 = update the alien

LDX #0                 \ Set X = 0 act as an offset in the loop below, so we
\ iterate through the x, y and z axes

STX alien              \ Set alien = 0, to indicate that we should draw the
\ runway for when we fall through into DrawRadarBlip
\ below

CPY #33                \ If Y = 33, skip the following two instructions
BNE blip1

LDA #1                 \ Y = 33, which is the alien, so set alien = 1, to
STA alien              \ indicate that we should draw the alien for when we
\ fall through into DrawRadarBlip below

\ We now loop through the x-, y- and z-axes to do the
\ following, where the object is either the runway or
\ the alien:
\
\   xTemp2 = xObject - xPlane
\   yTemp2 = yObject - yPlane
\   zTemp2 = zObject - zPlane
\
\ so (xTemp2 yTemp2 zTemp2) contains the vector from the
\ plane to the object, which is the same as the vector
\ from the centre of the radar to the blip
\
\ Note that we only bother with the top and high bytes
\ of the calculation (where top, high and low are the
\ bytes in a 24-bit number), as the radar isn't accurate
\ enough to show the low byte, so we can just ignore it
\
\ As the object coordinates don't have a top byte, we
\ use 0 rather than the non-existent xObjectTop
\
\ The loop comments are for the xTemp2 iteration

.blip1

LDA xObjectHi,Y        \ Set (xTemp2Hi xTemp2Lo) to the following:
SEC                    \
SBC xPlaneHi,X         \   (0 xObjectHi) - (xPlaneTop xPlaneHi)
STA xTemp2Lo,X         \
\ starting with the top bytes

LDA #0                 \ and then the high bytes (we don't bother with the low
SBC xPlaneTop,X        \ bytes)
STA xTemp2Hi,X

TYA                    \ Point Y to the next axis (xObject, yObject, zObject)
CLC
TAY

INX                    \ Increment X to move on to the next axis

CPX #3                 \ Loop back until we have done all 3 axes
BNE blip1

\ We now want to calculate the coordinates for this
\ vector when rotated correctly, so we first set up
\ the coordinates, and then rotate them

LDX #LO(xTemp2Lo)      \ Set X so the call to CopyWorkToPoint copies the
\ coordinates from (xTemp2, yTemp2, zTemp2)

LDY #0                 \ Set Y so the call to CopyWorkToPoint copies the
\ coordinates to point 0

STY GG                 \ Set GG = 0

JSR CopyWorkToPoint    \ Copy the coordinates from (xTemp2, yTemp2, zTemp2)
\ to point 0

LDA #0                 \ Set the matrix number so the call to SetPointCoords
STA matrixNumber       \ uses matrix 1 in the calculation, which will rotate
\ the point by the plane's pitch, roll and yaw angles,
\ transforming it from the outside world's frame of
\ reference to the plane's frame of reference

JSR SetPointCoords     \ Calculate the coordinates for point 0

\ We now take the rotated x- and z-coordinates and
\ scale them down so they work as screen coordinates
\ within the range of the radar display (we can ignore
\ the y-coordinate, as the radar is a top-down display
\ that ignores altitude)
\
\ Specifically, we do this by dividing the z-coordinate
\ by 8, and the x-coordinate by 16, and then using the
\ low bytes of the result as the radar coordinates
\ (along the sign bit from the high byte of the result)
\
\ For the z-coordinate, his reduces the value from the
\ range -256 to +256 down to -32 to +32, which maps onto
\ the four character rows above and below the centre of
\ the radar (each of which contains eight pixels, so the
\ vertical range of the radar is -32 to +32 pixels, as
\ 8 * 4 = 32)
\
\ For the x-coordinate, we halve it again as mode 5
\ pixels are twice as wide as they are high

LDA xPointHi           \ Set R = xPointHi so we can shift xPoint below without
STA R                  \ affecting the value of xPointHi

LDA zPointHi           \ Set S = zPointHi so we can shift zPoint below without
STA S                  \ affecting the value of zPointHi

LDX #3                 \ We now want to shift the point values right by 3
\ places, so set a shift counter in X

.blip2

LSR R                  \ Set (R xPointLo) = (R xPointLo) >> 1
ROR xPointLo           \                  = xPoint / 2

LSR S                  \ Set (S zPointLo) = (S zPointLo) >> 1
ROR zPointLo           \                  = zPoint / 2

DEX                    \ Decrement the shift counter

BPL blip2              \ Loop back until we have shifted right three times

\ Because mode 5 pixels are twice as wide as they are
\ high, we need to halve the x-coordinate one more time
\ to get the correct result for the pixel x-coordinate

LSR R                  \ Set (R A) = (R xPointLo) >> 1
LDA xPointLo
ROR A

ADC #0                 \ Add bit 0 of the original value to round up the
STA xPointLo           \ division and store the result in xPointLo

\ We now fall through into DrawRadarBlip to erase and
\ redraw the blip on the radar at the coordinates in
\ (xPointLo, zPointLo)
```