Repository navigation
Expand file tree
/
Copy pathgameobj.zig
More file actions
2954 lines (2744 loc) · 131 KB
/
Copy pathgameobj.zig
File metadata and controls
2954 lines (2744 loc) · 131 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\gameobj.cpp`: the game's live objects, the ships, stations, gates, missiles and
//! markers of a running mission. `object_alloc` (`0x00475DD0`) allocates one.
//!
//! `create_object` (`0x00466C10`) fills a slot of `game_objects`, which it calls the GO array: 400
//! pointers to objects, a mission ship's slot being its index among the mission's ship records.
//! [`create.Objects`](create.zig) is OpenReliant's. Each object embeds the root of a hierarchy of
//! nodes, one for each part of its model.
const std = @import("std");
const assert = std.debug.assert;
const layout = @import("../../formats/layout.zig");
const dte = @import("../../formats/dte.zig");
const shp = @import("../../formats/shp.zig");
const math = @import("../surrender/math.zig");
const Vector = math.Vector;
const camera = @import("camera.zig");
const engine = @import("../../engine.zig");
const aigeneric = @import("aigeneric.zig");
const objects = @import("objects.zig");
const Node = objects.Node;
const Pointer = engine.Pointer;
const create = @import("create.zig");
const additions = @import("additions.zig");
const guns = @import("guns.zig");
const missiles = @import("missiles.zig");
const Random = @import("../random.zig").Random;
const motion = @import("motion.zig");
const input = @import("../input.zig");
const Clock = @import("main.zig").Clock;
const collision = @import("collision.zig");
const hooks = @import("../hooks.zig");
/// A slot of the object array as an object names one, or `none` for no slot, which the game holds
/// as -1.
pub const Slot = enum(i32) {
none = -1,
_,
pub fn of(slot: u16) Slot {
return @fromBackingInt(slot);
}
/// The slot `index` names, or none.
pub fn from(found: ?u16) Slot {
return if (found) |slot| of(slot) else .none;
}
/// The slot it names, or null for none.
pub fn index(slot: Slot) ?u16 {
return if (slot == .none) null else @intCast(@backingInt(slot));
}
};
/// One of the positional voices (`hog_snd.Sound.voices3d`) as an object names the one following
/// it, or `none`, which the game holds as `0xFFFF`.
pub const Voice = enum(u16) {
none = 0xFFFF,
_,
pub fn of(voice: u8) Voice {
return @fromBackingInt(voice);
}
/// The voice it names, or null for none.
pub fn index(voice: Voice) ?u8 {
return if (voice == .none) null else @intCast(@backingInt(voice));
}
};
test Voice {
try std.testing.expectEqual(null, Voice.none.index());
try std.testing.expectEqual(3, Voice.of(3).index());
}
/// Code that acts for the object in a slot: its `motion`, which moves it for one update, such as
/// `motion_forward` (`0x004744C0`), which flies it forward by the flight model, and the routines
/// of its orders.
pub const Routine = engine.Code("void __fastcall (int slot)");
/// Slots in `game_objects`. `create_object` stops the game with a fatal error past the last.
pub const max_objects = 400;
/// Whose side something is on: an object's (`GameObject.side`, four bytes) and a ship type's
/// (`create.ShipCombat.side`, two). The Alliance's types start friendly and the Coalition's
/// hostile; `SetHostile` makes an object one or the other. Two objects on different sides are
/// enemies.
pub fn Side(comptime Tag: type) type {
return enum(Tag) {
/// On the player's side.
friendly = 0,
hostile = 1,
neutral = 2,
_,
};
}
/// How far harm reaches an object (`GameObject.invulnerable`). The first four are the values
/// `SetInvulnerability`'s catalogue entry lists; the game sets the last two itself. Any but `none`
/// keeps its armour whole (`collision.armorDamage`).
pub const Invulnerability = enum(u8) {
none = 0,
/// Only a player's ship can harm it: an ejected pilot, until it is picked up
/// (`order_eject_spin`).
player_can_hit = 1,
/// Nothing harms it: what the Ripper has grabbed, and some types as they are created.
full = 2,
/// Its pilot ejects before it explodes.
eject_before_exploding = 3,
/// **Unknown.** Set as some types are created and as a deathmatch ship respawns. Shots pass its
/// shields to the hull (`guns.bulletHit`).
_unknown_4 = 4,
/// **Unknown.** Its shields are emptied each step (`rechargeShields`), shots pass them to the
/// hull, and a shield generator does not soften a component's hits.
_unknown_5 = 5,
_,
/// Whether it keeps off a hit: always under `full`, and under `player_can_hit` where a player's
/// ship did not deal it.
pub fn protects(invulnerable: Invulnerability, by_player: bool) bool {
return switch (invulnerable) {
.full => true,
.player_can_hit => !by_player,
else => false,
};
}
};
test "Invulnerability.protects" {
try std.testing.expect(Invulnerability.full.protects(true));
try std.testing.expect(Invulnerability.player_can_hit.protects(false));
try std.testing.expect(!Invulnerability.player_can_hit.protects(true));
try std.testing.expect(!Invulnerability.none.protects(false));
}
/// A deathmatch power-up (`GameObject.power_up`): its record of 0x28 bytes in the table at
/// `0x0050C510`, which gives how long it lasts and the routines that start and end it. `0x004B1C00`
/// hands one out, by the weights at `0x005DB4C4` where none is named, and `0x004B18E0` ends it
/// once it runs out. **Unknown:** what most of them do.
pub const PowerUp = enum(i32) {
none = -1,
/// **Unknown.** In a multiplayer game, `0x00412820` and `0x00491520` act differently while the
/// player holds it.
_unknown_5 = 5,
/// The player's throttle goes no higher than half (`player_controls`).
half_throttle = 7,
/// Its shields don't recharge (`rechargeShields`).
no_shield_recharge = 8,
/// The player's controls steer the other way round (`player_controls`).
reversed_controls = 9,
_,
};
/// A value for each quadrant of an object's shields or armour (`collision.Quadrant`), in the order
/// the game keeps them.
pub const Quadrants = extern struct {
left: f32,
right: f32,
fore: f32,
aft: f32,
/// The same value in each.
pub fn all(value: f32) Quadrants {
return .{ .left = value, .right = value, .fore = value, .aft = value };
}
pub fn get(quadrants: Quadrants, quadrant: collision.Quadrant) f32 {
return switch (quadrant) {
inline else => |named| @field(quadrants, @tagName(named)),
};
}
pub fn at(quadrants: *Quadrants, quadrant: collision.Quadrant) *f32 {
return switch (quadrant) {
inline else => |named| &@field(quadrants, @tagName(named)),
};
}
/// Each, in the game's order.
pub fn values(quadrants: Quadrants) [4]f32 {
return .{ quadrants.left, quadrants.right, quadrants.fore, quadrants.aft };
}
/// The least of the four.
pub fn weakest(quadrants: Quadrants) f32 {
const each = quadrants.values();
return @reduce(.Min, @as(@Vector(each.len, f32), each));
}
/// The four added together.
pub fn total(quadrants: Quadrants) f32 {
const each = quadrants.values();
return @reduce(.Add, @as(@Vector(each.len, f32), each));
}
comptime {
for (std.enums.values(collision.Quadrant), @typeInfo(Quadrants).@"struct".field_names) |quadrant, name| {
assert(std.mem.eql(u8, @tagName(quadrant), name));
assert(@offsetOf(Quadrants, name) == @as(usize, @backingInt(quadrant)) * @sizeOf(f32));
}
assert(@sizeOf(Quadrants) == 0x10);
}
};
/// Components an object can list.
pub const max_components = 60;
/// A part of an object that events and scripts can name by its index in `GameObject.components`,
/// such as a capital ship's turret: a trigger's qualifier, a squad member's component.
pub const Component = extern struct {
node: Pointer(Node),
/// Where the node's parent lists it.
slot: Pointer(Pointer(Node)),
/// The invulnerability `SetInvulnerability` gives the component (`protects`); 0 for none.
invulnerable: u16,
_unknown_0a: u16,
/// Whether its invulnerability keeps off a hit, as `component_damage` (`0x004645C0`) compares
/// the halfword: 2 keeps off every hit, and 1 every hit a player's ship did not deal, as
/// `Invulnerability.protects` reads an object's. Any other value keeps off none.
pub fn protects(component: Component, by_player: bool) bool {
return component.invulnerability().protects(by_player);
}
/// Its invulnerability, named as an object's is; `none` for a value too large for one.
pub fn invulnerability(component: Component) Invulnerability {
const invulnerable = std.math.cast(u8, component.invulnerable) orelse return .none;
return @fromBackingInt(invulnerable);
}
comptime {
assert(@sizeOf(Component) == 0x0C);
}
};
/// Missile racks an object can carry: one for each of its missile hardpoints.
pub const max_racks = 20;
/// One of an object's missile racks (`GameObject + 0x158`): what a missile hardpoint of its model
/// holds, a pod of missiles or one missile hung on a rail, and how many are left.
pub const Rack = extern struct {
type: missiles.Type,
_unknown_02: u16 = 0,
/// Where the node of the part that carries the hardpoint lists the hung pod or missile.
slot: Pointer(Pointer(Node)) = .null,
/// Missiles left: a pod's capacity, or 1 for a rail, when fitted; -1 once an empty pod is let
/// fall.
count: i32 = 0,
comptime {
assert(@sizeOf(Rack) == 0x0C);
}
};
/// Flags for the multiplayer code about an object (`GameObject + 0x0C`).
pub const NetworkFlags = packed struct(u32) {
/// Set by `object_move` when the object moves, and by code that places it. The multiplayer
/// code (`0x004BB2D0`) then sends the object's position.
moved: bool,
/// Set by `object_move` when the object turns. The multiplayer code then sends its
/// orientation.
turned: bool,
/// Set on a ship while it runs Scoop Up (`order_scoop_up_init`, cleared by
/// `order_scoop_up_exit`), and on its pod as Scoop Up draws it in (`order_scoop_up`). The
/// multiplayer code's round of updates (`0x004BBEF0`) skips the object.
scooping: bool,
_unknown_3: u29,
};
/// The wing a mission lists an object in, by its flight group (`mission.listPlayerWing`), which
/// the object keeps (`GameObject.wing`).
/// Whether a player's ship is to be sent home for its friendly fire (`GameObject.sent_home`,
/// `+0x678`), which `friendly_fire.sendHome` reads.
pub const SentHome = enum(i32) {
none = 0,
/// It destroyed a friend (`friendly_fire.friendDestroyed`).
friend_destroyed = 1,
/// In a multiplayer game, it destroyed a pilot's pod only a player could harm, which the
/// other players are told of; its carrier aborts the mission.
told = 2,
/// **Unknown:** as `told`, which a multiplayer game's code sets; the mission's ending is then
/// `main.Ending._unknown_7`.
_unknown_3 = 3,
_,
};
pub const Wing = enum(u16) {
/// The player's: the wing status window shows its ships, and an AI pilot in it may eject.
player = 0,
/// Two more wings a mission can list. **Unknown:** what reads them.
second = 1,
third = 2,
/// None, as the object is created.
none = 0xFFFF,
_,
};
/// The game's object types, by their numbers: for a ship, missile, mine or asteroid its record in
/// `shipstats.bin`, and past those what else the game places, markers and nav points among them,
/// which have no stats. Every type up to 255 has a name, from its record's name in `shipstats.bin`
/// where the game's code doesn't single it out. An object holds its type as a `Type`, whose `base`
/// is one of these.
pub const GameType = enum(u32) {
predator = 0x00,
naginata = 0x01,
/// The Grendel, the Wolverine and the Reaper, whose guns fire rounds, which the gunnery display
/// counts.
grendel = 0x02,
crusader = 0x03,
coyote = 0x04,
mirage = 0x05,
tempest = 0x06,
patriot = 0x07,
wolverine = 0x08,
reaper = 0x09,
shroud = 0x0A,
/// The Phoenix (`uspf_phx.shp`), which carries the Nova Cannon.
phoenix = 0x0B,
reliant = 0x0C,
/// The Yamato (`yamato.shp`), the carrier that takes the place of the ship the player launched
/// from once that ship explodes.
yamato = 0x0D,
alsace = 0x0E,
kestrel = 0x0F,
falco = 0x10,
/// The Victorious (`victorious.shp`), and the carriers after it whose ships launch from a bay
/// as its do: the Endeavour, the Mitchell and the Bremen.
victorious = 0x11,
endeavour = 0x12,
mitchell = 0x13,
bremen = 0x14,
arc_ship = 0x15,
/// The Ulysses (`ulysses.shp`), whose escape pods launch even as it is lost.
ulysses = 0x16,
sai = 0x17,
/// The Nanny (`nanny.shp`).
nanny = 0x18,
galahad = 0x19,
hades = 0x1A,
mule = 0x1B,
lonestar = 0x1C,
/// The limpet car (`limpet_t_car.shp`).
limpet_car = 0x1D,
/// The Prowler (`us_prowler.shp`).
prowler = 0x1E,
/// The Ripper (`ripper_2.shp`), which carries cargo pods in its beams
/// ([`airipper.zig`](airipper.zig)).
ripper = 0x1F,
lueneburg = 0x20,
/// The Mammoth (`a_mammoth.shp`), a freighter the Ripper loads with cargo pods.
mammoth = 0x21,
intruder = 0x22,
sentry = 0x23,
goodwill = 0x24,
/// The Stork (`stork.shp`).
stork = 0x25,
sky_hawk = 0x26,
haidar = 0x27,
karak = 0x28,
salin = 0x29,
azan = 0x2A,
sabre = 0x2B,
lagg = 0x2C,
kamov = 0x2D,
saracen = 0x2E,
salamander = 0x2F,
scimitar = 0x30,
basilisk = 0x31,
kossac = 0x32,
yao = 0x33,
/// The Ramases (`ramases.shp`).
ramases = 0x34,
kozlov = 0x35,
morzov = 0x36,
/// The Badanov (`cs_badanov.shp`), the smallest of the Coalition's capital ships.
badanov = 0x37,
/// The Pukov (`pukov.shp`).
pukov = 0x38,
xia = 0x39,
riza = 0x3A,
cyclops = 0x3B,
/// The Kurgan (`rus_kurgan.shp`).
kurgan = 0x3C,
/// The Sharov (`sharov.shp`).
sharov = 0x3D,
/// The Gurevich (`rmc_gurevich.shp`).
gurevich = 0x3E,
berijev = 0x3F,
kirov = 0x40,
loki = 0x41,
kalan_shuttle = 0x42,
/// Capital ships (`saladin.shp`, `stalag.shp`, `antanov.shp`, `kronstadt.shp`, `boridin.shp`).
saladin = 0x43,
/// The Dark Reign (`darkreign.shp`).
darkreign = 0x44,
stalag = 0x45,
antanov = 0x46,
kronstadt = 0x47,
boridin = 0x48,
/// The Russian troop car (`rus_troopcar.shp`).
troop_car = 0x49,
torpedo = 0x4A,
/// The Ulysses' back (`ulysses2.shp`), which it throws off as its top goes
/// (`explode/ulysses.zig`).
ulysses_back = 0x4B,
/// The Ulysses' fin (`ulysses3.shp`), which it throws off as its fin or its top goes.
ulysses_fin = 0x4C,
/// An escape pod (`uly_escape.shp`).
escape_pod = 0x4D,
/// The first of ten pieces of debris an explosion throws out (`deb_1.shp` to `deb_10.shp`).
debris = 0x4E,
debris_1 = 0x4F,
debris_2 = 0x50,
debris_3 = 0x51,
debris_4 = 0x52,
debris_5 = 0x53,
debris_6 = 0x54,
debris_7 = 0x55,
debris_8 = 0x56,
debris_9 = 0x57,
/// The first of four bodies an explosion throws out (`rus_man1.shp` to `rus_man4.shp`).
crewman = 0x58,
crewman_1 = 0x59,
crewman_2 = 0x5A,
crewman_3 = 0x5B,
/// The Russian torpedo (`rus_torp.shp`).
russian_torpedo = 0x5C,
mammoth_back = 0x5D,
baxter = 0x5E,
/// The planets with an atmosphere (`create.atmosphere`): Neptune (`neptune_hi_1.shp`), Uranus
/// (`uranus_hi_1.shp`), Jupiter (`jupiter_hi_1.shp`) and Venus (`venus1_hi_1.shp`), and their
/// models of less detail below.
neptune_hi = 0x5F,
triton_hi = 0x60,
uranus_hi = 0x61,
saturn_hi = 0x62,
titan_hi = 0x63,
jupiter_hi = 0x64,
europa_hi = 0x65,
ganymede_hi = 0x66,
io_hi = 0x67,
calisto_hi = 0x68,
venus_hi = 0x69,
earth = 0x6A,
kronstadt_wreck = 0x6B,
planet_placeholder_4 = 0x6C,
/// The Coalition's gates: the prototype (`coalprotogate.shp`) and the advanced one
/// (`coaladvgate.shp`).
proto_gate = 0x6D,
advanced_gate = 0x6E,
/// The proximity mine (`mine_prox.shp`).
proximity_mine = 0x6F,
/// A black box (`black_box.shp`).
black_box = 0x70,
satellite = 0x71,
/// The wrecks a capital ship leaves as it splits (`explode.split`), which burn
/// (`create.wreckMade`): the Mammoth's front and back (`mamdest.shp`, `mamdest2.shp`), the
/// Badanov's back and front (`cs_baddead.shp`, `cs_baddead2.shp`), and the Kurgan's
/// (`rus_kurgan_dest.shp`).
mammoth_wreck_front = 0x72,
mammoth_wreck_back = 0x73,
proto_gate_panel = 0x74,
badanov_wreck_back = 0x75,
badanov_wreck_front = 0x76,
kurgan_wreck = 0x77,
/// The Krasnaya (`krasnaya.shp`).
krasnaya = 0x78,
asteroid_1 = 0x79,
asteroid_2 = 0x7A,
asteroid_3 = 0x7B,
asteroid_4 = 0x7C,
asteroid_5 = 0x7D,
asteroid_6 = 0x7E,
asteroid_7 = 0x7F,
research_station = 0x80,
/// The Latov (`latov.shp`).
latov = 0x81,
gurevich_wreck_back = 0x82,
training_hoop = 0x83,
/// The Czar, docked (`czar_docked.shp`).
czar_docked = 0x84,
turret_asteroid_1 = 0x85,
turret_asteroid_2 = 0x86,
turret_asteroid_3 = 0x87,
turret_asteroid_4 = 0x88,
turret_asteroid_5 = 0x89,
turret_asteroid_6 = 0x8A,
turret_asteroid_7 = 0x8B,
powerup_random = 0x8C,
dm_beacon_gate = 0x8D,
/// A deathmatch beacon (`DMBeacon.shp`).
dm_beacon = 0x8E,
pukov_wreck_back = 0x8F,
/// Another escape pod (`ber_escape.shp`).
other_escape_pod = 0x90,
/// A cargo pod (`us_cargo.shp`), which the Ripper carries.
cargo_pod = 0x91,
other_cargo_pod = 0x92,
grazer_gun_sat = 0x93,
archer_missile_sat = 0x94,
/// The Kafelnikof (`kafelnikof.shp`).
kafelnikof = 0x95,
reliant_wreck_back = 0x96,
stalag_doors = 0x97,
stalag_duct_cover = 0x98,
/// The Yakob Shuttle, a support ship, which the StarLancer trial's executable makes a fighter
/// for its pirates.
yakob_shuttle = 0x99,
/// The Krasny (`krasny.shp`), whose ships launch as the Badanov's do.
krasny = 0x9A,
/// The Varyag (`varyag.shp`), and a second Ramases and Mitchell, of the same models.
varyag = 0x9B,
other_ramases = 0x9C,
fort_carter = 0x9D,
koenig = 0x9E,
washington = 0x9F,
other_mitchell = 0xA0,
ufelsky = 0xA1,
kresta = 0xA2,
krasnaya_left_arm = 0xA3,
krasnaya_right_arm = 0xA4,
/// The rogue base (`rogue_base.shp`).
rogue_base = 0xA5,
kronstadt_arm = 0xA6,
boridin_gun_dome = 0xA7,
/// The part of the Boridin that breaks away (`boridin breakaway.shp`).
boridin_breakaway = 0xA8,
darkreign_wreck_hat = 0xA9,
yamato_wreck_back = 0xAA,
maintenance_droid = 0xAB,
maintenance_astronaut = 0xAC,
rogue_base_wreck_top = 0xAD,
tank = 0xAE,
training_target = 0xAF,
/// The Zakov (`zakov.shp`).
zakov = 0xB0,
/// The Turret Flak's shell (`shell.shp`).
shell = 0xB1,
/// The first of five chunks of rock (`rockchunk00.SHP` to `rockchunk04.SHP`).
rock_chunk = 0xB2,
rock_chunk_tiny_1 = 0xB3,
rock_chunk_tiny_2 = 0xB4,
rock_chunk_tiny_3 = 0xB5,
rock_chunk_tiny_4 = 0xB6,
latov_wreck_1 = 0xB7,
latov_wreck_2 = 0xB8,
czar = 0xB9,
zakov_wreck_back = 0xBA,
sharov_wreck_back = 0xBB,
/// The limpet pod, which rides on a hull.
limpet_pod = 0xBC,
stalag_wreck_1 = 0xBD,
stalag_wreck_2 = 0xBE,
saladin_wreck = 0xBF,
gegarin = 0xC0,
krelov = 0xC1,
/// The Kiev (`kiev.shp`).
kiev = 0xC2,
kiev_wreck_back = 0xC3,
baxter_wreck_top = 0xC4,
victorious_wreck_front = 0xC5,
mitchell_wreck_back = 0xC6,
fort_bear = 0xC7,
fort_sherman = 0xC8,
/// The planets with an atmosphere in less detail: `neptune_lo_1.shp`, `uranus_lo_1.shp`,
/// `jupiter_lo_1.shp` and `venus1_lo_1.shp`.
neptune_lo = 0xC9,
triton_lo = 0xCA,
uranus_lo = 0xCB,
saturn_lo = 0xCC,
titan_lo = 0xCD,
jupiter_lo = 0xCE,
europa_lo = 0xCF,
ganymede_lo = 0xD0,
io_lo = 0xD1,
calisto_lo = 0xD2,
venus_lo = 0xD3,
/// The Yamato's launch tube (`yam_tube.shp`), shown as the player's hangar (`0x00419544`).
yamato_hangar = 0xD4,
/// The Yamato's landing bay (`yamhanger.shp`), the cutaway the Yamato's landing shows the
/// player's ship come down in (`land_yamato_init`, `0x0040EB60`).
yamato_landing_bay = 0xD5,
/// The Reliant's hangar (`reliant_hang.shp`): the cutaway the Reliant's launch shows the
/// player's ship in.
reliant_hangar = 0xD6,
/// A comms relay (`comms relay.shp`).
comms_relay = 0xD7,
saladin_link = 0xD8,
varyag_wreck_front = 0xD9,
churchill = 0xDA,
bokov = 0xDB,
bulatov = 0xDC,
shinnik = 0xDD,
kovtun = 0xDE,
/// Escape pods again, of the same models as `escape_pod` and `other_escape_pod`.
late_escape_pod = 0xDF,
other_late_escape_pod = 0xE0,
/// A fuel pod (`fuel_pod1.shp`), which a jump that clears its way marks jumping wherever it is
/// (`jump.markJumping`).
fuel_pod = 0xE1,
yevstafiy = 0xE2,
mammoth_gulliver = 0xE3,
mammoth_santa_maria = 0xE4,
mammoth_rosario = 0xE5,
mammoth_larsons_pride = 0xE6,
mammoth_brittania = 0xE7,
mammoth_sierra_madre = 0xE8,
mammoth_mayan_gold = 0xE9,
mammoth_dawn_chorus = 0xEA,
mammoth_calysto = 0xEB,
mammoth_sundown = 0xEC,
mammoth_krenna = 0xED,
mammoth_seabound = 0xEE,
mammoth_crimson_sky = 0xEF,
asteroid_with_hole_1 = 0xF0,
asteroid_with_hole_2 = 0xF1,
asteroid_with_hole_3 = 0xF2,
asteroid_with_hole_4 = 0xF3,
/// The second set of the player's ships, the same twelve with the `t_` twins of their models
/// (`player_twins_first`).
t_predator = 0xF4,
t_naginata = 0xF5,
t_grendel = 0xF6,
t_crusader = 0xF7,
t_coyote = 0xF8,
t_mirage = 0xF9,
t_tempest = 0xFA,
t_patriot = 0xFB,
t_wolverine = 0xFC,
t_reaper = 0xFD,
t_shroud = 0xFE,
/// A second Phoenix (`t_uspf_phx.shp`), the last type of the table.
t_phoenix = 0xFF,
/// The markers `backdrop_place` reads a mission's sun and nebula from.
sun_marker = 0x3DC,
nebula_marker = 0x3DD,
/// The deathmatch maps' markers, named after the objects their designers placed: the arena's
/// centre (`dm_sphere_centre`), the objects named `beacon1` to `beacon6`, beside the beacons
/// themselves (`dm_beacon`), the players' spawn points and the power-ups'. The game singles out
/// the players' spawn points alone, which `multiplayer_list_spawns` (`0x004AF5C0`) lists as a
/// multiplayer game starts; it sizes the arena round every object (`arena_frame`).
dm_sphere_centre = 0x3DF,
beacon_marker = 0x3E0,
spawn_player = 0x3E1,
spawn_powerup = 0x3E2,
/// What `mission_ship_create` makes of a mission's nav points and markers, and the mission's
/// start of the camera's marker.
marker = 1000,
/// What a slot holds until `create_object` fills it, and what a destroyed object becomes.
stand_in = 1001,
_,
/// What is made of rock: the asteroids, `ast_1.shp` to `ast_7.shp`; the turret asteroids,
/// `turast_1.shp` to `turast_7.shp`; and the holes, `ast_hole1.shp` to `ast_hole4.shp`.
pub const Rock = enum { asteroid, turret, hole };
const rocks: std.EnumArray(Rock, [2]GameType) = .init(.{
.asteroid = .{ .asteroid_1, .asteroid_7 },
.turret = .{ .turret_asteroid_1, .turret_asteroid_7 },
.hole = .{ .asteroid_with_hole_1, .asteroid_with_hole_4 },
});
comptime {
// The numbers are the game's own, so the models they stand for say which types they are.
const models = [_]struct { GameType, []const u8 }{
.{ .predator, "uslf_prd.shp" },
.{ .phoenix, "uspf_phx.shp" },
.{ .t_phoenix, "t_uspf_phx.shp" },
.{ .reliant, "reliant.shp" },
.{ .nanny, "nanny.shp" },
.{ .victorious, "victorious.shp" },
.{ .darkreign, "darkreign.shp" },
.{ .mammoth_wreck_front, "mamdest.shp" },
.{ .mammoth_wreck_back, "mamdest2.shp" },
.{ .badanov_wreck_back, "cs_baddead.shp" },
.{ .badanov_wreck_front, "cs_baddead2.shp" },
.{ .kurgan_wreck, "rus_kurgan_dest.shp" },
.{ .latov, "latov.shp" },
.{ .czar_docked, "czar_docked.shp" },
.{ .kafelnikof, "kafelnikof.shp" },
.{ .prowler, "us_prowler.shp" },
.{ .stalag, "stalag.shp" },
.{ .antanov, "antanov.shp" },
.{ .limpet_car, "limpet_t_car.shp" },
.{ .ripper, "ripper_2.shp" },
.{ .mammoth, "a_mammoth.shp" },
.{ .sharov, "sharov.shp" },
.{ .cargo_pod, "us_cargo.shp" },
.{ .sabre, "rus_sabre.shp" },
.{ .kamov, "rus_kamov.shp" },
.{ .scimitar, "scimitar.shp" },
.{ .badanov, "cs_badanov.shp" },
.{ .kurgan, "rus_kurgan.shp" },
.{ .gurevich, "rmc_gurevich.shp" },
.{ .troop_car, "rus_troopcar.shp" },
.{ .saladin, "saladin.shp" },
.{ .kronstadt, "kronstadt.shp" },
.{ .boridin, "boridin.shp" },
.{ .proto_gate, "coalprotogate.shp" },
.{ .advanced_gate, "coaladvgate.shp" },
.{ .boridin_breakaway, "boridin breakaway.shp" },
.{ .torpedo, "torpedo.shp" },
.{ .russian_torpedo, "rus_torp.shp" },
.{ .proximity_mine, "mine_prox.shp" },
.{ .black_box, "black_box.shp" },
.{ .dm_beacon, "DMBeacon.shp" },
.{ .comms_relay, "comms relay.shp" },
.{ .satellite, "stork_sat.shp" },
.{ .escape_pod, "uly_escape.shp" },
.{ .other_escape_pod, "ber_escape.shp" },
.{ .late_escape_pod, "uly_escape.shp" },
.{ .other_late_escape_pod, "ber_escape.shp" },
.{ .debris, "deb_1.shp" },
.{ .debris_9, "deb_10.shp" },
.{ .crewman, "rus_man1.shp" },
.{ .crewman_3, "rus_man4.shp" },
.{ .rock_chunk, "rockchunk00.SHP" },
.{ .rock_chunk_tiny_4, "rockchunk04.SHP" },
.{ .shell, "shell.shp" },
.{ .limpet_pod, "limpet_pod.shp" },
.{ .asteroid_1, "ast_1.shp" },
.{ .asteroid_7, "ast_7.shp" },
.{ .turret_asteroid_1, "turast_1.shp" },
.{ .turret_asteroid_7, "turast_7.shp" },
.{ .asteroid_with_hole_1, "ast_hole1.shp" },
.{ .asteroid_with_hole_4, "ast_hole4.shp" },
};
for (models) |named| assert(std.mem.eql(u8, create.models.ship_types[named[0].number()].model.?, named[1]));
}
pub fn number(object_type: GameType) u32 {
return @backingInt(object_type);
}
/// Where the second set of the player's ship types starts: the types from `t_predator` to
/// `t_phoenix`, whose models are the first twelve's `t_` twins, are the same twelve ships.
pub const player_twins_first = GameType.t_predator.number();
/// The type it stands for among the player's ships: one of the second set, from
/// `player_twins_first`, stands for the first set's in the same place, and any other for itself.
pub fn untwinned(object_type: GameType) GameType {
const at = object_type.number();
return @fromBackingInt(if (at >= player_twins_first) at - player_twins_first else at);
}
/// How many of the player's ship types have twins: the first twelve, from 0.
pub const player_twins = 0x100 - player_twins_first;
/// Whether the player can fly a ship of this type, a twin as the ship it twins: one of the
/// first twelve, the Predator to the Phoenix, which the start fits a cockpit to
/// (`main.player_ships`).
pub fn playerFlies(object_type: GameType) bool {
return object_type.untwinned().number() < player_twins;
}
/// Its twin among the second set of the player's ship types, for one of the first set's; null
/// for any other.
pub fn twin(object_type: GameType) ?GameType {
const at = object_type.number();
return if (at < player_twins) @fromBackingInt(at + player_twins_first) else null;
}
/// Whether it is a Phoenix, the ship that carries the Nova Cannon, or its twin.
pub fn carriesNova(object_type: GameType) bool {
return object_type == .phoenix or object_type == .t_phoenix;
}
/// What rock it is, if any.
pub fn rock(object_type: GameType) ?Rock {
const n = object_type.number();
for (std.enums.values(Rock)) |kind| {
const range = rocks.get(kind);
if (n >= range[0].number() and n <= range[1].number()) return kind;
}
return null;
}
pub fn isAsteroid(object_type: GameType) bool {
return object_type.rock() == .asteroid;
}
/// Whether it is one of the torpedoes, the Allies' or the Russians', by type (a ship's combat
/// class may also be `.torpedo`).
pub fn isTorpedo(object_type: GameType) bool {
return switch (object_type) {
.torpedo, .russian_torpedo => true,
else => false,
};
}
/// Asteroid `n`, from `ast_1.shp`, round and round the seven.
pub fn asteroid(n: usize) GameType {
const range = rocks.get(.asteroid);
const first = range[0].number();
return @fromBackingInt(@intCast(first + n % (range[1].number() - first + 1)));
}
/// The child of the root the AI aims at on an object of this type, where it aims at a part
/// rather than the whole (`0x004018F0`): the Saladin's and the troop car's twenty-first, the
/// Kronstadt's eighteenth, the Boridin's twentieth.
pub fn aimedChild(object_type: GameType) ?usize {
return switch (object_type) {
.saladin, .troop_car => 0x14,
.kronstadt => 0x11,
.boridin => 0x13,
else => null,
};
}
pub fn format(object_type: GameType, writer: *std.Io.Writer) std.Io.Writer.Error!void {
return layout.formatTagAs(GameType, object_type, "type", writer);
}
};
/// An object's type by its number (`GameObject.type`): one of the game's (`GameType`), or one a mod
/// adds past them (`additions.ships`). The code that singles out a type asks for its base (`base`), so
/// that a type a mod adds acts as the game's type it is based on.
pub const Type = enum(u32) {
/// The name scripts know these values by, and the names they know: the game's types', and the
/// qualified names of the types the mods add (`scriptName`).
pub const script_name = "ShipType";
pub const Named = GameType;
_,
/// The game's type `game`.
pub fn of(game: GameType) Type {
return @fromBackingInt(@backingInt(game));
}
/// The game's type it acts as: itself for one of the game's, and for one a mod adds, the type
/// it is based on.
pub fn base(object_type: Type) GameType {
const from_mod = additions.ships.get(object_type.number()) orelse return @fromBackingInt(object_type.number());
return from_mod.base;
}
/// The type a mod adds that it is, if it is one.
pub fn added(object_type: Type) ?*const additions.ships.Added {
return additions.ships.get(object_type.number());
}
/// The game's type it is; null for one a mod adds.
pub fn gameType(object_type: Type) ?GameType {
if (object_type.added() != null) return null;
return @fromBackingInt(object_type.number());
}
pub fn number(object_type: Type) u32 {
return @backingInt(object_type);
}
/// Whether it has a record in the ship tables: one of the game's 256, or one a mod adds.
pub fn hasStats(object_type: Type) bool {
return object_type.number() < create.ship_type_count or object_type.added() != null;
}
/// The type it stands for among the player's ships (`GameType.untwinned`); a type a mod adds
/// stands for itself.
pub fn untwinned(object_type: Type) Type {
if (object_type.added() != null) return object_type;
return .of(object_type.base().untwinned());
}
/// Its twin among the second set of the player's ship types (`GameType.twin`); a type a mod adds
/// has none.
pub fn twin(object_type: Type) ?Type {
if (object_type.added() != null) return null;
return .of(object_type.base().twin() orelse return null);
}
/// Asteroid `n` (`GameType.asteroid`).
pub fn asteroid(n: usize) Type {
return .of(GameType.asteroid(n));
}
/// The name scripts know it by, if it has one: its qualified name for a type a mod adds.
pub fn scriptName(object_type: Type) ?[]const u8 {
if (object_type.added()) |from_mod| return from_mod.name;
return layout.tagName(GameType, object_type.base());
}
/// The type scripts name `text`, if there is one.
pub fn fromScriptName(text: []const u8) ?Type {
if (additions.ships.find(text)) |found| return @fromBackingInt(found);
return .of(std.meta.stringToEnum(GameType, text) orelse return null);
}
pub fn format(object_type: Type, writer: *std.Io.Writer) std.Io.Writer.Error!void {
if (object_type.added()) |from_mod| return writer.writeAll(from_mod.name);
return object_type.base().format(writer);
}
};
/// A list of objects the avoidance code steers round (`avoidance_scan`), `max_avoided` at most.
pub const Avoided = extern struct {
count: i32,
slots: [max_avoided]i32,
pub fn list(avoided: *const Avoided) []const i32 {
return avoided.slots[0..@intCast(std.math.clamp(avoided.count, 0, max_avoided))];
}
/// Adds `slot`, where there is room.
pub fn add(avoided: *Avoided, slot: u16) void {
if (avoided.count >= max_avoided) return;
avoided.slots[@intCast(avoided.count)] = slot;
avoided.count += 1;
}
comptime {
assert(@sizeOf(Avoided) == 0x2C);
}
};
pub const max_avoided = 10;
/// A live object (`gameobj.cpp`), allocated at `0x00475DD0`.
pub const GameObject = extern struct {
type: Type,
/// Its slot in `game_objects`.
index: u32,
flags: Flags,
network: NetworkFlags,
combat: Pointer(create.ShipCombat),
flight: Pointer(create.FlightModel),
/// The type's model, as loaded.
model: Pointer(anyopaque),
/// Data kept for the ship type and shared by its objects.
type_data: Pointer(anyopaque),
/// The renderer's object for it, or null.
render: Pointer(anyopaque),
/// The shape the wing status window shows it by, which the mission's start gives each ship of
/// the player's wing by its type (`main.startWing`); 0 for none, as created (`wingIcon`).
wing_icon: u16,
_unknown_26: u16,
/// The root of its model hierarchy.
root: Node,
/// **Unknown.** 1.0 when created; the collision sweep multiplies `radius` by it.
/// How far off it stays worth drawing, over the distance its radius alone would give it
/// (`node_draw`). `object_alloc` and `create_object` both leave it at 1, so nothing in the
/// shipped game sees further or less far than its size says.
visibility: f32,
/// Its guns: `gun_count` records of 0x60 bytes at `guns`, which `create_object` walks to set up
/// the gun groups (#131).
gun_count: i16,
_unknown_132: u16,
guns: Pointer(anyopaque),
/// The count each gun keeps of the steps it has been firing (`guns.Fitted.sounded`), which
/// the game allocates with the guns, one word each.
_unknown_138: u32,
/// The rounds its guns have left: `ShipCombat.rounds` when created, one for each shot of a gun
/// of kind `rounds` (`guns.step`). The gunnery window shows them.
rounds: i32,
/// The guns' charge: `ShipCombat.gun_energy` when created, which the guns recharge to
/// (`0x00477114`). The display's right arc shows it against that.
gun_charge: f32,
/// How its guns fire, which the gun keys set.
gun_mode: GunMode,
_unknown_146: u16,
/// How far a gun that charges up before it fires has charged, from 0 to 1, which scales its
/// damage. Only gun type 11, the Nova Cannon, uses it. Its guns' charge does not recharge
/// while it is not zero (`guns.step`).
nova_charge: f32,
/// Which side of a gun group fires next, 0 or 1, while the ship fires one group out of step
/// (`guns.step`).
gun_turn: guns.GroupSide,
/// Its missile racks, `rack_count` of them, which `create_object` fits from its missile
/// hardpoints and each launch takes from.
rack_count: i16,
component_count: i16,
/// For an object that lists components, how many part nodes it has, its model's and those of
/// the models mounted on it (`object_number_parts`).
part_count: i16,
_unknown_156: [2]u8,
racks: [max_racks]Rack,
/// The parts of its model whose flags mark them as components, in the order `0x00468760`
/// finds them: each node's marked children, then each child's in turn.
components: [max_components]Component,
/// For an object that lists components, its part nodes by their numbers
/// (`object_number_parts`), which a shot's candidates name its parts by; null for any other.
part_nodes: Pointer(Pointer(objects.Node)),
/// How many knocks, from collisions and explosions, the object has taken since its last move
/// (`knock`). The next `object_move` applies them in place of the object's own motion
/// (`applyKnocks`).
knocks: u32,
/// The sum of its parts' masses (`object_recentre`).
mass: f32,
/// How far `object_recentre` moved the object's origin to its centre of mass
/// (`objects.Model.centre`).
centre: shp.Vec3,
/// The sum of the forces of the knocks since the last move.
impulse: shp.Vec3,
/// The sum of each knock's force × lever, in world coordinates: the opposite of the torque.
angular_impulse: shp.Vec3,
/// The inverse of the object's inertia tensor, which `object_recentre` builds from its parts
/// (`object_bounds`) and inverts (`0x004AD9F0`). `applyKnocks` turns the angular impulse by it.
angular_response: [9]f32,
/// The turn applied to its orientation each update, which `object_steer` builds from the
/// angular rates.
rotation: [9]f32,