persist choose items choice per building type

develop
Myk Taylor 2023-03-14 22:26:56 -07:00
parent 4d8580d9e7
commit 306d300edf
No known key found for this signature in database
4 changed files with 15 additions and 5 deletions

@ -55,7 +55,6 @@ static PersistentDataItem config;
// for use in counting available materials for the UI // for use in counting available materials for the UI
static map<string, std::pair<MaterialInfo, string>> mat_cache; static map<string, std::pair<MaterialInfo, string>> mat_cache;
static unordered_map<BuildingTypeKey, vector<const df::job_item *>, BuildingTypeKeyHash> job_item_cache; static unordered_map<BuildingTypeKey, vector<const df::job_item *>, BuildingTypeKeyHash> job_item_cache;
static unordered_map<BuildingTypeKey, bool, BuildingTypeKeyHash> cur_choose_items;
static unordered_map<BuildingTypeKey, HeatSafety, BuildingTypeKeyHash> cur_heat_safety; static unordered_map<BuildingTypeKey, HeatSafety, BuildingTypeKeyHash> cur_heat_safety;
static unordered_map<BuildingTypeKey, DefaultItemFilters, BuildingTypeKeyHash> cur_item_filters; static unordered_map<BuildingTypeKey, DefaultItemFilters, BuildingTypeKeyHash> cur_item_filters;
// building id -> PlannedBuilding // building id -> PlannedBuilding
@ -267,7 +266,6 @@ static void clear_state(color_ostream &out) {
call_buildingplan_lua(&out, "reload_pens"); call_buildingplan_lua(&out, "reload_pens");
planned_buildings.clear(); planned_buildings.clear();
tasks.clear(); tasks.clear();
cur_choose_items.clear();
cur_heat_safety.clear(); cur_heat_safety.clear();
cur_item_filters.clear(); cur_item_filters.clear();
for (auto &entry : job_item_cache ) { for (auto &entry : job_item_cache ) {
@ -895,7 +893,8 @@ static int getMaterialFilter(lua_State *L) {
static void setChooseItems(color_ostream &out, df::building_type type, int16_t subtype, int32_t custom, bool choose) { static void setChooseItems(color_ostream &out, df::building_type type, int16_t subtype, int32_t custom, bool choose) {
DEBUG(status,out).print("entering setChooseItems\n"); DEBUG(status,out).print("entering setChooseItems\n");
BuildingTypeKey key(type, subtype, custom); BuildingTypeKey key(type, subtype, custom);
cur_choose_items[key] = choose; auto &filters = get_item_filters(out, key);
filters.setChooseItems(choose);
// no need to reset signal; no change to the state of any other UI element // no need to reset signal; no change to the state of any other UI element
} }
@ -910,7 +909,7 @@ static int getChooseItems(lua_State *L) {
"entering getChooseItems building_type=%d subtype=%d custom=%d\n", "entering getChooseItems building_type=%d subtype=%d custom=%d\n",
type, subtype, custom); type, subtype, custom);
BuildingTypeKey key(type, subtype, custom); BuildingTypeKey key(type, subtype, custom);
Lua::Push(L, cur_choose_items[key]); Lua::Push(L, get_item_filters(*out, key).getChooseItems());
return 1; return 1;
} }

@ -27,6 +27,7 @@ enum FilterConfigValues {
FILTER_CONFIG_TYPE = 0, FILTER_CONFIG_TYPE = 0,
FILTER_CONFIG_SUBTYPE = 1, FILTER_CONFIG_SUBTYPE = 1,
FILTER_CONFIG_CUSTOM = 2, FILTER_CONFIG_CUSTOM = 2,
FILTER_CONFIG_CHOOSE_ITEMS = 3,
}; };
enum BuildingConfigValues { enum BuildingConfigValues {

@ -32,13 +32,14 @@ static int get_max_quality(const df::job_item *jitem) {
} }
DefaultItemFilters::DefaultItemFilters(color_ostream &out, BuildingTypeKey key, const std::vector<const df::job_item *> &jitems) DefaultItemFilters::DefaultItemFilters(color_ostream &out, BuildingTypeKey key, const std::vector<const df::job_item *> &jitems)
: key(key) { : key(key), choose_items(false) {
DEBUG(status,out).print("creating persistent data for filter key %d,%d,%d\n", 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)); std::get<0>(key), std::get<1>(key), std::get<2>(key));
filter_config = World::AddPersistentData(FILTER_CONFIG_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_TYPE, std::get<0>(key));
set_config_val(filter_config, FILTER_CONFIG_SUBTYPE, std::get<1>(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)); set_config_val(filter_config, FILTER_CONFIG_CUSTOM, std::get<2>(key));
set_config_bool(filter_config, FILTER_CONFIG_CHOOSE_ITEMS, choose_items);
item_filters.resize(jitems.size()); item_filters.resize(jitems.size());
for (size_t idx = 0; idx < jitems.size(); ++idx) { for (size_t idx = 0; idx < jitems.size(); ++idx) {
item_filters[idx].setMaxQuality(get_max_quality(jitems[idx]), true); item_filters[idx].setMaxQuality(get_max_quality(jitems[idx]), true);
@ -48,6 +49,7 @@ DefaultItemFilters::DefaultItemFilters(color_ostream &out, BuildingTypeKey key,
DefaultItemFilters::DefaultItemFilters(color_ostream &out, PersistentDataItem &filter_config, const std::vector<const df::job_item *> &jitems) DefaultItemFilters::DefaultItemFilters(color_ostream &out, PersistentDataItem &filter_config, const std::vector<const df::job_item *> &jitems)
: key(getKey(filter_config)), filter_config(filter_config) { : key(getKey(filter_config)), filter_config(filter_config) {
choose_items = get_config_bool(filter_config, FILTER_CONFIG_CHOOSE_ITEMS);
auto &serialized = filter_config.val(); auto &serialized = filter_config.val();
DEBUG(status,out).print("deserializing item filters for key %d,%d,%d: %s\n", DEBUG(status,out).print("deserializing item filters for key %d,%d,%d: %s\n",
std::get<0>(key), std::get<1>(key), std::get<2>(key), serialized.c_str()); std::get<0>(key), std::get<1>(key), std::get<2>(key), serialized.c_str());
@ -60,6 +62,11 @@ DefaultItemFilters::DefaultItemFilters(color_ostream &out, PersistentDataItem &f
item_filters = filters; item_filters = filters;
} }
void DefaultItemFilters::setChooseItems(bool choose) {
choose_items = choose;
set_config_bool(filter_config, FILTER_CONFIG_CHOOSE_ITEMS, choose);
}
void DefaultItemFilters::setItemFilter(DFHack::color_ostream &out, const ItemFilter &filter, int index) { void DefaultItemFilters::setItemFilter(DFHack::color_ostream &out, const ItemFilter &filter, int index) {
if (index < 0 || item_filters.size() <= (size_t)index) { if (index < 0 || item_filters.size() <= (size_t)index) {
WARN(status,out).print("invalid index for filter key %d,%d,%d: %d\n", WARN(status,out).print("invalid index for filter key %d,%d,%d: %d\n",

@ -14,11 +14,14 @@ public:
DefaultItemFilters(DFHack::color_ostream &out, BuildingTypeKey key, const std::vector<const df::job_item *> &jitems); DefaultItemFilters(DFHack::color_ostream &out, BuildingTypeKey key, const std::vector<const df::job_item *> &jitems);
DefaultItemFilters(DFHack::color_ostream &out, DFHack::PersistentDataItem &filter_config, const std::vector<const df::job_item *> &jitems); DefaultItemFilters(DFHack::color_ostream &out, DFHack::PersistentDataItem &filter_config, const std::vector<const df::job_item *> &jitems);
void setChooseItems(bool choose);
void setItemFilter(DFHack::color_ostream &out, const ItemFilter &filter, int index); void setItemFilter(DFHack::color_ostream &out, const ItemFilter &filter, int index);
bool getChooseItems() const { return choose_items; }
const std::vector<ItemFilter> & getItemFilters() const { return item_filters; } const std::vector<ItemFilter> & getItemFilters() const { return item_filters; }
private: private:
DFHack::PersistentDataItem filter_config; DFHack::PersistentDataItem filter_config;
bool choose_items;
std::vector<ItemFilter> item_filters; std::vector<ItemFilter> item_filters;
}; };