diff --git a/src/item.cpp b/src/item.cpp index b18dd7409fbf6..91f02f42a9a24 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -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() ) { @@ -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() ) { @@ -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 ) @@ -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 &info ) { if( info.empty() || info.back().sName != "--" ) { diff --git a/src/item.h b/src/item.h index 0cdaf4d78434d..ea010ce594ceb 100644 --- a/src/item.h +++ b/src/item.h @@ -1964,17 +1964,18 @@ class item : public visitable 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; @@ -2184,9 +2185,9 @@ class item : public visitable */ phase_id current_phase = static_cast( 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;