Skip to content

Commit

Permalink
Merge pull request #37597 from ifreund/better-open-close
Browse files Browse the repository at this point in the history
Improve directional highlight prompts
  • Loading branch information
ZhilkinSerg authored Feb 3, 2020
2 parents 0f8c9e1 + 3d68f8b commit aecfa31
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 55 deletions.
14 changes: 9 additions & 5 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mapdata.h"
#include "messages.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "path_info.h"
#include "translations.h"
Expand Down Expand Up @@ -1054,17 +1055,17 @@ cata::optional<tripoint> choose_adjacent( const std::string &message, const bool
}

cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const action_id action, const bool allow_vertical )
const std::string &failure_message, const action_id action, bool allow_vertical )
{
const std::function<bool( const tripoint & )> f = [&action]( const tripoint & p ) {
return can_interact_at( action, p );
};
return choose_adjacent_highlight( message, f, allow_vertical );
return choose_adjacent_highlight( message, failure_message, f, allow_vertical );
}

cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::function<bool ( const tripoint & )> &allowed,
const bool allow_vertical, const bool auto_select_if_single )
const std::string &failure_message, const std::function<bool ( const tripoint & )> &allowed,
const bool allow_vertical )
{
// Highlight nearby terrain according to the highlight function
if( allowed != nullptr ) {
Expand All @@ -1084,8 +1085,11 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
}
if( highlighted ) {
wrefresh( g->w_terrain );
} else {
add_msg( failure_message );
return cata::nullopt;
}
if( auto_select_if_single && single ) {
if( get_option<bool>( "AUTOSELECT_SINGLE_VALID_TARGET" ) && single ) {
return single;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,12 @@ cata::optional<tripoint> choose_direction( const std::string &message,
* the player to indicate valid squares for a given @ref action_id
*
* @param[in] message Message used in assembling the prompt to the player
* @param[in] failure_message Message used if there is no vaild adjacent tile
* @param[in] action An action ID to drive the highlighting output
* @param[in] allow_vertical Allows direction vector to have vertical component if true
*/
cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
action_id action, bool allow_vertical = false );
const std::string &failure_message, action_id action, bool allow_vertical = false );

/**
* Request player input of adjacent tile with highlighting, possibly on different z-level
Expand All @@ -477,13 +478,13 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
* function.
*
* @param[in] message Message used in assembling the prompt to the player
* @param[in] failure_message Message used if there is no vaild adjacent tile
* @param[in] allowed A function that will be called to determine if a given location is allowed for selection
* @param[in] allow_vertical Allows direction vector to have vertical component if true
*/
cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
const std::function<bool( const tripoint & )> &allowed,
bool allow_vertical = false,
bool auto_select_if_single = false );
const std::string &failure_message, const std::function<bool( const tripoint & )> &allowed,
bool allow_vertical = false );

// (Press X (or Y)|Try) to Z
std::string press_x( action_id act );
Expand Down
29 changes: 4 additions & 25 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5317,7 +5317,8 @@ void game::examine()
}

const cata::optional<tripoint> examp_ = choose_adjacent_highlight( _( "Examine where?" ),
ACTION_EXAMINE );
_( "There is nothing that can be examined nearby." ),
ACTION_EXAMINE, false );
if( !examp_ ) {
return;
}
Expand Down Expand Up @@ -5521,31 +5522,9 @@ void game::examine( const tripoint &examp )

void game::pickup()
{
// First check if there is no/only one option for pickup
int num_tiles_with_items = 0;
tripoint tile_with_items = u.pos();
for( const tripoint &p : m.points_in_radius( u.pos(), 1 ) ) {
bool veh_has_items = false;
const optional_vpart_position vp = m.veh_at( p );
if( vp ) {
const int cargo_part = vp->vehicle().part_with_feature( vp->part_index(), "CARGO", false );
veh_has_items = cargo_part >= 0 && !vp->vehicle().get_items( cargo_part ).empty();
}
if( m.has_items( p ) || veh_has_items ) {
++num_tiles_with_items;
tile_with_items = p;
}
}
if( num_tiles_with_items == 0 ) {
add_msg( _( "There's nothing to pick up there" ) );
return;
} else if( num_tiles_with_items == 1 ) {
pickup( tile_with_items );
return;
}

const cata::optional<tripoint> examp_ = choose_adjacent_highlight( _( "Pickup where?" ),
ACTION_PICKUP );
_( "There is nothing to pick up nearby." ),
ACTION_PICKUP, false );
if( !examp_ ) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ static void open()
map &m = g->m;

const cata::optional<tripoint> openp_ = choose_adjacent_highlight( _( "Open where?" ),
ACTION_OPEN );
pgettext( "no door, gate, curtain, etc.", "There is nothing that can be opened nearby." ),
ACTION_OPEN, false );

