Introduce ChunkCoords type

This commit is contained in:
Matthias Schiffer 2023-01-27 21:21:09 +01:00
parent 0d8b989c10
commit 48e03aa266
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
3 changed files with 26 additions and 10 deletions

View file

@ -12,7 +12,7 @@ struct Args {
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
minedmap::io::region::from_file(&args.file)?.foreach_chunk(|x, z, value: fastnbt::Value| { minedmap::io::region::from_file(&args.file)?.foreach_chunk(|coords, value: fastnbt::Value| {
println!("Chunk({}, {}): {:#x?}", x.0, z.0, value); println!("Chunk {:?}: {:#x?}", coords, value);
}) })
} }

View file

@ -15,8 +15,7 @@ const BLOCKSIZE: usize = 4096;
#[derive(Debug)] #[derive(Debug)]
struct ChunkDesc { struct ChunkDesc {
x: ChunkX, coords: ChunkCoords,
z: ChunkZ,
len: u8, len: u8,
} }
@ -38,8 +37,10 @@ fn parse_header(header: &[u8; BLOCKSIZE]) -> HashMap<u32, ChunkDesc> {
map.insert( map.insert(
offset, offset,
ChunkDesc { ChunkDesc {
x: ChunkX(x), coords: ChunkCoords {
z: ChunkZ(z), x: ChunkX(x),
z: ChunkZ(z),
},
len, len,
}, },
); );
@ -85,7 +86,7 @@ impl<R: Read + Seek> Region<R> {
where where
R: Read + Seek, R: Read + Seek,
T: DeserializeOwned, T: DeserializeOwned,
F: FnMut(ChunkX, ChunkZ, T), F: FnMut(ChunkCoords, T),
{ {
let Region { mut reader } = self; let Region { mut reader } = self;
@ -102,13 +103,13 @@ impl<R: Read + Seek> Region<R> {
let mut seen = [[false; CHUNKS_PER_REGION as usize]; CHUNKS_PER_REGION as usize]; let mut seen = [[false; CHUNKS_PER_REGION as usize]; CHUNKS_PER_REGION as usize];
while !chunk_map.is_empty() { while !chunk_map.is_empty() {
let Some(ChunkDesc { x, z, len }) = chunk_map.remove(&index) else { let Some(ChunkDesc { coords, len }) = chunk_map.remove(&index) else {
reader.seek(SeekFrom::Current(BLOCKSIZE as i64)).context("Failed to seek chunk data")?; reader.seek(SeekFrom::Current(BLOCKSIZE as i64)).context("Failed to seek chunk data")?;
index += 1; index += 1;
continue; continue;
}; };
let chunk_seen = &mut seen[x.0 as usize][z.0 as usize]; let chunk_seen = &mut seen[coords.x.0 as usize][coords.z.0 as usize];
if *chunk_seen { if *chunk_seen {
bail!("Duplicate chunk"); bail!("Duplicate chunk");
} }
@ -119,7 +120,7 @@ impl<R: Read + Seek> Region<R> {
.read_exact(&mut buffer[..]) .read_exact(&mut buffer[..])
.context("Failed to read chunk data")?; .context("Failed to read chunk data")?;
f(x, z, decode_chunk(&buffer[..])?); f(coords, decode_chunk(&buffer[..])?);
index += len as u32; index += len as u32;
} }

View file

@ -1,3 +1,5 @@
use std::fmt::Debug;
pub const CHUNKS_PER_REGION: u8 = 32; pub const CHUNKS_PER_REGION: u8 = 32;
/// A chunk X coordinate relative to a region /// A chunk X coordinate relative to a region
@ -7,3 +9,16 @@ pub struct ChunkX(pub u8);
/// A chunk Z coordinate relative to a region /// A chunk Z coordinate relative to a region
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ChunkZ(pub u8); pub struct ChunkZ(pub u8);
/// A pair of chunk coordinates relative to a region
#[derive(Clone, Copy)]
pub struct ChunkCoords {
pub x: ChunkX,
pub z: ChunkZ,
}
impl Debug for ChunkCoords {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x.0, self.z.0)
}
}