From b598b38891455cc7ef1b9a6b4578a81da08e8c22 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Wed, 18 Jan 2023 14:40:31 -0600 Subject: [PATCH] tailor: reenable and improve logging reenable plugin remove `using std;` switch to standard logger and add some debug and trace level log messages --- plugins/CMakeLists.txt | 2 +- plugins/tailor.cpp | 91 ++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 661fa2624..71a4fef82 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -158,7 +158,7 @@ dfhack_plugin(showmood showmood.cpp) #add_subdirectory(stockpiles) #dfhack_plugin(stocks stocks.cpp) #dfhack_plugin(strangemood strangemood.cpp) -#dfhack_plugin(tailor tailor.cpp) +dfhack_plugin(tailor tailor.cpp) dfhack_plugin(tiletypes tiletypes.cpp Brushes.h LINK_LIBRARIES lua) #dfhack_plugin(title-folder title-folder.cpp) #dfhack_plugin(title-version title-version.cpp) diff --git a/plugins/tailor.cpp b/plugins/tailor.cpp index 0a8a82f5b..2b1d0c63c 100644 --- a/plugins/tailor.cpp +++ b/plugins/tailor.cpp @@ -6,6 +6,7 @@ #include "Core.h" #include "DataDefs.h" +#include "Debug.h" #include "PluginManager.h" #include "df/creature_raw.h" @@ -29,18 +30,23 @@ #include "modules/World.h" using namespace DFHack; -using namespace std; using df::global::world; using df::global::plotinfo; DFHACK_PLUGIN("tailor"); + #define AUTOENABLE false DFHACK_PLUGIN_IS_ENABLED(enabled); REQUIRE_GLOBAL(world); REQUIRE_GLOBAL(plotinfo); +namespace DFHack { + DBG_DECLARE(tailor, cycle, DebugCategory::LINFO); + DBG_DECLARE(tailor, config, DebugCategory::LINFO); +} + class Tailor { // ARMOR, SHOES, HELM, GLOVES, PANTS @@ -48,7 +54,7 @@ class Tailor { private: - const map jobTypeMap = { + const std::map jobTypeMap = { { df::job_type::MakeArmor, df::item_type::ARMOR }, { df::job_type::MakePants, df::item_type::PANTS }, { df::job_type::MakeHelm, df::item_type::HELM }, @@ -56,7 +62,7 @@ private: { df::job_type::MakeShoes, df::item_type::SHOES } }; - const map itemTypeMap = { + const std::map itemTypeMap = { { df::item_type::ARMOR, df::job_type::MakeArmor }, { df::item_type::PANTS, df::job_type::MakePants }, { df::item_type::HELM, df::job_type::MakeHelm }, @@ -107,13 +113,13 @@ private: std::list all_materials = { M_SILK, M_CLOTH, M_YARN, M_LEATHER }; - map, int> available; // key is item type & size - map, int> needed; // same - map, int> queued; // same + std::map, int> available; // key is item type & size + std::map, int> needed; // same + std::map, int> queued; // same - map sizes; // this maps body size to races + std::map sizes; // this maps body size to races - map, int> orders; // key is item type, item subtype, size + std::map, int> orders; // key is item type, item subtype, size std::map supply; @@ -147,7 +153,7 @@ private: df::item_type t = i->getType(); int size = world->raws.creatures.all[i->getMakerRace()]->adultsize; - available[make_pair(t, size)] += 1; + available[std::make_pair(t, size)] += 1; } } @@ -180,7 +186,7 @@ private: supply[M_LEATHER] += i->getStackSize(); } - out->print("tailor: available silk %d yarn %d cloth %d leather %d\n", supply[M_SILK], supply[M_YARN], supply[M_CLOTH], supply[M_LEATHER]); + DEBUG(cycle).print("tailor: available silk %d yarn %d cloth %d leather %d\n", supply[M_SILK], supply[M_YARN], supply[M_CLOTH], supply[M_LEATHER]); } void scan_replacements() @@ -193,10 +199,10 @@ private: Units::isBaby(u)) continue; // skip units we don't control - set wearing; + std::set wearing; wearing.clear(); - deque worn; + std::deque worn; worn.clear(); for (auto inv : u->inventory) @@ -212,10 +218,16 @@ private: int size = world->raws.creatures.all[u->race]->adultsize; sizes[size] = u->race; - for (auto ty : set{ df::item_type::ARMOR, df::item_type::PANTS, df::item_type::SHOES }) + for (auto ty : std::set{ df::item_type::ARMOR, df::item_type::PANTS, df::item_type::SHOES }) { if (wearing.count(ty) == 0) - needed[make_pair(ty, size)] += 1; + { + TRACE(cycle).print("tailor: one %s of size %d needed to cover %s\n", + ENUM_KEY_STR(item_type, ty).c_str(), + size, + Translation::TranslateName(&u->name, false).c_str()); + needed[std::make_pair(ty, size)] += 1; + } } for (auto w : worn) @@ -227,13 +239,13 @@ private: std::string description; w->getItemDescription(&description, 0); - if (available[make_pair(ty, size)] > 0) + if (available[std::make_pair(ty, size)] > 0) { if (w->flags.bits.owned) { bool confiscated = Items::setOwner(w, NULL); - out->print( + INFO(cycle).print( "tailor: %s %s from %s.\n", (confiscated ? "confiscated" : "could not confiscate"), description.c_str(), @@ -242,18 +254,22 @@ private: } if (wearing.count(ty) == 0) - available[make_pair(ty, size)] -= 1; + { + DEBUG(cycle).print("tailor: allocating a %s to %s\n", + ENUM_KEY_STR(item_type, ty).c_str(), + Translation::TranslateName(&u->name, false).c_str()); + available[std::make_pair(ty, size)] -= 1; + } if (w->getWear() > 1) w->flags.bits.dump = true; } else { - // out->print("%s worn by %s needs replacement\n", - // description.c_str(), - // Translation::TranslateName(&u->name, false).c_str() - // ); - orders[make_tuple(o, w->getSubtype(), size)] += 1; + DEBUG(cycle).print ("%s worn by %s needs replacement, but none available\n", + description.c_str(), + Translation::TranslateName(&u->name, false).c_str()); + orders[std::make_tuple(o, w->getSubtype(), size)] += 1; } } } @@ -270,7 +286,7 @@ private: int count = a.second; int sub = 0; - vector v; + std::vector v; switch (ty) { case df::item_type::ARMOR: v = entity->resources.armor_type; break; @@ -299,7 +315,8 @@ private: } const df::job_type j = itemTypeMap.at(ty); - orders[make_tuple(j, sub, size)] += count; + orders[std::make_tuple(j, sub, size)] += count; + DEBUG(cycle).print("tailor: %s times %d of size %d ordered\n", ENUM_KEY_STR(job_type, j).c_str(), count, size); } } @@ -318,7 +335,11 @@ private: int size = world->raws.creatures.all[race]->adultsize; - orders[make_tuple(o->job_type, sub, size)] -= o->amount_left; + orders[std::make_tuple(o->job_type, sub, size)] -= o->amount_left; + TRACE(cycle).print("tailor: existing order for %d %s of size %d detected\n", + o->amount_left, + ENUM_KEY_STR(job_type, o->job_type).c_str(), + size); } } @@ -333,14 +354,14 @@ private: int sub; int size; - tie(ty, sub, size) = o.first; + std::tie(ty, sub, size) = o.first; int count = o.second; if (count > 0) { - vector v; + std::vector v; BitArray* fl; - string name_s, name_p; + std::string name_s, name_p; switch (ty) { @@ -382,7 +403,7 @@ private: if (!can_make) { - out->print("tailor: civilization cannot make %s, skipped\n", name_p.c_str()); + INFO(cycle).print("tailor: civilization cannot make %s, skipped\n", name_p.c_str()); continue; } @@ -416,7 +437,7 @@ private: world->manager_orders.push_back(order); - out->print("tailor: added order #%d for %d %s %s, sized for %s\n", + INFO(cycle).print("tailor: added order #%d for %d %s %s, sized for %s\n", order->id, c, bitfield_to_string(order->material_category).c_str(), @@ -464,9 +485,9 @@ public: } public: - command_result set_materials(color_ostream& out, vector& parameters) + command_result set_materials(color_ostream& out, std::vector& parameters) { - list newmat; + std::list newmat; newmat.clear(); for (auto m = parameters.begin() + 1; m != parameters.end(); m++) @@ -475,7 +496,7 @@ public: auto mm = std::find_if(all_materials.begin(), all_materials.end(), nameMatch); if (mm == all_materials.end()) { - out.print("tailor: material %s not recognized\n", m->c_str()); + WARN(config,out).print("tailor: material %s not recognized\n", m->c_str()); return CR_WRONG_USAGE; } else { @@ -484,7 +505,7 @@ public: } material_order = newmat; - out.print("tailor: material list set to %s\n", get_material_list().c_str()); + INFO(config,out).print("tailor: material list set to %s\n", get_material_list().c_str()); return CR_OK; } @@ -549,7 +570,7 @@ DFhackCExport command_result plugin_onupdate(color_ostream& out) return CR_OK; } -static command_result tailor_cmd(color_ostream& out, vector & parameters) { +static command_result tailor_cmd(color_ostream& out, std::vector & parameters) { bool desired = enabled; if (parameters.size() == 1 && (parameters[0] == "enable" || parameters[0] == "on" || parameters[0] == "1")) {