From b1255b0b3a10f2de7d881874f1b67c8c442525de Mon Sep 17 00:00:00 2001 From: Christian Doczkal <20443222+chdoc@users.noreply.github.com> Date: Sat, 21 Sep 2024 18:52:23 +0200 Subject: [PATCH 1/4] DFHack::Units: new function `setGoal` --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 5 +++++ library/LuaApi.cpp | 1 + library/include/modules/Units.h | 4 ++++ library/modules/Units.cpp | 23 +++++++++++++++++++++++ 5 files changed, 34 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index f3c68bb4a9..a03fe0fa2f 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -113,6 +113,7 @@ Template for new versions: - ``Units``: new ``isWildlife`` and ``isAgitated`` property checks - ``Items::createItem``: removed ``growth_print`` parameter; now determined automatically - ``DFHack::cuboid``: ``cuboid::clampMap`` now returns the cuboid itself (instead of boolean) to allow method chaining; call ``cuboid::isValid`` to determine success +- ``DFHack::Units``: new function ``setGoal`` ## Lua - ``dfhack.units``: ``isWildlife`` and ``isAgitated`` property checks diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 4967c58f0e..3b77b2561a 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1703,6 +1703,11 @@ Units module ``dfhack.units.isOwnCiv`` or another appropriate predicate on the unit in question. +* ``dfhack.units.setGoal(unit, pos, goal)`` + + Set target coordinates and goal (of type ``df.unit_path_goal``) for the given + unit. In case of a change, also clears the unit's current path. + * ``dfhack.units.create(race, caste)`` Creates a new unit from scratch. The unit will be added to the diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index e193bb8cc6..196b67276c 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2065,6 +2065,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = { WRAPM(Units, getIdentity), WRAPM(Units, getNemesis), WRAPM(Units, makeown), + WRAPM(Units, setGoal), WRAPM(Units, create), WRAPM(Units, getPhysicalAttrValue), WRAPM(Units, getMentalAttrValue), diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 7eaa61ec62..7060f0406b 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -42,6 +42,7 @@ distribution. #include "df/physical_attribute_type.h" #include "df/unit_action.h" #include "df/unit_action_type_group.h" +#include "df/unit_path_goal.h" #include @@ -238,6 +239,9 @@ DFHACK_EXPORT bool unassignTrainer(df::unit *unit); /// to determine if the makeown operation was successful. DFHACK_EXPORT void makeown(df::unit *unit); +// Set the units target location and goal, clearing any existing goal or path +DFHACK_EXPORT void setGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal); + /// Create new unit and add to all units vector (but not active). No HF, nemesis, pos, /// labors, or group associations. Will have race, caste, name, soul, body, and mind. DFHACK_EXPORT df::unit *create(int16_t race, int16_t caste); diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 6f58a38b2f..93ee63d5fc 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -73,6 +73,7 @@ distribution. #include "df/unit_action_type_group.h" #include "df/unit_inventory_item.h" #include "df/unit_misc_trait.h" +#include "df/unit_path_goal.h" #include "df/unit_relationship_type.h" #include "df/unit_skill.h" #include "df/unit_soul.h" @@ -933,6 +934,28 @@ void Units::makeown(df::unit *unit) { (*f)(unit); } +// functionality reverse-engineered from DF's unitst::set_goal +void Units::setGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal) +{ + if (unit->path.dest != pos || unit->path.goal != goal) + { + unit->path.dest = pos; + unit->path.goal = goal; + unit->path.path.clear(); + } + + if (unit->flags1.bits.rider && unit->mount_type == df::rider_positions_type::STANDARD) + { + auto mount = df::unit::find(unit->relationship_ids[df::unit_relationship_type::RiderMount]); + if (mount) + { + mount->path.dest = pos; + mount->path.goal = goal; + mount->path.path.clear(); + } + } +} + df::unit *Units::create(int16_t race, int16_t caste) { auto fp = df::global::unitst_more_convenient_create; CHECK_NULL_POINTER(fp); From 2c834b995a5c8b5e18fc9af4933f5a14eedcccac Mon Sep 17 00:00:00 2001 From: Christian Doczkal <20443222+chdoc@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:33:50 +0200 Subject: [PATCH 2/4] rename `setGoal` to `setPathGoal` --- docs/changelog.txt | 2 +- library/LuaApi.cpp | 2 +- library/include/modules/Units.h | 2 +- library/modules/Units.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index a03fe0fa2f..dbc1e97b64 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -113,7 +113,7 @@ Template for new versions: - ``Units``: new ``isWildlife`` and ``isAgitated`` property checks - ``Items::createItem``: removed ``growth_print`` parameter; now determined automatically - ``DFHack::cuboid``: ``cuboid::clampMap`` now returns the cuboid itself (instead of boolean) to allow method chaining; call ``cuboid::isValid`` to determine success -- ``DFHack::Units``: new function ``setGoal`` +- ``DFHack::Units``: new function ``setPathGoal`` ## Lua - ``dfhack.units``: ``isWildlife`` and ``isAgitated`` property checks diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 196b67276c..ddea7702f3 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2065,7 +2065,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = { WRAPM(Units, getIdentity), WRAPM(Units, getNemesis), WRAPM(Units, makeown), - WRAPM(Units, setGoal), + WRAPM(Units, setPathGoal), WRAPM(Units, create), WRAPM(Units, getPhysicalAttrValue), WRAPM(Units, getMentalAttrValue), diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 7060f0406b..9b356de105 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -240,7 +240,7 @@ DFHACK_EXPORT bool unassignTrainer(df::unit *unit); DFHACK_EXPORT void makeown(df::unit *unit); // Set the units target location and goal, clearing any existing goal or path -DFHACK_EXPORT void setGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal); +DFHACK_EXPORT void setPathGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal); /// Create new unit and add to all units vector (but not active). No HF, nemesis, pos, /// labors, or group associations. Will have race, caste, name, soul, body, and mind. diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 93ee63d5fc..c1c0a2c635 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -935,7 +935,7 @@ void Units::makeown(df::unit *unit) { } // functionality reverse-engineered from DF's unitst::set_goal -void Units::setGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal) +void Units::setPathGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal) { if (unit->path.dest != pos || unit->path.goal != goal) { From a0097a6ccb5a2002df769d7682b93ce5579c403a Mon Sep 17 00:00:00 2001 From: Christian Doczkal <20443222+chdoc@users.noreply.github.com> Date: Sat, 28 Sep 2024 11:12:15 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Myk --- docs/dev/Lua API.rst | 2 +- library/modules/Units.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 3b77b2561a..1bf0b40192 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1703,7 +1703,7 @@ Units module ``dfhack.units.isOwnCiv`` or another appropriate predicate on the unit in question. -* ``dfhack.units.setGoal(unit, pos, goal)`` +* ``dfhack.units.setPathGoal(unit, pos, goal)`` Set target coordinates and goal (of type ``df.unit_path_goal``) for the given unit. In case of a change, also clears the unit's current path. diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index c1c0a2c635..fc38bc2f34 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -946,8 +946,7 @@ void Units::setPathGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal) if (unit->flags1.bits.rider && unit->mount_type == df::rider_positions_type::STANDARD) { - auto mount = df::unit::find(unit->relationship_ids[df::unit_relationship_type::RiderMount]); - if (mount) + if (auto mount = df::unit::find(unit->relationship_ids[df::unit_relationship_type::RiderMount])) { mount->path.dest = pos; mount->path.goal = goal; From 2995c23c75ccdc3ad13e9b568c50123bafb31aab Mon Sep 17 00:00:00 2001 From: Christian Doczkal <20443222+chdoc@users.noreply.github.com> Date: Sat, 28 Sep 2024 11:23:07 +0200 Subject: [PATCH 4/4] fix changelog --- docs/changelog.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index dbc1e97b64..bfdcf087b0 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -66,8 +66,12 @@ Template for new versions: ## API +- ``DFHack::Units``: new function ``setPathGoal`` + ## Lua +- ``dfhack.units``: new function ``setPathGoal`` + ## Removed # 50.14-r1 @@ -113,7 +117,6 @@ Template for new versions: - ``Units``: new ``isWildlife`` and ``isAgitated`` property checks - ``Items::createItem``: removed ``growth_print`` parameter; now determined automatically - ``DFHack::cuboid``: ``cuboid::clampMap`` now returns the cuboid itself (instead of boolean) to allow method chaining; call ``cuboid::isValid`` to determine success -- ``DFHack::Units``: new function ``setPathGoal`` ## Lua - ``dfhack.units``: ``isWildlife`` and ``isAgitated`` property checks