forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentBuildingReader.cpp
434 lines (400 loc) · 14.6 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
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
#include <iostream>
#include <fstream>
#include <string>
#include "common.h"
#include "GameBuildings.h"
#include "BuildingConfiguration.h"
#include "TileCondition.h"
#include "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, SpriteTile* &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();
TileCondition* 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"), elemCondition->Attribute("pattern_index"));
}
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, "BuildingSpecial") == 0) {
cond = new BuildingSpecialCondition( 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, SpriteTile* &oldSibling)
{
const char* strType = elemNode->Value();
if (strcmp(strType, "if") == 0 || strcmp(strType, "else") == 0) {
SpriteTile* tile = new SpriteTile();
if (!elemNode->Attribute("file") && elemParent->Attribute("file")) {
elemNode->SetAttribute("file", elemParent->Attribute("file"));
}
if (!parseSpriteNode(tile,elemNode)) {
delete(tile);
return false;
}
if (elemNode->Attribute("else") || strcmp(strType, "else") == 0) {
if (!oldSibling) {
contentError("Misplaced or invalid element in SpriteNode",elemNode);
return false;
}
oldSibling->addElse(tile);
} else {
node->addChild(tile);
}
oldSibling = tile;
} else if (strcmp(strType, "rotate") == 0) {
RotationTile* tile = new RotationTile();
if (!elemNode->Attribute("file") && elemParent->Attribute("file")) {
elemNode->SetAttribute("file",elemParent->Attribute("file"));
}
if (!parseSpriteNode(tile,elemNode)) {
delete(tile);
return false;
} else {
node->addChild(tile);
}
oldSibling = NULL;
} else if ((strcmp(strType, "sprite") == 0) || (strcmp(strType, "empty") == 0)) {
int fileindex = 0;
const char* pfilename = elemParent->Attribute("file");
if (pfilename != NULL && pfilename[0] != 0) {
fileindex = loadConfigImgFile((char*)pfilename,elemNode);
if(fileindex == -1) {
return false;
}
}
SpriteElement* sprite = new SpriteElement();
sprite->sprite.set_by_xml(elemNode, fileindex);
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, SpriteTile* &oldSibling)
{
// get path... ugly
char configfilepath[FILENAME_BUFFERSIZE] = {0};
const char* documentRef = getDocument(includeNode);
if (!getLocalFilename(configfilepath,includeNode->Attribute("file"),documentRef)) {
return false;
}
ALLEGRO_PATH * incpath = al_create_path(configfilepath);
al_append_path_component(incpath, "include");
TiXmlDocument doc( al_path_cstr(incpath, ALLEGRO_NATIVE_PATH_SEP) );
al_destroy_path(incpath);
bool loadOkay = doc.LoadFile();
TiXmlHandle hDoc(&doc);
TiXmlElement* elemParent;
if(!loadOkay) {
contentError("Include failed",includeNode);
LogError("File load failed: %s\n",configfilepath);
LogError("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.
SpriteTile* 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,"custom_workshop") != 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 spritetiles
// should get here
int retvalue =parseConditionNode((SpriteTile *)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;
}
#include "df/siegeengine_type.h"
#include "df/workshop_type.h"
#include "df/trap_type.h"
#include "df/shop_type.h"
#include "df/construction_type.h"
#include "df/furnace_type.h"
bool addSingleBuildingConfig( TiXmlElement* elemRoot, vector<BuildingConfiguration>* knownBuildings )
{
const char* strName = elemRoot->Attribute("name");
const char* strGameID = elemRoot->Attribute("game_type");
const char* strGameSub = elemRoot->Attribute("game_subtype");
const char* strGameCustom = elemRoot->Attribute("game_custom");
if (strName == NULL || strGameID == NULL || strName[0] == 0 || strGameID[0] == 0) {
contentError("<building> node must have name and game_type attributes",elemRoot);
return false;
}
building_type::building_type main_type = BUILDINGTYPE_NA;
int subtype = INVALID_INDEX;
string game_type_s;
FOR_ENUM_ITEMS(building_type,i) {
game_type_s = strGameID;
if (game_type_s == ENUM_KEY_STR(building_type,i)) {
main_type = i;
break;
}
}
if(main_type == BUILDINGTYPE_NA) {
contentError("<building> unknown game_type value",elemRoot);
return false;
}
// get subtype string, if available
string sub;
if(strGameSub) {
sub = strGameSub;
}
bool needs_custom = false;
// process types
switch (main_type) {
case building_type::Furnace: {
if(!strGameSub) {
contentWarning("<building> Is generic - game_subtype missing.",elemRoot);
break;
}
FOR_ENUM_ITEMS(furnace_type,i) {
if (sub == ENUM_KEY_STR(furnace_type,i)) {
subtype = i;
break;
}
}
if(subtype == INVALID_INDEX) {
contentError("<building> unknown game_subtype value",elemRoot);
return false;
}
if(subtype == furnace_type::Custom) {
needs_custom = true;
}
break;
}
case building_type::Construction: {
if(!strGameSub) {
contentWarning("<building> Is generic - game_subtype missing.",elemRoot);
break;
}
FOR_ENUM_ITEMS(construction_type,i) {
if (sub == ENUM_KEY_STR(construction_type,i)) {
subtype = i;
break;
}
}
if(subtype == INVALID_INDEX) {
contentError("<building> unknown game_subtype value",elemRoot);
return false;
}
break;
}
case building_type::SiegeEngine: {
if(!strGameSub) {
contentWarning("<building> Is generic - game_subtype missing.",elemRoot);
break;
}
FOR_ENUM_ITEMS(siegeengine_type,i) {
if (sub == ENUM_KEY_STR(siegeengine_type,i)) {
subtype = i;
break;
}
}
if(subtype == INVALID_INDEX) {
contentError("<building> unknown game_subtype value",elemRoot);
return false;
}
break;
}
case building_type::Shop: {
if(!strGameSub) {
contentWarning("<building> Is generic - game_subtype missing.",elemRoot);
break;
}
FOR_ENUM_ITEMS(shop_type,i) {
if (sub == ENUM_KEY_STR(shop_type,i)) {
subtype = i;
break;
}
}
if(subtype == INVALID_INDEX) {
contentError("<building> unknown game_subtype value",elemRoot);
return false;
}
break;
}
case building_type::Workshop: {
if(!strGameSub) {
contentWarning("<building> Is generic - game_subtype missing.",elemRoot);
break;
}
FOR_ENUM_ITEMS(workshop_type,i) {
if (sub == ENUM_KEY_STR(workshop_type,i)) {
subtype = i;
break;
}
}
if(subtype == INVALID_INDEX) {
contentError("<building> unknown game_subtype value",elemRoot);
return false;
}
if(subtype == workshop_type::Custom) {
needs_custom = true;
}
break;
}
default: {
// we need no sub/custom type.
break;
}
}
string custom;
// needs custom building spec, doesn't have a string... FAIL
if (needs_custom && !strGameCustom) {
contentError("<building> game_custom attribute is required, but missng.",elemRoot);
return false;
} else if (strGameCustom) {
custom = strGameCustom;
}
BuildingConfiguration building(strName, main_type, subtype, custom );
RootTile* spriteroot = new RootTile();
building.sprites = spriteroot;
if (!parseSpriteNode(spriteroot,elemRoot)) {
delete(spriteroot);
contentError("<building> Failed while parsing sprite node",elemRoot);
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();
}