use ghast_protocol::{BlockPos, RawPacket};
use super::*;
use crate::common::{Abilities, Controls, Outbound, Pose, Teleport, Vec3};
use crate::hands::{BlockQuery, HeldItem};
pub(crate) struct Sim16 {
world: ghast_world::v1_16_5::World,
player: ghast_physics::v1_16_5::PlayerState,
packets: ghast_physics::Packets,
rec: Option<crate::record::Recorder>,
force_step_position: ForceStepPosition,
entities: Option<ghast_entity::v1_16_5::Tracker>,
updates: BlockUpdates,
session: SessionPackets,
block_entities: bool,
entity_push: bool,
teams: Option<Box<super::teams::Teams>>,
profile_name: Box<str>,
around: Around16,
filed_pos: [f64; 3],
ext: ghast_physics::v1_16_5::PlayerExt,
slow: bool,
rt: Riptide16,
unacked: crate::sim::predict::Unacked,
snapped: bool,
glide: Box<Glide16>,
ride: Option<Box<Ride16>>,
reply: Option<Outbound>,
parked: Option<Parked16>,
server_flags: Option<u8>,
absorption: f32,
living_flags: Option<u8>,
absorption_dirty: bool,
pistons: ghast_physics::v1_16_5::Pistons,
piston_edits: ghast_physics::v1_16_5::Edits,
piston_uncertain_at: Option<i32>,
piston_time: i64,
piston_carry: Vec<(i32, ghast_physics::v1_16_5::PistonCarry)>,
piston_moved_shulker: bool,
}
#[derive(Debug, Clone, Copy)]
struct Parked16 {
id: i32,
pos: [f64; 3],
bb: [f64; 6],
tracked: Option<[f64; 3]>,
}
fn view(
t: &ghast_entity::v1_16_5::Tracker,
parked: Option<Parked16>,
id: i32,
) -> (Option<[f64; 3]>, Option<[f64; 6]>) {
if let Some(k) = parked
&& k.id == id
&& t.client_position(id) == k.tracked
{
return (Some(k.pos), Some(k.bb));
}
(t.client_position(id), t.client_box(id))
}
#[derive(Debug)]
struct Cart16 {
id: i32,
cart: ghast_physics::v1_16_5::Minecart,
}
#[derive(Debug)]
struct BoatRide16 {
id: i32,
boat: ghast_physics::v1_16_5::Boat,
}
#[derive(Debug)]
enum Ride16 {
Boat(BoatRide16),
Cart(Cart16),
}
impl Ride16 {
fn id(&self) -> i32 {
match self {
Ride16::Boat(r) => r.id,
Ride16::Cart(c) => c.id,
}
}
}
fn boat_mut(r: &mut Option<Box<Ride16>>) -> Option<&mut BoatRide16> {
match r.as_deref_mut() {
Some(Ride16::Boat(b)) => Some(b),
_ => None,
}
}
fn cart_mut(r: &mut Option<Box<Ride16>>) -> Option<&mut Cart16> {
match r.as_deref_mut() {
Some(Ride16::Cart(c)) => Some(c),
_ => None,
}
}
#[derive(Debug, Default)]
struct Glide16 {
watch: bool,
pending: Vec<(i32, [f64; 3])>,
rockets: Vec<Rocket16>,
}
#[derive(Clone, Copy, Debug)]
struct Rocket16 {
id: i32,
pos: [f64; 3],
filed: bool,
before: bool,
}
const MAX_PENDING_ROCKETS_16: usize = 32;
const FIREWORK_ROCKET_16: i32 = 27;
#[derive(Debug, Default)]
struct HeldCells {
hands: std::cell::Cell<[(u8, bool, i32); 2]>,
watch: std::cell::Cell<bool>,
filter_dirty: std::cell::Cell<bool>,
}
#[derive(Debug)]
struct Riptide16 {
held: Box<HeldCells>,
using: Option<u8>,
use_ticks: u32,
rain_level: f32,
sky: bool,
seed: i64,
own_id: i32,
}
impl Default for Riptide16 {
fn default() -> Self {
Riptide16 {
held: Box::default(),
using: None,
use_ticks: 0,
rain_level: 0.0,
sky: true,
seed: 0,
own_id: i32::MIN,
}
}
}
fn voronoi_cell(seed: i64, x: i32, y: i32, z: i32) -> [i32; 3] {
let mix = |s: i64, salt: i64| {
s.wrapping_mul(
s.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407),
)
.wrapping_add(salt)
};
let distribute = |s: i64| (f64::from((s >> 24).rem_euclid(1024) as i32) / 1024.0 - 0.5) * 0.9;
let (x, y, z) = (x - 2, y - 2, z - 2);
let base = [x >> 2, y >> 2, z >> 2];
let frac = [
f64::from(x & 3) / 4.0,
f64::from(y & 3) / 4.0,
f64::from(z & 3) / 4.0,
];
let mut best = 0;
let mut dist = 0.0;
for o in 0..8 {
let hi = [o & 4 != 0, o & 2 != 0, o & 1 != 0];
let c: [i64; 3] = std::array::from_fn(|i| i64::from(base[i] + i32::from(hi[i])));
let f: [f64; 3] = std::array::from_fn(|i| if hi[i] { frac[i] - 1.0 } else { frac[i] });
let mut l = mix(seed, c[0]);
l = mix(l, c[1]);
l = mix(l, c[2]);
l = mix(l, c[0]);
l = mix(l, c[1]);
l = mix(l, c[2]);
let d = distribute(l);
l = mix(l, seed);
let e = distribute(l);
l = mix(l, seed);
let g = distribute(l);
let sq = |v: f64| v * v;
let v = sq(f[2] + g) + sq(f[1] + e) + sq(f[0] + d);
if o == 0 || dist > v {
best = o;
dist = v;
}
}
[
base[0] + i32::from(best & 4 != 0),
base[1] + i32::from(best & 2 != 0),
base[2] + i32::from(best & 1 != 0),
]
}
fn dimension_flag(dimension: &ghast_protocol::nbt::RawNbt, name: &str) -> Option<bool> {
use ghast_protocol::nbt::Tag;
let Ok(Some(t)) = dimension.parse() else {
return None;
};
match t.tag.as_compound().and_then(|c| c.get(name)) {
Some(Tag::Byte(b)) => Some(*b != 0),
_ => None,
}
}
fn trident_nbt(s: &ghast_inventory::Stack) -> (u8, i32) {
use ghast_protocol::nbt::Tag;
let Some(root) = s.nbt().and_then(|n| n.parse().ok().flatten()) else {
return (0, 0);
};
let Some(c) = root.tag.as_compound() else {
return (0, 0);
};
let num = |t: Option<&Tag>| match t {
Some(Tag::Short(v)) => i32::from(*v),
Some(Tag::Int(v)) => *v,
Some(Tag::Byte(v)) => i32::from(*v),
_ => 0,
};
let mut level = 0;
if let Some(Tag::List(l)) = c.get("Enchantments") {
for e in l.items.iter().filter_map(Tag::as_compound) {
if matches!(e.get("id"), Some(Tag::String(n)) if n == "minecraft:riptide") {
level = u8::try_from(num(e.get("lvl")).clamp(0, 255)).unwrap_or(0);
break;
}
}
}
(level, num(c.get("Damage")))
}
#[derive(Debug, Default)]
struct Around16 {
boxes: Vec<ghast_physics::math::Aabb>,
after: Vec<(u32, ghast_physics::v1_16_5::Pusher)>,
before: Vec<(u32, ghast_physics::v1_16_5::Pusher)>,
ticked: bool,
others: Vec<ghast_physics::v1_16_5::OtherEntity>,
}
const COLLIDES: u8 = 1;
const PUSH_LIVING: u8 = 2;
const PUSH_BOAT: u8 = 4;
fn type_name(e: &ghast_entity::Entity) -> Option<&'static str> {
static NAMES: std::sync::OnceLock<Box<[Option<&'static str>]>> = std::sync::OnceLock::new();
let names = NAMES.get_or_init(|| {
let types = ghast_data::v1_16_5::entities();
let n = types
.iter()
.map(|t| usize::from(t.id) + 1)
.max()
.unwrap_or(0);
let mut v = vec![None; n];
for t in types {
v[usize::from(t.id)] = Some(t.name);
}
v.into_boxed_slice()
});
match e.entity_type() {
ghast_entity::EntityType::Player => Some("player"),
ghast_entity::EntityType::Registry(id) => names.get(usize::from(id)).copied().flatten(),
_ => None,
}
}
fn entity_bits(e: &ghast_entity::Entity) -> u8 {
use ghast_physics::v1_16_5 as phys;
static BITS: std::sync::OnceLock<Box<[u8]>> = std::sync::OnceLock::new();
let bits = BITS.get_or_init(|| {
let types = ghast_data::v1_16_5::entities();
let mut v = vec![
0u8;
types
.iter()
.map(|t| usize::from(t.id) + 1)
.max()
.unwrap_or(0)
];
for t in types {
let mut b = 0;
if phys::collides(t.name) {
b |= COLLIDES;
}
match phys::push_kind(t.name, true) {
Some(phys::PushKind::Boat) => b |= PUSH_BOAT,
Some(phys::PushKind::Living) => b |= PUSH_LIVING,
_ => {}
}
v[usize::from(t.id)] = b;
}
v.into_boxed_slice()
});
match e.entity_type() {
ghast_entity::EntityType::Player => PUSH_LIVING,
ghast_entity::EntityType::Registry(id) => {
let b = bits.get(usize::from(id)).copied().unwrap_or(0);
if e.class() == ghast_entity::EntityClass::Mob {
b
} else {
b & !PUSH_LIVING
}
}
_ => 0,
}
}
fn aabb(b: [f64; 6]) -> ghast_physics::math::Aabb {
ghast_physics::math::Aabb::new(b[0], b[1], b[2], b[3], b[4], b[5])
}
fn near(b: &[f64; 6], p: &ghast_physics::math::Aabb, m: [f64; 3]) -> bool {
!(b[3] < p.min_x - m[0]
|| b[0] > p.max_x + m[0]
|| b[4] < p.min_y - m[1]
|| b[1] > p.max_y + m[1]
|| b[5] < p.min_z - m[2]
|| b[2] > p.max_z + m[2])
}
impl std::fmt::Debug for Sim16 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sim16")
.field("world", &self.world)
.finish_non_exhaustive()
}
}
impl Sim16 {
#[cfg(all(test, feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
pub(crate) fn test_world(&self) -> &ghast_world::v1_16_5::World {
&self.world
}
#[cfg(all(test, feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
pub(crate) fn test_set_block(
&mut self,
x: i32,
y: i32,
z: i32,
st: ghast_data::v1_16_5::BlockState,
) {
self.world.set_block_state(x, y, z, st);
}
pub(crate) fn new() -> Self {
Sim16 {
world: ghast_world::v1_16_5::World::new(config()),
player: ghast_physics::v1_16_5::PlayerState::new([0.0; 3], 0.0, 0.0),
packets: ghast_physics::Packets::new(),
rec: None,
force_step_position: ForceStepPosition::Off,
entities: None,
updates: BlockUpdates::default(),
session: SessionPackets::default(),
block_entities: false,
entity_push: true,
teams: None,
profile_name: "".into(),
around: Around16::default(),
filed_pos: [0.0; 3],
ext: ghast_physics::v1_16_5::PlayerExt::new(),
slow: false,
rt: Riptide16::default(),
unacked: Default::default(),
snapped: false,
glide: Box::default(),
ride: None,
reply: None,
parked: None,
server_flags: None,
absorption: 0.0,
living_flags: None,
absorption_dirty: false,
pistons: ghast_physics::v1_16_5::Pistons::new(),
piston_edits: ghast_physics::v1_16_5::Edits::new(),
piston_uncertain_at: None,
piston_time: 0,
piston_carry: Vec::new(),
piston_moved_shulker: false,
}
}
fn update_slow(&mut self) {
self.slow = self.entities.is_some()
|| self.ride.is_some()
|| !self.ext.is_inert()
|| self.rt.using.is_some()
|| !self.glide.rockets.is_empty();
}
fn elytra_equipped(inv: &ghast_inventory::Inventory) -> bool {
inv.armor(1).is_some_and(|s| {
u16::try_from(s.id())
.ok()
.and_then(ghast_data::v1_16_5::Item::from_id)
.is_some_and(|i| i.info().name == "elytra")
&& s.damage_value(ghast_inventory::Version::V1_16_5) < 432 - 1
})
}
#[cold]
#[inline(never)]
fn tick_rockets(&mut self, before: bool, yaw: f32, pitch: f32) {
let w = &self.world;
let loaded = |p: [f64; 3]| {
w.is_column_loaded((p[0].floor() as i32) >> 4, (p[2].floor() as i32) >> 4)
};
let mut gone = false;
for r in self.glide.rockets.iter_mut().filter(|r| r.before == before) {
if !loaded(r.pos) {
continue;
}
if r.filed {
if r.pos[1] < -64.0 {
r.id = i32::MIN;
gone = true;
}
ghast_physics::v1_16_5::rocket_boost(&mut self.player, &self.ext, yaw, pitch);
r.pos = self.player.pos;
}
r.filed = loaded(r.pos);
}
if gone {
self.glide.rockets.retain(|r| r.id != i32::MIN);
self.update_slow();
}
}
fn order_rockets(&mut self) {
let t = self.entities.as_ref();
for r in &mut self.glide.rockets {
r.before = t.is_some_and(|t| t.ticks_before_self(r.id));
}
}
#[cold]
#[inline(never)]
fn ride_tick(&mut self, input: &ghast_physics::TickInput) {
let Some(r) = boat_mut(&mut self.ride) else {
return;
};
let mut boat_boxes = self.around.boxes.clone();
if let Some(t) = &self.entities {
let me = t.self_id();
for e in t.iter() {
let living = matches!(e.entity_type(), ghast_entity::EntityType::Player)
|| e.class() == ghast_entity::EntityClass::Mob;
if !living || Some(e.id()) == me || t.is_spectator(e) || !t.client_alive(e.id()) {
continue;
}
if let Some(b) = t.client_box(e.id()) {
boat_boxes.push(aabb(b));
}
}
}
let around = ghast_physics::v1_16_5::Surroundings::new()
.with_entities(&self.around.boxes)
.with_others(&self.around.others);
let still = ghast_physics::v1_16_5::tick_boat(
&mut self.player,
&mut self.ext,
&mut r.boat,
&around,
&boat_boxes,
input,
&self.world,
&mut self.packets,
);
if !still {
let id = r.id;
log::debug!("got off boat {id} (sneak)");
let after = self
.entities
.as_ref()
.is_some_and(|t| t.ticks_before_self(id));
let bb = r.boat.bb;
self.park();
if after {
let mut boxes = self.around.boxes.clone();
boxes.push(bb);
let around = ghast_physics::v1_16_5::Surroundings::new()
.with_entities(&boxes)
.with_others(&self.around.others);
let mut ti = *input;
ti.yaw = self.player.yaw;
ti.pitch = self.player.pitch;
ghast_physics::v1_16_5::tick_ext(
&mut self.player,
&mut self.ext,
&around,
&ti,
&self.world,
&mut self.packets,
);
}
}
}
#[cold]
#[inline(never)]
fn cart_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
let Some(c) = cart_mut(&mut self.ride) else {
return Ok(());
};
let m = &mut c.cart;
match raw.id {
cb::id::ENTITY_TELEPORT => {
let p: cb::EntityTeleport = raw.decode_as()?;
if p.entity_id == c.id {
m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch);
}
}
cb::id::REL_ENTITY_MOVE => {
let p: cb::RelEntityMove = raw.decode_as()?;
if p.entity_id == c.id {
m.server_update(Some([p.d_x, p.d_y, p.d_z]), None);
}
}
cb::id::ENTITY_MOVE_LOOK => {
let p: cb::EntityMoveLook = raw.decode_as()?;
if p.entity_id == c.id {
m.server_update(Some([p.d_x, p.d_y, p.d_z]), Some((p.yaw, p.pitch)));
}
}
cb::id::ENTITY_LOOK => {
let p: cb::EntityLook = raw.decode_as()?;
if p.entity_id == c.id {
m.server_update(None, Some((p.yaw, p.pitch)));
}
}
cb::id::ENTITY_DESTROY => {
let p: cb::EntityDestroy = raw.decode_as()?;
if p.entity_ids.contains(&c.id) {
self.ride = None;
self.update_slow();
}
}
_ => {}
}
Ok(())
}
#[cold]
#[inline(never)]
fn cart_tick(&mut self, input: &ghast_physics::TickInput) {
let Some(c) = cart_mut(&mut self.ride) else {
return;
};
let around = ghast_physics::v1_16_5::Surroundings::new()
.with_entities(&self.around.boxes)
.with_others(&self.around.others);
let still = ghast_physics::v1_16_5::tick_minecart(
&mut self.player,
&mut self.ext,
&mut c.cart,
&around,
input,
&self.world,
&mut self.packets,
);
if !still {
let id = c.id;
log::debug!("got off minecart {id} (sneak)");
self.ride = None;
let after = self
.entities
.as_ref()
.is_some_and(|t| t.ticks_before_self(id));
if after {
ghast_physics::v1_16_5::tick_ext(
&mut self.player,
&mut self.ext,
&around,
input,
&self.world,
&mut self.packets,
);
}
}
}
fn park(&mut self) {
if let Some(Ride16::Boat(r)) = self.ride.take().map(|b| *b) {
let b = r.boat.bb;
self.parked = Some(Parked16 {
id: r.id,
pos: r.boat.pos,
bb: [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
tracked: self.entities.as_ref().and_then(|t| t.client_position(r.id)),
});
}
}
#[cold]
#[inline(never)]
fn vehicle_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
if raw.id == cb::id::VEHICLE_MOVE {
if let Some(r) = boat_mut(&mut self.ride) {
let p: cb::VehicleMove = raw.decode_as()?;
if let ghast_physics::Packet::MoveVehicle {
pos,
yaw,
pitch,
on_ground,
} = r.boat.server_move([p.x, p.y, p.z], p.yaw, p.pitch)
{
self.reply = Some(Outbound::VehicleMove {
pos: Vec3::new(pos[0], pos[1], pos[2]),
yaw,
pitch,
on_ground,
});
}
}
return Ok(());
}
let p: cb::SetPassengers = raw.decode_as()?;
let me = self.rt.own_id;
let on = p.passengers.contains(&me);
let riding_it =
matches!(self.ride.as_deref(), Some(Ride16::Boat(r)) if r.id == p.entity_id);
let in_cart = matches!(self.ride.as_deref(), Some(Ride16::Cart(c)) if c.id == p.entity_id);
if on && !riding_it && !in_cart {
let cart = self.entities.as_ref().and_then(|t| {
let e = t.get(p.entity_id)?;
(ghast_entity::v1_16_5::info(e.entity_type())?.name == "minecart").then(|| {
ghast_physics::v1_16_5::Minecart::new(e.position(), e.yaw(), e.pitch())
})
});
if let Some(cart) = cart {
let pl = &mut self.player;
pl.pose = ghast_physics::v1_16_5::Pose::Standing;
let [x, y, z] = pl.pos;
pl.set_position(x, y, z);
log::debug!("in minecart {}", p.entity_id);
self.parked = None;
self.ride = Some(Box::new(Ride16::Cart(Cart16 {
id: p.entity_id,
cart,
})));
self.update_slow();
return Ok(());
}
}
if !on && in_cart {
log::debug!("left minecart {}", p.entity_id);
self.ride = None;
self.update_slow();
return Ok(());
}
if on && !riding_it {
let boat = self.entities.as_ref().and_then(|t| {
let e = t.get(p.entity_id)?;
let is_boat = matches!(e.entity_type(), ghast_entity::EntityType::Registry(6));
is_boat.then(|| {
let pos = t.client_position(p.entity_id).unwrap_or(e.position());
ghast_physics::v1_16_5::Boat::new(pos, e.yaw(), 0.0)
})
});
match boat {
Some(boat) => {
let pl = &mut self.player;
pl.pose = ghast_physics::v1_16_5::Pose::Standing;
let [x, y, z] = pl.pos;
pl.set_position(x, y, z);
log::debug!("riding boat {}", p.entity_id);
self.parked = None;
self.ride = Some(Box::new(Ride16::Boat(BoatRide16 {
id: p.entity_id,
boat,
})));
}
None => log::debug!(
"riding {} (not simulated: no entity tracking or not a boat)",
p.entity_id
),
}
} else if !on && riding_it {
log::debug!("left boat {}", p.entity_id);
self.park();
}
self.update_slow();
Ok(())
}
#[cold]
#[inline(never)]
fn rocket_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
match raw.id {
cb::id::SPAWN_ENTITY => {
let p: cb::SpawnEntity = raw.decode_as()?;
self.drop_rocket(p.entity_id);
if p.r#type == FIREWORK_ROCKET_16 {
if self.glide.pending.len() >= MAX_PENDING_ROCKETS_16 {
self.glide.pending.remove(0);
}
self.glide.pending.push((p.entity_id, [p.x, p.y, p.z]));
}
}
cb::id::ENTITY_DESTROY => {
let p: cb::EntityDestroy = raw.decode_as()?;
for &id in &p.entity_ids {
self.drop_rocket(id);
}
}
_ => {}
}
self.update_slow();
Ok(())
}
fn drop_rocket(&mut self, id: i32) {
self.glide.pending.retain(|r| r.0 != id);
self.glide.rockets.retain(|r| r.id != id);
}
fn new_world(&mut self, dimension: &ghast_protocol::nbt::RawNbt, seed: i64) {
self.ext.ultrawarm = dimension_flag(dimension, "ultrawarm").unwrap_or(false);
self.rt.sky = dimension_flag(dimension, "has_skylight").unwrap_or(true);
self.rt.seed = seed;
self.update_slow();
}
fn rains_at(&self, x: i32, y: i32, z: i32) -> bool {
let raining = self.rt.rain_level.partial_cmp(&0.2) == Some(std::cmp::Ordering::Greater);
if !raining || !self.rt.sky {
return false;
}
let [qx, qy, qz] = voronoi_cell(self.rt.seed, x, y, z);
let id = match self.world.column(qx >> 2, qz >> 2) {
None => Some(1),
Some(c) => match c.biomes() {
ghast_world::Biomes::Cells(b) => {
let i = (qy.clamp(0, 63) << 4 | (qz & 3) << 2 | (qx & 3)) as usize;
Some(b[i])
}
_ => None,
},
};
let Some(id) = id else {
return true;
};
ghast_data::v1_16_5::biome(id)
.is_none_or(|b| b.precipitation == "rain" && b.temperature >= 0.15)
}
fn wet(&self) -> bool {
use ghast_physics::v1_16_5 as phys;
let p = &self.player;
if p.touching_water {
return true;
}
let (bx, bz) = (p.pos[0].floor() as i32, p.pos[2].floor() as i32);
let (feet, head) = (p.pos[1].floor() as i32, p.bb.max_y.floor() as i32);
let mut q = *p;
match (self.rains_at(bx, feet, bz), self.rains_at(bx, head, bz)) {
(false, false) => return false,
(true, true) => {}
(true, false) => q.bb.max_y = q.pos[1],
(false, true) => q.pos[1] = q.bb.max_y,
}
phys::in_water_or_rain(&q, &self.world, true)
}
fn riptide_use(&mut self, input: &ghast_physics::TickInput, out: &[Outbound]) {
let age = self.player.age;
let hands = self
.rt
.held
.hands
.get()
.map(|h| if h.2 == age { (h.0, h.1) } else { (0, false) });
match (self.rt.using, input.using_item) {
(None, true) => {
let hand = if hands[0].1 { hands[0] } else { hands[1] };
self.rt.using = Some(hand.0);
self.rt.use_ticks = 0;
}
(Some(level), false) => {
self.rt.using = None;
let released = out.iter().rev().any(|o| {
matches!(
o,
Outbound::Dig {
status: crate::common::DigStatus::ReleaseUseItem,
..
}
)
});
if released && level > 0 && self.rt.use_ticks >= 10 && self.wet() {
let boxes = self.launch_boxes();
let around = ghast_physics::v1_16_5::Surroundings::new().with_entities(&boxes);
ghast_physics::v1_16_5::riptide(
&mut self.player,
&mut self.ext,
&around,
&self.world,
level,
input.yaw,
input.pitch,
);
}
}
_ => {}
}
}
fn launch_boxes(&self) -> Vec<ghast_physics::math::Aabb> {
let Some(t) = self.entities.as_ref() else {
return Vec::new();
};
let w = &self.world;
t.iter()
.filter(|e| entity_bits(e) & COLLIDES != 0 && t.client_alive(e.id()))
.filter_map(|e| {
let (pos, b) = view(t, self.parked, e.id());
let pos = pos?;
w.is_column_loaded((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4)
.then(|| b.map(aabb))
.flatten()
})
.collect()
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_ext(&self) -> ghast_physics::v1_16_5::PlayerExt {
self.ext
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_watch_glide(&mut self) {
self.glide.watch = true;
}
fn tracker(c: ghast_entity::Config) -> ghast_entity::v1_16_5::Tracker {
let mut t = ghast_entity::v1_16_5::Tracker::new(c);
t.track_client_motion(true);
t
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_set_player(&mut self, p: ghast_physics::v1_16_5::PlayerState) {
self.player = p;
self.piston_uncertain_at = None;
self.filed_pos = p.pos;
if let Some(t) = &mut self.entities {
t.self_filed(p.pos);
}
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_set_profile_name(&mut self, name: &str) {
self.profile_name = name.into();
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_tracker_login(&mut self, own_id: i32) {
use ghast_protocol::v1_16_5::play::clientbound::id;
let Some(t) = &mut self.entities else { return };
let mut b = Vec::new();
b.extend_from_slice(&own_id.to_be_bytes());
b.extend_from_slice(&[0, 0, 0xFF, 1]);
let name = b"minecraft:overworld";
b.push(name.len() as u8);
b.extend_from_slice(name);
b.extend_from_slice(&[10, 0, 0, 0, 10, 0, 0, 0]);
b.push(name.len() as u8);
b.extend_from_slice(name);
b.extend_from_slice(&0i64.to_be_bytes());
b.extend_from_slice(&[20, 10, 0, 1, 0, 0]);
let raw = RawPacket {
id: id::LOGIN,
body: bytes::Bytes::from(b),
};
t.apply(&raw).unwrap();
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_client_box(&self, id: i32) -> Option<([f64; 3], [f64; 6])> {
let t = self.entities.as_ref()?;
let (p, b) = view(t, self.parked, id);
Some((p?, b?))
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_player(&self) -> &ghast_physics::v1_16_5::PlayerState {
&self.player
}
#[cfg(all(
test,
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
pub(crate) fn test_tick_order(&self) -> Vec<i32> {
self.entities
.as_ref()
.map(|t| t.client_tick_order().collect())
.unwrap_or_default()
}
#[inline(never)]
fn tick_tracked(&mut self, pose: &mut Pose) {
if !std::mem::take(&mut self.around.ticked) {
let w = &self.world;
if let Some(t) = &mut self.entities {
t.tick_client(|x, z| w.is_column_loaded(x, z));
}
return;
}
if self.around.after.is_empty() {
return;
}
let p = &mut self.player;
for (_, pusher) in &self.around.after {
ghast_physics::v1_16_5::push_by(p, self.filed_pos, &self.world, pusher);
}
self.around.after.clear();
pose.motion = Vec3::new(p.vel[0], p.vel[1], p.vel[2]);
}
fn entities_around(&mut self) {
let parked = self.parked;
let vehicle = self.ride.as_ref().map(|r| r.id());
let a = &mut self.around;
a.boxes.clear();
a.after.clear();
a.before.clear();
let Some(t) = self.entities.as_mut() else {
return;
};
let w = &self.world;
let loaded = |x: i32, z: i32| w.is_column_loaded(x, z);
let p = &self.player;
let reach = |v: f64| v.abs() + 3.0;
let mut m = [reach(p.vel[0]), reach(p.vel[1]), reach(p.vel[2])];
let mut at = p.bb;
let vehicle_box = match self.ride.as_deref() {
Some(Ride16::Boat(r)) => Some((r.boat.bb, r.boat.vel)),
Some(Ride16::Cart(c)) => Some((c.cart.bb, [0.0; 3])),
None => None,
};
if let Some((b, v)) = vehicle_box {
let b = &b;
at = ghast_physics::math::Aabb {
min_x: at.min_x.min(b.min_x),
min_y: at.min_y.min(b.min_y),
min_z: at.min_z.min(b.min_z),
max_x: at.max_x.max(b.max_x),
max_y: at.max_y.max(b.max_y),
max_z: at.max_z.max(b.max_z),
};
for i in 0..3 {
m[i] = m[i].max(reach(v[i]));
}
}
let mut any = false;
for e in t.iter() {
let bits = entity_bits(e);
if bits == 0 {
continue;
}
any = true;
if bits & COLLIDES == 0 || t.ticks_before_self(e.id()) || vehicle == Some(e.id()) {
continue;
}
let (vp, vb) = view(t, parked, e.id());
if let Some(b) = vb
&& near(&b, &at, m)
&& t.client_alive(e.id())
&& vp
.is_some_and(|q| loaded((q[0].floor() as i32) >> 4, (q[2].floor() as i32) >> 4))
{
a.boxes.push(aabb(b));
}
}
t.self_filed(self.filed_pos);
let mut shulkers: Vec<(i32, [f64; 3], [f64; 6])> = Vec::new();
if std::mem::take(&mut self.piston_moved_shulker) {
for e in t.iter() {
if type_name(e) == Some("shulker")
&& let (Some(p), Some(b)) = view(t, parked, e.id())
{
shulkers.push((e.id(), p, b));
}
}
}
t.tick_client(loaded);
a.ticked = true;
if !any {
return;
}
if let Some(me) = t.self_id()
&& t.iter()
.any(|e| e.passengers().contains(&me) && parked.is_none_or(|k| k.id != e.id()))
{
let Some(id) = vehicle else {
a.boxes.clear();
return;
};
let skip = t.client_box(id).map(aabb);
a.boxes.retain(|b| Some(*b) != skip);
}
let own: &str = &self.profile_name;
let teams = self.teams.as_deref().filter(|t| t.any_rule());
for e in t.iter() {
let bits = entity_bits(e);
if bits == 0 {
continue;
}
let id = e.id();
let before = t.ticks_before_self(id);
let (Some(pos), Some(b)) = view(t, parked, id) else {
continue;
};
if vehicle == Some(id) {
continue;
}
if bits & COLLIDES != 0
&& before
&& near(&b, &at, m)
&& t.client_alive(id)
&& loaded((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4)
{
a.boxes.push(aabb(b));
}
if !self.entity_push || bits & (PUSH_LIVING | PUSH_BOAT) == 0 {
continue;
}
let (pos, b) = match shulkers.iter().find(|s| s.0 == id) {
Some(s) if before => (s.1, s.2),
_ => (pos, b),
};
let (margin, from) = if before { ([0.25; 3], &p.bb) } else { (m, &at) };
if !near(&b, from, margin) || !t.client_ticks(e, loaded) {
continue;
}
if t.is_spectator(e) {
continue;
}
if let Some(teams) = teams {
let mut buf = [0u8; 36];
let name = match t.scoreboard_name(e) {
Some(n) => n,
None => e.uuid().hyphenated().encode_lower(&mut buf),
};
if !teams.may_push(name, own) {
continue;
}
}
let kind = if bits & PUSH_BOAT != 0 {
ghast_physics::v1_16_5::PushKind::Boat
} else {
ghast_physics::v1_16_5::PushKind::Living
};
let rank = t.client_tick_rank(id).unwrap_or(u32::MAX);
let pusher = ghast_physics::v1_16_5::Pusher::new(pos, aabb(b), kind);
if before {
a.before.push((rank, pusher));
} else {
a.after.push((rank, pusher));
}
}
a.before.sort_by_key(|x| x.0);
a.after.sort_by_key(|x| x.0);
let p = &mut self.player;
for (_, pusher) in &a.before {
ghast_physics::v1_16_5::push_by(p, self.filed_pos, &self.world, pusher);
}
}
fn own_metadata(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::defs::p754::types::EntityMetadataValue as V;
use ghast_protocol::v1_16_5::play::clientbound as cb;
let mut r = ghast_protocol::Reader::new(&raw.body);
let id = r.read_varint()?;
if id != self.rt.own_id {
if self.glide.pending.iter().any(|r| r.0 == id) {
self.rocket_metadata(raw, id)?;
}
return Ok(());
}
let p: cb::EntityMetadata = raw.decode_as()?;
for e in &p.metadata {
match (e.key, &e.value) {
(7, V::Byte(b)) => {
self.ext.apply_living_flags(*b as u8);
self.living_flags = Some(*b as u8);
}
(14, V::Float(a)) => {
self.absorption = *a;
self.absorption_dirty = true;
}
(0, V::Byte(b)) => {
self.server_flags = Some(*b as u8);
self.ext.apply_shared_flags(&mut self.player, *b as u8);
if let Some(r) = &mut self.rec {
r.note("own entity flags not representable");
}
}
_ => {}
}
}
self.update_slow();
Ok(())
}
#[cold]
#[inline(never)]
fn rocket_metadata(&mut self, raw: &RawPacket, id: i32) -> Result<(), SimError> {
use ghast_protocol::defs::p754::types::EntityMetadataValue as V;
use ghast_protocol::v1_16_5::play::clientbound as cb;
let p: cb::EntityMetadata = raw.decode_as()?;
let shooter = p.metadata.iter().find_map(|e| match (e.key, &e.value) {
(8, V::OptVarInt(v)) => Some(*v),
_ => None,
});
let Some(v) = shooter else { return Ok(()) };
let Some(k) = self.glide.pending.iter().position(|r| r.0 == id) else {
return Ok(());
};
let (rid, pos) = self.glide.pending.remove(k);
if v != 0 && v - 1 == self.rt.own_id {
let w = &self.world;
let filed =
w.is_column_loaded((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4);
self.glide.rockets.push(Rocket16 {
id: rid,
pos,
filed,
before: false,
});
log::debug!("firework rocket {rid} attached to us");
}
self.update_slow();
Ok(())
}
fn spin_contacts(&mut self, before: bool) {
let Some(t) = self.entities.as_ref() else {
return;
};
let w = &self.world;
for e in t.iter() {
let id = e.id();
if t.ticks_before_self(id) != before || t.is_spectator(e) {
continue;
}
let (Some(pos), Some(b)) = view(t, self.parked, id) else {
continue;
};
if !w.is_column_loaded((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4) {
continue;
}
let living = matches!(e.entity_type(), ghast_entity::EntityType::Player)
|| e.class() == ghast_entity::EntityClass::Mob;
self.around
.others
.push(ghast_physics::v1_16_5::OtherEntity::new(aabb(b), living));
}
}
#[inline(never)]
fn slow_tick(&mut self, input: &ghast_physics::TickInput, out: &[Outbound]) {
if self.rt.using.is_some() || input.using_item {
self.riptide_use(input, out);
}
self.around.others.clear();
let spinning = self.ext.riptide_ticks > 0;
if spinning {
self.spin_contacts(false);
}
if self.entities.is_some() {
self.entities_around();
}
if spinning {
self.spin_contacts(true);
}
if !self.glide.rockets.is_empty() {
self.order_rockets();
self.tick_rockets(true, input.yaw, input.pitch);
}
if let Some(r) = self.ride.as_deref() {
if matches!(r, Ride16::Boat(_)) {
self.ride_tick(input);
} else {
self.cart_tick(input);
}
if !self.pistons.is_empty() {
self.tick_piston_blocks();
}
self.update_slow();
return;
}
let around = ghast_physics::v1_16_5::Surroundings::new()
.with_entities(&self.around.boxes)
.with_others(&self.around.others);
if !self.pistons.is_empty() {
if let Some(r) = &mut self.rec {
r.note_once("moving pistons not representable");
}
ghast_physics::v1_16_5::tick_player_among_pistons(
&mut self.player,
&mut self.ext,
&around,
input,
&self.world,
&self.pistons,
&mut self.packets,
);
} else if self.ext.is_inert() {
ghast_physics::v1_16_5::tick_in(
&mut self.player,
&around,
input,
&self.world,
&mut self.packets,
);
} else {
ghast_physics::v1_16_5::tick_ext(
&mut self.player,
&mut self.ext,
&around,
input,
&self.world,
&mut self.packets,
);
}
if self.rt.using.is_some() {
self.rt.use_ticks += 1;
}
self.update_slow();
}
fn block_action(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
let p: cb::BlockAction = raw.decode_as()?;
self.piston_edits.clear();
self.pistons.block_event(
&self.world,
[p.location.x, p.location.y, p.location.z],
p.byte1,
p.byte2,
&mut self.piston_edits,
);
if self.pistons.take_unmodelled() {
self.piston_uncertain_at = Some(self.player.age);
}
self.apply_piston_edits();
Ok(())
}
#[cold]
#[inline(never)]
fn tick_piston_phase(&mut self, pose: &mut Pose) {
use ghast_physics::v1_16_5 as phys;
type Other = (
(i32, i32, i32, u32),
i32,
bool,
ghast_physics::v1_16_5::PistonEntity,
);
let mut others: Vec<Other> = Vec::new();
let mut player_order = None;
let mut unmodelled = false;
if let Some(t) = self.entities.as_ref() {
let parked = self.parked;
let me = t.self_id();
player_order = me.and_then(|id| t.search_order(id));
for e in t.iter() {
let id = e.id();
if Some(id) == me {
continue;
}
let Some(order) = t.search_order(id) else {
continue;
};
let (Some(pos), Some(b)) = view(t, parked, id) else {
continue;
};
let bb = aabb(b);
if parked.is_some_and(|k| k.id == id) {
unmodelled |= self.pistons.touches(&bb);
continue;
}
let Some(name) = type_name(e) else {
continue;
};
let player = e.entity_type() == ghast_entity::EntityType::Player;
let living =
player || e.class() == ghast_entity::EntityClass::Mob || name == "armor_stand";
let alive = t.client_alive(id);
let spectator = t.is_spectator(e);
let marker =
name == "armor_stand" && e.metadata().byte(14).is_some_and(|f| f & 0x10 != 0);
let ridden_horse = (name.ends_with("horse")
|| matches!(name, "donkey" | "mule" | "llama" | "trader_llama"))
&& !e.passengers().is_empty();
let pushable = alive
&& !spectator
&& !ridden_horse
&& phys::pushable(name, living)
&& !(living && phys::climbing_at(&self.world, pos));
let carry = self
.piston_carry
.iter()
.find(|c| c.0 == id)
.map_or_else(Default::default, |c| c.1);
let pe = phys::PistonEntity {
bb,
pos,
on_ground: t.client_on_ground(id).unwrap_or(false),
step_height: phys::step_height(name, living),
moved: !spectator && !marker,
collidable: alive && phys::collides(name),
pushable,
hits_pushable: phys::hits_pushable(name),
living,
section: [order.0, order.2, order.1],
carry,
};
others.push((order, id, name == "shulker", pe));
}
}
others.sort_by_key(|c| c.0);
let rank = player_order.map_or(others.len(), |o| others.iter().filter(|c| c.0 < o).count());
let mut list: Vec<phys::PistonEntity> = others.iter().map(|c| c.3).collect();
let around = phys::Surroundings::new();
self.piston_edits.clear();
self.pistons.tick_after_entities_among(
&mut self.player,
&mut self.ext,
&around,
&mut list,
phys::PlayerPlace {
rank,
section: {
let f = |v: f64| ghast_physics::math::floor(v) >> 4;
let p = self.filed_pos;
[f(p[0]), f(p[1]).clamp(0, 15), f(p[2])]
},
},
&self.world,
self.piston_time,
&mut self.piston_edits,
);
if let Some(t) = self.entities.as_mut() {
for ((_, id, shulker, before), after) in others.iter().zip(&list) {
if after.carry != before.carry {
match self.piston_carry.iter_mut().find(|c| c.0 == *id) {
Some(c) => c.1 = after.carry,
None => self.piston_carry.push((*id, after.carry)),
}
}
if after.bb != before.bb
|| after.pos != before.pos
|| after.on_ground != before.on_ground
{
self.piston_moved_shulker |= *shulker && after.bb != before.bb;
let b = after.bb;
t.client_pushed(
*id,
after.pos,
[b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
after.on_ground,
);
}
}
if self.piston_carry.len() > 64 {
self.piston_carry.retain(|c| t.get(c.0).is_some());
}
}
if unmodelled | self.pistons.take_unmodelled() {
self.piston_uncertain_at = Some(self.player.age);
}
self.apply_piston_edits();
sync_pose_16(pose, &self.player);
}
#[cold]
#[inline(never)]
fn tick_piston_blocks(&mut self) {
self.piston_edits.clear();
self.pistons
.tick_blocks(&self.world, self.piston_time, &mut self.piston_edits);
if self.pistons.take_unmodelled() {
self.piston_uncertain_at = Some(self.player.age);
}
self.apply_piston_edits();
}
fn apply_piston_edits(&mut self) {
for &([x, y, z], s) in &self.piston_edits {
if self.world.set_block_state(x, y, z, s) {
self.updates.push(BlockPos::new(x, y, z), u32::from(s.id()));
}
}
}
#[inline]
fn prune_pistons(&mut self) {
if !self.pistons.is_empty() {
self.prune_pistons_slow();
}
}
#[cold]
#[inline(never)]
fn prune_pistons_slow(&mut self) {
let w = &self.world;
self.pistons.retain(|m| {
w.block_state(m.pos[0], m.pos[1], m.pos[2])
.is_some_and(|s| s.block() == ghast_data::v1_16_5::Block::MOVING_PISTON)
});
}
pub(crate) fn with(opts: &SimOptions) -> Self {
let mut s = Self::new();
s.force_step_position = opts.force_step_position;
s.entities = opts.entities.map(Self::tracker);
s.teams = s.entities.is_some().then(Box::default);
if let Some(n) = &opts.profile_name {
s.profile_name = n.as_str().into();
}
if let Some(c) = opts.world {
s.world = ghast_world::v1_16_5::World::new(c);
s.block_entities = c.block_entities;
}
if opts.shared_chunks.is_some() {
log::debug!("shared chunks are not used on 1.16.5; every section stays owned");
}
s.update_slow();
if let Some(p) = opts.record.as_deref() {
match crate::record::Recorder::create(p) {
Ok(r) => s.rec = Some(r),
Err(e) => log::warn!("cannot record to {}: {e}", p.display()),
}
}
s
}
}
impl WorldView for Sim16 {
fn collisions(&self) -> (bool, bool) {
(
self.player.horizontal_collision,
self.player.vertical_collision,
)
}
fn piston_uncertain(&self) -> bool {
self.piston_uncertain_at == Some(self.player.age)
}
fn column(&self, chunk_x: i32, chunk_z: i32) -> Option<&ghast_world::Column> {
self.world.column(chunk_x, chunk_z)
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
self.world.is_column_loaded(chunk_x, chunk_z)
}
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
self.world.block_state(x, y, z).map(|s| u32::from(s.id()))
}
fn entities(&self) -> Option<&ghast_entity::Entities> {
self.entities.as_deref()
}
fn loaded_columns(&self) -> usize {
self.world.columns().len()
}
fn block_query(&self, x: i32, y: i32, z: i32) -> Option<BlockQuery> {
let s = self.world.block_state(x, y, z)?;
let info = s.block().info();
let air = s.block().is_air();
let passable = matches!(
info.name,
"water" | "lava" | "fire" | "nether_portal" | "end_portal"
);
Some(BlockQuery {
exact: Some(crate::hands::ExactDig::V1_16_5),
air,
water: info.name == "water",
block: s.block().id(),
hardness: if info.diggable {
info.hardness.unwrap_or(-1.0)
} else {
-1.0
},
dig_material: info.material,
harvest_tools: info.harvest_tools,
selection: if air || passable {
None
} else {
Some(union(s.shape()).unwrap_or([0.0, 0.0, 0.0, 1.0, 1.0, 1.0]))
},
stairs: None,
activates: activates_1_16(info.name),
})
}
fn crosshair(
&self,
eyes: Vec3,
(yaw, pitch): (f32, f32),
_look: Vec3,
reach: f64,
) -> Option<crate::BlockHit> {
use ghast_physics::pick::v1_16_5 as pick;
let eye = [eyes.x, eyes.y, eyes.z];
let hit = pick::pick(
&self.world,
eye,
pick::rotation_vector(yaw, pitch),
reach,
None,
);
block_hit(&hit, eye)
}
}
impl Sim for Sim16 {
fn crosshair_target(&self, q: &PickQuery<'_>) -> Crosshair {
use ghast_physics::pick::v1_16_5 as pick;
let held_block = q
.held
.and_then(ghast_data::v1_16_5::Item::from_id)
.and_then(|i| ghast_data::v1_16_5::Block::from_name(i.info().name));
let eye = if q.now {
[
q.eyes.x,
self.player.pos[1] + f64::from(self.player.eye_height()),
q.eyes.z,
]
} else {
[q.eyes.x, q.eyes.y, q.eyes.z]
};
let look = pick::rotation_vector(q.rotation.0, q.rotation.1);
if q.entities.is_empty() {
let reach = if q.creative {
pick::CREATIVE_REACH
} else {
pick::REACH
};
let hit = pick::pick(&self.world, eye, look, reach, held_block);
return Crosshair::Block(block_hit(&hit, eye));
}
let t = pick::pick_target(
&self.world,
&q.player_bb,
eye,
look,
q.creative,
held_block,
q.entities,
);
modern_crosshair(t, eye)
}
fn crosshair_now(
&self,
eyes: Vec3,
(yaw, pitch): (f32, f32),
_look: Vec3,
held: Option<u16>,
) -> Option<crate::BlockHit> {
use ghast_physics::pick::v1_16_5 as pick;
let held_block = held
.and_then(ghast_data::v1_16_5::Item::from_id)
.and_then(|i| ghast_data::v1_16_5::Block::from_name(i.info().name));
let eye = [eyes.x, eyes.y, eyes.z];
let hit = pick::pick(
&self.world,
eye,
pick::rotation_vector(yaw, pitch),
crate::hands::REACH,
held_block,
);
block_hit(&hit, eye)
}
fn world_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
{
use ghast_protocol::v1_16_5::play::clientbound::id;
self.session.note(raw, id::LOGIN, id::RESPAWN);
}
if let Some(t) = &mut self.entities
&& t.wants(raw.id)
{
t.apply(raw)?;
}
use ghast_protocol::v1_16_5::play::clientbound as cb;
if raw.id == cb::id::TEAMS {
if let Some(t) = &mut self.teams
&& !t.apply(&raw.body, super::teams::TeamLayout::Legacy1_16)
{
log::debug!("bad teams packet ({} B)", raw.body.len());
}
return Ok(());
}
if raw.id == cb::id::ENTITY_METADATA {
return self.own_metadata(raw);
}
if matches!(raw.id, cb::id::SET_PASSENGERS | cb::id::VEHICLE_MOVE) {
return self.vehicle_packet(raw);
}
if matches!(self.ride.as_deref(), Some(Ride16::Cart(_)))
&& matches!(
raw.id,
cb::id::ENTITY_TELEPORT
| cb::id::REL_ENTITY_MOVE
| cb::id::ENTITY_MOVE_LOOK
| cb::id::ENTITY_LOOK
| cb::id::ENTITY_DESTROY
)
{
self.cart_packet(raw)?;
}
if matches!(raw.id, cb::id::SPAWN_ENTITY | cb::id::ENTITY_DESTROY) {
if self.glide.watch {
self.rocket_packet(raw)?;
}
return Ok(());
}
let w = &mut self.world;
match raw.id {
cb::id::UPDATE_TIME => {
let p: cb::UpdateTime = raw.decode_as()?;
self.piston_time = p.age;
}
cb::id::LOGIN => {
let p: cb::Login = raw.decode_as()?;
self.pistons.clear();
self.piston_carry.clear();
self.piston_time = 0;
w.apply_login(&p);
self.rt.own_id = p.entity_id;
self.rt.rain_level = 0.0;
self.new_world(&p.dimension, p.hashed_seed);
if let Some(t) = &mut self.teams {
t.clear();
}
self.unacked.clear();
}
cb::id::RESPAWN => {
let p: cb::Respawn = raw.decode_as()?;
let new = *p.world_name != *w.world_name();
self.pistons.clear();
self.piston_carry.clear();
w.apply_respawn(&p);
if new {
self.rt.rain_level = 0.0;
self.new_world(&p.dimension, p.hashed_seed);
}
}
cb::id::MAP_CHUNK => {
let p: cb::MapChunk = raw.decode_as()?;
if p.ground_up && !self.pistons.is_empty() {
self.pistons.chunk_replaced(p.x, p.z);
}
w.apply_map_chunk(&p)?;
}
cb::id::UNLOAD_CHUNK => {
let p: cb::UnloadChunk = raw.decode_as()?;
if !self.pistons.is_empty() {
self.pistons.chunk_replaced(p.chunk_x, p.chunk_z);
}
w.apply_unload_chunk(&p);
}
cb::id::UPDATE_VIEW_POSITION => {
w.apply_view_position(&raw.decode_as()?);
}
cb::id::UPDATE_VIEW_DISTANCE => {
w.apply_view_distance(&raw.decode_as()?);
}
cb::id::BLOCK_CHANGE => {
let p: cb::BlockChange = raw.decode_as()?;
self.updates.push(p.location, p.r#type as u32);
w.apply_block_change(&p);
self.prune_pistons();
}
cb::id::BLOCK_ACTION => return self.block_action(raw),
cb::id::MULTI_BLOCK_CHANGE => {
let p: cb::MultiBlockChange = raw.decode_as()?;
let c = &p.chunk_coordinates;
let sy = ((c.y << 12) as i32) >> 12;
for &r in &p.records {
let local = (r & 0xFFF) as i32;
let pos = BlockPos::new(
c.x * 16 + ((local >> 8) & 15),
sy.wrapping_mul(16).wrapping_add(local & 15),
c.z * 16 + ((local >> 4) & 15),
);
self.updates.push(pos, ((r as u64) >> 12) as u32);
}
w.apply_multi_block_change(&p);
self.prune_pistons();
}
cb::id::EXPLOSION => {
w.apply_explosion(&raw.decode_as()?);
self.prune_pistons();
}
cb::id::ACKNOWLEDGE_PLAYER_DIGGING => {
self.action_answer(&raw.decode_as()?);
}
cb::id::WORLD_BORDER => {
w.apply_world_border(&raw.decode_as()?);
}
cb::id::TILE_ENTITY_DATA if self.block_entities => {
w.apply_tile_entity_data(&raw.decode_as()?);
}
_ => {}
}
Ok(())
}
fn reset_player(&mut self) {
let abilities = self.player.abilities;
let food = self.player.food_level;
self.player = ghast_physics::v1_16_5::PlayerState::new([0.0; 3], 0.0, 0.0);
self.piston_uncertain_at = None;
self.pistons.reset_player();
self.filed_pos = [0.0; 3];
self.player.abilities = abilities;
self.player.food_level = food;
self.ext.spin_attack = false;
self.ext.riptide_ticks = 0;
self.ext.fall_flying = false;
self.ext.glide_pose = false;
self.glide.rockets.clear();
self.glide.pending.clear();
self.ride = None;
self.rt.using = None;
self.rt.held.hands.set([(0, false, -1); 2]);
self.server_flags = None;
self.absorption_dirty |= self.absorption != 0.0;
self.absorption = 0.0;
self.update_slow();
}
fn teleport(&mut self, pose: &mut Pose, t: &Teleport) {
self.player.yaw = pose.yaw;
self.player.pitch = pose.pitch;
let _reply =
self.player
.apply_teleport([t.x, t.y, t.z], t.yaw, t.pitch, rel_flags(t.flags));
if let Some(r) = &mut self.rec {
let p = &self.player;
r.teleport(
[t.x, t.y, t.z],
t.yaw,
t.pitch,
t.flags,
t.teleport_id,
p.pos,
p.yaw,
p.pitch,
);
}
sync_pose_16(pose, &self.player);
}
fn abilities(&mut self, a: Abilities) {
let a = physics_abilities(a);
if let Some(r) = &mut self.rec {
r.abilities(&a);
}
self.player.apply_abilities(a);
}
fn food(&mut self, food: i32) {
self.player.food_level = food;
}
fn velocity(&mut self, v: Vec3, add: bool) {
if let Some(r) = &mut self.rec {
if add {
r.note("explosion push not representable");
} else {
r.velocity([
(v.x * 8000.0).round() as i32,
(v.y * 8000.0).round() as i32,
(v.z * 8000.0).round() as i32,
]);
}
}
let p = &mut self.player;
if add {
p.vel[0] += v.x;
p.vel[1] += v.y;
p.vel[2] += v.z;
} else {
p.vel = [v.x, v.y, v.z];
}
}
fn movement_speed(&mut self, base: f64, modifiers: &[(u128, f64, u8)]) {
if let Some(r) = &mut self.rec {
r.speed_attribute(base, modifiers);
}
self.player.apply_speed_attribute(base, modifiers);
}
fn status_effects(&mut self, effects: &std::collections::BTreeMap<i8, i8>) {
if let Some(r) = &mut self.rec {
r.effects(effects);
}
let e = &mut self.player.effects;
e.jump_boost = effects.get(&8).map(|a| i32::from(*a));
e.blindness = effects.contains_key(&15);
e.levitation = effects.get(&25).map(|a| i32::from(*a));
e.slow_falling = effects.contains_key(&28);
e.dolphins_grace = effects.contains_key(&30);
}
fn boots(&mut self, depth_strider: u8, soul_speed: u8) {
self.player.enchantments.depth_strider = depth_strider;
self.player.enchantments.soul_speed = soul_speed;
}
fn spectator(&mut self, on: bool) {
self.player.spectator = on;
}
fn set_flying(&mut self, flying: bool, out: &mut Vec<Outbound>) {
let a = &mut self.player.abilities;
if a.flying == flying || (flying && !a.allow_flying) {
return;
}
a.flying = flying;
let a = *a;
if let Some(r) = &mut self.rec {
r.abilities(&a);
}
out.push(Outbound::Abilities {
flags: abilities_flags(&a),
flying_speed: a.fly_speed,
walking_speed: a.walk_speed,
});
}
fn collect_block_updates(&mut self, on: bool) {
self.updates.on = on;
if !on {
self.updates.list.clear();
}
}
fn drain_block_updates(&mut self, out: &mut Vec<(BlockPos, u32)>) -> usize {
out.append(&mut self.updates.list);
std::mem::take(&mut self.updates.dropped)
}
#[cfg(all(test, ghast_modern, feature = "v1_8", feature = "v1_16_5"))]
fn test_set_own_id(&mut self, id: i32) {
self.rt.own_id = id;
}
fn eye_height(&self) -> Option<f64> {
Some(f64::from(self.player.eye_height()))
}
fn attack_hurts(&self, e: &ghast_entity::Entity) -> bool {
if e.is_player() {
return true;
}
ghast_entity::v1_16_5::info(e.entity_type()).is_some_and(|i| {
matches!(
i.name,
"boat" | "end_crystal" | "fireball" | "shulker_bullet"
) || i.name.ends_with("minecart")
})
}
fn attack_slow(&mut self, _target: i32, _yaw: f32, knockback: i32, sprint_bonus: bool) {
let p = &mut self.player;
if super::attack_knockback_applies(knockback, p.sprinting, sprint_bonus) {
p.vel[0] *= 0.6;
p.vel[2] *= 0.6;
p.set_sprinting(false);
if let Some(r) = &mut self.rec {
r.note("attack slow-down not representable");
}
}
}
fn server_flags(&self) -> Option<u8> {
self.server_flags
}
fn search_order(&self, e: &ghast_entity::Entity) -> Option<(i32, i32, i32, u32)> {
self.entities.as_ref()?.search_order(e.id())
}
#[inline]
fn take_living_flags(&mut self) -> Option<u8> {
self.living_flags.take()
}
#[inline]
fn take_absorption(&mut self) -> Option<f32> {
std::mem::take(&mut self.absorption_dirty).then_some(self.absorption)
}
fn velocity_now(&self) -> Vec3 {
let v = self.player.vel;
Vec3::new(v[0], v[1], v[2])
}
fn world_packet_ids(&self) -> Vec<i32> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
let mut ids: Vec<i32> = self.world.packet_ids().collect();
ids.push(cb::id::ACKNOWLEDGE_PLAYER_DIGGING);
ids.push(cb::id::ENTITY_METADATA);
ids.extend([cb::id::SET_PASSENGERS, cb::id::VEHICLE_MOVE]);
ids.push(cb::id::BLOCK_ACTION);
if self.glide.watch {
ids.extend([cb::id::SPAWN_ENTITY, cb::id::ENTITY_DESTROY]);
}
ids
}
fn take_filter_dirty(&mut self) -> bool {
self.rt.held.filter_dirty.replace(false)
}
#[inline(never)]
fn weather(&mut self, reason: u8, value: f32) {
self.rt.rain_level = match reason {
1 => 0.0,
2 => 1.0,
_ => value.clamp(0.0, 1.0),
};
}
#[inline(never)]
fn item_use_facts(
&self,
stack: &ghast_inventory::Stack,
off_hand: bool,
mut held: HeldItem,
) -> HeldItem {
let trident = u16::try_from(stack.id())
.ok()
.and_then(ghast_data::v1_16_5::Item::from_id)
.is_some_and(|i| i.info().name == "trident");
let mut level = 0;
if trident {
let (lvl, damage) = trident_nbt(stack);
level = lvl;
held.usable = held.usable && damage < 250 - 1 && (lvl == 0 || self.wet());
if lvl > 0 && !self.rt.held.watch.replace(true) {
self.rt.held.filter_dirty.set(true);
}
}
held.swing_on_use = held.swing_on_use
|| (self.ext.fall_flying
&& u16::try_from(stack.id())
.ok()
.and_then(ghast_data::v1_16_5::Item::from_id)
.is_some_and(|i| i.info().name == "firework_rocket"));
let starts =
held.usable && (!held.food || held.always_edible || self.player.food_level < 20);
let mut h = self.rt.held.hands.get();
h[usize::from(off_hand)] = (level, starts, self.player.age);
self.rt.held.hands.set(h);
held
}
fn entity_config(&self) -> Option<ghast_entity::Config> {
self.entities.as_ref().map(|t| t.config())
}
fn set_entities(&mut self, config: Option<ghast_entity::Config>) {
if self.entity_config() == config {
return;
}
self.teams = config.is_some().then(Box::default);
self.entities = config.map(|c| {
let mut t = Self::tracker(c);
self.session.replay(|r| {
if let Err(e) = t.apply(r) {
log::debug!("entity tracker: session packet 0x{:02x}: {e}", r.id);
}
});
t
});
self.update_slow();
}
fn entity_packet_ids(&self) -> Vec<i32> {
use ghast_protocol::v1_16_5::play::clientbound as cb;
self.entities
.as_ref()
.map(|t| t.packet_ids().chain([cb::id::TEAMS]).collect())
.unwrap_or_default()
}
#[inline(always)]
fn tick_entities(&mut self, pose: &mut Pose) {
if let Some(t) = self.entities.as_mut() {
t.self_filed(self.filed_pos);
self.tick_tracked(pose);
}
if self.slow && !self.glide.rockets.is_empty() {
let (yaw, pitch) = (self.player.yaw, self.player.pitch);
self.tick_rockets(false, yaw, pitch);
let p = &self.player;
pose.motion = Vec3::new(p.vel[0], p.vel[1], p.vel[2]);
}
if !self.pistons.is_empty() && self.ride.is_none() {
self.tick_piston_phase(pose);
}
self.piston_time = self.piston_time.wrapping_add(1);
}
#[inline(always)]
fn modern_inputs(&self) -> bool {
!self.player.on_ground
}
fn pre_tick(&mut self, inv: &ghast_inventory::Inventory, t: &crate::sim::PreTick) -> bool {
if t.controls.jump {
let e = Self::elytra_equipped(inv);
if e != self.ext.elytra {
self.ext.elytra = e;
self.update_slow();
}
if e && !self.glide.watch {
self.glide.watch = true;
self.rt.held.filter_dirty.set(true);
}
}
true
}
fn gliding(&self) -> bool {
self.ext.fall_flying
}
fn vehicle(&self) -> Option<crate::common::Vehicle> {
if let Some(Ride16::Cart(c)) = self.ride.as_deref() {
let m = &c.cart;
return Some(crate::common::Vehicle {
entity_id: c.id,
pos: Vec3::new(m.pos[0], m.pos[1], m.pos[2]),
yaw: m.yaw,
pitch: m.pitch,
on_ground: false,
controlled: false,
});
}
let Some(Ride16::Boat(r)) = self.ride.as_deref() else {
return None;
};
let b = &r.boat;
Some(crate::common::Vehicle {
entity_id: r.id,
pos: Vec3::new(b.pos[0], b.pos[1], b.pos[2]),
yaw: b.yaw,
pitch: b.pitch,
on_ground: b.on_ground,
controlled: true,
})
}
fn mount_rules(&self, entity_id: i32) -> Option<ghast_inventory::MountRules> {
use ghast_inventory::{MountArmor, MountRules};
let t = self.entities.as_ref()?;
let e = t.get(entity_id)?;
let armor = match ghast_entity::v1_16_5::info(e.entity_type())?.name {
"llama" | "trader_llama" => return Some(MountRules::new(false, MountArmor::Carpet)),
"horse" => MountArmor::HorseArmor,
"donkey" | "mule" | "zombie_horse" | "skeleton_horse" => MountArmor::None,
_ => return None,
};
if !t.config().metadata {
return None;
}
let m = e.metadata();
let alive = ghast_entity::v1_16_5::health(e).is_none_or(|h| h > 0.0);
let baby = matches!(m.get(15), Some(ghast_entity::MetaValue::Bool(true)));
let tame = m.byte(16).unwrap_or(0) & 2 != 0;
Some(MountRules::new(alive && !baby && tame, armor))
}
fn take_reply(&mut self) -> Option<Outbound> {
self.reply.take()
}
fn entities_mut(&mut self) -> Option<&mut ghast_entity::Entities> {
self.entities.as_mut().map(|t| t.store_mut())
}
fn set_entity_push(&mut self, on: bool) {
self.entity_push = on;
}
fn predict_air(&mut self, x: i32, y: i32, z: i32) {
self.world
.set_block_state(x, y, z, ghast_data::v1_16_5::BlockState::AIR);
}
fn dig_sent(
&mut self,
pos: BlockPos,
status: crate::common::DigStatus,
broken: bool,
_sequence: i32,
player: Vec3,
) {
use crate::common::DigStatus;
if matches!(
status,
DigStatus::Start | DigStatus::Abort | DigStatus::Finish
) {
self.unacked
.put(pos, status.id() as u8, [player.x, player.y, player.z]);
}
if broken {
use ghast_data::FluidKind;
use ghast_data::v1_16_5::{Block, BlockState};
let Some(old) = self.world.block_state(pos.x, pos.y, pos.z) else {
return;
};
let left = match old.fluid().map(|f| f.kind) {
Some(FluidKind::Water) => Block::WATER.default_state(),
Some(FluidKind::Lava) => Block::LAVA.default_state(),
None => BlockState::AIR,
};
log::debug!(
"predicted the break of {} at {pos:?}: {}",
old.block().name(),
left.block().name()
);
self.world.set_block_state(pos.x, pos.y, pos.z, left);
}
}
fn place_rules(
&self,
q: &crate::sim::predict::PlaceQuery<'_>,
) -> crate::sim::predict::PlaceRules {
crate::sim::place116::rules(&self.world, &self.player.bb, q)
}
fn air_use(&self, q: &crate::sim::predict::AirQuery<'_>) -> crate::sim::predict::AirAnswer {
let p = &self.player;
crate::sim::use116::air_use(
&crate::sim::use116::Ctx {
world: &self.world,
bb: p.bb,
climbing: !p.spectator && crate::sim::use116::climbing(&self.world, p.pos),
eye_y: p.pos[1] + f64::from(p.eye_height()),
ultrawarm: self.ext.ultrawarm,
fall_flying: self.ext.fall_flying,
entities: self.entities.as_deref(),
own_id: self.rt.own_id,
},
q,
)
}
fn air_use_sent(&mut self, blocks: &[(BlockPos, u32)]) {
for &(p, id) in blocks {
if let Some(s) = u16::try_from(id)
.ok()
.and_then(ghast_data::v1_16_5::BlockState::from_id)
{
log::debug!("item use set {} at {p:?}", s.block().name());
self.world.set_block_state(p.x, p.y, p.z, s);
}
}
}
fn place_sent(&mut self, p: &crate::sim::predict::PlaceSent<'_>) -> bool {
let player = self.player.bb;
if let Some(used) = crate::sim::place116::sent(&mut self.world, &player, p) {
log::debug!("place at {:?}: item used {used}", p.pos);
return used;
}
let Some((at, state)) = self.plain_placement(p) else {
return false;
};
log::debug!("predicted {} at {at:?}", state.block().name());
self.world.set_block_state(at.x, at.y, at.z, state);
true
}
#[cfg(test)]
fn set_player_pos(&mut self, p: Vec3) {
self.player.set_position(p.x, p.y, p.z);
}
#[inline(always)]
fn records_digs(&self) -> bool {
true
}
fn take_snap(&mut self) -> Option<Vec3> {
if !std::mem::take(&mut self.snapped) {
return None;
}
let p = self.player.pos;
Some(Vec3::new(p[0], p[1], p[2]))
}
fn held_item(&self, id: u16, count: u8, efficiency: u8) -> HeldItem {
held_item_facts(DataSet::V1_16_5, id, count, efficiency)
}
fn step(
&mut self,
pose: &mut Pose,
controls: Controls,
using_item: bool,
entity_id: i32,
out: &mut Vec<Outbound>,
) {
let input = ghast_physics::TickInput {
keys: keys(controls),
yaw: pose.yaw,
pitch: pose.pitch,
using_item,
};
self.packets.clear();
if let Some(r) = &mut self.rec
&& !r.started()
{
r.start_1_16(&self.player);
}
if let Some(r) = &mut self.rec {
let w = &self.world;
r.observe(self.player.pos, |x, y, z| {
w.is_column_loaded(x >> 4, z >> 4)
.then(|| w.block_state(x, y, z).map_or(0, |s| u32::from(s.id())))
});
}
if self.slow || using_item || !self.pistons.is_empty() {
self.slow_tick(&input, out);
} else {
ghast_physics::v1_16_5::tick(&mut self.player, &input, &self.world, &mut self.packets);
}
self.filed_pos = self.player.pos;
if self.force_step_position != ForceStepPosition::Off {
let p = &mut self.player;
let cur = p.pos;
force_position(
&mut self.packets,
cur,
&mut p.sent.pos,
&mut p.sent.ticks_since_pos,
self.force_step_position,
);
}
if let Some(r) = &mut self.rec {
r.tick(
&input,
&crate::record::state_1_16(&self.player),
&self.packets,
);
}
push_packets(&self.packets, entity_id, out);
sync_pose_16(pose, &self.player);
self.world.advance_time(50);
}
}
impl Sim16 {
#[cold]
#[inline(never)]
fn action_answer(
&mut self,
p: &ghast_protocol::v1_16_5::play::clientbound::AcknowledgePlayerDigging,
) {
use ghast_data::v1_16_5::BlockState;
let pos = p.location;
let at = u8::try_from(p.status)
.ok()
.and_then(|a| self.unacked.remove(pos, a));
let state = u16::try_from(p.block)
.ok()
.and_then(BlockState::from_id)
.unwrap_or(BlockState::AIR);
let now = self
.world
.block_state(pos.x, pos.y, pos.z)
.unwrap_or(BlockState::AIR);
let start = p.status == crate::common::DigStatus::Start.id();
if now != state && (at.is_none() || !p.successful || !start) {
log::debug!(
"dig answer {} ({}): {pos:?} {} back to the server's {}",
p.status,
if p.successful { "approved" } else { "refused" },
now.block().name(),
state.block().name()
);
self.world.set_block_state(pos.x, pos.y, pos.z, state);
if let Some(at) = at
&& holds_player(state, pos, &self.player)
{
self.player.set_position(at[0], at[1], at[2]);
self.snapped = true;
}
}
self.unacked.trim();
}
}
impl Sim16 {
fn plain_placement(
&self,
p: &crate::sim::predict::PlaceSent<'_>,
) -> Option<(BlockPos, ghast_data::v1_16_5::BlockState)> {
use crate::common::GameMode;
use ghast_data::v1_16_5::{Block, Item};
if !matches!(p.game_mode, GameMode::Survival | GameMode::Creative) {
return None;
}
let stack = p.held?;
if stack.nbt().is_some() {
return None;
}
let item = Item::from_id(u16::try_from(stack.id()).ok()?)?;
let block = Block::from_name(item.info().name)?;
let state = if block.layout().state_count == 1 {
block.default_state()
} else if is_pillar_16(block) {
use crate::common::Face;
let axis = match p.face {
Face::Down | Face::Up => "y",
Face::North | Face::South => "z",
Face::West | Face::East => "x",
};
block
.default_state()
.with(ghast_data::v1_16_5::Property::Axis, axis)?
} else {
return None;
};
if !state.full_cube()
|| block.info().name.ends_with("_concrete_powder")
{
return None;
}
if !crate::sim::predict::border_contains(
&ghast_physics::BlockView::world_border(&self.world),
p.pos,
) {
return None;
}
let clicked = self.world.block_state(p.pos.x, p.pos.y, p.pos.z)?;
if !clicked.full_cube() {
return None;
}
if !(p.sneaking && p.any_item) && activated_full_cube_16(clicked.block().info().name) {
return None;
}
let at = p.face.offset(p.pos);
if !(0..256).contains(&at.y) {
return None;
}
let there = self.world.block_state(at.x, at.y, at.z)?;
let name = there.block().info().name;
if !(there.is_air() || name == "water" || name == "lava") {
return None;
}
if holds_player(state, at, &self.player) {
return None;
}
Some((at, state))
}
}
fn activated_full_cube_16(name: &str) -> bool {
const LIST: &[&str] = &[
"barrel",
"beacon",
"blast_furnace",
"cartography_table",
"chain_command_block",
"command_block",
"crafting_table",
"dispenser",
"dropper",
"furnace",
"jigsaw",
"jukebox",
"loom",
"note_block",
"repeating_command_block",
"respawn_anchor",
"smithing_table",
"smoker",
"structure_block",
];
name.ends_with("shulker_box") || LIST.contains(&name)
}
fn holds_player(
state: ghast_data::v1_16_5::BlockState,
pos: BlockPos,
p: &ghast_physics::v1_16_5::PlayerState,
) -> bool {
let b = &p.bb;
ghast_physics::v1_16_5::state_collides(
state,
[pos.x, pos.y, pos.z],
[b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
&ghast_physics::v1_16_5::CollisionContext::new(p.pos[1], p.input.sneaking),
)
}
fn sync_pose_16(pose: &mut Pose, p: &ghast_physics::v1_16_5::PlayerState) {
pose.pos = Vec3::new(p.pos[0], p.pos[1], p.pos[2]);
pose.motion = Vec3::new(p.vel[0], p.vel[1], p.vel[2]);
pose.yaw = p.yaw;
pose.pitch = p.pitch;
pose.on_ground = p.on_ground;
pose.flying = p.abilities.flying;
pose.sneaking = p.sneaking();
pose.sprinting = p.sprinting;
}
#[cfg(all(test, ghast_modern))]
mod tests {
#[test]
fn voronoi_cell_is_the_modern_zoom() {
let mut r: u64 = 0x9E37_79B9_7F4A_7C15;
let mut next = || {
r ^= r << 13;
r ^= r >> 7;
r ^= r << 17;
r
};
for _ in 0..20_000 {
let seed = next() as i64;
let p = [next(), next(), next()].map(|v| (v as i32) >> 8);
assert_eq!(
super::voronoi_cell(seed, p[0], p[1], p[2]),
ghast_world::climate::zoom_cell(seed, p[0], p[1], p[2]),
"seed {seed} at {p:?}"
);
}
}
}
fn is_pillar_16(block: ghast_data::v1_16_5::Block) -> bool {
let mut props = block.properties();
props.len() == 1
&& props.next().is_some_and(|d| {
d.property == ghast_data::v1_16_5::Property::Axis && d.values.len() == 3
})
}