Skip to main content

unbanished_game/people/life/
update.rs

1//! A citizen's update: ageing, needs, health and the events they raise.
2
3use super::*;
4
5impl People<'_, '_> {
6    /// One scheduled update of citizen `c` over `dt`. Not ported: the debug flags that turn
7    /// hunger and cold off and make every adult educated.
8    ///
9    /// Original: `FUN_140118340`.
10    // Game +0xd0 bits 1, 2, 9
11    pub fn update(&mut self, c: usize, dt: i64) {
12        let r = self.rules;
13        let secs = seconds(dt);
14        let adult_age = r.adult_age();
15        // jb at 0x14011839e: a NaN age counts as a child.
16        #[allow(clippy::neg_cmp_op_on_partial_ord)]
17        let was_child = !(adult_age <= self.citizens[c].age) || self.states[c].school.is_some();
18        let age = secs * r.f(0x40) + self.citizens[c].age;
19        self.citizens[c].age = age;
20        {
21            let s = &mut self.states[c];
22            s.health_timer += dt;
23            s.happiness_timer += dt;
24            s.clothing_remaining -= dt;
25            if s.school.is_some() {
26                s.school_time += dt;
27            }
28            s.disease_clock += dt;
29            s.accident_timer += dt;
30            if s.clothing_remaining < 0 {
31                s.clothing = 0;
32                s.clothing_remaining = 0;
33            }
34        }
35        // Coming of age: a school place (code 10) or adulthood (code 9).
36        if was_child && adult_age <= age && self.states[c].school.is_none() {
37            let student = !self.states[c].schooled && self.join_service(c, service::SCHOOL, None);
38            self.report(
39                c,
40                if student {
41                    reason::STUDENT
42                } else {
43                    reason::ADULT
44                },
45                0,
46                None,
47            );
48        }
49        let s = &mut self.states[c];
50        if s.sick && s.doctor.is_some() {
51            s.hunger = 0.0;
52            s.cold = 0.0;
53        }
54        s.hunger += secs * r.f(0xc0) / HOUR;
55        // jbe at 0x14011852b: warm enough, or a NaN temperature, clears the cold.
56        #[allow(clippy::neg_cmp_op_on_partial_ord)]
57        if !(self.temperature < r.f(0xf4)) {
58            s.cold = 0.0;
59        } else {
60            let rate = r.f(0xdc + 4 * s.clothing.clamp(0, 2) as u32);
61            s.cold += secs * rate / MINUTE;
62        }
63        // An accident at work (`+0x1f8`, `FUN_14011bb20`): code 7 with the profession's
64        // death message.
65        if let Some(p) = self.states[c].accident {
66            let extra = self
67                .professions
68                .list
69                .get(p)
70                .map(|p| (p.string_table.clone(), p.death.clone()));
71            if self.die(c, reason::ACCIDENT, extra) {
72                return;
73            }
74        }
75        let s = &mut self.states[c];
76        if r.f(0xd0) <= s.hunger {
77            s.hunger = r.f(0xd0);
78            if self.idle[c] && self.die(c, reason::STARVED, None) {
79                return;
80            }
81        }
82        let s = &mut self.states[c];
83        if r.f(0xf0) <= s.cold {
84            s.cold = r.f(0xf0);
85            if self.idle[c] && self.die(c, reason::FROZE, None) {
86                return;
87            }
88        }
89        if self.citizens[c].lifespan <= self.citizens[c].age && self.die(c, reason::OLD_AGE, None) {
90            return;
91        }
92        if !self.disease(c, dt) {
93            let extra = self.states[c]
94                .disease
95                .and_then(|d| self.diseases.get(d))
96                .map(|d| (d.table.clone(), d.name.clone()));
97            if self.die(c, reason::DISEASE, extra) {
98                return;
99            }
100        }
101        if self.states[c].disease.is_some() {
102            self.states[c].flags |= 1;
103            if self.states[c].doctor.is_none() {
104                self.join_service(c, service::DOCTOR, None);
105            }
106        }
107        if self.states[c].priest.is_none() {
108            self.join_service(c, service::CHAPEL, None);
109        }
110        let warn_hunger = r.hunger_warning();
111        let warn_cold = r.cold_warning();
112        {
113            let s = &mut self.states[c];
114            crate::people::status::set_icon(&mut s.status, 0, s.home.is_none(), false);
115            crate::people::status::set_icon(&mut s.status, 1, warn_hunger < s.hunger, false);
116            crate::people::status::set_icon(&mut s.status, 2, warn_cold < s.cold, false);
117            // Icon 3: no workplace, for a profession that does not work anywhere
118            // (profession `+0x4c` clear, citizen `+0x248` unset).
119            let unplaced = !self.professions.works_anywhere(s.profession) && s.workplace.is_none();
120            crate::people::status::set_icon(&mut s.status, 3, unplaced, false);
121            crate::people::status::set_icon(&mut s.status, 5, s.disease.is_some(), false);
122            if warn_hunger < s.hunger {
123                s.health_events |= 1;
124                s.flags |= 1;
125            }
126            if warn_cold < s.cold {
127                s.health_events |= 2;
128                s.flags |= 1;
129            }
130        }
131        let interval = ticks(r.f(0x78) * MINUTE);
132        while interval > 0 && interval < self.states[c].health_timer {
133            self.states[c].health_timer -= interval;
134            self.health(c);
135        }
136        let interval = ticks(r.f(0xa0) * MINUTE);
137        while interval > 0 && interval < self.states[c].happiness_timer {
138            self.states[c].happiness_timer -= interval;
139            self.happiness(c);
140        }
141        // A home that cannot be reached is given up.
142        if self.idle[c] {
143            if let Some(home) = self.states[c].home {
144                if !self.reaches(c, home) {
145                    self.set_home(c, None, -1);
146                }
147            }
148        }
149        // jb at 0x1401189a8 and 0x140118a71: a NaN age counts as a child.
150        #[allow(clippy::neg_cmp_op_on_partial_ord)]
151        let child = !(adult_age <= self.citizens[c].age) || self.states[c].school.is_some();
152        // A child without a home follows its guardian's.
153        if child && self.idle[c] && self.states[c].home.is_none() {
154            if let Some(g) = self.citizens[c].parent {
155                if let Some(home) = self.states[g].home {
156                    if self.reaches(c, home) {
157                        let slot = self.states[g].home_slot;
158                        self.set_home(c, Some(home), slot);
159                    }
160                }
161            }
162        }
163        if !child && self.idle[c] {
164            self.search_workplace(c);
165            if self.states[c].home.is_none() || self.citizens[c].parent.is_some() {
166                if self.states[c].home.is_none() {
167                    self.states[c].flags |= 0x20;
168                }
169                let homeless = self.states[c].home.is_none();
170                if let Some((home, slot)) = self.find_home(c, homeless) {
171                    if self.citizens[c].parent.is_some() {
172                        self.states[c].flags |= 0x2000;
173                    }
174                    self.set_guardian(c, None);
175                    self.set_home(c, Some(home), slot);
176                    if let Some(p) = self.citizens[c].spouse {
177                        self.set_home(p, Some(home), slot);
178                    }
179                }
180            }
181            // A family in a shared residence looks for a house of its own.
182            if let Some(home) = self.states[c].home {
183                if self.homes.get(&home).is_some_and(|h| h.families.len() > 1) {
184                    if let Some((home, slot)) = self.find_home(c, false) {
185                        self.set_home(c, Some(home), slot);
186                        if let Some(p) = self.citizens[c].spouse {
187                            self.set_home(p, Some(home), slot);
188                        }
189                    }
190                }
191            }
192            // Marriage.
193            if self.citizens[c].parent.is_none()
194                && self.states[c].home.is_some()
195                && self.citizens[c].spouse.is_none()
196                && r.f(0x48) <= self.citizens[c].age
197            {
198                if let Some(p) = self.find_partner(c) {
199                    self.citizens[c].spouse = Some(p);
200                    self.states[c].flags |= 0x200;
201                    self.states[p].flags |= 0x200;
202                    self.set_guardian(p, None);
203                    self.citizens[p].spouse = Some(c);
204                    let (home, slot) = (self.states[c].home, self.states[c].home_slot);
205                    self.set_home(p, home, slot);
206                }
207            }
208        }
209        self.pregnancy(c, secs);
210    }
211}