Skip to main content

unbanished_game/people/life/
services.rs

1//! Workplaces and services: looking for a workplace, choosing and leaving a service (school, chapel, healer, tavern), lessons and treatment.
2
3use super::*;
4
5impl People<'_, '_> {
6    /// `FUN_140118340`'s workplace search: without a workplace, or when `FUN_140115eb0` says
7    /// to look again, `FUN_140115450` chooses one (or none) and `FUN_1401173b0` moves there.
8    pub(super) fn search_workplace(&mut self, c: usize) {
9        let town = self.town;
10        let citizens = &*self.citizens;
11        let cell = |i: usize| town.cell_of(citizens[i].position);
12        let professions = self.professions;
13        let anywhere = |p: usize| professions.works_anywhere(p);
14        let sites = self.sites;
15        let priority = |e: usize| {
16            sites
17                .get(&e)
18                .and_then(|s| s.work_priority)
19                .unwrap_or(i32::MAX)
20        };
21        let mut search = crate::behaviour::activities::TownSearch {
22            town,
23            cell: &cell,
24            anywhere: &anywhere,
25            priority: &priority,
26            rng: &mut *self.rng,
27        };
28        let (p, current) = (self.states[c].profession, self.states[c].workplace);
29        if current.is_none() || self.workforce.should_move(c, p, current, &mut search) {
30            let wp = self.workforce.find_workplace(c, p, &mut search);
31            if wp != current {
32                self.workforce.move_worker(c, current, wp);
33                self.states[c].workplace = wp;
34            }
35        }
36    }
37
38    /// The path-rank distance (`FUN_140008830`).
39    pub(super) fn rank(&mut self, a: i32, b: i32) -> i32 {
40        let (size, terrain, floors) = (self.town.size, self.terrain, self.town.floors);
41        let ground = move |cell: i32| crate::town::floor_ground(floors, size, terrain, cell);
42        let mut g = crate::behaviour::activities::StorageGeometry {
43            town: self.town,
44            sectors: self.sectors,
45            ground: &ground,
46        };
47        crate::work::stock::StorageSearchMap::rank(&mut g, a, b)
48    }
49
50    /// `FUN_140044a50`: room, a worker, and the building reachable from the citizen.
51    pub(super) fn accepts(&self, e: usize, c: usize) -> bool {
52        let Some(a) = self.attendance.get(&e) else {
53            return false;
54        };
55        if a.attendees.len() as i32 >= a.capacity {
56            return false;
57        }
58        if self
59            .workforce
60            .workplaces
61            .get(&e)
62            .is_none_or(|w| w.workers.is_empty())
63        {
64            return false;
65        }
66        let to = self.town.point_cell(e);
67        to >= 0 && self.town.reachable(WALKABLE, self.cell(c), to)
68    }
69
70    /// `FUN_140116e00`: the accepting attendance of `service` nearest the home (path rank
71    /// between entrances; any one without a home), skipping `exclude`.
72    pub(super) fn find_service(
73        &mut self,
74        c: usize,
75        service: usize,
76        exclude: Option<usize>,
77    ) -> Option<usize> {
78        let profession = self.rules.services[service]?;
79        let home = self.states[c]
80            .home
81            .map_or(-1, |h| self.town.entrance(h, self.rng));
82        let mut best = -1;
83        let mut found = None;
84        for &e in self.attendance_list {
85            // The workplace's description profession (`FUN_140116e00` compares `+0x48`).
86            if Some(e) == exclude
87                || self.workforce.workplaces.get(&e).and_then(|w| w.profession) != Some(profession)
88                || !self.accepts(e, c)
89            {
90                continue;
91            }
92            let entrance = self.town.entrance(e, self.rng);
93            let d = if home >= 0 {
94                self.rank(entrance, home)
95            } else {
96                1
97            };
98            if best < 0 || d < best {
99                best = d;
100                found = Some(e);
101            }
102        }
103        found
104    }
105
106    // +0x220
107    /// Join the nearest school, chapel or doctor; the school also sets.
108    ///
109    /// Original: `FUN_140116d10`, `FUN_140116bc0`, `FUN_140116a60`.
110    pub fn join_service(&mut self, c: usize, service: usize, exclude: Option<usize>) -> bool {
111        let Some(e) = self.find_service(c, service, exclude) else {
112            return false;
113        };
114        if let Some(a) = self.attendance.get_mut(&e) {
115            a.attendees.push(c);
116        }
117        let s = &mut self.states[c];
118        match service {
119            service::SCHOOL => {
120                s.school = Some(e);
121                s.schooled = true;
122            }
123            service::CHAPEL => s.priest = Some(e),
124            _ => s.doctor = Some(e),
125        }
126        true
127    }
128
129    /// Leave; the school can be swapped for another at once (`again`).
130    ///
131    /// Original: `FUN_14011af20`, `FUN_140116b20`, `FUN_14011ae80`.
132    pub fn leave_service(&mut self, c: usize, service: usize, again: bool) {
133        let slot = match service {
134            service::SCHOOL => self.states[c].school.take(),
135            service::CHAPEL => self.states[c].priest.take(),
136            _ => self.states[c].doctor.take(),
137        };
138        let Some(e) = slot else { return };
139        if let Some(a) = self.attendance.get_mut(&e) {
140            a.attendees.retain(|&x| x != c);
141        }
142        if again && service == service::SCHOOL && !self.join_service(c, service, None) {
143            self.states[c].school = None;
144        }
145    }
146
147    /// Citizen message 4: a lesson; at `D[0x118]` lessons, or after `D[0x11c]` hours at school,
148    /// educated (flag `0x400`), out of school and an adult (9).
149    ///
150    /// Original: `FUN_140116c70`.
151    pub fn lesson(&mut self, c: usize) {
152        let r = self.rules;
153        let s = &mut self.states[c];
154        self.citizens[c].education += 1;
155        // jbe at 0x140116cb8: a NaN time at school is not enough.
156        #[allow(clippy::neg_cmp_op_on_partial_ord)]
157        let short = !(r.f(0x11c) * HOUR < seconds(s.school_time));
158        if self.citizens[c].education < r.i(0x118) && short {
159            return;
160        }
161        s.flags |= 0x400;
162        self.citizens[c].education = r.i(0x118);
163        self.citizens[c].educated = true;
164        self.leave_service(c, service::SCHOOL, false);
165        self.report(c, reason::ADULT, 0, None);
166    }
167
168    /// Citizen message 5: the doctor shortens the illness by `D[0x12c]` minutes.
169    ///
170    /// Original: `FUN_140116a10`.
171    pub fn treated(&mut self, c: usize) {
172        if self.states[c].disease.is_some() {
173            self.states[c].disease_left -= ticks(self.rules.f(0x12c) * MINUTE);
174        }
175    }
176}