Skip to main content
Verticopolis engine

verticopolis_engine/view/
mod.rs

1//! What a renderer reads, sized for a frame budget. Three reads cover it:
2//!
3//! - [`structure_view`]: the floors, lobbies, rooms and shafts, as flat
4//!   records. Read it when you start, after a load, and whenever the frame
5//!   header's tower revision moves (a build, a sale, a fire).
6//! - [`ViewReader::frame`]: everything that moves, every frame: the clock,
7//!   money and rating, the people, the cars, and only the rooms whose
8//!   fields changed since this reader's last frame.
9//! - [`meta_since`]: room names and subtypes that changed, read when the
10//!   frame header's meta revision moves.
11//!
12//! [`layout`] names every slot, and [`layout_json`] hands the same tables
13//! and every code over as JSON. None of these reads changes the game.
14//!
15//! ```
16//! use verticopolis_engine::clock::GameMode;
17//! use verticopolis_engine::facilities::Kind;
18//! use verticopolis_engine::sim::Simulation;
19//! use verticopolis_engine::view::{layout, structure_view, ViewReader};
20//!
21//! let mut sim = Simulation::new_game(1, GameMode::Classic);
22//! let tiles: Vec<i64> = (100..124).collect();
23//! sim.paint_brush(Kind::Lobby, 1, &tiles);
24//!
25//! let structure = structure_view(&sim);
26//! assert_eq!(structure[layout::structure::LOWEST_FLOOR], 1.0);
27//!
28//! let mut reader = ViewReader::new();
29//! let first = reader.frame(&sim, false);
30//! assert_eq!(first[layout::header::FRAME_SEQ], 1.0);
31//! sim.tick(1.0);
32//! let next = reader.frame(&sim, false);
33//! assert_eq!(next[layout::header::FRAME_SEQ], 2.0);
34//! ```
35pub mod layout;
36mod marks;
37mod meta;
38mod reader;
39mod signature;
40mod structure;
41#[cfg(any(feature = "wasm", test))]
42mod tally;
43mod window;
44
45use std::cell::RefCell;
46
47pub use layout::{layout_json, VIEW_LAYOUT_VERSION};
48use marks::FrameMarks;
49pub use meta::meta_since;
50pub(crate) use meta::FIRST_META_REVISION;
51#[doc(hidden)]
52pub use meta::{meta_changed, whole_revision, MetaLog};
53pub use reader::ViewReader;
54pub use structure::structure_view;
55#[cfg(all(feature = "wasm", test))]
56pub(crate) use tally::TallyPasses;
57#[cfg(any(feature = "wasm", test))]
58pub(crate) use tally::{CensusTally, RecyclingCounts};
59pub use window::{PeopleWindow, FLOOR_PAD};
60
61use crate::sim::Simulation;
62
63#[doc(hidden)]
64/// What the view keeps on the tower between reads: the metadata log and the
65/// frame mark log the setters write, and the binding's frame reader.
66pub struct ViewState {
67    pub(crate) meta_log: MetaLog,
68    pub(crate) marks: FrameMarks,
69    pub(crate) reader: RefCell<ViewReader>,
70}
71
72impl Default for ViewState {
73    fn default() -> ViewState {
74        ViewState {
75            meta_log: MetaLog::new(),
76            marks: FrameMarks::default(),
77            reader: RefCell::new(ViewReader::new()),
78        }
79    }
80}
81
82#[doc(hidden)]
83/// The dynamic frame from the tower's own reader (the binding's): one
84/// reader per engine, so a second caller sees only what changed since the
85/// first one's read. `full` asks for every room.
86pub fn view_frame(sim: &Simulation, full: bool) -> Vec<f64> {
87    sim.tower.view.reader.borrow_mut().frame(sim, full)
88}
89
90#[doc(hidden)]
91/// Set the people window of the tower's own reader (the binding's), so its
92/// frames carry only the people a view draws; `None` sends every person.
93/// The window stays with the reader, so a new tower starts with none.
94pub fn set_people_window(sim: &Simulation, window: Option<PeopleWindow>) {
95    sim.tower.view.reader.borrow_mut().set_people_window(window);
96}
97
98#[cfg(test)]
99#[path = "census_reads_tests.rs"]
100mod census_reads_tests;
101#[cfg(test)]
102#[path = "frame_tests.rs"]
103mod frame_tests;
104#[cfg(test)]
105#[path = "kind_writes_tests.rs"]
106mod kind_writes_tests;
107#[cfg(test)]
108#[path = "layout_tests.rs"]
109mod layout_tests;
110#[cfg(test)]
111#[path = "lexer_tests.rs"]
112mod lexer_tests;
113#[cfg(test)]
114#[path = "marks_guard_tests.rs"]
115mod marks_guard_tests;
116#[cfg(test)]
117#[path = "marks_tests.rs"]
118mod marks_tests;
119#[cfg(test)]
120#[path = "merge_guard_tests.rs"]
121mod merge_guard_tests;
122#[cfg(test)]
123#[path = "meta_tests.rs"]
124mod meta_tests;
125#[cfg(test)]
126#[path = "oracle_tests.rs"]
127mod oracle_tests;
128#[cfg(test)]
129#[path = "session_tests.rs"]
130mod session_tests;
131#[cfg(test)]
132#[path = "signature_tests.rs"]
133mod signature_tests;
134#[cfg(test)]
135#[path = "source_scan_tests.rs"]
136mod source_scan_tests;
137#[cfg(test)]
138#[path = "structure_tests.rs"]
139mod structure_tests;
140#[cfg(test)]
141#[path = "tally_tests.rs"]
142mod tally_tests;
143#[cfg(test)]
144#[path = "unit_scan_tests.rs"]
145mod unit_scan_tests;
146#[cfg(test)]
147#[path = "write_cases_tests.rs"]
148mod write_cases_tests;