use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use crate::Version;
use crate::class::{Class, flag};
use crate::control::ControllerConfig;
use crate::cost::Costs;
use crate::door;
use crate::goal::{Goal, Heuristic};
use crate::jump;
use crate::moves::{
HEIGHT, JUMP_APEX, JUMP_MARGIN, MAX_JUMP_UP, MoveKind, NodeKind, STEP, Spot, ceiling, clear,
climb_wall, spot,
};
use crate::sim::{SimOutcome, SimReport, simulate};
use crate::world::{BlockSource, BuildKeyHasher, Grid, Sections};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DigCost {
pub base: f64,
pub ticks_per_hardness: f64,
pub max_hardness: f64,
}
impl Default for DigCost {
fn default() -> Self {
DigCost {
base: 6.0,
ticks_per_hardness: 30.0,
max_hardness: 2.0,
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Config {
pub sprint: bool,
pub parkour: bool,
pub max_fall: f64,
pub max_water_fall: f64,
pub swim: bool,
pub climb: bool,
pub dig: Option<DigCost>,
pub place: Option<f64>,
pub max_nodes: usize,
pub heuristic_weight: f64,
pub(crate) limits: Limits,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Limits {
pub(crate) step: f64,
pub(crate) jump_up: f64,
pub(crate) parkour: bool,
}
impl Default for Limits {
fn default() -> Self {
Limits {
step: STEP,
jump_up: MAX_JUMP_UP,
parkour: true,
}
}
}
impl Limits {
#[cfg(ghast_modern)]
pub(crate) fn of(p: &ghast_physics::modern::PlayerState) -> Limits {
let a = &p.attributes;
let boost = p
.effects
.jump_boost
.map_or(0.0, |amp| 0.1 * f64::from(amp + 1));
let power = a.jump_strength.value() + boost;
let gravity = a.gravity.value();
let (mut y, mut v, mut apex) = (0.0f64, power, 0.0f64);
for _ in 0..200 {
y += v;
apex = apex.max(y);
v = (v - gravity) * 0.98;
if v <= 0.0 {
break;
}
}
let eq = |x: f64, d: f64| (x - d).abs() < 1e-9;
let vanilla = eq(a.jump_strength.value(), f64::from(0.42f32))
&& boost == 0.0
&& eq(gravity, 0.08)
&& eq(a.movement_speed.base(), f64::from(0.1f32))
&& eq(a.scale.value(), 1.0)
&& !p.effects.slow_falling
&& p.effects.levitation.is_none();
Limits {
step: f64::from(p.step_height()).min(STEP),
jump_up: MAX_JUMP_UP.min(apex - 0.2),
parkour: vanilla,
}
}
}
impl Default for Config {
fn default() -> Self {
Config {
sprint: true,
parkour: true,
max_fall: 3.0,
max_water_fall: 16.0,
swim: true,
climb: true,
dig: None,
place: None,
max_nodes: 250_000,
heuristic_weight: 1.0,
limits: Limits::default(),
}
}
}
impl Config {
#[must_use]
pub fn with_sprint(mut self, on: bool) -> Self {
self.sprint = on;
self
}
#[must_use]
pub fn with_parkour(mut self, on: bool) -> Self {
self.parkour = on;
self
}
#[must_use]
pub fn with_max_fall(mut self, blocks: f64) -> Self {
self.max_fall = blocks;
self
}
#[must_use]
pub fn with_swim(mut self, on: bool) -> Self {
self.swim = on;
self
}
#[must_use]
pub fn with_climb(mut self, on: bool) -> Self {
self.climb = on;
self
}
#[must_use]
pub fn with_dig(mut self, dig: Option<DigCost>) -> Self {
self.dig = dig;
self
}
#[must_use]
pub fn with_place(mut self, cost: Option<f64>) -> Self {
self.place = cost;
self
}
#[must_use]
pub fn with_max_nodes(mut self, n: usize) -> Self {
self.max_nodes = n;
self
}
#[must_use]
pub fn with_heuristic_weight(mut self, w: f64) -> Self {
self.heuristic_weight = w.max(1.0);
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Place {
pub pos: [i32; 3],
pub against: [i32; 3],
}
impl Place {
#[must_use]
pub fn face(&self) -> [i32; 3] {
[
self.pos[0] - self.against[0],
self.pos[1] - self.against[1],
self.pos[2] - self.against[2],
]
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Step {
pub pos: [i32; 3],
pub feet_y: f64,
pub kind: MoveKind,
pub node: NodeKind,
pub dig: Vec<[i32; 3]>,
pub place: Option<Place>,
}
impl Step {
#[must_use]
pub fn target(&self) -> [f64; 3] {
[
f64::from(self.pos[0]) + 0.5,
self.feet_y,
f64::from(self.pos[2]) + 0.5,
]
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Path {
pub steps: Vec<Step>,
pub cost: f64,
pub partial: bool,
pub expanded: usize,
pub version: Version,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PlanError {
StartUnknown,
StartBlocked,
NoPath,
}
impl std::fmt::Display for PlanError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PlanError::StartUnknown => "start block not loaded",
PlanError::StartBlocked => "cannot stand at the start",
PlanError::NoPath => "no path",
})
}
}
impl std::error::Error for PlanError {}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Pathfinder {
cfg: Config,
}
#[derive(Clone, Copy)]
pub(crate) struct Edge {
pub(crate) to: [i32; 3],
pub(crate) spot: Spot,
pub(crate) kind: MoveKind,
pub(crate) cost: f64,
pub(crate) dig: [Option<[i32; 3]>; 3],
pub(crate) place: Option<Place>,
}
#[derive(Clone)]
struct Rec {
pos: [i32; 3],
spot: Spot,
g: f64,
h: f64,
parent: u32,
work: u32,
kind: MoveKind,
closed: bool,
}
const NO_WORK: u32 = u32::MAX;
type Work = ([Option<[i32; 3]>; 3], Option<Place>);
#[derive(Clone, PartialEq)]
struct Open {
f: f64,
h: f64,
idx: u32,
}
impl Eq for Open {}
impl PartialOrd for Open {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Open {
fn cmp(&self, other: &Self) -> Ordering {
other
.f
.total_cmp(&self.f)
.then_with(|| other.h.total_cmp(&self.h))
}
}
#[inline]
pub(crate) fn pack(p: [i32; 3]) -> u64 {
(p[0] as u32 as u64 & 0x3ff_ffff) << 34
| (p[2] as u32 as u64 & 0x3ff_ffff) << 8
| (p[1] as u8 as u64)
| ((p[1] >> 8) as u32 as u64 & 0xf) << 60
}
impl Pathfinder {
#[must_use]
pub fn new(cfg: Config) -> Self {
Pathfinder { cfg }
}
#[must_use]
pub fn config(&self) -> &Config {
&self.cfg
}
#[must_use]
pub fn start_block(pos: [f64; 3]) -> [i32; 3] {
[
pos[0].floor() as i32,
(pos[1] + 1e-4).floor() as i32,
pos[2].floor() as i32,
]
}
pub fn plan<S: BlockSource + ?Sized>(
&self,
version: Version,
world: &S,
from: [i32; 3],
goal: &Goal,
) -> Result<Path, PlanError> {
self.plan_avoiding(version, world, from, goal, &HashSet::new())
}
pub fn plan_avoiding<S: BlockSource + ?Sized>(
&self,
version: Version,
world: &S,
from: [i32; 3],
goal: &Goal,
banned: &HashSet<([i32; 3], [i32; 3])>,
) -> Result<Path, PlanError> {
let mut search = self.search(version, from, goal.clone(), banned.clone());
match search.step(world, Budget::UNLIMITED) {
Some(r) => r,
None => unreachable!("an unlimited slice finishes the search"),
}
}
#[must_use]
pub fn search(
&self,
version: Version,
from: [i32; 3],
goal: Goal,
banned: HashSet<([i32; 3], [i32; 3])>,
) -> Search {
Search {
cfg: self.cfg.clone(),
heur: self.heuristic(Costs::of(version)),
version,
from,
goal,
banned,
state: None,
result: None,
exhausted: false,
}
}
pub fn plan_verified<S: BlockSource + ?Sized>(
&self,
version: Version,
world: &S,
from: [i32; 3],
goal: &Goal,
attempts: usize,
) -> Result<(Path, SimReport), PlanError> {
let ctrl = ControllerConfig::default().with_sprint(self.cfg.sprint);
self.plan_verified_avoiding(
version,
world,
from,
goal,
&mut HashSet::new(),
attempts,
&ctrl,
)
}
#[allow(clippy::too_many_arguments)]
pub fn plan_verified_avoiding<S: BlockSource + ?Sized>(
&self,
version: Version,
world: &S,
from: [i32; 3],
goal: &Goal,
banned: &mut HashSet<([i32; 3], [i32; 3])>,
attempts: usize,
controller: &ControllerConfig,
) -> Result<(Path, SimReport), PlanError> {
let mut last = None;
for _ in 0..attempts.max(1) {
let path = self.plan_avoiding(version, world, from, goal, banned)?;
let sim = simulate(&path, world, controller);
if sim.outcome == SimOutcome::Arrived {
return Ok((path, sim));
}
let k = (sim.reached + 1).min(path.steps.len() - 1);
if k == 0 || !banned.insert((path.steps[k - 1].pos, path.steps[k].pos)) {
return Ok((path, sim));
}
last = Some((path, sim));
}
Ok(last.expect("at least one attempt"))
}
fn heuristic(&self, c: &Costs) -> Heuristic {
let per_block = 1.0
/ if self.cfg.sprint {
c.sprint_speed
} else {
c.walk_speed
};
let per_up = (c.jump_up)
.min(1.0 / c.climb_up_speed)
.min(1.0 / c.swim_up_speed)
.max(1.0);
let fall = c.fall_ticks(24.0) / 24.0;
let per_down = fall
.min(1.0 / c.climb_down_speed)
.min(1.0 / c.swim_down_speed);
Heuristic {
per_block,
per_up,
per_down,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Budget {
pub nodes: usize,
pub time: Option<std::time::Duration>,
}
impl Budget {
pub const UNLIMITED: Budget = Budget {
nodes: usize::MAX,
time: None,
};
#[must_use]
pub const fn nodes(n: usize) -> Budget {
Budget {
nodes: n,
time: None,
}
}
#[must_use]
pub const fn with_time(mut self, d: std::time::Duration) -> Budget {
self.time = Some(d);
self
}
}
#[derive(Clone)]
struct SearchState {
cache: Sections,
recs: Vec<Rec>,
work: Vec<Work>,
index: HashMap<u64, u32, BuildKeyHasher>,
open: BinaryHeap<Open>,
best: u32,
expanded: usize,
edges: Vec<Edge>,
building: usize,
}
const BUILDING_NODES: usize = 4096;
const BUILD_SLACK: f64 = 0.5;
fn is_building(e: &Edge) -> bool {
match e.kind {
MoveKind::Pillar | MoveKind::DigDown => true,
MoveKind::Descend => e.dig[0].is_some(),
_ => e.place.is_some_and(|p| p.face()[1] == 0),
}
}
#[derive(Clone)]
pub struct Search {
cfg: Config,
heur: Heuristic,
version: Version,
from: [i32; 3],
goal: Goal,
banned: HashSet<([i32; 3], [i32; 3])>,
state: Option<SearchState>,
result: Option<Result<Path, PlanError>>,
exhausted: bool,
}
impl std::fmt::Debug for Search {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Search")
.field("from", &self.from)
.field("goal", &self.goal)
.field("expanded", &self.expanded())
.field("done", &self.result.is_some())
.finish_non_exhaustive()
}
}
impl Search {
#[cfg(ghast_modern)]
#[must_use]
pub fn with_modern_player(mut self, player: &ghast_physics::modern::PlayerState) -> Search {
self.cfg.limits = Limits::of(player);
self
}
#[must_use]
pub fn expanded(&self) -> usize {
self.state.as_ref().map_or(0, |s| s.expanded)
}
#[must_use]
pub fn from(&self) -> [i32; 3] {
self.from
}
#[must_use]
pub fn goal(&self) -> &Goal {
&self.goal
}
#[must_use]
pub fn is_done(&self) -> bool {
self.result.is_some()
}
#[must_use]
pub fn exhausted(&self) -> bool {
self.exhausted
}
pub fn step<S: BlockSource + ?Sized>(
&mut self,
world: &S,
budget: Budget,
) -> Option<Result<Path, PlanError>> {
if let Some(r) = &self.result {
return Some(r.clone());
}
let started = budget.time.map(|_| std::time::Instant::now());
let cache = self.state.as_mut().map(|s| std::mem::take(&mut s.cache));
let mut g = match cache {
Some(c) => Grid::with_cache(world, self.version, c),
None => Grid::new(world, self.version),
};
if self.state.is_none() {
let start = match start_spot(&mut g, self.from) {
Ok(s) => s,
Err(e) => {
self.result = Some(Err(e));
return self.result.clone();
}
};
let h0 = self.goal.estimate(self.from, &self.heur);
let mut st = SearchState {
cache: Sections::default(),
recs: Vec::with_capacity(4096),
work: Vec::new(),
index: HashMap::with_capacity_and_hasher(4096, BuildKeyHasher),
open: BinaryHeap::with_capacity(4096),
best: 0,
expanded: 0,
edges: Vec::with_capacity(32),
building: 0,
};
st.recs.push(Rec {
pos: self.from,
spot: start,
g: 0.0,
h: h0,
parent: u32::MAX,
work: NO_WORK,
kind: MoveKind::Start,
closed: false,
});
st.index.insert(pack(self.from), 0);
st.open.push(Open {
f: h0 * self.cfg.heuristic_weight,
h: h0,
idx: 0,
});
self.state = Some(st);
}
let costs = Costs::of(self.version);
let w = self.cfg.heuristic_weight;
let st = self.state.as_mut().expect("initialised above");
let limit = budget.nodes.max(1);
let mut n = 0usize;
let mut end: Option<Option<u32>> = None;
loop {
if n >= limit {
break;
}
if n % 64 == 63
&& let (Some(t0), Some(max)) = (started, budget.time)
&& t0.elapsed() >= max
{
break;
}
let Some(Open { idx, .. }) = st.open.pop() else {
self.exhausted = true;
end = Some(None);
break;
};
let r = &mut st.recs[idx as usize];
if r.closed {
continue;
}
r.closed = true;
let (pos, sp, gcost) = (r.pos, r.spot, r.g);
if self.goal.is_reached(pos) {
end = Some(Some(idx));
break;
}
st.expanded += 1;
n += 1;
if st.expanded > self.cfg.max_nodes {
end = Some(None);
break;
}
st.edges.clear();
successors(
&mut g,
&self.cfg,
self.version,
costs,
pos,
&sp,
&mut st.edges,
);
for e in &st.edges {
if !self.banned.is_empty() && self.banned.contains(&(pos, e.to)) {
continue;
}
let ng = gcost + e.cost;
let key = pack(e.to);
let building = is_building(e);
if building
&& self.goal.blocks_left(e.to) > self.goal.blocks_left(pos) + BUILD_SLACK
{
continue;
}
let work = |w: &mut Vec<Work>| {
if e.dig[0].is_none() && e.place.is_none() {
NO_WORK
} else {
w.push((e.dig, e.place));
(w.len() - 1) as u32
}
};
match st.index.get(&key) {
Some(&i) => {
let rr = &mut st.recs[i as usize];
if rr.closed || ng >= rr.g - 1e-9 {
continue;
}
rr.g = ng;
rr.parent = idx;
rr.spot = e.spot;
rr.kind = e.kind;
let h = rr.h;
let wk = work(&mut st.work);
st.recs[i as usize].work = wk;
st.open.push(Open {
f: ng + h * w,
h,
idx: i,
});
}
None => {
if building && e.place.is_some() {
if st.building >= BUILDING_NODES {
continue;
}
st.building += 1;
}
let h = self.goal.estimate(e.to, &self.heur);
let i = st.recs.len() as u32;
let wk = work(&mut st.work);
st.recs.push(Rec {
pos: e.to,
spot: e.spot,
g: ng,
h,
parent: idx,
work: wk,
kind: e.kind,
closed: false,
});
st.index.insert(key, i);
let b = &st.recs[st.best as usize];
if h < b.h || (h == b.h && ng < b.g) {
st.best = i;
}
st.open.push(Open {
f: ng + h * w,
h,
idx: i,
});
}
}
}
}
st.cache = g.into_cache();
let found = end?;
let r = st.finish(found, self.exhausted, self.version);
let expanded = st.expanded;
self.state = Some(SearchState {
cache: Sections::default(),
recs: Vec::new(),
work: Vec::new(),
index: HashMap::default(),
open: BinaryHeap::new(),
best: 0,
expanded,
edges: Vec::new(),
building: 0,
});
self.result = Some(r);
self.result.clone()
}
}
impl SearchState {
fn finish(
&self,
found: Option<u32>,
exhausted: bool,
version: Version,
) -> Result<Path, PlanError> {
let (end, partial) = match found {
Some(i) => (i, false),
None if self.best != 0 => (self.best, true),
None if !exhausted => match self
.open
.iter()
.filter(|o| !self.recs[o.idx as usize].closed)
.min_by(|a, b| a.f.total_cmp(&b.f).then(a.h.total_cmp(&b.h)))
{
Some(o) => (o.idx, true),
None => return Err(PlanError::NoPath),
},
None => return Err(PlanError::NoPath),
};
let mut steps = Vec::new();
let mut i = end;
while i != u32::MAX {
let r = &self.recs[i as usize];
let (dig, place) = match self.work.get(r.work as usize) {
Some((d, p)) => (d.iter().flatten().copied().collect(), *p),
None => (Vec::new(), None),
};
steps.push(Step {
pos: r.pos,
feet_y: r.spot.floor,
kind: r.kind,
node: r.spot.kind,
dig,
place,
});
i = r.parent;
}
steps.reverse();
Ok(Path {
cost: self.recs[end as usize].g,
steps,
partial,
expanded: self.expanded,
version,
})
}
}
fn start_spot<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
p: [i32; 3],
) -> Result<Spot, PlanError> {
if g.unknown(p[0], p[1], p[2]) || g.unknown(p[0], p[1] - 1, p[2]) {
return Err(PlanError::StartUnknown);
}
if let Some(s) = spot(g, p[0], p[1], p[2]) {
return Ok(s);
}
let base = f64::from(p[1]);
if clear(g, p[0], p[2], base, base + HEIGHT) {
return Ok(Spot {
kind: NodeKind::Ground,
floor: base,
floor_flags: 0,
water: false,
submerged: false,
climb: false,
});
}
Err(PlanError::StartBlocked)
}
const CARDINALS: [(i32, i32); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];
const DIAGONALS: [(i32, i32); 4] = [(1, 1), (1, -1), (-1, 1), (-1, -1)];
fn floor_penalty(s: &Spot, per_block: f64) -> f64 {
let mut p = 0.0;
if s.floor_flags & flag::SLOW != 0 {
p += per_block * 1.5;
}
if s.floor_flags & flag::SLIPPERY != 0 {
p += 2.0;
}
if s.floor_flags & flag::BOUNCY != 0 {
p += 12.0;
}
if s.submerged {
p += 4.0;
}
p
}
fn dig_cost<S: BlockSource + ?Sized>(g: &mut Grid<'_, S>, d: &DigCost, p: [i32; 3]) -> Option<f64> {
let c = g.class(p[0], p[1], p[2]);
if !c.has(flag::SOLID) {
return Some(0.0);
}
if !c.has(flag::DIGGABLE) || f64::from(c.hardness) > d.max_hardness {
return None;
}
if g.class(p[0], p[1] + 1, p[2])
.has(flag::FALLING | flag::WATER | flag::LAVA)
{
return None;
}
for (dx, dz) in CARDINALS {
if g.class(p[0] + dx, p[1], p[2] + dz)
.has(flag::WATER | flag::LAVA)
{
return None;
}
}
Some(d.base + d.ticks_per_hardness * f64::from(c.hardness))
}
fn push(out: &mut Vec<Edge>, to: [i32; 3], spot: Spot, kind: MoveKind, cost: f64) {
out.push(Edge {
to,
spot,
kind,
cost,
dig: [None; 3],
place: None,
});
}
pub(crate) fn successors<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cfg: &Config,
version: Version,
c: &Costs,
p: [i32; 3],
s: &Spot,
out: &mut Vec<Edge>,
) {
moves_from(g, cfg, version, c, p, s, out);
if g.saw_doors() {
through_doors(g, p, s, out);
}
}
fn moves_from<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cfg: &Config,
version: Version,
c: &Costs,
p: [i32; 3],
s: &Spot,
out: &mut Vec<Edge>,
) {
let per_block = 1.0
/ if cfg.sprint {
c.sprint_speed
} else {
c.walk_speed
};
let swim_block = 1.0 / c.swim_speed;
let [x, y, z] = p;
let f0 = s.floor;
match s.kind {
NodeKind::Swim if cfg.swim => {
if let Some(t) = spot(g, x, y + 1, z) {
push(
out,
[x, y + 1, z],
t,
MoveKind::Swim,
1.0 / c.swim_up_speed + floor_penalty(&t, per_block),
);
}
if let Some(t) = spot(g, x, y - 1, z) {
push(
out,
[x, y - 1, z],
t,
MoveKind::Swim,
1.0 / c.swim_down_speed,
);
}
}
NodeKind::Ground if cfg.swim && s.water => {
if let Some(t) = spot(g, x, y + 1, z)
&& t.kind == NodeKind::Swim
{
push(out, [x, y + 1, z], t, MoveKind::Swim, 1.0 / c.swim_up_speed);
}
}
_ => {}
}
if cfg.climb && s.climb {
if let Some(t) = spot(g, x, y + 1, z)
&& t.climb
&& (climb_wall(g, x, y, z).is_some() || climbs_without_wall(version))
{
push(
out,
[x, y + 1, z],
t,
MoveKind::ClimbUp,
1.0 / c.climb_up_speed,
);
}
if s.kind == NodeKind::Climb
&& let Some(t) = spot(g, x, y - 1, z)
&& (t.climb || t.kind == NodeKind::Ground)
{
push(
out,
[x, y - 1, z],
t,
MoveKind::ClimbDown,
1.0 / c.climb_down_speed,
);
}
}
if s.kind == NodeKind::Ground
&& !s.water
&& let Some(d) = &cfg.dig
{
dig_down(g, cfg, d, c, p, f0, out);
}
if s.kind == NodeKind::Ground
&& !s.water
&& let Some(pc) = cfg.place
{
pillar(g, pc, c, p, s, out);
}
let swimming = s.kind == NodeKind::Swim;
let climbing = s.kind == NodeKind::Climb;
if (swimming && !cfg.swim) || (climbing && !cfg.climb) {
return;
}
for (dx, dz) in CARDINALS {
let (bx, bz) = (x + dx, z + dz);
let mut found = false;
if let Some(t) = spot(g, bx, y + 1, bz)
&& t.kind == NodeKind::Ground
{
let dh = t.floor - f0;
if climbing {
if dh <= 1.5 + 1e-6 && clear(g, x, z, f0 + HEIGHT, t.floor + HEIGHT) {
push(
out,
[bx, y + 1, bz],
t,
MoveKind::ClimbExit,
1.0 / c.climb_up_speed + per_block + floor_penalty(&t, per_block),
);
found = true;
}
} else if swimming || s.water {
if dh <= 1.2 && clear(g, x, z, f0 + HEIGHT, t.floor + HEIGHT + 0.4) {
push(
out,
[bx, y + 1, bz],
t,
MoveKind::Swim,
swim_block + c.jump_up + floor_penalty(&t, per_block),
);
found = true;
}
} else if dh <= cfg.limits.step + 1e-6 {
if clear(g, x, z, f0 + HEIGHT, t.floor + HEIGHT) {
push(
out,
[bx, y + 1, bz],
t,
MoveKind::Walk,
per_block + 0.5 + floor_penalty(&t, per_block),
);
found = true;
}
} else if dh <= cfg.limits.jump_up && jump_room(g, x, z, bx, bz, f0, &t) {
push(
out,
[bx, y + 1, bz],
t,
MoveKind::Ascend,
c.jump_up + floor_penalty(&t, per_block),
);
found = true;
}
}
if !found
&& !swimming
&& !climbing
&& let Some(d) = &cfg.dig
&& (f0 - f64::from(y)).abs() < 1e-6
{
found |= dig_up(g, d, c, p, (dx, dz), out);
}
if found {
continue;
}
let lowest = y - (cfg.max_fall.max(cfg.max_water_fall).ceil() as i32) - 1;
let mut yt = y;
while yt >= lowest.max(g.y_range().0 + 1) {
if let Some(t) = spot(g, bx, yt, bz) {
let dh = t.floor - f0;
let top = t.floor.max(f0) + HEIGHT;
if dh > cfg.limits.step + 1e-6 {
if !swimming
&& !climbing
&& dh <= cfg.limits.jump_up
&& jump_room(g, x, z, bx, bz, f0, &t)
{
push(
out,
[bx, yt, bz],
t,
MoveKind::Ascend,
c.jump_up + floor_penalty(&t, per_block),
);
} else if (swimming || s.water) && dh <= 1.2 {
push(out, [bx, yt, bz], t, MoveKind::Swim, swim_block + c.jump_up);
}
} else if dh >= -STEP - 1e-6 {
let kind = if swimming || t.kind == NodeKind::Swim || (s.water && t.water) {
MoveKind::Swim
} else {
MoveKind::Walk
};
let ok = match t.kind {
NodeKind::Swim => cfg.swim,
NodeKind::Climb => cfg.climb,
NodeKind::Ground => true,
};
if ok
&& clear(g, x, z, f0 + HEIGHT, top)
&& clear(g, bx, bz, t.floor + HEIGHT, top)
{
let cost = if kind == MoveKind::Swim {
swim_block
} else {
per_block
};
push(
out,
[bx, yt, bz],
t,
kind,
cost + floor_penalty(&t, per_block),
);
}
} else if t.kind == NodeKind::Climb && cfg.climb && -dh <= 1.5 + 1e-6 {
if clear(g, bx, bz, t.floor + HEIGHT, f0 + HEIGHT) {
push(
out,
[bx, yt, bz],
t,
MoveKind::ClimbDown,
per_block + (-dh) / c.climb_down_speed,
);
}
} else if !climbing {
let drop = -dh;
let deep = t.kind == NodeKind::Swim && g.class(bx, yt - 1, bz).has(flag::WATER);
let into_water = t.kind == NodeKind::Swim || t.water;
let limit = if deep {
cfg.max_water_fall
} else {
cfg.max_fall
};
let landing_ok = t.floor_flags & (flag::FRAGILE | flag::BOUNCY) == 0;
if drop <= limit + 1e-6
&& landing_ok
&& (cfg.swim || !into_water)
&& t.kind != NodeKind::Climb
&& clear(g, bx, bz, t.floor + HEIGHT, f0 + HEIGHT)
{
push(
out,
[bx, yt, bz],
t,
MoveKind::Descend,
per_block + c.fall_ticks(drop) + 2.0 + floor_penalty(&t, per_block),
);
}
}
found = true;
break;
}
if !clear(g, bx, bz, f64::from(yt), f64::from(yt) + 1.0) {
break;
}
yt -= 1;
}
if !found
&& !swimming
&& !climbing
&& let Some(d) = &cfg.dig
{
dig_through(g, d, per_block, p, f0, (dx, dz), out);
dig_stair(g, d, c, per_block, p, f0, (dx, dz), out);
}
if !found && !swimming && !climbing && g.saw_doors() {
open_block(g, version, per_block, p, f0, (dx, dz), out);
}
if !swimming
&& !climbing
&& let Some(pc) = cfg.place
&& spot(g, bx, y, bz).is_none()
&& !fill_hole(g, pc, per_block, p, f0, (dx, dz), out)
{
bridge(g, pc, per_block, p, s, (dx, dz), out);
}
if cfg.parkour
&& cfg.limits.parkour
&& !swimming
&& !climbing
&& s.kind == NodeKind::Ground
&& !s.water
{
parkour(g, cfg, version, p, s, (dx, dz), out);
}
}
if climbing {
return;
}
for (dx, dz) in DIAGONALS {
let (bx, bz) = (x + dx, z + dz);
for yt in [y + 1, y, y - 1] {
let Some(t) = spot(g, bx, yt, bz) else {
continue;
};
let dh = t.floor - f0;
if dh > cfg.limits.step + 1e-6 || dh < -STEP - 1e-6 {
if yt == y {
break;
}
continue;
}
if (t.kind == NodeKind::Swim) != swimming {
break;
}
let lo = t.floor.max(f0);
let hi = lo + HEIGHT;
let floored = |g: &mut Grid<'_, S>, sx: i32, sz: i32| {
[y, yt].iter().any(|&yy| {
spot(g, sx, yy, sz).is_some_and(|sp| {
sp.kind == NodeKind::Ground && sp.floor >= t.floor.min(f0) - STEP - 1e-6
})
})
};
if !swimming && (!floored(g, x + dx, z) || !floored(g, x, z + dz)) {
break;
}
if clear(g, x + dx, z, lo, hi)
&& clear(g, x, z + dz, lo, hi)
&& clear(g, x, z, f0 + HEIGHT, hi)
&& clear(g, bx, bz, t.floor + HEIGHT, hi)
{
let base = if swimming || (s.water && t.water) {
swim_block
} else {
per_block
};
let kind = if swimming || (s.water && t.water) {
MoveKind::Swim
} else {
MoveKind::Diagonal
};
push(
out,
[bx, yt, bz],
t,
kind,
base * std::f64::consts::SQRT_2
+ floor_penalty(&t, per_block)
+ if dh > 1e-6 { 0.5 } else { 0.0 },
);
}
break;
}
}
}
fn jump_room<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
z: i32,
bx: i32,
bz: i32,
f0: f64,
t: &Spot,
) -> bool {
let top = f0 + HEIGHT + JUMP_APEX;
let ca = ceiling(g, x, z, f0 + HEIGHT, top);
let cb = ceiling(g, bx, bz, t.floor + HEIGHT, top);
let apex = ca.min(cb) - HEIGHT - f0;
apex >= (t.floor - f0) + JUMP_MARGIN
}
fn parkour<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cfg: &Config,
version: Version,
p: [i32; 3],
s: &Spot,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) {
let [x, y, z] = p;
if (s.floor - f64::from(y)).abs() > 1e-6 || !jump::plain_floor(g.class(x, y - 1, z)) {
return;
}
let walkable = |g: &mut Grid<'_, S>, cx: i32, cz: i32| {
[y + 1, y].iter().any(|&yy| {
spot(g, cx, yy, cz).is_some_and(|t| (t.floor - s.floor).abs() <= STEP + 1e-6)
})
};
if walkable(g, x + dx, z + dz) {
return;
}
let jumps = jump::Jumps::of(version);
for gap in 1..=jumps.widest() {
if gap > 1 && walkable(g, x + dx * i32::from(gap), z + dz * i32::from(gap)) {
return;
}
let a = i32::from(gap) + 1;
let (tx, tz) = (x + dx * a, z + dz * a);
for dy in jump::DYS {
let ty = y + i32::from(dy);
let Some(t) = spot(g, tx, ty, tz) else {
continue;
};
if t.kind != NodeKind::Ground
|| t.water
|| (t.floor - f64::from(ty)).abs() > 1e-6
|| !jump::plain_floor(g.class(tx, ty - 1, tz))
{
continue;
}
let Some(spec) = jump::select(&jumps, p, (dx, dz), gap, dy, cfg.sprint, |q| {
g.class(q[0], q[1], q[2])
}) else {
continue;
};
if f64::from(spec.fall) > cfg.max_fall + 1e-6 {
continue;
}
push(
out,
[tx, ty, tz],
t,
MoveKind::Parkour { gap },
f64::from(spec.cost),
);
}
}
}
const DOOR_TICKS: f64 = 8.0;
fn panels_in<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
x: i32,
z: i32,
lo: f64,
hi: f64,
) -> ([(i32, Class); 4], usize) {
let mut out = [(0, Class::UNKNOWN); 4];
let mut n = 0;
for y in lo.floor() as i32..=(hi - 1e-6).floor() as i32 {
let c = g.class(x, y, z);
if c.has(flag::PANEL) && n < 4 {
out[n] = (y, c);
n += 1;
}
}
(out, n)
}
fn through_doors<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
p: [i32; 3],
s: &Spot,
out: &mut Vec<Edge>,
) {
let [x, _, z] = p;
let mut i = 0;
while i < out.len() {
let e = out[i];
let (dx, dz) = (e.to[0] - x, e.to[2] - z);
if dx == 0 && dz == 0 {
i += 1;
continue;
}
let lo = s.floor.min(e.spot.floor);
let hi = s.floor.max(e.spot.floor) + HEIGHT;
let (from, nf) = panels_in(g, x, z, lo, hi);
let (to, nt) = panels_in(g, e.to[0], e.to[2], lo, hi);
if nf == 0 && nt == 0 {
i += 1;
continue;
}
let keep = if dx.abs() + dz.abs() != 1 || matches!(e.kind, MoveKind::Parkour { .. }) {
false
} else {
let (out_wall, in_wall) = (door::wall_of(dx, dz), door::wall_of(-dx, -dz));
let mut blocker: Option<([i32; 3], bool)> = None;
let mut many = false;
for (col, list, n, wall) in [
([x, z], &from, nf, out_wall),
([e.to[0], e.to[2]], &to, nt, in_wall),
] {
for &(y, c) in &list[..n] {
if c.wall != wall {
continue;
}
let at = [col[0], y, col[1]];
let open = c.has(flag::OPENABLE) && !c.has(flag::HATCH);
match blocker {
None => blocker = Some((at, open)),
Some((b, o)) => {
if b[0] == at[0] && b[2] == at[2] {
blocker = Some((if at[1] < b[1] { at } else { b }, o && open));
} else {
many = true;
}
}
}
}
}
match blocker {
None => true,
Some((at, true))
if !many
&& e.kind == MoveKind::Walk
&& e.dig[0].is_none()
&& e.place.is_none() =>
{
out[i].kind = MoveKind::Door { block: at };
out[i].cost += DOOR_TICKS;
true
}
Some(_) => false,
}
};
if keep {
i += 1;
} else {
out.remove(i);
}
}
}
fn open_block<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
version: Version,
per_block: f64,
p: [i32; 3],
f0: f64,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) {
let [x, y, z] = p;
if (f0 - f64::from(y)).abs() > 1e-6 {
return;
}
let (bx, bz) = (x + dx, z + dz);
let entry = door::wall_of(-dx, -dz);
let passes = |c: &Class, hi: f32| {
!c.blocks_standing(0.0, hi) && !(c.has(flag::PANEL) && c.wall == entry)
};
let mut blocker = None;
for (yy, hi) in [(y, 1.0f32), (y + 1, (HEIGHT - 1.0) as f32)] {
let c = g.class(bx, yy, bz);
if c.has(flag::AVOID | flag::LAVA) {
return;
}
if passes(&c, hi) {
continue;
}
if !c.has(flag::OPENABLE) || blocker.is_some() {
return;
}
blocker = Some((yy, hi));
}
let Some((yy, hi)) = blocker else {
return;
};
let fl = g.class(bx, y - 1, bz);
if !fl.has(flag::SUPPORT)
|| fl.has(flag::IRREGULAR | flag::AVOID_FLOOR)
|| fl.top < 1.0 - 1e-6
|| fl.top_any > 1.0 + 1e-6
{
return;
}
match door::toggled_class(version, g.source(), [bx, yy, bz]) {
Some(after) if passes(&after, hi) => {}
_ => return,
}
out.push(Edge {
to: [bx, y, bz],
spot: Spot {
kind: NodeKind::Ground,
floor: f0,
floor_flags: fl.flags,
water: false,
submerged: false,
climb: false,
},
kind: MoveKind::Door {
block: [bx, yy, bz],
},
cost: per_block + DOOR_TICKS,
dig: [None; 3],
place: None,
});
}
fn dig_through<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
d: &DigCost,
per_block: f64,
p: [i32; 3],
f0: f64,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) -> bool {
let [x, y, z] = p;
if (f0 - f64::from(y)).abs() > 1e-6 {
return false;
}
let (bx, bz) = (x + dx, z + dz);
let fl = g.class(bx, y - 1, bz);
if !(fl.has(flag::SUPPORT) && fl.top >= 1.0 - 1e-6 && fl.top_any <= 1.0 + 1e-6)
|| fl.has(flag::AVOID_FLOOR | flag::IRREGULAR)
{
return false;
}
let mut cost = per_block;
let mut dig = [None; 3];
let mut n = 0;
for yy in [y + 1, y] {
let c = g.class(bx, yy, bz);
if g.unknown(bx, yy, bz) || c.has(flag::AVOID | flag::LAVA | flag::WATER) {
return false;
}
if c.has(flag::SOLID) {
match dig_cost(g, d, [bx, yy, bz]) {
Some(k) => {
cost += k;
dig[n] = Some([bx, yy, bz]);
n += 1;
}
None => return false,
}
}
}
if n == 0 {
return false;
}
let spot = Spot {
kind: NodeKind::Ground,
floor: f0,
floor_flags: fl.flags,
water: false,
submerged: false,
climb: false,
};
out.push(Edge {
to: [bx, y, bz],
spot,
kind: MoveKind::Walk,
cost,
dig,
place: None,
});
true
}
fn dig_down<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cfg: &Config,
d: &DigCost,
c: &Costs,
p: [i32; 3],
f0: f64,
out: &mut Vec<Edge>,
) {
let [x, y, z] = p;
if (f0 - f64::from(y)).abs() > 1e-6 {
return;
}
let below = [x, y - 1, z];
let fl = g.class(x, y - 1, z);
if !fl.has(flag::SUPPORT) || fl.has(flag::IRREGULAR) || fl.top < 1.0 - 1e-6 {
return;
}
let Some(k) = dig_cost(g, d, below) else {
return;
};
let lowest = (y - cfg.max_fall.floor() as i32).max(g.y_range().0 + 1);
let mut yt = y - 1;
while yt >= lowest {
if yt < y - 1 {
let c0 = g.class(x, yt, z);
if c0.has(flag::SOLID | flag::WATER | flag::LAVA | flag::AVOID | flag::CLIMB) {
return;
}
}
let cb = g.class(x, yt - 1, z);
if cb.has(flag::WATER | flag::LAVA | flag::AVOID | flag::CLIMB) {
return;
}
if cb.has(flag::SOLID) {
if !cb.has(flag::SUPPORT)
|| cb.has(flag::IRREGULAR | flag::AVOID_FLOOR | flag::FRAGILE | flag::BOUNCY)
|| cb.top < 1.0 - 1e-6
|| cb.top_any > 1.0 + 1e-6
{
return;
}
let drop = f64::from(y - yt);
out.push(Edge {
to: [x, yt, z],
spot: Spot {
kind: NodeKind::Ground,
floor: f64::from(yt),
floor_flags: cb.flags,
water: false,
submerged: false,
climb: false,
},
kind: MoveKind::DigDown,
cost: k + c.fall_ticks(drop) + 4.0,
dig: [Some(below), None, None],
place: None,
});
return;
}
yt -= 1;
}
}
#[allow(clippy::too_many_arguments)]
fn dig_stair<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
d: &DigCost,
c: &Costs,
per_block: f64,
p: [i32; 3],
f0: f64,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) -> bool {
let [x, y, z] = p;
if (f0 - f64::from(y)).abs() > 1e-6 || y - 1 <= g.y_range().0 {
return false;
}
let (bx, bz) = (x + dx, z + dz);
let fl = g.class(bx, y - 2, bz);
if !fl.has(flag::SUPPORT)
|| fl.has(flag::IRREGULAR | flag::AVOID_FLOOR | flag::FRAGILE | flag::BOUNCY | flag::WATER)
|| fl.top < 1.0 - 1e-6
|| fl.top_any > 1.0 + 1e-6
{
return false;
}
let mut cost = per_block + c.fall_ticks(1.0) + 2.0;
let mut dig = [None; 3];
let mut n = 0;
for yy in [y + 1, y, y - 1] {
let q = [bx, yy, bz];
let cl = g.class(q[0], q[1], q[2]);
if g.unknown(q[0], q[1], q[2]) || cl.has(flag::AVOID | flag::LAVA | flag::WATER) {
return false;
}
if cl.has(flag::SOLID) {
match dig_cost(g, d, q) {
Some(k) => {
cost += k;
dig[n] = Some(q);
n += 1;
}
None => return false,
}
}
}
if n == 0 {
return false;
}
out.push(Edge {
to: [bx, y - 1, bz],
spot: Spot {
kind: NodeKind::Ground,
floor: f0 - 1.0,
floor_flags: fl.flags,
water: false,
submerged: false,
climb: false,
},
kind: MoveKind::Descend,
cost,
dig,
place: None,
});
true
}
fn dig_up<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
d: &DigCost,
c: &Costs,
p: [i32; 3],
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) -> bool {
let [x, y, z] = p;
let (bx, bz) = (x + dx, z + dz);
let fl = g.class(bx, y, bz);
if !(fl.has(flag::SUPPORT) && fl.top >= 1.0 - 1e-6 && fl.top_any <= 1.0 + 1e-6)
|| fl.has(flag::AVOID_FLOOR | flag::IRREGULAR)
{
return false;
}
let cells = [[x, y + 2, z], [bx, y + 2, bz], [bx, y + 1, bz]];
let mut cost = c.jump_up;
let mut dig = [None; 3];
let mut n = 0;
for q in cells {
let cl = g.class(q[0], q[1], q[2]);
if g.unknown(q[0], q[1], q[2]) || cl.has(flag::AVOID | flag::LAVA | flag::WATER) {
return false;
}
if cl.has(flag::SOLID) {
match dig_cost(g, d, q) {
Some(k) => {
cost += k;
dig[n] = Some(q);
n += 1;
}
None => return false,
}
}
}
if n == 0 {
return false;
}
let f0 = f64::from(y);
let t_floor = f0 + 1.0;
let arc = (f0 + HEIGHT + JUMP_APEX - (f0 + 3.0)) as f32;
if g.class(x, y + 3, z).blocks_between(0.0, arc)
|| g.class(bx, y + 3, bz).blocks_between(0.0, arc)
{
return false;
}
let spot = Spot {
kind: NodeKind::Ground,
floor: t_floor,
floor_flags: fl.flags,
water: false,
submerged: false,
climb: false,
};
out.push(Edge {
to: [bx, y + 1, bz],
spot,
kind: MoveKind::Ascend,
cost,
dig,
place: None,
});
true
}
fn fill_hole<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cost: f64,
per_block: f64,
p: [i32; 3],
f0: f64,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) -> bool {
let [x, y, z] = p;
if (f0 - f64::from(y)).abs() > 1e-6 {
return false;
}
let (bx, bz) = (x + dx, z + dz);
let hole = g.class(bx, y - 1, bz);
let under = g.class(bx, y - 2, bz);
if !hole.has(flag::REPLACEABLE)
|| g.unknown(bx, y - 1, bz)
|| !(under.has(flag::SUPPORT) && under.top >= 1.0 - 1e-6 && under.top_any <= 1.0 + 1e-6)
|| under.has(flag::IRREGULAR | flag::AVOID_FLOOR)
{
return false;
}
if !clear(g, bx, bz, f0, f0 + HEIGHT) {
return false;
}
let spot = Spot {
kind: NodeKind::Ground,
floor: f0,
floor_flags: flag::PLACED,
water: false,
submerged: false,
climb: false,
};
out.push(Edge {
to: [bx, y, bz],
spot,
kind: MoveKind::Walk,
cost: per_block + cost,
dig: [None; 3],
place: Some(Place {
pos: [bx, y - 1, bz],
against: [bx, y - 2, bz],
}),
});
true
}
const BRIDGE_TICKS: f64 = 16.0;
fn full_floor<S: BlockSource + ?Sized>(g: &mut Grid<'_, S>, p: [i32; 3], s: &Spot) -> bool {
if (s.floor - f64::from(p[1])).abs() > 1e-6 {
return false;
}
if s.floor_flags & flag::PLACED != 0 {
return true;
}
let fl = g.class(p[0], p[1] - 1, p[2]);
fl.has(flag::SUPPORT)
&& !fl.has(flag::IRREGULAR | flag::THIN)
&& fl.bottom <= 1e-6
&& fl.top >= 1.0 - 1e-6
&& fl.top_any <= 1.0 + 1e-6
}
fn bridge<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cost: f64,
per_block: f64,
p: [i32; 3],
s: &Spot,
(dx, dz): (i32, i32),
out: &mut Vec<Edge>,
) -> bool {
let [x, y, z] = p;
if s.water || !full_floor(g, p, s) {
return false;
}
let (bx, bz) = (x + dx, z + dz);
if !g.class(bx, y - 1, bz).has(flag::REPLACEABLE) || g.unknown(bx, y - 1, bz) {
return false;
}
if !clear(g, bx, bz, s.floor, s.floor + HEIGHT) {
return false;
}
out.push(Edge {
to: [bx, y, bz],
spot: Spot {
kind: NodeKind::Ground,
floor: s.floor,
floor_flags: flag::PLACED,
water: false,
submerged: false,
climb: false,
},
kind: MoveKind::Walk,
cost: per_block + cost + BRIDGE_TICKS,
dig: [None; 3],
place: Some(Place {
pos: [bx, y - 1, bz],
against: [x, y - 1, z],
}),
});
true
}
fn pillar<S: BlockSource + ?Sized>(
g: &mut Grid<'_, S>,
cost: f64,
c: &Costs,
p: [i32; 3],
s: &Spot,
out: &mut Vec<Edge>,
) {
let [x, y, z] = p;
if y + 1 >= g.y_range().1 - 1 || !full_floor(g, p, s) {
return;
}
if !g.class(x, y, z).has(flag::REPLACEABLE) {
return;
}
if !clear(g, x, z, s.floor + HEIGHT, s.floor + HEIGHT + JUMP_APEX) {
return;
}
out.push(Edge {
to: [x, y + 1, z],
spot: Spot {
kind: NodeKind::Ground,
floor: s.floor + 1.0,
floor_flags: flag::PLACED,
water: false,
submerged: false,
climb: false,
},
kind: MoveKind::Pillar,
cost: c.jump_up + cost + 4.0,
dig: [None; 3],
place: Some(Place {
pos: [x, y, z],
against: [x, y - 1, z],
}),
});
}
fn climbs_without_wall(version: Version) -> bool {
match version {
#[cfg(feature = "v1_16_5")]
Version::V1_16_5 => true,
#[cfg(feature = "v26_1")]
Version::V26_1 => true,
#[cfg(feature = "v26_2")]
Version::V26_2 => true,
#[cfg(feature = "v26_3")]
Version::V26_3 => true,
#[allow(unreachable_patterns)]
_ => false,
}
}
#[cfg(test)]
mod pack_tests {
use super::pack;
#[test]
fn node_keys_are_unique_over_any_build_height() {
let mut seen = std::collections::HashSet::new();
for y in -2048..2048 {
assert!(seen.insert(pack([-3, y, 5])), "y {y}");
}
assert_eq!(pack([1, 200, 2]), 1 << 34 | 2 << 8 | 200);
}
}