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

Engine overview

Battlefield 2, 2142 and Heroes run on DICE's Refractor 2 engine. This page is the map: what the big systems are, how one frame flows through them, and which bf2-rs crate reimplements each one.

BF22142Heroes
Battlefield 2 (2005) mods/bf2 · Game::Bf2 Battlefield 2142 (2006) mods/bf2142 · Game::Bf2142 Battlefield Heroes (2009) mods/bfheroes · 2142 gen + Product::Heroes Game content: CON / tweak scripts · Python game modes · meshes · levels · Flash menus plain files in zip archives; everything a modder changes lives here Refractor 2 engine (reimplemented by bf2-rs) Console/CON Templates Objects/World Game logic Python host Network Physics Animation Renderer Audio HUD/Flash UI AI bots bf2_server · bfh-server authoritative simulation, Python, ghosts out bf2_client (native + browser/WebGPU) render, sound, input, HUD; ghosts in
Content on top, the engine's systems in the middle, the two programs (server, client) at the bottom.

The programs#

  • The server is authoritative: it loads the level, simulates every object, runs the Python game rules and sends the world to the clients. bf2-rs: crates/bf2-server (the bf2_server binary), with the Heroes-only parts in crates/bfh-server.
  • The client renders, plays sound, reads input, predicts the local player and shows the HUD and the Flash menus. bf2-rs: crates/bf2-client (native window, and the same code compiled to WebAssembly + WebGPU for the browser build).
  • Both read the same game data through the same file system (crates/bf2-fs) and the same CON interpreter (crates/bf2-con), so a template means the same thing on both sides.

Templates and objects#

Everything in the world is an object created from a template. A template is a named recipe of a class (PlayerControlObject, GenericFireArm, ControlPoint, ...) with properties and child templates, written in CON scripts (the CON system). An object is an instance with a position, a parent and runtime state (damage, occupants, ammo).

  • Classes are registered with numeric class ids (dice.hfe.world.ObjectTemplate.Bundle is 0x9471); a template type's is-a chain decides which properties it accepts. bf2-rs: crates/bf2-objects (ids generated from the original binaries, generated.rs; the template manager and properties in src/template/).
  • Components (armor, fire, ammo, deviation, ...) are sub-objects a template creates on demand.
  • Objects live in a world with a spatial grid; physics, collision and triggers query it (crates/bf2-world, bf2-physics, bf2-collision).

The object model in depth: notes/subsystems/object-model.md, notes/subsystems/object-templates.md.

One frame#

1 Time 2 Network in 3 Input / actions 4 Simulation 5 Game logic + Python 6 Network out 7 Render + sound client only server only 30 simulation ticks per second
The main loop (BF2Engine::updateFrame): time, input, network in, simulation, Python, network out, render.
  1. Time: the frame time is measured and clamped (bf2_engine::timing); a fixed 30 Hz simulation tick drives the game logic.
  2. Network in: packets are read and split into their three streams (below).
  3. Input and actions: the client turns input into player actions; the server applies the actions it received.
  4. Simulation: soldiers, vehicles, projectiles, physics, damage, control points, spawns.
  5. Game logic and Python (server): tickets, scores, the game mode's rules; events go to Python handlers, due timers fire.
  6. Network out: the server writes changed object state as ghosts and queued events; the client writes its actions.
  7. Render and sound (client): the scene, effects, HUD, menus and audio for the interpolated state.

Game logic#

GameLogic owns the round: teams, tickets, the time limit, control points, kits, spawn points, squads and the commander, scoring and the end of round. Most of the rules are delegated to Python (the game mode scripts); the engine raises events (PlayerKilled, ControlPointChangedOwner, ...) and exposes state through the host module. bf2-rs: crates/bf2-game (rounds, tickets, spawns, score, squads), crates/bf2-gameplay (weapon mechanics), the server crates. Notes: notes/subsystems/game-logic*.md, players.md.

Networking: ghosts, events, data blocks#

