Skip to content

Commit

Permalink
remove hecs
Browse files Browse the repository at this point in the history
no need for hecs as I don't do fancy ECS things

allows much simpler serialization and determinism guarantees

still need to fork slotmap to serialize the freelist and we're good on
that front
  • Loading branch information
Uriopass committed Aug 2, 2023
1 parent 154cc4d commit d43e998
Show file tree
Hide file tree
Showing 42 changed files with 1,644 additions and 1,619 deletions.
67 changes: 37 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ resolver = "2"
default-members = ["native_app"]

[workspace.dependencies]
hecs = "0.10.0"
egui = "0.22.0"
flat_spatial = "0.5.1"
egui-wgpu = "0.22.0"
Expand Down
2 changes: 1 addition & 1 deletion egregoria/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ common = { path = "../common" }
slotmap = { version = "1.0.2", default-features = false, features = ["serde", "unstable"] }
rayon = "1.6"
atomic_refcell = "0.1.6"
hecs = { workspace = true, features=["column-serialize"] }
profiling = "1.0.5"
inline_tweak = { version = "1.0.9", features = ["release_tweak"] }
pathfinding = "4.2.1"
serde-big-array = "0.5.0"
lazy_static = "1.4.0"
arc-swap = "1.3.0"
derive_more = "0.99.17"

[dev-dependencies]
easybench = "1.1.0"
Expand Down
16 changes: 9 additions & 7 deletions egregoria/src/economy/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub enum TradeTarget {
ExternalTrade,
}

debug_inspect_impl!(TradeTarget);

