From fbe0aee40d36e1f91cb656651860cbf5b934686a Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sun, 21 Aug 2022 19:16:34 +0200 Subject: [PATCH] move trading good properties (decay, transportation) from hard coded to ron file --- assets/common/economy/trading_goods.ron | 51 +++++++++++++ world/src/site/economy/cache.rs | 97 +++++++++++++++++++++++++ world/src/site/economy/mod.rs | 46 +----------- 3 files changed, 152 insertions(+), 42 deletions(-) create mode 100644 assets/common/economy/trading_goods.ron create mode 100644 world/src/site/economy/cache.rs diff --git a/assets/common/economy/trading_goods.ron b/assets/common/economy/trading_goods.ron new file mode 100644 index 0000000000..92fcacfd21 --- /dev/null +++ b/assets/common/economy/trading_goods.ron @@ -0,0 +1,51 @@ +{ + Armor: ( + // decay_rate: 0.03, // 23 years half-life + transport_effort: 2.0, // 1 person can carry half a set of entry armor + ), + Terrain(Void): ( // any terrain + transport_effort: 0.0, + storable: false, + ), + Territory(Void): ( // any territory + transport_effort: 0.0, + storable: false, + ), + Transportation: ( + storable: false, + ), + RoadSecurity: ( + transport_effort: 0.0, + storable: false, + ), + Coin: ( + transport_effort: 0.01, + ), + Potions: ( + transport_effort: 0.1, + decay_rate: 0.2, // 3 years half-life + ), + Stone: ( + transport_effort: 4.0, + ), + Food: ( + decay_rate: 0.2, // revisit + ), + Meat: ( + decay_rate: 0.25, // revisit + ), + Flour: ( + decay_rate: 0.1, // revisit + ), + Ingredients: ( + decay_rate: 0.1, // revisit + ), + Tools: ( + // TODO: Separate stone, metal, bone, wood + // decay_rate: 0.05, // 14 years half-life + ), + Wood: ( + //decay_rate: 0.1, // 6 years half-life + //transport_effort: 0.05, // 2kg/40kg + ), +} \ No newline at end of file diff --git a/world/src/site/economy/cache.rs b/world/src/site/economy/cache.rs new file mode 100644 index 0000000000..8288ddf096 --- /dev/null +++ b/world/src/site/economy/cache.rs @@ -0,0 +1,97 @@ +use super::{ + good_list, + map_types::{GoodIndex, GoodMap}, +}; +use crate::{ + assets::{self, AssetExt}, + util::DHashMap, +}; +use common::{ + terrain::BiomeKind, + trade::Good::{self, 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)] + // pub decay_regain: Option<(Good, f32)>, + // #[serde(default)] + // pub equivalent_to: Option<(Good, f32, EquivalenceType)>, + #[serde(default = "default_one")] + pub transport_effort: f32, + #[serde(default = "default_true")] + pub storable: bool, +} + +#[derive(Debug, Deserialize)] +#[serde(transparent)] +pub struct RawGoodPropertiesList(DHashMap); + +impl assets::Asset for RawGoodPropertiesList { + type Loader = assets::RonLoader; + + const EXTENSION: &'static str = "ron"; +} + +/// Contains all caches used for economical simulation +pub struct EconomyCache { + // professions: Vec, + // everybody: Profession, + pub(crate) transport_effort: GoodMap, + pub(crate) decay_rate: GoodMap, + pub(crate) direct_use_goods: Vec, +} + +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 = GoodMap::from_default(0.0); + let mut transport_effort: GoodMap = GoodMap::from_default(1.0); + let mut direct_use_goods: Vec = 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); + } + } + } + }, + _ => tracing::warn!("Raw good not in index: {:?}", i.0), + } + } + } + + EconomyCache { + transport_effort, + decay_rate, + direct_use_goods, + } +} diff --git a/world/src/site/economy/mod.rs b/world/src/site/economy/mod.rs index c5fb531e8e..dcc1f25570 100644 --- a/world/src/site/economy/mod.rs +++ b/world/src/site/economy/mod.rs @@ -22,6 +22,7 @@ pub use map_types::Labor; use map_types::{GoodIndex, GoodMap, LaborIndex, LaborMap, NaturalResources}; mod context; pub use context::simulate_economy; +mod cache; const INTER_SITE_TRADE: bool = true; const DAYS_PER_MONTH: f32 = 30.0; @@ -1374,51 +1375,12 @@ fn good_list() -> impl Iterator { (0..GoodIndex::LENGTH).map(GoodIndex::from_usize) } -// cache in GoodMap ? -fn transportation_effort(g: GoodIndex) -> f32 { - match Good::from(g) { - Terrain(_) | Territory(_) | RoadSecurity => 0.0, - Coin => 0.01, - Potions => 0.1, +fn transportation_effort(g: GoodIndex) -> f32 { cache::cache().transport_effort[g] } - Armor => 2.0, - Stone => 4.0, - - _ => 1.0, - } -} - -fn decay_rate(g: GoodIndex) -> f32 { - match Good::from(g) { - Food => 0.2, - Flour => 0.1, - Meat => 0.25, - Ingredients => 0.1, - _ => 0.0, - } -} +fn decay_rate(g: GoodIndex) -> f32 { cache::cache().decay_rate[g] } /** you can't accumulate or save these options/resources for later */ -fn direct_use_goods() -> &'static [GoodIndex] { - lazy_static! { - static ref DIRECT_USE: [GoodIndex; 13] = [ - GoodIndex::try_from(Transportation).unwrap_or_default(), - GoodIndex::try_from(Territory(BiomeKind::Grassland)).unwrap_or_default(), - GoodIndex::try_from(Territory(BiomeKind::Forest)).unwrap_or_default(), - GoodIndex::try_from(Territory(BiomeKind::Lake)).unwrap_or_default(), - GoodIndex::try_from(Territory(BiomeKind::Ocean)).unwrap_or_default(), - GoodIndex::try_from(Territory(BiomeKind::Mountain)).unwrap_or_default(), - GoodIndex::try_from(RoadSecurity).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Grassland)).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Forest)).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Lake)).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Ocean)).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Mountain)).unwrap_or_default(), - GoodIndex::try_from(Terrain(BiomeKind::Desert)).unwrap_or_default(), - ]; - } - &*DIRECT_USE -} +fn direct_use_goods() -> &'static [GoodIndex] { &cache::cache().direct_use_goods } pub struct GraphInfo { dummy: Economy,