Skip to content

Commit 2f4aa8a

Browse files
committed
Switched the chunk format to an enum for different palette types
1 parent 2b1082c commit 2f4aa8a

File tree

6 files changed

+337
-284
lines changed

6 files changed

+337
-284
lines changed

src/bin/src/systems/chunk_fetcher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl System for ChunkFetcher {
104104
}
105105
}
106106
}
107-
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
107+
tokio::time::sleep(std::time::Duration::from_millis(45)).await;
108108
}
109109
}
110110

src/bin/src/systems/chunk_sender.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl System for ChunkSenderSystem {
155155
}
156156
}
157157

158-
tokio::time::sleep(Duration::from_millis(5)).await;
158+
tokio::time::sleep(Duration::from_millis(45)).await;
159159
}
160160
}
161161

src/lib/core/src/chunks/chunk_receiver.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ impl ChunkReceiver {
3333

3434
pub fn queue_chunk_resend(&mut self, x: i32, z: i32, dimension: String) {
3535
if self.can_see.contains(&(x, z, dimension.clone())) {
36-
self.needed_chunks.insert((x, z, dimension), None);
36+
let entry = self.needed_chunks.get_mut(&(x, z, dimension.clone()));
37+
if let Some(entry) = entry {
38+
*entry = None;
39+
}
3740
}
3841
}
3942
}

src/lib/net/src/packets/incoming/player_action.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ impl IncomingPacket for PlayerAction {
3131
.world
3232
.load_chunk(self.location.x >> 4, self.location.z >> 4, "overworld")
3333
.await?;
34+
let block =
35+
chunk.get_block(self.location.x, self.location.y as i32, self.location.z)?;
36+
debug!("Block: {:?}", block);
3437
chunk.set_block(
3538
self.location.x,
3639
self.location.y as i32,
@@ -53,6 +56,7 @@ impl IncomingPacket for PlayerAction {
5356
{
5457
let q = state.universe.query::<&mut ChunkReceiver>();
5558
for (_, mut chunk_receiver) in q {
59+
debug!("Queueing chunk resend");
5660
chunk_receiver.queue_chunk_resend(
5761
self.location.x >> 4,
5862
self.location.z >> 4,

src/lib/net/src/packets/outgoing/chunk_and_light_data.rs

+31-40
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use ferrumc_macros::{packet, NetEncode};
44
use ferrumc_net_codec::net_types::bitset::BitSet;
55
use ferrumc_net_codec::net_types::length_prefixed_vec::LengthPrefixedVec;
66
use ferrumc_net_codec::net_types::var_int::VarInt;
7-
use ferrumc_world::chunk_format::{Chunk, Heightmaps};
7+
use ferrumc_world::chunk_format::{Chunk, Heightmaps, PaletteType};
88
use std::io::{Cursor, Write};
99
use std::ops::Not;
10-
use tracing::{trace, warn};
10+
use tracing::{debug, trace, warn};
1111

1212
const SECTIONS: usize = 24; // Number of sections, adjust for your Y range (-64 to 319)
1313

@@ -64,7 +64,7 @@ impl ChunkAndLightData {
6464
}
6565

6666
pub fn from_chunk(chunk: &Chunk) -> Result<Self, NetError> {
67-
let mut data = Cursor::new(Vec::new());
67+
let mut raw_data = Cursor::new(Vec::new());
6868
let mut sky_light_data = Vec::new();
6969
let mut block_light_data = Vec::new();
7070
for section in &chunk.sections {
@@ -89,49 +89,40 @@ impl ChunkAndLightData {
8989
};
9090
block_light_data.push(section_block_light_data);
9191

92-
data.write_u16::<BigEndian>(section.block_states.non_air_blocks)?;
92+
raw_data.write_u16::<BigEndian>(section.block_states.non_air_blocks)?;
9393

94-
let bits_per_block = section.block_states.bits_per_block;
95-
data.write_u8(bits_per_block)?;
96-
// If bits_per_block is 0, the section is using the single-value palette format
97-
// If bits_per_block is greater than 0, the section is using the indirect palette format
98-
if bits_per_block > 0 {
99-
// Write the palette
100-
VarInt::new(section.block_states.palette.len() as i32).write(&mut data)?;
101-
for palette_entry in &section.block_states.palette {
102-
palette_entry.write(&mut data)?;
94+
match &section.block_states.block_data {
95+
PaletteType::Single(val) => {
96+
debug!("Single palette type: {:?}", (chunk.x, chunk.z));
97+
raw_data.write_u8(0)?;
98+
val.write(&mut raw_data)?;
99+
VarInt::new(0).write(&mut raw_data)?;
103100
}
104-
105-
// Write the data
106-
VarInt::new(section.block_states.data.len() as i32).write(&mut data)?;
107-
for data_entry in &section.block_states.data {
108-
data.write_i64::<BigEndian>(*data_entry)?;
109-
}
110-
} else {
111-
// The 0s for air blocks and bits_per_block are already written
112-
// Get the only palette entry
113-
match section.block_states.palette.first() {
114-
Some(palette_entry) => {
115-
palette_entry.write(&mut data)?;
101+
PaletteType::Indirect {
102+
bits_per_block,
103+
data,
104+
palette,
105+
} => {
106+
debug!("Indirect palette type: {:?}", (chunk.x, chunk.z));
107+
raw_data.write_u8(*bits_per_block)?;
108+
VarInt::new(palette.len() as i32).write(&mut raw_data)?;
109+
for palette_entry in palette {
110+
palette_entry.write(&mut raw_data)?;
116111
}
117-
// If there is no palette entry, write a 0 (air) and log a warning
118-
None => {
119-
VarInt::new(0).write(&mut data)?;
120-
trace!(
121-
"No palette entry found for section at {}, {}, {}",
122-
chunk.x,
123-
section.y,
124-
chunk.z
125-
);
112+
VarInt::new(data.len() as i32).write(&mut raw_data)?;
113+
for data_entry in data {
114+
raw_data.write_i64::<BigEndian>(*data_entry)?;
126115
}
127116
}
128-
// Write the empty data section's length (0)
129-
VarInt::new(0).write(&mut data)?;
117+
PaletteType::Direct { .. } => {
118+
todo!("Direct palette type")
119+
}
130120
}
121+
131122
// Empty biome data for now
132-
data.write_u8(0)?;
133-
data.write_u8(0)?;
134-
data.write_u8(0)?;
123+
raw_data.write_u8(0)?;
124+
raw_data.write_u8(0)?;
125+
raw_data.write_u8(0)?;
135126
}
136127
let mut sky_light_mask = BitSet::new(SECTIONS + 2);
137128
let mut block_light_mask = BitSet::new(SECTIONS + 2);
@@ -168,7 +159,7 @@ impl ChunkAndLightData {
168159
chunk_x: chunk.x,
169160
chunk_z: chunk.z,
170161
heightmaps: chunk.heightmaps.serialize_as_network(),
171-
data: LengthPrefixedVec::new(data.into_inner()),
162+
data: LengthPrefixedVec::new(raw_data.into_inner()),
172163
block_entities: LengthPrefixedVec::new(Vec::new()),
173164
sky_light_mask,
174165
block_light_mask,

0 commit comments

Comments
 (0)