veloren_server/
lod.rs

1#[cfg(not(feature = "worldgen"))]
2use crate::test_world::{IndexRef, World};
3use common::lod;
4use hashbrown::HashMap;
5use vek::*;
6#[cfg(feature = "worldgen")]
7use world::{IndexRef, World};
8
9static EMPTY_ZONE: lod::Zone = lod::Zone {
10    objects: Vec::new(),
11};
12
13#[derive(Default)]
14pub struct Lod {
15    pub zones: HashMap<Vec2<i32>, lod::Zone>,
16}
17
18impl Lod {
19    #[cfg(feature = "worldgen")]
20    pub fn from_world(world: &World, index: IndexRef, threadpool: &rayon::ThreadPool) -> Self {
21        common_base::prof_span!("Lod::from_world");
22        threadpool.install(|| {
23            let zone_sz = (world.sim().get_size() + lod::ZONE_SIZE - 1) / lod::ZONE_SIZE;
24
25            use rayon::prelude::*;
26            let zones = (0..zone_sz.x)
27                .into_par_iter()
28                .flat_map(|i| (0..zone_sz.y).into_par_iter().map(move |j| (i, j)))
29                .map(|(i, j)| {
30                    let zone_pos = Vec2::new(i, j).map(|e| e as i32);
31                    (zone_pos, world.get_lod_zone(zone_pos, index))
32                })
33                .collect();
34
35            Self { zones }
36        })
37    }
38
39    #[cfg(not(feature = "worldgen"))]
40    pub fn from_world(_world: &World, _index: IndexRef, _threadpool: &rayon::ThreadPool) -> Self {
41        Self::default()
42    }
43
44    pub fn zone(&self, zone_pos: Vec2<i32>) -> &lod::Zone {
45        self.zones.get(&zone_pos).unwrap_or(&EMPTY_ZONE)
46    }
47}