openOMSI GitHub Discord

Docs / The project / Architecture

Known debt

9 min readEdit this pagedocs/ARCHITECTURE.md

  • What is already in place. The frame is a declared list of stages run by one runner (omsi-app/src/frame_stages.rs: app_events::WINDOW_FRAME for the window, offscreen::OFFSCREEN_STEP for the offscreen run, each stage naming the field groups 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);

    the AI traffic and the people simulate in omsi-sim, with omsi-app::traffic::Traffic and omsi-app::humans::Humans as thin wrappers (the simulation, with the cars' sounds) that dereference to them; every OMSI_* switch goes through omsi_cfg::flags (docs/DEBUG_FLAGS.md); App's state is grouped by subsystem (omsi-app/src/app/groups.rs).

  • The stages' declarations are checked. App::parts (omsi-app/src/app/parts.rs) takes App apart into one borrow per Group, naming every field of App and of SessionState (a new field does not build until it has its group).

    A stage handed a context made by stage_ctx! (placement, streaming, traffic, view_keys, humans, after_humans, on_foot, player_tick, scripted, lights, wheel_surface, audio, photo, lighting, trace_look) can touch its groups only, and its declaration is that context's READS/WRITES, so the compiler holds it to it.

    A helper such a stage shares with the rest of the game takes a narrower context of its own (shift_clock a ShiftClockCx), which the stage takes out of its own with narrow! and an App method with XCx::of.

    The other window stages still take the whole App: they call App methods of many groups, and several write what Common lends read-only (the controllers, duty and weather stages the command line's Args, the view camera the seat in Settings).

    For them frame_stages/access_tests.rs reads the crate's source (omsi-srcscan) and fails when a stage reaches, through App methods and the functions handed the App, a field of a group it does not declare.

    What the declarations show: the stages that take the whole App reach most of the sixteen groups each, so no two of them are independent, and the converted ones are a fraction of a millisecond: no stage runs in parallel (a stage becomes a candidate once its context is narrow and it costs enough).

    OMSI_PROFILE's keys are profile_keys::ProfKey, a slot of an array each (the names in the logs and in OMSI_PROFILE_JSON are unchanged).

  • The timetable is omsi-sim::timetable_run: the trip times, the IBIS, the player's duty, and ScheduleSim (timetable_run/sim/) - the day's departures and which are due, queued, waiting for their tiles or awaiting their tour's bus, the tours' vehicles (choose), the routes on the lanes and where on its route a due bus is (place_departure), the routes carried on as tiles load, the tours, the duty taken from one and the departure boards.

    It reads the map through TimetableWorld (the world snapshot omsi_sim::world_data::WorldData implements it) and the traffic as TrafficSim.

    omsi-app::schedule::Schedule holds it (Deref to ScheduleSim) as the GPU adapter: fleet reads and uploads the vehicle sets of the next departures, dispatch and spawn put the buses on the road (Traffic::spawn_bus, set_trailers, attach_cars, turn_train), hand a tour's bus its next trip, and take buses off, each telling ScheduleSim what became of the departure.

    Still in the adapter: the bus put on the road itself (its trailers and cars, where it starts, whether it may appear there), which needs the app's Traffic (trailer_chain, may_appear, blocked) besides the GPU.

  • The map world the simulation reads is omsi-sim::world_data: WorldData, an immutable snapshot of Arc'd parts - the loaded tiles' ground (GroundTiles: terrain and road faces, with the wheel, walking, camera and drawn-ground questions), the lists made from the tiles (TileLists: stops, waiting places, obstacles, parked cars' boxes, petrol stations, echoing places), where the map's objects stand (ObjectPlaces), the map index's stop facts and the map's own facts (MapFacts: the only copy of the AI lists, the date and the chrono folders).

    omsi-scene::World holds it in a WorldCell (arc-swap) and publishes a new one where a tile's ground comes or goes and where the tile lists are rebuilt; readers take it without a lock (peek for one look, snapshot for a step), the vehicles' wheels through LiveGround. It implements people::World and TimetableWorld.

    What loading hands the simulation goes through omsi-sim::handover::Inbox (pushed by the loader's threads, taken whole at the start of a step): the traffic's TrafficArrivals - the crossings' light programs as they are numbered, a tile's lanes and parked cars.

    Still in World and locked: the GPU side, the type and file caches of loading, the light programs' numbering by owner object, the scripted objects' view halves and the lamps.

  • LAN play (omsi-lan): the session as the game plays it is omsi_lan::game and runs on values - a LanConfig the game fills from its arguments (and takes back where a joining player adopted the host's map, date, time and weather), a LanInput each frame, our bus as a VehicleInstance - and asks the rest of the game through LanHost (the world as WorldData, its lanes, another player's vehicle made as the game makes its vehicles, destination displays).

    A frame is tick_send, the shared world, tick_receive, then view_sync::apply_lan draws and sounds the others from LanGame::drawn. The app's side is omsi-app/src/lan_play.rs (name tags, the HUD's line, AppLanHost).

    Still the app's: the shared world's mirror (omsi-app/src/lan_world.rs), which drives Traffic from the traffic table (omsi-net::traffic_table, see docs/TRAFFIC_TABLE.md), Humans and their view directly; it goes to omsi-lan once it is simulation state plus events for the view, as the other players' vehicles are.

  • The view sync phase (omsi-app/src/view_sync/): the code that turns the traffic's and the people's state into renderer instances is in one module - traffic.rs (the AI vehicles' renders, their drivers, the traffic lamps), people.rs (the people's meshes, posing and skinning, the coins and ticket blocks) - with one entry point, view_sync::sync(ViewSync { traffic, people }, sim_view, world, renderer, scene), traffic first.

    Its state, view_sync::SimView { traffic: TrafficView, people: PeopleView } (the cars' renders and drivers; the people's meshes, GPU materials, posing and ticket blocks), is held apart from the simulation: by the window in GfxState::sim_view, by the offscreen run in its own struct.

    Each part starts afresh with the simulation it shows (the Traffic put in place, Humans::new). It runs where each part was synced before, so that the renderer sees the same calls in the same order:

    • the window: the traffic at the end of frame_traffic (after its step and sound, before the player and the people move: the cars that parked leave the traffic's list there, and the people's step reads it), the people at the end of frame_people (after their step and the coins handed out);
    • the offscreen run: the traffic after each population (and before an OMSI_POPULATION_SHOTS picture), the people every step while OMSI_TRACE_PAX traces, both before each snapshot (the traffic, the player's pose, then the people from the snapshot's camera) and in the closing reports.

    The simulation's calls hand the view data, never renderer calls: what they did that the renders have to follow - a car put on the road with its coupled parts or taken off (now, or at the next sync for the step's retired and parked cars), parts coupled or let go, a train made anew under its id, a parked car driving off or one parking - is written down as omsi_sim::view_events::TrafficEvents (Traffic::events), somebody appearing, going or renamed and coins on the desk as omsi_sim::people::BodyOps (PeopleSim::bodies).

    view_sync::apply_traffic and apply_people replay them in order, called right after each call that can make them (population, the timetable's tick - before its vehicle sets go up and at its end -, the LAN mirror, admin and plugin removals, the clock's restart, the people's step, the avatar), where those calls made the renderer calls before: the renderer sees the same calls in the same order (OMSI_SCENE_DIGEST checks it on an offscreen run).

    None of those calls takes a renderer, a scene or a part of the view; Traffic::tick is TrafficSim::advance plus Traffic::after_step (the step's retired cars to events), so the step can run on another thread.

    The people hold no renderer handles (PeopleView::meshes, by person id); the parked cars' world side (World::take_departing_parked, take_returning_parked) is apart from their renderer side (show_parked_instances).

    The AI cars are drawn from an owned FrameSnapshot (omsi_sim::view_events: every car's parts, their meshes' transforms, visibility and per-slot values in flat buffers kept from frame to frame), taken at the sync, with their looks (TrafficSim::snapshot_looks, CarLooks: a copy of the variables for the switched materials, the string variables as they change, the interior lamps, the text, script and HTML pictures to upload, cracks, dents and reshaped bellows - the simulation's side of drawing a car is done there) and the timetable buses as their drivers read them (snapshot_drivers, DriverFrame; the figures read a bus through omsi_scene::driver::DriverCar).

    The AI cars' sounds play from a SoundFrame (TrafficSim::snapshot_sounds), the people are posed from a PeopleFrame (PeopleSim::snapshot_into; the pose as drawn is the view's, PeopleView).

    Still read from the simulation's state while syncing (the sync is therefore a point where the simulation stands still): the snapshots are taken there, not at the end of a step; the traffic's lamps (their scripts run there: the scenery state's LampScripts, by key);

    the player's vehicle (materials, lamps, textures, bellows, damage: view_sync/vehicle.rs reads it in place through LookVars), the bus the player rides in on foot (its sound); the people's coins and ticket blocks; the sync also hands the AI vehicles Envir_Brightness and swaps their poses between the traffic's last two steps.

  • Global mutable state. The introduction's "no global mutable state" is still not quite true, but what is left no longer carries a session's state into the next one.

    The game state that lived in statics (a second drive started from the phone's launcher runs in the same process, and the window, the offscreen run and the launcher's showroom share it) moved into its owners: the physical weather model and the classic sky's cloud type into weather_setup::SkyState (kept by the session, the offscreen run and the showroom), the duty start's opened world and the real-time start into Args, the map lights near the camera, the fading spotlight cookies, the spline-check ends and the season-mix counts into World, the passengers' waiting voice lines, the photo mode's settings and the settings window's flags into App's groups, the /admin password into LanGame, the drive's extremes into Offscreen, and which corona pictures a renderer holds into the Renderer (pictures_taken).

    Left: in omsi-app 20 module-level statics with interior mutability, 35 more inside functions and thread_local! blocks (9 of those); in omsi-scene 14 and 15, no thread_local! (count them with grep -rnE '^\s*(pub(\([a-z:]+\))? )?static ' crates/omsi-app/src | grep -E 'Mutex|Atomic|OnceLock|RwLock|Cell|Lazy|Once\b' and grep -rn 'thread_local!').

    They are install-once hooks, platform handles and process resources (the signal handler, the tilt sensor, the translation worker, thread pools, the LAN gateway and mods root), memos of file contents and environment switches, rate limits of log lines and refreshes, the picture-file numbering that is the same for every renderer.

    The settings and the session's weather that were broadcast to loading code through statics are passed down now: the session's look and switches in Args::session_settings (startup::SessionSettings: Enhanced+, Enhanced, Vanilla, clouds), carried on to the SkyState, the tyres' spray (scene::host::GraphicsKind) and the renderer's options;

    what the world's loading and sounds read (the scenery's volume, the mirrors' size, the rain's quality, the weather's snow) in each World (World::settings, a scene::host::SceneSettings); the traffic's volume, the rain's quality and the lamps' cone weather (lights::ConeWeather) as arguments of the calls that use them.

  • Very long functions and files. scripts/size-budget.sh (run on every pull request) keeps them from growing; scripts/size-budget.txt lists those over the limits (1500 lines a file, 300 a function) with their recorded sizes. The baseline only goes down.

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

↑↓ to move↵ to open