Skip to content

Commit

Permalink
Add hardcoded default light
Browse files Browse the repository at this point in the history
  • Loading branch information
ya7on committed Dec 6, 2023
1 parent c9f62fc commit dce18bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
35 changes: 26 additions & 9 deletions mclib/protocol/src/packets/client/chunk_data_and_update_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ mod tests {
use crate::nbt::NBT;
use crate::packets::base::MCPacket;
use crate::packets::client::chunk_data_and_update_light::ChunkDataAndUpdateLight;
use crate::types::bitset::MCBitSet;
use crate::types::byte_array::MCByteArray;
use crate::types::long::MCLong;

#[test]
#[ignore]
fn test_chunk_data() {
let reference =
include_bytes!("../../../../../assets/tests/chunk_data_and_update_light.packet");
Expand Down Expand Up @@ -96,22 +96,39 @@ mod tests {
};
23
]);
let mut sky_light_mask = MCBitSet::default();
sky_light_mask.push(false);
sky_light_mask.push(true);
sky_light_mask.push(true);
let block_light_mask = MCBitSet::default();
let mut empty_sky_light_mask = MCBitSet::default();
empty_sky_light_mask.push(true);
let mut empty_block_light_mask = MCBitSet::default();
empty_block_light_mask.push(true);
empty_block_light_mask.push(true);
empty_block_light_mask.push(true);

let mut sky = vec![0.into(); 512];
sky.extend(vec![(0x0F | (0x0F << 4)).into(); 1536]);
let sky_lights = vec![sky, vec![(0x0F | (0x0F << 4)).into(); 2048]];

let block_lights = vec![];

let chunk_data_and_update_light = ChunkDataAndUpdateLight {
chunk_x: (-1 as i32).into(),
chunk_z: 0.into(),
heightmaps: heightmap.into(),
data: MCByteArray::new(ChunkData(chunks)),
block_entities: vec![],
sky_light_mask: Default::default(),
block_light_mask: Default::default(),
empty_sky_light_mask: Default::default(),
empty_block_light_mask: Default::default(),
sky_lights: vec![],
block_lights: vec![],
sky_light_mask,
block_light_mask,
empty_sky_light_mask,
empty_block_light_mask,
sky_lights,
block_lights,
};

let packed = &chunk_data_and_update_light.pack()[2..];
assert_eq!(packed[..2899], reference[..2899]);
panic!("{:?}", packed.len());
assert_eq!(packed, reference);
}
}
31 changes: 24 additions & 7 deletions mclib/protocol/src/types/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ use crate::utils::TcpUtils;
use std::io::Read;

#[derive(Debug, Clone, Default)]
pub struct MCBitSet(Vec<i64>);
pub struct MCBitSet(Vec<bool>);

impl MCBitSet {
pub fn push(&mut self, value: bool) {
self.0.push(value)
}
}

impl MCType for MCBitSet {
fn pack(&self) -> Vec<u8> {
let mut result = Vec::new();
result.extend(MCVarInt::from(self.0.len() as i32).pack());
for long in self.0.iter() {
result.extend(long.to_be_bytes());
let mut data = Vec::new();
for long in self.0.chunks(64) {
let mut num = 0_i64;
for (bit_index, bit) in long.iter().enumerate() {
num |= (*bit as i64) << bit_index;
}
data.extend(num.to_be_bytes());
}
let mut result = Vec::new();
result.extend(MCVarInt::from((data.len() / 8) as i32).pack());
result.extend(data);
result
}

fn unpack(src: &mut dyn Read) -> Self {
let len = MCVarInt::unpack(src);
let mut result = Vec::new();
for _ in 0..len.into() {
result.push(i64::from_be_bytes([
let mut num = i64::from_be_bytes([
src.read_byte(),
src.read_byte(),
src.read_byte(),
Expand All @@ -29,7 +41,12 @@ impl MCType for MCBitSet {
src.read_byte(),
src.read_byte(),
src.read_byte(),
]));
]);

for _ in 0..64 {
result.push((num & 0b1) == 1);
num >>= 1;
}
}
Self(result)
}
Expand Down
33 changes: 26 additions & 7 deletions src/server/thread/tcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mclib::packets::server::{
FinishConfigurationServerbound, Handshake, HandshakeNextState, LoginAcknowledged, LoginStart,
PingRequest, ServerboundKeelAlivePlay, SetPlayerPosition, StatusRequest,
};
use mclib::types::{MCByteArray, MCLong, MCPosition, MCVarInt};
use mclib::types::{MCBitSet, MCByteArray, MCLong, MCPosition, MCVarInt};
use mclib::MCPacket;

pub struct TCPListenerThread {
Expand Down Expand Up @@ -206,18 +206,37 @@ impl TCPListenerThread {
};
23
]);

let mut sky_light_mask = MCBitSet::default();
sky_light_mask.push(false);
sky_light_mask.push(true);
sky_light_mask.push(true);
let block_light_mask = MCBitSet::default();
let mut empty_sky_light_mask = MCBitSet::default();
empty_sky_light_mask.push(true);
let mut empty_block_light_mask = MCBitSet::default();
empty_block_light_mask.push(true);
empty_block_light_mask.push(true);
empty_block_light_mask.push(true);

let mut sky = vec![0.into(); 512];
sky.extend(vec![(0x0F | (0x0F << 4)).into(); 1536]);
let sky_lights = vec![sky, vec![(0x0F | (0x0F << 4)).into(); 2048]];

let block_lights = vec![];

let chunk_data_and_update_light = ChunkDataAndUpdateLight {
chunk_x: x.into(),
chunk_z: z.into(),
heightmaps: heightmap.into(),
data: MCByteArray::new(ChunkData(chunks)),
block_entities: vec![],
sky_light_mask: Default::default(),
block_light_mask: Default::default(),
empty_sky_light_mask: Default::default(),
empty_block_light_mask: Default::default(),
sky_lights: vec![],
block_lights: vec![],
sky_light_mask,
block_light_mask,
empty_sky_light_mask,
empty_block_light_mask,
sky_lights,
block_lights,
};
self.tcp_writer_api.send(TCPWriterAPI::SendMessageRaw {
uid: self.uid,
Expand Down

0 comments on commit dce18bb

Please sign in to comment.