Skip to navigation

Elite on the BBC Micro and NES

Universe: KILLSHP

[NES version, Bank 0]

Name: KILLSHP [Show more] Type: Subroutine Category: Universe Summary: Remove a ship from our local bubble of universe
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * KS1 calls KILLSHP

Remove the ship in slot X from our local bubble of universe. This happens when we kill a ship, collide with a ship and destroy it, or when a ship moves outside our local bubble. We also use this routine when we move out of range of the space station, in which case we replace it with the sun. When removing a ship, this creates a gap in the ship slots at FRIN, so we shuffle all the later slots down to close the gap. We also shuffle the ship data blocks at K% to reclaim all the memory that the removed ship used to occupy.
Arguments: X The slot number of the ship to remove XX0 The address of the blueprint for the ship to remove INF The address of the data block for the ship to remove
.KILLSHP STX XX4 ; Store the slot number of the ship to remove in XX4 JSR HideFromScanner_b1 ; Hide the ship from the scanner LDX XX4 ; Set X to the slot number of the ship to remove LDA MSTG ; Check whether this slot matches the slot number in CMP XX4 ; MSTG, which is the target of our missile lock BNE KS5 ; If our missile is not locked on this ship, jump to KS5 LDY #108 ; Otherwise we need to remove our missile lock, so call JSR ABORT ; ABORT to disarm the missile and update the missile ; indicators on the dashboard to the pattern number in ; Y (black indicator = pattern 108) LDA #200 ; Print recursive token 40 ("TARGET LOST") as an JSR MESS ; in-flight message .KS5 LDY XX4 ; Restore the slot number of the ship to remove into Y LDX FRIN,Y ; Fetch the contents of the slot, which contains the ; ship type CPX #SST ; If this is the space station, then jump to KS4 to BNE kill1 ; replace the space station with the sun JMP KS4 .kill1 CPX #CON ; Did we just kill the Constrictor from mission 1? If BNE lll ; not, jump to lll LDA TP ; We just killed the Constrictor from mission 1, so set ORA #%00000010 ; bit 1 of TP to indicate that we have successfully STA TP ; completed mission 1 INC TALLY+1 ; Award 256 kill points for killing the Constrictor .lll CPX #HER ; Did we just kill a rock hermit? If we did, jump to BEQ blacksuspenders ; blacksuspenders to decrease the junk count CPX #JL ; If JL <= X < JH, i.e. the type of ship we killed in X BCC KS7 ; is junk (escape pod, alloy plate, cargo canister, CPX #JH ; asteroid, splinter, Shuttle or Transporter), then keep BCS KS7 ; going, otherwise jump to KS7 .blacksuspenders DEC JUNK ; We just killed junk, so decrease the junk counter .KS7 DEC MANY,X ; Decrease the number of this type of ship in our little ; bubble, which is stored in MANY+X (where X is the ship ; type) LDX XX4 ; Restore the slot number of the ship to remove into X .KSL1 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 INX ; On entry, X points to the empty slot we want to ; shuffle the next ship into (the destination), so ; this increment points X to the next slot - i.e. the ; source slot we want to shuffle down LDA FRIN,X ; Copy the contents of the source slot into the STA FRIN-1,X ; destination slot BNE P%+5 ; If the slot we just shuffled down is not empty, then ; skip the following instruction JMP KS2 ; The source slot is empty and we are done shuffling, ; so jump to KS2 to move on to processing missiles TXA ; Set Y = X * 2 so it can act as an index into the ASL A ; two-byte lookup table at UNIV, which contains the TAY ; addresses of the ship data blocks. In this case we are ; multiplying X by 2, and X contains the source ship's ; slot number so Y is now an index for the source ship's ; entry in UNIV LDA UNIV,Y ; Set SC(1 0) to the address of the data block for the STA SC ; source ship LDA UNIV+1,Y STA SC+1 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 ; We have now set up our variables as follows: ; ; SC(1 0) points to the source's ship data block ; ; INF(1 0) points to the destination's ship data block ; ; P(1 0) points to the destination's line heap ; ; so let's start copying data from the source to the ; destination LDY #41 ; We are going to be using Y as a counter for the 42 ; bytes of ship data we want to copy from the source ; to the destination, so we set it to 41 to start things ; off, and will decrement Y for each byte we copy .KSL2 LDA (SC),Y ; Copy the Y-th byte of the source to the Y-th byte of STA (INF),Y ; the destination DEY ; Decrement the counter BPL KSL2 ; Loop back to KSL2 to copy the next byte until we have ; copied the whole block ; We have now shuffled the ship's slot and the ship's ; data block, so we only have the heap data itself to do LDA SC ; First, we copy SC into INF, so when we loop round STA INF ; again, INF will correctly point to the destination for LDA SC+1 ; the next iteration STA INF+1 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 JMP KSL1 ; We have now shuffled everything down one slot, so ; jump back up to KSL1 to see if there is another slot ; that needs shuffling down