veloren_rtsim/data/nature.rs
1use common::{grid::Grid, rtsim::TerrainResource};
2use enum_map::EnumMap;
3use serde::{Deserialize, Serialize};
4use vek::*;
5use world::World;
6
7/// Represents the state of 'natural' elements of the world such as
8/// plant/animal/resource populations, weather systems, etc.
9///
10/// Where possible, this data does not define the state of natural aspects of
11/// the world, but instead defines 'modifications' that sit on top of the world
12/// data generated by initial generation.
13#[derive(Clone, Serialize, Deserialize)]
14pub struct Nature {
15 pub chunks: Grid<Chunk>,
16}
17
18impl Nature {
19 pub fn generate(world: &World) -> Self {
20 Self {
21 chunks: Grid::populate_from(world.sim().get_size().map(|e| e as i32), |_| Chunk {
22 res: EnumMap::<_, f32>::default().map(|_, _| 1.0),
23 }),
24 }
25 }
26
27 #[inline]
28 pub fn chunk_resources(&self, key: Vec2<i32>) -> Option<&EnumMap<TerrainResource, f32>> {
29 self.chunks.get(key).map(|c| &c.res)
30 }
31
32 #[inline]
33 pub fn chunk_resources_mut(
34 &mut self,
35 key: Vec2<i32>,
36 ) -> Option<&mut EnumMap<TerrainResource, f32>> {
37 self.chunks.get_mut(key).map(|c| &mut c.res)
38 }
39}
40
41#[derive(Clone, Serialize, Deserialize)]
42pub struct Chunk {
43 /// Represent the 'naturally occurring' resource proportion that exists in
44 /// this chunk.
45 ///
46 /// 0.0 => None of the resources generated by terrain generation should be
47 /// present
48 ///
49 /// 1.0 => All of the resources generated by terrain generation should be
50 /// present
51 ///
52 /// It's important to understand this this number does not represent the
53 /// total amount of a resource present in a chunk, nor is it even
54 /// proportional to the amount of the resource present. To get the total
55 /// amount of the resource in a chunk, one must first multiply this
56 /// factor by the amount of 'natural' resources given by terrain
57 /// generation. This value represents only the variable 'depletion' factor
58 /// of that resource, which shall change over time as the world evolves
59 /// and players interact with it.
60 // TODO: Consider whether we can use `i16` or similar here instead: `f32` has more resolution
61 // than we might need.
62 #[serde(rename = "r")]
63 #[serde(serialize_with = "crate::data::rugged_ser_enum_map::<_, _, _, 1>")]
64 #[serde(deserialize_with = "crate::data::rugged_de_enum_map::<_, _, _, 1>")]
65 pub res: EnumMap<TerrainResource, f32>,
66}