From e1bf8981fc92dd134da925254f10721f30941369 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 22:33:59 +0400 Subject: fix(ai): preserve raw script problem arguments Keep the target logical ID across Handler(2) and Handler(29). Verified slots 0, 1 and 2 with GOG ai.dll at RVA 0xc182. Single.01 now distinguishes nearby building IDs and leaves robot selection as the next blocker. --- crates/fparkan-runtime/src/clan_ai_events.rs | 43 ++++++++++++--------- crates/fparkan-script/src/lib.rs | 58 +++++++++++++--------------- docs/appendices/script-vm.md | 11 ++++-- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 481e9d08..17e57b76 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -170,7 +170,7 @@ impl LoadedMissionState { self.clan_ai_events.problems.get(&clan).is_some_and(|scheduler| { scheduler.records().iter().any(|record| { record.key.word_0 == 9 - && problem_dword(record.key.scalar_4_bits) == Some(id) + && record.key.argument_0 == id }) }) }, @@ -419,19 +419,21 @@ impl LoadedMissionState { let problem: &Handler2Record = problem .as_ref() .ok_or("Handler(29) requires a current problem")?; - let bits = + let argument = match single_dword(&instruction.references, values) { - Some(0) => problem.key.scalar_4_bits, - Some(1) => problem.key.scalar_5_bits, - Some(2) => problem.scalar_6.to_bits(), + Some(0) => problem.key.argument_0, + Some(1) => problem.key.argument_1, + Some(2) => problem.argument_2, _ => return Err( "Handler(29) requires a problem argument index from 0 to 2" .into(), ), }; - Ok(Some(VarSetDefault::Dword(problem_dword(bits).ok_or( - "Handler(29) argument is outside the supported DWORD range", - )?))) + let result = match values.get(instruction.header_words[1] as usize) { + Some(VarSetDefault::FloatBits(_)) => VarSetDefault::FloatBits(argument), + _ => VarSetDefault::Dword(argument), + }; + Ok(Some(result)) } ScriptDispatchSelector::Handler(0) if problem.is_some() && instruction.references.len() == 1 => @@ -689,6 +691,9 @@ fn problem_operands<'a>( VarSetDefault::Dword(value) => Some(value as f32), VarSetDefault::FloatBits(bits) => Some(f32::from_bits(bits)), }; + let argument = |index| match value(index)? { + VarSetDefault::Dword(value) | VarSetDefault::FloatBits(value) => Some(value), + }; Some(( varset.declarations.get(kind as usize)?.name.as_str(), Handler2RecordInput { @@ -696,9 +701,9 @@ fn problem_operands<'a>( scalar_1: scalar(priority)?, word_2: word(increment)?, word_3: word(sender)?, - scalar_4: scalar(first)?, - scalar_5: scalar(second)?, - scalar_6: scalar(third)?, + argument_0: argument(first)?, + argument_1: argument(second)?, + argument_2: argument(third)?, }, )) } @@ -818,7 +823,7 @@ mod tests { let values = [ VarSetDefault::Dword(6), VarSetDefault::FloatBits(5.75_f32.to_bits()), - VarSetDefault::Dword(12), + VarSetDefault::Dword(0x8000_0002), ]; let (name, problem) = problem_operands(&varset, &values, &[0, 1, 1, 2, 2, 1, 0]).unwrap(); assert_eq!(name, "PBM_ROBOT_NEEDED"); @@ -828,10 +833,10 @@ mod tests { word_0: 6, scalar_1: 5.75, word_2: 5, - word_3: 12, - scalar_4: 12.0, - scalar_5: 5.75, - scalar_6: 6.0, + word_3: 0x8000_0002, + argument_0: 0x8000_0002, + argument_1: 5.75_f32.to_bits(), + argument_2: 6, } ); } @@ -845,9 +850,9 @@ mod tests { scalar_1, word_2: 0, word_3: 0, - scalar_4: 0.0, - scalar_5: 0.0, - scalar_6: 0.0, + argument_0: 0, + argument_1: 0, + argument_2: 0, }); } let first = ranked_problems(queue.records()).pop().unwrap().key; diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs index b0d2c979..b409713c 100644 --- a/crates/fparkan-script/src/lib.rs +++ b/crates/fparkan-script/src/lib.rs @@ -1956,12 +1956,12 @@ pub struct Handler2RecordInput { pub word_2: u32, /// Resolved slot 3 word. pub word_3: u32, - /// Resolved slot 4 scalar. - pub scalar_4: f32, - /// Resolved slot 5 scalar. - pub scalar_5: f32, - /// Resolved slot 6 scalar. - pub scalar_6: f32, + /// Slot 4 as a raw DWORD or IEEE-754 word. + pub argument_0: u32, + /// Slot 5 as a raw DWORD or IEEE-754 word. + pub argument_1: u32, + /// Slot 6 as a raw DWORD or IEEE-754 word. + pub argument_2: u32, } /// The exact three-word identity used by the original `Handler(2)` scheduler. @@ -1969,18 +1969,18 @@ pub struct Handler2RecordInput { pub struct Handler2RecordKey { /// First identity word from resolved slot 0. pub word_0: u32, - /// IEEE-754 bits of resolved slot 4. - pub scalar_4_bits: u32, - /// IEEE-754 bits of resolved slot 5. - pub scalar_5_bits: u32, + /// Raw word of resolved slot 4. + pub argument_0: u32, + /// Raw word of resolved slot 5. + pub argument_1: u32, } impl From for Handler2RecordKey { fn from(input: Handler2RecordInput) -> Self { Self { word_0: input.word_0, - scalar_4_bits: input.scalar_4.to_bits(), - scalar_5_bits: input.scalar_5.to_bits(), + argument_0: input.argument_0, + argument_1: input.argument_1, } } } @@ -2002,8 +2002,8 @@ pub struct Handler2Record { increment: u32, /// Resolved slot 3 word. pub word_3: u32, - /// Resolved slot 6 scalar. - pub scalar_6: f32, + /// Slot 6 as a raw DWORD or IEEE-754 word. + pub argument_2: u32, } /// The result of submitting one resolved `Handler(2)` record. @@ -2039,7 +2039,7 @@ impl Handler2RecordScheduler { /// /// 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 `scalar_6`, then adds the slot-2 word + /// update replaces `scalar_1` and `argument_2`, then adds the slot-2 word /// captured when that record was created, with x86 wrapping arithmetic. pub fn submit(&mut self, input: Handler2RecordInput) -> Handler2RecordUpdate { let key = Handler2RecordKey::from(input); @@ -2051,7 +2051,7 @@ impl Handler2RecordScheduler { { if !handler_two_scalar_equal(record.scalar_1, input.scalar_1) { record.scalar_1 = input.scalar_1; - record.scalar_6 = input.scalar_6; + record.argument_2 = input.argument_2; record.counter = record.counter.wrapping_add(record.increment); return Handler2RecordUpdate { index, @@ -2072,7 +2072,7 @@ impl Handler2RecordScheduler { counter: input.word_2, increment: input.word_2, word_3: input.word_3, - scalar_6: input.scalar_6, + argument_2: input.argument_2, }); Handler2RecordUpdate { index, @@ -2539,9 +2539,9 @@ mod tests { scalar_1, word_2, word_3: 11, - scalar_4, - scalar_5, - scalar_6, + argument_0: scalar_4.to_bits(), + argument_1: scalar_5.to_bits(), + argument_2: scalar_6.to_bits(), } } @@ -2570,7 +2570,7 @@ mod tests { } ); assert_eq!(scheduler.records()[0].counter, 4); - assert_eq!(scheduler.records()[0].scalar_6.to_bits(), 9.0_f32.to_bits()); + 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); assert_eq!( @@ -2582,18 +2582,12 @@ mod tests { } ); assert_eq!(scheduler.records()[0].counter, 8); - assert_eq!( - scheduler.records()[0].scalar_6.to_bits(), - 12.0_f32.to_bits() - ); + assert_eq!(scheduler.records()[0].argument_2, 12.0_f32.to_bits()); let refreshed_again = handler_two_input(3.5, -0.0, 3.0, 13.0, 7); assert!(scheduler.submit(refreshed_again).refreshed); assert_eq!(scheduler.records()[0].counter, 12); - assert_eq!( - scheduler.records()[0].scalar_6.to_bits(), - 13.0_f32.to_bits() - ); + assert_eq!(scheduler.records()[0].argument_2, 13.0_f32.to_bits()); let positive_zero_key = handler_two_input(2.5, 0.0, 3.0, 12.0, 1); assert_eq!(scheduler.submit(positive_zero_key).index, 1); @@ -2608,12 +2602,12 @@ mod tests { assert_eq!(update.index, 0); assert!(update.refreshed); assert_eq!(scheduler.records()[0].counter, u32::MAX - 1); - assert_eq!(scheduler.records()[0].scalar_6.to_bits(), 4.0_f32.to_bits()); + assert_eq!(scheduler.records()[0].argument_2, 4.0_f32.to_bits()); let update = scheduler.submit(handler_two_input(f32::NAN, 1.0, 2.0, 5.0, 17)); assert!(update.refreshed); assert_eq!(scheduler.records()[0].counter, u32::MAX - 2); - assert_eq!(scheduler.records()[0].scalar_6.to_bits(), 5.0_f32.to_bits()); + assert_eq!(scheduler.records()[0].argument_2, 5.0_f32.to_bits()); } #[test] @@ -2624,7 +2618,7 @@ mod tests { assert!(!update.created); assert!(!update.refreshed); assert_eq!(scheduler.records()[0].counter, 5); - assert_eq!(scheduler.records()[0].scalar_6.to_bits(), 3.0_f32.to_bits()); + assert_eq!(scheduler.records()[0].argument_2, 3.0_f32.to_bits()); } #[test] diff --git a/docs/appendices/script-vm.md b/docs/appendices/script-vm.md index e52ab105..40b43164 100644 --- a/docs/appendices/script-vm.md +++ b/docs/appendices/script-vm.md @@ -316,8 +316,9 @@ ownership значения на FPU stack и следующий consumer, поэ `Handler(2)` (третья entry table, VA `0x10009610`) выбирает active event/instruction через runtime offsets `+0x48/+0x4c` и разрешает семь 32-bit slots через varset object `+0x18`. Их доказанный dataflow: slot 0 даёт один `u32` и base string, slot 1 -даёт numeric scalar, slots 2 и 3 — по `u32`, slots 4, 5 и 6 принимают только -kind `5`/`3` и иначе дают `0.0`. Затем он вызывает `0x100059f0` объекта по +даёт numeric scalar, slots 2 и 3 — по `u32`. Для slots 4, 5 и 6 kind `5` +копирует DWORD целиком, kind `3` сохраняет машинное слово float, остальные +дают ноль. Затем обработчик вызывает `0x100059f0` объекта по `this + 0x7c` и очищает flag `+0x50`. Этот callee больше не opaque. Он строит key из семи значений, ищет matching @@ -332,7 +333,9 @@ internal record. В этой ветке не видно прямого World3D/B `_Start`, затем перебирает остальные положительные веса. В оригинале RVA `0x1bf0` собирает набор равных наибольших весов и учитывает состояние заявки перед переходом к следующему весу. `Handler(29)` читает три аргумента -текущей заявки, `Handler(14)` выбирает исполнителя заданного класса. +текущей заявки. Логический ID `0x80000002` проходит через очередь как исходное +слово. Округление через float превратило бы его в `0x80000000` и указало бы +на другой объект. `Handler(14)` выбирает исполнителя заданного класса. `Handler(12)` по RVA `0x9bf1` читает вещественный вес заявки из смещения `+0x14`. Если клану нужен транспорт и свободной машины нет, секция передаёт тот же вес в заявку на выпуск транспортного робота. @@ -398,7 +401,7 @@ operand references: все 492 in-range и указывают на `DWORD`. По out-of-range и incomplete instructions вместо silent coercion. Identity ровно равна -`(slot0 word, slot4 IEEE-754 bits, slot5 IEEE-754 bits)`, поэтому `-0.0` и +`(slot0 word, slot4 word, slot5 word)`, поэтому вещественные `-0.0` и `+0.0` различаются. Новый 100-byte record получает slot1 в поле `+0x14`, slot2 одновременно в `+0x24/+0x28`, slot3 в `+0x2c` и slot6 в `+0x0c`. При совпавшем key refresh случается только когда slot1 сравнивается unequal -- cgit v1.2.3