aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/boarding.rs4
-rw-r--r--crates/fparkan-runtime/src/lib.rs19
-rw-r--r--crates/fparkan-runtime/src/player_weapon.rs37
3 files changed, 53 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs
index 50fa5919..257b81f7 100644
--- a/crates/fparkan-runtime/src/boarding.rs
+++ b/crates/fparkan-runtime/src/boarding.rs
@@ -6,6 +6,8 @@ use fparkan_world::{
handle_by_original_id, identity_metadata, set_transform, transform_state, ObjectHandle,
};
+pub(super) const WARBOT_CLASS: u32 = 0x0100_8000;
+
#[derive(Clone, Copy)]
pub(super) struct Boarding {
pub(super) cockpit: StartingCockpit,
@@ -43,7 +45,7 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
.iter()
.enumerate()
.filter_map(|(index, draft)| {
- if draft.unit_dat_header_flags != Some(0x0100_8000) {
+ if draft.unit_dat_header_flags != Some(WARBOT_CLASS) {
return None;
}
let handle = handle_by_original_id(&engine.world, draft.original_id?)?;
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index c71c5619..c98554a2 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -65,7 +65,7 @@ pub use mission_events::{
MissionNotice,
};
pub use player_motion::{plan_player_motion, PlayerMotion, PlayerMotionStep};
-pub use player_weapon::{starting_player_weapons, PlayerWeapons};
+pub use player_weapon::{prepared_warbot_weapons, starting_player_weapons, PlayerWeapons};
pub use production::order_factory_robot;
pub use projectile::{
apply_projectile_hit, load_player_projectile, projectile_terrain_hit, projectile_unit_hit,
@@ -17740,6 +17740,7 @@ mod tests {
.iter()
.any(|notice| matches!(notice, MissionNotice::ObjectiveComplete(0 | 2))));
let cockpit = starting_cockpit(&engine).unwrap();
+ let prepared = prepared_warbot_weapons(&engine).collect::<Vec<_>>();
assert!(order_factory_robot(&mut engine, cockpit, 1).is_err());
assert_eq!(
capture_neutral_building(&mut engine, cockpit).unwrap(),
@@ -17836,6 +17837,22 @@ mod tests {
.unwrap()
.expect("vehicle cockpit");
assert_eq!(vehicle.handle, robot.handle);
+ let (_, mut weapons) = prepared
+ .into_iter()
+ .find(|(index, _)| *index == vehicle.object_index)
+ .expect("prepared weapons for built warbot");
+ assert_eq!(weapons.weapons().len(), 2);
+ weapons.select(2);
+ assert_eq!(weapons.fire(true, 66_001).count(), 0);
+ let shots = weapons
+ .fire(true, 67_001)
+ .map(|(slot, weapon, _)| {
+ load_player_projectile(&engine, &weapon.projectile_archive, &weapon.projectile)
+ .unwrap();
+ (slot, weapon.projectile.clone())
+ })
+ .collect::<Vec<_>>();
+ assert_eq!(shots, [(1, "bf_b_01".into())]);
assert_eq!(board_friendly_warbot(&mut engine).unwrap(), None);
let motion = plan_player_motion(
&engine,
diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs
index f1fcbf7e..ce92b053 100644
--- a/crates/fparkan-runtime/src/player_weapon.rs
+++ b/crates/fparkan-runtime/src/player_weapon.rs
@@ -3,7 +3,7 @@ use fparkan_assets::{
PreparedComponentControl, PreparedControlEffectSection, PreparedControlPoint,
};
-/// The starting hero's selectable weapon devices, in resource order.
+/// A unit's selectable weapon devices, in resource order.
#[derive(Clone, Default)]
pub struct PlayerWeapons {
weapons: Vec<PlayerWeapon>,
@@ -15,6 +15,8 @@ pub struct PlayerWeapons {
/// A manual-fire device prepared from its CTLD record and effect rows.
#[derive(Clone)]
pub struct PlayerWeapon {
+ /// Component containing the barrel's CPT point.
+ pub component_index: usize,
/// Name of the projectile prototype.
pub projectile: String,
/// Archive named by the weapon's projectile descriptor.
@@ -172,12 +174,34 @@ pub fn starting_player_weapons(engine: &Engine, cockpit: StartingCockpit) -> Pla
.and_then(|controls| controls.get(cockpit.component_index))
.and_then(Option::as_ref);
PlayerWeapons {
- weapons: control.map_or_else(Vec::new, prepare_weapons),
+ weapons: control.map_or_else(Vec::new, |control| {
+ prepare_weapons(cockpit.component_index, control)
+ }),
..PlayerWeapons::default()
}
}
-fn prepare_weapons(control: &PreparedComponentControl) -> Vec<PlayerWeapon> {
+/// Resolves weapon devices for placed and prepared production warbots.
+pub fn prepared_warbot_weapons(
+ engine: &Engine,
+) -> impl Iterator<Item = (usize, PlayerWeapons)> + '_ {
+ engine.loaded.iter().flat_map(|mission| {
+ mission
+ .object_drafts
+ .iter()
+ .zip(&mission.mission_assets.object_component_controls)
+ .enumerate()
+ .filter(|(_, (draft, _))| {
+ draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS)
+ })
+ .map(|(index, (_, controls))| (index, prepare_unit_weapons(controls)))
+ })
+}
+
+fn prepare_weapons(
+ component_index: usize,
+ control: &PreparedComponentControl,
+) -> Vec<PlayerWeapon> {
let Some(points) = &control.point_list else {
return Vec::new();
};
@@ -256,6 +280,7 @@ fn prepare_weapons(control: &PreparedComponentControl) -> Vec<PlayerWeapon> {
_ => None,
};
Some(PlayerWeapon {
+ component_index,
projectile,
projectile_archive,
effect,
@@ -276,8 +301,9 @@ pub(super) fn prepare_unit_weapons(controls: &[Option<PreparedComponentControl>]
PlayerWeapons {
weapons: controls
.iter()
- .flatten()
- .flat_map(prepare_weapons)
+ .enumerate()
+ .filter_map(|(index, control)| Some((index, control.as_ref()?)))
+ .flat_map(|(index, control)| prepare_weapons(index, control))
.collect(),
..PlayerWeapons::default()
}
@@ -291,6 +317,7 @@ mod tests {
fn shot_finishes_after_trigger_release_and_then_spends_ammo() {
let mut weapons = PlayerWeapons {
weapons: vec![PlayerWeapon {
+ component_index: 0,
projectile: String::from("test"),
projectile_archive: String::from("test.rlb"),
effect: None,