unbanished_game/work/jobs/
offers.rs1use super::*;
4
5impl Jobs {
6 pub(super) fn try_one(
10 &mut self,
11 id: i32,
12 mut work: Work,
13 citizen: usize,
14 host: &mut impl WorkHost,
15 ) -> u8 {
16 let mut r = host.try_assign(&mut work, citizen, self.time);
17 self.works.insert(id, work);
18 while r == NESTED {
19 let again = host.offers_again();
20 for other in host.take_offers() {
21 if self.offer_one(other, citizen, again, host) {
22 return 2;
23 }
24 }
25 let Some(mut work) = self.works.remove(&id) else {
26 return 1;
27 };
28 r = host.resume(&mut work, citizen);
29 self.works.insert(id, work);
30 }
31 r
32 }
33
34 pub fn offer_one(
38 &mut self,
39 id: i32,
40 citizen: usize,
41 again: bool,
42 host: &mut impl WorkHost,
43 ) -> bool {
44 let Some(mut work) = self.works.remove(&id) else {
45 return false;
46 };
47 let mut taken = false;
48 if work.failed_pass != self.pass {
49 host.refresh(&mut work);
50 match self.try_one(id, work, citizen, host) {
51 2 => taken = true,
52 1 => {
53 if again {
54 if let Some(mut work) = self.works.remove(&id) {
55 host.refresh(&mut work);
56 taken = host.try_assign_again(&mut work, citizen, self.time);
57 self.works.insert(id, work);
58 }
59 }
60 if !taken {
61 if let Some(w) = self.works.get_mut(&id) {
62 w.failed_pass = self.pass;
63 }
64 }
65 }
66 _ => {}
67 }
68 } else {
69 self.works.insert(id, work);
70 }
71 self.drain(host);
72 taken
73 }
74
75 pub fn assign_work(&mut self, citizen: usize, host: &mut impl WorkHost) {
79 if self.works.is_empty() {
80 return;
81 }
82 let laborer = host.laborer();
83 let profession = host.profession(citizen);
84 if profession != laborer {
85 if let Some(set) = self.professions.get(&profession).cloned() {
86 if self.try_list(citizen, &set, profession, false, false, host) {
87 return;
88 }
89 }
90 }
91 if !self.assign_emergency(citizen, host) {
92 if let Some(set) = self.professions.get(&laborer).cloned() {
93 self.try_list(citizen, &set, laborer, false, false, host);
94 }
95 }
96 }
97
98 pub fn assign_emergency(&mut self, citizen: usize, host: &mut impl WorkHost) -> bool {
103 let mut lists: Vec<&[u32]> = Vec::new();
104 let emergency = host.has_emergency(citizen);
105 if emergency {
106 lists.push(&[EMERGENCY]);
107 } else {
108 if host.profession(citizen) == host.builder() {
109 lists.push(&[CONSTRUCTION]);
110 }
111 for (slot, categories) in EMERGENCY_LISTS {
112 let (count, limit) = host.stock(slot);
113 if count <= limit / 10 {
114 lists.push(categories);
115 }
116 }
117 }
118 let k = host.rng().rand_below(lists.len() as u32) as usize;
119 let Some(list) = lists.get(k).copied() else {
120 return false;
121 };
122 let profession = host.profession(citizen);
123 for &c in list {
124 let set = self.categories[c as usize - 1].clone();
125 if self.try_list(citizen, &set, profession, true, emergency, host) {
126 return true;
127 }
128 }
129 false
130 }
131
132 pub(super) fn try_list(
135 &mut self,
136 citizen: usize,
137 set: &BTreeSet<i32>,
138 profession: usize,
139 by_distance: bool,
140 emergency: bool,
141 host: &mut impl WorkHost,
142 ) -> bool {
143 let workplace = if emergency {
144 None
145 } else {
146 host.workplace(citizen)
147 };
148 if let Some(wp) = workplace {
149 if profession == host.profession(citizen) {
150 let order = self.sort(citizen, set, Some(wp), by_distance, host);
151 if self.offer(citizen, &order, host) {
152 return true;
153 }
154 }
155 }
156 if host.works_anywhere(profession) || emergency {
157 let order = self.sort(citizen, set, None, by_distance, host);
158 if self.offer(citizen, &order, host) {
159 return true;
160 }
161 }
162 false
163 }
164
165 pub(super) fn sort(
168 &self,
169 citizen: usize,
170 set: &BTreeSet<i32>,
171 owner: Option<usize>,
172 by_distance: bool,
173 host: &mut impl WorkHost,
174 ) -> Vec<i32> {
175 let from = host.cell(citizen);
176 let mut keyed: Vec<(i32, i32)> = set
177 .iter()
178 .filter_map(|id| self.works.get(id))
179 .filter(|w| owner.is_none_or(|o| w.entity == o))
180 .map(|w| {
181 let key = if by_distance {
182 host.rank(from, w.cell)
183 } else {
184 w.priority
185 };
186 (key, w.id)
187 })
188 .collect();
189 keyed.sort();
190 keyed.dedup();
191 keyed.into_iter().map(|(_, id)| id).collect()
192 }
193
194 pub(super) fn offer(
196 &mut self,
197 citizen: usize,
198 order: &[i32],
199 host: &mut impl WorkHost,
200 ) -> bool {
201 for &id in order {
202 let Some(mut work) = self.works.remove(&id) else {
203 continue;
204 };
205 let mut taken = false;
206 if work.failed_pass != self.pass {
207 host.refresh(&mut work);
208 match self.try_one(id, work, citizen, host) {
209 2 => taken = true,
210 1 => {
211 if let Some(w) = self.works.get_mut(&id) {
212 w.failed_pass = self.pass;
213 }
214 }
215 _ => {}
216 }
217 } else {
218 self.works.insert(id, work);
219 }
220 self.drain(host);
221 if taken {
222 return true;
223 }
224 }
225 false
226 }
227}