Repository navigation
Expand file tree
/
Copy pathobjects.zig
More file actions
4577 lines (4186 loc) · 216 KB
/
Copy pathobjects.zig
File metadata and controls
4577 lines (4186 loc) · 216 KB
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
//! `C:\lancer\game\objects.cpp`: the hierarchy of nodes each live object embeds, one for each part
//! of its model.
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const layout = @import("../../formats/layout.zig");
const engine = @import("../../engine.zig");
const Pointer = engine.Pointer;
const shp = @import("../../formats/shp.zig");
const math = @import("../surrender/math.zig");
const srapiext = @import("../surrender/surrenderlib/srapiext.zig");
const srcore = @import("../surrender/surrenderlib/srcore.zig");
const srlight = @import("../surrender/surrenderlib/srlight.zig");
const Frame = srapiext.Frame;
const gameobj = @import("gameobj.zig");
const GameObject = gameobj.GameObject;
const srofiles = @import("srofiles.zig");
const srtexture = @import("../surrender/surrenderlib/srtexture.zig");
const matmanager = @import("matmanager.zig");
const create = @import("create.zig");
const environfx = @import("environfx.zig");
const Random = @import("../random.zig").Random;
const xtrabits = @import("xtrabits.zig");
const Clock = @import("main.zig").Clock;
const aigeneric = @import("aigeneric.zig");
const events = @import("mission/events.zig");
const explode = @import("explode.zig");
const sound3d = @import("sound3d.zig");
const shield = @import("shield.zig");
const flash = @import("guns/flash.zig");
const cloak = @import("cloak.zig");
const Vector = math.Vector;
/// A node of an object's model hierarchy (`objects.cpp`), allocated at `0x004991D0`: the object's
/// root, then a node for each part of its model.
pub const Node = extern struct {
/// What the node draws.
kind: Kind,
flags: Flags,
/// The node's transform for the renderer, which holds the same place as `position` and
/// `orientation`.
frame: Pointer(Frame),
_unknown_0c: u32,
/// The capital shield showing on it, a slot of `capshields` (`capshield_create`); -1 for none,
/// as allocated.
capshield: i32,
/// Relative to the node it hangs from: a part's origin in its parent part. An object's root
/// holds the object's place in the world.
position: shp.Vec3,
/// Row-major 3x3, relative like `position`.
orientation: [9]f32,
/// The pose `node_place` placed a part's node by, committed with its place.
pose: Pose,
/// Where `position` goes next: `object_move` puts the position plus the velocity here, and
/// placing an object sets both. `node_tree_update` (`updateTree`) and `object_link_part` copy
/// it into `position`, and `node_place` works it out for a part's node.
next_position: shp.Vec3,
/// Likewise for `orientation`: `object_move` puts the orientation times the rotation here.
next_orientation: [9]f32,
/// The pose `node_place` worked the next place of a part's node out from: the animation's
/// angles plus the turret's, and the animation's offset.
next_pose: Pose,
/// The model part the node stands for, as loaded.
part: Pointer(shp.Part),
/// The object the node belongs to.
owner: Pointer(GameObject),
_unknown_ac: [8]u8,
/// How the node plays its animation track.
mode: Model.Mode,
/// Which of the part's animation tracks the node runs, and where `node_animate` reads its
/// keyframes; past the part's tracks, the node is not animated.
animation: i32,
/// Where the node is in its track, and how far it moves on each simulation step.
time: f32,
speed: f32,
/// Where the animation has the node turned, added to the part's own angles (`node_animate`).
animated_angles: shp.Vec3,
/// Where the animation has the node moved, added to its part's origin.
animated_offset: shp.Vec3,
/// The angles a turret turns the node by besides the animation's (`node_turn`), which
/// `node_place` adds to them.
angles: shp.Vec3,
/// A component's counterpart of `GameObject.armor`, which `ship_damage_value` reads for it.
armor: f32,
/// The node it hangs from; null for a root. The root's `owner` is the object's.
parent: Pointer(Node),
_unknown_f0: u32,
/// Children the list at `children` can hold: 100 once allocated.
child_capacity: i32,
child_count: i32,
/// Its number among the object's part nodes (`object_number_parts`), for an object that lists
/// components; -1 when allocated.
number: i32,
children: Pointer(Pointer(Node)),
/// What a node draws, which `node_draw` (`0x0049A8C0`) switches on, as the routine that makes
/// it gives it (`node_alloc`, `0x004991D0`).
pub const Kind = enum(u32) {
/// A model part (`node_add_part`).
part = 1,
/// An engine glow, which brightens with the throttle (`node_mount_glow`, `0x00499540`).
glow = 2,
/// A light's two sprites (`node_mount_light`, `Model.Light`).
light_sprites = 3,
/// A gun muzzle's flash (`node_mount_muzzle`, `0x00499680`; `muzzle_flash_draw`,
/// `0x0047BA80`).
muzzle = 4,
/// The point light a blinking light casts (`node_mount_light`, `Model.Light`).
point_light = 5,
/// What a hit leaves where it struck (`node_add_effect`, `0x004992D0`; `shieldfx.zig`).
hit = 6,
_,
pub fn format(kind: Kind, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return layout.formatTagAs(Kind, kind, "node kind", writer);
}
};
pub const Flags = packed struct(u32) {
/// Set while the node's next place is waiting to be committed. `object_move` sets it on an
/// object's root; `node_tree_update` commits the place and clears it (`commitNext`), and
/// `object_link_part` clears it along with the next three bits.
next_pending: bool,
/// Set when `node_tree_update` commits a new place for the node, and cleared the next time
/// it visits the node. While it's set, `node_frame_update` draws the node between that
/// place and the next.
committed: bool,
/// Set with `committed`, and cleared by `node_frame_update`.
unframed: bool,
/// Set by `node_place`, and cleared each time `node_tree_update` visits the node: the next
/// place comes from a pose, which `node_frame_update` draws the node between the poses by.
posed: bool,
/// **Unknown.** Set by `node_draw` (`0x0049A8C0`). Cycling subtargets passes over a
/// component with it.
_unknown_4: bool,
/// Hidden, as a component's damaged parts are while it is intact.
hidden: bool,
/// Set on a component's holder once `component_damage` takes the component's armor below
/// zero.
destroyed: bool,
/// **Unknown.** Set on the nodes that `0x004992D0`, `0x00499540`, `0x00499680` and
/// `0x00499730` make.
_unknown_7: bool,
/// Listed among the object's components.
component: bool,
/// **Unknown.** `create_object` sets it on the root.
_unknown_9: bool,
/// The base of a turret, which the turret fits mark (`turret_fit_aimed`); destroying the
/// node stops the turret's gun for good (`node_forget`, `0x00499BB0`).
turret: bool,
/// Set while the node or one hanging from it plays an animation track: `node_play` sets it
/// up to the root, and `node_tree_update` descends only into children that have it.
animating: bool,
_unknown_12: bool,
/// A component the player can pick as a subtarget: set for parts with the `targetable`
/// flag, and by `SetTargetable`.
targetable: bool,
_unknown_14: u18,
};
/// The first step of `node_tree_update` for a node: clears flag bits 1 and 3, and if the
/// node's next place is pending, commits it. The game copies the whole block from
/// `next_position` to the end of `next_pose` over the block from `position` to the end of
/// `pose`, then clears `next_pending` and sets `committed` and `unframed`.
pub fn commitNext(node: *Node) void {
node.flags.committed = false;
node.flags.posed = false;
if (!node.flags.next_pending) return;
node.position = node.next_position;
node.orientation = node.next_orientation;
node.pose = node.next_pose;
node.flags.next_pending = false;
node.markMoved();
}
/// Marks it moved: its place committed for the step, and its frame to be worked out again
/// (`committed`, `unframed`), as `commitNext` leaves it and as the orders and the missiles
/// that place an object themselves leave its root.
pub fn markMoved(node: *Node) void {
node.flags.committed = true;
node.flags.unframed = true;
}
/// A part node's pose: the animation's angles plus the turret's, and its offset.
pub const Pose = extern struct {
angles: shp.Vec3,
offset: shp.Vec3,
comptime {
assert(@offsetOf(Pose, "angles") == 0x00);
assert(@offsetOf(Pose, "offset") == 0x0C);
assert(@sizeOf(Pose) == 0x18);
}
};
/// `node_frame_update` (`0x0049A460`) for an object's root, once a frame before it is drawn,
/// `fraction` of the way through the simulation's step: where the object is drawn and what
/// the camera follows. Once a step has committed a new place, the root is drawn between that
/// place and the next, along the straight line between them and turned by that share of the
/// turn between; at the start of a step, at the committed place. Null where the frame stays
/// where it was, until the first step moves the object. A root is never posed, since
/// `node_place` places part nodes alone.
///
/// Not ported: in a multiplayer game, another player's ship, whose root has flag bit 9, is
/// drawn between the places its last two messages gave it (`+0x768`, `+0x798`)
/// ([#55](https://github.com/OpenReliant/openreliant/issues/55)).
pub fn framePlace(node: *Node, fraction: f32) ?Model.Local {
if (!node.flags.committed and !node.flags.unframed) return null;
node.flags.unframed = false;
const now: Model.Local = .{ .position = node.position.vector(), .orientation = node.orientation };
if (fraction == 0) return now;
const next: Model.Local = .{ .position = node.next_position.vector(), .orientation = node.next_orientation };
return between(now, next, fraction);
}
comptime {
assert(@offsetOf(Node, "next_position") - @offsetOf(Node, "position") == 0x48);
assert(@offsetOf(Node, "part") - @offsetOf(Node, "next_position") == 0x48);
assert(@bitOffsetOf(Flags, "hidden") == 5);
assert(@bitOffsetOf(Flags, "component") == 8);
assert(@bitOffsetOf(Flags, "targetable") == 13);
assert(@bitOffsetOf(Flags, "posed") == 3);
assert(@bitOffsetOf(Flags, "animating") == 11);
assert(@bitOffsetOf(Flags, "turret") == 10);
assert(@offsetOf(Node, "kind") == 0x00);
assert(@offsetOf(Node, "position") == 0x14);
assert(@offsetOf(Node, "pose") == 0x44);
assert(@offsetOf(Node, "next_pose") == 0x8C);
assert(@offsetOf(Node, "mode") == 0xB4);
assert(@offsetOf(Node, "time") == 0xBC);
assert(@offsetOf(Node, "speed") == 0xC0);
assert(@offsetOf(Node, "animated_angles") == 0xC4);
assert(@offsetOf(Node, "angles") == 0xDC);
assert(@offsetOf(Node, "orientation") == 0x20);
assert(@offsetOf(Node, "part") == 0xA4);
assert(@offsetOf(Node, "armor") == 0xE8);
assert(@offsetOf(Node, "child_count") == 0xF8);
assert(@offsetOf(Node, "children") == 0x100);
assert(@sizeOf(Node) == 0x104);
}
};
/// `object_set_position` (`0x0049B600`): places the object's root at `at`: its frame, which the
/// port keeps as `frame` (`frameTree`), where it is and where it goes next, so that it doesn't
/// move from where it was. The game also sets the two places a multiplayer game draws another
/// player's ship between (`+0x768`, `+0x798`), which OpenReliant doesn't keep (#55).
/// **Unverified:** it and the functions after it lie after this file's known code, before
/// `particles.cpp`'s.
pub fn setPosition(object: *GameObject, frame: *Model.Local, at: Vector) void {
const position: shp.Vec3 = .of(at);
frame.position = at;
object.root.next_position = position;
object.root.position = position;
}
/// `object_set_orientation` (`0x0049B650`): turns the object's root to `orientation`, as
/// `setPosition` places it.
pub fn setOrientation(object: *GameObject, frame: *Model.Local, orientation: math.Matrix) void {
frame.orientation = orientation;
object.root.next_orientation = orientation;
object.root.orientation = orientation;
}
/// Moves the object's root by `by`: where it is, where it goes next and its frame alike, so that the
/// move its step has left to make stays, where `setPosition` drops it.
pub fn shift(object: *GameObject, frame: *Model.Local, by: Vector) void {
frame.position += by;
object.root.next_position = .of(object.root.next_position.vector() + by);
object.root.position = .of(object.position() + by);
}
test shift {
var object = gameobj.testing.object();
var frame: Model.Local = .{ .position = .{ 0, 0, 100 }, .orientation = math.identity };
object.root.position = .{ .x = 0, .y = 0, .z = 100 };
object.root.next_position = .{ .x = 0, .y = 0, .z = 130 };
shift(&object, &frame, .{ 5, 0, 0 });
// It moves, and so does where its step takes it next, its move of 30 kept.
try std.testing.expectEqual(Vector{ 5, 0, 100 }, object.position());
try std.testing.expectEqual(Vector{ 5, 0, 130 }, object.root.next_position.vector());
try std.testing.expectEqual(Vector{ 5, 0, 100 }, frame.position);
}
/// `object_set_position` and `object_set_orientation` together: places the object's root at
/// `place`, where it stands and how it is turned (`setPosition`, then `setOrientation`).
pub fn setPlace(object: *GameObject, frame: *Model.Local, place: math.Place) void {
setPosition(object, frame, place.position);
setOrientation(object, frame, place.orientation);
}
test setPlace {
var object = gameobj.testing.object();
var frame: Model.Local = .{};
const place: math.Place = .{ .position = .{ 1, 2, 3 }, .orientation = math.rotation(.y, 0.5) };
setPlace(&object, &frame, place);
// The frame, and where it stands now and next, all at the place.
try std.testing.expectEqual(place, frame);
try std.testing.expectEqual(place, object.placeAt(.now));
try std.testing.expectEqual(place, object.placeAt(.next));
}
/// The file of a model made from none, as in a test.
const no_source: shp.Model = .{ .header = std.mem.zeroes(shp.Header), .parts = &.{}, .trailing_bytes = 0 };
const no_loaded: srofiles.Loaded = .{ .parts = &.{} };
/// A box: its centre, how it is turned, and how far it reaches from its centre along each of its
/// own axes.
pub const Box = struct {
centre: Vector,
orientation: math.Matrix = math.identity,
half: Vector,
/// A box of a part's collision tree, in the part's frame.
fn ofNode(node: shp.TreeNode) Box {
return .{ .centre = node.centre.vector(), .orientation = node.orientation, .half = node.half_size.vector() };
}
/// The box `model` stands in, its bounding box, with its root at `root` (`node_hit_test` for a
/// root node, by its object's bounds).
pub fn ofBounds(model: *const Model, root: math.Place) Box {
const half = (model.bounds[1] - model.bounds[0]) * @as(Vector, @splat(0.5));
const middle = (model.bounds[1] + model.bounds[0]) * @as(Vector, @splat(0.5));
return .{ .centre = root.point(middle), .orientation = root.orientation, .half = half };
}
/// `point` in the box's own frame, from its centre.
fn local(box: Box, point: Vector) Vector {
return math.transformTransposed(box.orientation, point - box.centre);
}
/// Whether the segment from `from` to `to` meets it (`segment_meets_box`).
pub fn meetsSegment(box: Box, from: Vector, to: Vector) bool {
return boxEntry(box.local(from), box.local(to), .{ -box.half, box.half }) != null;
}
/// Whether a sphere at `at` of `radius` meets it, as far as the box widened by the radius
/// along each axis goes (`collision_hull_test`).
pub fn meetsSphere(box: Box, at: Vector, radius: f32) bool {
return @reduce(.And, @abs(box.local(at)) <= box.half + @as(Vector, @splat(radius)));
}
};
/// A part of an object's model, or of a model mounted on it however deep: one of the object's part
/// nodes, which the game numbers from the root down (`object_number_parts`, `0x00466BA0`).
pub const PartRef = struct {
model: *Model,
index: usize,
pub fn part(ref: PartRef) *Model.Part {
return &ref.model.parts[ref.index];
}
/// Its part's record as the file holds it, with its collision tree (`Model.partData`).
pub fn data(ref: PartRef) ?shp.PartData {
return ref.model.partData(ref.index);
}
/// The root box of its part's collision tree, in the part's frame; null for a part with none.
pub fn rootBox(ref: PartRef) ?Box {
const found = ref.data() orelse return null;
return if (found.nodes.len > 0) .ofNode(found.nodes[0]) else null;
}
/// The polygon of its part's finest mesh that face `face` of its record goes into
/// (`srofiles.polygonOf`); the face's own number for a model with no file behind it. The game's
/// collision trees list the polygons themselves (`mesh_build`).
pub fn polygon(ref: PartRef, face: usize) ?usize {
const found = ref.data() orelse return face;
if (found.meshes.len == 0) return null;
return srofiles.polygonOf(found.meshes[0].faces, face);
}
/// Whether it still names a part of the model of the object in `slot`, or of a model that
/// model carries. The models it may name go with their object, so only then may it be read.
pub fn livesIn(ref: PartRef, slot: *const create.Slot) bool {
const model = if (slot.model) |*live| live else return false;
return model.holds(ref.model) and ref.index < ref.model.parts.len;
}
/// Its part's record where it has a collision tree over a mesh to test.
fn tree(ref: PartRef) ?shp.PartData {
const found = ref.data() orelse return null;
return if (found.nodes.len > 0 and found.meshes.len > 0) found else null;
}
};
/// A node of the model tree of the object in slot `object`: its root, or one of its model's parts,
/// as the root's child list holds them. The game keeps the node's address where something rides
/// it (`launch.State.node`).
pub const NodeOf = struct {
object: u16,
/// The part, or null for the root.
part: ?usize = null,
/// Where the node's frame stands in the world, brought up to date with the frames it hangs
/// from (`SR_object_concate_parents`, `0x004C3570`): the root's as its frame has it drawn, a
/// part's as its model places it (`Model.frameAt`). Null for a part the object's model does not
/// have.
pub fn place(node: NodeOf, all: *const create.Objects) ?math.Place {
if (node.object >= all.slots.len) return null;
const slot = &all.slots[node.object];
const part = node.part orelse return slot.drawn;
const model = if (slot.model) |*held| held else return null;
if (part >= model.parts.len) return null;
return model.frameAt(part, slot.drawn);
}
};
/// A part of the model of the object in slot `object`, or of a model that model carries: what an
/// effect hangs from.
pub const PartOf = struct {
object: u16,
part: PartRef,
/// The part, while it still belongs to its object (`PartRef.livesIn`).
pub fn live(on: PartOf, all: *const create.Objects) ?*Model.Part {
return if (on.part.livesIn(&all.slots[on.object])) on.part.part() else null;
}
};
/// `object_hit_test` (`0x0049BEF0`), with `node_hit_test` (`0x0049BD30`) for the roots: where
/// `query.meets` the box `model` stands in with its root at `root`, it hands each of its shown
/// parts to `query.part`, with where the part stands as the next step has it and whether it or a
/// part it hangs from, however far up, plays a track, and then walks each model the part carries in
/// turn. A hidden part is passed over with all it carries.
pub fn hitWalk(model: *Model, root: math.Place, query: anytype) void {
walkHits(model, root, query, false);
}
fn walkHits(model: *Model, root: math.Place, query: anytype, carried_moving: bool) void {
if (!query.meets(Box.ofBounds(model, root))) return;
for (model.parts, 0..) |*part, index| {
if (part.hidden) continue;
const moving = carried_moving or model.moving(index);
query.part(.{ .model = model, .index = index }, model.partPlace(index, .next).within(root), moving);
var each = model.carriedBy(index);
while (each.next()) |mount| walkHits(&mount.model, model.mountRoot(mount, root, .next), query, moving);
}
}
/// How many boxes of a part's tree wait to be tested at once. The trees the game ships are far
/// shallower than this; a node past it is passed over rather than tested.
const hit_stack = 100;
/// What a sphere meets on a model: the part and the face, the part's place as tested, and the
/// nearest point of the face and its normal, both in the part's frame.
pub const Hit = struct {
part: PartRef,
place: math.Place,
face: usize,
point: Vector,
normal: Vector,
/// How far the sphere's centre stands from that point.
distance: f32,
};
/// `object_hit_test` with `collision_hull_test` (`0x00465240`): the nearest face of `model`, with
/// its root at `root`, or of a model it carries, to a sphere, or null where it meets none. Each
/// part's collision tree is descended through the boxes the sphere reaches, and the faces of a leaf
/// it reaches are tested.
pub fn hitSphere(model: *Model, root: math.Place, at: Vector, radius: f32) ?Hit {
const Nearest = struct {
at: Vector,
radius: f32,
best: f32,
/// The part being tested, and the sphere's centre in its frame, which its tree's boxes and
/// faces are given in.
ref: PartRef = undefined,
place: math.Place = .{},
local: Vector = undefined,
hit: ?Hit = null,
pub fn meets(sphere: *@This(), box: Box) bool {
return box.meetsSphere(sphere.at, sphere.radius);
}
pub fn part(sphere: *@This(), ref: PartRef, place: math.Place, _: bool) void {
const found = ref.tree() orelse return;
sphere.ref = ref;
sphere.place = place;
sphere.local = place.inverse(sphere.at);
descend(found, ref.index, sphere);
}
fn reaches(sphere: @This(), node: shp.TreeNode) bool {
return Box.ofNode(node).meetsSphere(sphere.local, sphere.radius);
}
fn face(sphere: *@This(), _: usize, index: usize, triangle: [3]Vector, normal: Vector) void {
// Only a sphere in front of the face, and near enough, is worth the triangle.
const ahead = math.dot(sphere.local - triangle[0], normal);
if (ahead < 0 or ahead * ahead > sphere.best) return;
const point = closestOnTriangle(sphere.local, triangle);
const away = math.lengthSquared(sphere.local - point);
if (away >= sphere.best) return;
sphere.best = away;
sphere.hit = .{ .part = sphere.ref, .place = sphere.place, .face = index, .point = point, .normal = normal, .distance = @sqrt(away) };
}
};
var sphere: Nearest = .{ .at = at, .radius = radius, .best = radius * radius };
hitWalk(model, root, &sphere);
return sphere.hit;
}
/// What a segment crosses of a model: the part and the face, and where, in the part's frame.
pub const Crossing = struct {
part: PartRef,
face: usize,
point: Vector,
normal: Vector,
/// Where it crosses in the world, as the part stands drawn.
pub fn inWorld(crossing: Crossing) Vector {
return crossing.part.part().drawn().point(crossing.point);
}
};
/// `missile_hull_test` (`0x004959A0`) with `tree_leaf_segment_test` (`0x0049BAE0`): what crosses
/// the faces of parts' collision trees with a segment. A part's tree is descended through the boxes
/// the segment meets, and at a leaf each face the segment starts in front of, no further off than
/// the segment is long, is tested. The last face crossed counts, not the nearest.
const Crosser = struct {
from: Vector,
to: Vector,
reach: f32,
/// The part being tested, and the segment's ends in its frame.
ref: PartRef = undefined,
local_from: Vector = undefined,
local_to: Vector = undefined,
hit: ?Crossing = null,
fn init(from: Vector, to: Vector) Crosser {
return .{ .from = from, .to = to, .reach = math.lengthSquared(to - from) };
}
pub fn meets(segment: *Crosser, box: Box) bool {
return box.meetsSegment(segment.from, segment.to);
}
pub fn part(segment: *Crosser, ref: PartRef, place: math.Place, _: bool) void {
const found = ref.tree() orelse return;
segment.ref = ref;
segment.local_from = place.inverse(segment.from);
segment.local_to = place.inverse(segment.to);
descend(found, ref.index, segment);
}
fn reaches(segment: Crosser, node: shp.TreeNode) bool {
return Box.ofNode(node).meetsSegment(segment.local_from, segment.local_to);
}
fn face(segment: *Crosser, _: usize, index: usize, triangle: [3]Vector, normal: Vector) void {
const ahead = math.dot(segment.local_from - triangle[0], normal);
if (ahead < 0 or ahead * ahead > segment.reach) return;
const point = segmentMeetsTriangle(segment.local_from, segment.local_to, triangle) orelse return;
segment.hit = .{ .part = segment.ref, .face = index, .point = point, .normal = normal };
}
};
/// `node_hit_test` with `missile_hull_test` for one part, `ref`, standing at `place`: the face of
/// it the segment crosses (`Crosser`), or null where it crosses none.
pub fn crossPart(ref: PartRef, place: math.Place, from: Vector, to: Vector) ?Crossing {
var segment: Crosser = .init(from, to);
segment.part(ref, place, false);
return segment.hit;
}
/// `object_hit_test` with `missile_hull_test`: the face of `model`, with its root at `root`, or of
/// a model it carries, that the segment crosses (`Crosser`), or null where it crosses none. The
/// last face crossed counts.
pub fn hitSegment(model: *Model, root: math.Place, from: Vector, to: Vector) ?Crossing {
var segment: Crosser = .init(from, to);
hitWalk(model, root, &segment);
return segment.hit;
}
/// `node_hit_test` for one part: its collision tree descended from the root box, passing over the
/// boxes `query.reaches` does not, with each triangle of a leaf it reaches handed to `query.face`,
/// in the leaf's order. The children of a box are tested last first.
fn descend(data: shp.PartData, part: usize, query: anytype) void {
const level = data.meshes[0];
var stack: [hit_stack]u32 = undefined;
var top: usize = 1;
stack[0] = 0;
// A well formed tree holds each node once, so a file that names one twice cannot keep the
// descent going.
var left = data.nodes.len;
while (top > 0 and left > 0) {
left -= 1;
top -= 1;
const at_node = stack[top];
if (at_node >= data.nodes.len) continue;
const node = data.nodes[at_node];
if (!query.reaches(node)) continue;
const faces = data.node_faces[at_node];
if (faces.len == 0) {
for (node.children) |child| {
if (child < 0 or top >= stack.len) continue;
stack[top] = @intCast(child);
top += 1;
}
continue;
}
visitFaces(level, faces, part, query);
}
}
/// Hands `query` each of a leaf's `faces` of `level`, as a triangle with its normal.
fn visitFaces(level: shp.Mesh, faces: []const u32, part: usize, query: anytype) void {
for (faces) |face| {
if (face >= level.faces.len) continue;
const record = level.faces[face];
const triangle: [3]Vector = .{
corner(level, record.vertices[0]) orelse continue,
corner(level, record.vertices[1]) orelse continue,
corner(level, record.vertices[2]) orelse continue,
};
query.face(part, face, triangle, record.normal.vector());
}
}
/// `tree_leaf_segment_test` (`0x0049BAE0`) over every leaf of part `ref`'s collision tree, the
/// part standing at `place`: for each leaf whose box the segment from `from` to `to` meets, the
/// last of its faces the segment crosses (`Crosser`), into `out` as far as it has room.
pub fn leafCrossings(ref: PartRef, place: math.Place, from: Vector, to: Vector, out: []Crossing) []Crossing {
const data = ref.tree() orelse return out[0..0];
var segment: Crosser = .init(from, to);
segment.ref = ref;
segment.local_from = place.inverse(from);
segment.local_to = place.inverse(to);
var count: usize = 0;
for (data.nodes, data.node_faces) |node, faces| {
if (count == out.len) break;
if (faces.len == 0 or !segment.reaches(node)) continue;
segment.hit = null;
visitFaces(data.meshes[0], faces, ref.index, &segment);
if (segment.hit) |hit| {
out[count] = hit;
count += 1;
}
}
return out[0..count];
}
/// Where the segment from `from` to `to` crosses a triangle, or null where it misses it
/// (`0x004AD700`): solved for how far along the segment and across the triangle's two edges from
/// its last corner, each within 0 and 1 and the two edges' together too. A segment in the
/// triangle's plane misses it.
fn segmentMeetsTriangle(from: Vector, to: Vector, triangle: [3]Vector) ?Vector {
const span = to - from;
const first = triangle[0] - triangle[2];
const second = triangle[1] - triangle[2];
const across = math.cross(span, second);
const determinant = math.dot(first, across);
if (determinant == 0) return null;
const start = from - triangle[2];
const u = math.dot(start, across) / determinant;
const turned = math.cross(start, first);
const v = math.dot(span, turned) / determinant;
const t = math.dot(second, turned) / determinant;
if (t < 0 or t > 1 or u < 0 or v < 0 or u + v > 1) return null;
return from + span * @as(Vector, @splat(t));
}
/// A trigger polygon of a part, as `model_load` (`0x004A483E`) builds it from the file's record
/// (`shp.TriggerPolygon`): its corners, from the part's first level, in the part's frame, and the
/// plane they stand in, facing along `(c1 - c0) × (c2 - c0)`.
pub const Trigger = struct {
corners: [4]Vector,
quad: bool,
normal: Vector,
offset: f32,
/// The trigger of `polygon`, its corners from `mesh`'s vertices; null where it names a vertex
/// the mesh lacks.
pub fn of(polygon: shp.TriggerPolygon, mesh: shp.Mesh) ?Trigger {
var corners: [4]Vector = @splat(@splat(0));
const count: usize = if (polygon.quad()) 4 else 3;
for (polygon.vertices[0..count], corners[0..count]) |number, *at| {
at.* = corner(mesh, std.math.cast(u32, number) orelse return null) orelse return null;
}
const normal = math.normalize(math.cross(corners[1] - corners[0], corners[2] - corners[0]));
return .{ .corners = corners, .quad = polygon.quad(), .normal = normal, .offset = math.dot(normal, corners[0]) };
}
/// Which way the step from `from` to `to`, both in the part's frame, crosses it: in, to the
/// back of its plane, or out, to its front; null where it doesn't (`collision_test_hull`). A
/// step crosses it where it crosses its plane through it: through its triangle, or a quad's
/// second triangle (`segmentMeetsTriangle`).
///
/// **Fix:** the game takes a step that crosses a triangle's plane anywhere as crossing the
/// triangle; OpenReliant only where it goes through it. Only quads ship.
pub fn crossing(trigger: Trigger, from: Vector, to: Vector) ?Way {
const ahead = trigger.height(to) >= 0;
if (ahead == (trigger.height(from) >= 0)) return null;
const c = trigger.corners;
const through = segmentMeetsTriangle(from, to, .{ c[0], c[1], c[2] }) != null or
(trigger.quad and segmentMeetsTriangle(from, to, .{ c[0], c[2], c[3] }) != null);
if (!through) return null;
return if (ahead) .outside else .inside;
}
pub const Way = enum { inside, outside };
/// How far `point` stands in front of its plane.
fn height(trigger: Trigger, point: Vector) f32 {
return math.dot(point, trigger.normal) - trigger.offset;
}
};
test Trigger {
// A square in the plane z = 0, its corners anticlockwise seen from +Z, which it faces.
var vertices: [4]shp.Vertex = @splat(std.mem.zeroes(shp.Vertex));
for (&vertices, [_]shp.Vec3{ .{ .x = 0, .y = 0, .z = 0 }, .{ .x = 10, .y = 0, .z = 0 }, .{ .x = 10, .y = 10, .z = 0 }, .{ .x = 0, .y = 10, .z = 0 } }) |*vertex, at| vertex.position = at;
const mesh: shp.Mesh = .{ .lod = std.mem.zeroes(shp.Lod), .vertices = &vertices, .faces = &.{}, .materials = &.{} };
const square = Trigger.of(.{ .vertices = .{ 0, 1, 2, 3 } }, mesh).?;
try std.testing.expectEqual(Vector{ 0, 0, 1 }, square.normal);
// Going in from its front through either triangle, and out again; passing beside it, or
// staying on one side, crosses nothing.
try std.testing.expectEqual(.inside, square.crossing(.{ 8, 2, 5 }, .{ 8, 2, -5 }).?);
try std.testing.expectEqual(.inside, square.crossing(.{ 2, 8, 5 }, .{ 2, 8, -5 }).?);
try std.testing.expectEqual(.outside, square.crossing(.{ 2, 8, -5 }, .{ 2, 8, 5 }).?);
try std.testing.expectEqual(null, square.crossing(.{ 20, 2, 5 }, .{ 20, 2, -5 }));
try std.testing.expectEqual(null, square.crossing(.{ 2, 2, 5 }, .{ 2, 2, 1 }));
// A triangle is crossed only through it, unlike in the game.
const triangle = Trigger.of(.{ .vertices = .{ 0, 1, 2, -1 } }, mesh).?;
try std.testing.expectEqual(null, triangle.crossing(.{ 2, 8, 5 }, .{ 2, 8, -5 }));
try std.testing.expectEqual(.inside, triangle.crossing(.{ 8, 2, 5 }, .{ 8, 2, -5 }).?);
// A corner the mesh lacks makes none.
try std.testing.expectEqual(null, Trigger.of(.{ .vertices = .{ 0, 1, 9, -1 } }, mesh));
}
/// A segment from `from` to `to`, for what it passes.
pub const Segment = struct {
from: Vector,
span: Vector,
pub fn between(from: Vector, to: Vector) Segment {
return .{ .from = from, .span = to - from };
}
/// How far along it passes nearest `at`, as a share of its length within 0 and 1.
pub fn nearest(segment: Segment, at: Vector) f32 {
return std.math.clamp(math.dot(segment.span, at - segment.from) / math.dot(segment.span, segment.span), 0, 1);
}
/// How far from `at` it passes at `along`, squared.
pub fn missSquared(segment: Segment, at: Vector, along: f32) f32 {
return math.lengthSquared(segment.point(along) - at);
}
/// How far along it first meets the sphere of `radius` about `at`, as a share of its length.
pub fn sphereEntry(segment: Segment, at: Vector, radius: f32) f32 {
const to = at - segment.from;
const a = math.dot(segment.span, segment.span);
const b = math.dot(segment.span, to) * -2;
const c = math.dot(to, to) - radius * radius;
const root = @sqrt(@max(b * b - 4 * a * c, 0));
return (-b - root) / (a + a);
}
/// The point `along` of the way from its start.
pub fn point(segment: Segment, along: f32) Vector {
return segment.from + segment.span * @as(Vector, @splat(along));
}
};
/// Which of a model's parts a segment's box test walks, and which of those it meets counts.
pub const PartWalk = enum {
/// Every shown part; the last met (`bullet_hull_hit`, `0x00479940`).
last_shown,
/// Every part, the root's child list, hidden or not; the first met (`missile_hit_hull`,
/// `0x00495BB0`, which walks on with the segment cut short in the frame of the part met, not
/// the world's, so no part after it is truly tested).
first,
};
/// How far along the segment from `from` to `to` it enters the box a part of the model stands in,
/// the part `walk` picks, by its first level's mesh: how a shot or a missile finds what it has hit
/// of a hull. Null where it enters none.
pub fn partEntry(model: *const Model, from: Vector, to: Vector, walk: PartWalk) ?f32 {
var entry: ?f32 = null;
for (model.parts) |*part| {
switch (walk) {
.last_shown => if (part.hidden) continue,
.first => if (part.removed) continue,
}
if (part.object.levels.len == 0) continue;
const mesh = part.object.levels[0].mesh;
// The segment in the part's own frame, where its mesh's box stands.
const drawn = part.drawn();
const start = drawn.inverse(from);
const end = drawn.inverse(to);
if (boxEntry(start, end, mesh.bounds)) |along| {
entry = along;
if (walk == .first) break;
}
}
return entry;
}
/// How far along a segment it enters a box (`segment_meets_box`, `0x0049B6A0`), by the slab test:
/// 0 where it starts inside; null where it misses.
pub fn boxEntry(from: Vector, to: Vector, bounds: [2]Vector) ?f32 {
var near: f32 = 0;
var far: f32 = 1;
const span = to - from;
inline for (0..3) |axis| {
if (span[axis] == 0) {
if (from[axis] < bounds[0][axis] or from[axis] > bounds[1][axis]) return null;
} else {
const first = (bounds[0][axis] - from[axis]) / span[axis];
const second = (bounds[1][axis] - from[axis]) / span[axis];
near = @max(near, @min(first, second));
far = @min(far, @max(first, second));
if (near > far) return null;
}
}
return near;
}
/// A face's or a trigger polygon's corner, or null where the file names a vertex the level does not
/// hold.
fn corner(level: shp.Mesh, vertex: u32) ?Vector {
if (vertex >= level.vertices.len) return null;
return level.vertices[vertex].position.vector();
}
/// The point of a triangle nearest `from`.
///
/// **Improvement:** the game reaches the same point through a general routine for the nearest point
/// of a simplex (`0x00478360`), which also serves points, lines and tetrahedra.
fn closestOnTriangle(from: Vector, triangle: [3]Vector) Vector {
const ab = triangle[1] - triangle[0];
const ac = triangle[2] - triangle[0];
const ap = from - triangle[0];
const d1 = math.dot(ab, ap);
const d2 = math.dot(ac, ap);
if (d1 <= 0 and d2 <= 0) return triangle[0];
const bp = from - triangle[1];
const d3 = math.dot(ab, bp);
const d4 = math.dot(ac, bp);
if (d3 >= 0 and d4 <= d3) return triangle[1];
const vc = d1 * d4 - d3 * d2;
if (vc <= 0 and d1 >= 0 and d3 <= 0) return triangle[0] + ab * @as(Vector, @splat(d1 / (d1 - d3)));
const cp = from - triangle[2];
const d5 = math.dot(ab, cp);
const d6 = math.dot(ac, cp);
if (d6 >= 0 and d5 <= d6) return triangle[2];
const vb = d5 * d2 - d1 * d6;
if (vb <= 0 and d2 >= 0 and d6 <= 0) return triangle[0] + ac * @as(Vector, @splat(d2 / (d2 - d6)));
const va = d3 * d6 - d5 * d4;
if (va <= 0 and (d4 - d3) >= 0 and (d5 - d6) >= 0) {
const along = (d4 - d3) / ((d4 - d3) + (d5 - d6));
return math.lerp(triangle[1], triangle[2], along);
}
const denominator = 1 / (va + vb + vc);
return triangle[0] + ab * @as(Vector, @splat(vb * denominator)) + ac * @as(Vector, @splat(vc * denominator));
}
test boxEntry {
const bounds: [2]Vector = .{ .{ -10, -10, -10 }, .{ 10, 10, 10 } };
// Through the middle, entering 40 of 100 in; from a corner; ending inside; and from inside.
try std.testing.expectEqual(0.4, boxEntry(.{ 0, 0, -50 }, .{ 0, 0, 50 }, bounds));
try std.testing.expect(boxEntry(.{ -50, -50, -50 }, .{ 50, 50, 50 }, bounds) != null);
try std.testing.expect(boxEntry(.{ 0, 0, -50 }, .{ 0, 0, 0 }, bounds) != null);
try std.testing.expectEqual(0, boxEntry(.{ 0, 0, 0 }, .{ 0, 0, 50 }, bounds));
// Past it, short of it, and alongside it.
try std.testing.expectEqual(null, boxEntry(.{ 50, 0, -50 }, .{ 50, 0, 50 }, bounds));
try std.testing.expectEqual(null, boxEntry(.{ 0, 0, -50 }, .{ 0, 0, -20 }, bounds));
try std.testing.expectEqual(null, boxEntry(.{ 0, 20, -50 }, .{ 0, 20, 50 }, bounds));
}
test segmentMeetsTriangle {
const triangle: [3]Vector = .{ .{ -10, -10, 0 }, .{ 10, -10, 0 }, .{ 0, 10, 0 } };
// Through it, a quarter of the way along, and where.
try std.testing.expectEqual(Vector{ 0, 0, 0 }, segmentMeetsTriangle(.{ 0, 0, -25 }, .{ 0, 0, 75 }, triangle).?);
// Short of it, beside it, and along its plane.
try std.testing.expectEqual(null, segmentMeetsTriangle(.{ 0, 0, -25 }, .{ 0, 0, -5 }, triangle));
try std.testing.expectEqual(null, segmentMeetsTriangle(.{ 20, 0, -25 }, .{ 20, 0, 75 }, triangle));
try std.testing.expectEqual(null, segmentMeetsTriangle(.{ -20, 0, 0 }, .{ 20, 0, 0 }, triangle));
}
test Segment {
const segment: Segment = .between(.{ 0, 0, -100 }, .{ 0, 0, 100 });
// Nearest a point off its middle, and one past its end.
try std.testing.expectEqual(0.5, segment.nearest(.{ 30, 0, 0 }));
try std.testing.expectEqual(1, segment.nearest(.{ 0, 0, 500 }));
try std.testing.expectEqual(900, segment.missSquared(.{ 30, 0, 0 }, 0.5));
// Into a sphere of 50 about the middle a quarter of the way along.
try std.testing.expectEqual(0.25, segment.sphereEntry(@splat(0), 50));
try std.testing.expectEqual(Vector{ 0, 0, -50 }, segment.point(0.25));
}
test hitSphere {
const gpa = std.testing.allocator;
var model: create.testing.Model = undefined;
try model.init(gpa);
defer model.deinit(gpa);
model.withHull();
var live = try Model.create(gpa, &model.source, &model.loaded, .{});
defer live.deinit(gpa);
gameobj.linkParts(&live, &model.source);
const root: math.Place = .{};
// The part is a square in the XY plane facing -Z; a sphere in front of it meets it.
const hit = hitSphere(&live, root, .{ 0, 0, -60 }, 100) orelse return error.TestExpectedHit;
try std.testing.expectEqual(0, hit.part.index);
try std.testing.expectApproxEqAbs(60, hit.distance, 1e-3);
try std.testing.expectEqual(math.Vector{ 0, 0, -1 }, hit.normal);
// Behind the face, and too far off to the side, it meets nothing.
try std.testing.expectEqual(null, hitSphere(&live, root, .{ 0, 0, 60 }, 100));
try std.testing.expectEqual(null, hitSphere(&live, root, .{ 900, 0, -60 }, 100));
// Its root box widened by the sphere's radius takes in a sphere near its corner, not one past.
try std.testing.expect(Box.ofBounds(&live, root).meetsSphere(.{ 150, 150, 0 }, 100));
try std.testing.expect(!Box.ofBounds(&live, root).meetsSphere(.{ 250, 0, 0 }, 100));
}
test hitSegment {
const gpa = std.testing.allocator;
var model: create.testing.Model = undefined;
try model.init(gpa);
defer model.deinit(gpa);
model.withHull();
var live = try Model.create(gpa, &model.source, &model.loaded, .{});
defer live.deinit(gpa);
gameobj.linkParts(&live, &model.source);
const root: math.Place = .{};
// The part is a square in the XY plane facing -Z; a segment through it from in front crosses
// it where it passes.
const hit = hitSegment(&live, root, .{ 5, 5, -60 }, .{ 5, 5, 60 }) orelse return error.TestExpectedHit;
try std.testing.expectEqual(&live, hit.part.model);
try std.testing.expectEqual(0, hit.part.index);
try std.testing.expectEqual(math.Vector{ 5, 5, 0 }, hit.point);
try std.testing.expectEqual(math.Vector{ 0, 0, -1 }, hit.normal);
// From behind, short of it, and beside it, it crosses nothing.
try std.testing.expectEqual(null, hitSegment(&live, root, .{ 5, 5, 60 }, .{ 5, 5, -60 }));
try std.testing.expectEqual(null, hitSegment(&live, root, .{ 5, 5, -60 }, .{ 5, 5, -30 }));
try std.testing.expectEqual(null, hitSegment(&live, root, .{ 900, 0, -60 }, .{ 900, 0, 60 }));
// Moved, it is struck where it stands at the next step.
try std.testing.expect(hitSegment(&live, .{ .position = .{ 1000, 0, 0 } }, .{ 1005, 5, -60 }, .{ 1005, 5, 60 }) != null);
try std.testing.expectEqual(null, hitSegment(&live, .{ .position = .{ 1000, 0, 0 } }, .{ 5, 5, -60 }, .{ 5, 5, 60 }));
// Its root box and its tree's root box meet the segment; one beside them doesn't.
try std.testing.expect(Box.ofBounds(&live, root).meetsSegment(.{ 5, 5, -60 }, .{ 5, 5, 60 }));
const tree = (PartRef{ .model = &live, .index = 0 }).rootBox().?;
try std.testing.expect(tree.meetsSegment(root.inverse(.{ 5, 5, -60 }), root.inverse(.{ 5, 5, 60 })));
try std.testing.expect(!tree.meetsSegment(root.inverse(.{ 900, 0, -60 }), root.inverse(.{ 900, 0, 60 })));
}
test leafCrossings {
const gpa = std.testing.allocator;
var model: create.testing.Model = undefined;
try model.init(gpa);
defer model.deinit(gpa);
model.withHull();
var live = try Model.create(gpa, &model.source, &model.loaded, .{});
defer live.deinit(gpa);
gameobj.linkParts(&live, &model.source);
const ref: PartRef = .{ .model = &live, .index = 0 };
// The part's tree is one leaf over the square; a segment through it crosses it where it
// passes, in the part's frame, wherever the part stands.
var out: [4]Crossing = undefined;
const through = leafCrossings(ref, .{}, .{ 20, 5, -60 }, .{ 20, 5, 60 }, &out);
try std.testing.expectEqual(1, through.len);
try std.testing.expectEqual(math.Vector{ 20, 5, 0 }, through[0].point);
try std.testing.expectEqual(math.Vector{ 0, 0, -1 }, through[0].normal);
const moved = leafCrossings(ref, .{ .position = .{ 1000, 0, 0 } }, .{ 1020, 5, -60 }, .{ 1020, 5, 60 }, &out);
try std.testing.expectEqual(math.Vector{ 20, 5, 0 }, moved[0].point);
// Beside it, or with no room, it crosses nothing.
try std.testing.expectEqual(0, leafCrossings(ref, .{}, .{ 900, 0, -60 }, .{ 900, 0, 60 }, &out).len);
try std.testing.expectEqual(0, leafCrossings(ref, .{}, .{ 20, 5, -60 }, .{ 20, 5, 60 }, out[0..0]).len);
}
/// `node_tree_frames` (`0x0049A880`) for an object, once a frame before it is drawn and before the
/// camera's frame: its root's frame (`Node.framePlace`), which `drawn` keeps, then each of its
/// part nodes' (`Model.frame`), and the model placed where the root's frame has it.
///
/// **Improvement:** an object the orders place from tick to tick, rather than move, goes on by
/// `glide` from where they placed it (`create.Slot.glide`), so it moves on every frame as what
/// flies does. The game draws it where it was placed. Null leaves an object that no step has moved
/// where it was drawn.
pub fn frameTree(root: *Node, model: ?*Model, drawn: *Model.Local, fraction: f32, glide: ?Vector) void {
if (root.framePlace(fraction)) |place| {
drawn.* = place;