use crate::config::TerrainConfig;
use crate::error::PcgResult;
use crate::model::geometry::GridSize;
use crate::model::room::{DoorAnchor, Room};
use crate::model::terrain::{ConnectivitySummary, Grid2D, Terrain, TileKind};
use crate::rng::StableRng;
use super::carve::{extract_room_bounds, init_room_grid};
use super::strategy::TerrainStrategy;
#[non_exhaustive]
pub struct PillarStrategy;
impl TerrainStrategy for PillarStrategy {
fn name(&self) -> &str {
"pillar"
}
fn generate(
&self,
room: &Room,
anchors: &[DoorAnchor],
config: &TerrainConfig,
rng: &mut StableRng,
) -> PcgResult<Terrain> {
let (bounds, width, height) = extract_room_bounds(room)?;
let mut tiles = init_room_grid(width, height)?;
super::carve::mark_doorways(&mut tiles, anchors, &room.id, bounds.min);
let spacing = calculate_pillar_spacing(width, height, config.obstacle_density);
let pillar_size = if config.obstacle_density > 0.3 { 2 } else { 1 };
let start_x = spacing;
let start_y = spacing;
let mut y = start_y as i32;
while y < height as i32 - 1 {
let mut x = start_x as i32;
while x < width as i32 - 1 {
let offset_x = if spacing > 2 {
rng.random_range(-1, 2)
} else {
0
};
let offset_y = if spacing > 2 {
rng.random_range(-1, 2)
} else {
0
};
let px = x + offset_x;
let py = y + offset_y;
place_pillar(&mut tiles, px, py, pillar_size, width, height);
x += spacing as i32;
}
y += spacing as i32;
}
Ok(Terrain {
room_id: room.id.clone(),
grid_size: GridSize { width, height },
tiles,
reserved_zones: Vec::new(),
connectivity_summary: ConnectivitySummary {
all_doors_connected: false,
walkable_tile_count: 0,
total_tile_count: 0,
connected_region_count: 0,
},
})
}
}
fn calculate_pillar_spacing(width: u32, height: u32, density: f32) -> u32 {
let base_spacing = (width.min(height) / 4).max(3);
let adjusted = (base_spacing as f32 / (density * 2.0 + 0.5)).round() as u32;
adjusted.clamp(3, 8)
}
fn place_pillar(tiles: &mut Grid2D<TileKind>, x: i32, y: i32, size: i32, width: u32, height: u32) {
for dy in 0..size {
for dx in 0..size {
let px = x + dx;
let py = y + dy;
if px < 1 || py < 1 || px >= width as i32 - 1 || py >= height as i32 - 1 {
continue;
}
if tiles.get(px, py).copied() == Some(TileKind::Doorway) {
continue;
}
if tiles.get(px, py).copied() == Some(TileKind::Floor) {
tiles.set(px, py, TileKind::Obstacle);
}
}
}
}