Revert "fix Heightmap when spawning (#1067)"
This reverts commit 514460316a.
This commit is contained in:
1 parent
514460316a
commit
46e7eba48a
1 file changed
+8
-198
@@ -1,15 +1,12 @@
|
|||||||
use crate::block::entities::BlockEntity;
|
use crate::block::entities::BlockEntity;
|
||||||
use crate::tick::scheduler::ChunkTickScheduler;
|
use crate::tick::scheduler::ChunkTickScheduler;
|
||||||
use palette::{BiomePalette, BlockPalette};
|
use palette::{BiomePalette, BlockPalette};
|
||||||
use pumpkin_data::block_properties::blocks_movement;
|
use pumpkin_data::Block;
|
||||||
use pumpkin_data::fluid::Fluid;
|
use pumpkin_data::fluid::Fluid;
|
||||||
use pumpkin_data::tag::Tagable;
|
|
||||||
use pumpkin_data::{Block, BlockState};
|
|
||||||
use pumpkin_nbt::compound::NbtCompound;
|
use pumpkin_nbt::compound::NbtCompound;
|
||||||
use pumpkin_nbt::nbt_long_array;
|
use pumpkin_nbt::nbt_long_array;
|
||||||
use pumpkin_util::math::{position::BlockPos, vector2::Vector2};
|
use pumpkin_util::math::{position::BlockPos, vector2::Vector2};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::ops::{BitAnd, BitOr};
|
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -138,25 +135,6 @@ pub struct ChunkLight {
|
|||||||
pub block_light: Box<[LightContainer]>,
|
pub block_light: Box<[LightContainer]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum ChunkHeightmapType {
|
|
||||||
WorldSurface = 0,
|
|
||||||
MotionBlocking = 1,
|
|
||||||
MotionBlockingNoLeaves = 2,
|
|
||||||
}
|
|
||||||
impl TryFrom<usize> for ChunkHeightmapType {
|
|
||||||
type Error = &'static str;
|
|
||||||
|
|
||||||
fn try_from(value: usize) -> Result<Self, Self::Error> {
|
|
||||||
match value {
|
|
||||||
0 => Ok(ChunkHeightmapType::WorldSurface),
|
|
||||||
1 => Ok(ChunkHeightmapType::MotionBlocking),
|
|
||||||
2 => Ok(ChunkHeightmapType::MotionBlockingNoLeaves),
|
|
||||||
_ => Err("Invalid usize value for ChunkHeightmapType. The value should be 0~2."),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
#[serde(rename_all = "UPPERCASE")]
|
#[serde(rename_all = "UPPERCASE")]
|
||||||
pub struct ChunkHeightmaps {
|
pub struct ChunkHeightmaps {
|
||||||
@@ -168,111 +146,12 @@ pub struct ChunkHeightmaps {
|
|||||||
pub motion_blocking_no_leaves: Box<[i64]>,
|
pub motion_blocking_no_leaves: Box<[i64]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChunkHeightmaps {
|
|
||||||
pub fn set(&mut self, _type: ChunkHeightmapType, pos: BlockPos, min_y: i32) {
|
|
||||||
let data = match _type {
|
|
||||||
ChunkHeightmapType::WorldSurface => &mut self.world_surface,
|
|
||||||
ChunkHeightmapType::MotionBlocking => &mut self.motion_blocking,
|
|
||||||
ChunkHeightmapType::MotionBlockingNoLeaves => &mut self.motion_blocking_no_leaves,
|
|
||||||
};
|
|
||||||
|
|
||||||
let local_x = (pos.0.x & 15) as usize;
|
|
||||||
let local_z = (pos.0.z & 15) as usize;
|
|
||||||
|
|
||||||
let adjust_height = (pos.0.y + min_y.abs()) as usize;
|
|
||||||
|
|
||||||
assert!(adjust_height <= 2 << 9);
|
|
||||||
|
|
||||||
//chunk column index in 16*16 chunk.
|
|
||||||
let column_idx = local_z * 16 + local_x;
|
|
||||||
|
|
||||||
// Each height value uses 9 bits, calculate starting bit position
|
|
||||||
let bit_start_idx = column_idx * 9;
|
|
||||||
|
|
||||||
// Find where these 9 bits start within a 64-bit packed array element
|
|
||||||
// We use bit_start_index % 63, which means the last bit of i64 won't be used,
|
|
||||||
// but this avoids the hassle of bit concatenation.
|
|
||||||
let packed_array_bit_start_idx = bit_start_idx as u32 % 63;
|
|
||||||
|
|
||||||
let mask = {
|
|
||||||
if packed_array_bit_start_idx == 0 {
|
|
||||||
//0b0000_0000_0111_1111_...
|
|
||||||
!(0x1FF << (64 - 9))
|
|
||||||
} else {
|
|
||||||
!(0x1FF << (64 - packed_array_bit_start_idx - 9))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let height_bit_bytes = adjust_height
|
|
||||||
.wrapping_shl(64 - 9 - packed_array_bit_start_idx)
|
|
||||||
.to_ne_bytes();
|
|
||||||
let height = i64::from_ne_bytes(height_bit_bytes);
|
|
||||||
|
|
||||||
let packed_array_idx = column_idx / 7;
|
|
||||||
|
|
||||||
data[packed_array_idx] = data[packed_array_idx].bitand(mask).bitor(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_height(&self, _type: ChunkHeightmapType, x: i32, z: i32, min_y: i32) -> i32 {
|
|
||||||
let data = match _type {
|
|
||||||
ChunkHeightmapType::WorldSurface => &self.world_surface,
|
|
||||||
ChunkHeightmapType::MotionBlocking => &self.motion_blocking,
|
|
||||||
ChunkHeightmapType::MotionBlockingNoLeaves => &self.motion_blocking_no_leaves,
|
|
||||||
};
|
|
||||||
|
|
||||||
let local_x = (x & 15) as usize;
|
|
||||||
let local_z = (z & 15) as usize;
|
|
||||||
|
|
||||||
let column_idx = local_z * 16 + local_x;
|
|
||||||
let bit_start_idx = column_idx * 9;
|
|
||||||
|
|
||||||
let packed_array_bit_start_idx = bit_start_idx as u32 % 63;
|
|
||||||
|
|
||||||
let mask = {
|
|
||||||
if packed_array_bit_start_idx == 0 {
|
|
||||||
//0b1111_1111_1000_0000_...
|
|
||||||
0x1ff << (64 - 9)
|
|
||||||
} else {
|
|
||||||
0x1ff << (64 - packed_array_bit_start_idx - 9)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let packed_array_idx = column_idx / 7;
|
|
||||||
|
|
||||||
let height_bit_bytes_i64 = data[packed_array_idx].bitand(mask).to_ne_bytes();
|
|
||||||
|
|
||||||
(u64::from_ne_bytes(height_bit_bytes_i64)
|
|
||||||
.wrapping_shr(64 - (packed_array_bit_start_idx + 9)) as i32)
|
|
||||||
- min_y.abs()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn log_heightmap(&self, _type: ChunkHeightmapType, min_y: i32) {
|
|
||||||
let mut header = "Z/X".to_string();
|
|
||||||
for x in 0..16 {
|
|
||||||
header.push_str(&format!("{x:4}"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let grid: String = (0..16)
|
|
||||||
.map(|z| {
|
|
||||||
let mut row = format!("{z:3}");
|
|
||||||
row.push_str(
|
|
||||||
&(0..16)
|
|
||||||
.map(|x| format!("{:4}", self.get_height(_type, x, z, min_y)))
|
|
||||||
.collect::<String>(),
|
|
||||||
);
|
|
||||||
row
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
log::info!("\nHeightMap:\n{header}\n{grid}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The Heightmap for a completely empty chunk
|
/// The Heightmap for a completely empty chunk
|
||||||
impl Default for ChunkHeightmaps {
|
impl Default for ChunkHeightmaps {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
// 9 bits per entry
|
||||||
|
// 0 packed into an i64 7 times.
|
||||||
motion_blocking: vec![0; 37].into_boxed_slice(),
|
motion_blocking: vec![0; 37].into_boxed_slice(),
|
||||||
motion_blocking_no_leaves: vec![0; 37].into_boxed_slice(),
|
motion_blocking_no_leaves: vec![0; 37].into_boxed_slice(),
|
||||||
world_surface: vec![0; 37].into_boxed_slice(),
|
world_surface: vec![0; 37].into_boxed_slice(),
|
||||||
@@ -431,80 +310,11 @@ impl ChunkData {
|
|||||||
.set_relative_block(relative_x, relative_y, relative_z, block_state_id);
|
.set_relative_block(relative_x, relative_y, relative_z, block_state_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Tracking heightmaps update.
|
#[expect(dead_code)]
|
||||||
pub async fn calculate_heightmap(&mut self) -> ChunkHeightmaps {
|
fn calculate_heightmap(&self) -> ChunkHeightmaps {
|
||||||
let highest_non_empty_subchunk = self.get_highest_non_empty_subchunk();
|
// figure out how LongArray is formatted
|
||||||
let mut heightmaps = ChunkHeightmaps::default();
|
// figure out how to find out if block is motion blocking
|
||||||
|
todo!()
|
||||||
for x in 0..16 {
|
|
||||||
for z in 0..16 {
|
|
||||||
self.populate_heightmaps(&mut heightmaps, highest_non_empty_subchunk, x, z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// log::info!("WorldSurface:");
|
|
||||||
// heightmaps.log_heightmap(ChunkHeightmapType::WorldSurface, self.section.min_y);
|
|
||||||
// log::info!("MotionBlocking:");
|
|
||||||
// heightmaps.log_heightmap(ChunkHeightmapType::MotionBlocking, self.section.min_y);
|
|
||||||
// log::info!("min_y: {}", self.section.min_y);
|
|
||||||
heightmaps
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn populate_heightmaps(
|
|
||||||
&self,
|
|
||||||
heightmaps: &mut ChunkHeightmaps,
|
|
||||||
start_sub_chunk: usize,
|
|
||||||
x: usize,
|
|
||||||
z: usize,
|
|
||||||
) {
|
|
||||||
let start_height = (start_sub_chunk as i32) * 16 - self.section.min_y.abs() + 15;
|
|
||||||
let mut has_found = [false, false];
|
|
||||||
|
|
||||||
for y in (self.section.min_y..=start_height).rev() {
|
|
||||||
let pos = BlockPos::new(x as i32, y, z as i32);
|
|
||||||
let state_id = self.section.get_block_absolute_y(x, y, z).unwrap();
|
|
||||||
let block_state = BlockState::from_id(state_id);
|
|
||||||
let block = Block::from_state_id(state_id);
|
|
||||||
|
|
||||||
if !block_state.is_air() && !has_found[ChunkHeightmapType::WorldSurface as usize] {
|
|
||||||
heightmaps.set(ChunkHeightmapType::WorldSurface, pos, self.section.min_y);
|
|
||||||
has_found[ChunkHeightmapType::WorldSurface as usize] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !has_found[ChunkHeightmapType::MotionBlocking as usize]
|
|
||||||
&& (blocks_movement(block_state)
|
|
||||||
|| Fluid::from_registry_key(block.registry_key())
|
|
||||||
.is_some_and(|fluid| !fluid.states.is_empty()))
|
|
||||||
{
|
|
||||||
heightmaps.set(ChunkHeightmapType::MotionBlocking, pos, self.section.min_y);
|
|
||||||
has_found[ChunkHeightmapType::MotionBlocking as usize] = true;
|
|
||||||
}
|
|
||||||
//TODO: MotionBlockingNoLeaves
|
|
||||||
|
|
||||||
if !has_found.contains(&false) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let pos = BlockPos::new(x as i32, self.section.min_y, z as i32);
|
|
||||||
for (idx, is_set) in has_found.iter().enumerate() {
|
|
||||||
if !(*is_set) {
|
|
||||||
heightmaps.set(idx.try_into().unwrap(), pos, self.section.min_y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_highest_non_empty_subchunk(&self) -> usize {
|
|
||||||
let idx = self
|
|
||||||
.section
|
|
||||||
.sections
|
|
||||||
.iter()
|
|
||||||
.rev()
|
|
||||||
.position(|sub_chunk| sub_chunk.block_states.non_air_block_count() != 0)
|
|
||||||
.map(|idx| (self.section.sections.len() - idx).saturating_sub(1));
|
|
||||||
|
|
||||||
idx.unwrap_or_default()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user