Merge pull request #77 from jaxad0127/master.

Here's the finished prospector rewrite
develop
Petr Mrázek 2011-05-01 17:39:10 -07:00
commit a8fb188c09
1 changed files with 254 additions and 304 deletions

@ -1,412 +1,362 @@
// produces a list of vein materials available on the map. can be run with '-a' modifier to show even unrevealed minerals deep underground // Produces a list of materials available on the map.
// with -b modifier, it will show base layer materials too // Options:
// -a : show unrevealed tiles
// TODO: use material colors to make the output prettier // -p : don't show plants
// TODO: needs the tiletype filter! // -s : don't show slade
// TODO: tile override materials // -t : don't show demon temple
// TODO: material types, trees, ice, constructions
// TODO: GUI
#include <cstdlib>
#include <iostream> #include <iostream>
#include <string.h> // for memset
#include <string>
#include <vector>
#include <map> #include <map>
#include <stdio.h> #include <vector>
#include <algorithm>
using namespace std;
using namespace std;
#include <DFHack.h> #include <DFHack.h>
#include <dfhack/DFTileTypes.h> #include <dfhack/extra/MapExtras.h>
typedef std::map<int16_t, unsigned int> MatMap;
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::t_tree> PlantList;
template<template <typename> class P = std::greater > bool parseOptions(int argc, char **argv, bool &showHidden, bool &showPlants,
struct compare_pair_first bool &showSlade, bool &showTemple)
{ {
template<class T1, class T2> char c;
bool operator()(const std::pair<T1, T2>& left, const std::pair<T1, T2>& right) opterr = 0;
while ((c = getopt(argc, argv, "apst")) != -1)
{ {
return P<T1>()(left.first, right.first); switch (c)
{
case 'a':
showHidden = true;
break;
case 'p':
showPlants = false;
break;
case 's':
showSlade = false;
break;
case 't':
showTemple = false;
break;
case '?':
switch (optopt)
{
// For when we take arguments
default:
if (isprint(optopt))
std::cerr << "Unknown option -" << optopt << "!"
<< std::endl;
else
std::cerr << "Unknown option character " << (int) optopt << "!"
<< std::endl;
}
default:
// Um.....
return false;
}
} }
}; }
template<template <typename> class P = std::greater > void printMats(MatMap &mat, std::vector<DFHack::t_matgloss> &materials)
struct compare_pair_second
{ {
template<class T1, class T2> unsigned int total = 0;
bool operator()(const std::pair<T1, T2>& left, const std::pair<T1, T2>& right) for (MatMap::const_iterator it = mat.begin(); it != mat.end(); ++it)
{ {
return P<T2>()(left.second, right.second); DFHack::t_matgloss mat = materials[it->first];
std::cout << mat.id << " : " << it->second << std::endl;
total += it->second;
} }
}; std::cout << ">>> TOTAL = " << total << std::endl << std::endl;
}
int main (int argc, const char* argv[]) int main(int argc, char *argv[])
{ {
bool showhidden = false; bool showHidden = false;
for(int i = 1; i < argc; i++) bool showPlants = true;
bool showSlade = true;
bool showTemple = true;
if (!parseOptions(argc, argv, showHidden, showPlants, showSlade, showTemple))
{ {
string test = argv[i]; return -1;
if(test == "-a")
{
showhidden = true;
}
else if(test == "--help")
{
cout << "This is a prospector tool for the game Dwarf Fortress." << endl
<< "By default, only visible tiles are counted." << endl
<< "Use the parameter '-a' to scan all tiles." << endl;
return 0;
}
} }
uint32_t x_max,y_max,z_max;
DFHack::mapblock40d Block;
map <int16_t, uint32_t> hardcoded_m;
map <int16_t, uint32_t> layer_m;
map <int16_t, uint32_t> vein_m;
vector<DFHack::t_feature> global_features;
std::map <DFHack::DFCoord, std::vector<DFHack::t_feature *> > local_features;
vector< vector <uint16_t> > layerassign; uint32_t x_max = 0, y_max = 0, z_max = 0;
DFHack::ContextManager manager("Memory.xml");
DFHack::ContextManager DFMgr("Memory.xml"); DFHack::Context *context = manager.getSingleContext();
DFHack::Context *DF; if (!context->Attach())
try
{ {
DF = DFMgr.getSingleContext(); std::cerr << "Unable to attach to DF!" << std::endl;
DF->Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cin.ignore(); std::cin.ignore();
#endif #endif
return 1; return 1;
} }
DFHack::Maps * Maps = DF->getMaps(); DFHack::Maps *maps = context->getMaps();
DFHack::Materials * Mats = DF->getMaterials(); if (!maps->Start())
// init the map
if(!Maps->Start())
{ {
cerr << "Can't init map." << endl; std::cerr << "Cannot get map info!" << std::endl;
context->Detach();
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cin.ignore(); std::cin.ignore();
#endif #endif
return 1; return 1;
} }
Maps->getSize(x_max,y_max,z_max); maps->getSize(x_max, y_max, z_max);
MapExtras::MapCache map(maps);
if(!Maps->ReadGlobalFeatures(global_features)) DFHack::Materials *mats = context->getMaterials();
if (!mats->ReadInorganicMaterials())
{ {
cerr << "Can't get global features." << endl; std::cerr << "Unable to read inorganic material definitons!" << std::endl;
context->Detach();
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cin.ignore(); std::cin.ignore();
#endif #endif
return 1; return 1;
}
if (showPlants && !mats->ReadOrganicMaterials())
{
std::cerr << "Unable to read organic material definitons; plants won't be listed!" << std::endl;
showPlants = false;
} }
if(!Maps->ReadLocalFeatures(local_features)) 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;
if (!(showSlade && maps->ReadGlobalFeatures(globalFeatures)))
{ {
cerr << "Can't get local features." << endl; std::cerr << "Unable to read global features; slade won't be listed!" << std::endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
} }
// get stone matgloss mapping
if(!Mats->ReadInorganicMaterials()) if (!maps->ReadLocalFeatures(localFeatures))
{ {
//DF.DestroyMap(); std::cerr << "Unable to read local features; adamantine "
cerr << "Can't get the materials." << endl; << (showTemple ? "and demon temples " : "")
#ifndef LINUX_BUILD << "won't be listed!" << std::endl;
cin.ignore();
#endif
return 1;
} }
// get region geology uint32_t vegCount = 0;
if(!Maps->ReadGeology( layerassign )) DFHack::Vegetation *veg = context->getVegetation();
if (showPlants && !veg->Start(vegCount))
{ {
cerr << "Can't get region geology." << endl; std::cerr << "Unable to read vegetation; plants won't be listed!" << std::endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
} }
int16_t tempvein [16][16]; for(uint32_t z = 0; z < z_max; z++)
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 b_y = 0; b_y < y_max; b_y++)
{ {
for(uint32_t z = 0; z< z_max;z++) for(uint32_t b_x = 0; b_x < x_max; b_x++)
{ {
if(!Maps->isValidBlock(x,y,z)) // 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; continue;
}
// read data { // Find features
Maps->ReadBlock40d(x,y,z, &Block); uint16_t index = b->raw.global_feature;
DFHack::tiletypes40d & tt = Block.tiletypes; if (index != -1 && index < globalFeatures.size())
{
blockFeatureGlobal = &globalFeatures[index];
}
// hardcoded materials index = b->raw.local_feature;
for(uint32_t xx = 0;xx<16;xx++) FeatureMap::const_iterator it = localFeatures.find(blockCoord);
{ if (it != localFeatures.end())
for (uint32_t yy = 0; yy< 16;yy++)
{ {
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xx][yy]); FeatureListPointer features = it->second;
if(!DFHack::isWallTerrain(tt[xx][yy]))
continue; if (index != -1 && index < features.size())
if(Block.designation[xx][yy].bits.hidden && !showhidden)
continue;
if(hardcoded_m.count(mat))
{
hardcoded_m[mat] += 1;
}
else
{ {
hardcoded_m[mat] = 1; blockFeatureLocal = features[index];
} }
} }
} }
// get the layer materials // Iterate over all the tiles in the block
for(uint32_t xx = 0;xx<16;xx++) for(uint32_t y = 0; y < 16; y++)
{ {
for (uint32_t yy = 0; yy< 16;yy++) for(uint32_t x = 0; x < 16; x++)
{ {
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xx][yy]); DFHack::DFCoord coord(x, y);
DFHack::TileShape shape = DFHack::TileShape(tt[xx][yy]); DFHack::t_designation des = b->DesignationAt(coord);
if(mat != DFHack::SOIL && mat != DFHack::STONE) DFHack::t_occupancy occ = b->OccupancyAt(coord);
continue;
if(!DFHack::isWallTerrain(tt[xx][yy])) // Skip hidden tiles
continue; if (!showHidden && des.bits.hidden)
if(Block.designation[xx][yy].bits.hidden && !showhidden)
continue;
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; continue;
} }
uint16_t mat2 = layerassign [Block.biome_indices[test]] [Block.designation[xx][yy].bits.geolayer_index];
if(layer_m.count(mat2)) // Check for aquifer
if (des.bits.water_table)
{ {
layer_m[mat2] += 1; hasAquifer = true;
} }
else
// Check for lairs
if (occ.bits.monster_lair)
{ {
layer_m[mat2] = 1; hasLair = true;
} }
}
} uint16_t type = b->TileTypeAt(coord);
// global feature overrides const DFHack::TileRow *info = DFHack::getTileRow(type);
int16_t idx = Block.global_feature;
if( idx != -1 && uint16_t(idx) < global_features.size() && global_features[idx].type == DFHack::feature_Underworld) if (!info)
{ {
for(uint32_t xi = 0 ; xi< 16 ; xi++) for(uint32_t yi = 0 ; yi< 16 ; yi++) std::cerr << "Bad type: " << type << std::endl;
{
if(!DFHack::isWallTerrain(tt[xi][yi]))
continue; continue;
if(Block.designation[xi][yi].bits.hidden && !showhidden) }
// We only care about these types
switch (info->shape)
{
case DFHack::WALL:
case DFHack::PILLAR:
case DFHack::FORTIFICATION:
break;
default:
continue; continue;
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xi][yi]); }
if(Block.designation[xi][yi].bits.feature_global && mat == DFHack::FEATSTONE)
// Count the material type
baseMats[info->material]++;
// Find the type of the tile
switch (info->material)
{ {
if(global_features[idx].main_material == 0) // stone case DFHack::SOIL:
case DFHack::STONE:
layerMats[b->baseMaterialAt(coord)]++;
break;
case DFHack::VEIN:
veinMats[b->veinMaterialAt(coord)]++;
break;
case DFHack::FEATSTONE:
if (blockFeatureLocal)
{ {
int32_t mat2 = global_features[idx].sub_material; if (blockFeatureLocal->type == DFHack::feature_Adamantine_Tube
if(layer_m.count(mat2)) && blockFeatureLocal->main_material == 0) // stone
{ {
layer_m[mat2] += 1; veinMats[blockFeatureLocal->sub_material]++;
} }
else else if (showTemple
&& blockFeatureLocal->type == DFHack::feature_Hell_Temple)
{ {
layer_m[mat2] = 1; hasDemonTemple = true;
} }
} }
}
}
}
// vein stones if (showSlade && blockFeatureGlobal
memset(tempvein, 0xff, sizeof(tempvein)); && blockFeatureGlobal->type == DFHack::feature_Underworld
veins.clear(); && blockFeatureGlobal->main_material == 0) // stone
Maps->ReadVeins(x,y,z,&veins);
// 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++)
{
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[k][j]);
if(mat != DFHack::VEIN)
continue;
// 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 layerMats[blockFeatureGlobal->sub_material]++;
tempvein[k][j] = veins[i].type;
} }
break;
case DFHack::OBSIDIAN:
// TODO ?
break;
} }
} }
} }
idx = Block.local_feature; // Check plants this way, as the other way wasn't getting them all
if( idx != -1 ) // and we can check visibility more easily here
if (showPlants)
{ {
DFHack::DFCoord pc(x,y); PlantList plants;
std::map <DFHack::DFCoord, std::vector<DFHack::t_feature *> >::iterator it; if (maps->ReadVegetation(b_x, b_y, z, &plants))
it = local_features.find(pc);
if(it != local_features.end())
{ {
std::vector<DFHack::t_feature *>& vectr = (*it).second; for (PlantList::const_iterator it = plants.begin(); it != plants.end(); it++)
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++) DFHack::t_tree plant = *it;
DFHack::DFCoord loc(plant.x, plant.y);
loc = loc % 16;
if (showHidden || !b->DesignationAt(loc).bits.hidden)
{ {
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xi][yi]); plantMats[it->material]++;
if(Block.designation[xi][yi].bits.feature_local && mat == DFHack::FEATSTONE)
{
if(vectr[idx]->main_material == 0) // stone
{
tempvein[xi][yi] = vectr[idx]->sub_material;
}
else
{
tempvein[xi][yi] = -1;
}
}
} }
} }
} }
} }
// Block end
} // block x
// count the vein material types // Clean uneeded memory
for(uint32_t xi = 0 ; xi< 16 ; xi++) map.trash();
{ } // block y
for(uint32_t yi = 0 ; yi< 16 ; yi++) } // z
{
// 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;
// ignore stuff that isn't a vein
if(tempvein[xi][yi] < 0)
continue;
if(vein_m.count(tempvein[xi][yi])) MatMap::const_iterator it;
{
vein_m[tempvein[xi][yi]] += 1;
}
else
{
vein_m[tempvein[xi][yi]] = 1;
}
}
}
}
}
}
// print report
// some layer/geology debug stuff std::cout << "Base materials:" << std::endl;
if(maximum_regionoffset >= sizeof(Block.biome_indices) ) for (it = baseMats.begin(); it != baseMats.end(); ++it)
{ {
cerr << "Maximal regionoffset seen: " << maximum_regionoffset << "."; std::cout << DFHack::TileMaterialString[it->first] << " : " << it->second << std::endl;
cerr << " This is above the regionoffsets array size!" << endl;
cerr << "Number of overflows: " << num_overflows;
cerr << endl;
} }
vector <pair <int16_t, uint32_t> > veins_sort; std::cout << std::endl << "Layer materials:" << std::endl;
vector <pair <int16_t, uint32_t> > layers_sort; printMats(layerMats, mats->inorganic);
vector <pair <int16_t, uint32_t> > hardcoded_sort;
map<int16_t, uint32_t>::iterator p; std::cout << "Vein materials:" << std::endl;
// HARDCODED printMats(veinMats, mats->inorganic);
cout << endl << "Base materials:" << endl;
for(p = hardcoded_m.begin(); p != hardcoded_m.end(); p++) if (showPlants)
{ {
hardcoded_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) ); std::cout << "Plant materials:" << std::endl;
printMats(plantMats, mats->organic);
} }
std::sort(hardcoded_sort.begin(), hardcoded_sort.end(), compare_pair_second<>());
for(size_t i = 0; i < hardcoded_sort.size();i++) if (hasAquifer)
{ {
cout << DFHack::TileMaterialString[hardcoded_sort[i].first] << " : " << hardcoded_sort[i].second << endl; std::cout << "Has aquifer" << std::endl;
} }
// LAYERS
cout << endl << "Layer materials:" << endl; if (hasDemonTemple)
uint32_t layers_total = 0;
for(p = layer_m.begin(); p != layer_m.end(); p++)
{
if(p->first == -1)
{
cout << "Non-stone" << " : " << p->second << endl;
}
else
{
layers_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) );
layers_total += p->second;
}
}
std::sort(layers_sort.begin(), layers_sort.end(), compare_pair_second<>());
for(size_t i = 0; i < layers_sort.size();i++)
{ {
if(layers_sort[i].first >= Mats->inorganic.size()) std::cout << "Has demon temple" << std::endl;
{
cerr << "Error, material out of bounds: " << layers_sort[i].first << endl;
continue;
}
cout << Mats->inorganic[layers_sort[i].first].id << " : " << layers_sort[i].second << endl;
} }
cout << ">>> TOTAL = " << layers_total << endl;
// VEINS if (hasLair)
uint32_t veins_total = 0;
cout << endl << "Vein materials:" << endl;
for(p = vein_m.begin(); p != vein_m.end(); p++)
{ {
if(p->first == -1) std::cout << "Has lair" << std::endl;
{
cout << "Non-stone" << " : " << p->second << endl;
}
else
{
veins_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) );
veins_total += p->second;
}
} }
std::sort(veins_sort.begin(), veins_sort.end(), compare_pair_second<>());
for(size_t i = 0; i < veins_sort.size();i++) // Cleanup
if (showPlants)
{ {
if(veins_sort[i].first >= Mats->inorganic.size()) veg->Finish();
{
cerr << "Error, material out of bounds: " << veins_sort[i].first << endl;
continue;
}
cout << Mats->inorganic[veins_sort[i].first].id << " : " << veins_sort[i].second << endl;
} }
cout << ">>> TOTAL = " << veins_total << endl; mats->Finish();
maps->Finish();
DF->Detach(); context->Detach();
cout << endl << "Happy mining!";
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << " Press any key to finish."; std::cout << " Press any key to finish.";
cin.ignore(); std::cin.ignore();
#endif #endif
cout << endl; std::cout << std::endl;
return 0; return 0;
} }