From a8964cf84bca7cf0333f63b337cd27ffeb4d004c Mon Sep 17 00:00:00 2001 From: KorGgenT Date: Fri, 14 Feb 2020 14:46:08 -0500 Subject: [PATCH] move has_rotten_away from map to item --- src/item.cpp | 33 +++++++++++++++++++++++++++++++++ src/item.h | 7 +++++++ src/map.cpp | 35 +---------------------------------- src/map.h | 8 -------- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 90784a6201ac1..ee7897af2982b 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8137,6 +8137,39 @@ bool item::detonate( const tripoint &p, std::vector &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() ) { diff --git a/src/item.h b/src/item.h index fa349b8b2d05c..e37307f7b1bc2 100644 --- a/src/item.h +++ b/src/item.h @@ -725,6 +725,13 @@ class item : public visitable * @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. diff --git a/src/map.cpp b/src/map.cpp index 39196b66865da..6dd96ad944406 100755 --- a/src/map.cpp +++ b/src/map.cpp @@ -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 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 ); } diff --git a/src/map.h b/src/map.h index cf7e4e7f0bc0d..0e3207a061edd 100644 --- a/src/map.h +++ b/src/map.h @@ -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.