impl TradeTarget {
pub(crate) fn soul(self) -> SoulID {
match self {
Expand All @@ -88,7 +90,7 @@ impl TradeTarget {
}
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[derive(Inspect, Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Trade {
pub buyer: TradeTarget,
pub seller: TradeTarget,
Expand Down Expand Up @@ -438,19 +440,19 @@ mod tests {
use super::Market;
use crate::economy::{ItemRegistry, WORKER_CONSUMPTION_PER_SECOND};
use crate::souls::goods_company::{CompanyKind, GoodsCompanyDescription, Recipe};
use crate::world::CompanyID;
use crate::{map::BuildingGen, GoodsCompanyRegistry, SoulID};
use geom::{vec2, Vec2};
use hecs::Entity;

fn mk_ent(id: u64) -> Entity {
Entity::from_bits(id).unwrap()
fn mk_ent(id: u64) -> CompanyID {
CompanyID::from(slotmap::KeyData::from_ffi(id))
}

#[test]
fn test_match_orders() {
let seller = SoulID(mk_ent((1 << 32) | 1));
let seller_far = SoulID(mk_ent((1 << 32) | 2));
let buyer = SoulID(mk_ent((1 << 32) | 3));
let seller = SoulID::GoodsCompany(mk_ent((1 << 32) | 1));
let seller_far = SoulID::GoodsCompany(mk_ent((1 << 32) | 2));
let buyer = SoulID::GoodsCompany(mk_ent((1 << 32) | 3));

let mut registry = ItemRegistry::default();

Expand Down
42 changes: 24 additions & 18 deletions egregoria/src/economy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
//! - The government, which is the entity representing the player
//!
use crate::utils::resources::Resources;
use crate::World;
use crate::{GoodsCompanyRegistry, SoulID};
use egui_inspect::Inspect;
use hecs::World;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt::{Debug, Display, Formatter};
Expand All @@ -22,8 +22,8 @@ mod government;
mod item;
mod market;

use crate::souls::human::BasicWorker;
use crate::utils::time::{Tick, TICKS_PER_SECOND};
use crate::world::HumanID;
pub use ecostats::*;
pub use government::*;
pub use item::*;
Expand All @@ -37,6 +37,8 @@ const WORKER_CONSUMPTION_PER_SECOND: Money = Money::new_cents(1);
#[repr(transparent)]
pub struct Money(i64);

debug_inspect_impl!(Money);

impl Display for Money {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&(self.bucks()), f)?;
Expand Down Expand Up @@ -153,14 +155,14 @@ impl Money {
}
}

#[derive(Default, Serialize, Deserialize)]
#[derive(Inspect, Default, Serialize, Deserialize)]
pub struct Sold(pub Vec<Trade>);

#[derive(Default, Serialize, Deserialize)]
#[derive(Inspect, Default, Serialize, Deserialize)]
pub struct Bought(pub BTreeMap<ItemID, Vec<Trade>>);

#[derive(Debug, Default, Serialize, Deserialize, Inspect)]
pub struct Workers(pub Vec<SoulID>);
#[derive(Inspect, Debug, Default, Serialize, Deserialize)]
pub struct Workers(pub Vec<HumanID>);

#[cfg(not(test))]
const ITEMS_PATH: &str = "assets/items.json";
Expand Down Expand Up @@ -193,7 +195,7 @@ pub fn init_market(_: &mut World, res: &mut Resources) {

#[profiling::function]
pub fn market_update(world: &mut World, resources: &mut Resources) {
let n_workers = world.query::<&BasicWorker>().into_iter().len();
let n_workers = world.humans.len();

let mut m = resources.get_mut::<Market>().unwrap();
let job_opening = resources.get::<ItemRegistry>().unwrap().id("job-opening");
Expand All @@ -215,32 +217,36 @@ pub fn market_update(world: &mut World, resources: &mut Resources) {
log::debug!("A trade was made! {:?}", trade);

if trade.kind == job_opening {
// Jobs are guaranteed to not be external
world
.get::<&mut Workers>(trade.seller.soul().0)
.expect("employer has no component Workers")
.0
.push(trade.buyer.soul());
if let SoulID::GoodsCompany(id) = trade.seller.soul() {
let comp = world.companies.get_mut(id).unwrap();
comp.workers.0.push(trade.buyer.soul().try_into().unwrap())
}
}
gvt.money += trade.money_delta;

match trade.seller {
TradeTarget::Soul(id) => {
if trade.kind != job_opening {
if let Ok(mut v) = world.get::<&mut Sold>(id.0) {
v.0.push(trade)
if let SoulID::GoodsCompany(id) = id {
world.companies.get_mut(id).unwrap().sold.0.push(trade);
}
}
}
TradeTarget::ExternalTrade => {}
}

match trade.buyer {
TradeTarget::Soul(id) => {
if let Ok(mut v) = world.get::<&mut Bought>(id.0) {
v.0.entry(trade.kind).or_default().push(trade);
TradeTarget::Soul(SoulID::Human(id)) => {
if let Some(h) = world.humans.get_mut(id) {
h.bought.0.entry(trade.kind).or_default().push(trade);
}
}
TradeTarget::Soul(SoulID::GoodsCompany(id)) => {
if let Some(c) = world.companies.get_mut(id) {
c.bought.0.entry(trade.kind).or_default().push(trade)
}
}
TradeTarget::Soul(SoulID::FreightStation(_)) => {}
TradeTarget::ExternalTrade => {}
}
}
Expand Down
31 changes: 1 addition & 30 deletions egregoria/src/engine_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,12 @@ use crate::map_dynamic::BuildingInfos;
use crate::transportation::train::{spawn_train, RailWagonKind};
use crate::utils::time::{GameTime, Tick};
use crate::{Egregoria, EgregoriaOptions, Replay};
use geom::{vec3, Transform, Vec2, OBB};
use hecs::Entity;
use geom::{vec3, Vec2, OBB};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::time::Instant;
use WorldCommand::*;

#[derive(Clone, Serialize, Deserialize)]
pub struct Selectable {
pub radius: f32,
}

impl Selectable {
pub fn new(radius: f32) -> Self {
Self { radius }
}
}

impl Default for Selectable {
fn default() -> Self {
Self { radius: 5.0 }
}
}

#[derive(Clone, Default)]
pub struct WorldCommands {
pub(crate) commands: Vec<WorldCommand>,
Expand Down Expand Up @@ -85,7 +67,6 @@ pub enum WorldCommand {
},
ResetSave,
SetGameTime(GameTime),
UpdateTransform(Entity, Transform),
}

impl AsRef<[WorldCommand]> for WorldCommands {
Expand Down Expand Up @@ -123,10 +104,6 @@ impl WorldCommands {
self.commands.push(MapLoadTestField { pos, size, spacing })
}

pub fn update_transform(&mut self, e: Entity, trans: Transform) {
self.commands.push(UpdateTransform(e, trans))
}

pub fn reset_save(&mut self) {
self.commands.push(ResetSave)
}
Expand Down Expand Up @@ -212,7 +189,6 @@ impl WorldCommand {
| MapUpdateIntersectionPolicy { .. }
| UpdateZone { .. }
| SetGameTime(_)
| UpdateTransform(_, _)
)
}

Expand Down Expand Up @@ -307,11 +283,6 @@ impl WorldCommand {
let opts = *goria.read::<EgregoriaOptions>();
*goria = Egregoria::new_with_options(opts);
}
UpdateTransform(e, t) => {
if let Some(mut x) = goria.comp_mut(e) {
*x = t
}
}
Init(ref opts) => {
if opts.save_replay {
let mut rep = goria.resources.get_mut::<Replay>().unwrap();
Expand Down
Loading

0 comments on commit d43e998

Please sign in to comment.