diff --git a/src/activity_actor.cpp b/src/activity_actor.cpp index d8354a9828183..9475faed0ccdb 100644 --- a/src/activity_actor.cpp +++ b/src/activity_actor.cpp @@ -109,20 +109,11 @@ void dig_activity_actor::finish( player_activity &act, Character &who ) act.set_to_null(); } -/** @pre @p other is a dig_activity_actor */ -bool dig_activity_actor::can_resume_with( const activity_actor &other, - const Character & ) const -{ - const dig_activity_actor &d_actor = dynamic_cast - ( other ); - return *this == d_actor; -} - void dig_activity_actor::serialize( JsonOut &jsout ) const { jsout.start_object(); - jsout.member( "moves_total", moves_total ); + jsout.member( "moves", moves_total ); jsout.member( "location", location ); jsout.member( "result_terrain", result_terrain ); jsout.member( "byproducts_location", byproducts_location ); @@ -139,7 +130,7 @@ std::unique_ptr dig_activity_actor::deserialize( JsonIn &jsin ) JsonObject data = jsin.get_object(); - data.read( "moves_total", actor.moves_total ); + data.read( "moves", actor.moves_total ); data.read( "location", actor.location ); data.read( "result_terrain", actor.result_terrain ); data.read( "byproducts_location", actor.byproducts_location ); @@ -185,20 +176,11 @@ void dig_channel_activity_actor::finish( player_activity &act, Character &who ) act.set_to_null(); } -/** @pre @p other is a dig_channel_activity_actor */ -bool dig_channel_activity_actor::can_resume_with( const activity_actor &other, - const Character & ) const -{ - const dig_channel_activity_actor &dc_actor = dynamic_cast - ( other ); - return *this == dc_actor; -} - void dig_channel_activity_actor::serialize( JsonOut &jsout ) const { jsout.start_object(); - jsout.member( "moves_total", moves_total ); + jsout.member( "moves", moves_total ); jsout.member( "location", location ); jsout.member( "result_terrain", result_terrain ); jsout.member( "byproducts_location", byproducts_location ); @@ -215,7 +197,7 @@ std::unique_ptr dig_channel_activity_actor::deserialize( JsonIn JsonObject data = jsin.get_object(); - data.read( "moves_total", actor.moves_total ); + data.read( "moves", actor.moves_total ); data.read( "location", actor.location ); data.read( "result_terrain", actor.result_terrain ); data.read( "byproducts_location", actor.byproducts_location ); @@ -579,20 +561,11 @@ void open_gate_activity_actor::finish( player_activity &act, Character & ) act.set_to_null(); } -/** @pre @p other is an open_gate_activity_actor */ -bool open_gate_activity_actor::can_resume_with( const activity_actor &other, - const Character & ) const -{ - const open_gate_activity_actor &og_actor = dynamic_cast( other ); - - return *this == og_actor; -} - void open_gate_activity_actor::serialize( JsonOut &jsout ) const { jsout.start_object(); - jsout.member( "moves_total", moves_total ); + jsout.member( "moves", moves_total ); jsout.member( "placement", placement ); jsout.end_object(); @@ -604,7 +577,7 @@ std::unique_ptr open_gate_activity_actor::deserialize( JsonIn &j JsonObject data = jsin.get_object(); - data.read( "moves_total", actor.moves_total ); + data.read( "moves", actor.moves_total ); data.read( "placement", actor.placement ); return actor.clone(); diff --git a/src/activity_actor.h b/src/activity_actor.h index c6f454f5477e6..6383b828e021b 100644 --- a/src/activity_actor.h +++ b/src/activity_actor.h @@ -19,6 +19,20 @@ class player_activity; class activity_actor { + private: + /** + * Returns true if `this` activity is resumable, and `this` and @p other + * are "equivalent" i.e. similar enough that `this` activity + * can be resumed instead of starting @p other. + * Many activities are not resumable, so the default is returning + * false. + * @pre @p other is the same type of actor as `this` + */ + virtual bool can_resume_with_internal( const activity_actor &, + const Character & ) const { + return false; + } + public: virtual ~activity_actor() = default; @@ -48,11 +62,18 @@ class activity_actor /** * Called in player_activity::can_resume_with - * Returns true if activities are similar enough that this activity - * can be resumed instead of starting the other activity. - * @pre @p other is the same type of actor + * which allows suspended activities to be resumed instead of + * starting a new activity in certain cases. + * Checks that @p other has the same type as `this` so that + * `can_resume_with_internal` can safely `static_cast` @p other. */ - virtual bool can_resume_with( const activity_actor &other, const Character &who ) const = 0; + bool can_resume_with( const activity_actor &other, const Character &who ) const { + if( other.get_type() == get_type() ) { + return can_resume_with_internal( other, who ); + } + + return false; + } /** * Returns a deep copy of this object. Example implementation: @@ -86,6 +107,26 @@ class dig_activity_actor : public activity_actor int byproducts_count; std::string byproducts_item_group; + /** + * Returns true if @p other and `this` are "equivalent" in the sense that + * `this` can be resumed instead of starting @p other. + */ + bool equivalent_activity( const dig_activity_actor &other ) const { + return location == other.location && + result_terrain == other.result_terrain && + byproducts_location == other.byproducts_location && + byproducts_count == other.byproducts_count && + byproducts_item_group == other.byproducts_item_group; + } + + /** + * @pre @p other is a `dig_activity_actor` + */ + bool can_resume_with_internal( const activity_actor &other, const Character & ) const override { + const dig_activity_actor &d_actor = static_cast( other ); + return equivalent_activity( d_actor ); + } + public: dig_activity_actor( int dig_moves, const tripoint &dig_loc, @@ -105,15 +146,6 @@ class dig_activity_actor : public activity_actor void start( player_activity &act, Character & ) override; void do_turn( player_activity &, Character & ) override; void finish( player_activity &act, Character &who ) override; - bool can_resume_with( const activity_actor &other, const Character & ) const override; - - bool operator==( const dig_activity_actor &other ) const { - return location == other.location && - result_terrain == other.result_terrain && - byproducts_location == other.byproducts_location && - byproducts_count == other.byproducts_count && - byproducts_item_group == other.byproducts_item_group; - } std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -134,6 +166,27 @@ class dig_channel_activity_actor : public activity_actor int byproducts_count; std::string byproducts_item_group; + /** + * Returns true if @p other and `this` are "equivalent" in the sense that + * `this` can be resumed instead of starting @p other. + */ + bool equivalent_activity( const dig_channel_activity_actor &other ) const { + return location == other.location && + result_terrain == other.result_terrain && + byproducts_location == other.byproducts_location && + byproducts_count == other.byproducts_count && + byproducts_item_group == other.byproducts_item_group; + } + + /** + * @pre @p other is a `dig_activity_actor` + */ + bool can_resume_with_internal( const activity_actor &other, const Character & ) const override { + const dig_channel_activity_actor &dc_actor = static_cast + ( other ); + return equivalent_activity( dc_actor ); + } + public: dig_channel_activity_actor( int dig_moves, const tripoint &dig_loc, @@ -153,15 +206,6 @@ class dig_channel_activity_actor : public activity_actor void start( player_activity &act, Character & ) override; void do_turn( player_activity &, Character & ) override; void finish( player_activity &act, Character &who ) override; - bool can_resume_with( const activity_actor &other, const Character & ) const override; - - bool operator==( const dig_channel_activity_actor &other ) const { - return location == other.location && - result_terrain == other.result_terrain && - byproducts_location == other.byproducts_location && - byproducts_count == other.byproducts_count && - byproducts_item_group == other.byproducts_item_group; - } std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -183,9 +227,6 @@ class hacking_activity_actor : public activity_actor void start( player_activity &act, Character &who ) override; void do_turn( player_activity &, Character & ) override {}; void finish( player_activity &act, Character &who ) override; - bool can_resume_with( const activity_actor &, const Character & ) const override { - return false; - }; std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -216,9 +257,6 @@ class move_items_activity_actor : public activity_actor void start( player_activity &, Character & ) override {}; void do_turn( player_activity &act, Character &who ) override; void finish( player_activity &, Character & ) override {}; - bool can_resume_with( const activity_actor &, const Character & ) const override { - return false; - }; std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -256,9 +294,6 @@ class pickup_activity_actor : public activity_actor void start( player_activity &, Character & ) override {}; void do_turn( player_activity &act, Character &who ) override; void finish( player_activity &, Character & ) override {}; - bool can_resume_with( const activity_actor &, const Character & ) const override { - return false; - }; std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -280,9 +315,6 @@ class migration_cancel_activity_actor : public activity_actor void start( player_activity &, Character & ) override {}; void do_turn( player_activity &act, Character &who ) override; void finish( player_activity &, Character & ) override {}; - bool can_resume_with( const activity_actor &, const Character & ) const override { - return false; - }; std::unique_ptr clone() const override { return std::make_unique( *this ); @@ -298,6 +330,14 @@ class open_gate_activity_actor : public activity_actor int moves_total; tripoint placement; + /** + * @pre @p other is a open_gate_activity_actor + */ + bool can_resume_with_internal( const activity_actor &other, const Character & ) const override { + const open_gate_activity_actor &og_actor = static_cast( other ); + return placement == og_actor.placement; + } + public: open_gate_activity_actor( int gate_moves, const tripoint &gate_placement ) : moves_total( gate_moves ), placement( gate_placement ) {} @@ -309,11 +349,6 @@ class open_gate_activity_actor : public activity_actor void start( player_activity &act, Character & ) override; void do_turn( player_activity &, Character & ) override {}; void finish( player_activity &act, Character & ) override; - bool can_resume_with( const activity_actor &, const Character & ) const override; - - bool operator==( const open_gate_activity_actor &other ) const { - return placement == other.placement; - } std::unique_ptr clone() const override { return std::make_unique( *this );