How NetWars Works Chapter 3 of 13, plus appendices

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.

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).

+x +y +z origin: your ship starts here, nose along +z pod, 1000 units out A sketch of the world axes. Space itself has no "up"; what you see depends on the camera.
The single-player world: six pods on the axes around the origin.

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:

word 3 word 2 word 1 word 0 whole units, high part (rarely needed) "middle word": whole units fraction: 1/65536 unit one coordinate (x, y or z) = 64 bits value in world units = (the whole 64-bit number) ÷ 65536 (negative numbers work the same way)
One coordinate. Word 1 holds the whole units most of the game works with; word 0 is the fraction, words 2 and 3 extend the range far beyond anything reached in play.

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:

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.

right (b) "up" (c) forward (a): into the screen Screen pixel = centre + (right, "up") columns grow to the right, rows grow downward, so "up" (c) is drawn downward. Unturned ship: forward = +z, right = +x, "up" = +y.
The camera's three axes against the screen.

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.

For a rebuild

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:

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:

TurnRotatesOn screen (positive angle)Unchanged
pitchforward toward "up"nose dips (the ↓ key)right
yawforward toward rightnose swings right (the → key)"up"
rollright toward "up"the view tiltsforward

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:

  1. Shrink: while any of the three numbers is above 16384 (or below −16385), halve all three together and subtract 1 from a counter.
  2. 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:

exponent 3 4 5 Each square shell is one exponent value; the next shell inwards is half as wide. The shells are squares (cubes in 3-D) because only the largest of the three coordinates decides when to stop halving or doubling. Dashed: a circle through the middle of the outer shell's sides. A shell's corner is √3 ≈ 1.7 times as far away (in 3-D) as the middle of its face. So "exponent ≥ k" really asks: is the other object inside a cube around me of half-width about 16384 ÷ 2^k?
The distance exponent divides space into nested cubes (steering flavour; drawn in 2-D for clarity).
Quirk

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.

For a rebuild

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):

  1. m = floor of each coordinate difference in world units; e = 0 (steering) or the model's scale (display);
  2. while any m ≥ 16385 or any m < −16385: halve all three (rounding down), e = e − 1;
  3. 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.

Exact rules: precision