dfhack/plugins/prospector.cpp

355 lines
12 KiB
C++

// 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
2009-09-13 18:02:46 -06:00
//#include <cstdlib>
2009-09-13 18:02:46 -06:00
#include <iostream>
#include <iomanip>
2009-09-13 18:02:46 -06:00
#include <map>
#include <algorithm>
#include <vector>
2009-09-13 18:02:46 -06:00
using namespace std;
2010-05-25 22:48:23 -06:00
#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;
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);
}
};
void printMats(MatMap &mat, std::vector<DFHack::t_matgloss> &materials)
2010-06-20 16:31:43 -06:00
{
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)
2010-06-20 16:31:43 -06:00
{
if(it->first >= materials.size())
{
dfout << "Bad index: " << it->first << " out of " << materials.size() << endl;
continue;
}
DFHack::t_matgloss mat = materials[it->first];
dfout << std::setw(25) << mat.id << " : " << it->second << std::endl;
total += it->second;
2010-06-20 16:31:43 -06:00
}
dfout << ">>> TOTAL = " << total << std::endl << std::endl;
}
DFhackCExport command_result prospector (Core * c, vector <string> & parameters);
2010-06-20 16:31:43 -06:00
DFhackCExport const char * plugin_name ( void )
2009-09-13 18:02:46 -06:00
{
return "prospector";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("prospector","Show stats of available raw resources. Use parameter '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;
if(parameters.size() && parameters[0] == "all")
2009-09-13 18:02:46 -06:00
{
showHidden = true;
2009-09-13 18:02:46 -06:00
}
uint32_t x_max = 0, y_max = 0, z_max = 0;
c->Suspend();
DFHack::Maps *maps = c->getMaps();
if (!maps->Start())
{
dfout << "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())
2010-04-23 17:15:15 -06:00
{
dfout << "Unable to read inorganic material definitons!" << std::endl;
c->Resume();
return CR_FAILURE;
2010-04-23 17:15:15 -06:00
}
if (showPlants && !mats->ReadOrganicMaterials())
{
dfout << "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;
2010-04-23 17:15:15 -06:00
bool hasAquifer = false;
bool hasDemonTemple = false;
bool hasLair = false;
MatMap baseMats;
MatMap layerMats;
MatMap veinMats;
MatMap plantMats;
MatMap treeMats;
if (!(showSlade && maps->ReadGlobalFeatures(globalFeatures)))
2010-04-23 17:15:15 -06:00
{
dfout << "Unable to read global features; slade won't be listed!" << std::endl;
2010-05-22 05:20:58 -06:00
}
if (!maps->ReadLocalFeatures(localFeatures))
2009-09-13 18:02:46 -06:00
{
dfout << "Unable to read local features; adamantine "
<< (showTemple ? "and demon temples " : "")
<< "won't be listed!" << std::endl;
2009-09-13 18:02:46 -06:00
}
uint32_t vegCount = 0;
DFHack::Vegetation *veg = c->getVegetation();
if (showPlants && !veg->Start())
2009-09-13 18:02:46 -06:00
{
dfout << "Unable to read vegetation; plants won't be listed!" << std::endl;
2009-09-13 18:02:46 -06:00
}
for(uint32_t z = 0; z < z_max; z++)
2009-09-13 18:02:46 -06:00
{
for(uint32_t b_y = 0; b_y < y_max; b_y++)
2009-09-13 18:02:46 -06:00
{
for(uint32_t b_x = 0; b_x < x_max; b_x++)
2009-09-13 18:02:46 -06:00
{
// 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)
{
2009-09-13 18:02:46 -06:00
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++)
2009-09-13 18:02:46 -06:00
{
for(uint32_t x = 0; x < 16; x++)
2009-09-13 18:02:46 -06:00
{
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)
2009-09-13 18:02:46 -06:00
{
2011-04-19 17:28:47 -06:00
continue;
}
// Check for aquifer
if (des.bits.water_table)
2011-04-19 17:28:47 -06:00
{
hasAquifer = true;
2011-04-19 17:28:47 -06:00
}
// Check for lairs
if (occ.bits.monster_lair)
2011-04-19 17:28:47 -06:00
{
hasLair = true;
}
uint16_t type = b->TileTypeAt(coord);
const DFHack::TileRow *info = DFHack::getTileRow(type);
if (!info)
{
dfout << "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:
2011-04-19 17:28:47 -06:00
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
2009-09-13 18:02:46 -06:00
{
layerMats[blockFeatureGlobal->sub_material]++;
2009-09-13 18:02:46 -06:00
}
break;
case DFHack::OBSIDIAN:
// TODO ?
break;
2009-09-13 18:02:46 -06:00
}
}
}
// Check plants this way, as the other way wasn't getting them all
// and we can check visibility more easily here
if (showPlants)
2010-04-23 17:15:15 -06:00
{
PlantList * plants;
if (maps->ReadVegetation(b_x, b_y, z, plants))
2010-04-23 17:15:15 -06:00
{
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)
2010-04-23 17:15:15 -06:00
{
if(plant.is_shrub)
2011-05-14 16:26:44 -06:00
plantMats[plant.material]++;
else
2011-05-14 16:26:44 -06:00
treeMats[plant.material]++;
2010-04-23 17:15:15 -06:00
}
}
2010-04-23 17:15:15 -06:00
}
}
// Block end
} // block x
// Clean uneeded memory
map.trash();
} // block y
} // z
MatMap::const_iterator it;
dfout << "Base materials:" << std::endl;
for (it = baseMats.begin(); it != baseMats.end(); ++it)
{
dfout << std::setw(25) << DFHack::TileMaterialString[it->first] << " : " << it->second << std::endl;
}
dfout << std::endl << "Layer materials:" << std::endl;
printMats(layerMats, mats->inorganic);
dfout << "Vein materials:" << std::endl;
printMats(veinMats, mats->inorganic);
if (showPlants)
{
dfout << "Shrubs:" << std::endl;
printMats(plantMats, mats->organic);
dfout << "Wood in trees:" << std::endl;
printMats(treeMats, mats->organic);
}
if (hasAquifer)
{
dfout << "Has aquifer" << std::endl;
}
if (hasDemonTemple)
{
dfout << "Has demon temple" << std::endl;
}
if (hasLair)
2009-09-13 18:02:46 -06:00
{
dfout << "Has lair" << std::endl;
2009-09-13 18:02:46 -06:00
}
// Cleanup
if (showPlants)
2010-06-20 16:31:43 -06:00
{
veg->Finish();
2010-06-20 16:31:43 -06:00
}
mats->Finish();
maps->Finish();
c->Resume();
dfout << std::endl;
return CR_OK;
}