aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-assets/src/chassis_profile.rs52
-rw-r--r--crates/fparkan-assets/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/boarding.rs9
-rw-r--r--crates/fparkan-runtime/src/player_motion.rs60
4 files changed, 98 insertions, 25 deletions
diff --git a/crates/fparkan-assets/src/chassis_profile.rs b/crates/fparkan-assets/src/chassis_profile.rs
new file mode 100644
index 00000000..dfdaa599
--- /dev/null
+++ b/crates/fparkan-assets/src/chassis_profile.rs
@@ -0,0 +1,52 @@
+use fparkan_binary::{checked_count_bytes, read_lp_bytes, Cursor, DecodeError};
+
+/// Reads the bound `CanFly` boolean from a native chassis VAR profile.
+///
+/// # Errors
+/// Returns an error if the profile framing is invalid.
+pub fn decode_chassis_can_fly(bytes: &[u8]) -> Result<Option<bool>, DecodeError> {
+ let mut cursor = Cursor::new(bytes);
+ let count = cursor.read_u32_le()?;
+ checked_count_bytes(u64::from(count), 28, cursor.remaining() as u64)?;
+ let mut can_fly = None;
+ for _ in 0..count {
+ let size = cursor.read_u32_le()?;
+ let kind = cursor.read_u32_le()?;
+ cursor.read_u32_le()?;
+ let name = read_lp_bytes(&mut cursor, 64 * 1024)?;
+ read_lp_bytes(&mut cursor, 64 * 1024)?;
+ cursor.read_exact(size as usize)?;
+ if can_fly.is_none()
+ && name.split(|byte| *byte == 0).next() == Some(b"CanFly")
+ && size == 4
+ && kind == 2
+ {
+ can_fly = Some(cursor.read_u32_le()? == 1);
+ } else {
+ cursor.read_exact(size as usize)?;
+ }
+ cursor.read_exact(8)?;
+ }
+ cursor.require_eof()?;
+ Ok(can_fly)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::decode_chassis_can_fly;
+
+ #[test]
+ fn uses_the_bound_boolean_instead_of_its_default() {
+ let mut profile = Vec::new();
+ for word in [1_u32, 4, 2, 0, 6] {
+ profile.extend(word.to_le_bytes());
+ }
+ profile.extend(b"CanFly");
+ for word in [0_u32, 0, 1, 0, 1] {
+ profile.extend(word.to_le_bytes());
+ }
+ assert_eq!(decode_chassis_can_fly(&profile), Ok(Some(true)));
+ profile.truncate(profile.len() - 1);
+ assert!(decode_chassis_can_fly(&profile).is_err());
+ }
+}
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index 6c32bf62..7159eab2 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -30,6 +30,8 @@ pub use fparkan_terrain_format::{BuildCategory, TerrainFormatError};
use fparkan_texm::{decode_mip_rgba8, decode_texm, TexmDocument, TexmError};
pub use fparkan_texm::{PageRect, RgbaImage};
use fparkan_vfs::DirectoryVfs;
+mod chassis_profile;
+pub use chassis_profile::decode_chassis_can_fly;
mod research;
pub use research::{
calc_full_research_cost, ResearchCandidate, ResearchCandidateSet, ResearchCost, ResearchError,
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs
index ec786a21..9a1479f8 100644
--- a/crates/fparkan-runtime/src/boarding.rs
+++ b/crates/fparkan-runtime/src/boarding.rs
@@ -13,6 +13,7 @@ pub(super) const WARBOT_CLASS: u32 = 0x0100_8000;
pub(super) struct Boarding {
pub(super) cockpit: StartingCockpit,
pub(super) hero: StartingCockpit,
+ pub(super) can_fly: bool,
}
/// The vehicle cockpit while the player is aboard.
@@ -83,6 +84,7 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
let Some((_, cockpit, position)) = candidate else {
return Ok(None);
};
+ let can_fly = crate::player_motion::warbot_can_fly(engine, cockpit.object_index);
set_transform(
&mut engine.world,
hero.handle,
@@ -92,7 +94,11 @@ pub fn board_friendly_warbot(engine: &mut Engine) -> Result<Option<StartingCockp
},
)?;
if let Some(mission) = &mut engine.loaded {
- mission.boarding = Some(Boarding { cockpit, hero });
+ mission.boarding = Some(Boarding {
+ cockpit,
+ hero,
+ can_fly,
+ });
mission.player_pitch_bits = 0;
}
Ok(Some(cockpit))
@@ -122,6 +128,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>,
let Some(Boarding {
cockpit: vehicle,
hero,
+ ..
}) = mission.boarding
else {
return Ok(None);
diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs
index 877063f3..37ccbf4e 100644
--- a/crates/fparkan-runtime/src/player_motion.rs
+++ b/crates/fparkan-runtime/src/player_motion.rs
@@ -1,5 +1,38 @@
use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState};
use crate::TerrainWorld;
+use fparkan_resource::{resource_name, CachedResourceRepository, ResourceRepository};
+
+pub(super) fn warbot_can_fly(engine: &crate::Engine, object_index: usize) -> bool {
+ let profile = || {
+ let draft = engine.loaded.as_ref()?.object_drafts.get(object_index)?;
+ let chassis = draft.unit_components.first()?;
+ let archive = crate::bounded_ascii_c_string(&chassis.archive_raw)?;
+ let registry_path = crate::normalize_engine_path("chassis archive", &archive).ok()?;
+ let resources = CachedResourceRepository::new(engine.services.vfs.as_ref()?.clone());
+ let registry = resources.open_archive(&registry_path).ok()?;
+ let name = crate::bounded_ascii_c_string(&chassis.resource_raw)?;
+ let entry = resources
+ .find(registry, &resource_name(name.as_bytes()))
+ .ok()??;
+ let references =
+ fparkan_prototype::decode_registry_entry(resources.read(entry).ok()?.as_slice())
+ .ok()?;
+ // AniMesh supplies the sixth resource pair to CreateBehaviour.
+ let profile = references.get(5)?;
+ let archive = crate::bounded_ascii_c_string(&profile.archive_raw)?;
+ let path = crate::normalize_engine_path("chassis profile archive", &archive).ok()?;
+ let name = crate::bounded_ascii_c_string(&profile.resource_raw)?;
+ let archive = resources.open_archive(&path).ok()?;
+ let entry = resources
+ .find(archive, &resource_name(name.as_bytes()))
+ .ok()??;
+ fparkan_assets::decode_chassis_can_fly(resources.read(entry).ok()?.as_slice()).ok()?
+ };
+ profile().unwrap_or_else(|| {
+ eprintln!("sim_coverage player_chassis_profile_stub=1 object={object_index}");
+ false
+ })
+}
// shortcut: bound stair steps and drops until the native hero collider is wired.
const STEP_UP: f32 = 0.85;
@@ -161,30 +194,9 @@ fn plan_player_steps(
) else {
return Ok(None);
};
- let flying = mission.boarding.is_some_and(|boarding| {
- boarding.cockpit.handle == cockpit.handle
- && mission
- .object_drafts
- .get(cockpit.object_index)
- .and_then(|draft| draft.unit_components.first())
- .is_some_and(|chassis| {
- let key = chassis.resource_raw.split(|&byte| byte == 0).next();
- // shortcut: use the stock flying chassis until Behavior's CanFly is wired.
- matches!(
- key,
- Some(
- b"R_T_02"
- | b"R_L_02"
- | b"R_L_05"
- | b"R_L_07"
- | b"R_M_02"
- | b"R_B_02"
- | b"R_B_07"
- | b"R_B_08"
- )
- )
- })
- });
+ let flying = mission
+ .boarding
+ .is_some_and(|boarding| boarding.cockpit.handle == cockpit.handle && boarding.can_fly);
if input.movement == [0.0; 2]
&& (!flying || input.climb == 0.0)
&& input.mouse_x == 0.0