2019-04-27 05:59:27 -06:00
|
|
|
#include "Core.h"
|
2023-01-29 18:19:21 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Debug.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2019-04-27 08:24:52 -06:00
|
|
|
#include <map>
|
2019-04-27 07:56:50 -06:00
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
// DF data structure definition headers
|
|
|
|
#include "DataDefs.h"
|
|
|
|
|
2019-04-27 07:56:50 -06:00
|
|
|
#include "modules/Items.h"
|
2019-04-27 06:53:23 -06:00
|
|
|
#include "modules/Maps.h"
|
2019-04-27 10:02:50 -06:00
|
|
|
#include "modules/Materials.h"
|
2023-01-29 18:19:21 -07:00
|
|
|
#include "modules/Persistence.h"
|
2019-04-27 06:53:23 -06:00
|
|
|
#include "modules/Units.h"
|
2019-04-27 05:59:27 -06:00
|
|
|
#include "modules/World.h"
|
2023-01-13 22:20:01 -07:00
|
|
|
#include "modules/Translation.h"
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2023-01-14 02:28:13 -07:00
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/item_actual.h"
|
|
|
|
#include "df/item_crafted.h"
|
|
|
|
#include "df/item_constructed.h"
|
|
|
|
#include "df/item_armorst.h"
|
|
|
|
#include "df/item_glovesst.h"
|
|
|
|
#include "df/item_shoesst.h"
|
|
|
|
#include "df/item_helmst.h"
|
|
|
|
#include "df/item_pantsst.h"
|
2019-04-27 11:22:51 -06:00
|
|
|
#include "df/itemdef_armorst.h"
|
|
|
|
#include "df/itemdef_glovesst.h"
|
|
|
|
#include "df/itemdef_shoesst.h"
|
|
|
|
#include "df/itemdef_helmst.h"
|
|
|
|
#include "df/itemdef_pantsst.h"
|
2019-04-27 05:59:27 -06:00
|
|
|
#include "df/manager_order.h"
|
2019-04-27 07:56:50 -06:00
|
|
|
#include "df/creature_raw.h"
|
2019-04-27 05:59:27 -06:00
|
|
|
#include "df/world.h"
|
|
|
|
|
2022-11-07 10:12:46 -07:00
|
|
|
using std::endl;
|
2023-01-29 18:19:21 -07:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::map;
|
2022-11-07 10:12:46 -07:00
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
using namespace DFHack;
|
2019-04-27 07:56:50 -06:00
|
|
|
using namespace DFHack::Items;
|
2019-04-27 06:53:23 -06:00
|
|
|
using namespace DFHack::Units;
|
2019-04-27 05:59:27 -06:00
|
|
|
using namespace df::enums;
|
|
|
|
|
2019-04-27 20:01:12 -06:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("autoclothing");
|
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
|
|
|
|
// Only run if this is enabled
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(autoclothing_enabled);
|
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
// logging levels can be dynamically controlled with the `debugfilter` command.
|
|
|
|
namespace DFHack {
|
|
|
|
// for configuration-related logging
|
2023-01-29 18:48:23 -07:00
|
|
|
DBG_DECLARE(autoclothing, report, DebugCategory::LINFO);
|
2023-01-29 18:19:21 -07:00
|
|
|
// for logging during the periodic scan
|
|
|
|
DBG_DECLARE(autoclothing, cycle, DebugCategory::LINFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const string CONFIG_KEY = string(plugin_name) + "/config";
|
2023-03-25 11:25:45 -06:00
|
|
|
enum ConfigValues {
|
|
|
|
CONFIG_IS_ENABLED = 0,
|
|
|
|
};
|
2023-01-29 18:19:21 -07:00
|
|
|
|
2023-03-25 11:25:45 -06:00
|
|
|
//static int get_config_val(PersistentDataItem& c, int index) {
|
|
|
|
// if (!c.isValid())
|
|
|
|
// return -1;
|
|
|
|
// return c.ival(index);
|
|
|
|
//}
|
|
|
|
//static bool get_config_bool(PersistentDataItem& c, int index) {
|
|
|
|
// return get_config_val(c, index) == 1;
|
|
|
|
//}
|
|
|
|
static void set_config_val(PersistentDataItem& c, int index, int value) {
|
|
|
|
if (c.isValid())
|
|
|
|
c.ival(index) = value;
|
|
|
|
}
|
|
|
|
static void set_config_bool(PersistentDataItem& c, int index, bool value) {
|
|
|
|
set_config_val(c, index, value ? 1 : 0);
|
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
|
2019-04-27 20:01:12 -06:00
|
|
|
// Here go all the command declarations...
|
|
|
|
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
|
|
|
|
struct ClothingRequirement;
|
2023-01-29 18:19:21 -07:00
|
|
|
command_result autoclothing(color_ostream &out, vector <string> & parameters);
|
2019-04-27 20:01:12 -06:00
|
|
|
static void init_state(color_ostream &out);
|
|
|
|
static void save_state(color_ostream &out);
|
|
|
|
static void cleanup_state(color_ostream &out);
|
|
|
|
static void do_autoclothing();
|
|
|
|
static bool validateMaterialCategory(ClothingRequirement * requirement);
|
2023-01-29 18:19:21 -07:00
|
|
|
static bool setItem(string name, ClothingRequirement* requirement);
|
2023-01-13 22:20:01 -07:00
|
|
|
static void generate_report(color_ostream& out);
|
2023-01-14 02:28:13 -07:00
|
|
|
static bool isAvailableItem(df::item* item);
|
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
enum match_strictness
|
|
|
|
{
|
|
|
|
STRICT_PERMISSIVE,
|
|
|
|
STRICT_TYPE,
|
|
|
|
STRICT_MATERIAL
|
|
|
|
};
|
|
|
|
|
|
|
|
match_strictness strictnessSetting = STRICT_PERMISSIVE;
|
2019-04-27 20:01:12 -06:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
vector<ClothingRequirement>clothingOrders;
|
2019-04-27 20:01:12 -06:00
|
|
|
|
2019-04-27 07:56:50 -06:00
|
|
|
struct ClothingRequirement
|
|
|
|
{
|
2019-08-13 16:15:06 -06:00
|
|
|
df::job_type jobType;
|
|
|
|
df::item_type itemType;
|
2019-04-27 07:56:50 -06:00
|
|
|
int16_t item_subtype;
|
2019-04-27 10:02:50 -06:00
|
|
|
df::job_material_category material_category;
|
2019-04-27 07:56:50 -06:00
|
|
|
int16_t needed_per_citizen;
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int16_t, int32_t> total_needed_per_race;
|
2019-04-27 13:31:37 -06:00
|
|
|
|
|
|
|
bool matches(ClothingRequirement * b)
|
|
|
|
{
|
2019-08-13 16:15:06 -06:00
|
|
|
if (b->jobType != this->jobType)
|
2019-04-27 13:31:37 -06:00
|
|
|
return false;
|
2019-08-13 16:15:06 -06:00
|
|
|
if (b->itemType != this->itemType)
|
2019-04-27 13:31:37 -06:00
|
|
|
return false;
|
|
|
|
if (b->item_subtype != this->item_subtype)
|
|
|
|
return false;
|
|
|
|
if (b->material_category.whole != this->material_category.whole)
|
|
|
|
return false;
|
2019-04-27 14:56:58 -06:00
|
|
|
return true;
|
2019-04-27 13:31:37 -06:00
|
|
|
}
|
2019-04-27 07:56:50 -06:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
string Serialize()
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
2022-11-07 10:12:46 -07:00
|
|
|
std::stringstream stream;
|
2019-08-13 16:15:06 -06:00
|
|
|
stream << ENUM_KEY_STR(job_type, jobType) << " ";
|
|
|
|
stream << ENUM_KEY_STR(item_type,itemType) << " ";
|
2019-04-27 20:01:12 -06:00
|
|
|
stream << item_subtype << " ";
|
|
|
|
stream << material_category.whole << " ";
|
|
|
|
stream << needed_per_citizen;
|
|
|
|
return stream.str();
|
|
|
|
}
|
2019-04-27 07:56:50 -06:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
void Deserialize(string s)
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
2022-11-07 10:12:46 -07:00
|
|
|
std::stringstream stream(s);
|
2023-01-29 18:19:21 -07:00
|
|
|
string loadedJob;
|
2019-08-13 16:15:06 -06:00
|
|
|
stream >> loadedJob;
|
|
|
|
FOR_ENUM_ITEMS(job_type, job)
|
|
|
|
{
|
|
|
|
if (ENUM_KEY_STR(job_type, job) == loadedJob)
|
|
|
|
{
|
|
|
|
jobType = job;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
string loadedItem;
|
2019-08-13 16:15:06 -06:00
|
|
|
stream >> loadedItem;
|
|
|
|
FOR_ENUM_ITEMS(item_type, item)
|
|
|
|
{
|
|
|
|
if (ENUM_KEY_STR(item_type, item) == loadedItem)
|
|
|
|
{
|
|
|
|
itemType = item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 20:01:12 -06:00
|
|
|
stream >> item_subtype;
|
|
|
|
stream >> material_category.whole;
|
|
|
|
stream >> needed_per_citizen;
|
|
|
|
}
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
bool SetFromParameters(color_ostream &out, vector <string> & parameters)
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
|
|
|
if (!set_bitfield_field(&material_category, parameters[0], 1))
|
|
|
|
{
|
|
|
|
out << "Unrecognized material type: " << parameters[0] << endl;
|
|
|
|
}
|
|
|
|
if (!setItem(parameters[1], this))
|
|
|
|
{
|
|
|
|
out << "Unrecognized item name or token: " << parameters[1] << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!validateMaterialCategory(this))
|
|
|
|
{
|
|
|
|
out << parameters[0] << " is not a valid material category for " << parameters[1] << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
string ToReadableLabel()
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
2022-11-07 10:12:46 -07:00
|
|
|
std::stringstream stream;
|
2019-04-27 20:01:12 -06:00
|
|
|
stream << bitfield_to_string(material_category) << " ";
|
2023-01-29 18:19:21 -07:00
|
|
|
string adjective = "";
|
|
|
|
string name = "";
|
2019-08-13 16:15:06 -06:00
|
|
|
switch (itemType)
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
|
|
|
case df::enums::item_type::ARMOR:
|
|
|
|
adjective = world->raws.itemdefs.armor[item_subtype]->adjective;
|
|
|
|
name = world->raws.itemdefs.armor[item_subtype]->name;
|
|
|
|
break;
|
|
|
|
case df::enums::item_type::SHOES:
|
|
|
|
adjective = world->raws.itemdefs.shoes[item_subtype]->adjective;
|
|
|
|
name = world->raws.itemdefs.shoes[item_subtype]->name;
|
|
|
|
break;
|
|
|
|
case df::enums::item_type::HELM:
|
|
|
|
adjective = world->raws.itemdefs.helms[item_subtype]->adjective;
|
|
|
|
name = world->raws.itemdefs.helms[item_subtype]->name;
|
|
|
|
break;
|
|
|
|
case df::enums::item_type::GLOVES:
|
|
|
|
adjective = world->raws.itemdefs.gloves[item_subtype]->adjective;
|
|
|
|
name = world->raws.itemdefs.gloves[item_subtype]->name;
|
|
|
|
break;
|
|
|
|
case df::enums::item_type::PANTS:
|
|
|
|
adjective = world->raws.itemdefs.pants[item_subtype]->adjective;
|
|
|
|
name = world->raws.itemdefs.pants[item_subtype]->name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!adjective.empty())
|
|
|
|
stream << adjective << " ";
|
|
|
|
stream << name << " ";
|
|
|
|
stream << needed_per_citizen;
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2019-04-27 20:01:12 -06:00
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
};
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2019-04-27 06:53:23 -06:00
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
// Mandatory init function. If you have some global state, create it here.
|
2023-01-29 18:19:21 -07:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, vector <PluginCommand> &commands)
|
2019-04-27 05:59:27 -06:00
|
|
|
{
|
|
|
|
// Fill the command list with your commands.
|
|
|
|
commands.push_back(PluginCommand(
|
2022-07-18 14:02:03 -06:00
|
|
|
"autoclothing",
|
|
|
|
"Automatically manage clothing work orders",
|
|
|
|
autoclothing));
|
2019-04-27 05:59:27 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called right before the plugin library is removed from memory.
|
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream &out)
|
|
|
|
{
|
|
|
|
// You *MUST* kill all threads you created before this returns.
|
|
|
|
// If everything fails, just return CR_FAILURE. Your plugin will be
|
|
|
|
// in a zombie state, but things won't crash.
|
2019-04-27 20:01:12 -06:00
|
|
|
cleanup_state(out);
|
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called to notify the plugin about important state changes.
|
|
|
|
// Invoked with DF suspended, and always before the matching plugin_onupdate.
|
|
|
|
// More event codes may be added in the future.
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case SC_WORLD_LOADED:
|
2019-04-27 20:01:12 -06:00
|
|
|
init_state(out);
|
2019-04-27 05:59:27 -06:00
|
|
|
break;
|
|
|
|
case SC_WORLD_UNLOADED:
|
2019-04-27 20:01:12 -06:00
|
|
|
cleanup_state(out);
|
2019-04-27 05:59:27 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-25 11:25:45 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream& out, bool enable) {
|
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("Cannot enable %s without a loaded world.\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable != autoclothing_enabled) {
|
|
|
|
auto enabled = World::GetPersistentData("autoclothing/enabled");
|
|
|
|
autoclothing_enabled = enable;
|
|
|
|
DEBUG(report, out).print("%s from the API; persisting\n",
|
|
|
|
autoclothing_enabled ? "enabled" : "disabled");
|
|
|
|
set_config_bool(enabled, CONFIG_IS_ENABLED, autoclothing_enabled);
|
|
|
|
if (enable)
|
|
|
|
do_autoclothing();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG(report, out).print("%s from the API, but already %s; no action\n",
|
|
|
|
autoclothing_enabled ? "enabled" : "disabled",
|
|
|
|
autoclothing_enabled ? "enabled" : "disabled");
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
// Whatever you put here will be done in each game step. Don't abuse it.
|
|
|
|
// It's optional, so you can just comment it out like this if you don't need it.
|
|
|
|
|
2019-04-27 10:02:50 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out)
|
2019-04-27 05:59:27 -06:00
|
|
|
{
|
2019-04-27 06:53:23 -06:00
|
|
|
if (!autoclothing_enabled)
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (!Maps::IsValid())
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (DFHack::World::ReadPauseState())
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if ((world->frame_counter + 500) % 1200 != 0) // Check every day, but not the same day as other things
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
do_autoclothing();
|
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
static bool setItemFromName(string name, ClothingRequirement* requirement)
|
2019-04-27 11:22:51 -06:00
|
|
|
{
|
2019-08-13 16:15:06 -06:00
|
|
|
#define SEARCH_ITEM_RAWS(rawType, job, item) \
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& itemdef : world->raws.itemdefs.rawType) \
|
2019-04-27 11:41:25 -06:00
|
|
|
{ \
|
2023-01-29 18:19:21 -07:00
|
|
|
string fullName = itemdef->adjective.empty() ? itemdef->name : itemdef->adjective + " " + itemdef->name; \
|
2019-04-27 14:56:58 -06:00
|
|
|
if (fullName == name) \
|
2019-04-27 11:41:25 -06:00
|
|
|
{ \
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::job; \
|
|
|
|
requirement->itemType = item_type::item; \
|
2019-04-27 11:41:25 -06:00
|
|
|
requirement->item_subtype = itemdef->subtype; \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
SEARCH_ITEM_RAWS(armor, MakeArmor, ARMOR);
|
|
|
|
SEARCH_ITEM_RAWS(gloves, MakeGloves, GLOVES);
|
|
|
|
SEARCH_ITEM_RAWS(shoes, MakeShoes, SHOES);
|
|
|
|
SEARCH_ITEM_RAWS(helms, MakeHelm, HELM);
|
|
|
|
SEARCH_ITEM_RAWS(pants, MakePants, PANTS);
|
2019-04-27 11:22:51 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
static bool setItemFromToken(string token, ClothingRequirement* requirement)
|
2019-04-27 11:22:51 -06:00
|
|
|
{
|
|
|
|
ItemTypeInfo itemInfo;
|
|
|
|
if (!itemInfo.find(token))
|
|
|
|
return false;
|
|
|
|
switch (itemInfo.type)
|
|
|
|
{
|
|
|
|
case item_type::ARMOR:
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::MakeArmor;
|
2019-04-27 11:22:51 -06:00
|
|
|
break;
|
|
|
|
case item_type::GLOVES:
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::MakeGloves;
|
2019-04-27 11:22:51 -06:00
|
|
|
break;
|
|
|
|
case item_type::SHOES:
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::MakeShoes;
|
2019-04-27 11:22:51 -06:00
|
|
|
break;
|
|
|
|
case item_type::HELM:
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::MakeHelm;
|
2019-04-27 11:22:51 -06:00
|
|
|
break;
|
|
|
|
case item_type::PANTS:
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->jobType = job_type::MakePants;
|
2019-04-27 11:22:51 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-13 16:15:06 -06:00
|
|
|
requirement->itemType = itemInfo.type;
|
2019-04-27 11:22:51 -06:00
|
|
|
requirement->item_subtype = itemInfo.subtype;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
static bool setItem(string name, ClothingRequirement* requirement)
|
2019-04-27 11:22:51 -06:00
|
|
|
{
|
|
|
|
if (setItemFromName(name, requirement))
|
|
|
|
return true;
|
|
|
|
if (setItemFromToken(name, requirement))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-27 05:59:27 -06:00
|
|
|
|
2019-04-27 12:48:14 -06:00
|
|
|
static bool armorFlagsMatch(BitArray<df::armor_general_flags> * flags, df::job_material_category * category)
|
|
|
|
{
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::SOFT) && category->bits.cloth)
|
2019-04-27 12:48:14 -06:00
|
|
|
return true;
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::SOFT) && category->bits.yarn)
|
2019-04-27 12:50:52 -06:00
|
|
|
return true;
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::SOFT) && category->bits.silk)
|
2019-04-27 12:50:52 -06:00
|
|
|
return true;
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::BARRED) && category->bits.bone)
|
2019-04-27 12:48:14 -06:00
|
|
|
return true;
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::SCALED) && category->bits.shell)
|
2019-04-27 12:48:14 -06:00
|
|
|
return true;
|
2019-04-27 13:15:45 -06:00
|
|
|
if (flags->is_set(df::armor_general_flags::LEATHER) && category->bits.leather)
|
2019-04-27 12:48:14 -06:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validateMaterialCategory(ClothingRequirement * requirement)
|
|
|
|
{
|
2019-08-13 16:15:06 -06:00
|
|
|
auto itemDef = getSubtypeDef(requirement->itemType, requirement->item_subtype);
|
|
|
|
switch (requirement->itemType)
|
2019-04-27 12:48:14 -06:00
|
|
|
{
|
|
|
|
case item_type::ARMOR:
|
|
|
|
if (STRICT_VIRTUAL_CAST_VAR(armor, df::itemdef_armorst, itemDef))
|
|
|
|
return armorFlagsMatch(&armor->props.flags, &requirement->material_category);
|
|
|
|
case item_type::GLOVES:
|
|
|
|
if (STRICT_VIRTUAL_CAST_VAR(armor, df::itemdef_glovesst, itemDef))
|
|
|
|
return armorFlagsMatch(&armor->props.flags, &requirement->material_category);
|
|
|
|
case item_type::SHOES:
|
|
|
|
if (STRICT_VIRTUAL_CAST_VAR(armor, df::itemdef_shoesst, itemDef))
|
|
|
|
return armorFlagsMatch(&armor->props.flags, &requirement->material_category);
|
|
|
|
case item_type::HELM:
|
|
|
|
if (STRICT_VIRTUAL_CAST_VAR(armor, df::itemdef_helmst, itemDef))
|
|
|
|
return armorFlagsMatch(&armor->props.flags, &requirement->material_category);
|
|
|
|
case item_type::PANTS:
|
|
|
|
if (STRICT_VIRTUAL_CAST_VAR(armor, df::itemdef_pantsst, itemDef))
|
|
|
|
return armorFlagsMatch(&armor->props.flags, &requirement->material_category);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 20:01:12 -06:00
|
|
|
|
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
// A command! It sits around and looks pretty. And it's nice and friendly.
|
2023-01-29 18:19:21 -07:00
|
|
|
command_result autoclothing(color_ostream &out, vector <string> & parameters)
|
2019-04-27 05:59:27 -06:00
|
|
|
{
|
|
|
|
// It's nice to print a help message you get invalid options
|
|
|
|
// from the user instead of just acting strange.
|
|
|
|
// This can be achieved by adding the extended help string to the
|
|
|
|
// PluginCommand registration as show above, and then returning
|
|
|
|
// CR_WRONG_USAGE from the function. The same string will also
|
|
|
|
// be used by 'help your-command'.
|
2019-08-13 16:15:06 -06:00
|
|
|
if (parameters.size() == 0)
|
|
|
|
{
|
2023-02-02 14:06:14 -07:00
|
|
|
CoreSuspender suspend;
|
2023-03-25 11:25:45 -06:00
|
|
|
out << "Automatic clothing management is currently " << (autoclothing_enabled ? "enabled" : "disabled") << "." << endl;
|
2019-08-13 16:15:06 -06:00
|
|
|
out << "Currently set " << clothingOrders.size() << " automatic clothing orders" << endl;
|
|
|
|
for (size_t i = 0; i < clothingOrders.size(); i++)
|
|
|
|
{
|
|
|
|
out << clothingOrders[i].ToReadableLabel() << endl;
|
|
|
|
}
|
2023-01-13 22:20:01 -07:00
|
|
|
generate_report(out);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2023-02-02 14:06:14 -07:00
|
|
|
////Disabled until I have time to fully implement it.
|
|
|
|
//else if (parameters[0] == "strictness")
|
|
|
|
//{
|
|
|
|
// if (parameters.size() != 2)
|
|
|
|
// {
|
|
|
|
// out << "Wrong number of arguments." << endl;
|
|
|
|
// return CR_WRONG_USAGE;
|
|
|
|
// }
|
|
|
|
// if (parameters[1] == "permissive")
|
|
|
|
// strictnessSetting = STRICT_PERMISSIVE;
|
|
|
|
// else if (parameters[1] == "type")
|
|
|
|
// strictnessSetting = STRICT_TYPE;
|
|
|
|
// else if (parameters[1] == "material")
|
|
|
|
// strictnessSetting = STRICT_MATERIAL;
|
|
|
|
//}
|
2019-08-13 16:15:06 -06:00
|
|
|
else if (parameters.size() < 2 || parameters.size() > 3)
|
2019-04-27 11:22:51 -06:00
|
|
|
{
|
|
|
|
out << "Wrong number of arguments." << endl;
|
2019-04-27 05:59:27 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2019-04-27 11:22:51 -06:00
|
|
|
}
|
2019-04-27 05:59:27 -06:00
|
|
|
// Commands are called from threads other than the DF one.
|
|
|
|
// Suspend this thread until DF has time for us. If you
|
|
|
|
// use CoreSuspender, it'll automatically resume DF when
|
|
|
|
// execution leaves the current scope.
|
|
|
|
CoreSuspender suspend;
|
2019-11-02 20:00:43 -06:00
|
|
|
|
2019-08-13 16:15:06 -06:00
|
|
|
|
|
|
|
// Create a new requirement from the available parameters.
|
2019-04-27 11:22:51 -06:00
|
|
|
ClothingRequirement newRequirement;
|
2019-04-27 20:01:12 -06:00
|
|
|
if (!newRequirement.SetFromParameters(out, parameters))
|
2019-04-27 12:48:14 -06:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
//all checks are passed. Now we either show or set the amount.
|
2019-04-27 13:31:37 -06:00
|
|
|
bool settingSize = false;
|
|
|
|
bool matchedExisting = false;
|
|
|
|
if (parameters.size() > 2)
|
2019-04-27 11:22:51 -06:00
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
newRequirement.needed_per_citizen = std::stoi(parameters[2]);
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
|
|
|
out << parameters[2] << " is not a valid number." << endl;
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2019-04-27 13:31:37 -06:00
|
|
|
settingSize = true;
|
|
|
|
}
|
2019-04-27 20:01:12 -06:00
|
|
|
|
2019-04-27 13:31:37 -06:00
|
|
|
for (size_t i = 0; i < clothingOrders.size(); i++)
|
|
|
|
{
|
|
|
|
if (!clothingOrders[i].matches(&newRequirement))
|
|
|
|
continue;
|
|
|
|
matchedExisting = true;
|
|
|
|
if (settingSize)
|
|
|
|
{
|
|
|
|
if (newRequirement.needed_per_citizen == 0)
|
|
|
|
{
|
|
|
|
clothingOrders.erase(clothingOrders.begin() + i);
|
|
|
|
out << "Unset " << parameters[0] << " " << parameters[1] << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clothingOrders[i] = newRequirement;
|
2019-04-27 14:56:58 -06:00
|
|
|
out << "Set " << parameters[0] << " " << parameters[1] << " to " << parameters[2] << endl;
|
2019-04-27 13:31:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << parameters[0] << " " << parameters[1] << " is set to " << clothingOrders[i].needed_per_citizen << endl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!matchedExisting)
|
|
|
|
{
|
|
|
|
if (settingSize)
|
|
|
|
{
|
|
|
|
if (newRequirement.needed_per_citizen == 0)
|
|
|
|
{
|
|
|
|
out << parameters[0] << " " << parameters[1] << " already unset." << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clothingOrders.push_back(newRequirement);
|
2019-04-27 14:56:58 -06:00
|
|
|
out << "Added order for " << parameters[0] << " " << parameters[1] << " to " << parameters[2] << endl;
|
2019-04-27 13:31:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << parameters[0] << " " << parameters[1] << " is not set." << endl;
|
|
|
|
}
|
2019-04-27 11:22:51 -06:00
|
|
|
}
|
2019-04-27 14:56:58 -06:00
|
|
|
if (settingSize)
|
|
|
|
{
|
|
|
|
if (!autoclothing_enabled)
|
|
|
|
{
|
|
|
|
out << "Enabling automatic clothing management" << endl;
|
|
|
|
autoclothing_enabled = true;
|
|
|
|
}
|
|
|
|
do_autoclothing();
|
|
|
|
}
|
2019-04-27 20:01:12 -06:00
|
|
|
save_state(out);
|
|
|
|
|
2019-04-27 05:59:27 -06:00
|
|
|
// Give control back to DF.
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2019-04-27 06:53:23 -06:00
|
|
|
|
2019-04-27 10:11:16 -06:00
|
|
|
static void find_needed_clothing_items()
|
2019-04-27 06:53:23 -06:00
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& unit : world->units.active)
|
2019-04-27 06:53:23 -06:00
|
|
|
{
|
2019-04-27 10:02:50 -06:00
|
|
|
//obviously we don't care about illegal aliens.
|
2019-04-27 06:53:23 -06:00
|
|
|
if (!isCitizen(unit))
|
|
|
|
continue;
|
2019-04-27 10:02:50 -06:00
|
|
|
|
|
|
|
//now check each clothing order to see what the unit might be missing.
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& clothingOrder : clothingOrders)
|
2019-04-27 06:53:23 -06:00
|
|
|
{
|
2019-04-27 07:56:50 -06:00
|
|
|
int alreadyOwnedAmount = 0;
|
|
|
|
|
2019-04-27 10:02:50 -06:00
|
|
|
//looping through the items first, then clothing order might be a little faster, but this way is cleaner.
|
2023-01-29 18:51:10 -07:00
|
|
|
for (auto ownedItem : unit->owned_items)
|
2019-04-27 07:56:50 -06:00
|
|
|
{
|
|
|
|
auto item = findItemByID(ownedItem);
|
|
|
|
|
2023-01-29 18:48:23 -07:00
|
|
|
if (!item)
|
|
|
|
{
|
2023-04-24 01:31:20 -06:00
|
|
|
DEBUG(cycle).print("autoclothing: Invalid inventory item ID: %d\n", ownedItem);
|
2023-01-29 18:48:23 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-13 16:15:06 -06:00
|
|
|
if (item->getType() != clothingOrder.itemType)
|
2019-04-27 07:56:50 -06:00
|
|
|
continue;
|
|
|
|
if (item->getSubtype() != clothingOrder.item_subtype)
|
|
|
|
continue;
|
|
|
|
|
2019-04-27 10:02:50 -06:00
|
|
|
MaterialInfo matInfo;
|
|
|
|
matInfo.decode(item);
|
|
|
|
|
|
|
|
if (!matInfo.matches(clothingOrder.material_category))
|
|
|
|
continue;
|
|
|
|
|
2019-04-27 07:56:50 -06:00
|
|
|
alreadyOwnedAmount++;
|
|
|
|
}
|
2019-04-27 10:02:50 -06:00
|
|
|
int neededAmount = clothingOrder.needed_per_citizen - alreadyOwnedAmount;
|
|
|
|
|
|
|
|
if (neededAmount <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//technically, there's some leeway in sizes, but only caring about exact sizes is simpler.
|
2019-04-27 14:56:58 -06:00
|
|
|
clothingOrder.total_needed_per_race[unit->race] += neededAmount;
|
2019-04-27 10:02:50 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 10:11:16 -06:00
|
|
|
}
|
2019-04-27 10:02:50 -06:00
|
|
|
|
2019-04-27 10:11:16 -06:00
|
|
|
static void remove_available_clothing()
|
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& item : world->items.all)
|
2019-04-27 10:02:50 -06:00
|
|
|
{
|
|
|
|
//skip any owned items.
|
|
|
|
if (getOwner(item))
|
|
|
|
continue;
|
|
|
|
|
2019-04-27 15:58:21 -06:00
|
|
|
//again, for each item, find if any clothing order matches
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& clothingOrder : clothingOrders)
|
2019-04-27 10:02:50 -06:00
|
|
|
{
|
2019-08-13 16:15:06 -06:00
|
|
|
if (item->getType() != clothingOrder.itemType)
|
2019-04-27 10:02:50 -06:00
|
|
|
continue;
|
|
|
|
if (item->getSubtype() != clothingOrder.item_subtype)
|
|
|
|
continue;
|
2019-04-27 07:56:50 -06:00
|
|
|
|
2019-04-27 10:02:50 -06:00
|
|
|
MaterialInfo matInfo;
|
|
|
|
matInfo.decode(item);
|
2019-04-27 06:53:23 -06:00
|
|
|
|
2019-04-27 10:02:50 -06:00
|
|
|
if (!matInfo.matches(clothingOrder.material_category))
|
2019-04-27 07:56:50 -06:00
|
|
|
continue;
|
2019-04-27 10:02:50 -06:00
|
|
|
|
2019-04-27 10:20:36 -06:00
|
|
|
clothingOrder.total_needed_per_race[item->getMakerRace()] --;
|
2019-04-27 10:02:50 -06:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 10:11:16 -06:00
|
|
|
}
|
2019-04-27 10:02:50 -06:00
|
|
|
|
2019-04-27 10:11:16 -06:00
|
|
|
static void add_clothing_orders()
|
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& clothingOrder : clothingOrders)
|
2019-04-27 10:02:50 -06:00
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& orderNeeded : clothingOrder.total_needed_per_race)
|
2019-04-27 10:02:50 -06:00
|
|
|
{
|
|
|
|
auto race = orderNeeded.first;
|
|
|
|
auto amount = orderNeeded.second;
|
2019-04-27 15:29:30 -06:00
|
|
|
orderNeeded.second = 0; //once we get what we need, set it back to zero so we don't add it to further counts.
|
2019-04-27 10:02:50 -06:00
|
|
|
//Previous operations can easily make this negative. That jus means we have more than we need already.
|
|
|
|
if (amount <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool orderExistedAlready = false;
|
2019-04-27 14:56:58 -06:00
|
|
|
for (auto& managerOrder : world->manager_orders)
|
2019-04-27 10:02:50 -06:00
|
|
|
{
|
|
|
|
//Annoyingly, the manager orders store the job type for clothing orders, and actual item type is left at -1;
|
2019-08-13 16:15:06 -06:00
|
|
|
if (managerOrder->job_type != clothingOrder.jobType)
|
2019-04-27 10:02:50 -06:00
|
|
|
continue;
|
|
|
|
if (managerOrder->item_subtype != clothingOrder.item_subtype)
|
|
|
|
continue;
|
|
|
|
if (managerOrder->hist_figure_id != race)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//We found a work order, that means we don't need to make a new one.
|
|
|
|
orderExistedAlready = true;
|
|
|
|
amount -= managerOrder->amount_left;
|
|
|
|
if (amount > 0)
|
|
|
|
{
|
|
|
|
managerOrder->amount_left += amount;
|
|
|
|
managerOrder->amount_total += amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//if it wasn't there, we need to make a new one.
|
|
|
|
if (!orderExistedAlready)
|
|
|
|
{
|
2019-04-27 14:56:58 -06:00
|
|
|
df::manager_order * newOrder = new df::manager_order();
|
2019-04-27 10:02:50 -06:00
|
|
|
|
2019-04-27 14:56:58 -06:00
|
|
|
newOrder->id = world->manager_order_next_id;
|
2019-04-27 10:02:50 -06:00
|
|
|
world->manager_order_next_id++;
|
2019-08-13 16:15:06 -06:00
|
|
|
newOrder->job_type = clothingOrder.jobType;
|
2019-04-27 14:56:58 -06:00
|
|
|
newOrder->item_subtype = clothingOrder.item_subtype;
|
|
|
|
newOrder->hist_figure_id = race;
|
|
|
|
newOrder->material_category = clothingOrder.material_category;
|
|
|
|
newOrder->amount_left = amount;
|
|
|
|
newOrder->amount_total = amount;
|
|
|
|
world->manager_orders.push_back(newOrder);
|
2019-04-27 10:02:50 -06:00
|
|
|
}
|
2019-04-27 06:53:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 10:11:16 -06:00
|
|
|
|
|
|
|
static void do_autoclothing()
|
|
|
|
{
|
|
|
|
if (clothingOrders.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//first we look through all the units on the map to see who needs new clothes.
|
|
|
|
find_needed_clothing_items();
|
|
|
|
|
|
|
|
//Now we go through all the items in the map to see how many clothing items we have but aren't owned yet.
|
|
|
|
remove_available_clothing();
|
|
|
|
|
|
|
|
//Finally loop through the clothing orders to find ones that need more made.
|
|
|
|
add_clothing_orders();
|
|
|
|
}
|
2019-04-27 20:01:12 -06:00
|
|
|
|
|
|
|
static void cleanup_state(color_ostream &out)
|
|
|
|
{
|
|
|
|
clothingOrders.clear();
|
|
|
|
autoclothing_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_state(color_ostream &out)
|
|
|
|
{
|
|
|
|
auto enabled = World::GetPersistentData("autoclothing/enabled");
|
|
|
|
if (enabled.isValid() && enabled.ival(0) == 1)
|
|
|
|
{
|
|
|
|
out << "autoclothing enabled" << endl;
|
|
|
|
autoclothing_enabled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
autoclothing_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse constraints
|
2023-01-29 18:19:21 -07:00
|
|
|
vector<PersistentDataItem> items;
|
2019-04-27 20:01:12 -06:00
|
|
|
World::GetPersistentData(&items, "autoclothing/clothingItems");
|
|
|
|
|
|
|
|
for (auto& item : items)
|
|
|
|
{
|
|
|
|
if (!item.isValid())
|
|
|
|
continue;
|
|
|
|
ClothingRequirement req;
|
|
|
|
req.Deserialize(item.val());
|
|
|
|
clothingOrders.push_back(req);
|
|
|
|
out << "autoclothing added " << req.ToReadableLabel() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save_state(color_ostream &out)
|
|
|
|
{
|
|
|
|
auto enabled = World::GetPersistentData("autoclothing/enabled");
|
|
|
|
if (!enabled.isValid())
|
|
|
|
enabled = World::AddPersistentData("autoclothing/enabled");
|
|
|
|
enabled.ival(0) = autoclothing_enabled;
|
|
|
|
|
|
|
|
for (auto& order : clothingOrders)
|
|
|
|
{
|
|
|
|
auto orderSave = World::AddPersistentData("autoclothing/clothingItems");
|
|
|
|
orderSave.val() = order.Serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parse constraints
|
2023-01-29 18:19:21 -07:00
|
|
|
vector<PersistentDataItem> items;
|
2019-04-27 20:01:12 -06:00
|
|
|
World::GetPersistentData(&items, "autoclothing/clothingItems");
|
|
|
|
|
2019-08-13 16:15:06 -06:00
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
|
|
|
if (i < clothingOrders.size())
|
|
|
|
{
|
|
|
|
items[i].val() = clothingOrders[i].Serialize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
World::DeletePersistentData(items[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 16:15:06 -06:00
|
|
|
for (size_t i = items.size(); i < clothingOrders.size(); i++)
|
2019-04-27 20:01:12 -06:00
|
|
|
{
|
|
|
|
auto item = World::AddPersistentData("autoclothing/clothingItems");
|
|
|
|
item.val() = clothingOrders[i].Serialize();
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 22:20:01 -07:00
|
|
|
|
2023-01-29 18:19:21 -07:00
|
|
|
static void list_unit_counts(color_ostream& out, map<int, int>& unitList)
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
|
|
|
for (const auto& race : unitList)
|
|
|
|
{
|
|
|
|
if (race.second == 1)
|
|
|
|
out << " 1 " << Units::getRaceReadableNameById(race.first) << endl;
|
|
|
|
else
|
|
|
|
out << " " << race.second << " " << Units::getRaceNamePluralById(race.first) << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 02:28:13 -07:00
|
|
|
static bool isAvailableItem(df::item* item)
|
|
|
|
{
|
2023-03-25 11:25:45 -06:00
|
|
|
static struct BadFlags {
|
|
|
|
uint32_t whole;
|
|
|
|
|
|
|
|
BadFlags() {
|
|
|
|
df::item_flags flags;
|
|
|
|
#define F(x) flags.bits.x = true;
|
|
|
|
F(in_job); F(hostile); F(in_building); F(encased);
|
|
|
|
F(foreign); F(trader); F(owned); F(forbid);
|
|
|
|
F(dump); F(on_fire); F(melt); F(hidden);
|
|
|
|
|
|
|
|
F(garbage_collect); F(rotten); F(construction);
|
|
|
|
F(in_chest); F(removed); F(spider_web);
|
|
|
|
|
|
|
|
// F(artifact); -- TODO: should this be included?
|
|
|
|
#undef F
|
|
|
|
whole = flags.whole;
|
|
|
|
}
|
|
|
|
} badFlags;
|
|
|
|
|
|
|
|
if ((item->flags.whole & badFlags.whole) != 0)
|
2023-01-14 02:28:13 -07:00
|
|
|
return false;
|
2023-03-25 11:25:45 -06:00
|
|
|
|
2023-01-14 02:28:13 -07:00
|
|
|
if (item->getWear() > 1)
|
|
|
|
return false;
|
|
|
|
if (!item->isClothing())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:20:01 -07:00
|
|
|
static void generate_report(color_ostream& out)
|
|
|
|
{
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> fullUnitList;
|
|
|
|
map<int, int> missingArmor;
|
|
|
|
map<int, int> missingShoes;
|
|
|
|
map<int, int> missingHelms;
|
|
|
|
map<int, int> missingGloves;
|
|
|
|
map<int, int> missingPants;
|
2023-01-13 22:20:01 -07:00
|
|
|
for (df::unit* unit : world->units.active)
|
|
|
|
{
|
|
|
|
if (!Units::isCitizen(unit))
|
|
|
|
continue;
|
|
|
|
fullUnitList[unit->race]++;
|
|
|
|
int numArmor = 0, numShoes = 0, numHelms = 0, numGloves = 0, numPants = 0;
|
|
|
|
for (auto itemId : unit->owned_items)
|
|
|
|
{
|
|
|
|
auto item = Items::findItemByID(itemId);
|
2023-01-29 18:51:10 -07:00
|
|
|
if (!item)
|
|
|
|
{
|
2023-04-24 01:31:20 -06:00
|
|
|
DEBUG(cycle, out).print("autoclothing: Invalid inventory item ID: %d\n", itemId);
|
2023-01-29 18:51:10 -07:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-13 22:20:01 -07:00
|
|
|
if (item->getWear() >= 1)
|
|
|
|
continue;
|
|
|
|
switch (item->getType())
|
|
|
|
{
|
|
|
|
case df::item_type::ARMOR:
|
|
|
|
numArmor++;
|
|
|
|
break;
|
|
|
|
case df::item_type::SHOES:
|
|
|
|
numShoes++;
|
|
|
|
break;
|
|
|
|
case df::item_type::HELM:
|
|
|
|
numHelms++;
|
|
|
|
break;
|
|
|
|
case df::item_type::GLOVES:
|
|
|
|
numGloves++;
|
|
|
|
break;
|
|
|
|
case df::item_type::PANTS:
|
|
|
|
numPants++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (numArmor == 0)
|
|
|
|
missingArmor[unit->race]++;
|
|
|
|
if (numShoes < 2)
|
|
|
|
missingShoes[unit->race]++;
|
|
|
|
if (numHelms == 0)
|
|
|
|
missingHelms[unit->race]++;
|
|
|
|
if (numGloves < 2)
|
|
|
|
missingGloves[unit->race]++;
|
|
|
|
if (numPants == 0)
|
|
|
|
missingPants[unit->race]++;
|
2023-02-02 14:33:30 -07:00
|
|
|
DEBUG(report,out) << Translation::TranslateName(Units::getVisibleName(unit)) << " has " << numArmor << " armor, " << numShoes << " shoes, " << numHelms << " helms, " << numGloves << " gloves, " << numPants << " pants" << endl;
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
|
|
|
if (missingArmor.size() + missingShoes.size() + missingHelms.size() + missingGloves.size() + missingPants.size() == 0)
|
|
|
|
{
|
|
|
|
out << "Everybody has a full set of clothes to wear, congrats!" << endl;
|
|
|
|
return;
|
|
|
|
}
|
2023-01-14 02:28:13 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (missingArmor.size())
|
|
|
|
{
|
|
|
|
out << "Following units need new bodywear:" << endl;
|
|
|
|
list_unit_counts(out, missingArmor);
|
|
|
|
}
|
|
|
|
if (missingShoes.size())
|
|
|
|
{
|
|
|
|
out << "Following units need new shoes:" << endl;
|
|
|
|
list_unit_counts(out, missingShoes);
|
|
|
|
}
|
|
|
|
if (missingHelms.size())
|
|
|
|
{
|
|
|
|
out << "Following units need new headwear:" << endl;
|
|
|
|
list_unit_counts(out, missingHelms);
|
|
|
|
}
|
|
|
|
if (missingGloves.size())
|
|
|
|
{
|
|
|
|
out << "Following units need new handwear:" << endl;
|
|
|
|
list_unit_counts(out, missingGloves);
|
|
|
|
}
|
|
|
|
if (missingPants.size())
|
|
|
|
{
|
|
|
|
out << "Following units need new legwear:" << endl;
|
|
|
|
list_unit_counts(out, missingPants);
|
|
|
|
}
|
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> availableArmor;
|
2023-01-14 02:28:13 -07:00
|
|
|
for (auto armor : world->items.other.ARMOR)
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
2023-01-14 02:28:13 -07:00
|
|
|
if (!isAvailableItem(armor))
|
|
|
|
continue;
|
|
|
|
availableArmor[armor->maker_race]++;
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-01-14 02:28:13 -07:00
|
|
|
if (availableArmor.size())
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
2023-01-14 02:28:13 -07:00
|
|
|
out << "We have available bodywear for:" << endl;
|
|
|
|
list_unit_counts(out, availableArmor);
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> availableShoes;
|
2023-01-14 02:28:13 -07:00
|
|
|
for (auto shoe : world->items.other.SHOES)
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
2023-01-14 02:28:13 -07:00
|
|
|
if (!isAvailableItem(shoe))
|
|
|
|
continue;
|
|
|
|
availableShoes[shoe->maker_race]++;
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-01-14 02:28:13 -07:00
|
|
|
if (availableShoes.size())
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
2023-01-14 02:28:13 -07:00
|
|
|
out << "We have available footwear for:" << endl;
|
|
|
|
list_unit_counts(out, availableShoes);
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> availableHelms;
|
2023-01-14 02:28:13 -07:00
|
|
|
for (auto helm : world->items.other.HELM)
|
2023-01-13 22:20:01 -07:00
|
|
|
{
|
2023-01-14 02:28:13 -07:00
|
|
|
if (!isAvailableItem(helm))
|
|
|
|
continue;
|
|
|
|
availableHelms[helm->maker_race]++;
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-01-14 02:28:13 -07:00
|
|
|
if (availableHelms.size())
|
|
|
|
{
|
|
|
|
out << "We have available headwear for:" << endl;
|
|
|
|
list_unit_counts(out, availableHelms);
|
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> availableGloves;
|
2023-01-14 02:28:13 -07:00
|
|
|
for (auto glove : world->items.other.HELM)
|
|
|
|
{
|
|
|
|
if (!isAvailableItem(glove))
|
|
|
|
continue;
|
|
|
|
availableGloves[glove->maker_race]++;
|
|
|
|
}
|
|
|
|
if (availableGloves.size())
|
|
|
|
{
|
|
|
|
out << "We have available handwear for:" << endl;
|
|
|
|
list_unit_counts(out, availableGloves);
|
|
|
|
}
|
2023-01-29 18:19:21 -07:00
|
|
|
map<int, int> availablePants;
|
2023-01-14 02:28:13 -07:00
|
|
|
for (auto pants : world->items.other.HELM)
|
|
|
|
{
|
|
|
|
if (!isAvailableItem(pants))
|
|
|
|
continue;
|
|
|
|
availablePants[pants->maker_race]++;
|
|
|
|
}
|
|
|
|
if (availablePants.size())
|
|
|
|
{
|
|
|
|
out << "We have available legwear for:" << endl;
|
|
|
|
list_unit_counts(out, availablePants);
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:20:01 -07:00
|
|
|
}
|
2023-03-25 11:25:45 -06:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// Lua API
|
|
|
|
// TODO: implement Lua hooks to manipulate the persistent order configuration
|
|
|
|
//
|
|
|
|
|
|
|
|
static void autoclothing_doCycle(color_ostream& out) {
|
|
|
|
DEBUG(report, out).print("entering autoclothing_doCycle\n");
|
|
|
|
do_autoclothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS{
|
|
|
|
DFHACK_LUA_FUNCTION(autoclothing_doCycle),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
|
|
|
DFHACK_PLUGIN_LUA_COMMANDS{
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|