aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/mesh_collision.rs
blob: 6e9ae2d5b4963ce812c50a68aa3d62956354b6c6 (plain) (blame)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
//! World-space segment queries over selected building MSH nodes and slots.

use fparkan_msh::{
    node38_metadata, query_slot_segment, ModelAsset, MshError, NodeId, SegmentQueryMasks, SlotId,
};
use fparkan_render::RawCameraTransform;

/// One selected MSH node/slot and its affine transform into world space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BuildingMeshNode {
    /// Model-local node id.
    pub node: NodeId,
    /// Currently selected slot for this node.
    pub slot: SlotId,
    /// Row-major affine transform from node-local space to world space.
    pub world_from_node: [f32; 16],
}

/// One accepted building mesh hit in world space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BuildingMeshHit {
    /// Node that supplied this hit.
    pub node: NodeId,
    /// Selected slot that supplied this hit.
    pub slot: SlotId,
    /// Hit position in world space.
    pub position: [f32; 3],
    /// Squared world distance from the original segment start.
    pub distance_squared: f32,
    /// Global Batch20 index.
    pub batch_index: usize,
    /// Global type-7 triangle descriptor index.
    pub triangle_descriptor_index: usize,
    /// Global Res3 position indices of the hit triangle.
    pub vertex_indices: [usize; 3],
}

const MAX_INTERSECTION_VISITS: usize = 200;

