aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-assets/src')
-rw-r--r--crates/fparkan-assets/src/behavior_profile_values.rs57
-rw-r--r--crates/fparkan-assets/src/lib.rs6
2 files changed, 52 insertions, 11 deletions
diff --git a/crates/fparkan-assets/src/behavior_profile_values.rs b/crates/fparkan-assets/src/behavior_profile_values.rs
index 9600b581..1d07ed91 100644
--- a/crates/fparkan-assets/src/behavior_profile_values.rs
+++ b/crates/fparkan-assets/src/behavior_profile_values.rs
@@ -24,14 +24,34 @@ pub fn decode_behavior_work_rates(
})
}
+/// Reads the applied Go coefficient, retaining the native default when absent.
+///
+/// # Errors
+/// Rejects malformed framing and a selected record that is not a float.
+pub fn decode_behavior_go_speed_percent(bytes: &[u8]) -> Result<u32, BehaviorProfileVarError> {
+ let [value] = decode_values(bytes, ["Go_SpeedPercent"])?;
+ Ok(value.unwrap_or(1.0_f32.to_bits()))
+}
+
pub(super) fn decode_pair(
bytes: &[u8],
names: [&'static str; 2],
) -> Result<[u32; 2], BehaviorProfileVarError> {
+ let [first, second] = decode_values(bytes, names)?;
+ Ok([
+ first.ok_or(BehaviorProfileVarError::MissingField { name: names[0] })?,
+ second.ok_or(BehaviorProfileVarError::MissingField { name: names[1] })?,
+ ])
+}
+
+fn decode_values<const N: usize>(
+ bytes: &[u8],
+ names: [&'static str; N],
+) -> Result<[Option<u32>; N], BehaviorProfileVarError> {
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 found = [None, None];
+ let mut found = [None; N];
for _ in 0..count {
let size = cursor.read_u32_le()?;
let type_index = cursor.read_u32_le()?;
@@ -61,19 +81,16 @@ pub(super) fn decode_pair(
cursor.read_exact(8)?;
}
cursor.require_eof()?;
- Ok([
- found[0].ok_or(BehaviorProfileVarError::MissingField { name: names[0] })?,
- found[1].ok_or(BehaviorProfileVarError::MissingField { name: names[1] })?,
- ])
+ Ok(found)
}
#[cfg(test)]
mod tests {
use super::*;
- #[test]
- fn work_rates_use_the_applied_second_blob() {
- let mut bytes = 2_u32.to_le_bytes().to_vec();
- for (name, first, second) in [("Use_Power", 7.0_f32, 3.0_f32), ("Use_Ore", 9.0, 1.0)] {
+
+ fn profile(fields: &[(&str, f32, f32)]) -> Vec<u8> {
+ let mut bytes = (fields.len() as u32).to_le_bytes().to_vec();
+ for &(name, first, second) in fields {
for word in [4_u32, 3, 0] {
bytes.extend(word.to_le_bytes());
}
@@ -86,6 +103,12 @@ mod tests {
bytes.extend(second.to_le_bytes());
bytes.extend([0; 8]);
}
+ bytes
+ }
+
+ #[test]
+ fn work_rates_use_the_applied_second_blob() {
+ let bytes = profile(&[("Use_Power", 7.0, 3.0), ("Use_Ore", 9.0, 1.0)]);
assert_eq!(
decode_behavior_work_rates(&bytes).unwrap(),
BehaviorWorkRates {
@@ -94,4 +117,20 @@ mod tests {
}
);
}
+
+ #[test]
+ fn go_speed_keeps_native_default_or_uses_the_first_rows_applied_value() {
+ assert_eq!(
+ decode_behavior_go_speed_percent(&profile(&[])),
+ Ok(1.0_f32.to_bits())
+ );
+ let bytes = profile(&[
+ ("Go_SpeedPercent", 0.8, 0.5),
+ ("Go_SpeedPercent", 1.0, 0.25),
+ ]);
+ assert_eq!(
+ decode_behavior_go_speed_percent(&bytes),
+ Ok(0.5_f32.to_bits())
+ );
+ }
}
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index f9dbebe4..016eec41 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -32,7 +32,9 @@ pub use fparkan_texm::{PageRect, RgbaImage};
use fparkan_vfs::DirectoryVfs;
mod behavior_profile_values;
mod chassis_profile;
-pub use behavior_profile_values::{decode_behavior_work_rates, BehaviorWorkRates};
+pub use behavior_profile_values::{
+ decode_behavior_go_speed_percent, decode_behavior_work_rates, BehaviorWorkRates,
+};
pub use chassis_profile::decode_chassis_can_fly;
mod research;
pub use research::{
@@ -88,7 +90,7 @@ pub struct BehaviorProfileRates {
pub enum BehaviorProfileVarError {
/// The binary record framing is malformed.
Decode(DecodeError),
- /// A bound for one of the two fields used here is not a 32-bit float.
+ /// A selected registered field is not a 32-bit float.
InvalidFloatField {
name: &'static str,
size: u32,