Server (authoritative) simulation · Python · rules GhostManager GameEventManager PlayerActionManager UDP 16567 (BF2) · 1 socket Client prediction · render · sound ghosts -> objects events, data blocks input -> actions stream 2: ghosts (object state, server -> client) stream 1: reliable ordered game events + data blocks stream 0: player actions (input, client -> server) SWConnection: 6-bit sequence, 32-bit ack mask no compression, no fragmentation, one bit stream per packet
Every UDP packet carries up to three streams in one bit stream.
  • Transport: one UDP socket (BF2 16567), a sliding notify window per connection (6-bit sequence numbers, a 32-bit ack mask) that tells the sender which packets arrived. No compression, encryption or fragmentation.
  • Player actions (stream 0, client to server): the input of each tick (movement, aim, fire, use).
  • Game events (stream 1, both ways, reliable and ordered): one-shot facts such as a kill message, a chat line, a HUD update, a spawn request. Data blocks ride on events: a blob (server settings, the level to load, the map list) cut into 63-byte chunk events (bf2_net::datablock). Every event class and data block of the three games, with its id, wire fields and bf2-rs reader: Network events and data blocks.
  • Ghosts (stream 2, server to client): the replicated state of objects. The server keeps, per client, which objects are in scope and what the client has acknowledged, and sends only what changed, quantised per field (bf2_net::ghost*). The client creates, updates and deletes its copies from them.

Everything is bit-exact against the original: bf2-rs clients join original servers and original clients join bf2-rs servers. Notes: notes/subsystems/network.md, network-events.md; captures and the viewer: crates/bf2-netview.

The Python layer#

The server embeds CPython 2.3.4 and runs the game's scripts with one engine module, host: player and object queries, game logic calls, timers, triggers and event handlers. See the Python layer and the API reference.

Rendering#

The original renderer is a Direct3D 9 DLL (RendDX9.dll) with effect files (.fx) per material. bf2-rs renders with wgpu (Vulkan / D3D12 / Metal / WebGPU): it compiles the original shaders with the original D3DX compiler offline and translates the Direct3D 9 bytecode to WGSL (crates/bf2-shaders), so materials look the same. Static meshes, bundled meshes (vehicles, weapons), skinned meshes (soldiers), terrain, water, sky, particles, decals, lightmaps and the Flash menus are reproduced. bf2-rs: crates/bf2-render, bf2-mesh, bf2-terrain, bf2-texture, bf2-effects, bf2-ui (Flash through Ruffle). Notes: notes/subsystems/renderer.md, shaders.md.

Audio#

BF2Audio.dll (OpenAL) plays sound templates: 3D sounds attached to objects, envelopes driven by engine values (RPM, speed), music and the radio. bf2-rs: crates/bf2-audio (decoders, sound template rules, a cpal backend) and an AudioWorklet in the browser. Notes: notes/subsystems/audio.md.

Everything else#

Systembf2-rsNotes
File system, zip archives, modsbf2-fs, bf2-webfs (browser)notes/subsystems/filesystem.md
Console and CONbf2-con, bf2-runtimenotes/subsystems/console.md, notes/formats/con-scripts.md
Animationbf2-animnotes/subsystems/animation.md
Physics and collisionbf2-physics, bf2-collisionnotes/subsystems/physics*.md
HUDbf2-hudnotes/subsystems/hud.md
Botsbf2-ainotes/subsystems/ai*.md
Demosbf2-demonotes/subsystems/demo-recorder.md
Voicebf2-voip, bf2-speexnotes/subsystems/voip.md

Deeper pages#

How it is tested#

This page explains; it holds no code of its own. Each system it names has a page with its own tests: physics, collision, objects, terrain, the frame and the tick, the network. How the port as a whole is held to the original: test rigs and parity. The wiki's test checks that every link and anchor on it leads to a page of the build and that its diagrams are the ones the script draws.

In the code#

The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):

CrateWhat it is
crates/bf2-collisioncollision mesh files
crates/bf2-conthe console script language: lexer, interpreter, command registry
crates/bf2-fsthe engine's file system: file manager, archives
crates/bf2-netthe wire-compatible network protocol
crates/bf2-physicsphysics: collision queries, materials, rigid-body integration
crates/bf2-aibot AI: navigation data formats and pathfinding
crates/bf2-animskeletons, bone animations and the animation system
crates/bf2-audiothe sound system: decoders, sound templates, mixing rules
Source: docs/dev/engine/overview.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.