veloren/world/src/site/mod.rs

135 lines
3.2 KiB
Rust
Raw Normal View History

2020-04-15 19:41:40 +00:00
mod dungeon;
2020-04-17 23:29:01 +00:00
mod settlement;
// Reexports
2020-04-17 23:29:01 +00:00
pub use self::{dungeon::Dungeon, settlement::Settlement};
2020-04-23 15:16:08 +00:00
use crate::column::ColumnSample;
use common::{
generation::ChunkSupplement,
2020-04-17 23:29:01 +00:00
terrain::Block,
vol::{BaseVol, ReadVol, RectSizedVol, Vox, WriteVol},
};
use rand::Rng;
use std::{fmt, sync::Arc};
use vek::*;
2020-04-15 19:41:40 +00:00
#[derive(Copy, Clone)]
pub struct BlockMask {
block: Block,
priority: i32,
}
impl BlockMask {
2020-04-17 23:29:01 +00:00
pub fn new(block: Block, priority: i32) -> Self { Self { block, priority } }
2020-04-15 19:41:40 +00:00
pub fn nothing() -> Self {
Self {
block: Block::empty(),
priority: 0,
}
}
pub fn with_priority(mut self, priority: i32) -> Self {
self.priority = priority;
self
}
pub fn resolve_with(self, other: Self) -> Self {
if self.priority >= other.priority {
self
} else {
other
}
}
pub fn finish(self) -> Option<Block> {
if self.priority > 0 {
Some(self.block)
} else {
None
}
}
}
pub struct SpawnRules {
pub trees: bool,
}
2020-04-15 19:41:40 +00:00
impl Default for SpawnRules {
2020-04-17 23:29:01 +00:00
fn default() -> Self { Self { trees: true } }
2020-04-15 19:41:40 +00:00
}
#[derive(Clone)]
pub enum Site {
Settlement(Arc<Settlement>),
2020-04-15 19:41:40 +00:00
Dungeon(Arc<Dungeon>),
}
impl Site {
pub fn radius(&self) -> f32 {
match self {
Site::Settlement(settlement) => settlement.radius(),
2020-04-15 19:41:40 +00:00
Site::Dungeon(dungeon) => dungeon.radius(),
}
}
2020-04-23 20:59:09 +00:00
pub fn get_origin(&self) -> Vec2<i32> {
match self {
Site::Settlement(s) => s.get_origin(),
Site::Dungeon(d) => d.get_origin(),
}
}
pub fn spawn_rules(&self, wpos: Vec2<i32>) -> SpawnRules {
match self {
2020-04-15 19:41:40 +00:00
Site::Settlement(s) => s.spawn_rules(wpos),
Site::Dungeon(d) => d.spawn_rules(wpos),
}
}
pub fn apply_to<'a>(
&'a self,
wpos2d: Vec2<i32>,
get_column: impl FnMut(Vec2<i32>) -> Option<&'a ColumnSample<'a>>,
2020-04-11 19:04:19 +00:00
vol: &mut (impl BaseVol<Vox = Block> + RectSizedVol + ReadVol + WriteVol),
) {
2020-02-08 19:58:42 +00:00
match self {
Site::Settlement(settlement) => settlement.apply_to(wpos2d, get_column, vol),
2020-04-15 19:41:40 +00:00
Site::Dungeon(dungeon) => dungeon.apply_to(wpos2d, get_column, vol),
2020-02-08 19:58:42 +00:00
}
}
pub fn apply_supplement<'a>(
&'a self,
rng: &mut impl Rng,
wpos2d: Vec2<i32>,
get_column: impl FnMut(Vec2<i32>) -> Option<&'a ColumnSample<'a>>,
supplement: &mut ChunkSupplement,
) {
match self {
2020-04-17 23:29:01 +00:00
Site::Settlement(settlement) => {
settlement.apply_supplement(rng, wpos2d, get_column, supplement)
2020-04-17 23:29:01 +00:00
},
Site::Dungeon(dungeon) => dungeon.apply_supplement(rng, wpos2d, get_column, supplement),
}
}
}
impl From<Settlement> for Site {
fn from(settlement: Settlement) -> Self { Site::Settlement(Arc::new(settlement)) }
}
2020-04-15 19:41:40 +00:00
impl From<Dungeon> for Site {
fn from(dungeon: Dungeon) -> Self { Site::Dungeon(Arc::new(dungeon)) }
}
impl fmt::Debug for Site {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Site::Settlement(_) => write!(f, "Settlement"),
2020-04-15 19:41:40 +00:00
Site::Dungeon(_) => write!(f, "Dungeon"),
}
}
}