diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 38f7ab382..10a3b3d32 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -44,6 +44,8 @@ DFHACK_PLUGIN(autodump autodump.cpp) DFHACK_PLUGIN(cleanowned cleanowned.cpp) DFHACK_PLUGIN(deramp deramp.cpp) DFHACK_PLUGIN(flows flows.cpp) +DFHACK_PLUGIN(filltraffic filltraffic.cpp) +DFHACK_PLUGIN(buildprobe buildprobe.cpp) # this is the skeleton plugin. If you want to make your own, make a copy and then change it OPTION(BUILD_SKELETON "Build the skeleton plugin." OFF) diff --git a/plugins/buildprobe.cpp b/plugins/buildprobe.cpp new file mode 100644 index 000000000..0c7fbb7fd --- /dev/null +++ b/plugins/buildprobe.cpp @@ -0,0 +1,144 @@ +//Quick building occupancy flag test. +//Individual bits had no apparent meaning. Assume it's an enum, set by number. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using std::vector; +using std::string; +using std::stack; +using namespace DFHack; + +DFhackCExport DFHack::command_result readFlag (Core * c, vector & parameters); +DFhackCExport DFHack::command_result writeFlag (Core * c, vector & parameters); + +DFhackCExport const char * plugin_name ( void ) +{ + return "buildprobe"; +} + +DFhackCExport DFHack::command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("bshow","Output building occupancy value",readFlag)); + commands.push_back(PluginCommand("bset","Set building occupancy value",writeFlag)); + return CR_OK; +} + +DFhackCExport DFHack::command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +DFhackCExport DFHack::command_result readFlag (Core * c, vector & parameters) +{ + c->Suspend(); + + DFHack::Maps * Maps = c->getMaps(); + DFHack::Gui * Gui = c->getGui(); + // init the map + if(!Maps->Start()) + { + c->con.printerr("Can't init map. Make sure you have a map loaded in DF.\n"); + c->Resume(); + return CR_FAILURE; + } + + int32_t cx, cy, cz; + Gui->getCursorCoords(cx,cy,cz); + while(cx == -30000) + { + c->con.printerr("Cursor is not active.\n"); + c->Resume(); + return CR_FAILURE; + } + + DFHack::DFCoord cursor = DFHack::DFCoord(cx,cy,cz); + + MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps); + DFHack::t_occupancy oc = MCache->occupancyAt(cursor); + + c->con.print("Current Value: %d\n", oc.bits.building); + + c->Resume(); + return CR_OK; +} + +DFhackCExport DFHack::command_result writeFlag (Core * c, vector & parameters) +{ + if (parameters.size() == 0) + { + c->con.print("No value specified\n"); + return CR_FAILURE; + } + + if (parameters[0] == "help" || parameters[0] == "?") + { + c->con.print("Set the building occupancy flag.\n" + "Value must be between 0 and 7, inclusive.\n"); + return CR_OK; + } + + char value; + + switch (parameters[0][0]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + value = parameters[0][0] - '0'; + break; + + default: + c->con.print("Invalid value specified\n"); + return CR_FAILURE; + break; //Redundant. + } + + c->Suspend(); + + DFHack::Maps * Maps = c->getMaps(); + DFHack::Gui * Gui = c->getGui(); + // init the map + if(!Maps->Start()) + { + c->con.printerr("Can't init map. Make sure you have a map loaded in DF.\n"); + c->Resume(); + return CR_FAILURE; + } + + int32_t cx, cy, cz; + Gui->getCursorCoords(cx,cy,cz); + while(cx == -30000) + { + c->con.printerr("Cursor is not active.\n"); + c->Resume(); + return CR_FAILURE; + } + + DFHack::DFCoord cursor = DFHack::DFCoord(cx,cy,cz); + + MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps); + DFHack::t_occupancy oc = MCache->occupancyAt(cursor); + + oc.bits.building = value; + MCache->setOccupancyAt(cursor, oc); + MCache->WriteAll(); + + c->Resume(); + return CR_OK; +} \ No newline at end of file diff --git a/plugins/filltraffic.cpp b/plugins/filltraffic.cpp new file mode 100644 index 000000000..ed8e1130d --- /dev/null +++ b/plugins/filltraffic.cpp @@ -0,0 +1,303 @@ +// Wide-area traffic designation utility. +// Flood-fill from cursor or fill entire map. + +#include //For toupper(). +#include +#include +#include +#include +#include +#include +#include +#include +#include +using std::stack; +using MapExtras::MapCache; +using namespace DFHack; + +DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector & params); +DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector & params); + +DFhackCExport const char * plugin_name ( void ) +{ + return "filltraffic"; +} + +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("filltraffic","Flood-fill with selected traffic designation from cursor",filltraffic)); + commands.push_back(PluginCommand("tiletraffic","Set traffic for all tiles based on given criteria",tiletraffic)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector & params) +{ + //Maximum map size. + uint32_t x_max,y_max,z_max; + //Source and target traffic types. + e_traffic source = traffic_normal; + e_traffic target = traffic_normal; + //Option flags + bool updown = false; + bool checkpit = true; + bool checkbuilding = true; + + //Loop through parameters + for(int i = 0; i < params.size();i++) + { + if(params[i] == "help" || params[i] == "?") + { + c->con.print("Flood-fill selected traffic type from the cursor.\n" + "Traffic Type Codes:\n" + " H: High Traffic\n" + " N: Normal Traffic\n" + " L: Low Traffic\n" + " R: Restricted Traffic\n" + "Other Options:\n" + " X: Fill accross z-levels.\n" + " B: Include buildings and stockpiles.\n" + " P: Include empty space.\n" + ); + return CR_OK; + } + + switch (toupper(params[i][0])) + { + case 'H': + target = traffic_high; break; + case 'N': + target = traffic_normal; break; + case 'L': + target = traffic_low; break; + case 'R': + target = traffic_restricted; break; + case 'X': + updown = true; break; + case 'B': + checkbuilding = false; break; + case 'P': + checkpit = false; break; + } + } + + //Initialization. + c->Suspend(); + + DFHack::Maps * Maps = c->getMaps(); + DFHack::Gui * Gui = c->getGui(); + // init the map + if(!Maps->Start()) + { + c->con.printerr("Can't init map. Make sure you have a map loaded in DF.\n"); + c->Resume(); + return CR_FAILURE; + } + + int32_t cx, cy, cz; + Maps->getSize(x_max,y_max,z_max); + uint32_t tx_max = x_max * 16; + uint32_t ty_max = y_max * 16; + Gui->getCursorCoords(cx,cy,cz); + while(cx == -30000) + { + c->con.printerr("Cursor is not active.\n"); + c->Resume(); + return CR_FAILURE; + } + + DFHack::DFCoord xy ((uint32_t)cx,(uint32_t)cy,cz); + MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps); + + DFHack::t_designation des = MCache->designationAt(xy); + int16_t tt = MCache->tiletypeAt(xy); + DFHack::t_occupancy oc; + + if (checkbuilding) + oc = MCache->occupancyAt(xy); + + source = des.bits.traffic; + if(source == target) + { + c->con.printerr("This tile is already set to the target traffic type.\n"); + delete MCache; + c->Resume(); + return CR_FAILURE; + } + + if(DFHack::isWallTerrain(tt)) + { + c->con.printerr("This tile is a wall. Please select a passable tile.\n"); + delete MCache; + c->Resume(); + return CR_FAILURE; + } + + if(checkpit && DFHack::isOpenTerrain(tt)) + { + c->con.printerr("This tile is a hole. Please select a passable tile.\n"); + delete MCache; + c->Resume(); + return CR_FAILURE; + } + + if(checkbuilding && oc.bits.building) + { + c->con.printerr("This tile contains a building. Please select an empty tile.\n"); + delete MCache; + c->Resume(); + return CR_FAILURE; + } + + c->con.print("%d/%d/%d ... FILLING!\n", cx,cy,cz); + + //Naive four-way or six-way flood fill with possible tiles on a stack. + stack flood; + flood.push(xy); + + while(!flood.empty()) + { + xy = flood.top(); + flood.pop(); + + des = MCache->designationAt(xy); + if (des.bits.traffic != source) continue; + + tt = MCache->tiletypeAt(xy); + + if(DFHack::isWallTerrain(tt)) continue; + if(checkpit && DFHack::isOpenTerrain(tt)) continue; + + if (checkbuilding) + { + oc = MCache->occupancyAt(xy); + if(oc.bits.building) continue; + } + + //This tile is ready. Set its traffic level and add surrounding tiles. + if (MCache->testCoord(xy)) + { + des.bits.traffic = target; + MCache->setDesignationAt(xy,des); + + if (xy.x > 0) + { + flood.push(DFHack::DFCoord(xy.x - 1, xy.y, xy.z)); + } + if (xy.x < tx_max - 1) + { + flood.push(DFHack::DFCoord(xy.x + 1, xy.y, xy.z)); + } + if (xy.y > 0) + { + flood.push(DFHack::DFCoord(xy.x, xy.y - 1, xy.z)); + } + if (xy.y < ty_max - 1) + { + flood.push(DFHack::DFCoord(xy.x, xy.y + 1, xy.z)); + } + + if (updown) + { + if (xy.z > 0 && DFHack::LowPassable(tt)) + { + flood.push(DFHack::DFCoord(xy.x, xy.y, xy.z - 1)); + } + if (xy.z < z_max && DFHack::HighPassable(tt)) + { + flood.push(DFHack::DFCoord(xy.x, xy.y, xy.z + 1)); + } + } + + } + } + + MCache->WriteAll(); + c->Resume(); + return CR_OK; +} + +enum e_checktype {no_check, check_equal, check_nequal}; + +DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector & params) +{ + //Target traffic types. + e_traffic target = traffic_normal; + //!!! Options Later !!! + + //Loop through parameters + for(int i = 0; i < params.size();i++) + { + if(params[i] == "help" || params[i] == "?") + { + c->con.print("Set traffic types for all tiles on the map.\n" + "Traffic Type Codes:\n" + " H: High Traffic\n" + " N: Normal Traffic\n" + " L: Low Traffic\n" + " R: Restricted Traffic\n" + ); + return CR_OK; + } + + switch (toupper(params[i][0])) + { + case 'H': + target = traffic_high; break; + case 'N': + target = traffic_normal; break; + case 'L': + target = traffic_low; break; + case 'R': + target = traffic_restricted; break; + } + } + + //Initialization. + c->Suspend(); + + DFHack::Maps * Maps = c->getMaps(); + DFHack::Gui * Gui = c->getGui(); + // init the map + if(!Maps->Start()) + { + c->con.printerr("Can't init map. Make sure you have a map loaded in DF.\n"); + c->Resume(); + return CR_FAILURE; + } + + //Maximum map size. + uint32_t x_max,y_max,z_max; + Maps->getSize(x_max,y_max,z_max); + uint32_t tx_max = x_max * 16; + uint32_t ty_max = y_max * 16; + + MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps); + + c->con.print("Entire map ... FILLING!\n"); + + //Loop through every single tile + for(uint32_t x = 0; x <= tx_max; x++) + { + for(uint32_t y = 0; y <= ty_max; y++) + { + for(uint32_t z = 0; z <= z_max; z++) + { + DFHack::DFCoord tile = DFHack::DFCoord(x, y, z); + DFHack::t_designation des = MCache->designationAt(tile); + + des.bits.traffic = target; + MCache->setDesignationAt(tile, des); + } + } + } + + MCache->WriteAll(); + c->Resume(); + return CR_OK; +} \ No newline at end of file