On this page
Players: join, spawn, die, leave
To the server a player is a small record with an index, a name, a team and a few weak references: the thing he controls, his soldier, his kit. Everything a player does in a round is one of four transitions on that record: he connects, he spawns, he dies, he leaves. Each has an exact order of objects created, events sent and Python handlers called, and scripts depend on that order.
The record#
Player (constructor bf2_w32ded.exe!0x0047a580) lives in the PlayerManager, a list in the order the players were added, with a map by index and one by name (names compare without case). Creating a player is refused when the index or the name is taken (0x0044ffe0). What the record holds:
| Field | Meaning |
|---|---|
| index | the connection's id; what Python and the network call the player |
| name, profile id | |
| team | -1 until chosen |
| vehicle | what he controls now: his soldier, a seat, or a free camera |
| default vehicle | his soldier, while he has one |
| kit | |
| alive, man down, suicide | three bytes; setting alive to false clears man down |
| spawn tick, spawn group | when he may spawn next, and where he asked to |
| ping, interpolation time | for lag compensation |
| score block | kills, deaths, score, rank (kills and scoring) |
| squad, commander, squad leader | (squads and commander) |
| unlocks | per kit and team |
A team change acts only on a change: the spawn group is cleared, and the new team reaches the clients through the player's networked state, not through an event of its own.
Connecting, and starting the round#
A joining client becomes a player once the server has its client info. What the round does with players is the round state machine's business (ServerGameLogic::update, 0x00459950, server rules), which runs once a tick with the count of connected humans:
- with no players the round stays in its waiting phase;
- the first player's connection starts the start delay at the next update: in a trace of the original, 0.53 s after his connect event;
- each change of status goes to every client and to Python's status handlers.
Python hears PlayerConnect for each new player.
Spawning#
A dead player picks a spawn group: a control point's set of spawn points, a squad leader, a vehicle that carries spawn points. processSpawns (0x00457de0) runs in the tail of every playing tick, in two passes:
- every call: a dead player keeps his chosen group only while the group still exists, allows him, is his team's and has a free point; else the choice is cleared and his client is told. This is the moment the spawn screen un-selects a flag that was just lost;
- every third call: players who are dead or man down and whose spawn tick has passed. A man-down player dies first. Then, with a group chosen,
spawnPlayer.
Because pass 2 runs on every third tick, a respawn lands up to two ticks after the spawn tick.
What a spawn makes#
spawnPlayer (0x00456f90) needs the round to be playing, a group, a team with at least one ticket, and a spawn point. Then, in this order:
- the soldier is created at the point's position and rotation (the template by team and kit from the level's
gameLogic.setKitlines), and every client is told to create it; - the player enters the soldier: it becomes his vehicle and his default vehicle; Python hears
PlayerSpawn(player, soldier); - he is alive;
- the kit is created and picked up: Python hears
PlayerChangeWeapon, thenPickupKit(kits and supplies); - a last event tells every client the spawn is complete.
A spawn point's position is the point plus the template's offset, unless the point scatters. Which point of a group is used is a draw from the C runtime's random stream (random numbers): in recorded sessions the port and the original used the same two points of the same group, and with the seed pinned three players' spawn points were equal.
The respawn time#
At a death the spawn tick is set to the current tick plus spawnTime x 30, rounded (playerDeath, 0x004551d0). A player who lay man down keeps the time he spent down: waiting for a medic who never comes does not cost extra. The spawn time is the server's setting.
Dying#
playerDeath acts only on a living player: the spawn tick, alive off, an event to every client (with a flag for a suicide), and the default vehicle cleared. What Python hears for one kill, in order:
PlayerKilled -> (the scoring script's PlayerScore) -> DropKit(player, kit) -> PlayerDeath(player, soldier)During DropKit the player still has his kit; during PlayerDeath he has none. A script that reads the kit in the death handler gets nothing, on the original and on the port.
The body stays as the armor's wreck rule says (damage and armor): with the shipped soldiers 6 s as a wreck and 1 s more, about 7 s in all (6.96 s measured on the original, 7.0 s on the port). The dropped kit lies for 30 s (29.94 s measured on both).
Leaving#
GameServer::destroyPlayer (0x004482d0):
- a player who is alive or sits in anything but the free camera dies first;
- his camera object is destroyed;
- he leaves his squad and resigns as commander;
- Python hears
PlayerDisconnect(player); - every client is told to destroy the player, and he is removed from the manager.
What differs per game#
The same record and loop. A spawn can go through a drop pod (2142 by area), and the respawn delay has a random part the notes list among 2142's carried-over functions.
Heroes adds lives in team elimination, a hero's data on the player record, and its own spawn rules per mode (game modes); the respawn seconds on the HUD are a mended row of the HUD sweep (Heroes HUD).
What the port has#
| Part | State |
|---|---|
| the player record and manager | ported; the accessors matched against the Python attributes |
| the round machine's wiring to players | ported from the listing and a trace of the original |
processSpawns, spawnPlayer and the order of events | ported; compared with recorded sessions of the original server |
| respawn timing | the same code; a table of runs in the notes shows the 1 to 2 ticks of phase |
| death, the corpse's and the kit's times | compared: 6.96 s and 29.94 s on the original against 7.0 s and 29.94 s |
| the listener that drops the kit at death | modelled at that point, not traced, as the notes say |
| squad leave and commander resign at a disconnect | marked "not reproduced" in the players note; the squads note covers them |
How it is tested#
- Differentials: scripted headless players join, spawn, kill themselves, respawn and leave on the original server and on the port's; the control lines, Python events and object ids are compared line by line (test rigs).
- Scenarios:
join_while_spawningholds a client in its loading phase while another respawns every 4 s; the spawn screen scenarios check the spawn within a time (scenarios).
In the code#
| What | Where |
|---|---|
| players on the server | crates/bf2-server/src/players.rs, players_round.rs, players_spawn.rs, players_input.rs |
| the round state, spawn ticks, scores | crates/bf2-game/src/ (round, spawn, score) |
| the join | crates/bf2-net/src/join.rs |
Notes: notes/subsystems/players.md (sections 1 to 8 and 34), notes/subsystems/game-logic-rules.md.
docs/dev/server/players-and-teams.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.