mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
write some more azalea-world code
This commit is contained in:
parent
a24d00d998
commit
1e2ec61100
5 changed files with 118 additions and 20 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -164,6 +164,9 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "azalea-world"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"azalea-protocol",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -28,9 +28,3 @@ pub struct BlockEntity {
|
|||
}
|
||||
|
||||
pub struct ChunkSection {}
|
||||
|
||||
impl ClientboundLevelChunkPacketData {
|
||||
pub fn read(world_height: u32) {
|
||||
// let section_count
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"}
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue