Skip to navigation

Random numbers

How Aviator generates random numbers

Randomness is an essential part of gaming, and in particular simulation; if a simulator's behaviour is predictable, it feels less like a simulation and more like a computer program.

Aviator uses a common method of adding a random element to the code: it reads the 6522 User VIA's timer, and specifically the T1C-L timer 1 low-order counter, which is mapped to memory in SHEILA &64. This timer decrements one million times a second, so assuming it is read in a fairly asynchronous manner, the result will normally be a pretty random number.

This approach is used in the following places:

  • The SpawnAlien routine uses the VIA timer to pick a random field for spawning each alien when creating a new wave of aliens.
  • The bumpy ride that is simulated in ProcessLanding when performing an emergency landing uses the VIA timer to decide which way to roll the aircraft.
  • The ApplyBumpyRide routine uses the VIA timer to calculate a random amount in the range 0 to zVelocityPHi, which is then applied to the plane's roll or altitude (so the faster the plane, the bumpier the ride, but in a random way).
  • The SetRandomNumber routine adds a new random number to the randomNumbers list, using the VIA timer to generate the new number.

The random numbers generated by the SetRandomNumber routine are read by the NextRandomNumber routine. This is done in the following places:

  • When turbulence is calculated in part 3 of the ApplyFlightModel routine following an alien explosion, it pulls random numbers from the list to help calculate the turbulence to apply to the three flight control axes.
  • The ExplodeAlien routine pulls random numbers from the list when moving the alien's points around, so it explodes in an unpredictable fashion.

See the deep dive on explosions and turbulence for more details on the explosion process.

Interestingly, no random numbers are used when working out the alien's tactics for attacking the town; aliens are completely predictable, it seems. See the deep dive on Aliens attack Acornsville! for more details on aliens and their habits.