f87d2c518f
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
338 lines
10 KiB
Rust
338 lines
10 KiB
Rust
//! The world queries of capability `render` (AirDef fork, API level 12): a
|
|
//! segment walked block by block against collision shapes, and the
|
|
//! contents of a 16-block section as a bit set. Pure code over a
|
|
//! [`BlockProbe`], so the game answers from its chunks and the tests from
|
|
//! a few lines of fixture.
|
|
|
|
/// What a probe knows about one block cell.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub enum Cell<'a> {
|
|
/// No chunk holds this cell.
|
|
Unknown,
|
|
/// The cell does not stop the policy's queries.
|
|
Empty,
|
|
/// It does; its collision boxes in cell space (0..1), or none for a full
|
|
/// cube.
|
|
Solid(Option<&'a [[f64; 6]]>),
|
|
}
|
|
|
|
/// The blocks a query reads.
|
|
pub trait BlockProbe {
|
|
fn cell(&mut self, x: i32, y: i32, z: i32) -> Cell<'_>;
|
|
}
|
|
|
|
/// How a segment ended.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub enum Trace {
|
|
Clear,
|
|
Hit {
|
|
/// 0 to 1 along the segment.
|
|
fraction: f64,
|
|
normal: [i32; 3],
|
|
block: [i32; 3],
|
|
},
|
|
Unknown {
|
|
/// 0 to 1: where the unknown part begins.
|
|
known_to: f64,
|
|
},
|
|
}
|
|
|
|
/// Longest segment walked, in blocks; the rest is `Unknown`.
|
|
pub const MAX_RAY: f64 = 512.0;
|
|
|
|
/// The first solid block along `from` to `to`, or `Unknown` where the walk
|
|
/// reaches a cell the probe doesn't know. A segment starting inside a solid
|
|
/// block hits it at fraction 0.
|
|
pub fn trace(probe: &mut impl BlockProbe, from: [f64; 3], to: [f64; 3]) -> Trace {
|
|
if !from
|
|
.iter()
|
|
.chain(&to)
|
|
.all(|v| v.is_finite() && v.abs() < 3.0e7)
|
|
{
|
|
return Trace::Unknown { known_to: 0.0 };
|
|
}
|
|
let delta = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
|
|
let length = (delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]).sqrt();
|
|
// The part walked, as a fraction of the segment.
|
|
let walked = if length > MAX_RAY {
|
|
MAX_RAY / length
|
|
} else {
|
|
1.0
|
|
};
|
|
let mut pos = from.map(|v| v.floor() as i32);
|
|
let step: [i32; 3] = delta.map(|d| if d > 0.0 { 1 } else { -1 });
|
|
let mut t_max = [f64::INFINITY; 3];
|
|
let mut t_delta = [f64::INFINITY; 3];
|
|
for a in 0..3 {
|
|
if delta[a] != 0.0 {
|
|
let boundary = f64::from(pos[a]) + if delta[a] > 0.0 { 1.0 } else { 0.0 };
|
|
t_max[a] = (boundary - from[a]) / delta[a];
|
|
t_delta[a] = (1.0 / delta[a]).abs();
|
|
}
|
|
}
|
|
let mut face = [0i32; 3];
|
|
let mut t = 0.0f64;
|
|
loop {
|
|
match probe.cell(pos[0], pos[1], pos[2]) {
|
|
Cell::Unknown => {
|
|
return Trace::Unknown {
|
|
known_to: t.min(1.0),
|
|
};
|
|
}
|
|
Cell::Empty => {}
|
|
Cell::Solid(boxes) => {
|
|
if let Some((hit, normal)) = enter(boxes, pos, from, delta, face) {
|
|
return Trace::Hit {
|
|
fraction: hit.clamp(0.0, 1.0),
|
|
normal,
|
|
block: pos,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
let axis = if t_max[0] < t_max[1] {
|
|
if t_max[0] < t_max[2] { 0 } else { 2 }
|
|
} else if t_max[1] < t_max[2] {
|
|
1
|
|
} else {
|
|
2
|
|
};
|
|
t = t_max[axis];
|
|
if t > walked {
|
|
return if walked < 1.0 {
|
|
Trace::Unknown { known_to: walked }
|
|
} else {
|
|
Trace::Clear
|
|
};
|
|
}
|
|
t_max[axis] += t_delta[axis];
|
|
pos[axis] += step[axis];
|
|
face = [0; 3];
|
|
face[axis] = -step[axis];
|
|
}
|
|
}
|
|
|
|
/// Where the segment first meets the cell's boxes (slab method): its
|
|
/// fraction and the face's normal. `face` is the face it came in by.
|
|
fn enter(
|
|
boxes: Option<&[[f64; 6]]>,
|
|
cell: [i32; 3],
|
|
from: [f64; 3],
|
|
delta: [f64; 3],
|
|
face: [i32; 3],
|
|
) -> Option<(f64, [i32; 3])> {
|
|
const FULL: [[f64; 6]; 1] = [[0.0, 0.0, 0.0, 1.0, 1.0, 1.0]];
|
|
let boxes = boxes.unwrap_or(&FULL);
|
|
let mut best: Option<(f64, [i32; 3])> = None;
|
|
for b in boxes {
|
|
let (mut t0, mut t1) = (0.0f64, 1.0f64);
|
|
let mut normal = face;
|
|
let mut miss = false;
|
|
for a in 0..3 {
|
|
let (lo, hi) = (f64::from(cell[a]) + b[a], f64::from(cell[a]) + b[a + 3]);
|
|
if delta[a] == 0.0 {
|
|
miss |= from[a] < lo || from[a] > hi;
|
|
continue;
|
|
}
|
|
let (e, x) = ((lo - from[a]) / delta[a], (hi - from[a]) / delta[a]);
|
|
let (near, far) = if e < x { (e, x) } else { (x, e) };
|
|
if near > t0 {
|
|
t0 = near;
|
|
normal = [0; 3];
|
|
normal[a] = if delta[a] > 0.0 { -1 } else { 1 };
|
|
}
|
|
t1 = t1.min(far);
|
|
}
|
|
if miss || t0 > t1 {
|
|
continue;
|
|
}
|
|
if best.is_none_or(|(bt, _)| t0 < bt) {
|
|
best = Some((t0, normal));
|
|
}
|
|
}
|
|
best
|
|
}
|
|
|
|
/// A section's contents for a policy: whether every block, none or some stop
|
|
/// it, and for some the 512 bytes of bits (index `(y & 15) * 256 + (z & 15) *
|
|
/// 16 + (x & 15)`, bit `i & 7` of byte `i / 8`).
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum SectionBits {
|
|
Empty,
|
|
Full,
|
|
Mixed(Box<[u8; 512]>),
|
|
}
|
|
|
|
/// The bits of 4096 block states (in the index order above) under a state
|
|
/// table: `table[state] & mask != 0` stops the query.
|
|
pub fn section_bits(states: &[u32; 4096], table: &[u8], mask: u8) -> SectionBits {
|
|
let mut bits = Box::new([0u8; 512]);
|
|
let mut set = 0u32;
|
|
for (i, &s) in states.iter().enumerate() {
|
|
if table.get(s as usize).is_some_and(|f| f & mask != 0) {
|
|
bits[i >> 3] |= 1 << (i & 7);
|
|
set += 1;
|
|
}
|
|
}
|
|
match set {
|
|
0 => SectionBits::Empty,
|
|
4096 => SectionBits::Full,
|
|
_ => SectionBits::Mixed(bits),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::collections::HashMap;
|
|
|
|
/// A world of listed cells; any cell outside `known` columns is unknown.
|
|
struct Fixture {
|
|
solid: HashMap<(i32, i32, i32), Option<Vec<[f64; 6]>>>,
|
|
unknown_from_x: i32,
|
|
}
|
|
impl BlockProbe for Fixture {
|
|
fn cell(&mut self, x: i32, y: i32, z: i32) -> Cell<'_> {
|
|
if x >= self.unknown_from_x {
|
|
return Cell::Unknown;
|
|
}
|
|
match self.solid.get(&(x, y, z)) {
|
|
None => Cell::Empty,
|
|
Some(None) => Cell::Solid(None),
|
|
Some(Some(b)) => Cell::Solid(Some(b)),
|
|
}
|
|
}
|
|
}
|
|
fn wall(x: i32) -> Fixture {
|
|
let mut solid = HashMap::new();
|
|
for y in 60..80 {
|
|
for z in -5..5 {
|
|
solid.insert((x, y, z), None);
|
|
}
|
|
}
|
|
Fixture {
|
|
solid,
|
|
unknown_from_x: 10_000,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_wall_one_block_thick_is_hit_at_its_face() {
|
|
let mut w = wall(3);
|
|
let t = trace(&mut w, [0.5, 70.5, 0.5], [20.5, 70.5, 0.5]);
|
|
let Trace::Hit {
|
|
fraction,
|
|
normal,
|
|
block,
|
|
} = t
|
|
else {
|
|
panic!("{t:?}")
|
|
};
|
|
assert!((fraction - 2.5 / 20.0).abs() < 1e-9);
|
|
assert_eq!((normal, block), ([-1, 0, 0], [3, 70, 0]));
|
|
}
|
|
#[test]
|
|
fn a_diagonal_segment_does_not_slip_through_a_corner() {
|
|
let mut w = wall(3);
|
|
let t = trace(&mut w, [0.2, 65.2, 0.2], [6.2, 71.2, 0.2]);
|
|
assert!(
|
|
matches!(
|
|
t,
|
|
Trace::Hit {
|
|
block: [3, 68, 0],
|
|
..
|
|
}
|
|
),
|
|
"{t:?}"
|
|
);
|
|
}
|
|
#[test]
|
|
fn a_tunnel_is_clear() {
|
|
let mut w = wall(3);
|
|
w.solid.remove(&(3, 70, 0));
|
|
let t = trace(&mut w, [0.5, 70.5, 0.5], [20.5, 70.5, 0.5]);
|
|
assert_eq!(t, Trace::Clear);
|
|
}
|
|
#[test]
|
|
fn a_slab_is_passed_above_and_hit_below() {
|
|
let mut w = Fixture {
|
|
solid: HashMap::new(),
|
|
unknown_from_x: 10_000,
|
|
};
|
|
w.solid
|
|
.insert((2, 70, 0), Some(vec![[0.0, 0.0, 0.0, 1.0, 0.5, 1.0]]));
|
|
assert_eq!(
|
|
trace(&mut w, [0.5, 70.8, 0.5], [5.5, 70.8, 0.5]),
|
|
Trace::Clear
|
|
);
|
|
let t = trace(&mut w, [0.5, 70.3, 0.5], [5.5, 70.3, 0.5]);
|
|
assert!(
|
|
matches!(
|
|
t,
|
|
Trace::Hit {
|
|
block: [2, 70, 0],
|
|
normal: [-1, 0, 0],
|
|
..
|
|
}
|
|
),
|
|
"{t:?}"
|
|
);
|
|
}
|
|
#[test]
|
|
fn an_unloaded_chunk_is_unknown_not_air() {
|
|
let mut w = Fixture {
|
|
solid: HashMap::new(),
|
|
unknown_from_x: 8,
|
|
};
|
|
let t = trace(&mut w, [0.5, 70.5, 0.5], [20.5, 70.5, 0.5]);
|
|
let Trace::Unknown { known_to } = t else {
|
|
panic!("{t:?}")
|
|
};
|
|
assert!((known_to - 7.5 / 20.0).abs() < 1e-9);
|
|
// A hit before the unknown part still counts.
|
|
let mut w = wall(3);
|
|
w.unknown_from_x = 8;
|
|
assert!(matches!(
|
|
trace(&mut w, [0.5, 70.5, 0.5], [20.5, 70.5, 0.5]),
|
|
Trace::Hit { .. }
|
|
));
|
|
}
|
|
#[test]
|
|
fn a_long_segment_is_walked_only_so_far() {
|
|
let mut w = Fixture {
|
|
solid: HashMap::new(),
|
|
unknown_from_x: 10_000,
|
|
};
|
|
let t = trace(&mut w, [0.5, 70.5, 0.5], [2000.5, 70.5, 0.5]);
|
|
let Trace::Unknown { known_to } = t else {
|
|
panic!("{t:?}")
|
|
};
|
|
assert!((known_to - MAX_RAY / 2000.0).abs() < 1e-9);
|
|
}
|
|
#[test]
|
|
fn starting_inside_a_block_hits_at_zero() {
|
|
let mut w = wall(0);
|
|
let t = trace(&mut w, [0.5, 70.5, 0.5], [5.5, 70.5, 0.5]);
|
|
assert!(
|
|
matches!(t, Trace::Hit { fraction, .. } if fraction == 0.0),
|
|
"{t:?}"
|
|
);
|
|
}
|
|
#[test]
|
|
fn sections_pack_to_bits() {
|
|
let table = [0u8, 1, 7];
|
|
let mut states = [0u32; 4096];
|
|
assert_eq!(section_bits(&states, &table, 1), SectionBits::Empty);
|
|
states[256 + 16 * 3 + 5] = 1;
|
|
let SectionBits::Mixed(bits) = section_bits(&states, &table, 1) else {
|
|
panic!()
|
|
};
|
|
let i = 256 + 16 * 3 + 5;
|
|
assert_eq!(bits[i / 8], 1 << (i % 8));
|
|
// A sound-only policy ignores a block marked for collision alone.
|
|
assert_eq!(section_bits(&states, &table, 4), SectionBits::Empty);
|
|
states.fill(2);
|
|
assert_eq!(section_bits(&states, &table, 4), SectionBits::Full);
|
|
}
|
|
}
|