-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDevices.cpp
858 lines (640 loc) · 21.3 KB
/
Devices.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
// Devices.cpp
//
// Ship devices
#include "PreComp.h"
#define ENHANCE_ABILITIES_TAG CONSTLIT("EnhancementAbilities")
#define EQUIPMENT_TAG CONSTLIT("Equipment")
#define CAN_BE_DAMAGED_ATTRIB CONSTLIT("canBeDamaged")
#define CAN_BE_DISABLED_ATTRIB CONSTLIT("canBeDisabled")
#define CAN_BE_DISRUPTED_ATTRIB CONSTLIT("canBeDisrupted")
#define CATEGORY_ATTRIB CONSTLIT("category")
#define CRITERIA_ATTRIB CONSTLIT("criteria")
#define DEVICE_DAMAGE_IMMUNE_ATTRIB CONSTLIT("deviceDamageImmune")
#define DEVICE_DISRUPT_IMMUNE_ATTRIB CONSTLIT("deviceDisruptImmune")
#define DEVICE_SLOT_CATEGORY_ATTRIB CONSTLIT("deviceSlotCategory")
#define DEVICE_SLOTS_ATTRIB CONSTLIT("deviceSlots")
#define ENHANCEMENT_ATTRIB CONSTLIT("enhancement")
#define EXTERNAL_ATTRIB CONSTLIT("external")
#define ITEM_ID_ATTRIB CONSTLIT("itemID")
#define MAX_HP_BONUS_ATTRIB CONSTLIT("maxHPBonus")
#define OMNIDIRECTIONAL_ATTRIB CONSTLIT("omnidirectional")
#define OVERLAY_TYPE_ATTRIB CONSTLIT("overlayType")
#define POS_ANGLE_ATTRIB CONSTLIT("posAngle")
#define POS_RADIUS_ATTRIB CONSTLIT("posRadius")
#define POS_Z_ATTRIB CONSTLIT("posZ")
#define SECONDARY_WEAPON_ATTRIB CONSTLIT("secondaryWeapon")
#define TYPE_ATTRIB CONSTLIT("type")
#define UNID_ATTRIB CONSTLIT("UNID")
#define GET_OVERLAY_TYPE_EVENT CONSTLIT("GetOverlayType")
#define LINKED_FIRE_ALWAYS CONSTLIT("always")
#define LINKED_FIRE_ENEMY CONSTLIT("whenInFireArc")
#define LINKED_FIRE_TARGET CONSTLIT("targetInRange")
#define PROPERTY_CAN_BE_DAMAGED CONSTLIT("canBeDamaged")
#define PROPERTY_CAN_BE_DISABLED CONSTLIT("canBeDisabled")
#define PROPERTY_CAN_BE_DISRUPTED CONSTLIT("canBeDisrupted")
#define PROPERTY_CAPACITOR CONSTLIT("capacitor")
#define PROPERTY_DEVICE_SLOTS CONSTLIT("deviceSlots")
#define PROPERTY_ENABLED CONSTLIT("enabled")
#define PROPERTY_EXTERNAL CONSTLIT("external")
#define PROPERTY_EXTRA_POWER_USE CONSTLIT("extraPowerUse")
#define PROPERTY_FIRE_ARC CONSTLIT("fireArc")
#define PROPERTY_HP CONSTLIT("hp")
#define PROPERTY_LINKED_FIRE_OPTIONS CONSTLIT("linkedFireOptions")
#define PROPERTY_OMNIDIRECTIONAL CONSTLIT("omnidirectional")
#define PROPERTY_POS CONSTLIT("pos")
#define PROPERTY_POWER CONSTLIT("power")
#define PROPERTY_SECONDARY CONSTLIT("secondary")
#define PROPERTY_SLOT_ID CONSTLIT("slotID")
#define PROPERTY_TEMPERATURE CONSTLIT("temperature")
const int MAX_COUNTER = 100;
struct SStdDeviceStats
{
int iInstallCost; // Cost to install (credits)
};
static SStdDeviceStats STD_DEVICE_STATS[MAX_ITEM_LEVEL] =
{
// Install
{ 60, },
{ 160, },
{ 360, },
{ 760, },
{ 1500, },
{ 3200, },
{ 6500, },
{ 13000, },
{ 26000, },
{ 53000, },
{ 100000, },
{ 210000, },
{ 450000, },
{ 880000, },
{ 1700000, },
{ 3500000, },
{ 7100000, },
{ 14000000, },
{ 28000000, },
{ 56000000, },
{ 110000000, },
{ 230000000, },
{ 450000000, },
{ 910000000, },
{ 1800000000, },
};
static char *CACHED_EVENTS[CDeviceClass::evtCount] =
{
"GetOverlayType",
};
inline const SStdDeviceStats *GetStdDeviceStats (int iLevel)
{
if (iLevel >= 1 && iLevel <= MAX_ITEM_LEVEL)
return &STD_DEVICE_STATS[iLevel - 1];
else
return NULL;
}
void CDeviceClass::AccumulateAttributes (CItemCtx &ItemCtx, const CItem &Ammo, TArray<SDisplayAttribute> *retList)
// AccumulateAttributes
//
// Add display attributes to the list.
{
// Add general device attributes. If we have a variant, then it means
// we're interested in the attributes for a missile/ammo of the device
// (not the device itself).
if (Ammo.IsEmpty())
{
CInstalledDevice *pDevice = ItemCtx.GetDevice();
// Linked-fire
DWORD dwOptions = GetLinkedFireOptions(ItemCtx);
if (dwOptions != 0)
retList->Insert(SDisplayAttribute(attribPositive, CONSTLIT("linked-fire")));
}
// Let our subclasses add their own attributes
OnAccumulateAttributes(ItemCtx, Ammo, retList);
}
bool CDeviceClass::AccumulateEnhancements (CItemCtx &Device, CInstalledArmor *pTarget, TArray<CString> &EnhancementIDs, CItemEnhancementStack *pEnhancements)
// AccumulateEnhancements
//
// If this device can enhance the given armor, then we add to the list of enhancements.
// We return TRUE if we enhanced the target.
{
bool bEnhanced = false;
// See if we can enhance the target device
if (Device.IsDeviceWorking())
bEnhanced = m_Enhancements.Accumulate(Device, *pTarget->GetItem(), EnhancementIDs, pEnhancements);
// Let sub-classes add their own
if (OnAccumulateEnhancements(Device, pTarget, EnhancementIDs, pEnhancements))
bEnhanced = true;
// Done
return bEnhanced;
}
bool CDeviceClass::AccumulateEnhancements (CItemCtx &Device, CInstalledDevice *pTarget, TArray<CString> &EnhancementIDs, CItemEnhancementStack *pEnhancements)
// AccumulateEnhancements
//
// If this device can enhance pTarget, then we add to the list of enhancements.
{
bool bEnhanced = false;
// See if we can enhance the target device
if (Device.IsDeviceWorking())
bEnhanced = m_Enhancements.Accumulate(Device, *pTarget->GetItem(), EnhancementIDs, pEnhancements);
// Let sub-classes add their own
if (OnAccumulateEnhancements(Device, pTarget, EnhancementIDs, pEnhancements))
bEnhanced = true;
// Done
return bEnhanced;
}
bool CDeviceClass::AccumulatePerformance (CItemCtx &ItemCtx, SShipPerformanceCtx &Ctx) const
// AccumulatePerformance
//
// If this device alters a ship's performance, then we modified the ship
// performance context. We return TRUE if we modified the context.
{
bool bModified = false;
// If we install equipment, then add it.
if (ItemCtx.IsDeviceWorking())
Ctx.Abilities.Set(m_Equipment);
// Let sub-classes handle it
if (OnAccumulatePerformance(ItemCtx, Ctx))
bModified = true;
// Done
return bModified;
}
void CDeviceClass::AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// AddTypesUsed
//
// Adds types used by this class.
{
retTypesUsed->SetAt(m_pOverlayType.GetUNID(), true);
OnAddTypesUsed(retTypesUsed);
}
ALERROR CDeviceClass::Bind (SDesignLoadCtx &Ctx)
// Bind
//
// Bind the design
{
DEBUG_TRY
ALERROR error;
if (error = m_pOverlayType.Bind(Ctx))
return error;
m_pItemType->InitCachedEvents(evtCount, CACHED_EVENTS, m_CachedEvents);
return OnDesignLoadComplete(Ctx);
DEBUG_CATCH
}
COverlayType *CDeviceClass::FireGetOverlayType (CItemCtx &ItemCtx) const
// FireGetOverlayType
//
// Fire GetOverlayType event
{
SEventHandlerDesc Event;
if (FindEventHandlerDeviceClass(evtGetOverlayType, &Event))
{
// Setup arguments
CCodeChainCtx Ctx;
Ctx.DefineContainingType(GetItemType());
Ctx.SaveAndDefineSourceVar(ItemCtx.GetSource());
Ctx.SaveAndDefineItemVar(ItemCtx);
ICCItem *pResult = Ctx.Run(Event);
DWORD dwUNID = 0;
if (pResult->IsError())
ItemCtx.GetSource()->ReportEventError(GET_OVERLAY_TYPE_EVENT, pResult);
else if (!pResult->IsNil())
dwUNID = pResult->GetIntegerValue();
Ctx.Discard(pResult);
// Done
return COverlayType::AsType(g_pUniverse->FindDesignType(dwUNID));
}
else
return GetOverlayType();
}
bool CDeviceClass::GetAmmoItemPropertyBool (CItemCtx &Ctx, const CItem &Ammo, const CString &sProperty)
// GetAmmoItemPropertyBool
//
// Wrapper around properties that discard ICCItem appropriately.
{
ICCItem *pValue = FindAmmoItemProperty(Ctx, Ammo, sProperty);
if (pValue == NULL)
return false;
bool bValue = !pValue->IsNil();
pValue->Discard(&g_pUniverse->GetCC());
return bValue;
}
Metric CDeviceClass::GetAmmoItemPropertyDouble (CItemCtx &Ctx, const CItem &Ammo, const CString &sProperty)
// GetAmmoItemPropertyDouble
//
// Wrapper around properties that discard ICCItem appropriately.
{
ICCItem *pValue = FindAmmoItemProperty(Ctx, Ammo, sProperty);
if (pValue == NULL)
return 0.0;
Metric rValue = pValue->GetDoubleValue();
pValue->Discard(&g_pUniverse->GetCC());
return rValue;
}
int CDeviceClass::GetInstallCost (CItemCtx &ItemCtx)
// GetInstallCost
//
// Returns the standard install cost (in the default currency of the item).
{
if (m_pItemType == NULL)
return -1;
const SStdDeviceStats *pStats = GetStdDeviceStats(m_pItemType->GetApparentLevel(ItemCtx));
if (pStats == NULL)
return -1;
return (int)m_pItemType->GetCurrencyType()->Exchange(CEconomyType::Default(), pStats->iInstallCost);
}
ALERROR CDeviceClass::InitDeviceFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, CItemType *pType)
// InitDeviceFromXML
//
// Initializes the device class base
{
ALERROR error;
m_pItemType = pType;
// Number of slots that the device takes up (if the attribute is missing
// then we assume 1)
CString sAttrib;
if (pDesc->FindAttribute(DEVICE_SLOTS_ATTRIB, &sAttrib))
m_iSlots = strToInt(sAttrib, 1, NULL);
else
m_iSlots = 1;
// Is this device immune to damage and disrupt?
// Note: We assume that damageable and disruptable do not mean the same thing.
// There could be a device that is immune to disrupt but vulnerable to other damaging effects.
// There could also be a device that is disruptable but not damageable.
m_fDeviceDamageImmune = pDesc->GetAttributeBool(DEVICE_DAMAGE_IMMUNE_ATTRIB);
m_fDeviceDisruptImmune = pDesc->GetAttributeBool(DEVICE_DISRUPT_IMMUNE_ATTRIB);
// Sometimes we override the basic properties of a device category.
m_fCanBeDamagedOverride = pDesc->GetAttributeBool(CAN_BE_DAMAGED_ATTRIB);
m_fCanBeDisabledOverride = pDesc->GetAttributeBool(CAN_BE_DISABLED_ATTRIB);
m_fCanBeDisruptedOverride = pDesc->GetAttributeBool(CAN_BE_DISRUPTED_ATTRIB);
// Slot type
CString sSlotType;
if (pDesc->FindAttribute(CATEGORY_ATTRIB, &sSlotType)
|| pDesc->FindAttribute(DEVICE_SLOT_CATEGORY_ATTRIB, &sSlotType))
{
if (!CItemType::ParseItemCategory(sSlotType, &m_iSlotCategory))
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid deviceSlotCategory: %s."), sSlotType);
return ERR_FAIL;
}
// Make sure it is a valid device
switch (m_iSlotCategory)
{
// OK
case itemcatCargoHold:
case itemcatDrive:
case itemcatLauncher:
case itemcatMiscDevice:
case itemcatReactor:
case itemcatShields:
case itemcatWeapon:
break;
default:
{
Ctx.sError = strPatternSubst(CONSTLIT("Not a valid device category: %s."), sSlotType);
return ERR_FAIL;
}
}
}
else
// itemcatNone means use the actual item category
m_iSlotCategory = itemcatNone;
// Overlay
if (error = m_pOverlayType.LoadUNID(Ctx, pDesc->GetAttribute(OVERLAY_TYPE_ATTRIB)))
return error;
// Other settings
m_iMaxHPBonus = pDesc->GetAttributeIntegerBounded(MAX_HP_BONUS_ATTRIB, 0, -1, 150);
m_fExternal = pDesc->GetAttributeBool(EXTERNAL_ATTRIB);
// Does this device enhance other items?
CXMLElement *pEnhanceList = pDesc->GetContentElementByTag(ENHANCE_ABILITIES_TAG);
if (pEnhanceList)
{
if (error = m_Enhancements.InitFromXML(Ctx, pEnhanceList))
return error;
}
// Does this device provide some equipment when installed?
CXMLElement *pEquipmentList = pDesc->GetContentElementByTag(EQUIPMENT_TAG);
if (pEquipmentList)
{
if (error = m_Equipment.InitFromXML(Ctx, pEquipmentList))
return error;
}
return NOERROR;
}
bool CDeviceClass::FindAmmoDataField (CItemType *pItem, const CString &sField, CString *retsValue)
// FindAmmoDataField
//
// Finds the device that fires this item and returns the given field
{
CDeviceClass *pWeapon = (pItem->GetLaunchWeapons().GetCount() ? pItem->GetLaunchWeapons()[0] : NULL);
if (pWeapon == NULL)
return false;
return pWeapon->FindAmmoDataField(CItem(pItem, 1), sField, retsValue);
}
ICCItem *CDeviceClass::FindItemProperty (CItemCtx &Ctx, const CString &sName)
// FindItemProperty
//
// Returns the item property. Subclasses should call this if they do not
// understand the property.
//
// We return NULL if the property is not found. Otherwise, the caller is
// responsible for freeing.
//
// NOTE: We only return device-specific properties. We DO NOT return generic
// item properties.
{
CCodeChain &CC = g_pUniverse->GetCC();
CString sFieldValue;
// Get the device
CInstalledDevice *pDevice = Ctx.GetDevice();
// Get the property
if (strEquals(sName, PROPERTY_CAN_BE_DAMAGED))
return (pDevice ? CC.CreateBool(pDevice->CanBeDamaged()) : CC.CreateBool(CanBeDamaged()));
else if (strEquals(sName, PROPERTY_CAN_BE_DISABLED))
return (pDevice ? CC.CreateBool(pDevice->CanBeDisabled(Ctx)) : CC.CreateBool(CanBeDisabled(Ctx)));
else if (strEquals(sName, PROPERTY_CAN_BE_DISRUPTED))
return (pDevice ? CC.CreateBool(pDevice->CanBeDisrupted()) : CC.CreateBool(CanBeDisrupted()));
else if (strEquals(sName, PROPERTY_CAPACITOR))
{
CSpaceObject *pSource = Ctx.GetSource();
CounterTypes iType;
int iLevel;
GetCounter(pDevice, pSource, &iType, &iLevel);
if (iType != cntCapacitor || pDevice == NULL || pSource == NULL)
return CC.CreateNil();
return CC.CreateInteger(iLevel);
}
else if (strEquals(sName, PROPERTY_DEVICE_SLOTS))
return CC.CreateInteger(GetSlotsRequired());
else if (strEquals(sName, PROPERTY_ENABLED))
return (pDevice ? CC.CreateBool(pDevice->IsEnabled()) : CC.CreateNil());
else if (strEquals(sName, PROPERTY_EXTERNAL))
return CC.CreateBool(pDevice ? pDevice->IsExternal() : IsExternal());
else if (strEquals(sName, PROPERTY_EXTRA_POWER_USE))
{
if (pDevice == NULL)
return CC.CreateNil();
return CC.CreateInteger(pDevice->GetExtraPowerUse());
}
else if (strEquals(sName, PROPERTY_POS))
{
if (pDevice == NULL)
return CC.CreateNil();
// Create a list
ICCItem *pResult = CC.CreateLinkedList();
if (pResult->IsError())
return pResult;
CCLinkedList *pList = (CCLinkedList *)pResult;
// List contains angle, radius, and optional z
pList->AppendInteger(CC, pDevice->GetPosAngle());
pList->AppendInteger(CC, pDevice->GetPosRadius());
if (pDevice->GetPosZ() != 0)
pList->AppendInteger(CC, pDevice->GetPosZ());
// Done
return pResult;
}
else if (strEquals(sName, PROPERTY_POWER))
{
if (GetCategory() == itemcatReactor)
return CreatePowerResult(CC, GetPowerOutput(Ctx) * 100.0);
else
return CreatePowerResult(CC, GetPowerRating(Ctx) * 100.0);
}
else if (strEquals(sName, PROPERTY_SECONDARY))
return (pDevice ? CC.CreateBool(pDevice->IsSecondaryWeapon()) : CC.CreateNil());
else if (strEquals(sName, PROPERTY_SLOT_ID))
return (pDevice ? CC.CreateString(pDevice->GetID()) : CC.CreateNil());
else if (strEquals(sName, PROPERTY_TEMPERATURE))
{
CSpaceObject *pSource = Ctx.GetSource();
CounterTypes iType;
int iLevel;
GetCounter(pDevice, pSource, &iType, &iLevel);
if (iType != cntTemperature || pDevice == NULL || pSource == NULL)
return CC.CreateNil();
return CC.CreateInteger(iLevel);
}
else
return NULL;
}
bool CDeviceClass::FindWeaponFor (CItemType *pItem, CDeviceClass **retpWeapon, int *retiVariant, CWeaponFireDesc **retpDesc)
// FindWeaponFor
//
// Returns weapon data for the given item (which may be a weapon or a missile).
{
CDeviceClass *pDevice;
int iVariant;
// Get the device and variant
CItem Ammo;
if (pItem->IsMissile())
{
pDevice = (pItem->GetLaunchWeapons().GetCount() ? pItem->GetLaunchWeapons()[0] : NULL);
iVariant = (pDevice ? pDevice->GetAmmoVariant(pItem) : -1);
if (iVariant == -1)
return false;
Ammo = CItem(pItem, 1);
}
else
{
pDevice = pItem->GetDeviceClass();
if (pDevice == NULL)
return false;
iVariant = 0;
}
CWeaponClass *pWeapon = pDevice->AsWeaponClass();
if (pWeapon == NULL)
return false;
// Done
if (retpWeapon)
*retpWeapon = pDevice;
if (retiVariant)
*retiVariant = iVariant;
if (retpDesc)
*retpDesc = pWeapon->GetWeaponFireDesc(CItemCtx(), Ammo);
return true;
}
int CDeviceClass::GetFireArc (CItemCtx &Ctx) const
// GetFireArc
//
// Returns the fire arc for swivel weapons and turrets.
{
int iMinArc, iMaxArc;
switch (GetRotationType(Ctx, &iMinArc, &iMaxArc))
{
case rotOmnidirectional:
return 360;
case rotSwivel:
return AngleRange(iMinArc, iMaxArc);
default:
return 0;
}
}
ItemCategories CDeviceClass::GetItemCategory (DeviceNames iDev)
// GetItemCategory
//
// Gets the item category that corresponds to this device name.
{
switch (iDev)
{
case devPrimaryWeapon:
return itemcatWeapon;
case devMissileWeapon:
return itemcatLauncher;
case devShields:
return itemcatShields;
case devDrive:
return itemcatDrive;
case devCargo:
return itemcatCargoHold;
case devReactor:
return itemcatReactor;
default:
ASSERT(false);
return itemcatNone;
}
}
CString CDeviceClass::GetLinkedFireOptionString (DWORD dwOptions)
// GetLinkedFireOptionString
//
// Returns the string representing the given (single) option
{
switch ((LinkedFireOptions)dwOptions)
{
case lkfAlways:
return LINKED_FIRE_ALWAYS;
case lkfTargetInRange:
return LINKED_FIRE_TARGET;
case lkfEnemyInRange:
return LINKED_FIRE_ENEMY;
default:
ASSERT(false);
return NULL_STR;
}
}
CString CDeviceClass::GetReference (CItemCtx &Ctx, const CItem &Ammo, DWORD dwFlags)
// GetReference
//
// Returns reference string
{
CString sReference;
// For a device we always add power and other properties.
// (If iVariant != -1 then it means that we're looking for reference on a
// missile or someting).
if (Ammo.IsEmpty())
{
CInstalledDevice *pDevice = Ctx.GetDevice();
// Start with power requirements
AppendReferenceString(&sReference, GetReferencePower(Ctx));
// Non-standard slots
if (GetSlotsRequired() != 1)
AppendReferenceString(&sReference, strPatternSubst(CONSTLIT("%d slots"), GetSlotsRequired()));
// External devices
if (IsExternal() || (pDevice && pDevice->IsExternal()))
AppendReferenceString(&sReference, CONSTLIT("external"));
}
// Combine with our subclass
AppendReferenceString(&sReference, OnGetReference(Ctx, Ammo, dwFlags));
return sReference;
}
CString CDeviceClass::GetReferencePower (CItemCtx &Ctx)
// GetReferencePower
//
// Returns a string for the reference relating to the power required for
// this device.
{
int iPower = GetPowerRating(Ctx);
// Compute the power units
CString sUnit;
if (iPower == 0)
return NULL_STR;
else if (iPower >= 10000)
{
sUnit = CONSTLIT("GW");
iPower = iPower / 1000;
}
else
sUnit = CONSTLIT("MW");
// Decimal
int iMW = iPower / 10;
int iMWDecimal = iPower % 10;
if (iMW >= 100 || iMWDecimal == 0)
return strPatternSubst(CONSTLIT("%d %s"), iMW, sUnit);
else
return strPatternSubst(CONSTLIT("%d.%d %s"), iMW, iMWDecimal, sUnit);
}
bool CDeviceClass::OnDestroyCheck (CItemCtx &ItemCtx, DestructionTypes iCause, const CDamageSource &Attacker)
// OnDestroyCheck
//
// Returns FALSE if the device can prevent the source from getting destroyed.
{
CSpaceObject *pSource = ItemCtx.GetSource();
CInstalledDevice *pDevice = ItemCtx.GetDevice();
if (pDevice == NULL || !pDevice->IsEnabled())
return true;
// NOTE: We give damaged and disrupted devices a chance to handle this
// event in case they want to do something special.
return ItemCtx.GetItem().FireOnDestroyCheck(ItemCtx, iCause, Attacker);
}
Metric CDeviceClass::OnGetScaledCostAdj (CItemCtx &Ctx) const
// OnGetScaledCostAdj
//
// Default implementation. Descendants may override.
{
if (Ctx.IsItemNull())
return 1.0;
int iLevel = Ctx.GetItem().GetLevel();
if (iLevel <= 0)
return 1.0;
// We use weapon price increases as a guide.
return CWeaponClass::GetStdStats(iLevel).rCost / CWeaponClass::GetStdStats(m_pItemType->GetLevel()).rCost;
}
ALERROR CDeviceClass::ParseLinkedFireOptions (SDesignLoadCtx &Ctx, const CString &sDesc, DWORD *retdwOptions)
// ParseLinkedFireOptions
//
// Parses a linked-fire options string.
{
DWORD dwOptions = 0;
if (sDesc.IsBlank())
;
else if (strEquals(sDesc, LINKED_FIRE_ALWAYS))
dwOptions |= CDeviceClass::lkfAlways;
else if (strEquals(sDesc, LINKED_FIRE_TARGET))
dwOptions |= CDeviceClass::lkfTargetInRange;
else if (strEquals(sDesc, LINKED_FIRE_ENEMY))
dwOptions |= CDeviceClass::lkfEnemyInRange;
else
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid linkedFire option: %s"), sDesc);
return ERR_FAIL;
}
*retdwOptions = dwOptions;
return NOERROR;
}
int CDeviceClass::ParseVariantFromPropertyName (const CString &sName, CString *retsName)
// ParseVariantFromPropertyName
//
// If the name ends in :nn then nn is the variant. We also return the parsed
// property name (without the variant).
{
// Look for a :nn suffix specifying a variant
char *pStart = sName.GetASCIIZPointer();
char *pPos = pStart;
while (*pPos != '\0')
{
if (*pPos == ':')
{
if (retsName)
*retsName = CString(pStart, (int)(pPos - pStart));
return strParseInt(pPos + 1, 0);
}
pPos++;
}
// Not found
return -1;
}
bool CDeviceClass::SetItemProperty (CItemCtx &Ctx, const CString &sName, ICCItem *pValue, CString *retsError)
// SetItemProperty
//
// Sets a device property. Subclasses should call this if they do not
// understand the property.
{
*retsError = strPatternSubst(CONSTLIT("Unknown item property: %s."), sName);
return false;
}