Skip to content

Commit

Permalink
Reworks how the logic works
Browse files Browse the repository at this point in the history
There's one part I hate about this and that's the way I have to move stuff out of the `remove_with()` function to make it work.

Sort of works but non-count_by_charges harvest materials don't stack when spawning for some reason?
  • Loading branch information
KheirFerrum committed Jun 9, 2024
1 parent 3ffca8f commit 7f3ce80
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9916,14 +9916,14 @@ detached_ptr<item> item::process_internal( detached_ptr<item> &&self, player *ca
}
// All foods that go bad have temperature
if( ( self->is_food() || self->is_corpse() ) ) {
bool comestible = self->is_comestible();
item &obj = *self;
self = process_rot( std::move( self ), seals, pos, carrier, flag, weather_generator );
if( comestible && !self ) {
here.rotten_item_spawn( obj, pos );
}
if( obj.is_corpse() && !self ) {
here.handle_decayed_corpse( obj, here.getglobal( pos ) );
if( !self ) {
if( obj.is_comestible() ) {
here.rotten_item_spawn( obj, pos );
} else if( obj.is_corpse() ) {
here.handle_decayed_corpse( obj, pos );
}
}
}
return std::move( self );
Expand Down
37 changes: 17 additions & 20 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7263,25 +7263,32 @@ void map::loadn( const tripoint &grid, const bool update_vehicles )
template <typename Container>
void map::remove_rotten_items( Container &items, const tripoint &pnt, temperature_flag temperature )
{
items.remove_with( [this, &pnt, &temperature]( detached_ptr<item> &&it ) {
std::vector<item *> corpses_handle;
items.remove_with( [this, &pnt, &temperature, &corpses_handle]( detached_ptr<item> &&it ) {
item &obj = *it;
it = item::actualize_rot( std::move( it ), pnt, temperature, get_weather() );
if( !it && obj.is_comestible() ) {
rotten_item_spawn( obj, pnt );
// When !it, that means the item was removed from the world, ie: has rotted.
if( !it ) {
if( obj.is_comestible() ) {
rotten_item_spawn( obj, pnt );
} else if( obj.is_corpse() ) {
corpses_handle.push_back( &obj );
}
}
return std::move( it );
} );

for( const item *corpse : corpses_handle ) {
handle_decayed_corpse( *corpse, pnt );
}

}

void map::handle_decayed_corpse( const item &it, const tripoint_abs_ms &pnt )
void map::handle_decayed_corpse( const item &it, const tripoint &pnt )
{
if( !it.is_corpse() ) {
debugmsg( "Tried to decay a non-corpse item %s. Aborted", it.tname() );
return;
}
const mtype *dead_monster = it.get_corpse_mon();
if( !dead_monster ) {
debugmsg( "Corpse at tripoint %s has no associated monster?!", pnt.to_string() );
debugmsg( "Corpse at tripoint %s has no associated monster?!", getglobal( pnt ).to_string() );
return;
}

Expand All @@ -7302,7 +7309,7 @@ void map::handle_decayed_corpse( const item &it, const tripoint_abs_ms &pnt )
roll = std::min<int>( entry.max, std::round( rng_float( min_num, max_num ) ) );
}
for( int i = 0; i < roll; i++ ) {
add_item_or_charges( getlocal( pnt ), item::spawn( *harvest ) );
add_item_or_charges( pnt, item::spawn( *harvest ) );
}
}
}
Expand Down Expand Up @@ -7644,16 +7651,6 @@ void map::actualize( const tripoint &grid )
// plants contain a seed item which must not be removed under any circumstances
if( !furn.has_flag( "DONT_REMOVE_ROTTEN" ) ) {
temperature_flag temperature = temperature_flag_at_point( *this, pnt );
// Absolutely goofy, but checking for decay must be done early instead of in `remove_rotten_items` to avoid crashes
for( const auto &it : tmpsub->get_items( { x, y } ) ) {
if( it->is_corpse() ) {
detached_ptr<item> tmp = item::spawn( it->typeId(), it->birthday() );
it = item::actualize_rot( std::move( it ), pnt, temperature, get_weather() );
if( !it ) {
handle_decayed_corpse( *tmp, getglobal( pnt ) );
}
}
}
remove_rotten_items( tmpsub->get_items( { x, y } ), pnt, temperature );
}

Expand Down
2 changes: 1 addition & 1 deletion src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ class map
* @param it item that is spawning creatures
* @param pnt The point on this map where the item is and where bones/etc will be
*/
void handle_decayed_corpse( const item &it, const tripoint_abs_ms &pnt );
void handle_decayed_corpse( const item &it, const tripoint &pnt );

/**
* Checks to see if the item that is rotting away generates a creature when it does.
Expand Down

0 comments on commit 7f3ce80

Please sign in to comment.