Repository navigation
Expand file tree
/
Copy pathdte.zig
More file actions
2650 lines (2364 loc) · 112 KB
/
Copy pathdte.zig
File metadata and controls
2650 lines (2364 loc) · 112 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
//! `.DTE` missions: the 44 campaign and multiplayer missions, and everything scripted in them.
//!
//! A mission is a fixed-capacity image. A 27-entry directory at offset zero gives each section a
//! count and an offset, and the offsets are the same in every mission built from the same
//! template, so a section is a reservation that a mission fills as far as it needs.
//!
//! Inside a `.HOG` the image is RefPack compressed; a mission sitting loose in `missions\` is
//! stored expanded. `hog.Archive.read` handles the first case, so this module always sees the
//! expanded form.
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const layout = @import("layout.zig");
const refpack = @import("refpack.zig");
const commands = @import("../engine/game/executor/commands.zig");
const conditions = @import("../engine/vm/conditions.zig");
const opcodes = @import("../engine/vm/opcodes.zig");
pub const section_count = 27;
/// Writing mission files and their scripts.
pub const write = @import("dte/write.zig");
pub const assemble = @import("dte/assemble.zig");
/// A script's instructions as text.
pub const listing = @import("dte/listing.zig");
/// A routine's code as statements (#1127).
pub const statements = @import("dte/statements.zig");
/// Reading a routine's bytecode as statements.
pub const decompile = @import("dte/decompile.zig");
/// Whether two missions are the same mission.
pub const same = @import("dte/same.zig");
/// Identity and typed references for mission source collections (#608).
pub const source = @import("dte/source.zig");
/// What each directory slot holds. Sections the loader reads but this module does not interpret
/// keep their index as a name.
pub const Section = enum(u8) {
/// NUL-terminated names, addressed by byte offset rather than by index.
strings = 0,
operands_a = 1,
/// `u16` name offset and a `u32` value, read and written by script.
globals = 2,
/// The ships: every ship, station and nav point the mission places.
ships = 3,
/// Flight groups, stride `0x14`. Each starts with its object ID.
flight_groups = 4,
triggers = 5,
/// The script bytecode. Its count is in **halfwords**, so the section is `count * 2` bytes.
script = 6,
/// The object table, indexed by object ID: see [`Object`].
objects = 7,
/// One [`Part`] per named script routine in `script`, which the loader turns into the table
/// `call_part` indexes.
parts = 8,
unknown_9 = 9,
/// One flag per bytecode byte, which the interpreter consults for the script debugger.
script_flags = 10,
targets = 11,
/// Squads, stride `0x0C`. Each starts with its object ID, and lists its members in
/// `squad_members`.
squads = 12,
/// Squad membership records, stride `0x0C`: the member's object ID at `+0` and the owning
/// squad's index at `+4`. A squad's records are consecutive.
squad_members = 13,
/// The formations ([`Formation`]), which the formation orders fly flight groups into.
formations = 14,
/// The formations' points ([`FormationPoint`]).
formation_points = 15,
/// The curves, stride `0x44` ([`Curve`]): paths from one of the mission's ships to another,
/// which the director's camera flies along.
curves = 16,
/// Part descriptors for `script_b`, in the same form as `parts`.
parts_b = 17,
/// A second bytecode section, counted in halfwords like `script`. Empty in every shipped
/// mission.
script_b = 18,
unused_19 = 19,
unused_20 = 20,
/// **OpenReliant's own:** the mission's name, as `OpenReliantName` keeps it. The game binds
/// this section into a local variable of its binder and reads nothing of it, and no shipped
/// mission has one.
openreliant_name = 21,
operands_b = 22,
unknown_23 = 23,
/// One word of flags per Executor command (`CommandFlags`), which `command` sets its flag by
/// before each call. It reads the word the command's number places past the section's offset,
/// whatever the section's count. In the missions of the writer's template each word has a bit
/// for each of the command's parameters, save six whose word is 0
/// (`write.template.command_flags`). The other missions either leave the section unused, at
/// `DirectoryEntry.unused_offset`, inside section 1, whose operands then serve as the flags in
/// the game and the template's in OpenReliant, or start it at the file's end, past which
/// OpenReliant takes none.
command_flags = 24,
/// The same for the second, empty command catalogue.
command_flags_b = 25,
/// A third table of operands, which `0x004529D0` picks among `operands_a` and `operands_b` by a
/// bank number.
operands_c = 26,
_,
/// The bytes each of the section's records takes, which its directory count counts: bytes for
/// the string pool, the script flags and OpenReliant's name, halfwords for the scripts. Null
/// where the record's size is not known, as for section 20, which every shipped mission leaves
/// empty.
pub fn stride(section: Section) ?u8 {
return switch (section) {
.strings, .script_flags, .openreliant_name => 1,
.operands_a, .script, .script_b, .operands_b, .operands_c, .unknown_23, .command_flags, .command_flags_b => 2,
.unknown_9, .targets => 4,
.formations => @sizeOf(Formation),
.objects => @sizeOf(Object),
.globals, .squads, .squad_members, .unused_19 => 0x0C,
.formation_points => @sizeOf(FormationPoint),
.flight_groups => @sizeOf(FlightGroup),
.parts, .parts_b => @sizeOf(Part),
.triggers => @sizeOf(Trigger),
.curves => @sizeOf(Curve),
.ships => @sizeOf(Ship),
.unused_20, _ => null,
};
}
};
pub const DirectoryEntry = extern struct {
/// Records in use, not the capacity reserved for them.
count: u16,
_unused: u8,
formats: Formats,
offset: u32,
/// Sections the template reserves but this mission does not use.
pub const unused_offset: u32 = 0xFFFF;
/// The entry of a section the mission doesn't use.
pub const unused: DirectoryEntry = .{ .count = 0, ._unused = 0, .formats = .{}, .offset = unused_offset };
/// Four flags, in the low bits, which binding the mission notes where any section has them
/// (`mission_bind_section`); nothing reads them. Every entry of a shipped mission holds the
/// same: all four in most, the first three in `mission191` and `mission271`, the first two in
/// `mission801` and the first alone in `mission88`.
pub const Formats = packed struct(u8) {
first: bool = false,
second: bool = false,
third: bool = false,
fourth: bool = false,
/// Kept as the file has them, though no shipped mission sets them.
_unused: u4 = 0,
pub const all: Formats = .{ .first = true, .second = true, .third = true, .fourth = true };
/// These flags, with each that `other` sets set too, as binding notes them section by
/// section. The bits past the four are left as they are.
pub fn noting(formats: Formats, other: Formats) Formats {
return .{
.first = formats.first or other.first,
.second = formats.second or other.second,
.third = formats.third or other.third,
.fourth = formats.fourth or other.fourth,
._unused = formats._unused,
};
}
/// The flags as the file holds them, for a listing.
pub fn byte(formats: Formats) u8 {
return @bitCast(formats);
}
};
pub fn isUsed(entry: DirectoryEntry) bool {
return entry.offset != unused_offset;
}
comptime {
assert(@sizeOf(DirectoryEntry) == 8);
}
};
/// One placed object: a ship, a capital ship, a station or a nav point.
pub const Ship = extern struct {
/// The ship's object ID: its index into the object table.
object_id: u32,
/// Byte offset into the string pool.
name: u16,
_unknown_06: u16,
/// Mirrored from `position` when the mission loads, then copied from the live object's position
/// by `mission_ships_sync` (`0x0045A5F0`).
runtime_position: [3]f32,
/// Index of the ship's flight group, or `no_flight_group`.
flight_group: u8,
/// The record of `pilotstats.bin` that flies the ship (`object_set_pilot`), or `no_pilot`, as
/// the player's own record, the nav points and the planets have.
pilot: u8,
_unknown_16: u8,
/// The engine's own state: zero in the files, and cleared for every ship when the mission's
/// script starts.
flags: Flags,
/// Role. Ordinary ships stay below `0x100`; nav points and markers use 999 and the `0x3E3` to
/// `0x3E8` range, so reading this as a byte truncates many of them.
kind: u16,
_unknown_1a: u8,
/// Set for a waypoint once binding the mission has listed it (`mission_list_waypoints`).
waypoint_listed: u8,
/// As authored. The loader copies it into `runtime_position`.
position: [3]f32,
/// The kind of the ship it launches from, where `launch_gate` names a gate: the first of the
/// mission's ships of that kind (`mission_ship_create`).
launch_from: u16,
_unknown_2a: u8,
/// The gate of that ship it launches through, which its Launch order takes as its target's
/// component, or `no_launch`.
launch_gate: u8,
runtime_yaw: i16,
/// Whole degrees. The engine scales it by pi/180, which is what proves the unit.
yaw: i16,
/// The ship's components that are still intact, a bit each (`componentIntact`). Set to
/// `all_intact` when the mission's script starts; destroying component `n` clears bit `n & 31`
/// (`loseComponent`).
intact_components: u32,
/// Its place in a formation, by its index in `formation_points`, which the formation orders
/// fly it to, or `no_formation_point`. With the next two bytes it makes a reference: the tag of
/// `formation_points` where it names a point, `none` where it names none, then 0xFF.
formation_point: u16,
formation_point_tag: Reference.Tag,
_unknown_37: u8,
runtime_pitch: i16,
pitch: i16,
_unknown_3c: u8,
/// The loadout tier its missile racks are fitted by (`create_object`), as `create.settledTier`
/// settles it: 0 or 255, as most records hold, asks for the campaign's.
tier: u8,
_unknown_3e: [2]u8,
/// For a point (`point_kind`) that marks a place on a curve: the curve's index, which
/// `markedCurve` gives, and the share of the way along it the place lies at, which the
/// director's camera reaches as it passes (`0x00457510`). -1 and 0 elsewhere.
marker_curve: i16,
_unknown_42: [2]u8,
marker_at: f32,
runtime_roll: i16,
roll: i16,
/// The `flight_group` of a ship in none.
pub const no_flight_group: u8 = 0xFF;
/// The `pilot` of a record flown by no pilot of `pilotstats.bin`.
pub const no_pilot: u8 = 0xFF;
/// The `launch_gate` of a ship that does not launch.
pub const no_launch: u8 = 0xFF;
/// The `formation_point` of a ship in no formation.
pub const no_formation_point: u16 = 0xFFFF;
/// The `tier` most records hold, which asks for the campaign's loadout tier.
pub const campaign_tier: u8 = 0xFF;
/// The `launch_from` of a ship that doesn't launch, as every shipped record holds.
pub const no_launch_from: u16 = 0xFFFF;
/// The `marker_curve` of a record that marks no curve.
pub const no_marked_curve: i16 = -1;
/// A ship with nothing set: in no flight group, flown by no pilot, launching from nothing, in
/// no formation and marking no curve, with every component intact. The bytes the engine
/// doesn't read hold what most of the shipped missions' ships hold.
pub const blank: Ship = blank: {
var ship = std.mem.zeroes(Ship);
ship.flight_group = no_flight_group;
ship.pilot = no_pilot;
ship.launch_from = no_launch_from;
ship._unknown_2a = 0xFF;
ship.launch_gate = no_launch;
ship.intact_components = all_intact;
ship.formation_point = no_formation_point;
ship.formation_point_tag = .none;
ship._unknown_37 = 0xFF;
ship._unknown_3c = 0xFF;
ship.tier = campaign_tier;
ship.marker_curve = no_marked_curve;
ship._unknown_42 = .{ 0xFF, 0xFF };
break :blank ship;
};
/// The `kind` of a waypoint: a point a flight group's Patrol Route flies through, in the order
/// the mission lists them.
pub const waypoint_kind: u16 = 0x3E5;
/// Its flight group, where it is in one.
pub fn flightGroup(ship: Ship) ?u8 {
return if (ship.flight_group == no_flight_group) null else ship.flight_group;
}
/// Its point in a formation, where it has one.
pub fn formationPoint(ship: Ship) ?u16 {
return if (ship.formation_point == no_formation_point) null else ship.formation_point;
}
/// Its pilot, where it has one.
pub fn pilotRecord(ship: Ship) ?u8 {
return if (ship.pilot == no_pilot) null else ship.pilot;
}
/// The gate it launches through, where it launches.
pub fn launchGate(ship: Ship) ?u8 {
return if (ship.launch_gate == no_launch) null else ship.launch_gate;
}
/// The `intact_components` of a ship whose components are all intact.
pub const all_intact: u32 = std.math.maxInt(u32);
/// Whether its component `component` is intact: bit `component & 31` of `intact_components`.
pub fn componentIntact(ship: Ship, component: u8) bool {
return ship.intact_components & componentBit(component) != 0;
}
/// Its component `component` is destroyed: bit `component & 31` of `intact_components` clears.
pub fn loseComponent(ship: *align(1) Ship, component: u8) void {
ship.intact_components &= ~componentBit(component);
}
fn componentBit(component: u8) u32 {
return @as(u32, 1) << @as(u5, @truncate(component));
}
/// Whether it is a waypoint (`waypoint_kind`).
pub fn isWaypoint(ship: Ship) bool {
return ship.kind == waypoint_kind;
}
/// The `kind` of a point the mission marks, such as where a ship jumps in, or a place on a
/// curve (`marker_curve`).
pub const point_kind: u16 = 0x3E3;
/// The `kind` of the points the curves run between, and of those their tangents are drawn to.
pub const curve_point_kind: u16 = 0x3E4;
/// The `kind` of a mission's nav points (`mission_ship_create`, `0x00457CD9`), which
/// ShipReached's watches look from as from a waypoint (`0x0045B105`).
pub const nav_point_kind: u16 = 999;
/// Whether it is a nav point or one of the mission's markers, a waypoint or a point, which
/// `mission_ship_create` (`0x00457CD9` to `0x00457CF7`) makes a marker object of rather than a
/// ship.
pub fn isMarker(ship: Ship) bool {
return switch (ship.kind) {
nav_point_kind, waypoint_kind, curve_point_kind, point_kind => true,
else => false,
};
}
/// The curve whose place it marks, where it is a point that marks one.
pub fn markedCurve(ship: Ship) ?u16 {
if (ship.kind != point_kind) return null;
return std.math.cast(u16, ship.marker_curve);
}
pub const Flags = packed struct(u8) {
/// Set when the engine raises the ship's Destroyed event, which it then raises no more.
destroyed: bool,
_unknown: u7,
};
comptime {
assert(@offsetOf(Ship, "name") == 0x04);
assert(@offsetOf(Ship, "pilot") == 0x15);
assert(@offsetOf(Ship, "kind") == 0x18);
assert(@offsetOf(Ship, "position") == 0x1C);
assert(@offsetOf(Ship, "launch_from") == 0x28);
assert(@offsetOf(Ship, "launch_gate") == 0x2B);
assert(@offsetOf(Ship, "yaw") == 0x2E);
assert(@offsetOf(Ship, "formation_point") == 0x34);
assert(@offsetOf(Ship, "pitch") == 0x3A);
assert(@offsetOf(Ship, "tier") == 0x3D);
assert(@offsetOf(Ship, "roll") == 0x4A);
assert(@offsetOf(Ship, "marker_curve") == 0x40);
assert(@offsetOf(Ship, "marker_at") == 0x44);
assert(@sizeOf(Ship) == 0x4C);
}
};
/// A curve, section `curves`: a cubic Hermite spline from one of the mission's ships to another,
/// which leaves the first along one tangent and reaches the second along another. The director's
/// camera flies along the curves, and one that starts where another ends carries its path on.
/// Its points and tangents are the ships' places as the mission placed them, which the record
/// keeps as they are while the mission runs.
pub const Curve = extern struct {
/// The ship it starts at, and the ship it ends at, `Reference.unset` for none.
start: Reference,
end: Reference,
/// Where it starts and where it ends.
from: [3]f32,
to: [3]f32,
/// The ships its tangents are drawn to, one each side: `leaving` runs from `from` to the
/// first, and `arriving` from the second to `to`. `0x004573B0` makes the curve afresh from
/// the places of its four ships, on a message of the shared memory the game watches
/// (`0x00457730`).
leaving_handle: Reference,
arriving_handle: Reference,
/// Its tangents, each a tenth of its weight in the curve: the way it heads as it leaves its
/// start, and the way back from where it heads as it reaches its end.
leaving: [3]f32,
arriving: [3]f32,
_unknown_40: u32,
/// A curve between no ships, at the origin, its last word as most shipped curves hold it.
pub const blank: Curve = .{
.start = .none,
.end = .none,
.from = @splat(0),
.to = @splat(0),
.leaving_handle = .none,
.arriving_handle = .none,
.leaving = @splat(0),
.arriving = @splat(0),
._unknown_40 = 0xFFFFFFFF,
};
/// The ship it starts at, where it starts at one.
pub fn startShip(curve: Curve) ?u16 {
return if (curve.start.index == Reference.unset) null else curve.start.index;
}
/// The ship it ends at, where it ends at one.
pub fn endShip(curve: Curve) ?u16 {
return if (curve.end.index == Reference.unset) null else curve.end.index;
}
comptime {
assert(@offsetOf(Curve, "end") == 0x04);
assert(@offsetOf(Curve, "from") == 0x08);
assert(@offsetOf(Curve, "to") == 0x14);
assert(@offsetOf(Curve, "leaving_handle") == 0x20);
assert(@offsetOf(Curve, "arriving_handle") == 0x24);
assert(@offsetOf(Curve, "leaving") == 0x28);
assert(@offsetOf(Curve, "arriving") == 0x34);
assert(@sizeOf(Curve) == 0x44);
}
};
/// One named script routine.
///
/// The loader expands each of these into a 0x74-byte runtime entry, of which only the block
/// address and the argument count come from here; `call_part` and `spawn_part` index that table by
/// a single byte, so a mission has at most 256 parts. An `offset` of `no_block` leaves the entry
/// empty.
///
/// Parts are contiguous and in address order: each one's `offset + length` is the next one's
/// `offset`, and the last ends at the end of the script section.
pub const Part = extern struct {
/// Byte offset into the string pool. Missions ship with their authors' own names for these,
/// such as `(F)Arrival at CONVOY`.
name: u16,
_unknown_02: u16,
_unknown_04: [6]u8,
/// Start of the part, in **halfwords** from the start of the script section.
offset: u16,
flags: Flags,
/// Arguments the part takes. The caller reserves `4 * arguments + 16` bytes of frame for it.
arguments: u8,
_unknown_0e: u16,
/// Extent of the part, in halfwords: its entry block, then a trailer of zero or more 8-byte
/// records whose meaning is not yet known.
length: u16,
_unknown_12: [7]u8,
/// Read by the loader and passed to the routine that fills the runtime entry.
kind: u8,
_unknown_1a: u16,
/// An `offset` meaning the part has no block.
pub const no_block: u16 = 0xFFFF;
/// A part with no block, which takes no arguments and doesn't run at the start. The bytes the
/// engine doesn't read hold what most of the shipped missions' parts hold.
pub const blank: Part = .{
.name = 0,
._unknown_02 = 0,
._unknown_04 = @splat(0),
.offset = no_block,
.flags = .{ .start = false, ._unknown = 0 },
.arguments = 0,
._unknown_0e = 0,
.length = 0,
._unknown_12 = .{ 0, 0, 0, 0, 0xFF, 0xFF, 0xFF },
.kind = 0,
._unknown_1a = 2,
};
pub const Flags = packed struct(u8) {
/// Run when the mission starts, before any trigger is armed. Every mission has one such
/// part (`mission_script_start`, `0x0045CBC0`).
start: bool,
_unknown: u7,
};
pub fn isEmpty(part: Part) bool {
return part.offset == no_block;
}
/// Byte offset of the part's entry block within the script section.
pub fn start(part: Part) usize {
return halfwords(part.offset);
}
/// The block it runs, as a byte offset into the script: null for an empty part (`isEmpty`).
pub fn block(part: Part) ?usize {
return if (part.isEmpty()) null else part.start();
}
/// Bytes the part spans.
pub fn size(part: Part) usize {
return halfwords(part.length);
}
comptime {
assert(@offsetOf(Part, "name") == 0x00);
assert(@offsetOf(Part, "offset") == 0x0A);
assert(@offsetOf(Part, "arguments") == 0x0D);
assert(@offsetOf(Part, "length") == 0x10);
assert(@offsetOf(Part, "kind") == 0x19);
assert(@sizeOf(Part) == 0x1C);
}
};
/// What `Mission.findPart` and `Mission.findShip` find: one record, by its index, none, or more
/// than one.
pub const Found = union(enum) {
one: usize,
none,
several,
};
/// A block of script, the constants after it, and what runs it.
///
/// The script section is a sequence of these. Each is a block, then the block's constant table: a
/// whole number of 8-byte units, which `push_constant` reads a dword at a time. The table runs to
/// the start of the next routine.
pub const Routine = struct {
/// Byte offset of the block within the script.
start: usize,
/// Bytes of block and constants together.
extent: usize,
owner: Owner,
pub const Owner = union(enum) {
/// The indices of the triggers that run this block. Only triggers some object's slice
/// holds are counted, since no other can fire.
triggers: []const u16,
/// The index of the part this block is.
part: u16,
};
/// The constant table: the dwords from the block's end to the routine's.
pub fn constants(routine: Routine, script: []const u8) []align(1) const u32 {
const block = BlockReader.at(script, routine.start) orelse return &.{};
const from = routine.start + block.code.len + BlockReader.header_len;
const to = @min(routine.start + routine.extent, script.len);
if (from >= to) return &.{};
const bytes = script[from..to];
return std.mem.bytesAsSlice(u32, bytes[0 .. bytes.len - bytes.len % 4]);
}
};
/// A named value the script reads and writes.
pub const Global = extern struct {
name: u16,
_unknown_02: u16,
value: u32,
/// 7 in every shipped mission.
_unknown_08: u32,
/// A global that starts at 0, its last word as every shipped global holds it.
pub const blank: Global = .{ .name = 0, ._unknown_02 = 0, .value = 0, ._unknown_08 = 7 };
comptime {
assert(@sizeOf(Global) == 0x0C);
}
};
pub const Objective = extern struct {
data: [0x14]u8,
comptime {
assert(@sizeOf(Objective) == 0x14);
}
};
/// Runs a block of script when an event it watches happens to its subject.
///
/// A trigger holds no subject. It is reached through the subject object's entry in `objects`,
/// which gives the index of the ship's first trigger and how many follow, so a trigger that no
/// ship lists can never fire. When an event happens to a ship, the engine fires each of that
/// ship's triggers that is armed, whose `condition` and `qualifier` are the event's, and whose
/// operands pass the condition's checks. Firing starts a thread on the block `link` names, unless
/// a thread the trigger started is still running.
pub const Trigger = extern struct {
condition: Condition,
repeat: Repeat,
/// The block to run, as a halfword offset into the script, like a part's start. `0xFFFF` for
/// none. The blocks fill the script ahead of the first part.
link: u16,
_unknown_04: [16]u8,
/// Set for every trigger when the mission starts. Firing clears it, per `repeat`.
armed: u8,
/// The component of the subject the trigger watches, by its index among the subject's
/// components, or `whole_object`. It must equal the event's: a ShotAt or Destroyed event on a
/// component, such as a capital ship's turret, carries the component's index, and every other
/// event `whole_object`.
qualifier: u8,
/// Zero runs the new thread at once, inside the event; any other value leaves it to the
/// scheduler.
deferred: u8,
/// Byte arrays rather than wider types: these sit at odd offsets, and an `extern struct` would
/// pad a `u16` here into the wrong place.
_unknown_17: [2]u8,
/// Firings left, for `counted`.
repeat_counter: u8,
/// The firings a `counted` trigger has each time the script arms it (`SetTriggerState`), which
/// `repeat_counter` takes again then (`trigger_set_armed`).
repeat_count: u8,
_unknown_1b: u8,
/// Condition arguments, four bytes each, checked against the event's values: those the
/// condition marks as checked, and of those, the ones whose low halfword is not `0xFFFF`. A
/// Proximity trigger also keeps its distance in one (`proximity_distance`).
operands: [5]u32,
/// The qualifier of an event on the subject itself rather than one of its components.
pub const whole_object: u8 = 0xFF;
/// The operand of a Proximity trigger that gives how far its watch looks, in the radii of the
/// ship it watches (`0x0045AF60`). The matcher doesn't check it against the event's value, and
/// a trigger without it never fires.
pub const proximity_distance = 1;
/// A trigger on Shot At that runs nothing, fires once, watches the whole subject and checks no
/// value of its event. The bytes the engine doesn't read hold what most of the shipped
/// missions' triggers hold.
pub const blank: Trigger = .{
.condition = .shot_at,
.repeat = .once,
.link = Part.no_block,
._unknown_04 = @splat(0),
.armed = 1,
.qualifier = whole_object,
.deferred = 0,
._unknown_17 = .{ 0xFF, 0 },
.repeat_counter = 0,
.repeat_count = 0,
._unknown_1b = 0x88,
.operands = @splat(Operand.write(.unset)),
};
pub const Repeat = enum(u8) {
/// Disarms when it fires.
once = 0,
/// Never disarms, so it fires every time.
always = 1,
/// Disarms when `repeat_counter`, counted down on each firing, reaches zero.
counted = 2,
_,
pub fn format(repeat: Repeat, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return layout.formatTag(Repeat, repeat, writer);
}
};
/// The component its event is on, or null for the subject itself (`whole_object`).
pub fn component(trigger: Trigger) ?u8 {
return if (trigger.qualifier == whole_object) null else trigger.qualifier;
}
/// The block this trigger runs, as a byte offset into the script.
pub fn block(trigger: Trigger) ?usize {
if (trigger.link == Part.no_block) return null;
return halfwords(trigger.link);
}
comptime {
assert(@offsetOf(Trigger, "condition") == 0x00);
assert(@offsetOf(Trigger, "link") == 0x02);
assert(@offsetOf(Trigger, "armed") == 0x14);
assert(@offsetOf(Trigger, "qualifier") == 0x15);
assert(@offsetOf(Trigger, "deferred") == 0x16);
assert(@offsetOf(Trigger, "repeat_counter") == 0x19);
assert(@offsetOf(Trigger, "operands") == 0x1C);
assert(@sizeOf(Trigger) == 0x30);
}
};
/// A command's word of flags in section 24 (`Section.command_flags`), or in section 25 for the
/// second catalogue. `command` reads its low byte alone (`fromLow`). In the missions of the
/// writer's template the word has a bit for each of the command's parameters, the first's bit 0.
pub const CommandFlags = packed struct(u16) {
/// Whether `for_each_ship` walks the players' ships in a flight group, a squad's too, rather
/// than passing over them.
players: bool = false,
_unknown_1: u15 = 0,
/// The flags of a word whose low byte is `low`, all that `command` reads of it.
pub fn fromLow(low: u8) CommandFlags {
return @bitCast(@as(u16, low));
}
};
/// How a trigger operand names a ship, a flight group or a squad: an index into the section the tag
/// selects. The matcher turns a reference into the address of the record (`FUN_004530A0`), which is
/// how event values name them.
pub const Reference = packed struct(u32) {
index: u16,
tag: Tag,
/// **Unknown.** The matcher ignores it.
_unknown_24: u8,
/// The table a reference points into, by its entry in `editor_tables` (`0x004EE970`). The
/// executable's image holds the ships' and the flight groups' entries, and `editor_tables_fill`
/// (`0x00451620`) fills the others as the game starts: the squads' at `0x00451900`, their
/// members' at `0x0045192A`, the formations' at `0x0045197B` and their points' at
/// `0x004519A7`.
pub const Tag = enum(u8) {
ship = 0x00,
flight_group = 0x01,
squad = 0x16,
squad_member = 0x17,
formation = 0x19,
formation_point = 0x1A,
/// No table: the tag of a reference that is all ones, such as an unset operand.
none = 0xFF,
_,
};
/// An operand whose low halfword is this is not set, and is not checked.
pub const unset: u16 = 0xFFFF;
/// A reference to nothing, every bit set, as an unset trigger operand holds.
pub const none: Reference = .{ .index = unset, .tag = .none, ._unknown_24 = 0xFF };
/// A reference to record `index` of the table `tag` names, as the shipped missions write one:
/// its last byte 0xFF.
pub fn of(tag: Tag, index: u16) Reference {
return .{ .index = index, .tag = tag, ._unknown_24 = 0xFF };
}
/// Set in the index of an operand for a ship value, it matches any of the players' ships: in a
/// game of one, the player's.
pub const any_ship: u16 = 0x2000;
};
/// A trigger operand, read the way the matcher reads it for a value of the given kinds.
pub const Operand = union(enum) {
unset,
/// For a value that is a number: taken as it is.
number: u32,
/// For a ship value: any of the players' ships matches.
any_ship,
/// A ship, a flight group or a squad, by its index among the mission's records of its kind
/// (`Reference.Tag`).
ship: u16,
flight_group: u16,
squad: u16,
/// A tag the matcher cannot resolve.
other: u32,
/// The raw operand that reads back as `operand`, as the shipped missions write one
/// (`Reference.of`), and every bit set for none.
pub fn write(operand: Operand) u32 {
return switch (operand) {
.unset => @bitCast(Reference.none),
.number, .other => |raw| raw,
.any_ship => @bitCast(Reference.of(.ship, Reference.any_ship)),
.ship => |index| @bitCast(Reference.of(.ship, index)),
.flight_group => |index| @bitCast(Reference.of(.flight_group, index)),
.squad => |index| @bitCast(Reference.of(.squad, index)),
};
}
pub fn read(raw: u32, kinds: commands.Kinds) Operand {
const reference: Reference = @bitCast(raw);
if (reference.index == Reference.unset) return .unset;
if (kinds.number) return .{ .number = raw };
if (kinds.ship and reference.index & Reference.any_ship != 0) return .any_ship;
return switch (reference.tag) {
.ship => .{ .ship = reference.index },
.flight_group => .{ .flight_group = reference.index },
.squad => .{ .squad = reference.index },
.squad_member, .formation, .formation_point, .none, _ => .{ .other = raw },
};
}
};
test Operand {
const Kinds = commands.Kinds;
const ship: Kinds = @bitCast(@as(u32, 0x400));
const number: Kinds = @bitCast(@as(u32, 0x80));
try std.testing.expectEqual(Operand.unset, Operand.read(0xFFFFFFFF, ship));
try std.testing.expectEqual(Operand{ .number = 50 }, Operand.read(50, number));
try std.testing.expectEqual(Operand.any_ship, Operand.read(0xFF002000, ship));
try std.testing.expectEqual(Operand{ .flight_group = 4 }, Operand.read(0x00010004, ship));
try std.testing.expectEqual(Operand{ .ship = 7 }, Operand.read(0xFF000007, ship));
try std.testing.expectEqual(Operand{ .squad = 2 }, Operand.read(0x00160002, ship));
try std.testing.expectEqual(Operand{ .other = 0x000C0050 }, Operand.read(0x000C0050, @bitCast(@as(u32, 0x1000))));
// The matcher doesn't resolve a reference to a squad's member.
try std.testing.expectEqual(Operand{ .other = 0xFF170003 }, Operand.read(0xFF170003, ship));
// Written as the shipped missions write them, operands read back as they were.
for ([_]Operand{ .unset, .any_ship, .{ .ship = 7 }, .{ .flight_group = 4 }, .{ .squad = 2 }, .{ .other = 0xFF170003 } }) |operand| {
try std.testing.expectEqual(operand, Operand.read(Operand.write(operand), ship));
}
try std.testing.expectEqual(Operand{ .number = 50 }, Operand.read(Operand.write(.{ .number = 50 }), number));
try std.testing.expectEqual(0xFF002000, Operand.write(.any_ship));
}
/// One entry of the object table, section `objects`, indexed by object ID.
///
/// Ships, flight groups and squads each carry an object ID at their start, and an event names its
/// subject by one. The entry gives the object's kind and its slice of the trigger list.
pub const Object = extern struct {
kind: Kind,
/// Triggers in the object's slice.
count: u8,
/// Index of the first trigger in it.
first: u16,
tool_flags: ToolFlags,
/// Flags for the original's tools. Nothing in the game reads them. Only a ship's entry has
/// any set: a flight group's or a squad's is 0 in every shipped mission.
pub const ToolFlags = packed struct(u32) {
/// Set on exactly one entry, ship 0's, in most of the shipped missions. In most of those,
/// ship 0's name says it is the player's.
player: bool = false,
_unknown_1: u2 = 0,
/// Set only on curve points (kind `0x3E4`), on most of them.
curve_point: bool = false,
/// **Unknown:** set on some ships and some points, by no rule found.
_unknown_4: bool = false,
_unknown_5: u27 = 0,
};
/// The `first` of an object with no triggers, as every shipped mission's holds.
pub const no_triggers: u16 = 0xFFFF;
/// Where its slice starts and ends in a trigger list of `triggers` triggers, as far as the list
/// reaches.
pub fn triggerBounds(object: Object, triggers: usize) [2]usize {
const start = @min(object.first, triggers);
return .{ start, @min(start + object.count, triggers) };
}
pub const Kind = enum(u8) {
ship = 0,
flight_group = 1,
squad = 2,
_,
pub fn format(kind: Kind, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return layout.formatTag(Kind, kind, writer);
}
};
/// A set of kinds, a bit for each.
pub const KindSet = packed struct(u16) {
ship: bool,
flight_group: bool,
squad: bool,
_unused: u13,
pub fn has(set: KindSet, kind: Kind) bool {
return switch (kind) {
.ship => set.ship,
.flight_group => set.flight_group,
.squad => set.squad,
_ => false,
};
}
comptime {
for (.{ "ship", "flight_group", "squad" }) |name| {
assert(@bitOffsetOf(KindSet, name) == @backingInt(@field(Kind, name)));
}
}
};
comptime {
assert(@offsetOf(Object, "tool_flags") == 4);
assert(@sizeOf(Object) == 8);
}
};
/// **OpenReliant's own:** a mission's name, which OpenReliant keeps in section `openreliant_name`,
/// a section the game binds but never reads. The section's count is its size in bytes: this header,
/// then `length` bytes of the name in UTF-8, then a NUL. A mission is complete without it, and the
/// game plays one with it as it plays any other.
pub const OpenReliantName = extern struct {
tag: [4]u8 = OpenReliantName.expected_tag,
version: u16 = OpenReliantName.current_version,
length: u16,
/// The tag that marks the section as holding a name of this kind, and the one version so far.
pub const expected_tag = "ORMN".*;
pub const current_version: u16 = 1;
/// The name `section` holds, where it starts with a header of this kind and the name fits;
/// null for anything else, which OpenReliant leaves alone.
pub fn read(section: []const u8) ?[]const u8 {
if (section.len < @sizeOf(OpenReliantName)) return null;
const header: *align(1) const OpenReliantName = @ptrCast(section[0..@sizeOf(OpenReliantName)]);
if (!std.mem.eql(u8, &header.tag, &expected_tag) or header.version != current_version) return null;
const name = section[@sizeOf(OpenReliantName)..];
if (header.length > name.len) return null;
return name[0..header.length];
}
comptime {
assert(@sizeOf(OpenReliantName) == 8);
}
};
/// A flight group: its object ID, the wing it is listed in, and where its ships stand in the list
/// of the groups' ships that binding the mission makes.
pub const FlightGroup = extern struct {
object_id: u16,
/// 0 in every shipped mission, as are `_unknown_06` and `_unknown_0a`.
_unknown_02: u16,
/// Byte offset into the string pool, such as `(FG)Reliant`.
name: u16,
_unknown_06: u16,
/// The wing the mission lists the group's ships in (`mission_wings_build`).
wing: Wing,
/// How many of the mission's ships are in the group, and where the first stands in the list of
/// the groups' ships, or `no_ship`: both worked out as the mission is bound
/// (`mission_list_group_ships`, `0x00452EC0`), whatever the file holds.
ship_count: u8,
_unknown_0a: u16,
first_ship: u32,
/// A formation for the group, as a reference to `formations` (tag `formation`). No shipped
/// mission names one: each holds `no_formation`.
formation: Reference,
pub const no_ship: u32 = 0xFFFFFFFF;
/// The `formation` of a group with none, as every shipped mission's has.
pub const no_formation: Reference = .{ .index = Reference.unset, .tag = .formation, ._unknown_24 = 0xFF };
/// A group of no wing and no formation, whose ships binding the mission lists
/// (`mission.bind.listGroupShips`), its unread bytes 0, as in every shipped mission.
pub const blank: FlightGroup = .{
.object_id = 0,
._unknown_02 = 0,
.name = 0,
._unknown_06 = 0,
.wing = .none,
.ship_count = 0,
._unknown_0a = 0,
.first_ship = no_ship,
.formation = no_formation,
};
/// A flight group's wing, as a byte: the player's, which `mission_ship_create` tests for
/// (`0x00457D39`), two more, or none.
pub const Wing = enum(u8) {
player = 0,
second = 1,
third = 2,
none = 0xFF,
_,
pub fn format(wing: Wing, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return layout.formatTag(Wing, wing, writer);
}
};
/// Where its ships start in the flight groups' list, where it has any.
pub fn firstShip(group: FlightGroup) ?u32 {
return if (group.first_ship == no_ship) null else group.first_ship;
}
comptime {
assert(@offsetOf(FlightGroup, "name") == 0x04);
assert(@offsetOf(FlightGroup, "wing") == 0x08);
assert(@offsetOf(FlightGroup, "ship_count") == 0x09);
assert(@offsetOf(FlightGroup, "first_ship") == 0x0C);
assert(@offsetOf(FlightGroup, "formation") == 0x10);
assert(@sizeOf(FlightGroup) == 0x14);
}
};
/// A squad: its object ID, its name and its first membership record.
pub const Squad = extern struct {
object_id: u16,
/// 0 in every shipped mission, as is `_unknown_06`.
_unknown_02: u16,
/// Byte offset into the string pool, such as `(AI)sabretargets`.
name: u16,
_unknown_06: u16,
/// Index of its first record in `squad_members`, or `no_member`. With the next two bytes it
/// makes a reference: the tag of `squad_members` where the squad has members, `none` where it
/// has none, then 0xFF.
first_member: u16,
first_member_tag: Reference.Tag,
_unknown_0b: u8,
/// The `first_member` of a squad with none.