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

The dedicated server: start-up, level load and the main loop

BF22142Heroes

A dedicated server is the game without a window: the same engine, the same level files, the same Python, and a loop that never draws. The port's bf2_server boots through the same steps in the same order as the original's, because the order is visible: scripts run in it, and a script that runs before the thing it configures exists is silently refused.

A dedicated server, from the command line to the first tick engine defaults and the command line everything after is console commands: +config, +mapList, +loadLevel mod paths and archives the mod's ServerArchives.con mounts what the server reads settings, then the mod's Init.con its gameLogic lines are refused: the game does not exist yet the game, the server's settings, the map list from here gameLogic, scoreManager and admin commands exist the Python host imports bf2, runs the admin script the network opens; the first level loads templates before the level's Init.con; the level's objects last the main loop 30 ticks a second at about 32 frames; Python and the network once a frame bf2_w32ded.exe!0x00404500 (subsystems), 0x0044aca0 (init), 0x00446d10 (start), 0x00449e50 (loadLevel), 0x00403570 (frame)
From the command line to the first tick. Each step is a function of the original and one of the port.

The command line#

Everything a server is told comes as console commands: +config <file> runs a settings script, +mapList <file> the map list, +loadLevel <name> loads one level outright. The port takes its own options before a -- and the original's command line after it:

powershell
bf2_server --root <a server folder> [--seconds N] [--trace t.jsonl] -- +config settings\serversettings.con +mapList settings\maplist.con

The server changes into its root folder first: scripts and Python resolve loose files against the working directory, as in the original. Never point the root at your install: Python writes compiled files next to its sources. Build and run makes a server folder for you.

Start-up, step by step#

StepOriginal (bf2_w32ded.exe)Port
engine defaults, the command line0x00403bd0, 0x00404ad0bf2_engine::startup::run_startup
mod paths; the mod's ServerArchives.con mounts the archivesinitSubsystems 0x00404500the same hooks; mods/bf2 is always searched too
Settings/Usersettings.conSettingsManager::load 0x00656b30load_user_settings
the mod's Init.conthe engine's init, step 18the same; its gameLogic.* lines are refused: there is no game logic yet, in the original either
the game is created0x00402d00create_server_game: from here gameLogic.*, scoreManager.* and admin.* commands exist
the server's settings file, reserved slots, the map listGameServer::init 0x0044aca0server::game_server_config
the Python host starts and imports bf2; the admin script runs0x0042c450the port's Python host (the Python layer)
the network server opens; the first level loadsGameServer::start 0x00446d10the same

With no map in the list and no +loadLevel, the original prints "No maps in maplist. Please add atleast one map, or use +loadLevel command." and exits. So does the port, with the same sentence, spelling and all.

Loading a level#

GameServer::loadLevel (0x00449e50). The order was taken from a trace of the original, not guessed:

  1. the Python Reset event;
  2. GameLogicInit.con;
  3. the game mode's script is loaded (game modes);
  4. the level's archives are mounted; the level's size is probed;
  5. ScoreManagerSetup.con;
  6. the templates: every script under Objects/ in the mounted archives (GameLogic::loadTemplates, 0x0046b100), with the AI's when bots are on;
  7. MaterialManagerSettings.con: the material pairs (damage and armor);
  8. the level's own Init.con;
  9. the voice messages (0x00466030);
  10. StaticObjects.con, then GamePlayObjects.con for the game mode and size: the level's contents;
  11. ScoreManagerSettings.con and the rest of the level's settings.

Then the first update sets the status to Playing and tells the Python status handlers, and the ban list is read.

Why the order is a contract

Mods rely on it. A mod's Init.con cannot set a game logic value, because the original refuses it there; a level's Init.con can override a template, because the templates are loaded before it. A port that runs the same scripts in another order loads the same level differently, with no error to show for it.

The main loop#

BF2Engine::updateFrame (0x00403570), for ever:

  1. the tick clock turns the time since the last frame into ticks of exactly 1/30 s, at most 10 a frame (frame and tick order);
  2. the network is pumped: packets in, player actions queued;
  3. each tick: the game logic, the players' input, the objects, physics, the bots (bots);
  4. the Python host's update, once a frame, with the number of ticks: timers fire here;
  5. the network sends: ghosts, events, control blocks (ghosts);
  6. the loop sleeps.

30 ticks, 32 frames#

The original simulates exactly 30 ticks a second and runs about 32 frames a second: 2,000 frames took 62.45 s in a trace. The reason is the sleep. It asks for 28 ms, and on Windows a sleep ends on a timer period of 15.625 ms, so 28 ms lasts two periods, 31.25 ms. Most frames therefore hold one tick and some hold none.

The port calls the same Windows sleep on purpose. A modern high-resolution sleep gave 35 frames a second, and frames are not invisible: the Python update runs once a frame, and the network sends once a frame. With the same sleep the port measured 32.2 frames a second.

What the port is held to#

The server is checked by running it beside the original on the same data and settings and comparing what both write (test rigs):

ComparedResult, as the notes record it
the console commands at start-up and their answers769 of 769 lines equal
Python host output and events over a level load, a map change, a time limitequal
a soldier's walk, fall, ladder, swimidentical tick for tick where the input phase matches
vehicles: a slalom, a ram, a tank, a helicopteridentical
a level's load (Karkand)identical

What is not the same by design: the port's own options before --, its trace file, and that one program serves all three games.

2142
Battlefield 2142 and Heroes

2142's and Heroes' servers boot through the same sequence with their own tables: 2142's console commands and its own Python host functions, Heroes' game logic settings file and its heat and flag rules. The port picks the game from the root's data and runs the branches cited from that game's own server or client.

How it is tested#

  • The start-up sequence (crates/bf2-server/src/server.rs): what the game logic's init resets and in which order, the static networkables' ids, the mesh step of the load.
  • The level load starts the AI or not (load_ai_bf2_tests.rs): each case in a child process of its own, because the load seeds and counts process-wide.
  • Pure servers (content_check.rs): the archive lists parse as the original's stream reader takes them, and every shipped level passes the port's own check.
  • Against the original, whole: the game rules rig plays the same script on the port and on the original server and compares scores, tickets and the round's end (test rigs); a start-up step that seeds a generator differently shows there (random numbers).

Nine of twelve addresses stand in a test.

In the code#

WhatWhere
the server programcrates/bf2-server (binary bf2_server)
start-up shared with the clientcrates/bf2-engine (startup, cli)
the console dispatchcrates/bf2-runtime
the differential toolscrates/bf2-server/difftest/

Notes: notes/subsystems/server.md, notes/subsystems/engine.md, notes/play-guide.md.

Source: docs/dev/server/startup.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.