mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Merge branch 'christof/econ_goods_in_ron' into 'master'
Economy: Move decay and transportation effort from hardcoded into ron See merge request veloren/veloren!3578
This commit is contained in:
commit
aa7cdd2864
51
assets/common/economy/trading_goods.ron
Normal file
51
assets/common/economy/trading_goods.ron
Normal file
@ -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
|
||||
),
|
||||
}
|
102
world/src/site/economy/cache.rs
Normal file
102
world/src/site/economy/cache.rs
Normal file
@ -0,0 +1,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,
|
||||
}
|
||||
}
|
@ -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<Item = GoodIndex> {
|
||||
(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,
|
||||
|
Loading…
Reference in New Issue
Block a user