dfhack/tools/prospector.cpp

222 lines
6.8 KiB
C++

// 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 <integers.h>
#include <string.h> // for memset
#include <string>
#include <vector>
#include <map>
#include <stdio.h>
using namespace std;
#include <DFTypes.h>
#include <DFTileTypes.h>
#include <DFHackAPI.h>
#include <modules/Maps.h>
#include <modules/Materials.h>
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;
map <int16_t, uint32_t> materials;
materials.clear();
vector<DFHack::t_matgloss> stonetypes;
vector< vector <uint16_t> > layerassign;
DFHack::API DF("Memory.xml");
try
{
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);
// get stone matgloss mapping
if(!Mats->ReadInorganicMaterials(stonetypes))
{
//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;
vector <DFHack::t_frozenliquidvein> iceveins;
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->ReadTileTypes(x,y,z, &tiletypes);
Maps->ReadDesignations(x,y,z, &designations);
memset(tempvein, -1, sizeof(tempvein));
veins.clear();
Maps->ReadVeins(x,y,z,veins,iceveins);
if(showbaselayers)
{
Maps->ReadRegionOffsets(x,y,z, &regionoffsets);
// get the layer materials
for(uint32_t xx = 0;xx<16;xx++)
{
for (uint32_t yy = 0; yy< 16;yy++)
{
uint8_t test = designations[xx][yy].bits.biome;
if(test > maximum_regionoffset)
maximum_regionoffset = test;
if( test >= sizeof(regionoffsets))
{
num_overflows++;
continue;
}
tempvein[xx][yy] =
layerassign
[regionoffsets[test]]
[designations[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;
}
}
}
}
// 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( (designations[xi][yi].bits.hidden && !showhidden) || !DFHack::isWallTerrain(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(regionoffsets) )
{
cout << " This is above the regionoffsets array size!" << endl;
cout << "Number of overflows: " << num_overflows;
}
cout << endl;
map<int16_t, uint32_t>::iterator p;
for(p = materials.begin(); p != materials.end(); p++)
{
cout << stonetypes[p->first].id << " : " << p->second << endl;
}
DF.Detach();
#ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl;
cin.ignore();
#endif
return 0;
}