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.
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(thebf2_serverbinary), with the Heroes-only parts incrates/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.Bundleis0x9471); 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 insrc/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#
- Time: the frame time is measured and clamped (
bf2_engine::timing); a fixed 30 Hz simulation tick drives the game logic. - Network in: packets are read and split into their three streams (below).
- Input and actions: the client turns input into player actions; the server applies the actions it received.
- Simulation: soldiers, vehicles, projectiles, physics, damage, control points, spawns.
- Game logic and Python (server): tickets, scores, the game mode's rules; events go to Python handlers, due timers fire.
- Network out: the server writes changed object state as ghosts and queued events; the client writes its actions.
- 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#
- 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#
| System | bf2-rs | Notes |
|---|---|---|
| File system, zip archives, mods | bf2-fs, bf2-webfs (browser) | notes/subsystems/filesystem.md |
| Console and CON | bf2-con, bf2-runtime | notes/subsystems/console.md, notes/formats/con-scripts.md |
| Animation | bf2-anim | notes/subsystems/animation.md |
| Physics and collision | bf2-physics, bf2-collision | notes/subsystems/physics*.md |
| HUD | bf2-hud | notes/subsystems/hud.md |
| Bots | bf2-ai | notes/subsystems/ai*.md |
| Demos | bf2-demo | notes/subsystems/demo-recorder.md |
| Voice | bf2-voip, bf2-speex | notes/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):
| Crate | What it is |
|---|---|
crates/bf2-collision | collision mesh files |
crates/bf2-con | the console script language: lexer, interpreter, command registry |
crates/bf2-fs | the engine's file system: file manager, archives |
crates/bf2-net | the wire-compatible network protocol |
crates/bf2-physics | physics: collision queries, materials, rigid-body integration |
crates/bf2-ai | bot AI: navigation data formats and pathfinding |
crates/bf2-anim | skeletons, bone animations and the animation system |
crates/bf2-audio | the sound system: decoders, sound templates, mixing rules |
docs/dev/engine/overview.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.