2018-08-25 10:59:28 -06:00
|
|
|
/*
|
|
|
|
* Tailor plugin. Automatically manages keeping your dorfs clothed.
|
|
|
|
* For best effect, place "tailor enable" in your dfhack.init configuration,
|
|
|
|
* or set AUTOENABLE to true.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Core.h"
|
|
|
|
#include "DataDefs.h"
|
2023-01-18 13:40:31 -07:00
|
|
|
#include "Debug.h"
|
2018-08-25 10:59:28 -06:00
|
|
|
#include "PluginManager.h"
|
|
|
|
|
|
|
|
#include "df/creature_raw.h"
|
|
|
|
#include "df/global_objects.h"
|
|
|
|
#include "df/historical_entity.h"
|
|
|
|
#include "df/itemdef_armorst.h"
|
|
|
|
#include "df/itemdef_glovesst.h"
|
|
|
|
#include "df/itemdef_helmst.h"
|
|
|
|
#include "df/itemdef_pantsst.h"
|
|
|
|
#include "df/itemdef_shoesst.h"
|
|
|
|
#include "df/items_other_id.h"
|
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/job_type.h"
|
|
|
|
#include "df/manager_order.h"
|
2023-01-05 18:11:01 -07:00
|
|
|
#include "df/plotinfost.h"
|
2018-08-25 10:59:28 -06:00
|
|
|
#include "df/world.h"
|
|
|
|
|
|
|
|
#include "modules/Maps.h"
|
|
|
|
#include "modules/Units.h"
|
|
|
|
#include "modules/Translation.h"
|
|
|
|
#include "modules/World.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
using df::global::world;
|
2023-01-05 18:11:01 -07:00
|
|
|
using df::global::plotinfo;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("tailor");
|
2023-01-18 13:40:31 -07:00
|
|
|
|
2018-08-25 10:59:28 -06:00
|
|
|
#define AUTOENABLE false
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(enabled);
|
|
|
|
|
|
|
|
REQUIRE_GLOBAL(world);
|
2023-01-05 18:11:01 -07:00
|
|
|
REQUIRE_GLOBAL(plotinfo);
|
2023-01-20 14:21:45 -07:00
|
|
|
REQUIRE_GLOBAL(standing_orders_use_dyed_cloth);
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_DECLARE(tailor, cycle, DebugCategory::LINFO);
|
|
|
|
DBG_DECLARE(tailor, config, DebugCategory::LINFO);
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
class Tailor {
|
|
|
|
// ARMOR, SHOES, HELM, GLOVES, PANTS
|
|
|
|
|
|
|
|
// ah, if only STL had a bimap
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
const std::map<df::job_type, df::item_type> jobTypeMap = {
|
2021-08-08 08:53:32 -06:00
|
|
|
{ df::job_type::MakeArmor, df::item_type::ARMOR },
|
|
|
|
{ df::job_type::MakePants, df::item_type::PANTS },
|
|
|
|
{ df::job_type::MakeHelm, df::item_type::HELM },
|
|
|
|
{ df::job_type::MakeGloves, df::item_type::GLOVES },
|
|
|
|
{ df::job_type::MakeShoes, df::item_type::SHOES }
|
|
|
|
};
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
const std::map<df::item_type, df::job_type> itemTypeMap = {
|
2021-08-08 08:53:32 -06:00
|
|
|
{ df::item_type::ARMOR, df::job_type::MakeArmor },
|
|
|
|
{ df::item_type::PANTS, df::job_type::MakePants },
|
|
|
|
{ df::item_type::HELM, df::job_type::MakeHelm },
|
|
|
|
{ df::item_type::GLOVES, df::job_type::MakeGloves },
|
|
|
|
{ df::item_type::SHOES, df::job_type::MakeShoes }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define F(x) df::item_flags::mask_##x
|
|
|
|
const df::item_flags bad_flags = {
|
|
|
|
(
|
|
|
|
F(dump) | F(forbid) | F(garbage_collect) |
|
|
|
|
F(hostile) | F(on_fire) | F(rotten) | F(trader) |
|
|
|
|
F(in_building) | F(construction) | F(owned)
|
|
|
|
)
|
|
|
|
#undef F
|
|
|
|
};
|
|
|
|
|
|
|
|
class MatType {
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string name;
|
|
|
|
df::job_material_category job_material;
|
|
|
|
df::armor_general_flags armor_flag;
|
|
|
|
|
|
|
|
bool operator==(const MatType& m) const
|
|
|
|
{
|
|
|
|
return name == m.name;
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
// operator< is required to use this as a std::map key
|
|
|
|
bool operator<(const MatType& m) const
|
|
|
|
{
|
|
|
|
return name < m.name;
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
MatType(std::string& n, df::job_material_category jm, df::armor_general_flags af)
|
|
|
|
: name(n), job_material(jm), armor_flag(af) {};
|
|
|
|
MatType(const char* n, df::job_material_category jm, df::armor_general_flags af)
|
|
|
|
: name(std::string(n)), job_material(jm), armor_flag(af) {};
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
};
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
const MatType
|
|
|
|
M_SILK = MatType("silk", df::job_material_category::mask_silk, df::armor_general_flags::SOFT),
|
|
|
|
M_CLOTH = MatType("cloth", df::job_material_category::mask_cloth, df::armor_general_flags::SOFT),
|
|
|
|
M_YARN = MatType("yarn", df::job_material_category::mask_yarn, df::armor_general_flags::SOFT),
|
|
|
|
M_LEATHER = MatType("leather", df::job_material_category::mask_leather, df::armor_general_flags::LEATHER);
|
|
|
|
|
|
|
|
std::list<MatType> all_materials = { M_SILK, M_CLOTH, M_YARN, M_LEATHER };
|
2019-11-02 19:07:10 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::map<std::pair<df::item_type, int>, int> available; // key is item type & size
|
|
|
|
std::map<std::pair<df::item_type, int>, int> needed; // same
|
|
|
|
std::map<std::pair<df::item_type, int>, int> queued; // same
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::map<int, int> sizes; // this maps body size to races
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::map<std::tuple<df::job_type, int, int>, int> orders; // key is item type, item subtype, size
|
2019-11-02 19:07:10 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
std::map<MatType, int> supply;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
color_ostream* out;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
std::list<MatType> material_order = { M_SILK, M_CLOTH, M_YARN, M_LEATHER };
|
|
|
|
std::map<MatType, int> reserves;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
int default_reserve = 10;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void reset()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
available.clear();
|
|
|
|
needed.clear();
|
|
|
|
queued.clear();
|
|
|
|
sizes.clear();
|
|
|
|
orders.clear();
|
|
|
|
supply.clear();
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void scan_clothing()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto i : world->items.other[df::items_other_id::ANY_GENERIC37]) // GENERIC37 is "clothing"
|
2018-08-26 08:30:08 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
if (i->flags.whole & bad_flags.whole)
|
|
|
|
continue;
|
|
|
|
if (i->flags.bits.owned)
|
|
|
|
continue;
|
|
|
|
if (i->getWear() >= 1)
|
|
|
|
continue;
|
|
|
|
df::item_type t = i->getType();
|
|
|
|
int size = world->raws.creatures.all[i->getMakerRace()]->adultsize;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
available[std::make_pair(t, size)] += 1;
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void scan_materials()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2023-01-20 14:21:45 -07:00
|
|
|
bool require_dyed = df::global::standing_orders_use_dyed_cloth ? (*df::global::standing_orders_use_dyed_cloth) : false;
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto i : world->items.other[df::items_other_id::CLOTH])
|
|
|
|
{
|
|
|
|
if (i->flags.whole & bad_flags.whole)
|
|
|
|
continue;
|
2023-01-20 14:21:45 -07:00
|
|
|
|
|
|
|
if (require_dyed && !i->hasImprovements())
|
|
|
|
{
|
|
|
|
// only count dyed
|
|
|
|
std::string d;
|
|
|
|
i->getItemDescription(&d, 0);
|
|
|
|
TRACE(cycle).print("tailor: skipping undyed %s\n", d.c_str());
|
2021-08-08 08:53:32 -06:00
|
|
|
continue;
|
2023-01-20 14:21:45 -07:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
MaterialInfo mat(i);
|
|
|
|
int ss = i->getStackSize();
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
if (mat.material)
|
|
|
|
{
|
|
|
|
if (mat.material->flags.is_set(df::material_flags::SILK))
|
|
|
|
supply[M_SILK] += ss;
|
|
|
|
else if (mat.material->flags.is_set(df::material_flags::THREAD_PLANT))
|
|
|
|
supply[M_CLOTH] += ss;
|
|
|
|
else if (mat.material->flags.is_set(df::material_flags::YARN))
|
|
|
|
supply[M_YARN] += ss;
|
2023-01-20 14:21:45 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string d;
|
|
|
|
i->getItemDescription(&d, 0);
|
|
|
|
WARN(cycle).print("tailor: weird cloth item found: %s (%d)\n", d.c_str(), i->id);
|
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto i : world->items.other[df::items_other_id::SKIN_TANNED])
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
if (i->flags.whole & bad_flags.whole)
|
2018-08-25 10:59:28 -06:00
|
|
|
continue;
|
2021-08-08 08:53:32 -06:00
|
|
|
supply[M_LEATHER] += i->getStackSize();
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
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]);
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void scan_replacements()
|
|
|
|
{
|
|
|
|
for (auto u : world->units.active)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
if (!Units::isOwnCiv(u) ||
|
|
|
|
!Units::isOwnGroup(u) ||
|
|
|
|
!Units::isActive(u) ||
|
|
|
|
Units::isBaby(u))
|
|
|
|
continue; // skip units we don't control
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::set <df::item_type> wearing;
|
2021-08-08 08:53:32 -06:00
|
|
|
wearing.clear();
|
2019-11-02 19:07:10 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::deque<df::item*> worn;
|
2021-08-08 08:53:32 -06:00
|
|
|
worn.clear();
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto inv : u->inventory)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
if (inv->mode != df::unit_inventory_item::Worn)
|
|
|
|
continue;
|
|
|
|
if (inv->item->getWear() > 0)
|
|
|
|
worn.push_back(inv->item);
|
|
|
|
else
|
|
|
|
wearing.insert(inv->item->getType());
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
int size = world->raws.creatures.all[u->race]->adultsize;
|
|
|
|
sizes[size] = u->race;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
for (auto ty : std::set<df::item_type>{ df::item_type::ARMOR, df::item_type::PANTS, df::item_type::SHOES })
|
2021-08-08 08:53:32 -06:00
|
|
|
{
|
|
|
|
if (wearing.count(ty) == 0)
|
2023-01-18 13:40:31 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
for (auto w : worn)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
auto ty = w->getType();
|
|
|
|
auto o = itemTypeMap.at(ty);
|
|
|
|
|
|
|
|
int size = world->raws.creatures.all[w->getMakerRace()]->adultsize;
|
|
|
|
std::string description;
|
|
|
|
w->getItemDescription(&description, 0);
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
if (available[std::make_pair(ty, size)] > 0)
|
2021-08-08 08:53:32 -06:00
|
|
|
{
|
|
|
|
if (w->flags.bits.owned)
|
|
|
|
{
|
|
|
|
bool confiscated = Items::setOwner(w, NULL);
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
INFO(cycle).print(
|
2021-08-08 08:53:32 -06:00
|
|
|
"tailor: %s %s from %s.\n",
|
|
|
|
(confiscated ? "confiscated" : "could not confiscate"),
|
|
|
|
description.c_str(),
|
|
|
|
Translation::TranslateName(&u->name, false).c_str()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wearing.count(ty) == 0)
|
2023-01-18 13:40:31 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
if (w->getWear() > 1)
|
|
|
|
w->flags.bits.dump = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-20 14:21:45 -07:00
|
|
|
DEBUG(cycle).print ("tailor: %s worn by %s needs replacement, but none available\n",
|
2023-01-18 13:40:31 -07:00
|
|
|
description.c_str(),
|
|
|
|
Translation::TranslateName(&u->name, false).c_str());
|
|
|
|
orders[std::make_tuple(o, w->getSubtype(), size)] += 1;
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void create_orders()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2023-01-05 18:11:01 -07:00
|
|
|
auto entity = world->entities.all[plotinfo->civ_id];
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
for (auto& a : needed)
|
|
|
|
{
|
|
|
|
df::item_type ty = a.first.first;
|
|
|
|
int size = a.first.second;
|
|
|
|
int count = a.second;
|
|
|
|
|
|
|
|
int sub = 0;
|
2023-01-18 13:40:31 -07:00
|
|
|
std::vector<int16_t> v;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
|
|
|
switch (ty) {
|
2021-08-08 08:53:32 -06:00
|
|
|
case df::item_type::ARMOR: v = entity->resources.armor_type; break;
|
|
|
|
case df::item_type::GLOVES: v = entity->resources.gloves_type; break;
|
|
|
|
case df::item_type::HELM: v = entity->resources.helm_type; break;
|
|
|
|
case df::item_type::PANTS: v = entity->resources.pants_type; break;
|
|
|
|
case df::item_type::SHOES: v = entity->resources.shoes_type; break;
|
2019-12-06 11:24:27 -07:00
|
|
|
default: break;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
for (auto vv : v) {
|
|
|
|
bool isClothing = false;
|
|
|
|
switch (ty) {
|
|
|
|
case df::item_type::ARMOR: isClothing = world->raws.itemdefs.armor[vv]->armorlevel == 0; break;
|
|
|
|
case df::item_type::GLOVES: isClothing = world->raws.itemdefs.gloves[vv]->armorlevel == 0; break;
|
|
|
|
case df::item_type::HELM: isClothing = world->raws.itemdefs.helms[vv]->armorlevel == 0; break;
|
|
|
|
case df::item_type::PANTS: isClothing = world->raws.itemdefs.pants[vv]->armorlevel == 0; break;
|
|
|
|
case df::item_type::SHOES: isClothing = world->raws.itemdefs.shoes[vv]->armorlevel == 0; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (isClothing)
|
|
|
|
{
|
|
|
|
sub = vv;
|
|
|
|
break;
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
const df::job_type j = itemTypeMap.at(ty);
|
2023-01-18 13:40:31 -07:00
|
|
|
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);
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void scan_existing_orders()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto o : world->manager_orders)
|
|
|
|
{
|
|
|
|
auto f = jobTypeMap.find(o->job_type);
|
|
|
|
if (f == jobTypeMap.end())
|
|
|
|
continue;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
auto sub = o->item_subtype;
|
|
|
|
int race = o->hist_figure_id;
|
|
|
|
if (race == -1)
|
|
|
|
continue; // -1 means that the race of the worker will determine the size made; we must ignore these jobs
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
int size = world->raws.creatures.all[race]->adultsize;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
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);
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
void place_orders()
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2023-01-05 18:11:01 -07:00
|
|
|
auto entity = world->entities.all[plotinfo->civ_id];
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto& o : orders)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
df::job_type ty;
|
|
|
|
int sub;
|
|
|
|
int size;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
std::tie(ty, sub, size) = o.first;
|
2021-08-08 08:53:32 -06:00
|
|
|
int count = o.second;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
if (count > 0)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2023-01-18 13:40:31 -07:00
|
|
|
std::vector<int16_t> v;
|
2021-08-08 08:53:32 -06:00
|
|
|
BitArray<df::armor_general_flags>* fl;
|
2023-01-18 13:40:31 -07:00
|
|
|
std::string name_s, name_p;
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
switch (ty) {
|
|
|
|
|
|
|
|
case df::job_type::MakeArmor:
|
|
|
|
v = entity->resources.armor_type;
|
|
|
|
name_s = world->raws.itemdefs.armor[sub]->name;
|
|
|
|
name_p = world->raws.itemdefs.armor[sub]->name_plural;
|
|
|
|
fl = &world->raws.itemdefs.armor[sub]->props.flags;
|
|
|
|
break;
|
|
|
|
case df::job_type::MakeGloves:
|
|
|
|
name_s = world->raws.itemdefs.gloves[sub]->name;
|
|
|
|
name_p = world->raws.itemdefs.gloves[sub]->name_plural;
|
|
|
|
v = entity->resources.gloves_type;
|
|
|
|
fl = &world->raws.itemdefs.gloves[sub]->props.flags;
|
|
|
|
break;
|
|
|
|
case df::job_type::MakeHelm:
|
|
|
|
name_s = world->raws.itemdefs.helms[sub]->name;
|
|
|
|
name_p = world->raws.itemdefs.helms[sub]->name_plural;
|
|
|
|
v = entity->resources.helm_type;
|
|
|
|
fl = &world->raws.itemdefs.helms[sub]->props.flags;
|
|
|
|
break;
|
|
|
|
case df::job_type::MakePants:
|
|
|
|
name_s = world->raws.itemdefs.pants[sub]->name;
|
|
|
|
name_p = world->raws.itemdefs.pants[sub]->name_plural;
|
|
|
|
v = entity->resources.pants_type;
|
|
|
|
fl = &world->raws.itemdefs.pants[sub]->props.flags;
|
|
|
|
break;
|
|
|
|
case df::job_type::MakeShoes:
|
|
|
|
name_s = world->raws.itemdefs.shoes[sub]->name;
|
|
|
|
name_p = world->raws.itemdefs.shoes[sub]->name_plural;
|
|
|
|
v = entity->resources.shoes_type;
|
|
|
|
fl = &world->raws.itemdefs.shoes[sub]->props.flags;
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-25 10:59:28 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
bool can_make = std::find(v.begin(), v.end(), sub) != v.end();
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
if (!can_make)
|
|
|
|
{
|
2023-01-18 13:40:31 -07:00
|
|
|
INFO(cycle).print("tailor: civilization cannot make %s, skipped\n", name_p.c_str());
|
2021-08-08 08:53:32 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:21:45 -07:00
|
|
|
DEBUG(cycle).print("tailor: ordering %d %s\n", count, name_p.c_str());
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
for (auto& m : material_order)
|
|
|
|
{
|
|
|
|
if (count <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto r = reserves.find(m);
|
|
|
|
int res = (r == reserves.end()) ? default_reserve : r->second;
|
|
|
|
|
|
|
|
if (supply[m] > res && fl->is_set(m.armor_flag)) {
|
|
|
|
int c = count;
|
|
|
|
if (supply[m] < count + res)
|
2023-01-20 14:21:45 -07:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
c = supply[m] - res;
|
2023-01-20 14:21:45 -07:00
|
|
|
TRACE(cycle).print("tailor: order reduced from %d to %d to protect reserves of %s\n",
|
|
|
|
count, c, m.name.c_str());
|
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
supply[m] -= c;
|
|
|
|
|
|
|
|
auto order = new df::manager_order;
|
|
|
|
order->job_type = ty;
|
|
|
|
order->item_type = df::item_type::NONE;
|
|
|
|
order->item_subtype = sub;
|
|
|
|
order->mat_type = -1;
|
|
|
|
order->mat_index = -1;
|
|
|
|
order->amount_left = c;
|
|
|
|
order->amount_total = c;
|
|
|
|
order->status.bits.validated = false;
|
|
|
|
order->status.bits.active = false;
|
|
|
|
order->id = world->manager_order_next_id++;
|
|
|
|
order->hist_figure_id = sizes[size];
|
|
|
|
order->material_category = m.job_material;
|
|
|
|
|
|
|
|
world->manager_orders.push_back(order);
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
INFO(cycle).print("tailor: added order #%d for %d %s %s, sized for %s\n",
|
2021-08-08 08:53:32 -06:00
|
|
|
order->id,
|
|
|
|
c,
|
|
|
|
bitfield_to_string(order->material_category).c_str(),
|
|
|
|
(c > 1) ? name_p.c_str() : name_s.c_str(),
|
|
|
|
world->raws.creatures.all[order->hist_figure_id]->name[1].c_str()
|
|
|
|
);
|
|
|
|
|
|
|
|
count -= c;
|
|
|
|
}
|
2023-01-20 14:21:45 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE(cycle).print("tailor: material %s skipped due to lack of reserves, %d available\n", m.name.c_str(), supply[m]);
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
public:
|
|
|
|
void do_scan(color_ostream& o)
|
|
|
|
{
|
|
|
|
out = &o;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
reset();
|
|
|
|
|
|
|
|
// scan for useable clothing
|
|
|
|
|
|
|
|
scan_clothing();
|
|
|
|
|
|
|
|
// scan for clothing raw materials
|
|
|
|
|
|
|
|
scan_materials();
|
|
|
|
|
|
|
|
// scan for units who need replacement clothing
|
|
|
|
|
|
|
|
scan_replacements();
|
|
|
|
|
|
|
|
// create new orders
|
|
|
|
|
|
|
|
create_orders();
|
|
|
|
|
|
|
|
// scan existing orders and subtract
|
|
|
|
|
|
|
|
scan_existing_orders();
|
|
|
|
|
|
|
|
// place orders
|
|
|
|
|
|
|
|
place_orders();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2023-01-18 13:40:31 -07:00
|
|
|
command_result set_materials(color_ostream& out, std::vector<std::string>& parameters)
|
2021-08-08 08:53:32 -06:00
|
|
|
{
|
2023-01-18 13:40:31 -07:00
|
|
|
std::list<MatType> newmat;
|
2021-08-08 08:53:32 -06:00
|
|
|
newmat.clear();
|
|
|
|
|
|
|
|
for (auto m = parameters.begin() + 1; m != parameters.end(); m++)
|
|
|
|
{
|
|
|
|
auto nameMatch = [m](MatType& m1) { return *m == m1.name; };
|
|
|
|
auto mm = std::find_if(all_materials.begin(), all_materials.end(), nameMatch);
|
|
|
|
if (mm == all_materials.end())
|
|
|
|
{
|
2023-01-18 13:40:31 -07:00
|
|
|
WARN(config,out).print("tailor: material %s not recognized\n", m->c_str());
|
2021-08-08 08:53:32 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
else {
|
|
|
|
newmat.push_back(*mm);
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
material_order = newmat;
|
2023-01-18 13:40:31 -07:00
|
|
|
INFO(config,out).print("tailor: material list set to %s\n", get_material_list().c_str());
|
2021-08-08 08:53:32 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string get_material_list()
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
for (const auto& m : material_order)
|
|
|
|
{
|
|
|
|
if (!s.empty()) s += ", ";
|
|
|
|
s += m.name;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void process(color_ostream& out)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (df::job_list_link* link = &world->jobs.list; link != NULL; link = link->next)
|
|
|
|
{
|
|
|
|
if (link->item == NULL) continue;
|
|
|
|
if (link->item->job_type == df::enums::job_type::UpdateStockpileRecords)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
do_scan(out);
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static std::unique_ptr<Tailor> tailor_instance;
|
2018-08-25 10:59:28 -06:00
|
|
|
|
|
|
|
#define DELTA_TICKS 600
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream& out)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
if (!enabled || !tailor_instance)
|
2018-08-25 10:59:28 -06:00
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (!Maps::IsValid())
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (DFHack::World::ReadPauseState())
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (world->frame_counter % DELTA_TICKS != 0)
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
CoreSuspender suspend;
|
|
|
|
tailor_instance->process(out);
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:40:31 -07:00
|
|
|
static command_result tailor_cmd(color_ostream& out, std::vector <std::string>& parameters) {
|
2018-08-25 10:59:28 -06:00
|
|
|
bool desired = enabled;
|
2021-09-06 20:32:19 -06:00
|
|
|
if (parameters.size() == 1 && (parameters[0] == "enable" || parameters[0] == "on" || parameters[0] == "1"))
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
desired = true;
|
|
|
|
}
|
2021-09-06 20:32:19 -06:00
|
|
|
else if (parameters.size() == 1 && (parameters[0] == "disable" || parameters[0] == "off" || parameters[0] == "0"))
|
2021-08-08 08:53:32 -06:00
|
|
|
{
|
|
|
|
desired = false;
|
|
|
|
}
|
2021-09-06 20:32:19 -06:00
|
|
|
else if (parameters.size() == 1 && (parameters[0] == "usage" || parameters[0] == "help" || parameters[0] == "?"))
|
2021-08-08 08:53:32 -06:00
|
|
|
{
|
2022-07-31 14:30:46 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
else if (parameters.size() == 1 && parameters[0] == "test")
|
|
|
|
{
|
|
|
|
if (tailor_instance)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
tailor_instance->do_scan(out);
|
|
|
|
return CR_OK;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
else
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
out.print("%s: not instantiated\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
else if (parameters.size() > 1 && parameters[0] == "materials")
|
|
|
|
{
|
|
|
|
if (tailor_instance)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
return tailor_instance->set_materials(out, parameters);
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
else
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
out.print("%s: not instantiated\n", plugin_name);
|
|
|
|
return CR_FAILURE;
|
2018-08-25 10:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 08:53:32 -06:00
|
|
|
else if (parameters.size() == 1 && parameters[0] != "status")
|
|
|
|
{
|
2018-08-26 08:30:08 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2021-08-08 08:53:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
out.print("Tailor is %s %s.\n", (desired == enabled) ? "currently" : "now", desired ? "enabled" : "disabled");
|
|
|
|
if (tailor_instance)
|
|
|
|
{
|
|
|
|
out.print("Material list is: %s\n", tailor_instance->get_material_list().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.print("%s: not instantiated\n", plugin_name);
|
|
|
|
}
|
2018-08-25 10:59:28 -06:00
|
|
|
|
|
|
|
enabled = desired;
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream& out, state_change_event event)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_enable(color_ostream& out, bool enable)
|
|
|
|
{
|
|
|
|
enabled = enable;
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream& out, std::vector <PluginCommand>& commands)
|
2018-08-25 10:59:28 -06:00
|
|
|
{
|
2021-08-08 08:53:32 -06:00
|
|
|
tailor_instance = std::move(dts::make_unique<Tailor>());
|
|
|
|
|
2018-08-25 10:59:28 -06:00
|
|
|
if (AUTOENABLE) {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
2022-07-31 14:30:46 -06:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
plugin_name,
|
|
|
|
"Automatically keep your dwarves in fresh clothing.",
|
|
|
|
tailor_cmd));
|
2018-08-25 10:59:28 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-08 08:53:32 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream& out)
|
|
|
|
{
|
|
|
|
tailor_instance.release();
|
|
|
|
|
2018-08-25 10:59:28 -06:00
|
|
|
return plugin_enable(out, false);
|
|
|
|
}
|