forked from JerryZhou/aoi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
navi.c
1342 lines (1139 loc) · 41.8 KB
/
navi.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
Navigation Mesh In Game Developing
Copyright [email protected]
Licence: Apache 2.0
Project: https://github.com/JerryZhou/aoi.git
Purpose: Resolve the Navigation Problem In Game Developing
with high run fps
with minimal memory cost,
Please see examples for more details.
*/
#include "navi.h"
/* log some runtime */
#define __iopen_log_cell_add (1)
#define __iopen_log_cell_find (1)
#define __iopen_log_cell_del (1)
/* Max Path Finder Heap Depth */
#define KMAX_HEAP_DEPTH 64
/*************************************************************/
/* helper - coordinate system */
/*************************************************************/
/* change the pos3d to pos2d */
ipos _inavi_flat_pos(const ipos3 *p) {
ipos pos;
pos.x = p->x;
pos.y = p->z;
return pos;
}
/*************************************************************/
/* iheap - inavinode */
/*************************************************************/
/* declare meta for inavinode */
iideclareregister(inavinode);
/* navigation node in path*/
typedef struct inavinode {
irefdeclare;
/* cost */
ireal cost;
/* cell of this node */
inavicell *cell;
/* cell of connection to next */
inavicellconnection *connection;
} inavinode;
/* Compare the heap node with cost*/
static int _ientry_heap_node_cmp(iarray *arr, int i, int j) {
inavinode* lfs = iarrayof(arr, inavinode*, i);
inavinode* rfs = iarrayof(arr, inavinode*, j);
return lfs->cost < rfs->cost;
}
/* trace the cell index in heap */
static void _inode_trace_cell_heap_index(iarray *arr, iref *ref, int index) {
inavinode *node;
icheck(ref);
node = icast(inavinode, ref);
node->cell->heap_index = index;
}
/* def the entry for ref */
static const irefarrayentry _refarray_entry_inavinode = {
_inode_trace_cell_heap_index,
};
/* Make a Navi Nodes Heap with Cost Order Desc */
iheap* inavinodeheapmake() {
/*make heap with capacity: MAX_HEAP_DEPTH*/
iheap *heap = iarraymakeirefwithentry(KMAX_HEAP_DEPTH, &_refarray_entry_inavinode);
/*node cmp entry */
heap->cmp = _ientry_heap_node_cmp;
/* return heap*/
return heap;
}
/*************************************************************/
/* inavimap */
/*************************************************************/
/* Build all navi cells from blocks */
static void _inavimap_build_cells(inavimap *map, size_t width, size_t height, ireal * heightmap, ireal block) {
}
/* Release all the resouces hold by navi map */
static void _inavimap_entry_free(iref *ref) {
inavimap *map = icast(inavimap, ref);
iarrayfree(map->cells);
iarrayfree(map->polygons);
iarrayfree(map->connections);
iobjfree(map);
}
static void _ipolygon_edge(ipolygon3d *polygon, iline2d *edge, int index) {
edge->start = ipolygon3dposxz(polygon, index);
edge->end = ipolygon3dposxz(polygon, index+1);
}
/* Make connection */
inavicellconnection * inavicellconnectionmake() {
inavicellconnection *con = iobjmalloc(inavicellconnection);
/* no need descrtuor*/
iretain(con);
return con;
}
/* Release connection */
void inavicellconnectionfree(inavicellconnection* con) {
irelease(con);
}
/* release all the resource hold by cell */
static void _inavicell_entry_free(iref *ref) {
inavicell *cell = icast(inavicell, ref);
/* clear the neighbors */
ineighborsclean(icast(irefneighbors, cell));
/* release the polygons */
ipolygon3dfree(cell->polygon);
/* releaset the connections */
iarrayfree(cell->connections);
/* release the cell link from */
irelease(cell->link);
/* release the cell connection from */
irelease(cell->connection);
/* release the cell units */
inavicellunlinkaoi(cell);
iarrayfree(cell->aoi_cellunits);
iobjfree(cell);
}
/* Make a cell with poly and connections */
inavicell *inavicellmake(struct inavimap* map, ipolygon3d *poly, islice* connections, islice *costs) {
inavicell * cell = iobjmalloc(inavicell);
size_t len = imin(islicelen(connections), islicelen(costs));
size_t n = 0;
int next;
/* descructor */
cell->free = _inavicell_entry_free;
/*add poly and cell to map*/
iarrayadd(map->polygons, &poly);
iarrayadd(map->cells, &cell);
/* poly */
iassign(cell->polygon, poly);
/* connections */
cell->connections = iarraymakeint(islicelen(connections));
for (n=0; n < len; ++n) {
next = isliceof(connections, int, n);
/*if invalid connection */
if (next == kindex_invalid) {
continue;
}
/* add connection */
inavicelladdconnection(cell, map, n, next, isliceof(costs, ireal, n));
}
/* the cell units */
cell->aoi_cellunits = iarraymakeiref(4);
iretain(cell);
return cell;
}
/* add connection to cell */
void inavicelladdconnection(inavicell *cell, struct inavimap *map, int edge, int next, ireal cost) {
inavicellconnection * connection;
int append;
icheck(edge>=0);
/*make connection*/
connection = inavicellconnectionmake();
connection->index = edge;
connection->cost = cost;
connection->middle = ipolygon3dedgecenter(cell->polygon, edge);
connection->start = *ipolygon3dpos3(cell->polygon, edge);
connection->end = *ipolygon3dpos3(cell->polygon, edge+1);
connection->next = next;
connection->from = cell->cell_index;
/* add to map */
iarrayadd(map->connections, &connection);
if (edge<iarraylen(cell->connections)) {
/* set connection */
iarrayset(cell->connections, edge, &connection->location);
} else if (edge >= iarraylen(cell->connections)) {
/*fill the invalid*/
for (append = iarraylen(cell->connections); append<edge; ++append) {
iarrayadd(cell->connections, &kindex_invalid);
}
/* add to cell */
iarrayadd(cell->connections, &connection->location);
} else {
}
inavicellconnectionfree(connection);
}
/* Connect the cell to map */
void inavicellconnecttomap(inavicell *cell, struct inavimap* map) {
size_t len = iarraylen(cell->connections);
int conindex=0;
inavicellconnection *con=NULL;
inavicell *neighbor=NULL;
const void* convalue;
/*if not a new cell should disconnect first caller self */
while (len) {
/* get the cell connection */
conindex = iarrayof(cell->connections, int, len-1);
convalue = iarrayat(map->connections, conindex);
/* connected as neighbor */
if (convalue && (con = ((inavicellconnection**)(convalue))[0], con)) {
neighbor = iarrayof(map->cells, inavicell*, con->next);
/* add neighbor and append con as link resouce */
ineighborsaddvalue(icast(irefneighbors,cell),
icast(irefneighbors,neighbor), con, con);
}
--len;
}
}
/* Disconnect the cell to map */
void inavicelldisconnectfrommap(inavicell *cell) {
ineighborsclean(icast(irefneighbors, cell));
}
/* Release the cell */
void inavicellfree(inavicell *cell) {
irelease(cell);
}
/* Fetch the height from cell to pos */
int inavicellmapheight(inavicell *cell, ipos3 *pos) {
pos->y = iplanesolvefory(&cell->polygon->plane, pos->x, pos->z);
return iiok;
}
/* Release the relation with aoi */
void inavicellunaoi(inavicell *cell, imap *aoimap) {
iunit *u;
size_t size = iarraylen(cell->aoi_cellunits);
#if __iopen_log_cell_del
ilog("[INavi Cell-UnLink] ##Begin## cell:"__icell_format"\n", __icell_value(*cell));
#endif
for (; size; --size) {
u = iarrayof(cell->aoi_cellunits, iunit*, size-1);
#if __iopen_log_cell_del
ilog("[INavi Cell-UnLink] Unmapping from node:"__inode_format"\n", __inode_value(*aoimap, *u->node));
#endif
imapremoveunit(aoimap, u);
}
iarrayremoveall(cell->aoi_cellunits);
#if __iopen_log_cell_del
ilog("[INavi Cell-UnLink] ##End## cell:"__icell_format"\n", __icell_value(*cell));
#endif
}
/* Just Single Release the relation with aoi */
void inavicellunlinkaoi(inavicell *cell) {
iunit *u;
size_t size = iarraylen(cell->aoi_cellunits);
for (; size; --size) {
u = iarrayof(cell->aoi_cellunits, iunit*, size-1);
u->userdata.up1 = NULL;
u->flag &= ~EnumNaviUnitFlag_Cell;
}
}
/* classify the line relationship with cell */
int inavicellclassify(inavicell *cell, const iline2d *line,
ipos *intersection, int *connection) {
int interiorcount = 0;
int relation = EnumNaviCellRelation_OutCell;
iline2d edge;
int edgecount = islicelen(cell->polygon->pos);
int n = 0;
int linerelation;
while (n < edgecount) {
_ipolygon_edge(cell->polygon, &edge, n);
if (iline2dclassifypoint(&edge, &line->end, iepsilon) != EnumPointClass_Right) {
if (iline2dclassifypoint(&edge, &line->start, iepsilon) != EnumPointClass_Left) {
linerelation = iline2dintersection(&edge, line, intersection);
if ( linerelation== EnumLineClass_Segments_Intersect) {
/* if segement is cross by line edge,
should be some edge cross the edge interset with edge too */
/*|| linerelation == EnumLineClass_A_Bisects_B ){ */
relation = EnumNaviCellRelation_IntersetCell;
/* Find Connections */
if (connection ) {
if (n < iarraylen(cell->connections)) {
*connection = iarrayof(cell->connections, int, n);
} else {
*connection = kindex_invalid;
}
}
break;
}
}
}else {
interiorcount++;
}
++n;
}
/* all right */
if (interiorcount == edgecount) {
relation = EnumNaviCellRelation_InCell;
}
return relation;
}
/*************************************************************/
/* inavipathspeedup */
/*************************************************************/
/* free the speedup */
static void _inavipathspeedup_entry_free(iref *ref) {
inavipathspeedup *speed = icast(inavipathspeedup, ref);
irelease(speed->path);
iobjfree(speed);
}
/*make one*/
inavipathspeedup* inavipathspeedupmake() {
inavipathspeedup *speed = iobjmalloc(inavipathspeedup);
speed->free = _inavipathspeedup_entry_free;
iretain(speed);
return speed;
}
/*free it*/
void inavipathspeedupfree(inavipathspeedup *speed) {
irelease(speed);
}
/* release the resource hold by desc */
void inavimapdescfreeresource(inavimapdesc *desc) {
iarrayfree(desc->points); desc->points = NULL;
iarrayfree(desc->polygons); desc->polygons = NULL;
iarrayfree(desc->polygonsindex); desc->polygonsindex = NULL;
iarrayfree(desc->polygonsconnection); desc->polygonsconnection = NULL;
iarrayfree(desc->polygonscosts); desc->polygonscosts = NULL;
}
char * _file_read(const char *file) {
FILE *fp = NULL;
char *fcontent = NULL;
long size;
fp = fopen(file, "r");
if (fp) {
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
fcontent = (char*)malloc(size+1);
fread(fcontent, 1, size, fp);
fclose(fp);
fcontent[size] = 0;
}
return fcontent;
}
typedef enum EnumErrCode {
EnumErrCode_WrongHeaderFormat,
EnumErrCode_WrongPointFormat,
EnumErrCode_WrongPolygonFormat,
}EnumErrCode;
/* read the navimap from textfile */
int inavimapdescreadfromtextfile(inavimapdesc *desc, const char* file) {
FILE *fp = NULL;
int n = 0;
int m, k, i, j, c = 0;
ireal cost;
int err = 0;
ipos3 p;
fp = fopen(file, "r");
inavimapdescfreeresource(desc);
if (fp) {
while (true) {
/*read header */
n = fscanf(fp, "Map: width %lf height %lf points %ld polygons %ld polygonsize %ld\n",
&desc->header.size.w,
&desc->header.size.h,
&desc->header.points,
&desc->header.polygons,
&desc->header.polygonsize);
if (n != 5) {
err = EnumErrCode_WrongHeaderFormat; /* header format wrong */
break;
}
/*read the points */
desc->points = iarraymakeipos3(desc->header.points);
for (m=0; m <desc->header.points; ++m) {
if (m%6==0) {
fscanf(fp, "\n");
}
n = fscanf(fp, "(%lf,%lf,%lf)", &p.x, &p.y, &p.z);
if (n !=3) {
err = EnumErrCode_WrongPointFormat;
break;
} else {
iarrayadd(desc->points, &p);
}
}
if (err != 0) {
break;
}
fscanf(fp, "\n");
/* read the polygons */
desc->polygons = iarraymakeint(desc->header.polygons);
desc->polygonsindex = iarraymakeint(desc->header.polygonsize);
desc->polygonsconnection = iarraymakeint(desc->header.polygonsize);
desc->polygonscosts = iarraymakeireal(desc->header.polygonsize);
for (m=0; m<desc->header.polygons; ++m) {
n = fscanf(fp, "%d:", &k);
if (n != 1) {
err = EnumErrCode_WrongPolygonFormat;
break;
}
iarrayadd(desc->polygons, &k);
for (i=0; i<k; ++i) {
if (i == k-1) {
n = fscanf(fp, "(%d,%d,%lf)\n", &j, &c, &cost);
} else {
n = fscanf(fp, "(%d,%d,%lf) ", &j, &c, &cost);
}
if (n != 3) {
err = EnumErrCode_WrongPolygonFormat;
break;
} else {
iarrayadd(desc->polygonsindex, &j);
iarrayadd(desc->polygonsconnection, &c);
iarrayadd(desc->polygonscosts, &cost);
}
}
if (err != 0) {
break;
}
}
break;
}
}
return err;
}
/* write the navimap to textfile */
void inavimapdescwritetotextfile(inavimapdesc *desc, const char* file) {
}
/* trace the cell index */
static void _irefarray_cell_index_change(iarray *arr, iref *ref, int index) {
inavicell *cell = icast(inavicell, ref);
icheck(cell);
cell->cell_index = index;
}
/* cell array entry */
static const irefarrayentry _irefarray_entry_inavicell = {
_irefarray_cell_index_change,
};
/* trace the connection location in array */
static void _irefarray_connection_index_change(iarray *arr, iref *ref, int index) {
inavicellconnection *con = icast(inavicellconnection, ref);
icheck(con);
con->location = index;
}
/* cell connection entry */
static const irefarrayentry _irefarray_entry_inavicellconnection = {
_irefarray_connection_index_change,
};
/* Make navimap from the blocks */
inavimap* inavimapmake(size_t capacity){
inavimap *map = iobjmalloc(inavimap);
map->free = _inavimap_entry_free;
map->cells = iarraymakeirefwithentry(capacity, &_irefarray_entry_inavicell);
map->polygons = iarraymakeiref(capacity);
map->connections = iarraymakeirefwithentry(capacity*4, &_irefarray_entry_inavicellconnection);
iretain(map);
return map;
}
/* load the navimap from heightmap */
void inavimapload(inavimap *map, size_t width, size_t height, ireal *heightmap, ireal block) {
/*Build the Cells */
_inavimap_build_cells(map, width, height, heightmap, block);
}
/* load the navimap from desc */
void inavimaploadfromdesc(inavimap *map, const inavimapdesc *desc) {
int i = 0;
int j = 0;
size_t len = 0;
size_t lentotal = 0;
islice *sliceindex = NULL;
islice *sliceconnection = NULL;
islice *slicecosts = NULL;
inavicell *cell;
ipolygon3d *polygon;
/* remove all old cells */
iarrayremoveall(map->polygons);
iarrayremoveall(map->cells);
iarrayremoveall(map->connections);
/* make sure the capacity of polygons */
iarrayexpandcapacity(map->polygons, desc->header.polygons);
iarrayexpandcapacity(map->cells, desc->header.polygons);
/* make all the cells */
for (i=0, lentotal=0; i<desc->header.polygons; ++i) {
len = iarrayof(desc->polygons, int, i);
sliceindex = isliced(desc->polygonsindex, lentotal, lentotal+len);
sliceconnection = isliced(desc->polygonsconnection, lentotal, lentotal+len);
slicecosts = isliced(desc->polygonscosts, lentotal, lentotal+len);
/* make polygon */
polygon = ipolygon3dmake(len);
for (j=0; j<len; ++j) {
ipolygon3dadd(polygon , &iarrayof(desc->points, ipos3,
isliceof(sliceindex, int, j)),
1);
}
/* make cell */
cell = inavicellmake(map, polygon, sliceconnection, slicecosts);
/* release the middle values */
islicefree(sliceindex);
islicefree(sliceconnection);
islicefree(slicecosts);
ipolygon3dfree(polygon);
inavicellfree(cell);
/* next cell */
lentotal += len;
}
/* connected all the cells: then make all connections in map */
for (i=0; i<iarraylen(map->cells); ++i) {
inavicellconnecttomap(iarrayof(map->cells, inavicell*, i), map);
}
}
/* Free the navi map */
void inavimapfree(inavimap *map) {
/*disconnect all the navicell*/
irange(map->cells, inavicell*,
inavicelldisconnectfrommap(__value);
);
/*free all the resources*/
iarrayremoveall(map->cells);
iarrayremoveall(map->polygons);
iarrayremoveall(map->connections);
/*release the free */
irelease(map);
}
/* Navi map find the cell
* Should be carefully deal with profile */
inavicell* inavimapfind(const inavimap *map, const ipos3 *pos) {
islice *cells = islicemakearg(map->cells, ":");
inavicell * cell = inavimapfindclosestcell(map, cells, pos);
islicefree(cells);
return cell;
}
ireal _inavicell_pos_dist(inavicell *cell, const ipos3 *pos ) {
ipos intersection = {0, 0};
ipos start = {cell->polygon->center.x, cell->polygon->center.z};
ipos end = {pos->x, pos->z};
iline2d line = {start, end};
ipos3 closest = {0, 0, 0};
ireal dist = INT32_MAX;
if (inavicellclassify(cell, &line, &intersection, NULL) == EnumNaviCellRelation_IntersetCell) {
closest.x = intersection.x;
closest.z = intersection.y;
inavicellmapheight(cell, &closest);
dist = sqrtf(idistancepow3(pos, &closest));
}
return dist;
}
/* navi map find the cell */
inavicell* inavimapfindclosestcell(const inavimap *map, const islice* cells, const ipos3 *pos) {
inavicell *cell = NULL;
ireal maxdistance = INT32_MAX;
inavicell *closestcell = NULL;
size_t len = islicelen(cells);
int found = iino;
ipos3 newpos;
ireal dist;
while (len) {
cell = isliceof(cells, inavicell*, len-1);
if (ipolygon3dincollum(cell->polygon, pos) == iiok) {
/*found it, try closest, may be cell overlap in y value for 3d space*/
newpos = *pos;
inavicellmapheight(cell, &newpos);
dist = fabs(newpos.y - pos->y);
found = iiok;
if (dist < maxdistance) {
closestcell = cell;
maxdistance = dist;
}
} else if(found == iino) {
/*no found , find a closest cell in dist*/
dist = _inavicell_pos_dist(cell, pos);
if (cell == NULL || dist < maxdistance) {
closestcell = cell;
maxdistance = dist;
}
}
--len;
}
return closestcell;
}
/* Navigation Context*/
typedef struct inavicontext {
int64_t sessionid;
iheap *heap;
inavipath *path;
iunit *unit;
inavimap *map;
}inavicontext;
/* make a heuristic */
static ireal _inavicontext_heuristic(inavicontext *context, inavicell *cell) {
return idistancepow3(&cell->polygon->center, &context->path->endpos);
}
static void _inavinode_entry_free(iref *ref) {
inavinode *node = icast(inavinode, ref);
irelease(node->cell);
iobjfree(node);
}
/* Make a node by cell, NB!! not retain */
static inavinode * _inavicontext_makenode(inavicontext *context, inavicell *cell) {
inavinode *node = iobjmalloc(inavinode);
node->free = _inavinode_entry_free;
iassign(node->cell, cell);
node->cost = cell->costarrival + cell->costheuristic;
return node;
}
/* Add cell to heap or update the node by cell in heap*/
static void _inavicontext_heap_cell(inavicontext *context, inavicell *cell) {
inavinode *node;
if (cell->heap_index != kindex_invalid) {
/* update the cost of node */
node = iarrayof(context->heap, inavinode*, cell->heap_index);
node->cost = cell->costarrival + cell->costheuristic;
iheapadjust(context->heap, cell->heap_index);
} else {
node = _inavicontext_makenode(context, cell);
iheapadd(context->heap, &node);
}
}
/* setup the path */
void inavipathsetup(inavipath *path, int64_t sessionid,
inavicell *start, const ipos3 *startpos,
inavicell *end, const ipos3 *endpos) {
path->sessionid = sessionid;
iassign(path->start, start);
path->startpos = *startpos;
iassign(path->end, end);
path->endpos = *endpos;
ireflistremoveall(path->waypoints);
path->current = NULL;
}
static void _inavipath_entry_free(iref *ref) {
inavipath *path = icast(inavipath, ref);
irelease(path->start);
irelease(path->end);
ireflistfree(path->waypoints);
iobjfree(path);
}
inavipath *inavipathmake() {
inavipath *path = iobjmalloc(inavipath);
path->free = _inavipath_entry_free;
path->waypoints = ireflistmake();
iretain(path);
return path;
}
/* navigation path free */
void inavipathfree(inavipath *path) {
irelease(path);
}
ipos3 _ipolygon_point(ipolygon3d *polygon, int index) {
size_t len = islicelen(polygon->pos);
icheckret(len>0, kipos3_zero);
return isliceof(polygon->pos, ipos3, index%len);
}
/*find out the closest point in edge in 3d */
ipos3 _inavicellconnection_closest(const inavicellconnection *connection, const ipos3 *pos) {
iline3d edge = {connection->start, connection->end};
return iline3dclosestpoint(&edge, pos, iepsilon);
}
/*free the waypoint*/
static void _inaviwaypoint_entry_free(iref *ref) {
inaviwaypoint *waypoint = icast(inaviwaypoint, ref);
irelease(waypoint->cell);
irelease(waypoint->connection);
iobjfree(waypoint);
}
/* NB!! no retain */
static inaviwaypoint *_inaviwaypoint_make() {
inaviwaypoint *waypoint = iobjmalloc(inaviwaypoint);
waypoint->free = _inaviwaypoint_entry_free;
return waypoint;
}
/* NB!! no retain */
static inaviwaypoint *_inaviwaypoint_make_by_cell(inavipath *path, inavicell *cell) {
inaviwaypoint *waypoint = _inaviwaypoint_make();
waypoint->type = EnumNaviWayPointType_Cell;
iassign(waypoint->cell, cell);
waypoint->waypoint = cell->polygon->center;
return waypoint;
}
/* NB!! no retain */
static inaviwaypoint *_inaviwaypoint_make_by_connection(inavipath *path, inavicell *cell, inavicellconnection *connection) {
inaviwaypoint *waypoint = _inaviwaypoint_make();
inaviwaypoint *last;
waypoint->type = EnumNaviWayPointType_Connection;
iassign(waypoint->cell, cell);
iassign(waypoint->connection, connection);
if ( iiwaypoint_connection_cloest && ireflistlen(path->waypoints)) {
last = icast(inaviwaypoint, ireflistfirst(path->waypoints)->value);
waypoint->waypoint = _inavicellconnection_closest(connection, &last->waypoint);
} else {
waypoint->waypoint = connection->middle;
}
return waypoint;
}
/* NB!! no retain */
static inaviwaypoint *_inaviwaypoint_make_by_goal(inavipath *path, inavicell *cell, ipos3 *goal) {
inaviwaypoint *waypoint = _inaviwaypoint_make();
waypoint->type = EnumNaviWayPointType_Cell_Goal;
iassign(waypoint->cell, cell);
waypoint->waypoint = *goal;
return waypoint;
}
void _inavipath_begin(inavipath *path, inavicell *end) {
inaviwaypoint *waypoint;
/* we found it */
if (end == path->end) {
waypoint = _inaviwaypoint_make_by_goal(path, end, &path->endpos);
waypoint->flag |= EnumNaviWayPointFlag_End;
ireflistadd(path->waypoints, irefcast(waypoint));
} else {
/*should insert a dynamic waypoint */
waypoint = _inaviwaypoint_make_by_goal(path, path->end, &path->endpos);
waypoint->flag |= EnumNaviWayPointFlag_Dynamic;
ireflistadd(path->waypoints, irefcast(waypoint));
/*the nearest end way point */
waypoint = _inaviwaypoint_make_by_cell(path, end);
ireflistadd(path->waypoints, irefcast(waypoint));
}
}
void _inavipath_end(inavipath *path, inavicell* cell, inavicellconnection *connection) {
inaviwaypoint *waypoint;
/* should have connection */
icheck(connection);
if (cell == path->start) {
/* connection */
waypoint = _inaviwaypoint_make_by_connection(path, cell, connection);
ireflistadd(path->waypoints, irefcast(waypoint));
} else {
/*may happend some ugly*/
}
}
/* caculate the right cost */
static ireal _inavicell_arrivalcost(inavicell *cell, inavicontext *context,
inavinode *caller, inavicellconnection *connection) {
ireal distmiddle = 0; ireal disttake = 0; ireal distnow = 0;
ireal factor = 1.0;
icheckret(caller && connection, 0);
/* caculate the dist */
if (caller->cell == context->path->start) {
distmiddle = idistancepow3(&cell->polygon->center, &connection->middle);
disttake = distmiddle + idistancepow3(&connection->middle, &caller->cell->polygon->center);
distnow = distmiddle + idistancepow3(&connection->middle, &context->path->startpos);
} else if (cell == context->path->end) {
distmiddle = idistancepow3(&caller->cell->polygon->center, &connection->middle);
disttake = distmiddle + idistancepow3(&connection->middle, &cell->polygon->center);
distnow = distmiddle + idistancepow3(&connection->middle, &context->path->endpos);
}
/*there are some offset need to caculate the*/
if (!ireal_equal(disttake, distnow) && !ireal_equal_zero(disttake)) {
factor = distnow / disttake;
}
/* if we carefuly deal with cost , can be do some extermely moving stuffs*/
/* if we do not want it just open the next line code */
/* return caller->cell->costarrival + distnow; */
return caller->cell->costarrival + connection->cost * factor;
}
static void _inavicell_process(inavicell *cell, inavicontext *context,
inavinode *caller, inavicellconnection *connection) {
if (cell->sessionid != context->sessionid) {
cell->sessionid = context->sessionid;
cell->flag = EnumNaviCellFlag_Open;
iwassign(cell->link, caller ? caller->cell:NULL);
iwassign(cell->connection, connection);
cell->heap_index = kindex_invalid;
cell->costarrival = _inavicell_arrivalcost(cell, context, caller, connection);
cell->costheuristic = _inavicontext_heuristic(context, cell);
/* add cell to heap */
_inavicontext_heap_cell(context, cell);
} else if(cell->flag == EnumNaviCellFlag_Open) {
iwassign(cell->link, caller->cell);
iwassign(cell->connection, connection);
cell->costarrival = _inavicell_arrivalcost(cell, context, caller, connection);
cell->costheuristic = _inavicontext_heuristic(context, cell);
/* adjust cell in heap */
_inavicontext_heap_cell(context, cell);
}
}
/* Process the node with neighbors */
static void _inavinode_process(inavinode *node, inavicontext *context) {
irefjoint *joint;
inavicell *cell;
inavicellconnection *connection;
/* mark close */
node->cell->flag = EnumNaviCellFlag_Close;
/* all of children */
joint = ireflistfirst(node->cell->neighbors_to);
while (joint) {
cell = icast(inavicell, joint->value);
connection = icast(inavicellconnection, joint->res);
/* process cell */
_inavicell_process(cell, context, node, connection);
/* next */
joint = joint->next;
}
}
static void _inavicontext_setup(inavicontext *context,
inavimap *map, iunit *unit,
inavipath *path) {
context->map = map;
context->sessionid = path->sessionid;
context->unit = unit;
context->path = path;
context->heap = inavinodeheapmake();
/* add start cell to heap */
_inavicell_process(path->start, context, NULL, NULL);
}
static void _inavicontext_free(inavicontext *context) {
irelease(context->heap);
}
/* A* */
static void _inavimapfindpath_cell(inavimap *map,
iunit *unit,
inavipath *path,
int maxstep) {
inavinode * node = NULL;
inavicell * cell = NULL;
inavicellconnection * connection = NULL;
inaviwaypoint * waypoint = NULL;
inavicell * end = path->end;
inavicontext context = {0};
int found = iino;
int steps = maxstep;
_inavicontext_setup(&context, map, unit, path);
/* found the path */
while (iheapsize(context.heap) && found == iino && steps) {
/*get the top node */
node = iheappeekof(context.heap, inavinode*);
iretain(node);
/*pop heap*/
iheappop(context.heap);
/* Arrived */
if (node->cell == end) {
found = true;
} else {
/* Process current neighbor */
_inavinode_process(node, &context);
}
/* release node */
irelease(node);
--steps;
}
/* found a nearest reaching path */
if (steps == 0 && found == iino) {
end = node->cell;
found = iiok;
}
/* found path */
if (found == iiok) {
/* path begin */
_inavipath_begin(path, end);
/* reverse insert the waypoint to list */
connection = icast(inavicellconnection, iwrefunsafestrong(end->connection));
cell = icast(inavicell, iwrefunsafestrong(end->link));
while (cell && cell != path->start) {
/* connection */
waypoint = _inaviwaypoint_make_by_connection(path, cell, connection);
ireflistadd(path->waypoints, irefcast(waypoint));
#if iiwaypoint_cell
/* cell */
waypoint = _inaviwaypoint_make_by_cell(path, cell);
ireflistadd(path->waypoints, irefcast(waypoint));
#endif
/* get next point */
connection = icast(inavicellconnection, iwrefunsafestrong(cell->connection));
cell = icast(inavicell, iwrefunsafestrong(cell->link));
}
/* end of path */
_inavipath_end(path, cell, connection);
}
/* free the navigation context */
_inavicontext_free(&context);