2022-09-27 12:58:15 +00:00
|
|
|
use crate::{column::ColumnGen, sim, util::Sampler, ColumnSample, IndexRef};
|
2021-03-05 01:23:11 +00:00
|
|
|
use common::{terrain::TerrainChunkSize, vol::RectVolSize};
|
2021-02-17 01:47:16 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2021-03-05 01:23:11 +00:00
|
|
|
/// A wrapper type that may contain a reference to a generated world. If not,
|
|
|
|
/// default values will be provided.
|
2021-02-17 01:47:16 +00:00
|
|
|
pub struct Land<'a> {
|
|
|
|
sim: Option<&'a sim::WorldSim>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Land<'a> {
|
2021-03-05 01:23:11 +00:00
|
|
|
pub fn empty() -> Self { Self { sim: None } }
|
2021-02-17 01:47:16 +00:00
|
|
|
|
2022-06-27 21:32:57 +00:00
|
|
|
pub fn size(&self) -> Vec2<u32> { self.sim.map_or(Vec2::one(), |s| s.get_size()) }
|
|
|
|
|
2021-03-05 01:23:11 +00:00
|
|
|
pub fn from_sim(sim: &'a sim::WorldSim) -> Self { Self { sim: Some(sim) } }
|
2021-02-17 01:47:16 +00:00
|
|
|
|
|
|
|
pub fn get_alt_approx(&self, wpos: Vec2<i32>) -> f32 {
|
2021-03-05 01:23:11 +00:00
|
|
|
self.sim
|
|
|
|
.and_then(|sim| sim.get_alt_approx(wpos))
|
|
|
|
.unwrap_or(0.0)
|
2021-02-17 01:47:16 +00:00
|
|
|
}
|
2021-02-23 12:42:45 +00:00
|
|
|
|
|
|
|
pub fn get_gradient_approx(&self, wpos: Vec2<i32>) -> f32 {
|
|
|
|
self.sim
|
2022-01-28 15:17:56 +00:00
|
|
|
.and_then(|sim| sim.get_gradient_approx(self.wpos_chunk_pos(wpos)))
|
2021-02-23 12:42:45 +00:00
|
|
|
.unwrap_or(0.0)
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:17:56 +00:00
|
|
|
pub fn wpos_chunk_pos(&self, wpos: Vec2<i32>) -> Vec2<i32> {
|
|
|
|
wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e.div_euclid(sz as i32))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_chunk(&self, chunk_pos: Vec2<i32>) -> Option<&sim::SimChunk> {
|
|
|
|
self.sim.and_then(|sim| sim.get(chunk_pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_chunk_wpos(&self, wpos: Vec2<i32>) -> Option<&sim::SimChunk> {
|
2021-02-23 12:42:45 +00:00
|
|
|
self.sim.and_then(|sim| sim.get_wpos(wpos))
|
|
|
|
}
|
2022-10-31 18:55:13 +00:00
|
|
|
|
|
|
|
pub fn get_nearest_path(
|
|
|
|
&self,
|
|
|
|
wpos: Vec2<i32>,
|
|
|
|
) -> Option<(f32, Vec2<f32>, sim::Path, Vec2<f32>)> {
|
|
|
|
self.sim.and_then(|sim| sim.get_nearest_path(wpos))
|
|
|
|
}
|
2022-09-27 12:58:15 +00:00
|
|
|
|
|
|
|
pub fn column_sample<'sample>(
|
|
|
|
&'sample self,
|
|
|
|
wpos: Vec2<i32>,
|
|
|
|
index: IndexRef<'sample>,
|
|
|
|
) -> Option<ColumnSample<'sample>> {
|
|
|
|
self.sim
|
|
|
|
.and_then(|sim| ColumnGen::new(sim).get((wpos, index, None)))
|
|
|
|
}
|
2021-02-17 01:47:16 +00:00
|
|
|
}
|