2026-03-06 08:41:39 -06:00
|
|
|
mod layout;
|
2026-03-06 09:40:22 -06:00
|
|
|
mod seed;
|
2026-03-06 09:56:26 -06:00
|
|
|
mod settings;
|
2026-03-06 09:24:24 -06:00
|
|
|
mod ui;
|
2026-03-06 08:41:39 -06:00
|
|
|
|
2026-03-06 09:19:03 -06:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
2026-03-05 19:17:01 -06:00
|
|
|
use eframe::egui;
|
|
|
|
|
use egui::{Color32, Stroke};
|
2026-03-06 10:26:23 -06:00
|
|
|
use layout::{DungeonLayout, shortest_path_cells};
|
2026-03-06 09:24:24 -06:00
|
|
|
use ui::{UiSettings, draw_side_panel};
|
2026-03-05 19:17:01 -06:00
|
|
|
|
|
|
|
|
fn main() -> eframe::Result<()> {
|
|
|
|
|
let options = eframe::NativeOptions::default();
|
|
|
|
|
|
|
|
|
|
eframe::run_native(
|
|
|
|
|
"Desktop Dungeon Generator",
|
|
|
|
|
options,
|
|
|
|
|
Box::new(|_cc| Ok(Box::new(DungeonApp::default()))),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct DungeonApp {
|
2026-03-06 09:24:24 -06:00
|
|
|
settings: UiSettings,
|
2026-03-06 08:41:39 -06:00
|
|
|
layout: DungeonLayout,
|
2026-03-06 10:26:23 -06:00
|
|
|
drag_state: Option<DragState>,
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for DungeonApp {
|
|
|
|
|
fn default() -> Self {
|
2026-03-06 09:56:26 -06:00
|
|
|
let settings = settings::load_settings().unwrap_or_default();
|
2026-03-06 10:26:23 -06:00
|
|
|
let layout = layout::generate_layout(
|
2026-03-06 09:24:24 -06:00
|
|
|
settings.cols,
|
|
|
|
|
settings.rows,
|
|
|
|
|
settings.room_count,
|
2026-03-06 09:40:22 -06:00
|
|
|
settings.seed,
|
2026-03-06 09:24:24 -06:00
|
|
|
settings.min_room_size,
|
|
|
|
|
settings.max_room_size,
|
|
|
|
|
settings.square_rooms_only,
|
|
|
|
|
settings.corridor_randomness,
|
|
|
|
|
settings.dead_end_rooms_percent,
|
2026-03-06 08:52:56 -06:00
|
|
|
);
|
2026-03-06 09:56:26 -06:00
|
|
|
Self {
|
|
|
|
|
settings,
|
|
|
|
|
layout,
|
2026-03-06 10:26:23 -06:00
|
|
|
drag_state: None,
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 09:42:57 -06:00
|
|
|
impl Drop for DungeonApp {
|
|
|
|
|
fn drop(&mut self) {
|
2026-03-06 09:56:26 -06:00
|
|
|
if let Err(err) = settings::save_settings(&self.settings) {
|
2026-03-06 09:42:57 -06:00
|
|
|
eprintln!("Failed to save settings: {err}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 19:17:01 -06:00
|
|
|
impl eframe::App for DungeonApp {
|
|
|
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
2026-03-06 09:24:24 -06:00
|
|
|
let panel_result = draw_side_panel(ctx, &mut self.settings);
|
2026-03-06 08:41:39 -06:00
|
|
|
|
2026-03-06 09:24:24 -06:00
|
|
|
if self.settings.min_room_size > self.settings.max_room_size {
|
|
|
|
|
self.settings.max_room_size = self.settings.min_room_size;
|
2026-03-06 08:52:56 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
if panel_result.reset_clicked || panel_result.settings_changed {
|
2026-03-06 09:24:24 -06:00
|
|
|
self.regenerate_layout();
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-05 19:17:01 -06:00
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
|
|
|
let available = ui.available_size_before_wrap();
|
2026-03-06 09:56:26 -06:00
|
|
|
let (response, painter) = ui.allocate_painter(available, egui::Sense::click_and_drag());
|
2026-03-05 19:17:01 -06:00
|
|
|
let canvas = response.rect.shrink(12.0);
|
2026-03-06 09:24:24 -06:00
|
|
|
let geometry = draw_grid(&painter, canvas, self.settings.cols, self.settings.rows);
|
2026-03-06 10:26:23 -06:00
|
|
|
self.handle_drag(&response, &geometry);
|
2026-03-06 08:41:39 -06:00
|
|
|
draw_layout(&painter, &geometry, &self.layout);
|
2026-03-05 19:17:01 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 09:24:24 -06:00
|
|
|
impl DungeonApp {
|
|
|
|
|
fn regenerate_layout(&mut self) {
|
2026-03-06 10:26:23 -06:00
|
|
|
self.drag_state = None;
|
|
|
|
|
self.layout = layout::generate_layout(
|
2026-03-06 09:24:24 -06:00
|
|
|
self.settings.cols,
|
|
|
|
|
self.settings.rows,
|
|
|
|
|
self.settings.room_count,
|
2026-03-06 09:40:22 -06:00
|
|
|
self.settings.seed,
|
2026-03-06 09:24:24 -06:00
|
|
|
self.settings.min_room_size,
|
|
|
|
|
self.settings.max_room_size,
|
|
|
|
|
self.settings.square_rooms_only,
|
|
|
|
|
self.settings.corridor_randomness,
|
|
|
|
|
self.settings.dead_end_rooms_percent,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-06 09:56:26 -06:00
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
fn handle_drag(&mut self, response: &egui::Response, geometry: &GridGeometry) {
|
2026-03-06 09:56:26 -06:00
|
|
|
if response.drag_started()
|
|
|
|
|
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
|
|
|
|
&& let Some((room_idx, offset_x, offset_y)) =
|
|
|
|
|
self.room_at_pointer(pointer_pos, geometry)
|
|
|
|
|
{
|
2026-03-06 10:26:23 -06:00
|
|
|
self.drag_state = Some(DragState::Room(RoomDragState {
|
2026-03-06 09:56:26 -06:00
|
|
|
room_idx,
|
|
|
|
|
offset_x,
|
|
|
|
|
offset_y,
|
2026-03-06 10:26:23 -06:00
|
|
|
}));
|
|
|
|
|
} else if response.drag_started()
|
|
|
|
|
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
|
|
|
|
&& let Some(corridor_drag) = self.corridor_drag_at_pointer(pointer_pos, geometry)
|
|
|
|
|
{
|
|
|
|
|
self.drag_state = Some(DragState::Corridor(corridor_drag));
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response.dragged()
|
|
|
|
|
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
2026-03-06 10:26:23 -06:00
|
|
|
&& let Some(drag_state) = self.drag_state.clone()
|
2026-03-06 09:56:26 -06:00
|
|
|
{
|
2026-03-06 10:26:23 -06:00
|
|
|
match drag_state {
|
|
|
|
|
DragState::Room(drag) => self.drag_room_to_pointer(drag, pointer_pos, geometry),
|
|
|
|
|
DragState::Corridor(drag) => {
|
|
|
|
|
self.drag_corridor_to_pointer(drag, pointer_pos, geometry)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response.drag_stopped() || !response.ctx.input(|i| i.pointer.primary_down()) {
|
2026-03-06 10:26:23 -06:00
|
|
|
self.drag_state = None;
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn room_at_pointer(
|
|
|
|
|
&self,
|
|
|
|
|
pointer_pos: egui::Pos2,
|
|
|
|
|
geometry: &GridGeometry,
|
|
|
|
|
) -> Option<(usize, f32, f32)> {
|
|
|
|
|
let (grid_x, grid_y) = pointer_to_grid(pointer_pos, geometry)?;
|
|
|
|
|
|
|
|
|
|
for (room_idx, room) in self.layout.rooms.iter().enumerate().rev() {
|
|
|
|
|
let room_x = room.x as f32;
|
|
|
|
|
let room_y = room.y as f32;
|
|
|
|
|
let room_right = room_x + room.width as f32;
|
|
|
|
|
let room_bottom = room_y + room.height as f32;
|
|
|
|
|
|
|
|
|
|
if grid_x >= room_x && grid_x < room_right && grid_y >= room_y && grid_y < room_bottom {
|
|
|
|
|
return Some((room_idx, grid_x - room_x, grid_y - room_y));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn drag_room_to_pointer(
|
|
|
|
|
&mut self,
|
|
|
|
|
drag: RoomDragState,
|
|
|
|
|
pointer_pos: egui::Pos2,
|
|
|
|
|
geometry: &GridGeometry,
|
|
|
|
|
) {
|
|
|
|
|
let Some((grid_x, grid_y)) = pointer_to_grid(pointer_pos, geometry) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if drag.room_idx >= self.layout.rooms.len() {
|
2026-03-06 10:26:23 -06:00
|
|
|
self.drag_state = None;
|
2026-03-06 09:56:26 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
let (width, height, old_x, old_y) = {
|
2026-03-06 09:56:26 -06:00
|
|
|
let room = &self.layout.rooms[drag.room_idx];
|
2026-03-06 10:26:23 -06:00
|
|
|
(room.width, room.height, room.x, room.y)
|
2026-03-06 09:56:26 -06:00
|
|
|
};
|
|
|
|
|
let max_x = self.settings.cols.saturating_sub(width);
|
|
|
|
|
let max_y = self.settings.rows.saturating_sub(height);
|
|
|
|
|
|
|
|
|
|
let target_x = (grid_x - drag.offset_x).floor().max(0.0) as usize;
|
|
|
|
|
let target_y = (grid_y - drag.offset_y).floor().max(0.0) as usize;
|
|
|
|
|
let new_x = target_x.min(max_x);
|
|
|
|
|
let new_y = target_y.min(max_y);
|
|
|
|
|
|
|
|
|
|
if new_x == old_x && new_y == old_y {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.layout.rooms[drag.room_idx].x = new_x;
|
|
|
|
|
self.layout.rooms[drag.room_idx].y = new_y;
|
2026-03-06 10:26:23 -06:00
|
|
|
self.reroute_corridors_for_room(drag.room_idx);
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
fn reroute_corridors_for_room(&mut self, room_idx: usize) {
|
|
|
|
|
let empty = HashSet::new();
|
|
|
|
|
for corridor in &mut self.layout.corridors {
|
|
|
|
|
if corridor.start_room_id != room_idx && corridor.end_room_id != room_idx {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let start = self.layout.rooms[corridor.start_room_id].center_cell();
|
|
|
|
|
let end = self.layout.rooms[corridor.end_room_id].center_cell();
|
|
|
|
|
corridor.path =
|
|
|
|
|
shortest_path_cells(start, end, self.settings.cols, self.settings.rows, &empty)
|
|
|
|
|
.unwrap_or_else(|| vec![start, end]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn corridor_drag_at_pointer(
|
|
|
|
|
&self,
|
|
|
|
|
pointer_pos: egui::Pos2,
|
|
|
|
|
geometry: &GridGeometry,
|
|
|
|
|
) -> Option<CorridorDragState> {
|
|
|
|
|
let (grid_x, grid_y) = pointer_to_grid(pointer_pos, geometry)?;
|
|
|
|
|
let clicked = (grid_x.floor() as usize, grid_y.floor() as usize);
|
|
|
|
|
let corridor_index = self
|
|
|
|
|
.layout
|
|
|
|
|
.corridors
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|corridor| corridor.path.contains(&clicked))?;
|
|
|
|
|
|
|
|
|
|
Some(CorridorDragState {
|
|
|
|
|
corridor_index,
|
|
|
|
|
original_path: self.layout.corridors[corridor_index].path.clone(),
|
|
|
|
|
original_cell: clicked,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn drag_corridor_to_pointer(
|
2026-03-06 09:56:26 -06:00
|
|
|
&mut self,
|
2026-03-06 10:26:23 -06:00
|
|
|
drag: CorridorDragState,
|
|
|
|
|
pointer_pos: egui::Pos2,
|
|
|
|
|
geometry: &GridGeometry,
|
2026-03-06 09:56:26 -06:00
|
|
|
) {
|
2026-03-06 10:26:23 -06:00
|
|
|
let Some((grid_x, grid_y)) = pointer_to_grid(pointer_pos, geometry) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if drag.corridor_index >= self.layout.corridors.len() {
|
|
|
|
|
self.drag_state = None;
|
2026-03-06 09:56:26 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
let target = (
|
|
|
|
|
(grid_x.floor().max(0.0) as usize).min(self.settings.cols.saturating_sub(1)),
|
|
|
|
|
(grid_y.floor().max(0.0) as usize).min(self.settings.rows.saturating_sub(1)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let Some(new_path) = reroute_path_through_cell(
|
|
|
|
|
&drag.original_path,
|
|
|
|
|
drag.original_cell,
|
|
|
|
|
target,
|
|
|
|
|
self.settings.cols,
|
|
|
|
|
self.settings.rows,
|
|
|
|
|
) else {
|
|
|
|
|
return;
|
2026-03-06 09:56:26 -06:00
|
|
|
};
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
self.layout.corridors[drag.corridor_index].path = new_path;
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2026-03-06 09:56:26 -06:00
|
|
|
struct RoomDragState {
|
|
|
|
|
room_idx: usize,
|
|
|
|
|
offset_x: f32,
|
|
|
|
|
offset_y: f32,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
struct CorridorDragState {
|
|
|
|
|
corridor_index: usize,
|
|
|
|
|
original_path: Vec<(usize, usize)>,
|
|
|
|
|
original_cell: (usize, usize),
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
enum DragState {
|
|
|
|
|
Room(RoomDragState),
|
|
|
|
|
Corridor(CorridorDragState),
|
2026-03-06 09:24:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 08:41:39 -06:00
|
|
|
struct GridGeometry {
|
|
|
|
|
rect: egui::Rect,
|
|
|
|
|
cell_size: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_grid(painter: &egui::Painter, area: egui::Rect, cols: usize, rows: usize) -> GridGeometry {
|
2026-03-05 19:17:01 -06:00
|
|
|
let cols = cols.max(1);
|
|
|
|
|
let rows = rows.max(1);
|
|
|
|
|
|
|
|
|
|
let cell_size = (area.width() / cols as f32).min(area.height() / rows as f32);
|
|
|
|
|
let grid_width = cell_size * cols as f32;
|
|
|
|
|
let grid_height = cell_size * rows as f32;
|
|
|
|
|
|
|
|
|
|
let grid_rect = egui::Rect::from_min_size(
|
|
|
|
|
egui::pos2(
|
|
|
|
|
area.center().x - (grid_width * 0.5),
|
|
|
|
|
area.center().y - (grid_height * 0.5),
|
|
|
|
|
),
|
|
|
|
|
egui::vec2(grid_width, grid_height),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let stroke = Stroke::new(1.0, Color32::from_gray(160));
|
|
|
|
|
painter.rect_stroke(grid_rect, 0.0, stroke, egui::StrokeKind::Middle);
|
|
|
|
|
|
|
|
|
|
for col in 1..cols {
|
|
|
|
|
let x = grid_rect.left() + (col as f32 * cell_size);
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(x, grid_rect.top()),
|
|
|
|
|
egui::pos2(x, grid_rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for row in 1..rows {
|
|
|
|
|
let y = grid_rect.top() + (row as f32 * cell_size);
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(grid_rect.left(), y),
|
|
|
|
|
egui::pos2(grid_rect.right(), y),
|
|
|
|
|
],
|
|
|
|
|
stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-06 08:41:39 -06:00
|
|
|
|
|
|
|
|
GridGeometry {
|
|
|
|
|
rect: grid_rect,
|
|
|
|
|
cell_size,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw_layout(painter: &egui::Painter, geometry: &GridGeometry, layout: &DungeonLayout) {
|
|
|
|
|
let room_fill = Color32::from_rgb(70, 120, 160);
|
|
|
|
|
let corridor_fill = Color32::from_rgb(210, 190, 120);
|
2026-03-06 09:19:03 -06:00
|
|
|
let outline_stroke = Stroke::new(3.0, Color32::BLACK);
|
|
|
|
|
let mut corridor_cells_set = HashSet::new();
|
|
|
|
|
let mut corridor_edges_set = HashSet::new();
|
2026-03-06 08:41:39 -06:00
|
|
|
|
|
|
|
|
for corridor in &layout.corridors {
|
2026-03-06 10:26:23 -06:00
|
|
|
for &cell in &corridor.path {
|
|
|
|
|
corridor_cells_set.insert(cell);
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|
2026-03-06 10:26:23 -06:00
|
|
|
for pair in corridor.path.windows(2) {
|
2026-03-06 09:56:26 -06:00
|
|
|
corridor_edges_set.insert(normalized_edge(pair[0], pair[1]));
|
2026-03-06 09:19:03 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for &(col, row) in &corridor_cells_set {
|
|
|
|
|
painter.rect_filled(cell_rect(geometry, col, row), 0.0, corridor_fill);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for &(col, row) in &corridor_cells_set {
|
|
|
|
|
let rect = cell_rect(geometry, col, row);
|
|
|
|
|
let right = (col + 1, row);
|
|
|
|
|
let bottom = (col, row + 1);
|
|
|
|
|
|
|
|
|
|
if col == 0 || !corridor_cells_set.contains(&(col - 1, row)) {
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.top()),
|
|
|
|
|
egui::pos2(rect.left(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if !corridor_cells_set.contains(&right)
|
|
|
|
|
|| !corridor_edges_set.contains(&normalized_edge((col, row), right))
|
|
|
|
|
{
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.right(), rect.top()),
|
|
|
|
|
egui::pos2(rect.right(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if row == 0 || !corridor_cells_set.contains(&(col, row - 1)) {
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.top()),
|
|
|
|
|
egui::pos2(rect.right(), rect.top()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if !corridor_cells_set.contains(&bottom)
|
|
|
|
|
|| !corridor_edges_set.contains(&normalized_edge((col, row), bottom))
|
|
|
|
|
{
|
|
|
|
|
painter.line_segment(
|
|
|
|
|
[
|
|
|
|
|
egui::pos2(rect.left(), rect.bottom()),
|
|
|
|
|
egui::pos2(rect.right(), rect.bottom()),
|
|
|
|
|
],
|
|
|
|
|
outline_stroke,
|
|
|
|
|
);
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for room in &layout.rooms {
|
|
|
|
|
let left = geometry.rect.left() + room.x as f32 * geometry.cell_size;
|
|
|
|
|
let top = geometry.rect.top() + room.y as f32 * geometry.cell_size;
|
|
|
|
|
let right = left + room.width as f32 * geometry.cell_size;
|
|
|
|
|
let bottom = top + room.height as f32 * geometry.cell_size;
|
|
|
|
|
let room_rect = egui::Rect::from_min_max(egui::pos2(left, top), egui::pos2(right, bottom));
|
|
|
|
|
|
|
|
|
|
painter.rect_filled(room_rect, 0.0, room_fill);
|
2026-03-06 09:10:40 -06:00
|
|
|
painter.rect_stroke(room_rect, 0.0, outline_stroke, egui::StrokeKind::Middle);
|
2026-03-06 08:41:39 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cell_rect(geometry: &GridGeometry, col: usize, row: usize) -> egui::Rect {
|
|
|
|
|
let left = geometry.rect.left() + col as f32 * geometry.cell_size;
|
|
|
|
|
let top = geometry.rect.top() + row as f32 * geometry.cell_size;
|
|
|
|
|
egui::Rect::from_min_size(
|
|
|
|
|
egui::pos2(left, top),
|
|
|
|
|
egui::vec2(geometry.cell_size, geometry.cell_size),
|
|
|
|
|
)
|
2026-03-05 19:17:01 -06:00
|
|
|
}
|
2026-03-06 09:19:03 -06:00
|
|
|
|
|
|
|
|
fn normalized_edge(a: (usize, usize), b: (usize, usize)) -> ((usize, usize), (usize, usize)) {
|
|
|
|
|
if a <= b { (a, b) } else { (b, a) }
|
|
|
|
|
}
|
2026-03-06 09:56:26 -06:00
|
|
|
|
|
|
|
|
fn pointer_to_grid(pointer_pos: egui::Pos2, geometry: &GridGeometry) -> Option<(f32, f32)> {
|
|
|
|
|
if !geometry.rect.contains(pointer_pos) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let x = (pointer_pos.x - geometry.rect.left()) / geometry.cell_size;
|
|
|
|
|
let y = (pointer_pos.y - geometry.rect.top()) / geometry.cell_size;
|
|
|
|
|
Some((x, y))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 10:26:23 -06:00
|
|
|
fn reroute_path_through_cell(
|
|
|
|
|
original_path: &[(usize, usize)],
|
|
|
|
|
original_cell: (usize, usize),
|
|
|
|
|
target_cell: (usize, usize),
|
2026-03-06 09:56:26 -06:00
|
|
|
cols: usize,
|
|
|
|
|
rows: usize,
|
2026-03-06 10:26:23 -06:00
|
|
|
) -> Option<Vec<(usize, usize)>> {
|
|
|
|
|
let (&start, &end) = (original_path.first()?, original_path.last()?);
|
|
|
|
|
if start == end {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut blocked = HashSet::new();
|
|
|
|
|
blocked.insert(original_cell);
|
|
|
|
|
blocked.remove(&start);
|
|
|
|
|
blocked.remove(&end);
|
|
|
|
|
blocked.remove(&target_cell);
|
|
|
|
|
|
|
|
|
|
let first_leg = shortest_path_cells(start, target_cell, cols, rows, &blocked)?;
|
|
|
|
|
|
|
|
|
|
let mut blocked_second = blocked.clone();
|
|
|
|
|
for &cell in first_leg
|
|
|
|
|
.iter()
|
|
|
|
|
.skip(1)
|
|
|
|
|
.take(first_leg.len().saturating_sub(2))
|
|
|
|
|
{
|
|
|
|
|
if cell != target_cell && cell != end {
|
|
|
|
|
blocked_second.insert(cell);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let second_leg = shortest_path_cells(target_cell, end, cols, rows, &blocked_second)
|
|
|
|
|
.or_else(|| shortest_path_cells(target_cell, end, cols, rows, &blocked))?;
|
|
|
|
|
|
|
|
|
|
let mut full_path = first_leg;
|
|
|
|
|
for cell in second_leg.into_iter().skip(1) {
|
|
|
|
|
if full_path.last().copied() != Some(cell) {
|
|
|
|
|
full_path.push(cell);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify_path_loops(&mut full_path);
|
|
|
|
|
|
|
|
|
|
if full_path.len() < 2 || !full_path.contains(&target_cell) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if original_cell != start && original_cell != end && full_path.contains(&original_cell) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(full_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn simplify_path_loops(path: &mut Vec<(usize, usize)>) {
|
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
for &cell in path.iter() {
|
|
|
|
|
if let Some(pos) = out.iter().position(|&c| c == cell) {
|
|
|
|
|
out.truncate(pos + 1);
|
|
|
|
|
} else {
|
|
|
|
|
out.push(cell);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*path = out;
|
2026-03-06 09:56:26 -06:00
|
|
|
}
|