veloren/common/src/volumes/vol_map.rs

165 lines
4.4 KiB
Rust
Raw Normal View History

2019-01-02 19:22:01 +00:00
// Standard
use std::collections::HashMap;
// Library
use vek::*;
// Crate
use crate::{
vol::{
2019-01-14 18:49:53 +00:00
Vox,
2019-01-02 19:22:01 +00:00
BaseVol,
2019-01-14 18:49:53 +00:00
SizedVol,
2019-01-02 19:22:01 +00:00
ReadVol,
2019-01-14 18:49:53 +00:00
SampleVol,
2019-01-02 19:22:01 +00:00
WriteVol,
VolSize,
},
2019-01-14 18:49:53 +00:00
volumes::{
chunk::{Chunk, ChunkErr},
dyna::{Dyna, DynaErr},
},
2019-01-02 19:22:01 +00:00
};
#[derive(Debug)]
2019-01-02 19:22:01 +00:00
pub enum VolMapErr {
NoSuchChunk,
ChunkErr(ChunkErr),
2019-01-14 18:49:53 +00:00
DynaErr(DynaErr),
2019-01-02 19:22:01 +00:00
}
// V = Voxel
// S = Size (replace with a const when const generics is a thing)
// M = Chunk metadata
2019-01-14 18:49:53 +00:00
pub struct VolMap<V: Vox, S: VolSize, M> {
2019-01-02 19:22:01 +00:00
chunks: HashMap<Vec3<i32>, Chunk<V, S, M>>,
}
2019-01-14 18:49:53 +00:00
impl<V: Vox, S: VolSize, M> VolMap<V, S, M> {
2019-01-02 19:22:01 +00:00
#[inline(always)]
fn chunk_key(pos: Vec3<i32>) -> Vec3<i32> {
pos.map2(S::SIZE, |e, sz| e.div_euclid(sz as i32))
}
#[inline(always)]
fn chunk_offs(pos: Vec3<i32>) -> Vec3<i32> {
pos.map2(S::SIZE, |e, sz| e.rem_euclid(sz as i32))
}
}
2019-01-14 18:49:53 +00:00
impl<V: Vox, S: VolSize, M> BaseVol for VolMap<V, S, M> {
2019-01-02 19:22:01 +00:00
type Vox = V;
type Err = VolMapErr;
}
2019-01-14 18:49:53 +00:00
impl<V: Vox, S: VolSize, M> ReadVol for VolMap<V, S, M> {
2019-01-02 19:22:01 +00:00
#[inline(always)]
fn get(&self, pos: Vec3<i32>) -> Result<&V, VolMapErr> {
let ck = Self::chunk_key(pos);
self.chunks.get(&ck)
.ok_or(VolMapErr::NoSuchChunk)
.and_then(|chunk| {
let co = Self::chunk_offs(pos);
chunk.get(co).map_err(|err| VolMapErr::ChunkErr(err))
})
}
}
2019-01-14 18:49:53 +00:00
impl<V: Vox + Clone, S: VolSize, M> SampleVol for VolMap<V, S, M> {
type Sample = Dyna<V, ()>;
/// 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: Aabb<i32>) -> Result<Self::Sample, VolMapErr> {
2019-01-23 20:01:58 +00:00
// Return early if we don't have all the needed chunks that we need!
/*
2019-01-23 20:01:58 +00:00
let min_chunk = Self::chunk_key(range.min);
let max_chunk = Self::chunk_key(range.max - Vec3::one());
for x in min_chunk.x..=max_chunk.x {
for y in min_chunk.y..=max_chunk.y {
for z in min_chunk.z..=max_chunk.z {
if self.chunks.get(&Vec3::new(x, y, z)).is_none() {
return Err(VolMapErr::NoSuchChunk);
}
}
}
}
*/
2019-01-23 20:01:58 +00:00
2019-01-14 18:49:53 +00:00
let mut sample = Dyna::filled(
range.size().map(|e| e as u32).into(),
V::empty(),
(),
);
for pos in sample.iter_positions() {
sample.set(pos, self.get(range.min + pos).map(|v| v.clone()).unwrap_or(V::empty()))
2019-01-14 18:49:53 +00:00
.map_err(|err| VolMapErr::DynaErr(err))?;
}
Ok(sample)
}
}
impl<V: Vox, S: VolSize, M> WriteVol for VolMap<V, S, M> {
2019-01-02 19:22:01 +00:00
#[inline(always)]
fn set(&mut self, pos: Vec3<i32>, vox: V) -> Result<(), VolMapErr> {
let ck = Self::chunk_key(pos);
self.chunks.get_mut(&ck)
.ok_or(VolMapErr::NoSuchChunk)
.and_then(|chunk| {
let co = Self::chunk_offs(pos);
chunk.set(co, vox).map_err(|err| VolMapErr::ChunkErr(err))
})
}
}
2019-01-14 18:49:53 +00:00
impl<V: Vox, S: VolSize, M> VolMap<V, S, M> {
2019-01-02 19:22:01 +00:00
pub fn new() -> Self {
Self {
chunks: HashMap::new(),
}
}
2019-01-15 15:13:11 +00:00
2019-01-23 20:01:58 +00:00
pub fn chunk_size() -> Vec3<u32> { S::SIZE }
2019-01-15 15:13:11 +00:00
pub fn insert(&mut self, key: Vec3<i32>, chunk: Chunk<V, S, M>) -> Option<Chunk<V, S, M>> {
self.chunks.insert(key, chunk)
}
pub fn get_key(&self, key: Vec3<i32>) -> Option<&Chunk<V, S, M>> {
self.chunks.get(&key)
}
pub fn remove(&mut self, key: Vec3<i32>) -> Option<Chunk<V, S, M>> {
self.chunks.remove(&key)
2019-01-15 15:13:11 +00:00
}
pub fn key_pos(&self, key: Vec3<i32>) -> Vec3<i32> {
key * S::SIZE.map(|e| e as i32)
}
pub fn pos_key(&self, pos: Vec3<i32>) -> Vec3<i32> {
Self::chunk_key(pos)
}
pub fn iter<'a>(&'a self) -> ChunkIter<'a, V, S, M> {
ChunkIter {
iter: self.chunks.iter(),
}
}
}
pub struct ChunkIter<'a, V: Vox, S: VolSize, M> {
iter: std::collections::hash_map::Iter<'a, Vec3<i32>, Chunk<V, S, M>>,
}
impl<'a, V: Vox, S: VolSize, M> Iterator for ChunkIter<'a, V, S, M> {
type Item = (Vec3<i32>, &'a Chunk<V, S, M>);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(k, c)| (*k, c))
}
2019-01-02 19:22:01 +00:00
}