cleanup
This commit is contained in:
@@ -1,49 +0,0 @@
|
|||||||
use rand::{RngExt, SeedableRng, rngs::StdRng};
|
|
||||||
|
|
||||||
use crate::config::TERRAIN_COLS;
|
|
||||||
|
|
||||||
const SALT_DOOR: u64 = 1;
|
|
||||||
|
|
||||||
const DOOR_HALF_WIDTH: i32 = 4;
|
|
||||||
const DOOR_MIN_WIDTH: i32 = 2;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub struct Door {
|
|
||||||
pub min_col: i32,
|
|
||||||
pub max_col: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Door {
|
|
||||||
pub fn width(&self) -> i32 {
|
|
||||||
self.max_col - self.min_col + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn door(seed: u64, k: i64) -> Option<Door> {
|
|
||||||
if k < 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chunk 0
|
|
||||||
if k == 0 {
|
|
||||||
let center = TERRAIN_COLS / 2;
|
|
||||||
return Some(Door {
|
|
||||||
min_col: center - DOOR_HALF_WIDTH,
|
|
||||||
max_col: center + DOOR_HALF_WIDTH - 1,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut rng = StdRng::seed_from_u64(seed ^ (k as u64).wrapping_mul(SALT_DOOR));
|
|
||||||
|
|
||||||
// Random width
|
|
||||||
let width = DOOR_MIN_WIDTH + rng.random_range(0..(2 * DOOR_HALF_WIDTH - DOOR_MIN_WIDTH + 1));
|
|
||||||
|
|
||||||
// Door stays in terrain
|
|
||||||
let center = DOOR_HALF_WIDTH + rng.random_range(0..(TERRAIN_COLS - 2 * DOOR_HALF_WIDTH));
|
|
||||||
|
|
||||||
let min_col = (center - width / 2).clamp(1, TERRAIN_COLS - 1 - width);
|
|
||||||
Some(Door {
|
|
||||||
min_col,
|
|
||||||
max_col: min_col + width - 1,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
use crate::config::{
|
|
||||||
CELL_SIZE, CHUNK_ROWS, DESIGN_SLOPE_COLS_PER_ROW, HALF_TERRAIN_WIDTH, INERTIA_MARGIN,
|
|
||||||
PLAYER_HITBOX, TERRAIN_COLS,
|
|
||||||
};
|
|
||||||
use crate::world::{doors::Door, grid::GridRect};
|
|
||||||
|
|
||||||
const INFLATE_X: f32 = PLAYER_HITBOX.x / 2.0 + INERTIA_MARGIN;
|
|
||||||
const INFLATE_Y: f32 = PLAYER_HITBOX.y / 2.0 + INERTIA_MARGIN;
|
|
||||||
|
|
||||||
const EXPAND_PER_ROW: f32 = DESIGN_SLOPE_COLS_PER_ROW * CELL_SIZE;
|
|
||||||
|
|
||||||
fn door_px(door: Door) -> (f32, f32) {
|
|
||||||
let offset = TERRAIN_COLS as f32 / 2.0;
|
|
||||||
(
|
|
||||||
(door.min_col as f32 - offset) * CELL_SIZE,
|
|
||||||
(door.max_col as f32 + 1.0 - offset) * CELL_SIZE,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn free_intervals(rects: &[GridRect], row: i32) -> Vec<(f32, f32)> {
|
|
||||||
let row_y0 = row as f32 * CELL_SIZE;
|
|
||||||
let row_y1 = row_y0 + CELL_SIZE;
|
|
||||||
let mut blocked: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for r in rects {
|
|
||||||
let y0 = r.row as f32 * CELL_SIZE - INFLATE_Y;
|
|
||||||
let y1 = (r.row + r.height) as f32 * CELL_SIZE + INFLATE_Y;
|
|
||||||
if y1 <= row_y0 || y0 >= row_y1 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let offset = TERRAIN_COLS as f32 / 2.0;
|
|
||||||
let x0 = (r.col as f32 - offset) * CELL_SIZE - INFLATE_X;
|
|
||||||
let x1 = ((r.col + r.width) as f32 - offset) * CELL_SIZE + INFLATE_X;
|
|
||||||
blocked.push((x0, x1));
|
|
||||||
}
|
|
||||||
blocked = merge(blocked);
|
|
||||||
|
|
||||||
let mut free: Vec<(f32, f32)> = vec![(
|
|
||||||
-HALF_TERRAIN_WIDTH + INFLATE_X,
|
|
||||||
HALF_TERRAIN_WIDTH - INFLATE_X,
|
|
||||||
)];
|
|
||||||
for (a, b) in &blocked {
|
|
||||||
free = subtract_all(&free, *a, *b);
|
|
||||||
}
|
|
||||||
free
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate(rects: &[GridRect], entry: Door, exit: Door) -> bool {
|
|
||||||
let mut safe: Vec<(f32, f32)> = vec![door_px(exit)];
|
|
||||||
|
|
||||||
for row in (0..CHUNK_ROWS).rev() {
|
|
||||||
let free = free_intervals(rects, row);
|
|
||||||
|
|
||||||
let mut next: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for f in &free {
|
|
||||||
for (a, b) in &safe {
|
|
||||||
if let Some((i0, i1)) = intersect((a - EXPAND_PER_ROW, b + EXPAND_PER_ROW), *f) {
|
|
||||||
next.push((i0, i1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
safe = merge(next);
|
|
||||||
if safe.is_empty() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let e = door_px(entry);
|
|
||||||
safe.iter().any(|(a, b)| *a <= e.0 && e.1 <= *b)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn intersect((a0, a1): (f32, f32), (b0, b1): (f32, f32)) -> Option<(f32, f32)> {
|
|
||||||
let lo = a0.max(b0);
|
|
||||||
let hi = a1.min(b1);
|
|
||||||
(lo < hi).then_some((lo, hi))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn merge(mut list: Vec<(f32, f32)>) -> Vec<(f32, f32)> {
|
|
||||||
list.sort_by(|a, b| a.0.total_cmp(&b.0));
|
|
||||||
let mut out: Vec<(f32, f32)> = Vec::new();
|
|
||||||
for (a, b) in list {
|
|
||||||
match out.last_mut() {
|
|
||||||
Some(last) if a <= last.1 => last.1 = last.1.max(b),
|
|
||||||
_ => out.push((a, b)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subtract_all(free: &[(f32, f32)], a: f32, b: f32) -> Vec<(f32, f32)> {
|
|
||||||
let mut out = Vec::new();
|
|
||||||
for (x0, x1) in free {
|
|
||||||
if b <= *x0 || a >= *x1 {
|
|
||||||
out.push((*x0, *x1));
|
|
||||||
} else {
|
|
||||||
if a > *x0 {
|
|
||||||
out.push((*x0, a));
|
|
||||||
}
|
|
||||||
if b < *x1 {
|
|
||||||
out.push((b, *x1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user