Skip to content

Commit

Permalink
Add a start function to activity_actor
Browse files Browse the repository at this point in the history
  • Loading branch information
ifreund committed Apr 17, 2020
1 parent 5dd7e74 commit a4624d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/activity_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class activity_actor
*/
virtual activity_id get_type() const = 0;

/**
* Called once at the start of the activity.
* This may be used to preform setup actions and/or set
* player_activity::moves_left/moves_total.
*/
virtual void start( player_activity &act, Character &who ) = 0;

/**
* Called on every turn of the activity
* It may be used to stop the activity prematurely by setting it to null.
Expand Down Expand Up @@ -77,6 +84,8 @@ class move_items_activity_actor : public activity_actor
activity_id get_type() const override {
return activity_id( "ACT_MOVE_ITEMS" );
}

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

Expand All @@ -97,6 +106,7 @@ class migration_cancel_activity_actor : public activity_actor
return activity_id( "ACT_MIGRATION_CANCEL" );
}

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

Expand Down
5 changes: 2 additions & 3 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9132,9 +9132,8 @@ void Character::assign_activity( const player_activity &act, bool allow_resume )
activity = act;
}

if( activity.rooted() ) {
rooted_message();
}
activity.start( *this );

if( is_npc() ) {
cancel_stashed_activity();
npc *guy = dynamic_cast<npc *>( this );
Expand Down
10 changes: 10 additions & 0 deletions src/player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ cata::optional<std::string> player_activity::get_progress_message( const avatar
get_verb().translated(), extra_info );
}

void player_activity::start( Character &who )
{
if( actor ) {
actor->start( *this, who );
}
if( rooted() ) {
who.rooted_message();
}
}

void player_activity::do_turn( player &p )
{
// Should happen before activity or it may fail du to 0 moves
Expand Down
12 changes: 10 additions & 2 deletions src/player_activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class player_activity
int moves_total = 0;
/** The number of moves remaining in this activity before it is complete. */
int moves_left = 0;
/** An activity specific value. */

// The members in the following block are deprecated, prefer creating a new
// activity_actor.
int index = 0;
/**
* An activity specific value.
* DO NOT USE FOR ITEM INDEX
*/
int position = 0;
/** An activity specific value. */
std::string name;
std::vector<item_location> targets;
std::vector<int> values;
Expand All @@ -55,6 +56,7 @@ class player_activity
std::unordered_set<tripoint> coord_set;
std::vector<weak_ptr_fast<monster>> monsters;
tripoint placement;

bool no_drink_nearby_for_auto_consume = false;
bool no_food_nearby_for_auto_consume = false;
/** If true, the activity will be auto-resumed next time the player attempts
Expand Down Expand Up @@ -122,6 +124,12 @@ class player_activity
/** Convert from the old enumeration to the new string_id */
void deserialize_legacy_type( int legacy_type, activity_id &dest );

/**
* Preform necessary initialization to start the activity. Must be
* called whenever a Character starts a new activity.
*/
void start( Character &who );

/**
* Performs the activity for a single turn. If the activity is complete
* at the end of the turn, do_turn also executes whatever actions, if
Expand Down

0 comments on commit a4624d0

Please sign in to comment.