Skip to content

Commit

Permalink
Merge pull request CleverRaven#38334 from davidpwbrown/dead_npc_owner…
Browse files Browse the repository at this point in the history
…_fix

Clear up item ownership for dead factions
  • Loading branch information
Rivet-the-Zombie authored Feb 26, 2020
2 parents de54ef5 + e9c6642 commit 241522e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ bool item::is_owned_by( const Character &c, bool available_to_take ) const
// owner.is_null() implies faction_id( "no_faction" ) which shouldnt happen, or no owner at all.
// either way, certain situations this means the thing is available to take.
// in other scenarios we actaully really want to check for id == id, even for no_faction
if( owner.is_null() ) {
if( get_owner().is_null() ) {
return available_to_take;
}
if( !c.get_faction() ) {
Expand All @@ -1048,7 +1048,7 @@ bool item::is_owned_by( const Character &c, bool available_to_take ) const

bool item::is_old_owner( const Character &c, bool available_to_take ) const
{
if( old_owner.is_null() ) {
if( get_old_owner().is_null() ) {
return available_to_take;
}
if( !c.get_faction() ) {
Expand All @@ -1060,11 +1060,11 @@ bool item::is_old_owner( const Character &c, bool available_to_take ) const

std::string item::get_owner_name() const
{
if( !g->faction_manager_ptr->get( owner ) ) {
if( !g->faction_manager_ptr->get( get_owner() ) ) {
debugmsg( "item::get_owner_name() item %s has no valid nor null faction id ", tname() );
return "no owner";
}
return g->faction_manager_ptr->get( owner )->name;
return g->faction_manager_ptr->get( get_owner() )->name;
}

void item::set_owner( const Character &c )
Expand All @@ -1078,14 +1078,26 @@ void item::set_owner( const Character &c )

faction_id item::get_owner() const
{
validate_ownership();
return owner;
}

faction_id item::get_old_owner() const
{
validate_ownership();
return old_owner;
}

void item::validate_ownership() const
{
if( !old_owner.is_null() && !g->faction_manager_ptr->get( old_owner, false ) ) {
remove_old_owner();
}
if( !owner.is_null() && !g->faction_manager_ptr->get( owner, false ) ) {
remove_owner();
}
}

static void insert_separation_line( std::vector<iteminfo> &info )
{
if( info.empty() || info.back().sName != "--" ) {
Expand Down
9 changes: 5 additions & 4 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1964,17 +1964,18 @@ class item : public visitable<item>
void set_birthday( const time_point &bday );
void handle_pickup_ownership( Character &c );
int get_gun_ups_drain() const;
void validate_ownership() const;
inline void set_old_owner( const faction_id &temp_owner ) {
old_owner = temp_owner;
}
inline void remove_old_owner() {
inline void remove_old_owner() const {
old_owner = faction_id::NULL_ID();
}
inline void set_owner( const faction_id &new_owner ) {
owner = new_owner;
}
void set_owner( const Character &c );
inline void remove_owner() {
inline void remove_owner() const {
owner = faction_id::NULL_ID();
}
faction_id get_owner() const;
Expand Down Expand Up @@ -2184,9 +2185,9 @@ class item : public visitable<item>
*/
phase_id current_phase = static_cast<phase_id>( 0 );
// The faction that owns this item.
faction_id owner = faction_id::NULL_ID();
mutable faction_id owner = faction_id::NULL_ID();
// The faction that previously owned this item
faction_id old_owner = faction_id::NULL_ID();
mutable faction_id old_owner = faction_id::NULL_ID();
int damage_ = 0;
light_emission light = nolight;

Expand Down

0 comments on commit 241522e

Please sign in to comment.