aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/cockpit_instruments.rs63
-rw-r--r--crates/fparkan-runtime/src/projectile_shield.rs8
-rw-r--r--crates/fparkan-runtime/src/weapon_pose.rs50
3 files changed, 114 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/cockpit_instruments.rs b/crates/fparkan-runtime/src/cockpit_instruments.rs
index 6aa2e3f6..88ea8df5 100644
--- a/crates/fparkan-runtime/src/cockpit_instruments.rs
+++ b/crates/fparkan-runtime/src/cockpit_instruments.rs
@@ -3,8 +3,10 @@
use crate::{
control_device_provider_value, control_device_selector_zero,
projectile::unit_sphere,
+ projectile_shield::{enclosing_sphere, world_sphere},
query_control_device_table_selector,
strategic_map_readout::{contact_readout, ContactReadout},
+ weapon_pose::WeaponPoses,
Engine, MissionControlDeviceTableStatus, StartingCockpit, HERO_UNIT_CLASS,
};
use fparkan_world::{identity_metadata, transform_state, ObjectHandle};
@@ -87,15 +89,27 @@ pub fn instrument_readout(
})
}
-/// The same root sphere used for the live object's spatial queries.
+/// Local sphere used by AniMesh selector 2 with the targeter's node filter.
+pub fn instrument_bounds(engine: &Engine, object_index: usize) -> Option<([f32; 3], f32)> {
+ let mission = engine.loaded.as_ref()?;
+ let draft = mission.object_drafts.get(object_index)?;
+ let poses = WeaponPoses::new(draft, &mission.mission_assets, object_index);
+ enclosing_sphere(&poses.instrument_node_spheres(&mission.mission_assets, object_index))
+}
+
+/// Places the sphere in the object's current world pose.
pub fn instrument_sphere(
engine: &Engine,
subject: ObjectHandle,
object_index: usize,
+ bounds: Option<([f32; 3], f32)>,
) -> Option<([f32; 3], f32)> {
let mission = engine.loaded.as_ref()?;
let pose = transform_state(&engine.world, subject).ok()?;
- unit_sphere(&mission.mission_assets, object_index, pose)
+ bounds.map_or_else(
+ || unit_sphere(&mission.mission_assets, object_index, pose),
+ |bounds| world_sphere(bounds, pose),
+ )
}
/// Targeter distance is the 3D separation of the two centers in world units.
@@ -138,3 +152,48 @@ fn targeter_distance_includes_height_and_rounds_to_even() {
assert_eq!(instrument_distance([0.0; 3], [0.0, 0.0, 4.5]), Some(4));
assert_eq!(instrument_distance([0.0; 3], [0.0; 3]), None);
}
+
+#[cfg(test)]
+#[test]
+#[ignore = "requires licensed corpus"]
+fn licensed_targeter_bounds_match_the_original_hero_and_bunker() {
+ use crate::{
+ create, load_gameplay_mission, starting_cockpit, EngineServices, MissionMode,
+ MissionRequest, MissionStartupInput,
+ };
+ use fparkan_vfs::DirectoryVfs;
+ use std::sync::Arc;
+
+ let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
+ let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
+ load_gameplay_mission(
+ &mut engine,
+ MissionRequest {
+ key: "MISSIONS/Single.01/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+
+ // Local bounds observed on the stock start, AniMesh+0xf3b0.
+ let hero = starting_cockpit(&engine).unwrap();
+ for (index, expected_center, expected_radius) in [
+ (
+ hero.object_index,
+ [-0.001875045, -0.17754595, 0.0161541],
+ 1.5871644,
+ ),
+ (0, [-0.00038418506, -0.036218945, 5.9308352], 54.111202),
+ ] {
+ let (center, radius) = instrument_bounds(&engine, index).unwrap();
+ for (actual, expected) in center.into_iter().zip(expected_center) {
+ assert!((actual - expected).abs() < 0.0001, "{center:?}");
+ }
+ assert!((radius - expected_radius).abs() < 0.0001, "{radius}");
+ }
+}
diff --git a/crates/fparkan-runtime/src/projectile_shield.rs b/crates/fparkan-runtime/src/projectile_shield.rs
index bb766fc5..5ff876a0 100644
--- a/crates/fparkan-runtime/src/projectile_shield.rs
+++ b/crates/fparkan-runtime/src/projectile_shield.rs
@@ -228,7 +228,7 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) -
remaining
}
-fn world_sphere(
+pub(super) fn world_sphere(
(center, radius): ([f32; 3], f32),
pose: TransformState,
) -> Option<([f32; 3], f32)> {
@@ -243,9 +243,9 @@ fn world_sphere(
))
}
-fn enclosing_sphere(parts: &[([f32; 3], f32)]) -> Option<([f32; 3], f32)> {
- // AniMesh+0x9510 weights model centers by the authored radii. A second
- // pass then puts even the most distant model sphere inside the result.
+pub(super) fn enclosing_sphere(parts: &[([f32; 3], f32)]) -> Option<([f32; 3], f32)> {
+ // AniMesh+0x9510 weights centers by the authored radii. A second pass
+ // puts even the most distant part inside the result.
let weight: f32 = parts.iter().map(|(_, radius)| radius).sum();
if weight <= 0.0 {
return None;
diff --git a/crates/fparkan-runtime/src/weapon_pose.rs b/crates/fparkan-runtime/src/weapon_pose.rs
index f01a93e1..97f239aa 100644
--- a/crates/fparkan-runtime/src/weapon_pose.rs
+++ b/crates/fparkan-runtime/src/weapon_pose.rs
@@ -10,7 +10,8 @@ use fparkan_assets::{
PreparedControlNodeBinding, PreparedControlPoint,
};
use fparkan_msh::{
- node38_fallback_hierarchy, node38_fallback_pose, node38_sampled_hierarchy_at_times, NodeId,
+ node38_fallback_hierarchy, node38_fallback_pose, node38_metadata, node38_sampled_hierarchy,
+ node38_sampled_hierarchy_at_times, selected_slot, Group, Lod, NodeId,
};
use fparkan_prototype::{unit_component_tree, PROTOTYPE_TYPE_EXTO};
use fparkan_render::LegacyIron3dEulerTransform;
@@ -251,6 +252,53 @@ impl WeaponPoses {
.collect()
}
+ pub(super) fn instrument_node_spheres(
+ &self,
+ assets: &MissionAssets,
+ index: usize,
+ ) -> Vec<([f32; 3], f32)> {
+ // AniMesh+0x9510 builds the filtered sphere in unit space at frame zero.
+ let models: Vec<_> = assets
+ .visuals_for_object(index)
+ .iter()
+ .map(|&id| assets.model_by_id(assets.visual_by_id(id)?.model_id?))
+ .collect();
+ let hierarchies: Vec<_> = models
+ .iter()
+ .map(|model| node38_sampled_hierarchy(&(*model)?.validated, 0))
+ .collect();
+ let mut parts = Vec::new();
+ for (component, model) in models.into_iter().enumerate() {
+ let Some(model) = model else { continue };
+ if matches!(self.mounts[component], Mount::Unavailable) {
+ continue;
+ }
+ let model = &model.validated;
+ // The attached model's root is replaced by the parent's socket.
+ let first_node = usize::from(component != 0);
+ for node in first_node..model.node_count {
+ let id = NodeId(node as u32);
+ if !node38_metadata(model, id).is_some_and(|meta| meta.header_flags & 1 == 0) {
+ continue;
+ }
+ let Some(slot) = selected_slot(model, id, Lod(0), Group(0)) else {
+ continue;
+ };
+ let slot = &model.slots[slot.0 as usize];
+ if let Some(center) = mounted_point(
+ &hierarchies,
+ &self.mounts,
+ component,
+ node,
+ slot.sphere_center,
+ ) {
+ parts.push((center, slot.sphere_radius));
+ }
+ }
+ }
+ parts
+ }
+
#[cfg(test)]
pub(super) fn has_aim(&self) -> bool {
self.aims.iter().any(Option::is_some)