use std::collections::HashMap;
use crate::Version;
use crate::class::{Class, Table, flag, table};
pub trait BlockSource {
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32>;
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
self.raw_block(chunk_x << 4, 0, chunk_z << 4).is_some()
}
fn build_height(&self) -> Option<(i32, i32)> {
None
}
}
impl<T: BlockSource + ?Sized> BlockSource for &T {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
(**self).raw_block(x, y, z)
}
#[inline]
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
(**self).is_column_loaded(chunk_x, chunk_z)
}
#[inline]
fn build_height(&self) -> Option<(i32, i32)> {
(**self).build_height()
}
}
#[cfg(ghast_modern)]
#[inline]
fn modern_range<S: BlockSource + ?Sized>(src: &S) -> (i32, i32) {
let (min_y, height) = src.build_height().unwrap_or((-64, 384));
(min_y, min_y.saturating_add(height.max(0)))
}
#[inline]
pub(crate) fn y_range<S: BlockSource + ?Sized>(version: Version, src: &S) -> (i32, i32) {
match version {
#[cfg(feature = "v26_1")]
Version::V26_1 => modern_range(src),
#[cfg(feature = "v26_2")]
Version::V26_2 => modern_range(src),
#[cfg(feature = "v26_3")]
Version::V26_3 => modern_range(src),
#[allow(unreachable_patterns)]
_ => {
let _ = src;
(0, 256)
}
}
}
#[cfg(feature = "v26_1")]
impl BlockSource for ghast_physics::modern::MemoryWorld26 {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
let lo = self.min_y();
if y < lo || y >= lo + self.height() || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
Some((self.min_y(), self.height()))
}
}
#[cfg(feature = "v26_2")]
impl BlockSource for ghast_physics::modern::MemoryWorld262 {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
let lo = self.min_y();
if y < lo || y >= lo + self.height() || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
Some((self.min_y(), self.height()))
}
}
#[cfg(feature = "v26_3")]
impl BlockSource for ghast_physics::modern::MemoryWorld263 {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
let lo = self.min_y();
if y < lo || y >= lo + self.height() || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
Some((self.min_y(), self.height()))
}
}
#[cfg(feature = "v1_8")]
impl BlockSource for ghast_physics::MemoryWorld<ghast_data::v1_8::BlockState> {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
if !(0..256).contains(&y) || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).raw()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "v1_12_2")]
impl BlockSource for ghast_physics::MemoryWorld<ghast_data::v1_12::BlockState> {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
if !(0..256).contains(&y) || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).raw()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "v1_16_5")]
impl BlockSource for ghast_physics::MemoryWorld<ghast_data::v1_16_5::BlockState> {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
use ghast_physics::BlockView;
if !(0..256).contains(&y) || !self.is_chunk_loaded(x >> 4, z >> 4) {
return None;
}
Some(u32::from(self.block(x, y, z).id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_physics::BlockView::is_chunk_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "client")]
mod client_impls {
use super::BlockSource;
impl BlockSource for dyn ghast_client::WorldView + '_ {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(-2048..2048).contains(&y) {
return None;
}
ghast_client::WorldView::raw_block(self, x, y, z)
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::WorldView::is_column_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "v1_8")]
impl BlockSource for ghast_client::world::v1_8::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(0..256).contains(&y) {
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.raw()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v1_8::World::is_column_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "v1_12_2")]
impl BlockSource for ghast_client::world::v1_12::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(0..256).contains(&y) {
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.raw()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v1_12::World::is_column_loaded(self, chunk_x, chunk_z)
}
}
#[cfg(feature = "v26_1")]
impl BlockSource for ghast_client::world::v26_1::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if let Some((lo, h)) = self.build_height()
&& (y < lo || y >= lo + h)
{
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v26_1::World::is_column_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
self.dimension().map(|d| (d.kind.min_y, d.kind.height))
}
}
#[cfg(feature = "v26_2")]
impl BlockSource for ghast_client::world::v26_2::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if let Some((lo, h)) = self.build_height()
&& (y < lo || y >= lo + h)
{
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v26_2::World::is_column_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
self.dimension().map(|d| (d.kind.min_y, d.kind.height))
}
}
#[cfg(feature = "v26_3")]
impl BlockSource for ghast_client::world::v26_3::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if let Some((lo, h)) = self.build_height()
&& (y < lo || y >= lo + h)
{
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v26_3::World::is_column_loaded(self, chunk_x, chunk_z)
}
fn build_height(&self) -> Option<(i32, i32)> {
self.dimension().map(|d| (d.kind.min_y, d.kind.height))
}
}
#[cfg(feature = "v1_16_5")]
impl BlockSource for ghast_client::world::v1_16_5::World {
#[inline]
fn raw_block(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(0..256).contains(&y) {
return None;
}
self.block_state(x, y, z).map(|s| u32::from(s.id()))
}
fn is_column_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
ghast_client::world::v1_16_5::World::is_column_loaded(self, chunk_x, chunk_z)
}
}
}
const NOT_CACHED: u16 = u16::MAX;
const UNKNOWN: u16 = u16::MAX - 1;
type Section = Box<[u16; 4096]>;
#[derive(Clone)]
pub(crate) struct Sections {
index: HashMap<u64, u32, BuildKeyHasher>,
secs: Vec<Section>,
last: (u64, u32),
doors: bool,
}
impl Default for Sections {
fn default() -> Self {
Sections {
index: HashMap::with_capacity_and_hasher(64, BuildKeyHasher),
secs: Vec::with_capacity(64),
last: (u64::MAX, u32::MAX),
doors: false,
}
}
}
pub(crate) struct Grid<'w, S: ?Sized> {
src: &'w S,
table: &'static Table,
sections: Sections,
y_lo: i32,
y_hi: i32,
}
impl<'w, S: BlockSource + ?Sized> Grid<'w, S> {
pub(crate) fn new(src: &'w S, version: Version) -> Self {
Self::with_cache(src, version, Sections::default())
}
pub(crate) fn with_cache(src: &'w S, version: Version, sections: Sections) -> Self {
let (y_lo, y_hi) = y_range(version, src);
Grid {
src,
table: table(version),
sections,
y_lo,
y_hi,
}
}
pub(crate) fn source(&self) -> &'w S {
self.src
}
pub(crate) fn into_cache(self) -> Sections {
self.sections
}
#[inline]
pub(crate) fn y_range(&self) -> (i32, i32) {
(self.y_lo, self.y_hi)
}
pub(crate) const AIR: u16 = 0;
#[inline]
fn key(x: i32, y: i32, z: i32) -> u64 {
let sx = (x >> 4) as u32 as u64 & 0x3f_ffff;
let sz = (z >> 4) as u32 as u64 & 0x3f_ffff;
let sy = (y >> 4) as u32 as u64 & 0xfffff;
sx << 42 | sz << 20 | sy
}
#[inline]
pub(crate) fn index(&mut self, x: i32, y: i32, z: i32) -> u16 {
if y < self.y_lo || y >= self.y_hi {
return if y >= self.y_hi { Self::AIR } else { UNKNOWN };
}
let key = Self::key(x, y, z);
let i = (((y & 15) as usize) << 8) | (((z & 15) as usize) << 4) | ((x & 15) as usize);
let c = &mut self.sections;
let si = if c.last.0 == key {
c.last.1
} else {
let n = c.secs.len() as u32;
let si = *c.index.entry(key).or_insert(n);
if si == n {
c.secs.push(Box::new([NOT_CACHED; 4096]));
}
c.last = (key, si);
si
};
let v = c.secs[si as usize][i];
if v != NOT_CACHED {
return v;
}
let v = match self.src.raw_block(x, y, z) {
Some(raw) => {
let mut v = self.table.index(raw);
let f = self.table.classes[v as usize].flags;
if f & (flag::DOOR_HALF | flag::PANEL | flag::OPENABLE) != 0 {
if f & flag::DOOR_HALF != 0
&& let Some(r) =
crate::door::resolve_legacy(self.table, self.src, [x, y, z], raw)
{
v = r;
}
self.sections.doors |=
self.table.classes[v as usize].has(flag::PANEL | flag::OPENABLE);
}
v
}
None => UNKNOWN,
};
self.sections.secs[si as usize][i] = v;
v
}
#[inline]
pub(crate) fn saw_doors(&self) -> bool {
self.sections.doors
}
#[inline]
pub(crate) fn class(&mut self, x: i32, y: i32, z: i32) -> Class {
let i = self.index(x, y, z);
if i == UNKNOWN {
Class::UNKNOWN
} else {
self.table.classes[i as usize]
}
}
#[inline]
pub(crate) fn unknown(&mut self, x: i32, y: i32, z: i32) -> bool {
self.index(x, y, z) == UNKNOWN
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct BuildKeyHasher;
impl std::hash::BuildHasher for BuildKeyHasher {
type Hasher = KeyHasher;
fn build_hasher(&self) -> KeyHasher {
KeyHasher(0)
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct KeyHasher(u64);
impl std::hash::Hasher for KeyHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
}
#[inline]
fn write(&mut self, bytes: &[u8]) {
for &b in bytes {
self.0 = (self.0.rotate_left(8) ^ u64::from(b)).wrapping_mul(0x9e37_79b9_7f4a_7c15);
}
}
#[inline]
fn write_u64(&mut self, v: u64) {
self.0 = (self.0 ^ v)
.wrapping_mul(0x9e37_79b9_7f4a_7c15)
.rotate_left(29);
}
#[inline]
fn write_u32(&mut self, v: u32) {
self.write_u64(u64::from(v));
}
#[inline]
fn write_i32(&mut self, v: i32) {
self.write_u64(u64::from(v as u32));
}
}