Skip to content

Commit

Permalink
Refactor 'Examine inventory item' menu (cataclysmbnteam#1802)
Browse files Browse the repository at this point in the history
* Remove some avatar actions out of game class

* Refactor game::inventory_item_menu

* Make throwing into an activity

* Move all rate_action_* funcs into examine_item_menu where they belong

* Tidy-clang
  • Loading branch information
olanti-p authored Sep 3, 2022
1 parent 59bdbf6 commit 3f2dd6e
Show file tree
Hide file tree
Showing 29 changed files with 1,183 additions and 870 deletions.
8 changes: 8 additions & 0 deletions data/json/player_activities.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,5 +757,13 @@
"suspendable": false,
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_THROW",
"type": "activity_type",
"verb": "throwing",
"suspendable": false,
"based_on": "neither",
"no_resume": true
}
]
140 changes: 140 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3057,5 +3057,145 @@
"id": "CHANGE_PROFESSION_NAME",
"name": "Change profession name",
"bindings": [ { "input_method": "keyboard", "key": "*" } ]
},
{
"id": "ACTIVATE",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Activate",
"bindings": [ { "input_method": "keyboard", "key": "a" } ]
},
{
"id": "READ",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Read",
"bindings": [ { "input_method": "keyboard", "key": "R" } ]
},
{
"id": "EAT",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Eat",
"bindings": [ { "input_method": "keyboard", "key": "E" } ]
},
{
"id": "WEAR",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Wear",
"bindings": [ { "input_method": "keyboard", "key": "W" } ]
},
{
"id": "WIELD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Wield",
"bindings": [ { "input_method": "keyboard", "key": "w" } ]
},
{
"id": "UNWIELD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Unwield",
"bindings": [ { "input_method": "keyboard", "key": "w" } ]
},
{
"id": "THROW",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Throw",
"bindings": [ { "input_method": "keyboard", "key": "t" } ]
},
{
"id": "CHANGE_SIDE",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Change side",
"bindings": [ { "input_method": "keyboard", "key": "c" } ]
},
{
"id": "TAKE_OFF",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Take off",
"bindings": [ { "input_method": "keyboard", "key": "T" } ]
},
{
"id": "DROP",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Drop",
"bindings": [ { "input_method": "keyboard", "key": "d" } ]
},
{
"id": "UNLOAD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Unload",
"bindings": [ { "input_method": "keyboard", "key": "u" } ]
},
{
"id": "RELOAD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Reload",
"bindings": [ { "input_method": "keyboard", "key": "r" } ]
},
{
"id": "PART_RELOAD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Part reload",
"bindings": [ { "input_method": "keyboard", "key": "p" } ]
},
{
"id": "MEND",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Mend",
"bindings": [ { "input_method": "keyboard", "key": "m" } ]
},
{
"id": "DISASSEMBLE",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Disassemble",
"bindings": [ { "input_method": "keyboard", "key": "D" } ]
},
{
"id": "FAVORITE_ADD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Favorite",
"bindings": [ { "input_method": "keyboard", "key": "f" } ]
},
{
"id": "FAVORITE_REMOVE",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Unfavorite",
"bindings": [ { "input_method": "keyboard", "key": "f" } ]
},
{
"id": "REASSIGN",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Reassign",
"bindings": [ { "input_method": "keyboard", "key": "=" } ]
},
{
"id": "AUTOPICKUP_ADD",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Autopickup (add)",
"bindings": [ { "input_method": "keyboard", "key": "+" } ]
},
{
"id": "AUTOPICKUP_REMOVE",
"type": "keybinding",
"category": "INVENTORY_ITEM",
"name": "Autopickup (remove)",
"bindings": [ { "input_method": "keyboard", "key": "-" } ]
}
]
77 changes: 76 additions & 1 deletion src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void aim_activity_actor::finish( player_activity &act, Character &who )
if( reload_requested ) {
// Reload the gun / select different arrows
// May assign ACT_RELOAD
g->reload_wielded( true );
avatar_action::reload_wielded( true );
}
return;
}
Expand Down Expand Up @@ -1029,6 +1029,80 @@ std::unique_ptr<activity_actor> stash_activity_actor::deserialize( JsonIn &jsin
return actor.clone();
}

