use ghast_protocol::{BlockPos, RawPacket};
use super::*;
use crate::common::{Abilities, Controls, Outbound, Pose, Teleport, Vec3};
use crate::hands::{BlockQuery, HeldItem};
mod entities;
mod fishing;
mod hooks;
mod nonliving;
mod piston;
mod vehicles;
fn arr(b: &ghast_physics::math::Aabb) -> [f64; 6] {
[b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z]
}
fn ray_1_8(w: &ghast_world::v1_8::World, x: i32, y: i32, z: i32) -> RayBox {
use ghast_physics::v1_8::RayShape;
match ghast_physics::v1_8::ray_shape(w, x, y, z, false) {
RayShape::None => (None, None),
RayShape::Bounds(b) => (Some(arr(&b)), None),
RayShape::Stairs(p) => (None, Some(p.boxes().map(|b| b.as_ref().map(arr)))),
}
}
type RayBox = (Option<[f64; 6]>, Option<[Option<[f64; 6]>; 8]>);
pub(crate) struct Sim18 {
world: ghast_world::v1_8::World,
pub(super) player: ghast_physics::v1_8::PlayerState,
packets: ghast_physics::Packets,
rec: Option<crate::record::Recorder>,
force_step_position: ForceStepPosition,
entities: Option<ghast_entity::v1_8::Tracker>,
updates: BlockUpdates,
session: SessionPackets,
block_entities: bool,
ride: Option<Box<Ride18>>,
own_id: Option<i32>,
#[cfg(test)]
vaporizes: bool,
server_flags: Option<u8>,
absorption: f32,
absorption_dirty: bool,
asleep: bool,
pistons: ghast_physics::v1_8::Pistons,
piston_edits: ghast_physics::v1_8::Edits,
piston_uncertain_at: Option<i32>,
piston_phase: bool,
ray_missed: bool,
head_watch: bool,
heads: Vec<[i32; 3]>,
heads_center: Option<[i32; 3]>,
boarded: Vec<i32>,
watched: Vec<(i32, ghast_physics::v1_8::Mount)>,
tracker_late: bool,
#[cfg(test)]
trig: Vec<(u64, f64, f64)>,
jump_bar: ghast_physics::v1_8::JumpBar,
nonliving: Option<Box<nonliving::NonLiving18>>,
fishing: crate::fishing::Fishing,
filter_dirty: bool,
}
#[cfg(test)]
struct TableTrig<'a>(&'a [(u64, f64, f64)]);
#[cfg(test)]
impl ghast_physics::v1_8::JavaTrig for TableTrig<'_> {
fn sin(&self, x: f64) -> f64 {
self.0
.iter()
.find(|e| e.0 == x.to_bits())
.map_or_else(|| x.sin(), |e| e.1)
}
fn cos(&self, x: f64) -> f64 {
self.0
.iter()
.find(|e| e.0 == x.to_bits())
.map_or_else(|| x.cos(), |e| e.2)
}
}
#[derive(Debug)]
struct Ride18 {
id: i32,
seat: Seat18,
}
#[derive(Debug)]
enum Seat18 {
Boat(ghast_physics::v1_8::Boat),
Cart(ghast_physics::v1_8::Minecart),
Mount(ghast_physics::v1_8::Mount),
}
#[cold]
#[inline(never)]
fn tick_watched_mounts(
watched: &mut Vec<(i32, ghast_physics::v1_8::Mount)>,
t: &ghast_entity::v1_8::Tracker,
ridden: Option<i32>,
) {
let me = t.self_id();
watched.retain(|(id, _)| {
t.get(*id)
.is_some_and(|e| e.vehicle().is_none() && e.passengers().iter().all(|&p| Some(p) == me))
});
for (id, m) in watched.iter_mut() {
if ridden != Some(*id) {
m.tick(None);
}
}
}
fn mount_of(e: &ghast_entity::Entity, kind: ghast_physics::v1_8::MountKind) -> Seat18 {
use ghast_entity::MetaValue;
let (yaw, pitch) = e.drawn_rotation();
let mut m = ghast_physics::v1_8::Mount::new(kind, e.drawn_position(), yaw, pitch);
m.tracked = e.position().map(|v| (v * 32.0).round() as i32);
if let Some((target, steps)) = e.interpolation() {
m.set_interpolation(target, e.yaw(), e.pitch(), i32::from(steps));
}
m.head_yaw = e.head_yaw();
m.body_yaw = m.head_yaw;
m.on_ground = e.drawn_on_ground();
match e.metadata().get(16) {
Some(MetaValue::Int(f)) => m.set_flags(*f),
Some(MetaValue::Byte(b)) => m.set_flags(i32::from(*b)),
_ => {
log::warn!(
"riding {kind:?} {} without its entity data (the metadata store is off): its \
saddle / rearing flags are unknown, taken as saddled and not rearing",
e.id()
);
m.set_flags(ghast_physics::v1_8::HORSE_SADDLED | 1);
}
}
Seat18::Mount(m)
}
impl std::fmt::Debug for Sim18 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sim18")
.field("world", &self.world)
.field("pos", &self.player.pos)
.finish_non_exhaustive()
}
}
#[cfg(test)]
impl Sim18 {
pub(crate) fn test_player(&self) -> &ghast_physics::v1_8::PlayerState {
&self.player
}
#[cfg(all(feature = "v1_12_2", feature = "v1_16_5", feature = "v26_1"))]
pub(crate) fn test_set_trig(&mut self, t: Vec<(u64, f64, f64)>) {
self.trig = t;
}
pub(crate) fn test_set_player(&mut self, pose: &mut Pose, p: ghast_physics::v1_8::PlayerState) {
self.player = p;
self.piston_uncertain_at = None;
self.pistons.forget_shared_bounds();
sync_pose(pose, &self.player);
}
#[cfg(all(test, feature = "v1_12_2", feature = "v1_16_5"))]
pub(crate) fn test_set_block(
&mut self,
x: i32,
y: i32,
z: i32,
s: ghast_data::v1_8::BlockState,
) {
let old = self.world.block_state(x, y, z);
self.head_change(x, y, z, old, s.block().id() == 34);
self.world.set_block_state(x, y, z, s);
}
pub(crate) fn test_world(&self) -> &ghast_world::v1_8::World {
&self.world
}
}
impl Sim18 {
pub(crate) fn new() -> Self {
Sim18 {
world: ghast_world::v1_8::World::new(config()),
player: ghast_physics::v1_8::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,
ride: None,
own_id: None,
#[cfg(test)]
vaporizes: false,
server_flags: None,
absorption: 0.0,
absorption_dirty: false,
pistons: ghast_physics::v1_8::Pistons::new(),
piston_edits: ghast_physics::v1_8::Edits::new(),
piston_uncertain_at: None,
piston_phase: false,
ray_missed: false,
head_watch: false,
heads: Vec::new(),
heads_center: None,
boarded: Vec::new(),
watched: Vec::new(),
tracker_late: false,
#[cfg(test)]
trig: Vec::new(),
jump_bar: ghast_physics::v1_8::JumpBar::default(),
asleep: false,
nonliving: None,
fishing: crate::fishing::Fishing::default(),
filter_dirty: false,
}
}
#[cold]
#[inline(never)]
pub(super) fn step_asleep(&mut self, input: &ghast_physics::TickInput) {
ghast_physics::v1_8::tick_asleep(
&mut self.player,
input,
&self.world,
&ghast_physics::v1_8::Options::default(),
&mut self.packets,
);
}
pub(super) fn wake_up_legacy(&mut self) {
if std::mem::take(&mut self.asleep) {
let b = self.player.bb;
let (w, h) = (
f64::from(ghast_physics::v1_8::WIDTH),
f64::from(ghast_physics::v1_8::HEIGHT),
);
self.player.bb = ghast_physics::math::Aabb {
max_x: b.min_x + w,
max_y: b.min_y + h,
max_z: b.min_z + w,
..b
};
}
}
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(ghast_entity::v1_8::Tracker::new);
if let Some(c) = opts.world {
s.world = ghast_world::v1_8::World::new(c);
s.block_entities = c.block_entities;
}
if opts.shared_chunks.is_some() {
log::debug!("shared chunks are not used on 1.8.9; every section stays owned");
}
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
}
}
sync_pose_fn!(sync_pose(p: ghast_physics::v1_8::PlayerState), y: p.bb.min_y, sneaking: sneaking);