diff --git a/hes-engine/src/diff.rs b/hes-engine/src/diff.rs index 6830d66d..1e46b0c3 100644 --- a/hes-engine/src/diff.rs +++ b/hes-engine/src/diff.rs @@ -170,6 +170,17 @@ impl Diff for State { .short_units() .diff(&other.resources.available.short_units()), )); + changes.push(Change::Nested( + "resource_surplus".into(), + (self.resources.available + - self.resource_demand.total()) + .short_units() + .diff( + &(other.resources.available + - other.resource_demand.total()) + .short_units(), + ), + )); changes.push(Change::Nested( "feedstocks".into(), self.feedstocks.available.short_units().diff( diff --git a/hes-engine/src/events/effects.rs b/hes-engine/src/events/effects.rs index c309df5e..fb1052c7 100644 --- a/hes-engine/src/events/effects.rs +++ b/hes-engine/src/events/effects.rs @@ -530,7 +530,14 @@ impl Effect { state.output_demand.modifier[*output] += amount; } Effect::Output(output, pct_change) => { - state.produced.factor[*output] += pct_change; + for process in state + .world + .processes + .iter_mut() + .filter(|p| p.output == *output) + { + process.output_modifier += pct_change; + } } Effect::OutputForFeature(feat, pct_change) => { for process in state @@ -857,7 +864,14 @@ impl Effect { state.output_demand.modifier[*output] -= amount; } Effect::Output(output, pct_change) => { - state.produced.factor[*output] -= pct_change; + for process in state + .world + .processes + .iter_mut() + .filter(|p| p.output == *output) + { + process.output_modifier -= pct_change; + } } Effect::OutputForFeature(feat, pct_change) => { for process in state @@ -1200,4 +1214,17 @@ mod tests { state.world.update_climate(temp_prev); assert_eq!(state.world.temperature, temp_next); } + + #[test] + fn test_output_demand_amount() { + let mut state = State::default(); + state.output_demand.base.plant_calories = 5.; + let effect = + Effect::DemandAmount(Output::PlantCalories, 1.); + state.apply_effects(&[effect], None); + assert_eq!( + state.output_demand.of(Output::PlantCalories), + 6. + ); + } } diff --git a/hes-engine/src/state.rs b/hes-engine/src/state.rs index 3ec1f073..62656882 100644 --- a/hes-engine/src/state.rs +++ b/hes-engine/src/state.rs @@ -797,14 +797,12 @@ impl State { #[derive(Serialize, Deserialize, Clone, PartialEq)] pub struct Production { - pub factor: OutputMap, pub amount: OutputMap, pub by_process: BTreeMap, } impl Default for Production { fn default() -> Self { Self { - factor: OutputMap::splat(1.), amount: OutputMap::default(), by_process: BTreeMap::default(), } @@ -812,11 +810,11 @@ impl Default for Production { } impl Production { pub fn of(&self, output: Output) -> f32 { - self.amount[output] * self.factor[output] + self.amount[output] } pub fn total(&self) -> OutputMap { - self.amount * self.factor + self.amount } }