src/family.rs 21.8 K · 683 lines · raw · history

1 use serde::{Deserialize, Serialize};
2 use std::collections::{HashMap, HashSet};
3
4 use bevy::prelude::*;
5
6 use crate::{
7 building::{Blueprints, BuildKind, enclosed_room},
8 colonist::{Colonist, ColonistAssets, NAMES, spawn_colonist},
9 economy::in_colony,
10 items::{Item, ItemAssets},
11 pathfinding::nearest_tile,
12 population::{ColonyLog, MAX_COLONISTS, food_in_store},
13 rng::SimRng,
14 sim::{SimSystems, SimTick, TICKS_PER_HOUR},
15 structures::{Structure, StructureKind},
16 world::{TilePos, WorldMap},
17 worldgen::WorldGenerated,
18 zones::{Zones, can_zone},
19 };
20
21 pub struct FamilyPlugin;
22
23 impl Plugin for FamilyPlugin {
24 fn build(&self, app: &mut App) {
25 app.init_resource::<Homes>()
26 .init_resource::<Affinity>()
27 .add_systems(Update, clear_families.run_if(on_message::<WorldGenerated>))
28 .add_systems(
29 FixedUpdate,
30 (
31 plan_homes.in_set(SimSystems::Decide),
32 (bond, births, grow_up).chain().in_set(SimSystems::Act),
33 ),
34 );
35 }
36 }
37
38 pub const HOME_INTERIOR: UVec2 = UVec2::new(4, 3);
39 pub const HOME_MIN_DISTANCE: i32 = 6;
40 pub const BOND_RANGE: i32 = 4;
41 pub const BOND_HOURS: u32 = 16;
42 pub const BOND_CHANCE: f32 = 0.2;
43 pub const MAX_CHILDREN: usize = 2;
44 pub const BIRTH_CHANCE: f32 = 0.35;
45 pub const CHILDHOOD_TICKS: u64 = 4 * 24 * TICKS_PER_HOUR;
46 pub const CHILD_SCALE: f32 = 0.6;
47 const BIRTH_HOUR_TICK: u64 = 20 * TICKS_PER_HOUR;
48 const DAY_TICKS: u64 = 24 * TICKS_PER_HOUR;
49 const HOME_PLAN_INTERVAL_TICKS: u64 = 300;
50 const HOME_SEARCH_TILES: usize = 6000;
51 const HOME_CLEARANCE: i32 = 1;
52
53 #[derive(Component, Clone, Copy, Debug, PartialEq, Eq, Hash)]
54 pub struct Family(pub u32);
55
56 #[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
57 pub struct Partner(pub Entity);
58
59 #[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
60 pub struct Child {
61 pub born: u64,
62 }
63
64 impl Child {
65 pub fn days_left(&self, tick: SimTick) -> u64 {
66 (self.born + CHILDHOOD_TICKS)
67 .saturating_sub(tick.0)
68 .div_ceil(DAY_TICKS)
69 }
70 }
71
72 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
73 pub struct Home {
74 pub lo: UVec2,
75 pub hi: UVec2,
76 }
77
78 impl Home {
79 pub fn at(lo: UVec2) -> Self {
80 Self {
81 lo,
82 hi: lo + HOME_INTERIOR + UVec2::ONE,
83 }
84 }
85
86 pub fn contains(&self, tile: UVec2) -> bool {
87 tile.cmpge(self.lo).all() && tile.cmple(self.hi).all()
88 }
89
90 pub fn interior(&self) -> impl Iterator<Item = UVec2> + use<> {
91 let (lo, hi) = (self.lo + UVec2::ONE, self.hi - UVec2::ONE);
92 (lo.y..=hi.y).flat_map(move |y| (lo.x..=hi.x).map(move |x| UVec2::new(x, y)))
93 }
94
95 pub fn ring(&self) -> impl Iterator<Item = UVec2> + use<> {
96 let (lo, hi) = (self.lo, self.hi);
97 (lo.y..=hi.y)
98 .flat_map(move |y| (lo.x..=hi.x).map(move |x| UVec2::new(x, y)))
99 .filter(move |t| t.x == lo.x || t.x == hi.x || t.y == lo.y || t.y == hi.y)
100 }
101
102 pub fn bed_tiles(&self) -> [UVec2; 2] {
103 let corner = UVec2::new(self.lo.x + 1, self.hi.y - 1);
104 [corner, corner + UVec2::X]
105 }
106
107 pub fn statue_tile(&self) -> UVec2 {
108 UVec2::new(self.hi.x - 1, self.lo.y + 1)
109 }
110
111 pub fn blueprints(&self, toward: UVec2) -> HashMap<UVec2, BuildKind> {
112 let corner = |t: &UVec2| {
113 (t.x == self.lo.x || t.x == self.hi.x) && (t.y == self.lo.y || t.y == self.hi.y)
114 };
115 let door = self
116 .ring()
117 .filter(|t| !corner(t))
118 .min_by(|a, b| {
119 a.as_vec2()
120 .distance_squared(toward.as_vec2())
121 .total_cmp(&b.as_vec2().distance_squared(toward.as_vec2()))
122 .then((a.y, a.x).cmp(&(b.y, b.x)))
123 })
124 .unwrap_or(self.lo);
125 let mut plan: HashMap<UVec2, BuildKind> = self
126 .ring()
127 .map(|t| (t, BuildKind::Structure(StructureKind::Wall)))
128 .collect();
129 plan.insert(door, BuildKind::Structure(StructureKind::Door));
130 for bed in self.bed_tiles() {
131 plan.insert(bed, BuildKind::Structure(StructureKind::Bed));
132 }
133 plan
134 }
135 }
136
137 #[derive(Resource, Default, Debug, Clone, Serialize, Deserialize)]
138 pub struct Homes {
139 pub by_family: HashMap<u32, Home>,
140 next_family: u32,
141 }
142
143 impl Homes {
144 pub fn new_family(&mut self) -> Family {
145 self.next_family += 1;
146 Family(self.next_family)
147 }
148
149 pub fn owner(&self, tile: UVec2) -> Option<u32> {
150 self.by_family
151 .iter()
152 .find(|(_, home)| home.contains(tile))
153 .map(|(family, _)| *family)
154 }
155
156 pub fn of(&self, family: Option<Family>) -> Option<&Home> {
157 self.by_family.get(&family?.0)
158 }
159 }
160
161 #[derive(Resource, Default, Debug)]
162 pub struct Affinity(pub HashMap<(Entity, Entity), u32>);
163
164 fn clear_families(mut homes: ResMut<Homes>, mut affinity: ResMut<Affinity>) {
165 homes.by_family.clear();
166 affinity.0.clear();
167 }
168
169 #[derive(Clone, Copy)]
170 pub struct Single {
171 pub entity: Entity,
172 pub tile: UVec2,
173 pub family: Option<Family>,
174 }
175
176 pub fn bond_hour(
177 affinity: &mut Affinity,
178 singles: &[Single],
179 rng: &mut SimRng,
180 ) -> Vec<(Entity, Entity)> {
181 let mut bonded: Vec<(Entity, Entity)> = Vec::new();
182 let taken =
183 |bonded: &[(Entity, Entity)], e: Entity| bonded.iter().any(|&(a, b)| a == e || b == e);
184 for (i, a) in singles.iter().enumerate() {
185 for b in &singles[i + 1..] {
186 let near = (a.tile.as_ivec2() - b.tile.as_ivec2()).abs().max_element() <= BOND_RANGE;
187 let kin = a.family.is_some() && a.family == b.family;
188 if !near || kin {
189 continue;
190 }
191 let key = (a.entity.min(b.entity), a.entity.max(b.entity));
192 let hours = affinity.0.entry(key).or_insert(0);
193 *hours += 1;
194 if *hours >= BOND_HOURS
195 && !taken(&bonded, a.entity)
196 && !taken(&bonded, b.entity)
197 && rng.range_f32(0.0..1.0) < BOND_CHANCE
198 {
199 bonded.push(key);
200 }
201 }
202 }
203 for pair in &bonded {
204 affinity
205 .0
206 .retain(|&(a, b), _| ![a, b].iter().any(|e| *e == pair.0 || *e == pair.1));
207 }
208 bonded
209 }
210
211 #[allow(clippy::type_complexity)]
212 fn bond(
213 mut commands: Commands,
214 tick: Res<SimTick>,
215 mut rng: ResMut<SimRng>,
216 mut homes: ResMut<Homes>,
217 mut affinity: ResMut<Affinity>,
218 mut log: ResMut<ColonyLog>,
219 colonists: Query<(
220 Entity,
221 &Colonist,
222 &TilePos,
223 Option<&Family>,
224 Option<&Partner>,
225 Has<Child>,
226 )>,
227 ) {
228 if !tick.0.is_multiple_of(TICKS_PER_HOUR) {
229 return;
230 }
231 for (entity, _, _, _, partner, _) in &colonists {
232 if let Some(partner) = partner
233 && !colonists.contains(partner.0)
234 {
235 commands.entity(entity).remove::<Partner>();
236 }
237 }
238 let singles: Vec<Single> = colonists
239 .iter()
240 .filter(|(.., partner, child)| partner.is_none() && !child)
241 .map(|(entity, _, tile, family, ..)| Single {
242 entity,
243 tile: tile.0,
244 family: family.copied(),
245 })
246 .collect();
247 for (a, b) in bond_hour(&mut affinity, &singles, &mut rng) {
248 let family = homes.new_family();
249 commands.entity(a).insert((Partner(b), family));
250 commands.entity(b).insert((Partner(a), family));
251 let name = |e: Entity| {
252 colonists
253 .get(e)
254 .map(|c| c.1.name.clone())
255 .unwrap_or_default()
256 };
257 log.push(
258 *tick,
259 format!("{} and {} became partners", name(a), name(b)),
260 );
261 }
262 }
263
264 pub struct HomeContext<'a> {
265 pub map: &'a WorldMap,
266 pub zones: &'a Zones,
267 pub blueprints: &'a Blueprints,
268 pub homes: &'a Homes,
269 pub occupied: &'a HashSet<UVec2>,
270 }
271
272 impl HomeContext<'_> {
273 fn claimed(&self, tile: UVec2) -> bool {
274 self.zones.is_stockpile(tile)
275 || self.zones.is_farm(tile)
276 || self.blueprints.get(tile).is_some()
277 || self.homes.owner(tile).is_some()
278 }
279
280 fn open(&self, tile: IVec2) -> bool {
281 self.map.in_bounds(tile)
282 && can_zone(self.map, tile)
283 && self.map.plant(tile).is_none()
284 && !self.map.is_wall(tile)
285 && !self.occupied.contains(&tile.as_uvec2())
286 && !self.claimed(tile.as_uvec2())
287 }
288
289 fn fits(&self, home: Home) -> bool {
290 let (lo, hi) = (home.lo.as_ivec2(), home.hi.as_ivec2());
291 let (outer_lo, outer_hi) = (lo - HOME_CLEARANCE, hi + HOME_CLEARANCE);
292 (outer_lo.y..=outer_hi.y).all(|y| {
293 (outer_lo.x..=outer_hi.x).all(|x| {
294 let tile = IVec2::new(x, y);
295 let core = tile.cmpge(lo).all() && tile.cmple(hi).all();
296 if core {
297 self.open(tile)
298 } else {
299 !self.map.in_bounds(tile) || !self.claimed(tile.as_uvec2())
300 }
301 })
302 })
303 }
304
305 pub fn plan(&self, colony_home: UVec2) -> Option<Home> {
306 let center = (HOME_INTERIOR / 2 + UVec2::ONE).as_ivec2();
307 let mut found = None;
308 nearest_tile(self.map, colony_home, HOME_SEARCH_TILES, |p| {
309 let lo = p.as_ivec2() - center;
310 if lo.min_element() < 0 {
311 return false;
312 }
313 let home = Home::at(lo.as_uvec2());
314 let middle = (home.lo + home.hi).as_ivec2() / 2;
315 if (middle - colony_home.as_ivec2()).abs().max_element() < HOME_MIN_DISTANCE {
316 return false;
317 }
318 found = self.fits(home).then_some(home);
319 found.is_some()
320 })?;
321 found
322 }
323 }
324
325 pub fn roofs_needed(map: &WorldMap, doors: &HashSet<UVec2>, home: &Home) -> Vec<UVec2> {
326 let interior: Vec<UVec2> = home.interior().collect();
327 let Some(room) = enclosed_room(map, doors, interior[0]) else {
328 return Vec::new();
329 };
330 if room.len() != interior.len() {
331 return Vec::new();
332 }
333 interior
334 .into_iter()
335 .filter(|t| !map.is_roofed(t.as_ivec2()))
336 .collect()
337 }
338
339 #[allow(clippy::too_many_arguments)]
340 fn plan_homes(
341 tick: Res<SimTick>,
342 map: Res<WorldMap>,
343 zones: Res<Zones>,
344 mut homes: ResMut<Homes>,
345 mut blueprints: ResMut<Blueprints>,
346 structures: Query<(&TilePos, &Structure), Without<Colonist>>,
347 items: Query<&TilePos, With<Item>>,
348 families: Query<(&Family, Has<Partner>), With<Colonist>>,
349 ) {
350 if !tick.0.is_multiple_of(HOME_PLAN_INTERVAL_TICKS) {
351 return;
352 }
353 let living: HashSet<u32> = families.iter().map(|(f, _)| f.0).collect();
354 homes.by_family.retain(|family, _| living.contains(family));
355 let doors: HashSet<UVec2> = structures
356 .iter()
357 .filter(|(_, s)| s.kind == StructureKind::Door)
358 .map(|(pos, _)| pos.0)
359 .collect();
360 let statues: HashSet<UVec2> = structures
361 .iter()
362 .filter(|(_, s)| s.kind == StructureKind::Statue)
363 .map(|(pos, _)| pos.0)
364 .collect();
365 for home in homes.by_family.values() {
366 if blueprints.0.keys().any(|t| home.contains(*t)) {
367 continue;
368 }
369 let roofs = roofs_needed(&map, &doors, home);
370 let built = roofs.is_empty() && home.interior().all(|t| map.is_roofed(t.as_ivec2()));
371 for tile in roofs {
372 blueprints.0.insert(tile, BuildKind::Roof);
373 }
374 if built && !statues.iter().any(|&t| home.contains(t)) {
375 let statue = BuildKind::Structure(StructureKind::Statue);
376 blueprints.0.insert(home.statue_tile(), statue);
377 }
378 }
379 let Some(family) = families
380 .iter()
381 .filter(|(f, partnered)| *partnered && !homes.by_family.contains_key(&f.0))
382 .map(|(f, _)| f.0)
383 .min()
384 else {
385 return;
386 };
387 let occupied: HashSet<UVec2> = structures
388 .iter()
389 .map(|(pos, _)| pos.0)
390 .chain(items.iter().map(|pos| pos.0))
391 .collect();
392 let colony_home = zones.home(map.size / 2);
393 let ctx = HomeContext {
394 map: &map,
395 zones: &zones,
396 blueprints: &blueprints,
397 homes: &homes,
398 occupied: &occupied,
399 };
400 if let Some(home) = ctx.plan(colony_home) {
401 blueprints.0.extend(home.blueprints(colony_home));
402 homes.by_family.insert(family, home);
403 }
404 }
405
406 #[allow(clippy::too_many_arguments, clippy::type_complexity)]
407 fn births(
408 mut commands: Commands,
409 tick: Res<SimTick>,
410 map: Res<WorldMap>,
411 zones: Res<Zones>,
412 homes: Res<Homes>,
413 assets: Res<ColonistAssets>,
414 item_assets: Res<ItemAssets>,
415 mut materials: ResMut<Assets<ColorMaterial>>,
416 mut rng: ResMut<SimRng>,
417 mut log: ResMut<ColonyLog>,
418 items: Query<(&Item, &TilePos)>,
419 structures: Query<(&TilePos, &Structure), Without<Colonist>>,
420 colonists: Query<(&Colonist, Option<&Family>, Has<Partner>, Has<Child>)>,
421 ) {
422 if tick.0 % DAY_TICKS != BIRTH_HOUR_TICK {
423 return;
424 }
425 let count = colonists.iter().count();
426 let colony_home = zones.home(map.size / 2);
427 let food = food_in_store(
428 items
429 .iter()
430 .filter(|(_, pos)| in_colony(colony_home, pos.0))
431 .map(|(item, _)| item),
432 );
433 let mut born = 0;
434 let mut taken: Vec<&str> = colonists.iter().map(|c| c.0.name.as_str()).collect();
435 let mut families: Vec<(&u32, &Home)> = homes.by_family.iter().collect();
436 families.sort_by_key(|(family, _)| **family);
437 for (&family, home) in families {
438 let members = || {
439 colonists
440 .iter()
441 .filter(move |c| c.1 == Some(&Family(family)))
442 };
443 let parents: Vec<&str> = members()
444 .filter(|c| c.2)
445 .map(|c| c.0.name.as_str())
446 .collect();
447 let children = members().filter(|c| c.3).count();
448 let beds = structures
449 .iter()
450 .filter(|(pos, s)| s.kind == StructureKind::Bed && home.contains(pos.0))
451 .count();
452 let built = home.interior().all(|t| map.is_roofed(t.as_ivec2()));
453 let room = count + born < MAX_COLONISTS && food >= (count + born) as f32;
454 if parents.len() < 2 || children >= MAX_CHILDREN || beds < 2 || !built || !room {
455 continue;
456 }
457 if rng.range_f32(0.0..1.0) >= BIRTH_CHANCE {
458 continue;
459 }
460 let index = (0..NAMES.len())
461 .find(|&i| !taken.contains(&NAMES[i]))
462 .unwrap_or(count + born);
463 if let Some(name) = NAMES.get(index) {
464 taken.push(name);
465 }
466 let tile = home
467 .interior()
468 .find(|t| !home.bed_tiles().contains(t))
469 .unwrap_or(home.lo);
470 let child = spawn_colonist(
471 &mut commands,
472 &assets,
473 &item_assets,
474 &mut materials,
475 index,
476 tile,
477 );
478 commands.entity(child).insert((
479 Child { born: tick.0 },
480 Family(family),
481 Transform::from_translation(WorldMap::tile_center(tile).extend(1.0))
482 .with_scale(Vec3::splat(CHILD_SCALE)),
483 ));
484 born += 1;
485 log.push(
486 *tick,
487 format!(
488 "{} and {} had a child, {}",
489 parents[0],
490 parents[1],
491 NAMES[index % NAMES.len()]
492 ),
493 );
494 }
495 }
496
497 fn grow_up(
498 mut commands: Commands,
499 tick: Res<SimTick>,
500 mut log: ResMut<ColonyLog>,
501 mut children: Query<(Entity, &Colonist, &Child, &mut Transform)>,
502 ) {
503 if !tick.0.is_multiple_of(TICKS_PER_HOUR) {
504 return;
505 }
506 for (entity, colonist, child, mut transform) in &mut children {
507 if tick.0 < child.born + CHILDHOOD_TICKS {
508 continue;
509 }
510 transform.scale = Vec3::ONE;
511 commands.entity(entity).remove::<Child>();
512 log.push(*tick, format!("{} has grown up", colonist.name));
513 }
514 }
515
516 #[cfg(test)]
517 mod tests {
518 use super::*;
519 use crate::world::Terrain;
520
521 fn singles(app_world: &mut World, tiles: &[UVec2]) -> Vec<Single> {
522 tiles
523 .iter()
524 .map(|&tile| Single {
525 entity: app_world.spawn_empty().id(),
526 tile,
527 family: None,
528 })
529 .collect()
530 }
531
532 fn bond_for(hours: u32, singles: &[Single]) -> Vec<(Entity, Entity)> {
533 let mut affinity = Affinity::default();
534 let mut rng = SimRng::new(3);
535 let mut bonded: Vec<(Entity, Entity)> = Vec::new();
536 for _ in 0..hours {
537 let free: Vec<Single> = singles
538 .iter()
539 .filter(|s| !bonded.iter().any(|&(a, b)| a == s.entity || b == s.entity))
540 .copied()
541 .collect();
542 bonded.extend(bond_hour(&mut affinity, &free, &mut rng));
543 }
544 bonded
545 }
546
547 #[test]
548 fn colonists_who_spend_time_together_become_partners() {
549 let mut world = World::new();
550 let near = singles(&mut world, &[UVec2::new(5, 5), UVec2::new(6, 7)]);
551 assert!(bond_for(BOND_HOURS - 1, &near).is_empty());
552 assert_eq!(bond_for(BOND_HOURS * 4, &near).len(), 1);
553 let apart = singles(&mut world, &[UVec2::new(5, 5), UVec2::new(15, 5)]);
554 assert!(bond_for(BOND_HOURS * 4, &apart).is_empty());
555 }
556
557 #[test]
558 fn everyone_has_at_most_one_partner_and_family_never_pairs() {
559 let mut world = World::new();
560 let mut trio = singles(&mut world, &[UVec2::ONE, UVec2::ONE, UVec2::ONE]);
561 let bonded = bond_for(BOND_HOURS * 10, &trio);
562 assert_eq!(bonded.len(), 1);
563 for single in &mut trio {
564 single.family = Some(Family(1));
565 }
566 assert!(bond_for(BOND_HOURS * 10, &trio).is_empty());
567 }
568
569 fn plan_on(map: &WorldMap, zones: &Zones, occupied: &HashSet<UVec2>) -> Home {
570 HomeContext {
571 map,
572 zones,
573 blueprints: &Blueprints::default(),
574 homes: &Homes::default(),
575 occupied,
576 }
577 .plan(UVec2::new(20, 20))
578 .unwrap()
579 }
580
581 #[test]
582 fn homes_go_on_open_ground_away_from_the_colony_center() {
583 let mut map = WorldMap::filled(UVec2::splat(40), Terrain::Grass);
584 for x in 0..40 {
585 map.set_plant(UVec2::new(x, 14), Some(crate::world::Plant::Tree));
586 }
587 let mut zones = Zones::default();
588 zones.add_tiles(&map, [UVec2::new(20, 20), UVec2::new(21, 20)]);
589 let occupied = HashSet::from([UVec2::new(20, 26)]);
590 let home = plan_on(&map, &zones, &occupied);
591 let center = (home.lo + home.hi).as_ivec2() / 2;
592 assert!((center - IVec2::new(20, 20)).abs().max_element() >= HOME_MIN_DISTANCE);
593 for y in home.lo.y - 1..=home.hi.y + 1 {
594 for x in home.lo.x - 1..=home.hi.x + 1 {
595 let tile = UVec2::new(x, y);
596 assert!(!zones.is_stockpile(tile));
597 if home.contains(tile) {
598 assert!(map.plant(tile.as_ivec2()).is_none());
599 assert!(!occupied.contains(&tile));
600 }
601 }
602 }
603 }
604
605 #[test]
606 fn a_home_is_a_walled_room_with_a_door_and_two_beds() {
607 let home = Home::at(UVec2::new(10, 10));
608 let plan = home.blueprints(UVec2::new(12, 0));
609 let count = |kind: StructureKind| {
610 plan.values()
611 .filter(|k| **k == BuildKind::Structure(kind))
612 .count()
613 };
614 let ring = 2 * (HOME_INTERIOR.x + 2) + 2 * HOME_INTERIOR.y;
615 assert_eq!(
616 count(StructureKind::Wall) + count(StructureKind::Door),
617 ring as usize
618 );
619 assert_eq!(count(StructureKind::Door), 1);
620 assert_eq!(count(StructureKind::Bed), 2);
621 let door = plan
622 .iter()
623 .find(|(_, k)| **k == BuildKind::Structure(StructureKind::Door))
624 .unwrap()
625 .0;
626 assert_eq!(door.y, home.lo.y);
627 for bed in home.bed_tiles() {
628 assert!(home.interior().any(|t| t == bed));
629 }
630 }
631
632 #[test]
633 fn finished_homes_get_a_roof() {
634 let mut map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
635 let home = Home::at(UVec2::new(5, 5));
636 let door = UVec2::new(7, 5);
637 for tile in home.ring().filter(|t| *t != door) {
638 map.set_wall(tile, true);
639 }
640 assert!(roofs_needed(&map, &HashSet::new(), &home).is_empty());
641 let roofs = roofs_needed(&map, &HashSet::from([door]), &home);
642 assert_eq!(roofs.len(), (HOME_INTERIOR.x * HOME_INTERIOR.y) as usize);
643 }
644
645 // Gold has one use: a statue in each finished home, which lifts the family's mood. Planning
646 // it is what makes the colony want gold at all.
647 #[test]
648 fn finished_homes_get_a_statue() {
649 let mut map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
650 let home = Home::at(UVec2::new(5, 5));
651 let door = UVec2::new(7, 5);
652 for tile in home.ring().filter(|t| *t != door) {
653 map.set_wall(tile, true);
654 }
655 for tile in home.interior() {
656 map.set_roof(tile, true);
657 }
658 let mut app = App::new();
659 app.insert_resource(map)
660 .init_resource::<SimTick>()
661 .init_resource::<Zones>()
662 .init_resource::<Blueprints>()
663 .insert_resource(Homes {
664 by_family: HashMap::from([(1, home)]),
665 ..default()
666 })
667 .add_systems(Update, plan_homes);
668 app.world_mut().spawn((
669 Structure {
670 kind: StructureKind::Door,
671 },
672 TilePos(door),
673 ));
674 app.world_mut()
675 .spawn((Colonist { name: "Ada".into() }, Family(1)));
676 app.update();
677 let blueprints = app.world().resource::<Blueprints>();
678 assert_eq!(
679 blueprints.get(home.statue_tile()),
680 Some(BuildKind::Structure(StructureKind::Statue))
681 );
682 }
683 }