src/jobs/tests.rs 33.9 K · 1061 lines · raw · history

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