On this page
The frame and the tick
Refractor 2 simulates at a fixed 30 ticks a second and draws as often as it can. Everything that must be the same on the server and on every client happens in a tick; everything that only has to look smooth happens in a frame. This page walks through one frame of the main loop and then through one tick.
One frame of the main loop#
BF2Engine::updateFrame (bf2_w32ded.exe!0x00403570, client BF2.exe!0x0040ca80) is the body of the main loop in both programs. Its steps, in order:
- Quit and pause. The quit flag ends the loop; a process without a game sleeps 100 ms.
- The frame's target.
1 / lockFps(100 by default, 60 on a dedicated server), unless a fixed-step demo forces 1/30 s. - The client waits busily until that time has passed since the previous frame. The dedicated server does not.
- The timer. Game time advances by the real time that passed, times the time scale. The frame's own delta is clamped to [0.00001, 1/15] s (
0x00402500). - The tick clock says how many ticks are due (below).
- Client:
BF2FrameInterpolator::preSimulate(BF2.exe!0x0045bbb0) takes a snapshot of the transforms of the objects in view. Game::simulate(ticks): the simulation ticks, when a game exists and the time scale is not 0.- Per-frame updates: the event manager and the systems that run on frame time rather than tick time.
- Client: interpolate, draw, restore (
BF2.exe!0x0045c190,0x0045c4b0): the objects are put between their last two tick states, the frame is drawn, and the objects are put back. - The throttle. A dedicated server sleeps what is left of 1/30 s, less the variable
server-sleep-overhead-time(default 0.005 s): at most 30 frames a second, one tick a frame on average.
The tick clock#
TickClock::tick(gameTime) (bf2_w32ded.exe!0x00656a60, BF2.exe!0x006a41c0) counts the whole steps of 1/30 s (the float 0x3d088889) since the last tick:
| Steps due | What the clock does |
|---|---|
| 0 | nothing: this frame draws between the same two ticks as the last |
| 1 to 10 | previous = next, last += n x step, next = last + step; the frame runs n ticks |
| 11 or more | the clock gives up catching up: last = previous = next = now, and the frame runs 10 ticks |
Set the frame rate under 30 and every frame runs more than one tick; set it high and most frames run none. Add one slow frame of more than a third of a second and the clock stops catching up.
The arithmetic is x87 double, not float. A decompiler shows float casts here that the listing does not have (fild / fmul / fadd qword at 0x00656acf); this is one of the places where the port was written from the disassembly.
The blend#
BF2FrameInterpolator::interpolate(gameTime, clock.previous) computes alpha = (t - previous) / (ticks x 1/30), 0 when the frame ran no tick, clamped to [-0.1, 1.1]. The chase camera is drawn from the blended vehicle, not from its tick state, which is why a vehicle does not shake in its own camera on a fast client.
A page must take its frame time from the animation frame's own time stamp, not from a clock read inside the frame. Reading a clock gives a time that jitters against the display's refresh, and the blend factor jitters with it.
One tick#
Game::simulateTick (bf2_w32ded.exe!0x0044a840; GameServer::simulateFrame BF2.exe!0x004cf470, bf2142ded!0x080b55d0, BF2142.exe!0x004afb20) runs, in this order:
- The round's state machine (
ServerGameLogic::update0x00459950): pre-game, playing, end of round; a reset takes away the objects created after the load and resets control points and spawners. - The objects' update (
ObjectManager::updateObjects0x006e2270), unless paused: every object's own update, the control points, the object spawners, then the objects marked for destruction go. - The physics, five stages in a fixed order (the picture below).
After the frame's ticks the server writes the packets: per connection whose packet timer is due, the control object, the scope pass and one packet (packets, ghosts).
Why the players have stages of their own#
A player's soldier, or the whole tree of the vehicle he sits in, is simulated at his turn, in the order of the player list, once for the nodes (stage 2, Game::updateControlledTreeNodes bf2_w32ded.exe!0x00445b10) and once for the collision responses (stage 5, Game::updateControlledTreeResponses 0x00445b70). The objects nobody steers run between the two, in the managers' own order. The order decides who pushes whom when two players' vehicles touch in the same tick, so a port that simulates "all vehicles, then all soldiers" gives other results in exactly the cases a player notices: a ram, a road kill, a soldier standing on a moving vehicle.
In Battlefield 2142 the Titans move first (TitanManager::setTick): the objects update with the Titans where the tick before left them, and the nodes and the responses then meet them at this tick's state.
Heroes' frame was not read to its end yet. The port keeps the order it had there: every vehicle node, every soldier in the managers' passes, every player's tree.
How it is tested#
What the tests prove here is order, which no single function's test can:
crates/bf2-server/tests/it_bf2/soldier_tick_order_bf2.rsbuilds a server with several players and asserts where in a tick each soldier's physics runs: a player's own soldier in the player's stage, the others in the manager's, and a driver between two walkers takes his turn between theirs. It was written from the original's listing of the tick.- The frame timer's tests (
crates/bf2-client/src/frame.rs,crates/bf2-engine/src/timing.rs) hold the arithmetic of this page: thirty ticks a second at sixty frames, the coarse clock, the catch-up after a long frame. crates/bf2-client/tests/it_bf2/spawn_frame_order_bf2.rsandpredict_titan_order_2142.rsdo the same for the client's frame.- In play: the soldier movement and vehicle rigs compare positions with the original server's on every tick of a recording, so a step in the wrong place shows as a state that differs one tick early or late (test rigs).
Not proven by a test of its own: the order of the render half of the client's frame, which is checked by eye on the render pipeline's stage pictures.
In the code#
| What | Where |
|---|---|
| the frame | crates/bf2-server/src/lib.rs (Session::frame), crates/bf2-client/app/src/client.rs |
| the tick clock, the timer | crates/bf2-engine/src/timing.rs (TickClock) |
| the round state machine and the objects' update | crates/bf2-server/src/world_tick.rs (tick) |
| the five physics stages | crates/bf2-server/src/lib.rs (tick_physics) |
| the stages' order as a test reads it | crates/bf2-server/src/tick_order.rs (a log that only a test starts) |
Notes: notes/subsystems/engine.md section 7 (the main loop), notes/subsystems/client.md (frame interpolation).
The server's side of the loop, from its command line to its first tick: start-up and the main loop.
docs/dev/engine/tick-order.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.