verticopolis_engine/view/signature.rs
1// The tower signature: undo's per-command fingerprint. It reads the fields
2// the web game's undo signature string reads (`towerStateSig`), so a capture
3// and a commit compare equal when those fields are equal, whatever was read
4// in between. Money, the other half of undo's check, stays a value compare
5// beside it. A full state digest would answer too, but costs about 108 ms on
6// the large tower.
7use crate::economy::screens::screen_policies;
8use crate::sim::Simulation;
9use crate::tower::Tower;
10
11impl Simulation {
12 /// A fingerprint of the tower, for undo: read it before and after a
13 /// command, and the command changed the tower exactly when the two differ
14 /// (barring a 64-bit collision). Money is not part of it; compare that
15 /// beside it. It walks every room and shaft, so read it once per
16 /// command; it is too slow to read every frame.
17 ///
18 /// ```
19 /// use verticopolis_engine::clock::GameMode;
20 /// use verticopolis_engine::facilities::Kind;
21 /// use verticopolis_engine::sim::Simulation;
22 ///
23 /// let mut sim = Simulation::new_game(1, GameMode::Classic);
24 /// let before = sim.tower_signature();
25 /// sim.build(Kind::Lobby, 1, 100);
26 /// assert_ne!(sim.tower_signature(), before);
27 /// ```
28 pub fn tower_signature(&self) -> u64 {
29 fingerprint(&self.tower)
30 }
31}
32
33/// A 64-bit multiply-rotate hash over the fields, written word by word.
34/// Deterministic and seedless: it is compared only with itself in the same
35/// engine, and never saved or hashed.
36struct Fingerprint(u64);
37
38impl Fingerprint {
39 const K: u64 = 0x9e37_79b9_7f4a_7c15;
40
41 fn word(&mut self, w: u64) {
42 self.0 = (self.0.rotate_left(5) ^ w).wrapping_mul(Self::K);
43 }
44
45 fn int(&mut self, n: i64) {
46 self.word(n as u64);
47 }
48
49 /// A number as `towerStateSig` prints it: `-0` prints as `0`, and every
50 /// NaN as `NaN`.
51 fn num(&mut self, x: f64) {
52 let x = if x == 0.0 { 0.0 } else { x };
53 self.word(if x.is_nan() {
54 f64::NAN.to_bits()
55 } else {
56 x.to_bits()
57 });
58 }
59
60 /// An optional value, tagged so an absent one differs from any present.
61 fn opt<T>(&mut self, v: Option<T>, f: impl FnOnce(&mut Self, T)) {
62 match v {
63 None => self.word(0),
64 Some(t) => {
65 self.word(1);
66 f(self, t);
67 }
68 }
69 }
70
71 fn text(&mut self, s: &str) {
72 let bytes = s.as_bytes();
73 self.word(bytes.len() as u64);
74 let mut chunks = bytes.chunks_exact(8);
75 for c in &mut chunks {
76 self.word(u64::from_le_bytes(c.try_into().unwrap()));
77 }
78 let rest = chunks.remainder();
79 if !rest.is_empty() {
80 let mut last = [0u8; 8];
81 last[..rest.len()].copy_from_slice(rest);
82 self.word(u64::from_le_bytes(last));
83 }
84 }
85
86 /// A list as `towerStateSig` joins it: an absent list reads as an empty
87 /// one.
88 fn list<T: Copy>(&mut self, v: Option<&[T]>, f: impl Fn(&mut Self, T)) {
89 let v = v.unwrap_or(&[]);
90 self.word(v.len() as u64);
91 for &x in v {
92 f(self, x);
93 }
94 }
95}
96
97/// The fields `towerStateSig` reads, in its order: per unit its kind, floor,
98/// x, label, rent, No Rate flag, film policy (and a second screen's, where
99/// it differs), ticket price and subtype; per transport its
100/// kind, x, bottom, top, cars, skipped floors and schedule (`schedSig`'s
101/// five player-set fields).
102fn fingerprint(tower: &Tower) -> u64 {
103 let mut h = Fingerprint(0);
104 h.word(tower.units.len() as u64);
105 for u in &tower.units {
106 h.word(u.kind as u64);
107 h.int(u.floor);
108 h.int(u.x);
109 h.text(&u.label);
110 h.opt(u.rent, Fingerprint::num);
111 h.word(u.no_rate as u64);
112 h.opt(u.extras().film_policy, Fingerprint::text);
113 // A second screen's policy counts where it differs from the first's
114 // (#1950), so a one-screen theater's fingerprint is as it was.
115 let [first, second] = screen_policies(u);
116 if second != first {
117 h.opt(second, Fingerprint::text);
118 }
119 h.opt(u.extras().ticket_price, Fingerprint::text);
120 h.opt(u.subtype, Fingerprint::text);
121 }
122 h.word(tower.transports.len() as u64);
123 for t in &tower.transports {
124 h.word(t.kind as u64);
125 h.int(t.x);
126 h.int(t.bottom);
127 h.int(t.top);
128 h.int(t.cars);
129 h.list(t.skip_floors.as_deref(), Fingerprint::num);
130 h.opt(t.schedule.as_ref(), |h, s| {
131 h.list(s.weekday.as_deref(), Fingerprint::int);
132 h.list(s.weekend.as_deref(), Fingerprint::int);
133 h.opt(s.waiting_car_response, Fingerprint::num);
134 h.opt(s.standard_floor_departure, Fingerprint::num);
135 h.list(s.home_floors.as_deref(), Fingerprint::int);
136 });
137 }
138 h.0
139}