aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/behavior_clock.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-25 17:57:03 +0300
committerValentin Popov <valentin@popov.link>2026-11-25 17:57:03 +0300
commitf0e3a84cf5c0d7a0edd890c303299306c513ba8b (patch)
treeed672b3883d3e3c12c8bfb78c8fcc3cb90cd3366 /crates/fparkan-runtime/src/behavior_clock.rs
parentcd67d44101faf180e2f2891e897d0215689fd706 (diff)
downloadfparkan-f0e3a84cf5c0d7a0edd890c303299306c513ba8b.tar.xz
fparkan-f0e3a84cf5c0d7a0edd890c303299306c513ba8b.zip
fix(research): update laboratories on the native Behavior timer
Active laboratory work now uses the owner task timer instead of consuming and clearing its ore buffer on every host frame. The native 1/1 timer waits for a clock strictly beyond its deadline, draws one Behavior RNG value, and reschedules 64..127 ms from the observed clock. Resource distributors reuse the same deadline arithmetic with their existing 3/1 parameters. Behavior.dll 0x4c550 calls verify deadlines, equality, late calls and RNG state. The original laboratory was sampled through 20 bounded debugger hits; its caller uses the owner +0x930 timer initialized at 0x398f. A second native task fixture uses the original ore getter/setter and confirms that the buffer really is cleared, ruling out the host callback interpretation. Workspace fmt/check/tests, the licensed stock research chain, all 29 missions at 900 seconds and a live Vulkan window check pass. The active-only clock still reports script_lab_clock_approx: idle owner phases and full native event/RNG ordering remain open. Factory selection 69 and query 65 still need Control prototype metadata. This does not close G7.
Diffstat (limited to 'crates/fparkan-runtime/src/behavior_clock.rs')
-rw-r--r--crates/fparkan-runtime/src/behavior_clock.rs46
1 files changed, 46 insertions, 0 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));
+ }
+}