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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
//! CPU poses for mounted weapon CPT points, independent of the renderer.
use crate::{
turret_aim::{apply_autonomous_turret_pose, has_turret_aim, TurretAim},
MissionAssets, MissionObjectDraft,
};
use fparkan_animation::{AnimationTime, NodePoseBuffer, Pose};
use fparkan_assets::{
sample_control_node_times, ControlInstanceState, PreparedControlGateProvider,
PreparedControlNodeBinding, PreparedControlPoint,
};
use fparkan_msh::{node38_fallback_hierarchy, node38_sampled_hierarchy_at_times};
use fparkan_prototype::{unit_component_tree, PROTOTYPE_TYPE_EXTO};
use fparkan_render::LegacyIron3dEulerTransform;
use fparkan_world::TransformState;
use std::collections::BTreeMap;
/// Unresolved autonomous weapon poses, grouped by mission object and component.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct WeaponPoseReport {
/// Samples using authored initial bindings because a live Control track is unsupported.
pub fallbacks: BTreeMap<(u32, usize), (u64, String)>,
/// Trigger samples or due shots without a resolvable CPT mount chain.
pub missing: BTreeMap<u32, u64>,
}
#[derive(Clone, Copy)]
enum Mount {
Root,
Socket(usize, usize),
Unavailable,
}
#[derive(Clone)]
pub(super) struct WeaponPoses {
mounts: Vec<Mount>,
states: Vec<ControlInstanceState>,
hierarchies: Vec<Option<NodePoseBuffer>>,
aims: Vec<Option<TurretAim>>,
pub(super) mounted_radius: f32,
}
impl WeaponPoses {
pub(super) fn new(draft: &MissionObjectDraft, assets: &MissionAssets, index: usize) -> Self {
let visuals = assets.visuals_for_object(index);
let tree = unit_component_tree(&draft.unit_components).ok();
let visible = |component| {
component == 0
|| visuals
.get(component)
.and_then(|&id| assets.visual_by_id(id))
.is_some_and(|visual| visual.source_type == Some(PROTOTYPE_TYPE_EXTO))
};
let mounts = (0..visuals.len())
.map(|component| {
if draft.unit_components.is_empty() || component == 0 {
return Mount::Root;
}
let socket = || {
let parent = tree.as_ref()?.get(component)?.parent_index?;
let socket =
usize::try_from(draft.unit_components[component].parent_or_link).ok()?;
(visible(component) && visible(parent)).then_some(Mount::Socket(parent, socket))
};
socket().unwrap_or(Mount::Unavailable)
})
.collect();
Self {
mounts,
states: vec![ControlInstanceState::default(); visuals.len()],
hierarchies: vec![None; visuals.len()],
aims: vec![None; visuals.len()],
mounted_radius: 0.0,
}
}
pub(super) fn sample(
&mut self,
assets: &MissionAssets,
index: usize,
object: u32,
now: u32,
hull_local_target: Option<[f32; 3]>,
report: &mut WeaponPoseReport,
) {
self.mounted_radius = 0.0;
for (component, &visual) in assets.visuals_for_object(index).iter().enumerate() {
let model = assets
.visual_by_id(visual)
.and_then(|visual| visual.model_id)
.and_then(|id| assets.model_by_id(id));
let Some(model) = model else { continue };
let control = assets
.object_component_controls
.get(index)
.and_then(|controls| controls.get(component))
.and_then(Option::as_ref);
let aim = control
.filter(|control| has_turret_aim(control))
.and_then(|_| self.aim_at(component, hull_local_target?));
self.aims[component] = aim;
let times = control.and_then(|control| {
// Match the renderer's query gates. Recoil still uses the
// initial Control providers.
let gates = control.typed_records.iter().filter(|record| record.tag == 21)
.map(|record| match record.gate_provider {
PreparedControlGateProvider::Resolved { initial_type1_value, .. }
if initial_type1_value.is_finite() => Some(initial_type1_value),
_ => None,
}).collect::<Vec<_>>();
let mut times = match sample_control_node_times(control, now, &gates, &mut self.states[component]) {
Ok(times) => times,
Err(error) => {
let (count, reason) = report.fallbacks.entry((object, component)).or_default();
if *count == 0 {
eprintln!("unit_weapon_pose_fallback object={object} component={component} reason={error:?}");
*reason = format!("{error:?}");
}
*count += 1;
initial_component_node_times(model.validated.node_count, &control.bindings).ok()?
.into_iter().map(|time| time.map(|time| time.0)).collect()
}
};
if let Some(aim) = aim {
apply_autonomous_turret_pose(control, aim, &mut times);
}
Some(times.into_iter().map(|time| time.map(AnimationTime)).collect())
});
let times = times.or_else(|| {
control
.is_none()
.then(|| vec![Some(AnimationTime(0.0)); model.validated.node_count])
});
self.hierarchies[component] = times
.as_ref()
.and_then(|times| node38_sampled_hierarchy_at_times(&model.validated, times))
.or_else(|| node38_fallback_hierarchy(&model.validated));
if let Some(center) =
self.model_point_on_hull(component, model.validated.bounding_sphere_center)
{
let distance = center[0].hypot(center[1]).hypot(center[2]);
self.mounted_radius = self
.mounted_radius
.max(distance + model.validated.bounding_sphere_radius);
}
}
}
pub(super) fn model_point_on_hull(
&self,
component: usize,
point: [f32; 3],
) -> Option<[f32; 3]> {
let Mount::Socket(parent, socket) = *self.mounts.get(component)? else {
return None;
};
mounted_point(&self.hierarchies, &self.mounts, parent, socket, point)
}
pub(super) fn model_hierarchy(&self, component: usize) -> Option<&NodePoseBuffer> {
self.hierarchies.get(component)?.as_ref()
}
fn aim_at(&self, component: usize, target: [f32; 3]) -> Option<TurretAim> {
let local = match *self.mounts.get(component)? {
Mount::Root => target,
Mount::Unavailable => return None,
Mount::Socket(parent, socket) => {
let to_hull =
|point| mounted_point(&self.hierarchies, &self.mounts, parent, socket, point);
let origin = to_hull([0.0; 3])?;
let project = |axis| {
let end = to_hull(axis)?;
Some(
(0..3)
.map(|i| (end[i] - origin[i]) * (target[i] - origin[i]))
.sum(),
)
};
[
project([1.0, 0.0, 0.0])?,
project([0.0, 1.0, 0.0])?,
project([0.0, 0.0, 1.0])?,
]
}
};
TurretAim::toward(local)
}
pub(super) fn aim(&self, component: usize) -> Option<TurretAim> {
self.aims.get(component).copied().flatten()
}
pub(super) fn initial_model_spheres(
&self,
assets: &MissionAssets,
index: usize,
) -> Vec<([f32; 3], f32)> {
let models: Vec<_> = assets
.visuals_for_object(index)
.iter()
.map(|&id| assets.model_by_id(assets.visual_by_id(id)?.model_id?))
.collect();
let controls = assets.object_component_controls.get(index);
let hierarchies: Vec<_> = models
.iter()
.enumerate()
.map(|(component, model)| {
let model = &model.as_ref()?.validated;
let bindings = controls
.and_then(|controls| controls.get(component)?.as_ref())
.map_or(&[][..], |control| control.bindings.as_slice());
initial_component_node_times(model.node_count, bindings)
.ok()
.and_then(|times| node38_sampled_hierarchy_at_times(model, ×))
.or_else(|| node38_fallback_hierarchy(model))
})
.collect();
models
.into_iter()
.zip(&self.mounts)
.filter_map(|(model, mount)| {
let model = &model?.validated;
let center = model.bounding_sphere_center;
let center = match *mount {
Mount::Root => center,
Mount::Socket(parent, socket) => {
mounted_point(&hierarchies, &self.mounts, parent, socket, center)?
}
Mount::Unavailable => return None,
};
Some((center, model.bounding_sphere_radius))
})
.collect()
}
#[cfg(test)]
pub(super) fn has_aim(&self) -> bool {
self.aims.iter().any(Option::is_some)
}
pub(super) fn muzzle(
&self,
state: TransformState,
component: usize,
barrel: &PreparedControlPoint,
) -> Option<[f32; 3]> {
let node = usize::try_from(barrel.raw_signed_field_04).ok()?;
let point = mounted_point(
&self.hierarchies,
&self.mounts,
component,
node,
barrel.position,
)?;
LegacyIron3dEulerTransform {
translation: state.position.map(f32::from_bits),
orientation_radians: state.orientation.map(f32::from_bits),
}
.try_transform_scaled_point(point, state.scale.map(f32::from_bits))
}
}
fn mounted_point(
hierarchies: &[Option<NodePoseBuffer>],
mounts: &[Mount],
mut component: usize,
mut node: usize,
mut point: [f32; 3],
) -> Option<[f32; 3]> {
// Components are a preorder tree, but bound traversal also rejects a bad cycle.
for _ in 0..mounts.len() {
let hierarchy = hierarchies.get(component)?.as_ref();
let pose = match hierarchy {
Some(hierarchy) => *hierarchy.poses.get(node)?,
None if node == 0 => Pose::default(),
None => return None,
};
point = pose.transform_point(point);
match *mounts.get(component)? {
Mount::Root => return Some(point),
Mount::Unavailable => return None,
Mount::Socket(parent, socket) => {
if let Some(root) = hierarchy.and_then(|hierarchy| hierarchy.poses.first()) {
let [x, y, z, w] = root.rotation;
let length_squared = x * x + y * y + z * z + w * w;
let rotation = if length_squared.is_finite() && length_squared > f32::EPSILON {
[
-x / length_squared,
-y / length_squared,
-z / length_squared,
w / length_squared,
]
} else {
Pose::default().rotation
};
point = Pose {
translation: [0.0; 3],
rotation,
}
.transform_point(std::array::from_fn(|axis| {
point[axis] - root.translation[axis]
}));
}
component = parent;
node = socket;
}
}
}
None
}
/// Samples the authored initial Control bindings in source order.
pub fn initial_component_node_times(
node_count: usize,
bindings: &[PreparedControlNodeBinding],
) -> Result<Vec<Option<AnimationTime>>, String> {
let mut node_times = vec![Some(AnimationTime(0.0)); node_count];
for binding in bindings {
// Disabled rows are filtered while loading CTLD, but preserve the
// native guard here in case a caller constructs MissionAssets itself.
if binding.flags & 4 != 0 {
continue;
}
if binding.node_index >= node_count {
return Err(format!(
"control binding node {} is outside model node count {node_count}",
binding.node_index
));
}
if !binding.frame_a.is_finite()
|| !binding.frame_b.is_finite()
|| !binding.initial_blend.is_finite()
|| binding.frame_a < 0.0
|| binding.frame_b < 0.0
{
return Err(format!(
"control binding for node {} contains an invalid frame or blend",
binding.node_index
));
}
let mut blend = binding.initial_blend;
if binding.flags & 1 != 0 {
if blend > 1.0 {
blend -= 1.0;
}
if blend < 0.0 {
blend += 1.0;
}
} else {
blend = blend.clamp(0.0, 1.0);
}
if binding.flags & 2 != 0 {
blend = 1.0 - blend;
}
let sample_time = (1.0 - blend) * binding.frame_a + blend * binding.frame_b;
if !sample_time.is_finite() || sample_time < 0.0 {
return Err(format!(
"control binding for node {} produced a negative or non-finite sample time unsupported by the static sampler",
binding.node_index
));
}
// Source order is significant: native initialization writes each
// enabled row in turn, so a later row for the same node wins.
node_times[binding.node_index] = Some(AnimationTime(sample_time));
}
Ok(node_times)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cpt_scale_and_world_transform_match_control_dll_query() {
// GOG Control.dll RVA 0x1b4f0, native execution with a supplied node matrix.
let poses = WeaponPoses {
mounted_radius: 0.0,
mounts: vec![Mount::Root],
states: vec![],
aims: vec![],
hierarchies: vec![Some(NodePoseBuffer {
poses: vec![Pose::default()],
})],
};
let state = TransformState {
position: [20.0_f32, 30.0, 40.0].map(f32::to_bits),
orientation: [0.0_f32, 0.0, std::f32::consts::FRAC_PI_2].map(f32::to_bits),
scale: [2.0_f32, 3.0, 4.0].map(f32::to_bits),
};
let barrel = PreparedControlPoint {
raw_flags: 0,
raw_signed_field_04: 0,
raw_signed_field_08: 0,
position: [1.0, 2.0, 3.0],
direction: [0.0, 1.0, 0.0],
name: [0; 32],
};
let actual = poses.muzzle(state, 0, &barrel).unwrap();
for (actual, expected) in actual.into_iter().zip([14.0, 32.0, 52.0]) {
assert!((actual - expected).abs() < 0.00001);
}
}
#[test]
fn mounted_muzzle_replaces_child_roots_and_follows_rotated_parent_socket() {
let quarter_turn = [
0.0,
0.0,
std::f32::consts::FRAC_1_SQRT_2,
std::f32::consts::FRAC_1_SQRT_2,
];
let translated = |translation| Pose {
translation,
..Pose::default()
};
let poses = [
Some(NodePoseBuffer {
poses: vec![
Pose::default(),
Pose {
translation: [10.0, 0.0, 0.0],
rotation: quarter_turn,
},
],
}),
Some(NodePoseBuffer {
poses: vec![
Pose {
translation: [100.0, 0.0, 0.0],
rotation: quarter_turn,
},
Pose {
translation: [96.0, 0.0, 0.0],
rotation: quarter_turn,
},
],
}),
Some(NodePoseBuffer {
poses: vec![translated([0.0, 200.0, 0.0]), translated([0.0, 200.0, 3.0])],
}),
];
let mounts = [Mount::Root, Mount::Socket(0, 1), Mount::Socket(1, 1)];
let point = mounted_point(&poses, &mounts, 2, 1, [0.0, 2.0, 0.0]).unwrap();
for (actual, expected) in point.into_iter().zip([4.0, 0.0, 3.0]) {
assert!((actual - expected).abs() < 0.00001, "{point:?}");
}
assert!(mounted_point(&poses, &mounts, 2, 9, [0.0; 3]).is_none());
assert!(mounted_point(
&poses,
&[Mount::Root, Mount::Unavailable, mounts[2]],
2,
1,
[0.0; 3]
)
.is_none());
}
}
|