diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 9c9a29454..5c9869ff8 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -32,9 +32,7 @@ DFHACK_PLUGIN(getplants getplants.cpp) DFHACK_PLUGIN(plants plants.cpp) DFHACK_PLUGIN(fastdwarf fastdwarf.cpp) DFHACK_PLUGIN(prospector prospector.cpp) -DFHACK_PLUGIN(cleanmap cleanmap.cpp) -DFHACK_PLUGIN(cleanitems cleanitems.cpp) -DFHACK_PLUGIN(cleanunits cleanunits.cpp) +DFHACK_PLUGIN(cleaners cleaners.cpp) DFHACK_PLUGIN(weather weather.cpp) DFHACK_PLUGIN(vdig vdig.cpp) DFHACK_PLUGIN(colonies colonies.cpp) diff --git a/plugins/cleaners.cpp b/plugins/cleaners.cpp new file mode 100644 index 000000000..47bd00883 --- /dev/null +++ b/plugins/cleaners.cpp @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::vector; +using std::string; +using namespace DFHack; + +DFhackCExport command_result clean (Core * c, vector & parameters); + +DFhackCExport const char * plugin_name ( void ) +{ + return "cleaners"; +} + +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("clean","Removes contaminants from map tiles, items and creatures.",clean)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +command_result cleanmap (Core * c, bool snow, bool mud) +{ + const uint32_t water_idx = 6; + const uint32_t mud_idx = 12; + vector splatter; + DFHack::Maps *Mapz = c->getMaps(); + + // init the map + if(!Mapz->Start()) + { + c->con << "Can't init map." << std::endl; + c->Resume(); + return CR_FAILURE; + } + + uint32_t x_max,y_max,z_max; + Mapz->getSize(x_max,y_max,z_max); + int num_blocks = 0; + int blocks_total = 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 z = 0; z< z_max;z++) + { + df_block * block = Mapz->getBlock(x,y,z); + if(block) + { + blocks_total ++; + bool cleaned = false; + Mapz->SortBlockEvents(x,y,z,0,0,&splatter); + for(int i = 0; i < 16; i++) + for(int j = 0; j < 16; j++) + { + block->occupancy[i][j].bits.arrow_color = 0; + block->occupancy[i][j].bits.broken_arrows_variant = 0; + } + for(uint32_t i = 0; i < splatter.size(); i++) + { + DFHack::t_spattervein * vein = splatter[i]; + // filter snow + if(!snow && vein->mat1 == water_idx && vein->matter_state == DFHack::state_powder) + continue; + // filter mud + if(!mud && vein->mat1 == mud_idx && vein->matter_state == DFHack::state_solid) + continue; + Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein); + cleaned = true; + } + num_blocks += cleaned; + } + } + } + } + if(num_blocks) + c->con.print("Cleaned %d of %d map blocks.\n", num_blocks, blocks_total); + return CR_OK; +} + +command_result cleanitems (Core * c) +{ + DFHack::Items * Items = c->getItems(); + + vector p_items; + if(!Items->readItemVector(p_items)) + { + c->con.printerr("Can't access the item vector.\n"); + c->Resume(); + return CR_FAILURE; + } + std::size_t numItems = p_items.size(); + + int cleaned_items = 0, cleaned_total = 0; + for (std::size_t i = 0; i < numItems; i++) + { + df_item * itm = p_items[i]; + if(!itm->contaminants) + continue; + if (itm->contaminants->size()) + { + cleaned_items++; + cleaned_total += itm->contaminants->size(); + itm->contaminants->clear(); + } + } + if(cleaned_total) + c->con.print("Removed %d contaminants from %d items.\n", cleaned_total, cleaned_items); + return CR_OK; +} + +command_result cleanunits (Core * c) +{ + DFHack::Creatures * Creatures = c->getCreatures(); + + uint32_t num_creatures; + if (!Creatures->Start(num_creatures)) + { + c->con.printerr("Can't read unit list!\n"); + c->Resume(); + return CR_FAILURE; + } + int cleaned_units = 0, cleaned_total = 0; + for (std::size_t i = 0; i < num_creatures; i++) + { + df_creature *unit = Creatures->creatures->at(i); + int num = unit->contaminants.size(); + if (num) + { + cleaned_units++; + cleaned_total += num; + unit->contaminants.clear(); + } + } + if(cleaned_total) + c->con.print("Removed %d contaminants from %d creatures.\n", cleaned_total, cleaned_units); + return CR_OK; +} + +DFhackCExport command_result clean (Core * c, vector & parameters) +{ + bool help = false; + bool map = false; + bool snow = false; + bool mud = false; + bool units = false; + bool items = false; + for(int i = 0; i < parameters.size();i++) + { + if(parameters[i] == "map") + map = true; + else if(parameters[i] == "units") + units = true; + else if(parameters[i] == "items") + items = true; + else if(parameters[i] == "all") + { + map = true; + items = true; + units = true; + } + if(parameters[i] == "snow") + snow = true; + else if(parameters[i] == "mud") + mud = true; + else if(parameters[i] == "help" ||parameters[i] == "?") + { + help = true; + } + } + if(!map && !units && !items) + help = true; + if(help) + { + c->con.print("Removes contaminants from map tiles, items and creatures.\n" + "Options:\n" + "map - clean the map tiles\n" + "items - clean all items\n" + "units - clean all creatures\n" + "all - clean everything.\n" + "More options for 'map':\n" + "snow - also remove snow\n" + "mud - also remove mud\n" + "Example: clean all mud snow\n" + "This removes all spatter, including mud and snow from map tiles." + ); + return CR_OK; + } + c->Suspend(); + if(map) + cleanmap(c,snow,mud); + if(units) + cleanunits(c); + if(items) + cleanitems(c); + c->Resume(); + return CR_OK; +} diff --git a/plugins/cleanitems.cpp b/plugins/cleanitems.cpp deleted file mode 100644 index 4c7386c0b..000000000 --- a/plugins/cleanitems.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Clean Items : Remove contaminants from all objects -#include -#include -#include -#include -#include -#include -using namespace std; - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace DFHack; - -DFhackCExport command_result df_cleanitems (Core * c, vector & parameters); - -DFhackCExport const char * plugin_name ( void ) -{ - return "cleanitems"; -} - -DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) -{ - commands.clear(); - commands.push_back(PluginCommand("cleanitems", "Removes contaminants from items.", df_cleanitems)); - return CR_OK; -} - -DFhackCExport command_result plugin_shutdown ( Core * c ) -{ - return CR_OK; -} - -DFhackCExport command_result df_cleanitems (Core * c, vector & parameters) -{ - if(parameters.size() > 0) - { - string & p = parameters[0]; - if(p == "?" || p == "help") - { - c->con.print("This utility removes all contaminants from all objects in your fortress.\n"); - return CR_OK; - } - } - c->Suspend(); - DFHack::Items * Items = c->getItems(); - - vector p_items; - if(!Items->readItemVector(p_items)) - { - c->con.printerr("Can't access the item vector.\n"); - c->Resume(); - return CR_FAILURE; - } - std::size_t numItems = p_items.size(); - - int cleaned_items = 0, cleaned_total = 0; - for (std::size_t i = 0; i < numItems; i++) - { - df_item * itm = p_items[i]; - if(!itm->contaminants) - continue; - if (itm->contaminants->size()) - { - cleaned_items++; - cleaned_total += itm->contaminants->size(); - itm->contaminants->clear(); - } - } - c->Resume(); - c->con.print("Done. %d items cleaned of %d contaminants.\n", cleaned_items, cleaned_total); - return CR_OK; -} \ No newline at end of file diff --git a/plugins/cleanmap.cpp b/plugins/cleanmap.cpp deleted file mode 100644 index 3277d0093..000000000 --- a/plugins/cleanmap.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -using std::vector; -using std::string; -using namespace DFHack; - -DFhackCExport command_result cleanmap (Core * c, vector & parameters); - -DFhackCExport const char * plugin_name ( void ) -{ - return "cleanmap"; -} - -DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) -{ - commands.clear(); - commands.push_back(PluginCommand("cleanmap","Cleans the map from various substances.",cleanmap)); - return CR_OK; -} - -DFhackCExport command_result plugin_shutdown ( Core * c ) -{ - return CR_OK; -} - -DFhackCExport command_result cleanmap (Core * c, vector & parameters) -{ - const uint32_t water_idx = 6; - const uint32_t mud_idx = 12; - - bool snow = false; - bool mud = false; - bool help = false; - for(int i = 0; i < parameters.size();i++) - { - if(parameters[i] == "snow") - snow = true; - else if(parameters[i] == "mud") - mud = true; - else if(parameters[i] == "help" ||parameters[i] == "?") - { - help = true; - } - } - if(help) - { - c->con.print("This command cleans the coverings from the map. Snow and mud are ignored by default.\n" - "Options:\n" - "snow - also remove snow\n" - "mud - also remove mud\n" - ); - return CR_OK; - } - c->Suspend(); - vector splatter; - DFHack::Maps *Mapz = c->getMaps(); - - // init the map - if(!Mapz->Start()) - { - c->con << "Can't init map." << std::endl; - c->Resume(); - return CR_FAILURE; - } - - uint32_t x_max,y_max,z_max; - Mapz->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++) - { - df_block * block = Mapz->getBlock(x,y,z); - if(block) - { - Mapz->SortBlockEvents(x,y,z,0,0,&splatter); - for(int i = 0; i < 16; i++) - for(int j = 0; j < 16; j++) - { - block->occupancy[i][j].bits.arrow_color = 0; - block->occupancy[i][j].bits.broken_arrows_variant = 0; - } - for(uint32_t i = 0; i < splatter.size(); i++) - { - DFHack::t_spattervein * vein = splatter[i]; - // filter snow - if(!snow && vein->mat1 == water_idx && vein->matter_state == DFHack::state_powder) - continue; - // filter mud - if(!mud && vein->mat1 == mud_idx && vein->matter_state == DFHack::state_solid) - continue; - Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein); - } - } - } - } - } - c->Resume(); - return CR_OK; -} diff --git a/plugins/cleanunits.cpp b/plugins/cleanunits.cpp deleted file mode 100644 index 0e9171a44..000000000 --- a/plugins/cleanunits.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// Clean Units : Remove contaminants from all creatures -#include -#include -#include -#include -#include -#include -using namespace std; - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace DFHack; - -DFhackCExport command_result df_cleanunits (Core * c, vector & parameters); - -DFhackCExport const char * plugin_name ( void ) -{ - return "cleanunits"; -} - -DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) -{ - commands.clear(); - commands.push_back(PluginCommand("cleanunits", "Removes contaminants from creatures.", df_cleanunits)); - return CR_OK; -} - -DFhackCExport command_result plugin_shutdown ( Core * c ) -{ - return CR_OK; -} - -DFhackCExport command_result df_cleanunits (Core * c, vector & parameters) -{ - if(parameters.size() > 0) - { - string & p = parameters[0]; - if(p == "?" || p == "help") - { - c->con.print("This utility removes all contaminants from all creatures in your fortress.\n"); - return CR_OK; - } - } - c->Suspend(); - DFHack::Creatures * Creatures = c->getCreatures(); - - uint32_t num_creatures; - if (!Creatures->Start(num_creatures)) - { - c->con.printerr("Can't read unit list!\n"); - c->Resume(); - return CR_FAILURE; - } - - int cleaned_units = 0, cleaned_total = 0; - for (std::size_t i = 0; i < num_creatures; i++) - { - df_creature *unit = Creatures->creatures->at(i); - int num = unit->contaminants.size(); - if (num) - { - cleaned_units++; - cleaned_total += num; - unit->contaminants.clear(); - } - } - c->Resume(); - c->con.print("Done. %d creatures cleaned of %d contaminants.\n", cleaned_units, cleaned_total); - return CR_OK; -} \ No newline at end of file