BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Players: join, spawn, die, leave

BF22142Heroes

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.

A player on the server connected a record, a team, no soldier yet waiting to spawn a spawn group chosen, the spawn tick not yet passed alive a soldier and a kit, created in a fixed order man down revivable; keeps the time spent down gone his objects destroyed, Python told death: the spawn tick is now + spawnTime x 30; the spawn loop's second pass runs every third tick bf2_w32ded.exe!0x0044ffe0 (createPlayer), 0x00457de0 (processSpawns), 0x00456f90 (spawnPlayer), 0x004551d0 (playerDeath)
A player's states. The arrows are the four transitions this page follows.

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:

FieldMeaning
indexthe connection's id; what Python and the network call the player
name, profile id
team-1 until chosen
vehiclewhat he controls now: his soldier, a seat, or a free camera
default vehiclehis soldier, while he has one
kit
alive, man down, suicidethree bytes; setting alive to false clears man down
spawn tick, spawn groupwhen he may spawn next, and where he asked to
ping, interpolation timefor lag compensation
score blockkills, deaths, score, rank (kills and scoring)
squad, commander, squad leader(squads and commander)
unlocksper 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:

  1. 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;
  2. 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:

  1. the soldier is created at the point's position and rotation (the template by team and kit from the level's gameLogic.setKit lines), and every client is told to create it;
  2. the player enters the soldier: it becomes his vehicle and his default vehicle; Python hears PlayerSpawn(player, soldier);
  3. he is alive;
  4. the kit is created and picked up: Python hears PlayerChangeWeapon, then PickupKit (kits and supplies);
  5. 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:

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

  1. a player who is alive or sits in anything but the free camera dies first;
  2. his camera object is destroyed;
  3. he leaves his squad and resigns as commander;
  4. Python hears PlayerDisconnect(player);
  5. every client is told to destroy the player, and he is removed from the manager.

What differs per game#

2142
Battlefield 2142

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
Battlefield Heroes

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#

PartState
the player record and managerported; the accessors matched against the Python attributes
the round machine's wiring to playersported from the listing and a trace of the original
processSpawns, spawnPlayer and the order of eventsported; compared with recorded sessions of the original server
respawn timingthe 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 timescompared: 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 deathmodelled at that point, not traced, as the notes say
squad leave and commander resign at a disconnectmarked "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_spawning holds 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#

WhatWhere
players on the servercrates/bf2-server/src/players.rs, players_round.rs, players_spawn.rs, players_input.rs
the round state, spawn ticks, scorescrates/bf2-game/src/ (round, spawn, score)
the joincrates/bf2-net/src/join.rs

Notes: notes/subsystems/players.md (sections 1 to 8 and 34), notes/subsystems/game-logic-rules.md.

Source: 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.