Skip to main content

unbanished_game/work/professions/
settings.rs

1//! Counts, capacity and the player's settings per profession and workplace, and choosing a workplace.
2
3use super::*;
4
5impl Workforce {
6    /// Adults per profession.
7    ///
8    /// Original: `FUN_14007ba70` (counts).
9    pub fn counts(&self, workers: &[Worker], professions: usize) -> Vec<i32> {
10        let mut out = vec![0; professions];
11        for w in workers.iter().filter(|w| w.adult) {
12            if let Some(n) = out.get_mut(w.profession) {
13                *n += 1;
14            }
15        }
16        out
17    }
18
19    /// The most workers of every enabled workplace of a profession.
20    ///
21    /// Original: `FUN_14007ba70` ("of N").
22    pub fn capacity(&self, profession: usize) -> i32 {
23        self.registry
24            .get(&profession)
25            .into_iter()
26            .flatten()
27            .filter_map(|e| self.workplaces.get(e))
28            .filter(|w| w.enabled)
29            .map(|w| w.max())
30            .sum()
31    }
32
33    /// Move the profession's count toward `count`, one citizen at a time.
34    ///
35    /// Original: `FUN_14007b890`.
36    pub fn set_count(
37        &mut self,
38        workers: &mut [Worker],
39        professions: &Professions,
40        profession: usize,
41        count: i32,
42        homes: &BTreeMap<usize, Home>,
43        map: &mut dyn StorageSearchMap,
44        rng: &mut Random,
45    ) {
46        let current = self.counts(workers, professions.list.len())[profession];
47        for _ in current..count {
48            let wp = self.free_workplace(profession, rng);
49            self.hire(
50                workers,
51                professions.laborer,
52                profession,
53                wp,
54                homes,
55                map,
56                rng,
57            );
58        }
59        for _ in count..current {
60            self.fire(workers, professions.laborer, profession);
61        }
62    }
63
64    /// Set the most workers, then the first workers move to another workplace
65    /// ([`Workforce::find_workplace`], or none) until they fit.
66    ///
67    /// Original: `FUN_140134fd0`.
68    pub fn set_max_workers(
69        &mut self,
70        workers: &mut [Worker],
71        entity: usize,
72        max: i32,
73        search: &mut dyn Search,
74    ) {
75        let Some(w) = self.workplaces.get_mut(&entity) else {
76            return;
77        };
78        w.max_workers = max;
79        while max < self.workplaces[&entity].workers.len() as i32 {
80            let c = self.workplaces[&entity].workers[0];
81            let wp = self.find_workplace(c, workers[c].profession, search);
82            self.set_workplace(workers, c, wp);
83        }
84    }
85
86    /// Accept workers or not; a disabled workplace's workers move to another
87    /// ([`Workforce::find_workplace`], or none). Switched off it shows `BuildPaused` while being
88    /// `building` (states 0, 1, 8), else `WorkDisabled`, and neither on; switched off it drops the
89    /// limit, work and workers icons, on without workers of its own profession it shows
90    /// `WorkersUnavailable`.
91    ///
92    /// Original: `FUN_1401350d0`.
93    pub fn set_enabled(
94        &mut self,
95        workers: &mut [Worker],
96        entity: usize,
97        enabled: bool,
98        building: bool,
99        search: &mut dyn Search,
100    ) {
101        let Some(w) = self.workplaces.get_mut(&entity) else {
102            return;
103        };
104        w.enabled = enabled;
105        if !enabled {
106            let bit = if building {
107                icon::BUILD_PAUSED
108            } else {
109                icon::WORK_DISABLED
110            };
111            w.set_icon(bit, true);
112            for bit in [icon::LIMIT, icon::NO_WORK, icon::NO_WORKERS] {
113                w.set_icon(bit, false);
114            }
115        } else {
116            w.set_icon(icon::WORK_DISABLED, false);
117            w.set_icon(icon::BUILD_PAUSED, false);
118            if w.workers.is_empty()
119                && w.profession.is_some()
120                && w.profession == w.description_profession
121            {
122                w.set_icon(icon::NO_WORKERS, true);
123            }
124        }
125        if !enabled {
126            while let Some(&c) = self.workplaces.get(&entity).and_then(|w| w.workers.first()) {
127                let wp = self.find_workplace(c, workers[c].profession, search);
128                self.set_workplace(workers, c, wp);
129            }
130        }
131    }
132
133    /// A workplace of citizen `c`'s profession, enabled, with room and reachable: for a profession
134    /// that works anywhere, the one whose work comes first (by work priority), else the one with
135    /// the most room, the first of equals.
136    ///
137    /// Original: `FUN_140115940`, `FUN_140115490`, `FUN_140115450`; the priority sort is
138    /// `FUN_140115800`.
139    pub fn find_workplace(&self, c: usize, p: usize, search: &mut dyn Search) -> Option<usize> {
140        let list = self.registry.get(&p)?;
141        let anywhere = search.anywhere(p);
142        let cell = search.cell(c);
143        let mut best = None;
144        let mut most = 0;
145        let mut candidates = Vec::new();
146        for &e in list {
147            let Some(w) = self.workplaces.get(&e) else {
148                continue;
149            };
150            let free = w.max() - w.workers.len() as i32;
151            if free <= 0 || !w.enabled || !search.reach(cell, e) {
152                continue;
153            }
154            if anywhere {
155                candidates.push((e, search.priority(e)));
156            } else if most < free {
157                most = free;
158                best = Some(e);
159            }
160        }
161        if anywhere {
162            // `FUN_140030c00`: the CRT qsort by work priority (`FUN_140115800`); the first.
163            crate::behaviour::ai::crt_qsort_less(&mut candidates, |a, b| a.1 < b.1);
164            return candidates.first().map(|c| c.0);
165        }
166        best
167    }
168
169    /// Should citizen `c` look again? A profession that works anywhere when its workplace cannot be
170    /// reached or another with room has work that comes first; the others when the reachable
171    /// workplaces' room differs by more than one.
172    ///
173    /// Original: `FUN_140115eb0`.
174    pub fn should_move(
175        &self,
176        c: usize,
177        p: usize,
178        current: Option<usize>,
179        search: &mut dyn Search,
180    ) -> bool {
181        let list = self.registry.get(&p).cloned().unwrap_or_default();
182        let cell = search.cell(c);
183        if !search.anywhere(p) {
184            let (mut most, mut least) = (0, 0xffff);
185            for e in list {
186                let Some(w) = self.workplaces.get(&e) else {
187                    continue;
188                };
189                if !w.enabled || !search.reach(cell, e) {
190                    continue;
191                }
192                let free = w.max() - w.workers.len() as i32;
193                if most < free {
194                    most = free;
195                }
196                if free < least {
197                    least = free;
198                }
199            }
200            return 1 < most - least;
201        }
202        let Some(current) = current else {
203            return false;
204        };
205        let priority = search.priority(current);
206        if !search.reach(cell, current) {
207            return true;
208        }
209        list.into_iter().any(|e| {
210            e != current
211                && self
212                    .workplaces
213                    .get(&e)
214                    .is_some_and(|w| w.enabled && (w.workers.len() as i32) < w.max())
215                && search.priority(e) < priority
216        })
217    }
218
219    /// Every worker leaves.
220    ///
221    /// Original: `FUN_140134330`.
222    pub fn clear_workers(&mut self, workers: &mut [Worker], entity: usize) {
223        while let Some(&c) = self.workplaces.get(&entity).and_then(|w| w.workers.first()) {
224            self.set_workplace(workers, c, None);
225        }
226    }
227}