Skip to content

Commit

Permalink
anti-crash refactor
Browse files Browse the repository at this point in the history
 - move `squares[destarea]` to where it is not undefined behaviour
 - typo fix
 - style
  • Loading branch information
Brambor committed Apr 22, 2024
1 parent e9d07b7 commit 4ec47a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,9 +2139,7 @@ bool advanced_inventory::query_charges( aim_location destarea, const advanced_in
// valid item is obviously required
cata_assert( !sitem.items.empty() );
const item &it = *sitem.items.front();
advanced_inv_area &p = squares[destarea];
const bool by_charges = it.count_by_charges();
const units::volume free_volume = panes[dest].free_volume( p );
// default to move all, unless if being equipped
const int input_amount = by_charges ? it.charges : action == "MOVE_SINGLE_ITEM" ? 1 : sitem.stacks;
// there has to be something to begin with
Expand All @@ -2159,8 +2157,9 @@ bool advanced_inventory::query_charges( aim_location destarea, const advanced_in
}

// Check volume, this should work the same for inventory, map and vehicles, but not for worn
const int room_for = it.charges_per_volume( free_volume );
if( amount > room_for && destarea != AIM_WORN && destarea != AIM_WIELD ) {
if( destarea != AIM_WORN && destarea != AIM_WIELD ) {
const units::volume free_volume = panes[dest].free_volume( squares[destarea] );
const int room_for = it.charges_per_volume( free_volume );
if( room_for <= 0 ) {
if( destarea == AIM_INVENTORY ) {
popup_getkey( _( "You have no space for the %s." ), it.tname() );
Expand All @@ -2175,7 +2174,9 @@ bool advanced_inventory::query_charges( aim_location destarea, const advanced_in
if( destarea != AIM_INVENTORY &&
destarea != AIM_WORN &&
destarea != AIM_WIELD &&
destarea != AIM_CONTAINER ) {
destarea != AIM_CONTAINER
) {
advanced_inv_area &p = squares[destarea];
const int cntmax = p.max_size - p.get_item_count();
// For items counted by charges, adding it adds 0 items if something there stacks with it.
const bool adds0 = by_charges && std::any_of( panes[dest].items.begin(), panes[dest].items.end(),
Expand Down
2 changes: 1 addition & 1 deletion src/advanced_inv.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class advanced_inventory
std::vector<drop_or_stash_item_info> &fav_list, bool forbid_buckets );

// Returns the x coordinate where the header started. The header is
// displayed right of it, everything left of it is till free.
// displayed right of it, everything left of it is still free.
int print_header( advanced_inventory_pane &pane, aim_location sel );
void init();
/**
Expand Down

0 comments on commit 4ec47a7

Please sign in to comment.