From db0b41135105d165ead640d4d4d46d220491c873 Mon Sep 17 00:00:00 2001 From: joveeater Date: Sat, 13 Jan 2024 12:21:59 +0000 Subject: [PATCH] fix: Prevents reloading from deleting containers (#4104) * Prevents reloading from deleting containers * style(autofix.ci): automated formatting --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/item.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/item.cpp b/src/item.cpp index 25051aeb76c..b4667994e17 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -8186,9 +8186,7 @@ bool item::reload( player &u, item &loc, int qty ) } } - detached_ptr to_reload = item::spawn( *ammo ); - to_reload->charges = qty; - ammo->charges -= qty; + detached_ptr to_reload = ammo->split( qty ); bool merged = false; for( item *it : contents.all_items_top() ) { if( it->merge_charges( std::move( to_reload ) ) ) { @@ -8246,19 +8244,17 @@ bool item::reload( player &u, item &loc, int qty ) ammo->charges -= qty; charges += qty; } - } - - // we have transfered ammo from the container to the item - // therefore, we erase the 0-charge item inside container - // TODO: why don't we just remove 0-charge items? - if( ammo->charges == 0 && !ammo->has_flag( flag_SPEEDLOADER ) ) { - if( container != nullptr && !container->contents.empty() ) { - container->remove_item( container->contents.front() ); - u.inv_restack( ); // emptied containers do not stack with non-empty ones - } else { - loc.detach(); + // we have transfered ammo from the container to the item + // therefore, we erase the 0-charge item inside container + // TODO: why don't we just remove 0-charge items? + if( ammo->charges == 0 && !ammo->has_flag( flag_SPEEDLOADER ) ) { + ammo->detach(); + if( container != nullptr ) { + u.inv_restack(); + } } } + return true; } -- 2.43.0