-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCAutonAI.cpp
390 lines (309 loc) · 8.18 KB
/
CAutonAI.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
// CAutonAI.cpp
//
// CAutonAI class
#include "PreComp.h"
#define PATROL_SENSOR_RANGE (g_KlicksPerPixel * 1000.0)
#define MAX_ESCORT_DISTANCE (g_KlicksPerPixel * 400.0)
#define MAX_FOLLOW_DISTANCE (g_KlicksPerPixel * 350.0)
#define ATTACK_RANGE (g_KlicksPerPixel * 600.0)
CAutonAI::CAutonAI (void) :
m_State(stateNone),
m_pDest(NULL),
m_pTarget(NULL)
// CAutonAI constructor
{
}
void CAutonAI::Behavior (SUpdateCtx &Ctx)
// Behavior
//
// Fly, fight, die
{
// Reset
ResetBehavior();
// Behave according to our state
switch (m_State)
{
case stateNone:
BehaviorStart();
break;
case stateAttackingTarget:
ASSERT(m_pTarget);
m_AICtx.ImplementAttackTarget(m_pShip, m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip, m_pTarget);
break;
case stateEscorting:
m_AICtx.ImplementEscort(m_pShip, m_pDest, &m_pTarget);
break;
case stateFollowing:
{
ASSERT(m_pDest);
CVector vTarget = m_pDest->GetPos() - m_pShip->GetPos();
Metric rTargetDist2 = vTarget.Dot(vTarget);
Metric rMaxDist = (MAX_FOLLOW_DISTANCE) + (g_KlicksPerPixel * (m_pShip->GetDestiny() % 120));
if (rTargetDist2 > (rMaxDist * rMaxDist))
m_AICtx.ImplementCloseOnTarget(m_pShip, m_pDest, vTarget, rTargetDist2);
else if (rTargetDist2 < (g_KlicksPerPixel * g_KlicksPerPixel * 1024.0))
m_AICtx.ImplementSpiralOut(m_pShip, vTarget);
else
m_AICtx.ImplementStop(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;
}
case stateOnCourseForStargate:
m_AICtx.ImplementGating(m_pShip, m_pDest);
m_AICtx.ImplementAttackNearestTarget(m_pShip, ATTACK_RANGE, &m_pTarget);
m_AICtx.ImplementFireOnTargetsOfOpportunity(m_pShip);
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;
default:
ASSERT(false);
}
}
void CAutonAI::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::orderEscort:
{
// If this is a support ship, then we follow. Otherwise we
// are an armed escort.
if (m_AICtx.IsNonCombatant())
SetState(stateFollowing);
else
SetState(stateEscorting);
m_pDest = GetCurrentOrderTarget();
ASSERT(m_pDest);
m_pShip->Communicate(m_pDest, 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::orderWaitForPlayer:
{
SetState(stateWaiting);
break;
}
}
}
CString CAutonAI::DebugCrashInfo (void)
// DebugCrashInfo
//
// Returns debug crash info
{
CString sResult;
sResult.Append(CONSTLIT("CAutonAI\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_pDest: %s\r\n"), CSpaceObject::DebugDescribe(m_pDest)));
sResult.Append(strPatternSubst(CONSTLIT("m_pTarget: %s\r\n"), CSpaceObject::DebugDescribe(m_pTarget)));
return sResult;
}
CSpaceObject *CAutonAI::GetTarget (CItemCtx &ItemCtx, bool bNoAutoTarget) const
// GetTarget
//
// Returns the target that this ship is attacking
{
switch (m_State)
{
case stateNone:
return NULL;
default:
return m_pTarget;
}
}
DWORD CAutonAI::OnCommunicate (CSpaceObject *pSender, MessageTypes iMessage, CSpaceObject *pParam1, DWORD dwParam2)
// Communicate
//
// Handle communications from other objects
{
switch (iMessage)
{
case msgAttack:
case msgAttackDeter:
{
if (GetCurrentOrder() == IShipController::orderEscort
&& !m_AICtx.IsNonCombatant())
{
SetState(stateAttackingTarget);
m_pTarget = pParam1;
return resAck;
}
else
return resNoAnswer;
}
case msgAbort:
{
SetState(stateNone);
return resAck;
}
case msgFormUp:
{
if (m_State == stateWaiting || m_State == stateAttackingTarget)
{
SetState(stateNone);
return resAck;
}
else
return resNoAnswer;
}
case msgQueryCommunications:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
DWORD dwRes = 0;
if (!m_AICtx.IsNonCombatant())
dwRes |= resCanAttack;
if (m_State == stateAttackingTarget)
dwRes |= (resCanAbortAttack | resCanFormUp);
if (m_State != stateWaiting)
dwRes |= resCanWait;
else
dwRes |= resCanFormUp;
return dwRes;
}
else
return 0;
}
case msgQueryEscortStatus:
{
if (GetEscortPrincipal() == pParam1)
return resAck;
else
return resNoAnswer;
}
case msgQueryWaitStatus:
return (m_State == stateWaiting ? resAck : resNoAnswer);
case msgWait:
{
if (GetCurrentOrder() == IShipController::orderEscort)
{
SetState(stateWaiting);
m_pDest = GetCurrentOrderTarget();
return resAck;
}
else
return resNoAnswer;
}
default:
return resNoAnswer;
}
}
void CAutonAI::OnObjDestroyedNotify (const SDestroyCtx &Ctx)
// OnObjDestroyedNotify
//
// Handle the case where another object is destroyed
{
// If our target gets destroyed...
if (m_State == stateAttackingTarget)
if (Ctx.pObj == m_pTarget)
SetState(stateNone);
// Reset
if (m_pDest == Ctx.pObj)
m_pDest = NULL;
if (m_pTarget == Ctx.pObj)
m_pTarget = NULL;
}
void CAutonAI::OnOrderChanged (void)
// OnOrderChanged
//
// The order list has changed
{
SetState(stateNone);
}
void CAutonAI::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Read our data
//
// DWORD m_State
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pDest (CSpaceObject ref)
{
Ctx.pStream->Read((char *)&m_State, sizeof(DWORD));
CSystem::ReadObjRefFromStream(Ctx, &m_pTarget);
CSystem::ReadObjRefFromStream(Ctx, &m_pDest);
}
void CAutonAI::OnSystemLoadedNotify (void)
// OnSystemLoadedNotify
//
// Deal with stuff after the system is loaded.
{
#ifdef DEBUG
// Due to a bug in previous versions, we ended up with m_pDest NULL.
// I'm still not sure how this happened, but it could have been due
// to a bug with not being able to save objects out of the system.
//
// See:
// http://multiverse.kronosaur.com/crashReport.hexm?clientVersion=Transcendence%2fWindows%2f1.1&reportedOn=2013-11-26T09%3a19%3a51.0976
// http://multiverse.kronosaur.com/crashReport.hexm?clientVersion=Transcendence%2fWindows%2f1.1&reportedOn=2013-11-26T08%3a58%3a43.0510
if (m_State == stateOnCourseForStargate
&& m_pDest == NULL)
m_pDest = m_pShip->GetNearestStargate();
#endif
}
void CAutonAI::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write our data
//
// DWORD m_State
// DWORD m_pTarget (CSpaceObject ref)
// DWORD m_pDest (CSpaceObject ref)
{
pStream->Write((char *)&m_State, sizeof(DWORD));
m_pShip->WriteObjRefToStream(m_pTarget, pStream);
m_pShip->WriteObjRefToStream(m_pDest, pStream);
}
void CAutonAI::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;
m_pDest = 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();
}