forked from bhhbazinga/AOI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoi.h
207 lines (171 loc) · 5.32 KB
/
aoi.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef AOI_H
#define AOI_H
#include <cassert>
#include <functional>
#include <unordered_map>
#include <unordered_set>
class AOI {
public:
struct Unit;
typedef int UnitID;
typedef std::unordered_set<Unit*> UnitSet;
typedef std::unordered_map<int, Unit*> UnitMap;
struct Unit {
Unit(UnitID id_, float x_, float y_) : id(id_), x(x_), y(y_) {}
~Unit(){};
void Subscribe(const Unit* other) {
subscribe_set.insert(const_cast<Unit*>(other));
}
void UnSubscribe(const Unit* other) {
subscribe_set.erase(const_cast<Unit*>(other));
}
UnitID id;
float x;
float y;
UnitSet subscribe_set;
};
public:
typedef std::function<void(int, int)> Callback;
AOI(float width, float height, float visible_range, Callback enter_callback,
Callback leave_callback)
: width_(width),
height_(height),
visible_range_(visible_range),
enter_callback_(enter_callback),
leave_callback_(leave_callback) {
assert(width_ >= 0);
assert(height_ >= 0);
assert(visible_range >= 0);
assert(nullptr != enter_callback);
assert(nullptr != leave_callback);
}
virtual ~AOI(){};
AOI(const AOI&) = delete;
AOI(AOI&&) = delete;
AOI& operator=(const AOI&) = delete;
AOI& operator=(AOI&&) = delete;
// Update when unit moved
// id is a custom integer
virtual void UpdateUnit(UnitID id, float x, float y) = 0;
// Remove unit from AOI
// id is a custom integer
virtual void RemoveUnit(UnitID id) = 0;
// Add unit to AOI
// id is a custom integer
virtual void AddUnit(UnitID id, float x, float y) = 0;
// Find units in range near the given id, and exclude id itself
std::unordered_set<int> FindNearbyUnit(UnitID id, float range) const {
Unit* unit = get_unit(id);
UnitSet unit_set = FindNearbyUnit(unit, range);
std::unordered_set<int> id_set;
for (const auto& unit : unit_set) {
id_set.insert(unit->id);
}
return id_set;
};
// Find units in the subscribe set of given id
std::unordered_set<int> GetSubScribeSet(UnitID id) const {
Unit* unit = get_unit(id);
UnitSet subscribe_set = unit->subscribe_set;
std::unordered_set<int> id_set;
for (const auto& unit : subscribe_set) {
id_set.insert(unit->id);
}
return id_set;
};
const float& get_width() const { return width_; }
const float& get_height() const { return height_; }
protected:
// Find units in the given range
virtual UnitSet FindNearbyUnit(const Unit* unit, float range) const = 0;
virtual Unit* NewUnit(UnitID id, float x, float y) = 0;
virtual void DeleteUnit(Unit* unit) = 0;
void ValidatePosition(float x, float y) {
assert(x <= width_ && y <= height_);
}
void ValidatetUnitID(UnitID id) {
assert(unit_map_.find(id) == unit_map_.end());
}
Unit* get_unit(UnitID id) const {
Unit* unit = unit_map_[id];
assert(nullptr != unit);
return unit;
}
float get_visible_range() const { return visible_range_; }
const std::unordered_map<int, Unit*>& get_unit_map() const {
return unit_map_;
}
std::vector<UnitID> get_unit_ids() const {
const UnitMap& unit_map = get_unit_map();
std::vector<UnitID> unit_ids(unit_map.size());
int i = 0;
for (const auto& pair : unit_map) {
unit_ids[i++] = pair.first;
}
return unit_ids;
}
UnitSet Intersection(const UnitSet& set, const UnitSet& other) const {
UnitSet res;
for (const auto& unit : set) {
if (other.find(unit) != other.end()) {
res.insert(unit);
}
}
return res;
}
UnitSet Difference(const UnitSet& set, const UnitSet& other) const {
UnitSet res;
for (const auto& unit : set) {
if (other.find(unit) == other.end()) {
res.insert(unit);
}
}
return res;
}
void NotifyEnter(Unit* unit, const UnitSet& enter_set) const {
for (const auto& other : enter_set) {
enter_callback_(other->id, unit->id);
other->Subscribe(unit);
enter_callback_(unit->id, other->id);
unit->Subscribe(other);
}
}
void NotifyLeave(Unit* unit, const UnitSet& leave_set) const {
for (const auto& other : leave_set) {
leave_callback_(other->id, unit->id);
other->UnSubscribe(unit);
leave_callback_(unit->id, other->id);
unit->UnSubscribe(other);
}
}
void OnAddUnit(Unit* unit) {
unit_map_.insert(std::pair(unit->id, unit));
UnitSet enter_set = FindNearbyUnit(unit, visible_range_);
NotifyEnter(unit, enter_set);
unit->subscribe_set = std::move(enter_set);
}
void OnUpdateUnit(Unit* unit) {
const UnitSet& old_set = unit->subscribe_set;
UnitSet new_set = FindNearbyUnit(static_cast<Unit*>(unit), visible_range_);
UnitSet move_set = Intersection(old_set, new_set);
UnitSet enter_set = Difference(new_set, move_set);
UnitSet leave_set = Difference(old_set, new_set);
unit->subscribe_set = std::move(new_set);
NotifyEnter(static_cast<Unit*>(unit), enter_set);
NotifyLeave(static_cast<Unit*>(unit), leave_set);
}
void OnRemoveUnit(Unit* unit) {
const UnitSet& subscribe_set = unit->subscribe_set;
NotifyLeave(unit, UnitSet(subscribe_set));
unit_map_.erase(unit->id);
DeleteUnit(unit);
}
private:
float width_;
float height_;
float visible_range_;
mutable UnitMap unit_map_;
Callback enter_callback_;
Callback leave_callback_;
};
#endif // AOI_H