Skip to content

Commit 9c69796

Browse files
authored
Merge pull request #4952 from chdoc/units-setGoal
DFHack::Units: new function `setGoal`
2 parents 7c417a4 + 2995c23 commit 9c69796

5 files changed

Lines changed: 36 additions & 0 deletions

File tree

docs/changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ Template for new versions:
6767

6868
## API
6969

70+
- ``DFHack::Units``: new function ``setPathGoal``
71+
7072
## Lua
7173

74+
- ``dfhack.units``: new function ``setPathGoal``
75+
7276
## Removed
7377

7478
# 50.14-r1

docs/dev/Lua API.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,11 @@ Units module
17031703
``dfhack.units.isOwnCiv`` or another appropriate predicate on the unit
17041704
in question.
17051705

1706+
* ``dfhack.units.setPathGoal(unit, pos, goal)``
1707+
1708+
Set target coordinates and goal (of type ``df.unit_path_goal``) for the given
1709+
unit. In case of a change, also clears the unit's current path.
1710+
17061711
* ``dfhack.units.create(race, caste)``
17071712

17081713
Creates a new unit from scratch. The unit will be added to the

library/LuaApi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
20652065
WRAPM(Units, getIdentity),
20662066
WRAPM(Units, getNemesis),
20672067
WRAPM(Units, makeown),
2068+
WRAPM(Units, setPathGoal),
20682069
WRAPM(Units, create),
20692070
WRAPM(Units, getPhysicalAttrValue),
20702071
WRAPM(Units, getMentalAttrValue),

library/include/modules/Units.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ distribution.
4242
#include "df/physical_attribute_type.h"
4343
#include "df/unit_action.h"
4444
#include "df/unit_action_type_group.h"
45+
#include "df/unit_path_goal.h"
4546

4647
#include <ranges>
4748

@@ -238,6 +239,9 @@ DFHACK_EXPORT bool unassignTrainer(df::unit *unit);
238239
/// to determine if the makeown operation was successful.
239240
DFHACK_EXPORT void makeown(df::unit *unit);
240241

242+
// Set the units target location and goal, clearing any existing goal or path
243+
DFHACK_EXPORT void setPathGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal);
244+
241245
/// Create new unit and add to all units vector (but not active). No HF, nemesis, pos,
242246
/// labors, or group associations. Will have race, caste, name, soul, body, and mind.
243247
DFHACK_EXPORT df::unit *create(int16_t race, int16_t caste);

library/modules/Units.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ distribution.
7373
#include "df/unit_action_type_group.h"
7474
#include "df/unit_inventory_item.h"
7575
#include "df/unit_misc_trait.h"
76+
#include "df/unit_path_goal.h"
7677
#include "df/unit_relationship_type.h"
7778
#include "df/unit_skill.h"
7879
#include "df/unit_soul.h"
@@ -933,6 +934,27 @@ void Units::makeown(df::unit *unit) {
933934
(*f)(unit);
934935
}
935936

937+
// functionality reverse-engineered from DF's unitst::set_goal
938+
void Units::setPathGoal(df::unit *unit, df::coord pos, df::unit_path_goal goal)
939+
{
940+
if (unit->path.dest != pos || unit->path.goal != goal)
941+
{
942+
unit->path.dest = pos;
943+
unit->path.goal = goal;
944+
unit->path.path.clear();
945+
}
946+
947+
if (unit->flags1.bits.rider && unit->mount_type == df::rider_positions_type::STANDARD)
948+
{
949+
if (auto mount = df::unit::find(unit->relationship_ids[df::unit_relationship_type::RiderMount]))
950+
{
951+
mount->path.dest = pos;
952+
mount->path.goal = goal;
953+
mount->path.path.clear();
954+
}
955+
}
956+
}
957+
936958
df::unit *Units::create(int16_t race, int16_t caste) {
937959
auto fp = df::global::unitst_more_convenient_create;
938960
CHECK_NULL_POINTER(fp);

0 commit comments

Comments
 (0)