src/building.rs 22.6 K · 684 lines · raw · history

1 use serde::{Deserialize, Serialize};
2 use std::collections::{HashMap, HashSet, VecDeque};
3
4 use bevy::prelude::*;
5
6 use crate::{
7 actions::Action,
8 colonist::Colonist,
9 family::Homes,
10 items::{Item, ItemKind},
11 selection::{Cursor, update_cursor},
12 sim::{SimSystems, SimTick},
13 structures::{Structure, StructureKind},
14 ui::PointerOverUi,
15 world::{TILE_SIZE, Terrain, TilePos, WorldMap},
16 worldgen::WorldGenerated,
17 zones::{PREVIEW_ADD_COLOR, PREVIEW_REMOVE_COLOR, Zones, can_zone, removing},
18 };
19
20 pub struct BuildingPlugin;
21
22 impl Plugin for BuildingPlugin {
23 fn build(&self, app: &mut App) {
24 app.init_resource::<Blueprints>()
25 .add_systems(
26 Update,
27 (
28 clear_blueprints.run_if(on_message::<WorldGenerated>),
29 draw_blueprints,
30 ),
31 )
32 .init_resource::<BuildTool>()
33 .add_systems(
34 PostUpdate,
35 (build_tool, draw_build_preview)
36 .chain()
37 .after(update_cursor),
38 )
39 .add_systems(FixedUpdate, plan_shelter.in_set(SimSystems::Decide));
40 }
41 }
42
43 pub const MAX_ROOM_TILES: usize = 120;
44 const PLAN_INTERVAL_TICKS: u64 = 300;
45 const MAX_ROOM_MARGIN: i32 = 2;
46 const CRAMPED_SIDE_COST: i32 = 3;
47 const WALL_BLUEPRINT_COLOR: Color = Color::srgba(0.5, 0.75, 1.0, 0.8);
48 const DOOR_BLUEPRINT_COLOR: Color = Color::srgba(1.0, 0.8, 0.4, 0.8);
49 const PAVE_BLUEPRINT_COLOR: Color = Color::srgba(0.85, 0.85, 0.85, 0.5);
50 const DECONSTRUCT_COLOR: Color = Color::srgba(1.0, 0.3, 0.25, 0.9);
51 const FLOOR_BLUEPRINT_COLOR: Color = Color::srgba(0.75, 0.5, 0.25, 0.6);
52 const ROOF_BLUEPRINT_COLOR: Color = Color::srgba(0.8, 0.6, 1.0, 0.35);
53
54 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
55 pub enum BuildKind {
56 Structure(StructureKind),
57 Roof,
58 Pave,
59 Floor,
60 Refuel(StructureKind),
61 Deconstruct,
62 }
63
64 impl BuildKind {
65 pub fn cost(self) -> (ItemKind, u32) {
66 match self {
67 BuildKind::Structure(kind) => kind.cost(),
68 BuildKind::Roof => (ItemKind::Wood, 1),
69 BuildKind::Pave => (ItemKind::Stone, 1),
70 BuildKind::Floor => (ItemKind::Wood, 1),
71 BuildKind::Refuel(kind) => kind.refuel_cost(),
72 BuildKind::Deconstruct => (ItemKind::Wood, 0),
73 }
74 }
75
76 pub fn work(self) -> u32 {
77 match self {
78 BuildKind::Structure(StructureKind::Campfire) => 360,
79 BuildKind::Structure(StructureKind::Torch) => 120,
80 BuildKind::Structure(StructureKind::Bed) => 600,
81 BuildKind::Structure(StructureKind::Wall) => 300,
82 BuildKind::Structure(StructureKind::Door) => 300,
83 BuildKind::Structure(StructureKind::Workbench) => 600,
84 BuildKind::Structure(StructureKind::Statue) => 900,
85 BuildKind::Structure(StructureKind::Hearth) => 400,
86 BuildKind::Roof => 120,
87 BuildKind::Pave => 120,
88 BuildKind::Floor => 90,
89 BuildKind::Refuel(_) => 60,
90 BuildKind::Deconstruct => 200,
91 }
92 }
93
94 pub fn name(self) -> &'static str {
95 match self {
96 BuildKind::Structure(kind) => kind.name(),
97 BuildKind::Roof => "roof",
98 BuildKind::Pave => "paving",
99 BuildKind::Floor => "floor",
100 BuildKind::Refuel(_) => "refuelling",
101 BuildKind::Deconstruct => "deconstruction",
102 }
103 }
104
105 pub fn blocks_tile(self) -> bool {
106 self == BuildKind::Structure(StructureKind::Wall)
107 }
108 }
109
110 #[derive(Resource, Default, Debug, Clone)]
111 pub struct Blueprints(pub HashMap<UVec2, BuildKind>);
112
113 impl Blueprints {
114 pub fn get(&self, tile: UVec2) -> Option<BuildKind> {
115 self.0.get(&tile).copied()
116 }
117
118 pub fn tiles(&self) -> HashSet<UVec2> {
119 self.0.keys().copied().collect()
120 }
121 }
122
123 fn clear_blueprints(mut blueprints: ResMut<Blueprints>) {
124 blueprints.0.clear();
125 }
126
127 pub fn enclosed_room(map: &WorldMap, doors: &HashSet<UVec2>, start: UVec2) -> Option<Vec<UVec2>> {
128 let mut seen = HashSet::from([start]);
129 let mut queue = VecDeque::from([start]);
130 while let Some(tile) = queue.pop_front() {
131 if seen.len() > MAX_ROOM_TILES {
132 return None;
133 }
134 for step in [IVec2::X, IVec2::NEG_X, IVec2::Y, IVec2::NEG_Y] {
135 let next = tile.as_ivec2() + step;
136 if !map.in_bounds(next) {
137 return None;
138 }
139 let next = next.as_uvec2();
140 let barrier = map.is_wall(next.as_ivec2())
141 || doors.contains(&next)
142 || map.get(next.as_ivec2()).is_some_and(|t| t.is_rock());
143 if !barrier && seen.insert(next) {
144 queue.push_back(next);
145 }
146 }
147 }
148 let mut room: Vec<UVec2> = seen.into_iter().collect();
149 room.sort_by_key(|t| (t.y, t.x));
150 Some(room)
151 }
152
153 pub struct ShelterContext<'a> {
154 pub map: &'a WorldMap,
155 pub structures: &'a [(UVec2, StructureKind)],
156 pub items: &'a [UVec2],
157 }
158
159 impl ShelterContext<'_> {
160 fn doors(&self) -> HashSet<UVec2> {
161 self.structures
162 .iter()
163 .filter(|(_, kind)| *kind == StructureKind::Door)
164 .map(|(pos, _)| *pos)
165 .collect()
166 }
167
168 fn wall_buildable(&self, tile: IVec2) -> bool {
169 can_zone(self.map, tile)
170 && self.map.plant(tile).is_none()
171 && !self.items.contains(&tile.as_uvec2())
172 && !self
173 .structures
174 .iter()
175 .any(|(pos, _)| *pos == tile.as_uvec2())
176 }
177
178 fn already_closed(&self, tile: IVec2, doors: &HashSet<UVec2>) -> bool {
179 self.map.is_wall(tile)
180 || doors.contains(&tile.as_uvec2())
181 || self.map.get(tile).is_some_and(|t| t.is_rock())
182 }
183
184 pub fn plan(&self, beds: &[UVec2], toward: UVec2) -> Option<HashMap<UVec2, BuildKind>> {
185 let doors = self.doors();
186 if let Some(&first) = beds.first()
187 && let Some(room) = enclosed_room(self.map, &doors, first)
188 {
189 let roofs: HashMap<UVec2, BuildKind> = room
190 .into_iter()
191 .filter(|t| !self.map.is_roofed(t.as_ivec2()))
192 .map(|t| (t, BuildKind::Roof))
193 .collect();
194 return (!roofs.is_empty()).then_some(roofs);
195 }
196 let min = beds.iter().copied().reduce(|a, b| a.min(b))?.as_ivec2();
197 let max = beds.iter().copied().reduce(|a, b| a.max(b))?.as_ivec2();
198 let margins = 0..=MAX_ROOM_MARGIN;
199 let shapes = margins.clone().flat_map(|l| {
200 let margins = margins.clone();
201 margins.clone().flat_map(move |r| {
202 let margins = margins.clone();
203 margins
204 .clone()
205 .flat_map(move |d| margins.clone().map(move |u| IVec4::new(l, r, d, u)))
206 })
207 });
208 shapes
209 .filter_map(|m| {
210 let lo = min - IVec2::new(m.x, m.z) - 1;
211 let hi = max + IVec2::new(m.y, m.w) + 1;
212 let plan = self.walls_around(lo, hi, toward, &doors)?;
213 let cramped = m.to_array().iter().filter(|&&side| side == 0).count() as i32;
214 let cost = plan.len() as i32 + CRAMPED_SIDE_COST * cramped;
215 Some((cost, m.to_array(), plan))
216 })
217 .min_by_key(|(cost, margins, _)| (*cost, *margins))
218 .map(|(_, _, plan)| plan)
219 }
220
221 fn walls_around(
222 &self,
223 lo: IVec2,
224 hi: IVec2,
225 toward: UVec2,
226 doors: &HashSet<UVec2>,
227 ) -> Option<HashMap<UVec2, BuildKind>> {
228 let ring: Vec<IVec2> = (lo.y..=hi.y)
229 .flat_map(|y| (lo.x..=hi.x).map(move |x| IVec2::new(x, y)))
230 .filter(|t| t.x == lo.x || t.x == hi.x || t.y == lo.y || t.y == hi.y)
231 .collect();
232 let mut plan = HashMap::new();
233 for &tile in &ring {
234 if !self.map.in_bounds(tile) {
235 return None;
236 }
237 if self.already_closed(tile, doors) {
238 continue;
239 }
240 if !self.wall_buildable(tile) {
241 return None;
242 }
243 plan.insert(tile.as_uvec2(), BuildKind::Structure(StructureKind::Wall));
244 }
245 let has_door = ring.iter().any(|t| doors.contains(&t.as_uvec2()));
246 if !has_door {
247 let corner = |t: &UVec2| {
248 let t = t.as_ivec2();
249 (t.x == lo.x || t.x == hi.x) && (t.y == lo.y || t.y == hi.y)
250 };
251 let door = plan.keys().filter(|t| !corner(t)).copied().min_by(|a, b| {
252 a.as_vec2()
253 .distance_squared(toward.as_vec2())
254 .total_cmp(&b.as_vec2().distance_squared(toward.as_vec2()))
255 .then((a.y, a.x).cmp(&(b.y, b.x)))
256 })?;
257 plan.insert(door, BuildKind::Structure(StructureKind::Door));
258 }
259 Some(plan)
260 }
261 }
262
263 pub fn plan_shelter(
264 tick: Res<SimTick>,
265 map: Res<WorldMap>,
266 mut zones: ResMut<Zones>,
267 structures: Query<(&TilePos, &Structure), Without<Colonist>>,
268 items: Query<&TilePos, With<Item>>,
269 mut blueprints: ResMut<Blueprints>,
270 homes: Res<Homes>,
271 ) {
272 let shelter_pending = blueprints
273 .0
274 .values()
275 .any(|k| !matches!(k, BuildKind::Pave | BuildKind::Floor));
276 if !tick.0.is_multiple_of(PLAN_INTERVAL_TICKS) || shelter_pending {
277 return;
278 }
279 let structures: Vec<(UVec2, StructureKind)> =
280 structures.iter().map(|(pos, s)| (pos.0, s.kind)).collect();
281 let beds: Vec<UVec2> = structures
282 .iter()
283 .filter(|(pos, kind)| {
284 *kind == StructureKind::Bed && !map.is_roofed(pos.as_ivec2()) && !homes.is_home(*pos)
285 })
286 .map(|(pos, _)| *pos)
287 .collect();
288 if beds.is_empty() {
289 return;
290 }
291 let items: Vec<UVec2> = items.iter().map(|pos| pos.0).collect();
292 let ctx = ShelterContext {
293 map: &map,
294 structures: &structures,
295 items: &items,
296 };
297 let toward = structures
298 .iter()
299 .find(|(_, kind)| *kind == StructureKind::Campfire)
300 .map(|(pos, _)| *pos)
301 .unwrap_or_else(|| zones.home(beds[0]));
302 if let Some(plan) = ctx.plan(&beds, toward) {
303 for (tile, kind) in &plan {
304 if *kind != BuildKind::Roof {
305 zones.remove_tile(*tile);
306 }
307 }
308 blueprints.0.extend(plan);
309 }
310 }
311
312 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
313 pub enum BuildMode {
314 Place(StructureKind),
315 Floor,
316 Deconstruct,
317 }
318
319 pub const BUILD_TOOL_CYCLE: [BuildMode; 10] = [
320 BuildMode::Place(StructureKind::Wall),
321 BuildMode::Place(StructureKind::Door),
322 BuildMode::Place(StructureKind::Campfire),
323 BuildMode::Place(StructureKind::Torch),
324 BuildMode::Place(StructureKind::Bed),
325 BuildMode::Place(StructureKind::Workbench),
326 BuildMode::Place(StructureKind::Statue),
327 BuildMode::Place(StructureKind::Hearth),
328 BuildMode::Floor,
329 BuildMode::Deconstruct,
330 ];
331
332 #[derive(Resource, Default, Debug)]
333 pub struct BuildTool {
334 pub mode: Option<BuildMode>,
335 drag_start: Option<UVec2>,
336 }
337
338 impl BuildTool {
339 pub fn active(&self) -> bool {
340 self.mode.is_some()
341 }
342
343 fn cycle(&mut self) {
344 let next = match self.mode {
345 None => Some(0),
346 Some(mode) => BUILD_TOOL_CYCLE
347 .iter()
348 .position(|m| *m == mode)
349 .map(|i| i + 1)
350 .filter(|&i| i < BUILD_TOOL_CYCLE.len()),
351 };
352 self.mode = next.map(|i| BUILD_TOOL_CYCLE[i]);
353 }
354 }
355
356 fn rect(start: UVec2, end: UVec2) -> impl Iterator<Item = UVec2> {
357 let (lo, hi) = (start.min(end), start.max(end));
358 (lo.y..=hi.y).flat_map(move |y| (lo.x..=hi.x).map(move |x| UVec2::new(x, y)))
359 }
360
361 pub fn mark_deconstruction(
362 blueprints: &mut Blueprints,
363 occupied: &HashSet<UVec2>,
364 start: UVec2,
365 end: UVec2,
366 ) {
367 for tile in rect(start, end).filter(|t| occupied.contains(t)) {
368 blueprints.0.insert(tile, BuildKind::Deconstruct);
369 }
370 }
371
372 pub fn place_floors(map: &WorldMap, blueprints: &mut Blueprints, start: UVec2, end: UVec2) {
373 for tile in rect(start, end) {
374 let p = tile.as_ivec2();
375 let floorable = can_zone(map, p)
376 && !map.is_wall(p)
377 && map.plant(p).is_none()
378 && map.get(p) != Some(Terrain::WoodFloor);
379 if floorable && blueprints.get(tile).is_none() {
380 blueprints.0.insert(tile, BuildKind::Floor);
381 }
382 }
383 }
384
385 pub fn placements(kind: StructureKind, start: UVec2, end: UVec2) -> Vec<UVec2> {
386 if kind != StructureKind::Wall {
387 return vec![end];
388 }
389 let (lo, hi) = (start.min(end), start.max(end));
390 (lo.y..=hi.y)
391 .flat_map(|y| (lo.x..=hi.x).map(move |x| UVec2::new(x, y)))
392 .filter(|t| t.x == lo.x || t.x == hi.x || t.y == lo.y || t.y == hi.y)
393 .collect()
394 }
395
396 pub fn place_blueprints(
397 map: &WorldMap,
398 zones: &mut Zones,
399 blueprints: &mut Blueprints,
400 occupied: &HashSet<UVec2>,
401 kind: StructureKind,
402 tiles: &[UVec2],
403 ) {
404 for &tile in tiles {
405 let free = can_zone(map, tile.as_ivec2())
406 && map.plant(tile.as_ivec2()).is_none()
407 && !map.is_wall(tile.as_ivec2())
408 && !occupied.contains(&tile);
409 if !free {
410 continue;
411 }
412 if matches!(kind, StructureKind::Wall | StructureKind::Door) {
413 zones.remove_tile(tile);
414 }
415 blueprints.0.insert(tile, BuildKind::Structure(kind));
416 }
417 }
418
419 #[allow(clippy::too_many_arguments)]
420 fn build_tool(
421 mut actions: MessageReader<Action>,
422 keys: Res<ButtonInput<KeyCode>>,
423 buttons: Res<ButtonInput<MouseButton>>,
424 over_ui: Res<PointerOverUi>,
425 cursor: Res<Cursor>,
426 map: Res<WorldMap>,
427 mut tool: ResMut<BuildTool>,
428 mut zones: ResMut<Zones>,
429 mut blueprints: ResMut<Blueprints>,
430 structures: Query<&TilePos, (With<Structure>, Without<Colonist>)>,
431 ) {
432 for action in actions.read() {
433 match action {
434 Action::CycleBuildTool => tool.cycle(),
435 Action::Cancel
436 | Action::ToggleStockpileTool
437 | Action::ToggleFarmTool
438 | Action::ToggleDesignateTool => tool.mode = None,
439 _ => continue,
440 }
441 tool.drag_start = None;
442 return;
443 }
444 let Some(mode) = tool.mode else {
445 return;
446 };
447 if buttons.just_pressed(MouseButton::Left) && !over_ui.0 {
448 tool.drag_start = cursor.tile;
449 }
450 if !buttons.just_released(MouseButton::Left) {
451 return;
452 }
453 let (Some(start), Some(end)) = (tool.drag_start.take(), cursor.tile) else {
454 return;
455 };
456 if removing(&keys) {
457 let (lo, hi) = (start.min(end), start.max(end));
458 blueprints
459 .0
460 .retain(|t, _| !(t.cmpge(lo).all() && t.cmple(hi).all()));
461 return;
462 }
463 let occupied: HashSet<UVec2> = structures.iter().map(|p| p.0).collect();
464 match mode {
465 BuildMode::Place(kind) => {
466 let tiles = placements(kind, start, end);
467 place_blueprints(&map, &mut zones, &mut blueprints, &occupied, kind, &tiles);
468 }
469 BuildMode::Floor => place_floors(&map, &mut blueprints, start, end),
470 BuildMode::Deconstruct => mark_deconstruction(&mut blueprints, &occupied, start, end),
471 }
472 }
473
474 fn draw_build_preview(
475 keys: Res<ButtonInput<KeyCode>>,
476 cursor: Res<Cursor>,
477 tool: Res<BuildTool>,
478 mut gizmos: Gizmos,
479 ) {
480 let (Some(mode), Some(end)) = (tool.mode, cursor.tile) else {
481 return;
482 };
483 let start = tool.drag_start.unwrap_or(end);
484 let color = if removing(&keys) || mode == BuildMode::Deconstruct {
485 PREVIEW_REMOVE_COLOR
486 } else {
487 PREVIEW_ADD_COLOR
488 };
489 let tiles: Vec<UVec2> = match mode {
490 BuildMode::Place(kind) if !removing(&keys) => placements(kind, start, end),
491 _ => rect(start, end).collect(),
492 };
493 for tile in tiles {
494 gizmos.rect_2d(
495 WorldMap::tile_center(tile),
496 Vec2::splat(TILE_SIZE * 0.7),
497 color,
498 );
499 }
500 }
501
502 fn draw_blueprints(mut gizmos: Gizmos, blueprints: Res<Blueprints>) {
503 for (tile, kind) in &blueprints.0 {
504 let color = match kind {
505 BuildKind::Structure(StructureKind::Door) => DOOR_BLUEPRINT_COLOR,
506 BuildKind::Roof => ROOF_BLUEPRINT_COLOR,
507 BuildKind::Pave => PAVE_BLUEPRINT_COLOR,
508 BuildKind::Floor => FLOOR_BLUEPRINT_COLOR,
509 BuildKind::Deconstruct => DECONSTRUCT_COLOR,
510 _ => WALL_BLUEPRINT_COLOR,
511 };
512 gizmos.rect_2d(
513 WorldMap::tile_center(*tile),
514 Vec2::splat(TILE_SIZE * 0.85),
515 color,
516 );
517 }
518 }
519
520 #[cfg(test)]
521 mod tests {
522 use super::*;
523 use crate::world::Terrain;
524
525 fn ctx<'a>(map: &'a WorldMap, structures: &'a [(UVec2, StructureKind)]) -> ShelterContext<'a> {
526 ShelterContext {
527 map,
528 structures,
529 items: &[],
530 }
531 }
532
533 fn count(plan: &HashMap<UVec2, BuildKind>, kind: BuildKind) -> usize {
534 plan.values().filter(|k| **k == kind).count()
535 }
536
537 const WALL: BuildKind = BuildKind::Structure(StructureKind::Wall);
538 const DOOR: BuildKind = BuildKind::Structure(StructureKind::Door);
539
540 #[test]
541 fn the_build_tool_cycles_through_structures_and_off() {
542 let mut tool = BuildTool::default();
543 for mode in BUILD_TOOL_CYCLE {
544 tool.cycle();
545 assert_eq!(tool.mode, Some(mode));
546 }
547 tool.cycle();
548 assert_eq!(tool.mode, None);
549 }
550
551 #[test]
552 fn deconstruction_marks_only_structures() {
553 let mut blueprints = Blueprints::default();
554 let occupied = HashSet::from([UVec2::new(1, 1), UVec2::new(9, 9)]);
555 mark_deconstruction(&mut blueprints, &occupied, UVec2::ZERO, UVec2::splat(3));
556 assert_eq!(blueprints.0.len(), 1);
557 assert_eq!(blueprints.get(UVec2::ONE), Some(BuildKind::Deconstruct));
558 }
559
560 #[test]
561 fn dragging_walls_plans_a_room_outline_on_free_ground() {
562 let mut map = WorldMap::filled(UVec2::splat(10), Terrain::Grass);
563 map.set_plant(UVec2::new(2, 2), Some(crate::world::Plant::Tree));
564 let mut zones = Zones::default();
565 zones.add_tiles(&map, [UVec2::new(5, 2)]);
566 let mut blueprints = Blueprints::default();
567 let occupied = HashSet::from([UVec2::new(5, 5)]);
568 let tiles = placements(StructureKind::Wall, UVec2::new(5, 5), UVec2::new(2, 2));
569 assert_eq!(tiles.len(), 12);
570 place_blueprints(
571 &map,
572 &mut zones,
573 &mut blueprints,
574 &occupied,
575 StructureKind::Wall,
576 &tiles,
577 );
578 assert_eq!(blueprints.0.len(), 10);
579 assert!(!zones.is_stockpile(UVec2::new(5, 2)));
580 assert_eq!(
581 placements(StructureKind::Campfire, UVec2::ZERO, UVec2::ONE),
582 vec![UVec2::ONE]
583 );
584 }
585
586 #[test]
587 fn shelter_is_a_wall_ring_with_one_door_around_the_beds() {
588 let map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
589 let beds = [UVec2::new(10, 10), UVec2::new(11, 10)];
590 let plan = ctx(&map, &[]).plan(&beds, UVec2::new(10, 2)).unwrap();
591 assert_eq!(plan.len(), 6 * 5 - 4 * 3);
592 assert_eq!(count(&plan, DOOR), 1);
593 let door = plan.iter().find(|(_, k)| **k == DOOR).unwrap().0;
594 assert_eq!(door.y, 8);
595 }
596
597 // Mountains are free walls: a shelter dug against a cliff only needs walls on the open sides.
598 #[test]
599 fn rock_counts_as_wall() {
600 let mut map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
601 for y in 0..20 {
602 map.set(UVec2::new(12, y), Terrain::Stone);
603 }
604 let plan = ctx(&map, &[])
605 .plan(&[UVec2::new(10, 10)], UVec2::new(10, 2))
606 .unwrap();
607 assert!(plan.keys().all(|t| t.x != 12));
608 assert_eq!(count(&plan, WALL) + count(&plan, DOOR), 16 - 5);
609 }
610
611 #[test]
612 fn shelters_route_around_unbuildable_tiles() {
613 let mut map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
614 map.set(UVec2::new(10, 8), Terrain::ShallowWater);
615 let plan = ctx(&map, &[])
616 .plan(&[UVec2::new(10, 10)], UVec2::new(10, 2))
617 .unwrap();
618 assert!(!plan.contains_key(&UVec2::new(10, 8)));
619 assert_eq!(
620 plan.values()
621 .filter(|k| **k == BuildKind::Structure(StructureKind::Door))
622 .count(),
623 1
624 );
625 }
626
627 // Beds often end up right next to the stockpile; walls may take over empty stockpile tiles
628 // (which then stop being storage) instead of the shelter never getting planned at all.
629 #[test]
630 fn walls_may_replace_empty_stockpile_tiles_but_not_stored_items() {
631 let map = WorldMap::filled(UVec2::splat(20), Terrain::Grass);
632 let mut zones = Zones::default();
633 zones.add_stockpile(&map, IVec2::new(12, 5), IVec2::new(15, 15));
634 let items = [UVec2::new(13, 10)];
635 let ctx = ShelterContext {
636 map: &map,
637 structures: &[],
638 items: &items,
639 };
640 let plan = ctx.plan(&[UVec2::new(11, 10)], UVec2::new(5, 10)).unwrap();
641 assert!(plan.keys().any(|t| zones.is_stockpile(*t)));
642 assert!(!plan.contains_key(&UVec2::new(13, 10)));
643 }
644
645 #[test]
646 fn enclosed_rooms_get_roof_blueprints() {
647 let mut map = WorldMap::filled(UVec2::splat(10), Terrain::Grass);
648 for i in 3..=6 {
649 for t in [
650 UVec2::new(i, 3),
651 UVec2::new(i, 6),
652 UVec2::new(3, i),
653 UVec2::new(6, i),
654 ] {
655 map.set_wall(t, true);
656 }
657 }
658 map.set_wall(UVec2::new(4, 3), false);
659 let structures = [(UVec2::new(4, 3), StructureKind::Door)];
660 let plan = ctx(&map, &structures)
661 .plan(&[UVec2::new(4, 4)], UVec2::ZERO)
662 .unwrap();
663 assert_eq!(plan.len(), 4);
664 assert!(plan.values().all(|k| *k == BuildKind::Roof));
665 }
666
667 #[test]
668 fn open_areas_are_not_rooms() {
669 let map = WorldMap::filled(UVec2::splat(30), Terrain::Grass);
670 assert!(enclosed_room(&map, &HashSet::new(), UVec2::new(15, 15)).is_none());
671 }
672
673 #[test]
674 fn the_floor_tool_fills_an_area_but_skips_walls_and_water() {
675 let mut map = WorldMap::filled(UVec2::splat(10), Terrain::Grass);
676 map.set_wall(UVec2::new(2, 2), true);
677 map.set(UVec2::new(3, 3), Terrain::ShallowWater);
678 map.set(UVec2::new(1, 1), Terrain::WoodFloor);
679 let mut blueprints = Blueprints::default();
680 place_floors(&map, &mut blueprints, UVec2::new(3, 3), UVec2::ONE);
681 assert_eq!(blueprints.0.len(), 9 - 3);
682 assert!(blueprints.0.values().all(|k| *k == BuildKind::Floor));
683 }
684 }