Writing a 0.75 client

What a client needs to join and play on an Ace of Spades 0.75 server, as implemented by ngspades-client (a BetterSpades fork). It is written for people building their own client. Where our client does something on purpose, the text says so under Our client, with the file it lives in. Our client is under conformance review: it is not a reference for edge cases, so section 8 lists what to check rather than what we do.

The companion documents:

Version 0.76 differs only where noted (a different connect value, a bigger MapStart and a WorldUpdate with player ids).

1. Transport

1.1 ENet

0.75 runs over ENet 1.3 on UDP/IPv4. Set the connection up like this:

SettingValue
Channels1 (channel 0)
Compressionthe range coder (enet_host_compress_with_range_coder). A server may compress, so decompression is mandatory.
Connect data3 for 0.75, 4 for 0.76
Reliabilityeverything reliable, except WorldUpdate, which servers send unreliable
Default port32887
ENetHost* host = enet_host_create(NULL, 1, 1, 0, 0);
enet_host_compress_with_range_coder(host);
ENetPeer* peer = enet_host_connect(host, &address, 1, 3); /* 3 = 0.75 */

Each ENet packet holds exactly one game packet: one id byte, then the payload.

Our client (network.c, transport_enet.c) connects with the 1.0 magic 0x08101919 first. A vanilla server refuses it at once with reason 3 (“wrong protocol”), and the client tries 3, then 4. SP_PROTO10=0 skips the 1.0 attempt. It waits up to 2.5 s for the ENet connect, then listens for 1 s more for a refusal before it counts the connection as up.

1.2 Disconnect reasons

The server passes a uint32 with the ENet disconnect event. ENet also uses 0 for a timeout, so 0 can mean either.

ValueMeaning
0timed out (or an unspecified server reason)
1banned
2kicked. Some servers (piqueserver) use it for “too many connections from your address”
3wrong protocol version
4server full
5server shutting down
8, 9, 10, 11banned, full, kicked, wrong version, sent while in game instead of during the map download
20invalid name
61server shutting down (alternative value)

Any other value must be shown as an unknown reason, never trusted as an index into a table.

Our client (network_reason_disconnect) knows 1, 2 (as “Connection limit”), 3, 4, 5, 10 and 20, and shows everything else as “Unknown”. When it gets 3 while trying 0.75 it moves on to 0.76 without leaving the menu. A server can override the text: a system chat message from player 255 just before the disconnect becomes the reason shown.

