1use super::*;
4
5impl Needs<'_, '_> {
6 pub fn eat(&mut self, who: &mut Who) -> bool {
10 let cell = self.town.cell_of(who.ch.position);
11 let home = who.state.home;
12 let mut chosen = None;
13 if self.life.f(0xcc) < who.state.hunger {
14 if let Some(h) = home {
16 if 0 < self.home_food(h) {
17 chosen = Some((h, who.ch.position, self.warm_home(h)));
18 }
19 }
20 if chosen.is_none() {
21 if let Some((e, p)) = self.urgent_search(cell, home) {
22 let warm = self.warm_home(e);
23 chosen = Some((e, p, warm));
24 }
25 }
26 }
27 if chosen.is_none() {
28 if let Some(h) = home {
29 if self.town.can_reach(cell, h, self.rng) && 0 < self.home_food(h) {
30 chosen = Some((h, self.point(h), true));
31 }
32 }
33 }
34 if chosen.is_none() {
35 match self.search(0, cell, home) {
36 Some((e, p)) => {
37 let warm = self.warm_home(e);
38 chosen = Some((e, p, warm));
39 }
40 None => {
41 who.ai.cooldowns[0] = ticks(RETRY);
42 who.ai.cooldowns[14] = ticks(RETRY);
43 return false;
44 }
45 }
46 }
47 let (entity, target, warm) = chosen.unwrap();
48 let facing = work_facing(self.centre(entity), target);
50 let mut stack = self.begin(who);
51 if warm {
52 stack.push(who.ch, who.group, citizen_message(0, entity));
53 }
54 stack.push(who.ch, who.group, citizen_message(2, entity));
55 stack.push(
56 who.ch,
57 who.group,
58 Action::PerformWork {
59 facing,
60 anim: 8,
61 duration: ticks(2.0),
62 elapsed: 0,
63 consume: false,
64 },
65 );
66 let walk = MoveTo::new(target, 1, 0, WALKABLE, 0, -1.0, self.rng);
67 stack.push(who.ch, who.group, Action::move_to(walk));
68 who.ch.stack = stack;
69 who.state.job = -1;
70 who.state.health_events |= 4;
71 who.state.supply_activity = 0;
72 true
73 }
74
75 pub fn warm(&mut self, who: &mut Who) -> bool {
80 let cell = self.town.cell_of(who.ch.position);
81 let mut chosen = None;
82 if self.life.f(0xec) < who.state.cold {
83 let (size, terrain) = (self.size, self.terrain);
85 let floors = self.town.floors;
86 let ground_at = move |c: i32| crate::town::floor_ground(floors, size, terrain, c);
87 let mut best = i32::MAX;
88 let homes: Vec<usize> = self
89 .residences
90 .iter()
91 .copied()
92 .filter(|e| self.homes.contains_key(e))
93 .collect();
94 for e in homes {
95 let p = self.point(e);
96 let to = self.town.cell_of(p);
97 let mut g = StorageGeometry {
98 town: self.town,
99 sectors: self.sectors,
100 ground: &ground_at,
101 };
102 let rank = crate::work::stock::StorageSearchMap::rank(&mut g, to, cell);
103 if rank < best
104 && self.town.reachable(WALKABLE, cell, to)
105 && self.warm_home(e)
106 && rank >= 0
107 {
108 best = rank;
109 chosen = Some((e, p));
110 }
111 }
112 }
113 if chosen.is_none() {
114 if let Some(h) = who.state.home.filter(|&h| self.warm_home(h)) {
115 chosen = Some((h, self.point(h)));
116 }
117 }
118 let Some((entity, target)) = chosen else {
119 who.ai.cooldowns[1] = ticks(RETRY);
120 who.ai.cooldowns[15] = ticks(RETRY);
121 return false;
122 };
123 let mut stack = self.begin(who);
125 stack.push(who.ch, who.group, citizen_message(0, entity));
126 stack.push(
127 who.ch,
128 who.group,
129 Action::PerformWork {
130 facing: -1000.0,
131 anim: 8,
132 duration: ticks(4.0),
133 elapsed: 0,
134 consume: false,
135 },
136 );
137 let walk = MoveTo::new(target, 1, 0, WALKABLE, 0, -1.0, self.rng);
138 stack.push(who.ch, who.group, Action::move_to(walk));
139 who.ch.stack = stack;
140 who.state.job = -1;
141 who.state.health_events |= 4;
142 who.state.supply_activity = 0;
143 true
144 }
145
146 pub fn stock_home(&mut self, who: &mut Who, fuel: bool, reachable: bool) -> bool {
153 let (slot, category, icon, supply) = if fuel { (7, 1, 7, 2) } else { (6, 0, 6, 1) };
154 let cell = self.town.cell_of(who.ch.position);
155 let Some(home) = who.state.home.filter(|_| reachable) else {
156 who.ai.cooldowns[slot] = ticks(self.life.f(0xd8) * STOCK_UNREACHABLE);
157 return false;
158 };
159 let Some(home_storage) = self.storage(home) else {
160 return false;
161 };
162 let Some((source, from)) = self.search(category, cell, Some(home)) else {
163 who.ai.cooldowns[slot] = ticks(self.life.f(0xd8) * STOCK_NOTHING);
164 who.state.flags |= 4;
165 if stored(
166 &self.storages[home_storage],
167 self.materials,
168 if fuel { 6 } else { 5 },
169 ) == 0
170 {
171 if let Some(h) = self.homes.get_mut(&home) {
172 crate::people::status::set_icon(&mut h.status, icon, true, false);
173 }
174 }
175 return false;
176 };
177 if let Some(h) = self.homes.get_mut(&home) {
178 crate::people::status::set_icon(&mut h.status, icon, false, false);
179 }
180 let Some(source_storage) = self.storage(source) else {
181 return false;
182 };
183 let target = self.point(home);
184 let walk_home = MoveTo::new(target, 4, 0, WALKABLE, 0, -1.0, self.rng);
187 let walk_source = MoveTo::new(from, 1, 0, WALKABLE, 0, -1.0, self.rng);
188 let free = who.state.carried.capacity - who.state.carried.used(self.materials);
190 let mut room = self.carry.max(free);
191 let mut items: Vec<(usize, i32)> = Vec::new();
192 while let Some(m) =
193 self.storages[source_storage].find_item(self.materials, category, self.rng)
194 {
195 let size = self.materials[m].size;
196 if room < size {
197 break;
198 }
199 self.storages[source_storage].reserve(m, 1);
200 match items.iter_mut().find(|(x, _)| *x == m) {
201 Some((_, n)) => *n += 1,
202 None => items.push((m, 1)),
203 }
204 room -= size;
205 }
206 let face_target = work_facing(self.centre(home), target);
208 let face_source = work_facing(self.centre(source), from);
209 let perform = |facing: f32, anim: usize| Action::PerformWork {
210 facing,
211 anim,
212 duration: ticks(0.75),
213 elapsed: 0,
214 consume: false,
215 };
216 let mut stack = self.begin(who);
217 stack.push(who.ch, who.group, citizen_message(0, home));
218 stack.push(who.ch, who.group, perform(face_target, 7));
219 stack.push(who.ch, who.group, Action::ShowTool(-1));
220 stack.push(
221 who.ch,
222 who.group,
223 Action::DropOff {
224 storage: home_storage,
225 material: usize::MAX,
226 count: -1,
227 redirect: true,
229 },
230 );
231 stack.push(who.ch, who.group, perform(face_target, 6));
232 stack.push(who.ch, who.group, Action::move_to(walk_home));
233 stack.push(who.ch, who.group, perform(face_source, 7));
234 stack.push(who.ch, who.group, Action::ShowTool(-2));
235 stack.push(
236 who.ch,
237 who.group,
238 Action::PickupStoredRawMaterial {
239 storage: source_storage,
240 items,
241 partial: true,
242 },
243 );
244 stack.push(who.ch, who.group, perform(face_source, 6));
245 stack.push(who.ch, who.group, Action::move_to(walk_source));
246 who.ch.stack = stack;
247 who.state.health_events |= 4;
248 who.state.job = -1;
249 who.state.supply_activity = supply;
250 true
251 }
252}