-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCInstalledDevice.cpp
1315 lines (1013 loc) · 30.9 KB
/
CInstalledDevice.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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CInstalledDevice.cpp
//
// CInstalledDevice class
// Copyright (c) 2013 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define DEVICE_ID_ATTRIB CONSTLIT("deviceID")
#define ITEM_ATTRIB CONSTLIT("item")
#define PROPERTY_CAPACITOR CONSTLIT("capacitor")
#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_SECONDARY CONSTLIT("secondary")
#define PROPERTY_TEMPERATURE CONSTLIT("temperature")
// CInstalledDevice class
CInstalledDevice::CInstalledDevice (void) :
m_pItem(NULL),
m_pOverlay(NULL),
m_dwTargetID(0),
m_iDeviceSlot(-1),
m_iPosAngle(0),
m_iPosRadius(0),
m_iPosZ(0),
m_iMinFireArc(0),
m_iMaxFireArc(0),
m_iTimeUntilReady(0),
m_iFireAngle(0),
m_iTemperature(0),
m_iActivateDelay(0),
m_iExtraPowerUse(0),
m_iSlotPosIndex(-1),
m_fOmniDirectional(false),
m_fSecondaryWeapon(false),
m_fTriggered(false),
m_fLastActivateSuccessful(false),
m_f3DPosition(false),
m_fLinkedFireAlways(false),
m_fLinkedFireTarget(false),
m_fLinkedFireEnemy(false),
m_fDuplicate(false),
m_fCannotBeEmpty(false),
m_fFateDamaged(false),
m_fFateDestroyed(false),
m_fFateSurvives(false),
m_fFateComponetized(false)
{
}
bool CInstalledDevice::AccumulateSlotEnhancements (CSpaceObject *pSource, TArray<CString> &EnhancementIDs, CItemEnhancementStack *pEnhancements) const
// AccumulateSlotEnhancements
//
// Accumulates enhancements confered by the slot itself.
{
bool bEnhanced = false;
// Slot enhancements
if (!m_SlotEnhancements.IsEmpty())
bEnhanced = m_SlotEnhancements.Accumulate(CItemCtx(pSource, const_cast<CInstalledDevice *>(this)), *GetItem(), EnhancementIDs, pEnhancements);
return bEnhanced;
}
int CInstalledDevice::CalcPowerUsed (SUpdateCtx &Ctx, CSpaceObject *pSource)
// CalcPowerUsed
//
// Calculates how much power this device used this tick. Positive numbers are
// consumption; negative numbers are power generation.
{
if (IsEmpty())
return 0;
// Let the device compute power used.
int iPower = m_pClass->CalcPowerUsed(Ctx, this, pSource);
// Add extra power used
iPower += m_iExtraPowerUse;
// Done
return iPower;
}
void CInstalledDevice::FinishInstall (CSpaceObject *pSource)
// FinishInstall
//
// Finishes the parts of the install that fire events
// (At object creation time these events fire after the entire object
// is created).
{
DEBUG_TRY
m_pItem->FireOnInstall(pSource);
m_pItem->FireOnEnabled(pSource);
CShip *pShip = pSource->AsShip();
if (pShip)
pShip->GetController()->OnItemInstalled(*m_pItem);
// If necessary create an overlay for this device
COverlayType *pOverlayType;
pOverlayType = m_pClass->FireGetOverlayType(CItemCtx(pSource, this));
// Add it
if (pOverlayType)
{
DWORD dwID;
pSource->AddOverlay(pOverlayType, GetPosAngle(), GetPosRadius(), 0, 0, -1, &dwID);
COverlay *pOverlay = pSource->GetOverlay(dwID);
if (pOverlay)
{
pOverlay->SetDevice(GetDeviceSlot());
SetOverlay(pOverlay);
}
}
DEBUG_CATCH
}
int CInstalledDevice::GetActivateDelay (CSpaceObject *pSource) const
// GetActivateDelay
//
// Returns the number of ticks to wait for activation
{
return m_iActivateDelay;
}
CString CInstalledDevice::GetEnhancedDesc (CSpaceObject *pSource, const CItem *pItem)
// GetEnhancedDesc
//
// Returns description of the enhancement
{
CItemCtx ItemCtx(pSource, this);
TArray<SDisplayAttribute> Attribs;
if (!ItemCtx.GetEnhancementDisplayAttributes(&Attribs))
return NULL_STR;
CString sResult = Attribs[0].sText;
for (int i = 1; i < Attribs.GetCount(); i++)
{
sResult.Append(CONSTLIT(" "));
sResult.Append(Attribs[i].sText);
}
return sResult;
}
ItemFates CInstalledDevice::GetFate (void) const
// GetFate
//
// Returns the current fate option for the device slot.
{
if (m_fFateDamaged)
return fateDamaged;
else if (m_fFateDestroyed)
return fateDestroyed;
else if (m_fFateSurvives)
return fateSurvives;
else if (m_fFateComponetized)
return fateComponetized;
else
return fateNone;
}
int CInstalledDevice::GetHitPointsPercent (CSpaceObject *pSource)
// GetHitPointsPercent
//
// Returns the integrity of the device (usually a shield) as a percent of max hp.
{
int iHP;
int iMaxHP;
GetStatus(pSource, &iHP, &iMaxHP);
if (iMaxHP <= 0)
return -1;
else if (iMaxHP <= iHP)
return 100;
return ((1000 * iHP / iMaxHP) + 5) / 10;
}
CSpaceObject *CInstalledDevice::GetLastShot (CSpaceObject *pSource, int iIndex) const
// GetLastShot
//
// Returns the last projectile object for this index.
{
if (pSource
&& iIndex < m_LastShotIDs.GetCount()
&& m_LastShotIDs[iIndex] != 0)
return pSource->GetSystem()->FindObject(m_LastShotIDs[iIndex]);
else
return NULL;
}
DWORD CInstalledDevice::GetLinkedFireOptions (void) const
// GetLinkedFireOptions
//
// Returns linked-fire options for the device slot.
{
if (m_fLinkedFireAlways)
return CDeviceClass::lkfAlways;
else if (m_fLinkedFireTarget)
return CDeviceClass::lkfTargetInRange;
else if (m_fLinkedFireEnemy)
return CDeviceClass::lkfEnemyInRange;
else if (m_fSecondaryWeapon)
return CDeviceClass::lkfEnemyInRange;
else
return 0;
}
CVector CInstalledDevice::GetPos (CSpaceObject *pSource)
// GetPos
//
// Returns the position of the device
{
if (m_f3DPosition)
{
int iScale = pSource->GetImageScale();
CVector vOffset;
C3DConversion::CalcCoord(iScale, pSource->GetRotation() + m_iPosAngle, m_iPosRadius, m_iPosZ, &vOffset);
return pSource->GetPos() + vOffset;
}
else if (m_iPosRadius)
{
return pSource->GetPos()
+ PolarToVector((m_iPosAngle + pSource->GetRotation()) % 360,
m_iPosRadius * g_KlicksPerPixel);
}
else
return pSource->GetPos();
}
CVector CInstalledDevice::GetPosOffset (CSpaceObject *pSource)
// GetPosOffset
//
// Returns the position of the device
{
if (m_f3DPosition)
{
int iScale = pSource->GetImageScale();
CVector vOffset;
C3DConversion::CalcCoord(iScale, 90 + m_iPosAngle, m_iPosRadius, m_iPosZ, &vOffset);
return vOffset;
}
else if (m_iPosRadius)
{
return PolarToVector((m_iPosAngle + 90) % 360,
m_iPosRadius * g_KlicksPerPixel);
}
else
return CVector();
}
void CInstalledDevice::InitFromDesc (const SDeviceDesc &Desc)
// InitFromDesc
//
// Initializes from a desc
{
m_sID = Desc.sID;
m_fOmniDirectional = Desc.bOmnidirectional;
m_iMinFireArc = Desc.iMinFireArc;
m_iMaxFireArc = Desc.iMaxFireArc;
m_iPosAngle = Desc.iPosAngle;
m_iPosRadius = Desc.iPosRadius;
m_f3DPosition = Desc.b3DPosition;
m_iPosZ = Desc.iPosZ;
m_fExternal = Desc.bExternal;
m_fCannotBeEmpty = Desc.bCannotBeEmpty;
SetLinkedFireOptions(Desc.dwLinkedFireOptions);
SetFate(Desc.iFate);
m_fSecondaryWeapon = Desc.bSecondary;
m_SlotEnhancements = Desc.Enhancements;
if (Desc.iSlotBonus != 0)
m_SlotEnhancements.InsertHPBonus(Desc.iSlotBonus);
m_iSlotPosIndex = -1;
}
ALERROR CInstalledDevice::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// InitFromXML
//
// Initializes each device slot from an XML structure
{
ALERROR error;
CString sUNID = pDesc->GetAttribute(DEVICE_ID_ATTRIB);
if (sUNID.IsBlank())
sUNID = pDesc->GetAttribute(ITEM_ATTRIB);
if (error = m_pClass.LoadUNID(Ctx, sUNID))
return error;
SDeviceDesc DeviceDesc;
if (error = IDeviceGenerator::InitDeviceDescFromXML(Ctx, pDesc, &DeviceDesc))
return error;
InitFromDesc(DeviceDesc);
return NOERROR;
}
void CInstalledDevice::Install (CSpaceObject *pObj, CItemListManipulator &ItemList, int iDeviceSlot, bool bInCreate)
// Install
//
// Installs a new device of the given class
{
DEBUG_TRY
const CItem &Item = ItemList.GetItemAtCursor();
m_pClass.Set(Item.GetType()->GetDeviceClass());
m_iDeviceSlot = iDeviceSlot;
m_iSlotPosIndex = -1;
m_pOverlay = NULL;
m_dwData = 0;
m_iExtraPowerUse = 0;
m_iTemperature = 0;
m_fWaiting = false;
m_fEnabled = true;
m_fTriggered = false;
m_fRegenerating = false;
m_fLastActivateSuccessful = false;
m_fDuplicate = false;
// Call the class
m_pClass->OnInstall(this, pObj, ItemList);
// Mark the item as installed
ItemList.SetInstalledAtCursor(iDeviceSlot);
// We remember the item after it is installed;
// otherwise, we get a pointer to the wrong item
m_pItem = ItemList.GetItemPointerAtCursor();
ASSERT(m_pItem);
// Select the variant. We need to do this AFTER m_pItem is set because
// we need to check things like charges.
m_pClass->SelectFirstVariant(pObj, this);
// Default to basic fire delay. Callers must set the appropriate delay
// based on enhancements later.
m_iActivateDelay = m_pClass->GetActivateDelay(CItemCtx(pObj, this));
// If we're installing a device after creation then we
// zero-out the device position, etc. If necessary the
// caller or the device can set these fields later.
//
// Note: This will overwrite whatever values were set
// at creation time.
if (!bInCreate)
{
// Desc is initialized to defaults even if FindDeviceSlotDesc fails.
SDeviceDesc Desc;
pObj->FindDeviceSlotDesc(Item, &Desc);
// Set the device slot properties
m_sID = Desc.sID;
m_iPosAngle = Desc.iPosAngle;
m_iPosRadius = Desc.iPosRadius;
m_iPosZ = Desc.iPosZ;
m_f3DPosition = Desc.b3DPosition;
m_fCannotBeEmpty = Desc.bCannotBeEmpty;
SetFate(Desc.iFate);
m_fExternal = (Desc.bExternal || m_pClass->IsExternal());
m_fOmniDirectional = Desc.bOmnidirectional;
m_iMinFireArc = Desc.iMinFireArc;
m_iMaxFireArc = Desc.iMaxFireArc;
SetLinkedFireOptions(Desc.dwLinkedFireOptions);
m_fSecondaryWeapon = Desc.bSecondary;
m_SlotEnhancements = Desc.Enhancements;
if (Desc.iSlotBonus != 0)
m_SlotEnhancements.InsertHPBonus(Desc.iSlotBonus);
}
// Event (when creating a ship we wait until the
// whole ship is created before firing the event)
if (!bInCreate)
FinishInstall(pObj);
DEBUG_CATCH
}
bool CInstalledDevice::IsLinkedFire (CItemCtx &Ctx, ItemCategories iTriggerCat) const
// IsLinkedFire
//
// Returns TRUE if we're linked to weapon trigger
{
DWORD dwOptions = GetClass()->GetLinkedFireOptions(Ctx);
if (dwOptions == 0)
return false;
else if (iTriggerCat == itemcatNone)
return true;
else
return (GetClass()->GetCategory() == iTriggerCat);
}
bool CInstalledDevice::IsSelectable (CItemCtx &Ctx) const
// IsSelectable
//
// Returns TRUE if device can be selected as a primary weapon or launcher.
{
return (!IsSecondaryWeapon()
&& GetClass()->GetLinkedFireOptions(Ctx) == 0);
}
ALERROR CInstalledDevice::OnDesignLoadComplete (SDesignLoadCtx &Ctx)
// OnDesignLoadComplete
//
// Done loading all design elements
{
ALERROR error;
if (error = m_pClass.Bind(Ctx))
return error;
return NOERROR;
}
void CInstalledDevice::PaintDevicePos (const SDeviceDesc &Device, CG32bitImage &Dest, int x, int y, int iScale, int iRotation)
// PaintDevicePos
//
// Paints the position of the device.
{
const int ARC_RADIUS = 30;
CG32bitPixel RGB_LINE = CG32bitPixel(255, 255, 0);
CG32bitPixel RGB_ARC = CG32bitPixel(RGB_LINE, 128);
CDeviceClass *pDeviceClass = Device.Item.GetType()->GetDeviceClass();
if (pDeviceClass == NULL)
return;
// If this is a weapon, then we can take some settings from the weapon.
bool bWeaponIsOmnidirectional = false;
int iWeaponMinFireArc = -1;
int iWeaponMaxFireArc = -1;
CWeaponClass *pWeapon = pDeviceClass->AsWeaponClass();
if (pWeapon)
{
switch (pWeapon->GetRotationType(CItemCtx(), &iWeaponMinFireArc, &iWeaponMaxFireArc))
{
case CDeviceClass::rotOmnidirectional:
bWeaponIsOmnidirectional = true;
break;
case CDeviceClass::rotSwivel:
break;
default:
iWeaponMinFireArc = -1;
iWeaponMaxFireArc = -1;
break;
}
}
// Compute the position of the device, accounting for rotation.
int xPos;
int yPos;
if (Device.b3DPosition)
C3DConversion::CalcCoord(iScale, iRotation + Device.iPosAngle, Device.iPosRadius, Device.iPosZ, &xPos, &yPos);
else
C3DConversion::CalcCoordCompatible(iRotation + Device.iPosAngle, Device.iPosRadius, &xPos, &yPos);
// Offset from where we want the center of the source to paint.
xPos += x;
yPos += y;
// Draw the center
Dest.DrawDot(xPos, yPos, RGB_LINE, markerMediumCross);
// Draw fire arc
if (!Device.bOmnidirectional && !bWeaponIsOmnidirectional)
{
int iMinFireArc;
int iMaxFireArc;
if (iWeaponMinFireArc != -1)
{
iMinFireArc = AngleMod(iRotation + iWeaponMinFireArc);
iMaxFireArc = AngleMod(iRotation + iWeaponMaxFireArc);
}
else
{
iMinFireArc = AngleMod(iRotation + Device.iMinFireArc);
iMaxFireArc = AngleMod(iRotation + Device.iMaxFireArc);
}
if (iMinFireArc != iMaxFireArc)
CGDraw::Arc(Dest, CVector(xPos, yPos), ARC_RADIUS, mathDegreesToRadians(iMinFireArc), mathDegreesToRadians(iMaxFireArc), ARC_RADIUS / 2, RGB_ARC);
int iFireAngle = AngleMiddle(iMinFireArc, iMaxFireArc);
CVector vDir = PolarToVector(iFireAngle, ARC_RADIUS);
CGDraw::Line(Dest, xPos, yPos, xPos + (int)vDir.GetX(), yPos - (int)vDir.GetY(), 1, RGB_LINE);
}
}
void CInstalledDevice::ReadFromStream (CSpaceObject *pSource, SLoadCtx &Ctx)
// ReadFromStream
//
// Read object from stream
//
// CString m_sID
// DWORD device: class UNID (0xffffffff if not installed)
// DWORD device: m_dwTargetID
// DWORD device: m_dwData
//
// DWORD device: DWORD m_LastShotIDs count
// DWORD device: DWORD ID
//
// DWORD device: low = m_iPosAngle; hi = m_iPosRadius
// DWORD device: low = m_iMinFireArc; hi = m_iMaxFireArc
// DWORD device: low = m_iTimeUntilReady; hi = m_iFireAngle
// DWORD device: low = m_iSlotPosIndex; hi = m_iTemperature
// DWORD device: low = m_iSlotBonus; hi = m_iDeviceSlot
// DWORD device: low = m_iActivateDelay; hi = m_iPosZ
// DWORD device: flags
//
// CItemEnhancementStack
{
int i;
DWORD dwLoad;
// ID
if (Ctx.dwVersion >= 157)
m_sID.ReadFromStream(Ctx.pStream);
// Class
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
if (dwLoad == 0xffffffff)
return;
m_pClass.Set(g_pUniverse->FindDeviceClass(dwLoad));
// Other data
if (Ctx.dwVersion >= 66)
Ctx.pStream->Read((char *)&m_dwTargetID, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_dwData, sizeof(DWORD));
// Last shots
if (Ctx.dwVersion >= 139)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_LastShotIDs.InsertEmpty(dwLoad);
for (i = 0; i < (int)dwLoad; i++)
Ctx.pStream->Read((char *)&m_LastShotIDs[i], sizeof(DWORD));
}
// In 1.08 we changed how we store alternating and repeating counters.
if (Ctx.dwVersion < 75 && m_pClass && m_pClass->AsWeaponClass())
m_dwData = (m_dwData & 0xFFFF0000);
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iPosAngle = (int)LOWORD(dwLoad);
m_iPosRadius = (int)HIWORD(dwLoad);
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iMinFireArc = (int)LOWORD(dwLoad);
m_iMaxFireArc = (int)HIWORD(dwLoad);
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iTimeUntilReady = (int)LOWORD(dwLoad);
m_iFireAngle = (int)HIWORD(dwLoad);
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iTemperature = (int)HIWORD(dwLoad);
if (Ctx.dwVersion < 92)
{
int iBonus = (int)LOWORD(dwLoad);
if (iBonus != 0)
{
m_pEnhancements.TakeHandoff(new CItemEnhancementStack);
m_pEnhancements->InsertHPBonus(iBonus);
}
m_iSlotPosIndex = -1;
}
else if (Ctx.dwVersion < 106)
m_iSlotPosIndex = -1;
else
m_iSlotPosIndex = (int)LOWORD(dwLoad);
// In version 159, we replace slot bonus with extra power variable
int iSlotBonus = 0;
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
if (Ctx.dwVersion >= 159)
m_iExtraPowerUse = (int)LOWORD(dwLoad);
else
{
m_iExtraPowerUse = 0;
iSlotBonus = (int)LOWORD(dwLoad);
}
if (Ctx.dwVersion >= 29)
m_iDeviceSlot = (int)HIWORD(dwLoad);
else
m_iDeviceSlot = -1;
if (Ctx.dwVersion >= 44)
{
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_iActivateDelay = (int)LOWORD(dwLoad);
m_iPosZ = (int)HIWORD(dwLoad);
}
else
{
m_iActivateDelay = 8;
m_iPosZ = 0;
}
// In newer versions we store an activation delay instead of an adjustment
if (Ctx.dwVersion < 93)
m_iActivateDelay = m_iActivateDelay * m_pClass->GetActivateDelay(CItemCtx(pSource, this)) / 100;
// We no longer store mods in the device structure
if (Ctx.dwVersion < 58)
{
CItemEnhancement Dummy;
Dummy.ReadFromStream(Ctx);
}
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
m_fOmniDirectional = ((dwLoad & 0x00000001) ? true : false);
m_f3DPosition = (((dwLoad & 0x00000002) ? true : false) && (Ctx.dwVersion >= 73));
m_fFateSurvives = (((dwLoad & 0x00000004) ? true : false) && (Ctx.dwVersion >= 58));
m_fOverdrive = ((dwLoad & 0x00000008) ? true : false);
m_fOptimized = ((dwLoad & 0x00000010) ? true : false);
m_fSecondaryWeapon = ((dwLoad & 0x00000020) ? true : false);
m_fFateDamaged = (((dwLoad & 0x00000040) ? true : false) && (Ctx.dwVersion >= 58));
m_fEnabled = ((dwLoad & 0x00000080) ? true : false);
m_fWaiting = ((dwLoad & 0x00000100) ? true : false);
m_fTriggered = ((dwLoad & 0x00000200) ? true : false);
m_fRegenerating = ((dwLoad & 0x00000400) ? true : false);
m_fLastActivateSuccessful = ((dwLoad & 0x00000800) ? true : false);
m_fLinkedFireAlways = ((dwLoad & 0x00001000) ? true : false);
m_fLinkedFireTarget = ((dwLoad & 0x00002000) ? true : false);
m_fLinkedFireEnemy = ((dwLoad & 0x00004000) ? true : false);
m_fExternal = ((dwLoad & 0x00008000) ? true : false);
m_fDuplicate = ((dwLoad & 0x00010000) ? true : false);
m_fCannotBeEmpty = ((dwLoad & 0x00020000) ? true : false);
m_fFateDestroyed = ((dwLoad & 0x00040000) ? true : false);
m_fFateComponetized = ((dwLoad & 0x00080000) ? true : false);
bool bSlotEnhancements =((dwLoad & 0x00100000) ? true : false);
// Previous versions did not save this flag
if (Ctx.dwVersion < 107)
m_fExternal = m_pClass->IsExternal();
// Fix up the item pointer (but only if it is installed)
m_pItem = NULL;
if (m_pClass != NULL && m_iDeviceSlot != -1)
{
CItemListManipulator ItemList(pSource->GetItemList());
pSource->SetCursorAtDevice(ItemList, this);
if (ItemList.IsCursorValid())
m_pItem = ItemList.GetItemPointerAtCursor();
// In previous versions we automatically offset weapon positions.
// In later versions we explicitly set the position, so we have
// to do so here.
if (Ctx.dwVersion < 62)
{
if (m_iPosRadius == 0
&& (m_pClass->GetCategory() == itemcatWeapon || m_pClass->GetCategory() == itemcatLauncher))
{
m_iPosAngle = 0;
m_iPosRadius = 20;
m_iPosZ = 0;
m_f3DPosition = false;
}
}
}
// Enhancement stack
if (Ctx.dwVersion >= 92)
m_pEnhancements = CItemEnhancementStack::ReadFromStream(Ctx);
if (bSlotEnhancements)
m_SlotEnhancements.ReadFromStream(Ctx);
if (iSlotBonus != 0)
m_SlotEnhancements.InsertHPBonus(iSlotBonus);
}
int CInstalledDevice::IncCharges (CSpaceObject *pSource, int iChange)
// IncCharges
//
// Increments charges
{
CShip *pShip = pSource->AsShip();
if (pShip == NULL)
return -1;
CItemListManipulator ItemList(pSource->GetItemList());
pShip->SetCursorAtDevice(ItemList, m_iDeviceSlot);
pShip->RechargeItem(ItemList, iChange);
return ItemList.GetItemAtCursor().GetCharges();
}
void CInstalledDevice::SetCharges (CSpaceObject *pSource, int iCharges)
// SetCharges
//
// Sets charges to this value.
{
CShip *pShip = pSource->AsShip();
if (pShip == NULL)
return;
CItemListManipulator ItemList(pSource->GetItemList());
pShip->SetCursorAtDevice(ItemList, m_iDeviceSlot);
pShip->RechargeItem(ItemList, iCharges - ItemList.GetItemAtCursor().GetCharges());
}
bool CInstalledDevice::SetEnabled (CSpaceObject *pSource, bool bEnabled)
// SetEnabled
//
// Enable/disable device. Returns TRUE if the enabled status changed.
{
DEBUG_TRY
if (m_fEnabled == bEnabled)
return false;
m_fEnabled = bEnabled;
// Fire event
CItem *pItem = GetItem();
if (pItem)
{
if (bEnabled)
pItem->FireOnEnabled(pSource);
else
pItem->FireOnDisabled(pSource);
}
// Done
return true;
DEBUG_CATCH
}
void CInstalledDevice::SetEnhancements (const TSharedPtr<CItemEnhancementStack> &pStack)
// SetEnhancements
//
// Sets the enhancements stack. NOTE: We take ownership of the stack.
{
if (pStack && !pStack->IsEmpty())
m_pEnhancements = pStack;
else
m_pEnhancements.Delete();
}
void CInstalledDevice::SetFate (ItemFates iFate)
// SetFate
//
// Sets the fate of the device when the ship is destroyed.
{
// Clear out all the flats
m_fFateDamaged = false;
m_fFateDestroyed = false;
m_fFateSurvives = false;
m_fFateComponetized = false;
// Now set the flags appropriately
switch (iFate)
{
case fateComponetized:
m_fFateComponetized = true;
break;
case fateDamaged:
m_fFateDamaged = true;
break;
case fateDestroyed:
m_fFateDestroyed = true;
break;
case fateSurvives:
m_fFateSurvives = true;
break;
default:
// Default.
break;
}
}
void CInstalledDevice::SetLastShot (CSpaceObject *pObj, int iIndex)
// SetLastShot
//
// Remembers the last projectile object for this index.
{
if (iIndex < m_LastShotIDs.GetCount())
m_LastShotIDs[iIndex] = (pObj ? pObj->GetID() : 0);
}
void CInstalledDevice::SetLastShotCount (int iCount)
// SetLastShotCount
//
// Sets the number of shots
{
int i;
m_LastShotIDs.DeleteAll();
m_LastShotIDs.InsertEmpty(iCount);
for (i = 0; i < iCount; i++)
m_LastShotIDs[i] = 0;
}
void CInstalledDevice::SetLinkedFireOptions (DWORD dwOptions)
// SetLinkedFireOptions
//
// Sets linked fire options
{
m_fLinkedFireAlways = false;
m_fLinkedFireTarget = false;
m_fLinkedFireEnemy = false;
if (dwOptions & CDeviceClass::lkfAlways)
m_fLinkedFireAlways = true;
else if (dwOptions & CDeviceClass::lkfTargetInRange)
m_fLinkedFireTarget = true;
else if (dwOptions & CDeviceClass::lkfEnemyInRange)
m_fLinkedFireEnemy = true;
}
bool CInstalledDevice::SetProperty (CItemCtx &Ctx, const CString &sName, ICCItem *pValue, CString *retsError)
// SetProperty
//
// Sets a property for an installed device. Returns FALSE if we could not set
// the property for some reason.
{
CCodeChain &CC = g_pUniverse->GetCC();
if (IsEmpty())
{
if (retsError) *retsError = CONSTLIT("No device installed.");
return false;
}
// Figure out what to set
if (strEquals(sName, PROPERTY_CAPACITOR))
{
CSpaceObject *pSource = Ctx.GetSource();
if (!m_pClass->SetCounter(this, pSource, CDeviceClass::cntCapacitor, pValue->GetIntegerValue()))
{
if (retsError) *retsError = CONSTLIT("Unable to set capacitor value.");
return false;
}
}
else if (strEquals(sName, PROPERTY_EXTERNAL))
{
bool bSetExternal = (pValue && !pValue->IsNil());
if (IsExternal() != bSetExternal)
{
// If the class is external and we don't want it to be external, then
// we cannot comply.
if (m_pClass->IsExternal() && !bSetExternal)
{
if (retsError) *retsError = CONSTLIT("Device is natively external and cannot be made internal.");
return false;
}
SetExternal(bSetExternal);
}
}
else if (strEquals(sName, PROPERTY_EXTRA_POWER_USE))
{
m_iExtraPowerUse = pValue->GetIntegerValue();
}
else if (strEquals(sName, PROPERTY_FIRE_ARC))
{
// A value of nil means no fire arc (and no omni)