Debian Codegraph @

plugins/dm.objectives/Component.h

raw
1 #pragma once
2
3 #include "SpecifierType.h"
4 #include "Specifier.h"
5 #include "ComponentType.h"
6
7 #include <cassert>
8 #include <vector>
9 #include <string>
10 #include "string/join.h"
11 #include <sigc++/signal.h>
12
13 namespace objectives
14 {
15
16 /**
17 * A component of an objective.
18 *
19 * Each objective may have a number of components, which are combined using
20 * boolean operations to determine whether the objective is satisfied or not.
21 * A component is essentially a condition which needs to be met in order to
22 * satisfy the overall objective.
23 */
24 class Component
25 {
26 private:
27 // Completion state flag
28 bool _satisfied;
29
30 // Inverted flag
31 bool _inverted;
32
33 // Irreversible (latched) flag
34 bool _irreversible;
35
36 // Player responsible flag
37 bool _playerResponsible;
38
39 // The clock interval in seconds (only applicable for clocked components)
40 // is negative if not used
41 float _clockInterval;
42
43 // Component type
44 ComponentType _type;
45
46 // List of Specifiers (SpecifierType + value pairs)
47 SpecifierList _specifiers;
48
49 /**
50 * greebo: Each component can have an arbitrary number of arguments.
51 */
52 typedef std::vector<std::string> ArgumentList;
53 ArgumentList _arguments;
54
55 sigc::signal<void> _sigChanged;
56
57 public:
58
59 /**
60 * Construct a Component with default settings.
61 *
62 * All flags are set to false, and the type is set to an arbitrary value.
63 */
64 Component()
65 : _satisfied(false),
66 _inverted(false),
67 _irreversible(false),
68 _playerResponsible(false),
69 _clockInterval(-1.0f),
70 _type(ComponentType::COMP_KILL()), // arbitrary choice, no NONE option
71 _specifiers(static_cast<std::size_t>(Specifier::MAX_SPECIFIERS))
72 { }
73
74 // Copy-constructor
75 Component(const Component& other) :
76 _satisfied(other._satisfied),
77 _inverted(other._inverted),
78 _irreversible(other._irreversible),
79 _playerResponsible(other._playerResponsible),
80 _clockInterval(other._clockInterval),
81 _type(other._type),
82 _specifiers(other._specifiers),
83 _arguments(other._arguments)
84 // don't copy signals
85 { }
86
87 /**
88 * Set the flag to indicate that this component has been satisfied.
89 */
90 void setSatisfied(bool satisfied) {
91 _satisfied = satisfied;
92 _sigChanged();
93 }
94
95 /**
96 * Get the satisfied status flag.
97 */
98 bool isSatisfied() const {
99 return _satisfied;
100 }
101
102 /**
103 * Set the flag to indicate that the sense of this component is inverted.
104 *
105 * If true, this component is logically <b>NOT</b>ed, so when the conditions
106 * described by the type and specifiers are not met, the component state is
107 * true, and when they are met, it is false.
108 */
109 void setInverted(bool inverted) {
110 _inverted = inverted;
111 _sigChanged();
112 }
113
114 /**
115 * Get the inverted status.
116 */
117 bool isInverted() const {
118 return _inverted;
119 }
120
121 /**
122 * Set the flag to indicate that this component changes state once then
123 * latches, even if its in-game condition is no longer satisfied.
124 */
125 void setIrreversible(bool irreversible) {
126 _irreversible = irreversible;
127 _sigChanged();
128 }
129
130 /**
131 * Get the irreversible status.
132 */
133 bool isIrreversible() const {
134 return _irreversible;
135 }
136
137 /**
138 * Set the flag to indicate that the player must be responsible for
139 * satisfying this component.
140 *
141 * If this flag is set, the component will not be satisfied by an event
142 * which is not <i>directly</i> caused by the player. For example, if the
143 * component requires the killing of an AI, it will not be satisfied if the
144 * AI is killed by another AI rather than the player.
145 */
146 void setPlayerResponsible(bool playerResponsible) {
147 _playerResponsible = playerResponsible;
148 _sigChanged();
149 }
150
151 /**
152 * Get the player-responsible status.
153 */
154 bool isPlayerResponsible() const {
155 return _playerResponsible;
156 }
157
158 /**
159 * Set the clock interval for clocked components in seconds.
160 */
161 void setClockInterval(float clockInterval) {
162 _clockInterval = clockInterval;
163 _sigChanged();
164 }
165
166 /**
167 * Get the clock interval for clocked components in seconds.
168 * Returns negative values if this option is not used for this component.
169 */
170 float getClockInterval() const {
171 return _clockInterval;
172 }
173
174 /**
175 * Set the type of this component ("kill", "ko" etc).
176 */
177 void setType(ComponentType type) {
178 _type = type;
179 _sigChanged();
180 }
181
182 /**
183 * Get the component type.
184 */
185 ComponentType getType() const {
186 return _type;
187 }
188
189 /**
190 * Return a string description of this Component.
191 */
192 std::string getString();
193
194 /**
195 * Set the Specifier at the given index.
196 *
197 * @param idx
198 * The index of the specifier, starting from 1.
199 */
200 void setSpecifier(Specifier::SpecifierNumber num, SpecifierPtr spec)
201 {
202 assert(
203 _specifiers.size() == static_cast<std::size_t>(
204 Specifier::MAX_SPECIFIERS
205 )
206 );
207 _specifiers[static_cast<std::size_t>(num)] = spec;
208 _sigChanged();
209 }
210
211 /**
212 * Get the Specifier at the given index.
213 *
214 * @param idx
215 * The index of the specifier, starting from 1.
216 */
217 SpecifierPtr getSpecifier(Specifier::SpecifierNumber num) const
218 {
219 assert(
220 _specifiers.size() == static_cast<std::size_t>(
221 Specifier::MAX_SPECIFIERS
222 )
223 );
224 return _specifiers[static_cast<std::size_t>(num)];
225 }
226
227 void clearArguments() {
228 _arguments.clear();
229 _sigChanged();
230 }
231
232 void addArgument(const std::string& arg) {
233 _arguments.push_back(arg);
234 _sigChanged();
235 }
236
237 std::size_t getNumArguments() {
238 return _arguments.size();
239 }
240
241 // returns "" if the argument with the given index doesn't exist
242 std::string getArgument(std::size_t index) {
243 return index < _arguments.size() ? _arguments[index] : "";
244 }
245
246 // Sets the argument with the given index
247 void setArgument(std::size_t index, const std::string& value) {
248 // Ensure that the vector is large enough
249 if (_arguments.size() <= index) {
250 _arguments.resize(index+1);
251 }
252
253 // Now set the value
254 _arguments[index] = value;
255
256 _sigChanged();
257 }
258
259 // Returns all arguments in a space-delimited string
260 std::string getArgumentString() const
261 {
262 return string::join(_arguments, " ");
263 }
264
265 // Allow client code to be notified on change events
266 sigc::signal<void> signal_Changed() const
267 {
268 return _sigChanged;
269 }
270 };
271
272 }