2020-03-28 18:16:19 +00:00
|
|
|
mod econ;
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
use crate::{
|
|
|
|
sim::{SimChunk, WorldSim},
|
|
|
|
site::{Dungeon, Settlement, Site as WorldSite},
|
|
|
|
util::{attempt, seed_expan},
|
2020-03-29 19:48:51 +00:00
|
|
|
};
|
2020-03-27 13:16:02 +00:00
|
|
|
use common::{
|
|
|
|
astar::Astar,
|
2020-04-17 23:29:01 +00:00
|
|
|
path::Path,
|
2020-04-11 00:09:01 +00:00
|
|
|
spiral::Spiral2d,
|
2020-04-17 23:29:01 +00:00
|
|
|
store::{Id, Store},
|
|
|
|
terrain::TerrainChunkSize,
|
|
|
|
vol::RectVolSize,
|
2020-04-11 00:09:01 +00:00
|
|
|
};
|
2020-04-17 23:29:01 +00:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
|
|
|
use rand::prelude::*;
|
|
|
|
use rand_chacha::ChaChaRng;
|
|
|
|
use std::{fmt, hash::Hash, ops::Range};
|
|
|
|
use vek::*;
|
2020-03-27 13:16:02 +00:00
|
|
|
|
|
|
|
const CARDINALS: [Vec2<i32>; 4] = [
|
|
|
|
Vec2::new(1, 0),
|
|
|
|
Vec2::new(-1, 0),
|
|
|
|
Vec2::new(0, 1),
|
|
|
|
Vec2::new(0, -1),
|
|
|
|
];
|
|
|
|
|
|
|
|
const DIAGONALS: [Vec2<i32>; 8] = [
|
|
|
|
Vec2::new(1, 0),
|
|
|
|
Vec2::new(1, 1),
|
|
|
|
Vec2::new(-1, 0),
|
|
|
|
Vec2::new(-1, 1),
|
|
|
|
Vec2::new(0, 1),
|
|
|
|
Vec2::new(1, -1),
|
|
|
|
Vec2::new(0, -1),
|
|
|
|
Vec2::new(-1, -1),
|
|
|
|
];
|
|
|
|
|
2020-04-01 20:38:01 +00:00
|
|
|
const INITIAL_CIV_COUNT: usize = 32;
|
2020-03-27 13:16:02 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Civs {
|
|
|
|
civs: Store<Civ>,
|
|
|
|
places: Store<Place>,
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-27 18:52:28 +00:00
|
|
|
tracks: Store<Track>,
|
2020-03-27 23:06:23 +00:00
|
|
|
track_map: HashMap<Id<Site>, HashMap<Id<Site>, Id<Track>>>,
|
|
|
|
|
|
|
|
sites: Store<Site>,
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 18:16:19 +00:00
|
|
|
pub struct GenCtx<'a, R: Rng> {
|
2020-03-27 13:16:02 +00:00
|
|
|
sim: &'a mut WorldSim,
|
2020-04-19 12:12:08 +00:00
|
|
|
rng: R,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, R: Rng> GenCtx<'a, R> {
|
|
|
|
pub fn reseed(&mut self) -> GenCtx<'_, impl Rng> {
|
|
|
|
GenCtx {
|
|
|
|
sim: self.sim,
|
|
|
|
rng: ChaChaRng::from_seed(self.rng.gen()),
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Civs {
|
|
|
|
pub fn generate(seed: u32, sim: &mut WorldSim) -> Self {
|
|
|
|
let mut this = Self::default();
|
2020-04-19 12:12:08 +00:00
|
|
|
let rng = ChaChaRng::from_seed(seed_expan::rng_state(seed));
|
|
|
|
let mut ctx = GenCtx { sim, rng };
|
2020-03-27 13:16:02 +00:00
|
|
|
|
|
|
|
for _ in 0..INITIAL_CIV_COUNT {
|
2020-04-17 15:46:08 +00:00
|
|
|
log::info!("Creating civilisation...");
|
2020-04-19 12:12:08 +00:00
|
|
|
if let None = this.birth_civ(&mut ctx.reseed()) {
|
2020-04-17 15:46:08 +00:00
|
|
|
log::warn!("Failed to find starting site for civilisation.");
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:46:08 +00:00
|
|
|
for _ in 0..100 {
|
2020-04-15 19:41:40 +00:00
|
|
|
attempt(5, || {
|
|
|
|
let loc = find_site_loc(&mut ctx, None)?;
|
2020-04-19 12:12:08 +00:00
|
|
|
this.establish_site(&mut ctx.reseed(), loc, |place| Site {
|
2020-04-15 19:41:40 +00:00
|
|
|
kind: SiteKind::Dungeon,
|
|
|
|
center: loc,
|
|
|
|
place,
|
|
|
|
|
|
|
|
population: 0.0,
|
|
|
|
|
|
|
|
stocks: Stocks::from_default(100.0),
|
|
|
|
surplus: Stocks::from_default(0.0),
|
|
|
|
values: Stocks::from_default(None),
|
|
|
|
|
|
|
|
labors: MapVec::from_default(0.01),
|
|
|
|
yields: MapVec::from_default(1.0),
|
|
|
|
productivity: MapVec::from_default(1.0),
|
|
|
|
|
|
|
|
last_exports: Stocks::from_default(0.0),
|
|
|
|
export_targets: Stocks::from_default(0.0),
|
|
|
|
trade_states: Stocks::default(),
|
|
|
|
coin: 1000.0,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
// Tick
|
2020-03-28 18:16:19 +00:00
|
|
|
const SIM_YEARS: usize = 1000;
|
2020-03-27 23:06:23 +00:00
|
|
|
for _ in 0..SIM_YEARS {
|
2020-03-28 18:16:19 +00:00
|
|
|
this.tick(&mut ctx, 1.0);
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 13:16:02 +00:00
|
|
|
// Temporary!
|
2020-03-27 18:52:28 +00:00
|
|
|
for track in this.tracks.iter() {
|
|
|
|
for loc in track.path.iter() {
|
2020-04-17 23:29:01 +00:00
|
|
|
ctx.sim.get_mut(*loc).unwrap().place =
|
|
|
|
Some(this.civs.iter().next().unwrap().homeland);
|
2020-04-11 00:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 21:44:15 +00:00
|
|
|
// Flatten ground around sites
|
2020-04-11 00:09:01 +00:00
|
|
|
for site in this.sites.iter() {
|
2020-04-17 23:29:01 +00:00
|
|
|
if let SiteKind::Settlement = &site.kind {
|
|
|
|
} else {
|
2020-04-15 19:41:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-11 15:50:40 +00:00
|
|
|
let radius = 48i32;
|
2020-04-11 00:09:01 +00:00
|
|
|
let wpos = site.center * Vec2::from(TerrainChunkSize::RECT_SIZE).map(|e: u32| e as i32);
|
2020-04-11 15:50:40 +00:00
|
|
|
|
|
|
|
// Flatten ground
|
2020-04-13 20:37:47 +00:00
|
|
|
let flatten_radius = 10.0;
|
2020-04-11 15:50:40 +00:00
|
|
|
if let Some(center_alt) = ctx.sim.get_alt_approx(wpos) {
|
2020-04-13 20:37:47 +00:00
|
|
|
for offs in Spiral2d::new().take(radius.pow(2) as usize) {
|
2020-04-17 23:29:01 +00:00
|
|
|
let center_alt = center_alt
|
|
|
|
+ if offs.magnitude_squared() <= 6i32.pow(2) {
|
|
|
|
16.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}; // Raise the town centre up a little
|
2020-04-13 20:37:47 +00:00
|
|
|
let pos = site.center + offs;
|
2020-04-17 23:29:01 +00:00
|
|
|
let factor = (1.0
|
|
|
|
- (site.center - pos).map(|e| e as f32).magnitude() / flatten_radius)
|
|
|
|
* 1.15;
|
2020-04-11 15:50:40 +00:00
|
|
|
ctx.sim
|
|
|
|
.get_mut(pos)
|
|
|
|
// Don't disrupt chunks that are near water
|
|
|
|
.filter(|chunk| !chunk.river.near_water())
|
|
|
|
.map(|chunk| {
|
|
|
|
let diff = Lerp::lerp_precise(chunk.alt, center_alt, factor) - chunk.alt;
|
|
|
|
chunk.alt += diff;
|
|
|
|
chunk.basement += diff;
|
|
|
|
chunk.rockiness = 0.0;
|
2020-04-17 15:46:08 +00:00
|
|
|
chunk.warp_factor = 0.0;
|
2020-04-11 15:50:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 21:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place sites in world
|
|
|
|
for site in this.sites.iter() {
|
|
|
|
let wpos = site.center * Vec2::from(TerrainChunkSize::RECT_SIZE).map(|e: u32| e as i32);
|
2020-04-11 15:50:40 +00:00
|
|
|
|
2020-04-15 19:41:40 +00:00
|
|
|
let world_site = match &site.kind {
|
2020-04-17 23:29:01 +00:00
|
|
|
SiteKind::Settlement => {
|
2020-04-19 12:12:08 +00:00
|
|
|
WorldSite::from(Settlement::generate(wpos, Some(ctx.sim), &mut ctx.rng))
|
2020-04-17 23:29:01 +00:00
|
|
|
},
|
|
|
|
SiteKind::Dungeon => {
|
2020-04-19 12:12:08 +00:00
|
|
|
WorldSite::from(Dungeon::generate(wpos, Some(ctx.sim), &mut ctx.rng))
|
2020-04-17 23:29:01 +00:00
|
|
|
},
|
2020-04-15 19:41:40 +00:00
|
|
|
};
|
2020-04-11 15:50:40 +00:00
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
let radius_chunks =
|
|
|
|
(world_site.radius() / TerrainChunkSize::RECT_SIZE.x as f32).ceil() as usize;
|
|
|
|
for pos in Spiral2d::new()
|
|
|
|
.map(|offs| site.center + offs)
|
|
|
|
.take((radius_chunks * 2).pow(2))
|
|
|
|
{
|
2020-04-11 00:09:01 +00:00
|
|
|
ctx.sim
|
|
|
|
.get_mut(pos)
|
2020-04-15 19:41:40 +00:00
|
|
|
.map(|chunk| chunk.sites.push(world_site.clone()));
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
2020-04-17 15:46:08 +00:00
|
|
|
log::info!("Placed site at {:?}", site.center);
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 15:46:08 +00:00
|
|
|
//this.display_info();
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-27 13:16:02 +00:00
|
|
|
this
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
pub fn place(&self, id: Id<Place>) -> &Place { self.places.get(id) }
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
pub fn sites(&self) -> impl Iterator<Item = &Site> + '_ { self.sites.iter() }
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
fn display_info(&self) {
|
|
|
|
for (id, civ) in self.civs.iter_ids() {
|
|
|
|
println!("# Civilisation {:?}", id);
|
|
|
|
println!("Name: {}", "<unnamed>");
|
|
|
|
println!("Homeland: {:#?}", self.places.get(civ.homeland));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (id, site) in self.sites.iter_ids() {
|
|
|
|
println!("# Site {:?}", id);
|
2020-03-28 18:16:19 +00:00
|
|
|
println!("{:#?}", site);
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:52:28 +00:00
|
|
|
/// Return the direct track between two places
|
2020-03-27 23:06:23 +00:00
|
|
|
fn track_between(&self, a: Id<Site>, b: Id<Site>) -> Option<Id<Track>> {
|
2020-03-27 18:52:28 +00:00
|
|
|
self.track_map
|
|
|
|
.get(&a)
|
|
|
|
.and_then(|dests| dests.get(&b))
|
2020-04-17 23:29:01 +00:00
|
|
|
.or_else(|| self.track_map.get(&b).and_then(|dests| dests.get(&a)))
|
2020-03-27 18:52:28 +00:00
|
|
|
.copied()
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
/// Return an iterator over a site's neighbors
|
2020-04-17 23:29:01 +00:00
|
|
|
fn neighbors(&self, site: Id<Site>) -> impl Iterator<Item = Id<Site>> + '_ {
|
|
|
|
let to = self
|
|
|
|
.track_map
|
|
|
|
.get(&site)
|
|
|
|
.map(|dests| dests.keys())
|
|
|
|
.into_iter()
|
|
|
|
.flatten();
|
|
|
|
let fro = self
|
|
|
|
.track_map
|
|
|
|
.iter()
|
|
|
|
.filter(move |(_, dests)| dests.contains_key(&site))
|
|
|
|
.map(|(p, _)| p);
|
2020-03-27 23:06:23 +00:00
|
|
|
to.chain(fro).filter(move |p| **p != site).copied()
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:52:28 +00:00
|
|
|
/// Find the cheapest route between two places
|
2020-03-27 23:06:23 +00:00
|
|
|
fn route_between(&self, a: Id<Site>, b: Id<Site>) -> Option<(Path<Id<Site>>, f32)> {
|
2020-04-17 23:29:01 +00:00
|
|
|
let heuristic = move |p: &Id<Site>| {
|
|
|
|
(self
|
|
|
|
.sites
|
|
|
|
.get(*p)
|
|
|
|
.center
|
|
|
|
.distance_squared(self.sites.get(b).center) as f32)
|
|
|
|
.sqrt()
|
|
|
|
};
|
2020-03-27 23:06:23 +00:00
|
|
|
let neighbors = |p: &Id<Site>| self.neighbors(*p);
|
2020-04-17 23:29:01 +00:00
|
|
|
let transition =
|
|
|
|
|a: &Id<Site>, b: &Id<Site>| self.tracks.get(self.track_between(*a, *b).unwrap()).cost;
|
2020-03-27 23:06:23 +00:00
|
|
|
let satisfied = |p: &Id<Site>| *p == b;
|
2020-03-27 18:52:28 +00:00
|
|
|
let mut astar = Astar::new(100, a, heuristic);
|
|
|
|
astar
|
|
|
|
.poll(100, heuristic, neighbors, transition, satisfied)
|
|
|
|
.into_path()
|
|
|
|
.and_then(|path| astar.get_cheapest_cost().map(|cost| (path, cost)))
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:16:02 +00:00
|
|
|
fn birth_civ(&mut self, ctx: &mut GenCtx<impl Rng>) -> Option<Id<Civ>> {
|
2020-03-27 23:06:23 +00:00
|
|
|
let site = attempt(5, || {
|
2020-03-27 13:16:02 +00:00
|
|
|
let loc = find_site_loc(ctx, None)?;
|
2020-04-15 19:41:40 +00:00
|
|
|
self.establish_site(ctx, loc, |place| Site {
|
|
|
|
kind: SiteKind::Settlement,
|
|
|
|
center: loc,
|
|
|
|
place,
|
|
|
|
|
|
|
|
population: 24.0,
|
|
|
|
|
|
|
|
stocks: Stocks::from_default(100.0),
|
|
|
|
surplus: Stocks::from_default(0.0),
|
|
|
|
values: Stocks::from_default(None),
|
|
|
|
|
|
|
|
labors: MapVec::from_default(0.01),
|
|
|
|
yields: MapVec::from_default(1.0),
|
|
|
|
productivity: MapVec::from_default(1.0),
|
|
|
|
|
|
|
|
last_exports: Stocks::from_default(0.0),
|
|
|
|
export_targets: Stocks::from_default(0.0),
|
|
|
|
trade_states: Stocks::default(),
|
|
|
|
coin: 1000.0,
|
|
|
|
})
|
2020-03-27 13:16:02 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let civ = self.civs.insert(Civ {
|
2020-03-27 23:06:23 +00:00
|
|
|
capital: site,
|
|
|
|
homeland: self.sites.get(site).place,
|
2020-03-27 13:16:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Some(civ)
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
fn establish_place(
|
|
|
|
&mut self,
|
|
|
|
ctx: &mut GenCtx<impl Rng>,
|
|
|
|
loc: Vec2<i32>,
|
|
|
|
area: Range<usize>,
|
|
|
|
) -> Option<Id<Place>> {
|
2020-03-27 13:16:02 +00:00
|
|
|
let mut dead = HashSet::new();
|
|
|
|
let mut alive = HashSet::new();
|
|
|
|
alive.insert(loc);
|
|
|
|
|
|
|
|
// Fill the surrounding area
|
2020-04-19 12:12:08 +00:00
|
|
|
while let Some(cloc) = alive.iter().choose(&mut ctx.rng).copied() {
|
2020-03-27 13:16:02 +00:00
|
|
|
for dir in CARDINALS.iter() {
|
|
|
|
if site_in_dir(&ctx.sim, cloc, *dir) {
|
|
|
|
let rloc = cloc + *dir;
|
2020-04-17 23:29:01 +00:00
|
|
|
if !dead.contains(&rloc)
|
|
|
|
&& ctx
|
|
|
|
.sim
|
|
|
|
.get(rloc)
|
|
|
|
.map(|c| c.place.is_none())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2020-03-27 13:16:02 +00:00
|
|
|
alive.insert(rloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alive.remove(&cloc);
|
|
|
|
dead.insert(cloc);
|
|
|
|
|
|
|
|
if dead.len() + alive.len() >= area.end {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure the place is large enough
|
|
|
|
if dead.len() + alive.len() <= area.start {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:52:28 +00:00
|
|
|
let place = self.places.insert(Place {
|
|
|
|
center: loc,
|
2020-03-27 23:06:23 +00:00
|
|
|
nat_res: NaturalResources::default(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Write place to map
|
|
|
|
for cell in dead.union(&alive) {
|
|
|
|
if let Some(chunk) = ctx.sim.get_mut(*cell) {
|
|
|
|
chunk.place = Some(place);
|
|
|
|
self.places.get_mut(place).nat_res.include_chunk(ctx, *cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(place)
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
fn establish_site(
|
|
|
|
&mut self,
|
|
|
|
ctx: &mut GenCtx<impl Rng>,
|
|
|
|
loc: Vec2<i32>,
|
|
|
|
site_fn: impl FnOnce(Id<Place>) -> Site,
|
|
|
|
) -> Option<Id<Site>> {
|
2020-03-27 23:06:23 +00:00
|
|
|
const SITE_AREA: Range<usize> = 64..256;
|
|
|
|
|
|
|
|
let place = match ctx.sim.get(loc).and_then(|site| site.place) {
|
|
|
|
Some(place) => place,
|
|
|
|
None => self.establish_place(ctx, loc, SITE_AREA)?,
|
|
|
|
};
|
|
|
|
|
2020-04-15 19:41:40 +00:00
|
|
|
let site = self.sites.insert(site_fn(place));
|
2020-03-27 18:52:28 +00:00
|
|
|
|
2020-03-27 13:16:02 +00:00
|
|
|
// Find neighbors
|
2020-03-27 18:52:28 +00:00
|
|
|
const MAX_NEIGHBOR_DISTANCE: f32 = 250.0;
|
2020-04-17 23:29:01 +00:00
|
|
|
let mut nearby = self
|
|
|
|
.sites
|
2020-03-27 13:16:02 +00:00
|
|
|
.iter_ids()
|
|
|
|
.map(|(id, p)| (id, (p.center.distance_squared(loc) as f32).sqrt()))
|
|
|
|
.filter(|(p, dist)| *dist < MAX_NEIGHBOR_DISTANCE)
|
|
|
|
.collect::<Vec<_>>();
|
2020-03-27 18:52:28 +00:00
|
|
|
nearby.sort_by_key(|(_, dist)| *dist as i32);
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
for (nearby, _) in nearby.into_iter().take(5) {
|
2020-03-27 18:52:28 +00:00
|
|
|
// Find a novel path
|
2020-03-27 23:06:23 +00:00
|
|
|
if let Some((path, cost)) = find_path(ctx, loc, self.sites.get(nearby).center) {
|
2020-03-27 18:52:28 +00:00
|
|
|
// Find a path using existing paths
|
|
|
|
if self
|
2020-03-27 23:06:23 +00:00
|
|
|
.route_between(site, nearby)
|
2020-03-27 18:52:28 +00:00
|
|
|
// If the novel path isn't efficient compared to existing routes, don't use it
|
|
|
|
.filter(|(_, route_cost)| *route_cost < cost * 3.0)
|
|
|
|
.is_none()
|
|
|
|
{
|
2020-04-17 23:29:01 +00:00
|
|
|
let track = self.tracks.insert(Track { cost, path });
|
2020-03-27 18:52:28 +00:00
|
|
|
self.track_map
|
2020-03-27 23:06:23 +00:00
|
|
|
.entry(site)
|
2020-03-27 18:52:28 +00:00
|
|
|
.or_default()
|
|
|
|
.insert(nearby, track);
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
Some(site)
|
|
|
|
}
|
|
|
|
|
2020-03-28 18:16:19 +00:00
|
|
|
fn tick(&mut self, ctx: &mut GenCtx<impl Rng>, years: f32) {
|
|
|
|
for site in self.sites.iter_mut() {
|
2020-03-30 16:46:44 +00:00
|
|
|
site.simulate(years, &self.places.get(site.place).nat_res);
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trade stocks
|
2020-04-01 20:38:01 +00:00
|
|
|
// let mut stocks = TRADE_STOCKS;
|
2020-04-17 23:29:01 +00:00
|
|
|
// stocks.shuffle(ctx.rng); // Give each stock a chance to be traded
|
|
|
|
// first for stock in stocks.iter().copied() {
|
2020-04-01 20:38:01 +00:00
|
|
|
// let mut sell_orders = self.sites
|
|
|
|
// .iter_ids()
|
|
|
|
// .map(|(id, site)| (id, {
|
|
|
|
// econ::SellOrder {
|
2020-04-17 23:29:01 +00:00
|
|
|
// quantity:
|
|
|
|
// site.export_targets[stock].max(0.0).min(site.stocks[stock]),
|
|
|
|
// price:
|
|
|
|
// site.trade_states[stock].sell_belief.choose_price(ctx) * 1.25, //
|
|
|
|
// Trade cost q_sold: 0.0,
|
2020-04-01 20:38:01 +00:00
|
|
|
// }
|
|
|
|
// }))
|
|
|
|
// .filter(|(_, order)| order.quantity > 0.0)
|
|
|
|
// .collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// let mut sites = self.sites
|
|
|
|
// .ids()
|
|
|
|
// .collect::<Vec<_>>();
|
|
|
|
// sites.shuffle(ctx.rng); // Give all sites a chance to buy first
|
|
|
|
// for site in sites {
|
|
|
|
// let (max_spend, max_price, max_import) = {
|
|
|
|
// let site = self.sites.get(site);
|
|
|
|
// let budget = site.coin * 0.5;
|
2020-04-17 23:29:01 +00:00
|
|
|
// let total_value = site.values.iter().map(|(_, v)|
|
|
|
|
// (*v).unwrap_or(0.0)).sum::<f32>(); (
|
|
|
|
// 100000.0,//(site.values[stock].unwrap_or(0.1) /
|
2020-04-19 12:12:08 +00:00
|
|
|
// total_value * budget).min(budget),
|
|
|
|
// site.trade_states[stock].buy_belief.price,
|
2020-04-17 23:29:01 +00:00
|
|
|
// -site.export_targets[stock].min(0.0), )
|
2020-04-01 20:38:01 +00:00
|
|
|
// };
|
|
|
|
// let (quantity, spent) = econ::buy_units(ctx, sell_orders
|
|
|
|
// .iter_mut()
|
2020-04-17 23:29:01 +00:00
|
|
|
// .filter(|(id, _)| site != *id && self.track_between(site,
|
|
|
|
// *id).is_some()) .map(|(_, order)| order),
|
2020-04-01 20:38:01 +00:00
|
|
|
// max_import,
|
|
|
|
// 1000000.0, // Max price TODO
|
|
|
|
// max_spend,
|
|
|
|
// );
|
|
|
|
// let mut site = self.sites.get_mut(site);
|
|
|
|
// site.coin -= spent;
|
|
|
|
// if quantity > 0.0 {
|
|
|
|
// site.stocks[stock] += quantity;
|
|
|
|
// site.last_exports[stock] = -quantity;
|
2020-04-17 23:29:01 +00:00
|
|
|
// site.trade_states[stock].buy_belief.update_buyer(years,
|
|
|
|
// spent / quantity); println!("Belief: {:?}",
|
|
|
|
// site.trade_states[stock].buy_belief); }
|
2020-04-01 20:38:01 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// for (site, order) in sell_orders {
|
|
|
|
// let mut site = self.sites.get_mut(site);
|
|
|
|
// site.coin += order.q_sold * order.price;
|
|
|
|
// if order.q_sold > 0.0 {
|
|
|
|
// site.stocks[stock] -= order.q_sold;
|
|
|
|
// site.last_exports[stock] = order.q_sold;
|
2020-04-19 12:12:08 +00:00
|
|
|
//
|
2020-04-17 23:29:01 +00:00
|
|
|
// site.trade_states[stock].sell_belief.update_seller(order.q_sold /
|
|
|
|
// order.quantity); }
|
2020-04-01 20:38:01 +00:00
|
|
|
// }
|
|
|
|
// }
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to find a path between two locations
|
2020-04-17 23:29:01 +00:00
|
|
|
fn find_path(
|
|
|
|
ctx: &mut GenCtx<impl Rng>,
|
|
|
|
a: Vec2<i32>,
|
|
|
|
b: Vec2<i32>,
|
|
|
|
) -> Option<(Path<Vec2<i32>>, f32)> {
|
2020-03-27 13:16:02 +00:00
|
|
|
let sim = &ctx.sim;
|
|
|
|
let heuristic = move |l: &Vec2<i32>| (l.distance_squared(b) as f32).sqrt();
|
|
|
|
let neighbors = |l: &Vec2<i32>| {
|
|
|
|
let l = *l;
|
2020-04-17 23:29:01 +00:00
|
|
|
DIAGONALS
|
|
|
|
.iter()
|
|
|
|
.filter(move |dir| walk_in_dir(sim, l, **dir).is_some())
|
|
|
|
.map(move |dir| l + *dir)
|
2020-03-27 13:16:02 +00:00
|
|
|
};
|
2020-04-17 23:29:01 +00:00
|
|
|
let transition =
|
|
|
|
|a: &Vec2<i32>, b: &Vec2<i32>| 1.0 + walk_in_dir(sim, *a, *b - *a).unwrap_or(10000.0);
|
2020-03-27 13:16:02 +00:00
|
|
|
let satisfied = |l: &Vec2<i32>| *l == b;
|
2020-03-27 18:52:28 +00:00
|
|
|
let mut astar = Astar::new(20000, a, heuristic);
|
|
|
|
astar
|
|
|
|
.poll(20000, heuristic, neighbors, transition, satisfied)
|
2020-03-27 13:16:02 +00:00
|
|
|
.into_path()
|
2020-03-27 18:52:28 +00:00
|
|
|
.and_then(|path| astar.get_cheapest_cost().map(|cost| (path, cost)))
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
/// Return true if travel between a location and a chunk next to it is permitted
|
|
|
|
/// (TODO: by whom?)
|
2020-03-27 13:16:02 +00:00
|
|
|
fn walk_in_dir(sim: &WorldSim, a: Vec2<i32>, dir: Vec2<i32>) -> Option<f32> {
|
2020-04-17 23:29:01 +00:00
|
|
|
if loc_suitable_for_walking(sim, a) && loc_suitable_for_walking(sim, a + dir) {
|
2020-03-27 13:16:02 +00:00
|
|
|
let a_alt = sim.get(a)?.alt;
|
|
|
|
let b_alt = sim.get(a + dir)?.alt;
|
2020-03-27 23:06:23 +00:00
|
|
|
Some((b_alt - a_alt).abs() / 2.5)
|
2020-03-27 13:16:02 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if a position is suitable for walking on
|
|
|
|
fn loc_suitable_for_walking(sim: &WorldSim, loc: Vec2<i32>) -> bool {
|
|
|
|
if let Some(chunk) = sim.get(loc) {
|
|
|
|
!chunk.river.is_ocean() && !chunk.river.is_lake()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
/// Return true if a site could be constructed between a location and a chunk
|
|
|
|
/// next to it is permitted (TODO: by whom?)
|
2020-03-27 13:16:02 +00:00
|
|
|
fn site_in_dir(sim: &WorldSim, a: Vec2<i32>, dir: Vec2<i32>) -> bool {
|
2020-04-17 23:29:01 +00:00
|
|
|
loc_suitable_for_site(sim, a) && loc_suitable_for_site(sim, a + dir)
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
/// Return true if a position is suitable for site construction (TODO:
|
|
|
|
/// criteria?)
|
2020-03-27 13:16:02 +00:00
|
|
|
fn loc_suitable_for_site(sim: &WorldSim, loc: Vec2<i32>) -> bool {
|
|
|
|
if let Some(chunk) = sim.get(loc) {
|
2020-04-17 23:29:01 +00:00
|
|
|
!chunk.river.is_ocean()
|
|
|
|
&& !chunk.river.is_lake()
|
|
|
|
&& sim
|
|
|
|
.get_gradient_approx(loc)
|
|
|
|
.map(|grad| grad < 1.0)
|
|
|
|
.unwrap_or(false)
|
2020-03-27 13:16:02 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to search for a location that's suitable for site construction
|
|
|
|
fn find_site_loc(ctx: &mut GenCtx<impl Rng>, near: Option<(Vec2<i32>, f32)>) -> Option<Vec2<i32>> {
|
|
|
|
const MAX_ATTEMPTS: usize = 100;
|
|
|
|
let mut loc = None;
|
|
|
|
for _ in 0..MAX_ATTEMPTS {
|
|
|
|
let test_loc = loc.unwrap_or_else(|| match near {
|
2020-04-17 23:29:01 +00:00
|
|
|
Some((origin, dist)) => {
|
|
|
|
origin
|
|
|
|
+ (Vec2::new(ctx.rng.gen_range(-1.0, 1.0), ctx.rng.gen_range(-1.0, 1.0))
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* ctx.rng.gen::<f32>()
|
|
|
|
* dist)
|
|
|
|
.map(|e| e as i32)
|
|
|
|
},
|
2020-03-27 13:16:02 +00:00
|
|
|
None => Vec2::new(
|
|
|
|
ctx.rng.gen_range(0, ctx.sim.get_size().x as i32),
|
|
|
|
ctx.rng.gen_range(0, ctx.sim.get_size().y as i32),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
if loc_suitable_for_site(&ctx.sim, test_loc) {
|
|
|
|
return Some(test_loc);
|
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
loc = ctx.sim.get(test_loc).and_then(|c| {
|
|
|
|
Some(
|
|
|
|
c.downhill?
|
|
|
|
.map2(Vec2::from(TerrainChunkSize::RECT_SIZE), |e, sz: u32| {
|
|
|
|
e / (sz as i32)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
});
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Civ {
|
2020-03-27 23:06:23 +00:00
|
|
|
capital: Id<Site>,
|
2020-03-27 13:16:02 +00:00
|
|
|
homeland: Id<Place>,
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:23 +00:00
|
|
|
#[derive(Debug)]
|
2020-03-27 13:16:02 +00:00
|
|
|
pub struct Place {
|
|
|
|
center: Vec2<i32>,
|
2020-03-27 23:06:23 +00:00
|
|
|
nat_res: NaturalResources,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Productive capacity per year
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct NaturalResources {
|
|
|
|
wood: f32,
|
2020-03-30 16:46:44 +00:00
|
|
|
rock: f32,
|
2020-03-27 23:06:23 +00:00
|
|
|
river: f32,
|
|
|
|
farmland: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NaturalResources {
|
|
|
|
fn include_chunk(&mut self, ctx: &mut GenCtx<impl Rng>, loc: Vec2<i32>) {
|
2020-04-17 23:29:01 +00:00
|
|
|
let chunk = if let Some(chunk) = ctx.sim.get(loc) {
|
|
|
|
chunk
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2020-03-27 23:06:23 +00:00
|
|
|
|
|
|
|
self.wood += chunk.tree_density;
|
2020-03-30 16:46:44 +00:00
|
|
|
self.rock += chunk.rockiness;
|
2020-03-29 19:48:51 +00:00
|
|
|
self.river += if chunk.river.is_river() { 5.0 } else { 0.0 };
|
2020-04-17 23:29:01 +00:00
|
|
|
self.farmland += if chunk.humidity > 0.35
|
|
|
|
&& chunk.temp > -0.3
|
|
|
|
&& chunk.temp < 0.75
|
|
|
|
&& chunk.chaos < 0.5
|
|
|
|
&& ctx
|
|
|
|
.sim
|
|
|
|
.get_gradient_approx(loc)
|
|
|
|
.map(|grad| grad < 0.7)
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
1.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
2020-03-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 18:52:28 +00:00
|
|
|
pub struct Track {
|
2020-04-17 23:29:01 +00:00
|
|
|
/// Cost of using this track relative to other paths. This cost is an
|
|
|
|
/// arbitrary unit and doesn't make sense unless compared to other track
|
|
|
|
/// costs.
|
2020-03-27 18:52:28 +00:00
|
|
|
cost: f32,
|
2020-03-27 13:16:02 +00:00
|
|
|
path: Path<Vec2<i32>>,
|
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Site {
|
|
|
|
kind: SiteKind,
|
|
|
|
center: Vec2<i32>,
|
2020-03-28 18:16:19 +00:00
|
|
|
pub place: Id<Place>,
|
|
|
|
|
|
|
|
population: f32,
|
2020-03-30 16:46:44 +00:00
|
|
|
|
|
|
|
// Total amount of each stock
|
2020-03-28 18:16:19 +00:00
|
|
|
stocks: Stocks<f32>,
|
2020-03-30 22:45:41 +00:00
|
|
|
// Surplus stock compared to demand orders
|
|
|
|
surplus: Stocks<f32>,
|
2020-03-30 16:46:44 +00:00
|
|
|
// For some goods, such a goods without any supply, it doesn't make sense to talk about value
|
|
|
|
values: Stocks<Option<f32>>,
|
|
|
|
|
|
|
|
// Proportion of individuals dedicated to an industry
|
|
|
|
labors: MapVec<Occupation, f32>,
|
|
|
|
// Per worker, per year, of their output good
|
|
|
|
yields: MapVec<Occupation, f32>,
|
2020-04-01 20:38:01 +00:00
|
|
|
productivity: MapVec<Occupation, f32>,
|
2020-03-30 16:46:44 +00:00
|
|
|
|
2020-04-01 20:38:01 +00:00
|
|
|
last_exports: Stocks<f32>,
|
|
|
|
export_targets: Stocks<f32>,
|
2020-03-30 22:45:41 +00:00
|
|
|
trade_states: Stocks<TradeState>,
|
|
|
|
coin: f32,
|
2020-03-30 16:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Site {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self.kind {
|
|
|
|
SiteKind::Settlement => writeln!(f, "Settlement")?,
|
2020-04-15 19:41:40 +00:00
|
|
|
SiteKind::Dungeon => writeln!(f, "Dungeon")?,
|
2020-03-30 16:46:44 +00:00
|
|
|
}
|
|
|
|
writeln!(f, "- population: {}", self.population.floor() as u32)?;
|
2020-03-30 22:45:41 +00:00
|
|
|
writeln!(f, "- coin: {}", self.coin.floor() as u32)?;
|
2020-03-30 16:46:44 +00:00
|
|
|
writeln!(f, "Stocks")?;
|
|
|
|
for (stock, q) in self.stocks.iter() {
|
|
|
|
writeln!(f, "- {}: {}", stock, q.floor())?;
|
|
|
|
}
|
2020-04-01 20:38:01 +00:00
|
|
|
writeln!(f, "Values")?;
|
|
|
|
for stock in TRADE_STOCKS.iter() {
|
2020-04-17 23:29:01 +00:00
|
|
|
writeln!(
|
|
|
|
f,
|
|
|
|
"- {}: {}",
|
|
|
|
stock,
|
|
|
|
self.values[*stock]
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.unwrap_or_else(|| "N/A".to_string())
|
|
|
|
)?;
|
2020-03-30 16:46:44 +00:00
|
|
|
}
|
|
|
|
writeln!(f, "Laborers")?;
|
|
|
|
for (labor, n) in self.labors.iter() {
|
|
|
|
writeln!(f, "- {}: {}", labor, (*n * self.population).floor() as u32)?;
|
|
|
|
}
|
2020-04-01 20:38:01 +00:00
|
|
|
writeln!(f, "Export targets")?;
|
|
|
|
for (stock, n) in self.export_targets.iter() {
|
|
|
|
writeln!(f, "- {}: {}", stock, n)?;
|
|
|
|
}
|
2020-03-30 16:46:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SiteKind {
|
|
|
|
Settlement,
|
2020-04-15 19:41:40 +00:00
|
|
|
Dungeon,
|
2020-03-29 19:48:51 +00:00
|
|
|
}
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
impl Site {
|
2020-03-30 16:46:44 +00:00
|
|
|
pub fn simulate(&mut self, years: f32, nat_res: &NaturalResources) {
|
|
|
|
// Insert natural resources into the economy
|
|
|
|
if self.stocks[FISH] < nat_res.river {
|
|
|
|
self.stocks[FISH] = nat_res.river;
|
|
|
|
}
|
|
|
|
if self.stocks[WHEAT] < nat_res.farmland {
|
|
|
|
self.stocks[WHEAT] = nat_res.farmland;
|
|
|
|
}
|
|
|
|
if self.stocks[LOGS] < nat_res.wood {
|
|
|
|
self.stocks[LOGS] = nat_res.wood;
|
|
|
|
}
|
|
|
|
if self.stocks[GAME] < nat_res.wood {
|
|
|
|
self.stocks[GAME] = nat_res.wood;
|
|
|
|
}
|
|
|
|
if self.stocks[ROCK] < nat_res.rock {
|
|
|
|
self.stocks[ROCK] = nat_res.rock;
|
2020-03-29 19:48:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
let orders = vec![
|
2020-04-01 20:38:01 +00:00
|
|
|
(None, vec![(FOOD, 0.5)]),
|
|
|
|
(Some(COOK), vec![(FLOUR, 16.0), (MEAT, 4.0), (WOOD, 3.0)]),
|
2020-03-30 16:46:44 +00:00
|
|
|
(Some(LUMBERJACK), vec![(LOGS, 4.5)]),
|
|
|
|
(Some(MINER), vec![(ROCK, 7.5)]),
|
|
|
|
(Some(FISHER), vec![(FISH, 4.0)]),
|
|
|
|
(Some(HUNTER), vec![(GAME, 4.0)]),
|
|
|
|
(Some(FARMER), vec![(WHEAT, 4.0)]),
|
|
|
|
]
|
2020-04-17 23:29:01 +00:00
|
|
|
.into_iter()
|
|
|
|
.collect::<HashMap<_, Vec<(Stock, f32)>>>();
|
2020-03-30 16:46:44 +00:00
|
|
|
|
2020-04-01 20:38:01 +00:00
|
|
|
// Per labourer, per year
|
|
|
|
let production = Stocks::from_list(&[
|
|
|
|
(FARMER, (FLOUR, 2.0)),
|
|
|
|
(LUMBERJACK, (WOOD, 1.5)),
|
|
|
|
(MINER, (STONE, 0.6)),
|
|
|
|
(FISHER, (MEAT, 3.0)),
|
|
|
|
(HUNTER, (MEAT, 0.25)),
|
|
|
|
(COOK, (FOOD, 20.0)),
|
|
|
|
]);
|
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
let mut demand = Stocks::from_default(0.0);
|
|
|
|
for (labor, orders) in &orders {
|
2020-04-17 23:29:01 +00:00
|
|
|
let scale = if let Some(labor) = labor {
|
|
|
|
self.labors[*labor]
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
} * self.population;
|
2020-03-30 16:46:44 +00:00
|
|
|
for (stock, amount) in orders {
|
|
|
|
demand[*stock] += *amount * scale;
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-04-01 20:38:01 +00:00
|
|
|
let mut supply = Stocks::from_default(0.0);
|
|
|
|
for (labor, (output_stock, _)) in production.iter() {
|
|
|
|
supply[*output_stock] += self.yields[labor] * self.labors[labor] * self.population;
|
|
|
|
}
|
|
|
|
|
|
|
|
let last_exports = &self.last_exports;
|
|
|
|
let stocks = &self.stocks;
|
2020-04-17 23:29:01 +00:00
|
|
|
self.surplus = demand
|
|
|
|
.clone()
|
|
|
|
.map(|stock, tgt| supply[stock] + stocks[stock] - demand[stock] - last_exports[stock]);
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
// Update values according to the surplus of each stock
|
2020-03-30 22:45:41 +00:00
|
|
|
let values = &mut self.values;
|
|
|
|
self.surplus.iter().for_each(|(stock, surplus)| {
|
2020-03-30 23:03:56 +00:00
|
|
|
let val = 3.5f32.powf(1.0 - *surplus / demand[stock]);
|
2020-04-17 23:29:01 +00:00
|
|
|
values[stock] = if val > 0.001 && val < 1000.0 {
|
|
|
|
Some(val)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-03-30 16:46:44 +00:00
|
|
|
});
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-04-01 20:38:01 +00:00
|
|
|
// Update export targets based on relative values
|
2020-04-17 23:29:01 +00:00
|
|
|
let value_avg = values
|
|
|
|
.iter()
|
|
|
|
.map(|(_, v)| (*v).unwrap_or(0.0))
|
|
|
|
.sum::<f32>()
|
|
|
|
.max(0.01)
|
2020-04-01 20:38:01 +00:00
|
|
|
/ values.iter().filter(|(_, v)| v.is_some()).count() as f32;
|
|
|
|
let export_targets = &mut self.export_targets;
|
|
|
|
let last_exports = &self.last_exports;
|
|
|
|
let trade_states = &self.trade_states;
|
|
|
|
self.values.iter().for_each(|(stock, value)| {
|
|
|
|
let rvalue = (*value).map(|v| v - value_avg).unwrap_or(0.0);
|
|
|
|
//let factor = if export_targets[stock] > 0.0 { 1.0 / rvalue } else { rvalue };
|
2020-04-17 23:29:01 +00:00
|
|
|
export_targets[stock] = last_exports[stock] - rvalue * 0.1; // + (trade_states[stock].sell_belief.price - trade_states[stock].buy_belief.price) * 0.025;
|
2020-04-01 20:38:01 +00:00
|
|
|
});
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
let population = self.population;
|
2020-03-29 19:48:51 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
// Redistribute workforce according to relative good values
|
|
|
|
let labor_ratios = production.clone().map(|labor, (output_stock, _)| {
|
2020-04-01 20:38:01 +00:00
|
|
|
self.productivity[labor] * demand[output_stock] / supply[output_stock].max(0.001)
|
2020-03-30 16:46:44 +00:00
|
|
|
});
|
|
|
|
let labor_ratio_sum = labor_ratios.iter().map(|(_, r)| *r).sum::<f32>().max(0.01);
|
|
|
|
production.iter().for_each(|(labor, _)| {
|
2020-04-01 20:38:01 +00:00
|
|
|
let smooth = 0.8;
|
2020-04-17 23:29:01 +00:00
|
|
|
self.labors[labor] = smooth * self.labors[labor]
|
|
|
|
+ (1.0 - smooth)
|
|
|
|
* (labor_ratios[labor].max(labor_ratio_sum / 1000.0) / labor_ratio_sum);
|
2020-03-30 16:46:44 +00:00
|
|
|
});
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
// Production
|
|
|
|
let stocks_before = self.stocks.clone();
|
|
|
|
for (labor, orders) in orders.iter() {
|
2020-04-17 23:29:01 +00:00
|
|
|
let scale = if let Some(labor) = labor {
|
|
|
|
self.labors[*labor]
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
} * population;
|
|
|
|
|
|
|
|
// For each order, we try to find the minimum satisfaction rate - this limits
|
|
|
|
// how much we can produce! For example, if we need 0.25 fish and
|
|
|
|
// 0.75 oats to make 1 unit of food, but only 0.5 units of oats are
|
|
|
|
// available then we only need to consume 2/3rds
|
2020-03-30 16:46:44 +00:00
|
|
|
// of other ingredients and leave the rest in stock
|
2020-04-01 20:38:01 +00:00
|
|
|
// In effect, this is the productivity
|
|
|
|
let productivity = orders
|
2020-03-30 16:46:44 +00:00
|
|
|
.iter()
|
|
|
|
.map(|(stock, amount)| {
|
|
|
|
// What quantity is this order requesting?
|
|
|
|
let quantity = *amount * scale;
|
|
|
|
// What proportion of this order is the economy able to satisfy?
|
|
|
|
let satisfaction = (stocks_before[*stock] / demand[*stock]).min(1.0);
|
|
|
|
satisfaction
|
|
|
|
})
|
|
|
|
.min_by(|a, b| a.partial_cmp(b).unwrap())
|
2020-04-17 23:29:01 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
panic!("Industry {:?} requires at least one input order", labor)
|
|
|
|
});
|
2020-03-30 16:46:44 +00:00
|
|
|
|
|
|
|
for (stock, amount) in orders {
|
|
|
|
// What quantity is this order requesting?
|
|
|
|
let quantity = *amount * scale;
|
|
|
|
// What amount gets actually used in production?
|
2020-04-01 20:38:01 +00:00
|
|
|
let used = quantity * productivity;
|
2020-03-30 16:46:44 +00:00
|
|
|
|
|
|
|
// Deplete stocks accordingly
|
2020-03-30 22:45:41 +00:00
|
|
|
self.stocks[*stock] = (self.stocks[*stock] - used).max(0.0);
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
// Industries produce things
|
|
|
|
if let Some(labor) = labor {
|
|
|
|
let (stock, rate) = production[*labor];
|
2020-03-30 22:45:41 +00:00
|
|
|
let workers = self.labors[*labor] * population;
|
2020-04-01 20:38:01 +00:00
|
|
|
let final_rate = rate;
|
|
|
|
let yield_per_worker = productivity * final_rate;
|
|
|
|
self.yields[*labor] = yield_per_worker;
|
|
|
|
self.productivity[*labor] = productivity;
|
2020-03-30 22:45:41 +00:00
|
|
|
self.stocks[stock] += yield_per_worker * workers.powf(1.1);
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
2020-03-30 16:46:44 +00:00
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-30 22:45:41 +00:00
|
|
|
// Denature stocks
|
|
|
|
self.stocks.iter_mut().for_each(|(_, v)| *v *= 0.9);
|
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
// Births/deaths
|
|
|
|
const NATURAL_BIRTH_RATE: f32 = 0.15;
|
|
|
|
const DEATH_RATE: f32 = 0.05;
|
2020-04-17 23:29:01 +00:00
|
|
|
let birth_rate = if self.surplus[FOOD] > 0.0 {
|
|
|
|
NATURAL_BIRTH_RATE
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-03-30 16:46:44 +00:00
|
|
|
self.population += years * self.population * (birth_rate - DEATH_RATE);
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
type Occupation = &'static str;
|
|
|
|
const FARMER: Occupation = "farmer";
|
|
|
|
const LUMBERJACK: Occupation = "lumberjack";
|
|
|
|
const MINER: Occupation = "miner";
|
|
|
|
const FISHER: Occupation = "fisher";
|
2020-03-30 16:46:44 +00:00
|
|
|
const HUNTER: Occupation = "hunter";
|
|
|
|
const COOK: Occupation = "cook";
|
2020-03-29 19:48:51 +00:00
|
|
|
|
2020-03-28 18:16:19 +00:00
|
|
|
type Stock = &'static str;
|
2020-03-30 16:46:44 +00:00
|
|
|
const WHEAT: Stock = "wheat";
|
|
|
|
const FLOUR: Stock = "flour";
|
|
|
|
const MEAT: Stock = "meat";
|
|
|
|
const FISH: Stock = "fish";
|
|
|
|
const GAME: Stock = "game";
|
2020-03-28 18:16:19 +00:00
|
|
|
const FOOD: Stock = "food";
|
2020-03-30 16:46:44 +00:00
|
|
|
const LOGS: Stock = "logs";
|
2020-03-28 18:16:19 +00:00
|
|
|
const WOOD: Stock = "wood";
|
|
|
|
const ROCK: Stock = "rock";
|
2020-03-30 16:46:44 +00:00
|
|
|
const STONE: Stock = "stone";
|
2020-04-17 23:29:01 +00:00
|
|
|
const TRADE_STOCKS: [Stock; 5] = [FLOUR, MEAT, FOOD, WOOD, STONE];
|
2020-03-28 18:16:19 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct TradeState {
|
|
|
|
buy_belief: econ::Belief,
|
|
|
|
sell_belief: econ::Belief,
|
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
|
2020-03-28 18:16:19 +00:00
|
|
|
impl Default for TradeState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
buy_belief: econ::Belief {
|
|
|
|
price: 1.0,
|
|
|
|
confidence: 0.25,
|
|
|
|
},
|
|
|
|
sell_belief: econ::Belief {
|
|
|
|
price: 1.0,
|
|
|
|
confidence: 0.25,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
pub type Stocks<T> = MapVec<Stock, T>;
|
|
|
|
|
2020-03-28 18:16:19 +00:00
|
|
|
#[derive(Default, Clone, Debug)]
|
2020-03-29 19:48:51 +00:00
|
|
|
pub struct MapVec<K, T> {
|
2020-03-29 20:46:19 +00:00
|
|
|
entries: HashMap<K, T>,
|
2020-03-30 16:46:44 +00:00
|
|
|
default: T,
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
impl<K: Copy + Eq + Hash, T: Default + Clone> MapVec<K, T> {
|
2020-04-17 23:29:01 +00:00
|
|
|
pub fn from_list<'a>(i: impl IntoIterator<Item = &'a (K, T)>) -> Self
|
|
|
|
where
|
|
|
|
K: 'a,
|
|
|
|
T: 'a,
|
2020-03-28 18:16:19 +00:00
|
|
|
{
|
|
|
|
Self {
|
2020-03-29 20:46:19 +00:00
|
|
|
entries: i.into_iter().cloned().collect(),
|
2020-03-30 16:46:44 +00:00
|
|
|
default: T::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_default(default: T) -> Self {
|
|
|
|
Self {
|
|
|
|
entries: HashMap::default(),
|
|
|
|
default,
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 20:46:19 +00:00
|
|
|
pub fn get_mut(&mut self, entry: K) -> &mut T {
|
2020-03-30 16:46:44 +00:00
|
|
|
let default = &self.default;
|
2020-04-17 23:29:01 +00:00
|
|
|
self.entries.entry(entry).or_insert_with(|| default.clone())
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
pub fn get(&self, entry: K) -> &T { self.entries.get(&entry).unwrap_or(&self.default) }
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-30 16:46:44 +00:00
|
|
|
pub fn map<U: Default>(mut self, mut f: impl FnMut(K, T) -> U) -> MapVec<K, U> {
|
|
|
|
MapVec {
|
2020-04-17 23:29:01 +00:00
|
|
|
entries: self
|
|
|
|
.entries
|
|
|
|
.into_iter()
|
|
|
|
.map(|(s, v)| (s.clone(), f(s, v)))
|
|
|
|
.collect(),
|
2020-03-30 16:46:44 +00:00
|
|
|
default: U::default(),
|
|
|
|
}
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (K, &T)> + '_ {
|
2020-03-29 20:46:19 +00:00
|
|
|
self.entries.iter().map(|(s, v)| (*s, v))
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 23:29:01 +00:00
|
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = (K, &mut T)> + '_ {
|
2020-03-29 20:46:19 +00:00
|
|
|
self.entries.iter_mut().map(|(s, v)| (*s, v))
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::Index<K> for MapVec<K, T> {
|
2020-03-28 18:16:19 +00:00
|
|
|
type Output = T;
|
2020-04-17 23:29:01 +00:00
|
|
|
|
2020-03-29 20:46:19 +00:00
|
|
|
fn index(&self, entry: K) -> &Self::Output { self.get(entry) }
|
2020-03-27 23:06:23 +00:00
|
|
|
}
|
2020-03-28 18:16:19 +00:00
|
|
|
|
2020-03-29 19:48:51 +00:00
|
|
|
impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::IndexMut<K> for MapVec<K, T> {
|
2020-03-29 20:46:19 +00:00
|
|
|
fn index_mut(&mut self, entry: K) -> &mut Self::Output { self.get_mut(entry) }
|
2020-03-28 18:16:19 +00:00
|
|
|
}
|