forked from lichuang/AOI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.cpp
329 lines (302 loc) · 9.49 KB
/
scene.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "scene.h"
Scene::Scene() {
}
Scene::~Scene() {
ObjMap::iterator iter, tmp;
for (iter = obj_set_.begin(); iter != obj_set_.end(); ) {
tmp = iter;
++iter;
delete tmp->second;
}
}
void Scene::Update(Object *object) {
ObjMap::iterator iter;
// send ENTER msg
for (iter = enter_set_.begin(); iter != enter_set_.end(); ++iter) {
printf("send [%d, %d:%d]\t ENTER msg to obj [%d, %d:%d]\n",
object->id, object->x, object->y,
iter->first, iter->second->x, iter->second->y);
printf("send [%d, %d:%d]\t ENTER msg to obj [%d, %d:%d]\n",
iter->first, iter->second->x, iter->second->y,
object->id, object->x, object->y);
}
// send MOVE msg
for (iter = move_set_.begin(); iter != move_set_.end(); ++iter) {
printf("send [%d, %d:%d]\t MOVE msg to obj [%d, %d:%d]\n",
object->id, object->x, object->y,
iter->first, iter->second->x, iter->second->y);
}
// send LEAVE msg
for (iter = leave_set_.begin(); iter != leave_set_.end(); ++iter) {
printf("send [%d, %d:%d]\t LEAVE msg to obj [%d, %d:%d]\n",
object->id, object->x, object->y,
iter->first, iter->second->x, iter->second->y);
}
// clean the move, leave, enter set
enter_set_.clear();
move_set_.clear();
leave_set_.clear();
}
void Scene::Add(int id, int x, int y, int distance) {
if (obj_set_.find(id) != obj_set_.end()) {
return;
}
Object *object = new Object(id, x, y, distance);
obj_set_[object->id] = object;
ObjList::iterator iter, pos;
bool flag;
ObjMap x_set;
// iterator x-axis object list
flag = false;
for (iter = obj_x_list_.begin(); iter != obj_x_list_.end(); ++iter) {
int diff = (*iter)->x - object->x;
// into the x set
if (abs(diff) <= distance) {
x_set[(*iter)->id] = *iter;
}
if (!flag && diff > 0) {
pos = iter;
flag = true;
}
if (diff > distance) {
break;
}
}
if (flag) {
obj_x_list_.insert(pos, object);
object->x_pos = --pos;
} else {
obj_x_list_.push_front(object);
object->x_pos = obj_x_list_.begin();
}
// iterator y-axis object list
flag = false;
for (iter = obj_y_list_.begin(); iter != obj_y_list_.end(); ++iter) {
int diff = (*iter)->y - object->y;
// into the enter set
if (abs(diff) <= distance && x_set.find((*iter)->id) != x_set.end()) {
enter_set_[(*iter)->id] = *iter;
}
if (!flag && diff > 0) {
pos = iter;
flag = true;
}
if (diff > distance) {
break;
}
}
if (flag) {
obj_y_list_.insert(pos, object);
object->y_pos = --pos;
} else {
obj_y_list_.push_front(object);
object->y_pos = obj_y_list_.begin();
}
Update(object);
}
void Scene::UpdateObjectPosition(Object *object, int x, int y) {
int old_x = object->x;
int old_y = object->y;
object->x = x;
object->y = y;
ObjList::iterator iter, pos;
// find the new x pos
if (x > old_x) {
if (object->x_pos != obj_x_list_.end()) {
iter = object->x_pos;
++iter;
obj_x_list_.erase(object->x_pos);
while (iter != obj_x_list_.end()) {
if (object->x - (*iter)->x < 0) {
pos = iter;
break;
}
++iter;
}
if (iter != obj_x_list_.end()) {
obj_x_list_.insert(pos, object);
object->x_pos = --pos;
} else {
obj_x_list_.push_back(object);
object->x_pos = --obj_x_list_.end();
}
}
} else if (x < old_x) {
if (object->x_pos != obj_x_list_.begin()) {
iter = object->x_pos;
--iter;
obj_x_list_.erase(object->x_pos);
while (iter != obj_x_list_.begin()) {
if (object->x - (*iter)->x > 0) {
pos = ++iter;
break;
}
--iter;
}
if (iter != obj_x_list_.begin()) {
obj_x_list_.insert(pos, object);
object->x_pos = --pos;
} else {
obj_x_list_.push_front(object);
object->x_pos = obj_x_list_.begin();
}
}
}
// find the new y pos
if (y > old_y) {
if (object->y_pos != obj_y_list_.end()) {
iter = object->y_pos;
++iter;
obj_y_list_.erase(object->y_pos);
while (iter != obj_y_list_.end()) {
if (object->y - (*iter)->y < 0) {
pos = iter;
break;
}
++iter;
}
if (iter != obj_y_list_.end()) {
obj_y_list_.insert(pos, object);
object->y_pos = --pos;
} else {
obj_y_list_.push_back(object);
object->y_pos = --obj_y_list_.end();
}
}
} else if (y < old_y) {
if (object->y_pos != obj_y_list_.begin()) {
iter = object->y_pos;
--iter;
obj_y_list_.erase(object->y_pos);
while (iter != obj_y_list_.begin()) {
if (object->y - (*iter)->y > 0) {
pos = ++iter;
break;
}
--iter;
}
if (iter != obj_y_list_.begin()) {
obj_y_list_.insert(pos, object);
object->y_pos = --pos;
} else {
obj_y_list_.push_front(object);
object->y_pos = obj_y_list_.begin();
}
}
}
}
void Scene::Move(int id, int x, int y) {
ObjMap::iterator obj_iter = obj_set_.find(id);
if (obj_iter == obj_set_.end()) {
return;
}
Object *object = obj_iter->second;
ObjList::iterator list_iter;
ObjMap old_set, new_set;
// get the old set
GetRangeSet(object, &old_set);
// update object position
UpdateObjectPosition(object, x, y);
// get the new set
GetRangeSet(object, &new_set);
// move_set = old_set MIX new_set
ObjMap::iterator iter;
for (iter = old_set.begin(); iter != old_set.end(); ++iter) {
if (new_set.find(iter->first) != new_set.end()) {
move_set_[iter->first] = iter->second;
}
}
// leave_set = old_set SUB move_set
for (iter = old_set.begin(); iter != old_set.end(); ++iter) {
if (move_set_.find(iter->first) == move_set_.end()) {
leave_set_[iter->first] = iter->second;
}
}
// enter_set = new_set SUB move_set
for (iter = new_set.begin(); iter != new_set.end(); ++iter) {
if (move_set_.find(iter->first) == move_set_.end()) {
enter_set_[iter->first] = iter->second;
}
}
Update(object);
}
void Scene::GetRangeSet(Object *object, ObjMap *set) {
ObjMap x_set;
ObjList::iterator iter;
int distance = object->radius;
// iterator x-axis object list
if (object->x_pos != obj_x_list_.end()) {
iter = object->x_pos;
while (1) {
++iter;
if (iter == obj_x_list_.end()) {
break;
}
if (object->x - (*iter)->x > distance) {
break;
}
x_set[(*iter)->id] = *iter;
}
}
if (object->x_pos != obj_x_list_.begin()) {
iter = object->x_pos;
while (1) {
--iter;
if ((*iter)->x - object->x > distance) {
break;
}
x_set[(*iter)->id] = *iter;
if (iter == obj_x_list_.begin()) {
break;
}
}
}
// iterator y-axis object list
if (object->y_pos != obj_y_list_.end()) {
iter = object->y_pos;
while (1) {
++iter;
if (iter == obj_y_list_.end()) {
break;
}
if (object->y - (*iter)->y > distance) {
break;
}
if (x_set.find((*iter)->id) != x_set.end()) {
(*set)[(*iter)->id] = *iter;
}
}
}
if (object->y_pos != obj_y_list_.begin()) {
iter = object->y_pos;
while (1) {
--iter;
if ((*iter)->y - object->y > distance) {
break;
}
if (x_set.find((*iter)->id) != x_set.end()) {
(*set)[(*iter)->id] = *iter;
}
if (iter == obj_y_list_.begin()) {
break;
}
}
}
}
void Scene::Leave(int id) {
ObjMap::iterator obj_iter = obj_set_.find(id);
if (obj_iter == obj_set_.end()) {
return;
}
Object *object = obj_iter->second;
// get the leave set
GetRangeSet(object, &leave_set_);
Update(object);
obj_x_list_.erase(object->x_pos);
obj_y_list_.erase(object->y_pos);
obj_set_.erase(object->id);
delete object;
}