-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAIManeuvers.cpp
1997 lines (1500 loc) · 52.8 KB
/
AIManeuvers.cpp
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
// AIManeuvers.cpp
//
// Implementation of IOrderModule class
// Copyright (c) 2011 by George Moromisato. All Rights Reserved.
#include "PreComp.h"
const Metric ATTACK_RANGE = (20.0 * LIGHT_SECOND);
const Metric CLOSE_RANGE = (50.0 * LIGHT_SECOND);
const Metric DEFAULT_DIST_CHECK = (700.0 * KLICKS_PER_PIXEL);
const Metric DOCKING_APPROACH_DISTANCE = (200.0 * KLICKS_PER_PIXEL);
const Metric ESCORT_DISTANCE = (6.0 * LIGHT_SECOND);
const Metric HIT_NAV_POINT_DIST = (24.0 * LIGHT_SECOND);
const Metric MAX_DELTA = (2.0 * KLICKS_PER_PIXEL);
const Metric MAX_DELTA_VEL = (g_KlicksPerPixel / 2.0);
const Metric MAX_DISTANCE = (400 * KLICKS_PER_PIXEL);
const Metric MAX_DOCK_DISTANCE = (15.0 * LIGHT_SECOND);
const Metric MAX_ESCORT_DISTANCE = (12.0 * LIGHT_SECOND);
const Metric MAX_GATE_DISTANCE = (90.0 * KLICKS_PER_PIXEL);
const Metric MAX_IN_FORMATION_DELTA = (2.0 * KLICKS_PER_PIXEL);
const Metric MAX_TARGET_OF_OPPORTUNITY_RANGE = (20.0 * LIGHT_SECOND);
const int MAX_TARGETS = 10;
const Metric MIN_FLYBY_SPEED = (2.0 * KLICKS_PER_PIXEL);
const Metric MIN_POTENTIAL2 = (KLICKS_PER_PIXEL* KLICKS_PER_PIXEL * 25.0);
const Metric MIN_STATION_TARGET_DIST = (10.0 * LIGHT_SECOND);
const Metric MIN_TARGET_DIST = (3.0 * LIGHT_SECOND);
const Metric MAX_HEADING_DELTA_V = g_KlicksPerPixel;
const Metric MAX_HEADING_DELTA_V2 = MAX_HEADING_DELTA_V * MAX_HEADING_DELTA_V;
const Metric CLOSE_RANGE2 = (CLOSE_RANGE * CLOSE_RANGE);
const Metric HIT_NAV_POINT_DIST2 = (HIT_NAV_POINT_DIST * HIT_NAV_POINT_DIST);
const Metric MAX_DELTA2 = (MAX_DELTA * MAX_DELTA);
const Metric MAX_DELTA_VEL2 = (MAX_DELTA_VEL * MAX_DELTA_VEL);
const Metric MAX_IN_FORMATION_DELTA2 = (MAX_IN_FORMATION_DELTA * MAX_IN_FORMATION_DELTA);
const Metric MIN_FLYBY_SPEED2 = (MIN_FLYBY_SPEED * MIN_FLYBY_SPEED);
const Metric MIN_STATION_TARGET_DIST2 = (MIN_STATION_TARGET_DIST * MIN_STATION_TARGET_DIST);
const Metric MIN_TARGET_DIST2 = (MIN_TARGET_DIST * MIN_TARGET_DIST);
const Metric MAX_FLOCK_DIST = (600.0 * KLICKS_PER_PIXEL);
const Metric MAX_FLOCK_DIST2 = (MAX_FLOCK_DIST * MAX_FLOCK_DIST);
const Metric FLOCK_SEPARATION_RANGE = (100.0 * KLICKS_PER_PIXEL);
const Metric FLOCK_SEPARATION_RANGE2 = (FLOCK_SEPARATION_RANGE * FLOCK_SEPARATION_RANGE);
const Metric FLOCK_COMBAT_RANGE = (300.0 * KLICKS_PER_PIXEL);
const Metric FLOCK_COMBAT_RANGE2 = (FLOCK_COMBAT_RANGE * FLOCK_COMBAT_RANGE);
const Metric CLOSE_DELTA_V_RATIO = 0.12;
const Metric MIN_SPEED_RATIO = 0.01;
const Metric FORMATION_MAX_TIME = 5.0;
const Metric POTENTIAL_TO_POS_ADJ = 100.0;
const Metric MAX_IMMOBILE_SPEED = (1.0 * KLICKS_PER_PIXEL);
const Metric MAX_IMMOBILE_SPEED2 = MAX_IMMOBILE_SPEED * MAX_IMMOBILE_SPEED;
void CAIBehaviorCtx::CalcEscortFormation (CShip *pShip, CSpaceObject *pLeader, CVector *retvPos, CVector *retvVel, int *retiFacing)
// CalcEscortFormation
//
// Computes the position of the escort
{
// If we're flocking, use flocking algorithm (if a flock is available).
if (m_AISettings.IsFlocker()
&& CalcFlockingFormation(pShip, pLeader, retvPos, retvVel, retiFacing))
;
// Otherwise, we do a normal escort.
// Pick escort position relative to our current position
// at time = 0
else
{
CVector vEscortPos;
// Escort position is encoded in order data
IShipController::SData Data;
pShip->GetCurrentOrder(NULL, &Data);
if (Data.IsIntegerOrPair())
{
int iAngle = Data.AsInteger();
int iDistance = Data.AsInteger2();
if (iDistance == 0)
iDistance = 6;
vEscortPos = PolarToVector(pLeader->GetRotation() + iAngle, iDistance * LIGHT_SECOND);
}
// Otherwise, generate a position
else
{
int iAngle = (pLeader->GetRotation() + 45 + 5 * (pShip->GetDestiny() % 54)) % 360;
Metric rRadius = ESCORT_DISTANCE * (0.75 + (pShip->GetDestiny() / 360.0));
vEscortPos = PolarToVector(iAngle, rRadius);
}
// Compute absolute positions
*retvPos = pLeader->GetPos() + vEscortPos;
*retvVel = pLeader->GetVel();
*retiFacing = pLeader->GetRotation();
}
}
bool CAIBehaviorCtx::CalcFlockingFormation (CShip *pShip,
CSpaceObject *pLeader,
CVector *retvPos,
CVector *retvVel,
int *retiFacing)
// CalcFlockingFormation
//
// Calculates the position that this ship should take relative to the rest of the flock. Returns FALSE
// if the current ship is a leader in the flock.
{
DEBUG_TRY
switch (m_AISettings.GetFlockingStyle())
{
case CAISettings::flockCompact:
// LATER: Not Yet Implemented.
return CalcFlockingFormationRandom(pShip, pLeader, retvPos, retvVel, retiFacing);
case CAISettings::flockRandom:
return CalcFlockingFormationRandom(pShip, pLeader, retvPos, retvVel, retiFacing);
default:
return CalcFlockingFormationCloud(pShip, pLeader, MAX_FLOCK_DIST, FLOCK_SEPARATION_RANGE, retvPos, retvVel, retiFacing);
}
DEBUG_CATCH
}
bool CAIBehaviorCtx::CalcFlockingFormationCloud (CShip *pShip, CSpaceObject *pLeader, Metric rFOVRange, Metric rSeparationRange, CVector *retvPos, CVector *retvVel, int *retiFacing)
// CalcFlockingFormation
//
// Calculates the position that this ship should take relative to the rest of the flock. Returns FALSE
// if the current ship is a leader in the flock.
{
int i;
CVector vFlockPos;
CVector vFlockVel;
CVector vFlockHeading;
CVector vAvoid;
Metric rFOVRange2 = rFOVRange * rFOVRange;
Metric rSeparationRange2 = rSeparationRange * rSeparationRange;
Metric rFlockCount = 0.0;
Metric rAvoidCount = 0.0;
for (i = 0; i < pShip->GetSystem()->GetObjectCount(); i++)
{
CSpaceObject *pObj = pShip->GetSystem()->GetObject(i);
if (pObj
&& pObj->GetSovereign() == pShip->GetSovereign()
&& pObj->GetCategory() == CSpaceObject::catShip
&& pObj->CanAttack() // Excludes attached ship sections
&& pObj != pShip
&& pObj != pLeader)
{
CVector vTarget = pObj->GetPos() - pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
// Only consider ships within a certain range
if (rTargetDist2 < rFOVRange2)
{
CVector vTargetRot = vTarget.Rotate(360 - pShip->GetRotation());
// Only consider ships in front of us
if (vTargetRot.GetX() > 0.0)
{
// Only ships of a certain destiny
if (pObj->GetDestiny() > pShip->GetDestiny())
{
vFlockPos = vFlockPos + vTarget;
vFlockVel = vFlockVel + (pObj->GetVel() - pShip->GetVel());
vFlockHeading = vFlockHeading + PolarToVector(pObj->GetRotation(), 1.0);
rFlockCount = rFlockCount + 1.0;
}
// Avoid ships that are too close
if (rTargetDist2 < rSeparationRange2)
{
vAvoid = vAvoid + vTarget;
rAvoidCount = rAvoidCount + 1.0;
}
}
}
}
}
// If we've got a leader, add separately
if (pLeader)
{
CVector vTarget = pLeader->GetPos() - pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
vFlockPos = vFlockPos + vTarget;
vFlockVel = vFlockVel + (pLeader->GetVel() - pShip->GetVel());
vFlockHeading = vFlockHeading + PolarToVector(pLeader->GetRotation(), 1.0);
rFlockCount = rFlockCount + 1.0;
// Avoid ships that are too close
if (rTargetDist2 < rSeparationRange2)
{
vAvoid = vAvoid + vTarget;
rAvoidCount = rAvoidCount + 1.0;
}
}
// Compute the averages
if (rFlockCount > 0.0)
{
CVector vAimPos = (vFlockPos / rFlockCount);
if (rAvoidCount > 0.0)
{
int iAimAngle = VectorToPolar(vAimPos);
CVector vAvoidAverage = (vAvoid / rAvoidCount);
CVector vAvoidRot = vAvoidAverage.Rotate(360 - iAimAngle);
CVector vAimPerp = vAimPos.Normal().Perpendicular();
Metric rAvoidAverage = vAvoidAverage.Length();
Metric rAvoidMag = 2.0 * (rSeparationRange - rAvoidAverage);
CVector vAvoidAdj;
if (rAvoidMag > 0.0)
{
if (vAvoidRot.GetY() > 0.0)
vAvoidAdj = -rAvoidMag * vAimPerp;
else
vAvoidAdj = rAvoidMag * vAimPerp;
}
vAimPos = vAimPos + vAvoidAdj;
}
*retvPos = pShip->GetPos() + vAimPos;
*retvVel = pShip->GetVel() + (vFlockVel / rFlockCount);
*retiFacing = VectorToPolar(vFlockHeading);
return true;
}
else
{
return false;
}
}
bool CAIBehaviorCtx::CalcFlockingFormationRandom (CShip *pShip, CSpaceObject *pLeader, CVector *retvPos, CVector *retvVel, int *retiFacing)
// CalcFlockingFormationRandom
//
// Calculates the position that this ship should take relative to the rest of the flock. Returns FALSE
// if the current ship is a leader in the flock.
{
if (pLeader == NULL)
return false;
// Compute the size of the ship
Metric rSpacing = 0.5 * pShip->GetHitSize();
// Pick a random angle and position with respect to the leader
int iAngle = pShip->GetDestiny();
Metric rRange = (2 + (pShip->GetDestiny() % 10)) * rSpacing;
*retvPos = pLeader->GetPos() + PolarToVector(iAngle, rRange);
*retvVel = pLeader->GetVel();
*retiFacing = pLeader->GetRotation();
return true;
}
CVector CAIBehaviorCtx::CalcFlankPos (CShip *pShip, const CVector &vInterceptPos)
// CalcFlankPos
//
// Computes the flanking position based on the interception position (both are
// relative to the ship position).
{
CVector vFlankingLine = vInterceptPos.Normal().Perpendicular();
vFlankingLine = vFlankingLine * ((pShip->GetDestiny() % 2) == 0 ? GetFlankDist() : -GetFlankDist());
return vInterceptPos + vFlankingLine;
}
bool CAIBehaviorCtx::CalcFormationParams (CShip *pShip,
const CVector &vDestPos,
const CVector &vDestVel,
int iDestAngle,
CVector *retvRecommendedVel,
Metric *retrDeltaPos2,
Metric *retrDeltaVel2)
// CalcFormationParams
//
// Computes the parameters required to hold a formation.
//
// retvRecommendedVel is the recommended velocity that we need to achieve in
// order to reach the desired state.
//
// retrDeltaPos2 is the current distance (^2) to the desired pos
// retrDelatVel2 is the magnitude (^2) of the recommended velocity
//
// We return TRUE if the recommended velocity is a small enough fraction of
// ship speed.
{
#ifdef DEBUG_FORMATION
bool bResult = CShipAIHelper::CalcFormationParams(pShip, vDestPos, vDestVel, retvRecommendedVel, retrDeltaPos2, retrDeltaVel2);
if (g_pUniverse->GetPlayerShip()
&& g_pUniverse->GetPlayerShip()->GetTarget(CItemCtx(), true) == pShip
&& m_pUpdateCtx->pAnnotations)
{
m_pUpdateCtx->pAnnotations->bDebugFormation = true;
m_pUpdateCtx->pAnnotations->vFormationPos = vDestPos;
m_pUpdateCtx->pAnnotations->iFormationAngle = iDestAngle;
m_pUpdateCtx->pAnnotations->vFormationCurPos = pShip->GetPos();
}
return bResult;
#else
return CShipAIHelper::CalcFormationParams(pShip, vDestPos, vDestVel, retvRecommendedVel, retrDeltaPos2, retrDeltaVel2);
#endif
}
CVector CAIBehaviorCtx::CalcManeuverCloseOnTarget (CShip *pShip,
CSpaceObject *pTarget,
const CVector &vTarget,
Metric rTargetDist2,
bool bFlank)
// CalcManeuverCloseOnTarget
//
// Returns the vector that the ship should move in for a close-on-target maneuver
{
CVector vInterceptPoint;
#ifdef DEBUG_SHIP
bool bDebug = pShip->IsSelected();
#endif
// If we don't have a target object, then go to the position
if (pTarget == NULL)
vInterceptPoint = vTarget;
// If we are very far (>10M klicks) from the target then
// compensate for the target's motion.
else if (rTargetDist2 > 1.0e14)
{
// Compute the speed with which the target is closing
// the distance (this may be a negative number). Note
// that we care about the target's absolute velocity
// (not its relative velocity because we are trying to
// adjust our velocity).
CVector vAbsVel = pTarget->GetVel();
Metric rClosingSpeed = -vAbsVel.Dot(vTarget.Normal());
// Figure out how long it will take to overtake the target's
// current position at maximum speed. (This is just a heuristic
// that gives us an approximation of the length of an intercept
// course.)
rClosingSpeed = rClosingSpeed + pShip->GetMaxSpeed();
if (rClosingSpeed > 0.0)
{
Metric rTimeToIntercept = vTarget.Length() / (rClosingSpeed);
vInterceptPoint = vTarget + vAbsVel * rTimeToIntercept;
}
else
vInterceptPoint = vTarget;
#ifdef DEBUG_SHIP
if (bDebug)
g_pUniverse->DebugOutput("Adjust for distance: %d at %d%%c",
(int)(vTarget.Length() / LIGHT_SECOND),
(int)(100.0 * rClosingSpeed / LIGHT_SPEED));
#endif
}
else
{
CVector vTargetVel = pTarget->GetVel() - pShip->GetVel();
vInterceptPoint = vTarget + (vTargetVel * g_SecondsPerUpdate);
// Close on a flanking point
if (bFlank)
{
vInterceptPoint = CalcFlankPos(pShip, vInterceptPoint);
#ifdef DEBUG_SHIP
if (bDebug)
g_pUniverse->DebugOutput("Flank target");
#endif
}
}
return vInterceptPoint;
}
CVector CAIBehaviorCtx::CalcManeuverFormation (CShip *pShip, const CVector vDest, const CVector vDestVel, int iDestFacing)
// CalcManeuverFormation
//
// Returns the vector that the ship should move in to achieve the given formation
{
// Compute our formation parameters
CVector vDeltaV;
bool bCloseEnough = CalcFormationParams(pShip, vDest, vDestVel, iDestFacing, &vDeltaV);
// If we're close enough to the velocity, cheat a little by
// accelerating without using the main engine
if (bCloseEnough)
{
if (!pShip->IsParalyzed())
{
pShip->Accelerate(vDeltaV * pShip->GetMass() / 2000.0, g_SecondsPerUpdate);
#ifdef DEBUG_ATTACK_TARGET_MANEUVERS
pShip->SetDebugVector(vDeltaV.Normal() * 100. * g_KlicksPerPixel);
#endif
}
return CVector();
}
// Otherwise, thrust with the main engines
else
{
int iAngle = VectorToPolar(vDeltaV);
return PolarToVector(iAngle, 5.0 * LIGHT_SECOND);
}
}
CVector CAIBehaviorCtx::CalcManeuverSpiralIn (CShip *pShip, const CVector &vTarget, int iTrajectory)
// CalcManeuverSpiralIn
//
// Returns the vector that the ship should move in for a spiral-in maneuver
{
CVector vTangent = (vTarget.Perpendicular()).Normal() * pShip->GetMaxSpeed() * g_SecondsPerUpdate * 8;
Metric rRadius;
int iAngle = VectorToPolar(vTangent, &rRadius);
// Curve inward
return PolarToVector(iAngle + 360 - iTrajectory, rRadius);
}
CVector CAIBehaviorCtx::CalcManeuverSpiralOut (CShip *pShip, const CVector &vTarget, int iTrajectory)
// CalcManeuverSpiralOut
//
// Returns the vector that the ship should move in for a spiral-out maneuver
{
CVector vTangent = (vTarget.Perpendicular()).Normal() * pShip->GetMaxSpeed() * g_SecondsPerUpdate * 8;
Metric rRadius;
int iAngle = VectorToPolar(vTangent, &rRadius);
// Handle the case where we vTangent is 0 (i.e., we are on top of the enemy)
if (rRadius == 0.0)
rRadius = g_KlicksPerPixel;
// Curve out
return PolarToVector(iAngle + iTrajectory, rRadius);
}
void CAIBehaviorCtx::DebugAIOutput (CShip *pShip, LPCSTR pText)
// DebugAIOutput
//
// Output AI state
{
if (g_pUniverse->GetDebugOptions().IsShowAIDebugEnbled())
pShip->Highlight(strPatternSubst(CONSTLIT("%d: %s"), pShip->GetID(), CString(pText)));
}
void CAIBehaviorCtx::ImplementAttackNearestTarget (CShip *pShip, Metric rMaxRange, CSpaceObject **iopTarget, CSpaceObject *pExcludeObj, bool bTurn)
// ImplementAttackNearestTarget
//
// Sets m_pTarget to be the nearest target and attacks it. This method
// should only be used while in a state that does not need m_pTarget.
{
DEBUG_TRY
CSpaceObject *pNewTarget;
if (pShip->IsDestinyTime(19)
&& !m_AISettings.NoTargetsOfOpportunity()
&& (pNewTarget = pShip->GetNearestVisibleEnemy(rMaxRange, false, pExcludeObj)))
(*iopTarget) = pNewTarget;
if (*iopTarget)
{
CVector vTarget = (*iopTarget)->GetPos() - pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
// Don't bother if the target is too far away
if (rTargetDist2 > rMaxRange * rMaxRange)
{
(*iopTarget) = NULL;
return;
}
int iFireDir;
ImplementFireWeaponOnTarget(pShip, -1, -1, *iopTarget, vTarget, rTargetDist2, &iFireDir);
// If we don't have a good weapon, then don't go after this target
if (GetBestWeapon() == NULL)
{
// Some ships only have secondary weapons. In that case, keep the
// target so that we can attack with secondaries.
if (!HasSecondaryWeapons())
(*iopTarget) = NULL;
return;
}
// If we have a fire direction, try to turn towards the target
if (bTurn
&& iFireDir != -1
&& !NoDogfights())
ImplementManeuver(pShip, iFireDir, false);
}
DEBUG_CATCH
}
void CAIBehaviorCtx::ImplementAttackTarget (CShip *pShip, CSpaceObject *pTarget, bool bMaintainCourse, bool bDoNotShoot)
// ImplementAttackTarget
//
// Maneuvers towards and attacks target
{
DEBUG_TRY
CVector vTarget = pTarget->GetPos() - pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
// Fire on the target as best we can
int iDesiredFacingDir;
ImplementFireWeaponOnTarget(pShip, -1, -1, pTarget, vTarget, rTargetDist2, &iDesiredFacingDir, bDoNotShoot);
#ifdef DEBUG_ATTACK_TARGET
pShip->SetDebugVector(CVector());
#endif
CVector vFlockPos;
CVector vFlockVel;
int iFlockFacing;
// If we're maintaining our course, then no need to maneuver.
if (bMaintainCourse || IsImmobile())
NULL;
// If we're flocking, then implement flocking maneuverses
else if (m_AISettings.IsFlocker()
&& rTargetDist2 > FLOCK_COMBAT_RANGE2
&& CalcFlockingFormation(pShip, NULL, &vFlockPos, &vFlockVel, &iFlockFacing))
ImplementFormationManeuver(pShip, vFlockPos, vFlockVel, pShip->AlignToRotationAngle(iFlockFacing));
// Otherwise, implement maneuvers
else
{
// If the ship has shields then figure out their state.
// (We need it inside of ImplementAttackTargetManeuver.)
CalcShieldState(pShip);
// Compute avoidance potential, if necessary
CalcAvoidPotential(pShip, pTarget);
// Maneuver according to combat style. If we maneuvered, then
// ignore the fire solution because we've already maneuvered
if (ImplementAttackTargetManeuver(pShip, pTarget, vTarget, rTargetDist2))
iDesiredFacingDir = -1;
}
// Turn towards fire solution, if we have one
if (iDesiredFacingDir != -1
&& !NoDogfights())
{
bool bThrustWhileAim = false;
int iCurrentFacingDir = pShip->GetRotation();
// If we're roughly facing the target and not too close,
// then thrust while we aim
if (rTargetDist2 > GetPrimaryAimRange2() / 4)
{
if (AreAnglesAligned(VectorToPolar(vTarget), iCurrentFacingDir, 30))
bThrustWhileAim = true;
}
// If we're close to the target, thrust if it will
// decrease our relative velocity
else
{
CVector vTargetVel = pTarget->GetVel() - pShip->GetVel();
if (vTargetVel.Length() > pShip->GetMaxSpeed() / 3
&& AreAnglesAligned(VectorToPolar(vTargetVel), iCurrentFacingDir, 30))
bThrustWhileAim = true;
}
ImplementManeuver(pShip, iDesiredFacingDir, bThrustWhileAim);
}
DEBUG_CATCH
}
bool CAIBehaviorCtx::ImplementAttackTargetManeuver (CShip *pShip, CSpaceObject *pTarget, const CVector &vTarget, Metric rTargetDist2)
// ImplementAttackTargetManeuver
//
// Implements maneuvers to attack the target based on the ship's combat style
// and taking into account the hazard potential vector.
//
// Returns TRUE if a maneuver was made (if FALSE, then ship is free to maneuver to align weapon)
{
CVector vDirection;
bool bNoThrustThroughTurn = false;
// Maneuver according to the ship's combat style. This block will initialize
// vDirection to point to the desired direction
switch (GetCombatStyle())
{
case aicombatStandard:
{
bool bFaster = (pShip->GetMaxSpeed() > pTarget->GetMaxSpeed());
// If we're waiting for shields to regenerate, then
// spiral away
if (IsWaitingForShieldsToRegen() && bFaster)
{
DebugAIOutput(pShip, "Wait for shields");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget, 75));
}
// If we're not well in range of our primary weapon then
// get closer to the target. (Or if we are not moving)
else if (rTargetDist2 > GetPrimaryAimRange2())
{
DebugAIOutput(pShip, "Close on target");
// Try to flank our target, if we are faster
vDirection = CombinePotential(CalcManeuverCloseOnTarget(pShip, pTarget, vTarget, rTargetDist2, bFaster));
}
// If we're attacking a static target then find a good spot
// and shoot from there.
else if (!pTarget->CanThrust())
{
int iClock = g_pUniverse->GetTicks() / (170 + pShip->GetDestiny() / 3);
int iAngle = pShip->AlignToRotationAngle((pShip->GetDestiny() + (iClock * 141 * (1 + pShip->GetDestiny()))) % 360);
Metric rRadius = Max(MIN_STATION_TARGET_DIST, pTarget->GetHitSize()) + (LIGHT_SECOND * (pShip->GetDestiny() % 100) / 10.0);
// This is the position that we want to go to
CVector vPos;
if (m_fHasAvoidPotential)
vPos = pShip->GetPos() + (POTENTIAL_TO_POS_ADJ * GetPotential());
else
vPos = pTarget->GetPos() + PolarToVector(iAngle + 180, rRadius);
// We don't want to thrust unless we're in position
bNoThrustThroughTurn = true;
// Figure out which way we need to move to end up where we want
// (Note that we don't combine the potential because we've already accounted for
// it above).
vDirection = CalcManeuverFormation(pShip, vPos, CVector(), iAngle);
}
// If we're attacking a station, then keep our distance so that
// we don't get caught in the explosion
else if (m_fAvoidExplodingStations
&& rTargetDist2 < MIN_STATION_TARGET_DIST2
&& pTarget->GetMass() > 5000.0)
{
DebugAIOutput(pShip, "Spiral away to avoid explosion");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget));
}
// If we're too close to our target, spiral away
else if (rTargetDist2 < MIN_TARGET_DIST2)
{
DebugAIOutput(pShip, "Spiral away");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget));
}
// If we're moving too slowly, move away
else if (pTarget->CanThrust()
&& (pShip->GetVel().Length2() < (0.01 * 0.01 * LIGHT_SPEED * LIGHT_SPEED)))
{
DebugAIOutput(pShip, "Speed away");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget));
}
// Otherwise, hazard avoidance only
else
vDirection = GetPotential();
break;
}
case aicombatAdvanced:
{
bool bWeAreFaster = (pShip->GetMaxSpeed() >= pTarget->GetMaxSpeed());
bool bUsingStandOffWeapon = (m_pBestWeapon && m_pBestWeapon->IsAreaWeapon(pShip));
const int MAX_BRAVERY_TICKS = 300; // Number of ticks since last attack to be 100% brave
const Metric BRAVERY_DECAY_POWER = 2.0;
const Metric EXTRA_RANGE = LIGHT_SECOND * 8.0;
// Compute how brave we are based on the last time we got hit.
// rBravery goes from 0.0 (scared) to 1.0 (brave)
int iLastHit = Max(0, Min(MAX_BRAVERY_TICKS, (g_pUniverse->GetTicks() - m_iLastAttack)));
const Metric rBravery = pow((Metric)iLastHit / (Metric)MAX_BRAVERY_TICKS, BRAVERY_DECAY_POWER);
// Do we need to avoid getting too close?
bool bAvoidExplodingTarget = m_fAvoidExplodingStations
&& rTargetDist2 < MIN_STATION_TARGET_DIST2
&& pTarget->GetMass() > 5000.0;
// Compute the maximum distance at which we'll start firing. If we're feeling brave,
// this will be close to the flank distance (which is often close). Otherwise, we'll
// try to stay back.
const Metric rFlankAimDist = GetFlankDist() + (EXTRA_RANGE * (1.0 - rBravery));
// In certain cases, though, we pick a longer range.
Metric rMaxAimDist2;
if (bWeAreFaster && !bAvoidExplodingTarget && !bUsingStandOffWeapon)
rMaxAimDist2 = Min(GetPrimaryAimRange2(), rFlankAimDist * rFlankAimDist);
else
rMaxAimDist2 = GetPrimaryAimRange2();
// Minimum distance is a never more than one-half the maximum distance.
const Metric rMinDist2 = Min(MIN_TARGET_DIST2, 0.25 * rMaxAimDist2);
// If we're waiting for shields to regenerate, then
// spiral away
if (IsWaitingForShieldsToRegen()
&& bWeAreFaster
&& pShip->GetController()->GetCurrentOrderEx() != IShipController::orderEscort)
{
DebugAIOutput(pShip, "Wait for shields");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget, 75));
}
// If we're attacking a static target then find a good spot
// and shoot from there.
//
// NOTE: We need to check this before the code below because otherwise we
// won't get here.
else if (!pTarget->CanThrust())
{
int iClock = g_pUniverse->GetTicks() / (170 + pShip->GetDestiny() / 3);
int iAngle = pShip->AlignToRotationAngle((pShip->GetDestiny() + (iClock * 141 * (1 + pShip->GetDestiny()))) % 360);
Metric rRadius = Max(MIN_STATION_TARGET_DIST, pTarget->GetHitSize()) + (LIGHT_SECOND * (pShip->GetDestiny() % 100) / 10.0);
// This is the position that we want to go to
CVector vPos;
if (m_fHasAvoidPotential)
vPos = pShip->GetPos() + (POTENTIAL_TO_POS_ADJ * GetPotential());
else
vPos = pTarget->GetPos() + PolarToVector(iAngle + 180, rRadius);
// We don't want to thrust unless we're in position
bNoThrustThroughTurn = true;
// Figure out which way we need to move to end up where we want
// (Note that we don't combine the potential because we've already accounted for
// it above).
vDirection = CalcManeuverFormation(pShip, vPos, CVector(), iAngle);
}
// If we're not well in range of our primary weapon then
// get closer to the target. (Or if we are not moving)
else if (rTargetDist2 > rMaxAimDist2)
{
// If we're faster, try to be clever
if (bWeAreFaster && !bAvoidExplodingTarget && !bUsingStandOffWeapon)
{
CVector vToTargetN = (pTarget->GetPos() - pShip->GetPos()).Normal();
// Compute the position we want
CVector vPos;
if (m_fHasAvoidPotential)
{
DebugAIOutput(pShip, "Avoid hazards");
vPos = pShip->GetPos() + (POTENTIAL_TO_POS_ADJ * GetPotential());
}
else
{
DebugAIOutput(pShip, "Close to aim point");
// Pick a position at the flank distance between us and the target
vPos = pTarget->GetPos() + (vToTargetN * -rFlankAimDist);
}
// We want to end with a non-zero velocity. Otherwise, we'll be a
// sitting duck.
CVector vVel = pTarget->GetVel() + (vToTargetN * (1.0 - Min(0.7, rBravery)) * pShip->GetMaxSpeed());
// Maneuver to that point
vDirection = CalcManeuverFormation(pShip, vPos, vVel, 0);
}
// Otherwise, we just try to close as best as possible
else
{
DebugAIOutput(pShip, "Close on target");
vDirection = CombinePotential(CalcManeuverCloseOnTarget(pShip, pTarget, vTarget, rTargetDist2));
}
}
// If we're attacking a station, then keep our distance so that
// we don't get caught in the explosion
else if (bAvoidExplodingTarget)
{
DebugAIOutput(pShip, "Spiral away to avoid explosion");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget));
}
// If we're too close to our target, spiral away
else if (rTargetDist2 < rMinDist2)
{
DebugAIOutput(pShip, "Spiral away");
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget));
}
// Otherwise, hazard avoidance only.
else
vDirection = GetPotential();
break;
}
case aicombatStandOff:
{
Metric rMaxRange2 = m_rBestWeaponRange * m_rBestWeaponRange;
Metric rIdealRange2 = 0.45 * rMaxRange2;
// If we're beyond our weapon's max range, then close on target
if (rTargetDist2 > rMaxRange2)
{
vDirection = CombinePotential(CalcManeuverCloseOnTarget(pShip, pTarget, vTarget, rTargetDist2));
}
// If we're inside the ideal range, then move away from the target
else if (rTargetDist2 < rIdealRange2)
{
vDirection = CombinePotential(CalcManeuverSpiralOut(pShip, vTarget, 45));
}
// Otherwise, hazard avoidance only
else
vDirection = GetPotential();
break;
}
case aicombatChase:
{
Metric rMaxRange2 = m_rBestWeaponRange * m_rBestWeaponRange;
// Compute the angle line along the target's motion (and make sure
// it is aligned on a rotation angle, so we can get a shot in)
int iTargetMotion = (pTarget->CanThrust() ?
pShip->AlignToRotationAngle(VectorToPolar(pTarget->GetVel()))
: pShip->AlignToRotationAngle(pShip->GetDestiny()));
// Compute the target's angle with respect to us. We want to end up facing
// directly towards the target
int iTargetAngle = VectorToPolar(vTarget);
// Pick a point behind the target (and add hazard potential)
Metric rRange = Min(0.5 * m_rBestWeaponRange, 10.0 * LIGHT_SECOND);
CVector vPos;
if (m_fHasAvoidPotential)
vPos = pShip->GetPos() + (POTENTIAL_TO_POS_ADJ * GetPotential());
else
vPos = pTarget->GetPos() + PolarToVector(iTargetMotion + 180, rRange);
// Figure out which way we need to move to end up where we want
// (Note that we don't combine the potential because we've already accounter for
// it above).
vDirection = CalcManeuverFormation(pShip, vPos, pTarget->GetVel(), iTargetAngle);
// We don't want to thrust unless we're in position
bNoThrustThroughTurn = true;
// Debug
#ifdef DEBUG_ATTACK_TARGET
pTarget->SetDebugVector(vPos - pTarget->GetPos());
#endif
break;
}
case aicombatFlyby:
{
Metric rCloseRange2 = 0.25 * GetPrimaryAimRange2();
// If we're beyond our weapon's effective range, then close on target
if (rTargetDist2 > GetPrimaryAimRange2())
vDirection = CombinePotential(CalcManeuverCloseOnTarget(pShip, pTarget, vTarget, rTargetDist2));
// If we're too close to the target, move
else if (rTargetDist2 < rCloseRange2)
{
// Compute our bearing from the target's perspective