1.3 Server addresses (aos://)

aos://<address>[:<port>][<version suffix>]
uint32_t n = strtoul(text, NULL, 10);
snprintf(ip, sizeof ip, "%u.%u.%u.%u", n & 255, (n >> 8) & 255, (n >> 16) & 255, n >> 24);

Our client (network_identifier_split) also accepts a dotted address (aos://1.2.3.4:32887), a bare host:port, and aos10:// (protocol 1.0, with IPv6 in brackets).

1.4 The server list

The public lists are JSON arrays served at /serverlist.json, for example http://services.buildandshoot.com/serverlist.json. Each entry has at least:

KeyExampleNotes
name"Deuces Wild"
identifier"aos://16777343:32887"the address to join
map"Hallway"
game_mode"ctf"
country"US"ISO 3166-1 alpha-2
latency120ms, between the list and the server
players_current, players_max16, 32
last_updated1301931731Unix seconds
game_version"0.75"

Ignore keys you do not know, and expect duplicate entries.

Our client (hud.c, master.c) merges this list with our own master’s (https://master.shootanddestroy.obex.pw/v1/servers, whose rows carry the same keys plus id, identifier6 and info) and lists a server found in both once. It fetches over HTTPS through the system’s HTTP client (http.c).

2. Data on the wire

#pragma pack(push, 1)
typedef struct { float x, y, z; } vec3f;
typedef struct { int32_t x, y, z; } vec3i;
typedef struct { uint8_t b, g, r; } color3;
#pragma pack(pop)

2.1 Coordinates

The map is 512 × 512 × 64 voxels.

Our client keeps OpenGL coordinates internally (y up): it converts (x, y, z) from the wire to (x, 63 − z, y) and back on every packet.

2.2 Sectors

The minimap grid is 8 × 8 sectors of 64 × 64. A sector is named by a letter A..H from floor(x / 64) and a digit 1..8 from floor(y / 64) + 1. Territory captures are announced by sector (network.c, TerritoryCapture).

2.3 Text

Strings are NUL-terminated or sit in fixed-size fields. Two encodings are in use:

Inside chat, a few control bytes change the color of the text that follows:

ByteColor
0x01team 1’s color
0x02team 2’s color
0x03spectators’ color
0x04red
0x05green
0x06normal (white)
0x07gray
0x08yellow

\n and \r break the line. Other bytes below 0x20 are shown as a replacement character.

Our client (lang.c, network.c):

3. Game rules the client must know

3.1 Players

Player ids go from 0 to 255; most servers use 32 slots. Keep state for all 256. Per player you need at least: joined, alive, team, weapon, held tool, score, name, block color, position, orientation, movement and tool inputs. Only for yourself do you also track HP, blocks, grenades, magazine and reserve.

Our client: PLAYERS_MAX is 256 (player.h). Size the name buffer for UTF-8 (accents take 2 bytes each), or a long name loses its last characters.

3.2 Enumerations

Teams
−1 (255 on the wire)spectator
0team 1 (left, usually blue)
1team 2 (right, usually green)
2, 3neutral, only for the owner of a territory
WeaponsTools
0rifle0spade
1SMG1block
2shotgun2gun
3grenade

When a value is out of range, fall back to a safe one (spectator, rifle, spade) instead of indexing a table with it.

Check in yours: validate player ids, ignore a SetTool above 3, and map an out-of-range team or weapon to a safe default (spectator, rifle) instead of using it as an index.

3.3 Block colors and the palette

Placing a block uses the color the player holds. The classic palette is 8 × 8: row 0 is grays, rows 1..7 are red, orange, yellow, green, cyan, blue and magenta, from dark to light. The arrow keys move a cursor over it; up and down wrap within the column. Picking a color sends SetColor.

E copies the color of the block under the crosshair, up to 128 blocks away, or the fog color when there is none.

Our client (texture_block_color, hud.c) generates exactly the classic 64 colors. Check how your palette steps at the row edges: the classic behaviour moves to the previous or next row. E picks the color under the crosshair while holding a block.

3.4 Fog

Fog limits sight to a cylinder of radius 128 around the camera. Nothing beyond it is drawn, shots stop there, and the sky is the fog color. StateData and FogColor set it.

Our client: settings.render_distance, 128 by default.

3.5 Block physics

When a voxel stops being solid, check its six face neighbors. Each solid one starts a flood fill through solid voxels; if the fill reaches the ground layer (z ≥ 62), nothing happens. Otherwise everything it reached falls and is removed. Voxels that were already empty inside a spade or grenade area must not start a check.

Our client (map_update_physics, map.c) runs the flood fill on a worker thread and animates the falling pieces. It checks the neighbors of every voxel in a grenade or spade area, even the ones that were already empty.

3.6 Applying block edits

Edits that arrive together should land together. Collect the voxels every BlockAction and BlockLine touches in a table keyed by position (create or destroy, the last one wins). After draining the ENet events of a frame, apply the creates, then the destroys. Servers rely on this to repaint a block (a destroy followed by a build at the same position), and a structure resting on that block must not collapse in between.

Check in yours: whether you apply edits per packet or per frame. Applying each packet at once starts the physics check early, so a repaint can make a structure fall.

4. Connecting, step by step

client                                   server
  ENet connect (data 3) ----------------->
            <----------------------- MapStart (18)
            <----------------------- MapChunk (19) × N
            <----------------------- VersionGet (33), HandshakeInit (31)   (optional)
  VersionSend (34), HandshakeReturn (32) -->
            <----------------------- ExistingPlayer (9) × players already in
            <----------------------- StateData (15)        (download done)
  ExistingPlayer (9): team, weapon, name -->                 (team/weapon menu)
            <----------------------- CreatePlayer (12)      (you spawn)
            <----------------------- WorldUpdate (2), ... (the match)
  1. Map. MapStart gives the compressed size. Append every MapChunk payload; the chunks may be any size. The concatenation is a zlib stream of a VXL map.
  2. State. StateData ends the download: decompress and load the map, and take your player id, the fog, the team names and colors and the game mode from it.
  3. Join. Show the team and weapon menus, then send ExistingPlayer with your id, team, weapon and name. Choosing spectator joins at once.
  4. Spawn. The server answers with CreatePlayer for you (and for everyone else, whenever anyone spawns).

Our client (network.c, hud.c): hud_join sends the ExistingPlayer join; read_PacketStateData loads the map and opens the team menu.

5. Packets from the server

Sizes include the id byte. Positions are in wire coordinates (section 2.1).

IdNameSize
0PositionData13
1OrientationData13
2WorldUpdate1 + 24 × n (0.76: 1 + 25 × n)
3InputData3
4WeaponInput3
5SetHP15
6Grenade30
7SetTool3
8SetColor5
9ExistingPlayer12 + name
11MoveObject15
12CreatePlayer16 + name
13BlockAction15
14BlockLine26
15StateData84 (CTF), 33 + 13 × n (TC)
16KillAction5
17ChatMessage3 + text
18MapStart5 (0.76: 73)
19MapChunk1 + data
20PlayerLeft2
21TerritoryCapture4
22ProgressBar8
23IntelCapture3
24IntelPickup2
25IntelDrop14
26Restock2
27FogColor5
28WeaponReload4
31HandshakeInit5
33VersionGet1

Check every packet’s length before reading it, and ignore ids you do not handle.

Check in yours: verify a packet’s length against its id before decoding it, so a short or malformed packet is never read past its end.

0 PositionData

vec3f pos: moves you there at once (a teleport or a correction).

1 OrientationData

vec3f ori: turns you to that direction.

Check in yours: store it as the orientation, never as a position.

2 WorldUpdate

One {vec3f pos; vec3f ori;} per player slot, indexed by player id, up to 256 (0.76 adds a uint8 player_id in front of each). Skip your own entry.

Our client also skips an all-zero position: servers send zeros for players you should not see, and teleporting them to the corner looks wrong.

3 InputData

uint8 player_id, uint8 keys: bits 1 forward, 2 back, 4 left, 8 right, 16 jump, 32 crouch, 64 sneak, 128 sprint. For yourself only the jump bit counts.

4 WeaponInput

uint8 player_id, uint8 buttons: bit 1 primary, bit 2 secondary. Ignore it for yourself. It is not the raw mouse: the server sends 0 while a player sprints, switches tools or reloads.

5 SetHP

uint8 hp, uint8 type, vec3f source: your new HP. When it drops, show the hurt effect. When source is not zero, also point the damage indicator at it. type is 0 for fall damage and 1 for anything else.

Check in yours: which type values show the hurt effect and play the sound.

6 Grenade

uint8 player_id, float fuse, vec3f pos, vec3f vel: spawns a grenade that explodes fuse seconds after the packet arrives.

7 SetTool

uint8 player_id, uint8 tool: that player switches to tool.

8 SetColor

uint8 player_id, color3 color: that player’s block color. Keep it even for a player who is not alive or not joined yet, since later block packets use it.

9 ExistingPlayer

struct { uint8_t id; int8_t team; uint8_t weapon; uint8_t tool;
         uint32_t kills; color3 color; char name[]; /* NUL-terminated */ };

A player who was already in the game when you joined. It carries no position: keep the player out of sight until a WorldUpdate or CreatePlayer places them.

11 MoveObject

uint8 object, int8 team, vec3f pos. In CTF, object 0 and 1 are the two intels and 2 and 3 the two tents. Moving an intel also drops it from whoever carried it, silently. In TC, object is a territory index and team its new owner.

12 CreatePlayer

struct { uint8_t id; uint8_t weapon; int8_t team; vec3f pos; char name[]; };

That player spawns at pos (or joins, the first time). The player is alive unless the team is spectator. It faces east for team 1 and west for team 2. Reset its block color to (111, 111, 111) and restock it. A packet of exactly 16 bytes has no name: keep the one you had.

The classic client places the player at pos.z − 2 (two blocks higher).

Our client:

13 BlockAction

uint8 player_id, uint8 type, vec3i pos:

TypeEffect
0build one block with the player’s color (a repaint when the voxel is already solid, silently)
1destroy one block
2spade: destroy the block and the ones above and below it (1 × 1 × 3)
3grenade: destroy 3 × 3 × 3, silently
4destroy one block (a gun)
5destroy one block (a spade)

Types 2 and 3 leave the bottom layers (z ≥ 62) alone. Voxels outside the map are ignored. For yourself, a type 0 costs one block, and types 1 (while holding the spade) and 5 give one back, up to 50.

Our client (read_PacketBlockAction):

14 BlockLine

uint8 player_id, vec3i start, vec3i end: builds a line of blocks between the two points (a 3D DDA), skipping voxels that are already solid. It costs |dx| + |dy| + |dz| + 1 blocks.

15 StateData

struct {
    uint8_t player_id;            /* you */
    color3 fog, team1_color, team2_color;
    char team1_name[10], team2_name[10];  /* NUL not guaranteed */
    uint8_t mode;                 /* 0 CTF, 1 TC */
    /* CTF: scores, limit, intel holders/positions, tent positions (52 bytes)
       TC:  uint8 count + count × {vec3f pos; uint8 team} */
};

Ends the map download. Read the team names as 10 bytes that may lack a NUL. Treat an unknown mode as TC.

Our client cuts the names at 10 bytes and converts them from CP437. For an unknown mode it logs an error and keeps going.

16 KillAction

uint8 victim, uint8 killer, uint8 kind, uint8 respawn_seconds. kind: 0 weapon, 1 headshot, 2 melee, 3 grenade, 4 fall, 5 team change, 6 class change. The killer gets a point unless it is a suicide.

Our client draws the kill feed with weapon icons and starts the respawn countdown from respawn_seconds.

17 ChatMessage

uint8 player_id, uint8 type, char text[]. Types: 0 all, 1 team, 2 system; newer servers also send 3 big, 4 info, 5 warning, 6 error.

Our client shows types 3..6 as popups. A system message from player 255 is kept as the reason for a disconnect that follows.

18 MapStart, 19 MapChunk

See section 4. 0.76’s MapStart adds a CRC32 and the map name, and the client answers whether it has the map cached (MapCached, id 31, C→S).

20 PlayerLeft

uint8 player_id: the player is gone; clear their slot.

21 TerritoryCapture, 22 ProgressBar

uint8 index, uint8 winning, int8 team and uint8 index, int8 team, int8 rate, float progress: TC captures and the capture bar.

Check in yours: a capture may name a team other than 0 or 1 (neutral): do not use the value as an index.

23 IntelCapture, 24 IntelPickup, 25 IntelDrop

uint8 player_id, uint8 winning, uint8 player_id, uint8 player_id, vec3f pos. A capture is worth 10 points to the player and 1 to the team; winning ends the round.

26 Restock

uint8 player_id: you get full HP, 50 blocks, 3 grenades and a full reserve. When you are dead, only play the sound.

Check in yours: restock only a living player, and take the counts from the rules of the server’s mode.

27 FogColor

uint8 alpha, color3 color: the new fog color.

28 WeaponReload

uint8 player_id, uint8 magazine, uint8 reserve. For yourself it sets the ammo; for anyone else it only plays the reload sound.

31 HandshakeInit, 33 VersionGet

Some servers fingerprint clients. Answer HandshakeInit with HandshakeReturn (32) carrying the same 4 bytes, and VersionGet with VersionSend (34).

A VersionGet that lists property ids (the aosprotocol specification’s extended form) is answered with ExistingPlayer (9) marked by 'x' as its first byte, then id, length, payload per property in ascending order, one packet per property, each repeating the ones before. Our client answers 0 (application: 0 1 5 and BetterSpades, what it calls itself on a 0.75 server), 1 (the interface language, en_us, pt_br, …) and 2 (feature flags, bit 0: it reads UTF-8 chat); an unknown id gets an empty payload.

6. Packets to the server

IdNameWhen our client sends it
0PositionDataevery second while alive, if you moved more than 0.01
1OrientationDataup to 120 times a second when the aim turned more than 0.5°, and right before each shot
3InputDatawhenever the movement keys change
4WeaponInputwhenever the buttons change, except while sprinting
5Hituint8 victim, uint8 part (0 torso, 1 head, 2 arms, 3 legs, 4 melee) for each hit your shot or spade made
6Grenadewhen you throw: fuse left, position, velocity
7SetToolwhenever the held tool changes
8SetColorwhen you pick a color
9ExistingPlayerto join: team, weapon, name (section 4)
13BlockActionbuild, dig, spade (1 × 1 × 3), or a gun hit on a block
14BlockLinedragging to build a line
17ChatMessagechat, in CP437, or 0xFF + UTF-8 to a server known to read it
28WeaponReloadwhen you start reloading
29ChangeTeamuint8 player_id, int8 team from the in-game team menu
30ChangeWeaponuint8 player_id, uint8 weapon from the in-game weapon menu
32HandshakeReturnthe answer to 31
34VersionSendchar client ('B'), uint8 major, minor, revision, char os[]
9ExistingPlayer 'x'the extended version answer, to a VersionGet with property ids
114Flashlight Lightuint8 sub (0), uint8 player, uint8 on: asks to switch our light (key F, a tap of the pad’s Back)

The classic rules are stricter in two places: send the position once a second even when you did not move, and send the orientation whenever it changes at all (an exact comparison, not a threshold).

The client reports its own hits; the server decides the damage. Servers check the distance, the line of sight and the fire rate, so a client should only send hits it really saw.

7. Extensions our client understands

Servers announce extensions with ExtInfo (id 60), as the aosprotocol specification (https://github.com/zerospades/aosprotocol) describes. Our client answers with the ones both sides know, never one the server did not offer: PlayerProperties (0x00, packet 0x40, the exact HP, blocks, grenades, ammo and score), Damage Markers (0x20, packet 0x60), Teamplay (0x30, packet 0x70: Config, Ping, ESP Mark), Flashlight (0x32, packet 0x72: Light, Light State, Light Config; every lit player’s beam is drawn, where it lands and the lamp’s glare by the spec’s Beam, Lens, Distance and Fog terms, hidden by blocks and bodies, never a light the server did not switch on), Player Limit (0xC0), Message Types (0xC1: 3 big, 4 info, 5 warning, 6 error, shown as popups) and Kick Reason (0xC2: a system line from player 255 before the disconnect is the reason shown). UTF-8 Chat has no id (section 2.3). The Powerthirst packets are not supported.

Our own server used HostCommand (63) and HostStatus (64) for bot management before it moved to protocol 1.0. Packet 64 is also Player Properties: our client reads it as Player Properties once the server agreed to that extension, and as HostStatus otherwise.

8. What a client implementer must check

This list is deliberately not a list of our client’s faults: that changes with every release. It is what to verify in any 0.75 client, ours included. The ngspades-client conformance checklist, kept current, is at https://ngspades.github.io/shoot-and-destroy/.