Merge pull request #3020 from myk002/myk_save_choice

[buildingplan] remember "choose items" choice per building type
develop
Myk 2023-03-15 00:27:20 -07:00 committed by GitHub
commit 7edc686753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 3 deletions

@ -36,6 +36,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## New Plugins ## New Plugins
## Fixes ## Fixes
-@ `buildingplan`: remember choice per building type for whether the player wants to choose specific items
-@ `buildingplan`: items are now attached correctly to screw pumps and other multi-item buildings -@ `buildingplan`: items are now attached correctly to screw pumps and other multi-item buildings
-@ `buildingplan`: you can now attach multiple weapons to spike traps -@ `buildingplan`: you can now attach multiple weapons to spike traps

@ -151,7 +151,9 @@ or hit :kbd:`i` before placing the building. When you click to place the
building, a dialog will come up that allows you choose which items to use. The building, a dialog will come up that allows you choose which items to use. The
list is sorted by most recently used materials for that building type by list is sorted by most recently used materials for that building type by
default, but you can change to sort by name or by available quantity by default, but you can change to sort by name or by available quantity by
clicking on the "Sort by" selector or hitting :kbd:`R`. clicking on the "Sort by" selector or hitting :kbd:`R`. The configuration for
whether you would like to choose specific items is saved per building type and
will be restored when you plan more of that building type.
You can select the maximum quantity of a specified item by clicking on the item You can select the maximum quantity of a specified item by clicking on the item
name or selecting it with the arrow keys and hitting :kbd:`Enter`. You can name or selecting it with the arrow keys and hitting :kbd:`Enter`. You can

@ -890,6 +890,29 @@ static int getMaterialFilter(lua_State *L) {
return 1; return 1;
} }
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");
BuildingTypeKey key(type, subtype, custom);
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
}
static int getChooseItems(lua_State *L) {
color_ostream *out = Lua::GetOutput(L);
if (!out)
out = &Core::getInstance().getConsole();
df::building_type type = (df::building_type)luaL_checkint(L, 1);
int16_t subtype = luaL_checkint(L, 2);
int32_t custom = luaL_checkint(L, 3);
DEBUG(status,*out).print(
"entering getChooseItems building_type=%d subtype=%d custom=%d\n",
type, subtype, custom);
BuildingTypeKey key(type, subtype, custom);
Lua::Push(L, get_item_filters(*out, key).getChooseItems());
return 1;
}
static void setHeatSafetyFilter(color_ostream &out, df::building_type type, int16_t subtype, int32_t custom, int heat) { static void setHeatSafetyFilter(color_ostream &out, df::building_type type, int16_t subtype, int32_t custom, int heat) {
DEBUG(status,out).print("entering setHeatSafetyFilter\n"); DEBUG(status,out).print("entering setHeatSafetyFilter\n");
BuildingTypeKey key(type, subtype, custom); BuildingTypeKey key(type, subtype, custom);
@ -1061,6 +1084,7 @@ DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(countAvailableItems), DFHACK_LUA_FUNCTION(countAvailableItems),
DFHACK_LUA_FUNCTION(hasFilter), DFHACK_LUA_FUNCTION(hasFilter),
DFHACK_LUA_FUNCTION(clearFilter), DFHACK_LUA_FUNCTION(clearFilter),
DFHACK_LUA_FUNCTION(setChooseItems),
DFHACK_LUA_FUNCTION(setHeatSafetyFilter), DFHACK_LUA_FUNCTION(setHeatSafetyFilter),
DFHACK_LUA_FUNCTION(setQualityFilter), DFHACK_LUA_FUNCTION(setQualityFilter),
DFHACK_LUA_FUNCTION(getDescString), DFHACK_LUA_FUNCTION(getDescString),
@ -1076,6 +1100,7 @@ DFHACK_PLUGIN_LUA_COMMANDS {
DFHACK_LUA_COMMAND(getMaterialMaskFilter), DFHACK_LUA_COMMAND(getMaterialMaskFilter),
DFHACK_LUA_COMMAND(setMaterialFilter), DFHACK_LUA_COMMAND(setMaterialFilter),
DFHACK_LUA_COMMAND(getMaterialFilter), DFHACK_LUA_COMMAND(getMaterialFilter),
DFHACK_LUA_COMMAND(getChooseItems),
DFHACK_LUA_COMMAND(getHeatSafetyFilter), DFHACK_LUA_COMMAND(getHeatSafetyFilter),
DFHACK_LUA_COMMAND(getQualityFilter), DFHACK_LUA_COMMAND(getQualityFilter),
DFHACK_LUA_END DFHACK_LUA_END

@ -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;
}; };

@ -409,6 +409,9 @@ function PlannerOverlay:init()
end end
end end
end, end,
on_change=function(choose)
buildingplan.setChooseItems(uibs.building_type, uibs.building_subtype, uibs.custom_type, choose)
end,
}, },
widgets.CycleHotkeyLabel{ widgets.CycleHotkeyLabel{
view_id='safety', view_id='safety',
@ -549,7 +552,6 @@ function PlannerOverlay:onInput(keys)
end end
self.selected = 1 self.selected = 1
self.subviews.hollow:setOption(false) self.subviews.hollow:setOption(false)
self.subviews.choose:setOption(false)
self:reset() self:reset()
reset_counts_flag = true reset_counts_flag = true
return false return false
@ -635,6 +637,8 @@ function PlannerOverlay:onRenderFrame(dc, rect)
if reset_counts_flag then if reset_counts_flag then
self:reset() self:reset()
self.subviews.choose:setOption(require('plugins.buildingplan').getChooseItems(
uibs.building_type, uibs.building_subtype, uibs.custom_type))
self.subviews.safety:setOption(require('plugins.buildingplan').getHeatSafetyFilter( self.subviews.safety:setOption(require('plugins.buildingplan').getHeatSafetyFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type)) uibs.building_type, uibs.building_subtype, uibs.custom_type))
end end