/
ContentBuildingReader.cpp
380 lines (337 loc) · 10.8 KB
/
ContentBuildingReader.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
#include <iostream>
#include <fstream>
#include <string>
#include "common.h"
#include "BuildingConfiguration.h"
#include "BlockCondition.h"
#include "dfhack/library/tinyxml/tinyxml.h"
#include "GUI.h"
#include "ContentLoader.h"
int parseConditionNode(ConditionalNode* node, TiXmlElement* elemCondition, bool silent);
bool parseSpriteNode(SpriteNode* node, TiXmlElement* elemParent);
bool includeFile(SpriteNode* node, TiXmlElement* includeNode, SpriteBlock* &oldSibling);
bool parseRecursiveNodes (ConditionalNode* pnode, TiXmlElement* pelem)
{
TiXmlElement* elemCondition = pelem->FirstChildElement();
while( elemCondition )
{
if (!parseConditionNode( pnode, elemCondition, false ))
return false;
elemCondition = elemCondition->NextSiblingElement();
}
return true;
}
int parseConditionNode(ConditionalNode* node, TiXmlElement* elemCondition, bool silent){
const char* strType = elemCondition->Value();
BlockCondition* cond = NULL;
if( strcmp(strType, "NeighbourWall") == 0){
cond = new NeighbourWallCondition( elemCondition->Attribute("dir") );
}
else if( strcmp(strType, "PositionIndex") == 0){
cond = new PositionIndexCondition( elemCondition->Attribute("value") );
}
else if( strcmp(strType, "MaterialType") == 0){
cond = new MaterialTypeCondition( elemCondition->Attribute("value") , elemCondition->Attribute("subtype"));
}
else if( strcmp(strType, "always") == 0){
cond = new AlwaysCondition();
}
else if( strcmp(strType, "never") == 0){
cond = new NeverCondition();
}
else if( strcmp(strType, "BuildingOccupancy") == 0){
cond = new BuildingOccupancyCondition( elemCondition->Attribute("value") );
}
else if( strcmp(strType, "NeighbourSameBuilding") == 0){
cond = new NeighbourSameBuildingCondition( elemCondition->Attribute("dir") );
}
else if( strcmp(strType, "NeighbourSameType") == 0){
cond = new NeighbourSameTypeCondition( elemCondition->Attribute("dir") );
}
else if( strcmp(strType, "NeighbourOfType") == 0){
cond = new NeighbourOfTypeCondition( elemCondition->Attribute("dir") , elemCondition->Attribute("value") );
}
else if( strcmp(strType, "NeighbourIdentical") == 0){
cond = new NeighbourIdenticalCondition( elemCondition->Attribute("dir") );
}
else if( strcmp(strType, "AnimationFrame") == 0){
cond = new AnimationFrameCondition( elemCondition->Attribute("value") );
}
else if( strcmp(strType, "FluidBelow") == 0){
cond = new FluidBelowCondition( elemCondition->Attribute("value") );
}
else if( strcmp(strType, "HaveFloor") == 0){
cond = new HaveFloorCondition();
}
else if( strcmp(strType, "and") == 0){
AndConditionalNode* andNode = new AndConditionalNode();
cond = andNode;
if (!parseRecursiveNodes(andNode, elemCondition))
{
delete(andNode);
return 0;
}
}
else if( strcmp(strType, "or") == 0){
OrConditionalNode* orNode = new OrConditionalNode();
cond = orNode;
if (!parseRecursiveNodes(orNode, elemCondition))
{
delete(orNode);
return 0;
}
}
else if( strcmp(strType, "not") == 0){
NotConditionalNode* notNode = new NotConditionalNode();
cond = notNode;
if (!parseRecursiveNodes(notNode, elemCondition))
{
delete(notNode);
return 0;
}
}
if (cond != NULL)
{
if (!node->addCondition( cond ))
{
contentError("Unable to add subcondition",elemCondition);
delete(cond);
return 0;
}
return 1;
}
else if (!silent)
{
contentError("Misplaced or invalid element in Condition",elemCondition);
return 0;
}
return -1;
}
inline bool readNode(SpriteNode* node, TiXmlElement* elemNode, TiXmlElement* elemParent, SpriteBlock* &oldSibling)
{
const char* strType = elemNode->Value();
if (strcmp(strType, "if") == 0 || strcmp(strType, "else") == 0)
{
SpriteBlock* block = new SpriteBlock();
if (!elemNode->Attribute("file") && elemParent->Attribute("file"))
{
elemNode->SetAttribute("file", elemParent->Attribute("file"));
}
if (!parseSpriteNode(block,elemNode))
{
delete(block);
return false;
}
if (elemNode->Attribute("else") || strcmp(strType, "else") == 0)
{
if (!oldSibling)
{
contentError("Misplaced or invalid element in SpriteNode",elemNode);
return false;
}
oldSibling->addElse(block);
}
else
{
node->addChild(block);
}
oldSibling = block;
}
else if (strcmp(strType, "rotate") == 0)
{
RotationBlock* block = new RotationBlock();
if (!elemNode->Attribute("file") && elemParent->Attribute("file"))
{
elemNode->SetAttribute("file",elemParent->Attribute("file"));
}
if (!parseSpriteNode(block,elemNode))
{
delete(block);
return false;
}
else
{
node->addChild(block);
}
oldSibling = NULL;
}
else if ((strcmp(strType, "sprite") == 0) || (strcmp(strType, "empty") == 0))
{
SpriteElement* sprite = new SpriteElement();
const char* strSheetIndex = elemNode->Attribute("index");
const char* strOffsetX = elemNode->Attribute("offsetx");
const char* strOffsetY = elemNode->Attribute("offsety");
const char* filename = elemNode->Attribute("file");
getAnimFrames(elemNode->Attribute("frames"));
sprite->sprite.animFrames = getAnimFrames(elemNode->Attribute("frames"));
if (sprite->sprite.animFrames == 0)
sprite->sprite.animFrames = ALL_FRAMES;
sprite->sprite.sheetIndex = (strSheetIndex != 0 ? atoi(strSheetIndex) : -1);
sprite->sprite.x = (strOffsetX != 0 ? atoi(strOffsetX) : 0);
sprite->sprite.y = (strOffsetY != 0 ? atoi(strOffsetY) : 0);
sprite->sprite.fileIndex = -1;
if (filename != NULL && filename[0] != 0)
{
sprite->sprite.fileIndex = loadConfigImgFile((char*)filename,elemNode);
}
else
{
const char* pfilename = elemParent->Attribute("file");
if (pfilename != NULL && pfilename[0] != 0)
{
sprite->sprite.fileIndex = loadConfigImgFile((char*)pfilename,elemNode);
}
}
node->addChild(sprite);
}
else if (strcmp(strType, "include") == 0)
{
if (!includeFile(node,elemNode,oldSibling))
{
return false;
}
}
else
{
contentError("Misplaced or invalid element in SpriteNode",elemNode);
return false;
}
return true;
}
bool includeFile(SpriteNode* node, TiXmlElement* includeNode, SpriteBlock* &oldSibling)
{
string filenameStr("buildings/include/");
filenameStr.append(includeNode->Attribute("file"));
TiXmlDocument doc( filenameStr.c_str() );
bool loadOkay = doc.LoadFile();
TiXmlHandle hDoc(&doc);
TiXmlElement* elemParent;
if(!loadOkay)
{
contentError("Include failed",includeNode);
WriteErr("File load failed: %s\n",filenameStr.c_str());
WriteErr("Line %d: %s\n",doc.ErrorRow(),doc.ErrorDesc());
return false;
}
elemParent = hDoc.FirstChildElement("include").Element();
if( elemParent == NULL)
{
contentError("Main <include> node not present",&doc);
return false;
}
TiXmlElement* elemNode = elemParent->FirstChildElement();
if (elemNode == NULL)
{
contentError("Empty include",elemParent);
return false;
}
while (elemNode)
{
if (!readNode(node, elemNode, elemParent, oldSibling))
return false;
elemNode = elemNode->NextSiblingElement();
}
return true;
}
bool parseSpriteNode(SpriteNode* node, TiXmlElement* elemParent)
{
//TODO: there is a leak here somewhere.
SpriteBlock* oldSibling = NULL;
TiXmlElement* elemNode = elemParent->FirstChildElement();
const char* strParent = elemParent->Value();
if (elemNode == NULL)
{
contentError("Empty SpriteNode Element",elemParent);
return false;
}
if ( strcmp(strParent,"building") != 0 && strcmp(strParent,"rotate") != 0)
{
//flag to allow else statements to be empty, rather than needing an "always" tag
bool allowBlank = (strcmp(strParent,"else") == 0 || elemParent->Attribute("else"));
// cast should be safe, because only spriteblocks
// should get here
int retvalue =parseConditionNode((SpriteBlock *)node,elemNode,allowBlank);
if (retvalue == 0)
return false;
if (retvalue > 0)
elemNode = elemNode->NextSiblingElement();
}
while (elemNode)
{
if (!readNode(node, elemNode, elemParent, oldSibling))
return false;
elemNode = elemNode->NextSiblingElement();
}
return true;
}
bool addSingleBuildingConfig( TiXmlElement* elemRoot, vector<BuildingConfiguration>* knownBuildings ){
const char* strName = elemRoot->Attribute("name");
const char* strGameID = elemRoot->Attribute("gameID");
if (strName == NULL || strGameID == NULL || strName[0] == 0 || strGameID[0] == 0)
{
contentError("<building> node must have name and gameID attributes",elemRoot);
return false;
}
int gameID = TranslateBuildingName(strGameID, contentLoader.buildingNameStrings );
if (gameID == INVALID_INDEX) {
//warning was already given
return false;
}
BuildingConfiguration building(strName, gameID );
RootBlock* spriteroot = new RootBlock();
building.sprites = spriteroot;
if (!parseSpriteNode(spriteroot,elemRoot))
{
delete(spriteroot);
return false;
}
//add a copy of 'building' to known buildings
knownBuildings->push_back( building );
return true;
}
void flushBuildingConfig( vector<BuildingConfiguration>* knownBuildings )
{
// clean up building data trees before deleting them
// a nasty cludge that only works cause knownbuildings
// isnt modified anywhere else
// TODO: look into smart pointers or something
uint32_t numBuildings = (uint32_t)knownBuildings->size();
for(uint32_t i = 0; i < numBuildings; i++)
{
delete(knownBuildings->at(i).sprites);
//should set to null, but we will nuke the lot in a second
}
knownBuildings->clear();
}
/*
bool LoadBuildingConfiguration( vector<BuildingConfiguration>* knownBuildings ){
string line;
ifstream myfile ("buildings/index.txt");
if (myfile.is_open() == false){
WriteErr("Unable to load building config index file!\n");
return false;
}
//img files are now unused
flushImgFiles();
while ( !myfile.eof() )
{
char filepath[50] = {0};
getline (myfile,line);
//some systems don't remove the \r char as a part of the line change:
if(line.size() > 0 && line[line.size() -1 ] == '\r' )
line.resize(line.size() -1);
if(line.size() > 0){
sprintf(filepath, "buildings/%s", line.c_str() );
WriteErr("Reading %s...\t\t", filepath);
bool result = addSingleConfig( filepath, knownBuildings );
if (result)
WriteErr("ok\n");
else
WriteErr("Unable to load building config %s\n(Will ignore building and continue)\n", filepath);
}
}
myfile.close();
BuildingNamesTranslatedFromGame = false;
return true;
}
*/