-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCNameDesc.cpp
264 lines (196 loc) · 5.16 KB
/
CNameDesc.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
// CNameDesc.cpp
//
// CNameDesc class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define FIELD_NAME CONSTLIT("name")
#define TAG_NAMES CONSTLIT("Names")
CNameDesc::CNameDesc (void) :
m_dwConstantNameFlags(0),
m_dwNameFlags(0),
m_iNamesGenerated(0)
// CNameDesc constructor
{
}
ALERROR CNameDesc::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// InitFromXML
//
// Initializes the name descriptor.
{
m_dwNameFlags = CLanguage::LoadNameFlags(pDesc);
// Parse the list into separate names.
CString sNameList = pDesc->GetContentText(0);
strDelimitEx(sNameList, ';', DELIMIT_AUTO_COMMA | DELIMIT_TRIM_WHITESPACE | DELIMIT_QUOTE_ESCAPE, 0, &m_Names);
// If we don't have a constant name, use the first name in the list as
// constant/generic name.
if (m_sConstantName.IsBlank() && m_Names.GetCount() > 0)
{
m_sConstantName = m_Names[0];
m_dwConstantNameFlags = m_dwNameFlags;
}
// Randomize order.
Reinit();
return NOERROR;
}
ALERROR CNameDesc::InitFromXMLRoot (SDesignLoadCtx &Ctx, CXMLElement *pDesc)
// InitFromXMLRoot
//
// Initializes the name descriptor from a parent element of <Names>.
{
ALERROR error;
// Get the constant name
m_sConstantName = pDesc->GetAttribute(FIELD_NAME);
m_dwConstantNameFlags = CLanguage::LoadNameFlags(pDesc);
// Load list of random names. These names may have replaceable parameters.
CXMLElement *pNames = pDesc->GetContentElementByTag(TAG_NAMES);
if (pNames)
{
if (error = InitFromXML(Ctx, pNames))
return error;
}
return NOERROR;
}
CString CNameDesc::GenerateName (TSortMap<CString, CString> *pParams, DWORD *retdwNameFlags) const
// GenerateName
//
// Generates a name.
{
if (m_Names.GetCount() == 0)
{
if (retdwNameFlags)
*retdwNameFlags = m_dwConstantNameFlags;
return m_sConstantName;
}
// Pick the next name in the list.
const CString &sName = m_Names[m_iNamesGenerated % m_Names.GetCount()];
// Advance to the next name (for next time)
m_iNamesGenerated++;
// Done
if (retdwNameFlags)
*retdwNameFlags = m_dwNameFlags;
return GenerateName(sName, pParams);
}
CString CNameDesc::GenerateName (const CString &sName, TSortMap<CString, CString> *pParams) const
// GenerateName
//
// Generate a name by substituting all parameters.
{
// Substitute string
char szResult[1024];
char *pDest = szResult;
char *pDestEnd = pDest + 1023;
char *pPos = sName.GetASCIIZPointer();
while (*pPos && pDest < pDestEnd)
{
switch (*pPos)
{
case '%':
{
pPos++;
char *pStart = pPos;
// A double percent character is an escape code.
if (*pPos == '%' || *pPos == '\0')
{
*pDest++ = *pPos++;
break;
}
// Previous versions used a single character as a replaceable
// parameter.
pPos++;
// If we've got another percent, then it means that there is
// another parameter right after this one.
//
// Also, if we hit the end of the string or if we've got whitespace
// or a delimiter of some sort, then we've hit the end.
CString sToken;
if (*pPos == '\0' || *pPos == '%' || *pPos == ' ' || *pPos == '-'
|| *pPos == ':' || *pPos == '/' || *pPos == '(' || *pPos == ')')
{
if (*pStart == 's')
sToken = CONSTLIT("systemName");
else
sToken = CString(pStart, 1);
}
// Otherwise, we assume this is a new style parameter and we
// search for the terminating %.
else
{
while (*pPos != '\0' && *pPos != '%')
pPos++;
sToken = CString(pStart, (int)(pPos - pStart));
if (*pPos == '%')
pPos++;
}
// Figure out what we're going to replace this token with.
if (sToken.GetLength() == 1)
{
switch (*sToken.GetASCIIZPointer())
{
case '0':
*pDest++ = '0' + mathRandom(0, 9);
break;
case '1':
*pDest++ = '1' + mathRandom(0, 8);
break;
case 'A':
*pDest++ = 'A' + mathRandom(0, 25);
break;
case 'a':
*pDest++ = 'a' + mathRandom(0, 25);
break;
}
}
else
{
CString *pParam = (pParams ? pParams->GetAt(sToken) : NULL);
if (pParam)
{
char *pSrc = pParam->GetASCIIZPointer();
while (*pSrc && pDest < pDestEnd)
*pDest++ = *pSrc++;
}
// If this is not a variable in the parameter block, then
// interpret the token as a series of alphanumeric codes
// (as above).
else
{
char *pSrc = sToken.GetASCIIZPointer();
while (*pSrc && pDest < pDestEnd)
{
switch (*pSrc)
{
case '0':
*pDest++ = '0' + mathRandom(0, 9);
break;
case '1':
*pDest++ = '1' + mathRandom(0, 8);
break;
case 'A':
*pDest++ = 'A' + mathRandom(0, 25);
break;
case 'a':
*pDest++ = 'a' + mathRandom(0, 25);
break;
}
pSrc++;
}
}
}
break;
}
default:
*pDest++ = *pPos++;
}
}
// Done
*pDest = '\0';
return CString(szResult);
}
void CNameDesc::Reinit (void)
// Reinit
//
// Reinitialize in preparation for a new game.
{
m_Names.Shuffle();
m_iNamesGenerated = 0;
}