NOW IT FULLY WORKS

This commit is contained in:
lukas0008 committed 2024-08-12 20:23:15 +02:00
1 parent a991ce214b
commit eaf806a928
4 files changed
+87 -52

No files matched your search

@@ -4,7 +4,7 @@ use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt};
use fastnbt::LongArray;
use itertools::Itertools;
use pumpkin_macros::packet;
use pumpkin_world::chunk::ChunkData;
use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS};
#[packet(0x27)]
pub struct CChunkData<'a>(pub &'a ChunkData);
@@ -37,45 +37,66 @@ impl<'a> ClientPacket for CChunkData<'a> {
// Block count
data_buf.put_i16(block_count);
//// Block states
let palette = chunk.clone().into_iter().dedup().collect_vec();
let mut palette_map = HashMap::new();
let block_size = 8;
// let block_size = {
// let palette_bit_len = palette.len().leading_zeros();
// if palette_bit_len > 3 {
// palette_bit_len
// } else {
// 4
// }
// };
// Bits per entry
data_buf.put_u8(block_size as u8);
// Palette length
data_buf.put_var_int(&VarInt(palette.len() as i32));
palette.iter().enumerate().for_each(|(i, id)| {
palette_map.insert(*id, i);
// Palette
data_buf.put_var_int(&VarInt(**id as i32));
});
// TODO: make dynamic block_size work
// TODO: make direct block_size work
enum PaletteType {
Indirect(u32),
Direct,
}
let palette_type = {
let palette_bit_len = 64 - (palette.len() as i64 - 1).leading_zeros();
if palette_bit_len > 8 {
PaletteType::Direct
} else if palette_bit_len > 3 {
PaletteType::Indirect(palette_bit_len)
} else {
PaletteType::Indirect(4)
}
// TODO: fix indirect palette to work correctly
// PaletteType::Direct
};
let mut block_data_array = Vec::new();
for block_clump in chunk.chunks(64 / block_size as usize) {
let mut out_long: i64 = 0;
let mut first = true;
for block in block_clump {
if first {
first = false;
} else {
out_long = out_long << block_size;
match palette_type {
PaletteType::Indirect(block_size) => {
// Bits per entry
data_buf.put_u8(block_size as u8);
// Palette length
data_buf.put_var_int(&VarInt(palette.len() as i32));
let mut palette_map = HashMap::new();
palette.iter().enumerate().for_each(|(i, id)| {
palette_map.insert(**id, i);
// Palette
data_buf.put_var_int(&VarInt(**id as i32));
});
for block_clump in chunk.chunks(64 / block_size as usize) {
let mut out_long: i64 = 0;
for block in block_clump.iter().rev() {
let index = palette_map
.get(*block)
.expect("Its just got added, ofc it should be there");
out_long = out_long << block_size | (*index as i64);
}
block_data_array.push(out_long);
}
let index = palette_map
.get(block)
.expect("Its just got added, ofc it should be there");
out_long = out_long | *index as i64;
}
PaletteType::Direct => {
// Bits per entry
data_buf.put_u8(DIRECT_PALETTE_BITS as u8);
for block_clump in chunk.chunks(64 / DIRECT_PALETTE_BITS as usize) {
let mut out_long: i64 = 0;
for block in block_clump.iter().rev() {
out_long = out_long << DIRECT_PALETTE_BITS | (**block as i64);
}
block_data_array.push(out_long);
}
}
block_data_array.push(out_long);
}
// Data array length
// TODO: precompute this and omit making the `block_data_array`
data_buf.put_var_int(&VarInt(block_data_array.len() as i32));
// Data array
for data_int in block_data_array {