Skip to content

Commit

Permalink
Continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Asurar0 committed Sep 17, 2024
1 parent d7476e1 commit 31b83f5
Showing 1 changed file with 70 additions and 53 deletions.
123 changes: 70 additions & 53 deletions pumpkin-world/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,33 @@
//! This module defines a minecraft chunk data strcture.
//!
// ========================= Imports =========================

use std::cmp::max;
use std::collections::HashMap;
use std::ops::Index;

use fastnbt::LongArray;
use pumpkin_core::math::vector2::Vector2;
use serde::{Deserialize, Serialize};

use pumpkin_core::math::vector2::Vector2;

use crate::{
block::BlockId,
coordinates::{ChunkRelativeBlockCoordinates, Height},
level::{ChunkNotGeneratedError, WorldError},
WORLD_HEIGHT,
};

// ======================== Constants ========================

const CHUNK_AREA: usize = 16 * 16;
const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16;
const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT;

pub struct ChunkData {
pub blocks: ChunkBlocks,
pub position: Vector2<i32>,
}

pub struct ChunkBlocks {
// TODO make this a Vec that doesn't store the upper layers that only contain air

// The packet relies on this ordering -> leave it like this for performance
/// Ordering: yzx (y being the most significant)
blocks: Box<[BlockId; CHUNK_VOLUME]>,

/// See `https://minecraft.fandom.com/wiki/Heightmap` for more info
pub heightmap: ChunkHeightmaps,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
struct PaletteEntry {
name: String,
properties: Option<HashMap<String, String>>,
}

#[derive(Deserialize, Debug, Clone)]
struct ChunkSectionBlockStates {
data: Option<LongArray>,
palette: Vec<PaletteEntry>,
}
// ======================== Structure ========================

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "UPPERCASE")]
pub struct ChunkHeightmaps {
motion_blocking: LongArray,
world_surface: LongArray,
}

#[derive(Deserialize, Debug)]
#[expect(dead_code)]
struct ChunkSection {
#[serde(rename = "Y")]
y: i32,
block_states: Option<ChunkSectionBlockStates>,
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct ChunkNbt {
#[expect(dead_code)]
Expand Down Expand Up @@ -104,11 +67,11 @@ struct ChunkNbt {
/// TODO
#[serde(rename = "fluid_ticks")]
#[serde(skip)]
fluid_ticks: Vec<()>,
fluid_ticks: (),
/// TODO
#[serde(rename = "block_ticks")]
#[serde(skip)]
block_ticks: Vec<()>,
block_ticks: (),
/// TODO
#[serde(skip)]
inhabited_time: i64,
Expand All @@ -118,28 +81,82 @@ struct ChunkNbt {
blending_data: ChunkBlendingData,
/// TODO
#[serde(skip)]
post_processing: Vec<()>,
post_processing: (),
/// TODO
#[serde(skip)]
structures: Vec<()>,
structures: (),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BlockEntity {}
pub struct BlockEntity {
// TODO
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ChunkLight {}
pub struct ChunkLight {
// TODO
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ChunkEntity {}
pub struct ChunkEntity {
// TODO
}

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct ChunkBlendingData {
min_section: i32,
max_section: i32,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]




pub struct ChunkData {
pub blocks: ChunkBlocks,
pub position: Vector2<i32>,
}

pub struct ChunkBlocks {
// TODO make this a Vec that doesn't store the upper layers that only contain air

// The packet relies on this ordering -> leave it like this for performance
/// Ordering: yzx (y being the most significant)
blocks: Box<[BlockId; CHUNK_VOLUME]>,

/// See `https://minecraft.fandom.com/wiki/Heightmap` for more info
pub heightmap: ChunkHeightmaps,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
struct PaletteEntry {
name: String,
properties: Option<HashMap<String, String>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ChunkSectionBlockStates {
data: Option<LongArray>,
palette: Vec<PaletteEntry>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "UPPERCASE")]
pub struct ChunkHeightmaps {
motion_blocking: LongArray,
world_surface: LongArray,
}

#[derive(Serialize, Deserialize, Debug)]
struct ChunkSection {
#[serde(rename = "Y")]
y: i32,
block_states: Option<ChunkSectionBlockStates>,
}


#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "Status")]
enum ChunkStatus {
#[serde(rename = "minecraft:empty")]
Expand Down

0 comments on commit 31b83f5

Please sign in to comment.