-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCFleetShipAI.cpp
892 lines (715 loc) · 19.1 KB
/
CFleetShipAI.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
// CFleetShipAI.cpp
//
// CFleetShipAI class
#include "PreComp.h"
#define ATTACK_AT_WILL_RANGE (g_KlicksPerPixel * 2400.0)
#define PATROL_SENSOR_RANGE (g_KlicksPerPixel * 1000.0)
const Metric PATROL_ORBIT_DIST = (LIGHT_SECOND * 10.0);
#define ATTACK_RANGE (g_KlicksPerPixel * 600.0)
struct SFormationPlace
{
int iAngle; // Angle with respect to leader
int iDistance; // Distance in pixels
};
struct SFormationDesc
{
SFormationPlace *pFormation;
int iCount;
};
static SFormationPlace g_ArrowFormation[] =
{
{ 120, 80 },
{ 240, 80 },
{ 120, 160 },
{ 240, 160 },
{ 150, 75 }, // 5
{ 210, 75 },
{ 120, 240 },
{ 240, 240 },
{ 150, 150 },
{ 210, 150 }, // 10
{ 120, 320 },
{ 240, 320 },
{ 150, 225 },
{ 210, 225 },
{ 120, 400 }, // 15
{ 240, 400 },
{ 135, 200 },
{ 225, 200 },
{ 150, 300 },
{ 210, 300 }, // 20
{ 120, 480 },
{ 240, 480 },
{ 135, 280 },
{ 225, 280 },
{ 135, 360 }, // 25
{ 225, 360 },
{ 180, 125 },
{ 170, 175 },
{ 190, 175 },
{ 180, 250 }, // 30
{ 165, 240 },
{ 195, 240 },
{ 170, 300 },
{ 190, 300 },
{ 135, 130 }, // 35
{ 225, 130 },
};
#define ARROW_FORMATION_COUNT (sizeof(g_ArrowFormation) / sizeof(g_ArrowFormation[0]))
static SFormationPlace g_FrontFormation[] =
{
{ 90, 80 },
{ 270, 80 },
{ 90, 160 },
{ 270, 160 },
{ 90, 240 },
{ 270, 240 },
{ 90, 320 },
{ 270, 320 },
{ 90, 400 },
{ 270, 400 },
{ 90, 480 },
{ 270, 480 },
{ 90, 560 },
{ 270, 560 },
{ 90, 640 },
{ 270, 640 },
{ 135, 57 },
{ 225, 57 },
{ 108, 126 },
{ 252, 126 },
{ 101, 204 },
{ 259, 204 },
{ 98, 283 },
{ 262, 283 },
};
#define FRONT_FORMATION_COUNT (sizeof(g_FrontFormation) / sizeof(g_FrontFormation[0]))
static SFormationPlace g_ShellFormation[] =
{
{ 30, 240 },
{ 330, 240 },
{ 60, 240 },
{ 300, 240 },
{ 90, 240 },
{ 270, 240 },
{ 120, 240 },
{ 240, 240 },
{ 150, 240 },
{ 210, 240 },
{ 180, 240 },
{ 0, 240 },
{ 15, 320 },
{ 345, 320 },
{ 45, 320 },
{ 315, 320 },
{ 75, 320 },
{ 285, 320 },
{ 105, 320 },
{ 255, 320 },
{ 135, 320 },
{ 225, 320 },
{ 165, 320 },
{ 195, 320 },
};
#define SHELL_FORMATION_COUNT (sizeof(g_ShellFormation) / sizeof(g_ShellFormation[0]))
SFormationDesc g_Formations[] =
{
{ g_ArrowFormation, ARROW_FORMATION_COUNT },
{ g_FrontFormation, FRONT_FORMATION_COUNT },
{ g_ShellFormation, SHELL_FORMATION_COUNT },
};
#define FORMATIONS_COUNT (sizeof(g_Formations) / sizeof(g_Formations[0]))
CFleetShipAI::CFleetShipAI (void) :
m_State(stateNone),
m_pLeader(NULL),
m_pTarget(NULL),
m_pDest(NULL)
// CFleetShipAI constructor
{
}
void CFleetShipAI::Behavior (SUpdateCtx &Ctx)
// Behavior
{
// Reset
ResetBehavior();
// Use basic items
UseItemsBehavior();
// Behave according to our state
switch (m_State)
{
case stateNone:
BehaviorStart();
break;
case stateAttackAtWill:
{
// If we don't have a target, find one
if (m_pTarget == NULL && m_pShip->IsDestinyTime(13))
m_pTarget = m_pShip->GetNearestVisibleEnemy(ATTACK_AT_WILL_RANGE, true);
// If we have a target, attack
if (m_pTarget)
{
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip, m_pTarget);
}
// Otherwise, stay in formation
else
{
ImplementKeepFormation();
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
ImplementFireOnNearestTarget();
}
break;
}
case stateAttackInFormation:
{
bool bInFormation;
ImplementKeepFormation(&bInFormation);
// Fire the primary weapon (most of the time)
if (bInFormation && mathRandom(1, 3) > 1)
m_AICtx.ImplementFireWeapon(m_pShip);
// Decrement counter
if (m_iCounter > 0)
{
if (--m_iCounter == 0)
{
SetState(stateKeepFormation);
m_iCounter = 0;
}
}
break;
}
case stateAttackOnPatrol:
ASSERT(m_pTarget);
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip, m_pTarget);
// Check to see if we've wandered outside our patrol zone
if (m_pShip->IsDestinyTime(20))
{
CSpaceObject *pCenter = GetCurrentOrderTarget();
Metric rMaxRange2 = PATROL_ORBIT_DIST + PATROL_SENSOR_RANGE;
rMaxRange2 = rMaxRange2 * rMaxRange2;
Metric rMinRange2 = max(0.0, PATROL_ORBIT_DIST - PATROL_SENSOR_RANGE);
rMinRange2 = rMinRange2 * rMinRange2;
int iTick = m_pShip->GetSystem()->GetTick();
CVector vRange = pCenter->GetPos() - m_pShip->GetPos();
Metric rDistance2 = vRange.Dot(vRange);
// If we're outside of our patrol range then stop the attack
if (rDistance2 > rMaxRange2 || rDistance2 < rMinRange2)
SetState(stateNone);
}
break;
case stateAttackTarget:
{
ASSERT(m_pTarget);
if (m_pTarget->CanThrust())
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget);
else
{
// Attack the target as best we can
CVector vTarget = m_pTarget->GetPos() - m_pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
m_AICtx.ImplementFireWeaponOnTarget(m_pShip, -1, -1, m_pTarget, vTarget, rTargetDist2);
// Maneuver to a proper position near the target
Metric rRange;
int iCounterAdj;
if (m_AICtx.GetCombatStyle() == aicombatStandOff)
{
rRange = Max(m_pTarget->GetHitSize(), (0.5 * m_AICtx.GetBestWeaponRange()));
iCounterAdj = 2;
}
else
{
rRange = (0.5 * m_pTarget->GetHitSize()) + (0.2 * m_AICtx.GetBestWeaponRange());
iCounterAdj = 1;
}
if (m_iCounter == 0)
{
m_vVector = ComputeAttackPos(m_pTarget, rRange, &m_iAngle);
m_iCounter = mathRandom(130, 160) * iCounterAdj;
}
else
m_iCounter--;
m_AICtx.ImplementFormationManeuver(m_pShip, m_vVector, NullVector, m_pShip->AlignToRotationAngle(m_iAngle));
}
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip, m_pTarget);
break;
}
case stateKeepFormation:
ImplementKeepFormation();
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
ImplementFireOnNearestTarget();
break;
case stateOnCourseForDocking:
ASSERT(m_pDest);
m_AICtx.ImplementDocking(m_pShip, m_pDest);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
ImplementFireOnNearestTarget();
break;
case stateOnCourseForStargate:
ASSERT(m_pDest);
m_AICtx.ImplementGating(m_pShip, m_pDest);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
ImplementFireOnNearestTarget();
break;
case stateOnPatrolOrbit:
ASSERT(m_pDest);
m_AICtx.ImplementOrbit(m_pShip, m_pDest, PATROL_ORBIT_DIST);
m_AICtx.ImplementAttackNearestTarget(m_pShip, m_AICtx.GetMaxWeaponRange(), &m_pTarget, m_pDest);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip, NULL, m_pDest);
// Check to see if any enemy ships appear
if (m_pShip->IsDestinyTime(30))
{
CSpaceObject *pPrincipal = GetCurrentOrderTarget();
CSpaceObject *pTarget = CalcEnemyShipInRange(pPrincipal, PATROL_SENSOR_RANGE, m_pDest);
if (pTarget)
{
SetState(stateAttackOnPatrol);
m_pTarget = pTarget;
ASSERT(m_pTarget->DebugIsValid() && m_pTarget->NotifyOthersWhenDestroyed());
}
}
break;
case stateWaiting:
m_AICtx.ImplementHold(m_pShip);
if (m_pTarget)
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget, true);
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
break;
}
}
void CFleetShipAI::BehaviorStart (void)
// BehaviorStart
//
// Figure out what to do based on orders
{
switch (GetCurrentOrder())
{
case IShipController::orderNone:
{
if (m_pShip->GetDockedObj() == NULL)
AddOrder(IShipController::orderGate, NULL, IShipController::SData());
break;
}
case IShipController::orderAttackNearestEnemy:
{
CSpaceObject *pTarget = m_pShip->GetNearestVisibleEnemy(ATTACK_AT_WILL_RANGE, true);
if (pTarget)
{
SetState(stateAttackTarget);
m_pTarget = pTarget;
m_iCounter = 0;
}
else
CancelCurrentOrder();
break;
}
case IShipController::orderDestroyTarget:
{
SetState(stateAttackTarget);
m_pTarget = GetCurrentOrderTarget();
m_iCounter = 0;
ASSERT(m_pTarget);
break;
}
case IShipController::orderDock:
{
CSpaceObject *pDest = GetCurrentOrderTarget();
ASSERT(pDest);
// If we're docked with our destination then we're done.
if (m_pShip->GetDockedObj() == pDest)
CancelCurrentOrder();
// Otherwise, try to dock
else
{
SetState(stateOnCourseForDocking);
m_pDest = pDest;
}
break;
}
case IShipController::orderEscort:
{
SetState(stateKeepFormation);
m_pLeader = GetCurrentOrderTarget();
ASSERT(m_pLeader);
m_iFormation = (int)HIWORD(GetCurrentOrderData());
m_iPlace = (int)LOWORD(GetCurrentOrderData());
ASSERT(m_iFormation < FORMATIONS_COUNT);
// If there is no place for this ship in the formation, then
// gate-out
if (m_iFormation >= FORMATIONS_COUNT
|| m_iPlace >= g_Formations[m_iFormation].iCount
|| m_pLeader == NULL)
{
CancelCurrentOrder();
break;
}
// Keep formation
m_pShip->Communicate(m_pLeader, msgEscortReportingIn, m_pShip);
break;
}
case IShipController::orderFollowPlayerThroughGate:
{
SetState(stateOnCourseForStargate);
m_pDest = m_pShip->GetNearestStargate();
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_pDest = pGate;
}
break;
}
case IShipController::orderPatrol:
case IShipController::orderGuard:
{
SetState(stateOnPatrolOrbit);
m_pDest = GetCurrentOrderTarget();
ASSERT(m_pDest);
break;
}
}
}
CVector CFleetShipAI::ComputeAttackPos (CSpaceObject *pTarget, Metric rDist, int *retiFacing)
// ComputeAttackPos
//
// Computes a random point to attack a static target
{
int iAngle = m_pShip->AlignToRotationAngle(mathRandom(0, 359));
if (retiFacing)
*retiFacing = (iAngle + 180) % 360;
Metric rVariation = mathRandom(90, 110) / 100.0f;
return pTarget->GetPos() + PolarToVector(iAngle, rVariation * rDist);
}
CString CFleetShipAI::DebugCrashInfo (void)
// DebugCrashInfo
//
// Returns debug crash info
{
CString sResult;
sResult.Append(CONSTLIT("CFleetShipAI\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_pLeader: %s\r\n"), CSpaceObject::DebugDescribe(m_pLeader)));
sResult.Append(strPatternSubst(CONSTLIT("m_pDest: %s\r\n"), CSpaceObject::DebugDescribe(m_pDest)));
sResult.Append(strPatternSubst(CONSTLIT("m_pTarget: %s\r\n"), CSpaceObject::DebugDescribe(m_pTarget)));
return sResult;
}
CSpaceObject *CFleetShipAI::GetTarget (CItemCtx &ItemCtx, bool bNoAutoTarget) const
// GetTarget
//
// Returns the target for this ship
{
return m_pTarget;
}
void CFleetShipAI::ImplementFireOnNearestTarget (void)
// ImplementFireOnNearestTarget
//
// Fire on the nearest target
{
if (m_pShip->IsDestinyTime(20))
m_pTarget = m_pShip->GetNearestVisibleEnemy();
if (m_pTarget)
{
bool bOutOfRange;
m_AICtx.ImplementFireOnTarget(m_pShip, m_pTarget, &bOutOfRange);
if (bOutOfRange)
m_pTarget = NULL;
}
}
void CFleetShipAI::ImplementKeepFormation (bool *retbInFormation)
// ImplementKeepFormation
//
// Stay in formation
{
if (m_pLeader == NULL)
{
if (retbInFormation)
*retbInFormation = true;
return;
}
SFormationPlace *pFormation = g_Formations[m_iFormation].pFormation;
// Figure out where our place in the formation is
int iAngle = pFormation[m_iPlace].iAngle;
int iDist = pFormation[m_iPlace].iDistance;
CVector vDest = m_pLeader->GetPos()
+ PolarToVector(m_pLeader->GetRotation() + iAngle, iDist * g_KlicksPerPixel);
CVector vDestVel = m_pLeader->GetVel();
// Form up
m_AICtx.ImplementFormationManeuver(m_pShip, vDest, vDestVel, m_pShip->AlignToRotationAngle(m_pLeader->GetRotation()), retbInFormation);
}
DWORD CFleetShipAI::OnCommunicate (CSpaceObject *pSender, MessageTypes iMessage, CSpaceObject *pParam1, DWORD dwParam2)
// Communicate
//
// Handle communications from other objects
{
switch (iMessage)
{
case msgAbort:
{
if (m_State == stateAttackTarget
|| m_State == stateAttackAtWill
|| m_State == stateAttackInFormation)
SetState(stateNone);
return resAck;
}
case msgAttack:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
SetState(stateAttackTarget);
m_pTarget = pParam1;
m_iCounter = 0;
ASSERT(m_pTarget);
return resAck;
}
else
return resNoAnswer;
}
case msgAttackInFormation:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
SetState(stateAttackInFormation);
m_iCounter = dwParam2;
return resAck;
}
else
return resNoAnswer;
}
case msgBreakAndAttack:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
SetState(stateAttackAtWill);
return resAck;
}
else
return resNoAnswer;
}
case msgFormUp:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
if (dwParam2 != 0xffffffff)
SetCurrentOrderData(SData(dwParam2));
SetState(stateNone);
return resAck;
}
else
return resNoAnswer;
}
case msgQueryCommunications:
{
if (GetCurrentOrder() == IShipController::orderEscort && GetCurrentOrderTarget() == pSender)
{
DWORD dwRes = (resCanBeInFormation | resCanBreakAndAttack);
if (pSender->GetTarget(CItemCtx(), true))
dwRes |= resCanAttack;
if (m_State == stateAttackTarget
|| m_State == stateAttackAtWill
|| m_State == stateAttackInFormation)
dwRes |= resCanAbortAttack;
else if (m_State == stateKeepFormation)
dwRes |= resCanAttackInFormation;
if (m_State == stateWaiting)
dwRes |= resCanFormUp;
else
dwRes |= resCanWait;
return dwRes;
}
else
return resNoAnswer;
}
case msgQueryEscortStatus:
case msgQueryFleetStatus:
{
if (GetEscortPrincipal() == pParam1)
return resAck;
else
return resNoAnswer;
}
case msgQueryWaitStatus:
return (m_State == stateWaiting ? resAck : resNoAnswer);
case msgWait:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
SetState(stateWaiting);
return resAck;
}
else
return resNoAnswer;
}
default:
return resNoAnswer;
}
}
void CFleetShipAI::OnDockedEvent (CSpaceObject *pObj)
// OnDockedEvent
//
// Event when the ship is docked
{
SetState(stateNone);
}
CSpaceObject *CFleetShipAI::OnGetBase (void) const
// OnGetBase
//
// Returns the base for this ship.
{
switch (GetCurrentOrder())
{
case IShipController::orderPatrol:
case IShipController::orderGuard:
return GetCurrentOrderTarget();
default:
return NULL;
}
}
void CFleetShipAI::OnObjDestroyedNotify (const SDestroyCtx &Ctx)
// OnObjDestroyedNotify
//
// Deal with an object that has been destroyed
{
switch (GetCurrentOrder())
{
case IShipController::orderEscort:
if (Ctx.pObj == GetCurrentOrderTarget())
{
CancelCurrentOrder();
// Get the orders of the leader
IShipController::OrderTypes iLeaderOrders = IShipController::orderNone;
CSpaceObject *pLeaderTarget = NULL;
if (Ctx.pObj && Ctx.pObj->GetCategory() == CSpaceObject::catShip)
{
CShip *pLeader = Ctx.pObj->AsShip();
if (pLeader)
iLeaderOrders = pLeader->GetController()->GetCurrentOrderEx(&pLeaderTarget);
}
// Avenge the leader
int iAvengeChance = (pLeaderTarget ? 40 : 100);
CSpaceObject *pAttacker = Ctx.Attacker.GetObj();
if (pAttacker
&& Ctx.Attacker.IsCausedByNonFriendOf(m_pShip)
&& pAttacker != pLeaderTarget
&& mathRandom(1, 100) <= iAvengeChance)
AddOrder(IShipController::orderDestroyTarget, pAttacker, IShipController::SData());
// Take on leader's orders
switch (iLeaderOrders)
{
case IShipController::orderDestroyTarget:
case IShipController::orderGuard:
if (pLeaderTarget)
AddOrder(iLeaderOrders, pLeaderTarget, IShipController::SData());
break;
}
// Attack other enemies
AddOrder(IShipController::orderAttackNearestEnemy, NULL, IShipController::SData());
}
break;
case IShipController::orderDock:
case IShipController::orderDestroyTarget:
case IShipController::orderPatrol:
case IShipController::orderGuard:
if (Ctx.pObj == GetCurrentOrderTarget())
CancelCurrentOrder();
break;
}
// If our target gets destroyed...
switch (m_State)
{
case stateAttackTarget:
case stateAttackOnPatrol:
if (Ctx.pObj == m_pTarget)
SetState(stateNone);
break;
}
// Reset
if (m_pDest == Ctx.pObj)
m_pDest = NULL;
if (m_pTarget == Ctx.pObj)
m_pTarget = NULL;
}
void CFleetShipAI::OnOrderChanged (void)
// OnOrderChanged
//
// The order list has changed
{
SetState(stateNone);
m_pLeader = NULL;
m_pDest = NULL;
}
void CFleetShipAI::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Read our data
//
// DWORD m_State
// DWORD m_pLeader (CSpaceObject ref)
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pDest (CSpaceObject ref)
// DWORD m_iFormation
// DWORD m_iPlace
// DWORD m_iCounter
// DWORD m_iAngle
// DWORD m_vVector
{
Ctx.pStream->Read((char *)&m_State, sizeof(DWORD));
CSystem::ReadObjRefFromStream(Ctx, &m_pLeader);
CSystem::ReadObjRefFromStream(Ctx, &m_pTarget);
CSystem::ReadObjRefFromStream(Ctx, &m_pDest);
Ctx.pStream->Read((char *)&m_iFormation, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iPlace, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iCounter, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iAngle, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_vVector, sizeof(CVector));
}
void CFleetShipAI::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write our data
//
// DWORD m_State
// DWORD m_pLeader (CSpaceObject ref)
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pDest (CSpaceObject ref)
// DWORD m_iFormation
// DWORD m_iPlace
// DWORD m_iCounter
// DWORD m_iAngle
// DWORD m_vVector
{
pStream->Write((char *)&m_State, sizeof(DWORD));
m_pShip->WriteObjRefToStream(m_pLeader, pStream);
m_pShip->WriteObjRefToStream(m_pTarget, pStream);
m_pShip->WriteObjRefToStream(m_pDest, pStream);
pStream->Write((char *)&m_iFormation, sizeof(DWORD));
pStream->Write((char *)&m_iPlace, sizeof(DWORD));
pStream->Write((char *)&m_iCounter, sizeof(DWORD));
pStream->Write((char *)&m_iAngle, sizeof(DWORD));
pStream->Write((char *)&m_vVector, sizeof(CVector));
}
void CFleetShipAI::SetState (StateTypes State)
// SetState
//
// Sets the current state
{
// Set state (NOTE: We do this before we undock because the Undock
// call may destroy the station and cause us to recurse into SetState.
// This happens when a ship is guarding an empty cargo crate).
m_State = State;
m_pTarget = NULL;
// If we're currently docked and we're changing to a state that
// does not support docking, then we undock first.
if (m_pShip->GetDockedObj()
&& State != stateNone)
m_pShip->Undock();
}