-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCreateSystem.cpp
4602 lines (3597 loc) · 118 KB
/
CreateSystem.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
// CreateSystem.cpp
//
// CreateSystem class
#include "PreComp.h"
#include "math.h"
#define ALWAYS_SEPARATE_ENEMIES
#ifdef DEBUG
//#define DEBUG_STATION_TABLE_CACHE
//#define DEBUG_STRESS_TEST
//#define DEBUG_STATION_TABLES
//#define DEBUG_STATION_PLACEMENT
#endif
#define ADD_ATTRIBUTE_TAG CONSTLIT("AddAttribute")
#define ADD_TERRITORY_TAG CONSTLIT("AddTerritory")
#define ANTI_TROJAN_TAG CONSTLIT("AntiTrojan")
#define CODE_TAG CONSTLIT("Code")
#define ENCOUNTER_GROUP_TAG CONSTLIT("EncounterGroup")
#define ESCORTS_TAG CONSTLIT("Escorts")
#define EVENTS_TAG CONSTLIT("Events")
#define FILL_LOCATIONS_TAG CONSTLIT("FillLocations")
#define FILL_RANDOM_LOCATION_TAG CONSTLIT("FillRandomLocation")
#define GROUP_TAG CONSTLIT("Group")
#define INITIAL_DATA_TAG CONSTLIT("InitialData")
#define ITEM_TAG CONSTLIT("Item")
#define ITEMS_TAG CONSTLIT("Items")
#define LABEL_TAG CONSTLIT("Label")
#define LEVEL_TABLE_TAG CONSTLIT("LevelTable")
#define LOCATION_CRITERIA_TABLE_TAG CONSTLIT("LocationCriteriaTable")
#define LOOKUP_TAG CONSTLIT("Lookup")
#define MARKER_TAG CONSTLIT("Marker")
#define NULL_TAG CONSTLIT("Null")
#define OFFSET_TAG CONSTLIT("Offset")
#define ON_CREATE_TAG CONSTLIT("OnCreate")
#define ORBITAL_DISTRIBUTION_TAG CONSTLIT("OrbitalDistribution")
#define ORBITALS_TAG CONSTLIT("Orbitals")
#define PARTICLES_TAG CONSTLIT("Particles")
#define PLACE_RANDOM_STATION_TAG CONSTLIT("PlaceRandomStation")
#define PRIMARY_TAG CONSTLIT("Primary")
#define RANDOM_LOCATION_TAG CONSTLIT("RandomLocation")
#define RANDOM_STATION_TAG CONSTLIT("RandomStation")
#define SATELLITES_TAG CONSTLIT("Satellites")
#define SHIP_TAG CONSTLIT("Ship")
#define SHIPS_TAG CONSTLIT("Ships")
#define SIBLINGS_TAG CONSTLIT("Siblings")
#define SPACE_ENVIRONMENT_TAG CONSTLIT("SpaceEnvironment")
#define STARGATE_TAG CONSTLIT("Stargate")
#define STATION_TAG CONSTLIT("Station")
#define SYSTEM_GROUP_TAG CONSTLIT("SystemGroup")
#define TABLE_TAG CONSTLIT("Table")
#define TABLES_TAG CONSTLIT("Tables")
#define TRADE_TAG CONSTLIT("Trade")
#define TROJAN_TAG CONSTLIT("Trojan")
#define VARIANT_TAG CONSTLIT("Variant")
#define VARIANTS_TAG CONSTLIT("Variants")
#define VARIANTS_TABLE_TAG CONSTLIT("VariantTable")
#define ANGLE_ATTRIB CONSTLIT("angle")
#define ANGLE_ADJ_ATTRIB CONSTLIT("angleAdj")
#define ANGLE_INC_ATTRIB CONSTLIT("angleInc")
#define ARC_INC_ATTRIB CONSTLIT("arcInc")
#define ARC_LENGTH_ATTRIB CONSTLIT("arcLength")
#define ATTRIBUTES_ATTRIB CONSTLIT("attributes")
#define BACKGROUND_PLANE_ATTRIB CONSTLIT("backgroundPlane")
#define BODE_DISTANCE_END_ATTRIB CONSTLIT("BodeDistanceEnd")
#define BODE_DISTANCE_START_ATTRIB CONSTLIT("BodeDistanceStart")
#define CHANCE_ATTRIB CONSTLIT("chance")
#define CLASS_ATTRIB CONSTLIT("class")
#define CONTROLLER_ATTRIB CONSTLIT("controller")
#define COUNT_ATTRIB CONSTLIT("count")
#define CRITERIA_ATTRIB CONSTLIT("criteria")
#define DEBUG_ONLY_ATTRIB CONSTLIT("debugOnly")
#define DISTANCE_ATTRIB CONSTLIT("distance")
#define DISTRIBUTION_ATTRIB CONSTLIT("distribution")
#define ECCENTRICITY_ATTRIB CONSTLIT("eccentricity")
#define ENCOUNTERS_ATTRIB CONSTLIT("encountersCount")
#define ERODE_ATTRIB CONSTLIT("erode")
#define EVENT_HANDLER_ATTRIB CONSTLIT("eventHandler")
#define EXCLUSION_RADIUS_ATTRIB CONSTLIT("exclusionRadius")
#define ID_ATTRIB CONSTLIT("id")
#define IMAGE_VARIANT_ATTRIB CONSTLIT("imageVariant")
#define INCLUDE_ALL_ATTRIB CONSTLIT("includeAll")
#define INTERVAL_ATTRIB CONSTLIT("interval")
#define LEVEL_FREQUENCY_ATTRIB CONSTLIT("levelFrequency")
#define LOCATION_ATTRIBS_ATTRIB CONSTLIT("locationAttribs")
#define LOCATION_CRITERIA_ATTRIB CONSTLIT("locationCriteria")
#define MATCH_ATTRIB CONSTLIT("match")
#define MAX_ATTRIB CONSTLIT("max")
#define MAX_DIST_ATTRIB CONSTLIT("maxDist")
#define MAX_RADIUS_ATTRIB CONSTLIT("maxRadius")
#define MAX_SHIPS_ATTRIB CONSTLIT("maxShips")
#define MIN_ATTRIB CONSTLIT("min")
#define MIN_DIST_ATTRIB CONSTLIT("minDist")
#define MIN_RADIUS_ATTRIB CONSTLIT("minRadius")
#define NAME_ATTRIB CONSTLIT("name")
#define NO_CONSTRUCTION_ATTRIB CONSTLIT("noConstruction")
#define NO_MAP_LABEL_ATTRIB CONSTLIT("noMapLabel")
#define NO_OVERLAP_ATTRIB CONSTLIT("noOverlap")
#define NO_RANDOM_ENCOUNTERS_ATTRIB CONSTLIT("noRandomEncounters")
#define NO_REINFORCEMENTS_ATTRIB CONSTLIT("noReinforcements")
#define NO_SATELLITES_ATTRIB CONSTLIT("noSatellites")
#define OBJ_NAME_ATTRIB CONSTLIT("objName")
#define OFFSET_ATTRIB CONSTLIT("offset")
#define ORDERS_ATTRIB CONSTLIT("orders")
#define OVERLAP_CHECK_ATTRIB CONSTLIT("overlapCheck")
#define PAINT_LAYER_ATTRIB CONSTLIT("paintLayer")
#define PATCHES_ATTRIB CONSTLIT("patchType")
#define PATCH_FREQUENCY_ATTRIB CONSTLIT("patchFrequency")
#define PERCENT_ENEMIES_ATTRIB CONSTLIT("percentEnemies")
#define PERCENT_FULL_ATTRIB CONSTLIT("percentFull")
#define PROBABILITY_ATTRIB CONSTLIT("probability")
#define RADIAL_EDGE_WIDTH_ATTRIB CONSTLIT("radialEdgeWidth")
#define RADIAL_WIDTH_ATTRIB CONSTLIT("radialWidth")
#define RADIUS_ATTRIB CONSTLIT("radius")
#define RADIUS_DEC_ATTRIB CONSTLIT("radiusDec")
#define RADIUS_INC_ATTRIB CONSTLIT("radiusInc")
#define ROTATION_ATTRIB CONSTLIT("rotation")
#define SEGMENT_ATTRIB CONSTLIT("segment")
#define SEPARATE_ENEMIES_ATTRIB CONSTLIT("separateEnemies")
#define SHAPE_ATTRIB CONSTLIT("shape")
#define SHOW_ORBIT_ATTRIB CONSTLIT("showOrbit")
#define SOVEREIGN_ATTRIB CONSTLIT("sovereign")
#define SPACE_SCALE_ATTRIB CONSTLIT("spaceScale")
#define SPAN_ATTRIB CONSTLIT("span")
#define STARGATE_ATTRIB CONSTLIT("stargate")
#define STATION_CRITERIA_ATTRIB CONSTLIT("stationCriteria")
#define TABLE_ATTRIB CONSTLIT("table")
#define TIME_SCALE_ATTRIB CONSTLIT("timeScale")
#define TYPE_ATTRIB CONSTLIT("type")
#define VARIANT_ATTRIB CONSTLIT("variant")
#define VARIANT_LOCATION_CRITERIA_ATTRIB CONSTLIT("variantLocationCriteria")
#define WIDTH_ATTRIB CONSTLIT("width")
#define WIDTH_VARIATION_ATTRIB CONSTLIT("widthVariation")
#define WRECK_TYPE_ATTRIB CONSTLIT("wreckType")
#define X_OFFSET_ATTRIB CONSTLIT("xOffset")
#define Y_OFFSET_ATTRIB CONSTLIT("yOffset")
#define SPECIAL_ATTRIB_INNER_SYSTEM CONSTLIT("innerSystem")
#define SPECIAL_ATTRIB_OUTER_SYSTEM CONSTLIT("outerSystem")
#define SPECIAL_ATTRIB_LIFE_ZONE CONSTLIT("lifeZone")
#define SPECIAL_ATTRIB_NEAR_FRIENDS CONSTLIT("nearFriends")
#define SPECIAL_ATTRIB_NEAR_ENEMIES CONSTLIT("nearEnemies")
#define SPECIAL_ATTRIB_NEAR_STATIONS CONSTLIT("nearStations")
#define RANDOM_ANGLE CONSTLIT("random")
#define EQUIDISTANT_ANGLE CONSTLIT("equidistant")
#define INCREMENTING_ANGLE CONSTLIT("incrementing")
#define MIN_SEPARATION_ANGLE CONSTLIT("minSeparation")
#define SHAPE_CIRCULAR CONSTLIT("circular")
#define SHAPE_ARC CONSTLIT("arc")
#define TYPE_NEBULA CONSTLIT("nebula")
#define MATCH_ALL CONSTLIT("*")
#define ENEMY_ATTRIBUTE CONSTLIT("enemy")
#define FRIEND_ATTRIBUTE CONSTLIT("friendly")
#define REQUIRE_ENEMY CONSTLIT("*enemy")
#define REQUIRE_FRIEND CONSTLIT("*friendly")
#define ON_CREATE_EVENT CONSTLIT("OnCreate")
// Minimum distance that two enemy stations can be (in light-seconds)
#define MIN_ENEMY_DIST 30
#define MAX_NEBULAE 100000
#define OVERLAP_DIST (25.0 * LIGHT_SECOND)
const Metric DEFAULT_EXCLUSION = 50.0 * LIGHT_SECOND;
const Metric DEFAULT_EXCLUSION2 = DEFAULT_EXCLUSION * DEFAULT_EXCLUSION;
constexpr int FILL_LOCATIONS_MAX_TRIES = 10;
constexpr int RANDOM_ANGLE_MAX_TRIES = 20;
constexpr int DIST_ANGLE_MAX_TRIES = 10;
constexpr int PLACE_RANDOM_STATION_MAX_TRIES = 10;
constexpr int RANDOM_POSITION_MAX_TRIES = 100;
static char g_ProbabilitiesAttrib[] = "probabilities";
// Debugging Support
#ifdef DEBUG_STRESS_TEST
#define STRESS_ITERATIONS 50
#ifdef DEBUG_STRING_LEAKS
#define START_STRING_LEAK_TEST CString::DebugMark();
#define STOP_STRING_LEAK_TEST CString::DebugOutputLeakedStrings(); \
char szBuffer[1024]; \
wsprintf(szBuffer, "Total Strings: %d\n", CString::DebugGetStringCount()); \
::OutputDebugString(szBuffer);
#else
#define START_STRING_LEAK_TEST
#define STOP_STRING_LEAK_TEST
#endif
#define START_STRESS_TEST \
for (int k = 0; k < STRESS_ITERATIONS; k++) \
{ \
START_STRING_LEAK_TEST
#define STOP_STRESS_TEST \
if (k < (STRESS_ITERATIONS-1)) \
{ \
delete pSystem; \
STOP_STRING_LEAK_TEST \
} \
}
#else
#define START_STRESS_TEST
#define STOP_STRESS_TEST
#endif
#ifdef DEBUG_STATION_PLACEMENT
#define STATION_PLACEMENT_OUTPUT(x) ::OutputDebugString(x)
#else
#define STATION_PLACEMENT_OUTPUT(x)
#endif
// Classes and structures
ALERROR AddAttribute (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc);
ALERROR AddTerritory (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc);
bool CheckForOverlap (SSystemCreateCtx *pCtx, const CVector &vPos);
ALERROR ChooseRandomLocation (SSystemCreateCtx *pCtx,
const SLocationCriteria &Criteria,
const COrbit &OrbitDesc,
CStationType *pStationToPlace,
COrbit *retOrbitDesc,
CString *retsAttribs,
int *retiLabelPos);
ALERROR ChooseRandomLocation (SSystemCreateCtx *pCtx,
const CString &sCriteria,
CStationType *pStationToPlace,
COrbit *retOrbitDesc,
CString *retsAttribs,
int *retiLabelPos);
int ComputeLocationWeight (SSystemCreateCtx *pCtx,
const CString &sLocationAttribs,
const CVector &vPos,
const CString &sAttrib,
DWORD dwMatchStrength);
const COrbit *ComputeOffsetOrbit (CXMLElement *pObj, const COrbit &Original, COrbit *retOrbit);
int ComputeStationWeight (SSystemCreateCtx *pCtx, CStationType *pType, const CString &sAttrib, DWORD dwMatchStrength);
ALERROR CreateAppropriateStationAtRandomLocation (SSystemCreateCtx *pCtx,
TProbabilityTable<int> &LocationTable,
const CString &sStationCriteria,
bool bSeparateEnemies,
bool *retbEnemy = NULL);
ALERROR CreateArcDistribution (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc);
ALERROR CreateLabel (SSystemCreateCtx *pCtx,
CXMLElement *pObj,
const COrbit &OrbitDesc);
ALERROR CreateLevelTable (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR CreateLocationCriteriaTable (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR CreateObjectAtRandomLocation (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR CreateOffsetObjects (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc);
ALERROR CreateOrbitals (SSystemCreateCtx *pCtx,
CXMLElement *pObj,
const COrbit &OrbitDesc);
ALERROR CreateRandomStation (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR CreateRandomStationAtAppropriateLocation (SSystemCreateCtx *pCtx, CXMLElement *pDesc);
ALERROR CreateSatellites (SSystemCreateCtx *pCtx, CSpaceObject *pStation, CXMLElement *pSatellites, const COrbit &OrbitDesc);
ALERROR CreateShipsForStation (CSpaceObject *pStation, CXMLElement *pShips);
ALERROR CreateSiblings (SSystemCreateCtx *pCtx,
CXMLElement *pObj,
const COrbit &OrbitDesc);
ALERROR CreateSpaceEnvironment (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
void CreateSpaceEnvironmentTile (SSystemCreateCtx *pCtx,
const CVector &vPos,
int xTile,
int yTile,
CSpaceEnvironmentType *pEnvironment,
CEffectCreator *pPatch,
int iPatchFrequency);
ALERROR CreateStargate (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR CreateStationFromElement (SSystemCreateCtx *pCtx,
CXMLElement *pDesc,
const COrbit &OrbitDesc,
CStation **retpStation = NULL);
ALERROR CreateSystemObject (SSystemCreateCtx *pCtx,
CXMLElement *pObj,
const COrbit &OrbitDesc,
bool bIgnoreChance = false);
ALERROR CreateVariantsTable (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc);
ALERROR GenerateAngles (SSystemCreateCtx *pCtx, const CString &sAngle, int iCount, Metric *pAngles, int iJitter = 0);
void DumpDebugStack (SSystemCreateCtx *pCtx);
void GenerateRandomPosition (SSystemCreateCtx *pCtx, CStationType *pStationToPlace, COrbit *retOrbit);
ALERROR GenerateRandomStationTable (SSystemCreateCtx *pCtx,
const CString &sCriteria,
const CString &sLocationAttribs,
const CVector &vPos,
bool bIncludeAll,
TArray<CStationTableCache::SEntry> **retpTable,
bool *retbAddToCache);
ALERROR GetLocationCriteria (SSystemCreateCtx *pCtx, CXMLElement *pDesc, SLocationCriteria *retCriteria);
bool IsExclusionZoneClear (SSystemCreateCtx *pCtx, const CVector &vPos, Metric rRadius);
ALERROR ModifyCreatedStation (SSystemCreateCtx *pCtx, CStation *pStation, CXMLElement *pDesc, const COrbit &OrbitDesc);
inline void PopDebugStack (SSystemCreateCtx *pCtx) { if (g_pUniverse->InDebugMode()) pCtx->DebugStack.Pop(); }
inline void PushDebugStack (SSystemCreateCtx *pCtx, const CString &sLine) { if (g_pUniverse->InDebugMode()) pCtx->DebugStack.Push(sLine); }
// Helper functions
ALERROR AddAttribute (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc)
// AddAttribute
//
// Adds one or more attributes to the system/node
{
// Define the attributes
CString sAttribs;
if (pObj->FindAttribute(ATTRIBUTES_ATTRIB, &sAttribs))
pCtx->pTopologyNode->AddAttributes(sAttribs);
return NOERROR;
}
ALERROR AddTerritory (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc)
// AddTerritory
//
// Creates a simple territory
{
ALERROR error;
// Load the territory
CTerritoryDef *pTerritory;
if (error = CTerritoryDef::CreateFromXML(pObj, OrbitDesc, &pTerritory))
return error;
// Add to system
if (error = pCtx->pSystem->AddTerritory(pTerritory))
return error;
return NOERROR;
}
ALERROR ChooseRandomLocation (SSystemCreateCtx *pCtx,
const SLocationCriteria &Criteria,
const COrbit &OrbitDesc,
CStationType *pStationToPlace,
COrbit *retOrbitDesc,
CString *retsAttribs,
int *retiLabelPos)
// ChooseRandomLocation
//
// Returns the orbital position for a random label that
// matches the desired characteristics in sCriteria. If ERR_NOTFOUND
// is returned then it means that a label of that characteristic could
// not be found.
//
// If pStationToPlace is specified then we make sure that we don't pick
// a location near enemies of the station.
//
// If retiLabelPos is passed-in then we do not automatically remove the label
// from the list.
{
STATION_PLACEMENT_OUTPUT("ChooseRandomLocation\n");
// Choose a random location
int iLocID;
if (!pCtx->pSystem->FindRandomLocation(Criteria, 0, OrbitDesc, pStationToPlace, &iLocID))
return ERR_NOTFOUND;
// Return info
if (retOrbitDesc || retsAttribs)
{
CLocationDef *pLoc = pCtx->pSystem->GetLocation(iLocID);
if (retOrbitDesc)
*retOrbitDesc = pLoc->GetOrbit();
if (retsAttribs)
*retsAttribs = pLoc->GetAttributes();
}
if (retiLabelPos)
*retiLabelPos = iLocID;
#ifdef DEBUG_STATION_PLACEMENT
{
char szBuffer[1024];
wsprintf(szBuffer, " found random location (%s)\n", retsAttribs->GetASCIIZPointer());
::OutputDebugString(szBuffer);
}
#endif
return NOERROR;
}
ALERROR ChooseRandomLocation (SSystemCreateCtx *pCtx,
const CString &sCriteria,
CStationType *pStationToPlace,
COrbit *retOrbitDesc,
CString *retsAttribs,
int *retiLabelPos)
// ChooseRandomLocation
//
// Returns the orbital position for a random label that
// matches the desired characteristics in sCriteria. If ERR_NOTFOUND
// is returned then it means that a label of that characteristic could
// not be found.
//
// If pStationToPlace is specified then we make sure that we don't pick
// a location near enemies of the station.
//
// If retiLabelPos is passed-in then we do not automatically remove the label
// from the list.
{
ALERROR error;
// Parse the criteria
SLocationCriteria Criteria;
if (error = Criteria.AttribCriteria.Parse(sCriteria, 0, &pCtx->sError))
return error;
// Choose a random location
return ChooseRandomLocation(pCtx, Criteria, COrbit(), pStationToPlace, retOrbitDesc, retsAttribs, retiLabelPos);
}
ALERROR ChooseRandomStation (SSystemCreateCtx *pCtx,
const CString &sCriteria,
const CString &sLocationAttribs,
const CVector &vPos,
bool bSeparateEnemies,
bool bIncludeAll,
CStationType **retpType,
TArray<CStationTableCache::SEntry> **retpTable = NULL)
// ChooseRandomStation
//
// Picks a random station to create. The station is appropriate to the level
// of the system and to the given criteria.
{
ALERROR error;
int i;
// Generate a description of the table that we are about to generate
// to see if we've already got this table in the cache.
CString sTableDesc = strPatternSubst(CONSTLIT("%s|||%s|||%s|||%s"),
sCriteria,
sLocationAttribs,
pCtx->pSystem->GetAttribsAtPos(vPos),
(bIncludeAll ? CONSTLIT("ALL") : NULL_STR));
// If this table is not cached then generate it.
bool bFreeTable = false;
TArray<CStationTableCache::SEntry> *pTable;
if (!pCtx->StationTables.FindTable(sTableDesc, &pTable))
{
bool bAddToCache;
if (error = GenerateRandomStationTable(pCtx, sCriteria, sLocationAttribs, vPos, bIncludeAll, &pTable, &bAddToCache))
return error;
if (bAddToCache)
pCtx->StationTables.AddTable(sTableDesc, pTable);
else
bFreeTable = true;
}
// Add to stats
if (pCtx->pStats)
pCtx->pStats->AddStationTable(pCtx->pSystem, sCriteria, ::AppendModifiers(sLocationAttribs, pCtx->pSystem->GetAttribsAtPos(vPos)), *pTable);
// Now generate a probability table and add all the entries
TProbabilityTable<CStationType *> Table;
for (i = 0; i < pTable->GetCount(); i++)
{
const CStationTableCache::SEntry &Entry = pTable->GetAt(i);
// Make sure we can still encounter this type. [If we've already created
// a system-unique object then it will still be in the cached tables.]
if (!Entry.pType->CanBeEncountered(pCtx->pSystem))
continue;
// If we want to separate enemies, then see if there are any
// enemies of this station type at this location.
if (bSeparateEnemies && !pCtx->pSystem->IsExclusionZoneClear(vPos, Entry.pType))
continue;
// Add it
Table.Insert(Entry.pType, Entry.iChance);
}
// Returns the original table if desired for debugging purposes. Note, however, that
// we only return it if we add it to the cache (otherwise, it will get freed).
if (retpTable)
*retpTable = (!bFreeTable ? pTable : NULL);
// Short-circuit
if (Table.GetCount() == 0)
{
if (bFreeTable)
delete pTable;
STATION_PLACEMENT_OUTPUT(" no appropriate station found for this location\n");
return ERR_NOTFOUND;
}
// Roll
*retpType = Table.GetAt(Table.RollPos());
if (bFreeTable)
delete pTable;
return NOERROR;
}
const COrbit *ComputeOffsetOrbit (CXMLElement *pObj, const COrbit &Original, COrbit *retOrbit)
// ComputeOffsetOrbit
//
// If the element has xOffset and yOffset attributes, it generates a new orbit
{
int xOffset;
if (!pObj->FindAttributeInteger(X_OFFSET_ATTRIB, &xOffset))
return &Original;
int yOffset = pObj->GetAttributeInteger(Y_OFFSET_ATTRIB);
if (xOffset == 0 && yOffset == 0)
return &Original;
Metric rRadius;
Metric rAngle = VectorToPolarRadians(CVector(xOffset * g_KlicksPerPixel, yOffset * g_KlicksPerPixel), &rRadius);
*retOrbit = COrbit(Original.GetObjectPos(), rRadius, rAngle);
return retOrbit;
}
int ComputeLocationWeight (SSystemCreateCtx *pCtx,
const CString &sLocationAttribs,
const CVector &vPos,
const CString &sAttrib,
DWORD dwMatchStrength)
// ComputeLocationWeight
//
// Computes the weight of the given location if we're looking for
// locations with the given criteria.
{
return CAttributeCriteria::CalcLocationWeight(pCtx->pSystem, sLocationAttribs, vPos, sAttrib, dwMatchStrength);
}
int ComputeStationWeight (SSystemCreateCtx *pCtx, CStationType *pType, const CString &sAttrib, DWORD dwMatchStrength)
// ComputeStationWeight
//
// Returns the weight of this station type given the attribute and match weight
{
return CAttributeCriteria::CalcWeightAdj(pType->HasAttribute(sAttrib), dwMatchStrength);
}
ALERROR DistributeStationsAtRandomLocations (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc)
// DistributeStationsAtRandomLocations
//
// Fills several locations with random stations
{
ALERROR error;
int i;
STATION_PLACEMENT_OUTPUT("DistributeStationsAtRandomLocations\n");
// Load location criteria
SLocationCriteria LocationCriteria;
if (error = GetLocationCriteria(pCtx, pDesc, &LocationCriteria))
return error;
// Create a list of appropriate locations
TProbabilityTable<int> LocationTable;
if (!pCtx->pSystem->GetEmptyLocations(LocationCriteria, OrbitDesc, NULL, &LocationTable))
{
if (g_pUniverse->InDebugMode())
{
PushDebugStack(pCtx, strPatternSubst(CONSTLIT("FillLocations locationCriteria=%s"), pDesc->GetAttribute(LOCATION_CRITERIA_ATTRIB)));
g_pUniverse->LogOutput(CONSTLIT("Warning: No locations found."));
DumpDebugStack(pCtx);
PopDebugStack(pCtx);
}
return NOERROR;
}
// Figure out how many stations to create
int iLocationCount = LocationTable.GetCount();
int iCount;
int iPercent;
if (pDesc->FindAttributeInteger(PERCENT_FULL_ATTRIB, &iPercent))
iCount = iPercent * iLocationCount / 100;
else
iCount = GetDiceCountFromAttribute(pDesc->GetAttribute(COUNT_ATTRIB));
// Figure out how many friends and enemies we need to create
int iEnemies = 0;
int iFriends = 0;
int iPercentEnemies;
if (pDesc->FindAttributeInteger(PERCENT_ENEMIES_ATTRIB, &iPercentEnemies))
{
for (i = 0; i < iCount; i++)
{
if (mathRandom(1, 100) <= iPercentEnemies)
iEnemies++;
else
iFriends++;
}
}
else
{
iFriends = iCount;
iEnemies = iCount;
}
// Station criteria
CString sStationCriteria = pDesc->GetAttribute(STATION_CRITERIA_ATTRIB);
CString sEnemyOnlyCriteria = (sStationCriteria.IsBlank() ? REQUIRE_ENEMY : strPatternSubst(CONSTLIT("%s,%s"), sStationCriteria, REQUIRE_ENEMY));
CString sFriendOnlyCriteria = (sStationCriteria.IsBlank() ? REQUIRE_FRIEND : strPatternSubst(CONSTLIT("%s,%s"), sStationCriteria, REQUIRE_FRIEND));
#ifdef ALWAYS_SEPARATE_ENEMIES
bool bSeparateEnemies = true;
#else
bool bSeparateEnemies = pDesc->GetAttributeBool(SEPARATE_ENEMIES_ATTRIB);
#endif
PushDebugStack(pCtx, strPatternSubst(CONSTLIT("FillLocations locationCriteria=%s stationCriteria=%s"), pDesc->GetAttribute(LOCATION_CRITERIA_ATTRIB), sStationCriteria));
// If required, calculate stats
if (pCtx->pStats && bSeparateEnemies)
pCtx->pStats->AddFillLocationsTable(pCtx->pSystem, LocationTable, sStationCriteria);
// Create the stations
for (i = 0; i < iCount; i++)
{
// Create
if (iEnemies && iFriends)
{
bool bEnemy;
if (error = CreateAppropriateStationAtRandomLocation(pCtx, LocationTable, sStationCriteria, bSeparateEnemies, &bEnemy))
{
if (error == ERR_NOTFOUND)
continue;
return error;
}
if (bEnemy)
iEnemies--;
else
iFriends--;
}
else if (iEnemies)
{
if (error = CreateAppropriateStationAtRandomLocation(pCtx, LocationTable, sEnemyOnlyCriteria, bSeparateEnemies))
{
if (error == ERR_NOTFOUND)
continue;
return error;
}
iEnemies--;
}
else if (iFriends)
{
if (error = CreateAppropriateStationAtRandomLocation(pCtx, LocationTable, sFriendOnlyCriteria, bSeparateEnemies))
{
if (error == ERR_NOTFOUND)
continue;
return error;
}
iFriends--;
}
// If no more locations then we're done
if (LocationTable.GetCount() == 0)
break;
}
// See how many locations we actually filled. If we filled fewer locations
// than expected, then report the actual number.
#ifdef DEBUG_FILL_LOCATIONS
int iFilled = iLocationCount - LocationTable.GetCount();
if (iFilled < iCount
&& g_pUniverse->InDebugMode()
&& iLocationCount > 0)
{
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("%s: Only filled %d of %d locations (%d%%)"), pCtx->pTopologyNode->GetID(), iFilled, iLocationCount, iFilled * 100 / iLocationCount));
}
#endif
PopDebugStack(pCtx);
return NOERROR;
}
ALERROR CreateAppropriateStationAtRandomLocation (SSystemCreateCtx *pCtx,
TProbabilityTable<int> &LocationTable,
const CString &sStationCriteria,
bool bSeparateEnemies,
bool *retbEnemy)
// CreateAppropriateStationAtRandomLocation
//
// Picks a random location and fills it with a randomly chosen station approriate
// to the location.
{
ALERROR error;
STATION_PLACEMENT_OUTPUT("CreateAppropriateStationAtRandomLocation\n");
// Keep trying for a while to make sure that we find something that fits
int iTries = FILL_LOCATIONS_MAX_TRIES;
while (iTries > 0)
{
STATION_PLACEMENT_OUTPUT(strPatternSubst(CONSTLIT("try %d\n"), (FILL_LOCATIONS_MAX_TRIES + 1) - iTries).GetASCIIZPointer());
// Pick a random location that fits the criteria
int iTablePos = LocationTable.RollPos();
if (iTablePos == -1)
return NOERROR;
int iLocID = LocationTable[iTablePos];
CLocationDef *pLoc = pCtx->pSystem->GetLocation(iLocID);
// Now look for the most appropriate station to place at the location
CStationType *pType;
TArray<CStationTableCache::SEntry> *pStationTable;
if (error = ChooseRandomStation(pCtx,
sStationCriteria,
pLoc->GetAttributes(),
pLoc->GetOrbit().GetObjectPos(),
bSeparateEnemies,
false,
&pType,
&pStationTable))
{
// If we couldn't find an appropriate location then try picking
// a different location.
if (error == ERR_NOTFOUND)
{
iTries--;
// No more tries
if (iTries == 0)
return ERR_NOTFOUND;
#ifdef DEBUG_FILL_LOCATIONS
if (iTries == 0 && g_pUniverse->InDebugMode())
{
g_pUniverse->LogOutput(CONSTLIT("Warning: Ran out of tries in FillLocations"));
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("Level: %d; Loc attribs: %s; Node attribs: %s"),
pCtx->pTopologyNode->GetLevel(),
pLoc->GetAttributes(),
pCtx->pTopologyNode->GetAttributes()));
// If we have a station table, then output it, so we can debug what happened.
if (pStationTable)
{
if (pStationTable->GetCount() == 0)
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("NO ENTRIES IN STATION TABLE: %s"), sStationCriteria));
else
{
for (int i = 0; i < pStationTable->GetCount(); i++)
{
const CStationTableCache::SEntry &Entry = pStationTable->GetAt(i);
// Write out the station and its encounter chance:
if (!Entry.pType->CanBeEncountered(pCtx->pSystem))
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("%s: ALREADY ENCOUNTERED"), Entry.pType->GetNounPhrase()));
// If we want to separate enemies, then see if there are any
// enemies of this station type at this location.
else if (bSeparateEnemies && !pCtx->pSystem->IsExclusionZoneClear(pLoc->GetOrbit().GetObjectPos(), Entry.pType))
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("%s: ENEMIES NEARBY"), Entry.pType->GetNounPhrase()));
else
g_pUniverse->LogOutput(strPatternSubst(CONSTLIT("%s: %d%% chance"), Entry.pType->GetNounPhrase(), Entry.iChance));
}
}
}
DumpDebugStack(pCtx);
}
#endif
continue;
}
else
return error;
}
// Remember if this is friend or enemy
if (retbEnemy)
*retbEnemy = pType->HasLiteralAttribute(ENEMY_ATTRIBUTE);
// Remember object created
DWORD dwSavedLastObjID = pCtx->dwLastObjID;
pCtx->dwLastObjID = 0;
#ifdef DEBUG_CREATE_SYSTEM
CString sLocAttribs = pLoc->GetAttributes();
CString sPosAttribs = pCtx->pSystem->GetAttribsAtPos(pLoc->GetOrbit().GetObjectPos());
#endif
// Create the station at the location
SObjCreateCtx CreateCtx(*pCtx);
CreateCtx.vPos = pLoc->GetOrbit().GetObjectPos();
CreateCtx.pLoc = pLoc;
CreateCtx.pOrbit = &pLoc->GetOrbit();
CreateCtx.bCreateSatellites = true;
if (error = pCtx->pSystem->CreateStation(pCtx, pType, CreateCtx))
return error;
// Remove the location so it doesn't match again
pCtx->pSystem->SetLocationObjID(iLocID, pCtx->dwLastObjID);
pCtx->dwLastObjID = dwSavedLastObjID;
LocationTable.Delete(iTablePos);
break;
}
return NOERROR;
}
ALERROR CreateArcDistribution (SSystemCreateCtx *pCtx, CXMLElement *pObj, const COrbit &OrbitDesc)
// CreateArcDistribution
//
// Creates a set of objects arranged along an arc, with Gaussian distribution.
{
ALERROR error;
int i;
// Get the number of objects to create
int iCount = GetDiceCountFromAttribute(pObj->GetAttribute(COUNT_ATTRIB));
// Short-circuit if nothing to do
if (pObj->GetContentElementCount() == 0 || iCount <= 0)
return NOERROR;
// Get parameters
int iArcLength = pObj->GetAttributeIntegerBounded(ARC_LENGTH_ATTRIB, 0, -1, 0);
int iRadialWidth = pObj->GetAttributeIntegerBounded(RADIAL_WIDTH_ATTRIB, 0, -1, 0);
int iRadialEdgeWidth = pObj->GetAttributeIntegerBounded(RADIAL_EDGE_WIDTH_ATTRIB, 0, -1, 0);
Metric rScale = GetScale(pObj);
Metric rMaxStdDev = 3.0;
// If arc length is 0 then it means that we distribute evenly around the
// entire orbit.
if (iArcLength == 0)
{
Metric rWidth = rScale * iRadialWidth / 2.0;
for (i = 0; i < iCount; i++)
{
Metric rPos = mathRandomGaussian() / rMaxStdDev;
COrbit SiblingOrbit(OrbitDesc.GetFocus(),
OrbitDesc.GetSemiMajorAxis() + (rWidth * rPos),
OrbitDesc.GetEccentricity(),
OrbitDesc.GetRotation(),
COrbit::RandomAngle());
if (error = CreateSystemObject(pCtx,
pObj->GetContentElement(0),
SiblingOrbit))
return error;
}
}
// Otherwise, we focus on an arc centered on the orbit position
else
{
Metric rArc = rScale * iArcLength / 2.0;
Metric rMaxWidth = rScale * iRadialWidth / 2.0;
Metric rMinWidth = rScale * iRadialEdgeWidth / 2.0;
Metric rRadialRange = rMaxWidth - rMinWidth;
for (i = 0; i < iCount; i++)
{
// First compute a random offset along the orbit.
Metric rArcOffset = (mathRandomGaussian() / rMaxStdDev) * rArc;
Metric rAngleOffset = rArcOffset / OrbitDesc.GetSemiMajorAxis();
// The radial offset depends on the arc offset.
Metric rWidth = 1.0 - (Absolute(rArcOffset) / rArc);
Metric rRadialOffset = (mathRandomGaussian() / rMaxStdDev) * (rMinWidth + (rRadialRange * rWidth));
// Create orbit
COrbit SiblingOrbit(OrbitDesc.GetFocus(),
OrbitDesc.GetSemiMajorAxis() + rRadialOffset,
OrbitDesc.GetEccentricity(),
OrbitDesc.GetRotation(),
OrbitDesc.GetObjectAngle() + rAngleOffset);
if (error = CreateSystemObject(pCtx,
pObj->GetContentElement(0),
SiblingOrbit))
return error;
}
}
return NOERROR;
}
ALERROR CreateLabel (SSystemCreateCtx *pCtx,
CXMLElement *pObj,
const COrbit &OrbitDesc)
// CreateLabel
//
// Creates a labeled point
{
// Add the Orbit to the list
CLocationDef *pLoc;
pCtx->pSystem->CreateLocation(NULL_STR, OrbitDesc, pObj->GetAttribute(ATTRIBUTES_ATTRIB), &pLoc);
// Keep stats
if (pCtx->pStats)
{
CString sAttribs = ::AppendModifiers(pLoc->GetAttributes(), pCtx->pSystem->GetAttribsAtPos(pLoc->GetOrbit().GetObjectPos()));
pCtx->pStats->AddLabel(sAttribs);
}
STATION_PLACEMENT_OUTPUT("+create label\n");
return NOERROR;
}
ALERROR CreateLevelTable (SSystemCreateCtx *pCtx, CXMLElement *pDesc, const COrbit &OrbitDesc)