verticopolis_engine/load.rs
1//! Saves: writing a game to a `.vctower` file and reading one back.
2//!
3//! A `.vctower` file is the web game's save format: a first line naming the
4//! format ([`TOWER_FILE_MAGIC`]), then the save JSON, compressed. A save from
5//! any older version loads; the loader upgrades it first. A save from a newer
6//! version is refused.
7//!
8//! ```
9//! use verticopolis_engine::clock::GameMode;
10//! use verticopolis_engine::load::{
11//! decode_vctower, deserialize, encode_simulation_vctower, mark_founder_from_loaded_file,
12//! };
13//! use verticopolis_engine::sim::Simulation;
14//!
15//! let sim = Simulation::new_game(5, GameMode::Classic);
16//! let bytes = encode_simulation_vctower(&sim, "my-host 1.0.0", 1_791_849_600_000.0).unwrap();
17//!
18//! let text = String::from_utf8(bytes).unwrap();
19//! let save = decode_vctower(&text).unwrap();
20//! let mut loaded = deserialize(&save).unwrap();
21//! mark_founder_from_loaded_file(&mut loaded, &save);
22//! assert_eq!(loaded.money, sim.money);
23//! ```
24//!
25//! A save holds the tower and leaves out the people walking around and the
26//! elevators' dispatch state, so a loaded game restarts both and does not
27//! track the game that wrote it. Two loads of the same save do track each
28//! other exactly.
29//!
30//! The loader repairs: [`deserialize`] corrects damaged or edited fields the
31//! way the web game's loader does. It refuses a file it cannot read, and a
32//! save with no numeric `minutes` or no `units` list at its top level.
33//! The JSON inside the file is what
34//! [`Simulation::serialize`](crate::sim::Simulation::serialize) returns.
35
36use std::collections::{HashMap, HashSet};
37
38use base64::Engine;
39use indexmap::IndexMap;
40use serde_json::{json, Map, Value};
41
42use crate::clock::{CalendarKind, Clock, GameMode};
43use crate::econ::{classic_ladder, rent_config};
44use crate::events::PendingChoice;
45use crate::facilities::*;
46use crate::jsmath::{number_to_string, round};
47use crate::ledger::Ledger;
48use crate::rent::snap_to_ladder;
49use crate::rng::Rng;
50use crate::schedule::Schedule;
51use crate::sim::{LogEntry, LogKind, Simulation, LOG_RING_CAP, SAVE_VERSION};
52use crate::sim_loop::weather_for;
53use crate::tower::{unit_list_headroom, Transport, Unit, UnitState};
54
55const SOLD_CONDO_MIN_PRICE: f64 = 60_000.0;
56const SOLD_CONDO_MAX_PRICE: f64 = 240_000.0;
57const VIP_VISITS_CAP: f64 = 1_000_000.0;
58const LOG_TEXT_CAP: usize = 400;
59const LEGACY_CONDO_DEFAULT_PRICE: f64 = 120_000.0;
60/// `Number.MAX_SAFE_INTEGER`: saved whole numbers clamp to it in both loaders,
61/// so a value past `i64` reads the same in both. Without the clamp `as i64`
62/// saturates here to 2^63 - 1, which a double cannot hold exactly (#858).
63const MAX_SAFE_INTEGER: f64 = 9_007_199_254_740_991.0;
64pub(crate) const OLDEST_SAVE_VERSION: i64 = 1;
65/// `MAX_SAVE_INFLATED_BYTES` (saveCompression.ts).
66const MAX_SAVE_INFLATED_BYTES: u64 = 32 * 1024 * 1024;
67pub(crate) const UNIT_CAP: usize =
68 2 * (LOT_WIDTH as usize) * ((MAX_FLOOR - MIN_FLOOR + 1) as usize);
69const LOT: i64 = LOT_WIDTH;
70
71mod assemble;
72mod coerce;
73mod container;
74mod floor_runs;
75mod founder;
76mod guards;
77mod helpers;
78mod migrate;
79mod migrate_v1;
80mod migrate_v4;
81mod migrate_v5;
82mod reader;
83
84pub(crate) use assemble::*;
85use coerce::*;
86pub use container::*;
87pub(crate) use floor_runs::*;
88pub use founder::*;
89use guards::*;
90pub(crate) use helpers::*;
91pub use migrate::*;
92pub use migrate_v1::*;
93pub use migrate_v4::*;
94pub use migrate_v5::*;
95pub use reader::*;
96
97#[cfg(test)]
98#[path = "load_assemble_tests.rs"]
99mod assemble_tests;
100#[cfg(test)]
101mod loader_cases;
102#[cfg(test)]
103mod save_json_tests;
104#[cfg(test)]
105mod text_tests;
106#[cfg(test)]
107mod vctower_encode_tests;