Skip to content

Commit

Permalink
Merge pull request #36103 from KorGgenT/change-side
Browse files Browse the repository at this point in the history
refactor change_side()
  • Loading branch information
kevingranade authored Dec 22, 2019
2 parents 78e1ce6 + 1de9623 commit 4b92e43
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 75 deletions.
51 changes: 51 additions & 0 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,57 @@ int Character::extraEncumbrance( const layer_level level, const int bp ) const
return encumbrance_cache[bp].layer_penalty_details[static_cast<int>( level )].total;
}

hint_rating Character::rate_action_change_side( const item &it ) const
{
if( !is_worn( it ) ) {
return HINT_IFFY;
}

if( !it.is_sided() ) {
return HINT_CANT;
}

return HINT_GOOD;
}

bool Character::change_side( item &it, bool interactive )
{
if( !it.swap_side() ) {
if( interactive ) {
add_msg_player_or_npc( m_info,
_( "You cannot swap the side on which your %s is worn." ),
_( "<npcname> cannot swap the side on which their %s is worn." ),
it.tname() );
}
return false;
}

if( interactive ) {
add_msg_player_or_npc( m_info, _( "You swap the side on which your %s is worn." ),
_( "<npcname> swaps the side on which their %s is worn." ),
it.tname() );
}

mod_moves( -250 );
reset_encumbrance();

return true;
}

bool Character::change_side( item_location &loc, bool interactive )
{
if( !loc || !is_worn( *loc ) ) {
if( interactive ) {
add_msg_player_or_npc( m_info,
_( "You are not wearing that item." ),
_( "<npcname> isn't wearing that item." ) );
}
return false;
}

return change_side( *loc, interactive );
}

static void layer_item( std::array<encumbrance_data, num_bp> &vals,
const item &it,
std::array<layer_level, num_bp> &highest_layer_so_far,
Expand Down
9 changes: 9 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,15 @@ class Character : public Creature, public visitable<Character>
double footwear_factor() const;
/** Returns true if the player is wearing something on their feet that is not SKINTIGHT */
bool is_wearing_shoes( const side &which_side = side::BOTH ) const;

/** Swap side on which item is worn; returns false on fail. If interactive is false, don't alert player or drain moves */
bool change_side( item &it, bool interactive = true );
bool change_side( item_location &loc, bool interactive = true );

/** Used to determine player feedback on item use for the inventory code.
* rates usability lower for non-tools (books, etc.) */
hint_rating rate_action_change_side( const item &it ) const;

bool get_check_encumbrance() {
return check_encumbrance;
}
Expand Down
18 changes: 1 addition & 17 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ int game::inventory_item_menu( int pos, int iStartX, int iWidth,
avatar_action::plthrow( u, locThisItem );
break;
case 'c':
change_side( pos );
u.change_side( locThisItem );
break;
case 'T':
u.takeoff( oThisItem );
Expand Down Expand Up @@ -8338,22 +8338,6 @@ void game::butcher()
}
}

void game::change_side( int pos )
{
if( pos == INT_MIN ) {
auto filter = [&]( const item & it ) {
return u.is_worn( it ) && it.is_sided();
};
pos = u.get_item_position( game_menus::inv::titled_filter_menu( filter, u,
_( "Change side for item" ), _( "You don't have sided items worn." ) ).get_item() );
}
if( pos == INT_MIN ) {
add_msg( _( "Never mind." ) );
return;
}
u.change_side( pos );
}

void game::reload( item_location &loc, bool prompt, bool empty )
{
item *it = loc.get_item();
Expand Down
1 change: 0 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,6 @@ class game

void butcher(); // Butcher a corpse 'B'

void change_side( int pos = INT_MIN ); // Change the side on which an item is worn 'c'
void reload( item_location &loc, bool prompt = false, bool empty = true );
public:
void reload_item(); // Reload an item
Expand Down
53 changes: 0 additions & 53 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4296,19 +4296,6 @@ hint_rating player::rate_action_wear( const item &it ) const
return can_wear( it ).success() ? HINT_GOOD : HINT_IFFY;
}

hint_rating player::rate_action_change_side( const item &it ) const
{
if( !is_worn( it ) ) {
return HINT_IFFY;
}

if( !it.is_sided() ) {
return HINT_CANT;
}

return HINT_GOOD;
}

bool player::can_reload( const item &it, const itype_id &ammo ) const
{
if( !it.is_reloadable_with( ammo ) ) {
Expand Down Expand Up @@ -4773,46 +4760,6 @@ player::wear_item( const item &to_wear, bool interactive )
return new_item_it;
}

bool player::change_side( item &it, bool interactive )
{
if( !it.swap_side() ) {
if( interactive ) {
add_msg_player_or_npc( m_info,
_( "You cannot swap the side on which your %s is worn." ),
_( "<npcname> cannot swap the side on which their %s is worn." ),
it.tname() );
}
return false;
}

if( interactive ) {
add_msg_player_or_npc( m_info, _( "You swap the side on which your %s is worn." ),
_( "<npcname> swaps the side on which their %s is worn." ),
it.tname() );
}

mod_moves( -250 );
reset_encumbrance();

return true;
}

bool player::change_side( int pos, bool interactive )
{
item &it( i_at( pos ) );

if( !is_worn( it ) ) {
if( interactive ) {
add_msg_player_or_npc( m_info,
_( "You are not wearing that item." ),
_( "<npcname> isn't wearing that item." ) );
}
return false;
}

return change_side( it, interactive );
}

hint_rating player::rate_action_takeoff( const item &it ) const
{
if( !it.is_armor() ) {
Expand Down
4 changes: 0 additions & 4 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,6 @@ class player : public Character
*/
cata::optional<std::list<item>::iterator>
wear_item( const item &to_wear, bool interactive = true );
/** Swap side on which item is worn; returns false on fail. If interactive is false, don't alert player or drain moves */
bool change_side( item &it, bool interactive = true );
bool change_side( int pos, bool interactive = true );

/** Returns all items that must be taken off before taking off this item */
std::list<const item *> get_dependent_worn_items( const item &it ) const;
Expand Down Expand Up @@ -881,7 +878,6 @@ class player : public Character
* rates usability lower for non-tools (books, etc.) */
hint_rating rate_action_use( const item &it ) const;
hint_rating rate_action_wear( const item &it ) const;
hint_rating rate_action_change_side( const item &it ) const;
hint_rating rate_action_eat( const item &it ) const;
hint_rating rate_action_takeoff( const item &it ) const;
hint_rating rate_action_reload( const item &it ) const;
Expand Down

0 comments on commit 4b92e43

Please sign in to comment.