2021-02-28 23:34:36 +00:00
|
|
|
use common::{
|
|
|
|
terrain::Block,
|
|
|
|
store::{Id, Store},
|
|
|
|
};
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
pub enum Primitive {
|
|
|
|
Empty, // Placeholder
|
2021-02-28 23:59:04 +00:00
|
|
|
|
|
|
|
// Shapes
|
2021-02-28 23:34:36 +00:00
|
|
|
Aabb(Aabb<i32>),
|
2021-02-28 23:59:04 +00:00
|
|
|
Pyramid { aabb: Aabb<i32>, inset: i32 },
|
|
|
|
|
|
|
|
// Combinators
|
2021-02-28 23:34:36 +00:00
|
|
|
And(Id<Primitive>, Id<Primitive>),
|
|
|
|
Or(Id<Primitive>, Id<Primitive>),
|
|
|
|
Xor(Id<Primitive>, Id<Primitive>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Fill {
|
|
|
|
pub prim: Id<Primitive>,
|
|
|
|
pub block: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fill {
|
|
|
|
fn contains_at(&self, tree: &Store<Primitive>, prim: Id<Primitive>, pos: Vec3<i32>) -> bool {
|
2021-02-28 23:59:04 +00:00
|
|
|
// Custom closure because vek's impl of `contains_point` is inclusive :(
|
|
|
|
let aabb_contains = |aabb: Aabb<i32>, pos: Vec3<i32>| (aabb.min.x..aabb.max.x).contains(&pos.x)
|
|
|
|
&& (aabb.min.y..aabb.max.y).contains(&pos.y);
|
|
|
|
|
2021-02-28 23:34:36 +00:00
|
|
|
match &tree[prim] {
|
|
|
|
Primitive::Empty => false,
|
2021-02-28 23:59:04 +00:00
|
|
|
|
|
|
|
Primitive::Aabb(aabb) => aabb_contains(*aabb, pos),
|
|
|
|
Primitive::Pyramid { aabb, inset } => {
|
2021-03-01 21:14:38 +00:00
|
|
|
let inset = (*inset).max(aabb.size().reduce_min());
|
|
|
|
let inner = Aabr { min: aabb.min.xy() - 1 + inset, max: aabb.max.xy() - inset };
|
2021-02-28 23:59:04 +00:00
|
|
|
aabb_contains(*aabb, pos) && (inner.projected_point(pos.xy()) - pos.xy())
|
|
|
|
.map(|e| e.abs())
|
2021-03-01 21:14:38 +00:00
|
|
|
.reduce_max() as f32 / (inset as f32) < 1.0 - ((pos.z - aabb.min.z) as f32 + 0.5) / (aabb.max.z - aabb.min.z) as f32
|
2021-02-28 23:59:04 +00:00
|
|
|
},
|
|
|
|
|
2021-02-28 23:34:36 +00:00
|
|
|
Primitive::And(a, b) => self.contains_at(tree, *a, pos) & self.contains_at(tree, *b, pos),
|
|
|
|
Primitive::Or(a, b) => self.contains_at(tree, *a, pos) | self.contains_at(tree, *b, pos),
|
|
|
|
Primitive::Xor(a, b) => self.contains_at(tree, *a, pos) ^ self.contains_at(tree, *b, pos),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sample_at(&self, tree: &Store<Primitive>, pos: Vec3<i32>) -> Option<Block> {
|
|
|
|
Some(self.block).filter(|_| self.contains_at(tree, self.prim, pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_bounds_inner(&self, tree: &Store<Primitive>, prim: Id<Primitive>) -> Aabb<i32> {
|
|
|
|
match &tree[prim] {
|
|
|
|
Primitive::Empty => Aabb::new_empty(Vec3::zero()),
|
|
|
|
Primitive::Aabb(aabb) => *aabb,
|
2021-02-28 23:59:04 +00:00
|
|
|
Primitive::Pyramid { aabb, .. } => *aabb,
|
2021-02-28 23:34:36 +00:00
|
|
|
Primitive::And(a, b) => self.get_bounds_inner(tree, *a).intersection(self.get_bounds_inner(tree, *b)),
|
|
|
|
Primitive::Or(a, b) | Primitive::Xor(a, b) => self.get_bounds_inner(tree, *a).union(self.get_bounds_inner(tree, *b)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_bounds(&self, tree: &Store<Primitive>) -> Aabb<i32> {
|
|
|
|
self.get_bounds_inner(tree, self.prim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Structure {
|
|
|
|
fn render<F: FnMut(Primitive) -> Id<Primitive>, G: FnMut(Fill)>(
|
|
|
|
&self,
|
2021-03-01 23:43:26 +00:00
|
|
|
prim: F,
|
|
|
|
fill: G,
|
2021-02-28 23:34:36 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
// Generate a primitive tree and fills for this structure
|
|
|
|
fn render_collect(&self) -> (Store<Primitive>, Vec<Fill>) {
|
|
|
|
let mut tree = Store::default();
|
|
|
|
let mut fills = Vec::new();
|
|
|
|
let root = self.render(|p| tree.insert(p), |f| fills.push(f));
|
|
|
|
(tree, fills)
|
|
|
|
}
|
|
|
|
}
|