3. The world: space and numbers
Where things are, which way they point, and how near they are to each other. NetWars answers these three questions with whole numbers only, in clever ways that also shape how the game plays. This chapter explains the ideas, then what a rebuild with ordinary floating-point numbers must keep.
An empty space with a centre
The NetWars world has no ground, no walls and no edges. It is empty space with a centre point, the origin. Every position is three numbers (x, y, z): how far from the origin along three directions at right angles to each other, the world's x, y and z axes.
- In single player the six pods sit on the axes, 1000 units from the origin: one on each of +x, −x, +y, −y, +z, −z (so they form the corners of an octahedron, a double pyramid, around the centre). Your ship starts at the origin, nose pointing along +z.
- New aliens appear 16384 units away from you, to your side, never straight ahead (chapter 7).
- In multiplayer the four slots start on the corners of a 2048 × 2048 square in the x–z plane: PLAYER 1 at the origin facing −x, PLAYER 2 at (0, 0, 2048) facing +z, PLAYER 3 at (2048, 0, 2048) facing +x, PLAYER 4 at (2048, 0, 0) facing −z (chapter 8).
To get a feel for the unit: at full throttle your ship covers 144 units per frame (about 2600 units per second in single player); a laser shot adds 1024 units per frame to that and lives 24 frames, so it reaches about 25,000 units. A pod is about 300 units across (its spikes about 380). Aliens and missiles describe their speed in a smaller unit: a "speed" of s in their tables means s ÷ 4 world units per frame (chapters 5 and 7).
Positions: very large whole numbers
NetWars is a 16-bit program: its ordinary numbers are 16 binary digits ("bits") long and only go from −32768 to 32767. That is far too small for a world where an alien starts 16384 units away and where you can fly forever. So each position coordinate is stored as a 64-bit integer, four 16-bit pieces ("words") side by side. NetWars treats it as a fixed-point number: a whole number with an agreed, invisible point inside it. Here the point sits between word 1 and word 0, so the value in world units is the whole 64-bit number ÷ 65536:
In this document a world unit is one step of word 1, i.e. 65536 steps of the raw number. The ship's velocity and the players' laser shots use the same format, so "the ship moves by its velocity" is three big additions per frame: position = position + velocity. Fractions matter: a slowly drifting ship moves by less than one unit per frame and still gets somewhere.
Directions: arrows of length one, in Q14
Every ship, alien, missile and the camera has an orientation: which way its nose points and which way is its "right" and its "up". NetWars stores this as three axis vectors, arrows of length 1 fixed to the object:
- forward: where the nose points and where the object flies;
- right: to the object's right;
- "up": perpendicular to both (in quotes, because of the screen convention below).
The three arrows are always at right angles to each other, and they are tied together by right × "up" = forward (× is the cross product, explained below). An object with forward = (0, 0, 1), right = (1, 0, 0), "up" = (0, 1, 0) is lined up with the world's axes; that is how every single-player ship starts. The sources name the camera's three arrows a (forward), b (right) and c ("up").
The numbers in these arrows are all between −1 and 1, which whole numbers cannot hold. NetWars uses Q14 fixed point: a 16-bit whole number n stands for n ÷ 16384, i.e. the invisible point sits before the last 14 binary digits (hence "Q14"). So 16384 means 1.0, 8192 means 0.5, −11585 means about −0.707. Multiplying two Q14 numbers gives a result 16384 times too big, so after each multiplication the game divides by 16384 again (it drops 14 binary digits; programmers call this a shift).
"Up" in NetWars points down the screen
Space has no up or down, but the screen does, and here NetWars has a convention worth knowing before anything turns. The screen's columns grow to the right and its rows grow downward (row 0 is the top), and the game copies the camera's third arrow c straight into the row direction without flipping it. So the arrow named "up" points to the bottom of the screen O. An unturned ship looks along +z, with +x to the right of the screen and +y toward the bottom.
Nothing is mirrored: the world is an ordinary right-handed coordinate system, and "x right, y down, z into the screen" is right-handed too. Only the camera's idea of "up" differs from the usual one.
Keep all of NetWars' numbers unchanged (positions, start places, axes, spawn directions) and tell your 3-D library that the camera's up direction is −c (the opposite of the game's "up" arrow), looking along a. Equivalently, negate the projected y before drawing. Do not flip the sign of y in the world: that changes nothing on screen and only breaks the numbers. Chapter 4 shows the convention at work in a quirk of the original.
Measuring along an arrow: the dot product
The most-used operation in the game is the dot product of a vector with an axis: v · a = vxax + vyay + vzaz. If a has length 1, this is "how far v reaches in direction a". With it the game answers questions like:
- How fast am I flying forward, and how fast am I drifting sideways? (velocity · forward, velocity · right; chapter 4)
- Where is that alien as seen from my cockpit? Its position relative to me, dotted with each of my three axes, gives its coordinates in my own frame: across (b), down (c) and ahead (a). The screen point is then roughly "centre + 512 · across ÷ ahead, centre + 384 · down ÷ ahead", drawn only when the object is far enough ahead (chapter 9).
- Is the shot level with the target, or above it? (chapter 6)
The cross product of two arrows is a third arrow at right angles to both; for two perpendicular arrows of length 1 it again has length 1. NetWars uses it to rebuild an arrow from the other two.
Turning: rotating two arrows at a time
Angles are measured in steps of 1/2048 of a full turn (so 512 is a right angle). Sine and cosine come from two tables of 2048 Q14 values each, computed as 16384 · sin(2πk/2048) (and the same with cos) rounded toward zero. The values themselves are written out in src/data/sintab.h and src/data/costab.h (declared in src/globals.c), and tests/test_tables.py checks that the formula gives exactly these values and the original game's tables.
A turn rotates two of the three arrows within their common plane and leaves the third alone. There are three kinds:
| Turn | Rotates | On screen (positive angle) | Unchanged |
|---|---|---|---|
| pitch | forward toward "up" | nose dips (the ↓ key) | right |
| yaw | forward toward right | nose swings right (the → key) | "up" |
| roll | right toward "up" | the view tilts | forward |
For a turn of angle θ that rotates arrow p toward arrow q:
p′ = p · cos θ + q · sin θ, q′ = q · cos θ − p · sin θ.
In the original only p′ is computed with this formula (with a small correction of its length); q′ is then rebuilt as the cross product of the other two arrows, which gives the same result up to rounding and also keeps the three arrows perpendicular despite rounding errors. Chapter 4 gives the exact recipe and the order of the turns in each frame.
Try it: a ship's three arrows after turning from the starting orientation (yaw first, then pitch; 2048 steps = full turn). The sketch shows the world with +y drawn upward, so here the green "up" arrow points up; in the game's view it points down. The printed Q14 numbers are ideal values; the game's own come out a few units different because of its tables, rounding and small per-frame turns.
Nearness: the distance exponent
Games constantly ask "how near is that?": is the alien close enough to fire, has the missile reached its target, did I fly into the pod? The exact distance needs a square root, which was slow on a 1993 PC. NetWars uses a cheaper, rougher measure.
Take the vector from one object to the other (subtract the positions; use whole units, rounded down). Then:
- Shrink: while any of the three numbers is above 16384 (or below −16385), halve all three together and subtract 1 from a counter.
- Grow: while all three numbers are between −8192 and 8191, double all three together and add 1 to the counter, but stop as soon as the counter reaches 8.
The counter is the distance exponent. Afterwards the three numbers are the direction to the other object, as a Q14 arrow whose largest part is between 8192 and 16384 (so its length is between about 0.5 and 1.7, not exactly 1), unless the cap of 8 stopped the doubling first, which happens only for very near objects. The exponent says how many doublings were needed: a larger exponent means nearer, each step up about half the distance.
The counter does not always start at 0. Two flavours exist, and a rebuild must keep them apart:
- Steering exponent: starts at 0. Used by the aliens and missiles to judge the distance to their goal. Example: a pod 1000 units straight ahead needs 4 doublings (1000 → 16000), so its exponent is 4; at 500 units it is 5, at 2000 units 3. "Exponent ≥ 6" means every coordinate difference is below 256 units; the aliens treat that as "contact" (chapter 7).
- Display exponent: from an object to the player, computed by the renderer. It starts at a number stored with each model, its scale (1 for ships, aliens and pods, 0 for satellites and missiles), so the same pod at 1000 units has display exponent 5. Negative means very far (the object is drawn as a small "+"; that starts at about 32770 units for scale 1, 16385 for scale 0). 7 or more means touching: every coordinate difference below 256 units for scale 1, below 128 for scale 0. The renderer, the radar, collisions and some AI rules use this one (chapters 5 to 9).
Because the exponent measures cubes, not spheres, collisions and "close enough" tests in NetWars reach about 1.7 times farther along the diagonals than straight ahead. This is part of how the game feels, for example when you brush past a pod diagonally.
Use ordinary floating-point numbers (64-bit "doubles") for positions, velocities and axes: their precision is more than enough for this world. Convert the original's numbers on the way in: world units = raw ÷ 65536, Q14 values ÷ 16384, angles × 2π ÷ 2048, alien and missile speeds ÷ 4.
Do not replace the distance exponent by a true distance. Write one helper that computes it exactly as above, with the two loops on whole units rounded down (a log₂ formula is off by one at exact powers of two and gets the asymmetric limits wrong):
- m = floor of each coordinate difference in world units; e = 0 (steering) or the model's scale (display);
- while any m ≥ 16385 or any m < −16385: halve all three (rounding down), e = e − 1;
- then while every m is in [−8192, 8191] and e ≠ 8: double all three, e = e + 1.
Two objects at exactly the same place get 8. Then every threshold in chapters 5 to 9 keeps its meaning, cube shape included. The original routines are lvec3_normalize_down and lvec3_normalize_up in src/input_math.asm; the terms are summed up in include/netwars.h.
- The direction keeps its fraction. Shrinking and growing act on the full-precision difference vector (the 64-bit values, fraction included); only the tests that decide when to stop use whole units rounded down. So the direction is the exact difference times 2exponent, not a rounded one. Rounding it to whole units first makes near objects several pixels off on screen.
- Dot products round down. A dot product of a vector with a Q14 axis is the sum of the three products divided by 16384, rounded down (toward minus infinity), as a whole number. Where the game measures with dot products (velocity parts, steering, shot boxes, camera coordinates), it uses this whole number.
- Axes are Q14 numbers. For results that match the original to the last unit, round the axes to Q14 (whole multiples of 1/16384) before using them in dot products; with exact floating-point axes a turn rate or a pixel can occasionally come out one higher or lower.
- "Camera units". Camera coordinates are the direction (above) dotted with the camera axes, so they are in world units times 2exponent − scale for the display exponent, i.e. scaled up the nearer the object is. Thresholds given "in camera units" (chapter 9) therefore mean different world distances at different distances.