generalize to civ roles as well

develop
Myk Taylor 2023-07-05 17:37:49 -07:00
parent cbdb56a1ac
commit c587cad471
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 14 additions and 14 deletions

@ -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)``

@ -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<df::unit *> &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<df::unit *> &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<df::unit *> &units, const df::histor
static void get_units_by_noble_role(vector<df::unit *> &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<df::unit *> &units, std::string noble) {
units.clear();
get_units_by_noble_role(units, noble);
return !units.empty();
}