From 1763a16831e85051f277afb7c56e390b2c324bd0 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 20 Aug 2023 10:08:25 -0700 Subject: [PATCH 1/3] add training check functions to Units --- docs/dev/Lua API.rst | 4 ++++ library/LuaApi.cpp | 4 ++++ library/include/modules/Units.h | 4 ++++ library/modules/Units.cpp | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index f29df78c6..d14e56640 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1380,6 +1380,10 @@ Units module * ``dfhack.units.isTame(unit)`` * ``dfhack.units.isTamable(unit)`` * ``dfhack.units.isDomesticated(unit)`` +* ``dfhack.units.isMarkedForTraining(unit)`` +* ``dfhack.units.isMarkedForTaming(unit)`` +* ``dfhack.units.isMarkedForWarTraining(unit)`` +* ``dfhack.units.isMarkedForHuntTraining(unit)`` * ``dfhack.units.isMarkedForSlaughter(unit)`` * ``dfhack.units.isMarkedForGelding(unit)`` * ``dfhack.units.isGeldable(unit)`` diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index beaf50579..f470e69c0 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1777,6 +1777,10 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = { WRAPM(Units, isTame), WRAPM(Units, isTamable), WRAPM(Units, isDomesticated), + WRAPM(Units, isMarkedForTraining), + WRAPM(Units, isMarkedForTaming), + WRAPM(Units, isMarkedForWarTraining), + WRAPM(Units, isMarkedForHuntTraining), WRAPM(Units, isMarkedForSlaughter), WRAPM(Units, isMarkedForGelding), WRAPM(Units, isGeldable), diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 19af4ec9a..11a6c120a 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -109,6 +109,10 @@ DFHACK_EXPORT bool isWar(df::unit* unit); DFHACK_EXPORT bool isTame(df::unit* unit); DFHACK_EXPORT bool isTamable(df::unit* unit); DFHACK_EXPORT bool isDomesticated(df::unit* unit); +DFHACK_EXPORT bool isMarkedForTraining(df::unit* unit); +DFHACK_EXPORT bool isMarkedForTaming(df::unit* unit); +DFHACK_EXPORT bool isMarkedForWarTraining(df::unit* unit); +DFHACK_EXPORT bool isMarkedForHuntTraining(df::unit* unit); DFHACK_EXPORT bool isMarkedForSlaughter(df::unit* unit); DFHACK_EXPORT bool isMarkedForGelding(df::unit* unit); DFHACK_EXPORT bool isGeldable(df::unit* unit); diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 1605a3d06..ebfd13b5e 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -76,6 +76,7 @@ using namespace std; #include "df/tile_occupancy.h" #include "df/plotinfost.h" #include "df/syndrome.h" +#include "df/training_assignment.h" #include "df/unit_inventory_item.h" #include "df/unit_misc_trait.h" #include "df/unit_relationship_type.h" @@ -510,6 +511,38 @@ bool Units::isDomesticated(df::unit* unit) return tame; } +static df::training_assignment * get_training_assignment(df::unit* unit) { + return binsearch_in_vector(df::global::plotinfo->equipment.training_assignments, + &df::training_assignment::animal_id, unit->id); +} + +bool Units::isMarkedForTraining(df::unit* unit) +{ + CHECK_NULL_POINTER(unit); + return !!get_training_assignment(unit); +} + +bool Units::isMarkedForTaming(df::unit* unit) +{ + CHECK_NULL_POINTER(unit); + auto assignment = get_training_assignment(unit); + return assignment && !assignment->flags.bits.train_war && !assignment->flags.bits.train_hunt; +} + +bool Units::isMarkedForWarTraining(df::unit* unit) +{ + CHECK_NULL_POINTER(unit); + auto assignment = get_training_assignment(unit); + return assignment && assignment->flags.bits.train_war; +} + +bool Units::isMarkedForHuntTraining(df::unit* unit) +{ + CHECK_NULL_POINTER(unit); + auto assignment = get_training_assignment(unit); + return assignment && assignment->flags.bits.train_hunt; +} + bool Units::isMarkedForSlaughter(df::unit* unit) { CHECK_NULL_POINTER(unit); From 8e9c6c708ae20b097a8174f09e16d2bcc02a8db2 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 20 Aug 2023 10:08:42 -0700 Subject: [PATCH 2/3] don't butcher animals who have some kind of training also use new units functions in logistics --- plugins/autobutcher.cpp | 1 + plugins/logistics.cpp | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/plugins/autobutcher.cpp b/plugins/autobutcher.cpp index 536c74f0f..483e29759 100644 --- a/plugins/autobutcher.cpp +++ b/plugins/autobutcher.cpp @@ -746,6 +746,7 @@ static bool isInappropriateUnit(df::unit *unit) { static bool isProtectedUnit(df::unit *unit) { return Units::isWar(unit) // ignore war dogs etc || Units::isHunter(unit) // ignore hunting dogs etc + || Units::isMarkedForTraining(unit) // ignore units marked for any kind of training // ignore creatures in built cages which are defined as rooms to leave zoos alone // (TODO: better solution would be to allow some kind of slaughter cages which you can place near the butcher) || (isContainedInItem(unit) && isInBuiltCageRoom(unit)) // !!! see comments in isBuiltCageRoom() diff --git a/plugins/logistics.cpp b/plugins/logistics.cpp index 69b864c5c..86f65d351 100644 --- a/plugins/logistics.cpp +++ b/plugins/logistics.cpp @@ -392,14 +392,14 @@ public: bool is_designated(color_ostream& out, df::item* item) override { auto unit = get_caged_unit(item); - return unit && has_training_assignment(unit); + return unit && Units::isMarkedForTraining(unit); } bool can_designate(color_ostream& out, df::item* item) override { auto unit = get_caged_unit(item); return unit && !Units::isInvader(unit) && Units::isTamable(unit) && !Units::isTame(unit) && - !has_training_assignment(unit); + !Units::isMarkedForTraining(unit); } bool designate(color_ostream& out, df::item* item) override { @@ -424,11 +424,6 @@ private: return NULL; return gref->getUnit(); } - - static bool has_training_assignment(df::unit* unit) { - return binsearch_index(df::global::plotinfo->equipment.training_assignments, - &df::training_assignment::animal_id, unit->id) > -1; - } }; static const struct BadFlags { From 6caed7debe7c66d0982c5dafce1bcec9c57187b4 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 20 Aug 2023 10:09:08 -0700 Subject: [PATCH 3/3] update changelog --- docs/changelog.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 8e4ee5d8c..b8db746af 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -64,6 +64,7 @@ Template for new versions: ## Misc Improvements - Surround DFHack-specific UI elements with square brackets instead of red-yellow blocks for better readability +- `autobutcher`: don't mark animals for butchering if they are already marked for some kind of training (war, hunt) - `hotkeys`: don't display DFHack logo in legends mode since it covers up important interface elements. the Ctrl-Shift-C hotkey to bring up the menu and the mouseover hotspot still function, though. - `sort`: animals are now sortable by race on the assignment screens @@ -72,6 +73,7 @@ Template for new versions: ## API - ``Items::getValue()``: remove ``caravan_buying`` parameter since the identity of the selling party doesn't actually affect the item value - `RemoteFortressReader`: add a ``force_reload`` option to the GetBlockList RPC API to return blocks regardless of whether they have changed since the last request +- ``Units``: new animal propery check functions ``isMarkedForTraining(unit)``, ``isMarkedForTaming(unit)``, ``isMarkedForWarTraining(unit)``, and ``isMarkedForHuntTraining(unit)`` ## Lua - ``new()``: improved error handling so that certain errors that were previously uncatchable (creating objects with members with unknown vtables) are now catchable with ``pcall()`` @@ -79,6 +81,7 @@ Template for new versions: - ``widgets.BannerPanel``: panel with distinctive border for marking DFHack UI elements on otherwise vanilla screens - ``widgets.Panel``: new functions to override instead of setting corresponding properties (useful when subclassing instead of just setting attributes): ``onDragBegin``, ``onDragEnd``, ``onResizeBegin``, ``onResizeEnd`` - ``dfhack.screen.readTile()``: now populates extended tile property fields (like ``top_of_text``) in the returned ``Pen`` object +- ``dfhack.units``: new animal propery check functions ``isMarkedForTraining(unit)``, ``isMarkedForTaming(unit)``, ``isMarkedForWarTraining(unit)``, and ``isMarkedForHuntTraining(unit)`` ## Removed