-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCStationEncounterDesc.cpp
514 lines (397 loc) · 12.8 KB
/
CStationEncounterDesc.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
// CStationEncounterDesc.cpp
//
// CStationEncounterDesc class
// Copyright (c) 2013 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define CRITERIA_TAG CONSTLIT("Criteria")
#define ENCOUNTER_TAG CONSTLIT("Encounter")
#define SYSTEM_CRITERIA_TAG CONSTLIT("SystemCriteria")
#define DISTANCE_CRITERIA_ATTRIB CONSTLIT("distanceCriteria")
#define DISTANCE_FREQUENCY_ATTRIB CONSTLIT("distanceFrequency")
#define ENEMY_EXCLUSION_RADIUS_ATTRIB CONSTLIT("enemyExclusionRadius")
#define EXCLUSION_RADIUS_ATTRIB CONSTLIT("exclusionRadius")
#define LEVEL_FREQUENCY_ATTRIB CONSTLIT("levelFrequency")
#define LOCATION_CRITERIA_ATTRIB CONSTLIT("locationCriteria")
#define MAX_APPEARING_ATTRIB CONSTLIT("maxAppearing")
#define MAX_IN_SYSTEM_ATTRIB CONSTLIT("maxInSystem")
#define MIN_APPEARING_ATTRIB CONSTLIT("minAppearing")
#define NUMBER_APPEARING_ATTRIB CONSTLIT("numberAppearing")
#define SYSTEM_AFFINITY_ATTRIB CONSTLIT("systemAffinity")
#define SYSTEM_CRITERIA_ATTRIB CONSTLIT("systemCriteria")
#define UNIQUE_ATTRIB CONSTLIT("unique")
#define UNIQUE_IN_SYSTEM CONSTLIT("inSystem")
#define UNIQUE_IN_UNIVERSE CONSTLIT("inUniverse")
#define VALUE_FALSE CONSTLIT("false")
#define VALUE_TRUE CONSTLIT("true")
const Metric DEFAULT_ENEMY_EXCLUSION = 50.0 * LIGHT_SECOND;
int CStationEncounterDesc::CalcAffinity (CTopologyNode *pNode) const
// CalcAffinity
//
// Calculates the affinity for the given node. We return values between
// ftCommon and 0.
{
if (m_SystemAffinity.MatchesAll())
return ftCommon;
int iWeight = m_SystemAffinity.CalcNodeWeight(pNode);
return (ftCommon * iWeight / 1000);
}
int CStationEncounterDesc::CalcLevelFromFrequency (void) const
// CalcLevelFromFrequency
//
// Computes a level from a frequency. Returns 0 if we could not compute
// the level.
{
int iLevel = 1;
int iTotal = 0;
int iCount = 0;
char *pPos = m_sLevelFrequency.GetASCIIZPointer();
while (*pPos != '\0')
{
int iFreq = 0;
switch (*pPos)
{
case 'C':
case 'c':
iFreq = ftCommon;
break;
case 'U':
case 'u':
iFreq = ftUncommon;
break;
case 'R':
case 'r':
iFreq = ftRare;
break;
case 'V':
case 'v':
iFreq = ftVeryRare;
break;
}
iTotal += iFreq * iLevel;
iCount += iFreq;
pPos++;
if (*pPos != ' ')
iLevel++;
}
// Compute the level by averaging things out
return (iCount > 0 ? (int)(((double)iTotal / (double)iCount) + 0.5) : 0);
}
int CStationEncounterDesc::GetCountOfRandomEncounterLevels (void) const
// GetCountOfRandomEncounterLevels
//
// Returns the number of levels in which the station can be
// encountered.
{
int iCount = 0;
char *pPos = m_sLevelFrequency.GetASCIIZPointer();
while (*pPos != '\0')
{
if (*pPos != ' ' && *pPos != '-')
iCount++;
pPos++;
}
return iCount;
}
void CStationEncounterDesc::GetExclusionDesc (SExclusionDesc &Exclusion) const
// GetExclusionDesc
//
// Returns an exclusion descriptor
{
Exclusion.rAllExclusionRadius2 = m_rExclusionRadius * m_rExclusionRadius;
Exclusion.bHasAllExclusion = (m_rExclusionRadius > 0.0);
Exclusion.rEnemyExclusionRadius2 = m_rEnemyExclusionRadius * m_rEnemyExclusionRadius;
Exclusion.bHasEnemyExclusion = (m_rEnemyExclusionRadius > m_rExclusionRadius);
}
int CStationEncounterDesc::GetFrequencyByDistance (int iDistance) const
// GetFrequencyByDistance
//
// Returns the frequency of the encounter based on the distance to m_sDistanceCriteria.
// Distances >= 1 mean that we are that many nodes away from a node that matches
// m_sDistanceCriteria. Distances <= -1 mean that we are that many nodes away from
// a node that DOES NOT match m_sDistanceCriteria.
//
// m_sDistanceFrequency must have two sets of five elements:
//
// 54321 12345
// \___/ \___/
// | |
// Negative distances Positive distances
{
if (iDistance < -5)
return ::GetFrequencyByLevel(m_sDistanceFrequency, 1);
else if (iDistance < 0)
return ::GetFrequencyByLevel(m_sDistanceFrequency, iDistance + 6);
else if (iDistance > 5)
return ftNotRandom;
else
return ::GetFrequencyByLevel(m_sDistanceFrequency, iDistance + 5);
}
int CStationEncounterDesc::GetFrequencyByLevel (int iLevel) const
// GetFrequencyByLevel
//
// Returns the frequency of the encounter by level. Note this is used for stats
// and random encounters. To determine the frequency of a station in a given
// system, use GetFrequencyBySystem instead.
{
if (!CanBeRandomlyEncountered())
return 0;
if (m_sLevelFrequency.IsBlank())
return ftCommon;
else
return ::GetFrequencyByLevel(m_sLevelFrequency, iLevel);
}
bool CStationEncounterDesc::InitAsOverride (const CStationEncounterDesc &Original, const CXMLElement &Override, CString *retsError)
// InitAsOverride
//
// Initializes as an override. We take all data from the Original except where
// the Override defines something.
{
// Start as the original
*this = Original;
// Number appearing (at least this number, unique in system)
CString sAttrib;
if (Override.FindAttribute(MIN_APPEARING_ATTRIB, &sAttrib)
|| Override.FindAttribute(NUMBER_APPEARING_ATTRIB, &sAttrib))
{
m_bNumberAppearing = true;
if (m_NumberAppearing.LoadFromXML(sAttrib) != NOERROR)
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Invalid numberAppearing parameter."));
return false;
}
m_iMaxCountInSystem = 1;
}
// Get maximum limit (at most this number, unique in system)
if (Override.FindAttribute(MAX_APPEARING_ATTRIB, &sAttrib))
{
m_bMaxCountLimit = true;
if (m_MaxAppearing.LoadFromXML(sAttrib) != NOERROR)
{
if (retsError) *retsError = strPatternSubst(CONSTLIT("Invalid maxAppearing parameter."));
return false;
}
m_iMaxCountInSystem = 1;
}
// Otherwise, we check uniqueness values
if (Override.FindAttribute(UNIQUE_ATTRIB, &sAttrib))
{
if (strEquals(sAttrib, UNIQUE_IN_SYSTEM))
{
m_bMaxCountLimit = false;
m_iMaxCountInSystem = 1;
}
else if (strEquals(sAttrib, UNIQUE_IN_UNIVERSE) || strEquals(sAttrib, VALUE_TRUE))
{
m_bMaxCountLimit = true;
m_MaxAppearing.SetConstant(1);
m_iMaxCountInSystem = 1;
}
}
// System criteria
CXMLElement *pCriteria = Override.GetContentElementByTag(SYSTEM_CRITERIA_TAG);
if (pCriteria == NULL)
pCriteria = Override.GetContentElementByTag(CRITERIA_TAG);
if (pCriteria)
{
if (CTopologyNode::ParseCriteria(pCriteria, &m_SystemCriteria, retsError) != NOERROR)
return false;
m_bSystemCriteria = true;
}
else if (Override.FindAttribute(SYSTEM_CRITERIA_ATTRIB, &sAttrib))
{
if (CTopologyNode::ParseCriteria(sAttrib, &m_SystemCriteria, retsError) != NOERROR)
return false;
m_bSystemCriteria = true;
}
// Level frequency and criteria
if (Override.FindAttribute(DISTANCE_CRITERIA_ATTRIB, &sAttrib))
{
if (CTopologyNode::ParseAttributeCriteria(sAttrib, &m_DistanceCriteria) != NOERROR)
return false;
}
if (Override.FindAttribute(DISTANCE_FREQUENCY_ATTRIB, &sAttrib))
m_sDistanceFrequency = sAttrib;
if (Override.FindAttribute(LEVEL_FREQUENCY_ATTRIB, &sAttrib))
m_sLevelFrequency = sAttrib;
if (Override.FindAttribute(LOCATION_CRITERIA_ATTRIB, &sAttrib))
m_sLocationCriteria = sAttrib;
if (Override.FindAttribute(SYSTEM_AFFINITY_ATTRIB, &sAttrib))
{
if (m_SystemAffinity.Parse(sAttrib, 0, retsError) != NOERROR)
return false;
}
// Exclusion radius
int iRadius;
if (Override.FindAttributeInteger(ENEMY_EXCLUSION_RADIUS_ATTRIB, &iRadius)
&& iRadius >= 0)
m_rEnemyExclusionRadius = iRadius * LIGHT_SECOND;
if (Override.FindAttributeInteger(EXCLUSION_RADIUS_ATTRIB, &iRadius)
&& iRadius >= 0)
m_rExclusionRadius = iRadius * LIGHT_SECOND;
// Done
return true;
}
ALERROR CStationEncounterDesc::InitFromStationTypeXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// InitFromStationTypeXML
//
// Initialize from <StationType>
{
// If we have an encounter element, then use that.
CXMLElement *pEncounter = pDesc->GetContentElementByTag(ENCOUNTER_TAG);
if (pEncounter)
return InitFromXML(Ctx, pEncounter);
// Otherwise, we use information at the root
return InitFromXML(Ctx, pDesc);
}
ALERROR CStationEncounterDesc::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// InitFromXML
//
// Initialize from an <Encounter> element (or a root <StationType> element).
{
ALERROR error;
bool bNumberSet = false;
// Number appearing (at least this number, unique in system)
CString sAttrib;
if (pDesc->FindAttribute(MIN_APPEARING_ATTRIB, &sAttrib)
|| pDesc->FindAttribute(NUMBER_APPEARING_ATTRIB, &sAttrib))
{
m_bNumberAppearing = true;
if (error = m_NumberAppearing.LoadFromXML(sAttrib))
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid numberAppearing parameter."));
return error;
}
m_iMaxCountInSystem = 1;
bNumberSet = true;
}
// Get maximum limit (at most this number, unique in system)
if (pDesc->FindAttribute(MAX_APPEARING_ATTRIB, &sAttrib))
{
m_bMaxCountLimit = true;
if (error = m_MaxAppearing.LoadFromXML(sAttrib))
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid maxAppearing parameter."));
return error;
}
m_iMaxCountInSystem = 1;
bNumberSet = true;
}
// Otherwise, we check uniqueness values
if (!bNumberSet)
{
CString sUnique = pDesc->GetAttribute(UNIQUE_ATTRIB);
if (strEquals(sUnique, UNIQUE_IN_SYSTEM))
{
m_bMaxCountLimit = false;
m_iMaxCountInSystem = 1;
}
else if (strEquals(sUnique, UNIQUE_IN_UNIVERSE) || strEquals(sUnique, VALUE_TRUE))
{
m_bMaxCountLimit = true;
m_MaxAppearing.SetConstant(1);
m_iMaxCountInSystem = 1;
}
else if (pDesc->FindAttributeInteger(MAX_IN_SYSTEM_ATTRIB, &m_iMaxCountInSystem))
{
m_bMaxCountLimit = false;
m_iMaxCountInSystem = Max(1, m_iMaxCountInSystem);
}
else
{
m_bMaxCountLimit = false;
m_iMaxCountInSystem = -1;
}
}
// System criteria
CXMLElement *pCriteria = pDesc->GetContentElementByTag(SYSTEM_CRITERIA_TAG);
if (pCriteria == NULL)
pCriteria = pDesc->GetContentElementByTag(CRITERIA_TAG);
if (pCriteria)
{
if (error = CTopologyNode::ParseCriteria(pCriteria, &m_SystemCriteria, &Ctx.sError))
return error;
m_bSystemCriteria = true;
}
else if (pDesc->FindAttribute(SYSTEM_CRITERIA_ATTRIB, &sAttrib))
{
if (error = CTopologyNode::ParseCriteria(sAttrib, &m_SystemCriteria, &Ctx.sError))
return error;
m_bSystemCriteria = true;
}
// Level frequency and criteria
if (error = CTopologyNode::ParseAttributeCriteria(pDesc->GetAttribute(DISTANCE_CRITERIA_ATTRIB), &m_DistanceCriteria))
return error;
m_sDistanceFrequency = pDesc->GetAttribute(DISTANCE_FREQUENCY_ATTRIB);
m_sLevelFrequency = pDesc->GetAttribute(LEVEL_FREQUENCY_ATTRIB);
m_sLocationCriteria = pDesc->GetAttribute(LOCATION_CRITERIA_ATTRIB);
if (pDesc->FindAttribute(SYSTEM_AFFINITY_ATTRIB, &sAttrib))
{
if (error = m_SystemAffinity.Parse(sAttrib, 0, &Ctx.sError))
return error;
}
// Exclusion radius
int iRadius;
if (pDesc->FindAttributeInteger(ENEMY_EXCLUSION_RADIUS_ATTRIB, &iRadius)
&& iRadius >= 0)
m_rEnemyExclusionRadius = iRadius * LIGHT_SECOND;
else
m_rEnemyExclusionRadius = DEFAULT_ENEMY_EXCLUSION;
if (pDesc->FindAttributeInteger(EXCLUSION_RADIUS_ATTRIB, &iRadius)
&& iRadius >= 0)
m_rExclusionRadius = iRadius * LIGHT_SECOND;
else
m_rExclusionRadius = 0.0;
// Done
return NOERROR;
}
void CStationEncounterDesc::InitLevelFrequency (void)
// InitLevelFrequency
//
// Initializes m_sLevelFrequency if it is not set and if we have a system
// criteria. This must be called only after the topology has been initialized.
{
int i;
if ((m_sLevelFrequency.IsBlank() || m_bAutoLevelFrequency)
&& m_bSystemCriteria)
{
// Start with no encounters
m_sLevelFrequency = CString("----- ----- ----- ----- -----");
// Loop over all nodes and check to see if we appear at the node.
// If we do, we mark that level 'common.'
CTopologyNode::SCriteriaCtx Ctx;
CTopology &Topology = g_pUniverse->GetTopology();
for (i = 0; i < Topology.GetTopologyNodeCount(); i++)
{
CTopologyNode *pNode = Topology.GetTopologyNode(i);
if (pNode->MatchesCriteria(Ctx, m_SystemCriteria))
::SetFrequencyByLevel(m_sLevelFrequency, pNode->GetLevel(), ftCommon);
}
// Remember that we initialized this so that we can save it out.
m_bAutoLevelFrequency = true;
}
}
void CStationEncounterDesc::ReadFromStream (SUniverseLoadCtx &Ctx)
// ReadFromStream
//
// Reads from a stream
{
CString sLevelFrequency;
sLevelFrequency.ReadFromStream(Ctx.pStream);
if (!sLevelFrequency.IsBlank())
{
m_sLevelFrequency = sLevelFrequency;
m_bAutoLevelFrequency = true;
}
}
void CStationEncounterDesc::WriteToStream (IWriteStream *pStream)
// WriteToStream
//
// Writes to a stream
{
// We only need to write the level frequency if we generated it from
// topology.
if (m_bAutoLevelFrequency)
m_sLevelFrequency.WriteToStream(pStream);
else
NULL_STR.WriteToStream(pStream);
}