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-script | |
| 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-script')
| -rw-r--r-- | crates/fparkan-script/src/lib.rs | 49 | ||||
| -rw-r--r-- | crates/fparkan-script/tests/handler2_native_oracle.cs | 50 |
2 files changed, 69 insertions, 30 deletions
diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs index 4dd73efd..e9e10722 100644 --- a/crates/fparkan-script/src/lib.rs +++ b/crates/fparkan-script/src/lib.rs @@ -1967,9 +1967,10 @@ impl Handler2RecordScheduler { /// Inserts or refreshes one resolved handler input. /// /// The original compares the three identity words bit-for-bit. On an - /// existing key, it refreshes only when `scalar_1` compares unequal; that - /// update replaces `scalar_1` and `argument_2`, then adds the slot-2 word - /// captured when that record was created, with x86 wrapping arithmetic. + /// existing key, it accepts a weight at least as high as the stored + /// weight. An unordered comparison leaves the record unchanged. + /// Refresh updates `scalar_1` and `argument_2`, then adds the initial + /// slot-2 word with x86 wrapping arithmetic. pub fn submit(&mut self, input: Handler2RecordInput) -> Handler2RecordUpdate { let key = Handler2RecordKey::from(input); if let Some((index, record)) = self @@ -1978,7 +1979,7 @@ impl Handler2RecordScheduler { .enumerate() .find(|(_, record)| record.key == key) { - if !handler_two_scalar_equal(record.scalar_1, input.scalar_1) { + if record.scalar_1 <= input.scalar_1 { record.scalar_1 = input.scalar_1; record.argument_2 = input.argument_2; record.counter = record.counter.wrapping_add(record.increment); @@ -2011,19 +2012,6 @@ impl Handler2RecordScheduler { } } -fn handler_two_scalar_equal(left: f32, right: f32) -> bool { - if left.is_nan() || right.is_nan() { - return false; - } - let left_bits = left.to_bits(); - let right_bits = right.to_bits(); - left_bits == right_bits || (is_f32_zero_bits(left_bits) && is_f32_zero_bits(right_bits)) -} - -fn is_f32_zero_bits(bits: u32) -> bool { - matches!(bits, 0 | 0x8000_0000) -} - /// Parses the numeric `VAR(...)` declarations from a legacy `varset.var` file. /// /// Parsing is line-oriented and byte-safe: non-UTF-8 comments remain opaque, @@ -2489,7 +2477,7 @@ mod tests { assert_eq!(scheduler.records().len(), 1); assert_eq!(scheduler.records()[0].counter, 4); - let unchanged = handler_two_input(1.5, -0.0, 3.0, 12.0, 99); + let unchanged = handler_two_input(1.0, -0.0, 3.0, 12.0, 99); assert_eq!( scheduler.submit(unchanged), super::Handler2RecordUpdate { @@ -2501,7 +2489,7 @@ mod tests { assert_eq!(scheduler.records()[0].counter, 4); assert_eq!(scheduler.records()[0].argument_2, 9.0_f32.to_bits()); - let refreshed = handler_two_input(2.5, -0.0, 3.0, 12.0, 99); + let refreshed = handler_two_input(1.5, -0.0, 3.0, 12.0, 99); assert_eq!( scheduler.submit(refreshed), super::Handler2RecordUpdate { @@ -2536,30 +2524,31 @@ mod tests { } #[test] - fn handler_two_scheduler_refreshes_nan_and_wraps_counter() { + fn handler_two_scheduler_skips_unordered_weights_and_wraps_on_refresh() { let mut scheduler = Handler2RecordScheduler::default(); - scheduler.submit(handler_two_input(f32::NAN, 1.0, 2.0, 3.0, u32::MAX)); + scheduler.submit(handler_two_input(0.5, 1.0, 2.0, 3.0, u32::MAX)); let update = scheduler.submit(handler_two_input(f32::NAN, 1.0, 2.0, 4.0, 2)); assert_eq!(update.index, 0); - assert!(update.refreshed); - assert_eq!(scheduler.records()[0].counter, u32::MAX - 1); - assert_eq!(scheduler.records()[0].argument_2, 4.0_f32.to_bits()); + assert!(!update.refreshed); + assert_eq!(scheduler.records()[0].counter, u32::MAX); + assert_eq!(scheduler.records()[0].argument_2, 3.0_f32.to_bits()); - let update = scheduler.submit(handler_two_input(f32::NAN, 1.0, 2.0, 5.0, 17)); + let update = scheduler.submit(handler_two_input(0.5, 1.0, 2.0, 5.0, 17)); assert!(update.refreshed); - assert_eq!(scheduler.records()[0].counter, u32::MAX - 2); + assert_eq!(scheduler.records()[0].counter, u32::MAX - 1); assert_eq!(scheduler.records()[0].argument_2, 5.0_f32.to_bits()); } #[test] - fn handler_two_scheduler_treats_signed_zero_value_as_unchanged() { + fn handler_two_scheduler_refreshes_equal_weights_across_signed_zero() { let mut scheduler = Handler2RecordScheduler::default(); scheduler.submit(handler_two_input(-0.0, 1.0, 2.0, 3.0, 5)); let update = scheduler.submit(handler_two_input(0.0, 1.0, 2.0, 4.0, 9)); assert!(!update.created); - assert!(!update.refreshed); - assert_eq!(scheduler.records()[0].counter, 5); - assert_eq!(scheduler.records()[0].argument_2, 3.0_f32.to_bits()); + assert!(update.refreshed); + assert_eq!(scheduler.records()[0].counter, 10); + assert_eq!(scheduler.records()[0].scalar_1.to_bits(), 0); + assert_eq!(scheduler.records()[0].argument_2, 4.0_f32.to_bits()); } #[test] diff --git a/crates/fparkan-script/tests/handler2_native_oracle.cs b/crates/fparkan-script/tests/handler2_native_oracle.cs index f632c688..52e9456b 100644 --- a/crates/fparkan-script/tests/handler2_native_oracle.cs +++ b/crates/fparkan-script/tests/handler2_native_oracle.cs @@ -7,6 +7,7 @@ using System.Text; internal static class Handler2NativeOracle { private const uint LoadWithAlteredSearchPath = 0x00000008; private const int RefreshRecordRva = 0x5070; + private const int SubmitRva = 0x59f0; private const string ExpectedSha256 = "c19cd6cd31cd2896f8c4ba0f0e700dd3408aec8182daaf4863d98a7d233a2abc"; [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] @@ -18,6 +19,10 @@ internal static class Handler2NativeOracle { [UnmanagedFunctionPointer(CallingConvention.ThisCall)] private delegate void RefreshRecord(IntPtr record); + [UnmanagedFunctionPointer(CallingConvention.ThisCall)] + private delegate void SubmitRecord(IntPtr queue, int kind, float priority, uint increment, + uint sender, uint first, uint second, uint third, IntPtr name); + private static int Main() { if (IntPtr.Size != 4) { Console.Error.WriteLine("Build this oracle with an x86 target; ai.dll is 32-bit."); @@ -66,11 +71,56 @@ internal static class Handler2NativeOracle { AssertRefresh(refresh, record, UInt32.MaxValue - 2); } finally { Marshal.FreeHGlobal(record); } + + var submit = (SubmitRecord)Marshal.GetDelegateForFunctionPointer( + IntPtr.Add(module, SubmitRva), typeof(SubmitRecord)); + AssertMatchingSubmission(submit, 0.55011004f, 0.545f, false); + AssertMatchingSubmission(submit, 0.545f, 0.55011004f, true); + AssertMatchingSubmission(submit, 1.0f, 1.0f, true); + AssertMatchingSubmission(submit, -0.0f, 0.0f, true); + AssertMatchingSubmission(submit, Single.NaN, 1.0f, false); + AssertMatchingSubmission(submit, 1.0f, Single.NaN, false); } finally { FreeLibrary(module); } return 0; } + private static void AssertMatchingSubmission(SubmitRecord submit, float before, float incoming, + bool expectedRefresh) { + IntPtr queue = Marshal.AllocHGlobal(0x380); + IntPtr record = Marshal.AllocHGlobal(100); + try { + Marshal.Copy(new byte[0x380], 0, queue, 0x380); + Marshal.Copy(new byte[100], 0, record, 100); + Marshal.WriteInt32(queue, 0x28, 1); + Marshal.WriteIntPtr(queue, 0x30, record); + Marshal.WriteInt32(record, 0, 3); + Marshal.WriteInt32(record, 4, unchecked((int)0x80000010)); + Marshal.WriteInt32(record, 0xc, 91); + Marshal.WriteInt32(record, 0x14, Bits(before)); + Marshal.WriteInt32(record, 0x24, 7); + Marshal.WriteInt32(record, 0x28, 4); + submit(queue, 3, incoming, 99, 0, 0x80000010, 0, 92, IntPtr.Zero); + int counter = Marshal.ReadInt32(record, 0x24); + Console.WriteLine("stored={0:R} submitted={1:R} counter={2}", before, incoming, counter); + if (Marshal.ReadInt32(queue, 0x28) != 1 || + Marshal.ReadInt32(record, 0x14) != Bits(expectedRefresh ? incoming : before) || + counter != (expectedRefresh ? 11 : 7) || + Marshal.ReadInt32(record, 0xc) != (expectedRefresh ? 92 : 91) || + Marshal.ReadInt32(record, 0x28) != 4) { + throw new InvalidOperationException("Unexpected matching submission result."); + } + } + finally { + Marshal.FreeHGlobal(record); + Marshal.FreeHGlobal(queue); + } + } + + private static int Bits(float value) { + return BitConverter.ToInt32(BitConverter.GetBytes(value), 0); + } + private static void AssertRefresh(RefreshRecord refresh, IntPtr record, uint expectedCounter) { uint savedIncrement = unchecked((uint)Marshal.ReadInt32(record, 0x28)); refresh(record); |
