-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCSystem.cpp
5188 lines (3811 loc) · 115 KB
/
CSystem.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
// CSystem.cpp
//
// CSystem class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#include "math.h"
#define ENHANCED_SRS_BLOCK_SIZE 6
#define LEVEL_ENCOUNTER_CHANCE 10
const Metric MAX_AUTO_TARGET_DISTANCE = 30.0 * LIGHT_SECOND;
const Metric MAX_ENCOUNTER_DIST = 30.0 * LIGHT_MINUTE;
const Metric MAX_MIRV_TARGET_RANGE = 50.0 * LIGHT_SECOND;
const Metric GRAVITY_WARNING_THRESHOLD = 40.0; // Acceleration value at which we start warning
const Metric TIDAL_KILL_THRESHOLD = 7250.0; // Acceleration at which we get ripped apart
const BYTE MAX_SPACE_OPACITY = 128;
const int MAX_THREAD_COUNT = 16;
#define ON_CREATE_EVENT CONSTLIT("OnCreate")
#define ON_OBJ_JUMP_POS_ADJ CONSTLIT("OnObjJumpPosAdj")
#define SPECIAL_ATTRIB_INNER_SYSTEM CONSTLIT("innerSystem")
#define SPECIAL_ATTRIB_LIFE_ZONE CONSTLIT("lifeZone")
#define SPECIAL_ATTRIB_NEAR_STATIONS CONSTLIT("nearStations")
#define SPECIAL_ATTRIB_OUTER_SYSTEM CONSTLIT("outerSystem")
int g_iGateTimer = 0;
int g_iGateTimerTick = -1;
int g_cxStarField = -1;
int g_cyStarField = -1;
const CG32bitPixel g_rgbSpaceColor = CG32bitPixel(0,0,8);
const Metric g_MetersPerKlick = 1000.0;
const Metric MAP_VERTICAL_ADJUST = 1.4;
const CG32bitPixel RGB_GRID_LINE = CG32bitPixel(65, 68, 77);
const Metric BACKGROUND_OBJECT_FACTOR = 4.0;
const int GRID_SIZE = 256;
const Metric CELL_SIZE = (512.0 * g_KlicksPerPixel);
const Metric CELL_BORDER = (128.0 * g_KlicksPerPixel);
const Metric SAME_POS_THRESHOLD2 = (g_KlicksPerPixel * g_KlicksPerPixel);
const Metric MAP_GRID_SIZE = 3000.0 * LIGHT_SECOND;
CSystem::CSystem (CUniverse *pUniv, CTopologyNode *pTopology) :
m_dwID(OBJID_NULL),
m_pTopology(pTopology),
m_pEnvironment(NULL),
m_iTick(0),
m_iNextEncounter(0),
m_iTimeStopped(0),
m_rKlicksPerPixel(KLICKS_PER_PIXEL),
m_rTimeScale(TIME_SCALE),
m_iLastUpdated(-1),
m_fNoRandomEncounters(false),
m_fInCreate(false),
m_fEncounterTableValid(false),
m_fUseDefaultTerritories(true),
m_fEnemiesInLRS(false),
m_fEnemiesInSRS(false),
m_fPlayerUnderAttack(false),
m_fLocationsBlocked(false),
m_pThreadPool(NULL),
m_ObjGrid(GRID_SIZE, CELL_SIZE, CELL_BORDER)
// CSystem constructor
{
// Make sure our vectors are initialized
EuclidInit();
}
CSystem::~CSystem (void)
// CSystem destructor
{
int i;
// Set our topology node to NULL so that a new system is
// created next time we access this node.
if (m_pTopology)
m_pTopology->SetSystem(NULL);
if (m_pEnvironment)
delete m_pEnvironment;
if (m_pThreadPool)
delete m_pThreadPool;
// Clear out any attached object because those are freed by their owners.
for (i = 0; i < m_AllObjects.GetCount(); i++)
if (m_AllObjects[i] && m_AllObjects[i]->IsAttached())
m_AllObjects[i] = NULL;
// Free objects
for (i = 0; i < m_AllObjects.GetCount(); i++)
if (m_AllObjects[i])
delete m_AllObjects[i];
// Deleted objects
FlushDeletedObjects();
}
bool CSystem::AddJoint (CObjectJoint::ETypes iType, CSpaceObject *pFrom, CSpaceObject *pTo, CObjectJoint **retpJoint)
// AddJoint
//
// Creates a new joint and returns it.
{
m_Joints.CreateJoint(iType, pFrom, pTo, retpJoint);
return true;
}
bool CSystem::AddJoint (CObjectJoint::ETypes iType, CSpaceObject *pFrom, CSpaceObject *pTo, ICCItem *pOptions, DWORD *retdwID)
// AddJoint
//
// Adds a new joint between two objects.
{
CObjectJoint *pJoint;
m_Joints.CreateJoint(iType, pFrom, pTo, &pJoint);
// Apply the options
pJoint->ApplyOptions(pOptions);
// Done
if (retdwID)
*retdwID = pJoint->GetID();
return true;
}
ALERROR CSystem::AddTerritory (CTerritoryDef *pTerritory)
// AddTerritory
//
// Adds a territory
{
m_Territories.Insert(pTerritory);
// If we are defining one of the special attributes, then remember
// that so we don't implicitly define them.
if (m_fUseDefaultTerritories
&& (pTerritory->HasAttribute(SPECIAL_ATTRIB_INNER_SYSTEM)
|| pTerritory->HasAttribute(SPECIAL_ATTRIB_LIFE_ZONE)
|| pTerritory->HasAttribute(SPECIAL_ATTRIB_OUTER_SYSTEM)))
m_fUseDefaultTerritories = false;
return NOERROR;
}
ALERROR CSystem::AddTimedEvent (CSystemEvent *pEvent)
// AddTimedEvent
//
// Adds a timed event
{
m_TimedEvents.AddEvent(pEvent);
return NOERROR;
}
void CSystem::AddToDeleteList (CSpaceObject *pObj)
// AddToDeleteList
//
// Adds the object to a list to be deleted later.
{
ASSERT(pObj->IsDestroyed());
ASSERT(pObj->GetID() != 0xdddddddd);
m_DeletedObjects.FastAdd(pObj);
}
ALERROR CSystem::AddToSystem (CSpaceObject *pObj, int *retiIndex)
// AddToSystem
//
// Adds an object to the system
{
int i;
// If this object affects the enemy object cache, then
// flush the cache
if (pObj->ClassCanAttack())
FlushEnemyObjectCache();
// If this is a star, add it to our list of stars
if (pObj->GetScale() == scaleStar)
{
SStarDesc *pDesc = m_Stars.Insert();
pDesc->pStarObj = pObj;
}
// Reuse a slot first
for (i = 0; i < m_AllObjects.GetCount(); i++)
{
if (m_AllObjects[i] == NULL)
{
m_AllObjects[i] = pObj;
if (retiIndex)
*retiIndex = i;
return NOERROR;
}
}
// If we could not find a free place, add a new object
if (retiIndex)
*retiIndex = m_AllObjects.GetCount();
m_AllObjects.Insert(pObj);
return NOERROR;
}
bool CSystem::AscendObject (CSpaceObject *pObj, CString *retsError)
// AscendObject
//
// Removes the object from the system and adds it to the global list of
// ascended objects. Return FALSE if there was an error.
{
if (pObj->IsAscended())
return true;
if (pObj->IsDestroyed())
{
if (retsError)
*retsError = CONSTLIT("Cannot ascend destroyed objects.");
return false;
}
if (pObj->IsPlayer())
{
if (retsError)
*retsError = CONSTLIT("Cannot ascend the player ship.");
return false;
}
if (pObj->GetSystem() != this)
{
if (retsError)
*retsError = CONSTLIT("Cannot ascend object in another system.");
return false;
}
// Ascend the object
pObj->Ascend();
// Add to the list of ascended objects
g_pUniverse->AddAscendedObj(pObj);
// Done
return true;
}
void CSystem::BlockOverlappingLocations (void)
// BlockOverlappingLocations
//
// Remove any locations that overlap something or are too close to another
// location.
{
int i;
// If we've already done this, no need to do it again.
if (m_fLocationsBlocked)
return;
// Remove any locations that are directly on top of a planet. We only check
// for objects above a certain size because we can move/remove asteroids
// instead of locations.
for (i = 0; i < GetObjectCount(); i++)
{
CSpaceObject *pObj = GetObject(i);
if (pObj == NULL
|| pObj->IsDestroyed()
|| (pObj->GetScale() != scaleStar && pObj->GetPlanetarySize() < MIN_PLANET_SIZE))
continue;
// Fill any locations that overlap with this object.
m_Locations.FillOverlappingWith(pObj);
}
// Block any location that is too close to another.
m_Locations.FillCloseLocations();
}
Metric CSystem::CalcApparentSpeedAdj (Metric rSpeed)
// CalcApparentSpeedAdj
//
// Calculate adjustment to speed to simulate light-lag.
{
// We max out at 10x light-speed, apparent speed.
const Metric MAX_ADJ = 10.0;
// Compute speed as a fraction of light-speed. But we get infinities if we
// actually get to light-speed, so we stop near light-speed.
const Metric NEAR_LIGHT_SPEED = 0.99;
Metric rSpeedFrac = Min(NEAR_LIGHT_SPEED, rSpeed / LIGHT_SPEED);
// Adjust to simulate speed as seen by observer if object were heading
// straight for observer.
Metric rAdj = (1.0 / ((1.0 / rSpeedFrac) - 1.0)) + 1.0;
return Min(MAX_ADJ, rAdj);
}
void CSystem::CalcAutoTarget (SUpdateCtx &Ctx)
// CalcAutoTarget
//
// Initializes various fields in the context to figure out what the player's
// auto-target is.
{
if (Ctx.pPlayer == NULL)
return;
Ctx.pPlayerTarget = Ctx.pPlayer->GetTarget(CItemCtx(), true);
// Check to see if the primary weapon requires autotargetting
CInstalledDevice *pWeapon = Ctx.pPlayer->GetNamedDevice(devPrimaryWeapon);
if (pWeapon && pWeapon->IsEnabled())
{
CItemCtx ItemCtx(Ctx.pPlayer, pWeapon);
Ctx.bNeedsAutoTarget = pWeapon->GetClass()->NeedsAutoTarget(ItemCtx, &Ctx.iMinFireArc, &Ctx.iMaxFireArc);
}
// If the primary does not need it, check the missile launcher
CInstalledDevice *pLauncher;
if ((pLauncher = Ctx.pPlayer->GetNamedDevice(devMissileWeapon))
&& pLauncher->IsEnabled())
{
CItemCtx ItemCtx(Ctx.pPlayer, pLauncher);
int iLauncherMinFireArc, iLauncherMaxFireArc;
if (pLauncher->GetClass()->NeedsAutoTarget(ItemCtx, &iLauncherMinFireArc, &iLauncherMaxFireArc))
{
if (Ctx.bNeedsAutoTarget)
CGeometry::CombineArcs(Ctx.iMinFireArc, Ctx.iMaxFireArc, iLauncherMinFireArc, iLauncherMaxFireArc, &Ctx.iMinFireArc, &Ctx.iMaxFireArc);
else
{
Ctx.bNeedsAutoTarget = true;
Ctx.iMinFireArc = iLauncherMinFireArc;
Ctx.iMaxFireArc = iLauncherMaxFireArc;
}
}
}
// Set up perception and max target dist
Ctx.iPlayerPerception = Ctx.pPlayer->GetPerception();
Ctx.rTargetDist2 = MAX_AUTO_TARGET_DISTANCE * MAX_AUTO_TARGET_DISTANCE;
}
int CSystem::CalculateLightIntensity (const CVector &vPos, CSpaceObject **retpStar, const CG8bitSparseImage **retpVolumetricMask)
// CalculateLightIntensity
//
// Returns 0-100% the intensity of the light at this point
// in space.
{
DEBUG_TRY
int i;
// Find the nearest star to the position. We optimize the case where
// there is only a single star in the system.
int iBestDist;
SStarDesc *pBestStar = NULL;
if (m_Stars.GetCount() == 1)
{
pBestStar = &m_Stars[0];
iBestDist = (int)(vPos.Longest() / LIGHT_SECOND);
}
else
{
pBestStar = NULL;
iBestDist = 100000000;
for (i = 0; i < m_Stars.GetCount(); i++)
{
CSpaceObject *pStar = m_Stars[i].pStarObj;
CVector vDist = vPos - pStar->GetPos();
int iDistFromCenter = (int)(vDist.Longest() / LIGHT_SECOND);
if (iDistFromCenter < iBestDist)
{
iBestDist = iDistFromCenter;
pBestStar = &m_Stars[i];
}
}
if (pBestStar == NULL)
{
if (retpStar)
*retpStar = NULL;
if (retpVolumetricMask)
*retpVolumetricMask = NULL;
return 0;
}
}
// Compute the percentage
int iMaxDist = pBestStar->pStarObj->GetMaxLightDistance();
int iDistFromCenter = (iBestDist < 15 ? 0 : iBestDist - 15);
int iPercent = 100 - (iDistFromCenter * 100 / iMaxDist);
if (retpStar)
*retpStar = pBestStar->pStarObj;
if (retpVolumetricMask)
*retpVolumetricMask = &pBestStar->VolumetricMask;
return Max(0, iPercent);
DEBUG_CATCH
}
int CSystem::CalcLocationWeight (CLocationDef *pLoc, const CAttributeCriteria &Criteria)
// CalcLocationWeight
//
// Calculates the weight of the given location relative to the given
// criteria.
//
// See: CAttributeCriteria::CalcWeightAdj
//
// EXAMPLES:
//
// Criteria = "*" LocationAttribs = "{anything}" Result = 1000
// Criteria = "+asteroids" LocationAttribs = "asteroids" Result = 2000
// Criteria = "+asteroids" LocationAttribs = "foo" Result = 1000
// Criteria = "-asteroids" LocationAttribs = "asteroids" Result = 500
// Criteria = "-asteroids" LocationAttribs = "foo" Result = 1000
{
int i;
int iWeight = 1000;
// Handle edge cases
if (Criteria.MatchesAll())
return iWeight;
// Adjust weight based on criteria
const CString &sAttributes = pLoc->GetAttributes();
CVector vPos = pLoc->GetOrbit().GetObjectPos();
for (i = 0; i < Criteria.GetCount(); i++)
{
DWORD dwMatchStrength;
const CString &sAttrib = Criteria.GetAttribAndWeight(i, &dwMatchStrength);
// Do we have the attribute? Check the location and any attributes
// inherited from territories and the system.
bool bHasAttrib = (::HasModifier(sAttributes, sAttrib)
|| HasAttribute(vPos, sAttrib));
// Compute the frequency of the given attribute
int iAttribFreq = g_pUniverse->GetAttributeDesc().GetLocationAttribFrequency(sAttrib);
// Adjust probability based on the match strength
int iAdj = CAttributeCriteria::CalcWeightAdj(bHasAttrib, dwMatchStrength, iAttribFreq);
iWeight = iWeight * iAdj / 1000;
// If weight is 0, then no need to continue
if (iWeight == 0)
return 0;
}
// Done
return iWeight;
}
CG32bitPixel CSystem::CalculateSpaceColor (CSpaceObject *pPOV, CSpaceObject **retpStar, const CG8bitSparseImage **retpVolumetricMask)
// CalculateSpaceColor
//
// Calculates the color of space from the given object
{
CSpaceObject *pStar;
int iPercent = CalculateLightIntensity(pPOV->GetPos(), &pStar, retpVolumetricMask);
if (pStar == NULL || iPercent == 0)
return CG32bitPixel::Null();
CG32bitPixel rgbStarColor = pStar->GetSpaceColor();
if (rgbStarColor.IsNull())
return rgbStarColor;
if (retpStar)
*retpStar = pStar;
return CG32bitPixel(rgbStarColor, (BYTE)(MAX_SPACE_OPACITY * iPercent / 100));
}
void CSystem::CalcViewportCtx (SViewportPaintCtx &Ctx, const RECT &rcView, CSpaceObject *pCenter, DWORD dwFlags)
// CalcViewportCtx
//
// Initializes the viewport context
{
DEBUG_TRY
ASSERT(pCenter);
Ctx.pCenter = pCenter;
Ctx.vCenterPos = pCenter->GetPos();
Ctx.rcView = rcView;
Ctx.vDiagonal = CVector(g_KlicksPerPixel * (Metric)(RectWidth(rcView)) / 2,
g_KlicksPerPixel * (Metric)(RectHeight(rcView)) / 2);
Ctx.vUR = Ctx.vCenterPos + Ctx.vDiagonal;
Ctx.vLL = Ctx.vCenterPos - Ctx.vDiagonal;
// Perception
Ctx.iPerception = pCenter->GetPerception();
// Compute the transformation to map world coordinates to the viewport
Ctx.xCenter = rcView.left + RectWidth(rcView) / 2;
Ctx.yCenter = rcView.top + RectHeight(rcView) / 2;
Ctx.XForm = ViewportTransform(Ctx.vCenterPos, g_KlicksPerPixel, Ctx.xCenter, Ctx.yCenter);
Ctx.XFormRel = Ctx.XForm;
// Figure out the extended boundaries. This is used for enhanced display.
Ctx.vEnhancedDiagonal = CVector(g_LRSRange, g_LRSRange);
Ctx.vEnhancedUR = Ctx.vCenterPos + Ctx.vEnhancedDiagonal;
Ctx.vEnhancedLL = Ctx.vCenterPos - Ctx.vEnhancedDiagonal;
// Initialize some flags
Ctx.bEnhancedDisplay = ((dwFlags & VWP_ENHANCED_DISPLAY) ? true : false);
Ctx.bShowUnexploredAnnotation = ((dwFlags & VWP_MINING_DISPLAY) ? true : false);
Ctx.fNoStarfield = ((dwFlags & VWP_NO_STAR_FIELD) ? true : false);
Ctx.fShowManeuverEffects = g_pUniverse->GetSFXOptions().IsManeuveringEffectEnabled();
Ctx.fNoStarshine = !g_pUniverse->GetSFXOptions().IsStarshineEnabled();
Ctx.fNoSpaceBackground = !g_pUniverse->GetSFXOptions().IsSpaceBackgroundEnabled();
// Debug options
Ctx.bShowBounds = g_pUniverse->GetDebugOptions().IsShowBoundsEnabled();
Ctx.bShowFacingsAngle = g_pUniverse->GetDebugOptions().IsShowFacingsAngleEnabled();
// Figure out what color space should be. Space gets lighter as we get
// near the central star
Ctx.rgbSpaceColor = CalculateSpaceColor(pCenter, &Ctx.pStar, &Ctx.pVolumetricMask);
// Compute the radius of the circle on which we'll show target indicators
// (in pixels)
Ctx.rIndicatorRadius = Min(RectWidth(rcView), RectHeight(rcView)) / 2.0;
// If we don't have a thread pool yet, create it
if (m_pThreadPool == NULL)
{
m_pThreadPool = new CThreadPool;
m_pThreadPool->Boot(Min(MAX_THREAD_COUNT, sysGetProcessorCount()));
}
Ctx.pThreadPool = m_pThreadPool;
DEBUG_CATCH
}
void CSystem::CalcVolumetricMask (CSpaceObject *pStar, CG8bitSparseImage &VolumetricMask)
// CalcVolumetricMask
//
// Initializes the volumetric mask for the given star
{
int i;
Metric rMaxDist = (pStar->GetMaxLightDistance() + 100) * LIGHT_SECOND;
int iSize = (int)(2.0 * rMaxDist / g_KlicksPerPixel);
VolumetricMask.Create(iSize, iSize, 0xff);
// Star offset
int xStar = (int)(pStar->GetPos().GetX() / g_KlicksPerPixel);
int yStar = (int)(pStar->GetPos().GetY() / g_KlicksPerPixel);
// Loop over all planets/asteroids and generate a shadow
for (i = 0; i < GetObjectCount(); i++)
{
CSpaceObject *pObj = GetObject(i);
if (pObj == NULL
|| pObj->IsDestroyed()
|| !pObj->HasVolumetricShadow())
continue;
// Compute the angle of the object with respect to the star
// And skip any objects that are outside the star's light radius.
Metric rStarDist;
int iStarAngle = ::VectorToPolar(pObj->GetPos() - pStar->GetPos(), &rStarDist);
if (rStarDist > rMaxDist)
continue;
// Generate an image lit from the proper angle
pObj->CreateStarlightImage(iStarAngle, rStarDist);
// Add the shadow
CVolumetricShadowPainter Painter(pStar, xStar, yStar, iStarAngle, rStarDist, pObj, VolumetricMask);
Painter.PaintShadow();
}
}
void CSystem::CancelTimedEvent (CSpaceObject *pSource, const CString &sEvent, bool bInDoEvent)
// CancelTimedEvent
//
// Cancel event by name
{
m_TimedEvents.CancelEvent(pSource, sEvent, bInDoEvent);
}
void CSystem::CancelTimedEvent (CDesignType *pSource, const CString &sEvent, bool bInDoEvent)
// CancelTimedEvent
//
// Cancel event by name
{
int i;
for (i = 0; i < GetTimedEventCount(); i++)
{
CSystemEvent *pEvent = GetTimedEvent(i);
if (pEvent->GetEventHandlerType() == pSource
&& strEquals(pEvent->GetEventHandlerName(), sEvent))
{
if (bInDoEvent)
pEvent->SetDestroyed();
else
{
m_TimedEvents.RemoveEvent(i);
i--;
}
}
}
}
void CSystem::ComputeRandomEncounters (void)
// ComputeRandomEncounters
//
// Creates the table that lists all objects in the system that
// can generate random encounters
{
int i;
if (m_fEncounterTableValid)
return;
m_EncounterObjs.DeleteAll();
if (!m_fNoRandomEncounters)
{
for (i = 0; i < GetObjectCount(); i++)
{
CSpaceObject *pObj = GetObject(i);
if (pObj
&& pObj->HasRandomEncounters())
m_EncounterObjs.Add(pObj);
}
}
m_fEncounterTableValid = true;
}
void CSystem::ComputeStars (void)
// ComputeStars
//
// Keep a list of the stars in the system
{
int i;
m_Stars.DeleteAll();
for (i = 0; i < GetObjectCount(); i++)
{
CSpaceObject *pObj = GetObject(i);
if (pObj
&& pObj->GetScale() == scaleStar
&& !pObj->IsDestroyed())
{
SStarDesc *pNewStar = m_Stars.Insert();
pNewStar->pStarObj = pObj;
}
}
}
ALERROR CSystem::CreateFlotsam (const CItem &Item,
const CVector &vPos,
const CVector &vVel,
CSovereign *pSovereign,
CStation **retpFlotsam)
// CreateFlotsam
//
// Creates a floating item
{
CItemType *pItemType = Item.GetType();
if (pItemType == NULL)
return ERR_FAIL;
// Create the station
CStation *pFlotsam;
pItemType->CreateEmptyFlotsam(this, vPos, vVel, pSovereign, &pFlotsam);
// Add the items to the station
CItemListManipulator ItemList(pFlotsam->GetItemList());
ItemList.AddItem(Item);
// Done
if (retpFlotsam)
*retpFlotsam = pFlotsam;
return NOERROR;
}
ALERROR CSystem::CreateFromStream (CUniverse *pUniv,
IReadStream *pStream,
CSystem **retpSystem,
CString *retsError,
DWORD dwObjID,
CSpaceObject **retpObj,
CSpaceObject *pPlayerShip)
// CreateFromStream
//
// Creates the star system from the stream
//
// DWORD m_dwID
// DWORD m_iTick
// DWORD m_iTimeStopped
// CString m_sName
// CString Topology node ID
// DWORD (unused)
// DWORD m_iNextEncounter
// DWORD flags
// DWORD SAVE VERSION (only if [flags & 0x02])
// Metric m_rKlicksPerPixel
// Metric m_rTimeScale
// DWORD m_iLastUpdated
//
// DWORD Number of CNavigationPath
// CNavigationPath
//
// CEventHandlers
//
// DWORD Number of mission objects
// CSpaceObject
//
// DWORD Number of objects
// CSpaceObject
//
// DWORD Number of named objects
// CString entrypoint: name
// DWORD entrypoint: CSpaceObject ref
//
// DWORD Number of timed events
// CSystemEvent
//
// DWORD Number of environment maps
// CTileMap
//
// CLocationList m_Locations
// CTerritoryList m_Territories
// CObjectJointList m_Joints
{
int i;
DWORD dwLoad;
DWORD dwCount;
// Create a context block for loading
SLoadCtx Ctx;
Ctx.dwVersion = 0; // Default to 0
Ctx.pStream = pStream;
// Add all missions to the map so that they can be resolved
for (i = 0; i < pUniv->GetMissionCount(); i++)
{
CMission *pMission = pUniv->GetMission(i);
Ctx.ObjMap.AddEntry(pMission->GetID(), pMission);
}
// Create the new star system
Ctx.pSystem = new CSystem(pUniv, NULL);
if (Ctx.pSystem == NULL)
{
*retsError = CONSTLIT("Out of memory.");
return ERR_MEMORY;
}
// Load some misc info
Ctx.pStream->Read((char *)&Ctx.pSystem->m_dwID, sizeof(DWORD));
Ctx.pStream->Read((char *)&Ctx.pSystem->m_iTick, sizeof(DWORD));
Ctx.pStream->Read((char *)&Ctx.pSystem->m_iTimeStopped, sizeof(DWORD));
Ctx.pSystem->m_sName.ReadFromStream(Ctx.pStream);
// Load the topology node
CString sNodeID;
sNodeID.ReadFromStream(Ctx.pStream);
Ctx.pSystem->m_pTopology = pUniv->FindTopologyNode(sNodeID);
Ctx.pSystem->m_pType = pUniv->FindSystemType(Ctx.pSystem->m_pTopology->GetSystemTypeUNID());
// More misc info
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
Ctx.pStream->Read((char *)&Ctx.pSystem->m_iNextEncounter, sizeof(DWORD));
// Flags
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
Ctx.pSystem->m_fNoRandomEncounters = ((dwLoad & 0x00000001) ? true : false);
if (dwLoad & 0x00000002)
Ctx.pStream->Read((char *)&Ctx.dwVersion, sizeof(DWORD));
Ctx.pSystem->m_fUseDefaultTerritories = ((dwLoad & 0x00000004) ? false : true);
Ctx.pSystem->m_fEncounterTableValid = false;
Ctx.pSystem->m_fEnemiesInLRS = ((dwLoad & 0x00000008) ? false : true);
Ctx.pSystem->m_fEnemiesInSRS = ((dwLoad & 0x00000010) ? false : true);
Ctx.pSystem->m_fPlayerUnderAttack = ((dwLoad & 0x00000020) ? false : true);
if (Ctx.dwVersion >= 141)
Ctx.pSystem->m_fLocationsBlocked = ((dwLoad & 0x00000040) ? false : true);
else
{
// For previous versions we always assume we've already processed
// locations.
Ctx.pSystem->m_fLocationsBlocked = true;
}
// Scales
if (Ctx.dwVersion >= 9)
{
Ctx.pStream->Read((char *)&Ctx.pSystem->m_rKlicksPerPixel, sizeof(Metric));
Ctx.pStream->Read((char *)&Ctx.pSystem->m_rTimeScale, sizeof(Metric));
}
else
{
Ctx.pSystem->m_rKlicksPerPixel = KLICKS_PER_PIXEL;
Ctx.pSystem->m_rTimeScale = TIME_SCALE;
}
if (Ctx.dwVersion >= 57)
Ctx.pStream->Read((char *)&Ctx.pSystem->m_iLastUpdated, sizeof(DWORD));
else
Ctx.pSystem->m_iLastUpdated = -1;
// Load the navigation paths (we load these before objects
// because objects might have references to paths)
if (Ctx.dwVersion >= 10)
Ctx.pSystem->m_NavPaths.ReadFromStream(Ctx);
// Load the system event handlers
if (Ctx.dwVersion >= 35)
Ctx.pSystem->m_EventHandlers.ReadFromStream(Ctx);
// Load all objects
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
for (i = 0; i < (int)dwCount; i++)
{
// Load the object
CSpaceObject *pObj;
try
{
CSpaceObject::CreateFromStream(Ctx, &pObj);
}
catch (...)
{
*retsError = CSpaceObject::DebugLoadError(Ctx);
return ERR_FAIL;
}
// Add this object to the map
DWORD dwID = (Ctx.dwVersion >= 41 ? pObj->GetID() : pObj->GetIndex());
Ctx.ObjMap.AddEntry(dwID, pObj);
// Update any previous objects that are waiting for this reference
Ctx.ForwardReferences.ResolveRefs(dwID, pObj);
// Set the system (note: this will change the index to the new
// system)
pObj->AddToSystem(Ctx.pSystem, true);
}
// If we have old style registrations then we need to convert to subscriptions
if (Ctx.dwVersion < 77)
{
for (i = 0; i < Ctx.pSystem->GetObjectCount(); i++)
{
CSpaceObject *pObj = Ctx.pSystem->GetObject(i);
if (pObj)
{
TArray<CSpaceObject *> *pList = Ctx.Subscribed.GetAt(pObj->GetID());
if (pList)
{
for (int j = 0; j < pList->GetCount(); j++)
pObj->AddEventSubscriber(pList->GetAt(j));
}
}
}
}
// If there are still references to the player, resolve them now.
// [This happens because of a bug in 1.0 RC1]
if (pPlayerShip)
Ctx.ForwardReferences.ResolveRefs(pPlayerShip->GetID(), pPlayerShip);
// If we've got left over references, then dump debug output
if (Ctx.ForwardReferences.HasUnresolved())
{