src/building.rs 21 K · 646 lines · raw · history

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