#![allow(clippy::neg_cmp_op_on_partial_ord)]
use super::rules::shared::{BlockKind, StatePhys, VoxelClass, VoxelShapeRef, state_flags as sf};
use super::moving::{Grid, MovingShapes, Part};
use super::math::{V3, bx, expand_towards, floor, hi, intersects, java_max, java_min, lo, mv};
use super::rules::Rules;
use crate::math::Aabb;
use crate::voxel::{EPSILON, Points, VoxelShape};
use crate::world::{BlockView, WorldBorder};
#[derive(Clone, Copy, Debug)]
pub(crate) struct ShapeCtx {
pub feet_y: f64,
pub descending: bool,
pub fall_distance: f64,
pub leather_boots: bool,
}
pub(crate) struct Border {
xs: [f64; 4],
ys: [f64; 2],
zs: [f64; 4],
min_x: f64,
max_x: f64,
min_z: f64,
max_z: f64,
}
const INFINITE_POINTS: [f64; 2] = [f64::NEG_INFINITY, f64::INFINITY];
const BORDER_CELLS: [u64; 1] = [0b1_1110_1111];
impl Border {
pub(crate) fn new(b: &WorldBorder) -> Border {
let max = 29_999_984.0;
let c = |v: f64| {
if v < -max {
-max
} else if v > max {
max
} else {
v
}
};
let (west, east) = (c(b.center_x - b.size / 2.0), c(b.center_x + b.size / 2.0));
let (north, south) = (c(b.center_z - b.size / 2.0), c(b.center_z + b.size / 2.0));
Border {
xs: [f64::NEG_INFINITY, west.floor(), east.ceil(), f64::INFINITY],
ys: [f64::NEG_INFINITY, f64::INFINITY],
zs: [
f64::NEG_INFINITY,
north.floor(),
south.ceil(),
f64::INFINITY,
],
min_x: west,
max_x: east,
min_z: north,
max_z: south,
}
}
fn full(&self) -> bool {
self.xs[2] - self.xs[1] < EPSILON || self.zs[2] - self.zs[1] < EPSILON
}
pub(crate) fn shape(&self) -> VoxelShape<'_> {
if self.full() {
return VoxelShape {
axes: [
Points::Explicit(&INFINITE_POINTS),
Points::Explicit(&INFINITE_POINTS),
Points::Explicit(&INFINITE_POINTS),
],
cells: &[1],
};
}
VoxelShape {
axes: [
Points::Explicit(&self.xs),
Points::Explicit(&self.ys),
Points::Explicit(&self.zs),
],
cells: &BORDER_CELLS,
}
}
pub(crate) fn inside_close(&self, x: f64, z: f64, query: &Aabb) -> bool {
let size = java_max(
(query.max_x - query.min_x).abs(),
(query.max_z - query.min_z).abs(),
);
let bb_max = java_max(size, 1.0);
let dist = java_min(
java_min(java_min(x - self.min_x, self.max_x - x), z - self.min_z),
self.max_z - z,
);
dist < bb_max * 2.0
&& x >= self.min_x - bb_max
&& x < self.max_x + bb_max
&& z >= self.min_z - bb_max
&& z < self.max_z + bb_max
}
}
pub(crate) trait GridRef: Copy {
fn max_distance(&self, axis: usize, b: &Aabb, d: f64, o: [f64; 3]) -> f64;
fn intersects(&self, q: &Aabb, o: [f64; 3]) -> bool;
fn for_ys(&self, oy: f64, f: impl FnMut(f64) -> bool);
}
#[derive(Clone, Copy)]
pub(crate) enum NoMoving {}
impl GridRef for NoMoving {
#[inline(always)]
fn max_distance(&self, _: usize, _: &Aabb, _: f64, _: [f64; 3]) -> f64 {
match *self {}
}
#[inline(always)]
fn intersects(&self, _: &Aabb, _: [f64; 3]) -> bool {
match *self {}
}
#[inline(always)]
fn for_ys(&self, _: f64, _: impl FnMut(f64) -> bool) {
match *self {}
}
}
impl GridRef for &Grid {
fn max_distance(&self, axis: usize, b: &Aabb, d: f64, o: [f64; 3]) -> f64 {
self.view().max_distance(axis, b, d, Some(o))
}
fn intersects(&self, q: &Aabb, o: [f64; 3]) -> bool {
self.view().intersects(q, Some(o))
}
fn for_ys(&self, oy: f64, mut f: impl FnMut(f64) -> bool) {
for &y in self.ys() {
if !f(y + oy) {
return;
}
}
}
}
#[derive(Clone, Copy)]
pub(crate) enum Placed<M: GridRef = NoMoving> {
Cube([f64; 3]),
Shape(&'static VoxelShapeRef, Option<[f64; 3]>, [f64; 3]),
Border,
Moving(M, [f64; 3]),
}
impl Placed {
#[inline(always)]
pub(crate) fn widen<M: GridRef>(self) -> Placed<M> {
match self {
Placed::Cube(o) => Placed::Cube(o),
Placed::Shape(r, sh, o) => Placed::Shape(r, sh, o),
Placed::Border => Placed::Border,
Placed::Moving(n, _) => match n {},
}
}
}
fn entity_shape_collide(e: &Aabb, axis: usize, b: &Aabb, d: f64) -> f64 {
let xs = [e.min_x, e.max_x];
let ys = [e.min_y, e.max_y];
let zs = [e.min_z, e.max_z];
VoxelShape {
axes: [
Points::Explicit(&xs),
Points::Explicit(&ys),
Points::Explicit(&zs),
],
cells: &[1],
}
.max_distance(axis, b, d, None)
}
fn entity_shape_empty(e: &Aabb) -> bool {
e.max_x - e.min_x < EPSILON || e.max_y - e.min_y < EPSILON || e.max_z - e.min_z < EPSILON
}
pub(crate) fn voxel(r: &'static VoxelShapeRef, shift: Option<[f64; 3]>) -> VoxelShape<'static> {
let axis = |k: usize, n: u8, pts: &'static [f64]| match (shift, r.class) {
(Some(s), _) => Points::Shifted(pts, s[k]),
(None, VoxelClass::Simple) => Points::Fractional(u32::from(n)),
(None, VoxelClass::Array) => Points::Explicit(pts),
};
VoxelShape {
axes: [
axis(0, r.sizes[0], r.xs),
axis(1, r.sizes[1], r.ys),
axis(2, r.sizes[2], r.zs),
],
cells: r.cells,
}
}
#[inline]
fn cube_collide(axis: usize, b: &Aabb, d: f64, o: [f64; 3]) -> f64 {
if d.abs() < EPSILON {
return 0.0;
}
let (a2, a3) = ((axis + 1) % 3, (axis + 2) % 3);
let overlaps = |ax: usize| {
let p0 = 0.0 + o[ax];
let p1 = 1.0 + o[ax];
lo(b, ax) + EPSILON < p1 && hi(b, ax) - EPSILON >= p0
};
if !overlaps(a2) || !overlaps(a3) {
return d;
}
let p0 = 0.0 + o[axis];
let p1 = 1.0 + o[axis];
if d > 0.0 {
let max_a = hi(b, axis);
if max_a - EPSILON < p0 {
let gap = p0 - max_a;
if gap >= -EPSILON {
return java_min(d, gap);
}
}
d
} else {
let min_a = lo(b, axis);
if min_a + EPSILON >= p1 {
let gap = p1 - min_a;
if gap <= EPSILON {
return java_max(d, gap);
}
}
d
}
}
#[derive(Clone, Copy)]
struct ShapeAxis {
pts: &'static [f64],
n: usize,
simple: bool,
shift: Option<f64>,
o: f64,
}
impl ShapeAxis {
#[inline]
fn new(
r: &'static VoxelShapeRef,
axis: usize,
shift: Option<[f64; 3]>,
o: [f64; 3],
) -> ShapeAxis {
let (pts, n) = match axis {
0 => (r.xs, usize::from(r.sizes[0])),
1 => (r.ys, usize::from(r.sizes[1])),
_ => (r.zs, usize::from(r.sizes[2])),
};
ShapeAxis {
pts,
n,
simple: r.class == VoxelClass::Simple && shift.is_none(),
shift: shift.map(|s| s[axis]),
o: o[axis],
}
}
#[inline]
fn point(&self, i: usize) -> f64 {
let base = if self.simple {
i as f64 / self.n as f64
} else {
self.pts[i]
};
match self.shift {
Some(s) => (base + s) + self.o,
None => base + self.o,
}
}
#[inline]
fn find_index(&self, coord: f64) -> isize {
let (mut from, mut len) = (0usize, self.n + 1);
while len > 0 {
let half = len / 2;
let mid = from + half;
if coord < self.point(mid) {
len = half;
} else {
from = mid + 1;
len -= half + 1;
}
}
from as isize - 1
}
}
#[inline]
fn filled_axes(r: &VoxelShapeRef, axis: usize, a: usize, b: usize, c: usize) -> bool {
let mut x = [0usize; 3];
x[axis] = a;
x[(axis + 1) % 3] = b;
x[(axis + 2) % 3] = c;
r.filled(x[0], x[1], x[2])
}
fn shape_collide(
r: &'static VoxelShapeRef,
shift: Option<[f64; 3]>,
o: [f64; 3],
axis: usize,
b: &Aabb,
d: f64,
) -> f64 {
if r.empty {
return d;
}
if d.abs() < EPSILON {
return 0.0;
}
let (ax2, ax3) = ((axis + 1) % 3, (axis + 2) % 3);
let pa = ShapeAxis::new(r, axis, shift, o);
let pb = ShapeAxis::new(r, ax2, shift, o);
let pc = ShapeAxis::new(r, ax3, shift, o);
let (max_a, min_a) = (hi(b, axis), lo(b, axis));
let a_min = pa.find_index(min_a + EPSILON);
let a_max = pa.find_index(max_a - EPSILON);
let b_min = pb.find_index(lo(b, ax2) + EPSILON).max(0);
let b_max = (pb.find_index(hi(b, ax2) - EPSILON) + 1).min(pb.n as isize);
let c_min = pc.find_index(lo(b, ax3) + EPSILON).max(0);
let c_max = (pc.find_index(hi(b, ax3) - EPSILON) + 1).min(pc.n as isize);
let layer = |a: isize| {
(b_min..b_max).any(|bb| {
(c_min..c_max).any(|cc| filled_axes(r, axis, a as usize, bb as usize, cc as usize))
})
};
if d > 0.0 {
for a in a_max + 1..pa.n as isize {
if layer(a) {
let gap = pa.point(a as usize) - max_a;
return if gap >= -EPSILON { java_min(d, gap) } else { d };
}
}
} else if d < 0.0 {
for a in (0..a_min).rev() {
if layer(a) {
let gap = pa.point(a as usize + 1) - min_a;
return if gap <= EPSILON { java_max(d, gap) } else { d };
}
}
}
d
}
fn shape_intersects(
r: &'static VoxelShapeRef,
shift: Option<[f64; 3]>,
o: [f64; 3],
q: &Aabb,
) -> bool {
if r.empty {
return false;
}
let sep = 2.0 * EPSILON;
let mut range = [(0isize, 0isize); 3];
for (axis, rg) in range.iter_mut().enumerate() {
let ax = ShapeAxis::new(r, axis, shift, o);
let (b0, b1) = (lo(q, axis), hi(q, axis));
if !(b1 - b0 > sep) {
return voxel(r, shift).intersects(q, Some(o));
}
let (mut first, mut last) = (isize::MAX, -1isize);
for i in 0..=ax.n {
let p = ax.point(i);
if !((p - b0).abs() > sep && (p - b1).abs() > sep) {
return voxel(r, shift).intersects(q, Some(o));
}
if i < ax.n {
let next = ax.point(i + 1);
if p < b1 && next > b0 {
first = first.min(i as isize);
last = i as isize;
}
}
}
if last < 0 {
return false;
}
*rg = (first, last);
}
for x in range[0].0..=range[0].1 {
for y in range[1].0..=range[1].1 {
for z in range[2].0..=range[2].1 {
if r.filled(x as usize, y as usize, z as usize) {
return true;
}
}
}
}
false
}
impl<M: GridRef> Placed<M> {
pub(crate) fn for_boxes(&self, mut f: impl FnMut(Aabb) -> bool) {
match self {
Placed::Cube(o) => {
let _ = f(Aabb::new(
0.0 + o[0],
0.0 + o[1],
0.0 + o[2],
1.0 + o[0],
1.0 + o[1],
1.0 + o[2],
));
}
Placed::Shape(r, sh, o) => {
let k = sh.unwrap_or([0.0; 3]);
for b in r.boxes {
let w = match sh {
Some(_) => Aabb::new(
b.min_x + k[0] + o[0],
b.min_y + k[1] + o[1],
b.min_z + k[2] + o[2],
b.max_x + k[0] + o[0],
b.max_y + k[1] + o[1],
b.max_z + k[2] + o[2],
),
None => Aabb::new(
b.min_x + o[0],
b.min_y + o[1],
b.min_z + o[2],
b.max_x + o[0],
b.max_y + o[1],
b.max_z + o[2],
),
};
if !f(w) {
return;
}
}
}
Placed::Border | Placed::Moving(..) => {}
}
}
pub(crate) fn local_max_y(&self) -> Option<f64> {
match self {
Placed::Cube(_) => Some(1.0),
Placed::Shape(r, sh, _) => {
if r.empty {
return None;
}
let [nx, ny, nz] = r.sizes.map(usize::from);
let top = (0..ny)
.rev()
.find(|&y| (0..nx).any(|x| (0..nz).any(|z| r.filled(x, y, z))))?;
let s = voxel(r, *sh);
Some(s.axes[1].get(top + 1))
}
Placed::Border | Placed::Moving(..) => None,
}
}
pub(crate) fn is_empty(&self) -> bool {
match self {
Placed::Shape(r, _, _) => r.empty,
_ => false,
}
}
#[inline(always)]
pub(crate) fn collide(&self, border: &Border, axis: usize, b: &Aabb, d: f64) -> f64 {
match self {
Placed::Cube(o) => cube_collide(axis, b, d, *o),
Placed::Shape(r, sh, o) => shape_collide(r, *sh, *o, axis, b, d),
Placed::Border => border.shape().max_distance(axis, b, d, None),
Placed::Moving(g, o) => g.max_distance(axis, b, d, *o),
}
}
pub(crate) fn for_y_coords(&self, border: &Border, mut f: impl FnMut(f64) -> bool) {
match self {
Placed::Cube(o) => {
let _ = f(0.0 + o[1]) && f(1.0 + o[1]);
}
Placed::Shape(r, sh, o) => {
let s = voxel(r, *sh);
let n = s.axes[1].cells();
for i in 0..=n {
if !f(s.axes[1].get(i) + o[1]) {
return;
}
}
}
Placed::Border => {
for &y in &border.ys {
if !f(y) {
return;
}
}
}
Placed::Moving(g, o) => g.for_ys(o[1], f),
}
}
}
pub(crate) struct Colliders<M: GridRef = NoMoving> {
buf: [Placed<M>; INLINE],
len: usize,
spill: Vec<Placed<M>>,
}
const INLINE: usize = 48;
impl<M: GridRef> Colliders<M> {
pub(crate) fn new() -> Colliders<M> {
Colliders {
buf: [Placed::Border; INLINE],
len: 0,
spill: Vec::new(),
}
}
#[inline]
pub(crate) fn push(&mut self, p: Placed<M>) {
if self.len < INLINE {
self.buf[self.len] = p;
self.len += 1;
} else {
self.spill.push(p);
}
}
#[inline]
pub(crate) fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub(crate) fn clear(&mut self) {
self.len = 0;
self.spill.clear();
}
#[inline]
pub(crate) fn iter(&self) -> impl Iterator<Item = &Placed<M>> {
self.buf[..self.len].iter().chain(self.spill.iter())
}
}
#[inline]
pub(crate) fn shapes_collide(
shapes: &Colliders<impl GridRef>,
border: &Border,
axis: usize,
b: &Aabb,
mut d: f64,
) -> f64 {
for s in shapes.iter() {
if d.abs() < EPSILON {
return 0.0;
}
d = s.collide(border, axis, b, d);
}
d
}
pub(crate) fn collide_with_shapes(
m: V3,
b: &Aabb,
shapes: &Colliders<impl GridRef>,
border: &Border,
) -> V3 {
if shapes.is_empty() {
return m;
}
let mut r = V3::ZERO;
let order: [usize; 3] = if m.x.abs() < m.z.abs() {
[1, 2, 0]
} else {
[1, 0, 2]
};
for axis in order {
let am = m.get(axis);
if am != 0.0 {
let c = shapes_collide(shapes, border, axis, &mv(b, r.x, r.y, r.z), am);
r = r.with(axis, c);
}
}
r
}
pub(crate) trait Extra: Copy {
const ON: bool;
const VEHICLE: bool = false;
const MOVING: bool = false;
type M<'q>: GridRef
where
Self: 'q;
fn entities(&self) -> &[Aabb];
#[inline(always)]
fn moving<'q>(
&'q self,
_pos: [i32; 3],
_o: [f64; 3],
_f: &mut impl FnMut(Placed<Self::M<'q>>) -> bool,
) -> bool {
true
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct Plain;
impl Extra for Plain {
type M<'q>
= NoMoving
where
Self: 'q;
const ON: bool = false;
#[inline(always)]
fn entities(&self) -> &[Aabb] {
&[]
}
}
#[derive(Clone, Copy)]
pub(crate) struct Ents<'a>(pub &'a [Aabb]);
impl Extra for Ents<'_> {
type M<'q>
= NoMoving
where
Self: 'q;
const ON: bool = true;
#[inline(always)]
fn entities(&self) -> &[Aabb] {
self.0
}
}
#[derive(Clone, Copy)]
pub(crate) struct Vehicle<'a>(pub &'a [Aabb]);
impl Extra for Vehicle<'_> {
type M<'q>
= NoMoving
where
Self: 'q;
const ON: bool = true;
const VEHICLE: bool = true;
#[inline(always)]
fn entities(&self) -> &[Aabb] {
self.0
}
}
#[derive(Clone, Copy)]
pub(crate) struct Moving<'a> {
pub ents: &'a [Aabb],
pub shapes: &'a MovingShapes,
}
impl Extra for Moving<'_> {
type M<'q>
= &'q Grid
where
Self: 'q;
const ON: bool = true;
const MOVING: bool = true;
#[inline(always)]
fn entities(&self) -> &[Aabb] {
self.ents
}
fn moving<'q>(
&'q self,
pos: [i32; 3],
o: [f64; 3],
f: &mut impl FnMut(Placed<&'q Grid>) -> bool,
) -> bool {
for part in self.shapes.at(pos) {
let shape = match part {
Part::Cube => Placed::Cube(o),
Part::Table(r) => Placed::Shape(r, None, o),
Part::Grid(g) => Placed::Moving(g, o),
};
if !f(shape) {
return false;
}
}
true
}
}
pub(crate) struct World<'a, R: Rules, W, E: Extra = Plain> {
pub w: &'a W,
pub border: &'a Border,
pub min_y: i32,
pub max_y: i32,
pub ext: E,
pub _r: core::marker::PhantomData<R>,
}
impl<R: Rules, W: BlockView<State = R::State>, E: Extra> World<'_, R, W, E> {
#[inline]
pub(crate) fn state(&self, x: i32, y: i32, z: i32) -> R::State {
if y < self.min_y || y > self.max_y {
R::AIR
} else {
self.w.block(x, y, z)
}
}
#[inline]
pub(crate) fn phys(&self, x: i32, y: i32, z: i32) -> StatePhys {
R::phys(self.state(x, y, z))
}
#[inline]
pub(crate) fn shape_of_state(
&self,
s: R::State,
x: i32,
y: i32,
z: i32,
ctx: Option<&ShapeCtx>,
) -> Option<Placed> {
placed_shape::<R>(s, x, y, z, ctx)
}
#[inline]
pub(crate) fn each_collision<'q>(
&'q self,
query: &Aabb,
ctx: Option<&ShapeCtx>,
only_suffocating: bool,
mut f: impl FnMut(i32, i32, i32, Placed<E::M<'q>>) -> bool,
) {
let x0 = floor(query.min_x - EPSILON) - 1;
let x1 = floor(query.max_x + EPSILON) + 1;
let y0 = floor(query.min_y - EPSILON) - 1;
let y1 = floor(query.max_y + EPSILON) + 1;
let z0 = floor(query.min_z - EPSILON) - 1;
let z1 = floor(query.max_z + EPSILON) + 1;
let (ya, yb) = (y0.max(self.min_y), y1.min(self.max_y));
for z in z0..=z1 {
let ez = u8::from(z == z0 || z == z1);
let mut chunk = (i32::MIN, false);
for y in ya..=yb {
let ey = u8::from(y == y0 || y == y1);
for x in x0..=x1 {
let edges = u8::from(x == x0 || x == x1) + ey + ez;
if edges == 3 {
continue;
}
let cx = x >> 4;
if chunk.0 != cx {
chunk = (cx, self.w.is_chunk_loaded(cx, z >> 4));
}
if !chunk.1 {
continue;
}
let s = self.w.block(x, y, z);
let p = R::phys(s);
let fl = p.flags();
if (only_suffocating && fl & sf::SUFFOCATING == 0)
|| (edges == 1 && fl & sf::LARGE == 0)
|| (edges == 2 && fl & sf::MOVING_PISTON == 0)
{
continue;
}
if E::MOVING && fl & sf::MOVING_PISTON != 0 {
let o = [f64::from(x), f64::from(y), f64::from(z)];
let go = self.ext.moving([x, y, z], o, &mut |shape| {
!placed_intersects(&shape, query) || f(x, y, z, shape)
});
if !go {
return;
}
continue;
}
if fl & (sf::EMPTY | sf::CONTEXT) == sf::EMPTY {
continue;
}
let Some(shape) = self.shape_of_state(s, x, y, z, ctx) else {
continue;
};
let hit = match shape {
Placed::Cube(o) => {
intersects(query, o[0], o[1], o[2], o[0] + 1.0, o[1] + 1.0, o[2] + 1.0)
}
Placed::Shape(r, sh, o) => shape_intersects(r, sh, o, query),
Placed::Border => false,
Placed::Moving(n, _) => match n {},
};
if hit && !f(x, y, z, shape.widen()) {
return;
}
}
}
}
}
pub(crate) fn suffocates_in_column(&self, query: &Aabb, ctx: &ShapeCtx) -> bool {
let x0 = floor(query.min_x - EPSILON) - 1;
let x1 = floor(query.max_x + EPSILON) + 1;
let y0 = floor(query.min_y - EPSILON) - 1;
let y1 = floor(query.max_y + EPSILON) + 1;
let z0 = floor(query.min_z - EPSILON) - 1;
let z1 = floor(query.max_z + EPSILON) + 1;
for z in z0 + 1..z1 {
for y in (y0 + 1).max(self.min_y)..y1.min(self.max_y + 1) {
for x in x0 + 1..x1 {
if !self.w.is_chunk_loaded(x >> 4, z >> 4) {
continue;
}
let s = self.w.block(x, y, z);
let p = R::phys(s);
if p.flags() & sf::SUFFOCATING == 0 {
continue;
}
let Some(shape) = self.shape_of_state(s, x, y, z, Some(ctx)) else {
continue;
};
let hit = match shape {
Placed::Cube(o) => {
intersects(query, o[0], o[1], o[2], o[0] + 1.0, o[1] + 1.0, o[2] + 1.0)
}
Placed::Shape(r, sh, o) => shape_intersects(r, sh, o, query),
Placed::Border => false,
Placed::Moving(n, _) => match n {},
};
if hit {
return true;
}
}
}
}
false
}
pub(crate) fn no_collision(&self, src: [f64; 3], query: &Aabb, ctx: &ShapeCtx) -> bool {
let mut empty = true;
self.each_collision(query, Some(ctx), false, |_, _, _, _| {
empty = false;
false
});
if !empty {
return false;
}
if E::ON && !self.ext.entities().is_empty() && self.any_entity_collider(query) {
return false;
}
if self.border.inside_close(src[0], src[2], query) {
return !self.border.shape().intersects(query, None);
}
true
}
#[cold]
#[inline(never)]
fn any_entity_collider(&self, area: &Aabb) -> bool {
self.entity_colliders(area).next().is_some()
}
pub(crate) fn entity_colliders<'q>(&'q self, area: &Aabb) -> impl Iterator<Item = Aabb> + 'q {
let size =
((area.max_x - area.min_x) + (area.max_y - area.min_y) + (area.max_z - area.min_z))
/ 3.0;
let q = super::math::inflate(area, 1.0e-7, 1.0e-7, 1.0e-7);
let list = if size < 1.0e-7 {
&[][..]
} else {
self.ext.entities()
};
list.iter()
.copied()
.filter(move |e| intersects(e, q.min_x, q.min_y, q.min_z, q.max_x, q.max_y, q.max_z))
}
pub(crate) fn colliders<'q>(
&'q self,
src: [f64; 3],
query: &Aabb,
ctx: &ShapeCtx,
c: &mut Colliders<E::M<'q>>,
) {
c.clear();
if self.border.inside_close(src[0], src[2], query) {
c.push(Placed::Border);
}
self.each_collision(query, Some(ctx), false, |_, _, _, s| {
c.push(s);
true
});
}
pub(crate) fn shape_overlaps(&self, s: R::State, x: i32, y: i32, z: i32, query: &Aabb) -> bool {
match self.shape_of_state(s, x, y, z, None) {
Some(Placed::Cube(o)) => VoxelShape::FULL_CUBE.intersects(query, Some(o)),
Some(Placed::Shape(r, sh, o)) => shape_intersects(r, sh, o, query),
_ => false,
}
}
pub(crate) fn supporting_block(
&self,
pos: [f64; 3],
query: &Aabb,
ctx: &ShapeCtx,
) -> Option<[i32; 3]> {
let mut best: Option<[i32; 3]> = None;
let mut best_d = f64::MAX;
self.each_collision(query, Some(ctx), false, |x, y, z, _| {
let dx = f64::from(x) + 0.5 - pos[0];
let dy = f64::from(y) + 0.5 - pos[1];
let dz = f64::from(z) + 0.5 - pos[2];
let d = dx * dx + dy * dy + dz * dz;
if d < best_d || d == best_d && best.is_none_or(|b| cmp_pos(b, [x, y, z]) < 0) {
best = Some([x, y, z]);
best_d = d;
}
true
});
best
}
}
fn cmp_pos(a: [i32; 3], b: [i32; 3]) -> i32 {
if a[1] != b[1] {
a[1] - b[1]
} else if a[2] != b[2] {
a[2] - b[2]
} else {
a[0] - b[0]
}
}
#[inline]
pub(crate) fn placed_shape<R: Rules>(
s: R::State,
x: i32,
y: i32,
z: i32,
ctx: Option<&ShapeCtx>,
) -> Option<Placed> {
let p = R::phys(s);
let f = p.flags();
let o = [f64::from(x), f64::from(y), f64::from(z)];
if f & sf::FULL_CUBE != 0 {
return Some(Placed::Cube(o));
}
if f & sf::CONTEXT != 0 {
return context_shape::<R>(p, y, ctx).map(|i| {
if i == R::FULL_CUBE_VOXEL {
Placed::Cube(o)
} else {
Placed::Shape(R::voxel(i), None, o)
}
});
}
if f & sf::EMPTY != 0 {
return None;
}
let r = R::voxel(p.shape());
if f & sf::OFFSET != 0 {
let shift = model_offset(R::offset(s), x, z);
return Some(Placed::Shape(r, Some(shift), o));
}
Some(Placed::Shape(r, None, o))
}
#[inline]
pub(crate) fn placed_intersects<M: GridRef>(shape: &Placed<M>, query: &Aabb) -> bool {
match *shape {
Placed::Cube(o) => intersects(query, o[0], o[1], o[2], o[0] + 1.0, o[1] + 1.0, o[2] + 1.0),
Placed::Shape(r, sh, o) => shape_intersects(r, sh, o, query),
Placed::Border => false,
Placed::Moving(g, o) => g.intersects(query, o),
}
}
fn context_shape<R: Rules>(p: StatePhys, y: i32, ctx: Option<&ShapeCtx>) -> Option<u16> {
let eps = f64::from(1.0e-5f32);
if p.kind() == BlockKind::Scaffolding as u8 {
let above = |top: f64| ctx.is_none_or(|c| c.feet_y > f64::from(y) + top - eps);
let descending = ctx.is_some_and(|c| c.descending);
if above(1.0) && !descending {
return Some(p.shape());
}
if p.aux() & 1 != 0 && above(0.0) {
return Some(R::SCAFFOLDING_BOTTOM_VOXEL);
}
return None;
}
if p.kind() == BlockKind::PowderSnow as u8 {
let c = ctx?;
if c.fall_distance > 2.5 {
return Some(R::POWDER_SNOW_FALLING_VOXEL);
}
if c.leather_boots && c.feet_y > f64::from(y) + 1.0 - eps && !c.descending {
return Some(R::FULL_CUBE_VOXEL);
}
return None;
}
if p.flags() & sf::EMPTY != 0 {
None
} else {
Some(p.shape())
}
}
pub(crate) fn model_offset(limits: Option<(f32, f32, bool)>, x: i32, z: i32) -> [f64; 3] {
let Some((max_h, max_v, xyz)) = limits else {
return [0.0; 3];
};
let mut seed = i64::from(x.wrapping_mul(3_129_871)) ^ i64::from(z).wrapping_mul(116_129_781);
seed = seed
.wrapping_mul(seed)
.wrapping_mul(42_317_861)
.wrapping_add(seed.wrapping_mul(11));
let seed = seed >> 16;
let h = f64::from(max_h);
let comp = |bits: i64| {
let v = (f64::from((bits & 15) as f32 / 15.0) - 0.5) * 0.5;
super::math::clamp(v, -h, h)
};
let y = if xyz {
(f64::from(((seed >> 4) & 15) as f32 / 15.0) - 1.0) * f64::from(max_v)
} else {
0.0
};
[comp(seed), y, comp(seed >> 8)]
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn collide<R: Rules, W: BlockView<State = R::State>, E: Extra>(
world: &World<'_, R, W, E>,
src: [f64; 3],
bb: &Aabb,
movement: V3,
on_ground: bool,
step: f32,
ctx: &ShapeCtx,
) -> V3 {
if E::ON && !world.ext.entities().is_empty() {
return collide_among_entities(world, src, bb, movement, on_ground, step, ctx);
}
let mut colliders = Colliders::new();
let step_v = if movement.length_sqr() == 0.0 {
movement
} else {
world.colliders(src, &expand_towards(bb, movement), ctx, &mut colliders);
collide_with_shapes(movement, bb, &colliders, world.border)
};
let cx = movement.x != step_v.x;
let cy = movement.y != step_v.y;
let cz = movement.z != step_v.z;
let landing = cy && movement.y < 0.0;
if step > 0.0 && (landing || on_ground) && (cx || cz) {
let grounded = if landing {
mv(bb, 0.0, step_v.y, 0.0)
} else {
*bb
};
let mut up = expand_towards(&grounded, V3::new(movement.x, f64::from(step), movement.z));
if !landing {
up = expand_towards(&up, V3::new(0.0, f64::from(-1.0e-5f32), 0.0));
}
world.colliders(src, &up, ctx, &mut colliders);
let skip = step_v.y as f32;
let mut inline = [0.0f32; 64];
let mut n = 0usize;
let mut spill: Vec<f32> = Vec::new();
for c in colliders.iter() {
c.for_y_coords(world.border, |coord| {
let rel = (coord - grounded.min_y) as f32;
if rel < 0.0 || rel == skip {
return true;
}
if rel > step {
return false;
}
let seen = |v: &f32| v.to_bits() == rel.to_bits();
if !inline[..n].iter().any(seen) && !spill.iter().any(seen) {
if n < inline.len() {
inline[n] = rel;
n += 1;
} else {
spill.push(rel);
}
}
true
});
}
let mut all = spill;
let cands: &mut [f32] = if all.is_empty() {
&mut inline[..n]
} else {
all.extend_from_slice(&inline[..n]);
&mut all
};
cands.sort_by(f32::total_cmp);
for &h in cands.iter() {
let s = collide_with_shapes(
V3::new(movement.x, f64::from(h), movement.z),
&grounded,
&colliders,
world.border,
);
if s.horizontal_sqr() > step_v.horizontal_sqr() {
let d = bb.min_y - grounded.min_y;
return V3::new(s.x, s.y - d, s.z);
}
}
}
step_v
}
#[inline(never)]
fn collide_among_entities<R: Rules, W: BlockView<State = R::State>, E: Extra>(
world: &World<'_, R, W, E>,
src: [f64; 3],
bb: &Aabb,
movement: V3,
on_ground: bool,
step: f32,
ctx: &ShapeCtx,
) -> V3 {
let area = expand_towards(bb, movement);
let query = if R::ENTITY_QUERY_UP_STEP {
expand_towards(&area, V3::new(0.0, f64::from(step), 0.0))
} else {
area
};
let ents: Vec<Aabb> = world
.entity_colliders(&query)
.filter(|e| !entity_shape_empty(e))
.collect();
let mut colliders = Colliders::new();
let step_v = if movement.length_sqr() == 0.0 {
movement
} else {
world.colliders(src, &area, ctx, &mut colliders);
collide_with_entities_and_shapes(movement, bb, &ents, &colliders, world.border)
};
let cx = movement.x != step_v.x;
let cy = movement.y != step_v.y;
let cz = movement.z != step_v.z;
let landing = cy && movement.y < 0.0;
if step > 0.0 && (landing || on_ground) && (cx || cz) {
let grounded = if landing {
mv(bb, 0.0, step_v.y, 0.0)
} else {
*bb
};
let mut up = expand_towards(&grounded, V3::new(movement.x, f64::from(step), movement.z));
if !landing {
up = expand_towards(&up, V3::new(0.0, f64::from(-1.0e-5f32), 0.0));
}
world.colliders(src, &up, ctx, &mut colliders);
let skip = step_v.y as f32;
let mut cands: Vec<f32> = Vec::new();
let mut add = |coord: f64| {
let rel = (coord - grounded.min_y) as f32;
if rel < 0.0 || rel == skip {
return true;
}
if rel > step {
return false;
}
if !cands.iter().any(|v| v.to_bits() == rel.to_bits()) {
cands.push(rel);
}
true
};
for e in &ents {
let _ = add(e.min_y) && add(e.max_y);
}
for c in colliders.iter() {
c.for_y_coords(world.border, &mut add);
}
cands.sort_by(f32::total_cmp);
for &h in &cands {
let s = collide_with_entities_and_shapes(
V3::new(movement.x, f64::from(h), movement.z),
&grounded,
&ents,
&colliders,
world.border,
);
if s.horizontal_sqr() > step_v.horizontal_sqr() {
let d = bb.min_y - grounded.min_y;
return V3::new(s.x, s.y - d, s.z);
}
}
}
step_v
}
fn collide_with_entities_and_shapes(
m: V3,
b: &Aabb,
ents: &[Aabb],
shapes: &Colliders<impl GridRef>,
border: &Border,
) -> V3 {
if ents.is_empty() {
return collide_with_shapes(m, b, shapes, border);
}
let mut r = V3::ZERO;
let order: [usize; 3] = if m.x.abs() < m.z.abs() {
[1, 2, 0]
} else {
[1, 0, 2]
};
for axis in order {
let am = m.get(axis);
if am != 0.0 {
let moved = mv(b, r.x, r.y, r.z);
let mut d = am;
for e in ents {
if d.abs() < EPSILON {
d = 0.0;
break;
}
d = entity_shape_collide(e, axis, &moved, d);
}
for s in shapes.iter() {
if d.abs() < EPSILON {
d = 0.0;
break;
}
d = s.collide(border, axis, &moved, d);
}
r = r.with(axis, d);
}
}
r
}
#[inline]
pub(crate) fn make_box(width: f32, height: f32, x: f64, y: f64, z: f64) -> Aabb {
let w = f64::from(width / 2.0);
bx(x - w, y, z - w, x + w, y + f64::from(height), z + w)
}