diff options
Diffstat (limited to 'crates/fparkan-runtime/src/projectile_capacity.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/projectile_capacity.rs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/projectile_capacity.rs b/crates/fparkan-runtime/src/projectile_capacity.rs new file mode 100644 index 00000000..6da04342 --- /dev/null +++ b/crates/fparkan-runtime/src/projectile_capacity.rs @@ -0,0 +1,88 @@ +//! Control selector 0x35 for the material and explosion of a fresh BULL object. + +use crate::{bounded_ascii_c_string, checked_control_sum, AssetPreparationError}; +use fparkan_assets::{ + decode_explosion_capacity, PreparedControlNodeDataRow, PreparedControlResourcePair, +}; +use fparkan_path::{normalize_relative, PathPolicy}; +use fparkan_resource::{resource_name, ResourceError, ResourceRepository}; + +pub(super) fn projectile_capacity_bits( + repository: &dyn ResourceRepository, + rows: &[PreparedControlNodeDataRow], +) -> Result<Option<u32>, AssetPreparationError> { + let mut total = 0.0; + for row in rows { + let Some(explosion) = explosion_capacity(repository, &row.effect)? else { + return Ok(None); + }; + let Some(next) = add_row(total, row.max_volume, explosion) else { + return Ok(None); + }; + total = next; + } + Ok(Some(total.to_bits())) +} + +fn add_row(total: f32, volume: f32, explosion: f32) -> Option<f32> { + // Control+0x13620 publishes each addition separately at single precision. + let volume_sum = checked_control_sum(2, "projectile NDP capacity", total, volume).ok()?; + checked_control_sum(2, "projectile EXPL capacity", volume_sum, explosion).ok() +} + +fn explosion_capacity( + repository: &dyn ResourceRepository, + pair: &PreparedControlResourcePair, +) -> Result<Option<f32>, AssetPreparationError> { + let (Some(archive), Some(name)) = ( + bounded_ascii_c_string(&pair.archive), + bounded_ascii_c_string(&pair.name), + ) else { + return Ok(None); + }; + if archive.is_empty() || name.is_empty() { + return Ok(Some(0.0)); + } + let Ok(path) = normalize_relative(archive.as_bytes(), PathPolicy::StrictLegacy) else { + return Ok(None); + }; + let resource_error = |source| AssetPreparationError::Resource { + context: format!("projectile explosion {archive}:{name}"), + source: Box::new(source), + }; + let archive_id = match repository.open_archive_unchanged(&path) { + Ok(id) => id, + Err(ResourceError::MissingArchive { .. }) => return Ok(Some(0.0)), + Err(source) => return Err(resource_error(source)), + }; + let Some(entry) = repository + .find(archive_id, &resource_name(name.as_bytes())) + .map_err(resource_error)? + else { + return Ok(Some(0.0)); + }; + if repository + .entry_info(entry) + .map_err(resource_error)? + .key + .type_id + != Some(u32::from_le_bytes(*b"EXPL")) + { + return Ok(None); + } + let payload = repository.read(entry).map_err(resource_error)?; + Ok(decode_explosion_capacity(payload.as_slice())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn includes_explosion_capacity_after_the_node_volume() { + assert_eq!(add_row(0.0, 1600.0, 300.0), Some(1900.0)); + assert_eq!(add_row(0.0, 899.0, 1.0), Some(900.0)); + assert_eq!(add_row(16_777_216.0, 1.0, -16_777_216.0), Some(0.0)); + assert_eq!(add_row(0.0, f32::from_bits(1), 0.0), None); + } +} |
