use crate::{ vol::{BaseVol, ReadVol, SampleVol, VolSize, WriteVol}, volumes::dyna::DynaErr, }; use fxhash::FxHashMap; use std::{collections::hash_map, fmt::Debug, marker::PhantomData, sync::Arc}; use vek::*; #[derive(Debug)] pub enum VolMap2dErr { NoSuchChunk, ChunkErr(V::Err), DynaErr(DynaErr), InvalidChunkSize, } // V = Voxel // S = Size (replace with a const when const generics is a thing) // M = Chunk metadata #[derive(Clone)] pub struct VolMap2d { chunks: FxHashMap, Arc>, phantom: PhantomData, } impl VolMap2d { #[inline(always)] pub fn chunk_key>>(pos: P) -> Vec2 { pos.into().map2(S::SIZE.into(), |e, sz: u32| { // Horrid, but it's faster than a cheetah with a red bull blood transfusion let log2 = (sz - 1).count_ones(); ((((i64::from(e) + (1 << 32)) as u64) >> log2) - (1 << (32 - log2))) as i32 }) } #[inline(always)] pub fn chunk_offs(pos: Vec3) -> Vec3 { let offs = pos.map2(S::SIZE, |e, sz| { // Horrid, but it's even faster than the aforementioned cheetah (((i64::from(e) + (1 << 32)) as u64) & u64::from(sz - 1)) as i32 }); Vec3::new(offs.x, offs.y, pos.z) } } impl BaseVol for VolMap2d { type Vox = V::Vox; type Err = VolMap2dErr; } impl ReadVol for VolMap2d { #[inline(always)] fn get(&self, pos: Vec3) -> Result<&V::Vox, VolMap2dErr> { let ck = Self::chunk_key(pos); self.chunks .get(&ck) .ok_or(VolMap2dErr::NoSuchChunk) .and_then(|chunk| { let co = Self::chunk_offs(pos); chunk.get(co).map_err(VolMap2dErr::ChunkErr) }) } #[inline(always)] unsafe fn get_unchecked(&self, pos: Vec3) -> &V::Vox { let ck = Self::chunk_key(pos); let co = Self::chunk_offs(pos); self.chunks.get(&ck).unwrap().get_unchecked(co) } } // TODO: This actually breaks the API: samples are supposed to have an offset of zero! // TODO: Should this be changed, perhaps? impl>, V: BaseVol + ReadVol + Debug, S: VolSize> SampleVol for VolMap2d { type Sample = VolMap2d; /// Take a sample of the terrain by cloning the voxels within the provided range. /// /// Note that the resultant volume does not carry forward metadata from the original chunks. fn sample(&self, range: I) -> Result> { let range = range.into(); let mut sample = VolMap2d::new()?; let chunk_min = Self::chunk_key(range.min); let chunk_max = Self::chunk_key(range.max); for x in chunk_min.x..=chunk_max.x { for y in chunk_min.y..=chunk_max.y { let chunk_key = Vec2::new(x, y); let chunk = self.get_key_arc(chunk_key).cloned(); if let Some(chunk) = chunk { sample.insert(chunk_key, chunk); } } } Ok(sample) } } impl WriteVol for VolMap2d { #[inline(always)] fn set(&mut self, pos: Vec3, vox: V::Vox) -> Result<(), VolMap2dErr> { let ck = Self::chunk_key(pos); self.chunks .get_mut(&ck) .ok_or(VolMap2dErr::NoSuchChunk) .and_then(|chunk| { let co = Self::chunk_offs(pos); Arc::make_mut(chunk) .set(co, vox) .map_err(VolMap2dErr::ChunkErr) }) } } impl VolMap2d { pub fn new() -> Result> { if Self::chunk_size() .map(|e| e.is_power_of_two() && e > 0) .reduce_and() { Ok(Self { chunks: FxHashMap::default(), phantom: PhantomData, }) } else { Err(VolMap2dErr::InvalidChunkSize) } } pub fn chunk_size() -> Vec2 { S::SIZE.into() } pub fn insert(&mut self, key: Vec2, chunk: Arc) -> Option> { self.chunks.insert(key, chunk) } pub fn get_key(&self, key: Vec2) -> Option<&V> { match self.chunks.get(&key) { Some(arc_chunk) => Some(arc_chunk.as_ref()), None => None, } } pub fn get_key_arc(&self, key: Vec2) -> Option<&Arc> { self.chunks.get(&key) } pub fn clear(&mut self) { self.chunks.clear(); } pub fn drain(&mut self) -> hash_map::Drain, Arc> { self.chunks.drain() } pub fn remove(&mut self, key: Vec2) -> Option> { self.chunks.remove(&key) } pub fn key_pos(&self, key: Vec2) -> Vec2 { key * Vec2::::from(S::SIZE).map(|e| e as i32) } pub fn pos_key(&self, pos: Vec3) -> Vec2 { Self::chunk_key(pos) } pub fn iter(&self) -> ChunkIter { ChunkIter { iter: self.chunks.iter(), } } } pub struct ChunkIter<'a, V: BaseVol> { iter: std::collections::hash_map::Iter<'a, Vec2, Arc>, } impl<'a, V: BaseVol> Iterator for ChunkIter<'a, V> { type Item = (Vec2, &'a Arc); fn next(&mut self) -> Option { self.iter.next().map(|(k, c)| (*k, c)) } }