Repository navigation
Expand file tree
/
Copy pathmotion.zig
More file actions
733 lines (672 loc) · 36.2 KB
/
Copy pathmotion.zig
File metadata and controls
733 lines (672 loc) · 36.2 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
//! The code that moves live objects, which the binary names no file for: `object_move`
//! (`0x00473FF0`) and the flight model the motion routines run, `object_fly` (`0x004742E0`) with
//! `object_steer` (`0x00474150`). **Unknown:** its source file. The code lies after `explode.cpp`'s
//! and before `gameflow.cpp`'s, and nothing in it asserts. docs/engine/objects.md describes the
//! motion.
const std = @import("std");
const math = @import("../surrender/math.zig");
const ai = @import("ai.zig");
const camera = @import("camera.zig");
const create = @import("create.zig");
const gameobj = @import("gameobj.zig");
const jump = @import("jump.zig");
const follow = @import("ai/follow.zig");
const aidock = @import("aidock.zig");
const objects = @import("objects.zig");
const Clock = @import("main.zig").Clock;
const GameObject = gameobj.GameObject;
/// The routine `GameObject.motion` points at, which moves it for one update. `create_object` gives
/// every object `motion_forward`. The orders select eight more: those of the pilot's ejection, of
/// the launches, of the jumps and of the paths ships follow, which docs/engine/objects.md lists.
pub const Motion = enum {
/// `motion_forward` (`0x004744C0`): the flight model thrusting ahead.
forward,
/// `motion_backward` (`0x004744D0`): the flight model thrusting astern.
backward,
/// `motion_downward` (`0x004744E0`), which the Reliant's launch gives a ship as it drops out
/// of its bay: the plain flight model (`plain`) along the ship's own Y axis, which points below
/// it, its last throttle nothing.
downward,
/// `motion_plain` (`0x00474570`), which a torpedo's launch gives it as it leaves its tube: the
/// plain flight model along the ship's nose, save that the Ripper flies it by its own flight
/// model whatever its class.
plain,
/// `motion_brake` (`0x00474610`), which Eject gives the pilot's pod once it is clear of the
/// ship: it slows to `brake_share` of its velocity each update, its throttle nothing.
brake,
/// `motion_drift` (`0x00474B00`), which the ejection gives the ship it leaves: it slows to
/// `drift_share` of its velocity each update.
drift,
/// `motion_jump_out` (`0x00474640`), which Jump Out gives a ship as it goes
/// ([`jump.zig`](jump.zig)): it moves to a point between where it stood and where it goes
/// (`jump.State.from`, `to`), eased in by the square of how far through `jump.going_ticks` it
/// is since it went, as the mission's ticks count; turning nothing, its last throttle nothing.
jump_out,
/// `motion_jump_in` (`0x004746D0`), which Jump In gives a ship as it arrives: it flies along its
/// nose at `jump_in_speed`, or `jump_in_speed_components` for one that lists components, less
/// `jump_in_slowing` of that for each tick since the jump began, and at its cruise speed at
/// least; turning nothing, its last throttle nothing.
jump_in,
/// `motion_follow` (`0x00474770`), which Follow Curve and Dock give a ship: it turns toward the
/// point its order's path gives (`Following`), and moves straight to it, no faster than its top
/// speed times its order's limit (`Follower.limit`), its throttle the share of its top speed it
/// moves at. Turned toward the point within `aligned`, it rolls to stand as the way up the
/// path gives, where it gives one.
follow,
/// `motion_follow_backwards` (`0x00474930`): `follow`, its turns the other way round, for a
/// ship flying tail first.
follow_backwards,
/// Moves `object` for one update, by `flight` in `view` where it flies; a jump by its order's
/// state and the clock, and a path by the order's own (`By`).
pub fn run(motion: Motion, object: *GameObject, flight: Flight, view: camera.View, by: By) void {
const jumping = by.jumping;
switch (motion) {
.forward => fly(object, flight.own, view, ahead),
.backward => fly(object, flight.own, view, astern),
.downward => {
plain(object, flight.fighter orelse flight.own, .y);
object.last_throttle = 0;
},
.plain => {
plain(object, if (object.type.base() == .ripper) flight.own else flight.fighter orelse flight.own, .z);
object.last_throttle = object.throttle;
},
.brake => {
slow(object, brake_share);
object.last_throttle = 0;
},
.drift => slow(object, drift_share),
.jump_out => if (jumping) |on| {
const through = @as(f32, @floatFromInt(on.clock.mission_ticks -% on.state.since)) * jump_out_pace;
const at = math.lerp(on.state.from.vector(), on.state.to.vector(), through * through);
object.rotation = math.identity;
object.velocity = .of(at - object.position());
object.last_throttle = 0;
},
.jump_in => if (jumping) |on| {
const full = if (object.flags.components) jump_in_speed_components else jump_in_speed;
const since = @as(f32, @floatFromInt(on.clock.frame_start)) - @as(f32, @floatFromInt(on.state.since));
const speed = @max(full - since * jump_in_slowing * full, ai.cruiseSpeed(object, flight.own, view));
object.velocity = .of(math.forward(object.root.orientation) * @as(math.Vector, @splat(speed)));
object.rotation = math.identity;
object.last_throttle = 0;
},
.follow, .follow_backwards => |way| if (by.following) |on| {
followPath(object, flight.own, on.way(), on.limit(), way == .follow_backwards);
},
}
}
};
/// What an order's motion goes by besides the object, which the game's motions read from the
/// order's state and from globals.
pub const By = struct {
jumping: ?Jumping = null,
following: ?Following = null,
};
/// What the jumps' motions go by besides the object: its jump's state (`GameObject.order_state`)
/// and the mission's clock.
pub const Jumping = struct {
state: *const jump.State,
clock: *const Clock,
};
/// What `motion_follow` goes by besides the object: the world its order's path is worked out in,
/// and the slot the object is in.
pub const Following = struct {
world: gameobj.World,
index: u16,
/// Where the order's path has the ship go this update, which moves the path on: the routine its
/// state names (`Follower.path`), which the game calls through the state's first word.
fn way(following: Following) Way {
const slot = &following.world.objects.slots[following.index];
return switch (slot.state.follower.path) {
.curve => follow.curveWay(following.world, following.index),
.curve_backwards => follow.backwardsWay(following.world, following.index),
.dock => aidock.way(following.world, following.index),
_ => .{ .point = slot.object.position() },
};
}
fn limit(following: Following) f32 {
return following.world.objects.slots[following.index].state.follower.limit;
}
};
/// What an order that flies a ship by `motion_follow` holds at the start of its state
/// (`GameObject.order_state`): the path the motion follows (`+0x00`), where the game holds the
/// routine that gives the path's point, and the fastest the ship moves, as a share of its top
/// speed (`+0x04`).
pub const Follower = extern struct {
path: Path,
limit: f32,
pub const Path = enum(u32) {
/// Ship Follow Curve's (`follow_curve_way`, `0x00403200`).
curve = 1,
/// Ship Follow Curve Backwards' (`follow_back_way`, `0x00403600`).
curve_backwards = 2,
/// Dock's, as a ship slides into its berth (`dock_way`, `0x00406F20`).
dock = 3,
_,
};
};
/// Where a path has a ship go: its point, and the way up it has the ship stand, nothing for none.
pub const Way = struct {
point: math.Vector,
up: math.Vector = @splat(0),
};
/// How near the point the motion's turn must bring the ship, in radians each way, before it rolls
/// to stand as the path's way up (`0x004DC518`).
const aligned: f32 = 0.01;
/// `motion_follow`'s work for `object`, flown by `flight`, toward `way`, no faster than `limit` of
/// its top speed: its turning inputs from the angles between its orientation and the look at the
/// point, damped by its turn rates (`ai.rate_damping`), turned about for `backwards`; the steering
/// (`steer`); then its velocity straight to the point, held to `limit` of its top speed.
///
/// **Improvement:** the angles come from `std.math` rather than the engine's table (`sr_atan2`).
fn followPath(object: *GameObject, flight: *const create.FlightModel, way: Way, limit: f32, backwards: bool) void {
const at = object.position();
const look = math.lookAt(way.point - at);
var turn = math.angles(math.product(math.transpose(object.root.orientation), look));
if (@reduce(.Or, way.up != @as(math.Vector, @splat(0))) and @abs(turn[0]) < aligned and @abs(turn[1]) < aligned) {
const up = math.transformTransposed(object.root.orientation, way.up);
turn[2] = -std.math.atan2(up[0], up[1]);
}
if (backwards) turn = -turn;
object.pitch_input = (turn[0] - ai.rate_damping * object.pitch_rate) * ai.input_per_radian;
object.yaw_input = (turn[1] - ai.rate_damping * object.yaw_rate) * ai.input_per_radian;
object.roll_input = (turn[2] - ai.rate_damping * object.roll_rate) * ai.input_per_radian;
steer(object, flight, false);
var velocity = way.point - at;
object.throttle = math.length(velocity) / flight.max_speed;
if (object.throttle > limit) {
velocity = math.normalize(velocity) * @as(math.Vector, @splat(flight.max_speed * limit));
object.throttle = limit;
}
object.velocity = .of(velocity);
object.last_throttle = object.throttle;
}
/// How far through its going Jump Out's motion is for each of the mission's ticks, the share of
/// `jump.going_ticks` a tick (`0x004DC7B0`).
const jump_out_pace: f32 = 0.004;
/// How fast Jump In's motion flies a ship in, one that lists components and any other, and how
/// much of that it loses for each tick since the jump began (`0x004DC744`, `0x004DC860`,
/// `0x004DC864`).
const jump_in_speed_components: f32 = 2400;
const jump_in_speed: f32 = 600;
const jump_in_slowing: f32 = 0.003;
/// The flight models an object moves by: its type's own (`GameObject.flight`), and for a fighter
/// the first ship type's (`ship_flight_stats`, `0x004F9E70`), the Predator's, which the plain
/// motions fly every fighter by, so that each leaves its carrier alike.
pub const Flight = struct {
own: *const create.FlightModel,
/// Null for an object of any other class.
fighter: ?*const create.FlightModel = null,
};
/// The flight model's thrust flying ahead, and astern.
const ahead: f32 = 1;
const astern: f32 = -1;
/// What a braking and a drifting object keep of their velocity each update (`0x00474618`,
/// `0x00474B07`).
const brake_share: f32 = 0.97;
const drift_share: f32 = 0.99;
/// The object's velocity scaled by `share`.
fn slow(object: *GameObject, share: f32) void {
object.velocity = .of(object.velocity.vector() * @as(math.Vector, @splat(share)));
}
/// The share of the cruise speed the lateral input pushes a ship sideways at (`object_fly`,
/// `0x004DC3D4`).
const lateral_share: f32 = 0.25;
/// What each update of the afterburner or of reverse thrust burns of `afterburner_fuel`
/// (`object_fly`, `0x0047430B` and `0x00474330`).
const burn_fuel: i32 = 4;
/// The throttle the afterburner and reverse thrust hold a ship at (`object_fly`, `0x00474301` and
/// `0x00474326`): the most and the least a ship flies at.
pub const afterburner_throttle: f32 = 2;
pub const reverse_throttle: f32 = -1;
/// How far a steering input reaches either way (`GameObject.roll_input`, `pitch_input` and
/// `yaw_input`): `object_steer` holds each between minus this and this.
pub const full_input: f32 = 1;
/// How many times more slowly a ship with no throttle turns than one at full throttle, where the
/// steering slows it (`object_steer`, `0x004DC3D8`): the divisor falls from this to 1 as the
/// throttle rises.
const idle_turn_slowing: f32 = 3;
/// The rule every quantity of the flight model moves by: it gives up `inertia` of the way it was
/// going and takes the rest from where it is headed, once per update.
fn settle(current: f32, target: f32, inertia: f32) f32 {
return current * inertia + (1 - inertia) * target;
}
/// `x` squared, keeping its sign, which is the measure the model works in along the nose.
fn signedSquare(x: f32) f32 {
return @abs(x) * x;
}
/// The inverse of `signedSquare`.
fn signedRoot(x: f32) f32 {
return if (x >= 0) @sqrt(x) else -@sqrt(-x);
}
/// `object_steer` (`0x00474150`): each input is clamped to between -1 and 1, and each angular rate
/// settles toward the ship's rate for that axis times the input. Where `throttle_turns`, that
/// target is divided by `3 - 2 * |throttle|` (`idle_turn_slowing`) while that exceeds 1, so a ship
/// turns more slowly the less throttle it carries. The three rates then make the rotation.
pub fn steer(object: *GameObject, flight: *const create.FlightModel, throttle_turns: bool) void {
// The game doubles the throttle by adding it to itself, which the factor of 2 matches exactly.
const slowed = idle_turn_slowing - (idle_turn_slowing - 1) * @abs(object.throttle);
const divisor: f32 = if (throttle_turns and slowed >= 1) slowed else 1;
const axes = [_]struct { rate: *f32, input: *f32, full: f32, inertia: f32 }{
.{ .rate = &object.pitch_rate, .input = &object.pitch_input, .full = flight.pitch_rate, .inertia = flight.pitch_inertia },
.{ .rate = &object.yaw_rate, .input = &object.yaw_input, .full = flight.yaw_rate, .inertia = flight.yaw_inertia },
.{ .rate = &object.roll_rate, .input = &object.roll_input, .full = flight.roll_rate, .inertia = flight.roll_inertia },
};
for (axes) |axis| {
axis.input.* = std.math.clamp(axis.input.*, -full_input, full_input);
axis.rate.* = settle(axis.rate.*, axis.full * axis.input.* / divisor, axis.inertia);
}
object.rotation = math.fromAngles(object.pitch_rate, object.yaw_rate, object.roll_rate);
}
/// `object_fly` (`0x004742E0`): the flight model, run for one update by the motion routine. The
/// throttle settles first, then the steering, then the speed, the last in the ship's own frame.
///
/// Along the nose the model settles in speed times its own size, so that thrust tells evenly at
/// every speed: the speed is squared keeping its sign, settles toward the thrust times the
/// throttle squared the same way times the target speed squared, and is rooted again. Sideways it
/// settles toward a quarter of the target times the lateral input, and along the ship's own down
/// axis it only decays.
pub fn fly(object: *GameObject, flight: *const create.FlightModel, view: camera.View, thrust: f32) void {
if (object.afterburner) {
object.throttle = afterburner_throttle;
object.afterburner_fuel -= burn_fuel;
} else if (object.reverse_thrust) {
object.throttle = reverse_throttle;
object.afterburner_fuel -= burn_fuel;
} else {
object.throttle = std.math.clamp(object.throttle, 0, 1);
}
object.afterburner_fuel = @max(object.afterburner_fuel, 0);
steer(object, flight, true);
// The frame the last update left behind: `object_move` sets it once the motion has run.
const frame = object.root.next_orientation;
const inertia = flight.inertia;
const target = if (object.afterburner or object.reverse_thrust)
flight.max_speed
else
ai.cruiseSpeed(object, flight, view);
var speed = math.transformTransposed(frame, object.velocity.vector());
const push = thrust * object.throttle;
speed = .{
settle(speed[0], object.lateral_input * target * lateral_share, inertia),
settle(speed[1], 0, inertia),
signedRoot(settle(signedSquare(speed[2]), signedSquare(push) * target * target, inertia)),
};
object.velocity = .of(math.transform(frame, speed));
object.last_throttle = object.throttle;
}
/// The plain flight model of `motion_downward` and `motion_plain`, which has no rules for the
/// throttle or the burns: the steering, the throttle slowing no turn, then the velocity, which
/// keeps `inertia` of itself and takes the rest from the throttle times the top speed along
/// `axis` of the frame the last update left behind.
fn plain(object: *GameObject, flight: *const create.FlightModel, axis: math.Axis) void {
steer(object, flight, false);
const inertia = flight.inertia;
var along: [3]f32 = @splat(0);
along[@backingInt(axis)] = (1 - inertia) * object.throttle * flight.max_speed;
const kept = object.velocity.vector() * @as(math.Vector, @splat(inertia));
object.velocity = .of(kept + math.transform(object.root.next_orientation, along));
}
/// `object_move` (`0x00473FF0`): one update of an object. A `frozen` object stays where it is.
/// Otherwise the root's next place is marked as pending, which the next step's `node_tree_update`
/// commits (`objects.updateTree`). If knocks are waiting, they are applied instead of the object's
/// own motion, which for a jump's goes by `jumping` (`Jumping`). An `unpowered` object has no motion
/// of its own, and a jumping one only moves when it is knocked or `unpowered`. Then its next
/// orientation becomes its orientation turned by `rotation`, its next position becomes its position
/// plus its velocity, and its speed becomes the length of that velocity. It sets the network flags
/// when the object moves or turns.
///
/// For the player's ship, `player_shake` is the camera's shake (`hit_shake`). When the ship flies
/// faster than its cruise speed, as it does under afterburner, the move raises the shake to at
/// least `0.2 * (speed / cruise speed - 1)`. The game also stores the change in the player's
/// speed at `player_speed_change` (`0x00562CE4`), which nothing reads.
pub fn move(object: *GameObject, flight: Flight, view: camera.View, motion: ?Motion, player_shake: ?*f32, by: By) void {
if (object.flags.frozen) return;
object.root.flags.next_pending = true;
const knocked = object.knocks > 0;
if (object.flags.jumping and !knocked and !object.flags.unpowered) return;
if (!knocked and !object.flags.unpowered) {
if (motion) |routine| routine.run(object, flight, view, by);
} else {
gameobj.applyKnocks(object);
}
object.root.next_orientation = math.product(object.root.orientation, object.rotation);
object.root.next_position = .of(object.position() + object.velocity.vector());
object.speed = math.length(object.velocity.vector());
if (object.speed > 0) object.network.moved = true;
if (object.pitch_rate != 0 or object.yaw_rate != 0 or object.roll_rate != 0) object.network.turned = true;
if (player_shake) |shake| {
shake.* = @max(shake.*, speed_shake * (object.speed / ai.cruiseSpeed(object, flight.own, view)) - speed_shake);
}
}
/// The camera shake at twice the cruise speed (`0x004DC3F8`).
const speed_shake: f32 = 0.2;
/// The ship type whose flight model the plain motions fly every fighter by: the first,
/// `ship_flight_stats` itself.
const fighters_flight_type = 0;
/// `object_move` for the object in slot `index` of `world`, by its type's flight model, where it
/// has one (`move`), and for a fighter the first ship type's too, where the ship types' stats are
/// at hand (`gameobj.World.spawn`): the player's ship shakes the camera. `objects_update` moves
/// each object so, and `objects_collide` moves a pair again once it has shoved them.
pub fn moveSlot(world: gameobj.World, index: u16) void {
const all = world.objects;
const slot = &all.slots[index];
const own = slot.flight orelse return;
const fighter = if (slot.combat) |combat| combat.class == .fighter else false;
const flight: Flight = .{
.own = own,
.fighter = if (fighter) if (world.spawn) |spawn| &spawn.tables.flight[fighters_flight_type] else null else null,
};
const by: By = .{
.jumping = .{ .state = &slot.state.jump, .clock = world.clock },
.following = .{ .world = world, .index = index },
};
move(&slot.object, flight, world.view(), slot.motion, if (index == all.player) world.shake else null, by);
}
test steer {
var object = gameobj.testing.object();
object.throttle = 1;
// An input past the ends is clamped, and the rate settles toward the ship's own rate.
object.pitch_input = 5;
steer(&object, &gameobj.testing.flight, true);
try std.testing.expectEqual(1, object.pitch_input);
try std.testing.expectApproxEqAbs(0.4, object.pitch_rate, 1e-6);
for (0..200) |_| steer(&object, &gameobj.testing.flight, true);
try std.testing.expectApproxEqAbs(gameobj.testing.flight.pitch_rate, object.pitch_rate, 1e-4);
// At rest the same input turns it a third as fast, the divisor being 3 - 2 * |throttle|.
var idle = gameobj.testing.object();
idle.pitch_input = 1;
for (0..200) |_| steer(&idle, &gameobj.testing.flight, true);
try std.testing.expectApproxEqAbs(gameobj.testing.flight.pitch_rate / 3, idle.pitch_rate, 1e-4);
// A caller that does not ask for it gets no such division.
var full = gameobj.testing.object();
full.pitch_input = 1;
for (0..200) |_| steer(&full, &gameobj.testing.flight, false);
try std.testing.expectApproxEqAbs(gameobj.testing.flight.pitch_rate, full.pitch_rate, 1e-4);
}
test "the throttle settles between 0 and 1, and the burns take it past both ends" {
var object = gameobj.testing.object();
object.throttle = 5;
fly(&object, &gameobj.testing.flight, .chase, 1);
try std.testing.expectEqual(1, object.throttle);
try std.testing.expectEqual(1, object.last_throttle);
object.throttle = -3;
fly(&object, &gameobj.testing.flight, .chase, 1);
try std.testing.expectEqual(0, object.throttle);
// The afterburner runs it to 2 and burns fuel; reverse thrust to -1, and burns it as well.
object.afterburner = true;
object.afterburner_fuel = 10;
fly(&object, &gameobj.testing.flight, .chase, 1);
try std.testing.expectEqual(2, object.throttle);
try std.testing.expectEqual(6, object.afterburner_fuel);
object.afterburner = false;
object.reverse_thrust = true;
fly(&object, &gameobj.testing.flight, .chase, 1);
try std.testing.expectEqual(-1, object.throttle);
try std.testing.expectEqual(2, object.afterburner_fuel);
// The fuel stops at zero however long it burns.
for (0..4) |_| fly(&object, &gameobj.testing.flight, .chase, 1);
try std.testing.expectEqual(0, object.afterburner_fuel);
}
test "a ship settles at its cruise speed along its nose" {
var object = gameobj.testing.object();
object.throttle = 1;
for (0..400) |_| move(&object, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
// The model frame has Z forward, so all of the speed is along the nose.
try std.testing.expectApproxEqAbs(320, object.speed, 0.5);
try std.testing.expectApproxEqAbs(320, object.velocity.z, 0.5);
try std.testing.expectApproxEqAbs(0, object.velocity.x, 1e-3);
try std.testing.expectApproxEqAbs(0, object.velocity.y, 1e-3);
// It never runs past the speed it is settling toward.
try std.testing.expect(object.speed <= 320);
// Half its engines gone, it settles at half the speed.
object.engines_intact = 0.5;
for (0..400) |_| move(&object, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expectApproxEqAbs(160, object.speed, 0.5);
// Backward, the same ship ends up going the other way at the same speed.
var reversed = gameobj.testing.object();
reversed.throttle = 1;
for (0..400) |_| move(&reversed, .{ .own = &gameobj.testing.flight }, .chase, .backward, null, .{});
try std.testing.expectApproxEqAbs(-320, reversed.velocity.z, 0.5);
}
test "the lateral input pushes a ship a quarter as fast sideways" {
var object = gameobj.testing.object();
object.lateral_input = 1;
for (0..400) |_| move(&object, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expectApproxEqAbs(320 * lateral_share, object.velocity.x, 0.5);
}
test move {
var object = gameobj.testing.object();
object.root.position = .{ .x = 1, .y = 2, .z = 3 };
object.velocity = .{ .x = 10, .y = 0, .z = 20 };
// `create_object` leaves the rotation zeroed, and the steering builds one before the first
// move uses it; a turn of nothing stands in for that here.
object.rotation = math.identity;
// With no motion routine, it carries on at the velocity it has.
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, null, .{});
try std.testing.expectEqual(11, object.root.next_position.x);
try std.testing.expectEqual(2, object.root.next_position.y);
try std.testing.expectEqual(23, object.root.next_position.z);
try std.testing.expectApproxEqAbs(@sqrt(500.0), object.speed, 1e-4);
// Its next orientation is its orientation turned by the rotation the steering built.
try std.testing.expectEqual(math.identity, object.root.next_orientation);
try std.testing.expect(object.root.flags.next_pending);
}
test "a braking object slows faster than a drifting one, and lets go of its throttle" {
var braking = gameobj.testing.object();
braking.velocity = .{ .x = 0, .y = 0, .z = 100 };
braking.last_throttle = 1;
var drifting = braking;
Motion.brake.run(&braking, .{ .own = &gameobj.testing.flight }, .chase, .{});
Motion.drift.run(&drifting, .{ .own = &gameobj.testing.flight }, .chase, .{});
try std.testing.expectApproxEqAbs(100 * brake_share, braking.velocity.z, 1e-4);
try std.testing.expectEqual(0, braking.last_throttle);
try std.testing.expectApproxEqAbs(100 * drift_share, drifting.velocity.z, 1e-4);
try std.testing.expectEqual(1, drifting.last_throttle);
}
test "the plain motions push along the ship's own axes, a fighter by the first type's flight" {
var first = gameobj.testing.flight;
first.max_speed = 100;
const flight: Flight = .{ .own = &gameobj.testing.flight, .fighter = &first };
// Turned a quarter about Z, the ship's own Y axis points along the world's -X.
const turned = math.rotation(.z, std.math.pi / 2.0);
var dropping = gameobj.testing.object();
dropping.root.next_orientation = turned;
dropping.throttle = 1;
dropping.last_throttle = 1;
for (0..400) |_| Motion.downward.run(&dropping, flight, .chase, .{});
try std.testing.expectApproxEqAbs(-100, dropping.velocity.x, 0.5);
try std.testing.expectApproxEqAbs(0, dropping.velocity.y, 1e-3);
try std.testing.expectEqual(0, dropping.last_throttle);
// With no throttle, it turns no more slowly.
dropping.throttle = 0;
dropping.pitch_input = 1;
Motion.downward.run(&dropping, flight, .chase, .{});
try std.testing.expectApproxEqAbs(0.4, dropping.pitch_rate, 1e-6);
// Plain, along the nose, keeping its throttle as the last.
var boosting = gameobj.testing.object();
boosting.throttle = 2;
for (0..400) |_| Motion.plain.run(&boosting, flight, .chase, .{});
try std.testing.expectApproxEqAbs(200, boosting.velocity.z, 0.5);
try std.testing.expectEqual(2, boosting.last_throttle);
// The Ripper flies by its own flight model, and so does what is no fighter.
boosting.type = .of(.ripper);
for (0..400) |_| Motion.plain.run(&boosting, flight, .chase, .{});
try std.testing.expectApproxEqAbs(640, boosting.velocity.z, 0.5);
var other = gameobj.testing.object();
other.throttle = 1;
for (0..400) |_| Motion.downward.run(&other, .{ .own = &gameobj.testing.flight }, .chase, .{});
try std.testing.expectApproxEqAbs(320, other.velocity.y, 0.5);
}
test "a fighter moves by the first ship type's flight where the stats are at hand" {
var mission: gameobj.testing.Mission = undefined;
try mission.init(std.testing.allocator);
defer mission.deinit();
mission.tables.flight[fighters_flight_type].max_speed = 100;
const fighter = try mission.add(.of(.sabre), @splat(0));
const other = try mission.add(.of(.sabre), .{ 0, 0, 5000 });
mission.tables.combat[@backingInt(gameobj.GameType.sabre)].class = .fighter;
var world = mission.world();
world.spawn = mission.spawn(create.testing.no_models);
for ([_]u16{ fighter, other }) |index| {
const slot = mission.slot(index);
slot.motion = .downward;
slot.object.throttle = 1;
}
for (0..400) |_| moveSlot(world, fighter);
try std.testing.expectApproxEqAbs(100, mission.slot(fighter).object.velocity.y, 0.5);
// Without the stats, even a fighter flies by its own.
for (0..400) |_| moveSlot(mission.world(), other);
try std.testing.expectApproxEqAbs(320, mission.slot(other).object.velocity.y, 0.5);
}
test "an object travels from step to step" {
var object = gameobj.testing.object();
object.velocity = .{ .x = 0, .y = 0, .z = 10 };
object.rotation = math.identity;
// Each step commits the place the previous one worked out, then moves on from it.
for (0..3) |_| {
gameobj.updateTree(&object.root, null, null);
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, null, .{});
}
try std.testing.expectEqual(30, object.root.next_position.z);
// Between steps the committed position is one step behind.
try std.testing.expectEqual(20, object.root.position.z);
}
test "frozen, unpowered and jumping objects" {
// A frozen object isn't moved at all.
var frozen = gameobj.testing.object();
frozen.velocity = .{ .x = 0, .y = 0, .z = 10 };
frozen.rotation = math.identity;
frozen.flags.frozen = true;
move(&frozen, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expect(!frozen.root.flags.next_pending);
try std.testing.expectEqual(0, frozen.root.next_position.z);
// An unpowered one drifts: its motion routine doesn't run, so the throttle doesn't change its
// velocity.
var unpowered = gameobj.testing.object();
unpowered.velocity = .{ .x = 0, .y = 0, .z = 10 };
unpowered.rotation = math.identity;
unpowered.throttle = 1;
unpowered.flags.unpowered = true;
move(&unpowered, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expectEqual(10, unpowered.velocity.z);
try std.testing.expectEqual(10, unpowered.root.next_position.z);
// A jumping one stays where it is until it's knocked.
var jumping = gameobj.testing.object();
jumping.velocity = .{ .x = 0, .y = 0, .z = 10 };
jumping.rotation = math.identity;
jumping.mass = 1;
jumping.flags.jumping = true;
move(&jumping, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expect(jumping.root.flags.next_pending);
try std.testing.expectEqual(0, jumping.root.next_position.z);
gameobj.knock(&jumping, .{ 0, 0, 5 }, .{ 0, 0, 0 });
move(&jumping, .{ .own = &gameobj.testing.flight }, .chase, .forward, null, .{});
try std.testing.expectEqual(15, jumping.root.next_position.z);
}
test "the follow motion flies straight at its path's point, no faster than its limit" {
var object = gameobj.testing.object();
const flight = &gameobj.testing.flight;
// Straight ahead and near, it goes all the way in the step, at that share of its top speed.
followPath(&object, flight, .{ .point = .{ 0, 0, 160 } }, 1, false);
try std.testing.expectEqual(160, object.velocity.z);
try std.testing.expectEqual(0.5, object.throttle);
try std.testing.expectEqual(0, object.yaw_input);
// Far, it goes at its limit.
followPath(&object, flight, .{ .point = .{ 0, 0, 10000 } }, 0.5, false);
try std.testing.expectApproxEqAbs(160, object.velocity.z, 1e-3);
try std.testing.expectEqual(0.5, object.throttle);
try std.testing.expectEqual(0.5, object.last_throttle);
// To one side, it turns toward the point; tail first, the other way.
followPath(&object, flight, .{ .point = .{ 1000, 0, 1000 } }, 1, false);
const toward = object.yaw_input;
try std.testing.expect(toward != 0);
object.yaw_rate = 0;
object.pitch_rate = 0;
object.roll_rate = 0;
followPath(&object, flight, .{ .point = .{ 1000, 0, 1000 } }, 1, true);
try std.testing.expectApproxEqAbs(-toward, object.yaw_input, 1e-4);
}
test "Jump Out's motion eases a ship off, and Jump In's flies it in, slowing to its cruise speed" {
const flight: Flight = .{ .own = &gameobj.testing.flight };
var state = std.mem.zeroes(jump.State);
state.since = 100;
state.to = .{ .x = 0, .y = 0, .z = 500000 };
var clock: Clock = .{ .mission_ticks = 100 + jump.going_ticks / 2 };
const on: Jumping = .{ .state = &state, .clock = &clock };
var object = gameobj.testing.object();
object.root.position = .{ .x = 0, .y = 0, .z = 1000 };
object.last_throttle = 1;
// Halfway through its going, it is a quarter of the way there.
Motion.jump_out.run(&object, flight, .chase, .{ .jumping = on });
try std.testing.expectApproxEqAbs(125000 - 1000, object.velocity.z, 1e-2);
try std.testing.expectEqual(math.identity, object.rotation);
try std.testing.expectEqual(0, object.last_throttle);
// Jump In's flies it along its nose, from its full speed, slower each tick, but never below
// its cruise speed.
for ([_]struct { i32, f32 }{ .{ 100, 600 }, .{ 150, 510 }, .{ 300, 320 } }) |at| {
clock.frame_start = at[0];
Motion.jump_in.run(&object, flight, .chase, .{ .jumping = on });
try std.testing.expectApproxEqAbs(at[1], object.velocity.z, 1e-3);
}
// One that lists components comes in faster.
object.flags.components = true;
clock.frame_start = 150;
Motion.jump_in.run(&object, flight, .chase, .{ .jumping = on });
try std.testing.expectApproxEqAbs(2040, object.velocity.z, 1e-2);
}
test "moving and turning set the network flags" {
var object = gameobj.testing.object();
object.rotation = math.identity;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, null, .{});
try std.testing.expect(!object.network.moved and !object.network.turned);
object.velocity.z = 1;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, null, .{});
try std.testing.expect(object.network.moved and !object.network.turned);
object.yaw_rate = 0.1;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, null, .{});
try std.testing.expect(object.network.turned);
}
test moveSlot {
var mission: gameobj.testing.Mission = undefined;
try mission.init(std.testing.allocator);
defer mission.deinit();
const player = try mission.add(.of(.predator), @splat(0));
const other = try mission.add(.of(.predator), .{ 0, 0, 1000 });
for ([_]u16{ player, other }) |index| {
const slot = mission.slot(index);
slot.motion = null;
slot.object.rotation = math.identity;
slot.object.velocity = .{ .x = 0, .y = 0, .z = 640 };
}
// Both move on, and only the player's ship, flying past its cruise speed, shakes the camera.
moveSlot(mission.world(), other);
try std.testing.expectEqual(1640, mission.slot(other).object.root.next_position.z);
try std.testing.expectEqual(0, mission.shake);
moveSlot(mission.world(), player);
try std.testing.expectEqual(640, mission.slot(player).object.root.next_position.z);
try std.testing.expect(mission.shake > 0);
// An object with no flight model is not moved.
mission.slot(other).flight = null;
mission.slot(other).object.velocity.z = 10;
moveSlot(mission.world(), other);
try std.testing.expectEqual(1640, mission.slot(other).object.root.next_position.z);
}
test "flying faster than the cruise speed shakes the player's camera" {
var object = gameobj.testing.object();
object.rotation = math.identity;
var shake: f32 = 0;
// At the cruise speed, it doesn't.
object.velocity.z = 320;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, &shake, .{});
try std.testing.expectEqual(0, shake);
// At twice the cruise speed, by 0.2.
object.velocity.z = 640;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, &shake, .{});
try std.testing.expectApproxEqAbs(0.2, shake, 1e-6);
// It never lowers a stronger shake, such as a hit's.
shake = 1;
move(&object, .{ .own = &gameobj.testing.flight }, .chase, null, &shake, .{});
try std.testing.expectEqual(1, shake);
}