void throw_activity_actor::do_turn( player_activity &act, Character &who )
{
// Make copies of relevant values since the class would
// not be available after act.set_to_null()
item_location target = target_loc;
cata::optional<tripoint> blind_throw_pos = blind_throw_from_pos;

// Stop the activity. Whether we will or will not throw doesn't matter.
act.set_to_null();

if( !who.is_avatar() ) {
// Sanity check
debugmsg( "ACT_THROW is not applicable for NPCs." );
return;
}

// Shift our position to our "peeking" position, so that the UI
// for picking a throw point lets us target the location we couldn't
// otherwise see.
const tripoint original_player_position = who.pos();
if( blind_throw_pos ) {
who.setpos( *blind_throw_pos );
}

target_handler::trajectory trajectory = target_handler::mode_throw( *who.as_avatar(), *target,
blind_throw_pos.has_value() );

// If we previously shifted our position, put ourselves back now that we've picked our target.
if( blind_throw_pos ) {
who.setpos( original_player_position );
}

if( trajectory.empty() ) {
return;
}

if( &*target != &who.weapon ) {
// This is to represent "implicit offhand wielding"
int extra_cost = who.item_handling_cost( *target, true, INVENTORY_HANDLING_PENALTY / 2 );
who.mod_moves( -extra_cost );
}

item thrown = *target.get_item();
if( target->count_by_charges() && target->charges > 1 ) {
target->mod_charges( -1 );
thrown.charges = 1;
} else {
target.remove_item();
}
who.as_player()->throw_item( trajectory.back(), thrown, blind_throw_pos );
}

void throw_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();

jsout.member( "target_loc", target_loc );
jsout.member( "blind_throw_from_pos", blind_throw_from_pos );

jsout.end_object();
}

std::unique_ptr<activity_actor> throw_activity_actor::deserialize( JsonIn &jsin )
{
throw_activity_actor actor;

JsonObject data = jsin.get_object();

data.read( "target_loc", actor.target_loc );
data.read( "blind_throw_from_pos", actor.blind_throw_from_pos );

return actor.clone();
}

void wash_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
Expand Down Expand Up @@ -1068,6 +1142,7 @@ deserialize_functions = {
{ activity_id( "ACT_OPEN_GATE" ), &open_gate_activity_actor::deserialize },
{ activity_id( "ACT_PICKUP" ), &pickup_activity_actor::deserialize },
{ activity_id( "ACT_STASH" ), &stash_activity_actor::deserialize },
{ activity_id( "ACT_THROW" ), &throw_activity_actor::deserialize },
{ activity_id( "ACT_WASH" ), &wash_activity_actor::deserialize },
};
} // namespace activity_actors
Expand Down
31 changes: 31 additions & 0 deletions src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,37 @@ class stash_activity_actor : public activity_actor
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class throw_activity_actor : public activity_actor
{
private:
item_location target_loc;
cata::optional<tripoint> blind_throw_from_pos;

public:
throw_activity_actor() = default;
throw_activity_actor(
item_location target_loc,
cata::optional<tripoint> blind_throw_from_pos
) : target_loc( target_loc ),
blind_throw_from_pos( blind_throw_from_pos ) {}
~throw_activity_actor() = default;

activity_id get_type() const override {
return activity_id( "ACT_THROW" );
}

void start( player_activity &, Character & ) override {};
void do_turn( player_activity &act, Character &who ) override;
void finish( player_activity &, Character & ) override {};

std::unique_ptr<activity_actor> clone() const override {
return std::make_unique<throw_activity_actor>( *this );
}

void serialize( JsonOut &jsout ) const override;
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class wash_activity_actor : public activity_actor
{
private:
Expand Down
8 changes: 6 additions & 2 deletions src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "color.h"
#include "debug.h"
#include "enums.h"
#include "examine_item_menu.h"
#include "game.h"
#include "game_constants.h"
#include "ime.h"
Expand Down Expand Up @@ -1320,8 +1321,11 @@ void advanced_inventory::action_examine( advanced_inv_listitem *sitem,
// "return to AIM".
do_return_entry();
assert( g->u.has_activity( ACT_ADV_INVENTORY ) );
ret = g->inventory_item_menu( loc, info_startx, info_width,
src == advanced_inventory::side::left ? game::LEFT_OF_INFO : game::RIGHT_OF_INFO );

examine_item_menu::run( loc, info_startx, info_width,
src == advanced_inventory::side::left ?
examine_item_menu::menu_pos_t::left :
examine_item_menu::menu_pos_t::right );
if( !g->u.has_activity( ACT_ADV_INVENTORY ) ) {
exit = true;
} else {
Expand Down
14 changes: 0 additions & 14 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,20 +939,6 @@ bool avatar::has_identified( const itype_id &item_id ) const
return items_identified.count( item_id ) > 0;
}

hint_rating avatar::rate_action_read( const item &it ) const
{
if( !it.is_book() ) {
return hint_rating::cant;
}

if( !has_identified( it.typeId() ) ) {
return hint_rating::good;
}

std::vector<std::string> dummy;
return get_book_reader( it, dummy ) == nullptr ? hint_rating::iffy : hint_rating::good;
}

void avatar::wake_up()
{
if( has_effect( effect_sleep ) ) {
Expand Down
2 changes: 0 additions & 2 deletions src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ class avatar : public player
/** Note that we've read a book at least once. **/
bool has_identified( const itype_id &item_id ) const override;

hint_rating rate_action_read( const item &it ) const;

void wake_up();
// Grab furniture / vehicle
void grab( object_type grab_type, const tripoint &grab_point = tripoint_zero );
Expand Down
Loading

0 comments on commit 3f2dd6e

Please sign in to comment.