Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix consume issues #40660

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,42 +588,64 @@ std::unique_ptr<activity_actor> open_gate_activity_actor::deserialize( JsonIn &j

void consume_activity_actor::start( player_activity &act, Character &guy )
{
if( !loc ) {
debugmsg( "Item location to be consumed should not be null." );
int moves;
if( consume_location ) {
const auto ret = g->u.will_eat( *consume_location, true );
if( !ret.success() ) {
open_consume_menu = false;
return;
} else {
force = true;
}
moves = to_moves<int>( guy.get_consume_time( *consume_location ) );
} else if( !consume_item.is_null() ) {
const auto ret = g->u.will_eat( consume_item, true );
if( !ret.success() ) {
open_consume_menu = false;
return;
} else {
force = true;
}
moves = to_moves<int>( guy.get_consume_time( consume_item ) );
} else {
debugmsg( "Item/location to be consumed should not be null." );
return;
}
int moves = to_moves<int>( guy.get_consume_time( *loc ) );

act.moves_total = moves;
act.moves_left = moves;
}

void consume_activity_actor::finish( player_activity &act, Character & )
{
if( !loc ) {
debugmsg( "Item location to be consumed should not be null." );
act.set_to_null();
return;
}
if( loc.where() == item_location::type::character ) {
g->u.consume( loc );
} else if( g->u.consume( *loc ) ) {
loc.remove_item();
}
if( g->u.get_value( "THIEF_MODE_KEEP" ) != "YES" ) {
g->u.set_value( "THIEF_MODE", "THIEF_ASK" );
if( consume_location ) {
if( consume_location.where() == item_location::type::character ) {
g->u.consume( consume_location, force );
} else if( g->u.consume( *consume_location, force ) ) {
consume_location.remove_item();
}
if( g->u.get_value( "THIEF_MODE_KEEP" ) != "YES" ) {
g->u.set_value( "THIEF_MODE", "THIEF_ASK" );
}
} else if( !consume_item.is_null() ) {
g->u.consume( consume_item, force );
} else {
debugmsg( "Item location/name to be consumed should not be null." );
}
act.set_to_null();
if( open_consume_menu ) {
avatar_action::eat( g->u );
}
act.set_to_null();
}

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

jsout.member( "loc", loc );
jsout.member( "consume_location", consume_location );
jsout.member( "consume_item", consume_item );
jsout.member( "open_consume_menu", open_consume_menu );
jsout.member( "force", force );

jsout.end_object();
}
Expand All @@ -635,7 +657,10 @@ std::unique_ptr<activity_actor> consume_activity_actor::deserialize( JsonIn &jsi

JsonObject data = jsin.get_object();

data.read( "loc", actor.loc );
data.read( "consume_location", actor.consume_location );
data.read( "consume_item", actor.consume_item );
data.read( "open_consume_menu", actor.open_consume_menu );
data.read( "force", actor.force );

return actor.clone();
}
Expand Down
19 changes: 13 additions & 6 deletions src/activity_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "clone_ptr.h"
#include "item_location.h"
#include "item.h"
#include "point.h"
#include "type_id.h"

Expand Down Expand Up @@ -361,19 +362,25 @@ class open_gate_activity_actor : public activity_actor
class consume_activity_actor : public activity_actor
{
private:
item_location loc;
bool open_consume_menu;

item_location consume_location;
item consume_item;
bool open_consume_menu = false;
bool force = false;
/**
* @pre @p other is a consume_activity_actor
*/
bool can_resume_with_internal( const activity_actor &other, const Character & ) const override {
const consume_activity_actor &c_actor = static_cast<const consume_activity_actor &>( other );
return loc == c_actor.loc;
return ( consume_location == c_actor.consume_location &&
open_consume_menu == c_actor.open_consume_menu &&
force == c_actor.force && &consume_item == &c_actor.consume_item );
}
public:
consume_activity_actor( const item_location &loc, bool open_consume_menu = false ) :
loc( loc ), open_consume_menu( open_consume_menu ) {}
consume_activity_actor( const item_location &consume_location, bool open_consume_menu = false ) :
consume_location( consume_location ), open_consume_menu( open_consume_menu ) {}

consume_activity_actor( item consume_item, bool open_consume_menu = false ) :
consume_item( consume_item ), open_consume_menu( open_consume_menu ) {}

activity_id get_type() const override {
return activity_id( "ACT_CONSUME" );
Expand Down
8 changes: 2 additions & 6 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,10 @@ bool avatar_action::eat_here( avatar &you )
add_msg( _( "You're too full to eat the leaves from the %s." ), g->m.ter( you.pos() )->name() );
return true;
} else {
you.moves -= 400;
g->m.ter_set( you.pos(), t_grass );
add_msg( _( "You eat the underbrush." ) );
item food( "underbrush", calendar::turn, 1 );
you.consume( food );
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
you.assign_activity( player_activity( consume_activity_actor( food, false ) ) );
return true;
}
}
Expand All @@ -1039,11 +1037,9 @@ bool avatar_action::eat_here( avatar &you )
add_msg( _( "You're too full to graze." ) );
return true;
} else {
you.moves -= 400;
add_msg( _( "You eat the grass." ) );
item food( item( "grass", calendar::turn, 1 ) );
you.consume( food );
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
you.assign_activity( player_activity( consume_activity_actor( food, false ) ) );
if( g->m.ter( you.pos() ) == t_grass_tall ) {
g->m.ter_set( you.pos(), t_grass_long );
} else if( g->m.ter( you.pos() ) == t_grass_long ) {
Expand Down
8 changes: 4 additions & 4 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ static bool consume_med( item &target, player &you )
return target.charges <= 0;
}

bool player::consume( item &target )
bool player::consume( item &target, bool force )
{
if( target.is_null() ) {
add_msg_if_player( m_info, _( "You do not have that item." ) );
Expand All @@ -1791,7 +1791,7 @@ bool player::consume( item &target )
return false;
}
if( consume_med( comest, *this ) ||
eat( comest, *this ) || feed_reactor_with( comest ) || feed_furnace_with( comest ) ||
eat( comest, *this, force ) || feed_reactor_with( comest ) || feed_furnace_with( comest ) ||
fuel_bionic_with( comest ) ) {

if( &target != &comest ) {
Expand All @@ -1804,7 +1804,7 @@ bool player::consume( item &target )
return false;
}

bool player::consume( item_location loc )
bool player::consume( item_location loc, bool force )
{
if( !loc ) {
debugmsg( "Null loc to consume." );
Expand All @@ -1815,7 +1815,7 @@ bool player::consume( item_location loc )
bool worn = is_worn( target );
const bool inv_item = !( wielding || worn );

if( consume( target ) ) {
if( consume( target, force ) ) {

i_rem( loc.get_item() );

Expand Down
4 changes: 3 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,9 @@ void game::process_activity()
return;
}

if( calendar::once_every( 5_minutes ) ) {
if( calendar::once_every( 5_minutes )
&& u.activity.moves_total > to_moves<int>
( 5_minutes ) ) {//This is a hack to prevent an issue with the consume menu popping up again when this fires, since eating is not at present ever 5 minutes long this works
ui_manager::redraw();
refresh_display();
}
Expand Down
3 changes: 1 addition & 2 deletions src/handle_liquid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ static bool perform_liquid_transfer( item &liquid, const tripoint *const source_

switch( target.dest_opt ) {
case LD_CONSUME:
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
g->u.consume( liquid );
g->u.assign_activity( player_activity( consume_activity_actor( liquid, false ) ) );
transfer_ok = true;
break;
case LD_ITEM: {
Expand Down
13 changes: 4 additions & 9 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1611,11 +1611,9 @@ static bool can_drink_nectar( const player &p )
static bool drink_nectar( player &p )
{
if( can_drink_nectar( p ) ) {
p.moves -= to_moves<int>( 30_seconds );
add_msg( _( "You drink some nectar." ) );
item nectar( "nectar", calendar::turn, 1 );
p.consume( nectar );
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
p.assign_activity( player_activity( consume_activity_actor( nectar, false ) ) );
return true;
}

Expand Down Expand Up @@ -1656,11 +1654,9 @@ void iexamine::flower_poppy( player &p, const tripoint &examp )
g->m.furnname( examp ) ) ) {
return;
}
p.moves -= to_moves<int>( 30_seconds ); // You take your time...
add_msg( _( "You slowly suck up the nectar." ) );
item poppy( "poppy_nectar", calendar::turn, 1 );
p.consume( poppy );
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
p.assign_activity( player_activity( consume_activity_actor( poppy, false ) ) );
p.mod_fatigue( 20 );
p.add_effect( effect_pkill2, 7_minutes );
// Please drink poppy nectar responsibly.
Expand Down Expand Up @@ -3135,11 +3131,10 @@ void iexamine::keg( player &p, const tripoint &examp )
return;

case HAVE_A_DRINK:
if( !p.consume( drink ) ) {
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
if( !p.can_consume( drink ) ) {
return; // They didn't actually drink
}

p.assign_activity( player_activity( consume_activity_actor( drink, false ) ) );
if( drink.charges == 0 ) {
add_msg( _( "You squeeze the last drops of %1$s from the %2$s." ),
drink_tname, keg_name );
Expand Down
8 changes: 5 additions & 3 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6154,13 +6154,15 @@ bool item::is_comestible() const

bool item::is_food() const
{
return is_comestible() && ( get_comestible()->comesttype == "FOOD" ||
get_comestible()->comesttype == "DRINK" );
const std::string comest_type = get_comestible() ? get_comestible()->comesttype : "";
return is_comestible() && ( comest_type == "FOOD" ||
comest_type == "DRINK" );
}

bool item::is_medication() const
{
return is_comestible() && get_comestible()->comesttype == "MED";
const std::string comest_type = get_comestible() ? get_comestible()->comesttype : "";
return is_comestible() && comest_type == "MED";
}

bool item::is_brewable() const
Expand Down
4 changes: 2 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ class player : public Character
void siphon( vehicle &veh, const itype_id &desired_liquid );

/** Used for eating object at pos, returns true if object is removed from inventory (last charge was consumed) */
bool consume( item_location loc );
bool consume( item_location loc, bool force = false );
/** Used for eating a particular item that doesn't need to be in inventory.
* Returns true if the item is to be removed (doesn't remove). */
bool consume( item &target );
bool consume( item &target, bool force = false );

/** Handles the enjoyability value for a book. **/
int book_fun_for( const item &book, const player &p ) const;
Expand Down
4 changes: 1 addition & 3 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,10 +2072,8 @@ void vehicle::interact_with( const tripoint &pos, int interact_part )
case DRINK: {
item water( "water_clean", 0 );
if( g->u.can_consume( water ) ) {
//I tried to convert this the to use the consume activity but couldn't get the transformation of this item into an item location quite right
g->u.consume( water );
g->u.assign_activity( player_activity( consume_activity_actor( water, false ) ) );
drain( "water_clean", 1 );
g->u.moves -= 250;
}
return;
}
Expand Down