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)`` * ``dfhack.units.getUnitByNobleRole(role_name)``
Returns the unit assigned to the given noble role, if any. ``role_name`` must 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. be one of the position codes associated with the active fort or civilization
Normally, this includes: ``MILITIA_COMMANDER``, ``MILITIA_CAPTAIN``, government. For example: ``CAPTAIN_OF_THE_GUARD``, ``MAYOR``, or ``BARON``.
``SHERIFF``, ``CAPTAIN_OF_THE_GUARD``, ``EXPEDITION_LEADER``, ``MAYOR``, Note that if more than one unit has the role, only the first will be
``MANAGER``, ``CHIEF_MEDICAL_DWARF``, ``BROKER``, ``BOOKKEEPER``, returned. See ``getUnitsByNobleRole`` below for retrieving all units with a
``CHAMPION``, ``HAMMERER``, ``DUNGEON_MASTER``, and ``MESSENGER``. Note that particular role.
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)`` * ``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; 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) { 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) {
units.clear();
for (auto &assignment : positions.assignments) { for (auto &assignment : positions.assignments) {
if (assignment->position_id != noble_position_id) if (assignment->position_id != noble_position_id)
continue; 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) { 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]; auto &site = df::global::world->world_data->active_site[0];
for (auto &link : site->entity_links) { for (auto &link : site->entity_links) {
auto gov = df::historical_entity::find(link->entity_id); auto he = df::historical_entity::find(link->entity_id);
if (!gov || gov->type != df::historical_entity_type::SiteGovernment) if (!he ||
(he->type != df::historical_entity_type::SiteGovernment &&
he->type != df::historical_entity_type::Civilization))
continue; 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) if (noble_position_id < 0)
return; continue;
get_assigned_noble_units(units, gov->positions, noble_position_id, limit); add_assigned_noble_units(units, he->positions, noble_position_id, limit);
} }
} }
bool Units::getUnitsByNobleRole(vector<df::unit *> &units, std::string noble) { bool Units::getUnitsByNobleRole(vector<df::unit *> &units, std::string noble) {
units.clear();
get_units_by_noble_role(units, noble); get_units_by_noble_role(units, noble);
return !units.empty(); return !units.empty();
} }