From c587cad47150722c78b064e59215f00728c8a4fd Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 5 Jul 2023 17:37:49 -0700 Subject: [PATCH] generalize to civ roles as well --- docs/dev/Lua API.rst | 12 +++++------- library/modules/Units.cpp | 16 +++++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 57e63dabb..0554629a9 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1441,13 +1441,11 @@ Units module * ``dfhack.units.getUnitByNobleRole(role_name)`` Returns the unit assigned to the given noble role, if any. ``role_name`` must - be one of the position codes associated with the active fort government. - Normally, this includes: ``MILITIA_COMMANDER``, ``MILITIA_CAPTAIN``, - ``SHERIFF``, ``CAPTAIN_OF_THE_GUARD``, ``EXPEDITION_LEADER``, ``MAYOR``, - ``MANAGER``, ``CHIEF_MEDICAL_DWARF``, ``BROKER``, ``BOOKKEEPER``, - ``CHAMPION``, ``HAMMERER``, ``DUNGEON_MASTER``, and ``MESSENGER``. Note that - if more than one unit has the role, only the first will be returned. See - ``getUnitsByNobleRole`` below for retrieving all units with a particular role. + be one of the position codes associated with the active fort or civilization + government. For example: ``CAPTAIN_OF_THE_GUARD``, ``MAYOR``, or ``BARON``. + Note that if more than one unit has the role, only the first will be + returned. See ``getUnitsByNobleRole`` below for retrieving all units with a + particular role. * ``dfhack.units.getUnitsByNobleRole(role_name)`` diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 9fb19cfb3..737ff0982 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -782,8 +782,7 @@ static int32_t get_noble_position_id(const df::historical_entity::T_positions &p return -1; } -static void get_assigned_noble_units(vector &units, const df::historical_entity::T_positions &positions, int32_t noble_position_id, size_t limit) { - units.clear(); +static void add_assigned_noble_units(vector &units, const df::historical_entity::T_positions &positions, int32_t noble_position_id, size_t limit) { for (auto &assignment : positions.assignments) { if (assignment->position_id != noble_position_id) continue; @@ -802,17 +801,20 @@ static void get_assigned_noble_units(vector &units, const df::histor static void get_units_by_noble_role(vector &units, string noble, size_t limit = 0) { auto &site = df::global::world->world_data->active_site[0]; for (auto &link : site->entity_links) { - auto gov = df::historical_entity::find(link->entity_id); - if (!gov || gov->type != df::historical_entity_type::SiteGovernment) + auto he = df::historical_entity::find(link->entity_id); + if (!he || + (he->type != df::historical_entity_type::SiteGovernment && + he->type != df::historical_entity_type::Civilization)) continue; - int32_t noble_position_id = get_noble_position_id(gov->positions, noble); + int32_t noble_position_id = get_noble_position_id(he->positions, noble); if (noble_position_id < 0) - return; - get_assigned_noble_units(units, gov->positions, noble_position_id, limit); + continue; + add_assigned_noble_units(units, he->positions, noble_position_id, limit); } } bool Units::getUnitsByNobleRole(vector &units, std::string noble) { + units.clear(); get_units_by_noble_role(units, noble); return !units.empty(); }