reinstated Buildings.setOwner

develop
Myk Taylor 2023-05-31 18:48:08 -07:00
parent acca228ac8
commit 974a6155c0
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 18 additions and 19 deletions

@ -1944,9 +1944,9 @@ General
Searches for a specific_ref with the given type. Searches for a specific_ref with the given type.
* ``dfhack.buildings.setOwner(item,unit)`` * ``dfhack.buildings.setOwner(civzone,unit)``
Replaces the owner of the building. If unit is *nil*, removes ownership. Replaces the owner of the civzone. If unit is *nil*, removes ownership.
Returns *false* in case of error. Returns *false* in case of error.
* ``dfhack.buildings.getSize(building)`` * ``dfhack.buildings.getSize(building)``

@ -108,9 +108,9 @@ DFHACK_EXPORT df::general_ref *getGeneralRef(df::building *building, df::general
DFHACK_EXPORT df::specific_ref *getSpecificRef(df::building *building, df::specific_ref_type type); DFHACK_EXPORT df::specific_ref *getSpecificRef(df::building *building, df::specific_ref_type type);
/** /**
* Sets the owner unit for the building. * Sets the owner unit for the zone.
*/ */
DFHACK_EXPORT bool setOwner(df::building *building, df::unit *owner); DFHACK_EXPORT bool setOwner(df::building_civzonest *building, df::unit *owner);
/** /**
* Find the building located at the specified tile. * Find the building located at the specified tile.

@ -350,46 +350,45 @@ df::specific_ref *Buildings::getSpecificRef(df::building *building, df::specific
return findRef(building->specific_refs, type); return findRef(building->specific_refs, type);
} }
bool Buildings::setOwner(df::building *bld, df::unit *unit) bool Buildings::setOwner(df::building_civzonest *bld, df::unit *unit)
{ {
CHECK_NULL_POINTER(bld); CHECK_NULL_POINTER(bld);
/* TODO: understand how this changes for v50
if (!bld->is_room) if (bld->assigned_unit == unit)
return false;
if (bld->owner == unit)
return true; return true;
if (bld->owner) df::building * pbld = dynamic_cast<df::building*>(bld);
if (bld->assigned_unit)
{ {
auto &blist = bld->owner->owned_buildings; auto &blist = bld->assigned_unit->owned_buildings;
vector_erase_at(blist, linear_index(blist, bld)); vector_erase_at(blist, linear_index(blist, pbld));
if (auto spouse = df::unit::find(bld->owner->relationship_ids[df::unit_relationship_type::Spouse])) if (auto spouse = df::unit::find(bld->assigned_unit->relationship_ids[df::unit_relationship_type::Spouse]))
{ {
auto &blist = spouse->owned_buildings; auto &blist = spouse->owned_buildings;
vector_erase_at(blist, linear_index(blist, bld)); vector_erase_at(blist, linear_index(blist, pbld));
} }
} }
bld->owner = unit; bld->assigned_unit = unit;
if (unit) if (unit)
{ {
bld->owner_id = unit->id; bld->assigned_unit_id = unit->id;
unit->owned_buildings.push_back(bld); unit->owned_buildings.push_back(bld);
if (auto spouse = df::unit::find(unit->relationship_ids[df::unit_relationship_type::Spouse])) if (auto spouse = df::unit::find(unit->relationship_ids[df::unit_relationship_type::Spouse]))
{ {
auto &blist = spouse->owned_buildings; auto &blist = spouse->owned_buildings;
if (bld->canUseSpouseRoom() && linear_index(blist, bld) < 0) if (bld->canUseSpouseRoom() && linear_index(blist, pbld) < 0)
blist.push_back(bld); blist.push_back(bld);
} }
} }
else else
{ {
bld->owner_id = -1; bld->assigned_unit_id = -1;
} }
*/
return true; return true;
} }