Skip to content

Commit

Permalink
o
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Aug 21, 2023
1 parent 60d96a9 commit f124e0b
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 131 deletions.
28 changes: 27 additions & 1 deletion core/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,39 @@ pub trait Decode<'de> {
B: bytes::Buf;
}

pub trait NetSync: Encode {
fn read_buf<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
where
B: bytes::Buf;

fn write_buf<B>(&self, buf: &mut B) -> anyhow::Result<()>
where
B: bytes::BufMut,
{
self.encode(buf)
}
}

impl<T> NetSync for T
where
T: Encode + for<'de> Decode<'de, Output = T>,
{
fn read_buf<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
where
B: bytes::Buf,
{
*self = Self::decode(buf)?;
Ok(())
}
}

/// Layer for encoding and decoding in nbt binary format for packets.
pub struct Nbt<'a, T>(pub &'a T);

/// Layer for encoding and decoding in json utf8 for packets.
pub struct Json<'a, T>(pub &'a T);

mod packet_buf_impl {
mod packet_buf_imp {
use std::{hash::Hash, ops::Deref};

use crate::registry::{Registration, RegistryAccess};
Expand Down
4 changes: 2 additions & 2 deletions core/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<T> std::ops::Index<usize> for Registry<T> {
}

impl<T: PartialEq + Eq> crate::util::collections::Indexed<T> for Registry<T> {
fn get_raw_id(&self, value: &T) -> Option<usize> {
fn raw_id(&self, value: &T) -> Option<usize> {
self.entries
.iter()
.enumerate()
Expand All @@ -133,7 +133,7 @@ impl<T: PartialEq + Eq> crate::util::collections::Indexed<T> for Registry<T> {
}

impl<T: PartialEq + Eq> crate::util::collections::Indexed<Entry<T>> for Registry<T> {
fn get_raw_id(&self, value: &Entry<T>) -> Option<usize> {
fn raw_id(&self, value: &Entry<T>) -> Option<usize> {
self.entries
.iter()
.enumerate()
Expand Down
14 changes: 6 additions & 8 deletions core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ fn new_states<E: Clone, T: Deref<Target = State> + From<(E, State)>>(
temp = temp
.iter()
.flat_map(|list| {
unsafe { property.values_unchecked::<u8>() }
.into_iter()
.map(|i| {
let mut list2 = list.clone();
list2.push((property.clone(), i));
list2
})
property.values::<u8>().into_iter().map(|i| {
let mut list2 = list.clone();
list2.push((property.clone(), i));
list2
})
})
.collect()
}
Expand Down Expand Up @@ -280,7 +278,7 @@ impl StatesBuilder {
return Err(anyhow::anyhow!("Invalidly named property: {name}"));
}

if unsafe { property.values_unchecked::<u8>() }.len() <= 1 {
if property.values::<u8>().len() <= 1 {
return Err(anyhow::anyhow!(
"Attempted use property {name} with <= 1 possible values"
));
Expand Down
6 changes: 5 additions & 1 deletion core/src/state/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ impl Property {
}

pub fn values<T: From<u8> + std::any::Any>(&self) -> Vec<T> {
assert_eq!(self.type_id, std::any::TypeId::of::<T>());
assert!(
self.type_id == std::any::TypeId::of::<T>()
|| std::any::TypeId::of::<u8>() == std::any::TypeId::of::<T>()
);

unsafe { self.values_unchecked() }
}

Expand Down
8 changes: 6 additions & 2 deletions core/src/util/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const DEFAULT_INDEXED_INDEX: i32 = -1;

/// An extended version of [`std::ops::Index`].
pub trait Indexed<T> {
fn get_raw_id(&self, value: &T) -> Option<usize>;
fn raw_id(&self, value: &T) -> Option<usize>;
fn get(&self, index: usize) -> Option<&T>;
fn len(&self) -> usize;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<T: Hash + PartialEq + Eq + Clone> IdList<T> {
}

impl<T: Hash + PartialEq + Eq + Clone> Indexed<T> for IdList<T> {
fn get_raw_id(&self, value: &T) -> Option<usize> {
fn raw_id(&self, value: &T) -> Option<usize> {
self.id_map.get(value).copied().map(|e| e as usize)
}

Expand Down Expand Up @@ -501,3 +501,7 @@ mod tests_caches {
assert_eq!(caches.map.read().len(), 2);
}
}

pub trait Weighted {
fn weight(&self) -> u32;
}
2 changes: 1 addition & 1 deletion core/src/world/biome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type Shared<'w> = crate::Ref<'w, crate::registry::Entry<Biome>>;
pub struct SharedRegistry<'w>(pub crate::Ref<'w, crate::registry::Registry<Biome>>);

impl<'w> Indexed<Shared<'w>> for SharedRegistry<'w> {
fn get_raw_id(&self, value: &Shared<'w>) -> Option<usize> {
fn raw_id(&self, value: &Shared<'w>) -> Option<usize> {
self.0.iter().position(|entry| crate::Ref(entry) == *value)
}

Expand Down
58 changes: 51 additions & 7 deletions core/src/world/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ pub trait Chunk<'w>: super::Blocks + super::LightSources + std::any::Any {
fn remove_block_entity(&self, pos: BlockPos);
}

pub struct WorldChunk<'w> {
struct BaseChunk<'w> {
sections: Vec<Section<'w>>,
height_limit_view: *const dyn super::HeightLimit,
height_limit_view: &'static dyn super::HeightLimit,
heightmaps: Vec<(super::heightmap::Type, super::heightmap::Heightmap)>,
sky_light: light::ChunkSkyLight,
}

pub struct WorldChunk<'w> {
base: BaseChunk<'w>,
}

mod chunk_imp {
Expand All @@ -54,8 +60,8 @@ mod chunk_imp {
}

pub struct Section<'w> {
pub biome_container: palette::Container<'w, biome::Shared<'w>>,
pub block_state_container: palette::Container<'static, block::SharedBlockState>,
biome_container: palette::Container<'w, biome::Shared<'w>>,
block_state_container: palette::Container<'static, block::SharedBlockState>,

lock: std::sync::Mutex<()>,

Expand All @@ -74,7 +80,7 @@ impl<'w> Section<'w> {
),
biome_container: palette::Container::from_initialize(
biomes,
todo!(),
todo!("plains"),
palette::Provider::Biome,
),
lock: std::sync::Mutex::new(()),
Expand All @@ -84,7 +90,8 @@ impl<'w> Section<'w> {
}
}

pub fn get_block_state(&self, x: i32, y: i32, z: i32) -> Option<block::SharedBlockState> {
#[inline]
pub fn block_state(&self, x: i32, y: i32, z: i32) -> Option<block::SharedBlockState> {
self.block_state_container.get((x, y, z))
}

Expand All @@ -102,7 +109,7 @@ impl<'w> Section<'w> {
pos: (i32, i32, i32),
state: block::SharedBlockState,
) -> block::SharedBlockState {
let bs = unsafe { self.block_state_container.swap_unchecked(pos, state) };
let bs = self.block_state_container.swap_unchecked(pos, state);
let fs = bs.fluid_state();
let fs2 = state.fluid_state();

Expand Down Expand Up @@ -177,6 +184,16 @@ impl<'w> Section<'w> {
self.non_empty_fluid_count
.store(counter.non_empty_fluid_count, atomic::Ordering::Release);
}

#[inline]
pub fn block_state_container(&self) -> &palette::Container<'static, block::SharedBlockState> {
&self.block_state_container
}

#[inline]
pub fn biome_container(&self) -> &palette::Container<'w, biome::Shared<'w>> {
&self.biome_container
}
}

#[derive(Default)]
Expand Down Expand Up @@ -302,3 +319,30 @@ impl UpgradeData {
}
}
}

pub mod light {
pub struct ChunkSkyLight {
min_y: i32,
palette: super::palette::Storage,

bp1: crate::math::BlockPos,
bp2: crate::math::BlockPos,
}

impl ChunkSkyLight {
pub fn new(height_limit: &dyn crate::world::HeightLimit) -> Self {
let min_y = height_limit.bottom_y() - 1;

Self {
min_y,
palette: crate::collections::PackedArray::new(
crate::math::impl_helper::ceil_log_2(height_limit.top_y() - min_y + 1) as usize,
256,
None,
),
bp1: crate::math::BlockPos::ORIGIN,
bp2: crate::math::BlockPos::ORIGIN,
}
}
}
}
23 changes: 23 additions & 0 deletions core/src/world/gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fmt::Display;

pub struct StructConfig<'w> {
biomes: &'w crate::registry::Registry<super::biome::Biome>,
}

pub struct StructSpawns {
bounding_box: StructSpawnsBoundingBox,
}

pub enum StructSpawnsBoundingBox {
Piece,
Struct,
}

impl Display for StructSpawnsBoundingBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StructSpawnsBoundingBox::Piece => f.write_str("piece"),
StructSpawnsBoundingBox::Struct => f.write_str("full"),
}
}
}
3 changes: 3 additions & 0 deletions core/src/world/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub mod biome;
pub mod chunk;
pub mod gen;
pub mod palette;
pub mod tick;

use crate::prelude::*;

pub use heightmap::Heightmap;

/// A view with a height limit specification.
pub trait HeightLimit {
/// The difference in the [`Self::bottom_y`] and [`Self::top_y`] height.
Expand Down
Loading

0 comments on commit f124e0b

Please sign in to comment.