Horizon Official Technical Documentation
Attribute.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_ZONE_GAME_TRAITS_ATTRIBUTES_HPP
31#define HORIZON_ZONE_GAME_TRAITS_ATTRIBUTES_HPP
32
35#include <memory>
36#include <string>
37#include <vector>
38#include <chrono>
39#include <functional>
40
41namespace Horizon
42{
43namespace Zone
44{
45class Unit;
46class Player;
47namespace Traits
48{
50 {
51 public:
52 virtual void add_min(int32_t val) { set_min(_min + val); }
53 virtual void sub_min(int32_t val) { set_min(_min - std::min(_min, val)); }
54 virtual int32_t get_min() const { return _min; }
55
56 virtual void set_min(int32_t val)
57 {
58 _min = val;
59 }
60
61 virtual void add_max(int32_t val) { set_max(_max + val); }
62 virtual void sub_max(int32_t val) { set_max(_max - std::min(_max, val)); }
63 virtual int32_t get_max() const { return _max; }
64
65 virtual void set_max(int32_t val)
66 {
67 _max = val;
68 }
69
70 protected:
71 int32_t _min{ 0 }, _max{ 0 };
72 };
73
74 class Attribute;
75
77 {
79 {
80 s_attribute_min_max(int32_t min, int32_t max) : s_min_max(min, max) {}
81 s_attribute_min_max(int32_t min, Attribute *attr);
82 // create constructor for min attribute value if required.
84
86
87 bool is_valid() { return _attr != nullptr; }
88
89 private:
91 };
93 {
94 public:
96 : _attr(attr) {}
97
99
100 bool is_valid() { return _attr != nullptr; }
101
103
104 private:
106 };
107
108 int32_t base{ 0 };
109 int32_t equip{ 0 };
110 int32_t status{ 0 };
114
122
123 int32_t get_base() const { return base; }
124 void set_base(int32_t val) { base = val; }
125
126 int32_t get_equip() const { return equip; }
127 void set_equip(int32_t val) { equip = val; }
128
129 int32_t get_status() const { return status; }
130 void set_status(int32_t val) { status = val; }
131
132 int32_t get_max() { return minmax.get_max(); }
133 void set_max(int32_t val) { minmax.set_max(val); }
134
135 int32_t get_min() { return minmax.get_min(); }
136 void set_min(int32_t val) { minmax.set_min(val); }
137
139 {
140 if (get_live_attribute().is_valid()) {
142 }
143 if (minmax.is_valid()) {
145 }
146 }
147
150 {
152 }
153
156 };
157
159 {
160 public:
162
164 {
166 std::string source{ "" };
167 };
168
169 void add_change(s_attribute_change_values change, std::string source);
170
171 void remove_change(std::string source);
172
173 void apply(bool notify = true);
174
175 private:
176 std::vector<s_permanent_change> _changes;
178 };
179
181 {
182 public:
184
186 {
188 uint64_t duration{ 0 };
189 std::string source{ "" };
190 std::chrono::high_resolution_clock::time_point start_time{std::chrono::high_resolution_clock::now()};
191 };
192
193 void add_change(s_attribute_change_values change, uint64_t duration, std::string source);
194
195 void remove_change(std::string source);
196
197 void apply(bool notify = true);
198
199 void update(uint64_t delta);
200
201 private:
202 std::vector<s_temporary_change> _changes;
204 };
205
207 {
208 public:
210
212 {
214 uint64_t duration{ 0 };
215 uint64_t interval{ 0 };
216 std::string source{ "" };
217 std::chrono::high_resolution_clock::time_point last_update{std::chrono::high_resolution_clock::now()};
218 std::chrono::high_resolution_clock::time_point start_time{std::chrono::high_resolution_clock::now()};
219 };
220
221 void add_change(s_attribute_change_values change, uint64_t duration, uint64_t interval, std::string source);
222
223 void remove_change(std::string source);
224
225 void update(uint64_t delta);
226
227 private:
228 std::vector<s_periodic_change> _changes;
230 };
231
233 {
234 public:
235 // Default constructor
236 Attribute() = default;
237
238 // Parameterized constructor
239 Attribute(std::weak_ptr<Unit> unit, status_point_type st_type, int32_t base = 0, int32_t equip = 0, int32_t status = 0)
240 : _status_point_type(st_type), _unit(unit)
241 {
242 add_permanent_change({base, equip, status}, "initial");
243 }
244
245 // Copy constructor
246 Attribute(const Attribute &other)
248 {
249 add_permanent_change({other._base_val, other._equip_val, other._status_val}, "initial");
250 }
251
252 // Move constructor
253 Attribute(Attribute &&other) noexcept
254 : _unit(std::move(other._unit)), _status_point_type(other._status_point_type), _base_val(other._base_val), _equip_val(other._equip_val), _status_val(other._status_val)
255 {
256 add_permanent_change({other._base_val, other._equip_val, other._status_val}, "initial");
257 }
258
259 std::shared_ptr<Unit> unit() { return _unit.lock(); }
260 void unit(std::shared_ptr<Unit> e) { _unit = e; }
261
262 virtual void set_base(int32_t val, bool notify = true)
263 {
264 _base_val = val;
265 if (notify) this->notify();
266 }
267 virtual void add_base(int32_t val, bool notify = true);
268 virtual void sub_base(int32_t val, bool notify = true);
269 virtual int32_t get_base() const { return _base_val; }
270
271 virtual void set_equip(int32_t val, bool notify = true)
272 {
273 _equip_val = val;
274 if (notify) this->notify();
275 }
276 virtual void add_equip(int32_t val, bool notify = true);
277 virtual void sub_equip(int32_t val, bool notify = true);
278 virtual int32_t get_equip() const { return _equip_val; }
279
280 virtual void set_status(int32_t val, bool notify = true)
281 {
282 _status_val = val;
283 if (notify) this->notify();
284 }
285 virtual void add_status(int32_t val, bool notify = true);
286 virtual void sub_status(int32_t val, bool notify = true);
287 virtual int32_t get_status() const { return _status_val; }
288
289 virtual int32_t total() const { return _base_val + _equip_val + _status_val; }
290
291 virtual int32_t compute() { this->notify(); return total(); }
292
293 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
294 TT operator + (TT right) { return total() + right; }
295 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
296 TT operator / (TT right) { return total() / right; }
297 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
298 TT operator * (TT right) { return total() * right; }
299 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
300 TT operator - (TT right) { return total() - right; }
301
302 int operator + (Attribute const &right) const { return total() + right.total() ; }
303 double operator / (Attribute const &right) { return right.total() / total(); }
304 double operator * (Attribute const &right) { return right.total() * total(); }
305 int operator - (Attribute const &right) { return right.total() - total(); }
306
307 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
308 bool operator == (TT right) { return total() == right; }
309 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
310 bool operator != (TT right) { return total() != right; }
311 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
312 bool operator > (TT right) { return total() > right; }
313 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
314 bool operator >= (TT right) { return total() >= right; }
315 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
316 bool operator < (TT right) { return total() < right; }
317 template <typename TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
318 bool operator <= (TT right) { return total() <= right; }
319
320 bool operator == (Attribute const &right) { return total() == right.total(); }
321 bool operator != (Attribute const &right) { return total() != right.total(); }
322 bool operator > (Attribute const &right) { return total() > right.total(); }
323 bool operator >= (Attribute const &right) { return total() >= right.total(); }
324 bool operator < (Attribute const &right) { return total() < right.total(); }
325 bool operator <= (Attribute const &right) { return total() <= right.total(); }
326
328 {
329 unit(right.unit());
330 set_base(right.get_base());
331 set_equip(right.get_equip());
332 set_status(right.get_status());
333
334 return *this;
335 }
336
338 {
339 unit(right.unit());
340 set_base(right.get_base());
341 set_equip(right.get_equip());
342 set_status(right.get_status());
343
344 return *this;
345 }
346
347 void add_permanent_change(s_attribute_change_values change, std::string source)
348 {
349 _permanent_changes.add_change(change, source);
350 }
351
352 void remove_permanent_change(std::string source)
353 {
355 }
356
357 void add_temporary_change(s_attribute_change_values change, uint64_t duration, std::string source)
358 {
359 _temporary_changes.add_change(change, duration, source);
360 }
361
362 void remove_temporary_change(std::string source)
363 {
365 }
366
367 void add_periodic_change(s_attribute_change_values change, uint64_t duration, uint64_t interval, std::string source)
368 {
369 _periodic_changes.add_change(change, duration, interval, source);
370 }
371
372 void remove_periodic_change(std::string source)
373 {
375 }
376
377 void update(uint64_t delta)
378 {
380 if (_apply_periodic_changes == true) {
382 }
383 }
384
385 void apply(bool notify = true)
386 {
387 // do not calculate until ready. Changes to stats (via permanent changes and temporary changes)
388 // will not be calculated until ready.
389 _calculate_ready = false;
393 _calculate_ready = true;
394 compute();
395 }
396
397 void reset()
398 {
400 _base_val = 0;
401 _equip_val = 0;
402 _status_val = 0;
403 }
404
405 void notify();
406
408
409 bool needs_recalculation() const { return _recalculate_flag; }
410 void recalculate(bool flag) { _recalculate_flag = flag; }
411
412 bool is_compute_ready() const { return _calculate_ready; }
413
414 protected:
416 int32_t _base_val{0};
417 int32_t _equip_val{0};
418 int32_t _status_val{0};
420 bool _recalculate_flag{false};
421 bool _calculate_ready{false};
422 private:
423 std::weak_ptr<Unit> _unit;
427 };
428
429}
430}
431}
432
433#endif /* HORIZON_ZONE_GAME_STATUS_ATTRIBUTES_HPP */
status_point_type
Definition: UnitDefinitions.hpp:87
@ STATUS_POINT_INVALID
Definition: UnitDefinitions.hpp:88
Definition: Attribute.hpp:50
virtual void set_max(int32_t val)
Definition: Attribute.hpp:65
virtual void sub_max(int32_t val)
Definition: Attribute.hpp:62
virtual void add_max(int32_t val)
Definition: Attribute.hpp:61
virtual void set_min(int32_t val)
Definition: Attribute.hpp:56
virtual int32_t get_min() const
Definition: Attribute.hpp:54
int32_t _min
Definition: Attribute.hpp:71
virtual void sub_min(int32_t val)
Definition: Attribute.hpp:53
virtual int32_t get_max() const
Definition: Attribute.hpp:63
int32_t _max
Definition: Attribute.hpp:71
virtual void add_min(int32_t val)
Definition: Attribute.hpp:52
Definition: Attribute.hpp:233
Attribute(const Attribute &other)
Definition: Attribute.hpp:246
virtual void set_equip(int32_t val, bool notify=true)
Definition: Attribute.hpp:271
virtual void sub_equip(int32_t val, bool notify=true)
Definition: Attribute.cpp:256
TT operator/(TT right)
Definition: Attribute.hpp:296
void add_temporary_change(s_attribute_change_values change, uint64_t duration, std::string source)
Definition: Attribute.hpp:357
virtual void set_base(int32_t val, bool notify=true)
Definition: Attribute.hpp:262
void remove_temporary_change(std::string source)
Definition: Attribute.hpp:362
bool _recalculate_flag
Definition: Attribute.hpp:420
void update(uint64_t delta)
Definition: Attribute.hpp:377
bool operator>(TT right)
Definition: Attribute.hpp:312
Attribute operator=(Attribute &right)
Definition: Attribute.hpp:327
virtual void set_status(int32_t val, bool notify=true)
Definition: Attribute.hpp:280
bool operator<(TT right)
Definition: Attribute.hpp:316
TT operator*(TT right)
Definition: Attribute.hpp:298
virtual void sub_status(int32_t val, bool notify=true)
Definition: Attribute.cpp:264
int32_t _base_val
Definition: Attribute.hpp:416
TT operator-(TT right)
Definition: Attribute.hpp:300
virtual void add_base(int32_t val, bool notify=true)
Definition: Attribute.cpp:244
virtual int32_t compute()
Definition: Attribute.hpp:291
bool _calculate_ready
Definition: Attribute.hpp:421
virtual int32_t get_equip() const
Definition: Attribute.hpp:278
std::shared_ptr< Unit > unit()
Definition: Attribute.hpp:259
virtual void sub_base(int32_t val, bool notify=true)
Definition: Attribute.cpp:248
Attribute(std::weak_ptr< Unit > unit, status_point_type st_type, int32_t base=0, int32_t equip=0, int32_t status=0)
Definition: Attribute.hpp:239
bool needs_recalculation() const
Definition: Attribute.hpp:409
std::weak_ptr< Unit > _unit
Definition: Attribute.hpp:423
TT operator+(TT right)
Definition: Attribute.hpp:294
void reset()
Definition: Attribute.hpp:397
virtual int32_t get_status() const
Definition: Attribute.hpp:287
TemporaryChanges _temporary_changes
Definition: Attribute.hpp:425
void remove_periodic_change(std::string source)
Definition: Attribute.hpp:372
void remove_permanent_change(std::string source)
Definition: Attribute.hpp:352
void unit(std::shared_ptr< Unit > e)
Definition: Attribute.hpp:260
virtual void add_equip(int32_t val, bool notify=true)
Definition: Attribute.cpp:252
PeriodicChanges _periodic_changes
Definition: Attribute.hpp:426
void notify()
Definition: Attribute.cpp:269
void recalculate(bool flag)
Definition: Attribute.hpp:410
status_point_type _status_point_type
Definition: Attribute.hpp:415
bool operator<=(TT right)
Definition: Attribute.hpp:318
int32_t _equip_val
Definition: Attribute.hpp:417
int32_t _status_val
Definition: Attribute.hpp:418
bool _apply_periodic_changes
Definition: Attribute.hpp:419
Attribute(Attribute &&other) noexcept
Definition: Attribute.hpp:253
bool operator==(TT right)
Definition: Attribute.hpp:308
PermanentChanges _permanent_changes
Definition: Attribute.hpp:424
void apply(bool notify=true)
Definition: Attribute.hpp:385
bool operator!=(TT right)
Definition: Attribute.hpp:310
void add_permanent_change(s_attribute_change_values change, std::string source)
Definition: Attribute.hpp:347
virtual int32_t get_base() const
Definition: Attribute.hpp:269
bool operator>=(TT right)
Definition: Attribute.hpp:314
void add_periodic_change(s_attribute_change_values change, uint64_t duration, uint64_t interval, std::string source)
Definition: Attribute.hpp:367
virtual void add_status(int32_t val, bool notify=true)
Definition: Attribute.cpp:260
status_point_type get_type() const
Definition: Attribute.hpp:407
bool is_compute_ready() const
Definition: Attribute.hpp:412
virtual int32_t total() const
Definition: Attribute.hpp:289
Definition: Attribute.hpp:207
PeriodicChanges(Attribute *attr)
Definition: Attribute.hpp:209
void add_change(s_attribute_change_values change, uint64_t duration, uint64_t interval, std::string source)
Definition: Attribute.cpp:145
void update(uint64_t delta)
Definition: Attribute.cpp:155
Attribute * _attr
Definition: Attribute.hpp:229
void remove_change(std::string source)
Definition: Attribute.cpp:150
std::vector< s_periodic_change > _changes
Definition: Attribute.hpp:228
Definition: Attribute.hpp:159
PermanentChanges(Attribute *attr)
Definition: Attribute.hpp:161
std::vector< s_permanent_change > _changes
Definition: Attribute.hpp:176
Attribute * _attr
Definition: Attribute.hpp:177
void add_change(s_attribute_change_values change, std::string source)
Definition: Attribute.cpp:70
void apply(bool notify=true)
Definition: Attribute.cpp:80
void remove_change(std::string source)
Definition: Attribute.cpp:75
Definition: Attribute.hpp:181
TemporaryChanges(Attribute *attr)
Definition: Attribute.hpp:183
void remove_change(std::string source)
Definition: Attribute.cpp:106
void update(uint64_t delta)
Definition: Attribute.cpp:132
void apply(bool notify=true)
Definition: Attribute.cpp:111
Attribute * _attr
Definition: Attribute.hpp:203
std::vector< s_temporary_change > _changes
Definition: Attribute.hpp:202
void add_change(s_attribute_change_values change, uint64_t duration, std::string source)
Definition: Attribute.cpp:101
Attribute * get_attribute()
Definition: Attribute.hpp:102
ApplyLiveAttribute(Attribute *attr)
Definition: Attribute.hpp:95
void update_with_live_attribute(s_attribute_change_values *change)
Definition: Attribute.cpp:45
Definition: ReferenceListTest.cpp:114
Definition: Element.hpp:7
std::chrono::high_resolution_clock::time_point last_update
Definition: Attribute.hpp:217
s_attribute_change_values change
Definition: Attribute.hpp:213
std::string source
Definition: Attribute.hpp:216
std::chrono::high_resolution_clock::time_point start_time
Definition: Attribute.hpp:218
uint64_t duration
Definition: Attribute.hpp:214
uint64_t interval
Definition: Attribute.hpp:215
std::string source
Definition: Attribute.hpp:166
s_attribute_change_values change
Definition: Attribute.hpp:165
s_attribute_change_values change
Definition: Attribute.hpp:187
std::string source
Definition: Attribute.hpp:189
uint64_t duration
Definition: Attribute.hpp:188
std::chrono::high_resolution_clock::time_point start_time
Definition: Attribute.hpp:190
s_attribute_min_max(int32_t min, int32_t max)
Definition: Attribute.hpp:80
s_attribute_change_values(ApplyLiveAttribute apply_live_attribute, s_attribute_min_max minmax=s_attribute_min_max(0, 0), std::function< void(s_attribute_change_values &)> client_notify_function=nullptr)
Definition: Attribute.hpp:118
int32_t equip
Definition: Attribute.hpp:109
int32_t status
Definition: Attribute.hpp:110
void set_equip(int32_t val)
Definition: Attribute.hpp:127
int32_t get_min()
Definition: Attribute.hpp:135
int32_t get_status() const
Definition: Attribute.hpp:129
void set_max(int32_t val)
Definition: Attribute.hpp:133
void set_min(int32_t val)
Definition: Attribute.hpp:136
ApplyLiveAttribute apply_live_attribute
Definition: Attribute.hpp:112
int32_t get_equip() const
Definition: Attribute.hpp:126
s_attribute_change_values(Attribute *attr, s_attribute_min_max minmax=s_attribute_min_max(0, 0), std::function< void(s_attribute_change_values &)> client_notify_function=nullptr)
Definition: Attribute.hpp:120
void set_status(int32_t val)
Definition: Attribute.hpp:130
int32_t get_base() const
Definition: Attribute.hpp:123
void set_live_attribute(Attribute *attr)
Definition: Attribute.hpp:149
int32_t get_max()
Definition: Attribute.hpp:132
void set_client_notify_function(std::function< void(s_attribute_change_values &)> func)
Definition: Attribute.hpp:155
ApplyLiveAttribute get_live_attribute()
Definition: Attribute.hpp:148
s_attribute_min_max minmax
Definition: Attribute.hpp:111
s_attribute_change_values(int32_t base, int32_t equip=0, int32_t status=0, s_attribute_min_max minmax=s_attribute_min_max(0, 0), std::function< void(s_attribute_change_values &)> client_notify_function=nullptr)
Definition: Attribute.hpp:116
int32_t base
Definition: Attribute.hpp:108
void set_base(int32_t val)
Definition: Attribute.hpp:124
void update_with_live_attribute()
Definition: Attribute.hpp:138
std::function< void(s_attribute_change_values &)> client_notify_function
Definition: Attribute.hpp:113
std::function< void(s_attribute_change_values &)> get_client_notify_function()
Definition: Attribute.hpp:154
Definition: Horizon.hpp:84
int max
Definition: Horizon.hpp:94
void set_min(int min)
Definition: Horizon.hpp:87
int min
Definition: Horizon.hpp:93
int get_max()
Definition: Horizon.hpp:91
int get_min()
Definition: Horizon.hpp:90
void set_max(int max)
Definition: Horizon.hpp:88