1 //#define PATHLIB_RDFIELDS
2 #ifdef PATHLIB_RDFIELDS
3 #define path_next swampslug
4 #define path_prev lasertarget
10 #define medium spawnshieldtime
12 //#define DEBUGPATHING
18 .float pathlib_node_g;
19 .float pathlib_node_h;
20 .float pathlib_node_f;
22 float pathlib_open_cnt;
23 float pathlib_closed_cnt;
24 float pathlib_made_cnt;
25 float pathlib_merge_cnt;
26 float pathlib_recycle_cnt;
27 float pathlib_searched_cnt;
33 float pathlib_bestopen_seached;
34 float pathlib_bestcash_hits;
35 float pathlib_bestcash_saved;
37 float pathlib_gridsize;
39 float pathlib_movecost;
40 float pathlib_movecost_diag;
41 float pathlib_movecost_waterfactor;
43 float pathlib_edge_check_size;
45 float pathlib_foundgoal;
48 entity best_open_node;
53 float edge_show(vector point,float fsize);
54 void mark_error(vector where,float lifetime);
55 void mark_info(vector where,float lifetime);
56 entity mark_misc(vector where,float lifetime);
58 void pathlib_showpath(entity start)
64 te_lightning1(e,e.origin,e.path_next.origin);
71 pathlib_showpath(self);
72 self.nextthink = time + 1;
75 void __showpath2_think()
77 mark_info(self.origin,1);
80 self.path_next.think = __showpath2_think;
81 self.path_next.nextthink = time + 0.15;
85 self.owner.think = __showpath2_think;
86 self.owner.nextthink = time + 0.15;
90 void pathlib_showpath2(entity path)
92 path.think = __showpath2_think;
93 path.nextthink = time;
98 void pathlib_deletepath(entity start)
102 e = findchainentity(owner, start);
105 e.think = SUB_Remove;
111 float fsnap(float val,float fsize)
113 return rint(val / fsize) * fsize;
116 vector vsnap(vector point,float fsize)
120 vret_x = rint(point_x / fsize) * fsize;
121 vret_y = rint(point_y / fsize) * fsize;
122 vret_z = ceil(point_z / fsize) * fsize;
127 float walknode_stepsize;
128 vector walknode_stepup;
129 vector walknode_maxdrop;
130 vector walknode_boxup;
131 vector walknode_boxmax;
132 vector walknode_boxmin;
133 float pathlib_movenode_goodnode;
135 float floor_ok(vector point)
139 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
142 pc = pointcontents(point);
152 if not (pointcontents(point - '0 0 1') == CONTENT_SOLID)
158 if(pointcontents(point - '0 0 1') == CONTENT_SOLID)
164 #define inwater(point) (pointcontents(point) == CONTENT_WATER)
166 float inwater(vector point)
168 if(pointcontents(point) == CONTENT_WATER)
174 #define _pcheck(p) traceline(p+z_up,p-z_down,MOVE_WORLDONLY,self); if not(floor_ok(trace_endpos)) return 1
175 float edge_check(vector point,float fsize)
179 z_up = '0 0 1' * fsize;
180 z_down = '0 0 1' * fsize;
182 _pcheck(point + ('1 1 0' * fsize));
183 _pcheck(point + ('1 -1 0' * fsize));
184 _pcheck(point + ('1 0 0' * fsize));
186 _pcheck(point + ('0 1 0' * fsize));
187 _pcheck(point + ('0 -1 0' * fsize));
189 _pcheck(point + ('-1 0 0' * fsize));
190 _pcheck(point + ('-1 1 0' * fsize));
191 _pcheck(point + ('-1 -1 0' * fsize));
197 #define _pshow(p) mark_error(p,10)
198 float edge_show(vector point,float fsize)
201 _pshow(point + ('1 1 0' * fsize));
202 _pshow(point + ('1 -1 0' * fsize));
203 _pshow(point + ('1 0 0' * fsize));
205 _pshow(point + ('0 1 0' * fsize));
206 _pshow(point + ('0 -1 0' * fsize));
208 _pshow(point + ('-1 0 0' * fsize));
209 _pshow(point + ('-1 1 0' * fsize));
210 _pshow(point + ('-1 -1 0' * fsize));
216 var vector pathlib_movenode(vector start,vector end,float doedge);
217 vector pathlib_wateroutnode(vector start,vector end)
221 pathlib_movenode_goodnode = 0;
223 end_x = fsnap(end_x, pathlib_gridsize);
224 end_y = fsnap(end_y, pathlib_gridsize);
226 traceline(end + ('0 0 0.25' * pathlib_gridsize),end - ('0 0 1' * pathlib_gridsize),MOVE_WORLDONLY,self);
229 if not(pointcontents(end - '0 0 1') == CONTENT_SOLID)
232 for(surface = start ; surface_z < (end_z + 32); ++surface_z)
234 if(pointcontents(surface) == CONTENT_EMPTY)
238 if(pointcontents(surface + '0 0 1') != CONTENT_EMPTY)
241 tracebox(start + '0 0 64', walknode_boxmin,walknode_boxmax, end + '0 0 64', MOVE_WORLDONLY, self);
242 if(trace_fraction == 1)
243 pathlib_movenode_goodnode = 1;
245 if(fabs(surface_z - end_z) > 32)
246 pathlib_movenode_goodnode = 0;
251 vector pathlib_swimnode(vector start,vector end)
253 pathlib_movenode_goodnode = 0;
255 if(pointcontents(start) != CONTENT_WATER)
258 end_x = fsnap(end_x, pathlib_gridsize);
259 end_y = fsnap(end_y, pathlib_gridsize);
261 if(pointcontents(end) == CONTENT_EMPTY)
262 return pathlib_wateroutnode( start, end);
264 tracebox(start, walknode_boxmin,walknode_boxmax, end, MOVE_WORLDONLY, self);
265 if(trace_fraction == 1)
266 pathlib_movenode_goodnode = 1;
271 vector pathlib_flynode(vector start,vector end)
273 pathlib_movenode_goodnode = 0;
275 end_x = fsnap(end_x, pathlib_gridsize);
276 end_y = fsnap(end_y, pathlib_gridsize);
278 tracebox(start, walknode_boxmin,walknode_boxmax, end, MOVE_WORLDONLY, self);
279 if(trace_fraction == 1)
280 pathlib_movenode_goodnode = 1;
285 vector pathlib_walknode(vector start,vector end,float doedge)
287 vector direction,point,last_point,s,e;
288 float steps, distance, i,laststep;
290 pathlib_movenode_goodnode = 0;
296 direction = normalize(s - e);
298 distance = vlen(start - end);
299 laststep = distance / walknode_stepsize;
300 steps = floor(laststep);
301 laststep = laststep - steps;
304 s = point + walknode_stepup;
305 e = point - walknode_maxdrop;
307 traceline(s, e,MOVE_WORLDONLY,self);
308 if(trace_fraction == 1.0)
311 if (floor_ok(trace_endpos) == 0)
314 last_point = trace_endpos;
316 for(i = 0; i < steps; ++i)
318 point = last_point + direction * walknode_stepsize;
320 s = point + walknode_stepup;
321 e = point - walknode_maxdrop;
322 traceline(s, e,MOVE_WORLDONLY,self);
323 if(trace_fraction == 1.0)
326 point = trace_endpos;
327 if not(floor_ok(trace_endpos))
330 tracebox(last_point + walknode_boxup, walknode_boxmin,walknode_boxmax, point + walknode_boxup, MOVE_WORLDONLY, self);
331 if(trace_fraction != 1.0)
335 if(edge_check(point,pathlib_edge_check_size))
341 point = last_point + direction * walknode_stepsize * laststep;
343 point_x = fsnap(point_x, pathlib_gridsize);
344 point_y = fsnap(point_y, pathlib_gridsize);
346 s = point + walknode_stepup;
347 e = point - walknode_maxdrop;
348 traceline(s, e,MOVE_WORLDONLY,self);
350 if(trace_fraction == 1.0)
353 point = trace_endpos;
355 if not(floor_ok(trace_endpos))
358 tracebox(last_point + walknode_boxup, walknode_boxmin,walknode_boxmax, point + walknode_boxup, MOVE_WORLDONLY, self);
359 if(trace_fraction != 1.0)
362 pathlib_movenode_goodnode = 1;
366 var float pathlib_cost(entity parent,vector to, float static_cost);
367 float pathlib_g_static(entity parent,vector to, float static_cost)
370 return parent.pathlib_node_g + static_cost * pathlib_movecost_waterfactor;
372 return parent.pathlib_node_g + static_cost;
375 float pathlib_g_static_water(entity parent,vector to, float static_cost)
378 return parent.pathlib_node_g + static_cost * pathlib_movecost_waterfactor;
380 return parent.pathlib_node_g + static_cost;
383 float pathlib_g_euclidean(entity parent,vector to, float static_cost)
385 return parent.pathlib_node_g + vlen(parent.origin - to);
387 float pathlib_g_euclidean_water(entity parent,vector to, float static_cost)
390 return parent.pathlib_node_g + vlen(parent.origin - to) * pathlib_movecost_waterfactor;
392 return parent.pathlib_node_g + vlen(parent.origin - to);
395 var float(vector from,vector to) pathlib_heuristic;
398 Manhattan Menas we expect to move up,down left or right
399 No diagonal moves espected. (like moving bewteen city blocks)
401 float pathlib_h_manhattan(vector a,vector b)
403 //h(n) = D * (abs(n.x-goal.x) + abs(n.y-goal.y))
407 h += fabs(a_y - b_y);
408 h *= pathlib_gridsize;
414 This heuristic consider both stright and disagonal moves
415 to have teh same cost.
417 float pathlib_h_diagonal(vector a,vector b)
419 //h(n) = D * max(abs(n.x-goal.x), abs(n.y-goal.y))
424 h = pathlib_movecost * max(x,y);
430 This heuristic only considers the stright line distance.
431 Will usualy mean a lower H then G meaning A* Will speand more
434 float pathlib_h_euclidean(vector a,vector b)
440 This heuristic consider both stright and disagonal moves,
441 But has a separate cost for diagonal moves.
443 float pathlib_h_diagonal2(vector a,vector b)
445 float h_diag,h_str,h,x,y;
448 h_diagonal(n) = min(abs(n.x-goal.x), abs(n.y-goal.y))
449 h_straight(n) = (abs(n.x-goal.x) + abs(n.y-goal.y))
450 h(n) = D2 * h_diagonal(n) + D * (h_straight(n) - 2*h_diagonal(n)))
459 h = pathlib_movecost_diag * h_diag;
460 h += pathlib_movecost * (h_str - 2 * h_diag);
466 This heuristic consider both stright and disagonal moves,
467 But has a separate cost for diagonal moves.
471 float pathlib_h_diagonal2sdp(vector preprev,vector prev,vector point,vector end)
473 float h_diag,h_str,h,x,y,z;
475 //h_diagonal(n) = min(abs(n.x-goal.x), abs(n.y-goal.y))
476 //h_straight(n) = (abs(n.x-goal.x) + abs(n.y-goal.y))
477 //h(n) = D2 * h_diagonal(n) + D * (h_straight(n) - 2*h_diagonal(n)))
479 x = fabs(point_x - end_x);
480 y = fabs(point_y - end_y);
481 z = fabs(point_z - end_z);
483 h_diag = min3(x,y,z);
486 h = pathlib_movecost_diag * h_diag;
487 h += pathlib_movecost * (h_str - 2 * h_diag);
492 d1 = normalize(preprev - point);
493 d2 = normalize(prev - point);
495 //bprint("pathlib_h_diagonal2sdp-M = ",ftos(m),"\n");
501 float pathlib_h_diagonal3(vector a,vector b)
503 float h_diag,h_str,h,x,y,z;
505 //h_diagonal(n) = min(abs(n.x-goal.x), abs(n.y-goal.y))
506 //h_straight(n) = (abs(n.x-goal.x) + abs(n.y-goal.y))
507 //h(n) = D2 * h_diagonal(n) + D * (h_straight(n) - 2*h_diagonal(n)))
513 h_diag = min3(x,y,z);
516 h = pathlib_movecost_diag * h_diag;
517 h += pathlib_movecost * (h_str - 2 * h_diag);
522 //#define PATHLIB_USE_NODESCRAP
523 #define PATHLIB_NODEEXPIRE 0.05
524 float pathlib_scraplist_cnt;
528 #ifdef PATHLIB_USE_NODESCRAP
529 if(pathlib_scraplist_cnt)
531 n = findentity(world,owner,scraplist);
534 --pathlib_scraplist_cnt;
535 ++pathlib_recycle_cnt;
539 pathlib_scraplist_cnt = 0;
544 #ifdef PATHLIB_NODEEXPIRE
545 n.think = SUB_Remove;
546 n.nextthink = time + PATHLIB_NODEEXPIRE;
550 void dumpnode(entity n)
552 #ifdef PATHLIB_USE_NODESCRAP
553 ++pathlib_scraplist_cnt;
557 n.is_path_node = FALSE;
560 //n.is_path_node = FALSE;
561 n.think = SUB_Remove;
566 entity pathlib_mknode(vector where,entity parent)
571 node.is_path_node = TRUE;
572 node.owner = openlist;
573 node.path_prev = parent;
575 setorigin(node, where);
579 node.medium = pointcontents(where);
584 var float pathlib_expandnode(entity node, vector start, vector goal);
585 float pathlib_expandnode_star(entity node, vector start, vector goal);
586 float pathlib_expandnode_box(entity node, vector start, vector goal);
588 var float pathlib_makenode(entity parent,vector start, vector to, vector goal,float cost);
589 float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost)
595 ++pathlib_searched_cnt;
597 if(inwater(parent.origin))
599 pathlib_expandnode = pathlib_expandnode_box;
600 pathlib_movenode = pathlib_swimnode;
606 pathlib_expandnode = pathlib_expandnode_box;
607 pathlib_movenode = pathlib_swimnode;
612 pathlib_expandnode = pathlib_expandnode_star;
613 pathlib_movenode = pathlib_walknode;
618 where = pathlib_movenode(parent.origin,to,0);
619 if not(pathlib_movenode_goodnode)
623 if(edge_check(where,pathlib_edge_check_size))
627 pathlib_h_diagonal2sdp(parent.path_prev.origin,parent.origin,where,goal);
629 h = pathlib_heuristic(where,goal);
630 g = pathlib_cost(parent,where,cost);
633 node = findradius(where,pathlib_gridsize * 0.75);
636 if(node.is_path_node == TRUE)
639 if(node.owner == openlist)
641 if(node.pathlib_node_g > g)
643 node.pathlib_node_h = h;
644 node.pathlib_node_g = g;
645 node.pathlib_node_f = f;
646 node.path_prev = parent;
649 if not (best_open_node)
650 best_open_node = node;
651 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
652 best_open_node = node;
660 node = pathlib_mknode(where,parent);
661 node.pathlib_node_h = h;
662 node.pathlib_node_g = g;
663 node.pathlib_node_f = f;
665 if not (best_open_node)
666 best_open_node = node;
667 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
668 best_open_node = node;
673 entity pathlib_getbestopen()
680 ++pathlib_bestcash_hits;
681 pathlib_bestcash_saved += pathlib_open_cnt;
683 return best_open_node;
686 node = findchainentity(owner,openlist);
693 ++pathlib_bestopen_seached;
694 if(node.pathlib_node_f < bestnode.pathlib_node_f)
703 void pathlib_close_node(entity node,vector goal)
706 if(node.owner == closedlist)
708 dprint("Pathlib: Tried to close a closed node!\n");
712 if(node == best_open_node)
713 best_open_node = world;
715 ++pathlib_closed_cnt;
718 node.owner = closedlist;
720 if(vlen(node.origin - goal) <= pathlib_gridsize)
724 goalmove = pathlib_walknode(node.origin,goal,1);
725 if(pathlib_movenode_goodnode)
728 pathlib_foundgoal = TRUE;
733 float pathlib_expandnode_star(entity node, vector start, vector goal)
745 point = where + v_forward * pathlib_gridsize;
746 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
749 point = where - v_forward * pathlib_gridsize;
750 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
753 point = where + v_right * pathlib_gridsize;
754 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
757 point = where - v_right * pathlib_gridsize;
758 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
761 point = where + v_forward * pathlib_gridsize + v_right * pathlib_gridsize;
762 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
765 point = where + v_forward * pathlib_gridsize - v_right * pathlib_gridsize;
766 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
769 point = where - v_forward * pathlib_gridsize + v_right * pathlib_gridsize;
770 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
773 point = where - v_forward * pathlib_gridsize - v_right * pathlib_gridsize;
774 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
776 return pathlib_open_cnt;
779 float pathlib_expandnode_box(entity node, vector start, vector goal)
783 for(v_z = node.origin_z - pathlib_gridsize; v_z <= node.origin_z + pathlib_gridsize; v_z += pathlib_gridsize)
784 for(v_y = node.origin_y - pathlib_gridsize; v_y <= node.origin_y + pathlib_gridsize; v_y += pathlib_gridsize)
785 for(v_x = node.origin_x - pathlib_gridsize; v_x <= node.origin_x + pathlib_gridsize; v_x += pathlib_gridsize)
787 if(vlen(v - node.origin))
788 pathlib_makenode(node,start,v,goal,pathlib_movecost);
791 return pathlib_open_cnt;
794 void pathlib_cleanup()
798 node = findfloat(world,is_path_node, TRUE);
802 node = findfloat(node,is_path_node, TRUE);
811 best_open_node = world;
816 var float buildpath_nodefilter(vector n,vector c,vector p);
817 float buildpath_nodefilter_directional(vector n,vector c,vector p)
821 d2 = normalize(p - c);
822 d1 = normalize(c - n);
824 if(vlen(d1-d2) < 0.25)
830 float buildpath_nodefilter_moveskip(vector n,vector c,vector p)
832 pathlib_walknode(p,n,1);
833 if(pathlib_movenode_goodnode)
839 entity path_build(entity next, vector where, entity prev, entity start)
844 if(buildpath_nodefilter)
845 if(buildpath_nodefilter(next.origin,where,prev.origin))
851 path.path_next = next;
853 setorigin(path,where);
856 path.classname = "path_end";
860 path.classname = "path_start";
862 path.classname = "path_node";
868 entity pathlib_astar(vector from,vector to)
870 entity path, start, end, open, n, ln;
871 float ptime, ftime, ctime;
873 ptime = gettime(GETTIME_REALTIME);
877 // Select water<->land capable node make/link
878 pathlib_makenode = pathlib_makenode_adaptive;
879 // Select XYZ cost estimate
880 pathlib_heuristic = pathlib_h_diagonal3;
881 // Select distance + waterfactor cost
882 pathlib_cost = pathlib_g_euclidean_water;
883 // Select star expander
884 pathlib_expandnode = pathlib_expandnode_star;
885 // Select walk simulation movement test
886 pathlib_movenode = pathlib_walknode;
887 // Filter final nodes by direction
888 buildpath_nodefilter = buildpath_nodefilter_directional;
890 // If the start is in water we need diffrent settings
893 // Select volumetric node expaner
894 pathlib_expandnode = pathlib_expandnode_box;
896 // Water movement test
897 pathlib_movenode = pathlib_swimnode;
904 closedlist = spawn();
909 pathlib_closed_cnt = 0;
910 pathlib_open_cnt = 0;
911 pathlib_made_cnt = 0;
912 pathlib_merge_cnt = 0;
913 pathlib_searched_cnt = 0;
914 pathlib_bestopen_seached = 0;
915 pathlib_bestcash_hits = 0;
916 pathlib_bestcash_saved = 0;
917 pathlib_recycle_cnt = 0;
919 pathlib_gridsize = 128;
920 pathlib_movecost = pathlib_gridsize;
921 pathlib_movecost_diag = vlen(('1 1 0' * pathlib_gridsize));
922 pathlib_movecost_waterfactor = 1.1;
923 pathlib_foundgoal = 0;
925 walknode_boxmax = self.maxs * 1.5;
926 walknode_boxmin = self.mins * 1.5;
928 pathlib_edge_check_size = (vlen(walknode_boxmin - walknode_boxmax) * 0.5);
930 walknode_boxup = '0 0 2' * self.maxs_z;
931 walknode_stepsize = 32;
932 walknode_stepup = '0 0 1' * walknode_stepsize;
933 walknode_maxdrop = '0 0 3' * walknode_stepsize;
935 from_x = fsnap(from_x,pathlib_gridsize);
936 from_y = fsnap(from_y,pathlib_gridsize);
938 to_x = fsnap(to_x,pathlib_gridsize);
939 to_y = fsnap(to_y,pathlib_gridsize);
941 dprint("AStar init. ", ftos(pathlib_scraplist_cnt), " nodes on scrap\n");
942 path = pathlib_mknode(from,world);
943 pathlib_close_node(path,to);
944 if(pathlib_foundgoal)
946 dprint("AStar: Goal found on first node!\n");
950 open.classname = "path_end";
951 setorigin(open,path.origin);
958 if(pathlib_expandnode(path,from,to) <= 0)
960 dprint("AStar path fail.\n");
966 best_open_node = pathlib_getbestopen();
968 pathlib_close_node(best_open_node,to);
969 if(inwater(n.origin))
970 pathlib_expandnode_box(n,from,to);
972 pathlib_expandnode_star(n,from,to);
974 while(pathlib_open_cnt)
976 best_open_node = pathlib_getbestopen();
978 pathlib_close_node(best_open_node,to);
980 if(inwater(n.origin))
981 pathlib_expandnode_box(n,from,to);
983 pathlib_expandnode(n,from,to);
985 if(pathlib_foundgoal)
987 dprint("Target found. Rebuilding and filtering path...\n");
988 ftime = gettime(GETTIME_REALTIME);
989 ptime = ftime - ptime;
991 start = path_build(world,path.origin,world,world);
992 end = path_build(world,goal_node.origin,world,start);
996 for(open = goal_node; open.path_prev != path; open = open.path_prev)
998 n = path_build(ln,open.origin,open.path_prev,start);
1002 start.path_next = n;
1003 n.path_prev = start;
1004 ftime = gettime(GETTIME_REALTIME) - ftime;
1006 ctime = gettime(GETTIME_REALTIME);
1008 ctime = gettime(GETTIME_REALTIME) - ctime;
1012 pathlib_showpath2(start);
1014 dprint("Time used - pathfinding: ", ftos(ptime),"\n");
1015 dprint("Time used - rebuild & filter: ", ftos(ftime),"\n");
1016 dprint("Time used - cleanup: ", ftos(ctime),"\n");
1017 dprint("Time used - total: ", ftos(ptime + ftime + ctime),"\n");
1018 dprint("Time used - # frames: ", ftos(ceil((ptime + ftime + ctime) / sys_frametime)),"\n\n");
1019 dprint("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
1020 dprint("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
1021 dprint("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
1022 dprint("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
1023 dprint("Nodes - searched: ", ftos(pathlib_searched_cnt),"\n");
1025 if(pathlib_recycle_cnt)
1026 dprint("Nodes - make/reuse: ", ftos(pathlib_made_cnt / pathlib_recycle_cnt),"\n");
1027 if(pathlib_recycle_cnt)
1028 dprint("Nodes - reused: ", ftos(pathlib_recycle_cnt),"\n");
1030 dprint("Nodes bestopen searched: ", ftos(pathlib_bestopen_seached),"\n");
1031 dprint("Nodes bestcash - hits: ", ftos(pathlib_bestcash_hits),"\n");
1032 dprint("Nodes bestcash - save: ", ftos(pathlib_bestcash_saved),"\n");
1033 dprint("AStar done. ", ftos(pathlib_scraplist_cnt), " nodes on scrap\n\n");
1039 dprint("A* Faild to find a path! Try a smaller gridsize.\n");