2023-02-22 16:08:11 -07:00
|
|
|
#include "defaultitemfilters.h"
|
|
|
|
|
|
|
|
#include "Debug.h"
|
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
|
|
|
#include "modules/World.h"
|
|
|
|
|
|
|
|
namespace DFHack {
|
|
|
|
DBG_EXTERN(buildingplan, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
BuildingTypeKey DefaultItemFilters::getKey(PersistentDataItem &filter_config) {
|
|
|
|
return BuildingTypeKey(
|
|
|
|
(df::building_type)get_config_val(filter_config, FILTER_CONFIG_TYPE),
|
|
|
|
get_config_val(filter_config, FILTER_CONFIG_SUBTYPE),
|
|
|
|
get_config_val(filter_config, FILTER_CONFIG_CUSTOM));
|
|
|
|
}
|
|
|
|
|
2023-03-02 07:24:19 -07:00
|
|
|
static int get_max_quality(const df::job_item *jitem) {
|
|
|
|
if (jitem->flags2.bits.building_material ||
|
|
|
|
jitem->item_type == df::item_type::WOOD ||
|
|
|
|
jitem->item_type == df::item_type::BLOCKS ||
|
|
|
|
jitem->item_type == df::item_type::BAR ||
|
|
|
|
jitem->item_type == df::item_type::BOULDER)
|
|
|
|
return df::item_quality::Ordinary;
|
|
|
|
|
|
|
|
return df::item_quality::Masterful;
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:29:27 -06:00
|
|
|
static string serialize(const std::vector<ItemFilter> &item_filters, const std::set<std::string> &specials) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << serialize_item_filters(item_filters);
|
|
|
|
out << "|" << join_strings(",", specials);
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2023-02-22 16:08:11 -07:00
|
|
|
DefaultItemFilters::DefaultItemFilters(color_ostream &out, BuildingTypeKey key, const std::vector<const df::job_item *> &jitems)
|
2023-03-29 00:51:52 -06:00
|
|
|
: key(key), choose_items(ItemSelectionChoice::ITEM_SELECTION_CHOICE_FILTER) {
|
2023-02-22 16:08:11 -07:00
|
|
|
DEBUG(status,out).print("creating persistent data for filter key %d,%d,%d\n",
|
|
|
|
std::get<0>(key), std::get<1>(key), std::get<2>(key));
|
|
|
|
filter_config = World::AddPersistentData(FILTER_CONFIG_KEY);
|
|
|
|
set_config_val(filter_config, FILTER_CONFIG_TYPE, std::get<0>(key));
|
|
|
|
set_config_val(filter_config, FILTER_CONFIG_SUBTYPE, std::get<1>(key));
|
|
|
|
set_config_val(filter_config, FILTER_CONFIG_CUSTOM, std::get<2>(key));
|
2023-03-29 00:51:52 -06:00
|
|
|
set_config_val(filter_config, FILTER_CONFIG_CHOOSE_ITEMS, choose_items);
|
2023-02-22 16:08:11 -07:00
|
|
|
item_filters.resize(jitems.size());
|
2023-03-02 07:24:19 -07:00
|
|
|
for (size_t idx = 0; idx < jitems.size(); ++idx) {
|
2023-03-06 00:04:03 -07:00
|
|
|
item_filters[idx].setMaxQuality(get_max_quality(jitems[idx]), true);
|
2023-03-02 07:24:19 -07:00
|
|
|
}
|
2023-03-15 01:29:27 -06:00
|
|
|
filter_config.val() = serialize(item_filters, specials);
|
2023-02-22 16:08:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DefaultItemFilters::DefaultItemFilters(color_ostream &out, PersistentDataItem &filter_config, const std::vector<const df::job_item *> &jitems)
|
|
|
|
: key(getKey(filter_config)), filter_config(filter_config) {
|
2023-03-29 00:51:52 -06:00
|
|
|
choose_items = get_config_val(filter_config, FILTER_CONFIG_CHOOSE_ITEMS);
|
2023-03-29 16:09:18 -06:00
|
|
|
if (choose_items < ItemSelectionChoice::ITEM_SELECTION_CHOICE_FILTER ||
|
|
|
|
choose_items > ItemSelectionChoice::ITEM_SELECTION_CHOICE_AUTOMATERIAL)
|
|
|
|
choose_items = ItemSelectionChoice::ITEM_SELECTION_CHOICE_FILTER;
|
2023-02-22 16:08:11 -07:00
|
|
|
auto &serialized = filter_config.val();
|
2023-03-15 01:29:27 -06:00
|
|
|
DEBUG(status,out).print("deserializing default item filters for key %d,%d,%d: %s\n",
|
2023-02-22 16:08:11 -07:00
|
|
|
std::get<0>(key), std::get<1>(key), std::get<2>(key), serialized.c_str());
|
2023-03-27 09:38:18 -06:00
|
|
|
if (!jitems.size())
|
|
|
|
return;
|
2023-03-15 01:29:27 -06:00
|
|
|
std::vector<std::string> elems;
|
|
|
|
split_string(&elems, serialized, "|");
|
|
|
|
std::vector<ItemFilter> filters = deserialize_item_filters(out, elems[0]);
|
2023-02-22 16:08:11 -07:00
|
|
|
if (filters.size() != jitems.size()) {
|
|
|
|
WARN(status,out).print("ignoring invalid filters_str for key %d,%d,%d: '%s'\n",
|
|
|
|
std::get<0>(key), std::get<1>(key), std::get<2>(key), serialized.c_str());
|
|
|
|
item_filters.resize(jitems.size());
|
|
|
|
} else
|
|
|
|
item_filters = filters;
|
2023-03-15 01:29:27 -06:00
|
|
|
if (elems.size() > 1) {
|
|
|
|
vector<string> specs;
|
|
|
|
split_string(&specs, elems[1], ",");
|
2023-03-15 14:40:08 -06:00
|
|
|
for (auto & special : specs) {
|
|
|
|
if (special.size())
|
|
|
|
specials.emplace(special);
|
|
|
|
}
|
2023-03-15 01:29:27 -06:00
|
|
|
}
|
2023-02-22 16:08:11 -07:00
|
|
|
}
|
|
|
|
|
2023-03-29 00:51:52 -06:00
|
|
|
void DefaultItemFilters::setChooseItems(int choose) {
|
2023-03-14 23:26:56 -06:00
|
|
|
choose_items = choose;
|
2023-03-29 00:51:52 -06:00
|
|
|
set_config_val(filter_config, FILTER_CONFIG_CHOOSE_ITEMS, choose);
|
2023-03-14 23:26:56 -06:00
|
|
|
}
|
|
|
|
|
2023-03-15 01:29:27 -06:00
|
|
|
void DefaultItemFilters::setSpecial(const std::string &special, bool val) {
|
|
|
|
if (val)
|
|
|
|
specials.emplace(special);
|
|
|
|
else
|
|
|
|
specials.erase(special);
|
|
|
|
filter_config.val() = serialize(item_filters, specials);
|
|
|
|
}
|
|
|
|
|
2023-02-22 16:08:11 -07:00
|
|
|
void DefaultItemFilters::setItemFilter(DFHack::color_ostream &out, const ItemFilter &filter, int index) {
|
2023-02-23 00:19:04 -07:00
|
|
|
if (index < 0 || item_filters.size() <= (size_t)index) {
|
2023-02-22 16:08:11 -07:00
|
|
|
WARN(status,out).print("invalid index for filter key %d,%d,%d: %d\n",
|
|
|
|
std::get<0>(key), std::get<1>(key), std::get<2>(key), index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
item_filters[index] = filter;
|
2023-03-15 01:29:27 -06:00
|
|
|
filter_config.val() = serialize(item_filters, specials);
|
2023-02-22 16:08:11 -07:00
|
|
|
DEBUG(status,out).print("updated item filter and persisted for key %d,%d,%d: %s\n",
|
|
|
|
std::get<0>(key), std::get<1>(key), std::get<2>(key), filter_config.val().c_str());
|
|
|
|
}
|