diff options
| -rwxr-xr-x | Cargo.lock | 3 | ||||
| -rwxr-xr-x | azalea-protocol/src/mc_buf/mod.rs | 6 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs | 6 | ||||
| -rw-r--r-- | azalea-world/Cargo.toml | 3 | ||||
| -rw-r--r-- | azalea-world/src/lib.rs | 118 |
5 files changed, 117 insertions, 19 deletions
@@ -164,6 +164,9 @@ dependencies = [ [[package]] name = "azalea-world" version = "0.1.0" +dependencies = [ + "azalea-protocol", +] [[package]] name = "bitflags" diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs index a82334fb..74d69441 100755 --- a/azalea-protocol/src/mc_buf/mod.rs +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -1,5 +1,11 @@ //! Utilities for reading and writing for the Minecraft protocol +// TODO: have a separate azalea-protocol-definitions crate to house everything in mc_buf +// We need to do this to prevent cyclic dependencies. +// For example with azalea-protocol depending on azalea-world, +// it could be changed to azalea-protocol depending on azalea-world +// and azalea-world depending on azalea-protocol-definitions. + mod read; mod write; diff --git a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs index abd936dc..0ab581ec 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs @@ -28,9 +28,3 @@ pub struct BlockEntity { } pub struct ChunkSection {} - -impl ClientboundLevelChunkPacketData { - pub fn read(world_height: u32) { - // let section_count - } -} diff --git a/azalea-world/Cargo.toml b/azalea-world/Cargo.toml index afae93a7..54326fb7 100644 --- a/azalea-world/Cargo.toml +++ b/azalea-world/Cargo.toml @@ -1,8 +1,9 @@ [package] +edition = "2021" name = "azalea-world" version = "0.1.0" -edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +azalea-protocol = {path = "../azalea-protocol"} diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index 20043bbf..e6b90ad5 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -1,3 +1,6 @@ +use azalea_protocol::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use std::io::{Read, Write}; + #[cfg(test)] mod tests { #[test] @@ -7,22 +10,92 @@ mod tests { } } +const SECTION_HEIGHT: u32 = 16; + pub struct Chunk { - sections: Vec<Section>, + pub sections: Vec<Section>, +} + +impl Chunk { + fn read_with_world_height(buf: &mut impl Read, world_height: u32) -> Result<Self, String> { + let section_count = world_height / SECTION_HEIGHT; + let mut sections = Vec::with_capacity(section_count as usize); + for _ in 0..section_count { + let section = Section::read_into(buf)?; + sections.push(section); + } + Ok(Chunk { sections }) + } } +impl McBufWritable for Chunk { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + for section in &self.sections { + section.write_into(buf)?; + } + Ok(()) + } +} + +#[derive(Clone, Debug)] pub struct Section { - states: PalettedContainer, - biomes: PalettedContainer, + pub block_count: u16, + pub states: PalettedContainer, + pub biomes: PalettedContainer, +} + +impl McBufReadable for Section { + fn read_into(buf: &mut impl Read) -> Result<Self, String> { + let block_count = u16::read_into(buf)?; + let states = PalettedContainer::read_into(buf)?; + let biomes = PalettedContainer::read_into(buf)?; + Ok(Section { + block_count, + states, + biomes, + }) + } +} + +impl McBufWritable for Section { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + self.block_count.write_into(buf)?; + self.states.write_into(buf)?; + self.biomes.write_into(buf)?; + Ok(()) + } } +#[derive(Clone, Debug)] pub struct PalettedContainer { - bits_per_entry: u8, - palette: Palette, + pub bits_per_entry: u8, + pub palette: Palette, /// Compacted list of indices pointing to entry IDs in the Palette. - data: Vec<i64>, + pub data: Vec<i64>, +} + +impl McBufReadable for PalettedContainer { + fn read_into(buf: &mut impl Read) -> Result<Self, String> { + let bits_per_entry = buf.read_byte()?; + let palette = Palette::read_with_bits_per_entry(buf, bits_per_entry)?; + let data = Vec::<i64>::read_into(buf)?; + Ok(PalettedContainer { + bits_per_entry, + palette, + data, + }) + } +} +impl McBufWritable for PalettedContainer { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + buf.write_byte(self.bits_per_entry)?; + self.palette.write_into(buf)?; + self.data.write_into(buf)?; + Ok(()) + } } +#[derive(Clone, Debug)] pub enum Palette { /// ID of the corresponding entry in its global palette SingleValue(u32), @@ -32,12 +105,33 @@ pub enum Palette { } impl Palette { - fn choose_palette_for_states(bits_per_entry: u8) -> &'static Palette { - match bits_per_entry { - 0 => &Palette::SingleValue, - 1..=4 => &Palette::LinearPalette, - 5..=8 => &Palette::HashmapPalette, - _ => &Palette::GlobalPalette, + pub fn read_with_bits_per_entry( + buf: &mut impl Read, + bits_per_entry: u8, + ) -> Result<Palette, String> { + Ok(match bits_per_entry { + 0 => Palette::SingleValue(u32::read_into(buf)?), + 1..=4 => Palette::LinearPalette(Vec::<u32>::read_into(buf)?), + 5..=8 => Palette::HashmapPalette(Vec::<u32>::read_into(buf)?), + _ => Palette::GlobalPalette, + }) + } +} + +impl McBufWritable for Palette { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + match self { + Palette::SingleValue(value) => { + value.write_into(buf)?; + } + Palette::LinearPalette(values) => { + values.write_into(buf)?; + } + Palette::HashmapPalette(values) => { + values.write_into(buf)?; + } + Palette::GlobalPalette => {} } + Ok(()) } } |
