Repository navigation
Expand file tree
/
Copy pathvehicles.c
More file actions
2760 lines (2311 loc) · 80.3 KB
/
Copy pathvehicles.c
File metadata and controls
2760 lines (2311 loc) · 80.3 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
/*
VEHICLES.C
symbols in this file:
001A4E30 0010:
_vehicles_initialize (0000)
001A4E40 0010:
_vehicles_initialize_for_new_map (0000)
001A4E50 0010:
_vehicles_dispose_from_old_map (0000)
001A4E60 0010:
_vehicles_dispose (0000)
001A4E70 0030:
_vehicle_place (0000)
001A4EA0 0010:
_vehicle_delete (0000)
001A4EB0 0030:
_vehicle_causes_collision_damage (0000)
001A4EE0 0010:
_vehicle_build_update (0000)
001A4EF0 0010:
_vehicle_parse_update (0000)
001A4F00 0050:
_vehicle_hover (0000)
001A4F50 0020:
_code_001a4f50 (0000)
001A4F70 0030:
_vehicle_is_flipped (0000)
001A4FA0 00a0:
_code_001a4fa0 (0000)
001A5040 0020:
_set_real_quaternion (0000)
001A5060 00b0:
_vehicle_reset (0000)
001A5110 0070:
_vehicle_new (0000)
001A5180 0400:
_vehicle_preprocess_node_orientations (0000)
001A5580 0100:
_vehicle_accelerate (0000)
001A5680 0060:
_vehicle_render_debug (0000)
001A56E0 0130:
_vehicle_find_pathfinding_surface_index (0000)
001A5810 00d0:
_code_001a5810 (0000)
001A58E0 0150:
_code_001a58e0 (0000)
001A5A30 0110:
_code_001a5a30 (0000)
001A5B40 0310:
_code_001a5b40 (0000)
001A5E50 0440:
_code_001a5e50 (0000)
001A6290 0300:
_code_001a6290 (0000)
001A6590 0180:
_code_001a6590 (0000)
001A6710 0200:
_code_001a6710 (0000)
001A6910 0290:
_code_001a6910 (0000)
001A6BA0 0220:
_code_001a6ba0 (0000)
001A6DC0 0320:
_code_001a6dc0 (0000)
001A70E0 01d0:
_code_001a70e0 (0000)
001A72B0 0520:
_vehicle_export_function_values (0000)
001A77D0 0180:
_vehicle_moving_near_any_player (0000)
001A7950 0170:
_vehicle_stuck (0000)
001A7AC0 03a0:
_code_001a7ac0 (0000)
001A7E60 09a0:
_code_001a7e60 (0000)
001A8800 0070:
_code_001a8800 (0000)
001A8870 0910:
_vehicle_update (0000)
002A6200 000f:
??_C@_0P@GMMIJBCG@vehicle_update?$AA@ (0000)
002A6210 0004:
__real@3c64c389 (0000)
002A6214 0004:
__real@bc64c389 (0000)
002A6218 0008:
__real@3f9c987120000000 (0000)
002A6220 0004:
__real@3e088889 (0000)
002A6224 0004:
__real@3f866666 (0000)
002A6228 0004:
__real@3f55c28f (0000)
002A622C 000a:
??_C@_09IPNEIKLN@reflected?$AA@ (0000)
002A6238 000e:
??_C@_0O@FEOFEIPO@jet?5thrusters?$AA@ (0000)
002A6248 0010:
??_C@_0BA@JGHLOGHL@hover?5thrusters?$AA@ (0000)
002A6258 0009:
??_C@_08FLPDFBMI@midpoint?$AA@ (0000)
002A6264 0004:
__real@4235d174 (0000)
002A6268 0004:
__real@3fd55556 (0000)
002A626C 0004:
__real@4091745d (0000)
002A6270 0004:
__real@3f969697 (0000)
002A6274 0004:
__real@bd4ccccd (0000)
002A6278 0004:
__real@3b03126f (0000)
002A627C 0004:
__real@3b83126f (0000)
002A6280 0004:
__real@bbbea2f1 (0000)
002A6284 0004:
__real@3b7e2e97 (0000)
002A6288 0004:
__real@3acb5879 (0000)
002A628C 0004:
__real@3b64c389 (0000)
002A6290 0004:
__real@bb64c389 (0000)
002A6298 0008:
__real@3f7c987120000000 (0000)
002A62A0 0006:
??_C@_05KCCEOPLE@?$HOblur?$AA@ (0000)
002A62A8 0020:
??_C@_0CA@NBAFPFDO@c?3?2halo?2SOURCE?2units?2vehicles?4c?$AA@ (0000)
002A62C8 0008:
__real@3fd3333333333333 (0000)
002A62D0 0008:
__real@bfd3333333333333 (0000)
00316228 05f8:
_data_00316228 (0000)
*/
/* ---------- headers */
#include "cseries.h"
#include "cseries/errors.h"
#include "cseries/profile.h"
#include "vehicles.h"
#include "math/real_math.h"
#include "game/game_globals.h"
#include "game/players.h"
#include "effects/material_effects.h"
#include "effects/effects.h"
#include "models/model_animation_definitions.h"
#include "objects/damage.h"
#include "objects/objects.h"
#include "physics/collision_bsp.h"
#include "physics/collision_usage.h"
#include "physics/collisions.h"
#include "physics/physics.h"
#include "physics/physics_definitions.h"
#include "physics/physics_variables.h"
#include "scenario/scenario.h"
#include "sound/game_sound.h"
#include "structures/structure_bsp_definitions.h"
#include "units/unit_definitions.h"
#include "units/vehicle_definitions.h"
/* ---------- constants */
enum
{
_vehicle_type_human_tank = 0,
_vehicle_type_human_jeep,
_vehicle_type_human_boat,
_vehicle_type_human_plane,
_vehicle_type_alien_scout,
_vehicle_type_alien_fighter,
_vehicle_type_turret,
NUMBER_OF_VEHICLE_TYPES
};
/* ---------- macros */
/* 0.8 degrees in radians. Spelled as a float literal because MSVC folds
constant expressions in double and only rounds at the final assignment, so
((real)(0.8*_pi/180)) yields 0x3c64c388 where January has 0x3c64c389 -- and
2.0*it then differs in the double constant too. */
#define VEHICLE_ANGULAR_ACCELERATION 0.0139626344f
/* ---------- structures */
struct vehicle_definition
{
struct unit_definition unit;
unsigned long flags;
short vehicle_type;
short pad2f6;
/* 0x2f8 is a four-real 'speed' block and 0x308 a two-real 'turn' block;
their sub-field names are not recovered, so they keep offset names. */
real unknown2f8;
real unknown2fc;
real unknown300;
real unknown304;
real unknown308;
real unknown30c;
real wheel_circumference;
real unknown314;
real unknown318;
short function_modes[4];
byte unknown324[0xc];
real unknown330;
real unknown334;
byte unused338[8];
real unknown340;
real unknown344;
byte unused348[0x1c];
real unknown364;
byte unknown368[0x48];
struct tag_reference suspension_sound;
struct tag_reference crash_sound;
struct tag_reference material_effects;
struct tag_reference effect;
};
struct game_globals_falling_damage
{
byte unused0[0x2c];
struct tag_reference maximum_distance_damage;
struct tag_reference vehicle_hit_environment_damage_effect;
struct tag_reference vehicle_killed_unit_damage_effect;
struct tag_reference vehicle_collision_damage;
struct tag_reference flaming_death_damage;
long unused7c[4];
real runtime_maximum_falling_velocity;
real runtime_minimum_damage_velocity;
real runtime_maximum_damage_velocity;
};
#include "vehicle_datum.h"
struct physics_mass_point_definition
{
byte unused0[0x20];
short powered_mass_point_index;
short pad22;
unsigned long flags;
byte unused28[0x10];
real_point3d position;
byte unused44[0xc];
real_vector3d normal;
byte unused5c[0xc];
real unknown68;
byte unused6c[0x14];
};
struct vehicle_animation
{
struct animation_aiming_screen_bounds steering_screen_bounds;
long unused[0x11];
struct tag_block animations;
struct tag_block suspensions;
};
struct vehicle_suspension
{
short mass_point_index;
short animation_index;
real unknown4;
real unknown8;
byte unknownc[8];
};
#include "physics/mass_point_datum.h"
#include "physics/powered_mass_point_datum.h"
struct scenario_object_permutation;
struct scenario_unit;
struct scenario_vehicle
{
byte unused0[0x28];
byte permutation[0x20];
byte unit[0x4];
};
/* ---------- prototypes */
/* NOTE: code_001a5e50 and code_001a6290 are file statics in January, but they
are not reconstructed yet. They are declared here rather than defined so that
code_001a8800 can call them: January passes their arguments on the stack, and a
declared-but-undefined static would give MSVC a body to inspect and a register
convention to invent. The relocation carries the name either way, which is what
the comparator checks. */
extern boolean debug_objects_vehicle_powered_mass_points;
extern real global_gravity;
/* ---------- globals */
static struct profile_section vehicle_update_section = {"vehicle_update", NONE, TRUE};
/* ---------- code */
void vehicles_initialize(
void)
{
return;
}
void vehicles_initialize_for_new_map(
void)
{
return;
}
void vehicles_dispose_from_old_map(
void)
{
return;
}
void vehicles_dispose(
void)
{
return;
}
void vehicle_place(
long vehicle_index,
struct scenario_vehicle *vehicle)
{
unit_place(vehicle_index, (struct scenario_unit_datum const *)vehicle->unit);
object_add_scenario_permutation(vehicle_index, (struct scenario_object_permutation *)vehicle->permutation);
return;
}
void vehicle_delete(
long vehicle_index)
{
return;
}
boolean vehicle_causes_collision_damage(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct vehicle_definition *definition = vehicle_specific_definition_get(
vehicle->definition_index);
return TEST_FLAG(definition->flags, 7);
}
boolean vehicle_build_update(
long vehicle_index,
void *update)
{
return TRUE;
}
boolean vehicle_parse_update(
long vehicle_index,
void *update)
{
return TRUE;
}
void vehicle_hover(
long vehicle_index,
boolean hover)
{
if (vehicle_index!=NONE)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
if (hover)
{
object_get_origin(vehicle_index, &vehicle->vehicle.hover_position);
vehicle->vehicle.flags |= FLAG(1);
}
else
{
vehicle->vehicle.flags &= ~FLAG(1);
}
}
return;
}
boolean vehicle_is_flipped(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
return vehicle->object.up.k<0.2f;
}
void vehicle_reset(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
vehicle->vehicle.flags = 0;
vehicle->vehicle.stop_time = 0;
vehicle->vehicle.airborne_ticks = 0;
vehicle->vehicle.upending_type = 0;
vehicle->vehicle.upending_ticks = 0;
vehicle->vehicle.on_ground_ticks = 0;
vehicle->vehicle.speed = 0;
vehicle->vehicle.slide = 0;
vehicle->vehicle.turn = 0;
vehicle->vehicle.wheel = 0;
vehicle->vehicle.left_tread = 0;
vehicle->vehicle.right_tread = 0;
vehicle->vehicle.thrust = 0;
vehicle->vehicle.hover = 0;
csmemset(vehicle->vehicle.suspension, 0, sizeof(vehicle->vehicle.suspension));
set_real_quaternion(&vehicle->vehicle.collision_accumulator_head, 0.0f, 0.0f, 0.0f, 0.0f);
set_real_point2d(&vehicle->vehicle.collision_accumulator_tail, 0.0f, 0.0f);
vehicle->vehicle.stuck_mass_point_flags = 0;
return;
}
boolean vehicle_new(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct unit_definition *definition = vehicle_definition_get(vehicle->definition_index);
vehicle_reset(vehicle_index);
if (definition->object.physics.index==NONE)
vehicle->object.flags |= FLAG(5);
else
vehicle->object.flags &= ~FLAG(5);
if (definition->object.physics.index!=NONE)
vehicle->object.position.z += definition->object.bounding_radius*0.5f;
return TRUE;
}
void vehicle_accelerate(
long vehicle_index,
real_vector3d const *acceleration)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct unit_definition *definition = vehicle_definition_get(vehicle->definition_index);
if (definition->object.physics.index!=NONE)
{
struct physics_definition *physics = physics_definition_get(definition->object.physics.index);
real_vector3d torque;
real magnitude;
add_vectors3d(&vehicle->object.translational_velocity, acceleration,
&vehicle->object.translational_velocity);
cross_product3d(global_up3d, acceleration, &torque);
magnitude = normalize3d(&torque);
if (magnitude>0.0f)
{
scale_vector3d(&torque, magnitude*_pi, &torque);
add_vectors3d(&vehicle->object.angular_velocity, &torque,
&vehicle->object.angular_velocity);
}
vehicle->object.flags &= ~FLAG(5);
}
return;
}
void vehicle_render_debug(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct unit_definition *definition = vehicle_definition_get(vehicle->definition_index);
if (definition->object.physics.index!=NONE)
{
struct physics_definition *physics = physics_definition_get(definition->object.physics.index);
if (debug_objects_vehicle_powered_mass_points)
{
short mass_point_index;
for (mass_point_index = 0; mass_point_index<physics->mass_points.count; mass_point_index++)
{
}
}
}
return;
}
boolean vehicle_stuck(
long vehicle_index,
real_vector3d *direction)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
boolean stuck = FALSE;
if (vehicle->vehicle.stuck_mass_point_flags)
{
struct physics_instance instance;
if (physics_instance_new(&instance, vehicle_index))
{
real_point3d center = *global_origin3d;
short mass_point_count = 0;
short mass_point_index;
for (mass_point_index = 0;
mass_point_index<instance.physics->mass_points.count;
mass_point_index++)
{
if (TEST_FLAG(vehicle->vehicle.stuck_mass_point_flags, mass_point_index))
{
struct physics_mass_point_definition *mass_point = TAG_BLOCK_GET_ELEMENT(
&instance.physics->mass_points, mass_point_index,
struct physics_mass_point_definition);
add_vectors3d(¢er, &mass_point->position, ¢er);
mass_point_count++;
}
}
if (mass_point_count>0)
{
real scale = 1.0f/mass_point_count;
real_point3d center_in_world;
real_point3d origin;
center.x *= scale;
center.y *= scale;
center.z *= scale;
matrix4x3_transform_point(&instance.world_matrix, ¢er, ¢er_in_world);
object_get_origin(vehicle_index, &origin);
subtract_vectors3d(¢er_in_world, &origin, direction);
if (normalize3d(direction)!=0.0f)
stuck = TRUE;
}
}
}
return stuck;
}
/* NOTE: the vehicle function enum is not recovered from January. These names
describe what each case computes, read off the disassembly. They are descriptive,
not authentic. */
enum
{
_vehicle_function_none = 0,
_vehicle_function_speed_absolute,
_vehicle_function_speed_forward,
_vehicle_function_speed_reverse,
_vehicle_function_slide_absolute,
_vehicle_function_slide_left,
_vehicle_function_slide_right,
_vehicle_function_speed_or_slide,
_vehicle_function_turn_absolute,
_vehicle_function_turn_left,
_vehicle_function_turn_right,
_vehicle_function_flag2,
_vehicle_function_flag3,
_vehicle_function_unused13,
_vehicle_function_velocity_absolute,
_vehicle_function_velocity_moving,
_vehicle_function_velocity_sliding,
_vehicle_function_velocity_forward,
_vehicle_function_velocity_up,
_vehicle_function_velocity_up_alternate,
_vehicle_function_left_tread_position,
_vehicle_function_right_tread_position,
_vehicle_function_speed_minus_turn,
_vehicle_function_speed_plus_turn,
_vehicle_function_wheel_position_a,
_vehicle_function_wheel_position_b,
_vehicle_function_wheel_position_c,
_vehicle_function_wheel_position_d,
_vehicle_function_speed_absolute_a,
_vehicle_function_speed_absolute_b,
_vehicle_function_speed_absolute_c,
_vehicle_function_speed_absolute_d,
_vehicle_function_sideslip,
_vehicle_function_unknown444,
_vehicle_function_unknown448,
_vehicle_function_speed_blend,
_vehicle_function_boost,
NUMBER_OF_VEHICLE_FUNCTIONS
};
void vehicle_export_function_values(
long vehicle_index)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct vehicle_definition *definition = vehicle_specific_definition_get(
vehicle->definition_index);
real forward_speed = (real)fabs(definition->unknown2f8);
real reverse_speed = (real)fabs(definition->unknown2fc);
real maximum_speed = MAX(forward_speed, reverse_speed);
real left_slide = (real)fabs(definition->unknown330);
real right_slide = (real)fabs(definition->unknown334);
real maximum_slide = MAX(left_slide, right_slide);
real left_turn = (real)fabs(definition->unknown308);
real right_turn = (real)fabs(definition->unknown30c);
real maximum_turn = MAX(left_turn, right_turn);
real *value = vehicle->object.incoming_function_values;
short *mode = definition->function_modes;
long remaining;
for (remaining = NUMBER_OF_INCOMING_OBJECT_FUNCTIONS; remaining; remaining--)
{
if (*mode)
{
real result = 0.0f;
switch (*mode)
{
case _vehicle_function_speed_absolute:
case _vehicle_function_speed_absolute_a:
case _vehicle_function_speed_absolute_b:
case _vehicle_function_speed_absolute_c:
case _vehicle_function_speed_absolute_d:
result = (real)fabs(vehicle->vehicle.speed)/maximum_speed;
break;
case _vehicle_function_speed_forward:
result = (vehicle->vehicle.speed<0.0f ? 0.0f :
vehicle->vehicle.speed)/forward_speed;
break;
case _vehicle_function_speed_reverse:
result = (real)fabs(vehicle->vehicle.speed>0.0f ? 0.0f :
vehicle->vehicle.speed)/reverse_speed;
break;
case _vehicle_function_slide_absolute:
result = (real)fabs(vehicle->vehicle.slide)/maximum_slide;
break;
case _vehicle_function_slide_left:
result = (real)fabs(vehicle->vehicle.slide)/left_slide;
break;
case _vehicle_function_slide_right:
result = (real)fabs(vehicle->vehicle.slide)/right_slide;
break;
case _vehicle_function_speed_or_slide:
result = MAX(fabs(vehicle->vehicle.speed)/maximum_speed,
fabs(vehicle->vehicle.slide)/maximum_slide);
break;
case _vehicle_function_turn_absolute:
result = (real)fabs(vehicle->vehicle.turn)/maximum_turn;
break;
case _vehicle_function_turn_left:
result = (real)fabs(vehicle->vehicle.turn)/left_turn;
break;
case _vehicle_function_turn_right:
result = (real)fabs(vehicle->vehicle.turn)/right_turn;
break;
case _vehicle_function_flag2:
result = TEST_FLAG(vehicle->vehicle.flags, 2) ? 1.0f : 0.0f;
break;
case _vehicle_function_flag3:
result = TEST_FLAG(vehicle->vehicle.flags, 3) ? 1.0f : 0.0f;
break;
case _vehicle_function_velocity_absolute:
result = magnitude3d(&vehicle->object.translational_velocity)/
maximum_speed;
break;
case _vehicle_function_velocity_moving:
result = vehicle->object.flags&(FLAG(2)|FLAG(3)|FLAG(4)) ?
magnitude3d(&vehicle->object.translational_velocity)/maximum_speed :
0.0f;
break;
case _vehicle_function_velocity_sliding:
result = TEST_FLAG(vehicle->object.flags, 1) ?
magnitude3d(&vehicle->object.translational_velocity)/maximum_speed :
0.0f;
break;
case _vehicle_function_velocity_forward:
result = (real)fabs(dot_product3d(
&vehicle->object.translational_velocity,
&vehicle->object.forward))/maximum_speed;
break;
case _vehicle_function_velocity_up:
case _vehicle_function_velocity_up_alternate:
result = (real)fabs(dot_product3d(
&vehicle->object.translational_velocity,
&vehicle->object.up))/maximum_speed;
break;
case _vehicle_function_left_tread_position:
result = vehicle->vehicle.left_tread/definition->wheel_circumference;
break;
case _vehicle_function_right_tread_position:
result = vehicle->vehicle.right_tread/definition->wheel_circumference;
break;
case _vehicle_function_speed_minus_turn:
result = (real)fabs(vehicle->vehicle.speed-
vehicle->vehicle.turn)/maximum_speed;
break;
case _vehicle_function_speed_plus_turn:
result = (real)fabs(vehicle->vehicle.turn+
vehicle->vehicle.speed)/maximum_speed;
break;
case _vehicle_function_wheel_position_a:
case _vehicle_function_wheel_position_b:
case _vehicle_function_wheel_position_c:
case _vehicle_function_wheel_position_d:
result = vehicle->vehicle.wheel/definition->wheel_circumference;
break;
case _vehicle_function_sideslip:
{
real_vector3d along, across;
component_vectors_from_normal3d(
&vehicle->object.translational_velocity,
&vehicle->object.forward, &along, &across);
result = magnitude3d(&across)*(1.0f/0.3f);
result = result*result;
break;
}
case _vehicle_function_unknown448:
result = vehicle->vehicle.thrust;
break;
case _vehicle_function_unknown444:
result = vehicle->vehicle.hover;
break;
case _vehicle_function_speed_blend:
{
real dot_speed;
real forward_value;
real blend;
dot_speed = (real)fabs(dot_product3d(&vehicle->object.translational_velocity, &vehicle->object.forward))/maximum_speed;
forward_value = (real)fabs(vehicle->vehicle.speed)/forward_speed;
blend = PIN(((real)vehicle->vehicle.airborne_ticks*0.2f+1.0f)*0.5f, 0.0f, 1.0f);
result = dot_speed*(1.0f-blend)+forward_value*blend;
break;
}
case _vehicle_function_boost:
result = magnitude3d(&vehicle->object.translational_velocity)/
definition->unknown2f8;
result = (result*vehicle->vehicle.thrust-0.05f)*
(1.0f/(0.9f-0.05f));
break;
}
*value = PIN(result, 0.0f, 1.0f);
}
mode++;
value++;
}
return;
}
boolean vehicle_moving_near_any_player(
void)
{
long unit_indices[MAXIMUM_LOCAL_PLAYERS];
real_point3d unit_centers[MAXIMUM_LOCAL_PLAYERS];
short unit_count = 0;
boolean no_vehicles_moving = TRUE;
short local_player_index;
for (local_player_index = local_player_get_next(NONE);
local_player_index!=NONE;
local_player_index = local_player_get_next(local_player_index))
{
if (local_player_get_player_index(local_player_index)!=NONE)
{
long unit_index = player_get(local_player_get_player_index(local_player_index))->unit_index;
if (unit_index!=NONE
&& ((struct unit_datum *)unit_get(unit_index))->object.parent_object_index==NONE)
{
real radius;
unit_indices[unit_count] = unit_index;
object_get_bounding_sphere(unit_index, &unit_centers[unit_count], &radius);
unit_count++;
}
}
}
if (unit_count)
{
struct object_iterator iterator;
struct object_datum *object;
object_iterator_new(&iterator, _object_mask_vehicle, 0);
do
{
object = (struct object_datum *)object_iterator_next(&iterator);
if (object)
{
short index;
for (index = 0; index<unit_count; index++)
{
if (((struct unit_datum *)unit_get(unit_indices[index]))->object.parent_object_index!=iterator.index
&& distance_squared3d(&unit_centers[index], &object->object.bounding_sphere_center)<100.0f
&& magnitude_squared3d(&object->object.translational_velocity)>=(real)((1.0/30.0)*(1.0/30.0)))
{
no_vehicles_moving = FALSE;
goto done;
}
}
}
} while (object);
}
done:
return !no_vehicles_moving;
}
long vehicle_find_pathfinding_surface_index(
long vehicle_index,
real_point3d *position)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct vehicle_definition *definition = vehicle_specific_definition_get(
vehicle->definition_index);
long surface_index = NONE;
object_get_origin(vehicle_index, position);
switch (definition->vehicle_type)
{
case _vehicle_type_human_tank:
case _vehicle_type_human_jeep:
case _vehicle_type_alien_scout:
case _vehicle_type_turret:
{
struct collision_bsp *bsp = global_collision_bsp_get();
struct collision_bsp_test_vector_result result;
real_point3d origin;
real_vector3d vector;
object_get_origin(vehicle_index, &origin);
point_from_line3d(&origin, global_up3d, 0.4f, &origin);
add_vectors3d(global_down3d, global_down3d, &vector);
if (collision_bsp_test_vector(1, bsp, 0, NULL, &origin, &vector, FLT_MAX, &result))
{
surface_index = result.surface_index;
point_from_line3d(&origin, &vector, result.t, position);
}
break;
}
}
return surface_index;
}
static void compute_airborne_ticks(
long vehicle_index,
struct mass_point_datum *mass_points,
void *state)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct physics_definition *physics = physics_definition_get(
vehicle_definition_get(vehicle->definition_index)->object.physics.index);
short mass_point_index;
if (vehicle->vehicle.airborne_ticks<0xff)
vehicle->vehicle.airborne_ticks++;
for (mass_point_index = 0; mass_point_index<physics->mass_points.count; mass_point_index++)
{
struct mass_point_datum *mass_point = &mass_points[mass_point_index];
if (TEST_FLAG(mass_point->flags, 1))
{
vehicle->vehicle.airborne_ticks = 0;
if (vehicle->vehicle.on_ground_ticks<0xff)
vehicle->vehicle.on_ground_ticks++;
return;
}
if (TEST_FLAG(mass_point->flags, 4))
vehicle->vehicle.airborne_ticks = 0;
}
vehicle->vehicle.on_ground_ticks = 0;
return;
}
static real_vector3d *compute_acceleration(
real_vector3d *a,
real_vector3d *b,
real_vector3d *result,
real maximum,
real minimum);
static void update_alien_fighter_physics_new(
long vehicle_index,
struct powered_mass_point_datum *state,
struct mass_point_datum *mass_points)
{
struct vehicle_datum *vehicle = vehicle_datum_get(vehicle_index);
struct vehicle_definition *definition = vehicle_specific_definition_get(
vehicle->definition_index);
struct physics_definition *physics = physics_definition_get(
definition->unit.object.physics.index);
if (physics->powered_mass_points.count==2)
{
real_vector3d magic_force;
real_vector3d magic_torque;
real_vector3d axis;
real_vector3d desired_angular_velocity;
real_vector3d angular_acceleration;
real spin;
real thrust_delta;
{
real_vector3d desired_velocity;
real_vector3d acceleration;
real throttle;
scale_vector3d(&vehicle->object.forward, vehicle->vehicle.speed, &desired_velocity);
if (vehicle->vehicle.speed>0.0f)
throttle = vehicle->vehicle.speed/definition->unknown2f8;
else
throttle = -(vehicle->vehicle.speed/definition->unknown2fc);
compute_acceleration(&desired_velocity, &vehicle->object.translational_velocity, &acceleration,
throttle*definition->unknown300, throttle*definition->unknown304);
scale_vector3d(&acceleration, physics->mass, &magic_force);
scale_vector3d(&magic_force, vehicle->unit.seat_power[0], &magic_force);
}
{
real_matrix3x3 current_rotation, desired_rotation, rotation;
real_quaternion rotation_quaternion;
real yaw;
real angle;
matrix3x3_from_forward_and_up(¤t_rotation, &vehicle->object.forward,
&vehicle->object.up);
desired_rotation.forward = vehicle->unit.desired_facing_vector;
point_from_line3d(global_up3d, &desired_rotation.forward, -desired_rotation.forward.k, &desired_rotation.up);
if (normalize3d(&desired_rotation.up)==0.0f)
desired_rotation.up = *global_forward3d;
if (!unit_driven_by_ai(vehicle_index))
pitch_vectors(&desired_rotation.forward, &desired_rotation.up, sine(definition->unknown364),
cosine(definition->unknown364));
yaw = cross_product2d((real_vector2d const *)&desired_rotation.forward,
(real_vector2d const *)&vehicle->object.translational_velocity)/
definition->unknown2f8*definition->unknown308;
yaw_vectors(&desired_rotation.up, &desired_rotation.forward, sine(yaw), cosine(yaw));