1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{column::ColumnGen, sim, util::Sampler, ColumnSample, IndexRef};
use common::{terrain::TerrainChunkSize, vol::RectVolSize};
use vek::*;

/// A wrapper type that may contain a reference to a generated world. If not,
/// default values will be provided.
pub struct Land<'a> {
    sim: Option<&'a sim::WorldSim>,
}

impl<'a> Land<'a> {
    pub fn empty() -> Self { Self { sim: None } }

    pub fn size(&self) -> Vec2<u32> { self.sim.map_or(Vec2::one(), |s| s.get_size()) }

    pub fn from_sim(sim: &'a sim::WorldSim) -> Self { Self { sim: Some(sim) } }

    pub fn get_alt_approx(&self, wpos: Vec2<i32>) -> f32 {
        self.sim
            .and_then(|sim| sim.get_alt_approx(wpos))
            .unwrap_or(0.0)
    }

    pub fn get_downhill(&self, wpos: Vec2<i32>) -> Vec2<i32> {
        self.sim
            .and_then(|sim| sim.get_wpos(wpos))
            .and_then(|c| c.downhill)
            .unwrap_or(Vec2::zero())
    }

    pub fn get_gradient_approx(&self, wpos: Vec2<i32>) -> f32 {
        self.sim
            .and_then(|sim| sim.get_gradient_approx(self.wpos_chunk_pos(wpos)))
            .unwrap_or(0.0)
    }

    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> {
        self.sim.and_then(|sim| sim.get_wpos(wpos))
    }

    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))
    }

    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)))
    }
}