cache valid materials on world load

develop
Myk Taylor 2023-03-02 04:38:40 -08:00
parent 97e5fdb78e
commit 926bc8b7d4
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 54 additions and 6 deletions

@ -52,6 +52,7 @@ void set_config_bool(PersistentDataItem &c, int index, bool value) {
static PersistentDataItem config; static PersistentDataItem config;
// for use in counting available materials for the UI // for use in counting available materials for the UI
static vector<MaterialInfo> 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, 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;
@ -142,6 +143,47 @@ static const vector<const df::job_item *> & get_job_items(color_ostream &out, Bu
return jitems; return jitems;
} }
static void cache_matched(int16_t type, int32_t index) {
static const df::dfhack_material_category building_material_categories(
df::dfhack_material_category::mask_glass |
df::dfhack_material_category::mask_metal |
df::dfhack_material_category::mask_soap |
df::dfhack_material_category::mask_stone |
df::dfhack_material_category::mask_wood
);
MaterialInfo mi;
mi.decode(type, index);
if (mi.matches(building_material_categories)) {
DEBUG(status).print("cached material: %s\n", mi.toString().c_str());
mat_cache.emplace_back(mi);
}
else
TRACE(status).print("not matched: %s\n", mi.toString().c_str());
}
static void load_material_cache() {
df::world_raws &raws = world->raws;
for (int i = 1; i < DFHack::MaterialInfo::NUM_BUILTIN; ++i)
if (raws.mat_table.builtin[i])
cache_matched(i, -1);
for (size_t i = 0; i < raws.inorganics.size(); i++)
cache_matched(0, i);
for (size_t i = 0; i < raws.plants.all.size(); i++) {
df::plant_raw *p = raws.plants.all[i];
if (p->material.size() <= 1)
continue;
for (size_t j = 0; j < p->material.size(); j++) {
if (p->material[j]->id == "WOOD") {
cache_matched(DFHack::MaterialInfo::PLANT_BASE+j, i);
break;
}
}
}
}
static HeatSafety get_heat_safety_filter(const BuildingTypeKey &key) { static HeatSafety get_heat_safety_filter(const BuildingTypeKey &key) {
if (cur_heat_safety.count(key)) if (cur_heat_safety.count(key))
return cur_heat_safety.at(key); return cur_heat_safety.at(key);
@ -221,6 +263,7 @@ static void clear_state(color_ostream &out) {
} }
} }
job_item_cache.clear(); job_item_cache.clear();
mat_cache.clear();
} }
DFhackCExport command_result plugin_load_data (color_ostream &out) { DFhackCExport command_result plugin_load_data (color_ostream &out) {
@ -236,6 +279,8 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
DEBUG(status,out).print("loading persisted state\n"); DEBUG(status,out).print("loading persisted state\n");
clear_state(out); clear_state(out);
load_material_cache();
vector<PersistentDataItem> filter_configs; vector<PersistentDataItem> filter_configs;
World::GetPersistentData(&filter_configs, FILTER_CONFIG_KEY); World::GetPersistentData(&filter_configs, FILTER_CONFIG_KEY);
for (auto &cfg : filter_configs) { for (auto &cfg : filter_configs) {
@ -250,7 +295,7 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
PlannedBuilding pb(out, building_configs[idx]); PlannedBuilding pb(out, building_configs[idx]);
df::building *bld = df::building::find(pb.id); df::building *bld = df::building::find(pb.id);
if (!bld) { if (!bld) {
INFO(status).print("building %d no longer exists; skipping\n", pb.id); INFO(status,out).print("building %d no longer exists; skipping\n", pb.id);
pb.remove(out); pb.remove(out);
continue; continue;
} }
@ -639,7 +684,7 @@ static void clearFilter(color_ostream &out, df::building_type type, int16_t subt
TRACE(status,out).print("entering clearFilter\n"); TRACE(status,out).print("entering clearFilter\n");
BuildingTypeKey key(type, subtype, custom); BuildingTypeKey key(type, subtype, custom);
auto &filters = get_item_filters(out, key); auto &filters = get_item_filters(out, key);
if (filters.getItemFilters().size() <= index) if (index < 0 || filters.getItemFilters().size() <= (size_t)index)
return; return;
filters.setItemFilter(out, ItemFilter(), index); filters.setItemFilter(out, ItemFilter(), index);
call_buildingplan_lua(&out, "signal_reset"); call_buildingplan_lua(&out, "signal_reset");
@ -661,8 +706,8 @@ static int getMaterialFilter(lua_State *L) {
DEBUG(status,*out).print( DEBUG(status,*out).print(
"entering getMaterialFilter building_type=%d subtype=%d custom=%d index=%d\n", "entering getMaterialFilter building_type=%d subtype=%d custom=%d index=%d\n",
type, subtype, custom, index); type, subtype, custom, index);
vector<string> filter; map<MaterialInfo, int> counts_per_material;
Lua::PushVector(L, filter); Lua::Push(L, counts_per_material);
return 1; return 1;
} }
@ -697,7 +742,7 @@ static void setQualityFilter(color_ostream &out, df::building_type type, int16_t
DEBUG(status,out).print("entering setQualityFilter\n"); DEBUG(status,out).print("entering setQualityFilter\n");
BuildingTypeKey key(type, subtype, custom); BuildingTypeKey key(type, subtype, custom);
auto &filters = get_item_filters(out, key).getItemFilters(); auto &filters = get_item_filters(out, key).getItemFilters();
if (filters.size() <= index) if (index < 0 || filters.size() <= (size_t)index)
return; return;
ItemFilter filter = filters[index]; ItemFilter filter = filters[index];
filter.setDecoratedOnly(decorated != 0); filter.setDecoratedOnly(decorated != 0);
@ -825,7 +870,8 @@ DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(isPlannedBuilding), DFHACK_LUA_FUNCTION(isPlannedBuilding),
DFHACK_LUA_FUNCTION(addPlannedBuilding), DFHACK_LUA_FUNCTION(addPlannedBuilding),
DFHACK_LUA_FUNCTION(doCycle), DFHACK_LUA_FUNCTION(doCycle),
DFHACK_LUA_FUNCTION(scheduleCycle), DFHACK_LUA_FUNCTION(countAvailableItems), DFHACK_LUA_FUNCTION(scheduleCycle),
DFHACK_LUA_FUNCTION(countAvailableItems),
DFHACK_LUA_FUNCTION(hasFilter), DFHACK_LUA_FUNCTION(hasFilter),
DFHACK_LUA_FUNCTION(clearFilter), DFHACK_LUA_FUNCTION(clearFilter),
DFHACK_LUA_FUNCTION(setMaterialFilter), DFHACK_LUA_FUNCTION(setMaterialFilter),

@ -868,6 +868,8 @@ function QualityAndMaterialsPage:refresh()
subviews.min_quality:setOption(quality.min_quality) subviews.min_quality:setOption(quality.min_quality)
subviews.max_quality:setOption(quality.max_quality) subviews.max_quality:setOption(quality.max_quality)
local materials = getMaterialFilter(ibs.building_type, uibs.building_subtype, uibs.custom_type, self.index-1)
self.summary = summary self.summary = summary
self.dirty = false self.dirty = false
end end