verticopolis_engine/save_floor_runs.rs
1//! Floor tiles written as runs (save version 11).
2//!
3//! Most of a built tower's units are plain floor tiles: a large Modern tower
4//! lists about 19,000 of them, each one `{"id":43,"kind":"floor","floor":2,"x":167}`,
5//! and they were about 70% of its save. From version 11 the writer folds a
6//! row of them into the first tile's entry with a `run` count:
7//! `{"id":43,"kind":"floor","floor":2,"x":167,"run":60}` stands for 60 tiles
8//! on floor 2, at x 167 to 226, with ids 43 to 102.
9//!
10//! A run only takes tiles that sit next to each other in the unit list, one
11//! tile to the right and one id up from the tile before, and whose save entry
12//! holds nothing but `id`, `kind`, `floor` and `x`. So unfolding a run in
13//! place gives back exactly the list a version 10 writer would have written,
14//! in the same order, with the same ids. The loader unfolds it
15//! (`load::floor_runs`).
16
17use crate::facilities::{Kind, LOT_WIDTH};
18use crate::tower::{SavedUnit, Tower, Unit};
19
20/// The key on a run's first floor tile: how many tiles the run holds.
21pub const FLOOR_RUN_KEY: &str = "run";
22
23/// The save version that first writes floor runs. A build from before it
24/// would read a run as its first tile alone, so it refuses the save instead.
25pub const FLOOR_RUN_SAVE_VERSION: i64 = 11;
26
27/// Whether `next` could follow `tile` in a floor run: both floor tiles on
28/// the same floor, `next` one tile to the right and with the next id. Their
29/// other facts are not compared, so this holds wherever the writer might
30/// fold, and the version stamp leans on that.
31pub(crate) fn could_continue_run(tile: &Unit, next: &Unit) -> bool {
32 tile.kind == Kind::Floor
33 && next.kind == Kind::Floor
34 && tile.floor == next.floor
35 && next.x == tile.x + 1
36 && next.id == tile.id + 1
37}
38
39/// Whether the writer could fold any two of `units` into a run, so the save
40/// must be stamped [`FLOOR_RUN_SAVE_VERSION`]. One pass over the list with
41/// no allocation; it stops at the first pair.
42pub(crate) fn lists_a_floor_run(units: &[Unit]) -> bool {
43 units.windows(2).any(|w| could_continue_run(&w[0], &w[1]))
44}
45
46impl Tower {
47 /// [`lists_a_floor_run`] over this tower's units, walked once per tower
48 /// revision like the other unit indexes (`tower_index`): every save and
49 /// every sync delta stamps its version from it.
50 pub(crate) fn lists_a_floor_run(&self) -> bool {
51 // A list too short for a pair answers at once and leaves the memo
52 // alone: the sync delta writes the rest of a save with the units lent
53 // out, and that must not replace the tower's own answer.
54 if self.units.len() < 2 {
55 return false;
56 }
57 let key = self.index_key();
58 if let Some((built, listed)) = self.memo.borrow().floor_run {
59 if built == key {
60 return listed;
61 }
62 }
63 let listed = lists_a_floor_run(&self.units);
64 self.memo.borrow_mut().floor_run = Some((key, listed));
65 listed
66 }
67}
68
69/// Whether a unit's save entry is a plain floor tile, nothing but `id`,
70/// `kind`, `floor` and `x`: only such a tile joins a run, so a tile that
71/// carries any other fact keeps its own entry.
72fn is_plain_floor_tile(unit: &Unit) -> bool {
73 unit.kind == Kind::Floor && unit.saves_only_position()
74}
75
76/// Each unit's save entry in list order, with every run of plain floor tiles
77/// folded into its first tile's entry. Nothing is built: each entry is
78/// written straight from its unit (`SavedUnit`).
79pub(crate) struct SavedUnits<'a> {
80 units: &'a [Unit],
81 next: usize,
82}
83
84impl<'a> SavedUnits<'a> {
85 pub(crate) fn new(units: &'a [Unit]) -> Self {
86 SavedUnits { units, next: 0 }
87 }
88}
89
90impl<'a> Iterator for SavedUnits<'a> {
91 type Item = SavedUnit<'a>;
92
93 fn next(&mut self) -> Option<SavedUnit<'a>> {
94 let first = self.units.get(self.next)?;
95 self.next += 1;
96 if !is_plain_floor_tile(first) {
97 return Some(SavedUnit {
98 unit: first,
99 run: None,
100 });
101 }
102 let mut tiles = 1usize;
103 while let Some(unit) = self.units.get(self.next) {
104 // A run never holds more tiles than the lot is wide: the loader
105 // reads a longer one as its first tile alone. A labeled tile ends
106 // the run and opens the next entry.
107 if tiles >= LOT_WIDTH as usize
108 || !could_continue_run(&self.units[self.next - 1], unit)
109 || !is_plain_floor_tile(unit)
110 {
111 break;
112 }
113 tiles += 1;
114 self.next += 1;
115 }
116 Some(SavedUnit {
117 unit: first,
118 run: (tiles > 1).then_some(tiles),
119 })
120 }
121}
122
123#[cfg(test)]
124#[path = "save_floor_runs_tests.rs"]
125mod tests;