forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprospector.cpp
More file actions
329 lines (305 loc) · 10.7 KB
/
Copy pathprospector.cpp
File metadata and controls
329 lines (305 loc) · 10.7 KB
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
// produces a list of vein materials available on the map. can be run with '-a' modifier to show even unrevealed minerals deep underground
// with -b modifier, it will show base layer materials too
// TODO: use material colors to make the output prettier
// TODO: needs the tiletype filter!
// TODO: tile override materials
// TODO: material types, trees, ice, constructions
// TODO: GUI
#include <iostream>
#include <string.h> // for memset
#include <string>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;
#include <DFHack.h>
#include <dfhack/DFTileTypes.h>
template<template <typename> class P = std::less >
struct compare_pair_first
{
template<class T1, class T2>
bool operator()(const std::pair<T1, T2>& left, const std::pair<T1, T2>& right)
{
return P<T1>()(left.first, right.first);
}
};
template<template <typename> class P = std::less >
struct compare_pair_second
{
template<class T1, class T2>
bool operator()(const std::pair<T1, T2>& left, const std::pair<T1, T2>& right)
{
return P<T2>()(left.second, right.second);
}
};
int main (int argc, const char* argv[])
{
bool showhidden = false;
bool showbaselayers = false;
for(int i = 0; i < argc; i++)
{
string test = argv[i];
if(test == "-a")
{
showhidden = true;
}
else if(test == "-b")
{
showbaselayers = true;
}
else if(test == "-ab" || test == "-ba")
{
showhidden = true;
showbaselayers = true;
}
}
// let's be more useful when double-clicked on windows
#ifndef LINUX_BUILD
showhidden = true;
#endif
uint32_t x_max,y_max,z_max;
/*
DFHack::tiletypes40d tiletypes;
DFHack::designations40d designations;
DFHack::biome_indices40d regionoffsets;
*/
DFHack::mapblock40d Block;
map <int16_t, uint32_t> materials;
materials.clear();
vector<DFHack::t_feature> global_features;
std::map <DFHack::planecoord, std::vector<DFHack::t_feature *> > local_features;
vector< vector <uint16_t> > layerassign;
DFHack::ContextManager DFMgr("Memory.xml");
DFHack::Context *DF;
try
{
DF = DFMgr.getSingleContext();
DF->Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
DFHack::Maps * Maps = DF->getMaps();
DFHack::Materials * Mats = DF->getMaterials();
// init the map
if(!Maps->Start())
{
cerr << "Can't init map." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
Maps->getSize(x_max,y_max,z_max);
if(!Maps->ReadGlobalFeatures(global_features))
{
cerr << "Can't get global features." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
if(!Maps->ReadLocalFeatures(local_features))
{
cerr << "Can't get local features." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
// get stone matgloss mapping
if(!Mats->ReadInorganicMaterials())
{
//DF.DestroyMap();
cerr << "Can't get the materials." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
// get region geology
if(!Maps->ReadGeology( layerassign ))
{
cerr << "Can't get region geology." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
int16_t tempvein [16][16];
vector <DFHack::t_vein> veins;
uint32_t maximum_regionoffset = 0;
uint32_t num_overflows = 0;
// walk the map!
for(uint32_t x = 0; x< x_max;x++)
{
for(uint32_t y = 0; y< y_max;y++)
{
for(uint32_t z = 0; z< z_max;z++)
{
if(!Maps->isValidBlock(x,y,z))
continue;
// read data
Maps->ReadBlock40d(x,y,z, &Block);
//Maps->ReadTileTypes(x,y,z, &tiletypes);
//Maps->ReadDesignations(x,y,z, &designations);
memset(tempvein, -1, sizeof(tempvein));
veins.clear();
Maps->ReadVeins(x,y,z,&veins);
if(showbaselayers)
{
//Maps->ReadRegionOffsets(x,y,z, ®ionoffsets);
// get the layer materials
for(uint32_t xx = 0;xx<16;xx++)
{
for (uint32_t yy = 0; yy< 16;yy++)
{
uint8_t test = Block.designation[xx][yy].bits.biome;
if(test > maximum_regionoffset)
maximum_regionoffset = test;
if( test >= sizeof(Block.biome_indices))
{
num_overflows++;
continue;
}
tempvein[xx][yy] =
layerassign
[Block.biome_indices[test]]
[Block.designation[xx][yy].bits.geolayer_index];
}
}
}
// for each vein
for(int i = 0; i < (int)veins.size();i++)
{
//iterate through vein rows
for(uint32_t j = 0;j<16;j++)
{
//iterate through the bits
for (uint32_t k = 0; k< 16;k++)
{
// and the bit array with a one-bit mask, check if the bit is set
bool set = !!(((1 << k) & veins[i].assignment[j]) >> k);
if(set)
{
// store matgloss
tempvein[k][j] = veins[i].type;
}
}
}
}
// global feature overrides
int16_t idx = Block.global_feature;
if( idx != -1 && uint16_t(idx) < global_features.size() && global_features[idx].type == DFHack::feature_Underworld)
{
for(uint32_t xi = 0 ; xi< 16 ; xi++) for(uint32_t yi = 0 ; yi< 16 ; yi++)
{
if(Block.designation[xi][yi].bits.feature_global)
{
if(global_features[idx].main_material == 0) // stone
{
tempvein[xi][yi] = global_features[idx].sub_material;
}
else
{
tempvein[xi][yi] = -1;
}
}
}
}
idx = Block.local_feature;
if( idx != -1 )
{
DFHack::planecoord pc;
pc.dim.x = x;
pc.dim.y = y;
std::map <DFHack::planecoord, std::vector<DFHack::t_feature *> >::iterator it;
it = local_features.find(pc);
if(it != local_features.end())
{
std::vector<DFHack::t_feature *>& vectr = (*it).second;
if(uint16_t(idx) < vectr.size() && vectr[idx]->type == DFHack::feature_Adamantine_Tube)
for(uint32_t xi = 0 ; xi< 16 ; xi++) for(uint32_t yi = 0 ; yi< 16 ; yi++)
{
if(Block.designation[xi][yi].bits.feature_local && DFHack::isWallTerrain(Block.tiletypes[xi][yi]))
{
if(vectr[idx]->main_material == 0) // stone
{
tempvein[xi][yi] = vectr[idx]->sub_material;
}
else
{
tempvein[xi][yi] = -1;
}
}
}
}
}
// count the material types
for(uint32_t xi = 0 ; xi< 16 ; xi++)
{
for(uint32_t yi = 0 ; yi< 16 ; yi++)
{
// hidden tiles are ignored unless '-a' is provided on the command line
// non-wall tiles are ignored
if( (Block.designation[xi][yi].bits.hidden && !showhidden) || !DFHack::isWallTerrain(Block.tiletypes[xi][yi]))
continue;
if(tempvein[xi][yi] < 0)
continue;
if(materials.count(tempvein[xi][yi]))
{
materials[tempvein[xi][yi]] += 1;
}
else
{
materials[tempvein[xi][yi]] = 1;
}
}
}
}
}
}
// print report
cout << "Maximal regionoffset seen: " << maximum_regionoffset << ".";
if(maximum_regionoffset >= sizeof(Block.biome_indices) )
{
cout << " This is above the regionoffsets array size!" << endl;
cout << "Number of overflows: " << num_overflows;
}
cout << endl;
vector <pair <int16_t, uint32_t> > matss;
map<int16_t, uint32_t>::iterator p;
for(p = materials.begin(); p != materials.end(); p++)
{
if(p->first == -1)
{
cout << "Non-stone" << " : " << p->second << endl;
}
else
{
matss.push_back( pair<int16_t,uint32_t>(p->first, p->second) );
}
}
std::sort(matss.begin(), matss.end(), compare_pair_second<>());
for(int i = 0; i < matss.size();i++)
{
if(matss[i].first >= Mats->inorganic.size())
{
cerr << "Error, material out of bounds: " << matss[i].first << endl;
continue;
}
cout << Mats->inorganic[matss[i].first].id << " : " << matss[i].second << endl;
}
DF->Detach();
#ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl;
cin.ignore();
#endif
return 0;
}