7. Opponents and the rules of single player
The four alien kinds look clever, but each is a small program with a handful of states and a few distance thresholds. This chapter gives their numbers, the steering trick they all share, their behaviours step by step, and then the rules of a single-player game: pods, pick-ups, levels and score. Speeds are in world units per frame; the game runs at 18.2 frames per second in single player.
The four kinds
| Kind | Hits to destroy | Top speed | Turn rate | Weapon | Points |
|---|---|---|---|---|---|
| Pirate | 2 | 125 (rises from level 9, reaching 250 at level 13) | 24 steps (4.2°) per frame | homing missile, slow-turning | 100 |
| Fighter | 2 | 175 | 24 (4.2°) | laser | 150 |
| Guardian | 4 | 275 | 32 (5.6°) | laser | 200 |
| Commander | 8 | 225 | 32 (5.6°) | homing missile, agile | 400 |
| mutant | a Commander's numbers and its own model; fires a laser every frame while aimed (see below) | ||||
The table lives in src/tables.c (its "speed" column holds four times the numbers above, chapter 3). The Pirate's speed is changed per level: 125 up to level 8, then 150, 175, 200, 225 and from level 13 on 250 units per frame. Each alien starts at its top speed and, apart from the mutant, never accelerates on its own: only its behaviour code changes its speed. The level introduction screen shows the table's raw values ("Speed Factor" = 4 × the speeds above, for example 500 for a level-1 Pirate; "Turn Factor"; "Shield Strength" = hits to destroy) together with how many of each kind will come.
How aliens steer: one shared trick
Every steered object (aliens, missiles, and pods being towed or returning) uses the same few helpers in src/steer.c. The idea fits in three steps:
- Look at the goal from your own cockpit. Take the vector to the goal (fleeing uses the vector to you as well, and then turns the opposite way, see the exact rules below), shrink or grow it into a direction plus a distance exponent (chapter 3), and dot it with your own three axes. That gives three numbers: how far the goal is to your right (r), toward your "up" (u), and ahead (f).
- Turn proportionally. Pitch rate = u ÷ 128 and yaw rate = r ÷ 128 (in steps per frame),
each limited to the object's maximum turn rate. A goal slightly off the nose gives a gentle turn,
one far off gives the full rate. Roll is always 0. Outside the aimed cone (step 3) a rate is never
0: the division rounds down (an arithmetic shift by 7 bits, so −1 ÷ 128 gives −1) and a result of
exactly 0 is replaced by +1. In JavaScript that is
u >> 7, notMath.trunc(u / 128). - Special cases. If the goal is behind (f < 0), forget yaw and pitch round at the full rate toward the side the goal is on: a loop, the fastest way to turn around. If the goal is inside a narrow cone around the nose (|r| and |u| below 128, or 256 for some helpers: well under 2°), stop turning: "aimed".
Five helpers apply this to different goals:
| Helper | Goal | Speed | Tells the caller |
|---|---|---|---|
| chase | you | when aimed: speed up by the acceleration up to the top speed; with you behind: slow down by the acceleration while above 50 units per frame (of the aliens only the mutant has an acceleration, 12.5; missiles have one too) | "aimed at you" |
| flee | directly away from you (tail toward you) | speeds up whenever you are not ahead of it; with you ahead it brakes like chase | nothing |
| to object | a body (a pod, a missile's target, a towing alien) | like chase, with a wider aim cone, but with the goal behind it brakes all the way down to 0 | the steering exponent to the goal |
| tow | points the tail at a body (to drag it behind) | unchanged | "it is straight behind" |
| to point | a fixed place (the centre of the pod field, a pod's home) | unchanged | the steering exponent |
The aliens' own code reads two kinds of nearness (chapter 3) to switch states: the display exponent, how near the alien is to you as the renderer measured it in the previous frame (it starts at the model's scale, 1 for aliens and pods, 0 for missiles and satellites), and the steering exponent returned by a helper, how near it is to its goal (starting at 0). The thresholds below are given as distances: "within 2048 units" means every coordinate difference is under 2048 (the cube test of chapter 3). The detail box at the end of the behaviours lists which exponent each threshold uses.
The behaviours
Pirate: the pod thief
| State | What it does | Leaves the state when |
|---|---|---|
| attack | chases you | aimed at you, within 8192 units and a missile slot is free: fires a slow-turning missile → retreat; or within 2048 units → retreat |
| retreat | flees from you | beyond about 8192 units: claims the first free pod → approach (no free pod → attack) |
| approach | flies to its pod, braking by 12.5 units per frame down to 50 once within 2048 units of the pod | within 256 units: grabs the pod, stops, "WARNING: POD IN DANGER" → turn |
| turn | turns on the spot until the pod is straight behind it | pod behind → escape |
| escape | flies straight, speeding up by 12.5 per frame to its top speed, towing the pod | more than 32,768 units from you: the Pirate and the pod vanish, "POD DESTROYED", the pod is lost and the Pirate counts as done for the level |
If the pod is destroyed meanwhile, the Pirate goes back to attack. If you shoot the Pirate while it tows, the pod flies home by itself. Note that "escaped" is measured from you, not from the pod field: staying close to a fleeing Pirate keeps it from ever escaping D.
Fighter: the dogfighter
Attack: chases you and, while aimed, fires a laser every 4th frame (about 4.5 shots per second). Within 2048 units it switches to retreat and flees until it is beyond about 8192 units, then attacks again. Its speed never changes: a steady 175 units per frame. The result is a ship that makes passes at you.
Guardian: the turret
It flies to the centre of the pod field (the world origin). Within about 4096 units of it, it brakes by 25 units per frame to a standstill (if it drifts out of that zone while braking, it returns to full speed) and then stays there for good, turning toward you and firing a laser every other frame while aimed (about 9 shots per second). It never moves again.
Commander: the pod killer, and the mutant
| State | What it does | Leaves the state when |
|---|---|---|
| attack | chases you | aimed at you: fires an agile missile if a slot is free, and goes to retreat even if none was free; or within 1024 units → retreat |
| retreat | flees | beyond 16,384 units → pick a pod |
| pick a pod | claims the first free pod | at once → approach (none free → attack) |
| approach | flies to the pod, braking by 25 per frame to 50 within 2048 units of the pod | within 256 units of the pod: the pod explodes, "POD DESTROYED", the Commander stops (speed 0) and becomes a mutant |
| mutant | chases you for ever, accelerating to its top speed when aimed and braking when you are behind it, firing a laser every frame while aimed | never |
The mutant keeps the Commander's remaining hit points and is still worth 400 points. A Commander that turned into a mutant starts from standstill and gets going by accelerating 12.5 per frame while aimed at you. When no pods are left, every new alien is born as a mutant (starting at its full 225 units per frame), whatever kind it was meant to be.
Weapons the aliens use
- Lasers (Fighter, Guardian, mutant): 8 shared shot slots; each shot flies straight at 1024 units per frame for 24 frames and costs you 1 shield if it hits (chapter 6).
- Missiles (Pirate, Commander): 4 shared slots, "WARNING: MISSILE LAUNCH DETECTED". Both start at 400 units per frame and home on you with the chase helper. The Pirate's keeps that speed, turns slowly (8 steps, 1.4° per frame) and gives up beyond 16,384 units; the Commander's turns fast (32 steps), brakes by 12.5 per frame down to 50 while you are behind it, speeds back up to 400 once aimed, and never gives up. A hit costs 1 shield (chapter 6). You can shoot missiles down for 25 points.
- Ramming: any alien that comes within 256 units of you costs you a shield and loses a hit point itself (chapter 6), but gives no points.
Which nearness each threshold uses (display = to you, previous frame, starting at the model's scale; steering = to the goal, from the helper, starting at 0):
| Who | Check | Exponent | Distance |
|---|---|---|---|
| Pirate, attack | may fire at ≥ 2; retreat at ≥ 4 | display | within 8192 / 2048 of you |
| Pirate, retreat | claim a pod at < 2 | display | beyond 8192 |
| Pirate, approach | brake at ≥ 3; grab at ≥ 6 | steering | within 2048 / 256 of the pod |
| Pirate, escape | gone at < 0 | display | beyond 32,768 of you |
| Fighter | retreat at ≥ 4; attack again at < 2 | display | within 2048 / beyond 8192 |
| Guardian | brake at ≥ 2 | steering | within 4096 of the origin |
| Commander | retreat at ≥ 5; pick a pod at < 1 | display | within 1024 / beyond 16,384 |
| Commander, approach | brake at ≥ 3; destroy at ≥ 6 | steering | within 2048 / 256 of the pod |
| returning pod | home at ≥ 7 | steering | within 128 of home |
| Pirate's missile | gives up at < 0 | display (scale 0) | beyond 16,384 |
| satellite | expires at < −1; collected at ≥ 7 | display (scale 0) | beyond 32,768 / within 128 |
| ramming | at ≥ 7 | display (scale 1) | within 256 |
Spawning in detail. The kind search starts at an offset that goes up by one after each successful spawn and takes the first kind with budget left; the budget is used even when the alien becomes a mutant; no spawn, no rotation. A dying alien keeps its slot until its explosion ends, and the level ends only after the last explosion. The frame counter, the spawn side and the kind rotation restart every level, so the first alien of every level is a Pirate appearing to your right at frame 64, and the first satellite comes at frame 128. A new alien runs its behaviour in the frame it appears, at full speed, with its display exponent still 0 ("far").
Rounding. Braking is "if speed is above the floor, subtract the step", so with other numbers it could undershoot the floor; with the original numbers it lands exactly on it.
Pods
The six pods sit 1000 units from the origin on the six axis directions. Each idle pod tumbles gently in place. A pod is in one of five states:
| State | Meaning and behaviour |
|---|---|
| idle | free, tumbling in place; only an idle pod can be claimed |
| claimed | an alien is on its way; back to idle if that alien is destroyed |
| towed | grabbed by a Pirate: follows it at its speed |
| returning | its captor was destroyed: flies home at 50 units per frame and stops within 128 units of home |
| exploding | destroyed; removed when the explosion ends |
A pod is lost in three ways: a Pirate escapes with it, a Commander reaches it, or you shoot it (one hit destroys it: an explosion and its sound, but no message and no points). Surviving pods are put back home at the start of every level. All six come back at the start of levels 1, 5, 9, 13 and so on; a game started at any level begins with all six.
Satellites: the pick-ups
- When: every 128th frame (about 7 seconds), if you have no satellite at the moment.
- Where: at a random point within 10,000 units of the world origin (not of you), each coordinate chosen separately. It spins slowly.
- Which: the four kinds come in a fixed cycle that never restarts during a session: the first satellite after starting the program is Extra Thrust, then Extra Fire Power, Extra Shields, Extra Missiles, Extra Thrust, and so on, carrying on across levels and games.
- Taking it: fly within 128 units of it (and not right after a collision). In single player it disappears silently once you are more than 32,768 units away.
| Kind | Single player | Multiplayer (always also full shields, "SHIELDS RESTORED") |
|---|---|---|
| Shields | +8 shields, at most 32 ("EXTRA SHIELDS") | only the restore |
| Missiles | +3 missiles, at most 32 ("MISSILES") | 512 frames of cloak time (it looks like a cloak device) |
| Thrust | +1024 units of extra thrust, at most 4096 ("EXTRA THRUST") | extra thrust set to 1024 |
| Fire power | +64 powered shots, at most 256 ("EXTRA FIRE POWER") | fire power set to 64 |
Shooting a satellite destroys it, with no points; the next one is the next kind of the cycle.
Levels
A level is a wave with a fixed budget of aliens of each kind (level n, counting from 1):
- Pirates: 8; Fighters: 2(n−1); Guardians: 2(n−2) from level 3; Commanders: 2(n−4) from level 5; each kind at most 8, so from level 8 on every level has 8 of each (32 aliens).
- At most 4 aliens are in flight at once up to level 13; then 5, 6, 7, and 8 from level 17.
- Every 64th frame (about 3.5 s) a new alien appears if a slot is free. The kinds take turns among those whose budget is not used up. It appears 16,384 units from you, alternately to your right, left, below and above on screen (along your right axis and your "up" axis, which points down the screen), never ahead or behind.
- If the level starts with no pods left, the whole budget becomes mutants.
The level ends when every alien of the budget has appeared and is gone (destroyed or escaped with a pod) while you still have shields. "Level n Completed" then adds the bonus (pods left) × (level number) × 1000 to your score and 8 shields (at most 32). Enter goes on to the next level; there is no last level. Score, shields, missiles, extra thrust and fire power carry over; the ship is put back at the origin.
Score, game over, high scores
- A new game starts with 32 shields, 16 missiles, score 0 and all six pods.
- Points: alien destroyed by a shot or a missile: its kind's points; enemy missile shot down: 25; level bonus. The score never goes above 1,000,000: a total above it has 1,000,000 taken off (exactly 1,000,000 is kept).
- When the shields reach 0 the game is over ("Game Over"), while the world keeps running behind the text. After 64 frames, a score above the lowest of the ten high scores goes straight to entering three initials; otherwise "Press ENTER to restart".
- The table holds ten scores (built in: 1000, 2000 … 10,000 with no initials) and is saved in HISCORES.DAT next to the program. A new score must be strictly higher than the lowest entry; it is placed above any equal score.
- Esc during flight sets the score to 0, so an abandoned game never enters the table.
- An escaped Pirate counts as "done", so you can finish a level while it carries off your pods.
- At level start every alien slot faces world +z, so the first alien in each slot faces that way; a slot reused later in the level keeps the dead alien's orientation. A new alien never starts aimed at you.
- The satellite cycle is shared by the whole session, so the first pick-up of a game depends on how many satellites appeared before.
- The Guardian's "rally point" is a variable that is always (0, 0, 0): a feature that was perhaps meant to move but never does H.
The behaviours are small enough to copy exactly: keep the state tables, the thresholds (as cube tests on the largest coordinate difference), the proportional steering with its limits, the "loop when the goal is behind" rule, and the timing (every 64th frame, every 4th frame and so on). Most of the game's character comes from these numbers: Pirates sneaking off with pods, Fighters making passes, Guardians camping in the middle, Commanders turning into mutants.
Remaining fine details (the order of checks within a state) are in the comments of src/aliens.c, src/steer.c and src/pods.c.
- Which helper uses which cone: chase and flee ±128; to object, tow and to point ±256.
- Goal behind (chase, to object, to point; f < 0): yaw 0, pitch = −max turn if u < 0, else +max turn (u = 0 counts as positive). Flee with you ahead (f > 0): pitch = −max turn if u > 0, else +max turn. Tow with its goal ahead: pitch = +max turn if u < 0, else −max turn (here u = 0 gives −max).
- Flee's turn is computed exactly like chase's (u ÷ 128 rounded down, 0 replaced by 1, clamped), and then both rates are negated. Negating after rounding is not the same as rounding the negated value.
- Fighter, one frame: in attack it chases; if the chase reports "aimed" and the frame counter is a multiple of 4, it fires. Then, if its display exponent is 4 or more, it switches to retreat and runs the retreat code in the same frame (it flees at once). In retreat it flees and switches back to attack when the exponent is below 2 (for the next frame). Then it moves.