From b271b36a36f3e72c8842205b4e44febd469ee020 Mon Sep 17 00:00:00 2001 From: Eamon Heffernan Date: Sun, 20 Oct 2024 05:15:55 +1100 Subject: [PATCH 1/3] Auto surface lighting --- pumpkin-protocol/src/bytebuf/mod.rs | 4 ++ .../src/client/play/c_chunk_data.rs | 44 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/mod.rs b/pumpkin-protocol/src/bytebuf/mod.rs index 3b46f2f9..139a52b4 100644 --- a/pumpkin-protocol/src/bytebuf/mod.rs +++ b/pumpkin-protocol/src/bytebuf/mod.rs @@ -209,6 +209,10 @@ impl ByteBuffer { self.put_list(v, |p, &v| p.put_var_int(&v.into())) } + pub fn put_u8_arr(&mut self, v: &[u8]) { + self.put_list(v, |p, &v| p.put_u8(v)) + } + /* pub fn get_nbt(&mut self) -> Option { match crab_nbt::NbtTag::deserialize(self.buf()) { Ok(v) => Some(v), diff --git a/pumpkin-protocol/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index 2fbbb571..e100743b 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -3,7 +3,11 @@ use std::collections::HashMap; use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt}; use itertools::Itertools; use pumpkin_macros::packet; -use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS}; +use pumpkin_world::{ + block::{Block, BlockState}, + chunk::ChunkData, + DIRECT_PALETTE_BITS, +}; #[packet(0x27)] pub struct CChunkData<'a>(pub &'a ChunkData); @@ -110,17 +114,39 @@ impl<'a> ClientPacket for CChunkData<'a> { // TODO: block entities buf.put_var_int(&VarInt(0)); - // TODO + // Sky Light Mask + // All of the chunks, this is not optimal and uses way more data than needed but will be + // overhauled with full lighting system. + buf.put_bit_set(&BitSet(VarInt(1), &[0b01111111111111111111111110])); + // Block Light Mask buf.put_bit_set(&BitSet(VarInt(1), &[0])); + // Empty Sky Light Mask + buf.put_bit_set(&BitSet(VarInt(1), &[0b0])); + // Empty Block Light Mask buf.put_bit_set(&BitSet(VarInt(1), &[0])); - buf.put_bit_set(&BitSet(VarInt(1), &[0])); - buf.put_bit_set(&BitSet(VarInt(1), &[0])); - // buf.put_bit_set(&BitSet(VarInt(0), vec![])); - // buf.put_bit_set(&BitSet(VarInt(0), vec![])); - // buf.put_bit_set(&BitSet(VarInt(0), vec![])); - // buf.put_bit_set(&BitSet(VarInt(0), vec![])); - buf.put_var_int(&VarInt(0)); + let mut lighting_subchunks = Vec::new(); + + self.0.blocks.iter_subchunks().for_each(|chunk| { + let mut chunk_light = [0u8; 2048]; + for (i, block) in chunk.into_iter().enumerate() { + if !block.is_air() { + continue; + } + let index = i / 2; + let mask = if i % 2 == 1 { 0xF0 } else { 0x0F }; + chunk_light[index] |= mask; + } + + lighting_subchunks.push(chunk_light); + }); + + buf.put_var_int(&lighting_subchunks.len().into()); + for subchunk in lighting_subchunks { + buf.put_u8_arr(&subchunk); + } + + // Block Lighting buf.put_var_int(&VarInt(0)); } } From ce9cdac3d5c0f05dc1ca28fe8d35b838837cc321 Mon Sep 17 00:00:00 2001 From: Eamon Heffernan Date: Sun, 20 Oct 2024 05:21:39 +1100 Subject: [PATCH 2/3] Remove unused import --- pumpkin-protocol/src/client/play/c_chunk_data.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pumpkin-protocol/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index e100743b..ef8e066a 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -3,11 +3,7 @@ use std::collections::HashMap; use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt}; use itertools::Itertools; use pumpkin_macros::packet; -use pumpkin_world::{ - block::{Block, BlockState}, - chunk::ChunkData, - DIRECT_PALETTE_BITS, -}; +use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS}; #[packet(0x27)] pub struct CChunkData<'a>(pub &'a ChunkData); From f942a65dfdd6fd58b40c3d3a72ff3c562a862fc1 Mon Sep 17 00:00:00 2001 From: Eamon Heffernan Date: Sun, 20 Oct 2024 05:25:14 +1100 Subject: [PATCH 3/3] use put_slice --- pumpkin-protocol/src/bytebuf/mod.rs | 4 ---- pumpkin-protocol/src/client/play/c_chunk_data.rs | 5 +++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/mod.rs b/pumpkin-protocol/src/bytebuf/mod.rs index 139a52b4..3b46f2f9 100644 --- a/pumpkin-protocol/src/bytebuf/mod.rs +++ b/pumpkin-protocol/src/bytebuf/mod.rs @@ -209,10 +209,6 @@ impl ByteBuffer { self.put_list(v, |p, &v| p.put_var_int(&v.into())) } - pub fn put_u8_arr(&mut self, v: &[u8]) { - self.put_list(v, |p, &v| p.put_u8(v)) - } - /* pub fn get_nbt(&mut self) -> Option { match crab_nbt::NbtTag::deserialize(self.buf()) { Ok(v) => Some(v), diff --git a/pumpkin-protocol/src/client/play/c_chunk_data.rs b/pumpkin-protocol/src/client/play/c_chunk_data.rs index ef8e066a..0a68eace 100644 --- a/pumpkin-protocol/src/client/play/c_chunk_data.rs +++ b/pumpkin-protocol/src/client/play/c_chunk_data.rs @@ -125,7 +125,7 @@ impl<'a> ClientPacket for CChunkData<'a> { self.0.blocks.iter_subchunks().for_each(|chunk| { let mut chunk_light = [0u8; 2048]; - for (i, block) in chunk.into_iter().enumerate() { + for (i, block) in chunk.iter().enumerate() { if !block.is_air() { continue; } @@ -139,7 +139,8 @@ impl<'a> ClientPacket for CChunkData<'a> { buf.put_var_int(&lighting_subchunks.len().into()); for subchunk in lighting_subchunks { - buf.put_u8_arr(&subchunk); + buf.put_var_int(&VarInt(subchunk.len() as i32)); + buf.put_slice(&subchunk); } // Block Lighting