src/jobs/tests.rs 24 K · 777 lines · raw · history

1 use super::*;
2 use crate::{
3 colonist::follow_path,
4 items::STACK_LIMIT,
5 sim::{SimPos, sim_pos},
6 world::Terrain,
7 zones::NEW_STOCKPILE_SIZE,
8 };
9
10 const TICK: Duration = Duration::from_nanos(1_000_000_000 / 60);
11
12 fn test_app(map: WorldMap) -> App {
13 let mut app = App::new();
14 app.insert_resource(map)
15 .insert_resource(SimRng::new(1))
16 .init_resource::<Time>()
17 .init_resource::<Reservations>()
18 .init_resource::<ColonyGoals>()
19 .init_resource::<ItemAssets>()
20 .init_resource::<StructureAssets>()
21 .init_resource::<Blueprints>()
22 .init_resource::<LightMap>()
23 .init_resource::<SimTick>()
24 .init_resource::<Zones>()
25 .add_message::<Order>()
26 .add_message::<TileChanged>()
27 .add_systems(
28 Update,
29 (
30 handle_orders,
31 choose_jobs,
32 run_jobs,
33 follow_path,
34 crate::needs::update_rest,
35 )
36 .chain(),
37 );
38 app
39 }
40
41 fn spawn_colonist(app: &mut App, tile: UVec2) -> Entity {
42 app.world_mut()
43 .spawn((
44 Colonist {
45 name: "Test".into(),
46 },
47 TilePos(tile),
48 sim_pos(WorldMap::tile_center(tile)),
49 ))
50 .id()
51 }
52
53 fn run_ticks(app: &mut App, ticks: u32) {
54 for _ in 0..ticks {
55 app.world_mut().resource_mut::<Time>().advance_by(TICK);
56 app.update();
57 }
58 }
59
60 fn map_with_trees(trees: &[UVec2]) -> WorldMap {
61 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
62 for &t in trees {
63 map.set_plant(t, Some(Plant::Tree));
64 }
65 map
66 }
67
68 fn wood_on_map(app: &mut App) -> u32 {
69 let world = app.world_mut();
70 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
71 stock(items.iter(), ItemKind::Wood)
72 }
73
74 fn items_at(app: &mut App) -> Vec<(UVec2, u32)> {
75 let world = app.world_mut();
76 let mut items: Vec<(UVec2, u32)> = world
77 .query::<(&Item, &TilePos)>()
78 .iter(world)
79 .map(|(item, pos)| (pos.0, item.count))
80 .collect();
81 items.sort_by_key(|(p, _)| (p.y, p.x));
82 items
83 }
84
85 fn put_wood(app: &mut App, tile: UVec2, count: u32) {
86 app.world_mut().spawn((
87 Item {
88 kind: ItemKind::Wood,
89 count,
90 },
91 TilePos(tile),
92 ));
93 }
94
95 fn set_goals(app: &mut App, goals: &[(ItemKind, u32)]) {
96 *app.world_mut().resource_mut::<ColonyGoals>() = ColonyGoals(goals.iter().copied().collect());
97 }
98
99 fn stockpile_size(app: &App) -> usize {
100 let map = app.world().resource::<WorldMap>();
101 let zones = app.world().resource::<Zones>();
102 map.positions().filter(|&p| zones.is_stockpile(p)).count()
103 }
104
105 fn stored(app: &mut App) -> u32 {
106 let zones = app.world().resource::<Zones>().clone();
107 items_at(app)
108 .iter()
109 .filter(|(p, _)| zones.is_stockpile(*p))
110 .map(|(_, c)| c)
111 .sum()
112 }
113
114 fn put_beds(app: &mut App, count: u32) {
115 for x in 0..count {
116 app.world_mut().spawn((
117 Structure {
118 kind: StructureKind::Bed,
119 },
120 TilePos(UVec2::new(19 - x, 19)),
121 ));
122 }
123 }
124
125 fn hauling_app(stockpile: (IVec2, IVec2)) -> App {
126 let mut app = test_app(map_with_trees(&[]));
127 put_beds(&mut app, 2);
128 set_goals(&mut app, &[]);
129 let map = app.world().resource::<WorldMap>().clone();
130 app.world_mut()
131 .resource_mut::<Zones>()
132 .add_stockpile(&map, stockpile.0, stockpile.1);
133 app
134 }
135
136 fn carrying(app: &App, colonist: Entity) -> Option<Item> {
137 app.world().get::<Carrying>(colonist).unwrap().0
138 }
139
140 fn job(app: &App, colonist: Entity) -> Option<Job> {
141 app.world().get::<CurrentJob>(colonist).unwrap().0
142 }
143
144 #[test]
145 fn idle_colonists_cut_trees_when_the_colony_needs_wood() {
146 let tree = UVec2::new(8, 5);
147 let mut app = test_app(map_with_trees(&[tree]));
148 spawn_colonist(&mut app, UVec2::new(2, 5));
149 run_ticks(&mut app, 60 * 15);
150 assert_eq!(
151 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
152 None
153 );
154 assert_eq!(wood_on_map(&mut app), 10);
155 assert!(app.world().resource::<Reservations>().0.is_empty());
156 }
157
158 #[test]
159 fn colonists_leave_trees_alone_when_there_is_enough_wood() {
160 let tree = UVec2::new(8, 5);
161 let mut app = test_app(map_with_trees(&[tree]));
162 set_goals(&mut app, &[]);
163 spawn_colonist(&mut app, UVec2::new(2, 5));
164 run_ticks(&mut app, 60 * 15);
165 assert_eq!(
166 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
167 Some(Plant::Tree)
168 );
169 }
170
171 #[test]
172 fn colonists_never_pick_the_same_tree() {
173 let trees = [UVec2::new(10, 10), UVec2::new(10, 11)];
174 let mut app = test_app(map_with_trees(&trees));
175 let a = spawn_colonist(&mut app, UVec2::new(5, 10));
176 let b = spawn_colonist(&mut app, UVec2::new(5, 11));
177 run_ticks(&mut app, 61);
178 let target = |c| match job(&app, c) {
179 Some(Job {
180 task: Task::Work { site, .. },
181 ..
182 }) => site,
183 other => panic!("expected chop job, got {other:?}"),
184 };
185 assert_ne!(target(a), target(b));
186 }
187
188 // Each tree or rock being worked will add its yield; counting that as material on the way
189 // keeps the whole colony from felling a forest when only a little more wood is needed.
190 #[test]
191 fn work_in_progress_counts_towards_goals() {
192 let goals = ColonyGoals(HashMap::from([(ItemKind::Wood, 25)]));
193 assert!(goals.wanted(ItemKind::Wood, 10, 10));
194 assert!(!goals.wanted(ItemKind::Wood, 10, 20));
195 assert!(!goals.wanted(ItemKind::Wood, 30, 0));
196 assert!(!goals.wanted(ItemKind::Gold, 0, 0));
197 }
198
199 #[test]
200 fn ordered_chop_ignores_the_wood_goal() {
201 let tree = UVec2::new(8, 5);
202 let mut app = test_app(map_with_trees(&[tree]));
203 set_goals(&mut app, &[]);
204 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
205 app.world_mut().write_message(Order {
206 colonist,
207 tile: tree,
208 });
209 run_ticks(&mut app, 60 * 15);
210 assert_eq!(
211 app.world().resource::<WorldMap>().plant(tree.as_ivec2()),
212 None
213 );
214 }
215
216 #[test]
217 fn ordering_a_move_cancels_the_current_job_and_frees_its_tree() {
218 let tree = UVec2::new(15, 5);
219 let mut app = test_app(map_with_trees(&[tree]));
220 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
221 run_ticks(&mut app, 61);
222 assert!(matches!(
223 job(&app, colonist),
224 Some(Job {
225 task: Task::Work { .. },
226 ..
227 })
228 ));
229 app.world_mut().write_message(Order {
230 colonist,
231 tile: UVec2::new(2, 15),
232 });
233 run_ticks(&mut app, 1);
234 assert_eq!(
235 job(&app, colonist).map(|j| j.task),
236 Some(Task::MoveTo(UVec2::new(2, 15)))
237 );
238 assert!(
239 app.world()
240 .resource::<Reservations>()
241 .is_free(Target::Site(tree))
242 );
243 }
244
245 #[test]
246 fn idle_colonists_haul_loose_items_into_the_stockpile() {
247 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
248 put_wood(&mut app, UVec2::new(5, 5), 20);
249 spawn_colonist(&mut app, UVec2::new(2, 5));
250 run_ticks(&mut app, 60 * 15);
251 assert_eq!(items_at(&mut app), vec![(UVec2::new(15, 15), 20)]);
252 }
253
254 #[test]
255 fn large_stacks_take_several_trips_and_spill_into_more_stockpile_tiles() {
256 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
257 put_wood(&mut app, UVec2::new(5, 5), STACK_LIMIT + 10);
258 spawn_colonist(&mut app, UVec2::new(2, 5));
259 run_ticks(&mut app, 60 * 40);
260 let stacks = items_at(&mut app);
261 assert_eq!(stored(&mut app), STACK_LIMIT + 10);
262 assert!(stacks.len() >= 2);
263 assert!(stacks.iter().all(|(_, count)| *count <= STACK_LIMIT));
264 }
265
266 #[test]
267 fn items_already_in_a_stockpile_stay_put() {
268 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(16, 16)));
269 put_wood(&mut app, UVec2::new(5, 5), 20);
270 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
271 run_ticks(&mut app, 60 * 5);
272 assert_eq!(items_at(&mut app), vec![(UVec2::new(5, 5), 20)]);
273 assert_eq!(carrying(&app, colonist), None);
274 }
275
276 #[test]
277 fn colonists_found_a_stockpile_when_items_need_storing() {
278 let mut app = test_app(map_with_trees(&[]));
279 put_beds(&mut app, 1);
280 set_goals(&mut app, &[]);
281 put_wood(&mut app, UVec2::new(5, 5), 20);
282 spawn_colonist(&mut app, UVec2::new(2, 5));
283 run_ticks(&mut app, 60 * 15);
284 assert_eq!(
285 stockpile_size(&app),
286 (NEW_STOCKPILE_SIZE * NEW_STOCKPILE_SIZE) as usize
287 );
288 assert_eq!(stored(&mut app), 20);
289 }
290
291 #[test]
292 fn no_stockpile_is_founded_while_there_is_nothing_to_store() {
293 let mut app = test_app(map_with_trees(&[]));
294 set_goals(&mut app, &[]);
295 spawn_colonist(&mut app, UVec2::new(2, 5));
296 run_ticks(&mut app, 60 * 5);
297 assert_eq!(stockpile_size(&app), 0);
298 }
299
300 #[test]
301 fn two_colonists_never_haul_the_same_stack() {
302 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(16, 16)));
303 put_wood(&mut app, UVec2::new(5, 5), 10);
304 put_wood(&mut app, UVec2::new(5, 6), 10);
305 let a = spawn_colonist(&mut app, UVec2::new(2, 5));
306 let b = spawn_colonist(&mut app, UVec2::new(2, 6));
307 run_ticks(&mut app, 61);
308 let pickup = |c| match job(&app, c).map(|j| j.task) {
309 Some(Task::Haul { pickup, .. }) => pickup,
310 other => panic!("expected haul, got {other:?}"),
311 };
312 assert_ne!(pickup(a), pickup(b));
313 }
314
315 // Orders interrupt whatever a colonist is doing, but whatever it is carrying stays in its
316 // hands; once it is idle again it finishes delivering it instead of dropping it anywhere.
317 #[test]
318 fn interrupted_haulers_deliver_their_load_later() {
319 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(17, 15)));
320 put_wood(&mut app, UVec2::new(4, 5), 20);
321 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
322 let mut ticks = 0;
323 while carrying(&app, colonist).is_none() && ticks < 60 * 10 {
324 run_ticks(&mut app, 1);
325 ticks += 1;
326 }
327 app.world_mut().write_message(Order {
328 colonist,
329 tile: UVec2::new(2, 2),
330 });
331 run_ticks(&mut app, 60 * 3);
332 assert!(carrying(&app, colonist).is_some());
333 run_ticks(&mut app, 60 * 20);
334 assert_eq!(carrying(&app, colonist), None);
335 assert_eq!(stored(&mut app), 20);
336 }
337
338 #[test]
339 fn right_clicking_loose_items_orders_a_haul() {
340 let mut app = hauling_app((IVec2::new(15, 15), IVec2::new(15, 15)));
341 set_goals(&mut app, &[(ItemKind::Wood, 1000)]);
342 put_wood(&mut app, UVec2::new(5, 5), 20);
343 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
344 app.world_mut().write_message(Order {
345 colonist,
346 tile: UVec2::new(5, 5),
347 });
348 run_ticks(&mut app, 1);
349 assert!(matches!(
350 job(&app, colonist),
351 Some(Job {
352 task: Task::Haul { .. },
353 ordered: true
354 })
355 ));
356 }
357
358 fn mining_map() -> WorldMap {
359 let mut map = WorldMap::filled(UVec2::new(20, 20), Terrain::Grass);
360 for y in 0..20 {
361 map.set(UVec2::new(10, y), Terrain::Stone);
362 }
363 map.set(UVec2::new(10, 5), Terrain::IronOre);
364 map
365 }
366
367 fn items_of(app: &mut App, kind: ItemKind) -> u32 {
368 let world = app.world_mut();
369 let items: Vec<Item> = world.query::<&Item>().iter(world).copied().collect();
370 stock(items.iter(), kind)
371 }
372
373 #[test]
374 fn colonists_mine_rock_the_colony_needs_and_leave_gravel() {
375 let mut app = test_app(mining_map());
376 set_goals(&mut app, &[(ItemKind::Stone, 10)]);
377 spawn_colonist(&mut app, UVec2::new(8, 12));
378 run_ticks(&mut app, 60 * 12);
379 assert_eq!(items_of(&mut app, ItemKind::Stone), 10);
380 assert_eq!(items_of(&mut app, ItemKind::Iron), 0);
381 let map = app.world().resource::<WorldMap>();
382 let gravel = map
383 .positions()
384 .filter(|p| map.get(p.as_ivec2()) == Some(Terrain::Gravel))
385 .count();
386 assert_eq!(gravel, 1);
387 }
388
389 #[test]
390 fn colonists_walk_past_rock_they_do_not_need() {
391 let mut app = test_app(mining_map());
392 set_goals(&mut app, &[(ItemKind::Iron, 10)]);
393 spawn_colonist(&mut app, UVec2::new(8, 15));
394 run_ticks(&mut app, 60 * 15);
395 assert_eq!(items_of(&mut app, ItemKind::Iron), 10);
396 assert_eq!(items_of(&mut app, ItemKind::Stone), 0);
397 assert_eq!(
398 app.world().resource::<WorldMap>().get(IVec2::new(10, 5)),
399 Some(Terrain::Gravel)
400 );
401 }
402
403 #[test]
404 fn right_clicking_rock_orders_mining_regardless_of_goals() {
405 let mut app = test_app(mining_map());
406 set_goals(&mut app, &[]);
407 let colonist = spawn_colonist(&mut app, UVec2::new(8, 12));
408 app.world_mut().write_message(Order {
409 colonist,
410 tile: UVec2::new(10, 12),
411 });
412 run_ticks(&mut app, 60 * 10);
413 assert_eq!(
414 app.world().resource::<WorldMap>().get(IVec2::new(10, 12)),
415 Some(Terrain::Gravel)
416 );
417 }
418
419 #[test]
420 fn colonists_expand_a_stockpile_that_is_nearly_full() {
421 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(6, 5)));
422 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
423 run_ticks(&mut app, 61);
424 assert!(matches!(
425 job(&app, colonist).map(|j| j.task),
426 Some(Task::ExpandStockpile { .. })
427 ));
428 run_ticks(&mut app, 60 * 5);
429 assert_eq!(stockpile_size(&app), 2 + EXPANSION_TILES);
430 }
431
432 #[test]
433 fn roomy_stockpiles_are_left_alone() {
434 let mut app = hauling_app((IVec2::new(5, 5), IVec2::new(8, 5)));
435 spawn_colonist(&mut app, UVec2::new(2, 5));
436 run_ticks(&mut app, 60 * 5);
437 assert_eq!(stockpile_size(&app), 4);
438 }
439
440 fn set_hour(app: &mut App, hour: u64) {
441 let ticks = (hour + 24 - 6) % 24 * crate::sim::TICKS_PER_HOUR;
442 *app.world_mut().resource_mut::<SimTick>() = SimTick(ticks);
443 }
444
445 fn structures(app: &mut App) -> Vec<(UVec2, StructureKind)> {
446 let world = app.world_mut();
447 world
448 .query::<(&TilePos, &Structure)>()
449 .iter(world)
450 .map(|(pos, s)| (pos.0, s.kind))
451 .collect()
452 }
453
454 fn put_campfire(app: &mut App, tile: UVec2) {
455 app.world_mut().spawn((
456 Structure {
457 kind: StructureKind::Campfire,
458 },
459 Fuel(StructureKind::Campfire.fuel_ticks().unwrap()),
460 TilePos(tile),
461 ));
462 }
463
464 #[test]
465 fn colonists_light_a_campfire_near_the_stockpile_when_night_falls() {
466 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
467 put_wood(&mut app, UVec2::new(10, 10), 20);
468 set_hour(&mut app, 18);
469 spawn_colonist(&mut app, UVec2::new(2, 5));
470 run_ticks(&mut app, 60 * 20);
471 let built = structures(&mut app);
472 let campfire = built
473 .iter()
474 .find(|(_, kind)| *kind == StructureKind::Campfire)
475 .expect("a campfire");
476 assert!(campfire.0.as_vec2().distance(Vec2::new(11.0, 11.0)) <= HOME_RADIUS);
477 assert_eq!(wood_on_map(&mut app), 10);
478 }
479
480 #[test]
481 fn no_fires_are_built_during_the_day() {
482 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
483 put_wood(&mut app, UVec2::new(10, 10), 20);
484 set_hour(&mut app, 10);
485 spawn_colonist(&mut app, UVec2::new(2, 5));
486 run_ticks(&mut app, 60 * 10);
487 assert!(
488 structures(&mut app)
489 .iter()
490 .all(|(_, kind)| kind.light().is_none())
491 );
492 }
493
494 #[test]
495 fn without_wood_nothing_can_be_built() {
496 let mut app = hauling_app((IVec2::new(10, 10), IVec2::new(12, 12)));
497 set_hour(&mut app, 22);
498 spawn_colonist(&mut app, UVec2::new(2, 5));
499 run_ticks(&mut app, 60 * 10);
500 assert!(
501 structures(&mut app)
502 .iter()
503 .all(|(_, kind)| kind.light().is_none())
504 );
505 }
506
507 #[test]
508 fn torches_light_stockpile_tiles_beyond_the_campfire() {
509 let mut app = hauling_app((IVec2::new(2, 10), IVec2::new(17, 10)));
510 put_wood(&mut app, UVec2::new(9, 10), 20);
511 put_campfire(&mut app, UVec2::new(9, 12));
512 set_hour(&mut app, 22);
513 spawn_colonist(&mut app, UVec2::new(9, 8));
514 run_ticks(&mut app, 60 * 15);
515 assert!(
516 structures(&mut app)
517 .iter()
518 .any(|(_, kind)| *kind == StructureKind::Torch)
519 );
520 }
521
522 // Light is what makes fires worth building: work in darkness goes at half speed.
523 #[test]
524 fn work_is_slower_in_the_dark() {
525 let tree = UVec2::new(8, 5);
526 let finish_ticks = |daylight: f32| {
527 let mut app = test_app(map_with_trees(&[tree]));
528 let size = app.world().resource::<WorldMap>().size;
529 app.insert_resource(LightMap::compute(size, daylight, []));
530 spawn_colonist(&mut app, UVec2::new(7, 5));
531 let mut ticks = 0;
532 while app
533 .world()
534 .resource::<WorldMap>()
535 .plant(tree.as_ivec2())
536 .is_some()
537 {
538 run_ticks(&mut app, 1);
539 ticks += 1;
540 }
541 ticks
542 };
543 let (lit, dark) = (finish_ticks(1.0), finish_ticks(0.0));
544 assert!(
545 dark > lit + WorkKind::Chop.work() / 2 - 10,
546 "lit {lit}, dark {dark}"
547 );
548 }
549
550 const NAP_TEST_REST: f32 = 0.25;
551
552 fn set_rest(app: &mut App, colonist: Entity, rest: f32) {
553 app.world_mut().get_mut::<Rest>(colonist).unwrap().0 = rest;
554 }
555
556 fn rest(app: &App, colonist: Entity) -> f32 {
557 app.world().get::<Rest>(colonist).unwrap().0
558 }
559
560 fn task(app: &App, colonist: Entity) -> Option<Task> {
561 job(app, colonist).map(|j| j.task)
562 }
563
564 #[test]
565 fn tired_colonists_nap_and_get_up_when_rested() {
566 let mut app = test_app(map_with_trees(&[]));
567 set_goals(&mut app, &[]);
568 set_hour(&mut app, 10);
569 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
570 set_rest(&mut app, colonist, NAP_TEST_REST);
571 run_ticks(&mut app, 61);
572 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
573 let mut ticks = 0;
574 while matches!(task(&app, colonist), Some(Task::Sleep { .. })) && ticks < 60 * 60 * 10 {
575 run_ticks(&mut app, 60);
576 ticks += 60;
577 }
578 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
579 assert!(rest(&app, colonist) > 0.95);
580 }
581
582 #[test]
583 fn colonists_go_to_bed_at_night_and_sleep_until_morning() {
584 let mut app = test_app(map_with_trees(&[]));
585 set_goals(&mut app, &[]);
586 set_hour(&mut app, 22);
587 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
588 set_rest(&mut app, colonist, 0.8);
589 run_ticks(&mut app, 61);
590 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
591 run_ticks(&mut app, 3 * crate::sim::TICKS_PER_HOUR as u32);
592 assert_eq!(rest(&app, colonist), 1.0);
593 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
594 }
595
596 #[test]
597 fn rested_colonists_stay_up_during_the_day() {
598 let mut app = test_app(map_with_trees(&[]));
599 set_goals(&mut app, &[]);
600 set_hour(&mut app, 12);
601 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
602 set_rest(&mut app, colonist, 0.8);
603 run_ticks(&mut app, 60 * 5);
604 assert!(!matches!(task(&app, colonist), Some(Task::Sleep { .. })));
605 }
606
607 #[test]
608 fn exhausted_colonists_drop_their_work_to_sleep() {
609 let tree = UVec2::new(15, 5);
610 let mut app = test_app(map_with_trees(&[tree]));
611 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
612 run_ticks(&mut app, 61);
613 assert!(matches!(task(&app, colonist), Some(Task::Work { .. })));
614 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
615 run_ticks(&mut app, 30);
616 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
617 assert!(
618 app.world()
619 .resource::<Reservations>()
620 .is_free(Target::Site(tree))
621 );
622 }
623
624 #[test]
625 fn exhaustion_does_not_override_orders() {
626 let tree = UVec2::new(15, 5);
627 let mut app = test_app(map_with_trees(&[tree]));
628 let colonist = spawn_colonist(&mut app, UVec2::new(2, 5));
629 app.world_mut().write_message(Order {
630 colonist,
631 tile: tree,
632 });
633 run_ticks(&mut app, 2);
634 set_rest(&mut app, colonist, EXHAUSTED_REST / 2.0);
635 run_ticks(&mut app, 30);
636 assert!(matches!(
637 job(&app, colonist),
638 Some(Job {
639 task: Task::Work { .. },
640 ordered: true
641 })
642 ));
643 }
644
645 fn beds(app: &mut App) -> usize {
646 structures(app)
647 .iter()
648 .filter(|(_, kind)| *kind == StructureKind::Bed)
649 .count()
650 }
651
652 #[test]
653 fn colonists_build_one_bed_each() {
654 let mut app = test_app(map_with_trees(&[]));
655 set_goals(&mut app, &[]);
656 let map = app.world().resource::<WorldMap>().clone();
657 app.world_mut()
658 .resource_mut::<Zones>()
659 .add_stockpile(&map, IVec2::new(2, 2), IVec2::new(5, 5));
660 put_wood(&mut app, UVec2::new(2, 2), 50);
661 put_wood(&mut app, UVec2::new(3, 2), 50);
662 spawn_colonist(&mut app, UVec2::new(10, 10));
663 spawn_colonist(&mut app, UVec2::new(11, 10));
664 run_ticks(&mut app, 60 * 40);
665 assert_eq!(beds(&mut app), 2);
666 assert_eq!(wood_on_map(&mut app), 100 - 2 * StructureKind::Bed.cost().1);
667 }
668
669 // A shelter needs a wall ring with walking space around the beds; beds hugging the stockpile
670 // would leave stored items where the walls must go and the room could never be planned.
671 #[test]
672 fn beds_keep_clear_of_the_stockpile() {
673 let mut app = test_app(map_with_trees(&[]));
674 set_goals(&mut app, &[]);
675 let map = app.world().resource::<WorldMap>().clone();
676 app.world_mut().resource_mut::<Zones>().add_stockpile(
677 &map,
678 IVec2::new(8, 8),
679 IVec2::new(11, 11),
680 );
681 put_wood(&mut app, UVec2::new(8, 8), 50);
682 spawn_colonist(&mut app, UVec2::new(10, 4));
683 run_ticks(&mut app, 60 * 20);
684 let built = structures(&mut app);
685 let bed = built
686 .iter()
687 .find(|(_, kind)| *kind == StructureKind::Bed)
688 .expect("a bed");
689 let zones = app.world().resource::<Zones>();
690 assert!(zones.stockpile_tiles().all(|s| {
691 (s.as_ivec2() - bed.0.as_ivec2()).abs().max_element() >= BED_STOCKPILE_CLEARANCE
692 }));
693 }
694
695 #[test]
696 fn colonists_sleep_in_their_beds() {
697 let mut app = test_app(map_with_trees(&[]));
698 set_goals(&mut app, &[]);
699 put_beds(&mut app, 1);
700 set_hour(&mut app, 22);
701 let colonist = spawn_colonist(&mut app, UVec2::new(5, 5));
702 set_rest(&mut app, colonist, 0.5);
703 run_ticks(&mut app, 60 * 10);
704 assert_eq!(
705 app.world().get::<TilePos>(colonist).unwrap().0,
706 UVec2::new(19, 19)
707 );
708 assert!(matches!(task(&app, colonist), Some(Task::Sleep { .. })));
709 }
710
711 fn put_items(app: &mut App, tile: UVec2, kind: ItemKind, count: u32) {
712 app.world_mut().spawn((Item { 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 }