Skip to content

Commit

Permalink
rewrite V menu
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Oct 8, 2024
1 parent b65f3e6 commit 29b141c
Show file tree
Hide file tree
Showing 12 changed files with 1,628 additions and 25 deletions.
4 changes: 2 additions & 2 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@
},
{
"type": "keybinding",
"name": "List all items around the player",
"name": "List nearby items, monsters, terrain and furniture",
"category": "DEFAULTMODE",
"id": "listitems",
"bindings": [ { "input_method": "keyboard_char", "key": "V" }, { "input_method": "keyboard_code", "key": "v", "mod": [ "shift" ] } ]
Expand Down Expand Up @@ -3344,7 +3344,7 @@
"type": "keybinding",
"id": "LIST_ITEMS",
"category": "LOOK",
"name": "List items and monsters",
"name": "List nearby items, monsters, terrain and furniture",
"bindings": [ { "input_method": "keyboard_char", "key": "V" }, { "input_method": "keyboard_code", "key": "v", "mod": [ "shift" ] } ]
},
{
Expand Down
39 changes: 24 additions & 15 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
#include "stats_tracker.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "surroundings_menu.h"
#include "talker.h"
#include "text_snippets.h"
#include "tileray.h"
Expand Down Expand Up @@ -7617,7 +7618,7 @@ look_around_result game::look_around(
blink = !blink;
}
if( action == "LIST_ITEMS" ) {
list_items_monsters();
list_surroundings();
} else if( action == "TOGGLE_FAST_SCROLL" ) {
fast_scroll = !fast_scroll;
} else if( action == "map" ) {
Expand Down Expand Up @@ -8101,7 +8102,7 @@ void game::reset_item_list_state( const catacurses::window &window, int height,
}
}

void game::list_items_monsters()
void game::list_surroundings()
{
// Search whole reality bubble because each function internally verifies
// the visibility of the items / monsters in question.
Expand Down Expand Up @@ -8136,19 +8137,27 @@ void game::list_items_monsters()
}

temp_exit_fullscreen();
game::vmenu_ret ret;
while( true ) {
ret = uistate.vmenu_show_items ? list_items( items ) : list_monsters( mons );
if( ret == game::vmenu_ret::CHANGE_TAB ) {
uistate.vmenu_show_items = !uistate.vmenu_show_items;
} else {
break;
}
}

if( ret == game::vmenu_ret::FIRE ) {
avatar_action::fire_wielded_weapon( u );
}
std::optional<tripoint> path_start = u.pos();
std::optional<tripoint> path_end = std::nullopt;
surroundings_menu vmenu( u, m, path_end, 55 );
shared_ptr_fast<draw_callback_t> trail_cb = create_trail_callback( path_start, path_end, true );
add_draw_callback( trail_cb );
vmenu.execute();
//} else {
// game::vmenu_ret ret;
// while( true ) {
// ret = uistate.vmenu_show_items ? list_items( items ) : list_monsters( mons );
// if( ret == game::vmenu_ret::CHANGE_TAB ) {
// uistate.vmenu_show_items = !uistate.vmenu_show_items;
// } else {
// break;
// }
// }

// if( ret == game::vmenu_ret::FIRE ) {
// avatar_action::fire_wielded_weapon( u );
// }
//}
reenter_fullscreen();
}

Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ class game
const vproto_id &id, const point_abs_omt &origin, int min_distance,
int max_distance, const std::vector<std::string> &omt_search_types = {} );
// V Menu Functions and helpers:
void list_items_monsters(); // Called when you invoke the `V`-menu
void list_surroundings(); // Called when you invoke the `V`-menu

enum class vmenu_ret : int {
CHANGE_TAB,
Expand Down
2 changes: 1 addition & 1 deletion src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2451,7 +2451,7 @@ bool game::do_regular_action( action_id &act, avatar &player_character,
break;

case ACTION_LIST_ITEMS:
list_items_monsters();
list_surroundings();
break;

case ACTION_ZONES:
Expand Down
252 changes: 252 additions & 0 deletions src/map_entity_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
#include "map_entity_stack.h"

#include "creature.h"
#include "game.h"
#include "item.h"
#include "item_category.h"
#include "item_search.h"
#include "mapdata.h"

static const trait_id trait_INATTENTIVE( "INATTENTIVE" );

template<typename T>
const T *map_entity_stack<T>::get_selected_entity() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].entity;
}
return nullptr;
}

template<typename T>
std::optional<tripoint_rel_ms> map_entity_stack<T>::get_selected_pos() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].pos;
}
return std::nullopt;
}

