From 17ba4929926fac3902d406440956743784dd2c1c Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 17 May 2018 19:34:03 -0400 Subject: [PATCH] Add some exclusion functions to Kitchen module, expose to Lua, fix build --- docs/Lua API.rst | 21 +++++++++ docs/changelog.txt | 3 ++ library/LuaApi.cpp | 35 +++++++++------ library/include/modules/Kitchen.h | 22 ++++++++-- library/modules/Kitchen.cpp | 71 ++++++++++++++++++++++++++----- library/xml | 2 +- plugins/search.cpp | 4 +- 7 files changed, 128 insertions(+), 30 deletions(-) diff --git a/docs/Lua API.rst b/docs/Lua API.rst index aa96e90eb..08aaf84fe 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -1735,6 +1735,27 @@ Constructions module Returns *true, was_only_planned* if removed; or *false* if none found. +Kitchen module +-------------- + +* ``dfhack.kitchen.findExclusion(type, item_type, item_index, mat_type, mat_index)`` + + Finds a kitchen exclusion in the vectors in ``df.global.ui.kitchen``. Returns + -1 if not found. + + * ``type`` is a ``df.kitchen_exc_type``, i.e. ``df.kitchen_exc_type.Cook`` or + ``df.kitchen_exc_type.Brew``. + * ``item_type`` is a ``df.item_type`` + * ``item_index``, ``mat_type``, and ``mat_index`` are all numeric + +* ``dfhack.kitchen.addExclusion(type, item_type, item_index, mat_type, mat_index)`` +* ``dfhack.kitchen.removeExclusion(type, item_type, item_index, mat_type, mat_index)`` + + Adds or removes a kitchen exclusion, using the same parameters as + ``findExclusion``. Both return ``true`` on success and ``false`` on failure, + e.g. when adding an exclusion that already exists or removing one that does + not. + Screen API ---------- diff --git a/docs/changelog.txt b/docs/changelog.txt index 8610fc994..55086abfa 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -60,6 +60,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - ``Items::canTradeWithContents()`` - ``Items::isRouteVehicle()`` - ``Items::isSquadEquipment()`` + - ``Kitchen::addExclusion()`` + - ``Kitchen::findExclusion()`` + - ``Kitchen::removeExclusion()`` # 0.44.10-beta1 diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index c71a0de9d..27199c651 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -43,22 +43,23 @@ distribution. #include "tinythread.h" #include "md5wrapper.h" -#include "modules/World.h" +#include "modules/Buildings.h" +#include "modules/Burrows.h" +#include "modules/Constructions.h" +#include "modules/Designations.h" +#include "modules/Filesystem.h" #include "modules/Gui.h" -#include "modules/Screen.h" -#include "modules/Job.h" -#include "modules/Translation.h" -#include "modules/Units.h" #include "modules/Items.h" -#include "modules/Materials.h" -#include "modules/Maps.h" +#include "modules/Job.h" +#include "modules/Kitchen.h" #include "modules/MapCache.h" -#include "modules/Burrows.h" -#include "modules/Buildings.h" -#include "modules/Constructions.h" +#include "modules/Maps.h" +#include "modules/Materials.h" #include "modules/Random.h" -#include "modules/Filesystem.h" -#include "modules/Designations.h" +#include "modules/Screen.h" +#include "modules/Translation.h" +#include "modules/Units.h" +#include "modules/World.h" #include "LuaWrapper.h" #include "LuaTools.h" @@ -2446,6 +2447,15 @@ static const luaL_Reg dfhack_designations_funcs[] = { {NULL, NULL} }; +/***** Kitchen module *****/ + +static const LuaWrapper::FunctionReg dfhack_kitchen_module[] = { + WRAPM(Kitchen, findExclusion), + WRAPM(Kitchen, addExclusion), + WRAPM(Kitchen, removeExclusion), + {NULL, NULL} +}; + /***** Console module *****/ namespace console { @@ -2998,6 +3008,7 @@ void OpenDFHackApi(lua_State *state) OpenModule(state, "screen", dfhack_screen_module, dfhack_screen_funcs); OpenModule(state, "filesystem", dfhack_filesystem_module, dfhack_filesystem_funcs); OpenModule(state, "designations", dfhack_designations_module, dfhack_designations_funcs); + OpenModule(state, "kitchen", dfhack_kitchen_module); OpenModule(state, "console", dfhack_console_module); OpenModule(state, "internal", dfhack_internal_module, dfhack_internal_funcs); } diff --git a/library/include/modules/Kitchen.h b/library/include/modules/Kitchen.h index ca88e9163..1ddf239bd 100644 --- a/library/include/modules/Kitchen.h +++ b/library/include/modules/Kitchen.h @@ -32,6 +32,7 @@ distribution. #include "VersionInfo.h" #include "Core.h" #include "modules/Items.h" +#include "df/kitchen_exc_type.h" /** * \defgroup grp_kitchen Kitchen settings @@ -42,14 +43,11 @@ namespace DFHack { namespace Kitchen { -typedef uint8_t t_exclusionType; - const unsigned int seedLimit = 400; // a limit on the limits which can be placed on seeds const t_itemSubtype organicSubtype = -1; // seems to fixed -const t_exclusionType cookingExclusion = 1; // seems to be fixed const df::enums::item_type::item_type limitType = df::enums::item_type::BAR; // used to store limit as an entry in the exclusion list. 0 = BAR const t_itemSubtype limitSubtype = 0; // used to store limit as an entry in the exclusion list -const t_exclusionType limitExclusion = 4; // used to store limit as an entry in the exclusion list +const df::kitchen_exc_type limitExclusion = df::kitchen_exc_type(4); // used to store limit as an entry in the exclusion list /** * Kitchen exclusions manipulator. Currently geared towards plants and seeds. @@ -79,5 +77,21 @@ DFHACK_EXPORT void setLimit(t_materialIndex materialIndex, unsigned int limit); DFHACK_EXPORT void clearLimits(); DFHACK_EXPORT std::size_t size(); + +// Finds the index of a kitchen exclusion in ui.kitchen.exc_types. Returns -1 if not found. +DFHACK_EXPORT int findExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index); + +// Adds an exclusion. Returns false if already excluded. +DFHACK_EXPORT bool addExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index); + +// Removes an exclusion. Returns false if not excluded. +DFHACK_EXPORT bool removeExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index); + } } diff --git a/library/modules/Kitchen.cpp b/library/modules/Kitchen.cpp index aa235780d..17b54896c 100644 --- a/library/modules/Kitchen.cpp +++ b/library/modules/Kitchen.cpp @@ -57,7 +57,7 @@ void Kitchen::allowPlantSeedCookery(t_materialIndex materialIndex) { if(ui->kitchen.mat_indices[i] == materialIndex && (ui->kitchen.item_types[i] == item_type::SEEDS || ui->kitchen.item_types[i] == item_type::PLANT) - && ui->kitchen.exc_types[i] == cookingExclusion + && ui->kitchen.exc_types[i] == df::kitchen_exc_type::Cook ) { match = true; @@ -73,7 +73,7 @@ void Kitchen::allowPlantSeedCookery(t_materialIndex materialIndex) ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); } } while(match); -}; +} void Kitchen::denyPlantSeedCookery(t_materialIndex materialIndex) { @@ -83,7 +83,7 @@ void Kitchen::denyPlantSeedCookery(t_materialIndex materialIndex) for(std::size_t i = 0; i < size(); ++i) { if(ui->kitchen.mat_indices[i] == materialIndex - && ui->kitchen.exc_types[i] == cookingExclusion) + && ui->kitchen.exc_types[i] == df::kitchen_exc_type::Cook) { if(ui->kitchen.item_types[i] == item_type::SEEDS) SeedAlreadyIn = true; @@ -97,7 +97,7 @@ void Kitchen::denyPlantSeedCookery(t_materialIndex materialIndex) ui->kitchen.item_subtypes.push_back(organicSubtype); ui->kitchen.mat_types.push_back(type->material_defs.type_seed); ui->kitchen.mat_indices.push_back(materialIndex); - ui->kitchen.exc_types.push_back(cookingExclusion); + ui->kitchen.exc_types.push_back(df::kitchen_exc_type::Cook); } if(!PlantAlreadyIn) { @@ -105,9 +105,9 @@ void Kitchen::denyPlantSeedCookery(t_materialIndex materialIndex) ui->kitchen.item_subtypes.push_back(organicSubtype); ui->kitchen.mat_types.push_back(type->material_defs.type_basic_mat); ui->kitchen.mat_indices.push_back(materialIndex); - ui->kitchen.exc_types.push_back(cookingExclusion); + ui->kitchen.exc_types.push_back(df::kitchen_exc_type::Cook); } -}; +} void Kitchen::fillWatchMap(std::map& watchMap) { @@ -119,7 +119,7 @@ void Kitchen::fillWatchMap(std::map& watchMap) watchMap[ui->kitchen.mat_indices[i]] = (unsigned int) ui->kitchen.mat_types[i]; } } -}; +} void Kitchen::removeLimit(t_materialIndex materialIndex) { @@ -148,7 +148,7 @@ void Kitchen::removeLimit(t_materialIndex materialIndex) ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); } } while(match); -}; +} void Kitchen::setLimit(t_materialIndex materialIndex, unsigned int limit) { @@ -162,7 +162,7 @@ void Kitchen::setLimit(t_materialIndex materialIndex, unsigned int limit) ui->kitchen.mat_indices.push_back(materialIndex); ui->kitchen.mat_types.push_back((t_materialType) (limit < seedLimit) ? limit : seedLimit); ui->kitchen.exc_types.push_back(limitExclusion); -}; +} void Kitchen::clearLimits() { @@ -190,9 +190,58 @@ void Kitchen::clearLimits() ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); } } while(match); -}; +} size_t Kitchen::size() { return ui->kitchen.item_types.size(); -}; +} + +int Kitchen::findExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index) +{ + for (size_t i = 0; i < size(); i++) + { + if (ui->kitchen.item_types[i] == item_type && + ui->kitchen.item_subtypes[i] == item_subtype && + ui->kitchen.mat_types[i] == mat_type && + ui->kitchen.mat_indices[i] == mat_index && + ui->kitchen.exc_types[i] == type) + { + return int(i); + } + } + return -1; +} + +bool Kitchen::addExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index) +{ + if (findExclusion(type, item_type, item_subtype, mat_type, mat_index) >= 0) + return false; + + ui->kitchen.item_types.push_back(item_type); + ui->kitchen.item_subtypes.push_back(item_subtype); + ui->kitchen.mat_types.push_back(mat_type); + ui->kitchen.mat_indices.push_back(mat_index); + ui->kitchen.exc_types.push_back(type); + return true; +} + +bool Kitchen::removeExclusion(df::kitchen_exc_type type, + df::item_type item_type, int16_t item_subtype, + int16_t mat_type, int32_t mat_index) +{ + int i = findExclusion(type, item_type, item_subtype, mat_type, mat_index); + if (i < 0) + return false; + + ui->kitchen.item_types.erase(ui->kitchen.item_types.begin() + i); + ui->kitchen.item_subtypes.erase(ui->kitchen.item_subtypes.begin() + i); + ui->kitchen.mat_types.erase(ui->kitchen.mat_types.begin() + i); + ui->kitchen.mat_indices.erase(ui->kitchen.mat_indices.begin() + i); + ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + i); + return true; +} diff --git a/library/xml b/library/xml index 007a22bfe..238b3053f 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit 007a22bfef6ca4007bab7e2b8f7316165dc833a3 +Subproject commit 238b3053f37feffdc0ab2d5d4415987c33a5d0e4 diff --git a/plugins/search.cpp b/plugins/search.cpp index 2827f0efd..58fca7d44 100644 --- a/plugins/search.cpp +++ b/plugins/search.cpp @@ -2167,8 +2167,8 @@ public: KVEC(int16_t, mat_type); \ KVEC(int32_t, mat_index); \ KVEC(int32_t, count); \ - KVEC(uint8_t, forbidden); \ - KVEC(uint8_t, possible) + KVEC(df::kitchen_pref_flag, forbidden); \ + KVEC(df::kitchen_pref_flag, possible) virtual void do_post_init()