On this page
The dedicated server: start-up, level load and the main loop
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.
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:
bf2_server --root <a server folder> [--seconds N] [--trace t.jsonl] -- +config settings\serversettings.con +mapList settings\maplist.conThe 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#
| Step | Original (bf2_w32ded.exe) | Port |
|---|---|---|
| engine defaults, the command line | 0x00403bd0, 0x00404ad0 | bf2_engine::startup::run_startup |
mod paths; the mod's ServerArchives.con mounts the archives | initSubsystems 0x00404500 | the same hooks; mods/bf2 is always searched too |
Settings/Usersettings.con | SettingsManager::load 0x00656b30 | load_user_settings |
the mod's Init.con | the engine's init, step 18 | the same; its gameLogic.* lines are refused: there is no game logic yet, in the original either |
| the game is created | 0x00402d00 | create_server_game: from here gameLogic.*, scoreManager.* and admin.* commands exist |
| the server's settings file, reserved slots, the map list | GameServer::init 0x0044aca0 | server::game_server_config |
the Python host starts and imports bf2; the admin script runs | 0x0042c450 | the port's Python host (the Python layer) |
| the network server opens; the first level loads | GameServer::start 0x00446d10 | the 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:
- the Python
Resetevent; GameLogicInit.con;- the game mode's script is loaded (game modes);
- the level's archives are mounted; the level's size is probed;
ScoreManagerSetup.con;- the templates: every script under
Objects/in the mounted archives (GameLogic::loadTemplates,0x0046b100), with the AI's when bots are on; MaterialManagerSettings.con: the material pairs (damage and armor);- the level's own
Init.con; - the voice messages (
0x00466030); StaticObjects.con, thenGamePlayObjects.confor the game mode and size: the level's contents;ScoreManagerSettings.conand 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.
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:
- 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);
- the network is pumped: packets in, player actions queued;
- each tick: the game logic, the players' input, the objects, physics, the bots (bots);
- the Python host's update, once a frame, with the number of ticks: timers fire here;
- the network sends: ghosts, events, control blocks (ghosts);
- 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):
| Compared | Result, as the notes record it |
|---|---|
| the console commands at start-up and their answers | 769 of 769 lines equal |
| Python host output and events over a level load, a map change, a time limit | equal |
| a soldier's walk, fall, ladder, swim | identical tick for tick where the input phase matches |
| vehicles: a slalom, a ram, a tank, a helicopter | identical |
| 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'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#
| What | Where |
|---|---|
| the server program | crates/bf2-server (binary bf2_server) |
| start-up shared with the client | crates/bf2-engine (startup, cli) |
| the console dispatch | crates/bf2-runtime |
| the differential tools | crates/bf2-server/difftest/ |
Notes: notes/subsystems/server.md, notes/subsystems/engine.md, notes/play-guide.md.
docs/dev/server/startup.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.