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
|
//! Prepare and populate the shared projectile body and trail draw pools.
use super::{
append_static_preview_component, cross3, fixed_effect_field, normalize3, projectile_sound,
projectile_trail, shot_sound, static_preview_component_materials, EffectMaterials,
MaterialAnimationBinding, StaticPreviewScene, VulkanStaticMaterial,
};
use fparkan_assets::{
load_resource_entry_bytes_from_root, PreparedControlEffectRow, PreparedControlEffectSection,
};
use fparkan_fx::{ambient_sound::Op2SoundCommand, decode_fxid, FxDocument};
use fparkan_render::LegacyIron3dEulerTransform;
use fparkan_render_vulkan::{
project_msh_to_static_mesh_in_world_space_with_node_fallback_poses, VulkanStaticMesh,
VulkanStaticVertex,
};
use fparkan_runtime::{
autodemo_projectiles, autonomous_projectile_segments, load_player_projectile, Engine,
PlayerProjectile, ProjectileAssets,
};
use fparkan_world::ObjectHandle;
use std::{collections::HashMap, ops::Range, path::Path};
pub(super) struct ProjectileDraw {
pub(super) speed: f32,
pub(super) lifetime_ms: u32,
pub(super) volume: Option<f32>,
pub(super) sight_convergence: bool,
pub(super) launch_sounds: Vec<Op2SoundCommand>,
pub(super) engine_sounds: Vec<projectile_sound::EngineSound>,
pub(super) local_vertices: Vec<VulkanStaticVertex>,
pub(super) instances: Vec<ProjectileDrawSlot>,
}
pub(super) struct ProjectileDrawSlot {
pub(super) flight: Option<PlayerProjectile>,
pub(super) sound_born_ms: Option<u32>,
pub(super) shooter: Option<ObjectHandle>,
pub(super) vertex_start: usize,
pub(super) draw_ranges: Range<usize>,
pub(super) trails: Vec<projectile_trail::ProjectileTrail>,
pub(super) trail_segment: Option<[[f32; 3]; 2]>,
}
#[allow(clippy::too_many_arguments)]
pub(super) fn prepare_player_draws(
root: &Path,
engine: &Engine,
weapons_by_object: impl IntoIterator<Item = (usize, fparkan_runtime::PlayerWeapons)>,
first_pool: usize,
mesh: &mut VulkanStaticMesh,
materials: &mut Vec<VulkanStaticMaterial>,
animations: &mut Vec<MaterialAnimationBinding>,
effect_materials: &mut EffectMaterials,
) -> (
HashMap<usize, super::PlayerWeaponLayout>,
Vec<Option<ProjectileDraw>>,
) {
let mut pools = Vec::<(String, String, Vec<u32>)>::new();
let mut layouts = HashMap::new();
let mut projectile_draws = Vec::new();
for (index, weapons) in weapons_by_object {
let slots = weapons
.weapons()
.iter()
.map(|weapon| {
let pool = pools
.iter()
.position(|(archive, name, _)| {
archive.eq_ignore_ascii_case(&weapon.projectile_archive)
&& name.eq_ignore_ascii_case(&weapon.projectile)
})
.unwrap_or_else(|| {
pools.push((
weapon.projectile_archive.clone(),
weapon.projectile.clone(),
Vec::new(),
));
pools.len() - 1
});
pools[pool].2.push(weapon.interval_ms());
let sound = weapon
.effect
.as_ref()
.and_then(|(archive, name)| shot_sound::load(root, archive, name));
(first_pool + pool, sound)
})
.collect();
layouts.insert(index, super::PlayerWeaponLayout { weapons, slots });
}
for (archive, name, intervals) in pools {
let capacity = |lifetime: u32| {
intervals.iter().fold(0_u32, |total, &interval| {
total.saturating_add(lifetime.div_ceil(interval))
})
};
let draw = match prepare_projectile_draw_into(
root,
engine,
&archive,
&name,
capacity,
mesh,
materials,
animations,
effect_materials,
) {
Ok(draw @ Some(_)) => draw,
Ok(None) => {
eprintln!("sim_coverage player_projectile_visual_stub=1 projectile={name} (effect-only body)");
None
}
Err(error) => {
eprintln!(
"sim_coverage player_projectile_layout_stub=1 projectile={name} reason={error}"
);
None
}
};
projectile_draws.push(draw);
}
(layouts, projectile_draws)
}
pub(super) fn prepare_projectile_draw(
root: &Path,
engine: &Engine,
archive: &str,
name: &str,
capacity: impl FnOnce(u32) -> u32,
preview: &mut StaticPreviewScene,
trail_materials: &mut EffectMaterials,
) -> Result<Option<ProjectileDraw>, String> {
prepare_projectile_draw_into(
root,
engine,
archive,
name,
capacity,
&mut preview.mesh,
&mut preview.materials,
&mut preview.material_animations,
trail_materials,
)
}
#[allow(clippy::too_many_arguments)]
pub(super) fn prepare_projectile_draw_into(
root: &Path,
engine: &Engine,
archive: &str,
name: &str,
capacity: impl FnOnce(u32) -> u32,
mesh: &mut VulkanStaticMesh,
materials: &mut Vec<VulkanStaticMaterial>,
material_animations: &mut Vec<MaterialAnimationBinding>,
trail_materials: &mut EffectMaterials,
) -> Result<Option<ProjectileDraw>, String> {
let projectile = load_player_projectile(engine, archive, name)?;
let assets = &projectile.assets;
let [id] = assets.visuals_for_object(0) else {
return Err("projectile has no single visual".into());
};
let visual = assets
.visual_by_id(*id)
.ok_or("projectile visual unavailable")?;
let geometry = if let Some(model) = visual.model_id.and_then(|id| assets.model_by_id(id)) {
project_msh_to_static_mesh_in_world_space_with_node_fallback_poses(
&model.validated,
LegacyIron3dEulerTransform {
translation: [0.0; 3],
orientation_radians: [0.0; 3],
},
[1.0; 3],
)
.map_err(|error| error.to_string())?
} else {
VulkanStaticMesh {
vertices: Vec::new(),
indices: Vec::new(),
draw_ranges: Vec::new(),
}
};
let effects = initial_effects(root, &projectile, name);
let layers = projectile_trail::prepare(&effects, name, materials, trail_materials);
if geometry.draw_ranges.is_empty() && layers.is_empty() {
return Ok(None);
}
let selector_remap = static_preview_component_materials(
assets,
visual,
&geometry,
None,
materials,
material_animations,
)?;
let capacity = capacity(projectile.lifetime_ms).min(128);
let mut instances = Vec::new();
for _ in 0..capacity {
let vertex_start = mesh.vertices.len();
let range_start = mesh.draw_ranges.len();
append_static_preview_component(
mesh,
geometry.clone(),
&selector_remap,
Some(material_animations),
)?;
let draw_ranges = range_start..mesh.draw_ranges.len();
let trails = layers
.iter()
.map(|&layer| projectile_trail::ProjectileTrail::allocate(mesh, layer))
.collect::<Result<_, _>>()?;
instances.push(ProjectileDrawSlot {
flight: None,
sound_born_ms: None,
shooter: None,
vertex_start,
draw_ranges,
trails,
trail_segment: None,
});
}
Ok(Some(ProjectileDraw {
speed: projectile.speed,
lifetime_ms: projectile.lifetime_ms,
volume: projectile.volume,
sight_convergence: projectile.sight_convergence,
launch_sounds: shot_sound::projectile_launch(effects.iter().map(|effect| &effect.document)),
engine_sounds: projectile_sound::prepare(&projectile, &effects),
local_vertices: geometry.vertices,
instances,
}))
}
pub(super) struct ProjectileEffect<'a> {
pub(super) row: &'a PreparedControlEffectRow,
pub(super) resource: String,
pub(super) document: FxDocument,
}
pub(super) fn initial_effects<'a>(
root: &Path,
projectile: &'a ProjectileAssets,
name: &str,
) -> Vec<ProjectileEffect<'a>> {
let Some(control) = projectile.assets.object_component_controls[0][0].as_ref() else {
return Vec::new();
};
let PreparedControlEffectSection::Parsed(rows) = &control.effect_section else {
return Vec::new();
};
rows.iter()
.filter(|row| {
row.opcode == 4
&& Some(row.table_index) == control.initial_effect_group
&& row.raw_condition == [0; 3]
})
.filter_map(|row| {
let archive = fixed_effect_field(&row.resource.archive);
let fx = fixed_effect_field(&row.resource.name);
let effect = load_resource_entry_bytes_from_root(root, &archive, &fx)
.map_err(|error| error.to_string())
.and_then(|bytes| decode_fxid(bytes).map_err(|error| error.to_string()));
match effect {
Ok(document) => Some(ProjectileEffect {
row,
resource: format!("{archive}/{fx}"),
document,
}),
Err(_) => {
eprintln!("sim_coverage projectile_trail_resource_stub=1 projectile={name} effect={archive}/{fx}");
None
}
}
})
.collect()
}
pub(super) fn flight_basis(forward: [f32; 3]) -> [[f32; 3]; 3] {
let right = normalize3(cross3(forward, [0.0, 0.0, 1.0])).unwrap_or([1.0, 0.0, 0.0]);
[right, forward, cross3(right, forward)]
}
pub(super) fn sample_autonomous(draws: &mut [Option<ProjectileDraw>], engine: &Engine) {
for instance in draws
.iter_mut()
.flatten()
.flat_map(|draw| &mut draw.instances)
{
instance.flight = None;
instance.trail_segment = None;
}
for (slot, flight) in autodemo_projectiles(engine) {
if let Some(draw) = draws.get_mut(slot).and_then(Option::as_mut) {
if let Some(instance) = draw
.instances
.iter_mut()
.find(|instance| instance.flight.is_none())
{
instance.flight = Some(flight);
}
}
}
// Pack trails independently of bodies. A body can be removed on impact
// while its last visible segment still belongs to this frame.
for (slot, segment) in autonomous_projectile_segments(engine) {
if let Some(draw) = draws.get_mut(slot).and_then(Option::as_mut) {
if let Some(instance) = draw
.instances
.iter_mut()
.find(|instance| instance.trail_segment.is_none() && !instance.trails.is_empty())
{
instance.trail_segment = Some(segment);
}
}
}
}
|