diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 12:11:27 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 12:11:27 +0300 |
| commit | fac74ba2d2487cb500d24de4223a0ffec50fa580 (patch) | |
| tree | fcc13a4bb05b1a9a4fa7ea29f0fae5477e469f8c /crates/fparkan-runtime/src/clan_problem_priority.rs | |
| parent | 21b13ca1493d6f4d2da2add874f3fcd29b3cf5fe (diff) | |
| download | fparkan-fac74ba2d2487cb500d24de4223a0ffec50fa580.tar.xz fparkan-fac74ba2d2487cb500d24de4223a0ffec50fa580.zip | |
fix(ai): preserve clan job priorities through factory dispatch
Match GOG ai.dll when refreshing the same request and starting equally weighted jobs. Resolve generated factory IDs when dispatching or awaiting production.
Verified from stock starts in Two Fortresses and Third Part. Two Fortresses uses a newly built factory for a combat robot. The small factory in Third Part still lacks a compatible prepared design. The armed stock replay reached late cleanup and left a transport to pursue.
Diffstat (limited to 'crates/fparkan-runtime/src/clan_problem_priority.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_problem_priority.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_problem_priority.rs b/crates/fparkan-runtime/src/clan_problem_priority.rs new file mode 100644 index 00000000..200b76cd --- /dev/null +++ b/crates/fparkan-runtime/src/clan_problem_priority.rs @@ -0,0 +1,110 @@ +use fparkan_script::{Handler2Record, Handler2RecordKey}; +use std::collections::BTreeSet; + +#[derive(Default)] +pub(super) struct StartPass { + attempted: BTreeSet<Handler2RecordKey>, + stalled: bool, +} + +impl StartPass { + pub(super) fn next_batch( + &mut self, + records: &[Handler2Record], + in_progress: impl Fn(Handler2RecordKey) -> bool, + ) -> Vec<Handler2Record> { + if self.stalled { + return Vec::new(); + } + + let mut highest = 0.0; + let mut batch = Vec::new(); + for record in records { + if record.scalar_1 <= 0.0 + || self.attempted.contains(&record.key) + || in_progress(record.key) + { + continue; + } + if record.scalar_1 > highest { + highest = record.scalar_1; + batch.clear(); + } + if record.scalar_1 == highest { + batch.push(record.clone()); + } + } + self.attempted.extend(batch.iter().map(|record| record.key)); + batch.reverse(); + batch + } + + pub(super) fn stop_after_batch(&mut self) { + self.stalled = true; + } +} + +#[cfg(test)] +mod tests { + use super::*; + use fparkan_script::{Handler2RecordInput, Handler2RecordScheduler}; + + #[test] + fn attempts_all_tied_requests_and_keeps_lower_weights_waiting_if_one_stalls() { + let mut queue = Handler2RecordScheduler::default(); + for (kind, weight) in [(1, 5.0), (2, 5.0), (3, 1.0), (4, 0.0), (5, -1.0)] { + submit(&mut queue, kind, weight); + } + let mut pass = StartPass::default(); + let mut batch = pass.next_batch(queue.records(), |_| false); + assert_eq!(batch.pop().unwrap().key.word_0, 1); + pass.stop_after_batch(); + assert_eq!(batch.pop().unwrap().key.word_0, 2); + assert!(batch.is_empty()); + assert!(pass.next_batch(queue.records(), |_| false).is_empty()); + let mut next_pass = StartPass::default(); + assert_eq!( + kinds(next_pass.next_batch(queue.records(), |key| key.word_0 == 2)), + [1] + ); + } + + #[test] + fn reselects_from_live_weights_after_a_batch_advances() { + let mut queue = Handler2RecordScheduler::default(); + for (kind, weight) in [(1, 5.0), (2, 5.0), (3, 1.0), (4, 8.0)] { + submit(&mut queue, kind, weight); + } + let mut pass = StartPass::default(); + assert_eq!( + kinds(pass.next_batch(queue.records(), |key| key.word_0 == 4)), + [1, 2] + ); + submit(&mut queue, 5, 7.0); + for expected in [5, 3] { + assert_eq!( + kinds(pass.next_batch(queue.records(), |key| key.word_0 == 4)), + [expected] + ); + } + assert!(pass + .next_batch(queue.records(), |key| key.word_0 == 4) + .is_empty()); + } + + fn kinds(batch: Vec<Handler2Record>) -> Vec<u32> { + batch.iter().rev().map(|record| record.key.word_0).collect() + } + + fn submit(queue: &mut Handler2RecordScheduler, word_0: u32, scalar_1: f32) { + queue.submit(Handler2RecordInput { + word_0, + scalar_1, + word_2: 0, + word_3: 0, + argument_0: 0, + argument_1: 0, + argument_2: 0, + }); + } +} |
