Skip to main content

unbanished_game/people/needs/
services.rs

1//! Needs met elsewhere: school, the doctor, herbs and play.
2
3use super::*;
4
5impl Needs<'_, '_> {
6    /// Activity 10: sit a lesson at the school (message 4; its length is a value of the citizen
7    /// description).
8    ///
9    /// Original: `FUN_140112a30`.
10    pub fn school(&mut self, who: &mut Who) -> bool {
11        let Some(school) = who.state.school else {
12            return false;
13        };
14        let cell = self.town.cell_of(who.ch.position);
15        match self.nearest_point(school, cell) {
16            Some(target) => {
17                self.visit(who, target, 4, self.life.f(0x120));
18                true
19            }
20            None => {
21                self.reschool = true;
22                false
23            }
24        }
25    }
26
27    /// Activity 5: with a doctor, be treated (message 5); without one, or when the doctor cannot
28    /// be reached, rest at a home that can be reached. A homeless citizen whose doctor cannot be
29    /// reached still makes the visit, to the centre of the map. Like the original, it reports
30    /// "not started" after a treatment visit.
31    ///
32    /// Original: `FUN_140112c60`.
33    pub fn doctor(&mut self, who: &mut Who, me: usize) -> bool {
34        let cell = self.town.cell_of(who.ch.position);
35        let home = who
36            .state
37            .home
38            .filter(|&h| self.town.can_reach(cell, h, self.rng));
39        let Some(doctor) = who.state.doctor else {
40            return match home {
41                Some(h) => {
42                    self.rest_at_home(who, h);
43                    true
44                }
45                None => false,
46            };
47        };
48        let target = match self.nearest_point(doctor, cell) {
49            Some(target) => target,
50            None => {
51                // `FUN_14011ae80`, then home.
52                who.state.doctor = None;
53                if let Some(a) = self.attendance.get_mut(&doctor) {
54                    a.attendees.retain(|&x| x != me);
55                }
56                if let Some(h) = who.state.home {
57                    self.rest_at_home(who, h);
58                    return true;
59                }
60                // Without a home the original falls through to the visit with the point search's
61                // untouched output, the zeroed vector: the citizen walks to the map's centre.
62                [0.0; 3]
63            }
64        };
65        self.visit(who, target, 5, self.life.f(0x124));
66        false
67    }
68
69    /// Activity 4: take herbs from the nearest storage to the nearest herbalist with a worker, and
70    /// be treated there (5 seconds, message 3). The caller drops off what it carries first.
71    ///
72    /// Original: `FUN_140111d60`.
73    pub fn herbs(&mut self, who: &mut Who) -> bool {
74        who.ai.cooldowns[4] = ticks(75.0);
75        let cell = self.town.cell_of(who.ch.position);
76        let Some((source, from)) = self.search(3, cell, who.state.home) else {
77            return false;
78        };
79        let Some(storage) = self.storage(source) else {
80            return false;
81        };
82        let Some(herb) = self.storages[storage].find_item(self.materials, 3, self.rng) else {
83            return false;
84        };
85        // `FUN_140094700`: the nearest staffed, reachable healer.
86        let mut best = 0xffff;
87        let mut healer = None;
88        for &e in self.healers {
89            if self.workplaces.get(&e).is_none_or(|w| w.workers.is_empty()) {
90                continue;
91            }
92            let p = self.point(e);
93            let to = self.town.cell_of(p);
94            if !self.town.reachable(WALKABLE, cell, to) {
95                continue;
96            }
97            let rank = self.rank(cell, to);
98            if rank < best {
99                best = rank;
100                healer = Some((e, p));
101            }
102        }
103        let Some((healer, at)) = healer else {
104            return false;
105        };
106        // `FUN_1400c1c60`.
107        let face_healer = work_facing(self.centre(healer), at);
108        let face_source = work_facing(self.centre(source), from);
109        self.storages[storage].reserve(herb, 1);
110        let mut stack = self.begin(who);
111        stack.push(who.ch, who.group, citizen_message(3, healer));
112        stack.push(
113            who.ch,
114            who.group,
115            Action::ConsumeStoredRawMaterial {
116                storage: usize::MAX,
117                items: vec![(herb, 1)],
118                reserved: false,
119            },
120        );
121        stack.push(
122            who.ch,
123            who.group,
124            Action::PerformWork {
125                facing: face_healer,
126                anim: 8,
127                duration: ticks(5.0),
128                elapsed: 0,
129                consume: false,
130            },
131        );
132        let walk = MoveTo::new(at, 1, 0, WALKABLE, 0, -1.0, self.rng);
133        stack.push(who.ch, who.group, Action::move_to(walk));
134        stack.push(
135            who.ch,
136            who.group,
137            Action::PickupStoredRawMaterial {
138                storage,
139                items: vec![(herb, 1)],
140                partial: true,
141            },
142        );
143        stack.push(
144            who.ch,
145            who.group,
146            Action::PerformWork {
147                facing: face_source,
148                anim: 8,
149                duration: ticks(1.0),
150                elapsed: 0,
151                consume: false,
152            },
153        );
154        let walk = MoveTo::new(from, 1, 0, WALKABLE, 0, -1.0, self.rng);
155        stack.push(who.ch, who.group, Action::move_to(walk));
156        who.ch.stack = stack;
157        who.state.health_events |= 4;
158        who.state.job = -1;
159        who.state.supply_activity = 0;
160        who.ai.cooldowns[4] = ticks(self.life.f(0x78) * 75.0);
161        true
162    }
163
164    /// Activity 11: a child plays next to its guardian (or the guardian's partner), then waits
165    /// `RandFloat ยท 7 + 3` seconds.
166    ///
167    /// Original: `FUN_14010f780`.
168    pub fn play(&mut self, who: &mut Who, me: usize) -> bool {
169        let Some(mut g) = self.citizens.get(me).and_then(|c| c.parent) else {
170            return false;
171        };
172        if let Some(p) = self.citizens[g].spouse {
173            // `Rand(1)` is always 0: the partner.
174            if self.rng.rand_below(1) == 0 {
175                g = p;
176            }
177        }
178        let from = self.town.cell_of(self.citizens[g].position);
179        let to = self.town.cell_of(who.ch.position);
180        let Some(cell) =
181            crate::pathfind::nearest_neighbour(self.size, self.town.types, from, to, WALKABLE)
182        else {
183            return false;
184        };
185        let target = crate::town::floor_ground(self.town.floors, self.size, self.terrain, cell);
186        let idle = self.rng.rand_float() * 7.0 + 3.0;
187        // `FUN_1400c7b90`.
188        let mut stack = self.begin(who);
189        if 0.0 < idle {
190            stack.push(
191                who.ch,
192                who.group,
193                Action::PlayAnimation {
194                    anim: 0,
195                    duration: ticks(idle),
196                    elapsed: 0,
197                },
198            );
199        }
200        let walk = MoveTo::new(target, 1, 0, WALKABLE, 0, -1.0, self.rng);
201        stack.push(who.ch, who.group, Action::move_to(walk));
202        who.ch.stack = stack;
203        true
204    }
205}