/// Queries a world-space segment against all supplied building nodes and slots.
///
/// Node flags use the native camera query's `0/0` masks. The initial batch
/// query excludes `0x0200` while keeping bit `0x0008` visible to the building
/// retry rule; triangle flags use `0/0x0024`. A nearest bit-8 batch hit restarts
/// the query from that world-space point toward the original endpoint, just as
/// the native building camera does. Each helper's Batch20 `local_batch_index`
/// selects the next Node38 root; that retry visits its eligible descendants
/// child-before-self and queries only nodes with supplied geometry bindings.
pub fn query_building_segment_world(
    model: &ModelAsset,
    nodes: &[BuildingMeshNode],
    world_segment: [[f32; 3]; 2],
) -> Result<Option<BuildingMeshHit>, MshError> {
    if !world_segment
        .iter()
        .flatten()
        .all(|value| value.is_finite())
    {
        return Err(MshError::InvalidGeometry(
            "building query segment contains a non-finite endpoint".to_string(),
        ));
    }

    let prepared_nodes = nodes
        .iter()
        .map(|node| {
            prepare_node(*node).map_err(|reason| {
                MshError::InvalidGeometry(format!(
                    "building node {} has an invalid world transform: {reason}",
                    node.node.0
                ))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    let original_start = world_segment[0];
    let original_end = world_segment[1];
    let mut segment_start = original_start;
    let mut linked_root = None;
    let mut intersection_visits = 0;
    loop {
        let mut nearest = None;
        let query_nodes = if let Some(root) = linked_root {
            let order = linked_node_postorder(model, root)?;
            let mut ordered = Vec::new();
            for node_id in order {
                ordered.extend(
                    prepared_nodes
                        .iter()
                        .filter(|node| node.source.node == node_id),
                );
            }
            ordered
        } else {
            prepared_nodes.iter().collect()
        };
        for node in query_nodes {
            let local_segment = [
                transform_point(node.node_from_world, segment_start)
                    .ok_or_else(|| invalid_transformed_point(node.source.node, "world start"))?,
                transform_point(node.node_from_world, original_end)
                    .ok_or_else(|| invalid_transformed_point(node.source.node, "world endpoint"))?,
            ];
            let hit = query_slot_segment(
                model,
                node.source.node,
                node.source.slot,
                local_segment,
                SegmentQueryMasks {
                    node_include: 0,
                    node_exclude: 0,
                    batch_include: 0,
                    // CBuilding clears bit 8 from the camera's 0x208 exclude
                    // mask so it can restart after inspecting the raw batch.
                    batch_exclude: 0x0200,
                    triangle_include: 0,
                    triangle_exclude: 0x0024,
                },
            )
            .map_err(|source| {
                MshError::InvalidGeometry(format!(
                    "building node {} slot {} segment query failed: {source}",
                    node.source.node.0, node.source.slot.0
                ))
            })?;
            let Some(hit) = hit else {
                continue;
            };
            let position = transform_point(node.source.world_from_node, hit.position)
                .ok_or_else(|| invalid_transformed_point(node.source.node, "local hit"))?;
            let offset = subtract3(position, original_start);
            let distance_squared = dot3(offset, offset);
            if !distance_squared.is_finite() {
                return Err(MshError::InvalidGeometry(format!(
                    "building node {} produced a non-finite world hit distance",
                    node.source.node.0
                )));
            }
            let batch_flags = model
                .batches
                .get(hit.batch_index)
                .ok_or_else(|| {
                    MshError::InvalidGeometry(format!(
                        "building node {} query returned missing batch {}",
                        node.source.node.0, hit.batch_index
                    ))
                })?
                .batch_flags;
            let candidate = BuildingMeshHit {
                node: node.source.node,
                slot: node.source.slot,
                position,
                distance_squared,
                batch_index: hit.batch_index,
                triangle_descriptor_index: hit.triangle_descriptor_index,
                vertex_indices: hit.vertex_indices,
            };
            if nearest.is_none_or(|(best, _): (BuildingMeshHit, u16)| {
                candidate.distance_squared <= best.distance_squared
            }) {
                nearest = Some((candidate, batch_flags));
            }
        }

        let Some((nearest, batch_flags)) = nearest else {
            return Ok(None);
        };
        intersection_visits += 1;
        if intersection_visits == MAX_INTERSECTION_VISITS {
            return Err(MshError::InvalidGeometry(format!(
                "building query exhausted the native {MAX_INTERSECTION_VISITS}-intersection visit limit"
            )));
        }
        if batch_flags & 0x0008 == 0 {
            return Ok(Some(nearest));
        }
        let batch = model.batches.get(nearest.batch_index).ok_or_else(|| {
            MshError::InvalidGeometry(format!(
                "building query returned missing batch {}",
                nearest.batch_index
            ))
        })?;
        if batch.local_batch_index == u16::MAX {
            return Err(MshError::InvalidGeometry(format!(
                "building helper batch {} has the sentinel linked-node key",
                nearest.batch_index
            )));
        }
        let linked_node = NodeId(u32::from(batch.local_batch_index));
        if usize::try_from(linked_node.0).unwrap_or(usize::MAX) >= model.node_count {
            return Err(MshError::InvalidGeometry(format!(
                "building helper batch {} has linked node {} outside Node38 count {}",
                nearest.batch_index, linked_node.0, model.node_count
            )));
        }
        segment_start = nearest.position;
        linked_root = Some(linked_node);
    }
}

/// Builds the native child-before-self query order rooted at a portal link.
///
/// Node metadata is traversed even when the caller has no selected geometry
/// binding for that node, because eligible descendants can still carry hits.
fn linked_node_postorder(model: &ModelAsset, root: NodeId) -> Result<Vec<NodeId>, MshError> {
    let root_index = usize::try_from(root.0).map_err(|_| {
        MshError::InvalidGeometry(format!("linked query node {} is not addressable", root.0))
    })?;
    if root_index >= model.node_count {
        return Err(MshError::InvalidGeometry(format!(
            "linked query node {} is outside Node38 count {}",
            root.0, model.node_count
        )));
    }

    let metadata = (0..model.node_count)
        .map(|index| {
            let node = NodeId(u32::try_from(index).map_err(|_| {
                MshError::InvalidGeometry(format!("Node38 index {index} is not addressable"))
            })?);
            node38_metadata(model, node).ok_or_else(|| {
                MshError::InvalidGeometry(format!(
                    "linked query requires Node38 metadata for node {index}"
                ))
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    for (index, entry) in metadata.iter().enumerate() {
        if entry.parent_or_link_raw != u16::MAX
            && usize::from(entry.parent_or_link_raw) >= model.node_count
        {
            return Err(MshError::InvalidGeometry(format!(
                "Node38 parent {} for node {index} is outside node count {}",
                entry.parent_or_link_raw, model.node_count
            )));
        }
    }

    // Each stack frame marks entry or postorder exit. This keeps traversal
    // bounded by the model table rather than by the native hierarchy depth.
    let mut stack = vec![(root_index, false)];
    let mut active = vec![false; model.node_count];
    let mut visited = vec![false; model.node_count];
    let mut ordered = Vec::new();
    while let Some((index, exiting)) = stack.pop() {
        if exiting {
            active[index] = false;
            visited[index] = true;
            ordered.push(NodeId(u32::try_from(index).map_err(|_| {
                MshError::InvalidGeometry(format!("Node38 index {index} is not addressable"))
            })?));
            continue;
        }
        if active[index] {
            return Err(MshError::InvalidGeometry(format!(
                "linked Node38 query contains a cycle at node {index}"
            )));
        }
        if visited[index] {
            continue;
        }

        active[index] = true;
        stack.push((index, true));
        if metadata[index].header_flags & 0x0010 == 0 {
            let parent_word = u16::try_from(index).map_err(|_| {
                MshError::InvalidGeometry(format!(
                    "Node38 index {index} cannot be represented by its parent field"
                ))
            })?;
            for child in (0..model.node_count).rev() {
                if metadata[child].parent_or_link_raw != u16::MAX
                    && metadata[child].parent_or_link_raw == parent_word
                    && metadata[child].header_flags & 0x0004 == 0
                {
                    stack.push((child, false));
                }
            }
        }
    }
    Ok(ordered)
}

#[derive(Clone, Copy)]
struct PreparedNode {
    source: BuildingMeshNode,
    node_from_world: [f32; 16],
}

fn prepare_node(node: BuildingMeshNode) -> Result<PreparedNode, &'static str> {
    if !node.world_from_node.iter().all(|value| value.is_finite()) {
        return Err("matrix contains a non-finite value");
    }
    if node.world_from_node[12].abs() > f32::EPSILON
        || node.world_from_node[13].abs() > f32::EPSILON
        || node.world_from_node[14].abs() > f32::EPSILON
        || (node.world_from_node[15] - 1.0).abs() > f32::EPSILON
    {
        return Err("matrix is not affine");
    }
    let node_from_world = RawCameraTransform {
        words: node.world_from_node.map(f32::to_bits),
    }
    .try_inverse_affine_row_major()
    .ok_or("matrix is singular or its inverse is not finite")?;
    if !node_from_world.iter().all(|value| value.is_finite()) {
        return Err("matrix inverse contains a non-finite value");
    }
    Ok(PreparedNode {
        source: node,
        node_from_world,
    })
}

fn invalid_transformed_point(node: NodeId, label: &str) -> MshError {
    MshError::InvalidGeometry(format!(
        "building node {} transformed {label} to a non-finite point",
        node.0
    ))
}

fn transform_point(matrix: [f32; 16], point: [f32; 3]) -> Option<[f32; 3]> {
    let transformed = [
        matrix[0] * point[0] + matrix[1] * point[1] + matrix[2] * point[2] + matrix[3],
        matrix[4] * point[0] + matrix[5] * point[1] + matrix[6] * point[2] + matrix[7],
        matrix[8] * point[0] + matrix[9] * point[1] + matrix[10] * point[2] + matrix[11],
    ];
    transformed
        .iter()
        .all(|value| value.is_finite())
        .then_some(transformed)
}

fn subtract3(left: [f32; 3], right: [f32; 3]) -> [f32; 3] {
    [left[0] - right[0], left[1] - right[1], left[2] - right[2]]
}

fn dot3(left: [f32; 3], right: [f32; 3]) -> f32 {
    left[0] * right[0] + left[1] * right[1] + left[2] * right[2]
}

#[cfg(test)]
mod tests {
    use super::{
        query_building_segment_world, BuildingMeshHit, BuildingMeshNode, MAX_INTERSECTION_VISITS,
    };
    use fparkan_msh::{
        query_slot_segment, Batch, ModelAsset, MshError, NodeId, SegmentQueryMasks, Slot, SlotId,
        TriangleDescriptor,
    };

    const IDENTITY: [f32; 16] = [
        1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
    ];
    const CAMERA_MASKS: SegmentQueryMasks = SegmentQueryMasks {
        node_include: 0,
        node_exclude: 0,
        batch_include: 0,
        batch_exclude: 0x0200,
        triangle_include: 0,
        triangle_exclude: 0x0024,
    };

    fn node(node: u32, world_from_node: [f32; 16]) -> BuildingMeshNode {
        node_with_slot(node, 0, world_from_node)
    }

    fn node_with_slot(node: u32, slot: u32, world_from_node: [f32; 16]) -> BuildingMeshNode {
        BuildingMeshNode {
            node: NodeId(node),
            slot: SlotId(slot),
            world_from_node,
        }
    }

    fn model_with_planes(planes: &[(f32, u16)]) -> ModelAsset {
        model_with_slots(&[planes])
    }

    fn model_with_slots(planes_per_slot: &[&[(f32, u16)]]) -> ModelAsset {
        model_with_slots_and_nodes(planes_per_slot, 2)
    }

    fn model_with_slots_and_nodes(
        planes_per_slot: &[&[(f32, u16)]],
        node_count: usize,
    ) -> ModelAsset {
        let mut positions = Vec::new();
        let mut indices = Vec::new();
        let mut batches = Vec::new();
        let mut triangle_descriptors = Vec::new();
        let mut slots = Vec::new();
        for planes in planes_per_slot {
            let descriptor_start = triangle_descriptors.len();
            let batch_start = batches.len();
            let min_y = planes.iter().map(|(y, _)| *y).fold(0.0, f32::min);
            let max_y = planes.iter().map(|(y, _)| *y).fold(0.0, f32::max);
            let center_y = (min_y + max_y) * 0.5;
            let half_height = (max_y - min_y) * 0.5;
            for (y, batch_flags) in *planes {
                let base_vertex = positions.len();
                let index_start = indices.len();
                positions.extend([[-20.0, *y, -20.0], [0.0, *y, 20.0], [20.0, *y, -20.0]]);
                indices.extend([0, 1, 2]);
                batches.push(Batch {
                    batch_flags: *batch_flags,
                    material_index_hi: 0,
                    material_index: 0,
                    lightmap_index: 0,
                    local_batch_index: u16::MAX,
                    index_count: 3,
                    index_start: u32::try_from(index_start).expect("small synthetic index offset"),
                    vertex_count: 3,
                    base_vertex: u32::try_from(base_vertex).expect("small synthetic vertex offset"),
                });
                triangle_descriptors.push(TriangleDescriptor {
                    tri_flags: 0,
                    opaque: [0; 4],
                    normal: [0, 32767, 0],
                });
            }
            slots.push(Slot {
                tri_start: u16::try_from(descriptor_start)
                    .expect("small synthetic descriptor start"),
                tri_count: u16::try_from(triangle_descriptors.len() - descriptor_start)
                    .expect("small synthetic descriptor count"),
                batch_start: u16::try_from(batch_start).expect("small synthetic batch start"),
                batch_count: u16::try_from(batches.len() - batch_start)
                    .expect("small synthetic batch count"),
                aabb_min: [-20.0, min_y, -20.0],
                aabb_max: [20.0, max_y, 20.0],
                sphere_center: [0.0, center_y, 0.0],
                sphere_radius: (20.0_f32.powi(2) * 2.0 + half_height.powi(2)).sqrt(),
                opaque: [0; 5],
            });
        }

        ModelAsset {
            node_stride: 38,
            node_count,
            nodes_raw: vec![0; node_count * 38],
            type16_present: false,
            bounding_sphere_center: [0.0; 3],
            bounding_sphere_radius: 0.0,
            slots,
            positions,
            normals: None,
            uv0: None,
            uv1: None,
            indices,
            batches,
            triangle_descriptors: Some(triangle_descriptors),
            node_names: None,
            animation: None,
        }
    }

    fn set_node_metadata(model: &mut ModelAsset, node: usize, flags: u16, parent: u16) {
        let offset = node * model.node_stride;
        model.nodes_raw[offset..offset + 2].copy_from_slice(&flags.to_le_bytes());
        model.nodes_raw[offset + 2..offset + 4].copy_from_slice(&parent.to_le_bytes());
    }

    fn set_batch_link(model: &mut ModelAsset, batch: usize, node: u16) {
        model.batches[batch].local_batch_index = node;
    }

    fn set_root_node_metadata(model: &mut ModelAsset) {
        for node in 0..model.node_count {
            set_node_metadata(model, node, 0, u16::MAX);
        }
    }

    fn translated(x: f32, y: f32, z: f32) -> [f32; 16] {
        let mut matrix = IDENTITY;
        matrix[3] = x;
        matrix[7] = y;
        matrix[11] = z;
        matrix
    }

    fn hit(result: Option<BuildingMeshHit>) -> BuildingMeshHit {
        result.expect("segment should hit the synthetic plane")
    }

    fn assert_point(actual: [f32; 3], expected: [f32; 3]) {
        for axis in 0..3 {
            assert!(
                (actual[axis] - expected[axis]).abs() < 1.0e-4,
                "{actual:?} != {expected:?}"
            );
        }
    }

    #[test]
    fn translation_returns_a_world_space_hit() {
        let result = query_building_segment_world(
            &model_with_planes(&[(0.0, 0)]),
            &[node(0, translated(4.0, 5.0, -3.0))],
            [[4.0, 8.0, -3.0], [4.0, 2.0, -3.0]],
        )
        .expect("valid world query");

        let hit = hit(result);
        assert_point(hit.position, [4.0, 5.0, -3.0]);
        assert_eq!(hit.distance_squared, 9.0);
    }

    #[test]
    fn rotation_transforms_the_world_segment_into_node_space() {
        let rotated = [
            0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        ];
        let result = query_building_segment_world(
            &model_with_planes(&[(0.0, 0)]),
            &[node(0, rotated)],
            [[-3.0, 0.0, 0.0], [3.0, 0.0, 0.0]],
        )
        .expect("valid world query");

        assert_point(hit(result).position, [0.0, 0.0, 0.0]);
    }

    #[test]
    fn nonuniform_scale_preserves_world_hit_coordinates() {
        let scaled = [
            2.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0,
        ];
        let result = query_building_segment_world(
            &model_with_planes(&[(0.0, 0)]),
            &[node(0, scaled)],
            [[0.0, 2.0, 0.25], [0.0, -2.0, 0.25]],
        )
        .expect("valid world query");

        let hit = hit(result);
        assert_point(hit.position, [0.0, 0.0, 0.25]);
        assert_eq!(hit.distance_squared, 4.0);
    }

    #[test]
    fn mirrored_transform_uses_the_mirrored_world_side() {
        let mirrored = [
            1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        ];
        let result = query_building_segment_world(
            &model_with_planes(&[(0.0, 0)]),
            &[node(0, mirrored)],
            [[0.0, -3.0, 0.0], [0.0, 3.0, 0.0]],
        )
        .expect("valid mirrored world query");

        assert_point(hit(result).position, [0.0, 0.0, 0.0]);
    }

    #[test]
    fn nearest_hit_is_ranked_in_world_space_from_original_start() {
        let mut farther = translated(0.0, 0.0, 0.0);
        let mut nearer = translated(0.0, 2.0, 0.0);
        // Make local distances deliberately incomparable across nodes.
        farther[5] = 2.0;
        nearer[5] = 0.5;
        let result = query_building_segment_world(
            &model_with_planes(&[(0.0, 0)]),
            &[node(0, farther), node(1, nearer)],
            [[0.0, 5.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid world query");

        let hit = hit(result);
        assert_eq!(hit.node, NodeId(1));
        assert_point(hit.position, [0.0, 2.0, 0.0]);
        assert_eq!(hit.distance_squared, 9.0);
    }

    #[test]
    fn retries_from_bit8_helper_to_the_farther_wall() {
        let helper_plane = [(2.0, 0x0008)];
        let wall_plane = [(0.0, 0)];
        let mut model = model_with_slots(&[&helper_plane, &wall_plane]);
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 0, 1);
        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(0, 0, IDENTITY),
                node_with_slot(1, 1, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid world query");

        let hit = hit(result);
        assert_eq!(hit.node, NodeId(1));
        assert_eq!(hit.slot, SlotId(1));
        assert_eq!(hit.batch_index, 1);
        assert_point(hit.position, [0.0, 0.0, 0.0]);
        assert_eq!(hit.distance_squared, 9.0);
    }

    #[test]
    fn coplanar_helper_hit_is_requeried_instead_of_filtered_out() {
        let mut model = model_with_slots(&[&[(0.0, 0)], &[(0.0, 0x0008)]]);
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 1, 1);
        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(1, 0, IDENTITY),
                node_with_slot(0, 1, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid world query");

        assert!(result.is_none());
    }

    #[test]
    fn world_roundtrip_helper_cannot_be_reselected_after_portal_link() {
        const START: [f32; 3] = [301.39682, 806.24396, 2.868942];
        const END: [f32; 3] = [304.3922, 806.4102, 2.868942];
        let world_from_node = [
            f32::from_bits(0xbd62_f745),
            f32::from_bits(0xbf7f_9b4f),
            0.0,
            f32::from_bits(0x4379_43ca),
            f32::from_bits(0x3f7f_9b4f),
            f32::from_bits(0xbd62_f745),
            0.0,
            f32::from_bits(0x4448_b667),
            0.0,
            0.0,
            1.0,
            f32::from_bits(0x4110_2494),
            0.0,
            0.0,
            0.0,
            1.0,
        ];
        let mut model = model_with_slots_and_nodes(&[&[(-54.239994, 0x0008)]], 4);
        set_batch_link(&mut model, 0, 2);
        set_node_metadata(&mut model, 0, 0, u16::MAX);
        set_node_metadata(&mut model, 1, 0x0104, 0);
        set_node_metadata(&mut model, 2, 0x0004, 1);
        set_node_metadata(&mut model, 3, 0x00d1, 2);
        let source = node(1, world_from_node);

        // Reproduce the old all-node retry: the world-space first hit maps
        // back to a local start just above this same helper plane.
        let prepared = super::prepare_node(source).expect("invert actual AutoDemo matrix");
        let local_start =
            super::transform_point(prepared.node_from_world, START).expect("finite local start");
        let local_end =
            super::transform_point(prepared.node_from_world, END).expect("finite local endpoint");
        let first_hit = query_slot_segment(
            &model,
            NodeId(1),
            SlotId(0),
            [local_start, local_end],
            CAMERA_MASKS,
        )
        .expect("valid first helper query")
        .expect("first helper crossing");
        let world_hit =
            super::transform_point(world_from_node, first_hit.position).expect("finite world hit");
        let repeated_local_start = super::transform_point(prepared.node_from_world, world_hit)
            .expect("finite round-tripped start");
        assert!(query_slot_segment(
            &model,
            NodeId(1),
            SlotId(0),
            [repeated_local_start, local_end],
            CAMERA_MASKS,
        )
        .expect("valid repeated helper query")
        .is_some());

        let result = query_building_segment_world(&model, &[source], [START, END])
            .expect("valid portal-linked world query");
        assert!(result.is_none());
    }

    #[test]
    fn linked_subtree_finds_drawable_child_through_nondrawable_parent() {
        let mut model = model_with_slots_and_nodes(&[&[(2.0, 0x0008)], &[(0.0, 0)]], 4);
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 0, 2);
        set_node_metadata(&mut model, 1, 0x0104, 0);
        set_node_metadata(&mut model, 2, 0x0004, 1);
        set_node_metadata(&mut model, 3, 0x00d1, 2);

        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(1, 0, IDENTITY),
                node_with_slot(3, 1, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid linked-child query");

        let hit = hit(result);
        assert_eq!(hit.node, NodeId(3));
        assert_eq!(hit.slot, SlotId(1));
        assert_eq!(hit.batch_index, 1);
        assert_point(hit.position, [0.0, 0.0, 0.0]);
    }

    #[test]
    fn linked_query_is_child_before_self_and_self_bit4_does_not_skip_geometry() {
        let mut model =
            model_with_slots_and_nodes(&[&[(2.0, 0x0008)], &[(0.0, 0)], &[(0.0, 0)]], 4);
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 0, 2);
        set_node_metadata(&mut model, 1, 0x0104, 0);
        set_node_metadata(&mut model, 2, 0x0004, 1);
        set_node_metadata(&mut model, 3, 0x00d1, 2);

        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(1, 0, IDENTITY),
                node_with_slot(2, 1, IDENTITY),
                node_with_slot(3, 2, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid child-first linked query");

        let hit = hit(result);
        // The equal-distance later candidate replaces the child result.
        assert_eq!(hit.node, NodeId(2));
        assert_eq!(hit.slot, SlotId(1));
    }

    #[test]
    fn linked_query_traverses_nondrawable_nodes_and_skips_child_bit4_branches() {
        let mut model = model_with_slots_and_nodes(
            &[&[(2.0, 0x0008)], &[(0.0, 0)], &[(0.0, 0)], &[(0.0, 0)]],
            7,
        );
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 0, 2);
        set_node_metadata(&mut model, 1, 0x0104, 0);
        set_node_metadata(&mut model, 2, 0x0004, 1);
        set_node_metadata(&mut model, 3, 0x0000, 2);
        set_node_metadata(&mut model, 4, 0x0010, 3);
        set_node_metadata(&mut model, 5, 0x0004, 3);
        set_node_metadata(&mut model, 6, 0x0004, 2);

        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(1, 0, IDENTITY),
                node_with_slot(4, 1, IDENTITY),
                node_with_slot(5, 2, IDENTITY),
                node_with_slot(6, 3, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid linked branch query");

        let hit = hit(result);
        assert_eq!(hit.node, NodeId(4));
        assert_eq!(hit.slot, SlotId(1));
    }

    #[test]
    fn linked_query_keeps_native_batch_and_triangle_exclude_masks() {
        let mut model =
            model_with_slots_and_nodes(&[&[(2.0, 0x0008)], &[(0.0, 0)], &[(0.0, 0x0200)]], 5);
        set_root_node_metadata(&mut model);
        set_batch_link(&mut model, 0, 2);
        set_node_metadata(&mut model, 1, 0x0104, 0);
        set_node_metadata(&mut model, 2, 0x0004, 1);
        set_node_metadata(&mut model, 3, 0x00d1, 2);
        set_node_metadata(&mut model, 4, 0x00d1, 2);
        model
            .triangle_descriptors
            .as_mut()
            .expect("synthetic type-7 table")[1]
            .tri_flags = 0x0024;

        let result = query_building_segment_world(
            &model,
            &[
                node_with_slot(1, 0, IDENTITY),
                node_with_slot(3, 1, IDENTITY),
                node_with_slot(4, 2, IDENTITY),
            ],
            [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("valid query with native exclusion masks");

        assert!(result.is_none());
    }

    #[test]
    fn linked_order_stops_descent_at_header_bit10_but_keeps_self() {
        let mut model = model_with_slots_and_nodes(&[&[]], 5);
        set_root_node_metadata(&mut model);
        set_node_metadata(&mut model, 1, 0, 0);
        set_node_metadata(&mut model, 2, 0x0014, 1);
        set_node_metadata(&mut model, 3, 0, 2);
        set_node_metadata(&mut model, 4, 0, 3);

        let order = super::linked_node_postorder(&model, NodeId(2)).expect("valid linked tree");

        assert_eq!(order, [NodeId(2)]);
    }

    #[test]
    fn invalid_portal_link_and_linked_cycle_are_explicit_errors() {
        let mut invalid_link = model_with_planes(&[(2.0, 0x0008)]);
        set_root_node_metadata(&mut invalid_link);
        set_batch_link(&mut invalid_link, 0, 2);
        assert!(matches!(
            query_building_segment_world(
                &invalid_link,
                &[node(0, IDENTITY)],
                [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
            ),
            Err(MshError::InvalidGeometry(message)) if message.contains("linked")
        ));

        let mut cycle = model_with_slots_and_nodes(&[&[(2.0, 0x0008)]], 3);
        set_batch_link(&mut cycle, 0, 1);
        set_node_metadata(&mut cycle, 0, 0, u16::MAX);
        set_node_metadata(&mut cycle, 1, 0, 2);
        set_node_metadata(&mut cycle, 2, 0, 1);
        assert!(matches!(
            query_building_segment_world(
                &cycle,
                &[node(0, IDENTITY)],
                [[0.0, 3.0, 0.0], [0.0, -1.0, 0.0]],
            ),
            Err(MshError::InvalidGeometry(message)) if message.contains("cycle")
        ));
    }

    #[test]
    fn invalid_non_affine_singular_and_nonfinite_transforms_are_explicit_errors() {
        let mut non_affine = IDENTITY;
        non_affine[12] = 0.5;
        assert!(matches!(
            query_building_segment_world(
                &model_with_planes(&[(0.0, 0)]),
                &[node(0, non_affine)],
                [[0.0, 1.0, 0.0], [0.0, -1.0, 0.0]],
            ),
            Err(MshError::InvalidGeometry(message)) if message.contains("not affine")
        ));

        let mut singular = IDENTITY;
        singular[5] = 0.0;
        assert!(matches!(
            query_building_segment_world(
                &model_with_planes(&[(0.0, 0)]),
                &[node(0, singular)],
                [[0.0, 1.0, 0.0], [0.0, -1.0, 0.0]],
            ),
            Err(MshError::InvalidGeometry(message)) if message.contains("singular")
        ));

        let mut nonfinite = IDENTITY;
        nonfinite[0] = f32::NAN;
        assert!(matches!(
            query_building_segment_world(
                &model_with_planes(&[(0.0, 0)]),
                &[node(0, nonfinite)],
                [[0.0, 1.0, 0.0], [0.0, -1.0, 0.0]],
            ),
            Err(MshError::InvalidGeometry(message)) if message.contains("non-finite")
        ));
    }

    #[test]
    fn wall_after_198_helpers_is_accepted_as_the_199th_hit() {
        let helper_count = MAX_INTERSECTION_VISITS - 2;
        let mut planes = (0..helper_count)
            .map(|index| ((helper_count - index) as f32, 0x0008))
            .collect::<Vec<_>>();
        planes.push((0.0, 0));
        let mut model = model_with_planes(&planes);
        set_root_node_metadata(&mut model);
        for batch in 0..helper_count {
            set_batch_link(&mut model, batch, 0);
        }
        let result = query_building_segment_world(
            &model,
            &[node(0, IDENTITY)],
            [[0.0, (helper_count + 1) as f32, 0.0], [0.0, -1.0, 0.0]],
        )
        .expect("199th intersection is below the native limit");

        let hit = hit(result);
        assert_eq!(hit.batch_index, helper_count);
        assert_point(hit.position, [0.0, 0.0, 0.0]);
    }

    #[test]
    fn wall_after_199_helpers_errors_as_the_200th_hit() {
        let helper_count = MAX_INTERSECTION_VISITS - 1;
        let mut planes = (0..helper_count)
            .map(|index| ((helper_count - index) as f32, 0x0008))
            .collect::<Vec<_>>();
        planes.push((0.0, 0));
        let mut model = model_with_planes(&planes);
        set_root_node_metadata(&mut model);
        for batch in 0..helper_count {
            set_batch_link(&mut model, batch, 0);
        }
        let result = query_building_segment_world(
            &model,
            &[node(0, IDENTITY)],
            [[0.0, (helper_count + 1) as f32, 0.0], [0.0, -1.0, 0.0]],
        );

        assert!(matches!(
            result,
            Err(MshError::InvalidGeometry(message)) if message.contains("200")
        ));
    }

    fn compose_row_major(left: [f32; 16], right: [f32; 16]) -> [f32; 16] {
        let mut result = [0.0; 16];
        for row in 0..4 {
            for column in 0..4 {
                for inner in 0..4 {
                    result[row * 4 + column] += left[row * 4 + inner] * right[inner * 4 + column];
                }
            }
        }
        result
    }

    fn pose_matrix(pose: fparkan_animation::Pose) -> [f32; 16] {
        let x = rotate_unit_vector([1.0, 0.0, 0.0], pose.rotation);
        let y = rotate_unit_vector([0.0, 1.0, 0.0], pose.rotation);
        let z = rotate_unit_vector([0.0, 0.0, 1.0], pose.rotation);
        [
            x[0],
            y[0],
            z[0],
            pose.translation[0],
            x[1],
            y[1],
            z[1],
            pose.translation[1],
            x[2],
            y[2],
            z[2],
            pose.translation[2],
            0.0,
            0.0,
            0.0,
            1.0,
        ]
    }

    fn rotate_unit_vector(value: [f32; 3], rotation: [f32; 4]) -> [f32; 3] {
        let [x, y, z, w] = rotation;
        let tx = 2.0 * (y * value[2] - z * value[1]);
        let ty = 2.0 * (z * value[0] - x * value[2]);
        let tz = 2.0 * (x * value[1] - y * value[0]);
        [
            value[0] + w * tx + (y * tz - z * ty),
            value[1] + w * ty + (z * tx - x * tz),
            value[2] + w * tz + (x * ty - y * tx),
        ]
    }

    fn scale_matrix(scale: [f32; 3]) -> [f32; 16] {
        [
            scale[0], 0.0, 0.0, 0.0, 0.0, scale[1], 0.0, 0.0, 0.0, 0.0, scale[2], 0.0, 0.0, 0.0,
            0.0, 1.0,
        ]
    }

    fn node_times_from_bindings(
        node_count: usize,
        bindings: &[fparkan_assets::PreparedControlNodeBinding],
    ) -> Vec<Option<fparkan_animation::AnimationTime>> {
        use fparkan_animation::AnimationTime;

        let mut times = vec![Some(AnimationTime(0.0)); node_count];
        for binding in bindings {
            if binding.flags & 0x04 != 0 {
                continue;
            }
            assert!(binding.node_index < node_count);
            let mut blend = if binding.flags & 0x01 != 0 {
                let mut value = binding.initial_blend;
                if value > 1.0 {
                    value -= 1.0;
                }
                if value < 0.0 {
                    value += 1.0;
                }
                value
            } else {
                binding.initial_blend.clamp(0.0, 1.0)
            };
            if binding.flags & 0x02 != 0 {
                blend = 1.0 - blend;
            }
            times[binding.node_index] = Some(AnimationTime(
                (1.0 - blend) * binding.frame_a + blend * binding.frame_b,
            ));
        }
        times
    }

    fn nodes_for_hierarchy(
        model: &ModelAsset,
        poses: &[fparkan_animation::Pose],
        root_from_body: [f32; 16],
        owner_scale: [f32; 3],
    ) -> Vec<BuildingMeshNode> {
        use fparkan_msh::{selected_slot, Group, Lod};

        let scale = scale_matrix(owner_scale);
        (0..model.node_count)
            .filter_map(|index| {
                let node = NodeId(u32::try_from(index).expect("model node id fits u32"));
                let slot = selected_slot(model, node, Lod(0), Group(0))?;
                let slot_data = &model.slots[usize::try_from(slot.0).expect("slot id fits usize")];
                let batch_start = usize::from(slot_data.batch_start);
                let batch_end = batch_start + usize::from(slot_data.batch_count);
                if !model.batches[batch_start..batch_end]
                    .iter()
                    .any(|batch| batch.index_count != 0)
                {
                    return None;
                }
                let node_pose = pose_matrix(*poses.get(index).expect("Node38 pose available"));
                let body_from_node = compose_row_major(scale, node_pose);
                let world_from_node = compose_row_major(root_from_body, body_from_node);
                Some(BuildingMeshNode {
                    node,
                    slot,
                    world_from_node,
                })
            })
            .collect()
    }

    #[test]
    #[ignore = "requires licensed corpus"]
    fn factory_plant_world_query_preserves_native_fixture_hit_under_affine_transforms() {
        use fparkan_assets::decode_nres_payload;
        use fparkan_msh::{
            decode_msh, node38_fallback_hierarchy, selected_slot, validate_msh, Group, Lod, NodeId,
            SlotId,
        };
        use std::{path::PathBuf, sync::Arc};

        const BODY_WALL: [[f32; 3]; 2] = [[-10.5, 22.16, 2.5], [-10.5, 19.16, 2.5]];
        const BODY_DOOR: [[f32; 3]; 2] = [[0.5, 22.16, 2.5], [0.5, 19.16, 2.5]];
        const BODY_REVERSED_WALL: [[f32; 3]; 2] = [BODY_WALL[1], BODY_WALL[0]];
        const BODY_HIT: [f32; 3] = [-10.5, 20.16, 2.5];
        const ROTATED_SCALED_TRANSLATED: [f32; 16] = [
            0.0, -3.0, 0.0, 7.0, 2.0, 0.0, 0.0, -4.0, 0.0, 0.0, 0.5, 2.0, 0.0, 0.0, 0.0, 1.0,
        ];

        let root = PathBuf::from(
            std::env::var_os("FPARKAN_CORPUS_PART1_ROOT")
                .expect("FPARKAN_CORPUS_PART1_ROOT is required for licensed corpus tests"),
        );
        let archive_bytes = std::fs::read(root.join("fortif.rlb")).expect("fortif.rlb bytes");
        let archive = decode_nres_payload(Arc::from(archive_bytes.into_boxed_slice()))
            .expect("fortif.rlb archive");
        let entry = archive
            .entries()
            .iter()
            .find(|entry| entry.name_bytes().eq_ignore_ascii_case(b"fr_l_plant.msh"))
            .expect("plant factory MSH");
        let nested = decode_nres_payload(Arc::from(
            archive
                .payload(entry.id())
                .expect("MSH payload")
                .to_vec()
                .into_boxed_slice(),
        ))
        .expect("nested plant MSH");
        let model = validate_msh(&decode_msh(&nested).expect("plant MSH document"))
            .expect("plant MSH model");
        let node_id = NodeId(1);
        let slot = selected_slot(&model, node_id, Lod(0), Group(0)).expect("Node 1 slot");
        assert_eq!(slot, SlotId(0));

        let pose = node38_fallback_hierarchy(&model)
            .expect("Node38 fallback hierarchy")
            .poses[1];
        assert_eq!(pose.rotation, [0.0, 0.0, 0.0, 1.0]);
        let body_from_node = translated(
            pose.translation[0],
            pose.translation[1],
            pose.translation[2],
        );

        for body_to_world in [IDENTITY, ROTATED_SCALED_TRANSLATED] {
            let world_from_node = compose_row_major(body_to_world, body_from_node);
            let node = BuildingMeshNode {
                node: node_id,
                slot,
                world_from_node,
            };
            let to_world = |point| {
                super::transform_point(body_to_world, point).expect("finite fixture transform")
            };
            let world_wall = [to_world(BODY_WALL[0]), to_world(BODY_WALL[1])];
            let expected_hit = to_world(BODY_HIT);
            let hit = query_building_segment_world(&model, &[node], world_wall)
                .expect("valid factory wall query")
                .expect("solid factory wall hit");

            assert_eq!(hit.node, node_id);
            assert_eq!(hit.slot, slot);
            assert_eq!(hit.batch_index, 5);
            assert_eq!(hit.triangle_descriptor_index, 62);
            for axis in 0..3 {
                assert!(
                    (hit.position[axis] - expected_hit[axis]).abs() <= 1.0e-3,
                    "world hit {:?} != {:?}",
                    hit.position,
                    expected_hit
                );
            }
            let world_delta = [
                expected_hit[0] - world_wall[0][0],
                expected_hit[1] - world_wall[0][1],
                expected_hit[2] - world_wall[0][2],
            ];
            let expected_distance_squared = super::dot3(world_delta, world_delta);
            assert!(
                (hit.distance_squared - expected_distance_squared).abs() <= 1.0e-2,
                "world distance squared {} != {}",
                hit.distance_squared,
                expected_distance_squared
            );

            for body_segment in [BODY_DOOR, BODY_REVERSED_WALL] {
                let world_segment = [to_world(body_segment[0]), to_world(body_segment[1])];
                assert!(query_building_segment_world(&model, &[node], world_segment)
                    .expect("valid factory miss query")
                    .is_none());
            }
        }
    }

    #[test]
    #[ignore = "requires licensed AutoDemo corpus"]
    fn autodemo_portal_uses_node3_authored_closed_and_open_poses() {
        use crate::{
            create, load_mission_static_preview_roots, loaded_mission_assets,
            loaded_mission_object_drafts, EngineServices, MissionMode, MissionRequest,
        };
        use fparkan_animation::AnimationTime;
        use fparkan_msh::{node38_fallback_hierarchy, node38_sampled_hierarchy_at_times};
        use fparkan_render::LegacyIron3dEulerTransform;
        use fparkan_vfs::DirectoryVfs;
        use std::{num::NonZeroUsize, path::PathBuf, sync::Arc};

        const OBJECT_INDEX: usize = 4;
        const BODY_DOOR: [[f32; 3]; 2] = [[0.5, 22.16, 2.5], [0.5, 19.16, 2.5]];

        let root = PathBuf::from(
            std::env::var_os("FPARKAN_CORPUS_PART1_ROOT")
                .expect("FPARKAN_CORPUS_PART1_ROOT is required for licensed corpus tests"),
        );
        let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root))))
            .expect("create SingleMission fixture engine");
        load_mission_static_preview_roots(
            &mut engine,
            MissionRequest {
                key: "MISSIONS/Autodemo.00/data.tma".to_string(),
                mode: MissionMode::SingleMission,
            },
            NonZeroUsize::MAX,
        )
        .expect("load SingleMission preview roots");
        let assets = loaded_mission_assets(&engine).expect("loaded mission assets");
        let drafts = loaded_mission_object_drafts(&engine).expect("loaded object drafts");
        let draft = &drafts[OBJECT_INDEX];
        assert!(draft
            .resource_name_raw
            .eq_ignore_ascii_case(b"UNITS\\BUILDS\\PLANT\\wor_l_n1.dat"));
        let visual_id = *assets
            .visuals_for_object(OBJECT_INDEX)
            .first()
            .expect("PLANT root visual");
        let visual = assets.visual_by_id(visual_id).expect("PLANT visual");
        let model_id = visual.model_id.expect("PLANT MSH model");
        let model = &assets
            .model_by_id(model_id)
            .expect("prepared PLANT model")
            .validated;
        assert_eq!(model.node_count, 29);

        let controls = assets
            .object_component_controls
            .get(OBJECT_INDEX)
            .and_then(|components| components.first())
            .and_then(Option::as_ref)
            .expect("PLANT component CTLD bindings");
        let node3_binding = controls
            .bindings
            .iter()
            .find(|binding| binding.node_index == 3)
            .expect("Node3 CTLD binding");
        assert_eq!(node3_binding.frame_a, 0.0);
        assert_eq!(node3_binding.frame_b, 1.0);
        assert_eq!(node3_binding.initial_blend, 0.0);

        let mut node_times = node_times_from_bindings(model.node_count, &controls.bindings);
        let closed_hierarchy = node38_sampled_hierarchy_at_times(model, &node_times)
            .or_else(|| node38_fallback_hierarchy(model))
            .expect("initial Node38 hierarchy");
        assert_eq!(node_times[3], Some(AnimationTime(0.0)));

        let root_transform = LegacyIron3dEulerTransform {
            translation: draft.position,
            orientation_radians: draft.orientation_raw,
        };
        let root_from_body = root_transform
            .try_row_major()
            .expect("finite PLANT root matrix");
        let world_door = BODY_DOOR.map(|point| {
            root_transform
                .try_transform_scaled_point(point, draft.scale)
                .expect("finite BODY_DOOR world point")
        });
        let closed_nodes =
            nodes_for_hierarchy(model, &closed_hierarchy.poses, root_from_body, draft.scale);
        let closed = query_building_segment_world(model, &closed_nodes, world_door)
            .expect("closed-door building query")
            .expect("authored time-0 doorway blocker");
        assert_eq!(closed.node, NodeId(3));
        assert_eq!(closed.slot, SlotId(25));
        assert_eq!(closed.batch_index, 100);
        assert_eq!(closed.triangle_descriptor_index, 855);
        assert_eq!(model.batches[closed.batch_index].batch_flags, 0x2000);

        node_times[3] = Some(AnimationTime(node3_binding.frame_b));
        let open_hierarchy = node38_sampled_hierarchy_at_times(model, &node_times)
            .expect("authored Node3 time-1 hierarchy");
        let open_nodes =
            nodes_for_hierarchy(model, &open_hierarchy.poses, root_from_body, draft.scale);
        assert!(query_building_segment_world(model, &open_nodes, world_door)
            .expect("time-1 building query")
            .is_none());
    }
}