-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathOrderModules.cpp
360 lines (260 loc) · 7.62 KB
/
OrderModules.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
// OrderModules.cpp
//
// Implementation of IOrderModule classes
// Copyright (c) 2011 by George Moromisato. All Rights Reserved.
#include "PreComp.h"
IOrderModule::IOrderModule (int iObjCount)
// IOrderModule constructor
{
int i;
ASSERT(iObjCount <= MAX_OBJS);
m_iObjCount = iObjCount;
for (i = 0; i < m_iObjCount; i++)
m_Objs[i] = NULL;
}
IOrderModule::~IOrderModule (void)
// IOrderModule destructor
{
}
void IOrderModule::Attacked (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pAttacker, const SDamageCtx &Damage, bool bFriendlyFire)
// Attacked
//
// Handle an attack from another object
{
DEBUG_TRY
// Tell our escorts that we were attacked, if necessary
if (pAttacker
&& !bFriendlyFire
&& pAttacker->CanAttack())
Ctx.CommunicateWithEscorts(pShip, msgAttackDeter, pAttacker);
// Let our subclass handle it.
OnAttacked(pShip, Ctx, pAttacker, Damage, bFriendlyFire);
DEBUG_CATCH
}
DWORD IOrderModule::Communicate (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pSender, MessageTypes iMessage, CSpaceObject *pParam1, DWORD dwParam2)
// Communicate
//
// Handle communications from another ship
{
switch (iMessage)
{
case msgEscortAttacked:
{
// Treat this as an attack on ourselves
SDamageCtx Dummy;
pShip->GetController()->OnAttacked(pParam1, Dummy);
return resAck;
}
case msgEscortReportingIn:
Ctx.SetHasEscorts(true);
return resAck;
case msgQueryAttackStatus:
return (IsAttacking() ? resAck : resNoAnswer);
default:
return OnCommunicate(pShip, Ctx, pSender, iMessage, pParam1, dwParam2);
}
}
IOrderModule *IOrderModule::Create (IShipController::OrderTypes iOrder)
// Create
//
// Creates an order module
{
DEBUG_TRY
switch (iOrder)
{
case IShipController::orderNone:
return NULL;
case IShipController::orderApproach:
return new CApproachOrder;
case IShipController::orderDestroyTarget:
case IShipController::orderAttackNearestEnemy:
case IShipController::orderAttackArea:
case IShipController::orderHoldAndAttack:
return new CAttackOrder(iOrder);
case IShipController::orderAttackStation:
return new CAttackStationOrder;
case IShipController::orderEscort:
case IShipController::orderFollow:
return new CEscortOrder(iOrder);
case IShipController::orderFireEvent:
return new CFireEventOrder;
case IShipController::orderDock:
case IShipController::orderGate:
case IShipController::orderGoTo:
case IShipController::orderGoToPos:
case IShipController::orderNavPath:
return new CNavigateOrder(iOrder);
case IShipController::orderSendMessage:
return new CSendMessageOrder;
case IShipController::orderSentry:
return new CSentryOrder;
case IShipController::orderFireWeapon:
case IShipController::orderUseItem:
return new CSimpleOrder(iOrder);
case IShipController::orderHold:
case IShipController::orderWait:
case IShipController::orderWaitForEnemy:
case IShipController::orderWaitForPlayer:
case IShipController::orderWaitForTarget:
case IShipController::orderWaitForThreat:
case IShipController::orderWaitForUndock:
return new CWaitOrder(iOrder);
case IShipController::orderGuard:
case IShipController::orderGateOnThreat:
case IShipController::orderGateOnStationDestroyed:
case IShipController::orderPatrol:
case IShipController::orderScavenge:
case IShipController::orderFollowPlayerThroughGate:
case IShipController::orderTradeRoute:
case IShipController::orderWander:
case IShipController::orderLoot:
case IShipController::orderMine:
case IShipController::orderDestroyPlayerOnReturn:
case IShipController::orderBombard:
case IShipController::orderAimAtTarget:
case IShipController::orderOrbit:
case IShipController::orderHoldCourse:
case IShipController::orderTurnTo:
case IShipController::orderDestroyTargetHold:
return NULL;
default:
ASSERT(false);
return NULL;
}
DEBUG_CATCH
}
CString IOrderModule::DebugCrashInfo (CShip *pShip)
// DebugCrashInfo
//
// Output debug info
{
int i;
CString sResult;
sResult.Append(CONSTLIT("IOrderModule\r\n"));
sResult.Append(strPatternSubst(CONSTLIT("Order: %d\r\n"), (int)pShip->GetCurrentOrder()));
for (i = 0; i < m_iObjCount; i++)
sResult.Append(strPatternSubst(CONSTLIT("m_Objs[%d]: %s\r\n"), i, CSpaceObject::DebugDescribe(m_Objs[i])));
sResult.Append(OnDebugCrashInfo());
return sResult;
}
void IOrderModule::Destroyed (CShip *pShip, SDestroyCtx &Ctx)
// Destroyed
//
// We've been destroyed
{
OnDestroyed(pShip, Ctx);
}
void IOrderModule::ObjDestroyed (CShip *pShip, const SDestroyCtx &Ctx)
// ObjDestroyed
//
// And object was destroyed
{
int i;
bool bCancelOrder = false;
for (i = 0; i < m_iObjCount; i++)
if (Ctx.pObj == m_Objs[i])
{
// If this object is a target, and a friendly ship destroyed it, then
// thank the object who helped.
if (GetTarget() == Ctx.pObj && Ctx.Attacker.IsCausedByFriendOf(pShip) && Ctx.Attacker.GetObj())
pShip->Communicate(Ctx.Attacker.GetObj(), msgNiceShooting);
// Clear out the variable. We do this first because the derived class
// might set it to something else (thus we don't want to clear it after
// the OnObjDestroyed call).
m_Objs[i] = NULL;
// Let our derived class handle it
OnObjDestroyed(pShip, Ctx, i, &bCancelOrder);
// If our derived class wants us to cancel the order, then we're done.
// (After we cancel the order, the order module will be invalid, so
// we need to leave.
if (bCancelOrder)
{
pShip->CancelCurrentOrder();
return;
}
}
}
void IOrderModule::ReadFromStream (SLoadCtx &Ctx)
// ReadFromStream
//
// Load save file
{
int i;
// Load the objects
DWORD dwCount;
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
for (i = 0; i < (int)dwCount; i++)
{
if (i < m_iObjCount)
CSystem::ReadObjRefFromStream(Ctx, &m_Objs[i]);
else
{
CSpaceObject *pDummy;
CSystem::ReadObjRefFromStream(Ctx, &pDummy);
}
}
// Let our derived class load
OnReadFromStream(Ctx);
}
void IOrderModule::WriteToStream (CSystem *pSystem, IWriteStream *pStream)
// WriteToStream
//
// Write to save file
{
int i;
// Save the objects
DWORD dwCount = m_iObjCount;
pStream->Write((char *)&dwCount, sizeof(DWORD));
for (i = 0; i < (int)dwCount; i++)
pSystem->WriteObjRefToStream(m_Objs[i], pStream);
// Let our derived class save
OnWriteToStream(pSystem, pStream);
}
// CGuardOrder ----------------------------------------------------------------
void CGuardOrder::OnBehavior (CShip *pShip, CAIBehaviorCtx &Ctx)
// OnBehavior
//
// Do it
{
}
void CGuardOrder::OnBehaviorStart (CShip *pShip, CAIBehaviorCtx &Ctx, CSpaceObject *pOrderTarget, const IShipController::SData &Data)
// OnBehaviorStart
//
// Start/restart behavior
{
#if 0
ASSERT(pOrderTarget);
m_pBase = pOrderTarget;
// If we're docked, wait for threat
if (pShip->GetDockedObj())
m_iState = stateWaitingForThreat;
// If we're very far from our principal and we can use a nav
// path, do it
else if (pShip->GetDistance2(m_pBase) > NAV_PATH_THRESHOLD2
&& CalcNavPath(m_pBase))
m_iState = stateReturningViaNavPath;
// Otherwise, return directly to base
else
m_iState = stateReturningFromThreat;
#endif
}
void CGuardOrder::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Load from stream
{
DWORD dwLoad;
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iState = (States)dwLoad;
CSystem::ReadObjRefFromStream(Ctx, &m_pBase);
}
void CGuardOrder::OnWriteToStream (CSystem *pSystem, IWriteStream *pStream)
// OnWriteToStream
//
// Save to stream
{
DWORD dwSave;
dwSave = (DWORD)m_iState;
pStream->Write((char *)&dwSave, sizeof(DWORD));
pSystem->WriteObjRefToStream(m_pBase, pStream);
}