Skip to content
Navigation Menu
{{ message }}
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
421 lines (367 loc) · 13.4 KB
/
Copy pathprospector.cpp
File metadata and controls
421 lines (367 loc) · 13.4 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
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
// Produces a list of materials available on the map.
// Options:
// -a : show unrevealed tiles
// -p : don't show plants
// -s : don't show slade
// -t : don't show demon temple
//#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
#include <DFHack.h>
#include <dfhack/extra/MapExtras.h>
#include <dfhack/extra/termutil.h>
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/PluginManager.h>
using namespace DFHack;
typedef std::map<int16_t, unsigned int> MatMap;
typedef std::vector< pair<int16_t, unsigned int> > MatSorter;
typedef std::vector<DFHack::t_feature> FeatureList;
typedef std::vector<DFHack::t_feature*> FeatureListPointer;
typedef std::map<DFHack::DFCoord, FeatureListPointer> FeatureMap;
typedef std::vector<DFHack::df_plant *> PlantList;
#define TO_PTR_VEC(obj_vec, ptr_vec) \
ptr_vec.clear(); \
for (size_t i = 0; i < obj_vec.size(); i++) \
ptr_vec.push_back(&obj_vec[i])
template<template <typename> class P = std::greater >
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);
}
};
// printMats() accepts a vector of pointers to t_matgloss so that it can
// deal t_matgloss and all subclasses.
void printMats(DFHack::Console & con, MatMap &mat,
std::vector<DFHack::t_matgloss*> &materials)
{
unsigned int total = 0;
MatSorter sorting_vector;
for (MatMap::const_iterator it = mat.begin(); it != mat.end(); ++it)
{
sorting_vector.push_back(*it);
}
std::sort(sorting_vector.begin(), sorting_vector.end(),
compare_pair_second<>());
for (MatSorter::const_iterator it = sorting_vector.begin();
it != sorting_vector.end(); ++it)
{
if(it->first >= materials.size())
{
con << "Bad index: " << it->first << " out of "
<< materials.size() << endl;
continue;
}
DFHack::t_matgloss* mat = materials[it->first];
con << std::setw(25) << mat->id << " : " << it->second << std::endl;
total += it->second;
}
con << ">>> TOTAL = " << total << std::endl << std::endl;
}
void printMats(DFHack::Console & con, MatMap &mat,
std::vector<DFHack::t_matgloss> &materials)
{
std::vector<DFHack::t_matgloss*> ptr_vec;
TO_PTR_VEC(materials, ptr_vec);
printMats(con, mat, ptr_vec);
}
void printVeins(DFHack::Console & con, MatMap &mat_map,
DFHack::Materials* mats)
{
MatMap ores;
MatMap gems;
MatMap rest;
for (MatMap::const_iterator it = mat_map.begin(); it != mat_map.end(); ++it)
{
DFHack::t_matglossInorganic &gloss = mats->inorganic[it->first];
if (gloss.isGem())
gems[it->first] = it->second;
else if (gloss.isOre())
ores[it->first] = it->second;
else
rest[it->first] = it->second;
}
std::vector<DFHack::t_matgloss*> ptr_vec;
TO_PTR_VEC(mats->inorganic, ptr_vec);
con << "Ores:" << std::endl;
printMats(con, ores, ptr_vec);
con << "Gems:" << std::endl;
printMats(con, gems, ptr_vec);
con << "Other vein stone:" << std::endl;
printMats(con, rest, ptr_vec);
}
DFhackCExport command_result prospector (Core * c, vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
return "prospector";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("prospect","Show stats of available raw resources. Use option 'all' to show hidden resources.",prospector));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
DFhackCExport command_result prospector (DFHack::Core * c, vector <string> & parameters)
{
bool showHidden = false;
bool showPlants = true;
bool showSlade = true;
bool showTemple = true;
Console & con = c->con;
for(int i = 0; i < parameters.size();i++)
{
if (parameters[0] == "all")
{
showHidden = true;
}
else if(parameters[i] == "help" || parameters[i] == "?")
{
c->con.print("Prints a big list of all the present minerals.\n"
"By default, only the visible part of the map is scanned.\n"
"\n"
"Options:\n"
"all - Scan the whole map, as if it was revealed.\n"
);
return CR_OK;
}
}
uint32_t x_max = 0, y_max = 0, z_max = 0;
c->Suspend();
DFHack::Maps *maps = c->getMaps();
if (!maps->Start())
{
con << "Cannot get map info!" << std::endl;
c->Resume();
return CR_FAILURE;
}
maps->getSize(x_max, y_max, z_max);
MapExtras::MapCache map(maps);
DFHack::Materials *mats = c->getMaterials();
if (!mats->ReadInorganicMaterials())
{
con << "Unable to read inorganic material definitons!" << std::endl;
c->Resume();
return CR_FAILURE;
}
if (showPlants && !mats->ReadOrganicMaterials())
{
con << "Unable to read organic material definitons; plants won't be listed!" << std::endl;
showPlants = false;
}
FeatureList globalFeatures;
FeatureMap localFeatures;
DFHack::t_feature *blockFeatureGlobal = 0;
DFHack::t_feature *blockFeatureLocal = 0;
bool hasAquifer = false;
bool hasDemonTemple = false;
bool hasLair = false;
MatMap baseMats;
MatMap layerMats;
MatMap veinMats;
MatMap plantMats;
MatMap treeMats;
if (!(showSlade && maps->ReadGlobalFeatures(globalFeatures)))
{
con << "Unable to read global features; slade won't be listed!" << std::endl;
}
if (!maps->ReadLocalFeatures(localFeatures))
{
con << "Unable to read local features; adamantine "
<< (showTemple ? "and demon temples " : "")
<< "won't be listed!" << std::endl;
}
uint32_t vegCount = 0;
DFHack::Vegetation *veg = c->getVegetation();
if (showPlants && !veg->Start())
{
con << "Unable to read vegetation; plants won't be listed!" << std::endl;
}
for(uint32_t z = 0; z < z_max; z++)
{
for(uint32_t b_y = 0; b_y < y_max; b_y++)
{
for(uint32_t b_x = 0; b_x < x_max; b_x++)
{
// Get the map block
DFHack::DFCoord blockCoord(b_x, b_y);
MapExtras::Block *b = map.BlockAt(DFHack::DFCoord(b_x, b_y, z));
if (!b || !b->valid)
{
continue;
}
{ // Find features
uint16_t index = b->raw.global_feature;
if (index != -1 && index < globalFeatures.size())
{
blockFeatureGlobal = &globalFeatures[index];
}
index = b->raw.local_feature;
FeatureMap::const_iterator it = localFeatures.find(blockCoord);
if (it != localFeatures.end())
{
FeatureListPointer features = it->second;
if (index != -1 && index < features.size())
{
blockFeatureLocal = features[index];
}
}
}
// Iterate over all the tiles in the block
for(uint32_t y = 0; y < 16; y++)
{
for(uint32_t x = 0; x < 16; x++)
{
DFHack::DFCoord coord(x, y);
DFHack::t_designation des = b->DesignationAt(coord);
DFHack::t_occupancy occ = b->OccupancyAt(coord);
// Skip hidden tiles
if (!showHidden && des.bits.hidden)
{
continue;
}
// Check for aquifer
if (des.bits.water_table)
{
hasAquifer = true;
}
// Check for lairs
if (occ.bits.monster_lair)
{
hasLair = true;
}
uint16_t type = b->TileTypeAt(coord);
const DFHack::TileRow *info = DFHack::getTileRow(type);
if (!info)
{
con << "Bad type: " << type << std::endl;
continue;
}
// We only care about these types
switch (info->shape)
{
case DFHack::WALL:
case DFHack::PILLAR:
case DFHack::FORTIFICATION:
break;
default:
continue;
}
// Count the material type
baseMats[info->material]++;
// Find the type of the tile
switch (info->material)
{
case DFHack::SOIL:
case DFHack::STONE:
layerMats[b->baseMaterialAt(coord)]++;
break;
case DFHack::VEIN:
veinMats[b->veinMaterialAt(coord)]++;
break;
case DFHack::FEATSTONE:
if (blockFeatureLocal && des.bits.feature_local)
{
if (blockFeatureLocal->type == DFHack::feature_Adamantine_Tube
&& blockFeatureLocal->main_material == 0) // stone
{
veinMats[blockFeatureLocal->sub_material]++;
}
else if (showTemple
&& blockFeatureLocal->type == DFHack::feature_Hell_Temple)
{
hasDemonTemple = true;
}
}
if (showSlade && blockFeatureGlobal && des.bits.feature_global
&& blockFeatureGlobal->type == DFHack::feature_Underworld
&& blockFeatureGlobal->main_material == 0) // stone
{
layerMats[blockFeatureGlobal->sub_material]++;
}
break;
case DFHack::OBSIDIAN:
// TODO ?
break;
}
}
}
// Check plants this way, as the other way wasn't getting them all
// and we can check visibility more easily here
if (showPlants)
{
PlantList * plants;
if (maps->ReadVegetation(b_x, b_y, z, plants))
{
for (PlantList::const_iterator it = plants->begin(); it != plants->end(); it++)
{
const DFHack::df_plant & plant = *(*it);
DFHack::DFCoord loc(plant.x, plant.y);
loc = loc % 16;
if (showHidden || !b->DesignationAt(loc).bits.hidden)
{
if(plant.is_shrub)
plantMats[plant.material]++;
else
treeMats[plant.material]++;
}
}
}
}
// Block end
} // block x
// Clean uneeded memory
map.trash();
} // block y
} // z
MatMap::const_iterator it;
con << "Base materials:" << std::endl;
for (it = baseMats.begin(); it != baseMats.end(); ++it)
{
con << std::setw(25) << DFHack::TileMaterialString[it->first] << " : " << it->second << std::endl;
}
std::vector<t_matgloss*> ptr_vec;
TO_PTR_VEC(mats->inorganic, ptr_vec);
con << std::endl << "Layer materials:" << std::endl;
printMats(con, layerMats, ptr_vec);
printVeins(con, veinMats, mats);
if (showPlants)
{
con << "Shrubs:" << std::endl;
printMats(con, plantMats, mats->organic);
con << "Wood in trees:" << std::endl;
printMats(con, treeMats, mats->organic);
}
if (hasAquifer)
{
con << "Has aquifer" << std::endl;
}
if (hasDemonTemple)
{
con << "Has demon temple" << std::endl;
}
if (hasLair)
{
con << "Has lair" << std::endl;
}
// Cleanup
if (showPlants)
{
veg->Finish();
}
mats->Finish();
maps->Finish();
c->Resume();
con << std::endl;
return CR_OK;
}
You can’t perform that action at this time.
