diff --git a/library/include/dfhack/modules/Materials.h b/library/include/dfhack/modules/Materials.h index 813ac3585..5c77659be 100644 --- a/library/include/dfhack/modules/Materials.h +++ b/library/include/dfhack/modules/Materials.h @@ -369,7 +369,7 @@ namespace DFHack bool ReadDescriptorColors(void); bool ReadOthers (void); - void ReadAllMaterials(void); + bool ReadAllMaterials(void); std::string getType(const t_material & mat); std::string getDescription(const t_material & mat); diff --git a/library/modules/Materials.cpp b/library/modules/Materials.cpp index 09f275777..55145f002 100644 --- a/library/modules/Materials.cpp +++ b/library/modules/Materials.cpp @@ -341,16 +341,18 @@ bool Materials::ReadCreatureTypesEx (void) return true; } -void Materials::ReadAllMaterials(void) +bool Materials::ReadAllMaterials(void) { - this->ReadInorganicMaterials(); - this->ReadOrganicMaterials(); - this->ReadWoodMaterials(); - this->ReadPlantMaterials(); - this->ReadCreatureTypes(); - this->ReadCreatureTypesEx(); - this->ReadDescriptorColors(); - this->ReadOthers(); + bool ok = true; + ok &= this->ReadInorganicMaterials(); + ok &= this->ReadOrganicMaterials(); + ok &= this->ReadWoodMaterials(); + ok &= this->ReadPlantMaterials(); + ok &= this->ReadCreatureTypes(); + ok &= this->ReadCreatureTypesEx(); + ok &= this->ReadDescriptorColors(); + ok &= this->ReadOthers(); + return ok; } /// miserable pile of magic. The material system is insane. diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index d1204588f..3ca16df71 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -144,3 +144,6 @@ DFHACK_PLUGIN(mode mode.cpp) DFHACK_PLUGIN(liquids liquids.cpp) DFHACK_PLUGIN(tubefill tubefill.cpp) DFHACK_PLUGIN(autodump autodump.cpp) +DFHACK_PLUGIN(cleanowned cleanowned.cpp) +DFHACK_PLUGIN(cleartask cleartask.cpp) +DFHACK_PLUGIN(deramp deramp.cpp) diff --git a/plugins/cleanowned.cpp b/plugins/cleanowned.cpp index f54d3999a..094f3a4df 100644 --- a/plugins/cleanowned.cpp +++ b/plugins/cleanowned.cpp @@ -2,137 +2,151 @@ * Confiscates and dumps garbage owned by dwarfs. */ -#include -#include -#include #include #include #include +#include using namespace std; -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace DFHack; + +DFhackCExport command_result df_cleanowned (Core * c, vector & parameters); + +DFhackCExport const char * plugin_name ( void ) +{ + return "cleanowned"; +} + +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("cleanowned", + "Confiscates and dumps garbage owned by dwarfs.", + df_cleanowned)); + return CR_OK; +} -int main (int argc, char *argv[]) +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +typedef std::map coordmap; + +DFhackCExport command_result df_cleanowned (Core * c, vector & parameters) { - bool temporary_terminal = TemporaryTerminal(); bool dump_scattered = false; bool confiscate_all = false; bool dry_run = false; int wear_dump_level = 65536; - for(int i = 1; i < argc; i++) + for(int i = 0; i < parameters.size(); i++) { - char *arg = argv[i]; - if (arg[0] != '-') - continue; - - for (; *arg; arg++) { - switch (arg[0]) { - case 'd': - dry_run = true; - break; - case 'l': - dump_scattered = true; - break; - case 'a': - confiscate_all = true; - break; - case 'x': - wear_dump_level = 1; - break; - case 'X': - wear_dump_level = 2; - break; - } + string & param = parameters[i]; + if(param == "dryrun") + dry_run = true; + else if(param == "scattered") + dump_scattered = true; + else if(param == "all") + confiscate_all = true; + else if(param == "x") + wear_dump_level = 1; + else if(param == "X") + wear_dump_level = 1; + else if(param == "?" || param == "help") + { + c->con.print("Oh no! Someone has to write the help text!\n"); + return CR_OK; + } + else + { + c->con.printerr("Parameter '%s' is not valid. See 'cleanowned help'.\n",param.c_str()); + return CR_FAILURE; } } + DFHack::Materials *Materials = c->getMaterials(); + DFHack::Items *Items = c->getItems(); + DFHack::Creatures *Creatures = c->getCreatures(); + DFHack::Translation *Tran = c->getTranslation(); - DFHack::Process * p; - unsigned int i; - DFHack::ContextManager DFMgr("Memory.xml"); - DFHack::Context * DF; - try - { - DF = DFMgr.getSingleContext(); - DF->Attach(); - } - catch (exception& e) + uint32_t num_creatures; + bool ok = true; + ok &= Materials->ReadAllMaterials(); + ok &= Creatures->Start(num_creatures); + ok &= Tran->Start(); + + vector p_items; + ok &= Items->readItemVector(p_items); + if(!ok) { - cerr << e.what() << endl; - if(temporary_terminal) - cin.ignore(); - return 1; + c->con.printerr("Can't continue due to offset errors.\n"); + c->Resume(); + return CR_FAILURE; } + c->con.print("Found total %d items.\n", p_items.size()); - DFHack::VersionInfo * mem = DF->getMemoryInfo(); - DFHack::Materials *Materials = DF->getMaterials(); - DFHack::Items *Items = DF->getItems(); - DFHack::Creatures *Creatures = DF->getCreatures(); - DFHack::Translation *Tran = DF->getTranslation(); - - Materials->ReadAllMaterials(); - uint32_t num_creatures; - Creatures->Start(num_creatures); - Tran->Start(); - - p = DF->getProcess(); - DFHack::OffsetGroup* itemGroup = mem->getGroup("Items"); - unsigned vector_addr = itemGroup->getAddress("items_vector"); - DFHack::DfVector p_items (p, vector_addr); - uint32_t size = p_items.size(); - - printf("Found total %d items.\n", size); - - for (i=0;ireadItem(curItem, itm); bool confiscate = false; bool dump = false; - if (!itm.base.flags.owned) { + if (!itm.base->flags.owned) + { int32_t owner = Items->getItemOwnerID(itm); - if (owner >= 0) { - printf("Fixing a misflagged item: "); + if (owner >= 0) + { + c->con.print("Fixing a misflagged item: "); confiscate = true; } else + { continue; + } } std::string name = Items->getItemClass(itm.matdesc.itemType); - if (itm.base.flags.rotten) + if (itm.base->flags.rotten) { - printf("Confiscating a rotten item: \t"); + c->con.print("Confiscating a rotten item: \t"); confiscate = true; } - else if (itm.base.flags.on_ground && + else if (itm.base->flags.on_ground && (name == "food" || name == "meat" || name == "plant")) { - printf("Confiscating a dropped foodstuff: \t"); + c->con.print("Confiscating a dropped foodstuff: \t"); confiscate = true; } else if (itm.wear_level >= wear_dump_level) { - printf("Confiscating and dumping a worn item: \t"); + c->con.print("Confiscating and dumping a worn item: \t"); confiscate = true; dump = true; } - else if (dump_scattered && itm.base.flags.on_ground) + else if (dump_scattered && itm.base->flags.on_ground) { - printf("Confiscating and dumping litter: \t"); + c->con.print("Confiscating and dumping litter: \t"); confiscate = true; dump = true; } else if (confiscate_all) { - printf("Confiscating: \t"); + c->con.print("Confiscating: \t"); confiscate = true; } @@ -140,14 +154,14 @@ int main (int argc, char *argv[]) { if (!dry_run) { if (!Items->removeItemOwner(itm, Creatures)) - printf("(unsuccessfully) "); + c->con.print("(unsuccessfully) "); if (dump) - itm.base.flags.dump = 1; + itm.base->flags.dump = 1; Items->writeItem(itm); } - printf( + c->con.print( "%s (wear %d)", Items->getItemDescription(itm, Materials).c_str(), itm.wear_level @@ -167,10 +181,10 @@ int main (int argc, char *argv[]) info += std::string(" '") + temp.name.nickname + "'"; info += " "; info += Tran->TranslateName(temp.name,false); - printf(", owner %s", info.c_str()); + c->con.print(", owner %s", info.c_str()); } - printf("\n"); + c->con.print("\n"); /* printf( "%5d: %08x %08x (%d,%d,%d) #%08x [%d] %s - %s %s\n", @@ -185,10 +199,5 @@ int main (int argc, char *argv[]) */ } } - if(temporary_terminal) - { - cout << "Done. Press any key to continue" << endl; - cin.ignore(); - } - return 0; + return CR_OK; } diff --git a/plugins/cleartask.cpp b/plugins/cleartask.cpp index 0a9e521bf..8eb3ff72d 100644 --- a/plugins/cleartask.cpp +++ b/plugins/cleartask.cpp @@ -8,70 +8,62 @@ #include using namespace std; -#include -#include -#include +#include +#include +#include +#include +#include +#include #include +using namespace DFHack; -int main () +DFhackCExport command_result df_cleartask (Core * c, vector & parameters); + +DFhackCExport const char * plugin_name ( void ) { - bool temporary_terminal = TemporaryTerminal(); - DFHack::Process * p; - unsigned int i; - DFHack::ContextManager DFMgr("Memory.xml"); - DFHack::Context * DF; - DFHack::Items * Items; - try - { - DF = DFMgr.getSingleContext(); - DF->Attach(); - } - catch (exception& e) - { - cerr << e.what() << endl; - if(temporary_terminal) - cin.ignore(); - return 1; - } + return "cleartask"; +} - p = DF->getProcess(); +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("cleartask", + "Clears the \"tasked\" flag on all items. This is dangerous. Only use after reclaims.", + df_cleartask)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +DFhackCExport command_result df_cleartask (Core * c, vector & parameters) +{ + c->Suspend(); + DFHack::Items * Items = c->getItems(); uint32_t item_vec_offset = 0; - try - { - Items = DF->getItems(); - DFHack::OffsetGroup* itemGroup = p->getDescriptor()->getGroup("Items"); - item_vec_offset = itemGroup->getAddress("items_vector"); - } - catch(DFHack::Error::All & e) + vector p_items; + if(!Items->readItemVector(p_items)) { - cerr << "Fatal error, exiting :(" << endl << e.what() << endl; - if(temporary_terminal) - cin.ignore(); - return 1; + c->con.printerr("Can't read items...\n"); + c->Resume(); + return CR_FAILURE; } - DFHack::DfVector p_items (p, item_vec_offset); - uint32_t size = p_items.size(); - int numtasked = 0; - for (i=0;ireadItem(p_items[i],temp); - DFHack::t_itemflags & flags = temp.base.flags; - if (flags.in_job) + if (ptr->flags.in_job) { - flags.in_job = 0; - Items->writeItem(temp); + ptr->flags.in_job = 0; numtasked++; } } - cout << "Found and untasked " << numtasked << " items." << endl; - - if(temporary_terminal) - { - cout << "Done. Press any key to continue" << endl; - cin.ignore(); - } - return 0; + c->con.print("Found and untasked %d items.\n", numtasked); + c->Resume(); + return CR_OK; } diff --git a/plugins/deramp.cpp b/plugins/deramp.cpp index d660c9ed2..3f6fd73c3 100644 --- a/plugins/deramp.cpp +++ b/plugins/deramp.cpp @@ -7,14 +7,37 @@ #include #include using namespace std; +#include +#include +#include +#include +#include +#include +using namespace DFHack; -#include -#include -#include +DFhackCExport command_result df_deramp (Core * c, vector & parameters); -int main (void) +DFhackCExport const char * plugin_name ( void ) +{ + return "deramp"; +} + +DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) +{ + commands.clear(); + commands.push_back(PluginCommand("deramp", + "De-ramp. All ramps marked for removal are replaced with floors.", + df_deramp)); + return CR_OK; +} + +DFhackCExport command_result plugin_shutdown ( Core * c ) +{ + return CR_OK; +} + +DFhackCExport command_result df_deramp (Core * c, vector & parameters) { - bool temporary_terminal = TemporaryTerminal(); uint32_t x_max,y_max,z_max; uint32_t num_blocks = 0; uint32_t bytes_read = 0; @@ -29,33 +52,15 @@ int main (void) int count=0; int countbad=0; - DFHack::ContextManager DFMgr("Memory.xml"); - DFHack::Context *DF = DFMgr.getSingleContext(); - - //sanity check - assert( sizeof(designations) == (16*16*sizeof(DFHack::t_designation)) ); - - //Init - try - { - DF->Attach(); - } - catch (exception& e) - { - cerr << e.what() << endl; - if(temporary_terminal) - cin.ignore(); - return 1; - } - DFHack::Maps *Mapz = DF->getMaps(); + c->Suspend(); + DFHack::Maps *Mapz = c->getMaps(); // init the map if (!Mapz->Start()) { - cerr << "Can't init map." << endl; - if(temporary_terminal) - cin.ignore(); - return 1; + c->con.printerr("Can't init map.\n"); + c->Resume(); + return CR_FAILURE; } Mapz->getSize(x_max,y_max,z_max); @@ -69,12 +74,12 @@ int main (void) { for (uint32_t z = 0; z< z_max;z++) { - if (Mapz->isValidBlock(x,y,z)) + if (Mapz->getBlock(x,y,z)) { dirty= false; Mapz->ReadDesignations(x,y,z, &designations); Mapz->ReadTileTypes(x,y,z, &tiles); - if (Mapz->isValidBlock(x,y,z+1)) + if (Mapz->getBlock(x,y,z+1)) { Mapz->ReadTileTypes(x,y,z+1, &tilesAbove); } @@ -124,7 +129,7 @@ int main (void) { Mapz->WriteDesignations(x,y,z, &designations); Mapz->WriteTileTypes(x,y,z, &tiles); - if (Mapz->isValidBlock(x,y,z+1)) + if (Mapz->getBlock(x,y,z+1)) { Mapz->WriteTileTypes(x,y,z+1, &tilesAbove); } @@ -134,13 +139,10 @@ int main (void) } } } - DF->Detach(); - cout << "Found and changed " << count << " tiles." << endl; - cout << "Fixed " << countbad << " bad down ramps." << endl; - if(temporary_terminal) - { - cout << "Done. Press any key to continue" << endl; - cin.ignore(); - } - return 0; + c->Resume(); + if(count) + c->con.print("Found and changed %d tiles.",count); + if(countbad) + c->con.print("Fixed %d bad down ramps.",countbad); + return CR_OK; }