src/jobs/tests.rs 36.8 K · 1147 lines · raw · history

1 use std::collections::VecDeque;
2
3 use super::*;
4 use crate::{
5 animals::{Animal, AnimalKind},
6 colonist::follow_path,
7 economy::COLONY_RADIUS,
8 food::Hunger,
9 items::STACK_LIMIT,
10 sim::{SimPos, sim_pos},
11 world::Terrain,
12 zones::NEW_STOCKPILE_SIZE,
13 };
14
15 const TICK: Duration = Duration::from_nanos(1_000_000_000 / 60);
16
17 fn test_app(map: WorldMap) -> App {
18 let mut app = App::new();
19 app.insert_resource(map)
20 .insert_resource(SimRng::new(1))
21 .init_resource::<Time>()
22 .init_resource::<Reservations>()
23 .init_resource::<ColonyGoals>()
24 .init_resource::<ItemAssets>()
25 .init_resource::<StructureAssets>()
26 .init_resource::<Blueprints>()
27 .init_resource::<LightMap>()
28 .init_resource::<SimTick>()
29 .init_resource::<Zones>()
30 .add_message::<Order>()
31 .add_message::<TileChanged>()
32 .add_systems(
33 Update,
34 (
35 handle_orders,
36 choose_jobs,
37 run_jobs,
38 follow_path,
39 crate::needs::update_rest,
40 )
41 .chain(),
42 );
43 app
44 }
45
46 fn spawn_colonist(app: &mut App, tile: UVec2) -> Entity {
47 app.world_mut()
48 .spawn((
49 Colonist {
50 name: "Test".into(),
51 },
52 TilePos(tile),
53 sim_pos(WorldMap::tile_center(tile)),
54 ))
55 .id()
56 }
57
58 fn run_ticks(app: &mut App, ticks: u32) {
59 for _ in 0..ticks {
60 app.world_mut().resource_mut::<Time>().advance_by(TICK);
61 app.update();
62 }
63 }
64
65 fn map_with_trees(trees: &[UVec2]) -> WorldMap {
66 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
67 for &t in trees {
68 map.set_plant(t, Some(Plant::Tree));
69 }
70 map
71 }
72
73 fn wood_on_map(app: &mut App) -> u32 {
74 let world = app.world_mut();
75 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
76 stock(items.iter(), ItemKind::Wood)
77 }
78
79 fn items_at(app: &mut App) -> Vec<(UVec2, u32)> {
80 let world = app.world_mut();
81 let mut items: Vec<(UVec2, u32)> = world
82 .query::<(&Item, &TilePos)>()
83 .iter(world)
84 .map(|(item, pos)| (pos.0, item.count))
85 .collect();
86 items.sort_by_key(|(p, _)| (p.y, p.x));
87 items
88 }
89
90 fn put_wood(app: &mut App, tile: UVec2, count: u32) {
91 app.world_mut()
92 .spawn((Item::new(ItemKind::Wood, count), TilePos(tile)));
93 }
94
95 fn set_goals(app: &mut App, goals: &[(ItemKind, u32)]) {
96 *app.world_mut().resource_mut::<ColonyGoals>() = ColonyGoals::fixed(goals);
97 }
98
99 fn stockpile_size(app: &App) -> usize {
100 let map = app.world().resource::<WorldMap>();
101 let zones = app.world().resource::<Zones>();
102 map.positions().filter(|&p| zones.is_stockpile(p)).count()
103 }
104
105 fn stored(app: &mut App) -> u32 {
106 let zones = app.world().resource::<Zones>().clone();
107 items_at(app)
108 .iter()
109 .filter(|(p, _)| zones.is_stockpile(*p))
110 .map(|(_, c)| c)
111 .sum()
112 }
113
114 fn put_beds(app: &mut App, count: u32) {
115 for x in 0..count {
116 app.world_mut().spawn((
117 Structure {
118 kind: StructureKind::Bed,
119 },
120 TilePos(UVec2::new(19 - x, 19)),
121 ));
122 }
123 }
124
125 fn hauling_app(stockpile: (IVec2, IVec2)) -> App {
126 let mut app = test_app(map_with_trees(&[]));
127 put_beds(&mut app, 2);
128 set_goals(&mut app, &[]);
129 let map = app.world().resource::<WorldMap>().clone();
130 app.world_mut()
131 .resource_mut::<Zones>()
132 .add_stockpile(&map, stockpile.0, stockpile.1);
133 app
134 }
135
136 fn carrying(app: &App, colonist: Entity) -> Option<Item> {
137 app.world().get::<Carrying>(colonist).unwrap().0
138 }
139
140 fn job(app: &App, colonist: Entity) -> Option<Job> {
141 app.world().get::<CurrentJob>(colonist).unwrap().0
142 }
143
144 #[test]
145 fn idle_colonists_cut_trees_when_the_colony_needs_wood() {
146 let tree = UVec2::new(8, 5);
147 let mut app = test_app(map_with_trees(&[tree]));
148 spawn_colonist(&mut app, UVec2::new(2, 5));
149 run_ticks(&mut app, 60 * 15);
150 assert_eq!(
151 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
152 None
153 );
154 assert_eq!(wood_on_map(&mut app), 10);
155 assert!(app.world().resource::<Reservations>().0.is_empty());
156 }
157
158 #[test]
159 fn colonists_leave_trees_alone_when_there_is_enough_wood() {
160 let tree = UVec2::new(8, 5);
161 let mut app = test_app(map_with_trees(&[tree]));
162 set_goals(&mut app, &[]);
163 spawn_colonist(&mut app, UVec2::new(2, 5));
164 run_ticks(&mut app, 60 * 15);
165 assert_eq!(
166 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
167 Some(Plant::Tree)
168 );
169 }
170
171 #[test]
172 fn colonists_never_pick_the_same_tree() {
173 let trees = [UVec2::new(10, 10), UVec2::new(10, 11)];
174 let mut app = test_app(map_with_trees(&trees));
175 let a = spawn_colonist(&mut app, UVec2::new(5, 10));
176 let b = spawn_colonist(&mut app, UVec2::new(5, 11));
177 run_ticks(&mut app, 61);
178 let target = |c| match job(&app, c) {
179 Some(Job {
180 task: Task::Work { site, .. },
181 ..
182 }) => site,
183 other => panic!("expected chop job, got {other:?}"),
184 };
185 assert_ne!(target(a), target(b));
186 }
187
188 #[test]
189 fn ordered_chop_ignores_the_wood_goal() {
190 let tree = UVec2::new(8, 5);
191 let mut app = test_app(map_with_trees(&[tree]));
192 set_goals(&mut app, &[]);
193 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
194 app.world_mut().write_message(Order {
195 colonist,
196 tile: tree,
197 });
198 run_ticks(&mut app, 60 * 15);
199 assert_eq!(
200 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
201 None
202 );
203 }
204
205 #[test]
206 fn ordering_a_move_cancels_the_current_job_and_frees_its_tree() {
207 let tree = UVec2::new(15, 5);
208 let mut app = test_app(map_with_trees(&[tree]));
209 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
210 run_ticks(&mut app, 61);
211 assert!(matches!(
212 job(&app, colonist),
213 Some(Job {
214 task: Task::Work { .. },
215 ..
216 })
217 ));
218 app.world_mut().write_message(Order {
219 colonist,
220 tile: UVec2::new(2, 15),
221 });
222 run_ticks(&mut app, 1);
223 assert_eq!(
224 job(&app, colonist).map(|j| j.task),
225 Some(Task::MoveTo(UVec2::new(2, 15)))
226 );
227 assert!(
228 app.world()
229 .resource::<Reservations>()
230 .is_free(Target::Site(tree))
231 );
232 }
233
234 #[test]
235 fn idle_colonists_haul_loose_items_into_the_stockpile() {
236 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
237 put_wood(&mut app, UVec2::new(5, 5), 20);
238 spawn_colonist(&mut app, UVec2::new(2, 5));
239 run_ticks(&mut app, 60 * 15);
240 assert_eq!(items_at(&mut app), vec![(UVec2::new(15, 15), 20)]);
241 }
242
243 #[test]
244 fn large_stacks_take_several_trips_and_spill_into_more_stockpile_tiles() {
245 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
246 put_wood(&mut app, UVec2::new(5, 5), STACK_LIMIT + 10);
247 spawn_colonist(&mut app, UVec2::new(2, 5));
248 run_ticks(&mut app, 60 * 40);
249 let stacks = items_at(&mut app);
250 assert_eq!(stored(&mut app), STACK_LIMIT + 10);
251 assert!(stacks.len() >= 2);
252 assert!(stacks.iter().all(|(_, count)| *count <= STACK_LIMIT));
253 }
254
255 #[test]
256 fn items_already_in_a_stockpile_stay_put() {
257 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(16, 16)));
258 put_wood(&mut app, UVec2::new(5, 5), 20);
259 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
260 run_ticks(&mut app, 60 * 5);
261 assert_eq!(items_at(&mut app), vec![(UVec2::new(5, 5), 20)]);
262 assert_eq!(carrying(&app, colonist), None);
263 }
264
265 #[test]
266 fn colonists_found_a_stockpile_when_items_need_storing() {
267 let mut app = test_app(map_with_trees(&[]));
268 put_beds(&mut app, 1);
269 set_goals(&mut app, &[]);
270 put_wood(&mut app, UVec2::new(5, 5), 20);
271 spawn_colonist(&mut app, UVec2::new(2, 5));
272 run_ticks(&mut app, 60 * 15);
273 assert_eq!(
274 stockpile_size(&app),
275 (NEW_STOCKPILE_SIZE * NEW_STOCKPILE_SIZE) as usize
276 );
277 assert_eq!(stored(&mut app), 20);
278 }
279
280 #[test]
281 fn no_stockpile_is_founded_while_there_is_nothing_to_store() {
282 let mut app = test_app(map_with_trees(&[]));
283 set_goals(&mut app, &[]);
284 spawn_colonist(&mut app, UVec2::new(2, 5));
285 run_ticks(&mut app, 60 * 5);
286 assert_eq!(stockpile_size(&app), 0);
287 }
288
289 #[test]
290 fn two_colonists_never_haul_the_same_stack() {
291 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(16, 16)));
292 put_wood(&mut app, UVec2::new(5, 5), 10);
293 put_wood(&mut app, UVec2::new(5, 6), 10);
294 let a = spawn_colonist(&mut app, UVec2::new(2, 5));
295 let b = spawn_colonist(&mut app, UVec2::new(2, 6));
296 run_ticks(&mut app, 61);
297 let pickup = |c| match job(&app, c).map(|j| j.task) {
298 Some(Task::Haul { pickup, .. }) => pickup,
299 other => panic!("expected haul, got {other:?}"),
300 };
301 assert_ne!(pickup(a), pickup(b));
302 }
303
304 // Orders interrupt whatever a colonist is doing, but whatever it is carrying stays in its
305 // hands; once it is idle again it finishes delivering it instead of dropping it anywhere.
306 #[test]
307 fn interrupted_haulers_deliver_their_load_later() {
308 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
309 put_wood(&mut app, UVec2::new(4, 5), 20);
310 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
311 let mut ticks = 0;
312 while carrying(&app, colonist).is_none() && ticks < 60 * 10 {
313 run_ticks(&mut app, 1);
314 ticks += 1;
315 }
316 app.world_mut().write_message(Order {
317 colonist,
318 tile: UVec2::new(2, 2),
319 });
320 run_ticks(&mut app, 60 * 3);
321 assert!(carrying(&app, colonist).is_some());
322 run_ticks(&mut app, 60 * 20);
323 assert_eq!(carrying(&app, colonist), None);
324 assert_eq!(stored(&mut app), 20);
325 }
326
327 #[test]
328 fn right_clicking_loose_items_orders_a_haul() {
329 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(15, 15)));
330 set_goals(&mut app, &[(ItemKind::Wood, 1000)]);
331 put_wood(&mut app, UVec2::new(5, 5), 20);
332 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
333 app.world_mut().write_message(Order {
334 colonist,
335 tile: UVec2::new(5, 5),
336 });
337 run_ticks(&mut app, 1);
338 assert!(matches!(
339 job(&app, colonist),
340 Some(Job {
341 task: Task::Haul { .. },
342 ordered: true
343 })
344 ));
345 }
346
347 fn mining_map() -> WorldMap {
348 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
349 for y in 0..20 {
350 map.set(UVec2::new(10, y), Terrain::Stone);
351 }
352 map.set(UVec2::new(10, 5), Terrain::IronOre);
353 map
354 }
355
356 fn items_of(app: &mut App, kind: ItemKind) -> u32 {
357 let world = app.world_mut();
358 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
359 stock(items.iter(), kind)
360 }
361
362 #[test]
363 fn colonists_mine_rock_the_colony_needs_and_leave_gravel() {
364 let mut app = test_app(mining_map());
365 set_goals(&mut app, &[(ItemKind::Stone, 10)]);
366 spawn_colonist(&mut app, UVec2::new(8, 12));
367 run_ticks(&mut app, 60 * 12);
368 assert_eq!(items_of(&mut app, ItemKind::Stone), 10);
369 assert_eq!(items_of(&mut app, ItemKind::Iron), 0);
370 let map = app.world().resource::<WorldMap>();
371 let gravel = map
372 .positions()
373 .filter(|p| map.get(p.as_ivec2()) == Some(Terrain::Gravel))
374 .count();
375 assert_eq!(gravel, 1);
376 }
377
378 #[test]
379 fn colonists_walk_past_rock_they_do_not_need() {
380 let mut app = test_app(mining_map());
381 set_goals(&mut app, &[(ItemKind::Iron, 10)]);
382 spawn_colonist(&mut app, UVec2::new(8, 15));
383 run_ticks(&mut app, 60 * 15);
384 assert_eq!(items_of(&mut app, ItemKind::Iron), 10);
385 assert_eq!(items_of(&mut app, ItemKind::Stone), 0);
386 assert_eq!(
387 app.world().resource::<WorldMap>().get(IVec2::new(10, 5)),
388 Some(Terrain::Gravel)
389 );
390 }
391
392 #[test]
393 fn right_clicking_rock_orders_mining_regardless_of_goals() {
394 let mut app = test_app(mining_map());
395 set_goals(&mut app, &[]);
396 let colonist = spawn_colonist(&mut app, UVec2::new(8, 12));
397 app.world_mut().write_message(Order {
398 colonist,
399 tile: UVec2::new(10, 12),
400 });
401 run_ticks(&mut app, 60 * 10);
402 assert_eq!(
403 app.world().resource::<WorldMap>().get(IVec2::new(10, 12)),
404 Some(Terrain::Gravel)
405 );
406 }
407
408 #[test]
409 fn colonists_expand_a_stockpile_that_is_nearly_full() {
410 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(6, 5)));
411 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
412 run_ticks(&mut app, 61);
413 assert!(matches!(
414 job(&app, colonist).map(|j| j.task),
415 Some(Task::ExpandStockpile { .. })
416 ));
417 run_ticks(&mut app, 60 * 5);
418 assert_eq!(stockpile_size(&app), 2 + EXPANSION_TILES);
419 }
420
421 #[test]
422 fn roomy_stockpiles_are_left_alone() {
423 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(8, 5)));
424 spawn_colonist(&mut app, UVec2::new(2, 5));
425 run_ticks(&mut app, 60 * 5);
426 assert_eq!(stockpile_size(&app), 4);
427 }
428
429 fn set_hour(app: &mut App, hour: u64) {
430 let ticks = (hour + 24 - 6) % 24 * crate::sim::TICKS_PER_HOUR;
431 *app.world_mut().resource_mut::<SimTick>() = SimTick(ticks);
432 }
433
434 fn structures(app: &mut App) -> Vec<(UVec2, StructureKind)> {
435 let world = app.world_mut();
436 world
437 .query::<(&TilePos, &Structure)>()
438 .iter(world)
439 .map(|(pos, s)| (pos.0, s.kind))
440 .collect()
441 }
442
443 fn put_campfire(app: &mut App, tile: UVec2) {
444 app.world_mut().spawn((
445 Structure {
446 kind: StructureKind::Campfire,
447 },
448 Fuel(StructureKind::Campfire.fuel_ticks().unwrap()),
449 TilePos(tile),
450 ));
451 }
452
453 #[test]
454 fn colonists_light_a_campfire_near_the_stockpile_when_night_falls() {
455 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
456 put_wood(&mut app, UVec2::new(10, 10), 20);
457 set_hour(&mut app, 18);
458 spawn_colonist(&mut app, UVec2::new(2, 5));
459 run_ticks(&mut app, 60 * 20);
460 let built = structures(&mut app);
461 let campfire = built
462 .iter()
463 .find(|(_, kind)| *kind == StructureKind::Campfire)
464 .expect("a campfire");
465 assert!(campfire.0.as_vec2().distance(Vec2::new(11.0, 11.0)) <= HOME_RADIUS);
466 assert_eq!(wood_on_map(&mut app), 10);
467 }
468
469 #[test]
470 fn no_fires_are_built_during_the_day() {
471 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
472 put_wood(&mut app, UVec2::new(10, 10), 20);
473 set_hour(&mut app, 10);
474 spawn_colonist(&mut app, UVec2::new(2, 5));
475 run_ticks(&mut app, 60 * 10);
476 assert!(
477 structures(&mut app)
478 .iter()
479 .all(|(_, kind)| kind.light().is_none())
480 );
481 }
482
483 #[test]
484 fn without_wood_nothing_can_be_built() {
485 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
486 set_hour(&mut app, 22);
487 spawn_colonist(&mut app, UVec2::new(2, 5));
488 run_ticks(&mut app, 60 * 10);
489 assert!(
490 structures(&mut app)
491 .iter()
492 .all(|(_, kind)| kind.light().is_none())
493 );
494 }
495
496 #[test]
497 fn torches_light_stockpile_tiles_beyond_the_campfire() {
498 let mut app = hauling_app((IVec2::new(2, 10), IVec2::new(17, 10)));
499 put_wood(&mut app, UVec2::new(9, 10), 20);
500 put_campfire(&mut app, UVec2::new(9, 12));
501 set_hour(&mut app, 22);
502 spawn_colonist(&mut app, UVec2::new(9, 8));
503 run_ticks(&mut app, 60 * 15);
504 assert!(
505 structures(&mut app)
506 .iter()
507 .any(|(_, kind)| *kind == StructureKind::Torch)
508 );
509 }
510
511 // Light is what makes fires worth building: work in darkness goes at half speed.
512 #[test]
513 fn work_is_slower_in_the_dark() {
514 let tree = UVec2::new(8, 5);
515 let finish_ticks = |daylight: f32| {
516 let mut app = test_app(map_with_trees(&[tree]));
517 let size = app.world().resource::<WorldMap>().size;
518 app.insert_resource(LightMap::compute(size, daylight, []));
519 spawn_colonist(&mut app, UVec2::new(7, 5));
520 let mut ticks = 0;
521 while app
522 .world()
523 .resource::<WorldMap>()
524 .plant(tree.as_ivec2())
525 .is_some()
526 {
527 run_ticks(&mut app, 1);
528 ticks += 1;
529 }
530 ticks
531 };
532 let (lit, dark) = (finish_ticks(1.0), finish_ticks(0.0));
533 assert!(
534 dark > lit + WorkKind::Chop.work() / 2 - 10,
535 "lit {lit}, dark {dark}"
536 );
537 }
538
539 const NAP_TEST_REST: f32 = 0.25;
540
541 fn set_rest(app: &mut App, colonist: Entity, rest: f32) {
542 app.world_mut().get_mut::<Rest>(colonist).unwrap().0 = rest;
543 }
544
545 fn rest(app: &App, colonist: Entity) -> f32 {
546 app.world().get::<Rest>(colonist).unwrap().0
547 }
548
549 fn task(app: &App, colonist: Entity) -> Option<Task> {
550 job(app, colonist).map(|j| j.task)
551 }
552
553 #[test]
554 fn tired_colonists_nap_and_get_up_when_rested() {
555 let mut app = test_app(map_with_trees(&[]));
556 set_goals(&mut app, &[]);
557 set_hour(&mut app, 10);
558 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
559 set_rest(&mut app, colonist, NAP_TEST_REST);
560 run_ticks(&mut app, 61);
561 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
562 let mut ticks = 0;
563 while matches!(task(&app, colonist), Some(Task::Sleep { .. })) && ticks < 60 * 60 * 10 {
564 run_ticks(&mut app, 60);
565 ticks += 60;
566 }
567 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
568 assert!(rest(&app, colonist) > 0.95);
569 }
570
571 #[test]
572 fn colonists_go_to_bed_at_night_and_sleep_until_morning() {
573 let mut app = test_app(map_with_trees(&[]));
574 set_goals(&mut app, &[]);
575 set_hour(&mut app, 22);
576 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
577 set_rest(&mut app, colonist, 0.8);
578 run_ticks(&mut app, 61);
579 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
580 run_ticks(&mut app, 3 * crate::sim::TICKS_PER_HOUR as u32);
581 assert_eq!(rest(&app, colonist), 1.0);
582 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
583 }
584
585 #[test]
586 fn rested_colonists_stay_up_during_the_day() {
587 let mut app = test_app(map_with_trees(&[]));
588 set_goals(&mut app, &[]);
589 set_hour(&mut app, 12);
590 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
591 set_rest(&mut app, colonist, 0.8);
592 run_ticks(&mut app, 60 * 5);
593 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
594 }
595
596 #[test]
597 fn exhausted_colonists_drop_their_work_to_sleep() {
598 let tree = UVec2::new(15, 5);
599 let mut app = test_app(map_with_trees(&[tree]));
600 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
601 run_ticks(&mut app, 61);
602 assert!(matches!(task(&app, colonist), Some(Task::Work { .. })));
603 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
604 run_ticks(&mut app, 30);
605 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
606 assert!(
607 app.world()
608 .resource::<Reservations>()
609 .is_free(Target::Site(tree))
610 );
611 }
612
613 #[test]
614 fn exhaustion_does_not_override_orders() {
615 let tree = UVec2::new(15, 5);
616 let mut app = test_app(map_with_trees(&[tree]));
617 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
618 app.world_mut().write_message(Order {
619 colonist,
620 tile: tree,
621 });
622 run_ticks(&mut app, 2);
623 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
624 run_ticks(&mut app, 30);
625 assert!(matches!(
626 job(&app, colonist),
627 Some(Job {
628 task: Task::Work { .. },
629 ordered: true
630 })
631 ));
632 }
633
634 fn beds(app: &mut App) -> usize {
635 structures(app)
636 .iter()
637 .filter(|(_, kind)| *kind == StructureKind::Bed)
638 .count()
639 }
640
641 #[test]
642 fn colonists_build_one_bed_each() {
643 let mut app = test_app(map_with_trees(&[]));
644 set_goals(&mut app, &[]);
645 let map = app.world().resource::<WorldMap>().clone();
646 app.world_mut()
647 .resource_mut::<Zones>()
648 .add_stockpile(&map, IVec2::new(2, 2), IVec2::new(5, 5));
649 put_wood(&mut app, UVec2::new(2, 2), 50);
650 put_wood(&mut app, UVec2::new(3, 2), 50);
651 spawn_colonist(&mut app, UVec2::new(10, 10));
652 spawn_colonist(&mut app, UVec2::new(11, 10));
653 run_ticks(&mut app, 60 * 40);
654 assert_eq!(beds(&mut app), 2);
655 assert_eq!(wood_on_map(&mut app), 100 - 2 * StructureKind::Bed.cost().1);
656 }
657
658 // A shelter needs a wall ring with walking space around the beds; beds hugging the stockpile
659 // would leave stored items where the walls must go and the room could never be planned.
660 #[test]
661 fn beds_keep_clear_of_the_stockpile() {
662 let mut app = test_app(map_with_trees(&[]));
663 set_goals(&mut app, &[]);
664 let map = app.world().resource::<WorldMap>().clone();
665 app.world_mut().resource_mut::<Zones>().add_stockpile(
666 &map,
667 IVec2::new(8, 8),
668 IVec2::new(11, 11),
669 );
670 put_wood(&mut app, UVec2::new(8, 8), 50);
671 spawn_colonist(&mut app, UVec2::new(10, 4));
672 run_ticks(&mut app, 60 * 20);
673 let built = structures(&mut app);
674 let bed = built
675 .iter()
676 .find(|(_, kind)| *kind == StructureKind::Bed)
677 .expect("a bed");
678 let zones = app.world().resource::<Zones>();
679 assert!(zones.stockpile_tiles().all(|s| {
680 (s.as_ivec2() - bed.0.as_ivec2()).abs().max_element() >= BED_STOCKPILE_CLEARANCE
681 }));
682 }
683
684 #[test]
685 fn colonists_sleep_in_their_beds() {
686 let mut app = test_app(map_with_trees(&[]));
687 set_goals(&mut app, &[]);
688 put_beds(&mut app, 1);
689 set_hour(&mut app, 22);
690 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
691 set_rest(&mut app, colonist, 0.5);
692 run_ticks(&mut app, 60 * 10);
693 assert_eq!(
694 app.world().get::<TilePos>(colonist).unwrap().0,
695 UVec2::new(19, 19)
696 );
697 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
698 }
699
700 fn put_items(app: &mut App, tile: UVec2, kind: ItemKind, count: u32) {
701 app.world_mut()
702 .spawn((Item::new(kind, count), TilePos(tile)));
703 }
704
705 #[test]
706 fn blueprints_are_built_with_the_right_material() {
707 let mut app = test_app(map_with_trees(&[]));
708 set_goals(&mut app, &[]);
709 put_beds(&mut app, 1);
710 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 10);
711 let wall = UVec2::new(8, 8);
712 app.world_mut()
713 .resource_mut::<Blueprints>()
714 .0
715 .insert(wall, BuildKind::Structure(StructureKind::Wall));
716 spawn_colonist(&mut app, UVec2::new(5, 5));
717 run_ticks(&mut app, 60 * 10);
718 assert!(app.world().resource::<WorldMap>().is_wall(wall.as_ivec2()));
719 assert!(app.world().resource::<Blueprints>().0.is_empty());
720 assert_eq!(items_of(&mut app, ItemKind::Stone), 8);
721 }
722
723 #[test]
724 fn colonists_build_a_roofed_shelter_around_their_beds() {
725 let mut app = test_app(map_with_trees(&[]));
726 app.add_systems(Update, crate::building::plan_shelter.before(choose_jobs));
727 set_goals(&mut app, &[]);
728 for x in [10, 11] {
729 app.world_mut().spawn((
730 Structure {
731 kind: StructureKind::Bed,
732 },
733 TilePos(UVec2::new(x, 10)),
734 ));
735 }
736 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 50);
737 put_items(&mut app, UVec2::new(3, 2), ItemKind::Wood, 50);
738 spawn_colonist(&mut app, UVec2::new(4, 4));
739 spawn_colonist(&mut app, UVec2::new(5, 4));
740 run_ticks(&mut app, 60 * 150);
741 let map = app.world().resource::<WorldMap>();
742 assert!(map.is_roofed(IVec2::new(10, 10)) && map.is_roofed(IVec2::new(11, 10)));
743 let walls = map
744 .positions()
745 .filter(|p| map.is_wall(p.as_ivec2()))
746 .count();
747 assert_eq!(walls, 17);
748 assert!(
749 structures(&mut app)
750 .iter()
751 .any(|(_, kind)| *kind == StructureKind::Door)
752 );
753 }
754
755 #[test]
756 fn colonists_chop_from_a_neighbouring_tile() {
757 let tree = UVec2::new(8, 5);
758 let mut app = test_app(map_with_trees(&[tree]));
759 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
760 run_ticks(&mut app, 60 * 5);
761 let tile = app.world().get::<TilePos>(colonist).unwrap().0;
762 assert!(is_adjacent(tile.as_ivec2(), tree.as_ivec2()));
763 assert_eq!(
764 app.world().get::<SimPos>(colonist).unwrap().0,
765 WorldMap::tile_center(tile)
766 );
767 }
768
769 fn set_fed(app: &mut App, colonist: Entity, fed: f32) {
770 app.world_mut().get_mut::<Hunger>(colonist).unwrap().0 = fed;
771 }
772
773 fn fed(app: &App, colonist: Entity) -> f32 {
774 app.world().get::<Hunger>(colonist).unwrap().0
775 }
776
777 #[test]
778 fn hungry_colonists_eat_until_full() {
779 let mut app = test_app(map_with_trees(&[]));
780 set_goals(&mut app, &[]);
781 put_items(&mut app, UVec2::new(8, 5), ItemKind::Berries, 20);
782 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
783 set_fed(&mut app, colonist, 0.3);
784 run_ticks(&mut app, 60 * 6);
785 assert!(fed(&app, colonist) > 0.95);
786 assert_eq!(items_of(&mut app, ItemKind::Berries), 13);
787 }
788
789 #[test]
790 fn colonists_are_not_peckish_when_fed() {
791 let mut app = test_app(map_with_trees(&[]));
792 set_goals(&mut app, &[]);
793 put_items(&mut app, UVec2::new(8, 5), ItemKind::Berries, 20);
794 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
795 set_fed(&mut app, colonist, 0.8);
796 run_ticks(&mut app, 60 * 6);
797 assert_eq!(items_of(&mut app, ItemKind::Berries), 20);
798 }
799
800 #[test]
801 fn starving_colonists_drop_their_work_to_eat() {
802 let tree = UVec2::new(15, 5);
803 let mut app = test_app(map_with_trees(&[tree]));
804 put_items(&mut app, UVec2::new(2, 8), ItemKind::Meat, 20);
805 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
806 run_ticks(&mut app, 61);
807 assert!(task(&app, colonist).is_some_and(|t| !matches!(t, Task::Eat { .. })));
808 set_fed(&mut app, colonist, STARVING / 2.0);
809 run_ticks(&mut app, 30);
810 assert!(matches!(task(&app, colonist), Some(Task::Eat { .. })));
811 }
812
813 // Starvation only interrupts work when there is food to fetch. Without that check a starving
814 // colonist dropped their job every tick, searched the whole map for food and picked the same
815 // job again, never finishing anything and stalling the simulation.
816 #[test]
817 fn starving_colonists_without_food_keep_working() {
818 let tree = UVec2::new(6, 5);
819 let mut app = test_app(map_with_trees(&[tree]));
820 set_goals(&mut app, &[(ItemKind::Wood, 50)]);
821 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
822 set_fed(&mut app, colonist, STARVING / 2.0);
823 run_ticks(&mut app, 60 + 60 * 4 + WorkKind::Chop.work());
824 assert!(items_of(&mut app, ItemKind::Wood) > 0);
825 }
826
827 #[test]
828 fn colonists_forage_ripe_berries_when_the_colony_wants_them() {
829 let mut map = map_with_trees(&[]);
830 let ripe = UVec2::new(8, 5);
831 let unripe = UVec2::new(4, 5);
832 for (bush, growth) in [(ripe, 1.0), (unripe, 0.5)] {
833 map.set_plant(bush, Some(Plant::BerryBush));
834 map.set_growth(bush, growth);
835 }
836 let mut app = test_app(map);
837 set_goals(&mut app, &[(ItemKind::Berries, 30)]);
838 spawn_colonist(&mut app, UVec2::new(2, 5));
839 run_ticks(&mut app, 60 * 10);
840 assert_eq!(items_of(&mut app, ItemKind::Berries), 8);
841 let map = app.world().resource::<WorldMap>();
842 assert_eq!(map.plant(ripe.as_ivec2()), Some(Plant::BerryBush));
843 assert_eq!(map.growth(ripe.as_ivec2()), 0.0);
844 assert_eq!(map.growth(unripe.as_ivec2()), 0.5);
845 }
846
847 fn put_animal(app: &mut App, kind: AnimalKind, tile: UVec2) -> Entity {
848 app.world_mut()
849 .spawn((
850 Animal { kind },
851 TilePos(tile),
852 sim_pos(WorldMap::tile_center(tile)),
853 ))
854 .id()
855 }
856
857 #[test]
858 fn colonists_hunt_when_the_colony_wants_meat() {
859 let mut app = test_app(map_with_trees(&[]));
860 app.add_systems(Update, crate::animals::wander.before(choose_jobs));
861 set_goals(&mut app, &[(ItemKind::Meat, 20)]);
862 let deer = put_animal(&mut app, AnimalKind::Deer, UVec2::new(15, 15));
863 spawn_colonist(&mut app, UVec2::new(2, 2));
864 run_ticks(&mut app, 60 * 15);
865 assert!(app.world().get_entity(deer).is_err());
866 assert_eq!(items_of(&mut app, ItemKind::Meat), AnimalKind::Deer.meat());
867 }
868
869 #[test]
870 fn animals_are_left_alone_when_there_is_enough_meat() {
871 let mut app = test_app(map_with_trees(&[]));
872 set_goals(&mut app, &[]);
873 let rabbit = put_animal(&mut app, AnimalKind::Rabbit, UVec2::new(10, 10));
874 spawn_colonist(&mut app, UVec2::new(2, 2));
875 run_ticks(&mut app, 60 * 10);
876 assert!(app.world().get_entity(rabbit).is_ok());
877 }
878
879 #[test]
880 fn right_clicking_an_animal_orders_a_hunt() {
881 let mut app = test_app(map_with_trees(&[]));
882 set_goals(&mut app, &[]);
883 let rabbit = put_animal(&mut app, AnimalKind::Rabbit, UVec2::new(10, 10));
884 let colonist = spawn_colonist(&mut app, UVec2::new(2, 2));
885 app.world_mut().write_message(Order {
886 colonist,
887 tile: UVec2::new(10, 10),
888 });
889 run_ticks(&mut app, 60 * 10);
890 assert!(app.world().get_entity(rabbit).is_err());
891 assert_eq!(
892 items_of(&mut app, ItemKind::Meat),
893 AnimalKind::Rabbit.meat()
894 );
895 }
896
897 fn farm_app(tiles: &[UVec2]) -> App {
898 let mut map = map_with_trees(&[]);
899 for &t in tiles {
900 map.set(t, Terrain::Soil);
901 }
902 let mut app = test_app(map);
903 set_goals(&mut app, &[]);
904 put_beds(&mut app, 1);
905 let map = app.world().resource::<WorldMap>().clone();
906 app.world_mut()
907 .resource_mut::<Zones>()
908 .add_farm_tiles(&map, tiles.iter().copied());
909 app
910 }
911
912 #[test]
913 fn colonists_sow_empty_farm_tiles() {
914 let farm = [UVec2::new(8, 8), UVec2::new(9, 8)];
915 let mut app = farm_app(&farm);
916 spawn_colonist(&mut app, UVec2::new(2, 2));
917 run_ticks(&mut app, 60 * 10);
918 let map = app.world().resource::<WorldMap>();
919 assert!(
920 farm.iter()
921 .all(|t| map.plant(t.as_ivec2()) == Some(Plant::Crop))
922 );
923 }
924
925 // Sowing used to be the last resort, so a colony with any material or construction demand
926 // never planted its farm and starved once wild food ran out.
927 #[test]
928 fn hungry_colonies_sow_before_gathering_materials() {
929 let farm = [UVec2::new(8, 8)];
930 let mut app = farm_app(&farm);
931 app.world_mut()
932 .resource_mut::<WorldMap>()
933 .set_plant(UVec2::new(3, 2), Some(Plant::Tree));
934 set_goals(
935 &mut app,
936 &[(ItemKind::Vegetables, 30), (ItemKind::Wood, 30)],
937 );
938 let colonist = spawn_colonist(&mut app, UVec2::new(2, 2));
939 run_ticks(&mut app, 61);
940 assert!(matches!(
941 task(&app, colonist),
942 Some(Task::Work {
943 kind: WorkKind::Sow,
944 ..
945 })
946 ));
947 }
948
949 #[test]
950 fn hungry_colonies_harvest_before_gathering_materials() {
951 let farm = [UVec2::new(8, 8)];
952 let mut app = farm_app(&farm);
953 {
954 let mut map = app.world_mut().resource_mut::<WorldMap>();
955 map.set_plant(UVec2::new(3, 2), Some(Plant::Tree));
956 map.set_plant(farm[0], Some(Plant::Crop));
957 map.set_growth(farm[0], 1.0);
958 }
959 set_goals(
960 &mut app,
961 &[(ItemKind::Vegetables, 30), (ItemKind::Wood, 30)],
962 );
963 let colonist = spawn_colonist(&mut app, UVec2::new(2, 2));
964 run_ticks(&mut app, 61);
965 assert!(matches!(
966 task(&app, colonist),
967 Some(Task::Work {
968 kind: WorkKind::Harvest,
969 ..
970 })
971 ));
972 }
973
974 #[test]
975 fn ripe_crops_are_harvested_into_vegetables_and_resown() {
976 let farm = [UVec2::new(8, 8)];
977 let mut app = farm_app(&farm);
978 set_goals(&mut app, &[(ItemKind::Vegetables, 30)]);
979 {
980 let mut map = app.world_mut().resource_mut::<WorldMap>();
981 map.set_plant(farm[0], Some(Plant::Crop));
982 map.set_growth(farm[0], 1.0);
983 }
984 spawn_colonist(&mut app, UVec2::new(2, 2));
985 run_ticks(&mut app, 60 * 15);
986 assert_eq!(items_of(&mut app, ItemKind::Vegetables), 10);
987 let map = app.world().resource::<WorldMap>();
988 assert_eq!(map.plant(farm[0].as_ivec2()), Some(Plant::Crop));
989 assert!(map.growth(farm[0].as_ivec2()) < 0.1);
990 }
991
992 // Food the colony cannot reach must not count towards its stock: a stray stack far from home
993 // once satisfied the raw food target, so nobody harvested ripe crops while everyone starved.
994 #[test]
995 fn stock_out_of_the_colonys_reach_does_not_stop_harvesting() {
996 let farm = UVec2::new(8, 8);
997 let mut map = WorldMap::filled(UVec2::splat(150), Terrain::Grass);
998 map.set(farm, Terrain::Soil);
999 map.set_plant(farm, Some(Plant::Crop));
1000 map.set_growth(farm, 1.0);
1001 let mut app = test_app(map.clone());
1002 set_goals(&mut app, &[(ItemKind::Vegetables, 10)]);
1003 put_beds(&mut app, 1);
1004 let mut zones = app.world_mut().resource_mut::<Zones>();
1005 zones.add_farm_tiles(&map, [farm]);
1006 zones.add_tiles(&map, [UVec2::new(4, 4)]);
1007 let far = UVec2::new(4, 4) + UVec2::splat(COLONY_RADIUS as u32 + 1);
1008 put_items(&mut app, far, ItemKind::Meat, 20);
1009 spawn_colonist(&mut app, UVec2::new(2, 2));
1010 run_ticks(&mut app, 60 * 15);
1011 assert!(items_of(&mut app, ItemKind::Vegetables) > 0);
1012 }
1013
1014 #[test]
1015 fn colonist_searches_cover_the_colony_radius() {
1016 let map = WorldMap::filled(UVec2::splat(200), Terrain::Grass);
1017 let home = UVec2::splat(100);
1018 let corner = home + UVec2::splat(COLONY_RADIUS as u32);
1019 assert_eq!(
1020 nearest_tile(&map, home, SEARCH_TILES, |p| p == corner),
1021 Some(corner)
1022 );
1023 }
1024
1025 #[test]
1026 fn colonists_cook_raw_food_into_meals_at_a_campfire() {
1027 let mut app = test_app(map_with_trees(&[]));
1028 set_goals(&mut app, &[(ItemKind::Meal, 2)]);
1029 put_beds(&mut app, 1);
1030 put_campfire(&mut app, UVec2::new(10, 10));
1031 put_items(&mut app, UVec2::new(5, 5), ItemKind::Meat, 12);
1032 spawn_colonist(&mut app, UVec2::new(2, 2));
1033 run_ticks(&mut app, 60 * 20);
1034 assert_eq!(items_of(&mut app, ItemKind::Meal), 2);
1035 assert_eq!(items_of(&mut app, ItemKind::Meat), 0);
1036 }
1037
1038 #[test]
1039 fn colonists_light_a_fire_to_cook_on() {
1040 let mut app = test_app(map_with_trees(&[]));
1041 set_goals(&mut app, &[(ItemKind::Meal, 1)]);
1042 put_beds(&mut app, 1);
1043 put_items(&mut app, UVec2::new(5, 5), ItemKind::Berries, 6);
1044 put_items(&mut app, UVec2::new(6, 5), ItemKind::Wood, 10);
1045 spawn_colonist(&mut app, UVec2::new(2, 2));
1046 run_ticks(&mut app, 60 * 25);
1047 assert!(
1048 structures(&mut app)
1049 .iter()
1050 .any(|(_, kind)| *kind == StructureKind::Campfire)
1051 );
1052 assert_eq!(items_of(&mut app, ItemKind::Meal), 1);
1053 }
1054
1055 #[test]
1056 fn no_cooking_without_demand_for_meals() {
1057 let mut app = test_app(map_with_trees(&[]));
1058 set_goals(&mut app, &[]);
1059 put_beds(&mut app, 1);
1060 put_campfire(&mut app, UVec2::new(10, 10));
1061 put_items(&mut app, UVec2::new(5, 5), ItemKind::Meat, 12);
1062 spawn_colonist(&mut app, UVec2::new(2, 2));
1063 run_ticks(&mut app, 60 * 10);
1064 assert_eq!(items_of(&mut app, ItemKind::Meal), 0);
1065 }
1066
1067 // Chopping and hauling leave material in many small piles; a builder gathers from several
1068 // piles instead of waiting for one big enough to cover the whole cost.
1069 #[test]
1070 fn builders_gather_material_from_several_piles() {
1071 let mut app = test_app(map_with_trees(&[]));
1072 set_goals(&mut app, &[]);
1073 put_wood(&mut app, UVec2::new(4, 4), 10);
1074 put_wood(&mut app, UVec2::new(15, 4), 10);
1075 spawn_colonist(&mut app, UVec2::new(2, 2));
1076 run_ticks(&mut app, 60 * 30);
1077 assert_eq!(beds(&mut app), 1);
1078 assert_eq!(wood_on_map(&mut app), 20 - StructureKind::Bed.cost().1);
1079 }
1080
1081 #[test]
1082 fn colonists_pave_planned_paths_with_stone() {
1083 let mut app = test_app(map_with_trees(&[]));
1084 set_goals(&mut app, &[]);
1085 put_beds(&mut app, 1);
1086 let tile = UVec2::new(8, 8);
1087 app.world_mut()
1088 .resource_mut::<WorldMap>()
1089 .set(tile, Terrain::Path);
1090 app.world_mut()
1091 .resource_mut::<Blueprints>()
1092 .0
1093 .insert(tile, BuildKind::Pave);
1094 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 5);
1095 spawn_colonist(&mut app, UVec2::new(4, 4));
1096 run_ticks(&mut app, 60 * 10);
1097 assert_eq!(
1098 app.world().resource::<WorldMap>().get(tile.as_ivec2()),
1099 Some(Terrain::Paved)
1100 );
1101 assert_eq!(items_of(&mut app, ItemKind::Stone), 4);
1102 }
1103
1104 // The wall-builder must not trap a colonist who is just stepping onto the tile: that colonist's
1105 // TilePos is still the previous tile, so only checking TilePos would let the wall close on them.
1106 #[test]
1107 fn walls_wait_for_colonists_stepping_onto_the_tile() {
1108 let mut app = test_app(map_with_trees(&[]));
1109 set_goals(&mut app, &[]);
1110 put_beds(&mut app, 2);
1111 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 10);
1112 let wall = UVec2::new(8, 8);
1113 app.world_mut()
1114 .resource_mut::<Blueprints>()
1115 .0
1116 .insert(wall, BuildKind::Structure(StructureKind::Wall));
1117 let colonists = [
1118 spawn_colonist(&mut app, UVec2::new(4, 4)),
1119 spawn_colonist(&mut app, UVec2::new(8, 10)),
1120 ];
1121 let mut sent = false;
1122 for _ in 0..60 * 20 {
1123 let nearly_built = |c| {
1124 matches!(
1125 task(&app, c),
1126 Some(Task::Build { progress, kind, .. }) if progress + 4 >= kind.work()
1127 )
1128 };
1129 if !sent && let Some(&builder) = colonists.iter().find(|&&c| nearly_built(c)) {
1130 let walker = *colonists.iter().find(|&&c| c != builder).unwrap();
1131 let from = app.world().get::<TilePos>(walker).unwrap().0;
1132 let route: VecDeque<UVec2> = find_path(app.world().resource::<WorldMap>(), from, wall)
1133 .unwrap()
1134 .into();
1135 app.world_mut().get_mut::<Path>(walker).unwrap().0 = route;
1136 sent = true;
1137 }
1138 run_ticks(&mut app, 1);
1139 let map = app.world().resource::<WorldMap>();
1140 for &c in &colonists {
1141 let tile = app.world().get::<TilePos>(c).unwrap().0;
1142 assert!(!map.is_wall(tile.as_ivec2()), "colonist trapped in a wall");
1143 }
1144 }
1145 assert!(sent);
1146 assert!(app.world().resource::<WorldMap>().is_wall(wall.as_ivec2()));
1147 }