forked from spring/spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrafeAirMoveType.cpp
More file actions
1412 lines (1089 loc) · 46.9 KB
/
StrafeAirMoveType.cpp
File metadata and controls
1412 lines (1089 loc) · 46.9 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "StrafeAirMoveType.h"
#include "Game/Players/Player.h"
#include "Map/Ground.h"
#include "Map/MapInfo.h"
#include "Map/ReadMap.h"
#include "Sim/Misc/GeometricObjects.h"
#include "Sim/Misc/GroundBlockingObjectMap.h"
#include "Sim/Misc/ModInfo.h"
#include "Sim/Misc/QuadField.h"
#include "Sim/Units/Scripts/UnitScript.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "Sim/Weapons/Weapon.h"
#include "System/SpringMath.h"
#include "System/Sync/HsiehHash.h"
CR_BIND_DERIVED(CStrafeAirMoveType, AAirMoveType, (nullptr))
CR_REG_METADATA(CStrafeAirMoveType, (
CR_MEMBER(maneuverBlockTime),
CR_MEMBER(maneuverState),
CR_MEMBER(maneuverSubState),
CR_MEMBER(loopbackAttack),
CR_MEMBER(isFighter),
CR_MEMBER(wingDrag),
CR_MEMBER(wingAngle),
CR_MEMBER(invDrag),
CR_MEMBER(crashDrag),
CR_MEMBER(frontToSpeed),
CR_MEMBER(speedToFront),
CR_MEMBER(myGravity),
CR_MEMBER(maxBank),
CR_MEMBER(maxPitch),
CR_MEMBER(turnRadius),
CR_MEMBER(maxAileron),
CR_MEMBER(maxElevator),
CR_MEMBER(maxRudder),
CR_MEMBER(attackSafetyDistance),
CR_MEMBER(crashAileron),
CR_MEMBER(crashElevator),
CR_MEMBER(crashRudder),
CR_MEMBER(lastRudderPos),
CR_MEMBER(lastElevatorPos),
CR_MEMBER(lastAileronPos)
))
#define MEMBER_CHARPTR_HASH(memberName) HsiehHash(memberName, strlen(memberName), 0)
#define MEMBER_LITERAL_HASH(memberName) HsiehHash(memberName, sizeof(memberName) - 1, 0)
static const unsigned int BOOL_MEMBER_HASHES[] = {
MEMBER_LITERAL_HASH( "collide"),
MEMBER_LITERAL_HASH( "useSmoothMesh"),
MEMBER_LITERAL_HASH("loopbackAttack"),
};
static const unsigned int INT_MEMBER_HASHES[] = {
MEMBER_LITERAL_HASH("maneuverBlockTime"),
};
static const unsigned int FLOAT_MEMBER_HASHES[] = {
MEMBER_LITERAL_HASH( "wantedHeight"),
MEMBER_LITERAL_HASH( "turnRadius"),
MEMBER_LITERAL_HASH( "accRate"),
MEMBER_LITERAL_HASH( "decRate"),
MEMBER_LITERAL_HASH( "maxAcc"), // synonym for accRate
MEMBER_LITERAL_HASH( "maxDec"), // synonym for decRate
MEMBER_LITERAL_HASH( "maxBank"),
MEMBER_LITERAL_HASH( "maxPitch"),
MEMBER_LITERAL_HASH( "maxAileron"),
MEMBER_LITERAL_HASH( "maxElevator"),
MEMBER_LITERAL_HASH( "maxRudder"),
MEMBER_LITERAL_HASH("attackSafetyDistance"),
MEMBER_LITERAL_HASH( "myGravity"),
};
#undef MEMBER_CHARPTR_HASH
#undef MEMBER_LITERAL_HASH
extern AAirMoveType::GetGroundHeightFunc amtGetGroundHeightFuncs[6];
extern AAirMoveType::EmitCrashTrailFunc amtEmitCrashTrailFuncs[2];
static float TurnRadius(const float rawRadius, const float rawSpeed) {
return (std::min(1000.0f, rawRadius * rawSpeed));
}
static float GetRudderDeflection(
const CUnit* owner,
const CUnit* /*collidee*/,
const float3& pos,
const float4& spd,
const SyncedFloat3& rightdir,
const SyncedFloat3& updir,
const SyncedFloat3& frontdir,
const float3& goalDir,
float groundHeight,
float wantedHeight,
float maxRudder,
float /*maxYaw*/,
float goalDotRight,
float goalDotFront,
bool /*avoidCollision*/,
bool isAttacking
) {
float rudder = 0.0f;
const float minGroundHeight = std::min(groundHeight + 15.0f, wantedHeight) * (1.0f + isAttacking);
const float maxRudderSpeedf = std::max(0.01f, maxRudder * spd.w * (1.0f + isAttacking));
const float minHeightMult = (pos.y >= minGroundHeight);
rudder -= (1.0f * minHeightMult * (goalDotRight < -maxRudderSpeedf) );
rudder += (1.0f * minHeightMult * (goalDotRight > maxRudderSpeedf) );
rudder += (1.0f * minHeightMult * (goalDotRight / maxRudderSpeedf) * (rudder == 0.0f));
// we have to choose a direction in case our target is almost straight behind us
rudder += (1.0f * minHeightMult * ((goalDotRight < 0.0f)? -0.01f: 0.01f) * (math::fabs(rudder) < 0.01f && goalDotFront < 0.0f));
return rudder;
}
static float GetAileronDeflection(
const CUnit* owner,
const CUnit* /*collidee*/,
const float3& pos,
const float4& spd,
const SyncedFloat3& rightdir,
const SyncedFloat3& updir,
const SyncedFloat3& frontdir,
const float3& goalDir,
float groundHeight,
float wantedHeight,
float maxAileron,
float maxBank,
float goalDotRight,
float goalDotFront,
bool /*avoidCollision*/,
bool isAttacking
) {
float aileron = 0.0f;
// ailerons function less effectively at low (forward) speed
const float maxAileronSpeedf = std::max(0.01f, maxAileron * spd.w);
const float maxAileronSpeedf2 = std::max(0.01f, maxAileronSpeedf * 4.0f);
if (isAttacking) {
const float minPredictedHeight = pos.y + spd.y * 60.0f * math::fabs(frontdir.y) + std::min(0.0f, updir.y * 1.0f) * (GAME_SPEED * 5);
const float maxPredictedHeight = groundHeight + 60.0f + math::fabs(rightdir.y) * (GAME_SPEED * 5);
const float minSpeedMult = (spd.w > 0.45f && minPredictedHeight > maxPredictedHeight);
const float goalBankDif = goalDotRight + rightdir.y * 0.2f;
aileron += (1.0f * ( minSpeedMult) * (goalBankDif > maxAileronSpeedf2) );
aileron -= (1.0f * ( minSpeedMult) * (goalBankDif < -maxAileronSpeedf2) );
aileron += (1.0f * ( minSpeedMult) * (goalBankDif / maxAileronSpeedf2) * (aileron == 0.0f));
aileron += (1.0f * (1.0f - minSpeedMult) * (rightdir.y > 0.0f) * (rightdir.y > maxAileronSpeedf || frontdir.y < -0.7f));
aileron += (1.0f * (1.0f - minSpeedMult) * (aileron == 0.0f) * (rightdir.y > 0.0f) * (rightdir.y / maxAileronSpeedf ));
aileron -= (1.0f * (1.0f - minSpeedMult) * (rightdir.y < 0.0f) * (rightdir.y < -maxAileronSpeedf || frontdir.y < -0.7f));
aileron += (1.0f * (1.0f - minSpeedMult) * (aileron == 0.0f) * (rightdir.y < 0.0f) * (rightdir.y / maxAileronSpeedf ));
} else {
const float minPredictedHeight = pos.y + spd.y * 10.0f;
const float maxPredictedHeight = groundHeight + wantedHeight * 0.6f;
const float absRightDirY = math::fabs(rightdir.y);
const float goalBankDif = goalDotRight + rightdir.y * 0.5f;
const float relBankMult = (absRightDirY < maxBank && maxAileronSpeedf2 > 0.0f);
const float minSpeedMult = (spd.w > 1.5f && minPredictedHeight > maxPredictedHeight);
const float clampedBank = 1.0f;
// using this directly does not function well at higher pitch-angles, interplay with GetElevatorDeflection
// const float clampedBankAbs = math::fabs(Clamp(absRightDirY - maxBank, -1.0f, 1.0f));
// const float clampedBank = std::max(clampedBankAbs, 0.3f);
aileron -= (clampedBank * minSpeedMult * (goalBankDif < -maxAileronSpeedf2 && rightdir.y < maxBank));
aileron += (clampedBank * minSpeedMult * (goalBankDif > maxAileronSpeedf2 && rightdir.y > -maxBank));
/// NB: these ignore maxBank
aileron += (1.0f * minSpeedMult * (aileron == 0.0f) * ( relBankMult) * (goalBankDif / maxAileronSpeedf2));
aileron -= (1.0f * minSpeedMult * (aileron == 0.0f) * (1.0f - relBankMult) * (rightdir.y < 0.0f && goalBankDif < 0.0f));
aileron += (1.0f * minSpeedMult * (aileron == 0.0f) * (1.0f - relBankMult) * (rightdir.y > 0.0f && goalBankDif > 0.0f));
// if right wing too high, roll right (cw)
// if right wing too low, roll left (ccw)
aileron += std::copysign(1.0f, float(rightdir.y)) * (1.0f - minSpeedMult) * (absRightDirY > maxAileronSpeedf);
}
return aileron;
}
static float GetElevatorDeflection(
const CUnit* owner,
const CUnit* collidee,
const float3& pos,
const float4& spd,
const SyncedFloat3& rightdir,
const SyncedFloat3& updir,
const SyncedFloat3& frontdir,
const float3& goalDir,
float groundHeight,
float wantedHeight,
float maxElevator,
float maxPitch,
float goalDotRight,
float goalDotFront,
bool avoidCollision,
bool isAttacking
) {
float elevator = 0.0f;
const float upside = (updir.y >= -0.3f) * 2.0f - 1.0f;
const float speedMult = (spd.w >= 1.5f);
if (collidee == nullptr || !avoidCollision)
collidee = owner;
if (isAttacking) {
elevator += (upside * (1.0f - speedMult) * (frontdir.y < (-maxElevator * spd.w)));
elevator -= (upside * (1.0f - speedMult) * (frontdir.y > ( maxElevator * spd.w)));
const float posHeight = CGround::GetHeightAboveWater(pos.x + spd.x * 40.0f, pos.z + spd.z * 40.0f);
const float difHeight = std::max(posHeight, groundHeight) + 60 - pos.y - frontdir.y * spd.w * 20.0f;
const float goalDotY = goalDir.dot(updir);
const float maxElevatorSpeedf = std::max(0.01f, maxElevator * spd.w );
const float maxElevatorSpeedf2 = std::max(0.01f, maxElevatorSpeedf * spd.w * 20.0f);
// const float ydirMult = (updir.dot(collidee->midPos - owner->midPos) > 0.0f);
const float zdirMult = (frontdir.dot(collidee->pos + collidee->speed * 20.0f - pos - spd * 20.0f) < 0.0f);
float minPitch = 0.0f;
minPitch -= (1.0f * speedMult * (difHeight < -maxElevatorSpeedf2));
minPitch += (1.0f * speedMult * (difHeight > maxElevatorSpeedf2));
minPitch += (1.0f * speedMult * (minPitch == 0.0f) * (difHeight / maxElevatorSpeedf2));
// busted collision avoidance
// elevator += ((ydirMult * -2.0f + 1.0f) * zdirMult * speedMult * (collidee != owner));
elevator -= (1.0f * speedMult * (goalDotY < -maxElevatorSpeedf) * (collidee == owner) * (1.0f - zdirMult));
elevator += (1.0f * speedMult * (goalDotY > maxElevatorSpeedf) * (collidee == owner) * (1.0f - zdirMult));
elevator += (1.0f * speedMult * (elevator == 0.0f) * (goalDotY / maxElevatorSpeedf) * (collidee == owner) * (1.0f - zdirMult));
elevator = mix(elevator, minPitch * upside, (elevator * upside) < minPitch && spd.w >= 1.5f);
} else {
#if 0
bool colliding = false;
if (avoidCollision) {
const float3 cvec = collidee->midPos - owner->midPos;
const float3 svec = collidee->speed - spd;
// pitch down or up based on relative direction to
// a (collisionState == COLLISION_DIRECT) collidee
// this usually just results in jittering, callers
// disable it
// FIXME? zdirDot < 0 means collidee is behind us
const float ydirDot = updir.dot(cvec) * -1.0f;
const float zdirDot = frontdir.dot(cvec + svec * 20.0f);
elevator = Sign(ydirDot) * (spd.w > 0.8f) * (colliding = (zdirDot < 0.0f));
}
if (!colliding) {
#endif
{
const float maxElevatorSpeedf = std::max(0.001f, maxElevator * 20.0f * spd.w * spd.w);
const float posHeight = CGround::GetHeightAboveWater(pos.x + spd.x * 40.0f, pos.z + spd.z * 40.0f);
const float difHeight = std::max(groundHeight, posHeight) + wantedHeight - pos.y - (frontdir.y * spd.w * 20.0f);
const float absFrontDirY = math::fabs(frontdir.y);
const float clampedPitch = 1.0f;
// using this directly does not function well at higher bank-angles
// const float clampedPitchAbs = math::fabs(Clamp(absFrontDirY - maxPitch, -1.0f, 1.0f));
// const float clampedPitch = std::max(clampedPitchAbs, 0.3f);
elevator -= (clampedPitch * (spd.w > 0.8f) * (difHeight < -maxElevatorSpeedf && frontdir.y > -maxPitch));
elevator += (clampedPitch * (spd.w > 0.8f) * (difHeight > maxElevatorSpeedf && frontdir.y < maxPitch));
elevator += (1.0f * (elevator == 0.0f) * (spd.w > 0.8f) * (difHeight / maxElevatorSpeedf) * (absFrontDirY < maxPitch));
}
elevator += (1.0f * (spd.w < 0.8f) * (frontdir.y < -0.10f));
elevator -= (1.0f * (spd.w < 0.8f) * (frontdir.y > 0.15f));
}
return elevator;
}
static float3 GetControlSurfaceAngles(
const CUnit* owner,
const CUnit* collidee,
const float3& pos,
const float4& spd,
const SyncedFloat3& rightdir,
const SyncedFloat3& updir,
const SyncedFloat3& frontdir,
const float3& goalDir,
const float3& yprInputLocks,
const float3& maxBodyAngles, // .x := maxYaw, .y := maxPitch, .z := maxBank
const float3& maxCtrlAngles, // .x := maxRudder, .y := maxElevator, .z := maxAileron
const float3* prvCtrlAngles,
float groundHeight,
float wantedHeight,
float goalDotRight,
float goalDotFront,
bool avoidCollision,
bool isAttacking
) {
float3 ctrlAngles;
// yaw (rudder), pitch (elevator), roll (aileron)
ctrlAngles.x = (yprInputLocks.x != 0.0f)? GetRudderDeflection (owner, collidee, pos, spd, rightdir, updir, frontdir, goalDir, groundHeight, wantedHeight, maxCtrlAngles.x, maxBodyAngles.x, goalDotRight, goalDotFront, avoidCollision, isAttacking): 0.0f;
ctrlAngles.y = (yprInputLocks.y != 0.0f)? GetElevatorDeflection(owner, collidee, pos, spd, rightdir, updir, frontdir, goalDir, groundHeight, wantedHeight, maxCtrlAngles.y, maxBodyAngles.y, goalDotRight, goalDotFront, avoidCollision, isAttacking): 0.0f;
ctrlAngles.z = (yprInputLocks.z != 0.0f)? GetAileronDeflection (owner, collidee, pos, spd, rightdir, updir, frontdir, goalDir, groundHeight, wantedHeight, maxCtrlAngles.z, maxBodyAngles.z, goalDotRight, goalDotFront, avoidCollision, isAttacking): 0.0f;
// let the previous control angles have some authority
return (ctrlAngles * 0.6f + prvCtrlAngles[0] * 0.3f + prvCtrlAngles[1] * 0.1f);
}
static int SelectLoopBackManeuver(
const SyncedFloat3& frontdir,
const SyncedFloat3& rightdir,
const float4& spd,
float turnRadius,
float groundDist
) {
// do not start looping if already banked
if (math::fabs(rightdir.y) > 0.05f)
return CStrafeAirMoveType::MANEUVER_FLY_STRAIGHT;
if (groundDist > TurnRadius(turnRadius, spd.w)) {
if (math::fabs(frontdir.y) <= 0.2f && gsRNG.NextFloat() > 0.3f)
return CStrafeAirMoveType::MANEUVER_IMMELMAN_INV;
} else {
if (frontdir.y > -0.2f && gsRNG.NextFloat() > 0.7f)
return CStrafeAirMoveType::MANEUVER_IMMELMAN;
}
return CStrafeAirMoveType::MANEUVER_FLY_STRAIGHT;
}
CStrafeAirMoveType::CStrafeAirMoveType(CUnit* owner): AAirMoveType(owner)
{
maneuverBlockTime = GAME_SPEED * 3;
// creg
if (owner == nullptr)
return;
assert(owner->unitDef != nullptr);
isFighter = owner->unitDef->IsFighterAirUnit();
loopbackAttack = owner->unitDef->canLoopbackAttack && isFighter;
wingAngle = owner->unitDef->wingAngle;
crashDrag = 1.0f - owner->unitDef->crashDrag;
frontToSpeed = owner->unitDef->frontToSpeed;
speedToFront = owner->unitDef->speedToFront;
myGravity = math::fabs(owner->unitDef->myGravity);
maxBank = owner->unitDef->maxBank;
maxPitch = owner->unitDef->maxPitch;
turnRadius = owner->unitDef->turnRadius;
wantedHeight =
(owner->unitDef->wantedHeight * 1.5f) +
((gsRNG.NextFloat() - 0.3f) * 15.0f * (isFighter? 2.0f: 1.0f));
orgWantedHeight = wantedHeight;
maxAileron = owner->unitDef->maxAileron;
maxElevator = owner->unitDef->maxElevator;
maxRudder = owner->unitDef->maxRudder;
attackSafetyDistance = 0.0f;
maxRudder *= (0.99f + gsRNG.NextFloat() * 0.02f);
maxElevator *= (0.99f + gsRNG.NextFloat() * 0.02f);
maxAileron *= (0.99f + gsRNG.NextFloat() * 0.02f);
accRate *= (0.99f + gsRNG.NextFloat() * 0.02f);
// magic constant ensures the same (0.25) average as urand() * urand()
crashAileron = 1.0f - Square(gsRNG.NextFloat() * 0.8660254037844386f);
crashAileron *= ((gsRNG.NextInt(2) == 1)? -1.0f: 1.0f);
crashElevator = gsRNG.NextFloat();
crashRudder = gsRNG.NextFloat() - 0.5f;
SetMaxSpeed(maxSpeedDef);
}
bool CStrafeAirMoveType::Update()
{
const float3 lastPos = owner->pos;
const float4 lastSpd = owner->speed;
AAirMoveType::Update();
// need to additionally check that we are not crashing,
// otherwise we might fall through the map when stunned
// (the kill-on-impact code is not reached in that case)
if ((owner->IsStunned() && !owner->IsCrashing()) || owner->beingBuilt) {
UpdateAirPhysics({0.0f * lastRudderPos[0], lastElevatorPos[0], lastAileronPos[0], 0.0f}, ZeroVector);
return (HandleCollisions(collide && !owner->beingBuilt && (aircraftState != AIRCRAFT_TAKEOFF)));
}
if (aircraftState != AIRCRAFT_CRASHING) {
if (owner->UnderFirstPersonControl()) {
SetState(AIRCRAFT_FLYING);
const CPlayer* fpsPlayer = owner->fpsControlPlayer;
const FPSUnitController& fpsCon = fpsPlayer->fpsController;
float aileron = 0.0f;
float elevator = 0.0f;
elevator -= (1.0f * fpsCon.forward);
elevator += (1.0f * fpsCon.back );
aileron += (1.0f * fpsCon.right );
aileron -= (1.0f * fpsCon.left );
UpdateAirPhysics({0.0f, elevator, aileron, 1.0f}, owner->frontdir);
maneuverState = MANEUVER_FLY_STRAIGHT;
return (HandleCollisions(collide && !owner->beingBuilt && (aircraftState != AIRCRAFT_TAKEOFF)));
}
}
switch (aircraftState) {
case AIRCRAFT_FLYING: {
const CCommandQueue& cmdQue = owner->commandAI->commandQue;
const bool isAttacking = (!cmdQue.empty() && (cmdQue.front()).GetID() == CMD_ATTACK);
const bool keepAttacking = ((owner->curTarget.type == Target_Unit && !owner->curTarget.unit->isDead) || owner->curTarget.type == Target_Pos);
/*
const float brakeDistSq = Square(0.5f * lastSpd.SqLength2D() / decRate);
const float goalDistSq = (goalPos - lastPos).SqLength2D();
if (brakeDistSq >= goalDistSq && !owner->commandAI->HasMoreMoveCommands()) {
SetState(AIRCRAFT_LANDING);
} else
*/
{
if (isAttacking && keepAttacking) {
switch (owner->curTarget.type) {
case Target_None: { } break;
case Target_Unit: { SetGoal(owner->curTarget.unit->pos); } break;
case Target_Pos: { SetGoal(owner->curTarget.groundPos); } break;
case Target_Intercept: { } break;
}
const bool goalInFront = ((goalPos - lastPos).dot(owner->frontdir) > 0.0f);
const bool goalInRange = (goalPos.SqDistance(lastPos) < Square(owner->maxRange * 4.0f));
// NOTE: UpdateAttack changes goalPos
if (maneuverState != MANEUVER_FLY_STRAIGHT) {
UpdateManeuver();
} else if (goalInFront && goalInRange) {
UpdateAttack();
} else {
if (UpdateFlying(wantedHeight, 1.0f) && !goalInFront && loopbackAttack) {
// once yaw and roll are unblocked, semi-randomly decide to turn or loop
const SyncedFloat3& rightdir = owner->rightdir;
const SyncedFloat3& frontdir = owner->frontdir;
const float altitude = CGround::GetHeightAboveWater(owner->pos.x, owner->pos.z) - lastPos.y;
if ((maneuverState = SelectLoopBackManeuver(frontdir, rightdir, lastSpd, turnRadius, altitude)) == MANEUVER_IMMELMAN_INV)
maneuverSubState = 0;
}
}
} else {
UpdateFlying(wantedHeight, 1.0f);
}
}
} break;
case AIRCRAFT_LANDED:
UpdateLanded();
break;
case AIRCRAFT_LANDING:
UpdateLanding();
break;
case AIRCRAFT_CRASHING: {
// NOTE: the crashing-state can only be set (and unset) by scripts
UpdateAirPhysics({crashRudder, crashElevator, crashAileron, 0.0f}, owner->frontdir);
if ((CGround::GetHeightAboveWater(owner->pos.x, owner->pos.z) + 5.0f + owner->radius) > owner->pos.y)
owner->ForcedKillUnit(nullptr, true, false);
amtEmitCrashTrailFuncs[crashExpGenID != -1u](owner, crashExpGenID);
} break;
case AIRCRAFT_TAKEOFF:
UpdateTakeOff();
break;
default:
break;
}
if (lastSpd == ZeroVector && owner->speed != ZeroVector) { owner->script->StartMoving(false); }
if (lastSpd != ZeroVector && owner->speed == ZeroVector) { owner->script->StopMoving(); }
return (HandleCollisions(collide && !owner->beingBuilt && (aircraftState != AIRCRAFT_TAKEOFF)));
}
bool CStrafeAirMoveType::HandleCollisions(bool checkCollisions) {
const float3& pos = owner->pos;
#ifdef DEBUG_AIRCRAFT
switch (collisionState) {
case COLLISION_NEARBY: {
const int g = geometricObjects->AddLine(pos, lastCollidee->pos, 10, 1, 1);
geometricObjects->SetColor(g, 0.2f, 1, 0.2f, 0.6f);
} break;
case COLLISION_DIRECT: {
const int g = geometricObjects->AddLine(pos, lastCollidee->pos, 10, 1, 1);
if (owner->frontdir.dot(lastCollidee->midPos + lastCollidee->speed * 20.0f - owner->midPos - spd * 20.0f) < 0) {
geometricObjects->SetColor(g, 1, 0.2f, 0.2f, 0.6f);
} else {
geometricObjects->SetColor(g, 1, 1, 0.2f, 0.6f);
}
} break;
default: {
} break;
}
#endif
if (pos != oldPos) {
oldPos = pos;
bool hitBuilding = false;
// check for collisions if not being built or not taking off
if (checkCollisions) {
// copy on purpose, since the below can call Lua
QuadFieldQuery qfQuery;
quadField.GetUnitsExact(qfQuery, pos, owner->radius + 6);
for (CUnit* unit: *qfQuery.units) {
const bool unloadingUnit = ( unit->unloadingTransportId == owner->id);
const bool unloadingOwner = (owner->unloadingTransportId == unit->id);
const bool loadingUnit = ( unit->id == owner->loadingTransportId);
const bool loadingOwner = (owner->id == unit->loadingTransportId);
if (unloadingUnit)
unit->unloadingTransportId = -1;
if (unloadingOwner)
owner->unloadingTransportId = -1;
if (loadingUnit || loadingOwner || unit == owner->transporter || unit->transporter != nullptr)
continue;
const float sqDist = (pos - unit->pos).SqLength();
const float totRad = owner->radius + unit->radius;
if (sqDist <= 0.1f || sqDist >= (totRad * totRad))
continue;
if (unloadingUnit) {
unit->unloadingTransportId = owner->id;
continue;
}
if (unloadingOwner) {
owner->unloadingTransportId = unit->id;
continue;
}
const float dist = math::sqrt(sqDist);
const float3 dif = (pos - unit->pos).Normalize();
if (unit->immobile) {
const float damage = ((unit->speed - owner->speed) * 0.1f).SqLength();
owner->Move(-dif * (dist - totRad), true);
owner->SetVelocity(owner->speed * 0.99f);
if (modInfo.allowUnitCollisionDamage) {
owner->DoDamage(DamageArray(damage), ZeroVector, nullptr, -CSolidObject::DAMAGE_COLLISION_OBJECT, -1);
unit->DoDamage(DamageArray(damage), ZeroVector, nullptr, -CSolidObject::DAMAGE_COLLISION_OBJECT, -1);
}
hitBuilding = true;
} else {
const float part = owner->mass / (owner->mass + unit->mass);
const float damage = ((unit->speed - owner->speed) * 0.1f).SqLength();
owner->Move(-dif * (dist - totRad) * (1 - part), true);
owner->SetVelocity(owner->speed * 0.99f);
if (!unit->UsingScriptMoveType())
unit->Move(dif * (dist - totRad) * (part), true);
if (modInfo.allowUnitCollisionDamage) {
owner->DoDamage(DamageArray(damage), ZeroVector, nullptr, -CSolidObject::DAMAGE_COLLISION_OBJECT, -1);
unit->DoDamage(DamageArray(damage), ZeroVector, nullptr, -CSolidObject::DAMAGE_COLLISION_OBJECT, -1);
}
}
}
// update speed.w
owner->SetSpeed(owner->speed);
}
if (hitBuilding && owner->IsCrashing()) {
// if crashing and we hit a building, die right now
// rather than waiting until we are close enough to
// the ground
owner->ForcedKillUnit(nullptr, true, false);
return true;
}
if (pos.x < 0.0f) {
owner->Move( RgtVector * 1.5f, true);
} else if (pos.x > float3::maxxpos) {
owner->Move(-RgtVector * 1.5f, true);
}
if (pos.z < 0.0f) {
owner->Move( FwdVector * 1.5f, true);
} else if (pos.z > float3::maxzpos) {
owner->Move(-FwdVector * 1.5f, true);
}
return true;
}
return false;
}
void CStrafeAirMoveType::SlowUpdate()
{
// note: NOT AAirMoveType::SlowUpdate
AMoveType::SlowUpdate();
}
void CStrafeAirMoveType::UpdateManeuver()
{
const float speedf = owner->speed.w;
switch (maneuverState) {
case MANEUVER_IMMELMAN: {
float aileron = 0.0f;
float elevator = 1.0f * (math::fabs(owner->rightdir.y) < maxAileron * 3.0f * speedf || owner->updir.y < 0.0f);
aileron += (1.0f * (owner->updir.y > 0.0f) * (owner->rightdir.y > maxAileron * speedf));
aileron -= (1.0f * (owner->updir.y > 0.0f) * (owner->rightdir.y < -maxAileron * speedf));
UpdateAirPhysics({0.0f, elevator, aileron, 1.0f}, owner->frontdir);
if ((owner->updir.y < 0.0f && owner->frontdir.y < 0.0f) || speedf < 0.8f)
maneuverState = MANEUVER_FLY_STRAIGHT;
// [?] some seem to report that the "unlimited altitude" thing is because of these maneuvers
if ((owner->pos.y - CGround::GetApproximateHeight(owner->pos.x, owner->pos.z)) > (wantedHeight * 4.0f))
maneuverState = MANEUVER_FLY_STRAIGHT;
} break;
case MANEUVER_IMMELMAN_INV: {
// inverted Immelman
float aileron = 0.0f;
float elevator = 0.0f;
aileron -= (1.0f * (maneuverSubState == 0) * (owner->rightdir.y >= 0.0f));
aileron += (1.0f * (maneuverSubState == 0) * (owner->rightdir.y < 0.0f));
if (owner->frontdir.y < -0.7f)
maneuverSubState = 1;
if (maneuverSubState == 1 || owner->updir.y < 0.0f)
elevator = 1.0f;
UpdateAirPhysics({0.0f, elevator, aileron, 1.0f}, owner->frontdir);
if ((owner->updir.y > 0.0f && owner->frontdir.y > 0.0f && maneuverSubState == 1) || speedf < 0.2f)
maneuverState = MANEUVER_FLY_STRAIGHT;
} break;
default: {
UpdateAirPhysics({0.0f, 0.0f, 0.0f, 1.0f}, owner->frontdir);
maneuverState = MANEUVER_FLY_STRAIGHT;
} break;
}
}
void CStrafeAirMoveType::UpdateAttack()
{
if (!isFighter) {
UpdateFlying(wantedHeight, 1.0f);
return;
}
const float3& pos = owner->pos;
const float4& spd = owner->speed;
const SyncedFloat3& rightdir = owner->rightdir;
const SyncedFloat3& frontdir = owner->frontdir;
const SyncedFloat3& updir = owner->updir;
if (spd.w < 0.01f) {
UpdateAirPhysics({0.0f, 0.0f, 0.0f, 1.0f}, owner->frontdir);
return;
}
if (((gs->frameNum + owner->id) & 3) == 0)
CheckForCollision();
float3 rightDir2D = rightdir;
const float3 difGoalPos = (goalPos - oldGoalPos) * SQUARE_SIZE;
oldGoalPos = goalPos;
goalPos += difGoalPos;
const float gHeightAW = CGround::GetHeightAboveWater(pos.x, pos.z);
const float goalDist = pos.distance(goalPos);
const float3 goalDir = (goalDist > 0.0f)?
(goalPos - pos) / goalDist:
ZeroVector;
// if goal too close, stop dive and resume flying at normal desired height
// to avoid colliding with target, evade blast, friendly and enemy fire, etc.
if (goalDist < attackSafetyDistance) {
UpdateFlying(wantedHeight, 1.0f);
return;
}
float goalDotRight = goalDir.dot(rightDir2D.Normalize2D());
const float goalDotFront = goalDir.dot(frontdir);
const float goalDotFront01 = goalDotFront * 0.5f + 0.501f; // [0,1]
if (goalDotFront01 != 0.0f)
goalDotRight /= goalDotFront01;
{
const float3 maxBodyAngles = {0.0f, maxPitch, maxBank};
const float3 maxCtrlAngles = {maxRudder, maxElevator, maxAileron};
const float3 prvCtrlAngles[2] = {{lastRudderPos[0], lastElevatorPos[0], lastAileronPos[0]}, {lastRudderPos[1], lastElevatorPos[1], lastAileronPos[1]}};
const float3& curCtrlAngles = GetControlSurfaceAngles(owner, lastCollidee, pos, spd, rightdir, updir, frontdir, goalDir, OnesVector, maxBodyAngles, maxCtrlAngles, prvCtrlAngles, gHeightAW, wantedHeight, goalDotRight, goalDotFront, false && collisionState == COLLISION_DIRECT, true);
const CUnit* attackee = owner->curTarget.unit;
// limit thrust when in range of (air) target and directly behind it
const float rangeLim = goalDist / owner->maxRange;
const float angleLim = 1.0f - goalDotFront * 0.7f;
const float thrustLim = ((attackee == nullptr) || attackee->unitDef->IsGroundUnit())? 1.0f: std::min(1.0f, rangeLim + angleLim);
UpdateAirPhysics({curCtrlAngles.x, curCtrlAngles.y, curCtrlAngles.z, thrustLim}, frontdir);
}
}
bool CStrafeAirMoveType::UpdateFlying(float wantedHeight, float wantedThrottle)
{
const float3& pos = owner->pos;
const float4& spd = owner->speed;
const SyncedFloat3& rightdir = owner->rightdir;
const SyncedFloat3& frontdir = owner->frontdir;
const SyncedFloat3& updir = owner->updir;
// NOTE:
// turnRadius is often way too small, but cannot calculate one
// because we have no turnRate (and unitDef->turnRate can be 0)
// --> would lead to infinite circling without adjusting goal
const float3 goalVec = goalPos - pos;
const float goalDist2D = std::max(0.001f, goalVec.Length2D());
// const float goalDist3D = std::max(0.001f, goalVec.Length());
const float3 goalDir2D = goalVec / goalDist2D;
// const float3 goalDir3D = goalVec / goalDist3D;
const float3 rightDir2D = (rightdir * XZVector).Normalize2D();
if (((gs->frameNum + owner->id) & 3) == 0)
CheckForCollision();
// RHS is needed for moving targets (when called by UpdateAttack)
// yaw and roll have to be unconditionally unblocked after a certain
// time or aircraft can fly straight forever e.g. if their target is
// another chasing aircraft
const bool allowUnlockYawRoll = (goalDist2D >= TurnRadius(turnRadius, spd.w) || goalVec.dot(owner->frontdir) > 0.0f);
const bool forceUnlockYawRoll = ((gs->frameNum - owner->lastFireWeapon) >= maneuverBlockTime);
// do not check if the plane can be submerged here,
// since it'll cause ground collisions later on (?)
const float groundHeight = amtGetGroundHeightFuncs[5 * UseSmoothMesh()](pos.x, pos.z);
// If goal-distance is half turn radius then turn if
// goal-position is not in front within a ~45 degree
// arc.
// If goal-position is behind us and goal-distance is
// less than our turning radius, turn the other way.
// These conditions prevent becoming stuck in a small
// circle around goal-position.
const float nearGoal = ((goalDist2D < turnRadius * 0.5f && goalDir2D.dot(frontdir) < 0.7f) || (goalDist2D < turnRadius && goalDir2D.dot(frontdir) < -0.1f));
const float turnFlip = ((!owner->UnderFirstPersonControl() || owner->fpsControlPlayer->fpsController.mouse2) * -2.0f + 1.0f);
// if nearGoal=0, multiply by 1
// if nearGoal=1, multiply by turnFlip
const float goalDotFront = goalDir2D.dot(frontdir);
const float goalDotRight = goalDir2D.dot(rightDir2D) * ((1.0f - nearGoal) + (nearGoal * turnFlip));
#if 0
// try to steer (yaw) away from nearby aircraft in front of us
if (lastCollidee != nullptr) {
const float3 collideeVec = lastCollidee->pos - pos;
const float collideeDist = collideeVec.Length();
const float relativeDist = (collideeDist > 0.0f)?
std::max(1200.0f, goalDist2D) / collideeDist * 0.036f:
0.0f;
const float3 collideeDir = (collideeDist > 0.0f)?
(collideeVec / collideeDist):
ZeroVector;
goalDotRight -= (collideeDir.dot(rightdir) * relativeDist * std::max(0.0f, collideeDir.dot(frontdir)));
}
#endif
const float3 yprInputLocks = (XZVector * float(allowUnlockYawRoll || forceUnlockYawRoll)) + UpVector;
const float3 maxBodyAngles = {0.0f, maxPitch, maxBank};
const float3 maxCtrlAngles = {maxRudder, maxElevator, maxAileron};
const float3 prvCtrlAngles[2] = {{lastRudderPos[0], lastElevatorPos[0], lastAileronPos[0]}, {lastRudderPos[1], lastElevatorPos[1], lastAileronPos[1]}};
const float3& curCtrlAngles = GetControlSurfaceAngles(owner, lastCollidee, pos, spd, rightdir, updir, frontdir, goalDir2D, yprInputLocks, maxBodyAngles, maxCtrlAngles, prvCtrlAngles, groundHeight, wantedHeight, goalDotRight, goalDotFront, false && collisionState == COLLISION_DIRECT, false);
UpdateAirPhysics({curCtrlAngles, wantedThrottle}, owner->frontdir);
return (allowUnlockYawRoll || forceUnlockYawRoll);
}
static float GetVTOLAccelerationSign(float curHeight, float wtdHeight, float vertSpeed) {
const float nxtHeight = curHeight + vertSpeed * 20.0f;
const float tgtHeight = wtdHeight * 1.02f;
return (Sign<float>(nxtHeight < tgtHeight));
}
void CStrafeAirMoveType::UpdateTakeOff()
{
const float3& pos = owner->pos;
const float4& spd = owner->speed;
wantedHeight = orgWantedHeight;
SyncedFloat3& rightdir = owner->rightdir;
SyncedFloat3& frontdir = owner->frontdir;
SyncedFloat3& updir = owner->updir;
const float3 goalDir = (goalPos - pos).Normalize();
const float yawWeight = maxRudder * spd.y;
const float dirWeight = Clamp(goalDir.dot(rightdir), -1.0f, 1.0f);
// this tends to alternate between -1 and +1 when goalDir and rightdir are ~orthogonal
// const float yawSign = Sign(goalDir.dot(rightdir));
const float currentHeight = pos.y - amtGetGroundHeightFuncs[canSubmerge](pos.x, pos.z);
const float minAccHeight = wantedHeight * 0.4f;
frontdir += (rightdir * dirWeight * yawWeight);
frontdir.Normalize();
rightdir = frontdir.cross(updir);
owner->SetVelocity(spd + (UpVector * accRate * GetVTOLAccelerationSign(currentHeight, wantedHeight, spd.y)));
owner->SetVelocity(spd + (owner->frontdir * accRate * (currentHeight > minAccHeight)));
// initiate forward motion before reaching wantedHeight
// normally aircraft start taking off from the ground below wantedHeight,
// but state can also change to LANDING via StopMoving and then again to
// TAKEOFF (via StartMoving) while still in mid-air
if (currentHeight > wantedHeight || spd.SqLength2D() >= Square(maxWantedSpeed * 0.8f))
SetState(AIRCRAFT_FLYING);
owner->SetVelocityAndSpeed(spd * invDrag);
owner->Move(spd, true);
owner->SetHeadingFromDirection();
owner->UpdateMidAndAimPos();
}
void CStrafeAirMoveType::UpdateLanding()
{
const float3 pos = owner->pos;
SyncedFloat3& rightdir = owner->rightdir;
SyncedFloat3& frontdir = owner->frontdir;
SyncedFloat3& updir = owner->updir;
if (!HaveLandingPos()) {
reservedLandingPos = FindLandingPos(pos + frontdir * BrakingDistance(maxSpeed, decRate));
// if spot is valid, mark it on the blocking-map
// so other aircraft can not claim the same spot
if (HaveLandingPos()) {
wantedHeight = 0.0f;
owner->Move(reservedLandingPos, false);
owner->Block();
owner->Move(pos, false);
// Block updates the blocking-map position (mapPos)
// via GroundBlockingObjectMap (which calculates it
// from object->pos) and UnBlock always uses mapPos
// --> we cannot block in one part of the map *and*
// unblock in another
// owner->UnBlock();
} else {
goalPos.ClampInBounds();
UpdateFlying(wantedHeight, 1.0f);
return;
}
}
SetGoal(reservedLandingPos);
UpdateLandingHeight(wantedHeight);
const float3 reservedLandingPosDir = reservedLandingPos - pos;
const float3 brakeSpot = pos + frontdir * BrakingDistance(owner->speed.Length2D(), decRate);
if (brakeSpot.SqDistance2D(reservedLandingPos) > landRadiusSq) {
// If we're not going to land inside the landRadius, keep flying.
const float tempWantedHeight = wantedHeight;
UpdateFlying(wantedHeight = orgWantedHeight, 1.0f);
wantedHeight = tempWantedHeight;
return;
}
updir -= ((rightdir * 0.02f) * (rightdir.y < -0.01f));
updir += ((rightdir * 0.02f) * (rightdir.y > 0.01f));
frontdir += ((updir * 0.02f) * (frontdir.y < -0.01f));
frontdir -= ((updir * 0.02f) * (frontdir.y > 0.01f));
frontdir += ((rightdir * 0.02f) * (rightdir.dot(reservedLandingPosDir) > 0.01f));
frontdir -= ((rightdir * 0.02f) * (rightdir.dot(reservedLandingPosDir) < -0.01f));
{
//A Mangled UpdateAirPhysics
float4& spd = owner->speed;
float frontSpeed = spd.dot2D(frontdir);
if (frontSpeed > 0.0f) {
//Slow down before vertical landing
owner->SetVelocity(frontdir * (spd.Length2D() * invDrag - decRate));
//Calculate again for next check
frontSpeed = spd.dot2D(frontdir);
}
if (frontSpeed < 0.0f)
owner->SetVelocityAndSpeed(UpVector * owner->speed);
const float landPosDistXZ = reservedLandingPosDir.Length2D() + 0.1f;
const float landPosDistY = reservedLandingPosDir.y;
const float curSpeedXZ = spd.Length2D();