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
|
//! Destruction lifecycle and presentation events for damaged units.
use crate::{
autodemo::MOBILE_UNIT, ControlResourceAccessError, Engine, MissionControlDeviceTableState,
MissionControlNodeProviderRow, MissionControlNodeProviderTableStatus, ObjectHandle,
UNIT_DAT_CONTROL_CLASS_HIGH_BIT,
};
use fparkan_world::{identity_metadata, request_delete, transform_state};
pub(super) fn destroy(
engine: &mut Engine,
handle: ObjectHandle,
) -> Result<(), ControlResourceAccessError> {
let effect = pending_effect(engine, handle);
request_delete(&mut engine.world, handle).map_err(ControlResourceAccessError::World)?;
if let (Some(effect), Some(events)) = (effect, engine.destruction_effects.as_mut()) {
events.push(effect);
}
Ok(())
}
/// Geometry supplied by the destroyed object to its authored explosion.
pub enum DestroyedMesh {
/// Mounted models after the loss of the whole chassis.
Mounted {
/// Prepared actor index.
object_index: usize,
/// Component and node indices of pieces already separated before the chassis failed.
depleted_nodes: Vec<(usize, usize)>,
},
/// One separated node of a still live warbot.
ChassisNode {
/// Prepared actor index.
object_index: usize,
/// Unit DAT component containing the mesh.
component_index: usize,
/// Local node of that model.
node: usize,
/// World position of the hull at impact.
hull_origin: [f32; 3],
},
}
/// Position and attached EXPL resource of a destroyed chassis or building part.
pub struct UnitDestructionEffect {
/// Prepared mesh to move when the effect requests built-in fragments.
pub mesh: Option<DestroyedMesh>,
/// World-space origin of the effect.
pub position: [f32; 3],
/// Archive containing the explosion description.
pub archive: String,
/// Name of the explosion description.
pub name: String,
}
/// Collect lethal projectile events when a scene is presenting the mission.
pub fn collect_unit_destruction_effects(engine: &mut Engine) {
engine.destruction_effects.get_or_insert_with(Vec::new);
}
/// Takes each newly destroyed unit once, after both player and AI damage.
pub fn take_unit_destruction_effects(engine: &mut Engine) -> Vec<UnitDestructionEffect> {
engine
.destruction_effects
.as_mut()
.map(std::mem::take)
.unwrap_or_default()
}
/// Explosion descriptors for prepared chassis roots and damageable model parts.
pub fn unit_destruction_effect_sources(
engine: &Engine,
) -> impl Iterator<Item = (String, String)> + '_ {
engine.loaded.iter().flat_map(|mission| {
mission
.object_drafts
.iter()
.filter_map(|draft| {
let class = draft.unit_dat_header_flags?;
let id = draft.original_id?;
let table = mission
.control_device_tables
.iter()
.find(|table| table.original_id == Some(id))?;
let MissionControlNodeProviderTableStatus::Available(rows) =
&table.node_provider_status
else {
return None;
};
Some(if class & UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0 {
rows.as_slice()
} else if class == super::boarding::WARBOT_CLASS {
rows.as_slice()
} else if class & MOBILE_UNIT != 0 {
rows.get(..1).unwrap_or_default()
} else {
&[]
})
})
.flat_map(|rows| rows.iter().filter_map(node_effect))
})
}
pub(super) fn part_effect(
engine: &Engine,
row: &MissionControlNodeProviderRow,
contact: super::ProjectileContact,
) -> Option<UnitDestructionEffect> {
let (archive, name) = node_effect(row)?;
let mesh = match contact.node {
Some(super::ProjectileNode::Chassis(_) | super::ProjectileNode::Mounted(_))
if row.current_flags.is_some_and(|flags| flags & 2 == 0) =>
{
let id = identity_metadata(&engine.world, contact.handle)
.ok()?
.original_id?;
let hull = transform_state(&engine.world, contact.handle).ok()?;
Some(DestroyedMesh::ChassisNode {
object_index: id.0 as usize,
component_index: row.unit_component_index,
node: row.source_row_index,
hull_origin: hull.position.map(f32::from_bits),
})
}
_ => None,
};
Some(UnitDestructionEffect {
mesh,
position: contact.position,
archive,
name,
})
}
pub(super) fn pending_effect(
engine: &Engine,
handle: ObjectHandle,
) -> Option<UnitDestructionEffect> {
engine.destruction_effects.as_ref()?;
let mission = engine.loaded.as_ref()?;
let table = mission
.control_device_tables
.iter()
.find(|table| table.handle == handle)?;
let (archive, name) = root_effect(table)?;
let object_index = identity_metadata(&engine.world, handle)
.ok()
.and_then(|identity| identity.original_id)
.and_then(|id| {
mission
.object_drafts
.iter()
.position(|draft| draft.original_id == Some(id))
});
let position = transform_state(&engine.world, handle)
.ok()?
.position
.map(f32::from_bits);
Some(UnitDestructionEffect {
mesh: object_index.map(|object_index| {
let depleted_nodes = match &table.node_provider_status {
MissionControlNodeProviderTableStatus::Available(rows) => rows
.iter()
.filter(|row| {
row.unit_component_index != 0
&& row.current_flags.is_some_and(|flags| flags & 0x12 == 0x10)
})
.map(|row| (row.unit_component_index, row.source_row_index))
.collect(),
MissionControlNodeProviderTableStatus::Unavailable(_) => Vec::new(),
};
DestroyedMesh::Mounted {
object_index,
depleted_nodes,
}
}),
position,
archive,
name,
})
}
fn root_effect(table: &MissionControlDeviceTableState) -> Option<(String, String)> {
let row = crate::control_node_provider_row(table, 0).ok()?;
node_effect(row)
}
fn node_effect(row: &MissionControlNodeProviderRow) -> Option<(String, String)> {
if f32::from_bits(row.initial_amount_bits) <= 0.0 {
return None;
}
let effect = &row.source_row.effect;
let archive = crate::bounded_ascii_c_string(&effect.archive)?;
let name = crate::bounded_ascii_c_string(&effect.name)?;
(!archive.is_empty() && !name.is_empty()).then_some((archive, name))
}
|