SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
Entry.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/Types.h"
7 
8 namespace ai {
9 
10 enum ReductionType {
11  DISABLED, RATIO, VALUE
12 };
13 
17 class Entry {
18 protected:
19  float _aggro;
20  float _minAggro;
21  float _reduceRatioSecond;
22  float _reduceValueSecond;
23  ReductionType _reduceType;
24  CharacterId _id;
25 
26  void reduceByRatio(float ratio);
27  void reduceByValue(float value);
28 
29 public:
30  Entry(const CharacterId& id, float aggro = 0.0f) :
31  _aggro(aggro), _minAggro(0.0f), _reduceRatioSecond(0.0f), _reduceValueSecond(0.0f), _reduceType(DISABLED), _id(id) {
32  }
33 
34  Entry(const Entry &other) :
35  _aggro(other._aggro), _minAggro(other._minAggro), _reduceRatioSecond(other._reduceRatioSecond), _reduceValueSecond(other._reduceValueSecond), _reduceType(
36  other._reduceType), _id(other._id) {
37  }
38 
39  Entry(Entry &&other) :
40  _aggro(other._aggro), _minAggro(other._minAggro), _reduceRatioSecond(other._reduceRatioSecond), _reduceValueSecond(other._reduceValueSecond), _reduceType(
41  other._reduceType), _id(other._id) {
42  }
43 
44  float getAggro() const;
45  void addAggro(float aggro);
46  void setReduceByRatio(float reductionRatioPerSecond, float minimumAggro);
47  void setReduceByValue(float reductionValuePerSecond);
51  bool reduceByTime(int64_t millis);
52  void resetAggro();
53 
54  const CharacterId& getCharacterId() const;
55  bool operator <(Entry& other) const;
56  Entry& operator=(const Entry& other);
57 };
58 
59 typedef Entry* EntryPtr;
60 
61 inline void Entry::addAggro(float aggro) {
62  _aggro += aggro;
63 }
64 
65 inline void Entry::setReduceByRatio(float reduceRatioSecond, float minAggro) {
66  _reduceType = RATIO;
67  _reduceRatioSecond = reduceRatioSecond;
68  _minAggro = minAggro;
69 }
70 
71 inline void Entry::setReduceByValue(float reduceValueSecond) {
72  _reduceType = VALUE;
73  _reduceValueSecond = reduceValueSecond;
74 }
75 
76 inline bool Entry::reduceByTime(int64_t millis) {
77  switch (_reduceType) {
78  case RATIO: {
79  const float f = static_cast<float>(millis) / 1000.0f;
80  reduceByRatio(f * _reduceRatioSecond);
81  return true;
82  }
83  case VALUE: {
84  const float f = static_cast<float>(millis) / 1000.0f;
85  reduceByValue(f * _reduceValueSecond);
86  return true;
87  }
88  case DISABLED:
89  break;
90  }
91  return false;
92 }
93 
94 inline void Entry::reduceByRatio(float ratio) {
95  _aggro *= (1.0f - ratio);
96  if (_aggro < _minAggro) {
97  _aggro = 0.0f;
98  }
99 }
100 
101 inline void Entry::reduceByValue(float value) {
102  _aggro -= value;
103 
104  if (_aggro < 0.000001f) {
105  _aggro = 0.0f;
106  }
107 }
108 
109 inline float Entry::getAggro() const {
110  return _aggro;
111 }
112 
113 inline void Entry::resetAggro() {
114  _aggro = 0.0f;
115 }
116 
117 inline bool Entry::operator <(Entry& other) const {
118  return _aggro < other._aggro;
119 }
120 
121 inline Entry& Entry::operator=(const Entry& other) {
122  _aggro = other._aggro;
123  _minAggro = other._minAggro;
124  _reduceRatioSecond = other._reduceRatioSecond;
125  _reduceValueSecond = other._reduceValueSecond;
126  _reduceType = other._reduceType;
127  _id = other._id;
128  return *this;
129 }
130 
131 inline const CharacterId& Entry::getCharacterId() const {
132  return _id;
133 }
134 
135 }
bool reduceByTime(int64_t millis)
Definition: Entry.h:76
One entry for the AggroMgr.
Definition: Entry.h:17