Skip to main content
Verticopolis engine

verticopolis_engine/view/
layout.rs

1//! Every slot and record width of the view's frame and structural snapshot,
2//! by name. Decode both with these, such as `frame[header::MONEY]`, never
3//! with bare numbers. [`super::layout_json`] hands the same tables over as
4//! JSON, with the state, weather, mode and calendar codes, for a host in
5//! another language.
6use serde_json::{json, Map, Value};
7
8/// The version of the view's layout. It goes up when a slot moves, a width
9/// changes, or a slot or code changes meaning.
10pub const VIEW_LAYOUT_VERSION: u32 = 27;
11
12/// The dynamic frame's header slots, one name each. `UNLOCKED` holds the
13/// unlocked kinds as a bitmask by kind code in one double, with more than 32
14/// bits in use: read bit k as `Math.floor(mask / 2 ** k) % 2`, since
15/// JavaScript's bitwise operators cut a number to 32 bits. `FRAME_SEQ` counts
16/// the reader's frames from 1: a host that sees it skip a number missed a
17/// frame, and asks for a full one before it applies another. `DAY` through
18/// `WEEKEND_DAYS` are the clock's readings on the tower's calendar (the day
19/// count, the day of the week, the weekend flag as 0 or 1, the quarter and the
20/// year, all counted from 0) and the calendar's week and weekend lengths in
21/// days, so a host shows dates without doing the date arithmetic.
22/// `BOMB_SEARCH_SEQ` is 0 while no bomb search runs and otherwise moves with
23/// every change to it, so a host rereads the search only when it moved.
24/// `THIEF_FX_X` is a tile of the stretch of floor the last thief prowled,
25/// so a host runs him along that tower's rooms on a lot with several.
26// Each slot is named for the value it holds.
27#[allow(missing_docs)]
28pub mod header {
29    pub const MINUTES: usize = 0;
30    pub const TOWER_REVISION: usize = 1;
31    pub const META_REVISION: usize = 2;
32    pub const MEAL_OVERLAY_REVISION: usize = 3;
33    pub const LOG_SEQ: usize = 4;
34    pub const MONEY: usize = 5;
35    pub const STAR: usize = 6;
36    pub const UNLOCKED: usize = 7;
37    pub const POPULATION: usize = 8;
38    pub const WEATHER: usize = 9;
39    pub const SANTA_FX_SEQ: usize = 10;
40    pub const EXPLOSION_FX_SEQ: usize = 11;
41    pub const EXPLOSION_FX_FLOOR: usize = 12;
42    pub const EXPLOSION_FX_X: usize = 13;
43    pub const THIEF_FX_SEQ: usize = 14;
44    pub const THIEF_FX_CAUGHT: usize = 15;
45    pub const THIEF_FX_FLOOR: usize = 16;
46    pub const TREASURE_FX_SEQ: usize = 17;
47    pub const TREASURE_FX_FLOOR: usize = 18;
48    pub const TREASURE_FX_X: usize = 19;
49    pub const VIP_FX_SEQ: usize = 20;
50    pub const FIRES_STARTED: usize = 21;
51    pub const FIRES_GUT_ROOMS: usize = 22;
52    pub const BOMBS_DETONATED: usize = 23;
53    pub const PENDING_CHOICE: usize = 24;
54    pub const ON_HOUR_RUNS: usize = 25;
55    pub const ROOMS: usize = 26;
56    pub const FULL: usize = 27;
57    pub const FRAME_SEQ: usize = 28;
58    pub const PEOPLE: usize = 29;
59    pub const UNITS: usize = 30;
60    pub const TRANSPORTS: usize = 31;
61    pub const DAY: usize = 32;
62    pub const DAY_OF_WEEK: usize = 33;
63    pub const IS_WEEKEND: usize = 34;
64    pub const QUARTER: usize = 35;
65    pub const YEAR: usize = 36;
66    pub const WEEK_DAYS: usize = 37;
67    pub const WEEKEND_DAYS: usize = 38;
68    pub const BOMB_SEARCH_SEQ: usize = 39;
69    pub const THIEF_FX_X: usize = 40;
70}
71
72/// Slots of the dynamic frame's header.
73pub const HEADER: usize = header::THIEF_FX_X + 1;
74
75/// The slots of a drawn person's record.
76// Each slot is named for the value it holds.
77#[allow(missing_docs)]
78pub mod person {
79    pub const ID: usize = 0;
80    pub const SEED: usize = 1;
81    pub const STAFF: usize = 2;
82    pub const STATE: usize = 3;
83    pub const FLOOR: usize = 4;
84    pub const X: usize = 5;
85    pub const FY: usize = 6;
86    pub const WAIT: usize = 7;
87}
88
89/// Slots of a person record.
90pub const PERSON_RECORD: usize = person::WAIT + 1;
91
92/// The slots of a changed room's record: its slot in the structural
93/// snapshot's room list, then its frame fields.
94// Each slot is named for the value it holds.
95#[allow(missing_docs)]
96pub mod unit {
97    pub const SLOT: usize = 0;
98    pub const STATE: usize = 1;
99    pub const OCCUPANTS: usize = 2;
100    pub const CUSTOMERS_IN: usize = 3;
101    pub const HOTEL_CUSTOMERS_IN: usize = 4;
102    pub const OUT_FOR_MEAL: usize = 5;
103}
104
105/// Slots of a room record in the dynamic frame.
106pub const UNIT_RECORD: usize = unit::OUT_FOR_MEAL + 1;
107
108/// The slots of a transport's record in the dynamic frame: `ID` and `CARS`,
109/// then `CARS` car records.
110// Each slot is named for the value it holds.
111#[allow(missing_docs)]
112pub mod transport {
113    pub const ID: usize = 0;
114    pub const CARS: usize = 1;
115}
116
117/// Slots of a transport record before its cars.
118pub const TRANSPORT_FIXED: usize = transport::CARS + 1;
119
120/// The slots of one car, counted from the car's start.
121// Each slot is named for the value it holds.
122#[allow(missing_docs)]
123pub mod car {
124    pub const POSITION: usize = 0;
125    pub const LOAD: usize = 1;
126    pub const DIRECTION: usize = 2;
127}
128
129/// Slots of each car.
130pub const CAR_RECORD: usize = car::DIRECTION + 1;
131
132/// The structural snapshot's header slots. `HIGHEST_FLOOR` and
133/// `LOWEST_FLOOR` are the highest and lowest floor any unit stands on (1 on
134/// an empty tower), the bounds the camera reads; `MODE` and `CALENDAR` are
135/// the game mode and the Modern calendar, by the codes in `MODES` and
136/// `CALENDARS`; `SCENE_SEED` is the founding seed the scenery derives from
137/// (so the scenery stays the same across saves), fixed for a tower's life.
138// Each slot is named for the value it holds.
139#[allow(missing_docs)]
140pub mod structure {
141    pub const REVISION: usize = 0;
142    pub const ROOMS: usize = 1;
143    pub const RUNS: usize = 2;
144    pub const SHAFTS: usize = 3;
145    pub const HIGHEST_FLOOR: usize = 4;
146    pub const LOWEST_FLOOR: usize = 5;
147    pub const MODE: usize = 6;
148    pub const CALENDAR: usize = 7;
149    pub const SCENE_SEED: usize = 8;
150}
151
152/// Slots of the structural snapshot's header.
153pub const STRUCTURE_HEADER: usize = structure::SCENE_SEED + 1;
154
155/// The slots of a room in the structural snapshot.
156// Each slot is named for the value it holds.
157#[allow(missing_docs)]
158pub mod room {
159    pub const ID: usize = 0;
160    pub const KIND: usize = 1;
161    pub const FLOOR: usize = 2;
162    pub const X: usize = 3;
163    pub const WIDTH: usize = 4;
164}
165
166/// Slots of a room record in the structural snapshot.
167pub const ROOM_RECORD: usize = room::WIDTH + 1;
168
169/// The slots of a run of floor or lobby tiles: `COUNT` tiles of one kind and
170/// width side by side on one floor, the k-th at `X + k * WIDTH`, every one
171/// with the same frame fields.
172// Each slot is named for the value it holds.
173#[allow(missing_docs)]
174pub mod run {
175    pub const KIND: usize = 0;
176    pub const FLOOR: usize = 1;
177    pub const X: usize = 2;
178    pub const WIDTH: usize = 3;
179    pub const COUNT: usize = 4;
180    pub const STATE: usize = 5;
181    pub const OCCUPANTS: usize = 6;
182    pub const CUSTOMERS_IN: usize = 7;
183    pub const HOTEL_CUSTOMERS_IN: usize = 8;
184    pub const OUT_FOR_MEAL: usize = 9;
185}
186
187/// Slots of a run record.
188pub const RUN_RECORD: usize = run::OUT_FOR_MEAL + 1;
189
190/// The slots of a transport in the structural snapshot, before its
191/// `SKIPS` skipped floors.
192// Each slot is named for the value it holds.
193#[allow(missing_docs)]
194pub mod shaft {
195    pub const ID: usize = 0;
196    pub const KIND: usize = 1;
197    pub const X: usize = 2;
198    pub const WIDTH: usize = 3;
199    pub const BOTTOM: usize = 4;
200    pub const TOP: usize = 5;
201    pub const CARS: usize = 6;
202    pub const SKIPS: usize = 7;
203}
204
205/// Slots of a shaft record before its skipped floors.
206pub const SHAFT_FIXED: usize = shaft::SKIPS + 1;
207
208/// One named table of slots, as `layout_json` hands it over: its name in the
209/// JSON, the slots by camelCase name, the width key and the width.
210pub(crate) struct Table {
211    pub(crate) name: &'static str,
212    pub(crate) slots: &'static [(&'static str, usize)],
213    pub(crate) width_name: &'static str,
214    pub(crate) width: usize,
215}
216
217/// Every table of the layout, in the order `layout_json` writes them.
218pub(crate) const TABLES: &[Table] = &[
219    Table {
220        name: "header",
221        slots: &[
222            ("minutes", header::MINUTES),
223            ("towerRevision", header::TOWER_REVISION),
224            ("metaRevision", header::META_REVISION),
225            ("mealOverlayRevision", header::MEAL_OVERLAY_REVISION),
226            ("logSeq", header::LOG_SEQ),
227            ("money", header::MONEY),
228            ("star", header::STAR),
229            ("unlocked", header::UNLOCKED),
230            ("population", header::POPULATION),
231            ("weather", header::WEATHER),
232            ("santaFxSeq", header::SANTA_FX_SEQ),
233            ("explosionFxSeq", header::EXPLOSION_FX_SEQ),
234            ("explosionFxFloor", header::EXPLOSION_FX_FLOOR),
235            ("explosionFxX", header::EXPLOSION_FX_X),
236            ("thiefFxSeq", header::THIEF_FX_SEQ),
237            ("thiefFxCaught", header::THIEF_FX_CAUGHT),
238            ("thiefFxFloor", header::THIEF_FX_FLOOR),
239            ("treasureFxSeq", header::TREASURE_FX_SEQ),
240            ("treasureFxFloor", header::TREASURE_FX_FLOOR),
241            ("treasureFxX", header::TREASURE_FX_X),
242            ("vipFxSeq", header::VIP_FX_SEQ),
243            ("firesStarted", header::FIRES_STARTED),
244            ("firesGutRooms", header::FIRES_GUT_ROOMS),
245            ("bombsDetonated", header::BOMBS_DETONATED),
246            ("pendingChoice", header::PENDING_CHOICE),
247            ("onHourRuns", header::ON_HOUR_RUNS),
248            ("rooms", header::ROOMS),
249            ("full", header::FULL),
250            ("frameSeq", header::FRAME_SEQ),
251            ("people", header::PEOPLE),
252            ("units", header::UNITS),
253            ("transports", header::TRANSPORTS),
254            ("day", header::DAY),
255            ("dayOfWeek", header::DAY_OF_WEEK),
256            ("isWeekend", header::IS_WEEKEND),
257            ("quarter", header::QUARTER),
258            ("year", header::YEAR),
259            ("weekDays", header::WEEK_DAYS),
260            ("weekendDays", header::WEEKEND_DAYS),
261            ("bombSearchSeq", header::BOMB_SEARCH_SEQ),
262            ("thiefFxX", header::THIEF_FX_X),
263        ],
264        width_name: "headerWidth",
265        width: HEADER,
266    },
267    Table {
268        name: "person",
269        slots: &[
270            ("id", person::ID),
271            ("seed", person::SEED),
272            ("staff", person::STAFF),
273            ("state", person::STATE),
274            ("floor", person::FLOOR),
275            ("x", person::X),
276            ("fy", person::FY),
277            ("wait", person::WAIT),
278        ],
279        width_name: "personWidth",
280        width: PERSON_RECORD,
281    },
282    Table {
283        name: "unit",
284        slots: &[
285            ("slot", unit::SLOT),
286            ("state", unit::STATE),
287            ("occupants", unit::OCCUPANTS),
288            ("customersIn", unit::CUSTOMERS_IN),
289            ("hotelCustomersIn", unit::HOTEL_CUSTOMERS_IN),
290            ("outForMeal", unit::OUT_FOR_MEAL),
291        ],
292        width_name: "unitWidth",
293        width: UNIT_RECORD,
294    },
295    Table {
296        name: "transport",
297        slots: &[("id", transport::ID), ("cars", transport::CARS)],
298        width_name: "transportFixed",
299        width: TRANSPORT_FIXED,
300    },
301    Table {
302        name: "car",
303        slots: &[
304            ("position", car::POSITION),
305            ("load", car::LOAD),
306            ("direction", car::DIRECTION),
307        ],
308        width_name: "carWidth",
309        width: CAR_RECORD,
310    },
311    Table {
312        name: "structure",
313        slots: &[
314            ("revision", structure::REVISION),
315            ("rooms", structure::ROOMS),
316            ("runs", structure::RUNS),
317            ("shafts", structure::SHAFTS),
318            ("highestFloor", structure::HIGHEST_FLOOR),
319            ("lowestFloor", structure::LOWEST_FLOOR),
320            ("mode", structure::MODE),
321            ("calendar", structure::CALENDAR),
322            ("sceneSeed", structure::SCENE_SEED),
323        ],
324        width_name: "structureWidth",
325        width: STRUCTURE_HEADER,
326    },
327    Table {
328        name: "room",
329        slots: &[
330            ("id", room::ID),
331            ("kind", room::KIND),
332            ("floor", room::FLOOR),
333            ("x", room::X),
334            ("width", room::WIDTH),
335        ],
336        width_name: "roomWidth",
337        width: ROOM_RECORD,
338    },
339    Table {
340        name: "run",
341        slots: &[
342            ("kind", run::KIND),
343            ("floor", run::FLOOR),
344            ("x", run::X),
345            ("width", run::WIDTH),
346            ("count", run::COUNT),
347            ("state", run::STATE),
348            ("occupants", run::OCCUPANTS),
349            ("customersIn", run::CUSTOMERS_IN),
350            ("hotelCustomersIn", run::HOTEL_CUSTOMERS_IN),
351            ("outForMeal", run::OUT_FOR_MEAL),
352        ],
353        width_name: "runWidth",
354        width: RUN_RECORD,
355    },
356    Table {
357        name: "shaft",
358        slots: &[
359            ("id", shaft::ID),
360            ("kind", shaft::KIND),
361            ("x", shaft::X),
362            ("width", shaft::WIDTH),
363            ("bottom", shaft::BOTTOM),
364            ("top", shaft::TOP),
365            ("cars", shaft::CARS),
366            ("skips", shaft::SKIPS),
367        ],
368        width_name: "shaftFixed",
369        width: SHAFT_FIXED,
370    },
371];
372
373/// The unit state names by the code a frame carries (`frame::unit_state_code`).
374pub(crate) const UNIT_STATES: [&str; 10] = [
375    "construction",
376    "empty",
377    "occupied",
378    "moving_in",
379    "vacating",
380    "asleep",
381    "dirty",
382    "infested",
383    "fire",
384    "gutted",
385];
386
387/// The person state names by code (`frame::person_state_code`).
388pub(crate) const PERSON_STATES: [&str; 7] = [
389    "toShaft", "waiting", "riding", "climbing", "toDest", "dwelling", "done",
390];
391
392/// The weather names by code (`frame::weather_code`).
393pub(crate) const WEATHER: [&str; 3] = ["clear", "cloudy", "rain"];
394
395/// The game modes by code, as a save spells them (`structure::mode_code`).
396pub(crate) const MODES: [&str; 2] = ["classic", "modern"];
397
398/// The Modern calendars by code, as a save spells them
399/// (`structure::calendar_code`).
400pub(crate) const CALENDARS: [&str; 2] = ["canon", "realWorld"];
401
402/// The fields of a unit's metadata record in `unitMetaSince`'s listing, in
403/// order (`meta::meta_since`).
404pub(crate) const META_RECORD: [&str; 3] = ["id", "label", "subtype"];
405
406/// The layout as a value: the version, every table's slots and width, the
407/// codes (kinds, unit and person states, weather, modes, calendars) and the
408/// metadata record's fields. A kind's code is its index in `kinds` (the
409/// catalog's order, `Kind as usize`), and `names` holds each kind's catalog
410/// name, which is a unit's label when a full metadata listing leaves the
411/// unit out.
412pub fn layout_json() -> Value {
413    let mut m = Map::new();
414    m.insert("version".into(), json!(VIEW_LAYOUT_VERSION));
415    for t in TABLES {
416        let slots: Map<String, Value> = t
417            .slots
418            .iter()
419            .map(|(name, slot)| ((*name).to_owned(), json!(slot)))
420            .collect();
421        m.insert(t.name.into(), Value::Object(slots));
422        m.insert(t.width_name.into(), json!(t.width));
423    }
424    let facilities = &crate::facilities::FACILITIES;
425    m.insert(
426        "kinds".into(),
427        json!(facilities.iter().map(|f| f.key).collect::<Vec<_>>()),
428    );
429    m.insert(
430        "names".into(),
431        json!(facilities.iter().map(|f| f.name).collect::<Vec<_>>()),
432    );
433    m.insert("unitStates".into(), json!(UNIT_STATES));
434    m.insert("personStates".into(), json!(PERSON_STATES));
435    m.insert("weather".into(), json!(WEATHER));
436    m.insert("modes".into(), json!(MODES));
437    m.insert("calendars".into(), json!(CALENDARS));
438    m.insert("metaRecord".into(), json!(META_RECORD));
439    Value::Object(m)
440}