template<typename T>
int map_entity_stack<T>::get_selected_count() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].count;
}
return 0;
}

template<typename T>
void map_entity_stack<T>::select_next()
{
if( entities.empty() ) {
return;
}

selected_index++;

if( selected_index >= static_cast<int>( entities.size() ) ) {
selected_index = 0;
}
}

template<typename T>
void map_entity_stack<T>::select_prev()
{
if( entities.empty() ) {
return;
}

if( selected_index <= 0 ) {
selected_index = entities.size();
}

selected_index--;
}

template<typename T>
int map_entity_stack<T>::get_selected_index() const
{
return selected_index;
}

template<typename T>
map_entity_stack<T>::map_entity_stack() : totalcount( 0 )
{
entities.emplace_back();
}

template<typename T>
map_entity_stack<T>::map_entity_stack( const T *const entity, const tripoint_rel_ms &pos,
const int count ) : totalcount( count )
{
entities.emplace_back( pos, 1, entity );
}

template<typename T>
void map_entity_stack<T>::add_at_pos( const T *const entity, const tripoint_rel_ms &pos,
const int count )
{
if( entities.empty() || entities.back().pos != pos ) {
entities.emplace_back( pos, 1, entity );
} else {
entities.back().count++;
}

totalcount += count;
}

template<typename T>
const std::string map_entity_stack<T>::get_category() const
{
return std::string();
}

template<>
const std::string map_entity_stack<item>::get_category() const
{
const item *it = get_selected_entity();
if( it ) {
return it->get_category_of_contents().name_header();
}

return std::string();
}

template<>
const std::string map_entity_stack<Creature>::get_category() const
{
const Creature *mon = get_selected_entity();
if( mon ) {
const Character &you = get_player_character();
if( you.has_trait( trait_INATTENTIVE ) ) {
return _( "Unknown" );
}
return Creature::get_attitude_ui_data( mon->attitude_to( you ) ).first.translated();
}

return std::string();
}

template<>
const std::string map_entity_stack<map_data_common_t>::get_category() const
{
const map_data_common_t *tf = get_selected_entity();
if( tf ) {
if( tf->is_terrain() ) {
return "TERRAIN";
} else {
return "FURNITURE";
}
}

return std::string();
}

template<typename T>
bool map_entity_stack<T>::compare( const map_entity_stack<T> &, bool ) const
{
return false;
}

template<>
bool map_entity_stack<item>::compare( const map_entity_stack<item> &rhs, bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

const item_category &lhs_cat = get_selected_entity()->get_category_of_contents();
const item_category &rhs_cat = rhs.get_selected_entity()->get_category_of_contents();

if( lhs_cat == rhs_cat ) {
return compare_dist;
}

return lhs_cat < rhs_cat;
}

template<>
bool map_entity_stack<Creature>::compare( const map_entity_stack<Creature> &rhs,
bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

// maybe pass you as a parameter to avoid including game.h?
Character &you = get_player_character();
const Creature::Attitude att_lhs = get_selected_entity()->attitude_to( you );
const Creature::Attitude att_rhs = rhs.get_selected_entity()->attitude_to( you );

if( att_lhs == att_rhs ) {
return compare_dist;
}

return att_lhs < att_rhs;
}

template<>
bool map_entity_stack<map_data_common_t>::compare( const map_entity_stack<map_data_common_t> &rhs,
bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

const bool &lhs_cat = get_selected_entity()->is_terrain();
const bool &rhs_cat = rhs.get_selected_entity()->is_terrain();

if( lhs_cat == rhs_cat ) {
return compare_dist;
}

return rhs_cat;
}

//returns the first non priority items.
template<typename T>
int list_filter_high_priority( std::vector<map_entity_stack<T>> &, const std::string & )
{
}

template<typename T>
int list_filter_low_priority( std::vector<map_entity_stack<T>> &stack, const int start,
const std::string &priorities )
{
// todo: actually use it and specialization (only used for items)
// TODO:optimize if necessary
std::vector<map_entity_stack<T>> tempstack;
const auto filter_fn = item_filter_from_string( priorities );
for( auto it = stack.begin() + start; it != stack.end(); ) {
if( !priorities.empty() && it->example != nullptr && filter_fn( *it->example ) ) {
tempstack.push_back( *it );
it = stack.erase( it );
} else {
it++;
}
}

int id = stack.size();
for( map_item_stack &elem : tempstack ) {
stack.push_back( elem );
}
return id;
}

// explicit template instantiation
template class map_entity_stack<item>;
template class map_entity_stack<Creature>;
template class map_entity_stack<map_data_common_t>;
Loading

0 comments on commit 29b141c

Please sign in to comment.