Implement world-scalar packed event conditions

This commit is contained in:
Jan Petykiewicz 2026-04-16 13:48:55 -07:00
commit 3f2632e330
12 changed files with 1541 additions and 8 deletions

View file

@ -722,6 +722,67 @@ fn evaluate_record_conditions(
return Ok(None);
}
}
RuntimeCondition::NamedLocomotiveAvailabilityThreshold {
name,
comparator,
value,
} => {
let actual = state
.named_locomotive_availability
.get(name)
.copied()
.map(i64::from)
.unwrap_or(0);
if !compare_condition_value(actual, *comparator, *value) {
return Ok(None);
}
}
RuntimeCondition::NamedLocomotiveCostThreshold {
name,
comparator,
value,
} => {
let actual = state
.named_locomotive_cost
.get(name)
.copied()
.map(i64::from)
.unwrap_or(0);
if !compare_condition_value(actual, *comparator, *value) {
return Ok(None);
}
}
RuntimeCondition::CargoProductionTotalThreshold { comparator, value } => {
let actual = state
.cargo_production_overrides
.values()
.copied()
.map(i64::from)
.sum::<i64>();
if !compare_condition_value(actual, *comparator, *value) {
return Ok(None);
}
}
RuntimeCondition::LimitedTrackBuildingAmountThreshold { comparator, value } => {
let actual = state
.world_restore
.limited_track_building_amount
.map(i64::from)
.unwrap_or(0);
if !compare_condition_value(actual, *comparator, *value) {
return Ok(None);
}
}
RuntimeCondition::TerritoryAccessCostThreshold { comparator, value } => {
let actual = state
.world_restore
.territory_access_cost
.map(i64::from)
.unwrap_or(0);
if !compare_condition_value(actual, *comparator, *value) {
return Ok(None);
}
}
RuntimeCondition::EconomicStatusCodeThreshold { comparator, value } => {
let actual = state
.world_restore
@ -2142,6 +2203,91 @@ mod tests {
assert_eq!(state.world_flags.get("world_condition_failed"), None);
}
#[test]
fn evaluates_world_scalar_conditions_before_effects_run() {
let mut state = RuntimeState {
world_restore: RuntimeWorldRestoreState {
limited_track_building_amount: Some(18),
territory_access_cost: Some(750000),
..RuntimeWorldRestoreState::default()
},
named_locomotive_availability: BTreeMap::from([(String::from("Big Boy"), 42)]),
named_locomotive_cost: BTreeMap::from([(String::from("GP7"), 175000)]),
cargo_production_overrides: BTreeMap::from([(1, 125), (2, 75)]),
event_runtime_records: vec![
RuntimeEventRecord {
record_id: 25,
trigger_kind: 7,
active: true,
service_count: 0,
marks_collection_dirty: false,
one_shot: false,
has_fired: false,
conditions: vec![
RuntimeCondition::NamedLocomotiveAvailabilityThreshold {
name: "Big Boy".to_string(),
comparator: RuntimeConditionComparator::Eq,
value: 42,
},
RuntimeCondition::NamedLocomotiveCostThreshold {
name: "GP7".to_string(),
comparator: RuntimeConditionComparator::Eq,
value: 175000,
},
RuntimeCondition::CargoProductionTotalThreshold {
comparator: RuntimeConditionComparator::Eq,
value: 200,
},
RuntimeCondition::LimitedTrackBuildingAmountThreshold {
comparator: RuntimeConditionComparator::Eq,
value: 18,
},
RuntimeCondition::TerritoryAccessCostThreshold {
comparator: RuntimeConditionComparator::Eq,
value: 750000,
},
],
effects: vec![RuntimeEffect::SetWorldFlag {
key: "world_scalar_condition_passed".to_string(),
value: true,
}],
},
RuntimeEventRecord {
record_id: 26,
trigger_kind: 7,
active: true,
service_count: 0,
marks_collection_dirty: false,
one_shot: false,
has_fired: false,
conditions: vec![RuntimeCondition::NamedLocomotiveAvailabilityThreshold {
name: "Missing Loco".to_string(),
comparator: RuntimeConditionComparator::Gt,
value: 0,
}],
effects: vec![RuntimeEffect::SetWorldFlag {
key: "world_scalar_condition_failed".to_string(),
value: true,
}],
},
],
..state()
};
let result = execute_step_command(
&mut state,
&StepCommand::ServiceTriggerKind { trigger_kind: 7 },
)
.expect("world-scalar conditions should evaluate successfully");
assert_eq!(result.service_events[0].serviced_record_ids, vec![25]);
assert_eq!(
state.world_flags.get("world_scalar_condition_passed"),
Some(&true)
);
assert_eq!(state.world_flags.get("world_scalar_condition_failed"), None);
}
#[test]
fn one_shot_record_only_fires_once() {
let mut state = RuntimeState {