1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
//! 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<Item = &'a mut DamageStageRanges>,
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<Option<DynamicComponentGeometry>>],
) -> HashSet<usize> {
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<Option<DynamicComponentGeometry>>],
poses: &[Vec<Option<NodePoseBuffer>>],
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<Vec<usize>> {
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);
}
}
|