-
Notifications
You must be signed in to change notification settings - Fork 55
/
GroundMaterialConfiguration.cpp
371 lines (351 loc) · 15.8 KB
/
GroundMaterialConfiguration.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
#include "common.h"
#include "SpriteMaps.h"
#include "GroundMaterialConfiguration.h"
#include <set>
#include "tinyxml.h"
#include "GUI.h"
#include "ContentLoader.h"
#include "EnumToString.h"
#include "MiscUtils.h"
using namespace std;
using namespace DFHack;
using namespace df::enums;
#define PRIORITY_SHAPE 8
#define PRIORITY_SPECIAL 4
#define PRIORITY_VARIANT 2
#define PRIORITY_MATERIAL 1
#define PRIORITY_TOTAL (PRIORITY_SHAPE+PRIORITY_SPECIAL+PRIORITY_VARIANT+PRIORITY_MATERIAL+1)
TerrainMaterialConfiguration::TerrainMaterialConfiguration()
{
overridingMaterials.resize(NUM_FORMS);
defaultSprite.resize(NUM_FORMS);
for (int i = 0; i < NUM_FORMS; i++) {
defaultSprite[i].first.set_fileindex(INVALID_INDEX);
defaultSprite[i].first.set_sheetindex(UNCONFIGURED_INDEX);
defaultSprite[i].second = INVALID_INDEX;
}
//dont really care about the rest of the sprite right now.
}
TerrainConfiguration::TerrainConfiguration()
{
defaultSprite.resize(NUM_FORMS);
for (int i = 0; i < NUM_FORMS; i++) {
defaultSprite[i].first.set_fileindex(INVALID_INDEX);
defaultSprite[i].first.set_sheetindex(UNCONFIGURED_INDEX);
defaultSprite[i].second = INVALID_INDEX;
}
//dont really care about the rest of the sprite right now.
}
void DumpInorganicMaterialNamesToDisk()
{
FILE* fp = fopen("dump.txt", "w");
if (!fp) {
return;
}
for (uint32_t j = 0; j < contentLoader->inorganic.size(); j++) {
fprintf(fp, "%i:%s\n", j, contentLoader->inorganic[j].id.c_str());
}
fclose(fp);
}
void parseWallFloorSpriteElement(TiXmlElement* elemWallFloorSprite, vector<std::unique_ptr<TerrainConfiguration>>& configTable, int basefile, bool floor)
{
const char* spriteSheetIndexStr = elemWallFloorSprite->Attribute("sheetIndex");
const char* spriteSpriteStr = elemWallFloorSprite->Attribute("sprite");
const char* spriteIndexStr = elemWallFloorSprite->Attribute("index");
if ((spriteSheetIndexStr == NULL || spriteSheetIndexStr[0] == 0) && (spriteSpriteStr == NULL || spriteSpriteStr[0] == 0) && (spriteIndexStr == NULL || spriteIndexStr[0] == 0)) {
contentError("Invalid or missing sprite attribute", elemWallFloorSprite);
return; //nothing to work with
}
// make a base sprite
c_sprite sprite;
if (floor) {
sprite.set_size(SPRITEWIDTH, (TILETOPHEIGHT + FLOORHEIGHT));
sprite.set_offset(0, (WALLHEIGHT));
}
sprite.set_needoutline(1);
sprite.set_by_xml(elemWallFloorSprite, basefile);
vector<pair<int, int>> lookupKeys;
// look through terrain elements
for (TiXmlElement* elemTerrain = elemWallFloorSprite->FirstChildElement("terrain");
elemTerrain;
elemTerrain = elemTerrain->NextSiblingElement("terrain")) {
//get a terrain type
//
// NOTE(myk002): targetElem was changed from df::tiletype to an int because the underlying type of df::tiltype changed
// from signed to unsigned as we canonicalized DFHack xml structures against DF headers. This caused issues in this
// code because:
// - negative values (like INVALID_INDEX) are given special meaning
// - the code here (and elsewhere in stonesense) depends on signed comparisons
// - the value of int matchedness (below) is assigned to a tiletype field (though I can't determine why this is desired)
//
// a proper fix would take a fair bit of rearchitecting throughout stonesense
int targetElem = INVALID_INDEX;
const char* gameIDstr = elemTerrain->Attribute("value");
if (!(gameIDstr == NULL || gameIDstr[0] == 0))
{
targetElem = atoi(gameIDstr);
}
if (targetElem >= 0)
{
char buf[500];
if (is_valid_enum_item((df::tiletype)targetElem))
{
df::tiletype tt = (df::tiletype)targetElem;
auto shape = ENUM_ATTR(tiletype, shape, tt);
auto special = ENUM_ATTR(tiletype, special, tt);
auto variant = ENUM_ATTR(tiletype, variant, tt);
auto material = ENUM_ATTR(tiletype, material, tt);
sprintf(buf, "Use of deprecated terrain value \"%d\", use one of the following instead:\n <terrain token = \"%s\" />\n <terrain%s%s%s%s%s%s%s%s%s%s%s%s />\n in element",
targetElem,
enum_item_key_str(tt),
shape == tiletype_shape::NONE ? "" : " shape = \"",
shape == tiletype_shape::NONE ? "" : enum_item_key_str(shape),
shape == tiletype_shape::NONE ? "" : "\"",
special == tiletype_special::NONE ? "" : " special = \"",
special == tiletype_special::NONE ? "" : enum_item_key_str(special),
special == tiletype_special::NONE ? "" : "\"",
variant == tiletype_variant::NONE ? "" : " variant = \"",
variant == tiletype_variant::NONE ? "" : enum_item_key_str(variant),
variant == tiletype_variant::NONE ? "" : "\"",
material == tiletype_material::NONE ? "" : " material = \"",
material == tiletype_material::NONE ? "" : enum_item_key_str(material),
material == tiletype_material::NONE ? "" : "\""
);
}
else
sprintf(buf, "Terrain value \"%d\" is invalid", targetElem);
contentError(buf, elemTerrain);
}
const char* gameTokenstr = elemTerrain->Attribute("token");
df::tiletype_shape elemShape = StringToTiletypeShape(elemTerrain->Attribute("shape"));
df::tiletype_special elemSpecial = StringToTiletypeSpecial(elemTerrain->Attribute("special"));
df::tiletype_variant elemVariant = StringToTiletypeVariant(elemTerrain->Attribute("variant"));
df::tiletype_material elemMaterial = StringToTiletypeMaterial(elemTerrain->Attribute("material"));
FOR_ENUM_ITEMS(tiletype, i)
{
bool valid = true;
int matchness = INVALID_INDEX;
if (targetElem >= 0)
{
if (i == targetElem)
matchness = 0;
else
valid = false;
}
if (!(gameTokenstr == NULL || gameTokenstr[0] == 0))
{
if (enum_item_key(i) == gameTokenstr)
matchness = 0;
else
valid = false;
}
if (matchness != 0) //this means there's no exact match made.
{
int partialMatch = 0;
if (elemShape != tiletype_shape::NONE)
{
if (ENUM_ATTR(tiletype, shape, i) == elemShape)
partialMatch += PRIORITY_SHAPE;
else
valid = false;
}
if (elemSpecial != tiletype_special::NONE)
{
if (ENUM_ATTR(tiletype, special, i) == elemSpecial)
partialMatch += PRIORITY_SPECIAL;
else
valid = false;
}
if (elemVariant != tiletype_variant::NONE)
{
if (ENUM_ATTR(tiletype, variant, i) == elemVariant)
partialMatch += PRIORITY_VARIANT;
else
valid = false;
}
if (elemMaterial != tiletype_material::NONE)
{
if (ENUM_ATTR(tiletype, material, i) == elemMaterial)
partialMatch += PRIORITY_MATERIAL;
else
valid = false;
}
if (partialMatch > 0 && valid)
matchness = PRIORITY_TOTAL - partialMatch;
}
if (matchness >= 0 && valid)
{
//add it to the lookup vector
lookupKeys.push_back(make_pair(i, matchness));
//increase size if needed
while (configTable.size() <= (uint32_t)i) {
configTable.push_back(nullptr);
}
if (configTable[i] == nullptr) {
configTable[i] = std::make_unique<TerrainConfiguration>();
}
}
}
}
// check we have some terrain types set
int elems = (int)lookupKeys.size();
if (elems == 0) {
return; //nothing to link to
}
vector<bool> formToggle;
formToggle.resize(NUM_FORMS);
// parse weather plate is for a block, log, etc
TiXmlElement* elemForm = elemWallFloorSprite->FirstChildElement("form");
if (elemForm == NULL) {
formToggle[0] = true;
}
for (; elemForm; elemForm = elemForm->NextSiblingElement("form")) {
const char * strForm = elemForm->Attribute("value");
if (strcmp(strForm, "bar") == 0) {
formToggle[FORM_BAR] = true;
}
if (strcmp(strForm, "block") == 0) {
formToggle[FORM_BLOCK] = true;
}
if (strcmp(strForm, "boulder") == 0) {
formToggle[FORM_BOULDER] = true;
}
if (strcmp(strForm, "log") == 0) {
formToggle[FORM_LOG] = true;
}
}
// parse material elements
TiXmlElement* elemMaterial = elemWallFloorSprite->FirstChildElement("material");
if (elemMaterial == NULL) {
// if none, set default terrain sprites for each terrain type
for (int i = 0; i < elems; i++) {
TerrainConfiguration *tConfig = configTable[lookupKeys[i].first].get();
// if that was null we have *really* screwed up earlier
// only update if not by previous configs
for (int j = 0; j < NUM_FORMS; j++) {
if (formToggle[j])
if (tConfig->defaultSprite[j].second == INVALID_INDEX || tConfig->defaultSprite[j].second > lookupKeys[i].second) {
tConfig->defaultSprite[j].first = sprite;
tConfig->defaultSprite[j].second = lookupKeys[i].second;
}
}
}
}
for (; elemMaterial; elemMaterial = elemMaterial->NextSiblingElement("material")) {
// get material type
int elemIndex = lookupMaterialType(elemMaterial->Attribute("value"));
if (elemIndex == INVALID_INDEX) {
contentError("Invalid or missing value attribute", elemMaterial);
continue;
}
// parse subtype elements
TiXmlElement* elemSubtype = elemMaterial->FirstChildElement("subtype");
if (elemSubtype == NULL) {
// if none, set material default for each terrain type
for (int i = 0; i < elems; i++) {
TerrainConfiguration *tConfig = configTable[lookupKeys[i].first].get();
// if that was null we have *really* screwed up earlier
// create a new TerrainMaterialConfiguration if required
// make sure we have room for it first
while (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
// dont make a full size vector in advance- most of the time
// we will only need the first few
tConfig->terrainMaterials.push_back(nullptr);
}
if (tConfig->terrainMaterials[elemIndex] == nullptr) {
tConfig->terrainMaterials[elemIndex] = std::make_unique<TerrainMaterialConfiguration>();
}
// only update if not set by earlier configs,
//FIXME: figure out how to manage priorities here.
for (int j = 0; j < NUM_FORMS; j++) {
if (formToggle[j])
if (tConfig->terrainMaterials[elemIndex]->defaultSprite[j].second == INVALID_INDEX
|| tConfig->terrainMaterials[elemIndex]->defaultSprite[j].second > lookupKeys[i].second)
{
tConfig->terrainMaterials[elemIndex]->defaultSprite[j].first = sprite;
tConfig->terrainMaterials[elemIndex]->defaultSprite[j].second = lookupKeys[i].second;
}
}
}
}
for (; elemSubtype; elemSubtype = elemSubtype->NextSiblingElement("subtype")) {
// get subtype
int subtypeId = lookupMaterialIndex(elemIndex, elemSubtype->Attribute("value"));
if (subtypeId == INVALID_INDEX) {
contentError("Invalid or missing value attribute", elemSubtype);
continue;
}
// set subtype sprite for each terrain type
for (int i = 0; i < elems; i++) {
TerrainConfiguration *tConfig = configTable[lookupKeys[i].first].get();
//if that was null we have *really* screwed up earlier
//create a new TerrainMaterialConfiguration if required
//make sure we have room for it first
while (tConfig->terrainMaterials.size() <= (uint32_t)elemIndex) {
//dont make a full size vector in advance- we wont need it except
//for those who insist on Soap Fortresses
tConfig->terrainMaterials.push_back(nullptr);
}
if (tConfig->terrainMaterials[elemIndex] == nullptr) {
tConfig->terrainMaterials[elemIndex] = std::make_unique<TerrainMaterialConfiguration>();
}
// add to map (if not already present)
for (int j = 0; j < NUM_FORMS; j++) {
if (formToggle[j]) {
if (tConfig->terrainMaterials[elemIndex]->overridingMaterials[j].count(subtypeId))
{
if (tConfig->terrainMaterials[elemIndex]->overridingMaterials[j][subtypeId].second > lookupKeys[i].second)
tConfig->terrainMaterials[elemIndex]->overridingMaterials[j][subtypeId].first = sprite;
tConfig->terrainMaterials[elemIndex]->overridingMaterials[j][subtypeId].second = lookupKeys[i].second;
}
else
{
tConfig->terrainMaterials[elemIndex]->overridingMaterials[j][subtypeId].first = sprite;
tConfig->terrainMaterials[elemIndex]->overridingMaterials[j][subtypeId].second = lookupKeys[i].second;
}
}
}
}
}
}
}
bool addSingleTerrainConfig(TiXmlElement* elemRoot)
{
int basefile = INVALID_INDEX;
const char* filename = elemRoot->Attribute("file");
if (filename != NULL && filename[0] != 0) {
basefile = loadConfigImgFile((char*)filename, elemRoot);
if (basefile == -1) {
return false;
}
}
string elementType = elemRoot->Value();
if (elementType.compare("floors") == 0) {
//parse floors
TiXmlElement* elemFloor = elemRoot->FirstChildElement("floor");
while (elemFloor) {
parseWallFloorSpriteElement(elemFloor, contentLoader->terrainFloorConfigs, basefile, true);
elemFloor = elemFloor->NextSiblingElement("floor");
}
}
if (elementType.compare("walls") == 0) {
//parse walls
TiXmlElement* elemWall = elemRoot->FirstChildElement("wall");
while (elemWall) {
parseWallFloorSpriteElement(elemWall, contentLoader->terrainWallConfigs, basefile, false);
elemWall = elemWall->NextSiblingElement("wall");
}
}
return true;
}
void flushTerrainConfig(vector<std::unique_ptr<TerrainConfiguration>>& config)
{
uint32_t currentsize = (uint32_t)config.size();
config.clear();
if (currentsize < MAX_BASE_TERRAIN + FAKE_TERRAIN_COUNT) {
currentsize = MAX_BASE_TERRAIN + FAKE_TERRAIN_COUNT;
}
while (config.size() < currentsize) {
config.push_back(nullptr);
}
}