Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move has_rotten_away from map to item #38022

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8305,6 +8305,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 @@ -720,6 +720,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 @@ -6687,45 +6687,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 @@ -1481,14 +1481,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