Skip to content

Commit

Permalink
move has_rotten_away from map to item
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT committed Feb 14, 2020
1 parent 19dfdd4 commit a8964cf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
33 changes: 33 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8137,6 +8137,39 @@ bool item::detonate( const tripoint &p, std::vector<item> &drops )
return false;
}

bool item::has_rotten_away( const tripoint &pnt )
{
if( is_corpse() && goes_bad() ) {
process_temperature_rot( 1, pnt, nullptr );
return get_rot() > 10_days && !can_revive();
} else if( goes_bad() ) {
process_temperature_rot( 1, pnt, nullptr );
return has_rotten_away();
} else if( type->container && type->container->preserves ) {
// Containers like tin cans preserves all items inside, they do not rot at all.
return false;
} else if( type->container && type->container->seals ) {
// Items inside rot but do not vanish as the container seals them in.
for( auto &c : contents ) {
if( c.goes_bad() ) {
c.process_temperature_rot( 1, pnt, nullptr );
}
}
return false;
} else {
// Check and remove rotten contents, but always keep the container.
for( auto it = contents.begin(); it != contents.end(); ) {
if( it->has_rotten_away( pnt ) ) {
it = contents.erase( it );
} else {
++it;
}
}

return false;
}
}

bool item_ptr_compare_by_charges( const item *left, const item *right )
{
if( left->contents.empty() ) {
Expand Down
7 changes: 7 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,13 @@ class item : public visitable<item>
* @param mod How many charges should be removed.
*/
void mod_charges( int mod );
/**
* Whether the item has to be removed as it has rotten away completely. May change the item as it calls process_temperature_rot()
* @param pnt The *absolute* position of the item in the world (see @ref map::getabs),
* used for rot calculation.
* @return true if the item has rotten away and should be removed, false otherwise.
*/
bool has_rotten_away( const tripoint &pnt );

/**
* Accumulate rot of the item since last rot calculation.
Expand Down
35 changes: 1 addition & 34 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6665,45 +6665,12 @@ void map::loadn( const tripoint &grid, const bool update_vehicles )
abs_sub.z = old_abs_z;
}

bool map::has_rotten_away( item &itm, const tripoint &pnt ) const
{
if( itm.is_corpse() && itm.goes_bad() ) {
itm.process_temperature_rot( 1, pnt, nullptr );
return itm.get_rot() > 10_days && !itm.can_revive();
} else if( itm.goes_bad() ) {
itm.process_temperature_rot( 1, pnt, nullptr );
return itm.has_rotten_away();
} else if( itm.type->container && itm.type->container->preserves ) {
// Containers like tin cans preserves all items inside, they do not rot at all.
return false;
} else if( itm.type->container && itm.type->container->seals ) {
// Items inside rot but do not vanish as the container seals them in.
for( auto &c : itm.contents ) {
if( c.goes_bad() ) {
c.process_temperature_rot( 1, pnt, nullptr );
}
}
return false;
} else {
// Check and remove rotten contents, but always keep the container.
for( auto it = itm.contents.begin(); it != itm.contents.end(); ) {
if( has_rotten_away( *it, pnt ) ) {
it = itm.contents.erase( it );
} else {
++it;
}
}

return false;
}
}

template <typename Container>
void map::remove_rotten_items( Container &items, const tripoint &pnt )
{
const tripoint abs_pnt = getabs( pnt );
for( auto it = items.begin(); it != items.end(); ) {
if( has_rotten_away( *it, abs_pnt ) ) {
if( it->has_rotten_away( abs_pnt ) ) {
if( it->is_comestible() ) {
rotten_item_spawn( *it, pnt );
}
Expand Down
8 changes: 0 additions & 8 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1483,14 +1483,6 @@ class map
* Hacks in missing roofs. Should be removed when 3D mapgen is done.
*/
void add_roofs( const tripoint &grid );
/**
* Whether the item has to be removed as it has rotten away completely.
* @param itm Item to check for rotting
* @param pnt The *absolute* position of the item in the world (not just on this map!),
* used for rot calculation.
* @return true if the item has rotten away and should be removed, false otherwise.
*/
bool has_rotten_away( item &itm, const tripoint &pnt ) const;
/**
* Go through the list of items, update their rotten status and remove items
* that have rotten away completely.
Expand Down

0 comments on commit a8964cf

Please sign in to comment.