Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ Template for new versions:

## API

- ``DFHack::Units``: new function ``setPathGoal``

## Lua

- ``dfhack.units``: new function ``setPathGoal``

## Removed

# 50.14-r1
Expand Down
5 changes: 5 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,11 @@ Units module
``dfhack.units.isOwnCiv`` or another appropriate predicate on the unit
in question.

* ``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.

* ``dfhack.units.create(race, caste)``

Creates a new unit from scratch. The unit will be added to the
Expand Down
1 change: 1 addition & 0 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
WRAPM(Units, getIdentity),
WRAPM(Units, getNemesis),
WRAPM(Units, makeown),
WRAPM(Units, setPathGoal),
WRAPM(Units, create),
WRAPM(Units, getPhysicalAttrValue),
WRAPM(Units, getMentalAttrValue),
Expand Down
4 changes: 4 additions & 0 deletions library/include/modules/Units.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ranges>

Expand Down Expand Up @@ -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 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.
DFHACK_EXPORT df::unit *create(int16_t race, int16_t caste);
Expand Down
22 changes: 22 additions & 0 deletions library/modules/Units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -933,6 +934,27 @@ void Units::makeown(df::unit *unit) {
(*f)(unit);
}

// functionality reverse-engineered from DF's unitst::set_goal
void Units::setPathGoal(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)
{
if (auto mount = df::unit::find(unit->relationship_ids[df::unit_relationship_type::RiderMount]))
{
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);
Expand Down