2019-09-04 23:03:49 +00:00
|
|
|
use super::BlockKind;
|
2019-05-24 11:08:38 +00:00
|
|
|
use crate::{
|
2019-06-06 14:48:41 +00:00
|
|
|
assets::{self, Asset},
|
2019-07-08 14:51:38 +00:00
|
|
|
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},
|
2019-05-24 11:08:38 +00:00
|
|
|
};
|
2019-05-25 05:54:47 +00:00
|
|
|
use dot_vox::DotVoxData;
|
2019-08-06 06:31:48 +00:00
|
|
|
use std::fs::File;
|
2019-09-04 23:03:49 +00:00
|
|
|
use std::io::BufReader;
|
2019-05-25 05:54:47 +00:00
|
|
|
use vek::*;
|
2019-05-24 11:08:38 +00:00
|
|
|
|
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 {
|
2019-08-16 17:23:58 +00:00
|
|
|
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,
|
2019-06-12 20:22:16 +00:00
|
|
|
PalmLeaves,
|
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-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 {
|
2019-08-16 17:23:58 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:08:38 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StructureError {}
|
|
|
|
|
2019-05-24 12:25:31 +00:00
|
|
|
#[derive(Clone)]
|
2019-05-24 11:08:38 +00:00
|
|
|
pub struct Structure {
|
|
|
|
center: Vec3<i32>,
|
2019-06-12 20:22:16 +00:00
|
|
|
vol: Dyna<StructureBlock, ()>,
|
|
|
|
empty: StructureBlock,
|
2019-08-16 17:23:58 +00:00
|
|
|
default_kind: BlockKind,
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Structure {
|
|
|
|
pub fn with_center(mut self, center: Vec3<i32>) -> Self {
|
|
|
|
self.center = center;
|
|
|
|
self
|
|
|
|
}
|
2019-07-08 14:51:38 +00:00
|
|
|
|
2019-08-16 17:23:58 +00:00
|
|
|
pub fn with_default_kind(mut self, kind: BlockKind) -> Self {
|
|
|
|
self.default_kind = kind;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-07-08 14:51:38 +00:00
|
|
|
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,
|
2019-07-08 14:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 17:23:58 +00:00
|
|
|
|
|
|
|
pub fn default_kind(&self) -> BlockKind {
|
|
|
|
self.default_kind
|
|
|
|
}
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseVol for Structure {
|
2019-06-12 20:22:16 +00:00
|
|
|
type Vox = StructureBlock;
|
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;
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadVol for Structure {
|
|
|
|
#[inline(always)]
|
2019-06-12 20:22:16 +00:00
|
|
|
fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
|
2019-05-24 12:25:31 +00:00
|
|
|
match self.vol.get(pos + self.center) {
|
2019-05-24 11:08:38 +00:00
|
|
|
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),
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Asset for Structure {
|
2019-08-06 06:31:48 +00:00
|
|
|
const ENDINGS: &'static [&'static str] = &["vox"];
|
|
|
|
fn parse(buf_reader: BufReader<File>) -> Result<Self, assets::Error> {
|
|
|
|
let dot_vox_data = DotVoxData::parse(buf_reader)?;
|
2019-05-24 11:08:38 +00:00
|
|
|
|
|
|
|
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<_>>();
|
|
|
|
|
2019-05-25 05:54:47 +00:00
|
|
|
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(),
|
2019-05-25 05:54:47 +00:00
|
|
|
(),
|
|
|
|
);
|
2019-05-24 11:08:38 +00:00
|
|
|
|
|
|
|
for voxel in &model.voxels {
|
2019-06-12 20:22:16 +00:00
|
|
|
let block = match voxel.i {
|
|
|
|
0 => StructureBlock::TemperateLeaves,
|
|
|
|
1 => StructureBlock::PineLeaves,
|
|
|
|
2 => StructureBlock::PalmLeaves,
|
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-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()
|
2019-07-01 20:42:43 +00:00
|
|
|
.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(
|
2019-07-01 20:42:43 +00:00
|
|
|
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
|
|
|
);
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Structure {
|
|
|
|
center: Vec3::zero(),
|
|
|
|
vol,
|
2019-06-12 20:22:16 +00:00
|
|
|
empty: StructureBlock::empty(),
|
2019-08-16 17:23:58 +00:00
|
|
|
default_kind: BlockKind::Normal,
|
2019-05-24 11:08:38 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(Self {
|
|
|
|
center: Vec3::zero(),
|
2019-06-12 20:22:16 +00:00
|
|
|
vol: Dyna::filled(Vec3::zero(), StructureBlock::empty(), ()),
|
|
|
|
empty: StructureBlock::empty(),
|
2019-08-16 17:23:58 +00:00
|
|
|
default_kind: BlockKind::Normal,
|
2019-05-24 11:08:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|