diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/behavior_clock.rs | 46 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 7 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/laboratory.rs | 25 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 15 |
4 files changed, 79 insertions, 14 deletions
diff --git a/crates/fparkan-runtime/src/behavior_clock.rs b/crates/fparkan-runtime/src/behavior_clock.rs new file mode 100644 index 00000000..336225b4 --- /dev/null +++ b/crates/fparkan-runtime/src/behavior_clock.rs @@ -0,0 +1,46 @@ +/// Native Behavior timer deadline, in 64 ms base/jitter units. +pub(super) fn next_deadline(now_ms: u32, base: u16, jitter: u16, rng: &mut u32) -> u32 { + let random = next_crt_rand(rng) & 0xff; + let delay = (u32::from(base) << 6) + ((random * (u32::from(jitter) << 6)) >> 8); + now_ms.wrapping_add(delay) +} + +pub(super) fn next_crt_rand(state: &mut u32) -> u32 { + *state = state.wrapping_mul(0x343fd).wrapping_add(0x269ec3); + (*state >> 16) & 0x7fff +} + +/// The owner task timer initialized with native words 1/1. +#[derive(Default)] +pub(super) struct TaskClock { + deadline_ms: u32, +} + +impl TaskClock { + pub(super) fn due(&mut self, now_ms: u32, rng: &mut u32) -> bool { + if self.deadline_ms != 0 && now_ms <= self.deadline_ms { + return false; + } + self.deadline_ms = next_deadline(now_ms, 1, 1, rng); + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn native_task_timer_requires_a_strictly_later_clock_and_consumes_rng_only_when_due() { + let mut clock = TaskClock::default(); + let mut rng = 1; + assert!(clock.due(100, &mut rng)); + assert_eq!((clock.deadline_ms, rng), (174, 2_745_024)); + assert!(!clock.due(174, &mut rng)); + assert_eq!((clock.deadline_ms, rng), (174, 2_745_024)); + assert!(clock.due(175, &mut rng)); + assert_eq!((clock.deadline_ms, rng), (247, 3_357_800_067)); + assert!(clock.due(1000, &mut rng)); + assert_eq!((clock.deadline_ms, rng), (1111, 415_139_642)); + } +} diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index c80b7d03..f4ba65bf 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -90,7 +90,12 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec<MissionNotice let Some(loaded) = &mut engine.loaded else { return notices; }; - crate::laboratory::tick(loaded, &engine.world, now_ms); + crate::laboratory::tick( + loaded, + &engine.world, + now_ms, + &mut engine.behavior_rng_state, + ); crate::construction::tick( loaded, &mut engine.world, diff --git a/crates/fparkan-runtime/src/laboratory.rs b/crates/fparkan-runtime/src/laboratory.rs index ec9d27f9..08953eb9 100644 --- a/crates/fparkan-runtime/src/laboratory.rs +++ b/crates/fparkan-runtime/src/laboratory.rs @@ -17,6 +17,8 @@ pub struct LaboratoryReport { pub available: Vec<(usize, usize, u64)>, /// Orders stopped by unavailable runtime state, by clan and laboratory. pub stopped: Vec<(usize, u32, u64)>, + /// Due task updates; the idle owner phase is still approximate. + pub clock_ticks: Vec<(usize, u32, u64)>, } #[derive(Default)] @@ -28,6 +30,7 @@ pub(super) struct Laboratories { completed: BTreeMap<(usize, u32, usize), u64>, available: BTreeMap<(usize, usize), u64>, stopped: BTreeMap<(usize, u32), u64>, + clock_ticks: BTreeMap<(usize, u32), u64>, } struct Order { @@ -38,6 +41,7 @@ struct Order { current: Option<(usize, Work)>, duration: f32, last_ms: u32, + clock: crate::behavior_clock::TaskClock, } #[derive(Default)] @@ -121,6 +125,11 @@ impl Laboratories { .iter() .map(|(&(clan, lab), &count)| (clan, lab, count)) .collect(), + clock_ticks: self + .clock_ticks + .iter() + .map(|(&(clan, lab), &count)| (clan, lab, count)) + .collect(), } } } @@ -191,6 +200,7 @@ pub(super) fn order( current: None, duration, last_ms: now_ms, + clock: Default::default(), }); eprintln!( "sim_coverage script_lab_research_approx=1 clan={clan} lab={} project={}", @@ -200,10 +210,23 @@ pub(super) fn order( Ok(()) } -pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32) { +pub(super) fn tick(mission: &mut LoadedMissionState, world: &World, now_ms: u32, rng: &mut u32) { let mut state = std::mem::take(&mut mission.laboratories); let mut index = 0; while index < state.tasks.len() { + let task = &mut state.tasks[index]; + if !task.clock.due(now_ms, rng) { + index += 1; + continue; + } + let count = state + .clock_ticks + .entry((task.clan, task.lab.slot)) + .or_default(); + *count += 1; + if *count == 1 { + eprintln!("sim_coverage script_lab_clock_approx=1 clan={} lab={} (active task timer; idle owner phases remain open)", task.clan, task.lab.slot); + } let result = advance(&mut state, index, mission, world, now_ms); if matches!(result, Ok(true)) { index += 1; diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 9eb5f23d..20d05295 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -42,6 +42,7 @@ use std::num::NonZeroUsize; use std::sync::Arc; mod autodemo; +mod behavior_clock; mod boarding; mod builder_supply; mod building_door; @@ -60,6 +61,7 @@ mod clan_transport_schemes; mod construction; mod construction_site; mod laboratory; +use behavior_clock::next_crt_rand; mod mesh_collision; mod mining; pub use laboratory::LaboratoryReport; @@ -1063,25 +1065,14 @@ pub struct BehaviorWarmStartState { impl BehaviorWarmStartState { fn start(queue_time_ms: u32, rng_state: &mut u32) -> Self { - const BASE_INTERVAL_MS: u32 = 3 * 64; - const JITTER_RANGE_MS: u32 = 64; - - let random = next_crt_rand(rng_state); - let jitter_ms = ((random & 0xff) * JITTER_RANGE_MS) >> 8; - Self { first_time_ms: queue_time_ms, - next_deadline_ms: queue_time_ms.wrapping_add(BASE_INTERVAL_MS + jitter_ms), + next_deadline_ms: behavior_clock::next_deadline(queue_time_ms, 3, 1, rng_state), cleared_words: [0; 5], } } } -fn next_crt_rand(state: &mut u32) -> u32 { - *state = state.wrapping_mul(0x343fd).wrapping_add(0x269ec3); - (*state >> 16) & 0x7fff -} - fn behavior_elapsed_seconds(elapsed_ms: u32) -> f32 { // The exact integer product is rounded once to f32; scaling by 2^-33 is // exact and matches native FILD / FMUL / FSTP under the measured control word. |