if( !openp_ ) {
return;
}
Expand Down Expand Up @@ -517,7 +519,8 @@ static void open()
static void close()
{
if( const cata::optional<tripoint> pnt = choose_adjacent_highlight( _( "Close where?" ),
ACTION_CLOSE ) ) {
pgettext( "no door, gate, etc.", "There is nothing that can be closed nearby." ),
ACTION_CLOSE, false ) ) {
doors::close_door( g->m, g->u, *pnt );
}
}
Expand Down
35 changes: 18 additions & 17 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ int iuse::hammer( player *p, item *it, bool, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Pry where?" ), f, false, true );
_( "Pry where?" ), _( "There is nothing to pry nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -2387,9 +2387,8 @@ int iuse::crowbar( player *p, item *it, bool, const tripoint &pos )
return is_allowed;
};

const cata::optional<tripoint> pnt_ = ( pos != p->pos() ) ? pos :
choose_adjacent_highlight(
_( "Pry where?" ), f, false, true );
const cata::optional<tripoint> pnt_ = ( pos != p->pos() ) ? pos : choose_adjacent_highlight(
_( "Pry where?" ), _( "There is nothing to pry nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -2738,7 +2737,8 @@ int iuse::dig( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Deposit excavated materials where?" ), f, false );
_( "Deposit excavated materials where?" ),
_( "There is nowhere to deposit the excavated materials." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -2825,7 +2825,8 @@ int iuse::dig_channel( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Deposit excavated materials where?" ), f, false );
_( "Deposit excavated materials where?" ),
_( "There is nowhere to deposit the excavated materials." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -2885,7 +2886,7 @@ int iuse::fill_pit( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Fill which pit or mound?" ), f, false, true );
_( "Fill which pit or mound?" ), _( "There is no pit or mound to fill nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -2942,7 +2943,7 @@ int iuse::clear_rubble( player *p, item *it, bool, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Clear rubble where?" ), f, false, true );
_( "Clear rubble where?" ), _( "There is no rubble to clear nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -3000,7 +3001,7 @@ int iuse::siphon( player *p, item *it, bool, const tripoint & )
}
if( found_more_than_one ) {
cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Siphon from where?" ), f, false, true );
_( "Siphon from where?" ), _( "There is nothing to siphon nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -3426,8 +3427,8 @@ int iuse::geiger( player *p, item *it, bool t, const tripoint &pos )
return g->critter_at<npc>( pnt ) != nullptr || g->critter_at<player>( pnt ) != nullptr;
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight( _( "Scan whom?" ), f, false,
true );
const cata::optional<tripoint> pnt_ = choose_adjacent_highlight( _( "Scan whom?" ),
_( "There is no one to scan nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -4770,7 +4771,7 @@ int iuse::chop_tree( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Chop down which tree?" ), f, false, true );
_( "Chop down which tree?" ), _( "There is no tree to chop down nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -4816,7 +4817,7 @@ int iuse::chop_logs( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Chop which tree trunk?" ), f, false, true );
_( "Chop which tree trunk?" ), _( "There is no tree trunk to chop nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -4889,7 +4890,7 @@ int iuse::oxytorch( player *p, item *it, bool, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Cut up metal where?" ), f, false, true );
_( "Cut up metal where?" ), _( "There is no metal to cut up nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -4979,7 +4980,7 @@ int iuse::hacksaw( player *p, item *it, bool t, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Cut up metal where?" ), f, false, true );
_( "Cut up metal where?" ), _( "There is no metal to cut up nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -5036,7 +5037,7 @@ int iuse::boltcutters( player *p, item *it, bool, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Cut up metal where?" ), f, false, true );
_( "Cut up metal where?" ), _( "There is no metal to cut up nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down Expand Up @@ -5125,7 +5126,7 @@ int iuse::mop( player *p, item *it, bool, const tripoint & )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Mop where?" ), f, false, true );
_( "Mop where?" ), _( "There is nothing to mop nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ int pick_lock_actor::use( player &p, item &it, bool, const tripoint & ) const
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Use your lockpick where?" ), f, false, true );
_( "Use your lockpick where?" ), _( "There is nothing to lockpick nearby." ), f, false );
if( !pnt_ ) {
return 0;
}
Expand Down
7 changes: 7 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,13 @@ void options_manager::add_options_interface()
true
);

add( "AUTOSELECT_SINGLE_VALID_TARGET", "interface",
translate_marker( "Autoselect if exactly one valid target" ),
translate_marker( "If true, directional actions ( like \"Examine\", \"Open\", \"Pickup\" ) "
"will autoselect an adjacent tile if there is exactly one valid target." ),
true
);

add_empty_line();

add( "DIAG_MOVE_WITH_MODIFIERS_MODE", "interface",
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,8 @@ void vehicle::use_harness( int part, const tripoint &pos )
};

const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Where is the creature to harness?" ), f, false, true );
_( "Where is the creature to harness?" ), _( "There is no creature to harness nearby." ), f,
false );
if( !pnt_ ) {
add_msg( m_info, _( "Never mind." ) );
return;
Expand Down

0 comments on commit aecfa31

Please sign in to comment.