-
Notifications
You must be signed in to change notification settings - Fork 3
/
aoi.c
424 lines (400 loc) · 10.6 KB
/
aoi.c
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "aoi.h"
/* 创建Units对象 */
struct Units* aoi_units_create(void) {
struct Units* units = allocate(sizeof(struct Units));
if (!units)
return NULL;
units->next = NULL;
units->x = 0; units->y = 0; units->id = 0;
return units;
}
/* 销毁Units对象 */
void aoi_units_destory(struct Units* units) {
while (1) {
struct Units* next = units->next;
release(units);
if (!next)
break;
units = next;
}
}
static inline void aoi_node_destory(struct Aoi* aoi) {
struct DoubleLink* node = aoi->ylist;
aoi->ylist = NULL;
aoi->xlist = NULL;
while (node) {
struct DoubleLink* next = node->nexty;
release(node);
if (!next)
break;
node = next;
}
return ;
}
static inline uint32_t aoi_node_range(struct Aoi* aoi, struct DoubleLink* node, struct Units** units, uint32_t radius) {
uint32_t quantity = 0;
struct DoubleLink* prev = node->prevx;
struct DoubleLink* next = node->nextx;
int32_t maxY = node->y + radius;
int32_t minY = node->y - radius < 0 ? 0 : node->y - radius;
int32_t maxX = node->x + radius;
int32_t minX = node->x - radius < 0 ? 0 : node->x - radius;
*units = NULL;
struct Units** list;
while (prev) {
if (prev->x < minX)
break;
if (prev->y >= minY && prev->y <= maxY){
if (!quantity){
*units = aoi_units_create();
if (!*units)
return quantity;
(*units)->x = prev->x; (*units)->y = prev->y; (*units)->id = prev->id;
list = &((*units)->next);
quantity++;
} else {
*list = aoi_units_create();
if (!list)
return quantity;
(*list)->x = prev->x; (*list)->y = prev->y; (*list)->id = prev->id;
list = &((*list)->next);
quantity++;
}
}
prev = prev->prevx;
}
while (next) {
if (next->x > maxX)
break;
if (next->y >= minY && next->y <= maxY){
if (!quantity){
*units = aoi_units_create();
if (!*units)
return quantity;
(*units)->x = next->x; (*units)->y = next->y; (*units)->id = next->id;
list = &((*units)->next);
quantity++;
} else {
*list = aoi_units_create();
if (!list)
return quantity;
(*list)->x = next->x; (*list)->y = next->y; (*list)->id = next->id;
list = &((*list)->next);
quantity++;
}
}
next = next->nextx;
}
return quantity;
}
static inline void aoi_node_remove_x(struct Aoi* aoi, struct DoubleLink* node) {
/* 删除思路: (A <-> B <-> C) To (A < - > C) 并做好空指针判断. */
struct DoubleLink* prev = node->prevx;
struct DoubleLink* next = node->nextx;
if (prev) {
prev->nextx = next;
if (next)
next->prevx = prev;
} else {
aoi->xlist = next;
if (next)
next->prevx = NULL;
}
}
static inline void aoi_node_remove_y(struct Aoi* aoi, struct DoubleLink* node) {
/* 删除思路: (A <-> B <-> C) To (A < - > C) 并做好空指针判断. */
struct DoubleLink* prev = node->prevy;
struct DoubleLink* next = node->nexty;
if (prev) {
prev->nexty = next;
if (next)
next->prevy = prev;
} else {
aoi->ylist = next;
if (next)
next->prevy = NULL;
}
}
static inline struct DoubleLink* aoi_node_remove(struct Aoi* aoi, struct DoubleLink* node) {
aoi_node_remove_x(aoi, node);
aoi_node_remove_y(aoi, node);
return node;
}
static inline void aoi_node_insert_x(struct Aoi* aoi, struct DoubleLink* node) {
struct DoubleLink *xnode = aoi->xlist;
if (!xnode) {
aoi->xlist = node;
return ;
}
while (1) {
if (xnode->x < node->x) {
if (xnode->nextx) {
xnode = xnode->nextx;
continue;
}
xnode->nextx = node;
node->prevx = xnode;
return;
}
node->nextx = xnode;
node->prevx = xnode->prevx;
if (xnode->prevx) {
xnode->prevx = node;
node->prevx->nextx = node;
return ;
}
aoi->xlist = node;
xnode->prevx = node;
return ;
}
}
static inline void aoi_node_insert_y(struct Aoi* aoi, struct DoubleLink* node) {
struct DoubleLink *ynode = aoi->ylist;
if (!ynode) {
aoi->ylist = node;
return ;
}
while (1) {
if (ynode->y < node->y) {
if (ynode->nexty) {
ynode = ynode->nexty;
continue;
}
ynode->nexty = node;
node->prevy = ynode;
return;
}
node->nexty = ynode;
node->prevy = ynode->prevy;
if (ynode->prevy) {
ynode->prevy = node;
node->prevy->nexty = node;
return ;
}
aoi->ylist = node;
ynode->prevy = node;
return ;
}
}
static inline struct DoubleLink* aoi_node_insert(struct Aoi* aoi, struct DoubleLink* node) {
node->prevx = NULL; node->nextx = NULL;
node->prevy = NULL; node->nexty = NULL;
aoi_node_insert_x(aoi, node);
aoi_node_insert_y(aoi, node);
return node;
}
static inline void aoi_node_remove_and_insert_x(struct Aoi* aoi, struct DoubleLink* node, uint32_t to_x) {
if (node->x == to_x)
return;
struct DoubleLink* prev = node->prevx;
struct DoubleLink* next = node->nextx;
if (!prev && !next){
node->x = to_x;
return;
}
/* 以下判断对象在链表中是否需要移动. */
if (next && next->x < to_x) {
struct DoubleLink *before = NULL;
aoi_node_remove_x(aoi, node);
node->x = to_x;
while (1) {
if (next) {
if (next->x > node->x) {
node->nextx = next;
node->prevx = next->prevx;
node->prevx->nextx = node->nextx->prevx = node;
return ;
}
}else{
node->prevx = before;
node->nextx = NULL;
before->nextx = node;
return ;
}
before = next;
next = next->nextx;
}
}
if (prev && prev->x > to_x) {
struct DoubleLink *before = NULL;
aoi_node_remove_x(aoi, node);
node->x = to_x;
while (1) {
if (prev) {
if (prev->x < node->x) {
node->prevx = prev;
node->nextx = prev->nextx;
node->prevx->nextx = node->nextx->prevx = node;
return ;
}
}else{
node->nextx = before;
before->prevx = node;
aoi->xlist = node;
return;
}
before = prev;
prev = prev->prevx;
}
}
node->x = to_x;
return;
}
static inline void aoi_node_remove_and_insert_y(struct Aoi* aoi, struct DoubleLink* node, uint32_t to_y) {
if (node->y == to_y)
return;
struct DoubleLink* prev = node->prevy;
struct DoubleLink* next = node->nexty;
if (!prev && !next){
node->y = to_y;
return;
}
if (next && next->y < to_y) {
struct DoubleLink *before = NULL;
aoi_node_remove_y(aoi, node);
node->y = to_y;
while (1) {
if (next) {
if (next->y > node->y) {
node->nexty = next;
node->prevy = next->prevy;
node->prevy->nexty = node->nexty->prevy = node;
return ;
}
}else{
node->prevy = before;
node->nexty = NULL;
before->nexty = node;
return ;
}
before = next;
next = next->nexty;
}
}
if (prev && prev->y > to_y) {
struct DoubleLink *before = NULL;
aoi_node_remove_y(aoi, node);
node->y = to_y;
while (1) {
if (prev) {
if (prev->y < node->y) {
node->prevy = prev;
node->nexty = prev->nexty;
node->prevy->nexty = node->nexty->prevy = node;
return ;
}
}else{
node->nexty = before;
before->prevy = node;
aoi->ylist = node;
return;
}
before = prev;
prev = prev->prevy;
}
}
node->y = to_y;
return;
}
static inline uint32_t aoi_node_position_difference(uint32_t old_x, uint32_t new_x, uint32_t old_y, uint32_t new_y) {
uint32_t X = 0;
uint32_t Y = 0;
if (old_x > new_x)
X = old_x - new_x;
else
X = new_x - old_x;
if (old_y > new_y)
Y = old_y - new_y;
else
Y = new_y - old_y;
return X > Y ? X : Y;
}
static inline uint32_t aoi_node_move(struct Aoi* aoi, struct DoubleLink* node, uint32_t to_x, uint32_t to_y, struct Units** units, uint32_t* radius) {
uint32_t quantity = 0;
uint32_t old_x = node->x, old_y = node->y;
aoi_node_remove_and_insert_x(aoi, node, to_x);
aoi_node_remove_and_insert_y(aoi, node, to_y);
if (units && radius) {
uint32_t offset = *radius + aoi_node_position_difference(old_x, to_x, old_y, to_y);
quantity += aoi_node_range(aoi, node, units, offset);
}
return quantity;
}
/* 移动 */
int32_t aoi_move(struct Aoi* aoi, struct Unit* unit, uint32_t to_x, uint32_t to_y, struct Units** units, uint32_t* radius) {
if (aoi_maybe_not(!aoi))
return -1;
if (aoi_maybe_not(!unit))
return -2;
if (aoi_maybe_not(to_x > aoi->x))
return -3;
if (aoi_maybe_not(to_y > aoi->y))
return -4;
/* 位置相同什么都不做 */
if (unit->x == to_x && unit->y == to_y)
return 0;
return aoi_node_move(aoi, (struct DoubleLink*)unit, to_x, to_y, units, radius);
}
/* 离开 */
int32_t aoi_leave(struct Aoi* aoi, struct Unit* unit, struct Units** units, uint32_t* radius) {
if (aoi_maybe_not(!aoi))
return -1;
if (aoi_maybe_not(!unit))
return -2;
struct DoubleLink *node = (struct DoubleLink *)unit;
aoi_node_remove(aoi, node);
uint32_t quantity = 0;
if (units)
if (radius)
quantity += aoi_node_range(aoi, node, units, *radius);
node->prevx = NULL; node->nextx = NULL;
node->prevy = NULL; node->nexty = NULL;
return quantity;
}
/* 进入 */
int32_t aoi_enter(struct Aoi* aoi, struct Unit* unit, struct Units** units, uint32_t* radius) {
if (aoi_maybe_not(!aoi))
return -1;
if (aoi_maybe_not(!unit))
return -2;
if (aoi_maybe_not(unit->x > aoi->x))
return -3;
if (aoi_maybe_not(unit->y > aoi->y))
return -4;
aoi_node_insert(aoi, (struct DoubleLink*)unit);
uint32_t quantity = 0;
if (units)
if (radius)
quantity += aoi_node_range(aoi, (struct DoubleLink *)unit, units, *radius);
return quantity;
}
/* 创建aoi对象 */
struct Aoi* aoi_create(uint32_t x, uint32_t y) {
struct Aoi* aoi = allocate(sizeof(struct Aoi));
if (!aoi)
return NULL;
aoi->x = x, aoi->y = y;
aoi->xlist = NULL; aoi->ylist = NULL;
return aoi;
}
/* 销毁aoi对象 */
void aoi_destory(struct Aoi* aoi) {
aoi_node_destory(aoi);
aoi->x = 0; aoi->y = 0;
release(aoi);
return;
}
/* 创建unit对象 */
struct Unit* aoi_unit_create(int64_t id, uint32_t x, uint32_t y) {
struct DoubleLink* node = allocate(sizeof(struct DoubleLink));
if (!node)
return NULL;
node->id = id; node->x = x; node->y = y;
node->prevx = NULL; node->nextx = NULL;
node->prevy = NULL; node->nexty = NULL;
return (struct Unit *)node;
}
/* 销毁unit对象 */
void aoi_unit_destory(struct Unit* unit) {
release((struct DoubleLink *)unit);
}