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