-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCWaitOrder.cpp
399 lines (293 loc) · 9.05 KB
/
CWaitOrder.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
// CWaitOrder.cpp
//
// CWaitOrder class implementation
// Copyright (c) 2014 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
const int ATTACK_TIME_THRESHOLD = 150;
const Metric ATTACK_RANGE = (20.0 * LIGHT_SECOND);
const Metric PATROL_SENSOR_RANGE = (30.0 * LIGHT_SECOND);
CWaitOrder::CWaitOrder (IShipController::OrderTypes iOrder) : IOrderModule(objCount),
m_iOrder(iOrder),
m_fWaitUntilLeaderUndocks(false),
m_fWaitForLeaderToApproach(false),
m_fAttackEnemies(false),
m_fWaitForEnemy(false),
m_fDeterEnemies(false),
m_fIsDeterring(false),
m_fWaitForThreat(false)
// CWaitOrder constructor
{
switch (iOrder)
{
case IShipController::orderHold:
m_fAttackEnemies = true;
m_fDeterEnemies = true;
break;
case IShipController::orderWait:
break;
case IShipController::orderWaitForEnemy:
m_fWaitForEnemy = true;
m_fAttackEnemies = true;
break;
case IShipController::orderWaitForTarget:
m_fWaitForLeaderToApproach = true;
m_fAttackEnemies = true;
break;
case IShipController::orderWaitForThreat:
m_fWaitForThreat = true;
break;
case IShipController::orderWaitForUndock:
m_fWaitUntilLeaderUndocks = true;
break;
}
}
void CWaitOrder::AttackEnemies (CShip *pShip, CAIBehaviorCtx &Ctx, bool bReady)
// AttackEnemies
//
// Attack enemies
{
// If we're deterring an enemy, attack
if (m_fIsDeterring
&& m_Objs[objTarget])
{
if (bReady)
Ctx.ImplementAttackTarget(pShip, m_Objs[objTarget], true);
// See if we need to stop the attack.
if (pShip->IsDestinyTime(20))
{
CPerceptionCalc Perception(pShip->GetPerception());
Metric rDist2 = pShip->GetDistance2(m_Objs[objTarget]);
// If the target is out of range, or if we can no longer detect it, then
// we stop attacking.
if (rDist2 > (4.0 * Ctx.GetMaxWeaponRange() * Ctx.GetMaxWeaponRange())
|| !Perception.CanBeTargeted(m_Objs[objTarget], rDist2))
{
m_fIsDeterring = false;
m_Objs[objTarget] = NULL;
}
// If we're not aggressive and we haven't been attacked in a while, then
// we stop.
if (!Ctx.IsAggressor()
&& !Ctx.IsBeingAttacked(3 * ATTACK_TIME_THRESHOLD))
{
m_fIsDeterring = false;
m_Objs[objTarget] = NULL;
}
}
}
// Otherwise, see if there are enemy ships that we need to attack
else if (m_fDeterEnemies
&& !Ctx.NoAttackOnThreat())
{
if (pShip->IsDestinyTime(30))
{
Metric rRange = (Ctx.IsAggressor() ? Ctx.GetMaxWeaponRange() : PATROL_SENSOR_RANGE);
CSpaceObject *pTarget = pShip->GetVisibleEnemyInRange(pShip, rRange);
if (pTarget)
{
m_fIsDeterring = true;
m_Objs[objTarget] = pTarget;
ASSERT(m_Objs[objTarget]->DebugIsValid() && m_Objs[objTarget]->NotifyOthersWhenDestroyed());
}
}
}
// Otherwise, we just attack any target that gets too close.
else
Ctx.ImplementAttackNearestTarget(pShip, ATTACK_RANGE, &m_Objs[objTarget]);
}
bool CWaitOrder::IsLeaderInRange (CShip *pShip)
// IsLeaderInRange
//
// Returns TRUE if the leader is in range.
{
CPerceptionCalc Perception(pShip->GetPerception());
Metric rRange2 = (m_Objs[objLeader]->GetPos() - pShip->GetPos()).Length2();
return ((m_rDistance <= 0.0 || rRange2 < (m_rDistance * m_rDistance))
&& Perception.CanBeTargeted(m_Objs[objLeader], rRange2));
}
void CWaitOrder::OnAttacked (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pAttacker, const SDamageCtx &Damage, bool bFriendlyFire)
// OnAttacked
//
// We've been attacked.
{
DEBUG_TRY
// If we're waiting for a threat and we got attacked, then we're done
// waiting.
if (m_fWaitForThreat
&& pAttacker
&& pAttacker->CanAttack())
pShip->CancelCurrentOrder();
// If we're waiting for enemies and someone deliberately hits us, then
// that counts.
else if (m_fWaitForEnemy
&& Ctx.IsSecondAttack()
&& pAttacker
&& pAttacker->CanAttack()
&& !bFriendlyFire)
pShip->CancelCurrentOrder();
// If we're waiting for a target, and if we get hit (and it's not friendly)
// then the target is here.
//
// NOTE: We don't debouce hits (with IsBeingAttacked) because even a stray
// shot from the target means that the target is here.
else if (m_fWaitForLeaderToApproach
&& pAttacker == m_Objs[objLeader]
&& !bFriendlyFire)
pShip->CancelCurrentOrder();
// If we're deterring enemies and we don't already have a target, and we
// feel like it, then attack this target.
else if (m_fDeterEnemies
&& m_Objs[objTarget] == NULL
&& pAttacker
&& pAttacker->CanAttack()
&& !bFriendlyFire
&& mathRandom(1, 3) == 1)
{
m_fIsDeterring = true;
m_Objs[objTarget] = pAttacker;
ASSERT(m_Objs[objTarget]->DebugIsValid() && m_Objs[objTarget]->NotifyOthersWhenDestroyed());
}
DEBUG_CATCH
}
void CWaitOrder::OnBehavior (CShip *pShip, CAIBehaviorCtx &Ctx)
// OnBehavior
//
// Do it
{
DEBUG_TRY
// Handle waiting
bool bInPlace;
Ctx.ImplementHold(pShip, &bInPlace);
// Attack while we wait?
if (m_fAttackEnemies)
{
// If desired, use primary weapons to attack enemies
AttackEnemies(pShip, Ctx, bInPlace);
// Use secondary weapons to attack enemies
Ctx.ImplementFireOnTargetsOfOpportunity(pShip, m_Objs[objTarget]);
}
// See if our timer has expired
if (m_iCountdown != -1 && m_iCountdown-- == 0)
pShip->CancelCurrentOrder();
// Otherwise, see if other conditions are fullfilled.
else if (m_fWaitUntilLeaderUndocks
&& m_Objs[objLeader]->GetDockedObj() == NULL)
pShip->CancelCurrentOrder();
else if (m_fWaitForLeaderToApproach
&& pShip->IsDestinyTime(17)
&& IsLeaderInRange(pShip))
pShip->CancelCurrentOrder();
else if (m_fWaitForEnemy
&& pShip->IsDestinyTime(17)
&& pShip->GetNearestVisibleEnemy())
pShip->CancelCurrentOrder();
else if (m_fWaitForThreat
&& pShip->IsDestinyTime(90)
&& pShip->GetDockedObj()
&& (pShip->GetDockedObj()->IsAngry() || pShip->GetDockedObj()->IsAbandoned() || pShip->GetDockedObj()->IsDestroyed()))
pShip->CancelCurrentOrder();
DEBUG_CATCH
}
void CWaitOrder::OnBehaviorStart (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pOrderTarget, const IShipController::SData &Data)
// OnBehaviorStart
//
// Initialize order module
{
DEBUG_TRY
DWORD dwTimer;
// If we're attacking enemies, then we can't be docked.
if (m_fAttackEnemies)
Ctx.Undock(pShip);
// Waiting for leader to undock: we need a leader and an optional timer.
if (m_fWaitUntilLeaderUndocks)
{
m_Objs[objLeader] = pOrderTarget;
ASSERT(m_Objs[objLeader]->DebugIsValid() && m_Objs[objLeader]->NotifyOthersWhenDestroyed());
dwTimer = Data.AsInteger();
}
// Waiting for leader to approach: we need a leader, a distance (which may be 0)
// and an optional timer.
else if (m_fWaitForLeaderToApproach)
{
m_Objs[objLeader] = pOrderTarget;
ASSERT(m_Objs[objLeader]->DebugIsValid() && m_Objs[objLeader]->NotifyOthersWhenDestroyed());
m_rDistance = LIGHT_SECOND * Data.AsInteger();
dwTimer = Data.AsInteger2();
}
// Otherwise, just get a timer.
else
dwTimer = Data.AsInteger();
// Set the timer in ticks
m_iCountdown = (dwTimer ? 1 + (g_TicksPerSecond * dwTimer) : -1);
DEBUG_CATCH
}
DWORD CWaitOrder::OnCommunicate (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pSender, MessageTypes iMessage, CSpaceObject *pParam1, DWORD dwParam2)
// OnCommunicate
//
// Handle communications
{
switch (iMessage)
{
case msgQueryWaitStatus:
return resAck;
default:
return resNoAnswer;
}
}
void CWaitOrder::OnDestroyed (CShip *pShip, SDestroyCtx &Ctx)
// OnDestroyed
//
// We've been destroyed.
{
CSpaceObject *pBase;
// If we're waiting on a threat at a station, then ask the station to
// avenge us if we die.
if (m_fWaitForThreat
&& (pBase = pShip->GetDockedObj())
&& !pBase->IsDestroyed()
&& !pBase->IsEnemy(pShip))
pBase->OnSubordinateDestroyed(Ctx);
}
void CWaitOrder::OnObjDestroyed (CShip *pShip, const SDestroyCtx &Ctx, int iObj, bool *retbCancelOrder)
// OnObjDestroyed
//
// An object was destroyed
{
// If we're deterring and the target got destroyed, we stop attacking
if (m_fIsDeterring && iObj == objTarget)
m_fIsDeterring = false;
// If we're waiting for a threat and the station we're docked with got
// destroyed, then we leave.
if (m_fWaitForThreat
&& pShip->GetDockedObj() == Ctx.pObj)
*retbCancelOrder = true;
}
void CWaitOrder::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Load data from saved game
{
DWORD dwLoad;
// Older version
if (Ctx.dwVersion < 109)
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
// Read basic info
Ctx.pStream->Read((char *)&m_iCountdown, sizeof(DWORD));
if (Ctx.dwVersion >= 109)
Ctx.pStream->Read((char *)&m_rDistance, sizeof(Metric));
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_fIsDeterring = ((dwLoad & 0x00000001) ? true : false);
}
void CWaitOrder::OnWriteToStream (CSystem *pSystem, IWriteStream *pStream)
// OnWriteToStream
//
// Write data to saved game
{
pStream->Write((char *)&m_iCountdown, sizeof(DWORD));
pStream->Write((char *)&m_rDistance, sizeof(Metric));
// Flags
DWORD dwSave = 0;
dwSave |= (m_fIsDeterring ? 0x00000001 : 0);
pStream->Write((char *)&dwSave, sizeof(DWORD));
}