diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 82 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/fparkan-script/src/lib.rs | 53 |
3 files changed, 125 insertions, 14 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index b6cda1e8..7c50bf18 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -1,13 +1,16 @@ -use crate::{LoadedMissionState, MissionEventStop}; +use crate::{BehaviorOreState, LoadedMissionState, MissionEventStop}; use fparkan_script::{ScriptDispatchSelector, VarSetDefault}; -use fparkan_world::{OwnerId, World}; +use fparkan_world::{identity_metadata, registration_sequence, OwnerId, World}; use std::collections::BTreeMap; +const STORAGE_CLASS: u32 = 0x8000_0008; +const RESOURCE_ORE: u32 = 0x0200_0100; + #[derive(Default)] pub(super) struct ClanAiEvents { next_ms: BTreeMap<usize, u32>, stops: BTreeMap<(usize, usize, usize), (MissionEventStop, u64)>, - research_status_stubs: BTreeMap<usize, u64>, + research_stubs: BTreeMap<(usize, u8), u64>, } impl LoadedMissionState { @@ -55,6 +58,12 @@ impl LoadedMissionState { (available - demand).to_bits(), ))) } + ScriptDispatchSelector::Handler(16) + if instruction.dword_pair(values) == Some((STORAGE_CLASS, RESOURCE_ORE)) => + { + let remaining = storage_space(&self.behavior_ore_states, world, OwnerId(clan as u16)); + Ok(Some(VarSetDefault::FloatBits(remaining.to_bits()))) + } ScriptDispatchSelector::Handler(17) if instruction.dword_pair(values) == Some((crate::mining::MINE_CLASS, 0x0afc_0900)) => @@ -88,17 +97,17 @@ impl LoadedMissionState { ), ))) } - ScriptDispatchSelector::Handler(65) if instruction.references.is_empty() => { - // RVA 0x111d0 returns false while the research catalog is unloaded. + ScriptDispatchSelector::Handler(handler @ (56 | 65)) if instruction.references.is_empty() => { + // shortcut: keep research disabled until the native policy flag and catalog are wired. let count = self .clan_ai_events - .research_status_stubs - .entry(clan) + .research_stubs + .entry((clan, handler)) .or_default(); *count += 1; if *count == 1 { eprintln!( - "sim_coverage script_research_status_stub=1 clan={clan} script={}", + "sim_coverage script_research_status_stub=1 clan={clan} handler={handler} script={}", bundle.script_path ); } @@ -137,6 +146,19 @@ fn count_free_minds(slots: &[u32]) -> u32 { slots.iter().filter(|&&word| word == u32::MAX).count() as u32 } +fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32 { + ores.iter() + .filter(|ore| { + ore.class_flags == STORAGE_CLASS + && registration_sequence(world, ore.handle).is_ok_and(|sequence| sequence.is_some()) + && identity_metadata(world, ore.handle) + .is_ok_and(|identity| identity.owner_id == Some(clan)) + }) + .fold(0.0, |space, ore| { + space + (f32::from_bits(ore.maximum_ore_bits) - f32::from_bits(ore.current_ore_bits)) + }) +} + impl ClanAiEvents { fn due(&mut self, clan: usize, start_ms: u32, now_ms: u32) -> bool { let next = self.next_ms.entry(clan).or_insert(7_000); @@ -152,10 +174,10 @@ impl ClanAiEvents { self.stops.values().map(|(stop, count)| (stop, *count)) } - pub(super) fn research_status_stubs(&self) -> impl Iterator<Item = (usize, u64)> + '_ { - self.research_status_stubs + pub(super) fn research_status_stubs(&self) -> impl Iterator<Item = (usize, u8, u64)> + '_ { + self.research_stubs .iter() - .map(|(&clan, &count)| (clan, count)) + .map(|(&(clan, handler), &count)| (clan, handler, count)) } } @@ -179,4 +201,42 @@ mod tests { assert_eq!(count_free_minds(&[]), 0); assert_eq!(count_free_minds(&[u32::MAX, 0, u32::MAX, 1, 0xfffe]), 2); } + + #[test] + fn free_ore_space_follows_storage_ownership_and_lifecycle() { + use fparkan_world::{ + construct_object, new, register_object, request_delete, set_owner, step, InputSnapshot, + ObjectDraft, OriginalObjectId, WorldConfig, + }; + let mut world = new(WorldConfig); + let mut make = |id, class_flags, owner, ore: f32| { + let original_id = OriginalObjectId(id); + let handle = construct_object( + &mut world, + ObjectDraft { + original_id: Some(original_id), + }, + ) + .unwrap(); + set_owner(&mut world, handle, Some(OwnerId(owner))).unwrap(); + register_object(&mut world, handle).unwrap(); + BehaviorOreState { + original_id, + handle, + class_flags, + current_ore_bits: ore.to_bits(), + maximum_ore_bits: 100.0_f32.to_bits(), + } + }; + let ores = [ + make(1, STORAGE_CLASS, 1, 15.0), + make(2, STORAGE_CLASS, 1, 40.0), + make(3, STORAGE_CLASS, 0, 10.0), + make(4, crate::mining::MINE_CLASS, 1, 20.0), + ]; + assert_eq!(storage_space(&ores, &world, OwnerId(1)), 145.0); + request_delete(&mut world, ores[0].handle).unwrap(); + step(&mut world, &InputSnapshot).unwrap(); + assert_eq!(storage_space(&ores, &world, OwnerId(1)), 60.0); + } } diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 79292336..7b1eefa1 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2900,8 +2900,8 @@ pub struct SimulationReport { pub mission_event_stops: Vec<MissionEventStop>, /// Uncovered clan AI instructions and the number of invocations they stopped. pub clan_ai_event_stops: Vec<(MissionEventStop, u64)>, - /// Research status calls handled with an unloaded catalog, grouped by clan. - pub clan_research_status_stubs: Vec<(usize, u64)>, + /// Calls to stubbed research policy or status, grouped by clan and handler. + pub clan_research_status_stubs: Vec<(usize, u8, u64)>, /// Total requests for mission information messages. pub mission_messages: u64, /// Clock of the first autonomous shot, in milliseconds. diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs index 5df749ca..2c3eccf1 100644 --- a/crates/fparkan-script/src/lib.rs +++ b/crates/fparkan-script/src/lib.rs @@ -1141,7 +1141,10 @@ impl VarSet { ) -> Result<Option<VarSetDefault>, String>, ) -> Result<(), (usize, String)> { let mut conditions = Vec::new(); - for (index, instruction) in event.instructions.iter().enumerate() { + let mut pc = 0; + while let Some(instruction) = event.instructions.get(pc) { + let index = pc; + pc += 1; match advance_condition(&mut conditions, instruction, values) .map_err(|()| (index, "unsupported comparison operands".into()))? { @@ -1150,6 +1153,17 @@ impl VarSet { EventInstruction::Execute => {} } let control = instruction.header_words[3]; + if control == 3 && instruction.dispatch_selector() == ScriptDispatchSelector::Sentinel { + let &[target] = instruction.references.as_slice() else { + return Err((index, "jump requires one instruction index".into())); + }; + pc = target as usize; + if pc <= index || pc >= event.instructions.len() { + return Err((index, format!("unsupported jump to instruction {target}"))); + } + conditions.clear(); + continue; + } if instruction.dispatch_selector() != ScriptDispatchSelector::Sentinel { if control != u32::MAX { return Err((index, format!("unsupported handler control {control}"))); @@ -2397,6 +2411,43 @@ mod tests { } #[test] + fn active_jump_skips_to_the_raw_instruction_index_and_leaves_conditions() { + let varset = parse_varset(b"VAR(DWORD, left, 0)\nVAR(DWORD, right, 0)\n").unwrap(); + let event = ScriptEvent { + name_len: 9, + name_raw: b"Problems0\0".to_vec(), + event_word: 0, + instructions: [ + ( + [u32::MAX, u32::MAX, u32::MAX, 0, 1, 2, u32::MAX], + vec![0, 1], + ), + ([u32::MAX, u32::MAX, u32::MAX, 3, 6, 1, u32::MAX], vec![3]), + ([2, u32::MAX, u32::MAX, u32::MAX, 6, 0, u32::MAX], vec![]), + ([3, u32::MAX, u32::MAX, u32::MAX, 6, 0, u32::MAX], vec![]), + ] + .into_iter() + .map(|(header_words, references)| ScriptInstruction { + header_words, + references, + }) + .collect(), + }; + for (rhs, expected) in [(0, vec![3]), (1, vec![])] { + let mut values = [VarSetDefault::Dword(0), VarSetDefault::Dword(rhs)]; + let mut calls = Vec::new(); + varset + .execute_event_prefix(&event, None, &mut values, |instruction, _| { + calls.push(instruction.header_words[0]); + Ok(None) + }) + .unwrap(); + // A taken jump clears the conditions; an untaken one stays in the false branch. + assert_eq!(calls, expected); + } + } + + #[test] fn periodic_event_accepts_a_float_result_from_a_typed_host() { let varset = parse_varset(b"VAR(float, remaining, 0)\n").unwrap(); let event = ScriptEvent { |
