aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/lib.rs15
-rw-r--r--crates/fparkan-fx/src/ambient_sound.rs18
-rw-r--r--crates/fparkan-runtime/src/lib.rs26
-rw-r--r--crates/fparkan-runtime/src/player_weapon.rs196
4 files changed, 255 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index e8e892d8..615758bc 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -1104,6 +1104,8 @@ pub struct PreparedComponentControl {
/// Validated group selected by native Control's first effect-group
/// header word; `None` is the native no-group sentinel or an invalid index.
pub initial_effect_group: Option<usize>,
+ /// Node-to-effect links from Control opcode 14, resolved within each group.
+ pub node_effect_links: Vec<(usize, u32, u32)>,
/// Component-local points selected for parsed effect rows, when present.
pub point_list: Option<PreparedControlPointList>,
}
@@ -3950,6 +3952,7 @@ struct DecodedComponentEffectSection {
initial_effect_group: Option<usize>,
initial_group_opcodes: Option<Vec<u32>>,
typed_records: Vec<PreparedControlTypedRecord>,
+ node_effect_links: Vec<(usize, u32, u32)>,
}
fn decode_component_effect_section<F>(
@@ -3975,6 +3978,7 @@ where
initial_effect_group: None,
initial_group_opcodes: (cursor == payload.len()).then(Vec::new),
typed_records: Vec::new(),
+ node_effect_links: Vec::new(),
});
}
if typed_record_count > CONTROL_TYPED_RECORD_LIMIT {
@@ -3997,6 +4001,7 @@ where
initial_effect_group: None,
initial_group_opcodes: None,
typed_records,
+ node_effect_links: Vec::new(),
});
}
@@ -4098,6 +4103,7 @@ where
initial_effect_group: None,
initial_group_opcodes: complete.then(Vec::new),
typed_records,
+ node_effect_links: Vec::new(),
});
}
@@ -4117,6 +4123,7 @@ where
let mut initial_group_opcodes = Some(Vec::new());
let mut effect_rows = Vec::new();
+ let mut node_effect_links = Vec::new();
for table_index in 0..effect_group_count {
let row_count = usize::try_from(
control_u32_at(payload, cursor)
@@ -4143,6 +4150,11 @@ where
let row_start = rows_start + row_index * CONTROL_EFFECT_ROW_BYTES;
let row = &payload[row_start..row_start + CONTROL_EFFECT_ROW_BYTES];
let opcode = control_u32_at(row, 0x0C).expect("fixed effect row has opcode");
+ if opcode == 14 {
+ let selector = control_u32_at(row, 0x10).expect("fixed row has a selector");
+ let node = control_u32_at(row, 0x14).expect("fixed row has a node");
+ node_effect_links.push((table_index, node, selector));
+ }
if initial_effect_group == Some(table_index) {
initial_group_opcodes
.as_mut()
@@ -4190,6 +4202,7 @@ where
initial_effect_group: None,
initial_group_opcodes: None,
typed_records,
+ node_effect_links: Vec::new(),
});
}
Ok(DecodedComponentEffectSection {
@@ -4197,6 +4210,7 @@ where
initial_effect_group,
initial_group_opcodes,
typed_records,
+ node_effect_links,
})
}
@@ -4376,6 +4390,7 @@ fn decode_component_control_payload_with_opcodes(
node_data: None,
effect_section: decoded_effect.section,
initial_effect_group: decoded_effect.initial_effect_group,
+ node_effect_links: decoded_effect.node_effect_links,
point_list: None,
},
initial_group_opcodes,
diff --git a/crates/fparkan-fx/src/ambient_sound.rs b/crates/fparkan-fx/src/ambient_sound.rs
index bcc18149..c15e0637 100644
--- a/crates/fparkan-fx/src/ambient_sound.rs
+++ b/crates/fparkan-fx/src/ambient_sound.rs
@@ -89,6 +89,24 @@ impl Op2SoundCommand {
})
}
+ /// Starts a prepared one-shot at its initial authored parameters.
+ #[must_use]
+ pub fn one_shot_at(&self, position: [f32; 3]) -> Option<SoundEvent> {
+ if self.looping || !position.iter().all(|axis| axis.is_finite()) {
+ return None;
+ }
+ Some(SoundEvent {
+ archive: self.archive.clone(),
+ name: self.name.clone(),
+ position,
+ volume: 1.0,
+ min_distance: self.min_distance,
+ max_distance: self.max_distance,
+ frequency_ratio: self.frequency,
+ action: SoundAction::OneShot,
+ })
+ }
+
/// Evaluates Effect.dll's phase crossings and current source parameters.
/// `playing` comes from the audio owner, since a one-shot can finish between
/// updates. The matrix maps local command coordinates into world space.
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 883e5554..3dae4afa 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -45,6 +45,7 @@ mod autodemo;
mod mesh_collision;
mod mission_events;
mod player_motion;
+mod player_weapon;
pub use autodemo::{autodemo_camera, AutoDemoCamera, AutoDemoView};
pub use fparkan_assets::MissionAssets;
@@ -52,6 +53,7 @@ pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, Building
use mission_events::MissionEvents;
pub use mission_events::{MissionEventStop, MissionEventTick, MissionNotice};
pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep};
+pub use player_weapon::{starting_player_weapons, PlayerWeapons};
const MISSION_DECODED_PAYLOAD_CACHE_ENTRIES: usize = 256;
const MAX_MISSION_AI_RECORD_SLOTS: usize = 1_000_000;
@@ -8182,6 +8184,7 @@ mod tests {
node_data: None,
effect_section: PreparedControlEffectSection::Parsed(rows),
initial_effect_group,
+ node_effect_links: Vec::new(),
point_list: point_count.map(|count| PreparedControlPointList {
source: synthetic_resource_key(941),
points: (0..count)
@@ -8686,6 +8689,7 @@ mod tests {
node_data: None,
effect_section,
initial_effect_group: None,
+ node_effect_links: Vec::new(),
point_list: None,
}
}
@@ -17213,6 +17217,28 @@ mod tests {
},
)
.unwrap();
+ let cockpit = starting_cockpit(&engine).unwrap();
+ let weapons = starting_player_weapons(&engine, cockpit);
+ let projectiles = weapons
+ .weapons()
+ .iter()
+ .map(|weapon| weapon.projectile.as_str())
+ .collect::<Vec<_>>();
+ assert_eq!(projectiles, ["bb_h_01", "bp_h_01", "bl_h_01", "bm_h_01"]);
+ let effects = weapons
+ .weapons()
+ .iter()
+ .map(|weapon| weapon.effect.as_ref().map(|(_, name)| name.as_str()))
+ .collect::<Vec<_>>();
+ assert_eq!(
+ effects,
+ [
+ Some("hero_cannon"),
+ Some("hero_prifle"),
+ Some("hero_redlaser"),
+ None
+ ]
+ );
let first = frame(&mut engine, 5).unwrap().mission_event.unwrap();
assert_eq!(
first.notices,
diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs
new file mode 100644
index 00000000..510cabb4
--- /dev/null
+++ b/crates/fparkan-runtime/src/player_weapon.rs
@@ -0,0 +1,196 @@
+use crate::{Engine, StartingCockpit};
+use fparkan_assets::{
+ PreparedComponentControl, PreparedControlEffectSection, PreparedControlPoint,
+};
+
+/// The starting hero's selectable weapon devices, in resource order.
+#[derive(Default)]
+pub struct PlayerWeapons {
+ weapons: Vec<PlayerWeapon>,
+ selected: Option<usize>,
+}
+
+/// A manual-fire device prepared from its CTLD record and effect rows.
+pub struct PlayerWeapon {
+ /// Name of the projectile prototype.
+ pub projectile: String,
+ /// Archive named by the weapon's projectile descriptor.
+ pub projectile_archive: String,
+ /// Point used by the weapon animation's first firing row.
+ pub muzzle: PreparedControlPoint,
+ /// Optional type-4 effect started by this weapon.
+ pub effect: Option<(String, String)>,
+ interval_ms: u32,
+ last_fire_ms: Option<u32>,
+}
+
+impl PlayerWeapons {
+ /// Returns the prepared devices in selection order.
+ pub fn weapons(&self) -> &[PlayerWeapon] {
+ &self.weapons
+ }
+
+ /// Selects all weapons with zero, or one weapon by its one-based slot.
+ pub fn select(&mut self, slot: usize) {
+ match slot {
+ 0 => self.selected = None,
+ n if n <= self.weapons.len() => self.selected = Some(n - 1),
+ _ => {}
+ }
+ }
+
+ /// Samples the active trigger on the queue clock and emits due shots.
+ /// At most one shot per weapon is produced by a single simulation sample.
+ pub fn fire(
+ &mut self,
+ trigger: bool,
+ clock_ms: u32,
+ ) -> impl Iterator<Item = (usize, &PlayerWeapon)> {
+ let selected = self.selected;
+ self.weapons
+ .iter_mut()
+ .enumerate()
+ .filter_map(move |(index, weapon)| {
+ if !trigger || selected.is_some_and(|selected| selected != index) {
+ return None;
+ }
+ if weapon
+ .last_fire_ms
+ .is_some_and(|last| clock_ms.wrapping_sub(last) < weapon.interval_ms)
+ {
+ return None;
+ }
+ weapon.last_fire_ms = Some(clock_ms);
+ Some((index, &*weapon))
+ })
+ }
+}
+
+/// Resolves starting weapons from the hero's mounted CTLD and CPT resources.
+pub fn starting_player_weapons(engine: &Engine, cockpit: StartingCockpit) -> PlayerWeapons {
+ let control = engine
+ .loaded
+ .as_ref()
+ .and_then(|mission| {
+ mission
+ .mission_assets
+ .object_component_controls
+ .get(cockpit.object_index)
+ })
+ .and_then(|controls| controls.get(cockpit.component_index))
+ .and_then(Option::as_ref);
+ PlayerWeapons {
+ weapons: control.map_or_else(Vec::new, prepare_weapons),
+ ..PlayerWeapons::default()
+ }
+}
+
+fn prepare_weapons(control: &PreparedComponentControl) -> Vec<PlayerWeapon> {
+ let Some(points) = &control.point_list else {
+ return Vec::new();
+ };
+ control
+ .typed_records
+ .iter()
+ .filter(|record| record.tag == 2)
+ .filter_map(|record| {
+ let projectile_archive =
+ crate::fixed_words_ascii_c_string(&record.fixed_words[27..35])?;
+ let projectile = crate::fixed_words_ascii_c_string(&record.fixed_words[35..43])?;
+ if projectile_archive.is_empty() || projectile.is_empty() {
+ return None;
+ }
+ let row = control
+ .timeline_rows
+ .get(*record.values.first()? as usize)?;
+ if row.flags & 0x40 != 0 {
+ return None;
+ }
+ let muzzle = points.points.get(row.raw_parameters[1] as usize)?;
+ if muzzle.raw_signed_field_04 < 0
+ || muzzle.raw_signed_field_04 != muzzle.raw_signed_field_08
+ {
+ return None;
+ }
+ let interval_ms =
+ f32::from_bits(record.fixed_words[14]) + f32::from_bits(record.fixed_words[15]);
+ if !interval_ms.is_finite() || interval_ms < 0.0 {
+ return None;
+ }
+ let effect = match &control.effect_section {
+ PreparedControlEffectSection::Parsed(rows) => control
+ .node_effect_links
+ .iter()
+ .find(|&&(group, node, _)| {
+ Some(group) == control.initial_effect_group
+ && node as usize == row.node_index
+ })
+ .and_then(|&(group, _, selector)| {
+ rows.iter()
+ .find(|row| row.table_index == group && row.selector == selector)
+ })
+ .and_then(|row| {
+ Some((
+ crate::bounded_ascii_c_string(&row.resource.archive)?,
+ crate::bounded_ascii_c_string(&row.resource.name)?,
+ ))
+ }),
+ _ => None,
+ };
+ Some(PlayerWeapon {
+ projectile,
+ projectile_archive,
+ muzzle: muzzle.clone(),
+ effect,
+ // shortcut: begin the next cycle after the two configured
+ // waits and a 60 Hz step for each of the four native phases.
+ interval_ms: (interval_ms + 4.0 * (1000.0 / 60.0)).ceil() as u32,
+ last_fire_ms: None,
+ })
+ })
+ .collect()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn trigger_respects_selection_and_independent_weapon_waits() {
+ let mut weapons = PlayerWeapons {
+ selected: None,
+ weapons: [100, 250]
+ .map(|interval_ms| PlayerWeapon {
+ projectile: String::from("test"),
+ projectile_archive: String::from("test.rlb"),
+ muzzle: PreparedControlPoint {
+ raw_flags: 0,
+ raw_signed_field_04: 0,
+ raw_signed_field_08: 0,
+ position: [0.0; 3],
+ direction: [0.0, 1.0, 0.0],
+ name: [0; 32],
+ },
+ effect: None,
+ interval_ms,
+ last_fire_ms: None,
+ })
+ .into(),
+ };
+ let mut fired = |trigger, t| {
+ weapons
+ .fire(trigger, t)
+ .map(|(slot, _)| slot)
+ .collect::<Vec<_>>()
+ };
+ assert!(fired(false, 0).is_empty());
+ assert_eq!(fired(true, 0), [0, 1]);
+ assert!(fired(true, 99).is_empty());
+ assert_eq!(fired(true, 100), [0]);
+ weapons.select(2);
+ assert!(weapons.fire(true, 249).next().is_none());
+ assert_eq!(weapons.fire(true, 250).next().unwrap().0, 1);
+ weapons.select(0);
+ assert_eq!(weapons.fire(true, 251).next().unwrap().0, 0);
+ }
+}