openOMSI GitHub Discord
Architecture

Docs / The project / Architecture

The frame loop

2 min readEdit this pagedocs/ARCHITECTURE.md

On this page
  1. Stages
  2. The simulation thread

The simulation runs on a fixed step: omsi_sim::tick::RATE_HZ is 60 ticks a second, always. A frame runs at most 4 ticks, so the simulation keeps wall-clock pace down to 15 frames a second.

The window's frame is three declared lists of stages (app_events/frame/stages.rs, run by ticked.rs):

  1. FRAME_BEFORE, once a frame: housekeeping and the player's input.
  2. FRAME_TICK, once per tick: traffic, the player's bus and its scripts, people, the duty, the clock and the weather. What only the picture and sound need (view syncs, the AI's sounds) runs in the frame's last tick alone (SimTicks::view_due).
  3. FRAME_AFTER, once a frame: the view, sounds, multiplayer, the interface and the picture.

The player's bus and the camera are drawn between the last two ticks. The offscreen run's step is one tick (offscreen::OFFSCREEN_STEP), so --offscreen exercises the same stages as the game.

Two things still run per frame, not per tick: the tile streaming's handover and the population's camera. So the AI traffic is not yet identical at every frame rate; the player's bus is.

Stages#

One runner (omsi-app/src/frame_stages.rs) runs both the window's list (app_events::WINDOW_FRAME) and the offscreen list. Each stage names the field groups of App it reads and writes and the values it hands on. Tests pin both orders and the three places where the two paths knowingly differ. OMSI_FRAME_ORDER=1 logs them.

App's state is grouped by subsystem (omsi-app/src/app/groups.rs). App::parts (app/parts.rs) splits App into one borrow per group and names every field of App and SessionState; a new field does not build until it has a group.

The simulation thread#

With the sim_thread setting on (off by default, read once at start; OMSI_SIM_THREAD=0/1 overrides it), part of the simulation runs on a thread of its own while the main thread draws and presents:

  • The main thread keeps the window, the input and the surface.
  • The job runs the AI traffic's steps (TrafficSim::advance, one per tick), then the scenery objects' scripts, then the people's steps.
  • The traffic stage finishes the rest of the traffic from what the last step left. The stepped traffic is back before the frame is counted.
  • The scripted stage applies the commands of the step made during the last picture and hands the next step its inputs owned (OwnedSceneryInputs).
  • The people read the player's bus from a copy (omsi_sim::people::PlayerBusView). The humans stage applies the last step's result (steps::humans_after_step).
  • The timetable's step stays in its stage (its fleet uploads sit between its two halves).

The AI cars are then one frame behind the rest. The player's vehicle and input stay in their stage, so there is no added input latency. The offscreen run never reads the switch.

Something wrong or missing? Fix it on GitHub or open an issue.

↑↓ to move↵ to open