use super::*;
impl ModernSim {
pub(super) fn sync_level(&mut self) {
if let Some(d) = self.world.dimension() {
self.player.level = modern::LevelInfo {
min_y: d.kind.min_y,
height: d.kind.height,
fast_lava: d.kind.fast_lava,
};
}
}
pub(super) fn state(&self, x: i32, y: i32, z: i32) -> Option<BlockState> {
self.world.block_state(x, y, z)
}
#[cold]
#[inline(never)]
pub(super) fn verified_block_updates(&mut self, raw: &RawPacket) -> Result<(), SimError> {
use super::proto::play::clientbound as cb;
if raw.id == cb::id::BLOCK_CHANGE {
let p: cb::BlockChange = raw.decode_as()?;
self.updates.push(p.location, p.r#type as u32);
let s = state_from_wire(i64::from(p.r#type));
if self.predictions.update(p.location, s) {
log::debug!(
"block update {s} for predicted {:?} waits for the ack",
p.location
);
} else {
self.world.apply_block_change(&p);
}
return Ok(());
}
let mut p: cb::MultiBlockChange = raw.decode_as()?;
let (cx, cy, cz) = (
p.chunk_coordinates.x,
p.chunk_coordinates.y,
p.chunk_coordinates.z,
);
let sy = (cy << 12) >> 12;
let (updates, predictions) = (&mut self.updates, &mut self.predictions);
p.records.retain(|&r| {
let local = (r & 0xFFF) as i32;
let pos = BlockPos::new(
cx * 16 + ((local >> 8) & 15),
sy.wrapping_mul(16).wrapping_add(local & 15),
cz * 16 + ((local >> 4) & 15),
);
let state = ((r as u64) >> 12) as u32;
updates.push(pos, state);
!predictions.update(pos, state_from_wire(i64::from(state)))
});
self.world.apply_multi_block_change(&p);
Ok(())
}
#[cold]
#[inline(never)]
pub(super) fn end_predictions(&mut self, ack: i32) {
let (world, player) = (&mut self.world, &mut self.player);
let mut snap = None;
self.predictions.end_up_to(ack, |pos, state, at| {
let now = world
.block_state(pos.x, pos.y, pos.z)
.unwrap_or(BlockState::AIR);
if now == state {
log::debug!("block_changed_ack {ack}: {pos:?} stays {now}");
return;
}
log::debug!("block_changed_ack {ack}: {pos:?} {now} back to the server's {state}");
world.set_block_state(pos.x, pos.y, pos.z, state);
if let Some(at) = at
&& holds_player(state, pos, player)
{
player.set_pos(at[0], at[1], at[2]);
snap = Some(at);
}
});
if let Some(at) = snap {
self.old_pos = at;
self.snapped = true;
}
}
}
impl ModernSim {
pub(super) fn plain_placement(
&self,
p: &crate::sim::predict::PlaceSent<'_>,
) -> Option<(BlockPos, BlockState)> {
let at = p.face.offset(p.pos);
let state = plain_placement_rule(
p,
|q| self.world.block_state(q.x, q.y, q.z),
self.world
.dimension()
.map(|d| (d.kind.min_y, d.kind.height))?,
&ghast_physics::BlockView::world_border(&self.world),
)?;
if holds_player(state, at, &self.player) {
return None;
}
Some((at, state))
}
}
fn plain_placement_rule(
p: &crate::sim::predict::PlaceSent<'_>,
block_at: impl Fn(BlockPos) -> Option<BlockState>,
(min_y, height): (i32, i32),
border: &ghast_physics::WorldBorder,
) -> Option<BlockState> {
use super::data::{Block, Item};
use crate::common::GameMode;
use ghast_data::modern::state_flags as sf;
if !matches!(p.game_mode, GameMode::Survival | GameMode::Creative) {
return None;
}
let stack = p.held?;
if stack.components().is_some() {
return None;
}
let block = Item::from_id(u16::try_from(stack.id()).ok()?)?.block()?;
let state = if block.layout().state_count == 1 {
block.default_state()
} else if is_pillar(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(super::data::Property::Axis, axis)?
} else {
return None;
};
if !state.phys().has(sf::FULL_CUBE) || not_plain_placement(block.name()) {
return None;
}
if !crate::sim::predict::border_contains(border, p.pos) {
return None;
}
let clicked = block_at(p.pos)?;
if !clicked.phys().has(sf::FULL_CUBE) {
return None;
}
if !(p.sneaking && p.any_item) && activated_full_cube(clicked.block().name()) {
return None;
}
let at = p.face.offset(p.pos);
if at.y < min_y || at.y >= min_y + height {
return None;
}
let there = block_at(at)?;
if !(there.is_air() || matches!(there.block(), Block::WATER | Block::LAVA)) {
return None;
}
Some(state)
}
fn is_pillar(block: super::data::Block) -> bool {
let mut props = block.properties();
props.len() == 1
&& props
.next()
.is_some_and(|d| d.property == super::data::Property::Axis && d.values.len() == 3)
}
fn not_plain_placement(name: &str) -> bool {
name.ends_with("_concrete_powder") || name == "test_instance_block"
}
fn activated_full_cube(name: &str) -> bool {
const LIST: &[&str] = &[
"barrel",
"beacon",
"blast_furnace",
"cartography_table",
"chiseled_bookshelf",
"crafter",
"crafting_table",
"dispenser",
"dropper",
"furnace",
"loom",
"note_block",
"respawn_anchor",
"smithing_table",
"smoker",
"vault",
"bee_nest",
"beehive",
"chain_command_block",
"command_block",
"creaking_heart",
"jigsaw",
"jukebox",
"repeating_command_block",
"sculk_catalyst",
"spawner",
"structure_block",
"test_block",
"test_instance_block",
"trial_spawner",
];
name.ends_with("shulker_box") || LIST.contains(&name)
}
pub(super) fn fluid_left_behind(old: BlockState) -> BlockState {
use super::data::Block;
match old.phys().fluid() >> 6 {
1 => Block::WATER.default_state(),
2 => Block::LAVA.default_state(),
_ => BlockState::AIR,
}
}
fn state_from_wire(v: i64) -> BlockState {
u16::try_from(v)
.ok()
.and_then(BlockState::from_id)
.unwrap_or(BlockState::AIR)
}
fn holds_player(state: BlockState, pos: BlockPos, p: &modern::PlayerState) -> bool {
let b = &p.bb;
modern::state_collides::<Rules>(
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],
&modern::CollisionContext {
feet_y: p.pos[1],
descending: p.shift(),
fall_distance: p.fall_distance,
walks_on_powder_snow: p.leather_boots,
},
)
}
pub(super) fn outline_box(s: BlockState) -> Option<[f64; 6]> {
let shape = s.outline_shape();
if shape.empty {
return None;
}
union(shape.boxes)
}