-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCAutoDefenseClass.cpp
584 lines (428 loc) · 14.1 KB
/
CAutoDefenseClass.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
// CAutoDefenseClass.cpp
//
// CAutoDefenseClass class
// Copyright (c) 2004 by George Moromisato. All Rights Reserved.
#include "PreComp.h"
#define RECHARGE_TIME_ATTRIB CONSTLIT("rechargeTime")
#define FIRE_RATE_ATTRIB CONSTLIT("fireRate")
#define INTERCEPT_RANGE_ATTRIB CONSTLIT("interceptRange")
#define TARGET_ATTRIB CONSTLIT("target")
#define TARGET_CRITERIA_ATTRIB CONSTLIT("targetCriteria")
#define WEAPON_ATTRIB CONSTLIT("weapon")
#define MAX_FIRE_ARC_ATTRIB CONSTLIT("maxFireArc")
#define MIN_FIRE_ARC_ATTRIB CONSTLIT("minFireArc")
#define OMNIDIRECTIONAL_ATTRIB CONSTLIT("omnidirectional")
#define MISSILES_TARGET CONSTLIT("missiles")
#define PROPERTY_FIRE_DELAY CONSTLIT("fireDelay")
#define PROPERTY_FIRE_RATE CONSTLIT("fireRate")
#define PROPERTY_ENABLED CONSTLIT("enabled")
#define PROPERTY_EXTERNAL CONSTLIT("external")
#define PROPERTY_FIRE_ARC CONSTLIT("fireArc")
#define PROPERTY_OMNIDIRECTIONAL CONSTLIT("omnidirectional")
const int DEFAULT_INTERCEPT_RANGE = 10;
CAutoDefenseClass::CAutoDefenseClass (void)
// CAutoDefenseClass constructor
{
}
CSpaceObject *CAutoDefenseClass::FindTarget (CInstalledDevice *pDevice, CSpaceObject *pSource)
// FindTarget
//
// Returns an appropriate target (or NULL).
{
// Look for a target
bool isOmniDirectional = false;
int iMinFireArc, iMaxFireArc;
// Look for a target
CSpaceObject *pBestTarget = NULL;
CSystem *pSystem = pSource->GetSystem();
CVector vSourcePos = pDevice->GetPos(pSource);
// Find out whether our autoDefenseDevice is omnidirectional,
// directional or has a fixed angle
if (IsOmniDirectional(pDevice))
isOmniDirectional = true;
// If not omnidirectional, check directional. If not, then
// our device points in one direction
else if (!IsDirectional(pDevice, &iMinFireArc, &iMaxFireArc))
{
iMinFireArc = AngleMod((pDevice ? pDevice->GetRotation() : 0)
+ AngleMiddle(m_iMinFireArc, m_iMaxFireArc));
iMaxFireArc = iMinFireArc;
}
// Calculate min/max fire arcs given the object's rotation
iMinFireArc = (pSource->GetRotation() + iMinFireArc) % 360;
iMaxFireArc = (pSource->GetRotation() + iMaxFireArc) % 360;
// Use the appropriate targeting method
switch (m_iTargeting)
{
// Hard-code search for the nearest missile
case trgMissiles:
{
Metric rBestDist2 = m_rInterceptRange * m_rInterceptRange;
for (int i = 0; i < pSystem->GetObjectCount(); i++)
{
CSpaceObject *pObj = pSystem->GetObject(i);
if (pObj
&& pObj->GetCategory() == CSpaceObject::catMissile
&& !pObj->GetDamageSource().IsEqual(pSource)
&& !pObj->IsIntangible()
&& pSource->IsEnemy(pObj->GetDamageSource())
&& (AngleInArc(VectorToPolar((pObj->GetPos() - vSourcePos)), iMinFireArc, iMaxFireArc) ||
isOmniDirectional))
{
CVector vRange = pObj->GetPos() - vSourcePos;
Metric rDistance2 = vRange.Dot(vRange);
if (rDistance2 < rBestDist2)
{
pBestTarget = pObj;
rBestDist2 = rDistance2;
}
}
}
return pBestTarget;
}
// Look for an object by criteria
case trgCriteria:
{
// First we set the source
m_TargetCriteria.SetSource(pSource);
// Compute the range
Metric rBestDist2;
if (m_TargetCriteria.MatchesMaxRadius() < g_InfiniteDistance)
rBestDist2 = (m_TargetCriteria.MatchesMaxRadius() * m_TargetCriteria.MatchesMaxRadius());
else
rBestDist2 = m_rInterceptRange * m_rInterceptRange;
// Now look for the nearest object
CSpaceObjectCriteria::SCtx Ctx(m_TargetCriteria);
for (int i = 0; i < pSystem->GetObjectCount(); i++)
{
CSpaceObject *pObj = pSystem->GetObject(i);
Metric rDistance2;
if (pObj
&& (m_TargetCriteria.MatchesCategory(pObj->GetCategory()))
&& ((rDistance2 = (pObj->GetPos() - vSourcePos).Length2()) < rBestDist2)
&& pObj->MatchesCriteria(Ctx, m_TargetCriteria)
&& !pObj->IsIntangible()
&& pObj != pSource
&& (AngleInArc(VectorToPolar((pObj->GetPos() - vSourcePos)), iMinFireArc, iMaxFireArc) ||
isOmniDirectional))
{
pBestTarget = pObj;
rBestDist2 = rDistance2;
}
}
return pBestTarget;
}
default:
ASSERT(false);
return NULL;
}
}
int CAutoDefenseClass::GetActivateDelay (CItemCtx &ItemCtx) const
// GetActivateDelay
//
// Returns the activation delay
{
return m_iRechargeTicks;
}
int CAutoDefenseClass::CalcPowerUsed (SUpdateCtx &Ctx, CInstalledDevice *pDevice, CSpaceObject *pSource)
// CalcPowerUsed
//
// Return power used by device
{
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon == NULL || !pDevice->IsEnabled())
return 0;
return pWeapon->CalcPowerUsed(Ctx, pDevice, pSource);
}
DamageTypes CAutoDefenseClass::GetDamageType (CItemCtx &Ctx, const CItem &Ammo) const
// GetDamageType
//
// Returns the type of damage done by this device
{
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon)
return pWeapon->GetDamageType(Ctx, Ammo);
else
return damageGeneric;
}
ICCItem *CAutoDefenseClass::FindItemProperty (CItemCtx &Ctx, const CString &sProperty)
// FindItemProperty
//
// Returns the item property. Subclasses should call this if they do not
// understand the property.
{
CCodeChain &CC = g_pUniverse->GetCC();
CDeviceClass *pWeapon;
CInstalledDevice *pDevice = Ctx.GetDevice();
// Get the property
if (strEquals(sProperty, PROPERTY_FIRE_DELAY))
return CC.CreateInteger(m_iRechargeTicks);
else if (strEquals(sProperty, PROPERTY_FIRE_RATE))
{
Metric rDelay = m_iRechargeTicks;
if (rDelay <= 0.0)
return CC.CreateNil();
return CC.CreateInteger((int)(1000.0 / rDelay));
}
else if (strEquals(sProperty, PROPERTY_ENABLED))
return (pDevice ? CC.CreateBool(pDevice->IsEnabled()) : CC.CreateNil());
else if (strEquals(sProperty, PROPERTY_EXTERNAL))
return CC.CreateBool(pDevice ? pDevice->IsExternal() : IsExternal());
else if (strEquals(sProperty, PROPERTY_OMNIDIRECTIONAL))
return CC.CreateBool(IsOmniDirectional(pDevice));
else if (strEquals(sProperty, PROPERTY_FIRE_ARC))
{
int iMinFireArc;
int iMaxFireArc;
// Omnidirectional
if (IsOmniDirectional(pDevice))
return CC.CreateString(PROPERTY_OMNIDIRECTIONAL);
// Fire arc
else if (IsDirectional(pDevice, &iMinFireArc, &iMaxFireArc))
{
// Create a list
ICCItem *pResult = CC.CreateLinkedList();
if (pResult->IsError())
return pResult;
CCLinkedList *pList = (CCLinkedList *)pResult;
pList->AppendInteger(CC, iMinFireArc);
pList->AppendInteger(CC, iMaxFireArc);
return pResult;
}
// Otherwise, see if we are pointing in a particular direction
else
{
int iFacingAngle = AngleMod((pDevice ? pDevice->GetRotation() : 0) + AngleMiddle(m_iMinFireArc, m_iMaxFireArc));
if (iFacingAngle == 0)
return CC.CreateNil();
else
return CC.CreateInteger(iFacingAngle);
}
}
// Otherwise, just get the property from the weapon we're using
else if (pWeapon = GetWeapon())
{
CItem Weapon(pWeapon->GetItemType(), 1);
CItemCtx WeaponCtx(Weapon);
return pWeapon->FindItemProperty(WeaponCtx, sProperty);
}
else
return CDeviceClass::FindItemProperty(Ctx, sProperty);
}
int CAutoDefenseClass::GetPowerRating (CItemCtx &Ctx, int *retiIdlePowerUse) const
// GetPowerRating
//
// Returns the minimum reactor power needed for this device
{
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon)
return pWeapon->GetPowerRating(Ctx, retiIdlePowerUse);
else
{
if (retiIdlePowerUse)
*retiIdlePowerUse;
return 0;
}
}
bool CAutoDefenseClass::GetReferenceDamageType (CItemCtx &Ctx, const CItem &Ammo, DamageTypes *retiDamage, CString *retsReference) const
// GetReferenceDamageType
//
// Returns the damage type done by the weapon
{
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon)
return pWeapon->GetReferenceDamageType(Ctx, Ammo, retiDamage, retsReference);
else
return false;
}
void CAutoDefenseClass::OnAddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// OnAddTypesUsed
//
// Adds types used by this class
{
retTypesUsed->SetAt(m_pWeapon.GetUNID(), true);
}
CString CAutoDefenseClass::OnGetReference (CItemCtx &Ctx, const CItem &Ammo, DWORD dwFlags)
// OnGetReference
//
// Returns a string that describes the basic attributes
// of this weapon.
{
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon)
return pWeapon->OnGetReference(Ctx, Ammo);
else
return NULL_STR;
}
bool CAutoDefenseClass::IsDirectional(CInstalledDevice *pDevice, int *retiMinFireArc, int *retiMaxFireArc)
// IsDirectional
//
// Returns TRUE if the autodefensedevice can turn but is not omni
// Copied from CWeaponClass.cpp
{
// If the weapon is omnidirectional then we don't need directional
// calculations.
if (m_bOmnidirectional || (pDevice && pDevice->IsOmniDirectional()))
return false;
// If we have a device, combine the fire arcs of device slot and
// autodefensedevice
if (pDevice)
{
// If the device is directional then we always take the fire arc from
// the device slot.
if (pDevice->IsDirectional())
{
if (retiMinFireArc)
*retiMinFireArc = pDevice->GetMinFireArc();
if (retiMaxFireArc)
*retiMaxFireArc = pDevice->GetMaxFireArc();
return true;
}
// Otherwise, see if the autodefensedevice is directional.
else if (m_iMinFireArc != m_iMaxFireArc)
{
// If the device points in a specific direction then we offset the
// autodefensedevice's fire arc.
int iDeviceSlotOffset = pDevice->GetMinFireArc();
if (retiMinFireArc)
*retiMinFireArc = (m_iMinFireArc + iDeviceSlotOffset) % 360;
if (retiMaxFireArc)
*retiMaxFireArc = (m_iMaxFireArc + iDeviceSlotOffset) % 360;
return true;
}
// Otherwise, we are not directional
else
return false;
}
else
{
// Otherwise, just check the autodefensedevice
if (retiMinFireArc)
*retiMinFireArc = m_iMinFireArc;
if (retiMaxFireArc)
*retiMaxFireArc = m_iMaxFireArc;
return (m_iMinFireArc != m_iMaxFireArc);
}
}
bool CAutoDefenseClass::IsOmniDirectional(CInstalledDevice *pDevice)
// IsOmniDirectional
//
// Returns TRUE if the autodefensedevice is omnidirectional (not limited)
// Copied from CWeaponClass.cpp
{
// The device slot improves the autodefensedevice. If the device slot is
// directional, then the autodefensedevice is directional. If the device
// slot is omni directional, then the autodefensedevice is
// omnidirectional.
if (pDevice && pDevice->IsOmniDirectional())
return true;
return m_bOmnidirectional;
}
void CAutoDefenseClass::Update (CInstalledDevice *pDevice, CSpaceObject *pSource, SDeviceUpdateCtx &Ctx)
// Update
//
// Update device
{
DEBUG_TRY
CDeviceClass *pWeapon = GetWeapon();
if (pWeapon == NULL)
return;
// Update the weapon
pWeapon->Update(pDevice, pSource, Ctx);
// Skip if we're not ready or disabled
//
// NOTE: If pDevice is damaged or disrupted we handle this as a potential
// misfire inside of pWeapon->Activate.
if (!pDevice->IsReady() || !pDevice->IsEnabled())
return;
// If the ship is disarmed or paralyzed, then we do not fire.
if (pSource->GetCondition(CConditionSet::cndParalyzed)
|| pSource->GetCondition(CConditionSet::cndDisarmed))
return;
// Look for a target; if none, then skip.
CSpaceObject *pTarget = FindTarget(pDevice, pSource);
if (pTarget == NULL)
return;
// Shoot at target
int iFireAngle = pWeapon->CalcFireSolution(pDevice, pSource, pTarget);
if (iFireAngle != -1)
{
// Since we're using this as a target, set the destroy notify flag
// (Normally beams don't notify, so we need to override this).
pTarget->SetDestructionNotify();
// Fire
pDevice->SetFireAngle(iFireAngle);
pWeapon->Activate(pDevice, pSource, pTarget, &Ctx.bSourceDestroyed, &Ctx.bConsumedItems);
pDevice->SetTimeUntilReady(m_iRechargeTicks);
// Identify
if (pSource->IsPlayer())
GetItemType()->SetKnown();
}
DEBUG_CATCH
}
ALERROR CAutoDefenseClass::CreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, CItemType *pType, CDeviceClass **retpDevice)
// CreateFromXML
//
// Load the class
{
ALERROR error;
CAutoDefenseClass *pDevice;
pDevice = new CAutoDefenseClass;
if (pDevice == NULL)
return ERR_MEMORY;
if (error = pDevice->InitDeviceFromXML(Ctx, pDesc, pType))
return error;
int iFireRate = pDesc->GetAttributeInteger(FIRE_RATE_ATTRIB);
if (iFireRate == 0)
iFireRate = pDesc->GetAttributeInteger(RECHARGE_TIME_ATTRIB);
if (iFireRate == 0)
iFireRate = 15;
pDevice->m_iMinFireArc = AngleMod(pDesc->GetAttributeInteger(MIN_FIRE_ARC_ATTRIB));
pDevice->m_iMaxFireArc = AngleMod(pDesc->GetAttributeInteger(MAX_FIRE_ARC_ATTRIB));
// Set omnidirectional if either the "omnidirectional" attribute is True, OR
// if minfirearc = maxfirearc = 0.
pDevice->m_bOmnidirectional = (pDesc->GetAttributeBool(OMNIDIRECTIONAL_ATTRIB) ||
((pDevice->m_iMaxFireArc == pDevice->m_iMinFireArc) && pDevice->m_iMaxFireArc == 0));
pDevice->m_iRechargeTicks = (int)((iFireRate / STD_SECONDS_PER_UPDATE) + 0.5);
if (error = pDevice->m_pWeapon.LoadUNID(Ctx, pDesc->GetAttribute(WEAPON_ATTRIB)))
return error;
// Targeting. If we have a targetCriteria attribute then we use
// this to pick a target
CString sCriteria;
if (pDesc->FindAttribute(TARGET_CRITERIA_ATTRIB, &sCriteria))
{
pDevice->m_iTargeting = trgCriteria;
// We need to parse the criteria. But note that we have to parse
// it in an object-generic way, since one class could be used by
// multiple objects.
pDevice->m_TargetCriteria.Init(sCriteria);
}
// Otherwise, we use a hard-coded method
else
{
CString sTarget = pDesc->GetAttribute(TARGET_ATTRIB);
if (strEquals(sTarget, MISSILES_TARGET))
pDevice->m_iTargeting = trgMissiles;
else
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid target type: %s. Use target=\"missiles\" or targetCriteria=\"{criteria}\"."), sTarget);
return ERR_FAIL;
}
}
// Intercept range
int iInterceptRange = pDesc->GetAttributeIntegerBounded(INTERCEPT_RANGE_ATTRIB, 0, -1, DEFAULT_INTERCEPT_RANGE);
pDevice->m_rInterceptRange = (Metric)(iInterceptRange * LIGHT_SECOND);
// Done
*retpDevice = pDevice;
return NOERROR;
}
ALERROR CAutoDefenseClass::OnDesignLoadComplete (SDesignLoadCtx &Ctx)
// OnDesignLoadComplete
//
// All design elements loaded
{
ALERROR error;
if (error = m_pWeapon.Bind(Ctx))
return error;
return NOERROR;
}