use super::*;
impl Sim16 {
pub(super) 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)]
pub(super) 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)]
pub(super) 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();
}
piston_bookkeeping!(
moving: |s| s.block() == ghast_data::v1_16_5::Block::MOVING_PISTON,
wire_id: id
);
}