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/building_door.rs80
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/player_motion.rs53
3 files changed, 132 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/building_door.rs b/crates/fparkan-runtime/src/building_door.rs
new file mode 100644
index 00000000..0d9c88a1
--- /dev/null
+++ b/crates/fparkan-runtime/src/building_door.rs
@@ -0,0 +1,80 @@
+use fparkan_assets::PreparedComponentControl;
+use fparkan_msh::{selected_slot, Group, Lod, ModelAsset, NodeId};
+use fparkan_render::LegacyIron3dEulerTransform;
+
+/// Whether this controller has a door record with a supported node binding.
+pub fn has_proximity_doors(control: &PreparedComponentControl) -> bool {
+ door_bindings(control).next().is_some()
+}
+
+fn door_bindings(
+ control: &PreparedComponentControl,
+) -> impl Iterator<Item = &fparkan_assets::PreparedControlNodeBinding> {
+ control
+ .typed_records
+ .iter()
+ .filter(|record| record.tag == 12)
+ .filter_map(|record| {
+ let index = record.fixed_words[1] as usize;
+ control
+ .bindings
+ .iter()
+ .rev()
+ .find(|binding| binding.node_index == index)
+ .filter(|binding| binding.flags == 0)
+ })
+}
+
+/// Updates the authored door tracks next to the player.
+pub fn sample_proximity_doors(
+ control: &PreparedComponentControl,
+ model: &ModelAsset,
+ root: LegacyIron3dEulerTransform,
+ scale: [f32; 3],
+ player: [f32; 3],
+ delta_seconds: f32,
+ blends: &mut Vec<f32>,
+ times: &mut [Option<f32>],
+) {
+ // shortcut: use the closed model's door bounds and a local proximity trigger
+ // until the original building graph trigger and its timing are recovered.
+ let Some(closed) = fparkan_msh::node38_sampled_hierarchy(model, 0) else {
+ return;
+ };
+ let previous_len = blends.len();
+ blends.resize(model.node_count, 0.0);
+ for binding in door_bindings(control) {
+ let index = binding.node_index;
+ let node = NodeId(index as u32);
+ let Some(pose) = closed.poses.get(index) else {
+ continue;
+ };
+ let Some(slot) = selected_slot(model, node, Lod(0), Group(0)) else {
+ continue;
+ };
+ let slot = &model.slots[slot.0 as usize];
+ let center = std::array::from_fn(|axis| (slot.aabb_min[axis] + slot.aabb_max[axis]) * 0.5);
+ let Some(center) = root.try_transform_scaled_point(pose.transform_point(center), scale)
+ else {
+ continue;
+ };
+ let distance_squared: f32 = player
+ .into_iter()
+ .zip(center)
+ .map(|(p, c)| (p - c).powi(2))
+ .sum();
+ let blend = &mut blends[index];
+ if index >= previous_len {
+ *blend = binding.initial_blend.clamp(0.0, 1.0);
+ }
+ let direction = if distance_squared < 8.0_f32.powi(2) {
+ 1.0
+ } else {
+ -1.0
+ };
+ *blend = (*blend + direction * delta_seconds * 2.0).clamp(0.0, 1.0);
+ if let Some(time) = times.get_mut(index) {
+ *time = Some(binding.frame_a + (binding.frame_b - binding.frame_a) * *blend);
+ }
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index d4155228..716673e4 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -44,6 +44,7 @@ use std::sync::Arc;
mod autodemo;
mod autodemo_combat;
mod boarding;
+mod building_door;
mod mesh_collision;
mod mining;
mod mission_events;
@@ -60,6 +61,7 @@ pub use autodemo::{
};
pub use autodemo_combat::{AutoDemoCombatEvent, AutoDemoProjectileType};
pub use boarding::{board_friendly_warbot, disembark_warbot, player_vehicle};
+pub use building_door::{has_proximity_doors, sample_proximity_doors};
pub use fparkan_assets::MissionAssets;
pub use mesh_collision::{query_building_segment_world, BuildingMeshHit, BuildingMeshNode};
pub use mining::deploy_mine;
diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs
index 6cccc01e..c521b8b6 100644
--- a/crates/fparkan-runtime/src/player_motion.rs
+++ b/crates/fparkan-runtime/src/player_motion.rs
@@ -1,6 +1,10 @@
use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState};
use crate::TerrainWorld;
+// shortcut: bound stair steps and drops until the native hero collider is wired.
+const STEP_UP: f32 = 0.85;
+const STEP_DOWN: f32 = 2.0;
+
/// Player controls sampled once for a simulation step.
#[derive(Clone, Copy, Debug, Default)]
pub struct PlayerMotion {
@@ -22,6 +26,7 @@ pub struct PlayerMotionStep {
pub(super) pose: TransformState,
pub(super) pitch_bits: u32,
flying: bool,
+ terrain_height: Option<f32>,
/// Sweep segment when the step translates the hero.
pub sweep: Option<[[f32; 3]; 2]>,
}
@@ -34,8 +39,19 @@ impl PlayerMotionStep {
return None;
}
let [[_, _, start_z], [x, y, _]] = self.sweep?;
- // shortcut: fixed step and drop limits, until the hero collider is wired.
- Some([[x, y, start_z + 0.1], [x, y, start_z - 2.5]])
+ let feet_z = start_z - 0.5;
+ Some([[x, y, feet_z + STEP_UP], [x, y, feet_z - STEP_DOWN]])
+ }
+
+ /// Terrain can take over at the end of a floor within the same step limits.
+ #[must_use]
+ pub fn terrain_exit_floor(self) -> Option<(f32, [[f32; 3]; 2])> {
+ let [top, bottom] = self.floor_probe()?;
+ let [start, end] = self.sweep?;
+ let height = self.terrain_height?;
+ let [x, y, body_z] = start;
+ ((bottom[2]..=top[2]).contains(&height) && (end[2] - (height + 0.52)).abs() > 0.05)
+ .then_some((height, [[x, y, body_z - 0.45], [x, y, body_z - 0.60]]))
}
/// Places the player's feet just above a floor accepted by the scene query.
@@ -47,6 +63,18 @@ impl PlayerMotionStep {
}
self
}
+
+ /// Sweeps vertically before crossing a step up, and horizontally before a step down.
+ #[must_use]
+ pub fn floor_sweeps(self) -> Option<[[[f32; 3]; 2]; 2]> {
+ let [start, end] = self.sweep?;
+ let middle = if end[2] > start[2] {
+ [start[0], start[1], end[2]]
+ } else {
+ [end[0], end[1], start[2]]
+ };
+ Some([[start, middle], [middle, end]])
+ }
}
#[cfg(test)]
@@ -62,15 +90,31 @@ mod tests {
},
pitch_bits: 0,
flying: false,
+ terrain_height: Some(11.5),
sweep: Some([[3.0, 6.0, 12.5], [4.0, 6.0, 15.5]]),
};
assert_eq!(
step.floor_probe(),
- Some([[4.0, 6.0, 12.6], [4.0, 6.0, 10.0]])
+ Some([[4.0, 6.0, 12.85], [4.0, 6.0, 10.0]])
+ );
+ assert_eq!(
+ step.terrain_exit_floor().map(|(height, _)| height),
+ Some(11.5)
);
let lowered = step.with_building_floor(11.5);
assert_eq!(lowered.pose.position.map(f32::from_bits), [4.0, 6.0, 11.52]);
assert_eq!(lowered.sweep.unwrap()[1], [4.0, 6.0, 12.02]);
+ assert_eq!(
+ lowered.floor_sweeps().unwrap(),
+ [
+ [[3.0, 6.0, 12.5], [4.0, 6.0, 12.5]],
+ [[4.0, 6.0, 12.5], [4.0, 6.0, 12.02]],
+ ]
+ );
+ assert_eq!(
+ step.with_building_floor(12.5).floor_sweeps().unwrap()[0],
+ [[3.0, 6.0, 12.5], [3.0, 6.0, 13.02]]
+ );
}
}
@@ -188,6 +232,7 @@ fn plan_motion(
));
}
let mut sweep = None;
+ let mut terrain_height = None;
if flying && (xy != [from[0], from[1]] || climb != 0.0) {
let old_xy = [from[0], from[1]];
let floor = |xy| {
@@ -224,6 +269,7 @@ fn plan_motion(
.map_err(|err| EngineError::Movement(err.to_string()))?,
) {
let to = [xy[0], xy[1], new_ground + (from[2] - old_ground)];
+ terrain_height = Some(new_ground);
pose.position = to.map(f32::to_bits);
// shortcut: one sweep at body height until the physical collider
// and floor transitions are recovered.
@@ -240,6 +286,7 @@ fn plan_motion(
pose,
pitch_bits: pitch.to_bits(),
flying,
+ terrain_height,
sweep,
})
}