Skip to content

Instantly share code, notes, and snippets.

@guybrush
Created May 30, 2018 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guybrush/bb012ce3f7f4ae4090a3243bad455065 to your computer and use it in GitHub Desktop.
Save guybrush/bb012ce3f7f4ae4090a3243bad455065 to your computer and use it in GitHub Desktop.
pub fn pack_to(&mut self, chunk: &Chunk, buf: &mut Vec<u8>) {
let pow2_16 = 2u32.pow(16);
let mut last_v: u8 = chunk.data[0];
let mut i = 1;
let mut r = 1;
let mut v: u8 = 0;
let mut i_8 = 0;
let mut i_16 = 0;
let mut i_32 = 0;
let mut i_16 = 0;
let mut i_32 = 0;
self.packed_chunk.values[0] = last_v;
for u in 1..CHUNK_LENGTH_INT {
v = chunk.data[u as usize];
if v == last_v {
r = r + 1;
} else {
last_v = v;
if r < 256 {
self.packed_chunk.ranges_8[i_8] = r as u8;
i_8 = i_8 + 1;
} else if r < pow2_16 {
self.packed_chunk.indices_16[i_16] = (i - 1) as u32;
self.packed_chunk.ranges_16[i_16] = r as u16;
i_16 = i_16 + 1;
} else {
self.packed_chunk.indices_32[i_32] = (i - 1) as u32;
self.packed_chunk.ranges_32[i_32] = r as u32;
i_32 = i_32 + 1;
}
self.packed_chunk.values[i] = v;
i = i + 1;
r = 1;
}
}
if r < 256 {
self.packed_chunk.ranges_8[i_8] = r as u8;
i_8 = i_8 + 1;
} else if r < pow2_16 {
self.packed_chunk.indices_16[i_16] = (i - 1) as u32;
self.packed_chunk.ranges_16[i_16] = r as u16;
i_16 = i_16 + 1;
} else {
self.packed_chunk.indices_32[i_32] = (i - 1) as u32;
self.packed_chunk.ranges_32[i_32] = r as u32;
i_32 = i_32 + 1;
}
self.packed_chunk.n_ranges_all = (i_8 + i_16 + i_32) as u32;
self.packed_chunk.n_ranges_16 = i_16 as u32;
self.packed_chunk.n_ranges_32 = i_32 as u32;
self.packed_chunk.shape_x = 128;
self.packed_chunk.shape_y = 128;
self.packed_chunk.shape_z = 128;
let buf_len_header = 1 * 2 + 3 * 2 + 1 * 4 + 1 * 4 + i_32 * 4 + i_16 * 4;
let buf_len_data = i_32 * 4 + i_16 * 2 + i_8 * 1 + 1 * i;
// let buf_len_all = buf_len_header + buf_len_data;
// let mut buf = Vec::with_capacity(buf_len_all);
// let mut buf = BytesMut::with_capacity(buf_len_all);
buf.clear();
buf.put_u16_le(self.packed_chunk.version);
buf.put_u16_le(self.packed_chunk.shape_x);
buf.put_u16_le(self.packed_chunk.shape_y);
buf.put_u16_le(self.packed_chunk.shape_z);
buf.put_u32_le(self.packed_chunk.n_ranges_all);
buf.put_u32_le(self.packed_chunk.n_ranges_32);
buf.put_u32_le(self.packed_chunk.n_ranges_16);
if i_32 > 0 {
for x in self.packed_chunk.indices_32[..i_32].iter() {
buf.put_u32_le(*x);
}
}
if i_16 > 0 {
for x in self.packed_chunk.indices_16[..i_16].iter() {
buf.put_u32_le(*x);
}
}
if i_32 > 0 {
for x in self.packed_chunk.ranges_32[..i_32].iter() {
buf.put_u32_le(*x);
}
}
if i_16 > 0 {
for x in self.packed_chunk.ranges_16[..i_16].iter() {
buf.put_u16_le(*x);
}
}
if i_8 > 0 {
for x in self.packed_chunk.ranges_8[..i_8].iter() {
buf.put_u8(*x);
}
}
for x in self.packed_chunk.values[..i].iter() {
buf.put_u8(*x);
}
// println!("pack: {}, {}, {}, {}, {}, {}, {}",self.packed_chunk.version,self.packed_chunk.shape_x,self.packed_chunk.shape_y,self.packed_chunk.shape_z,self.packed_chunk.n_ranges_all,self.packed_chunk.n_ranges_32,self.packed_chunk.n_ranges_16);
// println!("pack: values: {}, {}, {}",self.packed_chunk.values[0],self.packed_chunk.values[1],self.packed_chunk.values[2],);
// let j = buf_len_header;
// if i_32 > 0 { buf[j..j+i_32*4].copy_from_slice(self.packed_chunk.indices_32.as_slice()[..i_32]); j = j + i_32*4; }
// if i_16 > 0 { buf[j..j+i_16*4].copy_from_slice(self.packed_chunk.indices_16[..i_16*4]); j = j + i_16*4; }
// if i_32 > 0 { buf[j..j+i_32*4].copy_from_slice(self.packed_chunk.ranges_32 [..i_32*4]); j = j + i_32*4; }
// if i_16 > 0 { buf[j..j+i_16*2].copy_from_slice(self.packed_chunk.ranges_16 [..i_16*2]); j = j + i_16*2; }
// if i_8 > 0 { buf[j..j+i_8 ].copy_from_slice(self.packed_chunk.ranges_8 [..i_8 ]); j = j + i_8 ; }
// buf[j..j+i];
// assert!(buf_len_all == j+i);
// let mut chain = Chain::new(&buf);
// if i_32 > 0 {
// buf.chain(Bytes::from(&packed.indices_32[..i_32]))
// }
// .chain(Bytes::from(&packed.indices_16[..i_16]))
// .chain(Bytes::from(&packed.ranges_32[..i_32]))
// .chain(Bytes::from(&packed.ranges_16[..i_16]))
// .chain(Bytes::from(&packed.ranges_8[..i_8]))
// .chain(Bytes::from(&packed.values[..i]));
// buf.put(&packed.indices_32);
// buf.put(&packed.indices_16);
// buf.put(&packed.ranges_32);
// buf.put(&packed.ranges_16);
// buf.put(&packed.ranges_8);
// buf.put(&packed.values);
// &buf[..buf_len_all]
// buf_len_all as usize
// buf
}
@guybrush
Copy link
Author

warning: variable does not need to be mutable
   --> src/chunk_manager.rs:259:13
    |
259 |         let mut i_32 = 0;
    |             ----^^^^
    |             |
    |             help: remove this `mut`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment