//! Hide exhausted chassis nodes without changing the uploaded mesh layout. use super::DynamicComponentGeometry; use fparkan_animation::NodePoseBuffer; use fparkan_msh::{draw_batches, selected_slot, Group, Lod, NodeId}; use fparkan_render_vulkan::{DamageStageRanges, VulkanSmokeRenderer, VulkanStaticMesh}; use fparkan_runtime::{ depleted_warbot_nodes, loaded_mission_control_device_tables, Engine, MissionControlDeviceTableState, MissionControlNodeProviderRow, MissionControlNodeProviderTableStatus, }; use std::collections::HashSet; pub(super) fn remap_building_ranges<'a>( stages: impl IntoIterator, remap: &[usize], ) -> Result<(), String> { for stage in stages { let indices = remap .get(stage.ranges.clone()) .ok_or_else(|| "building damage range remap is out of bounds".to_string())?; let Some(&start) = indices.first() else { continue; }; let ranges = start..start + indices.len(); if !indices.iter().copied().eq(ranges.clone()) { return Err("building damage ranges are no longer contiguous".to_string()); } stage.ranges = ranges; } Ok(()) } pub(super) fn building_provider_rows( tables: &[MissionControlDeviceTableState], object_index: usize, ) -> &[MissionControlNodeProviderRow] { tables .iter() .find(|table| { table .original_id .is_some_and(|id| id.0 as usize == object_index) }) .and_then(|table| match &table.node_provider_status { MissionControlNodeProviderTableStatus::Available(rows) => Some(rows.as_slice()), MissionControlNodeProviderTableStatus::Unavailable(_) => None, }) .unwrap_or_default() } pub(super) fn visible_node_stage( rows: &[MissionControlNodeProviderRow], component_index: usize, node: usize, ) -> u32 { rows.iter() .find(|row| row.unit_component_index == component_index && row.source_row_index == node) .and_then(|row| row.mesh_state) .map_or(0, |state| state.visible_stage) } pub(super) fn inactive_building_ranges( engine: &Engine, geometry: &[Vec>], ) -> HashSet { let mut hidden = HashSet::new(); let tables = loaded_mission_control_device_tables(engine).unwrap_or_default(); for (object_index, components) in geometry.iter().enumerate() { if !components .iter() .flatten() .any(|component| !world_damage_stages(component).is_empty()) { continue; } let rows = building_provider_rows(tables, object_index); for (component_index, component) in components.iter().enumerate() { let Some(component) = component else { continue; }; for variant in world_damage_stages(component) { let active = visible_node_stage(rows, component_index, variant.node); if u32::from(variant.stage) != active { hidden.extend(variant.ranges.clone()); } } } } hidden } fn world_damage_stages(component: &DynamicComponentGeometry) -> &[DamageStageRanges] { component .world_lods .as_ref() .filter(|lods| lods.include_damage) .map_or(component.damage_stages.as_slice(), |lods| { lods.stages.as_slice() }) } pub(super) fn apply( engine: &Engine, geometry: &[Vec>], poses: &[Vec>], mesh: &VulkanStaticMesh, renderer: &mut VulkanSmokeRenderer, fragment_is_visible: impl Fn(usize) -> bool, ) -> Result<(), String> { for (object, component_index, node) in depleted_warbot_nodes(engine) { let Some(component) = geometry .get(object) .and_then(|v| v.get(component_index)) .and_then(Option::as_ref) else { continue; }; if !poses .get(object) .and_then(|v| v.get(component_index)) .is_some_and(Option::is_some) { continue; } let ranges = node_draw_ranges(mesh, component, object, node).unwrap_or_default(); let lod_ranges = component .world_lods .iter() .flat_map(|lods| &lods.stages) .filter(|variant| variant.node == node) .flat_map(|variant| variant.ranges.clone()); for range in ranges .into_iter() .chain(lod_ranges) .filter(|&range| !fragment_is_visible(range)) { renderer .set_draw_range_index_count(range, 0) .map_err(|error| { format!("hide damaged warbot node {object}:{component_index}:{node}: {error}") })?; } } Ok(()) } pub(super) fn node_draw_ranges( mesh: &VulkanStaticMesh, component: &DynamicComponentGeometry, object: usize, node: usize, ) -> Option> { if component.model.node_stride != 38 || component.model.animation.is_none() { return None; } let owner = u32::try_from(object.checked_add(1)?).ok()?; let end = component.vertex_start.checked_add(component.vertex_count)?; let vertices = component.vertex_start..end; let mut ranges = mesh.draw_ranges.iter().enumerate().filter(|(_, range)| { range.owner_scope == owner && mesh .indices .get(range.first_index as usize) .is_some_and(|&vertex| vertices.contains(&(vertex as usize))) }); let model = &component.model; let mut matched = Vec::new(); for index in 0..model.node_count { let id = NodeId(u32::try_from(index).ok()?); let Some(slot) = selected_slot(model, id, Lod(0), Group(0)) else { continue; }; for batch in draw_batches(model, slot) .ok()? .iter() .filter(|batch| batch.index_count != 0) { let (range_index, range) = ranges.next()?; if range.index_count != u32::from(batch.index_count) || range.batch_flags != batch.batch_flags { return None; } if index == node { matched.push(range_index); } } } ranges.next().is_none().then_some(matched) } #[cfg(test)] mod tests { use super::*; #[test] fn building_stages_follow_world_ranges_when_sky_is_inserted() { let mut stages = [ DamageStageRanges { node: 1, stage: 0, ranges: 0..2, }, DamageStageRanges { node: 1, stage: 1, ranges: 2..3, }, ]; remap_building_ranges(&mut stages, &[1, 2, 3, 0]).expect("insert sky range"); assert_eq!(stages[0].ranges, 1..3); assert_eq!(stages[1].ranges, 3..4); } }