pub type Facing = u8;
#[inline]
#[must_use]
pub const fn offset(f: Facing) -> [i32; 3] {
match f {
0 => [0, -1, 0],
1 => [0, 1, 0],
2 => [0, 0, -1],
3 => [0, 0, 1],
4 => [-1, 0, 0],
_ => [1, 0, 0],
}
}
#[inline]
const fn opposite(f: Facing) -> Facing {
f ^ 1
}
#[inline]
const fn axis(f: Facing) -> u8 {
f >> 1
}
#[inline]
const fn axis_index(f: Facing) -> usize {
[1, 2, 0][(f >> 1) as usize]
}
#[inline]
fn step(p: [i32; 3], f: Facing, n: i32) -> [i32; 3] {
let o = offset(f);
[p[0] + o[0] * n, p[1] + o[1] * n, p[2] + o[2] * n]
}
const FACING_NAMES: [&str; 6] = ["down", "up", "north", "south", "west", "east"];
const UPDATE_SHAPE_ORDER: [Facing; 6] = [4, 5, 2, 3, 0, 1];
const PUSH_LIMIT: usize = 12;
const CLIENT_HOLD: u8 = 5;
const MAX_DEPTH: u32 = 512;
use crate::math::{Aabb, sorted_box};
fn stretch(b: &Aabb, dir: Facing, d: f64) -> Aabb {
let e = if dir & 1 == 1 { d } else { -d };
let (f, g) = (e.min(0.0), e.max(0.0));
match dir {
4 => sorted_box(b.min_x + f, b.min_y, b.min_z, b.min_x + g, b.max_y, b.max_z),
5 => sorted_box(b.max_x + f, b.min_y, b.min_z, b.max_x + g, b.max_y, b.max_z),
0 => sorted_box(b.min_x, b.min_y + f, b.min_z, b.max_x, b.min_y + g, b.max_z),
2 => sorted_box(b.min_x, b.min_y, b.min_z + f, b.max_x, b.max_y, b.min_z + g),
3 => sorted_box(b.min_x, b.min_y, b.max_z + f, b.max_x, b.max_y, b.max_z + g),
_ => sorted_box(b.min_x, b.max_y + f, b.min_z, b.max_x, b.max_y + g, b.max_z),
}
}
fn reach(b: &Aabb, dir: Facing, e: &Aabb) -> f64 {
match dir {
5 => b.max_x - e.min_x,
4 => e.max_x - b.min_x,
0 => e.max_y - b.min_y,
3 => b.max_z - e.min_z,
2 => e.max_z - b.min_z,
_ => b.max_y - e.min_y,
}
}
fn union(a: &Aabb, b: &Aabb) -> Aabb {
sorted_box(
a.min_x.min(b.min_x),
a.min_y.min(b.min_y),
a.min_z.min(b.min_z),
a.max_x.max(b.max_x),
a.max_y.max(b.max_y),
a.max_z.max(b.max_z),
)
}
fn intersect(a: &Aabb, b: &Aabb) -> Aabb {
sorted_box(
a.min_x.max(b.min_x),
a.min_y.max(b.min_y),
a.min_z.max(b.min_z),
a.max_x.min(b.max_x),
a.max_y.min(b.max_y),
a.max_z.min(b.max_z),
)
}
fn piston_budget(
delta: &mut [f64; 3],
delta_time: &mut i64,
time: i64,
d: [f64; 3],
) -> Option<[f64; 3]> {
if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] <= 1.0e-7 {
return Some(d);
}
if time != *delta_time {
*delta = [0.0; 3];
*delta_time = time;
}
let k = [0, 1, 2].into_iter().find(|&k| d[k] != 0.0)?;
let v = (d[k] + delta[k]).clamp(-0.51, 0.51);
let mv = v - delta[k];
delta[k] = v;
if mv.abs() <= f64::from(1.0e-5f32) {
return None;
}
let mut out = [0.0; 3];
out[k] = mv;
Some(out)
}
fn hash_map_order<S>(moved: &[[i32; 3]], left: &[([i32; 3], S)]) -> Option<Vec<usize>> {
let bucket = |p: [i32; 3]| {
let h = (p[1].wrapping_add(p[2].wrapping_mul(31)))
.wrapping_mul(31)
.wrapping_add(p[0]);
let spread = h ^ ((h as u32) >> 16) as i32;
(spread & 15) as usize
};
let mut counts = [0usize; 16];
for &p in moved {
counts[bucket(p)] += 1;
}
if counts.iter().any(|&c| c >= 8) {
return None;
}
let mut out = Vec::with_capacity(left.len());
for b in 0..16 {
for &p in moved {
if bucket(p) == b
&& let Some(k) = left.iter().position(|v| v.0 == p)
{
out.push(k);
}
}
}
Some(out)
}
#[inline]
fn search_finds(pos: [f64; 3], q: &Aabb) -> bool {
super::section_query_reaches(q, pos)
}
#[cfg(feature = "v26_1")]
pub mod v26_1 {
use super::super::V26_1 as R;
use ghast_data::v26_1 as ver;
static FACTS: &[u8] = &super::super::piston_facts::FACTS_26_1;
static UPDATE: &[u8] = &super::super::piston_facts::UPDATE_26_1;
static TAGS: &[u8] = &super::super::piston_facts::TAGS_26_1;
static ENTITIES: &[(&str, f32, u8)] = &super::super::piston_facts::ENTITIES_26_1;
include!("piston_body.rs");
}
#[cfg(feature = "v26_2")]
pub mod v26_2 {
use super::super::V26_2 as R;
use ghast_data::v26_2 as ver;
static FACTS: &[u8] = &super::super::piston_facts::FACTS_26_2;
static UPDATE: &[u8] = &super::super::piston_facts::UPDATE_26_2;
static TAGS: &[u8] = &super::super::piston_facts::TAGS_26_2;
static ENTITIES: &[(&str, f32, u8)] = &super::super::piston_facts::ENTITIES_26_2;
include!("piston_body.rs");
}
fn cell_index(shape: &crate::voxel::VoxelShape<'_>, axis: usize, coord: f64) -> isize {
let p = shape.axes[axis];
let n = p.cells();
if shape
.axes
.iter()
.all(|q| matches!(q, crate::voxel::Points::Fractional(_)))
{
let v = crate::math::floor(coord * n as f64);
return (v as isize).clamp(-1, n as isize);
}
(0..=n).find(|&k| coord < p.get(k)).unwrap_or(n + 1) as isize - 1
}
fn face_covers(
shape: &crate::voxel::VoxelShape<'_>,
dir: Facing,
rect: [f64; 4],
exact: bool,
) -> bool {
use crate::voxel::EPSILON;
let a = axis_index(dir);
let (b, c) = match a {
0 => (1, 2),
1 => (0, 2),
_ => (0, 1),
};
if shape.is_empty() {
return false;
}
let layer = cell_index(shape, a, if dir & 1 == 1 { 0.999_999_9 } else { EPSILON });
let pts = |k: usize| -> Vec<f64> {
let p = shape.axes[k];
(0..=p.cells()).map(|i| p.get(i)).collect()
};
let merged = |p: &[f64], lo: f64, hi: f64| -> Vec<f64> {
let mut all: Vec<f64> = p.iter().copied().chain([lo, hi]).collect();
all.sort_by(f64::total_cmp);
let mut kept: Vec<f64> = Vec::with_capacity(all.len());
for v in all {
if kept.last().is_none_or(|&l| l < v - EPSILON) {
kept.push(v);
}
}
kept
};
let cell = |p: &[f64], x: f64| -> Option<usize> {
let k = p.iter().rposition(|&q| q <= x)?;
(k + 1 < p.len()).then_some(k)
};
let (pb, pc) = (pts(b), pts(c));
let (mb, mc) = (merged(&pb, rect[0], rect[2]), merged(&pc, rect[1], rect[3]));
let n = [
shape.axes[0].cells(),
shape.axes[1].cells(),
shape.axes[2].cells(),
];
let in_layer = layer >= 0 && (layer as usize) < n[a];
for u in mb.windows(2) {
let inside_b = u[0] >= rect[0] - EPSILON && u[1] <= rect[2] + EPSILON;
let jb = cell(&pb, f64::midpoint(u[0], u[1]));
for w in mc.windows(2) {
let inside = inside_b && w[0] >= rect[1] - EPSILON && w[1] <= rect[3] + EPSILON;
let jc = cell(&pc, f64::midpoint(w[0], w[1]));
let filled = in_layer
&& match (jb, jc) {
(Some(jb), Some(jc)) => {
let mut q = [0isize; 3];
q[a] = layer;
q[b] = jb as isize;
q[c] = jc as isize;
shape.filled(q[0], q[1], q[2])
}
_ => false,
};
if inside != filled && (inside || exact) {
return false;
}
}
}
true
}
fn table_view(
r: &'static super::rules::shared::VoxelShapeRef,
) -> crate::voxel::VoxelShape<'static> {
super::collide::voxel(r, None)
}
fn part_view(p: &super::moving::Part) -> crate::voxel::VoxelShape<'_> {
match p {
super::moving::Part::Cube => crate::voxel::VoxelShape::FULL_CUBE,
super::moving::Part::Table(r) => table_view(r),
super::moving::Part::Grid(g) => g.view(),
}
}
const fn clockwise(f: Facing) -> Facing {
match f {
2 => 5,
5 => 3,
3 => 4,
_ => 2,
}
}
const fn counter_clockwise(f: Facing) -> Facing {
match f {
2 => 4,
4 => 3,
3 => 5,
_ => 2,
}
}
const TALL_SIDES: [[f64; 4]; 4] = [
[7.0, 0.0, 9.0, 9.0],
[7.0, 7.0, 16.0, 9.0],
[7.0, 7.0, 9.0, 16.0],
[0.0, 7.0, 9.0, 9.0],
];
const TALL_POST: [f64; 4] = [7.0, 7.0, 9.0, 9.0];
fn sixteenths(r: [f64; 4]) -> [f64; 4] {
r.map(|v| v / 16.0)
}
const fn bool_str(b: bool) -> &'static str {
if b { "true" } else { "false" }
}