From f50fb0dcae975cd964b7606cc7248d9e771abb2c Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 22:50:05 +0400 Subject: fix(production): use Control energy budgets for factory work Factories now run the recovered Control resource-budget pass on its own randomized clock before Behavior task updates. Paid production reads its current work-device E00 value, including the local power reserve and allocation, instead of gating full speed on any clan generation. The clock redraws jitter on every accepted frame and uses a separate Control RNG. Initial phase, seed and ordering with other owners remain explicit approximations. Budget failures preserve the table and are counted per owner. Original DLL calls verify the budget callbacks, strict deadline, per-call RNG, forced update and inactive baseline. A live class-3 factory executes the same budget with the default 250 ms setting. Licensed regressions cover an empty factory power reserve, subsequent generator capture, four completed paid orders, the Behavior task deadline and scripted warbot/transport/builder births. Workspace fmt/check/tests and release build pass. All 29 stock missions run 900 seconds without script or new budget stops. Stock mission 28 accepts and completes a free and then a paid player order in the Vulkan-validation window. --- crates/fparkan-runtime/src/production.rs | 48 ++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'crates/fparkan-runtime/src/production.rs') diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index d2d8a135..078fef31 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -278,16 +278,8 @@ impl Production { } let mut ready = now_ms.wrapping_sub(order.start_ms) >= order.duration_ms; if let Some(paid) = &mut order.paid { - // shortcut: gate the factory rate on clan power until Control - // propagates the delivered power to the production device. - let powered = mission.script_bundles.iter().any(|bundle| { - bundle.clan_index == usize::from(order.owner.0) - && bundle - .behavior_warm_start - .is_some_and(|state| f32::from_bits(state.cleared_words[0]) > 0.0) - }); let (table, ore) = factory_resources(mission, order.plant)?; - let rate = if powered { factory_rate(table)? } else { 0.0 }; + let rate = factory_rate(table)?; if paid.advance(now_ms, rate, ore) && paid.collected.power + RESOURCE_EPSILON >= paid.cost.power { @@ -1014,6 +1006,18 @@ mod tests { .unwrap() .1 .current_ore_bits = 100.0_f32.to_bits(); + // No Control budget runs in this isolated clock test: set the work multiplier explicitly. + let table = factory_resources(engine.loaded.as_mut().unwrap(), plant) + .unwrap() + .0; + let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { + panic!("factory devices"); + }; + devices + .iter_mut() + .find(|device| device.descriptor.tag == 26) + .unwrap() + .multiplier_bits = Some(0); // Isolate the factory's owner timer from unrelated clan updates. let mut rng = 1; for now in [100, 174] { @@ -1093,12 +1097,38 @@ mod tests { let cost = &order.paid.as_ref().unwrap().cost; assert!((cost.ore - 25.12).abs() < 0.001); assert!((cost.power - 72.7).abs() < 0.001); + // Debug fixture: empty the factory's local energy reserve as well as leaving it without a generator. + crate::set_control_device_table_multiplier( + factory_resources(engine.loaded.as_mut().unwrap(), plant) + .unwrap() + .0, + 0.0, + ) + .unwrap(); for now in (100..=6100).step_by(50) { assert!(step_headless(&mut engine, InputSnapshot::default(), now) .unwrap() .produced_unit .is_none()); } + let loaded = engine.loaded.as_ref().unwrap(); + assert!(loaded + .factory_resources + .ticks() + .any(|(unit, count)| unit == plant.slot && count > 0)); + assert!(!loaded + .factory_resources + .stops() + .any(|(unit, _, _)| unit == plant.slot)); + let table = loaded + .control_device_tables + .iter() + .find(|table| table.handle == plant) + .unwrap(); + assert!( + factory_rate(table).unwrap() < 5.0, + "unpowered production device must lose its full work rate" + ); let factory_position = pose.position; pose.position = [1288.99_f32, 807.4, 74.08].map(f32::to_bits); set_transform(&mut engine.world, cockpit.handle, pose).unwrap(); -- cgit v1.2.3