src/family.rs 21.7 K · 682 lines · raw · history

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