Skip to content

Commit

Permalink
Merge branch 'spring-refactor' into test-release
Browse files Browse the repository at this point in the history
  • Loading branch information
joveeater committed May 21, 2023
2 parents 38cbe94 + a05a51c commit 2b5d611
Show file tree
Hide file tree
Showing 19 changed files with 562 additions and 185 deletions.
16 changes: 6 additions & 10 deletions src/armor_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct item_penalties {
};

// Figure out encumbrance penalties this clothing is involved in
item_penalties get_item_penalties( ItemList::const_iterator worn_item_it,
item_penalties get_item_penalties( location_vector<item>::const_iterator worn_item_it,
const Character &c, int tabindex )
{
item *const &worn_item = *worn_item_it;
Expand Down Expand Up @@ -166,7 +166,7 @@ std::string body_part_names( const std::vector<body_part> &parts )
}

void draw_mid_pane( const catacurses::window &w_sort_middle,
ItemList::const_iterator const worn_item_it,
location_vector<item>::const_iterator const worn_item_it,
const Character &c, int tabindex )
{
item *const &worn_item = *worn_item_it;
Expand Down Expand Up @@ -490,7 +490,7 @@ void show_armor_layers_ui( Character &who )
int leftListLines = 0;
int rightListLines = 0;

std::vector<ItemList::iterator> tmp_worn;
std::vector<location_vector<item>::iterator> tmp_worn;
std::array<std::string, 13> armor_cat = {{
_( "Torso" ), _( "Head" ), _( "Eyes" ), _( "Mouth" ), _( "L. Arm" ), _( "R. Arm" ),
_( "L. Hand" ), _( "R. Hand" ), _( "L. Leg" ), _( "R. Leg" ), _( "L. Foot" ),
Expand Down Expand Up @@ -823,16 +823,12 @@ void show_armor_layers_ui( Character &who )
}
}
} else if( action == "SORT_ARMOR" ) {
// Copy to a vector because stable_sort requires random-access
// iterators

std::vector<item *> worn_copy( who.worn.begin(), who.worn.end() );
std::stable_sort( worn_copy.begin(), worn_copy.end(),
std::stable_sort( who.worn.begin(),
who.worn.end(),
[]( item * const & l, item * const & r ) {
return l->get_layer() < r->get_layer();
}
);
std::copy( worn_copy.begin(), worn_copy.end(), who.worn.begin() );
who.reset_encumbrance();
} else if( action == "EQUIP_ARMOR" ) {
// filter inventory for all items that are armor/clothing
Expand Down Expand Up @@ -873,7 +869,7 @@ void show_armor_layers_ui( Character &who )
if( loc ) {
// wear the item
loc->obtain( who );
ItemList::iterator cursor_it = tmp_worn[leftListIndex];
location_vector<item>::iterator cursor_it = tmp_worn[leftListIndex];
if( !who.as_player()->wear_possessed( *loc, true, cursor_it ) && who.is_npc() ) {
// TODO: Pass the reason here
popup( _( "Can't put this on!" ) );
Expand Down
12 changes: 6 additions & 6 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,7 @@ int Character::get_mod_stat_from_bionic( const character_stat &Stat ) const
}

detached_ptr<item> Character::wear_item( detached_ptr<item> &&wear,
bool interactive, std::optional<std::vector<item *>::iterator> position )
bool interactive, std::optional<location_vector<item>::iterator> position )
{
if( !wear ) {
return std::move( wear );
Expand All @@ -2334,7 +2334,7 @@ detached_ptr<item> Character::wear_item( detached_ptr<item> &&wear,
last_item = to_wear.typeId();


std::vector<item *>::iterator pos = position ? *position : position_to_wear_new_item( to_wear );
location_vector<item>::iterator pos = position ? *position : position_to_wear_new_item( to_wear );
worn.insert( pos, std::move( wear ) );

if( interactive ) {
Expand Down Expand Up @@ -2385,7 +2385,7 @@ void Character::add_worn( detached_ptr<item> &&wear )
return;
}
item &to_wear = *wear;
std::vector<item *>::iterator pos = position_to_wear_new_item( to_wear );
location_vector<item>::iterator pos = position_to_wear_new_item( to_wear );
worn.insert( pos, std::move( wear ) );
to_wear.on_wear( *this );
inv.update_invlet( to_wear );
Expand Down Expand Up @@ -3279,7 +3279,7 @@ ret_val<bool> Character::can_wear( const item &it, bool with_equip_change ) cons
}

bool Character::wear_possessed( item &to_wear, bool interactive,
std::optional<std::vector<item *>::iterator> position )
std::optional<location_vector<item>::iterator> position )
{
if( is_worn( to_wear ) ) {
if( interactive ) {
Expand Down Expand Up @@ -4260,7 +4260,7 @@ int layer_details::layer( const int encumbrance )
return total - current;
}

ItemList::iterator Character::position_to_wear_new_item( const item &new_item )
location_vector<item>::iterator Character::position_to_wear_new_item( const item &new_item )
{
// By default we put this item on after the last item on the same or any
// lower layer.
Expand Down Expand Up @@ -4296,7 +4296,7 @@ void Character::item_encumb( char_encumbrance_data &vals, const item &new_item )
vals = char_encumbrance_data();

// Figure out where new_item would be worn
ItemList::const_iterator new_item_position = worn.end();
location_vector<item>::const_iterator new_item_position = worn.end();
if( !new_item.is_null() ) {
// const_cast required to work around g++-4.8 library bug
// see the commit that added this comment to understand why
Expand Down
6 changes: 3 additions & 3 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ class Character : public Creature, public location_visitable<Character>

/** Return the position in the worn list where new_item would be
* put by default */
ItemList::iterator position_to_wear_new_item( const item &new_item );
location_vector<item>::iterator position_to_wear_new_item( const item &new_item );

/** Applies encumbrance from items only
* If new_item is not null, then calculate under the asumption that it
Expand Down Expand Up @@ -1379,15 +1379,15 @@ class Character : public Creature, public location_visitable<Character>
* @return nullopt on fail, pointer to newly worn item on success
*/
bool wear_possessed( item &to_wear, bool interactive = true,
std::optional<std::vector<item *>::iterator> position = std::nullopt );
std::optional<location_vector<item>::iterator> position = std::nullopt );
/**
* Wear a copy of specified item.
* @param to_wear Item to wear. Will be moved from if actually worn.
* @param interactive If set, won't alert the player or drain moves on completion
* @return the item if it was not worn
*/
detached_ptr<item> wear_item( detached_ptr<item> &&to_wear,
bool interactive = true, std::optional<std::vector<item *>::iterator> position = std::nullopt );
bool interactive = true, std::optional<location_vector<item>::iterator> position = std::nullopt );

/**
* Wears an item in its default location with no checks.
Expand Down
22 changes: 22 additions & 0 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,28 @@ static std::vector<std::list<item *>> restack_items( const item_stack::const_ite
return res;
}

static std::vector<std::list<item *>> restack_items( const std::vector<item *>::const_iterator
&from,
const std::vector<item *>::const_iterator &to, bool check_components = false )
{
std::vector<std::list<item *>> res;

for( auto it = from; it != to; ++it ) {
auto match = std::find_if( res.begin(), res.end(),
[ &it, check_components ]( const std::list<item *> &e ) {
return ( *it )->display_stacked_with( *const_cast<item *>( e.back() ), check_components );
} );

if( match != res.end() ) {
match->push_back( const_cast<item *>( *it ) );
} else {
res.emplace_back( 1, const_cast<item *>( *it ) );
}
}

return res;
}

const item_category *inventory_selector::naturalize_category( const item_category &category,
const tripoint &pos )
{
Expand Down
4 changes: 2 additions & 2 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const std::vector<item *> &item_contents::all_items_top() const

detached_ptr<item> item_contents::remove_top( item *it )
{
std::vector<item *>::iterator iter = std::find_if( items.begin(),
auto iter = std::find_if( items.begin(),
items.end(), [&it]( item *&against ) {
return against == it;
} );
Expand All @@ -153,7 +153,7 @@ detached_ptr<item> item_contents::remove_top( item *it )
return ret;
}

std::vector<item *>::iterator item_contents::remove_top( std::vector<item *>::iterator &it,
location_vector<item>::iterator item_contents::remove_top( location_vector<item>::iterator &it,
detached_ptr<item> *removed )
{
return items.erase( it, removed );
Expand Down
2 changes: 1 addition & 1 deletion src/item_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class item_contents

/** removes a top-level item */
detached_ptr<item> remove_top( item *it );
std::vector<item *>::iterator remove_top( std::vector<item *>::iterator &it,
location_vector<item>::iterator remove_top( location_vector<item>::iterator &it,
detached_ptr<item> *removed = nullptr );

// returns a list of pointers to all items inside recursively
Expand Down
12 changes: 6 additions & 6 deletions src/item_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class item_stack
location_vector<item> *items;

public:
using iterator = std::vector<item *>::iterator;
using const_iterator = std::vector<item *>::const_iterator;
using reverse_iterator = std::vector<item *>::reverse_iterator;
using const_reverse_iterator = std::vector<item *>::const_reverse_iterator;
using iterator = location_vector<item>::iterator;
using const_iterator = location_vector<item>::const_iterator;
using reverse_iterator = location_vector<item>::reverse_iterator;
using const_reverse_iterator = location_vector<item>::const_reverse_iterator;

item_stack( location_vector<item> *items ) : items( items ) { }
virtual ~item_stack() = default;
Expand All @@ -34,8 +34,8 @@ class item_stack
bool empty() const;
virtual void insert( detached_ptr<item> &&newitem ) = 0;
virtual iterator erase( const_iterator it, detached_ptr<item> *out = nullptr ) = 0;
virtual iterator erase( const_iterator first, const_iterator last,
std::vector<detached_ptr<item>> *out = nullptr ) = 0;
/*virtual iterator erase( const_iterator first, const_iterator last,
std::vector<detached_ptr<item>> *out = nullptr ) = 0;*/
virtual std::vector<detached_ptr<item>> clear();

void remove_top_items_with( std::function < detached_ptr<item>( detached_ptr<item> && ) > cb );
Expand Down
Loading

0 comments on commit 2b5d611

Please sign in to comment.