aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 03:13:04 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 03:13:04 +0300
commit446081d70a4bb6a1bc54fa486e9b439dc02cb31a (patch)
tree202b506f813eaa977d4ecf6afd7d80787ff7485e /crates
parent85f3d6a8b4c09604a560e03596357b7bcb62c496 (diff)
downloadfparkan-446081d70a4bb6a1bc54fa486e9b439dc02cb31a.tar.xz
fparkan-446081d70a4bb6a1bc54fa486e9b439dc02cb31a.zip
feat(runtime): start missions with explicit Init stubs
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/lib.rs192
-rw-r--r--crates/fparkan-script/src/lib.rs71
2 files changed, 191 insertions, 72 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index b9b624d2..7c9fea3e 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -24,10 +24,11 @@ use fparkan_prototype::{
use fparkan_resource::{
resource_name, CachedResourceRepository, ResourceError, ResourceKey, ResourceRepository,
};
+#[cfg(test)]
+use fparkan_script::ScriptInitResolution;
use fparkan_script::{
- FormulaTable, Handler19InitInput, ScriptInitResolution, ScriptInitResolveError,
- ScriptInitWrite, ScriptInstruction, ScriptPackage, VarSet, VarSetDefault,
- VmHostCallbackCommand,
+ FormulaTable, Handler19InitInput, ScriptInitResolveError, ScriptInitWrite, ScriptInstruction,
+ ScriptPackage, VarSet, VarSetDefault, VmHostCallbackCommand,
};
use fparkan_terrain::SurfaceQuery;
use fparkan_vfs::{Vfs, VfsError};
@@ -1079,7 +1080,7 @@ pub enum MissionScriptInitExecution {
/// Zero-based event table index selected by native lookup.
event_index: usize,
},
- /// A preview retained the decoded package but stopped before the first
+ /// The clan stopped before the first
/// instruction outside the recovered numeric `Init` subset.
Unsupported {
/// Zero-based event table index selected by native lookup.
@@ -1133,6 +1134,14 @@ pub enum MissionScriptHostEffect {
struct MissionLoadOptions {
fail_after_registered_objects: Option<usize>,
asset_scope: MissionAssetScope,
+ allow_partial_init: bool,
+}
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+enum InitPolicy {
+ Strict,
+ Preview,
+ KeepPrefix,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
@@ -1649,6 +1658,28 @@ pub fn load_mission(
)
}
+/// Loads the mission world even when a clan reaches an unsupported `Init` instruction.
+/// Keeps the supported prefix and records the stop in the clan's execution status.
+///
+/// # Errors
+///
+/// Malformed data and other mission load errors still abort loading.
+pub fn load_mission_with_init_stubs(
+ engine: &mut Engine,
+ request: MissionRequest,
+ startup_input: MissionStartupInput,
+) -> Result<LoadedMission, EngineError> {
+ load_mission_with_options(
+ engine,
+ request,
+ MissionLoadOptions {
+ allow_partial_init: true,
+ ..MissionLoadOptions::default()
+ },
+ Some(startup_input),
+ )
+}
+
/// Loads a mission using the bounded static-preview asset scope.
///
/// This mode preserves map, TMA, graph, construction and registration work,
@@ -2087,6 +2118,11 @@ fn load_mission_with_options(
&mut loaded_scripts,
queue_time_ms,
&mut next_behavior_rng_state,
+ if options.allow_partial_init {
+ InitPolicy::KeepPrefix
+ } else {
+ InitPolicy::Strict
+ },
) {
let _ = fparkan_world::shutdown(new_runtime_world);
return Err(error);
@@ -7183,7 +7219,7 @@ fn load_mission_scripts(
.collect::<Vec<_>>(),
&mut bundles,
varset,
- asset_scope,
+ InitPolicy::Preview,
)?;
let varset_states = materialize_script_varset_states(&bundles, varset, &init_states)?;
(init_states, varset_states)
@@ -7204,6 +7240,7 @@ fn start_mission_scripts(
scripts: &mut LoadedMissionScripts,
queue_time_ms: u32,
behavior_rng_state: &mut u32,
+ init_policy: InitPolicy,
) -> Result<(), EngineError> {
// The recovered slot refreshes one Behavior instance before dispatching
// that same instance's Init event. The Rust loader waits until its new
@@ -7225,7 +7262,7 @@ fn start_mission_scripts(
&clan_anchors,
std::slice::from_mut(bundle),
varset,
- MissionAssetScope::Full,
+ init_policy,
)?);
}
}
@@ -7283,7 +7320,7 @@ fn resolve_init_states(
clan_anchors: &[[f32; 2]],
bundles: &mut [MissionScriptBundle],
varset: &MissionScriptVarSet,
- asset_scope: MissionAssetScope,
+ policy: InitPolicy,
) -> Result<Vec<MissionScriptInitState>, EngineError> {
let mut states = Vec::new();
for bundle in bundles {
@@ -7310,47 +7347,20 @@ fn resolve_init_states(
message: "clan index does not fit the recovered DWORD ClanID field".to_string(),
})?,
};
- match varset
+ let prefix = varset
.declarations
- .resolve_init(&bundle.package, input, bundle.formula_table.as_ref())
+ .resolve_init_prefix(&bundle.package, input, bundle.formula_table.as_ref())
.map_err(|source| EngineError::ScriptInit {
clan_index: bundle.clan_index,
message: source.to_string(),
- })? {
- ScriptInitResolution::Missing => {
- bundle.init_execution = MissionScriptInitExecution::Missing;
- }
- ScriptInitResolution::Executed {
- event_index,
- event_word,
- writes,
- } => {
- bundle.init_execution = MissionScriptInitExecution::Executed { event_index };
- if !writes.is_empty() {
- states.push(MissionScriptInitState {
- clan_index: bundle.clan_index,
- event_index,
- event_word,
- writes,
- });
- }
- }
- ScriptInitResolution::Unsupported {
- event_index,
- instruction_index,
- error,
- } if asset_scope != MissionAssetScope::Full => {
- bundle.init_execution = MissionScriptInitExecution::Unsupported {
- event_index,
- instruction_index,
- error,
- };
- }
- ScriptInitResolution::Unsupported {
- event_index,
- instruction_index,
- error,
- } => {
+ })?;
+ let Some(prefix) = prefix else {
+ bundle.init_execution = MissionScriptInitExecution::Missing;
+ continue;
+ };
+ let event_index = prefix.event_index;
+ if let Some((instruction_index, error)) = prefix.stopped_at {
+ if policy == InitPolicy::Strict {
return Err(EngineError::ScriptInitUnsupported {
clan_index: bundle.clan_index,
path: bundle.script_path.clone(),
@@ -7359,6 +7369,24 @@ fn resolve_init_states(
source: error,
});
}
+ bundle.init_execution = MissionScriptInitExecution::Unsupported {
+ event_index,
+ instruction_index,
+ error,
+ };
+ if policy == InitPolicy::Preview {
+ continue;
+ }
+ } else {
+ bundle.init_execution = MissionScriptInitExecution::Executed { event_index };
+ }
+ if !prefix.writes.is_empty() {
+ states.push(MissionScriptInitState {
+ clan_index: bundle.clan_index,
+ event_index,
+ event_word: prefix.event_word,
+ writes: prefix.writes,
+ });
}
}
Ok(states)
@@ -11763,8 +11791,14 @@ mod tests {
};
let mut rng_state = INITIAL_BEHAVIOR_RNG_STATE;
- let error = start_mission_scripts(&mission, &mut scripts, 100_000, &mut rng_state)
- .expect_err("unsupported first Init stops startup before the second bundle");
+ let error = start_mission_scripts(
+ &mission,
+ &mut scripts,
+ 100_000,
+ &mut rng_state,
+ InitPolicy::Strict,
+ )
+ .expect_err("unsupported first Init stops startup before the second bundle");
assert!(matches!(
error,
@@ -15094,7 +15128,7 @@ mod tests {
&[[500.0, 752.0], [728.0, 449.0]],
&mut bundles,
&varset,
- MissionAssetScope::Full,
+ InitPolicy::Strict,
)
.expect("exact Init execution");
assert_eq!(
@@ -15198,13 +15232,9 @@ mod tests {
};
let anchors = [[500.0, 752.0]];
let mut preview_bundles = vec![bundle()];
- let preview_states = resolve_init_states(
- &anchors,
- &mut preview_bundles,
- &varset,
- MissionAssetScope::FirstMeshPreview,
- )
- .expect("static preview retains unsupported script");
+ let preview_states =
+ resolve_init_states(&anchors, &mut preview_bundles, &varset, InitPolicy::Preview)
+ .expect("static preview retains unsupported script");
assert!(preview_states.is_empty(), "no partial writes are retained");
assert_eq!(
preview_bundles[0].init_execution,
@@ -15231,13 +15261,63 @@ mod tests {
);
assert_eq!(preview_bundles[0].package, package);
+ let mut partial_bundles = vec![
+ bundle(),
+ MissionScriptBundle {
+ clan_index: 1,
+ ..bundle()
+ },
+ ];
+ partial_bundles[0].package.events[0]
+ .instructions
+ .push(fparkan_script::ScriptInstruction {
+ header_words: [u32::MAX, 0, 99, 6, 6, 0, u32::MAX],
+ references: Vec::new(),
+ });
+ partial_bundles[1].package.events[0]
+ .instructions
+ .truncate(1);
+ let partial_states = resolve_init_states(
+ &[anchors[0], [10.0, 20.0]],
+ &mut partial_bundles,
+ &varset,
+ InitPolicy::KeepPrefix,
+ )
+ .expect("partial Init keeps the prefix and continues with the next clan");
+ assert_eq!(
+ partial_bundles[0].init_execution,
+ preview_bundles[0].init_execution
+ );
+ assert_eq!(
+ partial_bundles[1].init_execution,
+ MissionScriptInitExecution::Executed { event_index: 0 }
+ );
+ let cells =
+ materialize_script_varset_states(&partial_bundles, &varset, &partial_states).unwrap();
+ assert_eq!(
+ cells[0].values,
+ [
+ VarSetDefault::Dword(502),
+ VarSetDefault::Dword(752),
+ VarSetDefault::Dword(0)
+ ]
+ );
+ assert_eq!(
+ cells[1].values,
+ [
+ VarSetDefault::Dword(10),
+ VarSetDefault::Dword(20),
+ VarSetDefault::Dword(1)
+ ]
+ );
+
let mut full_bundles = vec![bundle()];
assert!(matches!(
resolve_init_states(
&anchors,
&mut full_bundles,
&varset,
- MissionAssetScope::Full,
+ InitPolicy::Strict,
),
Err(EngineError::ScriptInitUnsupported {
clan_index: 0,
@@ -15264,7 +15344,7 @@ mod tests {
&anchors,
&mut malformed_bundle,
&varset,
- MissionAssetScope::FirstMeshPreview,
+ InitPolicy::Preview,
),
Err(EngineError::ScriptInit { message, .. })
if message.contains("Handler(19) is missing reference 0")
diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs
index 71e694d9..33da97bf 100644
--- a/crates/fparkan-script/src/lib.rs
+++ b/crates/fparkan-script/src/lib.rs
@@ -185,6 +185,18 @@ pub enum ScriptInitResolution {
},
}
+/// Proven writes before the first unsupported instruction of a selected `Init`.
+pub struct ScriptInitPrefix {
+ /// Zero-based selected event index.
+ pub event_index: usize,
+ /// Opaque event word retained from the package.
+ pub event_word: u32,
+ /// Ordered writes from instructions that ran.
+ pub writes: Vec<ScriptInitWrite>,
+ /// Zero-based instruction index and reason, if execution stopped early.
+ pub stopped_at: Option<(usize, ScriptInitResolveError)>,
+}
+
/// A typed failure or unsupported form in the recovered `Init` subset.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ScriptInitResolveError {
@@ -733,9 +745,8 @@ impl VarSet {
/// Resolves the first case-sensitive `Init` event's recovered write subset.
///
/// Handler(19) writes use the caller's original setter-boundary words. A
- /// sentinel selector is accepted only for the proven formula assignment
- /// with control and copy-source words both equal to `0xffffffff`; the
- /// referenced row is evaluated only when that instruction is reached.
+ /// sentinel selector accepts immediate and formula assignments. A
+ /// referenced formula row is evaluated only when its instruction is reached.
/// Formula atoms read defaults and earlier successful writes in this event.
///
/// # Errors
@@ -748,10 +759,40 @@ impl VarSet {
input: Handler19InitInput,
formulas: Option<&FormulaTable>,
) -> Result<ScriptInitResolution, ScriptInitResolveError> {
+ Ok(match self.resolve_init_prefix(package, input, formulas)? {
+ None => ScriptInitResolution::Missing,
+ Some(ScriptInitPrefix {
+ event_index,
+ stopped_at: Some((instruction_index, error)),
+ ..
+ }) => ScriptInitResolution::Unsupported {
+ event_index,
+ instruction_index,
+ error,
+ },
+ Some(prefix) => ScriptInitResolution::Executed {
+ event_index: prefix.event_index,
+ event_word: prefix.event_word,
+ writes: prefix.writes,
+ },
+ })
+ }
+
+ /// Resolves the supported prefix for a caller that can report a partial `Init`.
+ ///
+ /// # Errors
+ ///
+ /// Malformed instructions still return an error without publishing writes.
+ pub fn resolve_init_prefix(
+ &self,
+ package: &ScriptPackage,
+ input: Handler19InitInput,
+ formulas: Option<&FormulaTable>,
+ ) -> Result<Option<ScriptInitPrefix>, ScriptInitResolveError> {
let Some((event_index, event)) = package.events.iter().enumerate().find(|(_, event)| {
event.name_raw.split(|byte| *byte == 0).next() == Some(&b"Init"[..])
}) else {
- return Ok(ScriptInitResolution::Missing);
+ return Ok(None);
};
let mut values = self
@@ -759,7 +800,12 @@ impl VarSet {
.iter()
.map(|declaration| declaration.default_value)
.collect::<Vec<_>>();
- let mut writes = Vec::new();
+ let mut prefix = ScriptInitPrefix {
+ event_index,
+ event_word: event.event_word,
+ writes: Vec::new(),
+ stopped_at: None,
+ };
for (instruction_index, instruction) in event.instructions.iter().enumerate() {
match self.resolve_init_instruction(instruction, input, formulas, &values)? {
InitInstructionResolution::Executed(instruction_writes) => {
@@ -772,23 +818,16 @@ impl VarSet {
};
*value = write.value;
}
- writes.extend(instruction_writes);
+ prefix.writes.extend(instruction_writes);
}
InitInstructionResolution::Unsupported(error) => {
- return Ok(ScriptInitResolution::Unsupported {
- event_index,
- instruction_index,
- error,
- });
+ prefix.stopped_at = Some((instruction_index, error));
+ break;
}
}
}
- Ok(ScriptInitResolution::Executed {
- event_index,
- event_word: event.event_word,
- writes,
- })
+ Ok(Some(prefix))
}
fn resolve_init_instruction(