-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCStation.cpp
5086 lines (3838 loc) · 113 KB
/
CStation.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
// CStation.cpp
//
// CStation class
#include "PreComp.h"
#ifdef DEBUG
//#define DEBUG_DOCKING
//#define DEBUG_ALERTS
#endif
#define ITEM_TAG CONSTLIT("Item")
#define INITIAL_DATA_TAG CONSTLIT("InitialData")
#define DEVICES_TAG CONSTLIT("Devices")
#define ITEMS_TAG CONSTLIT("Items")
#define COUNT_ATTRIB CONSTLIT("count")
#define ITEM_ATTRIB CONSTLIT("item")
#define DEVICE_ID_ATTRIB CONSTLIT("deviceID")
#define X_ATTRIB CONSTLIT("x")
#define Y_ATTRIB CONSTLIT("y")
#define SHIPWRECK_UNID_ATTRIB CONSTLIT("shipwreckID")
#define NAME_ATTRIB CONSTLIT("name")
#define LANGID_DOCKING_REQUEST_DENIED CONSTLIT("core.dockingRequestDenied")
#define PAINT_LAYER_OVERHANG CONSTLIT("overhang")
#define PROPERTY_ABANDONED CONSTLIT("abandoned")
#define PROPERTY_ACTIVE CONSTLIT("active")
#define PROPERTY_ANGRY CONSTLIT("angry")
#define PROPERTY_BARRIER CONSTLIT("barrier")
#define PROPERTY_DEST_NODE_ID CONSTLIT("destNodeID")
#define PROPERTY_DEST_STARGATE_ID CONSTLIT("destStargateID")
#define PROPERTY_DOCKING_PORT_COUNT CONSTLIT("dockingPortCount")
#define PROPERTY_EXPLORED CONSTLIT("explored")
#define PROPERTY_HP CONSTLIT("hp")
#define PROPERTY_IGNORE_FRIENDLY_FIRE CONSTLIT("ignoreFriendlyFire")
#define PROPERTY_IMAGE_SELECTOR CONSTLIT("imageSelector")
#define PROPERTY_MAX_HP CONSTLIT("maxHP")
#define PROPERTY_MAX_STRUCTURAL_HP CONSTLIT("maxStructuralHP")
#define PROPERTY_OPEN_DOCKING_PORT_COUNT CONSTLIT("openDockingPortCount")
#define PROPERTY_ORBIT CONSTLIT("orbit")
#define PROPERTY_PAINT_LAYER CONSTLIT("paintLayer")
#define PROPERTY_PARALLAX CONSTLIT("parallax")
#define PROPERTY_PLAYER_BACKLISTED CONSTLIT("playerBlacklisted")
#define PROPERTY_RADIOACTIVE CONSTLIT("radioactive")
#define PROPERTY_ROTATION CONSTLIT("rotation")
#define PROPERTY_ROTATION_SPEED CONSTLIT("rotationSpeed")
#define PROPERTY_SHIP_CONSTRUCTION_ENABLED CONSTLIT("shipConstructionEnabled")
#define PROPERTY_SHIP_REINFORCEMENT_ENABLED CONSTLIT("shipReinforcementEnabled")
#define PROPERTY_SHOW_MAP_LABEL CONSTLIT("showMapLabel")
#define PROPERTY_SHOW_MAP_ORBIT CONSTLIT("showMapOrbit")
#define PROPERTY_STARGATE_ID CONSTLIT("stargateID")
#define PROPERTY_STRUCTURAL_HP CONSTLIT("structuralHP")
#define PROPERTY_SUBORDINATES CONSTLIT("subordinates")
#define PROPERTY_SUPERIOR CONSTLIT("superior")
#define STR_TRUE CONSTLIT("true")
const int TRADE_UPDATE_FREQUENCY = 1801; // Interval for checking trade
const int STATION_SCAN_TARGET_FREQUENCY = 29;
const int STATION_ATTACK_FREQUENCY = 67;
const int STATION_REINFORCEMENT_FREQUENCY = 607;
const int STATION_TARGET_FREQUENCY = 503;
const int DAYS_TO_REFRESH_INVENTORY = 5;
const int INVENTORY_REFRESHED_PER_UPDATE = 20; // % of inventory refreshed on each update frequency
const Metric MAX_ATTACK_DISTANCE = LIGHT_SECOND * 25;
const Metric MAX_ATTACK_DISTANCE2 = MAX_ATTACK_DISTANCE * MAX_ATTACK_DISTANCE;
const Metric BEACON_RANGE = (LIGHT_SECOND * 20);
const Metric BEACON_RANGE2 = (BEACON_RANGE * BEACON_RANGE);
const int INITIAL_INVENTORY_REFRESH = 300;
#define MIN_ANGER 300
#define MAX_ANGER 1800
#define ANGER_INC 30
const CG32bitPixel RGB_SIGN_COLOR = CG32bitPixel(196, 223, 155);
const CG32bitPixel RGB_ORBIT_LINE = CG32bitPixel(115, 149, 229);
const CG32bitPixel RGB_MAP_LABEL = CG32bitPixel(255, 217, 128);
const CG32bitPixel RGB_LRS_LABEL = CG32bitPixel(165, 140, 83);
static CObjectClass<CStation>g_Class(OBJID_CSTATION);
static char g_ImageTag[] = "Image";
static char g_ShipsTag[] = "Ships";
static char g_DockScreensTag[] = "DockScreens";
static char g_ShipTag[] = "Ship";
static char g_DockScreenAttrib[] = "dockScreen";
static char g_AbandonedScreenAttrib[] = "abandonedScreen";
static char g_HitPointsAttrib[] = "hitPoints";
static char g_ArmorIDAttrib[] = "armorID";
static char g_ProbabilityAttrib[] = "probability";
static char g_TableAttrib[] = "table";
#define MIN_DOCK_APPROACH_SPEED (g_KlicksPerPixel * 25.0 / g_TimeScale);
#define MAX_DOCK_APPROACH_SPEED (g_KlicksPerPixel * 50.0 / g_TimeScale);
#define MAX_DOCK_TANGENT_SPEED (g_KlicksPerPixel / g_TimeScale);
const Metric g_DockBeamStrength = 1000.0;
const Metric g_DockBeamTangentStrength = 250.0;
const int g_iMapScale = 5;
const int DEFAULT_TIME_STOP_TIME = 150;
CStation::CStation (void) : CSpaceObject(&g_Class),
m_fArmed(false),
m_dwSpare(0),
m_pType(NULL),
m_pRotation(NULL),
m_pMapOrbit(NULL),
m_pBase(NULL),
m_pDevices(NULL),
m_iAngryCounter(0),
m_iReinforceRequestCount(0),
m_pMoney(NULL),
m_pTrade(NULL)
// CStation constructor
{
}
CStation::~CStation (void)
// CStation destructor
{
if (m_pRotation)
delete m_pRotation;
if (m_pMapOrbit)
delete m_pMapOrbit;
if (m_pDevices)
delete [] m_pDevices;
if (m_pMoney)
delete m_pMoney;
if (m_pTrade)
delete m_pTrade;
}
void CStation::Abandon (DestructionTypes iCause, const CDamageSource &Attacker, CWeaponFireDesc *pWeaponDesc)
// Abandon
//
// Abandon the station.
{
// Only works for stations that can be abandoned.
if (IsDestroyed() || IsImmutable() || IsAbandoned())
return;
m_Hull.SetHitPoints(0);
SDestroyCtx DestroyCtx;
DestroyCtx.pObj = this;
DestroyCtx.pDesc = pWeaponDesc;
DestroyCtx.Attacker = Attacker;
DestroyCtx.pWreck = this;
DestroyCtx.iCause = iCause;
// Run OnDestroy script
m_Overlays.FireOnObjDestroyed(this, DestroyCtx);
FireItemOnObjDestroyed(DestroyCtx);
FireOnDestroy(DestroyCtx);
// Station is destroyed. Take all the installed devices and turn
// them into normal damaged items
CItemListManipulator Items(GetItemList());
while (Items.MoveCursorForward())
{
CItem Item = Items.GetItemAtCursor();
if (Item.IsInstalled())
{
// Uninstall the device
int iDevSlot = Item.GetInstalled();
CInstalledDevice *pDevice = &m_pDevices[iDevSlot];
pDevice->Uninstall(this, Items);
// Chance that the item is destroyed
if (Item.GetType()->IsVirtual() || mathRandom(1, 100) <= 50)
Items.DeleteAtCursor(1);
else
Items.SetDamagedAtCursor(true);
// Reset cursor because we may have changed position
Items.ResetCursor();
}
}
InvalidateItemListAddRemove();
// Alert others and retaliate, if necessary.
//
// NOTE: We need to do this before <OnObjDestroyed> because we want
// that event to be able to override this behavior.
// because we want those events to be able to override this behavior.
CSpaceObject *pOrderGiver = Attacker.GetOrderGiver();
if (pOrderGiver && pOrderGiver->CanAttack())
{
// If we're a subordinate of some base, then let it handle the
// retaliation.
if (m_pBase && !m_pBase->IsAbandoned() && !m_pBase->IsDestroyed())
m_pBase->OnSubordinateDestroyed(DestroyCtx);
// We also retailiate
if (!IsAngryAt(pOrderGiver))
OnDestroyedByFriendlyFire(Attacker.GetObj(), pOrderGiver);
else
OnDestroyedByHostileFire(Attacker.GetObj(), pOrderGiver);
}
// Tell all objects that we've been destroyed
for (int i = 0; i < GetSystem()->GetObjectCount(); i++)
{
CSpaceObject *pObj = GetSystem()->GetObject(i);
if (pObj && pObj != this)
pObj->OnStationDestroyed(DestroyCtx);
}
// Tell the system to notify events that we've been destroyed
GetSystem()->OnStationDestroyed(DestroyCtx);
// NOTE: We fire <OnObjDestroyed> AFTER OnStationDestroyed
// so that script inside <OnObjDestroyed> can add orders
// about the station (without getting clobbered in
// OnStationDestroyed).
NotifyOnObjDestroyed(DestroyCtx);
// Tell the system and universe that a station has been destroyed so
// that they handle timed events, etc.
GetSystem()->FireOnSystemObjDestroyed(DestroyCtx);
g_pUniverse->NotifyOnObjDestroyed(DestroyCtx);
// Clear destination
if (IsAutoClearDestinationOnDestroy())
ClearPlayerDestination();
}
void CStation::AddOverlay (COverlayType *pType, int iPosAngle, int iPosRadius, int iRotation, int iPosZ, int iLifeLeft, DWORD *retdwID)
// AddOverlay
//
// Adds an overlay to the ship
{
m_Overlays.AddField(this, pType, iPosAngle, iPosRadius, iRotation, iPosZ, iLifeLeft, retdwID);
// Recalc bonuses, etc.
CalcBounds();
}
void CStation::AddSubordinate (CSpaceObject *pSubordinate)
// AddSubordinate
//
// Add this object to our list of subordinates
{
m_Subordinates.Add(pSubordinate);
// HACK: If we're adding a station, set it to point back to us
CStation *pSatellite = pSubordinate->AsStation();
if (pSatellite)
pSatellite->SetBase(this);
}
CTradingDesc *CStation::AllocTradeDescOverride (void)
// AllocTradeDescOverride
//
// Makes sure that we have the m_pTrade structure allocated.
// This is an override of the trade desc in the type
{
if (m_pTrade == NULL)
{
m_pTrade = new CTradingDesc;
// Set the same economy type
CTradingDesc *pBaseTrade = m_pType->GetTradingDesc();
if (pBaseTrade)
{
m_pTrade->SetEconomyType(pBaseTrade->GetEconomyType());
m_pTrade->SetMaxCurrency(pBaseTrade->GetMaxCurrency());
m_pTrade->SetReplenishCurrency(pBaseTrade->GetReplenishCurrency());
}
}
return m_pTrade;
}
bool CStation::Blacklist (CSpaceObject *pObj)
// Blacklist
//
// pObj is blacklisted (this only works for the player). Returns TRUE if the
// object is blacklisted.
{
int i;
if (pObj == NULL || !pObj->IsPlayer())
return false;
// No need if we don't support blacklist
if (!CanBlacklist())
return false;
// Remember if we need to send out an event
bool bFireEvent = !m_Blacklist.IsBlacklisted();
// Remember that player is blacklisted
// (We do this early in case we recurse)
m_Blacklist.Blacklist();
// Fire event
if (bFireEvent)
FireOnPlayerBlacklisted();
// If we're still blacklisted (if the event did not reverse this) then we
// deter the attacker.
if (m_Blacklist.IsBlacklisted())
{
// Tell our subordinates to blacklist the player also.
for (i = 0; i < m_Subordinates.GetCount(); i++)
{
CStation *pSubordinate = m_Subordinates.GetObj(i)->AsStation();
if (pSubordinate)
pSubordinate->m_Blacklist.Blacklist();
}
return true;
}
else
return false;
}
void CStation::CalcBounds (void)
// CalcBounds
//
// Calculates and sets station bounds
{
const CObjectImageArray &Image = GetImage(false, NULL, NULL);
RECT rcBounds = Image.GetImageRect();
int xOffset;
int yOffset;
if (Image.GetImageOffset(0, 0, &xOffset, &yOffset))
{
rcBounds.right += 2 * Absolute(xOffset);
rcBounds.bottom += 2 * Absolute(yOffset);
}
// Add overlays
m_Overlays.AccumulateBounds(this, Image.GetImageViewportSize(), GetRotation(), &rcBounds);
// Set it
SetBounds(rcBounds, GetParallaxDist());
}
void CStation::CalcImageModifiers (CCompositeImageModifiers *retModifiers, int *retiTick) const
// CalcImageModifier
//
// Compute the modifiers for the station
{
// Modifiers (such as station damage)
if (retModifiers)
{
// System filters
retModifiers->SetFilters(GetSystemFilters());
// Damage
if (ShowStationDamage())
retModifiers->SetStationDamage(true);
// Rotation
if (m_pRotation)
retModifiers->SetRotation(m_pRotation->GetRotationAngle(m_pType->GetRotationDesc()));
}
// Tick
if (retiTick)
{
if (m_fActive && !IsTimeStopped())
*retiTick = GetSystem()->GetTick() + GetDestiny();
else
*retiTick = 0;
}
}
int CStation::CalcNumberOfShips (void)
// CalcNumberOfShips
//
// Returns the number of ships associated with this station
{
DEBUG_TRY
int i;
int iCount = 0;
for (i = 0; i < GetSystem()->GetObjectCount(); i++)
{
CSpaceObject *pObj = GetSystem()->GetObject(i);
if (pObj
&& pObj->GetBase() == this
&& pObj->GetCategory() == catShip
&& pObj->CanAttack() // Exclude ship sections
&& pObj != this)
iCount++;
}
return iCount;
DEBUG_CATCH
}
bool CStation::CalcVolumetricShadowLine (SLightingCtx &Ctx, int *retxCenter, int *retyCenter, int *retiWidth, int *retiLength)
// CalcVolumetricShadowLine
//
// Computes the line shadow line for the object.
{
// Get the image
int iTick, iVariant;
const CObjectImageArray &Image = GetImage(false, &iTick, &iVariant);
// Get the shadow line from the image
return Image.CalcVolumetricShadowLine(Ctx, iTick, iVariant, retxCenter, retyCenter, retiWidth, retiLength);
}
bool CStation::CanAttack (void) const
// CanAttack
//
// TRUE if the object can attack
{
return (!IsAbandoned()
&& !IsVirtual()
&& (m_fArmed
|| (m_Subordinates.GetCount() > 0)
|| m_pType->CanAttack()));
}
bool CStation::CanBlock (CSpaceObject *pObj)
// CanBlock
//
// Returns TRUE if this object can block the given object
{
return (m_fBlocksShips
|| (pObj->GetCategory() == catStation && !pObj->IsAnchored()));
}
CSpaceObject::RequestDockResults CStation::CanObjRequestDock (CSpaceObject *pObj) const
// CanObjRequestDock
//
// Returns TRUE if pObj can request to dock with us. We return FALSE if we
// don't want to give the player the flashing docking port UI. But we return
// TRUE if we allow requests, but the request could be denied (e.g., because
{
// There are various reasons why docking might be impossible with this
// ship, including no docking ports.
if (m_DockingPorts.GetPortCount() == 0
|| IsDestroyed())
return dockingNotSupported;
// If the object requesting docking services is an enemy,
// then deny docking services.
if (pObj
&& !IsAbandoned()
&& !m_pType->IsEnemyDockingAllowed()
&& (IsEnemy(pObj) || IsBlacklisted(pObj)))
return dockingDenied;
// If the player wants to dock with us and we don't have any docking
// screens, then we do not support docking.
//
// NOTE: We check this AFTER the dockingDenied check above because after
// a station is destroyed, the player will be able to dock (docking IS
// supported) but they're just not allowed to right now.
if (pObj && pObj->IsPlayer() && !HasDockScreen())
return dockingNotSupported;
// In some cases, the docking system is temporarily disabled. For example,
// if we're docked with another object, no one can dock with us.
if (IsTimeStopped())
return dockingDisabled;
// Otherwise, docking is allowed
return dockingOK;
}
bool CStation::ClassCanAttack (void)
// ClassCanAttack
//
// Only returns FALSE if this object can never attack
{
return (m_pType->CanAttack());
}
void CStation::ClearBlacklist (CSpaceObject *pObj)
// ClearBlacklist
//
// Removes blacklist
{
int i;
if (pObj && !pObj->IsPlayer())
return;
// No need if we don't support blacklist
if (!m_pType->IsBlacklistEnabled())
return;
// Remember that player is not blacklisted
// (We do this early in case we recurse)
m_Blacklist.ClearBlacklist();
// Tell our subordinates to clear the blacklist also.
for (i = 0; i < m_Subordinates.GetCount(); i++)
{
CStation *pSubordinate = m_Subordinates.GetObj(i)->AsStation();
if (pSubordinate)
pSubordinate->m_Blacklist.ClearBlacklist();
}
// Cancel attack
if (pObj)
{
// Remove object from target
m_Targets.Delete(pObj);
// Send all our subordinates to cancel attack
for (int i = 0; i < m_Subordinates.GetCount(); i++)
Communicate(m_Subordinates.GetObj(i), msgAbort, pObj);
}
}
void CStation::CreateDestructionEffect (void)
// CreateDestructionEffect
//
// Create the effect when the station is destroyed
{
// Start destruction animation
m_iDestroyedAnimation = 60;
// Explosion effect and damage
SExplosionType Explosion;
FireGetExplosionType(&Explosion);
if (Explosion.pDesc == NULL)
Explosion.pDesc = m_pType->GetExplosionType();
if (Explosion.pDesc)
{
SShotCreateCtx Ctx;
Ctx.pDesc = Explosion.pDesc;
if (Explosion.iBonus != 0)
{
Ctx.pEnhancements.TakeHandoff(new CItemEnhancementStack);
Ctx.pEnhancements->InsertHPBonus(Explosion.iBonus);
}
Ctx.Source = CDamageSource(this, Explosion.iCause);
Ctx.vPos = GetPos();
Ctx.vVel = GetVel();
Ctx.dwFlags = SShotCreateCtx::CWF_EXPLOSION;
GetSystem()->CreateWeaponFire(Ctx);
}
// Some air leaks
CParticleEffect *pEffect;
CParticleEffect::CreateEmpty(GetSystem(),
this,
GetPos(),
GetVel(),
&pEffect);
int iAirLeaks = mathRandom(0, 5);
for (int j = 0; j < iAirLeaks; j++)
{
CParticleEffect::SParticleType *pType = new CParticleEffect::SParticleType;
pType->m_fWake = true;
pType->m_fLifespan = true;
pType->iLifespan = 30;
pType->m_fRegenerate = true;
pType->iRegenerationTimer = 300 + mathRandom(0, 200);
pType->iDirection = mathRandom(0, 359);
pType->iDirRange = 3;
pType->rAveSpeed = 0.1 * LIGHT_SPEED;
pType->m_fMaxRadius = false;
pType->rRadius = pType->iLifespan * pType->rAveSpeed;
pType->rDampening = 0.75;
pType->iPaintStyle = CParticleEffect::paintSmoke;
pEffect->AddGroup(pType, mathRandom(50, 150));
}
// Sound effects
g_pUniverse->PlaySound(this, g_pUniverse->FindSound(g_StationExplosionSoundUNID));
}
void CStation::CreateEjectaFromDamage (int iDamage, const CVector &vHitPos, int iDirection, const DamageDesc &Damage)
// CreateEjectaFromDamage
//
// Create ejecta when hit by damage
{
int i;
int iEjectaAdj;
if (iEjectaAdj = m_pType->GetEjectaAdj())
{
// Ignore if damage came from ejecta (so that we avoid chain reactions)
if (Damage.GetCause() == killedByEjecta && mathRandom(1, 100) <= 90)
return;
// Adjust for the station propensity to create ejecta.
iDamage = iEjectaAdj * iDamage / 100;
if (iDamage == 0)
return;
// Compute the number of pieces of ejecta
int iCount;
if (iDamage <= 5)
iCount = ((mathRandom(1, 100) <= (iDamage * 20)) ? 1 : 0);
else if (iDamage <= 12)
iCount = mathRandom(1, 3);
else if (iDamage <= 24)
iCount = mathRandom(2, 6);
else
iCount = mathRandom(4, 12);
// Generate ejecta
CWeaponFireDesc *pEjectaType = m_pType->GetEjectaType();
for (i = 0; i < iCount; i++)
{
SShotCreateCtx Ctx;
Ctx.pDesc = pEjectaType;
Ctx.Source = CDamageSource(this, killedByEjecta);
Ctx.iDirection = AngleMod(iDirection
+ (mathRandom(0, 12) + mathRandom(0, 12) + mathRandom(0, 12) + mathRandom(0, 12) + mathRandom(0, 12))
+ (360 - 30));
Ctx.vPos = vHitPos;
Ctx.vVel = GetVel() + PolarToVector(Ctx.iDirection, pEjectaType->GetInitialSpeed());
Ctx.dwFlags = SShotCreateCtx::CWF_EJECTA;
GetSystem()->CreateWeaponFire(Ctx);
}
// Create geyser effect
CParticleEffect::CreateGeyser(GetSystem(),
this,
vHitPos,
NullVector,
mathRandom(5, 15),
mathRandom(50, 150),
CParticleEffect::paintFlame,
iDamage + 2,
0.15 * LIGHT_SPEED,
iDirection,
20,
NULL);
}
}
ALERROR CStation::CreateFromType (CSystem *pSystem,
CStationType *pType,
SObjCreateCtx &CreateCtx,
CStation **retpStation,
CString *retsError)
// CreateFromType
//
// Creates a new station based on the type
{
DEBUG_TRY
ALERROR error;
CStation *pStation;
CXMLElement *pDesc = pType->GetDesc();
int i;
if (!CreateCtx.bIgnoreLimits && !pType->CanBeEncountered())
{
if (retsError)
*retsError = CONSTLIT("Cannot be encountered");
return ERR_FAIL;
}
// Create the new station
pStation = new CStation;
if (pStation == NULL)
{
if (retsError)
*retsError = CONSTLIT("Out of memory");
return ERR_MEMORY;
}
// Initialize
pStation->m_pType = pType;
pStation->Place(CreateCtx.vPos, CreateCtx.vVel);
pStation->m_pTrade = NULL;
pStation->m_iDestroyedAnimation = 0;
pStation->m_fKnown = false;
pStation->m_fReconned = false;
pStation->m_fExplored = false;
pStation->m_fFireReconEvent = false;
pStation->m_fActive = pType->IsActive();
pStation->m_fNoReinforcements = false;
pStation->m_fNoConstruction = false;
pStation->m_fRadioactive = false;
pStation->m_iMapLabelPos = CMapLabelArranger::posRight;
pStation->m_rMass = pType->GetMass();
pStation->m_dwWreckUNID = 0;
pStation->m_fNoBlacklist = false;
pStation->SetHasGravity(pType->HasGravity());
pStation->m_fPaintOverhang = pType->IsPaintLayerOverhang();
pStation->m_fDestroyIfEmpty = false;
pStation->m_fIsSegment = CreateCtx.bIsSegment;
// Set up rotation, if necessary
if (CreateCtx.iRotation != -1)
{
pStation->m_pRotation = new CIntegralRotation;
pStation->m_pRotation->SetRotationAngle(pType->GetRotationDesc(), CreateCtx.iRotation);
}
else if (pType->GetImage().IsRotatable())
{
pStation->m_pRotation = new CIntegralRotation;
pStation->m_pRotation->SetRotationAngle(pType->GetRotationDesc(), mathRandom(0, 359));
}
else
pStation->m_pRotation = NULL;
// Handle moving stations
if (pType->IsMobile())
{
// Mobile objects cannot have a mass of 0 (otherwise the bouncing routines
// might fail).
pStation->m_rMass = Max(1.0, pStation->m_rMass);
pStation->SetCanBounce();
}
// Background objects cannot be hit
if (CreateCtx.rParallax != 1.0)
pStation->m_rParallaxDist = CreateCtx.rParallax;
else
pStation->m_rParallaxDist = pType->GetParallaxDist();
if (pStation->m_rParallaxDist != 1.0)
pStation->SetOutOfPlaneObj(true);
if (pType->IsVirtual())
pStation->SetCannotBeHit();
// Friendly fire?
if (!pType->CanHitFriends())
pStation->SetNoFriendlyFire();
if (!pType->CanBeHitByFriends())
pStation->SetNoFriendlyTarget();
// Name
const CNameDesc &Name = pType->GetNameDesc();
if (!Name.IsConstant())
pStation->m_sName = Name.GenerateName(&CreateCtx.SystemCtx.NameParams, &pStation->m_dwNameFlags);
else
pStation->m_dwNameFlags = 0;
// Stargates
pStation->m_sStargateDestNode = pType->GetDestNodeID();
pStation->m_sStargateDestEntryPoint = pType->GetDestEntryPoint();
// Get the scale
pStation->m_Scale = pType->GetScale();
// Map
if (CreateCtx.pOrbit)
pStation->m_pMapOrbit = new COrbit(*CreateCtx.pOrbit);
else
pStation->m_pMapOrbit = NULL;
pStation->m_fShowMapOrbit = false;
pStation->m_fNoMapLabel = !pType->ShowsMapLabel();
// We block others (CanBlock returns TRUE only for other stations)
if (pStation->m_Scale != scaleStar && pStation->m_Scale != scaleWorld)
pStation->SetIsBarrier();
pStation->m_fBlocksShips = pType->IsWall();
// Load hit points and armor information
pStation->m_Hull.Init(pType->GetHullDesc());
if (!pStation->m_Hull.CanBeHit())
pStation->SetCannotBeHit();
// Pick an appropriate image. This call will set the shipwreck image, if
// necessary or the variant (if appropriate).
SSelectorInitCtx InitCtx;
InitCtx.pSystem = pSystem;
InitCtx.vObjPos = CreateCtx.vPos;
InitCtx.sLocAttribs = (CreateCtx.pLoc ? CreateCtx.pLoc->GetAttributes() : NULL_STR);
pType->SetImageSelector(InitCtx, &pStation->m_ImageSelector);
// If the image is a shipwreck class, then we take the parameters from the
// appropriate class.
CShipClass *pWreckClass;
if (pType->GetImage().HasShipwreckClass(pStation->m_ImageSelector, &pWreckClass))
pStation->SetWreckParams(pWreckClass);
// Otherwise, if we've got a shipwreck class, take parameters from that.
else if (CreateCtx.pWreckClass)
{
// If necessary, set the image
if (pType->GetImage().NeedsShipwreckClass())
{
pStation->m_ImageSelector.DeleteAll();
pStation->m_ImageSelector.AddShipwreck(DEFAULT_SELECTOR_ID, CreateCtx.pWreckClass);
}
// Set the parameters
pStation->SetWreckParams(CreateCtx.pWreckClass, CreateCtx.pWreckShip);
}
// Now that we have an image, set the bound
pStation->CalcBounds();
// Create any items on the station
if (error = pStation->CreateRandomItems(pType->GetRandomItemTable(), pSystem))
{
if (retsError)
*retsError = CONSTLIT("Unable to create random items");
delete pStation;
return error;
}
// Initialize devices
CXMLElement *pDevices = pDesc->GetContentElementByTag(DEVICES_TAG);
if (pDevices)
{
CItemListManipulator Items(pStation->GetItemList());
for (i = 0;
(i < pDevices->GetContentElementCount() && i < maxDevices);
i++)
{
CXMLElement *pDeviceDesc = pDevices->GetContentElement(i);
DWORD dwDeviceID = pDeviceDesc->GetAttributeInteger(DEVICE_ID_ATTRIB);
if (dwDeviceID == 0)
dwDeviceID = pDeviceDesc->GetAttributeInteger(ITEM_ATTRIB);
CDeviceClass *pClass = g_pUniverse->FindDeviceClass(dwDeviceID);
if (pClass == NULL)
{
if (retsError)
*retsError = strPatternSubst(CONSTLIT("Cannot find deviceID: %08x"), dwDeviceID);
return ERR_FAIL;
}
// Allocate the devices structure
if (pStation->m_pDevices == NULL)
pStation->m_pDevices = new CInstalledDevice [maxDevices];
// Add as an item
CItem DeviceItem(pClass->GetItemType(), 1);
Items.AddItem(DeviceItem);
// Initialize properties of the device slot
SDesignLoadCtx Ctx;
pStation->m_pDevices[i].InitFromXML(Ctx, pDeviceDesc);
pStation->m_pDevices[i].OnDesignLoadComplete(Ctx);
// Install the device
pStation->m_pDevices[i].Install(pStation, Items, i, true);
// Is this a weapon? If so, set a flag so that we know that
// this station is armed. This is an optimization so that we don't
// do a lot of work for stations that have no weapons (e.g., asteroids)
if (pStation->m_pDevices[i].GetCategory() == itemcatWeapon
|| pStation->m_pDevices[i].GetCategory() == itemcatLauncher)
{
pStation->m_fArmed = true;
pStation->m_pDevices[i].SelectFirstVariant(pStation);
}
}
}