diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2026-01-12 02:09:41 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-12 02:09:41 -0600 |
| commit | 1accbac964168af5fa0d87cb170389f0a9d01363 (patch) | |
| tree | 1509b26c19beaa23a492289f6bf00d3958be44d5 /azalea-world/src | |
| parent | 58339b9d229592dee40e15b8648fe4075cc391f4 (diff) | |
| download | azalea-drasl-1accbac964168af5fa0d87cb170389f0a9d01363.tar.xz | |
Make Bevy dependencies optional in azalea-protocol (#303)
* Make Bevy dependencies optional in azalea-protocol
* derive serde traits on Direction again
* update docs for types that may not have Component
Diffstat (limited to 'azalea-world/src')
| -rw-r--r-- | azalea-world/src/chunk_storage.rs | 13 | ||||
| -rw-r--r-- | azalea-world/src/heightmap.rs | 86 | ||||
| -rw-r--r-- | azalea-world/src/world.rs | 80 |
3 files changed, 35 insertions, 144 deletions
diff --git a/azalea-world/src/chunk_storage.rs b/azalea-world/src/chunk_storage.rs index 65dbbb7a..3f8c4e2d 100644 --- a/azalea-world/src/chunk_storage.rs +++ b/azalea-world/src/chunk_storage.rs @@ -12,18 +12,19 @@ use azalea_block::{ fluid_state::FluidState, }; use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError}; -use azalea_core::position::{ - BlockPos, ChunkBiomePos, ChunkBlockPos, ChunkPos, ChunkSectionBiomePos, ChunkSectionBlockPos, +use azalea_core::{ + heightmap_kind::HeightmapKind, + position::{ + BlockPos, ChunkBiomePos, ChunkBlockPos, ChunkPos, ChunkSectionBiomePos, + ChunkSectionBlockPos, + }, }; use azalea_registry::data::Biome; use nohash_hasher::IntMap; use parking_lot::RwLock; use tracing::{debug, trace, warn}; -use crate::{ - heightmap::{Heightmap, HeightmapKind}, - palette::PalettedContainer, -}; +use crate::{heightmap::Heightmap, palette::PalettedContainer}; const SECTION_HEIGHT: u32 = 16; diff --git a/azalea-world/src/heightmap.rs b/azalea-world/src/heightmap.rs index 033fa569..bae11cea 100644 --- a/azalea-world/src/heightmap.rs +++ b/azalea-world/src/heightmap.rs @@ -1,33 +1,20 @@ -use std::{ - fmt::{self, Display}, - str::FromStr, -}; - use azalea_block::{BlockState, BlockTrait}; -use azalea_buf::AzBuf; -use azalea_core::{math, position::ChunkBlockPos}; +use azalea_core::{heightmap_kind::HeightmapKind as HeightmapKind_, math, position::ChunkBlockPos}; use azalea_registry::tags::blocks::LEAVES; use tracing::warn; use crate::{BitStorage, Section, chunk_storage::get_block_state_from_sections}; -// (wg stands for worldgen) - -#[derive(AzBuf, Clone, Copy, Debug, Eq, Hash, PartialEq)] -pub enum HeightmapKind { - WorldSurfaceWg, - WorldSurface, - OceanFloorWg, - OceanFloor, - MotionBlocking, - MotionBlockingNoLeaves, -} +// TODO: when removing this deprecated marker, also rename `HeightmapKind_` back +// to `HeightmapKind`. +#[deprecated = "moved to `azalea_core::heightmap_kind::HeightmapKind`."] +pub type HeightmapKind = azalea_core::heightmap_kind::HeightmapKind; #[derive(Clone, Debug)] pub struct Heightmap { pub data: BitStorage, pub min_y: i32, - pub kind: HeightmapKind, + pub kind: HeightmapKind_, } fn blocks_motion(block_state: BlockState) -> bool { @@ -43,25 +30,24 @@ fn motion_blocking(block_state: BlockState) -> bool { .unwrap_or_default() } -impl HeightmapKind { - pub fn is_opaque(self, block_state: BlockState) -> bool { - let block = Box::<dyn BlockTrait>::from(block_state); - let registry_block = block.as_registry_block(); - match self { - HeightmapKind::WorldSurfaceWg => !block_state.is_air(), - HeightmapKind::WorldSurface => !block_state.is_air(), - HeightmapKind::OceanFloorWg => blocks_motion(block_state), - HeightmapKind::OceanFloor => blocks_motion(block_state), - HeightmapKind::MotionBlocking => motion_blocking(block_state), - HeightmapKind::MotionBlockingNoLeaves => { - motion_blocking(block_state) && !LEAVES.contains(®istry_block) - } +pub fn is_heightmap_opaque(heightmap: HeightmapKind_, block_state: BlockState) -> bool { + let block = Box::<dyn BlockTrait>::from(block_state); + let registry_block = block.as_registry_block(); + type K = HeightmapKind_; + match heightmap { + K::WorldSurfaceWg => !block_state.is_air(), + K::WorldSurface => !block_state.is_air(), + K::OceanFloorWg => blocks_motion(block_state), + K::OceanFloor => blocks_motion(block_state), + K::MotionBlocking => motion_blocking(block_state), + K::MotionBlockingNoLeaves => { + motion_blocking(block_state) && !LEAVES.contains(®istry_block) } } } impl Heightmap { - pub fn new(kind: HeightmapKind, dimension_height: u32, min_y: i32, data: Box<[u64]>) -> Self { + pub fn new(kind: HeightmapKind_, dimension_height: u32, min_y: i32, data: Box<[u64]>) -> Self { let bits = math::ceil_log2(dimension_height + 1); let mut bit_storage = BitStorage::new(bits as usize, 16 * 16, None) .expect("data is empty, so this can't fail"); @@ -113,7 +99,7 @@ impl Heightmap { if pos.y <= first_available_y - 2 { return false; } - if self.kind.is_opaque(block_state) { + if is_heightmap_opaque(self.kind, block_state) { // increase y if pos.y >= first_available_y { self.set_height(pos.x, pos.z, pos.y + 1); @@ -122,7 +108,8 @@ impl Heightmap { } else if first_available_y - 1 == pos.y { // decrease y for y in (self.min_y..pos.y).rev() { - if self.kind.is_opaque( + if is_heightmap_opaque( + self.kind, get_block_state_from_sections( sections, &ChunkBlockPos::new(pos.x, y, pos.z), @@ -161,32 +148,3 @@ impl Heightmap { }) } } - -impl FromStr for HeightmapKind { - type Err = (); - - fn from_str(s: &str) -> Result<Self, Self::Err> { - match s { - "WORLD_SURFACE_WG" => Ok(HeightmapKind::WorldSurfaceWg), - "WORLD_SURFACE" => Ok(HeightmapKind::WorldSurface), - "OCEAN_FLOOR_WG" => Ok(HeightmapKind::OceanFloorWg), - "OCEAN_FLOOR" => Ok(HeightmapKind::OceanFloor), - "MOTION_BLOCKING" => Ok(HeightmapKind::MotionBlocking), - "MOTION_BLOCKING_NO_LEAVES" => Ok(HeightmapKind::MotionBlockingNoLeaves), - _ => Err(()), - } - } -} - -impl Display for HeightmapKind { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - HeightmapKind::WorldSurfaceWg => write!(f, "WORLD_SURFACE_WG"), - HeightmapKind::WorldSurface => write!(f, "WORLD_SURFACE"), - HeightmapKind::OceanFloorWg => write!(f, "OCEAN_FLOOR_WG"), - HeightmapKind::OceanFloor => write!(f, "OCEAN_FLOOR"), - HeightmapKind::MotionBlocking => write!(f, "MOTION_BLOCKING"), - HeightmapKind::MotionBlockingNoLeaves => write!(f, "MOTION_BLOCKING_NO_LEAVES"), - } - } -} diff --git a/azalea-world/src/world.rs b/azalea-world/src/world.rs index 26856fff..a6ee1a63 100644 --- a/azalea-world/src/world.rs +++ b/azalea-world/src/world.rs @@ -1,22 +1,16 @@ use std::{ collections::{HashMap, HashSet}, - fmt::{self, Debug, Display}, - hash::{Hash, Hasher}, - io::{self, Cursor}, + fmt::{self, Debug}, }; use azalea_block::{BlockState, fluid_state::FluidState}; -use azalea_buf::{AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_core::{ position::{BlockPos, ChunkPos}, registry_holder::RegistryHolder, }; use azalea_registry::data::Biome; -use bevy_ecs::{component::Component, entity::Entity}; -use derive_more::{Deref, DerefMut}; +use bevy_ecs::entity::Entity; use nohash_hasher::IntMap; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; use crate::{ChunkStorage, PartialChunkStorage}; @@ -50,70 +44,8 @@ impl PartialInstance { } } -/// An entity ID used by Minecraft. -/// -/// These IDs are picked by the server. Some server software (like Bungeecord) -/// may pick entity IDs per-player, so you should avoid relying on them for -/// identifying IDs (especially if you're using a shared world -- i.e. a swarm). -/// -/// You might find [`Entity`] more useful, since that's an ID decided by us that -/// is likely to be correct across shared worlds. You could also use the -/// `EntityUuid` from `azalea_entity`, that one is unlikely to change even -/// across server restarts. -/// -/// This serializes as a i32. Usually it's a VarInt in the protocol, but not -/// always. If you do need it to serialize as a VarInt, make sure to use use the -/// `#[var]` attribute. -/// -/// [`Entity`]: bevy_ecs::entity::Entity -#[derive(Clone, Component, Copy, Debug, Default, Deref, DerefMut, Eq, PartialEq)] -#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] -pub struct MinecraftEntityId(pub i32); - -impl Hash for MinecraftEntityId { - fn hash<H: Hasher>(&self, hasher: &mut H) { - hasher.write_i32(self.0); - } -} -impl nohash_hasher::IsEnabled for MinecraftEntityId {} - -// we can't have the default be #[var] because mojang doesn't use varints for -// entities sometimes :( -impl AzaleaRead for MinecraftEntityId { - fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - i32::azalea_read(buf).map(MinecraftEntityId) - } -} -impl AzaleaWrite for MinecraftEntityId { - fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> { - i32::azalea_write(&self.0, buf) - } -} -impl AzaleaReadVar for MinecraftEntityId { - fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - i32::azalea_read_var(buf).map(MinecraftEntityId) - } -} -impl AzaleaWriteVar for MinecraftEntityId { - fn azalea_write_var(&self, buf: &mut impl io::Write) -> io::Result<()> { - i32::azalea_write_var(&self.0, buf) - } -} -impl Display for MinecraftEntityId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "eid({})", self.0) - } -} -impl From<i32> for MinecraftEntityId { - fn from(id: i32) -> Self { - Self(id) - } -} -impl From<u32> for MinecraftEntityId { - fn from(id: u32) -> Self { - Self(id as i32) - } -} +#[deprecated = "moved to `azalea_core::entity_id::MinecraftEntityId`."] +pub type MinecraftEntityId = azalea_core::entity_id::MinecraftEntityId; /// Keep track of certain metadatas that are only relevant for this partial /// world. @@ -129,7 +61,7 @@ pub struct PartialEntityInfos { /// /// This is used for shared worlds (i.e. swarms), to make sure we don't /// update entities twice on accident. - pub updates_received: IntMap<MinecraftEntityId, u32>, + pub updates_received: IntMap<azalea_core::entity_id::MinecraftEntityId, u32>, // ^ note: using MinecraftEntityId for entity ids is acceptable here since // there's no chance of collisions } @@ -164,7 +96,7 @@ pub struct Instance { /// instead use `azalea_entity::EntityIdIndex`, since some servers may /// give different entity IDs for the same entities to different /// players. - pub entity_by_id: IntMap<MinecraftEntityId, Entity>, + pub entity_by_id: IntMap<azalea_core::entity_id::MinecraftEntityId, Entity>, pub registries: RegistryHolder, } |
