Expand description
Saves: writing a game to a .vctower file and reading one back.
A .vctower file is the web game’s save format: a first line naming the
format (TOWER_FILE_MAGIC), then the save JSON, compressed. A save from
any older version loads; the loader upgrades it first. A save from a newer
version is refused.
use verticopolis_engine::clock::GameMode;
use verticopolis_engine::load::{
decode_vctower, deserialize, encode_simulation_vctower, mark_founder_from_loaded_file,
};
use verticopolis_engine::sim::Simulation;
let sim = Simulation::new_game(5, GameMode::Classic);
let bytes = encode_simulation_vctower(&sim, "my-host 1.0.0", 1_791_849_600_000.0).unwrap();
let text = String::from_utf8(bytes).unwrap();
let save = decode_vctower(&text).unwrap();
let mut loaded = deserialize(&save).unwrap();
mark_founder_from_loaded_file(&mut loaded, &save);
assert_eq!(loaded.money, sim.money);A save holds the tower and leaves out the people walking around and the elevators’ dispatch state, so a loaded game restarts both and does not track the game that wrote it. Two loads of the same save do track each other exactly.
The loader repairs: deserialize corrects damaged or edited fields the
way the web game’s loader does, and refuses only a file it cannot read.
The JSON inside the file is what
Simulation::serialize returns.
Constants§
- TOWER_
FILE_ MAGIC - The first line of every
.vctowerfile, naming the format and its version.
Functions§
- decode_
vctower - Reads a
.vctowerfile’s text into its save JSON, ready fordeserialize. Refuses text that is not a.vctowerfile, and one that would inflate past 32 MiB. - deserialize
- Builds a game from save JSON (from
decode_vctowerorSimulation::serialize). An older save is upgraded first, and damaged or edited fields are corrected the way the web game’s loader corrects them. Refuses a save from a newer version of the engine, and one it cannot read, with a sentence a player can read. - deserialize_
owned deserializefor save JSON the caller no longer needs: it upgrades the save in place and frees it as it goes, so a large tower loads in less memory.- encode_
simulation_ vctower - Writes a game as
.vctowerfile bytes:Simulation::serialize,stamp_saveandencode_vctowerin one call, without holding the whole save in memory at once. The engine has no clock or version of its own, so pass your host’s name and version and the time in milliseconds since the Unix epoch. - encode_
vctower - Writes save JSON as
.vctowerfile bytes, as given. To write a player’s tower, stamp it first withstamp_save, or useencode_simulation_vctower, which does both: a save with no stamp loads as a founder’s tower. Refuses a save larger than a loader accepts. - mark_
founder_ from_ loaded_ file - Marks a freshly loaded game as a founder’s tower when its file carries no
build stamp, as the web game does. Call it after
deserializeon a file the player opened; skip it and the next save drops the status. - stamp_
save - Stamps save JSON with the app that wrote it and when, as the web game does
before writing a file.
app_versionnames your host and its version (it must not read as a build older than 2.0, which the web game takes for a founder’s tower);saved_at_msis the time in milliseconds since the Unix epoch. Refuses JSON that is not a tower.