forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_actor.h
142 lines (120 loc) · 4.67 KB
/
activity_actor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once
#ifndef CATA_SRC_ACTIVITY_ACTOR_H
#define CATA_SRC_ACTIVITY_ACTOR_H
#include <iosfwd>
#include <memory>
#include <unordered_map>
#include "activity_type.h"
#include "clone_ptr.h"
#include "point.h"
#include "type_id.h"
class Character;
class JsonIn;
class JsonOut;
class JsonValue;
class monster;
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;
/**
* Should return the activity id of the corresponding activity
*/
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.
*/
virtual void do_turn( player_activity &act, Character &who ) = 0;
/**
* Called when the activity runs out of moves, assuming that it has not
* already been set to null
*/
virtual void finish( player_activity &act, Character &who ) = 0;
/**
* Called just before Character::cancel_activity() executes.
* This may be used to perform cleanup
*/
virtual void canceled( player_activity &/*act*/, Character &/*who*/ ) {}
/**
* Called in player_activity::can_resume_with
* 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.
*/
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;
}
/**
* Used to generate the progress display at the top of the screen
*/
virtual std::string get_progress_message( const player_activity &act ) const;
/**
* Called every turn, in player_activity::do_turn
* (with some indirection through player_activity::exertion_level)
* How strenuous this activity level is
*/
virtual float exertion_level() const {
return get_type()->exertion_level();
}
/**
* Override to false in actors that are slow because they frequently
* invalidate the inventory and trigger excessive
* drop_invalid_inventory() calls, and can guarantee that it is called
* appropriately themselves.
*/
virtual bool do_drop_invalid_inventory() const {
return true;
}
/**
* Returns a deep copy of this object. Example implementation:
* \code
* class my_activity_actor {
* std::unique_ptr<activity_actor> clone() const override {
* return std::make_unique<my_activity_actor>( *this );
* }
* };
* \endcode
* The returned value should behave like the original item and must have the same type.
*/
virtual std::unique_ptr<activity_actor> clone() const = 0;
/**
* Must write any custom members of the derived class to json
* Note that a static member function for deserialization must also be created and
* added to the `activity_actor_deserializers` hashmap in activity_actor.cpp
*/
virtual void serialize( JsonOut &jsout ) const = 0;
};
void serialize( const cata::clone_ptr<activity_actor> &actor, JsonOut &jsout );
void deserialize( cata::clone_ptr<activity_actor> &actor, JsonIn &jsin );
namespace activity_actors
{
// defined in activity_actor.cpp
extern const std::unordered_map<activity_id, std::unique_ptr<activity_actor>( * )( JsonValue & )>
deserialize_functions;
} // namespace activity_actors
#endif // CATA_SRC_ACTIVITY_ACTOR_H