forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileTypes.cpp
377 lines (315 loc) · 11.6 KB
/
TileTypes.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
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include "TileTypes.h"
#include "Export.h"
#include <cassert>
#include <map>
using namespace DFHack;
const int NUM_TILETYPES = 1+(int)ENUM_LAST_ITEM(tiletype);
const int NUM_MATERIALS = 1+(int)ENUM_LAST_ITEM(tiletype_material);
const int NUM_CVTABLES = 1+(int)tiletype_material::CONSTRUCTION;
typedef std::map<df::tiletype_variant, df::tiletype> T_VariantMap;
typedef std::map<std::string, T_VariantMap> T_DirectionMap;
typedef std::map<df::tiletype_special, T_DirectionMap> T_SpecialMap;
typedef std::map<df::tiletype_shape, T_SpecialMap> T_ShapeMap;
typedef T_ShapeMap T_MaterialMap[NUM_MATERIALS];
static bool tables_ready = false;
static T_MaterialMap tile_table;
static df::tiletype tile_to_mat[NUM_CVTABLES][NUM_TILETYPES];
static df::tiletype find_match(
df::tiletype_material mat, df::tiletype_shape shape, df::tiletype_special special,
std::string dir, df::tiletype_variant variant, bool warn
) {
using namespace df::enums::tiletype_shape;
using namespace df::enums::tiletype_special;
if (mat < 0 || mat >= NUM_MATERIALS)
return tiletype::Void;
auto &sh_map = tile_table[mat];
if (!sh_map.count(shape))
{
if (warn)
{
fprintf(
stderr, "NOTE: No shape %s in %s.\n",
enum_item_key(shape).c_str(), enum_item_key(mat).c_str()
);
}
switch (shape)
{
case BROOK_BED:
if (sh_map.count(FORTIFICATION)) { shape = FORTIFICATION; break; }
case FORTIFICATION:
if (sh_map.count(WALL)) { shape = WALL; break; }
return tiletype::Void;
case BROOK_TOP:
case BOULDER:
case PEBBLES:
if (sh_map.count(FLOOR)) { shape = FLOOR; break; }
return tiletype::Void;
default:
return tiletype::Void;
};
}
auto &sp_map = sh_map[shape];
if (!sp_map.count(special))
{
if (warn)
{
fprintf(
stderr, "NOTE: No special %s in %s:%s.\n",
enum_item_key(special).c_str(), enum_item_key(mat).c_str(),
enum_item_key(shape).c_str()
);
}
switch (special)
{
case TRACK:
if (sp_map.count(SMOOTH)) {
special = SMOOTH; break;
}
case df::enums::tiletype_special::NONE:
case NORMAL:
case SMOOTH:
case WET:
case FURROWED:
case RIVER_SOURCE:
case WATERFALL:
case WORN_1:
case WORN_2:
case WORN_3:
if (sp_map.count(NORMAL)) {
special = NORMAL; break;
}
if (sp_map.count(df::enums::tiletype_special::NONE)) {
special = df::enums::tiletype_special::NONE; break;
}
// For targeting construction
if (sp_map.count(SMOOTH)) {
special = SMOOTH; break;
}
default:
return tiletype::Void;
}
}
auto &dir_map = sp_map[special];
if (!dir_map.count(dir))
{
if (warn)
{
fprintf(
stderr, "NOTE: No direction '%s' in %s:%s:%s.\n",
dir.c_str(), enum_item_key(mat).c_str(),
enum_item_key(shape).c_str(), enum_item_key(special).c_str()
);
}
if (dir_map.count("--------"))
dir = "--------";
else if (dir_map.count("NSEW"))
dir = "NSEW";
else if (dir_map.count("N-S-W-E-"))
dir = "N-S-W-E-";
else
dir = dir_map.begin()->first;
}
auto &var_map = dir_map[dir];
if (!var_map.count(variant))
{
if (warn)
{
fprintf(
stderr, "NOTE: No variant '%s' in %s:%s:%s:%s.\n",
enum_item_key(variant).c_str(), enum_item_key(mat).c_str(),
enum_item_key(shape).c_str(), enum_item_key(special).c_str(), dir.c_str()
);
}
variant = var_map.begin()->first;
}
return var_map[variant];
}
static void init_tables()
{
tables_ready = true;
memset(tile_to_mat, 0, sizeof(tile_to_mat));
// Index tile types
FOR_ENUM_ITEMS(tiletype, tt)
{
auto &attrs = df::enum_traits<df::tiletype>::attrs(tt);
if (attrs.material < 0)
continue;
tile_table[attrs.material][attrs.shape][attrs.special][attrs.direction][attrs.variant] = tt;
if (isCoreMaterial(attrs.material))
{
assert(attrs.material < NUM_CVTABLES);
tile_to_mat[attrs.material][tt] = tt;
}
}
// Build mapping of everything to STONE and back
FOR_ENUM_ITEMS(tiletype, tt)
{
auto &attrs = df::enum_traits<df::tiletype>::attrs(tt);
if (!isCoreMaterial(attrs.material))
continue;
if (attrs.material != tiletype_material::STONE)
{
df::tiletype ttm = find_match(
tiletype_material::STONE,
attrs.shape, attrs.special, attrs.direction, attrs.variant,
isStoneMaterial(attrs.material)
);
tile_to_mat[tiletype_material::STONE][tt] = ttm;
if (ttm == tiletype::Void)
fprintf(stderr, "No match for tile %s in STONE.\n",
enum_item_key(tt).c_str());
}
else
{
FOR_ENUM_ITEMS(tiletype_material, mat)
{
if (!isCoreMaterial(mat) || mat == attrs.material)
continue;
df::tiletype ttm = find_match(
mat,
attrs.shape, attrs.special, attrs.direction, attrs.variant,
isStoneMaterial(mat)
);
tile_to_mat[mat][tt] = ttm;
if (ttm == tiletype::Void)
fprintf(stderr, "No match for tile %s in %s.\n",
enum_item_key(tt).c_str(), enum_item_key(mat).c_str());
}
}
}
// Transitive closure via STONE
FOR_ENUM_ITEMS(tiletype_material, mat)
{
if (!isCoreMaterial(mat) || mat == tiletype_material::STONE)
continue;
FOR_ENUM_ITEMS(tiletype, tt)
{
if (!tt || tile_to_mat[mat][tt])
continue;
auto stone = tile_to_mat[tiletype_material::STONE][tt];
if (stone)
tile_to_mat[mat][tt] = tile_to_mat[mat][stone];
}
}
}
df::tiletype DFHack::matchTileMaterial(df::tiletype source, df::tiletype_material tmat)
{
if (!isCoreMaterial(tmat) || !source || source >= NUM_TILETYPES)
return tiletype::Void;
if (!tables_ready)
init_tables();
return tile_to_mat[tmat][source];
}
namespace DFHack
{
df::tiletype findSimilarTileType (const df::tiletype sourceTileType, const df::tiletype_shape tshape)
{
df::tiletype match = tiletype::Void;
int value = 0, matchv = 0;
const df::tiletype_shape cur_shape = tileShape(sourceTileType);
const df::tiletype_material cur_material = tileMaterial(sourceTileType);
const df::tiletype_special cur_special = tileSpecial(sourceTileType);
const df::tiletype_variant cur_variant = tileVariant(sourceTileType);
const TileDirection cur_direction = tileDirection(sourceTileType);
//Shortcut.
//If the current tile is already a shape match, leave.
if (tshape == cur_shape)
return sourceTileType;
#ifdef assert
assert(is_valid_enum_item(sourceTileType));
#endif
// Special case for smooth pillars.
// When you want a smooth wall, no need to search for best match. Just use a pillar instead.
// Choosing the right direction would require knowing neighbors.
if ((tshape == tiletype_shape::WALL) && ((cur_special == tiletype_special::SMOOTH) || (cur_material == tiletype_material::CONSTRUCTION)))
{
switch (cur_material)
{
case tiletype_material::CONSTRUCTION:
return tiletype::ConstructedPillar;
case tiletype_material::FROZEN_LIQUID:
return tiletype::FrozenPillar;
case tiletype_material::MINERAL:
return tiletype::MineralPillar;
case tiletype_material::FEATURE:
return tiletype::FeaturePillar;
case tiletype_material::LAVA_STONE:
return tiletype::LavaPillar;
case tiletype_material::STONE:
return tiletype::StonePillar;
default:
break;
}
}
// Run through until perfect match found or hit end.
FOR_ENUM_ITEMS(tiletype, tt)
{
if (value == (8|4|1))
break;
if (tileShape(tt) == tshape)
{
// Special flag match is mandatory, but only if it might possibly make a difference
if (tileSpecial(tt) != tiletype_special::NONE && cur_special != tiletype_special::NONE && tileSpecial(tt) != cur_special)
continue;
// Special case for constructions.
// Never turn a construction into a non-contruction.
if ((cur_material == tiletype_material::CONSTRUCTION) && (tileMaterial(tt) != cur_material))
continue;
value = 0;
//Material is high-value match
if (cur_material == tileMaterial(tt))
value |= 8;
// Direction is medium value match
if (cur_direction == tileDirection(tt))
value |= 4;
// Variant is low-value match
if (cur_variant == tileVariant(tt))
value |= 1;
// Check value against last match.
if (value > matchv)
{
match = tt;
matchv = value;
}
}
}
// If the selected tile has a variant, then pick a random one
match = findRandomVariant(match);
if (match)
return match;
return sourceTileType;
}
df::tiletype findRandomVariant (const df::tiletype tile)
{
if (tileVariant(tile) == tiletype_variant::NONE)
return tile;
std::vector<df::tiletype> matches;
FOR_ENUM_ITEMS(tiletype, tt)
{
if (tileShape(tt) == tileShape(tile) &&
tileMaterial(tt) == tileMaterial(tile) &&
tileSpecial(tt) == tileSpecial(tile))
matches.push_back(tt);
}
return matches[rand() % matches.size()];
}
}