use crate::class::{Class, flag};
use crate::world::{BlockSource, Grid};
pub(crate) const HEIGHT: f64 = 1.8;
pub(crate) const STEP: f64 = 0.6;
pub(crate) const JUMP_APEX: f64 = 1.2492;
pub(crate) const MAX_JUMP_UP: f64 = 1.0 + 1e-6;
pub(crate) const JUMP_MARGIN: f64 = 0.15;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NodeKind {
Ground,
Swim,
Climb,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MoveKind {
Start,
Walk,
Diagonal,
Ascend,
Descend,
Parkour {
gap: u8,
},
Swim,
ClimbUp,
ClimbDown,
ClimbExit,
DigDown,
Door {
block: [i32; 3],
},
Pillar,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Spot {
pub(crate) kind: NodeKind,
pub(crate) floor: f64,
pub(crate) floor_flags: u32,
pub(crate) water: bool,
pub(crate) submerged: bool,
pub(crate) climb: bool,
}
pub(crate) fn clear<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
z: i32,
lo: f64,
hi: f64,
) -> bool {
if hi <= lo {
return true;
}
let y0 = lo.floor() as i32 - 1;
let y1 = (hi - 1e-6).floor() as i32;
for y in y0..=y1 {
let c = g.class(x, y, z);
let fy = f64::from(y);
if fy < hi - 1e-6 && fy + 1.0 > lo + 1e-6 && c.has(flag::AVOID | flag::LAVA) {
return false;
}
let l = (lo - fy) as f32;
let h = (hi - fy) as f32;
if c.blocks_between(l, h) {
return false;
}
}
true
}
pub(crate) fn clear_standing<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
z: i32,
lo: f64,
hi: f64,
) -> bool {
if hi <= lo {
return true;
}
let y0 = lo.floor() as i32 - 1;
let y1 = (hi - 1e-6).floor() as i32;
for y in y0..=y1 {
let c = g.class(x, y, z);
let fy = f64::from(y);
if fy < hi - 1e-6 && fy + 1.0 > lo + 1e-6 && c.has(flag::AVOID | flag::LAVA) {
return false;
}
if c.blocks_standing((lo - fy) as f32, (hi - fy) as f32) {
return false;
}
}
true
}
pub(crate) fn ceiling<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
z: i32,
from: f64,
limit: f64,
) -> f64 {
let y0 = from.floor() as i32;
let y1 = (limit - 1e-6).floor() as i32;
for y in y0..=y1 {
let c = g.class(x, y, z);
let fy = f64::from(y);
if c.has(flag::AVOID | flag::LAVA) && fy + 1.0 > from {
return fy.max(from);
}
if c.blocks_between((from - fy) as f32, (limit - fy) as f32) {
let b = fy + f64::from(c.bottom);
return b.max(from);
}
}
limit
}
pub(crate) fn spot<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
y: i32,
z: i32,
) -> Option<Spot> {
let (lo, hi) = g.y_range();
if y <= lo || y >= hi - 1 {
return None;
}
let c0 = g.class(x, y, z);
if c0.has(flag::AVOID | flag::LAVA) {
return None;
}
let mut floor: Option<(f64, Class)> = None;
if c0.has(flag::SOLID) && !(c0.has(flag::THIN) && c0.has(flag::CLIMB)) && !c0.has(flag::PANEL) {
if c0.has(flag::SUPPORT)
&& !c0.has(flag::IRREGULAR)
&& c0.top < 1.0 - 1e-6
&& c0.bottom <= 1e-6
&& c0.top_any <= c0.top + 1e-6
{
floor = Some((f64::from(y) + f64::from(c0.top), c0));
} else {
return None;
}
} else {
let cb = g.class(x, y - 1, z);
if cb.has(flag::SUPPORT) && !cb.has(flag::IRREGULAR) && cb.top >= 1.0 - 1e-6 {
floor = Some((f64::from(y - 1) + f64::from(cb.top), cb));
}
}
let water = c0.has(flag::WATER);
let climb = c0.has(flag::CLIMB);
let (kind, base, floor_flags) = match floor {
Some((f, c)) => {
if c.has(flag::AVOID_FLOOR) || f >= f64::from(y + 1) - 1e-6 {
return None;
}
(NodeKind::Ground, f, c.flags)
}
None if water => (NodeKind::Swim, f64::from(y), 0),
None if climb => (NodeKind::Climb, f64::from(y), 0),
None => return None,
};
if !clear_standing(g, x, z, base, base + HEIGHT) {
return None;
}
let head = g.class(x, (base + 1.62).floor() as i32, z);
let submerged = head.has(flag::WATER);
if kind == NodeKind::Swim {
let up = g.class(x, y + 1, z);
if up.has(flag::SOLID) {
return None;
}
}
Some(Spot {
kind,
floor: base,
floor_flags,
water: water || submerged,
submerged,
climb,
})
}
pub(crate) fn climb_wall<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
y: i32,
z: i32,
) -> Option<(i32, i32)> {
let c = g.class(x, y, z);
if !c.has(flag::CLIMB) {
return None;
}
let side = match c.wall {
1 => Some((0, -1)),
2 => Some((0, 1)),
3 => Some((-1, 0)),
4 => Some((1, 0)),
_ => None,
};
if side.is_some() {
return side;
}
for (dx, dz) in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
let n = g.class(x + dx, y, z + dz);
if n.has(flag::SUPPORT) && n.top >= 1.0 && n.bottom <= 0.0 {
return Some((dx, dz));
}
}
None
}