veloren_rtsim/data/nature.rs
1use common::{grid::Grid, rtsim::ChunkResource};
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 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 // TODO: Clean up this API a bit
28 pub fn get_chunk_resources(&self, key: Vec2<i32>) -> EnumMap<ChunkResource, f32> {
29 self.chunks.get(key).map(|c| c.res).unwrap_or_default()
30 }
31
32 pub fn set_chunk_resources(&mut self, key: Vec2<i32>, res: EnumMap<ChunkResource, f32>) {
33 if let Some(chunk) = self.chunks.get_mut(key) {
34 chunk.res = res;
35 }
36 }
37}
38
39#[derive(Clone, Serialize, Deserialize)]
40pub struct Chunk {
41 /// Represent the 'naturally occurring' resource proportion that exists in
42 /// this chunk.
43 ///
44 /// 0.0 => None of the resources generated by terrain generation should be
45 /// present
46 ///
47 /// 1.0 => All of the resources generated by terrain generation should be
48 /// present
49 ///
50 /// It's important to understand this this number does not represent the
51 /// total amount of a resource present in a chunk, nor is it even
52 /// proportional to the amount of the resource present. To get the total
53 /// amount of the resource in a chunk, one must first multiply this
54 /// factor by the amount of 'natural' resources given by terrain
55 /// generation. This value represents only the variable 'depletion' factor
56 /// of that resource, which shall change over time as the world evolves
57 /// and players interact with it.
58 // TODO: Consider whether we can use `i16` or similar here instead: `f32` has more resolution
59 // than we might need.
60 #[serde(rename = "r")]
61 #[serde(serialize_with = "crate::data::rugged_ser_enum_map::<_, _, _, 1>")]
62 #[serde(deserialize_with = "crate::data::rugged_de_enum_map::<_, _, _, 1>")]
63 res: EnumMap<ChunkResource, f32>,
64}