veloren_world/site/economy/
cache.rs

1use super::{
2    good_list,
3    map_types::{GoodIndex, GoodMap},
4};
5use crate::{
6    assets::{AssetExt, Ron},
7    util::DHashMap,
8};
9use common::{
10    terrain::BiomeKind,
11    trade::Good::{self, Terrain, Territory},
12};
13use lazy_static::lazy_static;
14use serde::{Deserialize, Serialize};
15
16const fn default_one() -> f32 { 1.0 }
17const fn default_true() -> bool { true }
18
19#[derive(Debug, Serialize, Deserialize, Clone)]
20struct RawGoodProperties {
21    #[serde(default)]
22    pub decay_rate: f32,
23    #[serde(default = "default_one")]
24    pub transport_effort: f32,
25    #[serde(default = "default_true")]
26    pub storable: bool,
27}
28
29/// Contains caches used for economic simulation
30pub struct EconomyCache {
31    pub(crate) transport_effort: GoodMap<f32>,
32    pub(crate) decay_rate: GoodMap<f32>,
33    pub(crate) direct_use_goods: Vec<GoodIndex>,
34}
35
36lazy_static! {
37    static ref CACHE: EconomyCache = load_cache();
38}
39
40pub fn cache() -> &'static EconomyCache { &CACHE }
41
42fn load_cache() -> EconomyCache {
43    let good_properties =
44        Ron::<DHashMap<Good, RawGoodProperties>>::load_expect("common.economy.trading_goods")
45            .read()
46            .0
47            .clone();
48    let mut decay_rate: GoodMap<f32> = GoodMap::from_default(0.0);
49    let mut transport_effort: GoodMap<f32> = GoodMap::from_default(1.0);
50    let mut direct_use_goods: Vec<GoodIndex> = Vec::new();
51
52    for i in good_properties.iter() {
53        if let Ok(rawgood) = (*i.0).try_into() {
54            decay_rate[rawgood] = i.1.decay_rate;
55            if !i.1.storable {
56                direct_use_goods.push(rawgood);
57            }
58            transport_effort[rawgood] = i.1.transport_effort;
59        } else {
60            match *i.0 {
61                Territory(BiomeKind::Void) => {
62                    for j in good_list() {
63                        if let Territory(_) = Good::from(j) {
64                            decay_rate[j] = i.1.decay_rate;
65                            transport_effort[j] = i.1.transport_effort;
66                            if !i.1.storable {
67                                direct_use_goods.push(j);
68                            }
69                        }
70                    }
71                },
72                Terrain(BiomeKind::Void) => {
73                    for j in good_list() {
74                        if let Terrain(_) = Good::from(j) {
75                            decay_rate[j] = i.1.decay_rate;
76                            transport_effort[j] = i.1.transport_effort;
77                            if !i.1.storable {
78                                direct_use_goods.push(j);
79                            }
80                        }
81                    }
82                },
83                _ => tracing::warn!("Raw good not in index: {:?}", i.0),
84            }
85        }
86    }
87
88    EconomyCache {
89        transport_effort,
90        decay_rate,
91        direct_use_goods,
92    }
93}