-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCDesignTypeCriteria.cpp
458 lines (350 loc) · 9.29 KB
/
CDesignTypeCriteria.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
// CDesignTypeCriteria.cpp
//
// CDesignTypeCriteria class
// Copyright (c) 2015 by Kronosaur Productions, LLC.
#include "PreComp.h"
CDesignTypeCriteria::CDesignTypeCriteria (void) :
m_dwTypeSet(0),
m_iGreaterThanLevel(INVALID_COMPARE),
m_iLessThanLevel(INVALID_COMPARE),
m_bIncludeVirtual(false),
m_bStructuresOnly(false)
// CDesignTypeCriteria constructor
{
}
CString CDesignTypeCriteria::AsString (void) const
// AsString
//
// Outputs back to a string
{
int i;
CMemoryWriteStream Output(64 * 1024);
if (Output.Create() != NOERROR)
return NULL_STR;
// Write out the typeset
if (m_dwTypeSet == designSetAll)
Output.WriteChar('*');
else
{
for (i = 0; i < designCount; i++)
{
DWORD dwType = (1 << i);
if (m_dwTypeSet & dwType)
{
switch (i)
{
case designItemType:
Output.WriteChar(charItemType);
break;
case designItemTable:
Output.WriteChar(charItemTable);
break;
case designShipClass:
Output.WriteChar(charShipClass);
break;
case designOverlayType:
Output.WriteChar(charOverlayType);
break;
case designSystemType:
Output.WriteChar(charSystemType);
break;
case designStationType:
if (m_bStructuresOnly)
Output.WriteChar('T');
else
Output.WriteChar(charStationType);
break;
case designSovereign:
Output.WriteChar(charSovereign);
break;
case designDockScreen:
Output.WriteChar(charDockScreen);
break;
case designEffectType:
Output.WriteChar(charEffectType);
break;
case designPower:
Output.WriteChar(charPower);
break;
case designSpaceEnvironmentType:
Output.WriteChar(charSpaceEnvironmentType);
break;
case designShipTable:
Output.WriteChar(charShipTable);
break;
case designAdventureDesc:
Output.WriteChar(charAdventureDesc);
break;
case designGlobals:
Output.WriteChar(charGlobals);
break;
case designImage:
Output.WriteChar(charImage);
break;
case designMusic:
Output.WriteChar(charMusic);
break;
case designMissionType:
Output.WriteChar(charMissionType);
break;
case designSound:
Output.WriteChar(charSound);
break;
case designSystemTable:
Output.WriteChar(charSystemTable);
break;
case designSystemMap:
Output.WriteChar(charSystemMap);
break;
case designEconomyType:
Output.WriteChar(charEconomyType);
break;
case designTemplateType:
Output.WriteChar(charTemplateType);
break;
case designGenericType:
Output.WriteChar(charGenericType);
break;
case designImageComposite:
Output.WriteChar(charImageComposite);
break;
}
}
}
}
// Virtual
if (m_bIncludeVirtual)
Output.WriteChar('V');
// Write out all the attributes
for (i = 0; i < m_sRequire.GetCount(); i++)
Output.WriteChars(strPatternSubst(CONSTLIT(" +%s;"), m_sRequire[i]));
for (i = 0; i < m_sRequireSpecial.GetCount(); i++)
Output.WriteChars(strPatternSubst(CONSTLIT(" +%s;"), m_sRequireSpecial[i]));
for (i = 0; i < m_sExclude.GetCount(); i++)
Output.WriteChars(strPatternSubst(CONSTLIT(" -%s;"), m_sExclude[i]));
for (i = 0; i < m_sExcludeSpecial.GetCount(); i++)
Output.WriteChars(strPatternSubst(CONSTLIT(" -%s;"), m_sExcludeSpecial[i]));
// Level
if (m_iGreaterThanLevel != INVALID_COMPARE)
Output.WriteChars(strPatternSubst(CONSTLIT(" >%d"), m_iGreaterThanLevel));
if (m_iLessThanLevel != INVALID_COMPARE)
Output.WriteChars(strPatternSubst(CONSTLIT(" >%d"), m_iLessThanLevel));
// Done
return CString(Output.GetPointer(), Output.GetLength());
}
bool CDesignTypeCriteria::MatchesLevel (int iMinLevel, int iMaxLevel) const
// MatchesLevel
//
// Returns true if we match the level
{
if (m_iGreaterThanLevel != INVALID_COMPARE
&& iMaxLevel <= m_iGreaterThanLevel)
return false;
if (m_iLessThanLevel != INVALID_COMPARE
&& iMinLevel >= m_iLessThanLevel)
return false;
return true;
}
ALERROR CDesignTypeCriteria::ParseCriteria (const CString &sCriteria, CDesignTypeCriteria *retCriteria)
// ParseCriteria
//
// Parses the criteria and initializes retCriteria
{
// Initialize
retCriteria->m_dwTypeSet = 0;
retCriteria->m_iGreaterThanLevel = INVALID_COMPARE;
retCriteria->m_iLessThanLevel = INVALID_COMPARE;
retCriteria->m_bIncludeVirtual = false;
retCriteria->m_bStructuresOnly = false;
// Parse
char *pPos = sCriteria.GetPointer();
while (*pPos != '\0')
{
switch (*pPos)
{
case '*':
retCriteria->m_dwTypeSet = designSetAll;
break;
case charAdventureDesc:
retCriteria->m_dwTypeSet |= (1 << designAdventureDesc);
break;
case charItemTable:
retCriteria->m_dwTypeSet |= (1 << designItemTable);
break;
case charEffectType:
retCriteria->m_dwTypeSet |= (1 << designEffectType);
break;
case charDockScreen:
retCriteria->m_dwTypeSet |= (1 << designDockScreen);
break;
case charSpaceEnvironmentType:
retCriteria->m_dwTypeSet |= (1 << designSpaceEnvironmentType);
break;
case charEconomyType:
retCriteria->m_dwTypeSet |= (1 << designEconomyType);
break;
case charOverlayType:
retCriteria->m_dwTypeSet |= (1 << designOverlayType);
break;
case charGenericType:
retCriteria->m_dwTypeSet |= (1 << designGenericType);
break;
case charGlobals:
retCriteria->m_dwTypeSet |= (1 << designGlobals);
break;
case charShipTable:
retCriteria->m_dwTypeSet |= (1 << designShipTable);
break;
case charItemType:
retCriteria->m_dwTypeSet |= (1 << designItemType);
break;
case charImage:
retCriteria->m_dwTypeSet |= (1 << designImage);
break;
case charImageComposite:
retCriteria->m_dwTypeSet |= (1 << designImageComposite);
break;
case charMissionType:
retCriteria->m_dwTypeSet |= (1 << designMissionType);
break;
case charPower:
retCriteria->m_dwTypeSet |= (1 << designPower);
break;
case charShipClass:
retCriteria->m_dwTypeSet |= (1 << designShipClass);
break;
case charStationType:
retCriteria->m_dwTypeSet |= (1 << designStationType);
break;
case charMusic:
retCriteria->m_dwTypeSet |= (1 << designMusic);
break;
case charSound:
retCriteria->m_dwTypeSet |= (1 << designSound);
break;
case charSovereign:
retCriteria->m_dwTypeSet |= (1 << designSovereign);
break;
case charSystemTable:
retCriteria->m_dwTypeSet |= (1 << designSystemTable);
break;
case charSystemType:
retCriteria->m_dwTypeSet |= (1 << designSystemType);
break;
case charSystemMap:
retCriteria->m_dwTypeSet |= (1 << designSystemMap);
break;
case charTemplateType:
// We don't support enumerating template types
break;
case 'L':
{
int iHigh;
int iLow;
if (ParseCriteriaParamLevelRange(&pPos, &iLow, &iHigh))
{
if (iHigh == -1)
{
retCriteria->m_iGreaterThanLevel = iLow - 1;
retCriteria->m_iLessThanLevel = iLow + 1;
}
else
{
retCriteria->m_iGreaterThanLevel = iLow - 1;
retCriteria->m_iLessThanLevel = iHigh + 1;
}
}
break;
}
case 'T':
retCriteria->m_dwTypeSet |= (1 << designStationType);
retCriteria->m_bStructuresOnly = true;
break;
case 'V':
retCriteria->m_bIncludeVirtual = true;
break;
case '+':
case '-':
{
bool bRequired = (*pPos == '+');
bool bBinaryParam;
CString sParam = ParseCriteriaParam(&pPos, false, &bBinaryParam);
if (bRequired)
{
if (bBinaryParam)
retCriteria->m_sRequireSpecial.Insert(sParam);
else
retCriteria->m_sRequire.Insert(sParam);
}
else
{
if (bBinaryParam)
retCriteria->m_sExcludeSpecial.Insert(sParam);
else
retCriteria->m_sExclude.Insert(sParam);
}
break;
}
case '=':
case '>':
case '<':
{
char chChar = *pPos;
pPos++;
// <= or >=
int iEqualAdj;
if (*pPos == '=')
{
pPos++;
iEqualAdj = 1;
}
else
iEqualAdj = 0;
// Is this price?
char comparison;
if (*pPos == '$' || *pPos == '#')
comparison = *pPos++;
else
comparison = '\0';
// Get the number
char *pNewPos;
int iValue = strParseInt(pPos, 0, &pNewPos);
// Back up one because we will increment at the bottom
// of the loop.
if (pPos != pNewPos)
pPos = pNewPos - 1;
// Level limits
if (chChar == '=')
{
retCriteria->m_iGreaterThanLevel = iValue - 1;
retCriteria->m_iLessThanLevel = iValue + 1;
}
else if (chChar == '>')
retCriteria->m_iGreaterThanLevel = iValue - iEqualAdj;
else if (chChar == '<')
retCriteria->m_iLessThanLevel = iValue + iEqualAdj;
break;
}
}
pPos++;
}
return NOERROR;
}
void CDesignTypeCriteria::ReadFromStream (SLoadCtx &Ctx)
// ReadFromStream
//
// Reads from a stream
{
CString sCriteria;
sCriteria.ReadFromStream(Ctx.pStream);
ParseCriteria(sCriteria, this);
}
void CDesignTypeCriteria::WriteToStream (IWriteStream *pStream)
// WriteToStream
//
// Writes to a stream
{
// Convert to a string and save
CString sCriteria = AsString();
sCriteria.WriteToStream(pStream);
}