How NetWars Works Chapter 13 of 13, plus appendices

13. Building it again: a browser version

Everything from the previous chapters, put together for one concrete goal: NetWars in a web browser, at any resolution, where friends join a game by opening a link, with no host and no "start match", just as in 1993. This chapter is a plan, not code: what to keep, what to change, how to organise the program, and how the network works without a game server.

The contract: what must stay the same

"Plays like the original" means a fixed list of things. Keep these, and the game will feel like NetWars; change them, and it becomes a different game:

Free to change: the resolution, smooth drawing between steps, a depth buffer, sound quality, the network transport, and any bug you decide to fix (chapter 12), as long as you decide it on purpose.

The parts of the program

simulation core fixed step, original rules, no browser APIs inside inputkeys, pointer lock networkWebRTC mesh rendererWebGL or canvas audioWeb Audio squares screens and menusHTML/canvas UI signalling serviceintroductions only
The core knows nothing about the browser; everything else adapts it to the browser. The dashed service only introduces the browsers to each other.

The simulation core

The renderer

Audio

One square-wave oscillator followed by a gain node (a raw square wave is harsh), its frequency changed once per timer tick from the effect tables (chapter 10), scheduled on the audio clock with setValueAtTime rather than with timers. Keep the rule that a new effect replaces the running one. Browsers start audio suspended: create or resume the audio context on the first click or key press.

Input

Multiplayer without a game server

The original's model (chapter 8) fits the browser almost directly. The only new part is that browsers on the internet cannot find each other by broadcasting; something must introduce them.

The invitation link and the signalling service

Because the game state lives only in the players' browsers, the person who sent the invitation can leave and the others play on. Anyone can join later with the same link, as long as the signalling service is running, because it is the service, not any player, that introduces newcomers.

Messages

MessageWhenChannelContent
stateevery simulation step, to every peerunreliable, unordered ({ordered:false, maxRetransmits:0}), binaryslot, step number, name, position, velocity, axes, status, score, satellite (kind, position), shot flags (as in the original packet, chapter 8)
hitwhen your shot hits someonereliable (a second channel with default options)event id (sender peer id, counter), victim slot and victim peer id, powered
satellite eventwhen you collect or shoot someone's satellitereliableevent id, owner slot and owner peer id, collected or shot
slot claim / releasejoining and leaving a slotreliableslot, peer id
presencelobbythe signalling service (recommended)who holds which slot

Rules to decide

Bugs: keep or fix?

Chapter 12 lists the original's bugs and oddities. A suggested default for a rebuild:

BehaviourSuggestionWhy
repeated hit reports, two players in one slot, two hits in one frame counting once, stale satellite eventsfixbroken play, no one would miss them
camera rolled by the level introductionfixan accident with no gameplay meaning
no near clipping, painter's sort errorsfix or keepvisual only; keeping is authentic
laser tunnelling and blind spots, cube-shaped nearnesskeepthey shape how combat feels
collision push along a fixed world axiskeep or softenodd, but part of the original's feel
missile lock as a square beam (off-screen locks)keepa gameplay rule players learn

A building order

  1. The core helpers (distance exponent, rotations, dot products) with tests against the original through tools/doccheck/.
  2. Flying alone: ship control, stars, the instruments, one pod drawn with the renderer.
  3. Single player: aliens one kind at a time, shots and hits, pods, satellites, levels, score.
  4. Sound, menus and screens.
  5. Multiplayer through a simple in-browser transport first (BroadcastChannel between two visible windows; a background tab is slowed down and stops sending), with an optional layer that adds 50–150 ms of delay and some loss; then the signalling service and invitation links, then other machines (chrome://webrtc-internals and about:webrtc show what the connections do).
  6. Polishing: interpolation, resolution scaling, key remapping, the bug decisions above.