From 29b00f7c683e40cc2e701d38fadd70f2a6401672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 13 Feb 2010 23:34:18 +0000 Subject: [PATCH] added dfdigger --- tools/CMakeLists.txt | 5 +++ tools/digger.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tools/digger.cpp diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 1e4811df1..d99127df4 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -62,6 +62,11 @@ TARGET_LINK_LIBRARIES(dfitemdump dfhack) ADD_EXECUTABLE(dfbauxite dfbauxite.cpp) TARGET_LINK_LIBRARIES(dfbauxite dfhack) +# digger - designate for digging by tile class +# Author: mizipzor +ADD_EXECUTABLE(dfdigger digger.cpp) +TARGET_LINK_LIBRARIES(dfdigger dfhack) + # itemdesignator - change some item designations (dump, forbid, on-fire) for all items of a given type and material ADD_EXECUTABLE(dfitemdesignator itemdesignator.cpp) TARGET_LINK_LIBRARIES(dfitemdesignator dfhack) diff --git a/tools/digger.cpp b/tools/digger.cpp new file mode 100644 index 000000000..856ac8366 --- /dev/null +++ b/tools/digger.cpp @@ -0,0 +1,104 @@ +// digger.cpp + +// Usage: Call with a list of TileClass ids separated by a space, +// every (visible) tile on the map with that id will be designated for digging. + +#include +#include +#include +#include +#include +using namespace std; + +#include +#include +#include + +int vec_count(vector& vec, uint16_t t) +{ + int count = 0; + for (uint32_t i = 0; i < vec.size(); ++i) + { + if (vec[i] == t) + ++count; + } + return count; +} + +int dig(DFHack::API& DF, vector& targets) +{ + uint32_t x_max,y_max,z_max; + DFHack::t_designation designations[256]; + uint16_t tiles[256]; + uint32_t count = 0; + DF.getSize(x_max,y_max,z_max); + + // 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(DF.isValidBlock(x,y,z)) + { + // read block designations and tiletype + DF.ReadDesignations(x,y,z, (uint32_t *) designations); + DF.ReadTileTypes(x,y,z, (uint16_t *) tiles); + + // check all tiles, if type is in target list and its visible: designate for dig + for (uint32_t i = 0; i < 256; i++) + { + if (designations[i].bits.hidden == 0 && + vec_count(targets, DFHack::tileTypeTable[tiles[i]].c) > 0) + { + //cout << "target found at: "; + //cout << x << "," << y << "," << z << "," << i << endl; + designations[i].bits.dig = 1; + ++count; + } + } + + // write the designations back + // could probably be optimized in the cases where we dont changed anything + DF.WriteDesignations(x,y,z, (uint32_t *) designations); + } + } + } + } + return count; +} + +int main (int argc, const char* argv[]) +{ + vector targets; + for (int i = 1; i < argc; ++i) + { + targets.push_back(atoi(argv[i])); + } + if (targets.size() == 0) + { + cout << "Usage: Call with a list of TileClass ids separated by a space,\n"; + cout << "every (visible) tile on the map with that id will be designated for digging.\n\n"; + } + else + { + DFHack::API DF("Memory.xml"); + if(!DF.Attach()) + { + cerr << "DF not found" << endl; + return 1; + } + DF.InitMap(); + + int count = dig(DF, targets); // <-- important part + cout << count << " targets designated" << endl; + + DF.Detach(); + } + #ifndef LINUX_BUILD + cout << "Done. Press any key to continue" << endl; + cin.ignore(); + #endif + return 0; +} \ No newline at end of file