use ghast_protocol::{BlockPos, DecodeError, RawPacket};
use ghast_world::{WorldConfig, WorldError};
use crate::common::{
Abilities, Controls, EntityAction, MovePacket, Outbound, Pose, Teleport, Vec3,
};
use crate::hands::{BlockQuery, HeldItem};
use crate::version::DataSet;
#[cfg(feature = "v26_1")]
mod teams;
#[cfg(feature = "v1_12_2")]
mod v1_12;
#[cfg(feature = "v1_16_5")]
mod v1_16_5;
#[cfg(feature = "v1_8")]
mod v1_8;
#[cfg(feature = "v26_1")]
mod v26_1;
#[cfg(feature = "v1_8")]
pub(crate) use v1_8::Sim18;
#[cfg(feature = "v1_12_2")]
pub(crate) use v1_12::Sim12;
#[cfg(feature = "v1_16_5")]
pub(crate) use v1_16_5::Sim16;
#[cfg(feature = "v26_1")]
pub(crate) use v26_1::Sim26;
#[cfg_attr(
not(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5")),
allow(dead_code)
)]
pub(crate) struct PickQuery<'a> {
pub eyes: Vec3,
pub rotation: (f32, f32),
pub look: Vec3,
pub held: Option<u16>,
pub now: bool,
pub creative: bool,
pub player_bb: ghast_physics::math::Aabb,
pub entities: &'a [ghast_physics::pick::PickEntity],
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum Crosshair {
Block(Option<crate::BlockHit>),
Entity {
index: usize,
hit: Vec3,
},
}
#[derive(Debug)]
pub(crate) enum SimError {
Decode(DecodeError),
World(WorldError),
}
impl From<DecodeError> for SimError {
fn from(e: DecodeError) -> Self {
SimError::Decode(e)
}
}
impl From<WorldError> for SimError {
fn from(e: WorldError) -> Self {
SimError::World(e)
}
}
pub trait WorldView {
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool;
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32>;
fn loaded_columns(&self) -> usize;
fn build_height(&self) -> Option<(i32, i32)> {
None
}
fn entities(&self) -> Option<&ghast_entity::Entities>;
fn block_query(&self, x: i32, y: i32, z: i32) -> Option<BlockQuery>;
fn column(&self, chunk_x: i32, chunk_z: i32) -> Option<&ghast_world::Column> {
let _ = (chunk_x, chunk_z);
None
}
fn crosshair(
&self,
eyes: Vec3,
rotation: (f32, f32),
look: Vec3,
reach: f64,
) -> Option<crate::BlockHit> {
let _ = rotation;
let blocks = |x: i32, y: i32, z: i32| self.block_query(x, y, z);
crate::hands::pick(&blocks, eyes, look, reach)
}
}
#[cfg(any(
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
#[derive(Debug, Default)]
pub(super) struct SessionPackets {
login: Option<RawPacket>,
respawn: Option<RawPacket>,
}
#[cfg(any(
feature = "v1_8",
feature = "v1_12_2",
feature = "v1_16_5",
feature = "v26_1"
))]
impl SessionPackets {
fn note(&mut self, raw: &RawPacket, login: i32, respawn: i32) {
if raw.id == login {
self.login = Some(raw.clone());
self.respawn = None;
} else if raw.id == respawn {
self.respawn = Some(raw.clone());
}
}
fn replay(&self, mut apply: impl FnMut(&RawPacket)) {
for r in [&self.login, &self.respawn].into_iter().flatten() {
apply(r);
}
}
}
pub(super) const MAX_BLOCK_UPDATES: usize = 4096;
#[derive(Debug, Default)]
pub(super) struct BlockUpdates {
on: bool,
list: Vec<(BlockPos, u32)>,
dropped: usize,
}
impl BlockUpdates {
fn push(&mut self, pos: BlockPos, state: u32) {
if self.on {
if self.list.len() < MAX_BLOCK_UPDATES {
self.list.push((pos, state));
} else {
self.dropped += 1;
}
}
}
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(not(feature = "v26_1"), allow(dead_code))]
pub(crate) struct PreTick {
pub(crate) using: Option<(bool, u16)>,
pub(crate) released: Option<(bool, u16, u32)>,
pub(crate) controls: Controls,
pub(crate) look: (f32, f32),
}
pub(crate) trait Sim: WorldView + std::fmt::Debug {
fn world_packet(&mut self, raw: &RawPacket) -> Result<(), SimError>;
fn reset_player(&mut self);
#[inline(always)]
fn item_use_facts(
&self,
stack: &ghast_inventory::Stack,
off_hand: bool,
held: HeldItem,
) -> HeldItem {
let _ = (stack, off_hand);
held
}
#[inline(always)]
fn modern_inputs(&self) -> bool {
false
}
#[inline(always)]
fn held_use_inputs(&mut self, inv: &mut ghast_inventory::Inventory) {
let _ = inv;
}
#[inline(always)]
fn pre_tick_wanted(&self) -> bool {
false
}
#[inline(always)]
fn pre_tick(&mut self, inv: &ghast_inventory::Inventory, t: &PreTick) -> bool {
let _ = (inv, t);
true
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_set_modern(
&mut self,
p: ghast_physics::modern::PlayerState,
x: ghast_physics::modern::PlayerExt,
) {
let _ = (p, x);
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_shared_flags(&self) -> u8 {
0
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_set_own_id(&mut self, id: i32) {
let _ = id;
}
#[inline(always)]
fn gliding(&self) -> bool {
false
}
fn weather(&mut self, reason: u8, value: f32) {
let _ = (reason, value);
}
fn mount_info(&self, entity_id: i32) -> Option<(i32, bool, bool)> {
let _ = entity_id;
None
}
#[inline(always)]
fn take_reply(&mut self) -> Option<Outbound> {
None
}
#[inline(always)]
fn take_filter_dirty(&mut self) -> bool {
false
}
#[inline(always)]
fn vehicle(&self) -> Option<crate::common::Vehicle> {
None
}
#[inline(always)]
fn set_main_arm_left(&mut self, left: bool) {
let _ = left;
}
fn drop_level(&mut self) {
self.reset_player();
}
fn respawn_player(&mut self, keep: u8) {
let _ = keep;
self.reset_player();
}
fn teleport(&mut self, pose: &mut Pose, t: &Teleport);
fn abilities(&mut self, a: Abilities);
fn food(&mut self, food: i32);
fn velocity(&mut self, v: Vec3, add: bool);
fn movement_speed(&mut self, base: f64, modifiers: &[(u128, f64, u8)]);
fn status_effects(&mut self, effects: &std::collections::BTreeMap<i8, i8>);
fn boots(&mut self, depth_strider: u8, soul_speed: u8);
fn feet_item(&mut self, id: Option<i32>) {
let _ = id;
}
fn spectator(&mut self, on: bool);
#[cfg(feature = "v26_1")]
fn modern_player(&self) -> Option<&ghast_physics::modern::PlayerState> {
None
}
fn step(
&mut self,
pose: &mut Pose,
controls: Controls,
using_item: bool,
entity_id: i32,
out: &mut Vec<Outbound>,
);
fn entity_packet_ids(&self) -> Vec<i32>;
fn world_packet_ids(&self) -> Vec<i32>;
#[cfg_attr(
not(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5")),
allow(dead_code)
)]
fn entity_config(&self) -> Option<ghast_entity::Config>;
fn set_entities(&mut self, config: Option<ghast_entity::Config>);
fn set_flying(&mut self, flying: bool, out: &mut Vec<Outbound>);
fn collect_block_updates(&mut self, on: bool);
fn drain_block_updates(&mut self, out: &mut Vec<(BlockPos, u32)>) -> usize;
fn predict_air(&mut self, x: i32, y: i32, z: i32);
fn held_item(&self, id: u16, count: u8, efficiency: u8) -> HeldItem;
fn crosshair_now(
&self,
eyes: Vec3,
rotation: (f32, f32),
look: Vec3,
held: Option<u16>,
) -> Option<crate::BlockHit> {
let _ = held;
self.crosshair(eyes, rotation, look, crate::hands::REACH)
}
fn pick_margin(&self, e: &ghast_entity::Entity) -> Option<f32> {
crate::engine::legacy_pick_margin(self.entities()?, e)
}
fn tick_entities(&mut self, pose: &mut Pose) {
let _ = pose;
}
fn level_load_start(&mut self) {}
fn player_alive(&mut self, alive: bool) {
let _ = alive;
}
fn load_gate_tick(&mut self, section_compiled: bool, timed_out: bool) -> bool {
let _ = (section_compiled, timed_out);
false
}
fn tick_unloaded(&mut self) {}
fn crosshair_target(&self, q: &PickQuery<'_>) -> Crosshair {
Crosshair::Block(if q.now {
self.crosshair_now(q.eyes, q.rotation, q.look, q.held)
} else {
self.crosshair(q.eyes, q.rotation, q.look, crate::hands::REACH)
})
}
fn attributes(&mut self, attributes: &[crate::common::AttributeUpdate]) {
let _ = attributes;
}
fn chunk_batch_start(&mut self, now_nanos: i64) {
let _ = now_nanos;
}
fn chunk_batch_finished(&mut self, batch_size: i32, now_nanos: i64) -> f32 {
let _ = (batch_size, now_nanos);
3.5
}
fn registry_data(&mut self, raw: &RawPacket) {
let _ = raw;
}
}
#[derive(Debug, Clone, Default)]
pub(crate) struct SimOptions {
pub record: Option<std::path::PathBuf>,
pub force_step_position: ForceStepPosition,
pub entities: Option<ghast_entity::Config>,
pub world: Option<WorldConfig>,
#[cfg_attr(not(feature = "v26_1"), allow(dead_code))]
pub profile_name: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ForceStepPosition {
#[default]
Off,
Always,
StepUpOnly,
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2", feature = "v1_16_5"))]
pub(super) fn force_position(
packets: &mut ghast_physics::Packets,
cur: [f64; 3],
sent_pos: &mut [f64; 3],
ticks_since_pos: &mut i32,
mode: ForceStepPosition,
) {
use ghast_physics::Packet as P;
if mode == ForceStepPosition::Off || cur[1] == sent_pos[1] {
return;
}
let Some(last) = packets.as_slice().last().copied() else {
return;
};
if mode == ForceStepPosition::StepUpOnly {
let on_ground = matches!(
last,
P::Flying { on_ground: true }
| P::Look {
on_ground: true,
..
}
);
if !on_ground || cur[1] <= sent_pos[1] {
return;
}
}
let new = match last {
P::Flying { on_ground } => P::Position {
x: cur[0],
y: cur[1],
z: cur[2],
on_ground,
},
P::Look {
yaw,
pitch,
on_ground,
} => P::PositionLook {
x: cur[0],
y: cur[1],
z: cur[2],
yaw,
pitch,
on_ground,
},
_ => return,
};
let mut out = ghast_physics::Packets::new();
let n = packets.len();
for p in &packets.as_slice()[..n - 1] {
out.push(*p);
}
out.push(new);
*packets = out;
*sent_pos = cur;
*ticks_since_pos = 0;
}
#[cfg(any(feature = "v1_16_5", feature = "v26_1"))]
pub(super) fn activates_1_16(name: &str) -> bool {
const EXACT: &[&str] = &[
"chest",
"trapped_chest",
"ender_chest",
"crafting_table",
"furnace",
"blast_furnace",
"smoker",
"barrel",
"anvil",
"chipped_anvil",
"damaged_anvil",
"enchanting_table",
"brewing_stand",
"hopper",
"dispenser",
"dropper",
"repeater",
"comparator",
"note_block",
"jukebox",
"cake",
"loom",
"stonecutter",
"grindstone",
"cartography_table",
"smithing_table",
"lectern",
"bell",
"beacon",
"daylight_detector",
"respawn_anchor",
"composter",
"lever",
"command_block",
"chain_command_block",
"repeating_command_block",
"structure_block",
"jigsaw",
"dragon_egg",
"flower_pot",
"sweet_berry_bush",
];
EXACT.contains(&name)
|| name.ends_with("_shulker_box")
|| name == "shulker_box"
|| name.ends_with("_bed")
|| name.ends_with("_button")
|| name.ends_with("_fence_gate")
|| (name.ends_with("_door") && name != "iron_door")
|| (name.ends_with("_trapdoor") && name != "iron_trapdoor")
|| name.starts_with("potted_")
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2"))]
pub(super) fn legacy_crosshair(t: ghast_physics::pick::legacy::Target, eye: [f64; 3]) -> Crosshair {
use ghast_physics::pick::legacy::Target;
match t {
Target::Block(h) => Crosshair::Block(block_hit(&h, eye)),
Target::Entity(e) => Crosshair::Entity {
index: e.index,
hit: Vec3::new(e.location[0], e.location[1], e.location[2]),
},
Target::None | Target::TooFar { .. } => Crosshair::Block(None),
}
}
#[cfg(any(feature = "v1_16_5", feature = "v26_1"))]
pub(super) fn modern_crosshair(t: ghast_physics::pick::Target, eye: [f64; 3]) -> Crosshair {
use ghast_physics::pick::Target;
match t {
Target::Block(h) => Crosshair::Block(block_hit(&h, eye)),
Target::Entity(e) => Crosshair::Entity {
index: e.index,
hit: Vec3::new(e.location[0], e.location[1], e.location[2]),
},
}
}
pub(super) fn block_hit(
hit: &ghast_physics::pick::BlockHit,
eye: [f64; 3],
) -> Option<crate::BlockHit> {
use ghast_physics::pick::Direction;
if !hit.is_block() {
return None;
}
let face = match hit.face {
Direction::Down => crate::Face::Down,
Direction::Up => crate::Face::Up,
Direction::North => crate::Face::North,
Direction::South => crate::Face::South,
Direction::West => crate::Face::West,
Direction::East => crate::Face::East,
};
let [lx, ly, lz] = hit.location;
let distance = ((lx - eye[0]).powi(2) + (ly - eye[1]).powi(2) + (lz - eye[2]).powi(2)).sqrt();
Some(crate::BlockHit {
pos: BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]),
face,
hit: Vec3::new(lx, ly, lz),
distance,
})
}
#[cfg(any(feature = "v1_12_2", feature = "v1_16_5", feature = "v26_1"))]
pub(super) fn union(boxes: &[ghast_data::Aabb]) -> Option<[f64; 6]> {
let mut u = [1.0_f64, 1.0, 1.0, 0.0, 0.0, 0.0];
for b in boxes {
u[0] = u[0].min(b.min_x);
u[1] = u[1].min(b.min_y);
u[2] = u[2].min(b.min_z);
u[3] = u[3].max(b.max_x.min(1.0));
u[4] = u[4].max(b.max_y.min(1.0));
u[5] = u[5].max(b.max_z.min(1.0));
}
(!boxes.is_empty()).then_some(u)
}
#[cfg(any(feature = "v1_8", feature = "v1_12_2"))]
pub(super) const ACTIVATED_1_8: &[u8] = &[
23, 25, 26, 54, 58, 61, 62, 64, 69, 71, 77, 84, 92, 93, 94, 96, 107, 116, 117, 118, 120, 130,
138, 143, 145, 146, 149, 150, 151, 154, 158, 167, 178, 183, 184, 185, 186, 187, 193, 194, 195,
196, 197,
];
#[cfg(feature = "v1_12_2")]
const USABLE_1_12: &[u16] = &[373, 335, 261, 442];
#[cfg(feature = "v1_8")]
const USABLE_1_8: &[u16] = &[373, 335, 261, 267, 268, 272, 276, 283];
pub(crate) fn held_item_facts(data: DataSet, id: u16, count: u8, efficiency: u8) -> HeldItem {
match data {
#[cfg(feature = "v1_8")]
DataSet::V1_8 => {
HeldItem {
modern_item: None,
swing_on_use: false,
id,
count,
efficiency,
places_block: id < 256,
usable: ghast_data::v1_8::food(id).is_some() || USABLE_1_8.contains(&id),
food: ghast_data::v1_8::food(id).is_some(),
always_edible: id == 322,
tool_speed: Some(ghast_data::v1_8::tool_multiplier),
}
}
#[cfg(feature = "v1_12_2")]
DataSet::V1_12 => HeldItem {
modern_item: None,
swing_on_use: false,
id,
count,
efficiency,
places_block: id < 256,
usable: ghast_data::v1_12::food(id).is_some() || USABLE_1_12.contains(&id),
food: ghast_data::v1_12::food(id).is_some(),
always_edible: id == 322 || id == 432,
tool_speed: Some(ghast_data::v1_12::tool_multiplier),
},
#[cfg(feature = "v1_16_5")]
DataSet::V1_16_5 => HeldItem {
modern_item: None,
swing_on_use: false,
id,
count,
efficiency,
places_block: ghast_data::v1_16_5::Item::from_id(id)
.is_some_and(|i| ghast_data::v1_16_5::Block::from_name(i.info().name).is_some()),
usable: ghast_data::v1_16_5::food(id).is_some()
|| ghast_data::v1_16_5::Item::from_id(id).is_some_and(|i| {
matches!(
i.info().name,
"potion"
| "milk_bucket"
| "honey_bottle"
| "bow"
| "crossbow"
| "shield"
| "trident"
)
}),
food: ghast_data::v1_16_5::food(id).is_some(),
always_edible: ghast_data::v1_16_5::Item::from_id(id).is_some_and(|i| {
matches!(
i.info().name,
"golden_apple" | "enchanted_golden_apple" | "chorus_fruit"
)
}),
tool_speed: Some(ghast_data::v1_16_5::tool_multiplier),
},
#[cfg(feature = "v26_1")]
DataSet::V26_1 => {
let item = ghast_data::v26_1::Item::from_id(id);
let name = item.map_or("", ghast_data::v26_1::Item::name);
let food = item.and_then(ghast_data::v26_1::Item::food);
HeldItem {
id,
count,
efficiency,
places_block: item.is_some_and(|i| i.block().is_some()),
usable: food.is_some()
|| matches!(
name,
"potion"
| "milk_bucket"
| "honey_bottle"
| "bow"
| "crossbow"
| "shield"
| "trident"
| "spyglass"
| "goat_horn"
),
food: food.is_some(),
always_edible: food.is_some_and(|f| f.can_always_eat),
tool_speed: None,
modern_item: item.map(|_| id),
swing_on_use: false,
}
}
#[allow(unreachable_patterns)]
_ => HeldItem {
modern_item: None,
swing_on_use: false,
id,
count,
efficiency,
places_block: false,
usable: false,
food: false,
always_edible: false,
tool_speed: None,
},
}
}
pub(super) fn config() -> WorldConfig {
WorldConfig::default()
.with_biomes(false)
.with_light(false)
.with_block_entities(false)
}
pub(super) fn abilities_flags(a: &ghast_physics::Abilities) -> i8 {
let mut f = 0;
if a.invulnerable {
f |= 1;
}
if a.flying {
f |= 2;
}
if a.allow_flying {
f |= 4;
}
if a.creative {
f |= 8;
}
f
}
pub(super) fn push_packets(
packets: &ghast_physics::Packets,
entity_id: i32,
out: &mut Vec<Outbound>,
) {
for p in packets {
use ghast_physics::Packet as P;
let action = |action| Outbound::EntityAction { entity_id, action };
let wire = match *p {
P::StartSneaking => action(EntityAction::StartSneaking),
P::StopSneaking => action(EntityAction::StopSneaking),
P::StartSprinting => action(EntityAction::StartSprinting),
P::StopSprinting => action(EntityAction::StopSprinting),
P::StartFallFlying => action(EntityAction::StartFallFlying),
P::Abilities(a) => Outbound::Abilities {
flags: abilities_flags(&a),
flying_speed: a.fly_speed,
walking_speed: a.walk_speed,
},
P::Flying { on_ground } => Outbound::Move(MovePacket::Flying {
horizontal_collision: false,
on_ground,
}),
P::Position { x, y, z, on_ground } => Outbound::Move(MovePacket::Position {
horizontal_collision: false,
pos: Vec3::new(x, y, z),
on_ground,
}),
P::Look {
yaw,
pitch,
on_ground,
} => Outbound::Move(MovePacket::Look {
horizontal_collision: false,
yaw,
pitch,
on_ground,
}),
P::PositionLook {
x,
y,
z,
yaw,
pitch,
on_ground,
} => Outbound::Move(MovePacket::PositionLook {
horizontal_collision: false,
pos: Vec3::new(x, y, z),
yaw,
pitch,
on_ground,
}),
P::Move {
pos,
look,
on_ground,
horizontal_collision,
} => Outbound::Move(match (pos, look) {
(Some([x, y, z]), Some((yaw, pitch))) => MovePacket::PositionLook {
pos: Vec3::new(x, y, z),
yaw,
pitch,
on_ground,
horizontal_collision,
},
(Some([x, y, z]), None) => MovePacket::Position {
pos: Vec3::new(x, y, z),
on_ground,
horizontal_collision,
},
(None, Some((yaw, pitch))) => MovePacket::Look {
yaw,
pitch,
on_ground,
horizontal_collision,
},
(None, None) => MovePacket::Flying {
on_ground,
horizontal_collision,
},
}),
P::MoveVehicle {
pos,
yaw,
pitch,
on_ground,
} => Outbound::VehicleMove {
pos: Vec3::new(pos[0], pos[1], pos[2]),
yaw,
pitch,
on_ground,
},
P::PaddleBoat { left, right } => Outbound::SteerBoat { left, right },
P::PlayerInput(k) => Outbound::PlayerInput(Controls {
forward: k.forward,
back: k.back,
left: k.left,
right: k.right,
jump: k.jump,
sneak: k.sneak,
sprint: k.sprint,
}),
_ => {
debug_assert!(false, "physics packet without a wire form: {p:?}");
continue;
}
};
out.push(wire);
}
}
#[cfg(any(feature = "v1_12_2", feature = "v1_16_5", feature = "v26_1"))]
pub(super) fn physics_abilities(a: Abilities) -> ghast_physics::Abilities {
ghast_physics::Abilities {
invulnerable: a.flags & 1 != 0,
flying: a.flags & 2 != 0,
allow_flying: a.flags & 4 != 0,
creative: a.flags & 8 != 0,
fly_speed: a.flying_speed,
walk_speed: a.walking_speed,
}
}
pub(super) fn keys(c: Controls) -> ghast_physics::Keys {
ghast_physics::Keys {
forward: c.forward,
back: c.back,
left: c.left,
right: c.right,
jump: c.jump,
sneak: c.sneak,
sprint: c.sprint,
}
}
#[cfg(any(feature = "v1_12_2", feature = "v1_16_5"))]
pub(super) fn rel_flags(flags: i8) -> [bool; 5] {
use crate::common::teleport_flags as fl;
[
flags & fl::X != 0,
flags & fl::Y != 0,
flags & fl::Z != 0,
flags & fl::YAW != 0,
flags & fl::PITCH != 0,
]
}
#[cfg(test)]
#[allow(unused_imports, dead_code)] pub(crate) mod test_chunks {
use bytes::{BufMut, Bytes, BytesMut};
use ghast_protocol::RawPacket;
#[cfg(feature = "v1_8")]
pub(crate) fn v1_8(x: i32, z: i32) -> RawPacket {
let mut d = BytesMut::with_capacity(12_544);
for i in 0..4096 {
d.put_u16_le(if i >> 8 == 15 { 1 << 4 } else { 0 });
}
d.put_bytes(0, 2048); d.put_bytes(0xff, 2048); d.put_bytes(1, 256); RawPacket::from_packet(&ghast_protocol::v1_8::play::clientbound::MapChunk {
x,
z,
ground_up: true,
bit_map: 1 << 3,
chunk_data: d.freeze(),
})
.unwrap()
}
#[cfg(feature = "v1_12_2")]
pub(crate) fn v1_12_2(x: i32, z: i32) -> RawPacket {
let mut d = BytesMut::new();
d.put_u8(4); d.put_u8(2); d.put_u8(0); d.put_u8(1 << 4); d.put_u8(0x80); d.put_u8(0x02);
for w in 0..256 {
d.put_u64(if w >= 240 { 0x1111_1111_1111_1111 } else { 0 });
}
d.put_bytes(0, 2048); d.put_bytes(0xff, 2048); d.put_bytes(1, 256); RawPacket::from_packet(&ghast_protocol::v1_12_2::play::clientbound::MapChunk {
x,
z,
ground_up: true,
bit_map: 1 << 3,
chunk_data: d.freeze(),
block_entities: Vec::new(),
})
.unwrap()
}
#[cfg(feature = "v26_1")]
pub(crate) fn v26_1(x: i32, z: i32) -> RawPacket {
v26_1_sections(x, z, 24, 7)
}
pub(crate) fn v26_1_sections(x: i32, z: i32, sections: usize, stone_at: usize) -> RawPacket {
use ghast_protocol::McWrite;
let stone = ghast_data::v26_1::BlockState::parse("stone").unwrap().id();
let mut d = BytesMut::new();
for section in 0..sections {
if section == stone_at {
d.put_i16(256);
d.put_i16(0);
d.put_u8(4);
d.put_varint(2);
d.put_varint(0);
d.put_varint(i32::from(stone));
for w in 0..256 {
d.put_u64(if w >= 240 { 0x1111_1111_1111_1111 } else { 0 });
}
} else {
d.put_i16(0);
d.put_i16(0);
d.put_u8(0);
d.put_varint(0);
}
d.put_u8(0);
d.put_varint(0);
}
RawPacket::from_packet(&ghast_protocol::v26_1::play::clientbound::MapChunk {
x,
z,
heightmaps: Vec::new(),
chunk_data: d.freeze(),
block_entities: Vec::new(),
sky_light_mask: Vec::new(),
block_light_mask: Vec::new(),
empty_sky_light_mask: Vec::new(),
empty_block_light_mask: Vec::new(),
sky_light: Vec::new(),
block_light: Vec::new(),
})
.unwrap()
}
#[cfg(feature = "v1_16_5")]
pub(crate) fn v1_16_5(x: i32, z: i32) -> RawPacket {
RawPacket::from_packet(&ghast_protocol::v1_16_5::play::clientbound::MapChunk {
x,
z,
ground_up: true,
bit_map: 0,
heightmaps: ghast_protocol::nbt::RawNbt::from_bytes(Bytes::from_static(&[10, 0, 0, 0]))
.unwrap(),
biomes: Some(vec![1; 1024]),
chunk_data: Bytes::new(),
block_entities: Vec::new(),
})
.unwrap()
}
}
#[cfg(test)]
impl WorldView for Box<dyn Sim> {
fn column(&self, chunk_x: i32, chunk_z: i32) -> Option<&ghast_world::Column> {
(**self).column(chunk_x, chunk_z)
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
(**self).is_column_loaded(chunk_x, chunk_z)
}
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
(**self).raw_block(x, y, z)
}
fn entities(&self) -> Option<&ghast_entity::Entities> {
(**self).entities()
}
fn loaded_columns(&self) -> usize {
(**self).loaded_columns()
}
fn block_query(&self, x: i32, y: i32, z: i32) -> Option<BlockQuery> {
(**self).block_query(x, y, z)
}
fn crosshair(
&self,
eyes: Vec3,
rotation: (f32, f32),
look: Vec3,
reach: f64,
) -> Option<crate::BlockHit> {
(**self).crosshair(eyes, rotation, look, reach)
}
}
#[cfg(test)]
impl Sim for Box<dyn Sim> {
fn world_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
(**self).world_packet(raw)
}
fn reset_player(&mut self) {
(**self).reset_player();
}
fn respawn_player(&mut self, keep: u8) {
(**self).respawn_player(keep);
}
fn teleport(&mut self, pose: &mut Pose, t: &Teleport) {
(**self).teleport(pose, t);
}
fn abilities(&mut self, a: Abilities) {
(**self).abilities(a);
}
fn food(&mut self, food: i32) {
(**self).food(food);
}
fn velocity(&mut self, v: Vec3, add: bool) {
(**self).velocity(v, add);
}
fn step(
&mut self,
pose: &mut Pose,
controls: Controls,
using_item: bool,
entity_id: i32,
out: &mut Vec<Outbound>,
) {
(**self).step(pose, controls, using_item, entity_id, out);
}
fn set_flying(&mut self, flying: bool, out: &mut Vec<Outbound>) {
(**self).set_flying(flying, out);
}
fn collect_block_updates(&mut self, on: bool) {
(**self).collect_block_updates(on);
}
fn drain_block_updates(&mut self, out: &mut Vec<(BlockPos, u32)>) -> usize {
(**self).drain_block_updates(out)
}
fn world_packet_ids(&self) -> Vec<i32> {
(**self).world_packet_ids()
}
fn entity_config(&self) -> Option<ghast_entity::Config> {
(**self).entity_config()
}
fn set_entities(&mut self, config: Option<ghast_entity::Config>) {
(**self).set_entities(config);
}
fn entity_packet_ids(&self) -> Vec<i32> {
(**self).entity_packet_ids()
}
fn predict_air(&mut self, x: i32, y: i32, z: i32) {
(**self).predict_air(x, y, z);
}
fn movement_speed(&mut self, base: f64, modifiers: &[(u128, f64, u8)]) {
(**self).movement_speed(base, modifiers);
}
fn status_effects(&mut self, effects: &std::collections::BTreeMap<i8, i8>) {
(**self).status_effects(effects);
}
fn boots(&mut self, depth_strider: u8, soul_speed: u8) {
(**self).boots(depth_strider, soul_speed);
}
fn feet_item(&mut self, id: Option<i32>) {
(**self).feet_item(id);
}
fn spectator(&mut self, on: bool) {
(**self).spectator(on);
}
fn held_item(&self, id: u16, count: u8, efficiency: u8) -> HeldItem {
(**self).held_item(id, count, efficiency)
}
fn item_use_facts(
&self,
stack: &ghast_inventory::Stack,
off_hand: bool,
held: HeldItem,
) -> HeldItem {
(**self).item_use_facts(stack, off_hand, held)
}
fn modern_inputs(&self) -> bool {
(**self).modern_inputs()
}
fn held_use_inputs(&mut self, inv: &mut ghast_inventory::Inventory) {
(**self).held_use_inputs(inv);
}
fn pre_tick_wanted(&self) -> bool {
(**self).pre_tick_wanted()
}
fn pre_tick(&mut self, inv: &ghast_inventory::Inventory, t: &PreTick) -> bool {
(**self).pre_tick(inv, t)
}
fn gliding(&self) -> bool {
(**self).gliding()
}
fn weather(&mut self, reason: u8, value: f32) {
(**self).weather(reason, value);
}
fn mount_info(&self, entity_id: i32) -> Option<(i32, bool, bool)> {
(**self).mount_info(entity_id)
}
#[cfg(feature = "v26_1")]
fn modern_player(&self) -> Option<&ghast_physics::modern::PlayerState> {
(**self).modern_player()
}
fn take_reply(&mut self) -> Option<Outbound> {
(**self).take_reply()
}
fn take_filter_dirty(&mut self) -> bool {
(**self).take_filter_dirty()
}
fn vehicle(&self) -> Option<crate::common::Vehicle> {
(**self).vehicle()
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_set_modern(
&mut self,
p: ghast_physics::modern::PlayerState,
x: ghast_physics::modern::PlayerExt,
) {
(**self).test_set_modern(p, x);
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_shared_flags(&self) -> u8 {
(**self).test_shared_flags()
}
#[cfg(all(test, feature = "v26_1", feature = "v1_8", feature = "v1_16_5"))]
fn test_set_own_id(&mut self, id: i32) {
(**self).test_set_own_id(id);
}
fn set_main_arm_left(&mut self, left: bool) {
(**self).set_main_arm_left(left);
}
fn crosshair_now(
&self,
eyes: Vec3,
rotation: (f32, f32),
look: Vec3,
held: Option<u16>,
) -> Option<crate::BlockHit> {
(**self).crosshair_now(eyes, rotation, look, held)
}
fn crosshair_target(&self, q: &PickQuery<'_>) -> Crosshair {
(**self).crosshair_target(q)
}
fn pick_margin(&self, e: &ghast_entity::Entity) -> Option<f32> {
(**self).pick_margin(e)
}
fn tick_entities(&mut self, pose: &mut Pose) {
(**self).tick_entities(pose);
}
fn level_load_start(&mut self) {
(**self).level_load_start();
}
fn player_alive(&mut self, alive: bool) {
(**self).player_alive(alive);
}
fn load_gate_tick(&mut self, section_compiled: bool, timed_out: bool) -> bool {
(**self).load_gate_tick(section_compiled, timed_out)
}
fn tick_unloaded(&mut self) {
(**self).tick_unloaded();
}
fn attributes(&mut self, attributes: &[crate::common::AttributeUpdate]) {
(**self).attributes(attributes);
}
fn chunk_batch_start(&mut self, now_nanos: i64) {
(**self).chunk_batch_start(now_nanos);
}
fn chunk_batch_finished(&mut self, batch_size: i32, now_nanos: i64) -> f32 {
(**self).chunk_batch_finished(batch_size, now_nanos)
}
fn registry_data(&mut self, raw: &RawPacket) {
(**self).registry_data(raw);
}
}
#[cfg(all(test, feature = "v1_8"))]
mod tests {
use super::*;
use crate::version::{ClientVersion, V1_8};
#[test]
fn teleport_rules_agree() {
let cases = [
(1.5, 64.0, -2.5, 450.0_f32, 100.0_f32, 0_i8),
(0.25, -1.0, 0.5, -30.0, -95.0, 0x1f),
(3.0, 70.0, 3.0, 720.5, 10.0, 0x08),
];
for (x, y, z, yaw, pitch, flags) in cases {
let t = Teleport {
x,
y,
z,
yaw,
pitch,
flags,
teleport_id: None,
..Default::default()
};
let start = Pose {
pos: Vec3::new(10.0, 65.0, 10.0),
yaw: 33.0,
pitch: 5.0,
motion: Vec3::new(0.1, -0.0784, 0.2),
..Pose::default()
};
let mut a = start;
<V1_8 as ClientVersion>::apply_teleport(&mut a, &t);
let mut sim = Sim18::new();
let mut b = start;
sim.player
.set_position(start.pos.x, start.pos.y, start.pos.z);
sim.player.vel = [start.motion.x, start.motion.y, start.motion.z];
sim.teleport(&mut b, &t);
assert_eq!(
(a.pos, a.yaw, a.pitch, a.motion),
(b.pos, b.yaw, b.pitch, b.motion),
"{t:?}"
);
}
}
#[test]
fn force_position_only_on_height_change() {
use ghast_physics::Packet as P;
let mut pk = ghast_physics::Packets::new();
pk.push(P::StopSprinting);
pk.push(P::Look {
yaw: 1.0,
pitch: 2.0,
on_ground: true,
});
let mut sent = [1.0, 3.979, 8.3];
let mut ticks = 3;
force_position(
&mut pk,
[1.01, 3.979, 8.3],
&mut sent,
&mut ticks,
ForceStepPosition::Always,
);
assert!(matches!(pk.as_slice()[1], P::Look { .. }));
force_position(
&mut pk,
[1.01, 4.0, 8.28],
&mut sent,
&mut ticks,
ForceStepPosition::Always,
);
assert_eq!(pk.as_slice()[0], P::StopSprinting);
assert!(matches!(
pk.as_slice()[1],
P::PositionLook {
y: 4.0,
on_ground: true,
..
}
));
assert_eq!((sent, ticks), ([1.01, 4.0, 8.28], 0));
}
#[test]
fn force_position_step_up_only() {
use ghast_physics::Packet as P;
let look = |on_ground| P::Look {
yaw: 1.0,
pitch: 2.0,
on_ground,
};
let mode = ForceStepPosition::StepUpOnly;
let mut pk = ghast_physics::Packets::new();
pk.push(look(true));
let (mut sent, mut ticks) = ([1.0, 4.0, 8.3], 3);
force_position(&mut pk, [1.01, 3.979, 8.3], &mut sent, &mut ticks, mode);
assert!(matches!(pk.as_slice()[0], P::Look { .. }));
force_position(&mut pk, [1.01, 4.01, 8.3], &mut sent, &mut ticks, mode);
let mut air = ghast_physics::Packets::new();
air.push(look(false));
force_position(&mut air, [1.01, 4.01, 8.3], &mut sent, &mut ticks, mode);
assert!(matches!(air.as_slice()[0], P::Look { .. }));
let mut up = ghast_physics::Packets::new();
up.push(look(true));
let (mut sent, mut ticks) = ([1.0, 3.979, 8.3], 3);
force_position(&mut up, [1.01, 4.0, 8.28], &mut sent, &mut ticks, mode);
assert!(matches!(up.as_slice()[0], P::PositionLook { y: 4.0, .. }));
assert_eq!((sent, ticks), ([1.01, 4.0, 8.28], 0));
}
#[test]
fn physics_stands_on_the_test_floor() {
let mut sim = Sim18::new();
sim.world_packet(&test_chunks::v1_8(0, 0)).unwrap();
let mut pose = Pose::default();
sim.teleport(
&mut pose,
&Teleport {
x: 8.5,
y: 64.0,
z: 8.5,
yaw: 0.0,
pitch: 0.0,
flags: 0,
teleport_id: None,
..Default::default()
},
);
let mut out = Vec::new();
for _ in 0..30 {
sim.step(&mut pose, Controls::NONE, false, 1, &mut out);
}
assert_eq!(pose.pos, Vec3::new(8.5, 64.0, 8.5));
assert!(pose.on_ground);
for _ in 0..10 {
sim.step(
&mut pose,
Controls {
forward: true,
..Controls::NONE
},
false,
1,
&mut out,
);
}
assert!(pose.pos.z > 9.0, "{pose:?}");
}
}