Skip to content

Commit

Permalink
Added private virtual can_resume_with_internal to activity_actor which
Browse files Browse the repository at this point in the history
is called from activity_actor::can_resume_with to allow a safe
static_cast
Changed the json keys of open_gate_activity_actor, dig_activity_actor,
and dig_channel_activity_actor from moves_total to moves
  • Loading branch information
rsulli55 committed May 5, 2020
1 parent 058073b commit f5e57c7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
39 changes: 6 additions & 33 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const dig_activity_actor &>
( 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 );
Expand All @@ -139,7 +130,7 @@ std::unique_ptr<activity_actor> 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 );
Expand Down Expand Up @@ -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<const dig_channel_activity_actor &>
( 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 );
Expand All @@ -215,7 +197,7 @@ std::unique_ptr<activity_actor> 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 );
Expand Down Expand Up @@ -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<const open_gate_activity_actor &>( 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();
Expand All @@ -604,7 +577,7 @@ std::unique_ptr<activity_actor> 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();
Expand Down
113 changes: 74 additions & 39 deletions src/activity_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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<const dig_activity_actor &>( other );
return equivalent_activity( d_actor );
}

public:
dig_activity_actor(
int dig_moves, const tripoint &dig_loc,
Expand All @@ -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<activity_actor> clone() const override {
return std::make_unique<dig_activity_actor>( *this );
Expand All @@ -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<const dig_channel_activity_actor &>
( other );
return equivalent_activity( dc_actor );
}

public:
dig_channel_activity_actor(
int dig_moves, const tripoint &dig_loc,
Expand All @@ -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<activity_actor> clone() const override {
return std::make_unique<dig_channel_activity_actor>( *this );
Expand All @@ -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<activity_actor> clone() const override {
return std::make_unique<hacking_activity_actor>( *this );
Expand Down Expand Up @@ -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<activity_actor> clone() const override {
return std::make_unique<move_items_activity_actor>( *this );
Expand Down Expand Up @@ -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<activity_actor> clone() const override {
return std::make_unique<pickup_activity_actor>( *this );
Expand All @@ -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<activity_actor> clone() const override {
return std::make_unique<migration_cancel_activity_actor>( *this );
Expand All @@ -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<const open_gate_activity_actor &>( 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 ) {}
Expand All @@ -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<activity_actor> clone() const override {
return std::make_unique<open_gate_activity_actor>( *this );
Expand Down

0 comments on commit f5e57c7

Please sign in to comment.