Skip to content

Commit

Permalink
fix(game): fix problem in how production output effects were applied.
Browse files Browse the repository at this point in the history
before they were applied *after* production had been calculated. for
example, say there's a 10% reduction in plant calories. with plant
calorie demand at 100, we'd produce 100, and *then* apply the reduction
so that the "actual" production amount is just 90, thus leading to a
shortage (90 < 100).

the changed version just updates the per-process output modifier,
which is what I should have been doing from the start :)

likely the cause of #298 and #280
  • Loading branch information
frnsys committed Aug 30, 2024
1 parent eb83cad commit a65f7ae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
11 changes: 11 additions & 0 deletions hes-engine/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 29 additions & 2 deletions hes-engine/src/events/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
);
}
}
6 changes: 2 additions & 4 deletions hes-engine/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,26 +797,24 @@ impl State {

#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct Production {
pub factor: OutputMap,
pub amount: OutputMap,
pub by_process: BTreeMap<Id, f32>,
}
impl Default for Production {
fn default() -> Self {
Self {
factor: OutputMap::splat(1.),
amount: OutputMap::default(),
by_process: BTreeMap::default(),
}
}
}
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
}
}

Expand Down

0 comments on commit a65f7ae

Please sign in to comment.