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>
template<template <typename> class P = std::greater > typedef std::map<int16_t, unsigned int> MatMap;
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::greater > typedef std::vector<DFHack::t_feature> FeatureList;
struct compare_pair_second typedef std::vector<DFHack::t_feature*> FeatureListPointer;
{ typedef std::map<DFHack::DFCoord, FeatureListPointer> FeatureMap;
template<class T1, class T2> typedef std::vector<DFHack::t_tree> PlantList;
bool operator()(const std::pair<T1, T2>& left, const std::pair<T1, T2>& right)
bool parseOptions(int argc, char **argv, bool &showHidden, bool &showPlants,
bool &showSlade, bool &showTemple)
{ {
return P<T2>()(left.second, right.second); char c;
opterr = 0;
while ((c = getopt(argc, argv, "apst")) != -1)
{
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;
}
}
} }
};
int main (int argc, const char* argv[]) void printMats(MatMap &mat, std::vector<DFHack::t_matgloss> &materials)
{
bool showhidden = false;
for(int i = 1; i < argc; i++)
{ {
string test = argv[i]; unsigned int total = 0;
if(test == "-a") for (MatMap::const_iterator it = mat.begin(); it != mat.end(); ++it)
{ {
showhidden = true; DFHack::t_matgloss mat = materials[it->first];
std::cout << mat.id << " : " << it->second << std::endl;
total += it->second;
} }
else if(test == "--help") std::cout << ">>> TOTAL = " << total << std::endl << std::endl;
{
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; int main(int argc, char *argv[])
std::map <DFHack::DFCoord, std::vector<DFHack::t_feature *> > local_features; {
bool showHidden = false;
vector< vector <uint16_t> > layerassign; bool showPlants = true;
bool showSlade = true;
bool showTemple = true;
DFHack::ContextManager DFMgr("Memory.xml"); if (!parseOptions(argc, argv, showHidden, showPlants, showSlade, showTemple))
DFHack::Context *DF;
try
{ {
DF = DFMgr.getSingleContext(); return -1;
DF->Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
} }
DFHack::Maps * Maps = DF->getMaps(); uint32_t x_max = 0, y_max = 0, z_max = 0;
DFHack::Materials * Mats = DF->getMaterials(); DFHack::ContextManager manager("Memory.xml");
// init the map DFHack::Context *context = manager.getSingleContext();
if(!Maps->Start()) if (!context->Attach())
{ {
cerr << "Can't init map." << endl; std::cerr << "Unable to attach to DF!" << std::endl;
#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);
if(!Maps->ReadGlobalFeatures(global_features)) DFHack::Maps *maps = context->getMaps();
if (!maps->Start())
{ {
cerr << "Can't get global features." << 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);
MapExtras::MapCache map(maps);
if(!Maps->ReadLocalFeatures(local_features)) DFHack::Materials *mats = context->getMaterials();
if (!mats->ReadInorganicMaterials())
{ {
cerr << "Can't get local 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;
} }
// get stone matgloss mapping if (showPlants && !mats->ReadOrganicMaterials())
if(!Mats->ReadInorganicMaterials())
{ {
//DF.DestroyMap(); std::cerr << "Unable to read organic material definitons; plants won't be listed!" << std::endl;
cerr << "Can't get the materials." << endl; showPlants = false;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
} }
// get region geology FeatureList globalFeatures;
if(!Maps->ReadGeology( layerassign )) 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 region geology." << endl; std::cerr << "Unable to read global features; slade won't be listed!" << std::endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
} }
int16_t tempvein [16][16]; if (!maps->ReadLocalFeatures(localFeatures))
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++) std::cerr << "Unable to read local features; adamantine "
<< (showTemple ? "and demon temples " : "")
<< "won't be listed!" << std::endl;
}
uint32_t vegCount = 0;
DFHack::Vegetation *veg = context->getVegetation();
if (showPlants && !veg->Start(vegCount))
{ {
std::cerr << "Unable to read vegetation; plants won't be listed!" << std::endl;
}
for(uint32_t z = 0; z < z_max; z++) for(uint32_t z = 0; z < z_max; z++)
{ {
if(!Maps->isValidBlock(x,y,z)) for(uint32_t b_y = 0; b_y < y_max; b_y++)
continue;
// read data
Maps->ReadBlock40d(x,y,z, &Block);
DFHack::tiletypes40d & tt = Block.tiletypes;
// hardcoded materials
for(uint32_t xx = 0;xx<16;xx++)
{ {
for (uint32_t yy = 0; yy< 16;yy++) 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)
{ {
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xx][yy]);
if(!DFHack::isWallTerrain(tt[xx][yy]))
continue;
if(Block.designation[xx][yy].bits.hidden && !showhidden)
continue; continue;
if(hardcoded_m.count(mat)) }
{ // Find features
uint16_t index = b->raw.global_feature;
if (index != -1 && index < globalFeatures.size())
{ {
hardcoded_m[mat] += 1; blockFeatureGlobal = &globalFeatures[index];
} }
else
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())
{ {
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;
}
} }
}
// global feature overrides uint16_t type = b->TileTypeAt(coord);
int16_t idx = Block.global_feature; const DFHack::TileRow *info = DFHack::getTileRow(type);
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++)
{ {
if(!DFHack::isWallTerrain(tt[xi][yi])) std::cerr << "Bad type: " << type << std::endl;
continue;
if(Block.designation[xi][yi].bits.hidden && !showhidden)
continue; continue;
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xi][yi]);
if(Block.designation[xi][yi].bits.feature_global && mat == DFHack::FEATSTONE)
{
if(global_features[idx].main_material == 0) // stone
{
int32_t mat2 = global_features[idx].sub_material;
if(layer_m.count(mat2))
{
layer_m[mat2] += 1;
}
else
{
layer_m[mat2] = 1;
}
}
}
}
} }
// vein stones // We only care about these types
memset(tempvein, 0xff, sizeof(tempvein)); switch (info->shape)
veins.clear(); {
Maps->ReadVeins(x,y,z,&veins); case DFHack::WALL:
// for each vein case DFHack::PILLAR:
for(int i = 0; i < (int)veins.size();i++) case DFHack::FORTIFICATION:
{ break;
//iterate through vein rows default:
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; 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
tempvein[k][j] = veins[i].type;
}
}
}
} }
idx = Block.local_feature; // Count the material type
if( idx != -1 ) baseMats[info->material]++;
{
DFHack::DFCoord pc(x,y); // Find the type of the tile
std::map <DFHack::DFCoord, std::vector<DFHack::t_feature *> >::iterator it; switch (info->material)
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++)
{ {
DFHack::TileMaterial mat = DFHack::tileMaterial(tt[xi][yi]); case DFHack::SOIL:
if(Block.designation[xi][yi].bits.feature_local && mat == DFHack::FEATSTONE) case DFHack::STONE:
layerMats[b->baseMaterialAt(coord)]++;
break;
case DFHack::VEIN:
veinMats[b->veinMaterialAt(coord)]++;
break;
case DFHack::FEATSTONE:
if (blockFeatureLocal)
{ {
if(vectr[idx]->main_material == 0) // stone if (blockFeatureLocal->type == DFHack::feature_Adamantine_Tube
&& blockFeatureLocal->main_material == 0) // stone
{ {
tempvein[xi][yi] = vectr[idx]->sub_material; veinMats[blockFeatureLocal->sub_material]++;
} }
else else if (showTemple
&& blockFeatureLocal->type == DFHack::feature_Hell_Temple)
{ {
tempvein[xi][yi] = -1; hasDemonTemple = true;
} }
} }
if (showSlade && blockFeatureGlobal
&& blockFeatureGlobal->type == DFHack::feature_Underworld
&& blockFeatureGlobal->main_material == 0) // stone
{
layerMats[blockFeatureGlobal->sub_material]++;
} }
break;
case DFHack::OBSIDIAN:
// TODO ?
break;
} }
} }
} }
// count the vein material types // Check plants this way, as the other way wasn't getting them all
for(uint32_t xi = 0 ; xi< 16 ; xi++) // and we can check visibility more easily here
if (showPlants)
{ {
for(uint32_t yi = 0 ; yi< 16 ; yi++) PlantList plants;
if (maps->ReadVegetation(b_x, b_y, z, &plants))
{ {
// hidden tiles are ignored unless '-a' is provided on the command line for (PlantList::const_iterator it = plants.begin(); it != plants.end(); it++)
// 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]))
{ {
vein_m[tempvein[xi][yi]] += 1; DFHack::t_tree plant = *it;
} DFHack::DFCoord loc(plant.x, plant.y);
else loc = loc % 16;
if (showHidden || !b->DesignationAt(loc).bits.hidden)
{ {
vein_m[tempvein[xi][yi]] = 1; plantMats[it->material]++;
}
} }
} }
} }
} }
} // Block end
// print report } // block x
// some layer/geology debug stuff // Clean uneeded memory
if(maximum_regionoffset >= sizeof(Block.biome_indices) ) map.trash();
{ } // block y
cerr << "Maximal regionoffset seen: " << maximum_regionoffset << "."; } // z
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; MatMap::const_iterator it;
vector <pair <int16_t, uint32_t> > layers_sort;
vector <pair <int16_t, uint32_t> > hardcoded_sort; std::cout << "Base materials:" << std::endl;
map<int16_t, uint32_t>::iterator p; for (it = baseMats.begin(); it != baseMats.end(); ++it)
// HARDCODED
cout << endl << "Base materials:" << endl;
for(p = hardcoded_m.begin(); p != hardcoded_m.end(); p++)
{
hardcoded_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) );
}
std::sort(hardcoded_sort.begin(), hardcoded_sort.end(), compare_pair_second<>());
for(size_t i = 0; i < hardcoded_sort.size();i++)
{
cout << DFHack::TileMaterialString[hardcoded_sort[i].first] << " : " << hardcoded_sort[i].second << endl;
}
// LAYERS
cout << endl << "Layer materials:" << endl;
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; std::cout << DFHack::TileMaterialString[it->first] << " : " << it->second << std::endl;
} }
else
std::cout << std::endl << "Layer materials:" << std::endl;
printMats(layerMats, mats->inorganic);
std::cout << "Vein materials:" << std::endl;
printMats(veinMats, mats->inorganic);
if (showPlants)
{ {
layers_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) ); std::cout << "Plant materials:" << std::endl;
layers_total += p->second; printMats(plantMats, mats->organic);
} }
}
std::sort(layers_sort.begin(), layers_sort.end(), compare_pair_second<>()); if (hasAquifer)
for(size_t i = 0; i < layers_sort.size();i++)
{
if(layers_sort[i].first >= Mats->inorganic.size())
{ {
cerr << "Error, material out of bounds: " << layers_sort[i].first << endl; std::cout << "Has aquifer" << std::endl;
continue;
} }
cout << Mats->inorganic[layers_sort[i].first].id << " : " << layers_sort[i].second << endl;
} if (hasDemonTemple)
cout << ">>> TOTAL = " << layers_total << endl;
// VEINS
uint32_t veins_total = 0;
cout << endl << "Vein materials:" << endl;
for(p = vein_m.begin(); p != vein_m.end(); p++)
{
if(p->first == -1)
{ {
cout << "Non-stone" << " : " << p->second << endl; std::cout << "Has demon temple" << std::endl;
} }
else
if (hasLair)
{ {
veins_sort.push_back( pair<int16_t,uint32_t>(p->first, p->second) ); std::cout << "Has lair" << std::endl;
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())
{ {
cerr << "Error, material out of bounds: " << veins_sort[i].first << endl; veg->Finish();
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;
} }