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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use super::{
    good_list,
    map_types::{GoodIndex, GoodMap},
};
use crate::{
    assets::{self, AssetExt},
    util::DHashMap,
};
use common::{
    terrain::BiomeKind,
    trade::Good::{self, Terrain, Territory},
};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};

const fn default_one() -> f32 { 1.0 }
const fn default_true() -> bool { true }

#[derive(Debug, Serialize, Deserialize, Clone)]
struct RawGoodProperties {
    #[serde(default)]
    pub decay_rate: f32,
    #[serde(default = "default_one")]
    pub transport_effort: f32,
    #[serde(default = "default_true")]
    pub storable: bool,
}

#[derive(Debug, Deserialize)]
#[serde(transparent)]
pub struct RawGoodPropertiesList(DHashMap<Good, RawGoodProperties>);

impl assets::Asset for RawGoodPropertiesList {
    type Loader = assets::RonLoader;

    const EXTENSION: &'static str = "ron";
}

/// Contains caches used for economic simulation
pub struct EconomyCache {
    pub(crate) transport_effort: GoodMap<f32>,
    pub(crate) decay_rate: GoodMap<f32>,
    pub(crate) direct_use_goods: Vec<GoodIndex>,
}

lazy_static! {
    static ref CACHE: EconomyCache = load_cache();
}

pub fn cache() -> &'static EconomyCache { &CACHE }

fn load_cache() -> EconomyCache {
    let good_properties = RawGoodPropertiesList::load_expect("common.economy.trading_goods")
        .read()
        .0
        .clone();
    let mut decay_rate: GoodMap<f32> = GoodMap::from_default(0.0);
    let mut transport_effort: GoodMap<f32> = GoodMap::from_default(1.0);
    let mut direct_use_goods: Vec<GoodIndex> = Vec::new();

    for i in good_properties.iter() {
        if let Ok(rawgood) = (*i.0).try_into() {
            decay_rate[rawgood] = i.1.decay_rate;
            if !i.1.storable {
                direct_use_goods.push(rawgood);
            }
            transport_effort[rawgood] = i.1.transport_effort;
        } else {
            match *i.0 {
                Territory(BiomeKind::Void) => {
                    for j in good_list() {
                        if let Territory(_) = Good::from(j) {
                            decay_rate[j] = i.1.decay_rate;
                            transport_effort[j] = i.1.transport_effort;
                            if !i.1.storable {
                                direct_use_goods.push(j);
                            }
                        }
                    }
                },
                Terrain(BiomeKind::Void) => {
                    for j in good_list() {
                        if let Terrain(_) = Good::from(j) {
                            decay_rate[j] = i.1.decay_rate;
                            transport_effort[j] = i.1.transport_effort;
                            if !i.1.storable {
                                direct_use_goods.push(j);
                            }
                        }
                    }
                },
                _ => tracing::warn!("Raw good not in index: {:?}", i.0),
            }
        }
    }

    EconomyCache {
        transport_effort,
        decay_rate,
        direct_use_goods,
    }
}