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

Make consuming take time #40160

Merged
merged 1 commit into from
May 5, 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
7 changes: 7 additions & 0 deletions data/json/player_activities.json
Original file line number Diff line number Diff line change
Expand Up @@ -864,5 +864,12 @@
"suspendable": false,
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_CONSUME",
"type": "activity_type",
"activity_level": "NO_EXERCISE",
"verb": "consuming",
"based_on": "time"
}
]
60 changes: 60 additions & 0 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "npc.h"
#include "output.h"
#include "pickup.h"
#include "player.h"
#include "player_activity.h"
#include "point.h"
#include "timed_event.h"
Expand All @@ -33,6 +34,8 @@ static const skill_id skill_computer( "computer" );

static const trait_id trait_ILLITERATE( "ILLITERATE" );

static const std::string flag_USE_EAT_VERB( "USE_EAT_VERB" );

void hacking_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = to_moves<int>( 5_minutes );
Expand Down Expand Up @@ -409,12 +412,69 @@ std::unique_ptr<activity_actor> open_gate_activity_actor::deserialize( JsonIn &j
return actor.clone();
}

void consume_activity_actor::start( player_activity &act, Character & )
{
const int charges = std::max( loc->charges, 1 );
int volume = units::to_milliliter( loc->volume() ) / charges;
time_duration time = 0_seconds;
const bool eat_verb = loc->has_flag( flag_USE_EAT_VERB );
if( eat_verb || loc->get_comestible()->comesttype == "FOOD" ) {
time = time_duration::from_seconds( volume / 5 ); //Eat 5 mL (1 teaspoon) per second
} else if( !eat_verb && loc->get_comestible()->comesttype == "DRINK" ) {
time = time_duration::from_seconds( volume / 15 ); //Drink 15 mL (1 tablespoon) per second
} else if( loc->is_medication() ) {
time = time_duration::from_seconds(
30 ); //Medicine/drugs takes 30 seconds this is pretty arbitrary and should probable be broken up more but seems ok for a start
} else {
debugmsg( "Consumed something that was not food, drink or medicine/drugs" );
}

act.moves_total = to_moves<int>( time );
act.moves_left = to_moves<int>( time );
}

void consume_activity_actor::finish( player_activity &act, Character & )
{
if( loc.where() == item_location::type::character ) {
g->u.consume( loc );

} else if( g->u.consume_item( *loc ) ) {
loc.remove_item();
}
if( g->u.get_value( "THIEF_MODE_KEEP" ) != "YES" ) {
g->u.set_value( "THIEF_MODE", "THIEF_ASK" );
}
act.set_to_null();
}

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

jsout.member( "loc", loc );

jsout.end_object();
}

std::unique_ptr<activity_actor> consume_activity_actor::deserialize( JsonIn &jsin )
{
item_location null;
consume_activity_actor actor( null );

JsonObject data = jsin.get_object();

data.read( "loc", actor.loc );

return actor.clone();
}

namespace activity_actors
{

// Please keep this alphabetically sorted
const std::unordered_map<activity_id, std::unique_ptr<activity_actor>( * )( JsonIn & )>
deserialize_functions = {
{ activity_id( "ACT_CONSUME" ), &consume_activity_actor::deserialize },
{ activity_id( "ACT_HACKING" ), &hacking_activity_actor::deserialize },
{ activity_id( "ACT_MIGRATION_CANCEL" ), &migration_cancel_activity_actor::deserialize },
{ activity_id( "ACT_MOVE_ITEMS" ), &move_items_activity_actor::deserialize },
Expand Down
25 changes: 25 additions & 0 deletions src/activity_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ class open_gate_activity_actor : public activity_actor
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class consume_activity_actor : public activity_actor
{
private:
item_location loc;

public:
consume_activity_actor( const item_location &loc ) :
loc( loc ) {}

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

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

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

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

namespace activity_actors
{

Expand Down
11 changes: 1 addition & 10 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,16 +1079,7 @@ void avatar_action::eat( avatar &you, item_location loc )
add_msg( _( "Never mind." ) );
return;
}
item *it = loc.get_item();
if( loc.where() == item_location::type::character ) {
you.consume( loc );

} else if( you.consume_item( *it ) ) {
loc.remove_item();
}
if( g->u.get_value( "THIEF_MODE_KEEP" ) != "YES" ) {
g->u.set_value( "THIEF_MODE", "THIEF_ASK" );
}
you.assign_activity( player_activity( consume_activity_actor( loc ) ) );
}

void avatar_action::plthrow( avatar &you, item_location loc,
Expand Down