Skip to navigation

Elite on the BBC Micro and NES

Main loop: Main game loop (Part 4 of 6)

[Acorn Electron version]

Name: Main game loop (Part 4 of 6) [Show more] Type: Subroutine Category: Main loop Summary: Potentially spawn a lone bounty hunter or up to four pirates Deep dive: Program flow of the main game loop Ship data blocks Fixing ship positions
Context: See this subroutine in context in the source code Variations: See code variations for this subroutine in the different versions References: No direct references to this subroutine in this source file

This section covers the following: * Potentially spawn (35% chance) either a lone bounty hunter (a Mamba, Python or Cobra Mk III), or a group of up to 4 pirates (Sidewinders and/or Mambas)
DEC EV \ Decrement EV, the extra vessels spawning delay, and BPL MLOOP \ jump to MLOOP if it is still positive, so we only \ do the following when the EV counter runs down INC EV \ EV is negative, so bump it up again, setting it back \ to 0 JSR DORND \ Set A and X to random numbers LDY gov \ If the government of this system is 0 (anarchy), jump BEQ LABEL_2 \ straight to LABEL_2 to start spawning pirates or a \ lone bounty hunter CMP #90 \ If the random number in A >= 90 (65% chance), jump to BCS MLOOP \ MLOOP to stop spawning (so there's a 35% chance of \ spawning pirates or a lone bounty hunter) AND #7 \ Reduce the random number in A to the range 0-7, and CMP gov \ if A is less than government of this system, jump BCC MLOOP \ to MLOOP to stop spawning (so safer governments with \ larger gov numbers have a greater chance of jumping \ out, which is another way of saying that more \ dangerous systems spawn pirates and bounty hunters \ more often) .LABEL_2 \ Now to spawn a lone bounty hunter or a group of \ pirates JSR Ze \ Call Ze to initialise INWK to a potentially hostile \ ship, and set A and X to random values \ \ Note that because Ze uses the value of X returned by \ DORND, and X contains the value of A returned by the \ previous call to DORND, this does not set the new ship \ to a totally random location. See the deep dive on \ "Fixing ship positions" for details CMP #200 \ If the random number in A >= 200 (13% chance), jump BCS mt1 \ to mt1 to spawn pirates, otherwise keep going to \ spawn a lone bounty hunter INC EV \ Increase the extra vessels spawning counter, to \ prevent the next attempt to spawn extra vessels AND #3 \ Set A = Y = random number in the range 3-6, which ADC #3 \ we will use to determine the type of ship TAY \ We now build the AI flag for this ship in A TXA \ First, copy the random number in X to A CMP #200 \ First, set the C flag if X >= 200 (22% chance) ROL A \ Set bit 0 of A to the C flag (i.e. there's a 22% \ chance of this ship having E.C.M.) ORA #%11000000 \ Set bits 6 and 7 of A, so the ship is hostile (bit 6) \ and has AI (bit 7) STA INWK+32 \ Store A in the AI flag of this ship TYA \ Add a new ship of type Y to the local bubble, so JSR NWSHP \ that's a Mamba, Cobra Mk III or Python .mj1 JMP MLOOP \ Jump down to MLOOP, as we are done spawning ships .mt1 AND #3 \ It's time to spawn a group of pirates, so set A to a \ random number in the range 0-3, which will be the \ loop counter for spawning pirates below (so we will \ spawn 1-4 pirates) STA EV \ Delay further spawnings by this number STA XX13 \ Store the number in XX13, the pirate counter .mt3 JSR DORND \ Set A and X to random numbers AND #3 \ Set A to a random number in the range 0-3 ORA #1 \ Set A to %01 or %11 (Sidewinder or Mamba) JSR NWSHP \ Try adding a new ship of type A to the local bubble DEC XX13 \ Decrement the pirate counter BPL mt3 \ If we need more pirates, loop back up to mt3, \ otherwise we are done spawning, so fall through into \ the end of the main loop at MLOOP