src/jobs/tests.rs 26.3 K · 842 lines · raw · history

1 use super::*;
2 use crate::{
3 colonist::follow_path,
4 food::Hunger,
5 items::STACK_LIMIT,
6 sim::{SimPos, sim_pos},
7 world::Terrain,
8 zones::NEW_STOCKPILE_SIZE,
9 };
10
11 const TICK: Duration = Duration::from_nanos(1_000_000_000 / 60);
12
13 fn test_app(map: WorldMap) -> App {
14 let mut app = App::new();
15 app.insert_resource(map)
16 .insert_resource(SimRng::new(1))
17 .init_resource::<Time>()
18 .init_resource::<Reservations>()
19 .init_resource::<ColonyGoals>()
20 .init_resource::<ItemAssets>()
21 .init_resource::<StructureAssets>()
22 .init_resource::<Blueprints>()
23 .init_resource::<LightMap>()
24 .init_resource::<SimTick>()
25 .init_resource::<Zones>()
26 .add_message::<Order>()
27 .add_message::<TileChanged>()
28 .add_systems(
29 Update,
30 (
31 handle_orders,
32 choose_jobs,
33 run_jobs,
34 follow_path,
35 crate::needs::update_rest,
36 )
37 .chain(),
38 );
39 app
40 }
41
42 fn spawn_colonist(app: &mut App, tile: UVec2) -> Entity {
43 app.world_mut()
44 .spawn((
45 Colonist {
46 name: "Test".into(),
47 },
48 TilePos(tile),
49 sim_pos(WorldMap::tile_center(tile)),
50 ))
51 .id()
52 }
53
54 fn run_ticks(app: &mut App, ticks: u32) {
55 for _ in 0..ticks {
56 app.world_mut().resource_mut::<Time>().advance_by(TICK);
57 app.update();
58 }
59 }
60
61 fn map_with_trees(trees: &[UVec2]) -> WorldMap {
62 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
63 for &t in trees {
64 map.set_plant(t, Some(Plant::Tree));
65 }
66 map
67 }
68
69 fn wood_on_map(app: &mut App) -> u32 {
70 let world = app.world_mut();
71 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
72 stock(items.iter(), ItemKind::Wood)
73 }
74
75 fn items_at(app: &mut App) -> Vec<(UVec2, u32)> {
76 let world = app.world_mut();
77 let mut items: Vec<(UVec2, u32)> = world
78 .query::<(&Item, &TilePos)>()
79 .iter(world)
80 .map(|(item, pos)| (pos.0, item.count))
81 .collect();
82 items.sort_by_key(|(p, _)| (p.y, p.x));
83 items
84 }
85
86 fn put_wood(app: &mut App, tile: UVec2, count: u32) {
87 app.world_mut().spawn((
88 Item {
89 kind: ItemKind::Wood,
90 count,
91 },
92 TilePos(tile),
93 ));
94 }
95
96 fn set_goals(app: &mut App, goals: &[(ItemKind, u32)]) {
97 *app.world_mut().resource_mut::<ColonyGoals>() = ColonyGoals(goals.iter().copied().collect());
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 // Each tree or rock being worked will add its yield; counting that as material on the way
190 // keeps the whole colony from felling a forest when only a little more wood is needed.
191 #[test]
192 fn work_in_progress_counts_towards_goals() {
193 let goals = ColonyGoals(HashMap::from([(ItemKind::Wood, 25)]));
194 assert!(goals.wanted(ItemKind::Wood, 10, 10));
195 assert!(!goals.wanted(ItemKind::Wood, 10, 20));
196 assert!(!goals.wanted(ItemKind::Wood, 30, 0));
197 assert!(!goals.wanted(ItemKind::Gold, 0, 0));
198 }
199
200 #[test]
201 fn ordered_chop_ignores_the_wood_goal() {
202 let tree = UVec2::new(8, 5);
203 let mut app = test_app(map_with_trees(&[tree]));
204 set_goals(&mut app, &[]);
205 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
206 app.world_mut().write_message(Order {
207 colonist,
208 tile: tree,
209 });
210 run_ticks(&mut app, 60 * 15);
211 assert_eq!(
212 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
213 None
214 );
215 }
216
217 #[test]
218 fn ordering_a_move_cancels_the_current_job_and_frees_its_tree() {
219 let tree = UVec2::new(15, 5);
220 let mut app = test_app(map_with_trees(&[tree]));
221 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
222 run_ticks(&mut app, 61);
223 assert!(matches!(
224 job(&app, colonist),
225 Some(Job {
226 task: Task::Work { .. },
227 ..
228 })
229 ));
230 app.world_mut().write_message(Order {
231 colonist,
232 tile: UVec2::new(2, 15),
233 });
234 run_ticks(&mut app, 1);
235 assert_eq!(
236 job(&app, colonist).map(|j| j.task),
237 Some(Task::MoveTo(UVec2::new(2, 15)))
238 );
239 assert!(
240 app.world()
241 .resource::<Reservations>()
242 .is_free(Target::Site(tree))
243 );
244 }
245
246 #[test]
247 fn idle_colonists_haul_loose_items_into_the_stockpile() {
248 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
249 put_wood(&mut app, UVec2::new(5, 5), 20);
250 spawn_colonist(&mut app, UVec2::new(2, 5));
251 run_ticks(&mut app, 60 * 15);
252 assert_eq!(items_at(&mut app), vec![(UVec2::new(15, 15), 20)]);
253 }
254
255 #[test]
256 fn large_stacks_take_several_trips_and_spill_into_more_stockpile_tiles() {
257 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
258 put_wood(&mut app, UVec2::new(5, 5), STACK_LIMIT + 10);
259 spawn_colonist(&mut app, UVec2::new(2, 5));
260 run_ticks(&mut app, 60 * 40);
261 let stacks = items_at(&mut app);
262 assert_eq!(stored(&mut app), STACK_LIMIT + 10);
263 assert!(stacks.len() >= 2);
264 assert!(stacks.iter().all(|(_, count)| *count <= STACK_LIMIT));
265 }
266
267 #[test]
268 fn items_already_in_a_stockpile_stay_put() {
269 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(16, 16)));
270 put_wood(&mut app, UVec2::new(5, 5), 20);
271 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
272 run_ticks(&mut app, 60 * 5);
273 assert_eq!(items_at(&mut app), vec![(UVec2::new(5, 5), 20)]);
274 assert_eq!(carrying(&app, colonist), None);
275 }
276
277 #[test]
278 fn colonists_found_a_stockpile_when_items_need_storing() {
279 let mut app = test_app(map_with_trees(&[]));
280 put_beds(&mut app, 1);
281 set_goals(&mut app, &[]);
282 put_wood(&mut app, UVec2::new(5, 5), 20);
283 spawn_colonist(&mut app, UVec2::new(2, 5));
284 run_ticks(&mut app, 60 * 15);
285 assert_eq!(
286 stockpile_size(&app),
287 (NEW_STOCKPILE_SIZE * NEW_STOCKPILE_SIZE) as usize
288 );
289 assert_eq!(stored(&mut app), 20);
290 }
291
292 #[test]
293 fn no_stockpile_is_founded_while_there_is_nothing_to_store() {
294 let mut app = test_app(map_with_trees(&[]));
295 set_goals(&mut app, &[]);
296 spawn_colonist(&mut app, UVec2::new(2, 5));
297 run_ticks(&mut app, 60 * 5);
298 assert_eq!(stockpile_size(&app), 0);
299 }
300
301 #[test]
302 fn two_colonists_never_haul_the_same_stack() {
303 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(16, 16)));
304 put_wood(&mut app, UVec2::new(5, 5), 10);
305 put_wood(&mut app, UVec2::new(5, 6), 10);
306 let a = spawn_colonist(&mut app, UVec2::new(2, 5));
307 let b = spawn_colonist(&mut app, UVec2::new(2, 6));
308 run_ticks(&mut app, 61);
309 let pickup = |c| match job(&app, c).map(|j| j.task) {
310 Some(Task::Haul { pickup, .. }) => pickup,
311 other => panic!("expected haul, got {other:?}"),
312 };
313 assert_ne!(pickup(a), pickup(b));
314 }
315
316 // Orders interrupt whatever a colonist is doing, but whatever it is carrying stays in its
317 // hands; once it is idle again it finishes delivering it instead of dropping it anywhere.
318 #[test]
319 fn interrupted_haulers_deliver_their_load_later() {
320 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
321 put_wood(&mut app, UVec2::new(4, 5), 20);
322 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
323 let mut ticks = 0;
324 while carrying(&app, colonist).is_none() && ticks < 60 * 10 {
325 run_ticks(&mut app, 1);
326 ticks += 1;
327 }
328 app.world_mut().write_message(Order {
329 colonist,
330 tile: UVec2::new(2, 2),
331 });
332 run_ticks(&mut app, 60 * 3);
333 assert!(carrying(&app, colonist).is_some());
334 run_ticks(&mut app, 60 * 20);
335 assert_eq!(carrying(&app, colonist), None);
336 assert_eq!(stored(&mut app), 20);
337 }
338
339 #[test]
340 fn right_clicking_loose_items_orders_a_haul() {
341 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(15, 15)));
342 set_goals(&mut app, &[(ItemKind::Wood, 1000)]);
343 put_wood(&mut app, UVec2::new(5, 5), 20);
344 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
345 app.world_mut().write_message(Order {
346 colonist,
347 tile: UVec2::new(5, 5),
348 });
349 run_ticks(&mut app, 1);
350 assert!(matches!(
351 job(&app, colonist),
352 Some(Job {
353 task: Task::Haul { .. },
354 ordered: true
355 })
356 ));
357 }
358
359 fn mining_map() -> WorldMap {
360 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
361 for y in 0..20 {
362 map.set(UVec2::new(10, y), Terrain::Stone);
363 }
364 map.set(UVec2::new(10, 5), Terrain::IronOre);
365 map
366 }
367
368 fn items_of(app: &mut App, kind: ItemKind) -> u32 {
369 let world = app.world_mut();
370 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
371 stock(items.iter(), kind)
372 }
373
374 #[test]
375 fn colonists_mine_rock_the_colony_needs_and_leave_gravel() {
376 let mut app = test_app(mining_map());
377 set_goals(&mut app, &[(ItemKind::Stone, 10)]);
378 spawn_colonist(&mut app, UVec2::new(8, 12));
379 run_ticks(&mut app, 60 * 12);
380 assert_eq!(items_of(&mut app, ItemKind::Stone), 10);
381 assert_eq!(items_of(&mut app, ItemKind::Iron), 0);
382 let map = app.world().resource::<WorldMap>();
383 let gravel = map
384 .positions()
385 .filter(|p| map.get(p.as_ivec2()) == Some(Terrain::Gravel))
386 .count();
387 assert_eq!(gravel, 1);
388 }
389
390 #[test]
391 fn colonists_walk_past_rock_they_do_not_need() {
392 let mut app = test_app(mining_map());
393 set_goals(&mut app, &[(ItemKind::Iron, 10)]);
394 spawn_colonist(&mut app, UVec2::new(8, 15));
395 run_ticks(&mut app, 60 * 15);
396 assert_eq!(items_of(&mut app, ItemKind::Iron), 10);
397 assert_eq!(items_of(&mut app, ItemKind::Stone), 0);
398 assert_eq!(
399 app.world().resource::<WorldMap>().get(IVec2::new(10, 5)),
400 Some(Terrain::Gravel)
401 );
402 }
403
404 #[test]
405 fn right_clicking_rock_orders_mining_regardless_of_goals() {
406 let mut app = test_app(mining_map());
407 set_goals(&mut app, &[]);
408 let colonist = spawn_colonist(&mut app, UVec2::new(8, 12));
409 app.world_mut().write_message(Order {
410 colonist,
411 tile: UVec2::new(10, 12),
412 });
413 run_ticks(&mut app, 60 * 10);
414 assert_eq!(
415 app.world().resource::<WorldMap>().get(IVec2::new(10, 12)),
416 Some(Terrain::Gravel)
417 );
418 }
419
420 #[test]
421 fn colonists_expand_a_stockpile_that_is_nearly_full() {
422 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(6, 5)));
423 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
424 run_ticks(&mut app, 61);
425 assert!(matches!(
426 job(&app, colonist).map(|j| j.task),
427 Some(Task::ExpandStockpile { .. })
428 ));
429 run_ticks(&mut app, 60 * 5);
430 assert_eq!(stockpile_size(&app), 2 + EXPANSION_TILES);
431 }
432
433 #[test]
434 fn roomy_stockpiles_are_left_alone() {
435 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(8, 5)));
436 spawn_colonist(&mut app, UVec2::new(2, 5));
437 run_ticks(&mut app, 60 * 5);
438 assert_eq!(stockpile_size(&app), 4);
439 }
440
441 fn set_hour(app: &mut App, hour: u64) {
442 let ticks = (hour + 24 - 6) % 24 * crate::sim::TICKS_PER_HOUR;
443 *app.world_mut().resource_mut::<SimTick>() = SimTick(ticks);
444 }
445
446 fn structures(app: &mut App) -> Vec<(UVec2, StructureKind)> {
447 let world = app.world_mut();
448 world
449 .query::<(&TilePos, &Structure)>()
450 .iter(world)
451 .map(|(pos, s)| (pos.0, s.kind))
452 .collect()
453 }
454
455 fn put_campfire(app: &mut App, tile: UVec2) {
456 app.world_mut().spawn((
457 Structure {
458 kind: StructureKind::Campfire,
459 },
460 Fuel(StructureKind::Campfire.fuel_ticks().unwrap()),
461 TilePos(tile),
462 ));
463 }
464
465 #[test]
466 fn colonists_light_a_campfire_near_the_stockpile_when_night_falls() {
467 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
468 put_wood(&mut app, UVec2::new(10, 10), 20);
469 set_hour(&mut app, 18);
470 spawn_colonist(&mut app, UVec2::new(2, 5));
471 run_ticks(&mut app, 60 * 20);
472 let built = structures(&mut app);
473 let campfire = built
474 .iter()
475 .find(|(_, kind)| *kind == StructureKind::Campfire)
476 .expect("a campfire");
477 assert!(campfire.0.as_vec2().distance(Vec2::new(11.0, 11.0)) <= HOME_RADIUS);
478 assert_eq!(wood_on_map(&mut app), 10);
479 }
480
481 #[test]
482 fn no_fires_are_built_during_the_day() {
483 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
484 put_wood(&mut app, UVec2::new(10, 10), 20);
485 set_hour(&mut app, 10);
486 spawn_colonist(&mut app, UVec2::new(2, 5));
487 run_ticks(&mut app, 60 * 10);
488 assert!(
489 structures(&mut app)
490 .iter()
491 .all(|(_, kind)| kind.light().is_none())
492 );
493 }
494
495 #[test]
496 fn without_wood_nothing_can_be_built() {
497 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
498 set_hour(&mut app, 22);
499 spawn_colonist(&mut app, UVec2::new(2, 5));
500 run_ticks(&mut app, 60 * 10);
501 assert!(
502 structures(&mut app)
503 .iter()
504 .all(|(_, kind)| kind.light().is_none())
505 );
506 }
507
508 #[test]
509 fn torches_light_stockpile_tiles_beyond_the_campfire() {
510 let mut app = hauling_app((IVec2::new(2, 10), IVec2::new(17, 10)));
511 put_wood(&mut app, UVec2::new(9, 10), 20);
512 put_campfire(&mut app, UVec2::new(9, 12));
513 set_hour(&mut app, 22);
514 spawn_colonist(&mut app, UVec2::new(9, 8));
515 run_ticks(&mut app, 60 * 15);
516 assert!(
517 structures(&mut app)
518 .iter()
519 .any(|(_, kind)| *kind == StructureKind::Torch)
520 );
521 }
522
523 // Light is what makes fires worth building: work in darkness goes at half speed.
524 #[test]
525 fn work_is_slower_in_the_dark() {
526 let tree = UVec2::new(8, 5);
527 let finish_ticks = |daylight: f32| {
528 let mut app = test_app(map_with_trees(&[tree]));
529 let size = app.world().resource::<WorldMap>().size;
530 app.insert_resource(LightMap::compute(size, daylight, []));
531 spawn_colonist(&mut app, UVec2::new(7, 5));
532 let mut ticks = 0;
533 while app
534 .world()
535 .resource::<WorldMap>()
536 .plant(tree.as_ivec2())
537 .is_some()
538 {
539 run_ticks(&mut app, 1);
540 ticks += 1;
541 }
542 ticks
543 };
544 let (lit, dark) = (finish_ticks(1.0), finish_ticks(0.0));
545 assert!(
546 dark > lit + WorkKind::Chop.work() / 2 - 10,
547 "lit {lit}, dark {dark}"
548 );
549 }
550
551 const NAP_TEST_REST: f32 = 0.25;
552
553 fn set_rest(app: &mut App, colonist: Entity, rest: f32) {
554 app.world_mut().get_mut::<Rest>(colonist).unwrap().0 = rest;
555 }
556
557 fn rest(app: &App, colonist: Entity) -> f32 {
558 app.world().get::<Rest>(colonist).unwrap().0
559 }
560
561 fn task(app: &App, colonist: Entity) -> Option<Task> {
562 job(app, colonist).map(|j| j.task)
563 }
564
565 #[test]
566 fn tired_colonists_nap_and_get_up_when_rested() {
567 let mut app = test_app(map_with_trees(&[]));
568 set_goals(&mut app, &[]);
569 set_hour(&mut app, 10);
570 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
571 set_rest(&mut app, colonist, NAP_TEST_REST);
572 run_ticks(&mut app, 61);
573 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
574 let mut ticks = 0;
575 while matches!(task(&app, colonist), Some(Task::Sleep { .. })) && ticks < 60 * 60 * 10 {
576 run_ticks(&mut app, 60);
577 ticks += 60;
578 }
579 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
580 assert!(rest(&app, colonist) > 0.95);
581 }
582
583 #[test]
584 fn colonists_go_to_bed_at_night_and_sleep_until_morning() {
585 let mut app = test_app(map_with_trees(&[]));
586 set_goals(&mut app, &[]);
587 set_hour(&mut app, 22);
588 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
589 set_rest(&mut app, colonist, 0.8);
590 run_ticks(&mut app, 61);
591 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
592 run_ticks(&mut app, 3 * crate::sim::TICKS_PER_HOUR as u32);
593 assert_eq!(rest(&app, colonist), 1.0);
594 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
595 }
596
597 #[test]
598 fn rested_colonists_stay_up_during_the_day() {
599 let mut app = test_app(map_with_trees(&[]));
600 set_goals(&mut app, &[]);
601 set_hour(&mut app, 12);
602 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
603 set_rest(&mut app, colonist, 0.8);
604 run_ticks(&mut app, 60 * 5);
605 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
606 }
607
608 #[test]
609 fn exhausted_colonists_drop_their_work_to_sleep() {
610 let tree = UVec2::new(15, 5);
611 let mut app = test_app(map_with_trees(&[tree]));
612 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
613 run_ticks(&mut app, 61);
614 assert!(matches!(task(&app, colonist), Some(Task::Work { .. })));
615 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
616 run_ticks(&mut app, 30);
617 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
618 assert!(
619 app.world()
620 .resource::<Reservations>()
621 .is_free(Target::Site(tree))
622 );
623 }
624
625 #[test]
626 fn exhaustion_does_not_override_orders() {
627 let tree = UVec2::new(15, 5);
628 let mut app = test_app(map_with_trees(&[tree]));
629 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
630 app.world_mut().write_message(Order {
631 colonist,
632 tile: tree,
633 });
634 run_ticks(&mut app, 2);
635 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
636 run_ticks(&mut app, 30);
637 assert!(matches!(
638 job(&app, colonist),
639 Some(Job {
640 task: Task::Work { .. },
641 ordered: true
642 })
643 ));
644 }
645
646 fn beds(app: &mut App) -> usize {
647 structures(app)
648 .iter()
649 .filter(|(_, kind)| *kind == StructureKind::Bed)
650 .count()
651 }
652
653 #[test]
654 fn colonists_build_one_bed_each() {
655 let mut app = test_app(map_with_trees(&[]));
656 set_goals(&mut app, &[]);
657 let map = app.world().resource::<WorldMap>().clone();
658 app.world_mut()
659 .resource_mut::<Zones>()
660 .add_stockpile(&map, IVec2::new(2, 2), IVec2::new(5, 5));
661 put_wood(&mut app, UVec2::new(2, 2), 50);
662 put_wood(&mut app, UVec2::new(3, 2), 50);
663 spawn_colonist(&mut app, UVec2::new(10, 10));
664 spawn_colonist(&mut app, UVec2::new(11, 10));
665 run_ticks(&mut app, 60 * 40);
666 assert_eq!(beds(&mut app), 2);
667 assert_eq!(wood_on_map(&mut app), 100 - 2 * StructureKind::Bed.cost().1);
668 }
669
670 // A shelter needs a wall ring with walking space around the beds; beds hugging the stockpile
671 // would leave stored items where the walls must go and the room could never be planned.
672 #[test]
673 fn beds_keep_clear_of_the_stockpile() {
674 let mut app = test_app(map_with_trees(&[]));
675 set_goals(&mut app, &[]);
676 let map = app.world().resource::<WorldMap>().clone();
677 app.world_mut().resource_mut::<Zones>().add_stockpile(
678 &map,
679 IVec2::new(8, 8),
680 IVec2::new(11, 11),
681 );
682 put_wood(&mut app, UVec2::new(8, 8), 50);
683 spawn_colonist(&mut app, UVec2::new(10, 4));
684 run_ticks(&mut app, 60 * 20);
685 let built = structures(&mut app);
686 let bed = built
687 .iter()
688 .find(|(_, kind)| *kind == StructureKind::Bed)
689 .expect("a bed");
690 let zones = app.world().resource::<Zones>();
691 assert!(zones.stockpile_tiles().all(|s| {
692 (s.as_ivec2() - bed.0.as_ivec2()).abs().max_element() >= BED_STOCKPILE_CLEARANCE
693 }));
694 }
695
696 #[test]
697 fn colonists_sleep_in_their_beds() {
698 let mut app = test_app(map_with_trees(&[]));
699 set_goals(&mut app, &[]);
700 put_beds(&mut app, 1);
701 set_hour(&mut app, 22);
702 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
703 set_rest(&mut app, colonist, 0.5);
704 run_ticks(&mut app, 60 * 10);
705 assert_eq!(
706 app.world().get::<TilePos>(colonist).unwrap().0,
707 UVec2::new(19, 19)
708 );
709 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
710 }
711
712 fn put_items(app: &mut App, tile: UVec2, kind: ItemKind, count: u32) {
713 app.world_mut().spawn((Item { kind, count }, TilePos(tile)));
714 }
715
716 #[test]
717 fn blueprints_are_built_with_the_right_material() {
718 let mut app = test_app(map_with_trees(&[]));
719 set_goals(&mut app, &[]);
720 put_beds(&mut app, 1);
721 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 10);
722 let wall = UVec2::new(8, 8);
723 app.world_mut()
724 .resource_mut::<Blueprints>()
725 .0
726 .insert(wall, BuildKind::Structure(StructureKind::Wall));
727 spawn_colonist(&mut app, UVec2::new(5, 5));
728 run_ticks(&mut app, 60 * 10);
729 assert!(app.world().resource::<WorldMap>().is_wall(wall.as_ivec2()));
730 assert!(app.world().resource::<Blueprints>().0.is_empty());
731 assert_eq!(items_of(&mut app, ItemKind::Stone), 8);
732 }
733
734 #[test]
735 fn colonists_build_a_roofed_shelter_around_their_beds() {
736 let mut app = test_app(map_with_trees(&[]));
737 app.add_systems(Update, crate::building::plan_shelter.before(choose_jobs));
738 set_goals(&mut app, &[]);
739 for x in [10, 11] {
740 app.world_mut().spawn((
741 Structure {
742 kind: StructureKind::Bed,
743 },
744 TilePos(UVec2::new(x, 10)),
745 ));
746 }
747 put_items(&mut app, UVec2::new(2, 2), ItemKind::Stone, 50);
748 put_items(&mut app, UVec2::new(3, 2), ItemKind::Wood, 50);
749 spawn_colonist(&mut app, UVec2::new(4, 4));
750 spawn_colonist(&mut app, UVec2::new(5, 4));
751 run_ticks(&mut app, 60 * 150);
752 let map = app.world().resource::<WorldMap>();
753 assert!(map.is_roofed(IVec2::new(10, 10)) && map.is_roofed(IVec2::new(11, 10)));
754 let walls = map
755 .positions()
756 .filter(|p| map.is_wall(p.as_ivec2()))
757 .count();
758 assert_eq!(walls, 17);
759 assert!(
760 structures(&mut app)
761 .iter()
762 .any(|(_, kind)| *kind == StructureKind::Door)
763 );
764 }
765
766 #[test]
767 fn colonists_chop_from_a_neighbouring_tile() {
768 let tree = UVec2::new(8, 5);
769 let mut app = test_app(map_with_trees(&[tree]));
770 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
771 run_ticks(&mut app, 60 * 5);
772 let tile = app.world().get::<TilePos>(colonist).unwrap().0;
773 assert!(is_adjacent(tile.as_ivec2(), tree.as_ivec2()));
774 assert_eq!(
775 app.world().get::<SimPos>(colonist).unwrap().0,
776 WorldMap::tile_center(tile)
777 );
778 }
779
780 fn set_fed(app: &mut App, colonist: Entity, fed: f32) {
781 app.world_mut().get_mut::<Hunger>(colonist).unwrap().0 = fed;
782 }
783
784 fn fed(app: &App, colonist: Entity) -> f32 {
785 app.world().get::<Hunger>(colonist).unwrap().0
786 }
787
788 #[test]
789 fn hungry_colonists_eat_until_full() {
790 let mut app = test_app(map_with_trees(&[]));
791 set_goals(&mut app, &[]);
792 put_items(&mut app, UVec2::new(8, 5), ItemKind::Berries, 20);
793 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
794 set_fed(&mut app, colonist, 0.3);
795 run_ticks(&mut app, 60 * 6);
796 assert!(fed(&app, colonist) > 0.95);
797 assert_eq!(items_of(&mut app, ItemKind::Berries), 13);
798 }
799
800 #[test]
801 fn colonists_are_not_peckish_when_fed() {
802 let mut app = test_app(map_with_trees(&[]));
803 set_goals(&mut app, &[]);
804 put_items(&mut app, UVec2::new(8, 5), ItemKind::Berries, 20);
805 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
806 set_fed(&mut app, colonist, 0.8);
807 run_ticks(&mut app, 60 * 6);
808 assert_eq!(items_of(&mut app, ItemKind::Berries), 20);
809 }
810
811 #[test]
812 fn starving_colonists_drop_their_work_to_eat() {
813 let tree = UVec2::new(15, 5);
814 let mut app = test_app(map_with_trees(&[tree]));
815 put_items(&mut app, UVec2::new(2, 8), ItemKind::Meat, 20);
816 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
817 run_ticks(&mut app, 61);
818 assert!(task(&app, colonist).is_some_and(|t| !matches!(t, Task::Eat { .. })));
819 set_fed(&mut app, colonist, STARVING / 2.0);
820 run_ticks(&mut app, 30);
821 assert!(matches!(task(&app, colonist), Some(Task::Eat { .. })));
822 }
823
824 #[test]
825 fn colonists_forage_ripe_berries_when_the_colony_wants_them() {
826 let mut map = map_with_trees(&[]);
827 let ripe = UVec2::new(8, 5);
828 let unripe = UVec2::new(4, 5);
829 for (bush, growth) in [(ripe, 1.0), (unripe, 0.5)] {
830 map.set_plant(bush, Some(Plant::BerryBush));
831 map.set_growth(bush, growth);
832 }
833 let mut app = test_app(map);
834 set_goals(&mut app, &[(ItemKind::Berries, 30)]);
835 spawn_colonist(&mut app, UVec2::new(2, 5));
836 run_ticks(&mut app, 60 * 10);
837 assert_eq!(items_of(&mut app, ItemKind::Berries), 8);
838 let map = app.world().resource::<WorldMap>();
839 assert_eq!(map.plant(ripe.as_ivec2()), Some(Plant::BerryBush));
840 assert_eq!(map.growth(ripe.as_ivec2()), 0.0);
841 assert_eq!(map.growth(unripe.as_ivec2()), 0.5);
842 }