Skip to content

Commit

Permalink
Move ACT_READ from activity_handler to activity_actor (#46304)
Browse files Browse the repository at this point in the history
* act_read initial body

Co-authored-by: actual-nh <[email protected]>
Co-authored-by: Binrui Dong <[email protected]>

* convert act_read into an activity_actor

* update reading tests

* act_read minor fixes

* rebase act_read to experimental build 2021-07-17-0839

Co-authored-by: Saicchi <[email protected]>
Co-authored-by: actual-nh <[email protected]>
Co-authored-by: Binrui Dong <[email protected]>
Co-authored-by: Kevin Granade <[email protected]>
  • Loading branch information
5 people authored Jul 18, 2021
1 parent a17ccb1 commit ac81446
Show file tree
Hide file tree
Showing 18 changed files with 792 additions and 590 deletions.
474 changes: 474 additions & 0 deletions src/activity_actor.cpp

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class Creature;
class JsonIn;
class JsonOut;
class avatar;
class npc;
class SkillLevel;
class player_activity;

struct islot_book;

class aim_activity_actor : public activity_actor
{
private:
Expand Down Expand Up @@ -392,6 +396,60 @@ class bikerack_unracking_activity_actor : public activity_actor
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class read_activity_actor : public activity_actor
{
public:
enum class book_type : int { normal, martial_art };

read_activity_actor() = default;

read_activity_actor( int moves, item_location &book, bool continuous = false, int learner_id = -1 )
: moves_total( moves ), book( book ),
continuous( continuous ), learner_id( learner_id ) {};

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

static void read_book( Character &learner, const cata::value_ptr<islot_book> &islotbook,
SkillLevel &skill_level, double penalty );

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

std::string get_progress_message( const player_activity & ) const override;

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

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

private:
int moves_total;
item_location book;
cata::optional<book_type> bktype;

// Read until the learner with this ID gets a level
bool continuous;
int learner_id;

// Will return true if activity must be set to null
bool player_read( avatar &you );
bool player_readma( avatar &you ); // Martial arts book
bool npc_read( npc &learner );

bool can_resume_with_internal( const activity_actor &other,
const Character & ) const override {
const read_activity_actor &actor = static_cast<const read_activity_actor &>( other );
return continuous == actor.continuous &&
learner_id == actor.learner_id &&
book->typeId() == actor.book->typeId();
}
};

class move_items_activity_actor : public activity_actor
{
private:
Expand Down
47 changes: 0 additions & 47 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ static const activity_id ACT_PLANT_SEED( "ACT_PLANT_SEED" );
static const activity_id ACT_PRY_NAILS( "ACT_PRY_NAILS" );
static const activity_id ACT_PULP( "ACT_PULP" );
static const activity_id ACT_QUARTER( "ACT_QUARTER" );
static const activity_id ACT_READ( "ACT_READ" );
static const activity_id ACT_REPAIR_ITEM( "ACT_REPAIR_ITEM" );
static const activity_id ACT_ROBOT_CONTROL( "ACT_ROBOT_CONTROL" );
static const activity_id ACT_SKIN( "ACT_SKIN" );
Expand Down Expand Up @@ -304,7 +303,6 @@ activity_handlers::do_turn_functions = {
{ ACT_ROBOT_CONTROL, robot_control_do_turn },
{ ACT_TREE_COMMUNION, tree_communion_do_turn },
{ ACT_STUDY_SPELL, study_spell_do_turn},
{ ACT_READ, read_do_turn},
{ ACT_WAIT_STAMINA, wait_stamina_do_turn }
};

Expand Down Expand Up @@ -338,7 +336,6 @@ activity_handlers::finish_functions = {
{ ACT_GUNMOD_ADD, gunmod_add_finish },
{ ACT_TOOLMOD_ADD, toolmod_add_finish },
{ ACT_CLEAR_RUBBLE, clear_rubble_finish },
{ ACT_READ, read_finish },
{ ACT_WAIT, wait_finish },
{ ACT_WAIT_WEATHER, wait_weather_finish },
{ ACT_WAIT_NPC, wait_npc_finish },
Expand Down Expand Up @@ -3018,50 +3015,6 @@ void activity_handlers::butcher_do_turn( player_activity * /*act*/, player *p )
p->mod_stamina( -20 );
}

void activity_handlers::read_do_turn( player_activity *act, player *p )
{
if( p->is_player() ) {
// next check doesn't work for NPCs because it is counted for player's submap and z-level only
if( p->fine_detail_vision_mod() > 4 ) {
//It got too dark during the process of reading, bail out.
act->set_to_null();
p->add_msg_if_player( m_bad, _( "It's too dark to read!" ) );
return;
}

if( !act->str_values.empty() && act->str_values[0] == "martial_art" && one_in( 3 ) ) {
if( act->values.empty() ) {
act->values.push_back( p->get_stamina() );
}
p->set_stamina( act->values[0] - 1 );
act->values[0] = p->get_stamina();
}
} else {
p->moves = 0;
}
}

void activity_handlers::read_finish( player_activity *act, player *p )
{
if( !act || !act->targets.front() ) {
debugmsg( "Lost target of ACT_READ" );
return;
}
if( p->is_npc() ) {
npc *guy = dynamic_cast<npc *>( p );
guy->finish_read( * act->targets.front().get_item() );
} else {
if( avatar *u = p->as_avatar() ) {
u->do_read( *act->targets.front().get_item() );
} else {
act->set_to_null();
}
if( !act ) {
p->add_msg_if_player( m_info, _( "You finish reading." ) );
}
}
}

void activity_handlers::wait_finish( player_activity *act, player *p )
{
p->add_msg_if_player( _( "You finish waiting." ) );
Expand Down
2 changes: 0 additions & 2 deletions src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ void robot_control_do_turn( player_activity *act, player *p );
void tree_communion_do_turn( player_activity *act, player *p );
void spellcasting_do_turn( player_activity *act, player *p );
void study_spell_do_turn( player_activity *act, player *p );
void read_do_turn( player_activity *act, player *p );
void wait_stamina_do_turn( player_activity *act, player *p );

// defined in activity_handlers.cpp
Expand Down Expand Up @@ -212,7 +211,6 @@ void gunmod_add_finish( player_activity *act, player *p );
void toolmod_add_finish( player_activity *act, player *p );
void clear_rubble_finish( player_activity *act, player *p );
void heat_item_finish( player_activity *act, player *p );
void read_finish( player_activity *act, player *p );
void wait_finish( player_activity *act, player *p );
void wait_weather_finish( player_activity *act, player *p );
void wait_npc_finish( player_activity *act, player *p );
Expand Down
Loading

0 comments on commit ac81446

Please sign in to comment.