-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCFleetCommandAI.cpp
1332 lines (1009 loc) · 29.8 KB
/
CFleetCommandAI.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
// CFleetCommandAI.cpp
//
// CFleetCommandAI class
#include "PreComp.h"
#define ALLOC_GRANULARITY 16
#define ATTACK_RANGE (18.0 * LIGHT_SECOND)
// At this perception we should be able to detect a normal ship (stealth = 4)
// at almost 4 light-minutes distance
#define DEFAULT_PERCEPTION 6
#define MAX_DETECTION_RANGE (240.0 * LIGHT_SECOND)
#define MAX_DETECTION_RANGE2 (MAX_DETECTION_RANGE * MAX_DETECTION_RANGE)
#define MAX_THREAT_RANGE (120.0 * LIGHT_SECOND)
#define INTERCEPT_THREAT_RANGE (80.0 * LIGHT_SECOND)
#define INTERCEPT_THREAT_RANGE2 (INTERCEPT_THREAT_RANGE * INTERCEPT_THREAT_RANGE)
#define MIN_THREAT_RANGE (24.0 * LIGHT_SECOND)
#define MIN_THREAT_RANGE2 (MIN_THREAT_RANGE * MIN_THREAT_RANGE)
#define DEFENSE_RANGE (18.0 * LIGHT_SECOND)
#define SEPARATION_RANGE (8.0 * LIGHT_SECOND)
#define SEPARATION_RANGE2 (SEPARATION_RANGE * SEPARATION_RANGE)
#define RALLY_RANGE (260.0 * LIGHT_SECOND)
#define BREAK_CHARGE_RANGE (20.0 * LIGHT_SECOND)
#define CHARGE_ATTACK_RANGE (200.0 * LIGHT_SECOND)
CFleetCommandAI::CFleetCommandAI (void) :
m_State(stateNone),
m_pObjective(NULL),
m_pTarget(NULL),
m_iAssetCount(0),
m_iAssetAlloc(0),
m_pAssets(NULL),
m_iTargetCount(0),
m_iTargetAlloc(0),
m_pTargets(NULL),
m_iCounter(0),
m_iRallyFacing(0),
m_iStartingAssetCount(0)
// CFleetCommandAI constructor
{
}
CFleetCommandAI::~CFleetCommandAI (void)
// CFleetCommandAI destructor
{
if (m_pAssets)
delete [] m_pAssets;
if (m_pTargets)
delete [] m_pTargets;
}
CFleetCommandAI::SAsset *CFleetCommandAI::AddAsset (CSpaceObject *pAsset)
// AddAsset
//
// Add an asset to our list
{
int i;
// Make sure we have enough room in the list
if (m_iAssetCount + 1 > m_iAssetAlloc)
{
if (m_pAssets == NULL)
{
m_iAssetAlloc = ALLOC_GRANULARITY;
m_pAssets = new SAsset [m_iAssetAlloc];
}
else
{
m_iAssetAlloc += ALLOC_GRANULARITY;
SAsset *pNewAssets = new SAsset [m_iAssetAlloc];
for (i = 0; i < m_iAssetCount; i++)
pNewAssets[i] = m_pAssets[i];
delete [] m_pAssets;
m_pAssets = pNewAssets;
}
}
// Make sure that we are not adding a duplicate
#ifdef DEBUG
for (i = 0; i < m_iAssetCount; i++)
if (m_pAssets[i].pAsset == pAsset)
ASSERT(false);
#endif
// Add the ship
SAsset *pNewAsset = &m_pAssets[m_iAssetCount];
pNewAsset->pAsset = pAsset;
pNewAsset->iFormationPos = -1;
pNewAsset->pTarget = NULL;
m_iAssetCount++;
m_iStartingAssetCount++;
return pNewAsset;
}
void CFleetCommandAI::AddTarget (CSpaceObject *pTarget)
// AddTarget
//
// Add an target to our list
{
int i;
// Make sure we have enough room in the list
if (m_iTargetCount + 1 > m_iTargetAlloc)
{
if (m_pTargets == NULL)
{
m_iTargetAlloc = ALLOC_GRANULARITY;
m_pTargets = new STarget [m_iTargetAlloc];
}
else
{
m_iTargetAlloc += ALLOC_GRANULARITY;
STarget *pNewTargets = new STarget [m_iTargetAlloc];
for (i = 0; i < m_iTargetCount; i++)
pNewTargets[i] = m_pTargets[i];
delete [] m_pTargets;
m_pTargets = pNewTargets;
}
}
// Add the ship
m_pTargets[m_iTargetCount].pTarget = pTarget;
m_pTargets[m_iTargetCount].iAssignedTo = 0;
m_pTargets[m_iTargetCount].iKilled = 0;
m_iTargetCount++;
}
void CFleetCommandAI::Behavior (SUpdateCtx &Ctx)
// Behavior
//
// Fly, fight, die
{
// Reset
ResetBehavior();
// Use basic items
UseItemsBehavior();
// Behave according to our state
switch (m_State)
{
case stateNone:
BehaviorStart();
break;
case stateAttackAtWill:
ImplementAttackAtWill();
break;
case stateAttackFromRallyPoint:
ImplementAttackFromRallyPoint();
break;
case stateChargeInFormation:
ImplementChargeInFormation();
break;
case stateFormAtRallyPoint:
ImplementFormAtRallyPoint();
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
break;
case stateOnCourseForStargate:
m_AICtx.ImplementGating(m_pShip, m_pObjective);
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
break;
case stateWaiting:
ImplementWaitAtPosition();
break;
case stateWaitingForThreat:
{
ASSERT(m_pObjective);
// Compute threat vector
if (m_pShip->IsDestinyTime(31))
UpdateTargetListAndPotentials();
// Maneuver so that we can defend the station against this threat
CVector vPos = m_pObjective->GetPos() + (m_vThreatPotential.Normal() * DEFENSE_RANGE);
m_AICtx.ImplementFormationManeuver(m_pShip, vPos, NullVector, m_pShip->AlignToRotationAngle(VectorToPolar(m_vThreatPotential)));
// Loop over all our targets and make sure that they have enough ships
// taking care of them.
if (m_pShip->IsDestinyTime(17))
UpdateAttackTargets();
// Attack as appropriate
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
break;
}
default:
ASSERT(false);
}
}
void CFleetCommandAI::BehaviorStart (void)
// BehaviorStart
//
// Initiate behavior state based on orders
{
switch (GetCurrentOrder())
{
case IShipController::orderNone:
if (m_pShip->GetDockedObj() == NULL)
AddOrder(IShipController::orderGate, NULL, IShipController::SData());
break;
case IShipController::orderDestroyTarget:
{
CSpaceObject *pTarget = GetCurrentOrderTarget();
ASSERT(pTarget);
SetState(stateFormAtRallyPoint);
m_pObjective = pTarget;
m_vRallyPoint = ComputeRallyPoint(m_pObjective, &m_iRallyFacing);
OrderAllFormUp();
break;
}
case IShipController::orderGate:
{
// Look for the gate
CSpaceObject *pGate = GetCurrentOrderTarget();
if (pGate == NULL)
pGate = m_pShip->GetNearestStargate(true);
// Head for the gate
if (pGate)
{
SetState(stateOnCourseForStargate);
m_pObjective = pGate;
}
break;
}
case IShipController::orderGuard:
{
CSpaceObject *pPrincipal = GetCurrentOrderTarget();
ASSERT(pPrincipal);
SetState(stateWaitingForThreat);
m_pObjective = pPrincipal;
break;
}
case IShipController::orderWait:
{
DWORD dwWaitTime = GetCurrentOrderData();
SetState(stateWaiting);
if (dwWaitTime == 0)
m_iCounter = -1;
else
m_iCounter = 1 + (dwWaitTime * g_TicksPerSecond);
break;
}
}
}
void CFleetCommandAI::ComputeCombatPower (int *retiAssetPower, int *retiTargetPower)
// ComputeCombatPower
//
// Compute the power of assets and targets
{
int i;
if (retiAssetPower)
{
int iAssetPower = 0;
for (i = 0; i < m_iAssetCount; i++)
iAssetPower += m_pAssets[i].pAsset->GetCombatPower();
*retiAssetPower = iAssetPower;
}
if (retiTargetPower)
{
int iTargetPower = 0;
for (i = 0; i < m_iTargetCount; i++)
iTargetPower += m_pTargets[i].pTarget->GetCombatPower();
*retiTargetPower = iTargetPower;
}
}
bool CFleetCommandAI::ComputeFormationReady (void)
// ComputeFormationReady
//
// Returns TRUE if all our ships are in proper formation
{
int i;
Metric rMaxVel = (g_KlicksPerPixel / 4.0);
Metric rMaxVel2 = rMaxVel * rMaxVel;
for (i = 0; i < m_iAssetCount; i++)
if (m_pAssets[i].pAsset->GetVel().Length2() > rMaxVel2)
return false;
return true;
}
CVector CFleetCommandAI::ComputeRallyPoint (CSpaceObject *pTarget, int *retiFacing)
// ComputeRallyPoint
//
// Compute the point at which to rally to attack pTarget.
// We choose a point that is some distance away and aligned on a directional
// angle.
{
// Compute the current bearing and distance to the target
Metric rTargetDist;
int iTargetBearing = VectorToPolar(pTarget->GetPos() - m_pShip->GetPos(), &rTargetDist);
// Compute rally point
return ComputeRallyPointEx(iTargetBearing, pTarget, retiFacing);
}
CVector CFleetCommandAI::ComputeRallyPointEx (int iBearing, CSpaceObject *pTarget, int *retiFacing)
// ComputeRallyPointEx
//
// Compute the point at which to rally to attack pTarget based on an initial angle
{
// Reverse the angle and align to a direction
int iAngle = m_pShip->AlignToRotationAngle((iBearing + 180) % 360);
// Randomly offset (+/- rotation angle)
iAngle = ((iAngle + (mathRandom(-1, 1) * 18) + 360) % 360);
// Compute the distance from target. If we're a stand-off platform, make sure we
// rally inside of our weapon range.
Metric rDistance;
if (m_AICtx.GetCombatStyle() == aicombatStandOff)
{
m_AICtx.CalcBestWeapon(m_pShip, NULL, 0.0);
CInstalledDevice *pBestWeapon = m_AICtx.GetBestWeapon();
if (pBestWeapon)
rDistance = pBestWeapon->GetMaxEffectiveRange(m_pShip, pTarget);
else
rDistance = RALLY_RANGE;
}
else
rDistance = RALLY_RANGE;
// Return the facing
if (retiFacing)
*retiFacing = ((iAngle + 180) % 360);
// Return vector
return pTarget->GetPos() + PolarToVector(iAngle, rDistance);
}
CString CFleetCommandAI::DebugCrashInfo (void)
// DebugCrashInfo
//
// Returns debug crash info
{
int i;
CString sResult;
sResult.Append(CONSTLIT("CFleetCommandAI\r\n"));
sResult.Append(strPatternSubst(CONSTLIT("Order: %d\r\n"), (int)GetCurrentOrder()));
sResult.Append(strPatternSubst(CONSTLIT("m_State: %d\r\n"), m_State));
sResult.Append(strPatternSubst(CONSTLIT("m_pObjective: %s\r\n"), CSpaceObject::DebugDescribe(m_pObjective)));
sResult.Append(strPatternSubst(CONSTLIT("m_pTarget: %s\r\n"), CSpaceObject::DebugDescribe(m_pTarget)));
for (i = 0; i < m_iAssetCount; i++)
{
sResult.Append(strPatternSubst(CONSTLIT("Asset[%d].pAsset: %s\r\n"), i, CSpaceObject::DebugDescribe(m_pAssets[i].pAsset)));
sResult.Append(strPatternSubst(CONSTLIT("Asset[%d].pAsset: %s\r\n"), i, CSpaceObject::DebugDescribe(m_pAssets[i].pTarget)));
}
for (i = 0; i < m_iTargetCount; i++)
sResult.Append(strPatternSubst(CONSTLIT("Target[%d].pTarget: %s\r\n"), i, CSpaceObject::DebugDescribe(m_pTargets[i].pTarget)));
return sResult;
}
CFleetCommandAI::SAsset *CFleetCommandAI::FindAsset (CSpaceObject *pAsset, int *retiIndex)
// FindAsset
//
// Returns the asset record
{
int i;
for (i = 0; i < m_iAssetCount; i++)
if (m_pAssets[i].pAsset == pAsset)
{
if (retiIndex)
*retiIndex = i;
return &m_pAssets[i];
}
return NULL;
}
CFleetCommandAI::STarget *CFleetCommandAI::FindTarget (CSpaceObject *pTarget, int *retiIndex)
// FindTarget
//
// Returns the target record
{
int i;
for (i = 0; i < m_iTargetCount; i++)
if (m_pTargets[i].pTarget == pTarget)
{
if (retiIndex)
*retiIndex = i;
return &m_pTargets[i];
}
return NULL;
}
void CFleetCommandAI::ImplementAttackAtWill (void)
// ImplementAttackAtWill
//
// Attack objective and all enemies
{
// Attack our objective
m_AICtx.ImplementAttackTarget(m_pShip, m_pObjective);
// Recalc our targets
if (m_pShip->IsDestinyTime(31))
UpdateTargetList();
// Loop over all our targets and make sure that they have enough ships
// taking care of them.
if (m_pShip->IsDestinyTime(17))
{
UpdateAttackTargets();
OrderAttackTarget(m_pObjective);
}
// If we are outclassed, then we should re-group
if (m_pShip->IsDestinyTime(53))
{
int iAssetPower, iEnemyPower;
ComputeCombatPower(&iAssetPower, &iEnemyPower);
if (iEnemyPower > iAssetPower)
{
OrderAllFormUp(true);
SetState(stateNone);
}
}
}
void CFleetCommandAI::ImplementAttackFromRallyPoint (void)
// ImplementAttackFromRallyPoint
//
// Attack objective from rally point
{
// Stay in position
m_AICtx.ImplementFormationManeuver(m_pShip, m_vRallyPoint, NullVector, m_pShip->AlignToRotationAngle(m_iRallyFacing));
// Fire our weapon
m_AICtx.ImplementFireWeapon(m_pShip);
// Order all ships to open fire
if (!m_fOpenFireOrdered)
{
OrderAllOpenFire();
m_fOpenFireOrdered = true;
}
// If we're done, move to a new location
if (--m_iCounter <= 0)
{
SetState(stateFormAtRallyPoint);
m_pObjective = GetCurrentOrderTarget();
int iAngle = (m_iRallyFacing + 360 + mathRandom(-90, 90)) % 360;
m_vRallyPoint = ComputeRallyPointEx(iAngle, m_pObjective, &m_iRallyFacing);
OrderAllFormUp();
}
}
void CFleetCommandAI::ImplementChargeInFormation (void)
// ImplementChargeInFormation
//
// Charge towards objective
{
m_AICtx.ImplementFormationManeuver(m_pShip, m_pObjective->GetPos(), NullVector, m_pShip->AlignToRotationAngle(m_iRallyFacing));
// Compute our distance to the target
CVector vDist = m_pObjective->GetPos() - m_pShip->GetPos();
Metric rDist = vDist.Length();
// If we're within the break distance, then scatter
if (rDist < BREAK_CHARGE_RANGE)
{
#ifdef DEBUG_FLEET_COMMAND
g_pUniverse->GetPlayerShip()->SendMessage(m_pShip, CONSTLIT("Break charge range reached!"));
#endif
// Order fleet to break and attack targets
SetState(stateAttackAtWill);
m_pObjective = GetCurrentOrderTarget();
UpdateTargetList();
UpdateAttackTargets();
OrderAttackTarget(m_pObjective);
}
// Otherwise, fire as appropriate
else if (rDist < CHARGE_ATTACK_RANGE)
{
// Fire blindly
m_AICtx.ImplementFireWeapon(m_pShip);
// Order all ships to open fire
if (!m_fOpenFireOrdered)
{
#ifdef DEBUG_FLEET_COMMAND
g_pUniverse->GetPlayerShip()->SendMessage(m_pShip, CONSTLIT("Open fire!"));
#endif
OrderAllOpenFire();
m_fOpenFireOrdered = true;
}
}
}
void CFleetCommandAI::ImplementFormAtRallyPoint (void)
// ImplementFormAtRallyPoint
//
// Assemble the fleet at the rally point
{
// Move to rally point
m_AICtx.ImplementFormationManeuver(m_pShip, m_vRallyPoint, NullVector, m_pShip->AlignToRotationAngle(m_iRallyFacing));
// Every once in a while check to see if all our ships are in formation
if (m_pShip->IsDestinyTime(29))
{
// See if we are at the proper position. If not, bail out
CVector vDist = (m_vRallyPoint - m_pShip->GetPos());
Metric rDist2 = vDist.Length2();
if (rDist2 > (4.0 * g_KlicksPerPixel * g_KlicksPerPixel))
return;
// See if all our ships are in proper formation. If not, we're done
if (!ComputeFormationReady())
return;
// We are at the rally point, do the appropriate action based on
// the kind of ship that we are.
if (m_AICtx.GetCombatStyle() == aicombatStandOff)
{
SetState(stateAttackFromRallyPoint);
m_pObjective = GetCurrentOrderTarget();
m_iCounter = mathRandom(180, 360);
m_fOpenFireOrdered = false;
}
else
{
SetState(stateChargeInFormation);
m_pObjective = GetCurrentOrderTarget();
m_fOpenFireOrdered = false;
}
#ifdef DEBUG_FLEET_COMMAND
g_pUniverse->GetPlayerShip()->SendMessage(m_pShip, CONSTLIT("Rally point reached"));
#endif
}
}
void CFleetCommandAI::ImplementWaitAtPosition (void)
// ImplementWaitAtPosition
//
// Wait at the current location
{
// If we've lost a lot of ships, then attack a target
if (m_iStartingAssetCount > 2 * m_iAssetCount)
{
if (m_pTarget == NULL)
m_pTarget = m_pShip->GetNearestVisibleEnemy(ATTACK_RANGE);
if (m_pTarget)
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget);
else
m_AICtx.ImplementHold(m_pShip);
}
else
{
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
// Avoid target
if (m_pTarget)
{
CVector vTarget = m_pTarget->GetPos() - m_pShip->GetPos();
m_AICtx.ImplementSpiralOut(m_pShip, vTarget);
}
else
m_AICtx.ImplementHold(m_pShip);
}
// Recalc our targets
if (m_pShip->IsDestinyTime(31))
UpdateTargetList();
// Loop over all our targets and make sure that they have enough ships
// taking care of them.
if (m_pShip->IsDestinyTime(17))
UpdateAttackTargets();
// See if we're done waiting
if (m_iCounter > 0)
{
if (--m_iCounter == 0)
{
if (GetCurrentOrder() == IShipController::orderWait)
CancelCurrentOrder();
SetState(stateNone);
}
}
}
DWORD CFleetCommandAI::OnCommunicate (CSpaceObject *pSender, MessageTypes iMessage, CSpaceObject *pParam1, DWORD dwParam2)
// Communicate
//
// Handle communications from other objects
{
switch (iMessage)
{
case msgEscortReportingIn:
{
if (FindAsset(pSender) == NULL)
{
SAsset *pAsset = AddAsset(pSender);
// Enter formation
pAsset->iFormationPos = m_iAssetCount - 1;
DWORD dwFormation = MAKELONG(pAsset->iFormationPos, 0);
m_pShip->Communicate(pSender, msgFormUp, NULL, dwFormation);
}
return resAck;
}
default:
return resNoAnswer;
}
}
void CFleetCommandAI::OnDockedEvent (CSpaceObject *pObj)
// OnDockedEvent
//
// Event when the ship is docked
{
SetState(stateNone);
}
void CFleetCommandAI::OnNewSystemNotify (void)
// OnNewSystemNotify
//
// We find ourselves in a different system
{
int i;
CSystem *pNewSystem = m_pShip->GetSystem();
// Remove any assets that are not in the new system
int iNewCount = m_iAssetCount;
for (i = 0; i < m_iAssetCount; i++)
{
if (m_pAssets[i].pAsset->GetSystem() != pNewSystem)
{
IShipController::OrderTypes iOrder = m_pAssets[i].pAsset->AsShip()->GetController()->GetCurrentOrderEx();
m_pAssets[i].pAsset = NULL;
iNewCount--;
}
else
m_pAssets[i].pTarget = NULL;
}
// New array
if (iNewCount != m_iAssetCount)
{
SAsset *pNewArray = new SAsset [m_iAssetAlloc];
int j = 0;
for (i = 0; i < m_iAssetCount; i++)
if (m_pAssets[i].pAsset)
pNewArray[j++] = m_pAssets[i];
delete [] m_pAssets;
m_pAssets = pNewArray;
m_iAssetCount = j;
}
// Remove all targets
m_iTargetCount = 0;
}
void CFleetCommandAI::OnObjDestroyedNotify (const SDestroyCtx &Ctx)
// OnObjDestroyedNotify
//
// Handle the case where another object is destroyed
{
// Look for the object in the asset and target arrays
STarget *pTarget;
int iIndex;
// Remove the object from the asset data structure. If the
// object was a target, this also clears the pTarget variable
// (which makes the ship eligible for re-targeting).
if (RemoveAssetObj(Ctx.pObj))
{
// If one of our assets was destroyed, check to see if it was
// destroyed by a target.
if (Ctx.Attacker.GetObj()
&& (pTarget = FindTarget(Ctx.Attacker.GetObj())) != NULL)
pTarget->iKilled += Ctx.pObj->GetCombatPower();
// If our asset was attacking a target, remove it from the assignedTo
// field. (Obviously this could be a different enemy than the one
// that killed the asset).
if ((pTarget = FindTarget(Ctx.pObj->GetTarget(CItemCtx()))) != NULL)
pTarget->iAssignedTo -= Ctx.pObj->GetCombatPower();
}
// Otherwise, check to see if a target was destroyed
else if ((pTarget = FindTarget(Ctx.pObj, &iIndex)) != NULL)
RemoveTarget(iIndex);
// Act based on state
switch (m_State)
{
case stateAttackAtWill:
case stateChargeInFormation:
case stateFormAtRallyPoint:
case stateAttackFromRallyPoint:
case stateWaitingForThreat:
{
if (m_pObjective == Ctx.pObj)
CancelCurrentOrder();
break;
}
}
// Generic reset
if (m_pObjective == Ctx.pObj)
m_pObjective = NULL;
if (m_pTarget == Ctx.pObj)
m_pTarget = NULL;
}
void CFleetCommandAI::OnOrderChanged (void)
// OnOrderChanged
//
// The order list has changed
{
SetState(stateNone);
}
void CFleetCommandAI::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Read our data
//
// DWORD m_State
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pObjective (CSpaceObject ref)
// DWORD m_iCounter
// DWORD m_iStartingAssetCount
//
// DWORD no. of assets
// SAsset[]
//
// DWORD no. of targets
// STarget[]
//
// CVector m_vThreatPotential
// CVector m_vRallyPoint
// DWORD m_iRallyFacing
//
// DWORD flags
{
int i;
DWORD dwLoad;
Ctx.pStream->Read((char *)&m_State, sizeof(DWORD));
CSystem::ReadObjRefFromStream(Ctx, &m_pTarget);
CSystem::ReadObjRefFromStream(Ctx, &m_pObjective);
Ctx.pStream->Read((char *)&m_iCounter, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iStartingAssetCount, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iAssetCount, sizeof(DWORD));
if (m_iAssetCount)
{
m_iAssetAlloc = AlignUp(m_iAssetCount, ALLOC_GRANULARITY);
m_pAssets = new SAsset [m_iAssetAlloc];
for (i = 0; i < m_iAssetCount; i++)
{
CSystem::ReadObjRefFromStream(Ctx, &m_pAssets[i].pAsset);
Ctx.pStream->Read((char *)&m_pAssets[i].iFormationPos, sizeof(DWORD));
CSystem::ReadObjRefFromStream(Ctx, &m_pAssets[i].pTarget);
}
}
Ctx.pStream->Read((char *)&m_iTargetCount, sizeof(DWORD));
if (m_iTargetCount)
{
m_iTargetAlloc = AlignUp(m_iTargetCount, sizeof(DWORD));
m_pTargets = new STarget [m_iTargetAlloc];
for (i = 0; i < m_iTargetCount; i++)
{
CSystem::ReadObjRefFromStream(Ctx, &m_pTargets[i].pTarget);
Ctx.pStream->Read((char *)&m_pTargets[i].iAssignedTo, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_pTargets[i].iKilled, sizeof(DWORD));
}
}
Ctx.pStream->Read((char *)&m_vThreatPotential, sizeof(CVector));
Ctx.pStream->Read((char *)&m_vRallyPoint, sizeof(CVector));
Ctx.pStream->Read((char *)&m_iRallyFacing, sizeof(DWORD));
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_fOpenFireOrdered = ((dwLoad & 0x00000001) ? true : false);
}
void CFleetCommandAI::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write our data
//
// DWORD m_State
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pObjective (CSpaceObject ref)
// DWORD m_iCounter
// DWORD m_iStartingAssetCount
//
// DWORD no. of assets
// SAsset[]
//
// DWORD no. of targets
// STarget[]
//
// CVector m_vThreatPotential
// CVector m_vRallyPoint
// DWORD m_iRallyFacing
//
// DWORD flags
{
int i;
DWORD dwSave;
pStream->Write((char *)&m_State, sizeof(DWORD));
m_pShip->WriteObjRefToStream(m_pTarget, pStream);
m_pShip->WriteObjRefToStream(m_pObjective, pStream);
pStream->Write((char *)&m_iCounter, sizeof(DWORD));
pStream->Write((char *)&m_iStartingAssetCount, sizeof(DWORD));
pStream->Write((char *)&m_iAssetCount, sizeof(DWORD));
for (i = 0; i < m_iAssetCount; i++)
{
m_pShip->WriteObjRefToStream(m_pAssets[i].pAsset, pStream);
pStream->Write((char *)&m_pAssets[i].iFormationPos, sizeof(DWORD));
m_pShip->WriteObjRefToStream(m_pAssets[i].pTarget, pStream);
}
pStream->Write((char *)&m_iTargetCount, sizeof(DWORD));
for (i = 0; i < m_iTargetCount; i++)
{
m_pShip->WriteObjRefToStream(m_pTargets[i].pTarget, pStream);
pStream->Write((char *)&m_pTargets[i].iAssignedTo, sizeof(DWORD));
pStream->Write((char *)&m_pTargets[i].iKilled, sizeof(DWORD));
}
pStream->Write((char *)&m_vThreatPotential, sizeof(CVector));
pStream->Write((char *)&m_vRallyPoint, sizeof(CVector));
pStream->Write((char *)&m_iRallyFacing, sizeof(DWORD));
dwSave = 0;
dwSave |= (m_fOpenFireOrdered ? 0x00000001 : 0);