add more stuff

This commit is contained in:
mat 2022-05-08 23:45:15 -05:00
parent 122693a654
commit 345cecf7af
7 changed files with 67 additions and 19 deletions

2
.gitignore vendored
View file

@ -4,8 +4,6 @@ flamegraph.svg
perf.data
perf.data.old
# TODO: remove this after chunk-decoding is merged
/login.txt
code-generator/Burger
code-generator/client.jar

View file

@ -177,7 +177,6 @@ impl Client {
match packet {
GamePacket::ClientboundLoginPacket(p) => {
println!("Got login packet {:?}", p);
std::fs::write("login.txt", format!("{:#?}", p)).expect("Unable to write file");
let mut state = state.lock().await;
state.player.entity.id = p.player_id;

View file

@ -23,6 +23,15 @@ impl ChunkPos {
}
}
impl From<BlockPos> for ChunkPos {
fn from(pos: BlockPos) -> Self {
ChunkPos {
x: pos.x / 16,
z: pos.z / 16,
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct ChunkSectionPos {
pub x: i32,
@ -35,3 +44,19 @@ impl ChunkSectionPos {
ChunkSectionPos { x, y, z }
}
}
impl From<BlockPos> for ChunkSectionPos {
fn from(pos: BlockPos) -> Self {
ChunkSectionPos {
x: pos.x / 16,
y: pos.y / 16,
z: pos.z / 16,
}
}
}
impl From<ChunkSectionPos> for ChunkPos {
fn from(pos: ChunkSectionPos) -> Self {
ChunkPos { x: pos.x, z: pos.z }
}
}

View file

@ -70,9 +70,9 @@ const MAGIC: [(i32, i32, i32); 64] = [
];
/// A compact list of integers with the given number of bits per entry.
#[derive(Clone)]
#[derive(Clone, Debug, Default)]
pub struct BitStorage {
data: Vec<u64>,
pub data: Vec<u64>,
bits: usize,
mask: u64,
size: usize,
@ -103,9 +103,17 @@ impl BitStorage {
/// Create a new BitStorage with the given number of bits per entry.
/// `size` is the number of entries in the BitStorage.
pub fn new(bits: usize, size: usize, data: Option<Vec<u64>>) -> Result<Self, BitStorageError> {
if let Some(data) = &data {
if data.len() == 0 {
// TODO: make 0 bit storage actually work
return Ok(BitStorage::default());
}
}
let values_per_long = 64 / bits;
let magic_index = values_per_long - 1;
let (divide_mul, divide_add, divide_shift) = MAGIC[magic_index as usize];
println!("values_per_long: {}, size: {}", values_per_long, size);
let calculated_length = (size + values_per_long - 1) / values_per_long;
let mask = (1 << bits) - 1;

View file

@ -67,6 +67,15 @@ impl IndexMut<&ChunkPos> for World {
&mut self.storage[pos]
}
}
// impl Index<&BlockPos> for World {
// type Output = Option<Arc<Mutex<Chunk>>>;
// fn index(&self, pos: &BlockPos) -> &Self::Output {
// let chunk = &self[ChunkPos::from(pos)];
// // chunk.
// }
// }
pub struct ChunkStorage {
view_center: ChunkPos,

View file

@ -1,6 +1,8 @@
use azalea_protocol::mc_buf::{McBufReadable, McBufVarReadable, McBufWritable, Readable, Writable};
use std::io::{Read, Write};
use crate::BitStorage;
#[derive(Clone, Debug, Copy)]
pub enum PalettedContainerType {
Biomes,
@ -12,7 +14,7 @@ pub struct PalettedContainer {
pub bits_per_entry: u8,
pub palette: Palette,
/// Compacted list of indices pointing to entry IDs in the Palette.
pub data: Vec<u64>,
pub storage: BitStorage,
}
impl PalettedContainer {
@ -29,17 +31,23 @@ impl PalettedContainer {
Palette::biomes_read_with_bits_per_entry(buf, bits_per_entry)?
}
};
let size = match type_ {
PalettedContainerType::BlockStates => 4096,
PalettedContainerType::Biomes => 64,
};
let data = Vec::<u64>::read_into(buf)?;
debug_assert!(
bits_per_entry != 0 || data.is_empty(),
"Bits per entry is 0 but data is not empty."
);
println!("data: {:?}", data);
let storage = BitStorage::new(bits_per_entry.into(), size, Some(data)).unwrap();
Ok(PalettedContainer {
bits_per_entry,
palette,
data,
storage,
})
}
}
@ -47,7 +55,7 @@ impl McBufWritable for PalettedContainer {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
buf.write_byte(self.bits_per_entry)?;
self.palette.write_into(buf)?;
self.data.write_into(buf)?;
self.storage.data.write_into(buf)?;
Ok(())
}
}

View file

@ -1,5 +1,5 @@
use azalea_client::{Account, Event};
use azalea_core::ChunkPos;
use azalea_core::{BlockPos, ChunkPos};
#[tokio::main]
async fn main() {
@ -19,18 +19,19 @@ async fn main() {
while let Some(e) = client.next().await {
match e {
// TODO: have a "loaded" or "ready" event that fires when all chunks are loaded
Event::Login => {
// let state = client.state.lock().await;
// let world = state.world.as_ref().unwrap();
// let c = world[&ChunkPos::new(-1, -4)]
// .as_ref()
// .unwrap()
// .lock()
// .unwrap();
// println!("{:?}", c);
}
Event::Login => {}
Event::Chat(p) => {
println!("{}", p.message.to_ansi(None));
if p.message.to_ansi(None) == "<py5> ok" {
let state = client.state.lock().await;
let world = state.world.as_ref().unwrap();
// let c = world[&BlockPos::new(5, 78, -2)]
// .as_ref()
// .unwrap()
// .lock()
// .unwrap();
// println!("{:?}", c);
}
}
}
}