Skip to main content
Verticopolis engineGitHub

Module load

Module load 

Source
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 .vctower file, naming the format and its version.

Functions§

decode_vctower
Reads a .vctower file’s text into its save JSON, ready for deserialize. Refuses text that is not a .vctower file, and one that would inflate past 32 MiB.
deserialize
Builds a game from save JSON (from decode_vctower or Simulation::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
deserialize for 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 .vctower file bytes: Simulation::serialize, stamp_save and encode_vctower in 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 .vctower file bytes, as given. To write a player’s tower, stamp it first with stamp_save, or use encode_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 deserialize on 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_version names 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_ms is the time in milliseconds since the Unix epoch. Refuses JSON that is not a tower.