allowed users to move corredors
This commit is contained in:
+43
-83
@@ -1,6 +1,7 @@
|
|||||||
use crate::seed;
|
|
||||||
use std::collections::{HashSet, VecDeque};
|
use std::collections::{HashSet, VecDeque};
|
||||||
|
|
||||||
|
use crate::seed;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Room {
|
pub struct Room {
|
||||||
pub x: usize,
|
pub x: usize,
|
||||||
@@ -17,8 +18,11 @@ impl Room {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Corridor {
|
pub struct Corridor {
|
||||||
pub from: (usize, usize),
|
#[allow(dead_code)]
|
||||||
pub to: (usize, usize),
|
pub id: u64,
|
||||||
|
pub start_room_id: usize,
|
||||||
|
pub end_room_id: usize,
|
||||||
|
pub path: Vec<(usize, usize)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
@@ -49,6 +53,7 @@ pub fn generate_layout(
|
|||||||
let mut room_rng = SimpleRng::new(seed::derive_seed(base_seed, 1));
|
let mut room_rng = SimpleRng::new(seed::derive_seed(base_seed, 1));
|
||||||
let mut graph_rng = SimpleRng::new(seed::derive_seed(base_seed, 2));
|
let mut graph_rng = SimpleRng::new(seed::derive_seed(base_seed, 2));
|
||||||
let mut path_rng = SimpleRng::new(seed::derive_seed(base_seed, 3));
|
let mut path_rng = SimpleRng::new(seed::derive_seed(base_seed, 3));
|
||||||
|
|
||||||
let mut rooms = Vec::new();
|
let mut rooms = Vec::new();
|
||||||
let mut corridors = Vec::new();
|
let mut corridors = Vec::new();
|
||||||
|
|
||||||
@@ -124,31 +129,36 @@ pub fn generate_layout(
|
|||||||
let target_dead_end_rooms = ((rooms.len() * dead_end_room_percent.min(50)) + 50) / 100;
|
let target_dead_end_rooms = ((rooms.len() * dead_end_room_percent.min(50)) + 50) / 100;
|
||||||
let room_edges =
|
let room_edges =
|
||||||
build_room_connection_edges(¢ers, randomness, target_dead_end_rooms, &mut graph_rng);
|
build_room_connection_edges(¢ers, randomness, target_dead_end_rooms, &mut graph_rng);
|
||||||
let mut occupied_corridor_cells = HashSet::new();
|
|
||||||
|
|
||||||
for (a_idx, b_idx) in room_edges {
|
let mut next_corridor_id = 1u64;
|
||||||
let a = centers[a_idx];
|
for (start_room_id, end_room_id) in room_edges {
|
||||||
let b = centers[b_idx];
|
let start = centers[start_room_id];
|
||||||
|
let end = centers[end_room_id];
|
||||||
|
|
||||||
let preferred = if randomness <= 0.001 {
|
let path = if randomness <= 0.001 {
|
||||||
shortest_path(a, b, cols, rows).unwrap_or_else(|| vec![a, b])
|
shortest_path_cells(start, end, cols, rows, &HashSet::new())
|
||||||
} else {
|
} else {
|
||||||
noisy_path(a, b, cols, rows, randomness, &mut path_rng)
|
Some(noisy_path(
|
||||||
};
|
start,
|
||||||
|
end,
|
||||||
|
cols,
|
||||||
|
rows,
|
||||||
|
randomness,
|
||||||
|
&mut path_rng,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
.or_else(|| shortest_path_cells(start, end, cols, rows, &HashSet::new()));
|
||||||
|
|
||||||
if !try_place_cell_path(
|
if let Some(path) = path
|
||||||
&preferred,
|
&& path.len() >= 2
|
||||||
&mut occupied_corridor_cells,
|
{
|
||||||
&mut corridors,
|
corridors.push(Corridor {
|
||||||
true,
|
id: next_corridor_id,
|
||||||
) {
|
start_room_id,
|
||||||
let fallback = shortest_path(a, b, cols, rows).unwrap_or_else(|| vec![a, b]);
|
end_room_id,
|
||||||
let _ = try_place_cell_path(
|
path,
|
||||||
&fallback,
|
});
|
||||||
&mut occupied_corridor_cells,
|
next_corridor_id = next_corridor_id.wrapping_add(1);
|
||||||
&mut corridors,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,72 +283,19 @@ fn shuffle_indices(indices: &mut [usize], rng: &mut SimpleRng) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_place_cell_path(
|
pub fn shortest_path_cells(
|
||||||
path: &[(usize, usize)],
|
|
||||||
occupied: &mut HashSet<(usize, usize)>,
|
|
||||||
corridors: &mut Vec<Corridor>,
|
|
||||||
enforce_gap: bool,
|
|
||||||
) -> bool {
|
|
||||||
if path.len() < 2 {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if enforce_gap && !has_required_corridor_gap(path, occupied) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for segment in path.windows(2) {
|
|
||||||
if segment[0] != segment[1] {
|
|
||||||
corridors.push(Corridor {
|
|
||||||
from: segment[0],
|
|
||||||
to: segment[1],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for &cell in path {
|
|
||||||
occupied.insert(cell);
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_required_corridor_gap(path: &[(usize, usize)], occupied: &HashSet<(usize, usize)>) -> bool {
|
|
||||||
if path.len() < 3 {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx in 1..(path.len() - 1) {
|
|
||||||
let (x, y) = path[idx];
|
|
||||||
let x = x as isize;
|
|
||||||
let y = y as isize;
|
|
||||||
|
|
||||||
for dx in -1..=1 {
|
|
||||||
for dy in -1..=1 {
|
|
||||||
let nx = x + dx;
|
|
||||||
let ny = y + dy;
|
|
||||||
if nx < 0 || ny < 0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if occupied.contains(&(nx as usize, ny as usize)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn shortest_path(
|
|
||||||
start: (usize, usize),
|
start: (usize, usize),
|
||||||
end: (usize, usize),
|
end: (usize, usize),
|
||||||
cols: usize,
|
cols: usize,
|
||||||
rows: usize,
|
rows: usize,
|
||||||
|
blocked: &HashSet<(usize, usize)>,
|
||||||
) -> Option<Vec<(usize, usize)>> {
|
) -> Option<Vec<(usize, usize)>> {
|
||||||
if start == end {
|
if start == end {
|
||||||
return Some(vec![start]);
|
return Some(vec![start]);
|
||||||
}
|
}
|
||||||
|
if blocked.contains(&start) || blocked.contains(&end) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let total = cols.saturating_mul(rows);
|
let total = cols.saturating_mul(rows);
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
@@ -372,6 +329,9 @@ fn shortest_path(
|
|||||||
];
|
];
|
||||||
|
|
||||||
for neighbor in neighbors.into_iter().flatten() {
|
for neighbor in neighbors.into_iter().flatten() {
|
||||||
|
if blocked.contains(&neighbor) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let n_idx = index(neighbor);
|
let n_idx = index(neighbor);
|
||||||
if !visited[n_idx] {
|
if !visited[n_idx] {
|
||||||
visited[n_idx] = true;
|
visited[n_idx] = true;
|
||||||
@@ -482,7 +442,7 @@ fn noisy_path(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if current != end
|
if current != end
|
||||||
&& let Some(tail) = shortest_path(current, end, cols, rows)
|
&& let Some(tail) = shortest_path_cells(current, end, cols, rows, &HashSet::new())
|
||||||
{
|
{
|
||||||
for &cell in tail.iter().skip(1) {
|
for &cell in tail.iter().skip(1) {
|
||||||
path.push(cell);
|
path.push(cell);
|
||||||
|
|||||||
+162
-104
@@ -7,7 +7,7 @@ use std::collections::HashSet;
|
|||||||
|
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use egui::{Color32, Stroke};
|
use egui::{Color32, Stroke};
|
||||||
use layout::{DungeonLayout, generate_layout};
|
use layout::{DungeonLayout, shortest_path_cells};
|
||||||
use ui::{UiSettings, draw_side_panel};
|
use ui::{UiSettings, draw_side_panel};
|
||||||
|
|
||||||
fn main() -> eframe::Result<()> {
|
fn main() -> eframe::Result<()> {
|
||||||
@@ -23,13 +23,13 @@ fn main() -> eframe::Result<()> {
|
|||||||
struct DungeonApp {
|
struct DungeonApp {
|
||||||
settings: UiSettings,
|
settings: UiSettings,
|
||||||
layout: DungeonLayout,
|
layout: DungeonLayout,
|
||||||
room_drag: Option<RoomDragState>,
|
drag_state: Option<DragState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DungeonApp {
|
impl Default for DungeonApp {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let settings = settings::load_settings().unwrap_or_default();
|
let settings = settings::load_settings().unwrap_or_default();
|
||||||
let layout = generate_layout(
|
let layout = layout::generate_layout(
|
||||||
settings.cols,
|
settings.cols,
|
||||||
settings.rows,
|
settings.rows,
|
||||||
settings.room_count,
|
settings.room_count,
|
||||||
@@ -43,7 +43,7 @@ impl Default for DungeonApp {
|
|||||||
Self {
|
Self {
|
||||||
settings,
|
settings,
|
||||||
layout,
|
layout,
|
||||||
room_drag: None,
|
drag_state: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,11 +64,7 @@ impl eframe::App for DungeonApp {
|
|||||||
self.settings.max_room_size = self.settings.min_room_size;
|
self.settings.max_room_size = self.settings.min_room_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if panel_result.reset_clicked {
|
if panel_result.reset_clicked || panel_result.settings_changed {
|
||||||
self.regenerate_layout();
|
|
||||||
}
|
|
||||||
|
|
||||||
if panel_result.settings_changed {
|
|
||||||
self.regenerate_layout();
|
self.regenerate_layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +73,7 @@ impl eframe::App for DungeonApp {
|
|||||||
let (response, painter) = ui.allocate_painter(available, egui::Sense::click_and_drag());
|
let (response, painter) = ui.allocate_painter(available, egui::Sense::click_and_drag());
|
||||||
let canvas = response.rect.shrink(12.0);
|
let canvas = response.rect.shrink(12.0);
|
||||||
let geometry = draw_grid(&painter, canvas, self.settings.cols, self.settings.rows);
|
let geometry = draw_grid(&painter, canvas, self.settings.cols, self.settings.rows);
|
||||||
self.handle_room_drag(&response, &geometry);
|
self.handle_drag(&response, &geometry);
|
||||||
draw_layout(&painter, &geometry, &self.layout);
|
draw_layout(&painter, &geometry, &self.layout);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -85,8 +81,8 @@ impl eframe::App for DungeonApp {
|
|||||||
|
|
||||||
impl DungeonApp {
|
impl DungeonApp {
|
||||||
fn regenerate_layout(&mut self) {
|
fn regenerate_layout(&mut self) {
|
||||||
self.room_drag = None;
|
self.drag_state = None;
|
||||||
self.layout = generate_layout(
|
self.layout = layout::generate_layout(
|
||||||
self.settings.cols,
|
self.settings.cols,
|
||||||
self.settings.rows,
|
self.settings.rows,
|
||||||
self.settings.room_count,
|
self.settings.room_count,
|
||||||
@@ -99,28 +95,38 @@ impl DungeonApp {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_room_drag(&mut self, response: &egui::Response, geometry: &GridGeometry) {
|
fn handle_drag(&mut self, response: &egui::Response, geometry: &GridGeometry) {
|
||||||
if response.drag_started()
|
if response.drag_started()
|
||||||
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
||||||
&& let Some((room_idx, offset_x, offset_y)) =
|
&& let Some((room_idx, offset_x, offset_y)) =
|
||||||
self.room_at_pointer(pointer_pos, geometry)
|
self.room_at_pointer(pointer_pos, geometry)
|
||||||
{
|
{
|
||||||
self.room_drag = Some(RoomDragState {
|
self.drag_state = Some(DragState::Room(RoomDragState {
|
||||||
room_idx,
|
room_idx,
|
||||||
offset_x,
|
offset_x,
|
||||||
offset_y,
|
offset_y,
|
||||||
});
|
}));
|
||||||
|
} 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.dragged()
|
if response.dragged()
|
||||||
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
&& let Some(pointer_pos) = response.interact_pointer_pos()
|
||||||
&& let Some(drag) = self.room_drag
|
&& let Some(drag_state) = self.drag_state.clone()
|
||||||
{
|
{
|
||||||
self.drag_room_to_pointer(drag, pointer_pos, geometry);
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.drag_stopped() || !response.ctx.input(|i| i.pointer.primary_down()) {
|
if response.drag_stopped() || !response.ctx.input(|i| i.pointer.primary_down()) {
|
||||||
self.room_drag = None;
|
self.drag_state = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,13 +162,13 @@ impl DungeonApp {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if drag.room_idx >= self.layout.rooms.len() {
|
if drag.room_idx >= self.layout.rooms.len() {
|
||||||
self.room_drag = None;
|
self.drag_state = None;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (old_x, old_y, width, height) = {
|
let (width, height, old_x, old_y) = {
|
||||||
let room = &self.layout.rooms[drag.room_idx];
|
let room = &self.layout.rooms[drag.room_idx];
|
||||||
(room.x, room.y, room.width, room.height)
|
(room.width, room.height, room.x, room.y)
|
||||||
};
|
};
|
||||||
let max_x = self.settings.cols.saturating_sub(width);
|
let max_x = self.settings.cols.saturating_sub(width);
|
||||||
let max_y = self.settings.rows.saturating_sub(height);
|
let max_y = self.settings.rows.saturating_sub(height);
|
||||||
@@ -178,68 +184,96 @@ impl DungeonApp {
|
|||||||
|
|
||||||
self.layout.rooms[drag.room_idx].x = new_x;
|
self.layout.rooms[drag.room_idx].x = new_x;
|
||||||
self.layout.rooms[drag.room_idx].y = new_y;
|
self.layout.rooms[drag.room_idx].y = new_y;
|
||||||
self.adjust_corridors_for_room_move(old_x, old_y, width, height, new_x, new_y);
|
self.reroute_corridors_for_room(drag.room_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn adjust_corridors_for_room_move(
|
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(
|
||||||
&mut self,
|
&mut self,
|
||||||
old_x: usize,
|
drag: CorridorDragState,
|
||||||
old_y: usize,
|
pointer_pos: egui::Pos2,
|
||||||
width: usize,
|
geometry: &GridGeometry,
|
||||||
height: usize,
|
|
||||||
new_x: usize,
|
|
||||||
new_y: usize,
|
|
||||||
) {
|
) {
|
||||||
let dx = new_x as isize - old_x as isize;
|
let Some((grid_x, grid_y)) = pointer_to_grid(pointer_pos, geometry) else {
|
||||||
let dy = new_y as isize - old_y as isize;
|
return;
|
||||||
if dx == 0 && dy == 0 {
|
};
|
||||||
|
|
||||||
|
if drag.corridor_index >= self.layout.corridors.len() {
|
||||||
|
self.drag_state = None;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let room_cells = RoomCells {
|
let target = (
|
||||||
x: old_x,
|
(grid_x.floor().max(0.0) as usize).min(self.settings.cols.saturating_sub(1)),
|
||||||
y: old_y,
|
(grid_y.floor().max(0.0) as usize).min(self.settings.rows.saturating_sub(1)),
|
||||||
width,
|
);
|
||||||
height,
|
|
||||||
|
let Some(new_path) = reroute_path_through_cell(
|
||||||
|
&drag.original_path,
|
||||||
|
drag.original_cell,
|
||||||
|
target,
|
||||||
|
self.settings.cols,
|
||||||
|
self.settings.rows,
|
||||||
|
) else {
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
for corridor in &mut self.layout.corridors {
|
self.layout.corridors[drag.corridor_index].path = new_path;
|
||||||
if room_cells.contains(corridor.from) {
|
|
||||||
corridor.from = shift_cell(
|
|
||||||
corridor.from,
|
|
||||||
dx,
|
|
||||||
dy,
|
|
||||||
self.settings.cols,
|
|
||||||
self.settings.rows,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if room_cells.contains(corridor.to) {
|
|
||||||
corridor.to =
|
|
||||||
shift_cell(corridor.to, dx, dy, self.settings.cols, self.settings.rows);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
struct RoomDragState {
|
struct RoomDragState {
|
||||||
room_idx: usize,
|
room_idx: usize,
|
||||||
offset_x: f32,
|
offset_x: f32,
|
||||||
offset_y: f32,
|
offset_y: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RoomCells {
|
#[derive(Debug, Clone)]
|
||||||
x: usize,
|
struct CorridorDragState {
|
||||||
y: usize,
|
corridor_index: usize,
|
||||||
width: usize,
|
original_path: Vec<(usize, usize)>,
|
||||||
height: usize,
|
original_cell: (usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RoomCells {
|
#[derive(Debug, Clone)]
|
||||||
fn contains(&self, cell: (usize, usize)) -> bool {
|
enum DragState {
|
||||||
let (cx, cy) = cell;
|
Room(RoomDragState),
|
||||||
cx >= self.x && cx < self.x + self.width && cy >= self.y && cy < self.y + self.height
|
Corridor(CorridorDragState),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct GridGeometry {
|
struct GridGeometry {
|
||||||
@@ -302,11 +336,10 @@ fn draw_layout(painter: &egui::Painter, geometry: &GridGeometry, layout: &Dungeo
|
|||||||
let mut corridor_edges_set = HashSet::new();
|
let mut corridor_edges_set = HashSet::new();
|
||||||
|
|
||||||
for corridor in &layout.corridors {
|
for corridor in &layout.corridors {
|
||||||
let cells = corridor_cells(corridor.from, corridor.to);
|
for &cell in &corridor.path {
|
||||||
for cell in &cells {
|
corridor_cells_set.insert(cell);
|
||||||
corridor_cells_set.insert(*cell);
|
|
||||||
}
|
}
|
||||||
for pair in cells.windows(2) {
|
for pair in corridor.path.windows(2) {
|
||||||
corridor_edges_set.insert(normalized_edge(pair[0], pair[1]));
|
corridor_edges_set.insert(normalized_edge(pair[0], pair[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -374,35 +407,6 @@ fn draw_layout(painter: &egui::Painter, geometry: &GridGeometry, layout: &Dungeo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn corridor_cells(from: (usize, usize), to: (usize, usize)) -> Vec<(usize, usize)> {
|
|
||||||
let mut cells = Vec::new();
|
|
||||||
if from.0 == to.0 {
|
|
||||||
let x = from.0;
|
|
||||||
let start = from.1.min(to.1);
|
|
||||||
let end = from.1.max(to.1);
|
|
||||||
for y in start..=end {
|
|
||||||
cells.push((x, y));
|
|
||||||
}
|
|
||||||
} else if from.1 == to.1 {
|
|
||||||
let y = from.1;
|
|
||||||
let start = from.0.min(to.0);
|
|
||||||
let end = from.0.max(to.0);
|
|
||||||
for x in start..=end {
|
|
||||||
cells.push((x, y));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If an endpoint drift makes a segment diagonal, render it as an L path.
|
|
||||||
let corner = (to.0, from.1);
|
|
||||||
cells.extend(corridor_cells(from, corner));
|
|
||||||
for cell in corridor_cells(corner, to) {
|
|
||||||
if cells.last().copied() != Some(cell) {
|
|
||||||
cells.push(cell);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cells
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cell_rect(geometry: &GridGeometry, col: usize, row: usize) -> egui::Rect {
|
fn cell_rect(geometry: &GridGeometry, col: usize, row: usize) -> egui::Rect {
|
||||||
let left = geometry.rect.left() + col as f32 * geometry.cell_size;
|
let left = geometry.rect.left() + col as f32 * geometry.cell_size;
|
||||||
let top = geometry.rect.top() + row as f32 * geometry.cell_size;
|
let top = geometry.rect.top() + row as f32 * geometry.cell_size;
|
||||||
@@ -426,14 +430,68 @@ fn pointer_to_grid(pointer_pos: egui::Pos2, geometry: &GridGeometry) -> Option<(
|
|||||||
Some((x, y))
|
Some((x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shift_cell(
|
fn reroute_path_through_cell(
|
||||||
cell: (usize, usize),
|
original_path: &[(usize, usize)],
|
||||||
dx: isize,
|
original_cell: (usize, usize),
|
||||||
dy: isize,
|
target_cell: (usize, usize),
|
||||||
cols: usize,
|
cols: usize,
|
||||||
rows: usize,
|
rows: usize,
|
||||||
) -> (usize, usize) {
|
) -> Option<Vec<(usize, usize)>> {
|
||||||
let x = (cell.0 as isize + dx).clamp(0, cols.saturating_sub(1) as isize) as usize;
|
let (&start, &end) = (original_path.first()?, original_path.last()?);
|
||||||
let y = (cell.1 as isize + dy).clamp(0, rows.saturating_sub(1) as isize) as usize;
|
if start == end {
|
||||||
(x, y)
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user