veloren/common/src/terrain/structure.rs

151 lines
4.0 KiB
Rust
Raw Normal View History

2019-09-04 23:03:49 +00:00
use super::BlockKind;
use crate::{
assets::{self, Asset},
vol::{BaseVol, ReadVol, SizedVol, Vox, WriteVol},
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
volumes::dyna::{Dyna, DynaError},
};
use dot_vox::DotVoxData;
use std::{fs::File, io::BufReader};
use vek::*;
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
#[derive(Copy, Clone, PartialEq)]
2019-06-12 20:22:16 +00:00
pub enum StructureBlock {
None,
2019-06-12 20:22:16 +00:00
TemperateLeaves,
PineLeaves,
2019-07-08 19:28:48 +00:00
Acacia,
2019-09-01 19:04:03 +00:00
Mangrove,
2020-01-26 00:22:48 +00:00
PalmLeavesInner,
PalmLeavesOuter,
2019-08-15 13:50:46 +00:00
Water,
2019-08-15 14:23:14 +00:00
GreenSludge,
2019-07-09 16:08:43 +00:00
Fruit,
2019-10-09 19:28:05 +00:00
Chest,
2019-08-03 20:44:51 +00:00
Hollow,
2019-09-01 19:04:03 +00:00
Liana,
2019-08-18 14:33:16 +00:00
Normal(Rgb<u8>),
2019-06-12 20:22:16 +00:00
}
impl Vox for StructureBlock {
fn empty() -> Self { StructureBlock::None }
2019-06-12 20:22:16 +00:00
fn is_empty(&self) -> bool {
match self {
2019-08-18 14:33:16 +00:00
StructureBlock::None => true,
2019-06-12 20:22:16 +00:00
_ => false,
}
}
}
#[derive(Debug)]
pub enum StructureError {}
#[derive(Clone)]
pub struct Structure {
center: Vec3<i32>,
2019-06-12 20:22:16 +00:00
vol: Dyna<StructureBlock, ()>,
empty: StructureBlock,
default_kind: BlockKind,
}
impl Structure {
pub fn with_center(mut self, center: Vec3<i32>) -> Self {
self.center = center;
self
}
pub fn with_default_kind(mut self, kind: BlockKind) -> Self {
self.default_kind = kind;
self
}
pub fn get_bounds(&self) -> Aabb<i32> {
Aabb {
min: -self.center,
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
max: self.vol.size().map(|e| e as i32) - self.center,
}
}
pub fn default_kind(&self) -> BlockKind { self.default_kind }
}
impl BaseVol for Structure {
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
type Error = StructureError;
type Vox = StructureBlock;
}
impl ReadVol for Structure {
#[inline(always)]
2019-06-12 20:22:16 +00:00
fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
match self.vol.get(pos + self.center) {
Ok(block) => Ok(block),
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
Err(DynaError::OutOfBounds) => Ok(&self.empty),
}
}
}
impl Asset for Structure {
2019-08-06 06:31:48 +00:00
const ENDINGS: &'static [&'static str] = &["vox"];
2019-08-06 06:31:48 +00:00
fn parse(buf_reader: BufReader<File>) -> Result<Self, assets::Error> {
let dot_vox_data = DotVoxData::parse(buf_reader)?;
if let Some(model) = dot_vox_data.models.get(0) {
let palette = dot_vox_data
.palette
.iter()
.map(|col| Rgba::from(col.to_ne_bytes()).into())
.collect::<Vec<_>>();
let mut vol = Dyna::filled(
Vec3::new(model.size.x, model.size.y, model.size.z),
2019-06-12 20:22:16 +00:00
StructureBlock::empty(),
(),
);
for voxel in &model.voxels {
2019-06-12 20:22:16 +00:00
let block = match voxel.i {
0 => StructureBlock::TemperateLeaves,
1 => StructureBlock::PineLeaves,
2019-08-15 13:50:46 +00:00
3 => StructureBlock::Water,
2019-07-08 19:28:48 +00:00
4 => StructureBlock::Acacia,
2019-09-01 19:04:03 +00:00
5 => StructureBlock::Mangrove,
2019-08-15 14:23:14 +00:00
6 => StructureBlock::GreenSludge,
2019-07-09 16:08:43 +00:00
7 => StructureBlock::Fruit,
2019-09-01 19:04:03 +00:00
9 => StructureBlock::Liana,
2019-10-09 19:28:05 +00:00
10 => StructureBlock::Chest,
2020-01-26 00:22:48 +00:00
13 => StructureBlock::PalmLeavesOuter,
14 => StructureBlock::PalmLeavesInner,
2019-08-03 20:44:51 +00:00
15 => StructureBlock::Hollow,
2019-06-12 20:22:16 +00:00
index => {
2019-06-15 10:36:26 +00:00
let color = palette
.get(index as usize)
.copied()
.unwrap_or_else(|| Rgb::broadcast(0));
2019-08-18 14:33:16 +00:00
StructureBlock::Normal(color)
},
2019-06-12 20:22:16 +00:00
};
let _ = vol.set(
Vec3::new(voxel.x, voxel.y, voxel.z).map(|e| i32::from(e)),
2019-06-15 10:36:26 +00:00
block,
2019-06-12 20:22:16 +00:00
);
}
Ok(Structure {
center: Vec3::zero(),
vol,
2019-06-12 20:22:16 +00:00
empty: StructureBlock::empty(),
default_kind: BlockKind::Normal,
})
} else {
Ok(Self {
center: Vec3::zero(),
2019-06-12 20:22:16 +00:00
vol: Dyna::filled(Vec3::zero(), StructureBlock::empty(), ()),
empty: StructureBlock::empty(),
default_kind: BlockKind::Normal,
})
}
}
}