mod attr;
mod boat;
mod border;
pub(crate) mod collide;
mod entity;
mod ext;
mod fluid;
mod inside;
mod jdk_math;
mod load;
pub(crate) mod math;
mod moving;
pub mod piston;
mod piston_facts;
mod piston_shapes;
mod player;
mod rules;
#[cfg(feature = "v26_3")]
mod sturdy_facts;
pub use attr::{
Attribute, MAX_MODIFIERS, Modifier, ModifierId, SPRINT_AMOUNT, identifier_hash,
java_string_hash, sprinting_id,
};
pub use boat::{Boat, BoatKind, BoatStatus, Seat, tick_boat, tick_passenger};
pub use border::BorderState;
pub use entity::{
PushKind, Pusher, Surroundings, collides_26_1, push_by, push_kind_26_1, section_query_reaches,
};
pub use ext::{MAX_ROCKETS, MovementAttributes, PlayerExt};
pub use load::LoadGate;
pub use player::{
AttributeKind, Attributes, ClientInput, ClientOptions, CollisionContext, Dimensions, Effects,
LevelInfo, PlayerState, Pose, SentState, UseEffects, in_water_or_rain, no_block_collision,
on_climbable, on_climbable_ext, riptide, state_collides, tick, tick_ext, tick_in,
};
pub use rules::Rules;
#[cfg(feature = "v26_1")]
pub use rules::V26_1;
#[cfg(feature = "v26_2")]
pub use rules::V26_2;
#[cfg(feature = "v26_3")]
pub use rules::V26_3;
use crate::input::{Packets, TickInput};
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Options {}
#[cfg(feature = "v26_1")]
impl crate::Ruleset for V26_1 {
type BlockState = ghast_data::v26_1::BlockState;
type Player = PlayerState;
type Options = Options;
#[inline]
fn tick_with<W: BlockView<State = Self::BlockState>>(
player: &mut PlayerState,
input: &TickInput,
world: &W,
_options: &Options,
out: &mut Packets,
) {
tick::<V26_1, W>(player, input, world, out);
}
}
#[cfg(feature = "v26_2")]
impl crate::Ruleset for V26_2 {
type BlockState = ghast_data::v26_2::BlockState;
type Player = PlayerState;
type Options = Options;
#[inline]
fn tick_with<W: BlockView<State = Self::BlockState>>(
player: &mut PlayerState,
input: &TickInput,
world: &W,
_options: &Options,
out: &mut Packets,
) {
tick::<V26_2, W>(player, input, world, out);
}
}
#[cfg(feature = "v26_3")]
impl crate::Ruleset for V26_3 {
type BlockState = ghast_data::v26_3::BlockState;
type Player = PlayerState;
type Options = Options;
#[inline]
fn tick_with<W: BlockView<State = Self::BlockState>>(
player: &mut PlayerState,
input: &TickInput,
world: &W,
_options: &Options,
out: &mut Packets,
) {
tick::<V26_3, W>(player, input, world, out);
}
}
#[cfg(feature = "v26_1")]
pub type MemoryWorld26 = ModernMemoryWorld<ghast_data::v26_1::BlockState>;
#[cfg(feature = "v26_2")]
pub type MemoryWorld262 = ModernMemoryWorld<ghast_data::v26_2::BlockState>;
#[cfg(feature = "v26_3")]
pub type MemoryWorld263 = ModernMemoryWorld<ghast_data::v26_3::BlockState>;
#[derive(Clone, Debug)]
pub struct ModernMemoryWorld<S> {
columns: std::collections::HashMap<(i32, i32), Box<[S]>>,
loaded_rects: Vec<[i32; 4]>,
decode: fn(u16) -> S,
air: S,
min_y: i32,
height: i32,
border: crate::WorldBorder,
}
impl<S: Copy> ModernMemoryWorld<S> {
#[must_use]
pub fn new(decode: fn(u16) -> S, min_y: i32, height: i32) -> Self {
ModernMemoryWorld {
columns: std::collections::HashMap::new(),
loaded_rects: Vec::new(),
decode,
air: decode(0),
min_y,
height,
border: crate::WorldBorder::default(),
}
}
#[must_use]
pub fn min_y(&self) -> i32 {
self.min_y
}
#[must_use]
pub fn height(&self) -> i32 {
self.height
}
pub fn load_chunks(&mut self, cx0: i32, cz0: i32, cx1: i32, cz1: i32) {
self.loaded_rects.push([cx0, cz0, cx1, cz1]);
}
pub fn set_border(&mut self, b: crate::WorldBorder) {
self.border = b;
}
pub fn set_raw(&mut self, x: i32, y: i32, z: i32, raw: u16) {
if y < self.min_y || y >= self.min_y + self.height {
return;
}
let len = (16 * 16 * self.height) as usize;
let air = self.air;
let state = (self.decode)(raw);
let col = self
.columns
.entry((x >> 4, z >> 4))
.or_insert_with(|| vec![air; len].into_boxed_slice());
col[Self::index(x, y - self.min_y, z)] = state;
}
pub fn fill_raw(&mut self, from: [i32; 3], to: [i32; 3], raw: u16) {
for x in from[0]..=to[0] {
for z in from[2]..=to[2] {
for y in from[1]..=to[1] {
self.set_raw(x, y, z, raw);
}
}
}
}
#[inline]
fn index(x: i32, ry: i32, z: i32) -> usize {
((ry as usize) << 8) | (((z & 15) as usize) << 4) | ((x & 15) as usize)
}
#[inline]
fn loaded(&self, cx: i32, cz: i32) -> bool {
self.loaded_rects
.iter()
.any(|r| cx >= r[0] && cx <= r[2] && cz >= r[1] && cz <= r[3])
}
}
impl<S: Copy> BlockView for ModernMemoryWorld<S> {
type State = S;
#[inline]
fn block(&self, x: i32, y: i32, z: i32) -> S {
let (cx, cz) = (x >> 4, z >> 4);
let ry = y - self.min_y;
if ry < 0 || ry >= self.height || !self.loaded(cx, cz) {
return self.air;
}
match self.columns.get(&(cx, cz)) {
Some(col) => col[Self::index(x, ry, z)],
None => self.air,
}
}
#[inline]
fn is_chunk_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
self.loaded(chunk_x, chunk_z)
}
fn world_border(&self) -> crate::WorldBorder {
self.border
}
}
#[cfg(feature = "v26_3")]
#[must_use]
pub fn memory_world_26_3() -> MemoryWorld263 {
ModernMemoryWorld::new(
|raw| {
ghast_data::v26_3::BlockState::from_id(raw)
.unwrap_or(ghast_data::v26_3::BlockState::AIR)
},
-64,
384,
)
}
#[cfg(feature = "v26_2")]
#[must_use]
pub fn memory_world_26_2() -> MemoryWorld262 {
ModernMemoryWorld::new(
|raw| {
ghast_data::v26_2::BlockState::from_id(raw)
.unwrap_or(ghast_data::v26_2::BlockState::AIR)
},
-64,
384,
)
}
#[cfg(feature = "v26_1")]
#[must_use]
pub fn memory_world_26_1() -> MemoryWorld26 {
ModernMemoryWorld::new(
|raw| {
ghast_data::v26_1::BlockState::from_id(raw)
.unwrap_or(ghast_data::v26_1::BlockState::AIR)
},
-64,
384,
)
}