Skip to main content

unbanished_game/people/
cemetery.rs

1//! Cemeteries and burial: CemeteryComponent and TombstoneComponent. See docs/CITIZEN-LIFE.md
2//! "Burial".
3//!
4//! A cemetery lays out its graves on every other row inside its fence, clear of the gate. A citizen
5//! who dies is buried in the nearest reachable cemetery with a free grave, which gets a tombstone
6//! for five to seven years.
7//!
8//! Original: `FUN_140048100`, `0x140160aa0`.
9// vtable 0x14015f1c0
10
11use crate::scheduler::ticks;
12use crate::terrain::CellType;
13use crate::town::Sim;
14
15/// `DAT_140178934`.
16const PERIOD: f32 = 300.0;
17
18// +0x20
19/// CemeteryDescription (the tombstone template) with its TombstoneDescription: a grave lasts `(life
20/// + RandFloat × range)` × 300 s.
21#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct CemeteryTemplate {
23    /// The tombstone template.
24    pub tombstone: usize,
25    /// How long a grave lasts, in months of 300 seconds.
26    pub life: f32,
27    /// The random part of how long it lasts.
28    pub range: f32,
29}
30
31// TombstoneDescription +0x20 life, +0x24 range; the result goes to TombstoneComponent +0x28.
32/// How long a tombstone stays: `(RandFloat × range + life)` × 300 s. The original draws it in the
33/// TombstoneComponent's setup, while the citizen dies.
34///
35/// Original: `FUN_1400581c0`.
36pub fn tombstone_life(t: &CemeteryTemplate, rng: &mut crate::random::Random) -> i64 {
37    ticks((rng.rand_float() * t.range + t.life) * PERIOD)
38}
39
40impl Sim<'_, '_> {
41    /// Activation (`FUN_140047b60`): the cells inside the fence (one in from the map
42    /// rectangle) on every other row along the gate's side, except the two lines in front of
43    /// the gate (the first MultiObject part).
44    fn lay_graves(&self, e: usize) -> Vec<i32> {
45        let n = self.size as i32;
46        let (x0, y0) = self.placed[e].cell;
47        let (w, h) = self.placed[e].footprint;
48        let yaw = self.placed[e].yaw as i32;
49        let along = yaw == 0 || yaw == 180;
50        let gate = self.sites.get(&e).and_then(|s| s.parts.first().copied());
51        let off = gate.map_or(-10, |g| {
52            let (gx, gy) = self.placed[g].cell;
53            if along {
54                gx - x0
55            } else {
56                gy - y0
57            }
58        });
59        let (x1, y1, w2, h2) = (x0 + 1, y0 + 1, w - 2, h - 2);
60        let mut graves = Vec::new();
61        if along {
62            for row in (0..h2).step_by(2) {
63                for col in 0..w2 {
64                    if col != off + 1 && col != off {
65                        graves.push((row + y1) * n + col + x1);
66                    }
67                }
68            }
69        } else {
70            for col in (0..w2).step_by(2) {
71                for row in 0..h2 {
72                    if row != off + 1 && row != off {
73                        graves.push((row + y1) * n + col + x1);
74                    }
75                }
76            }
77        }
78        graves
79    }
80
81    /// State 2 enter (`FUN_140047920`): join the town's cemeteries; a grave without a
82    /// tombstone is plain ground.
83    pub(crate) fn cemetery_enter2(&mut self, e: usize) {
84        self.cemeteries.push(e);
85        if self.sites.get(&e).is_some_and(|s| s.graves.is_empty()) {
86            let graves = self.lay_graves(e);
87            self.sites.get_mut(&e).unwrap().graves = graves;
88        }
89        for cell in self.sites[&e].graves.clone() {
90            let i = cell as usize;
91            if self.cells.objects[i].is_none_or(|o| !self.tombstones.contains_key(&o)) {
92                self.cells.set_type(i, CellType(2));
93                self.cells.objects[i] = None;
94            }
95        }
96    }
97
98    /// The burials decided this update: the grave becomes plain ground and gets a tombstone turned
99    /// as the cemetery, which lasts the time drawn at the death and stamps its cell.
100    ///
101    /// Original: `FUN_1400473f0`.
102    pub fn place_burials(&mut self) {
103        for (cemetery, cell, life) in std::mem::take(self.burials) {
104            let Some(t) = self
105                .sites
106                .get(&cemetery)
107                .and_then(|s| self.site_templates[s.template].cemetery)
108            else {
109                continue;
110            };
111            let i = cell as usize;
112            self.cells.set_type(i, CellType(2));
113            self.cells.objects[i] = None;
114            let n = self.size as i32;
115            let template = &self.templates[t.tombstone];
116            let (variant, mesh) = template.model.as_ref().map_or((0, 0), |m| {
117                (m.variant.max(0) as usize, m.mesh.max(0) as usize)
118            });
119            let mut yaw = self.placed[cemetery].yaw as i32;
120            while yaw < 0 {
121                yaw += 360;
122            }
123            let id = self.placed.len();
124            self.placed.push(crate::nature::seeding::Placed {
125                template: t.tombstone,
126                bucket: template.bucket,
127                cell: (cell % n, cell / n),
128                footprint: (1, 1),
129                position: self.ground_at(cell),
130                yaw: yaw as f32,
131                variant,
132                mesh,
133                growth: 0.0,
134                growth_state: None,
135                spawn_state: None,
136                alive: true,
137                removal_requested: false,
138                animation: None,
139                scale: [1.0; 4],
140            });
141            self.tombstones.insert(id, life);
142            self.stamp(id);
143            *self.models_dirty = true;
144        }
145    }
146}
147
148/// TombstoneComponent's scheduled update: the time left runs down; at none the tombstone goes (its
149/// cell is plain ground again). Returns whether it went.
150///
151/// Original: `FUN_140058130`.
152pub fn tombstone_visit(
153    tombstones: &mut std::collections::BTreeMap<usize, i64>,
154    cells: &mut crate::nature::seeding::Cells,
155    placed: &mut [crate::nature::seeding::Placed],
156    id: usize,
157    elapsed: i64,
158) -> bool {
159    let Some(left) = tombstones.get_mut(&id) else {
160        return false;
161    };
162    *left -= elapsed;
163    if 0 < *left {
164        return false;
165    }
166    tombstones.remove(&id);
167    let p = &mut placed[id];
168    p.alive = false;
169    let i = (p.cell.1 as usize) * cells.size + p.cell.0 as usize;
170    if cells.objects[i] == Some(id) {
171        cells.objects[i] = None;
172        cells.set_type(i, CellType(2));
173    }
174    true
175}