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(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 (!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,float doedge)
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(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,float doedge)
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 (!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 (!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;
607 pathlib_expandnode = pathlib_expandnode_box;
608 pathlib_movenode = pathlib_swimnode;
614 pathlib_expandnode = pathlib_expandnode_star;
615 pathlib_movenode = pathlib_walknode;
620 where = pathlib_movenode(parent.origin,to,0);
621 if (!pathlib_movenode_goodnode)
625 if(edge_check(where,pathlib_edge_check_size))
629 pathlib_h_diagonal2sdp(parent.path_prev.origin,parent.origin,where,goal);
631 h = pathlib_heuristic(where,goal);
632 g = pathlib_cost(parent,where,cost);
635 node = findradius(where,pathlib_gridsize * 0.75);
638 if(node.is_path_node == TRUE)
641 if(node.owner == openlist)
643 if(node.pathlib_node_g > g)
645 node.pathlib_node_h = h;
646 node.pathlib_node_g = g;
647 node.pathlib_node_f = f;
648 node.path_prev = parent;
652 best_open_node = node;
653 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
654 best_open_node = node;
662 node = pathlib_mknode(where,parent);
663 node.pathlib_node_h = h;
664 node.pathlib_node_g = g;
665 node.pathlib_node_f = f;
668 best_open_node = node;
669 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
670 best_open_node = node;
675 entity pathlib_getbestopen()
682 ++pathlib_bestcash_hits;
683 pathlib_bestcash_saved += pathlib_open_cnt;
685 return best_open_node;
688 node = findchainentity(owner,openlist);
695 ++pathlib_bestopen_seached;
696 if(node.pathlib_node_f < bestnode.pathlib_node_f)
705 void pathlib_close_node(entity node,vector goal)
708 if(node.owner == closedlist)
710 dprint("Pathlib: Tried to close a closed node!\n");
714 if(node == best_open_node)
715 best_open_node = world;
717 ++pathlib_closed_cnt;
720 node.owner = closedlist;
722 if(vlen(node.origin - goal) <= pathlib_gridsize)
726 goalmove = pathlib_walknode(node.origin,goal,1);
727 if(pathlib_movenode_goodnode)
730 pathlib_foundgoal = TRUE;
735 float pathlib_expandnode_star(entity node, vector start, vector goal)
747 point = where + v_forward * pathlib_gridsize;
748 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
751 point = where - v_forward * pathlib_gridsize;
752 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
755 point = where + v_right * pathlib_gridsize;
756 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
759 point = where - v_right * pathlib_gridsize;
760 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost);
763 point = where + v_forward * pathlib_gridsize + v_right * pathlib_gridsize;
764 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
767 point = where + v_forward * pathlib_gridsize - v_right * pathlib_gridsize;
768 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
771 point = where - v_forward * pathlib_gridsize + v_right * pathlib_gridsize;
772 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
775 point = where - v_forward * pathlib_gridsize - v_right * pathlib_gridsize;
776 nodecnt += pathlib_makenode(node,start,point,goal,pathlib_movecost_diag);
778 return pathlib_open_cnt;
781 float pathlib_expandnode_box(entity node, vector start, vector goal)
785 for(v_z = node.origin_z - pathlib_gridsize; v_z <= node.origin_z + pathlib_gridsize; v_z += pathlib_gridsize)
786 for(v_y = node.origin_y - pathlib_gridsize; v_y <= node.origin_y + pathlib_gridsize; v_y += pathlib_gridsize)
787 for(v_x = node.origin_x - pathlib_gridsize; v_x <= node.origin_x + pathlib_gridsize; v_x += pathlib_gridsize)
789 if(vlen(v - node.origin))
790 pathlib_makenode(node,start,v,goal,pathlib_movecost);
793 return pathlib_open_cnt;
796 void pathlib_cleanup()
800 node = findfloat(world,is_path_node, TRUE);
804 node = findfloat(node,is_path_node, TRUE);
813 best_open_node = world;
818 var float buildpath_nodefilter(vector n,vector c,vector p);
819 float buildpath_nodefilter_directional(vector n,vector c,vector p)
823 d2 = normalize(p - c);
824 d1 = normalize(c - n);
826 if(vlen(d1-d2) < 0.25)
832 float buildpath_nodefilter_moveskip(vector n,vector c,vector p)
834 pathlib_walknode(p,n,1);
835 if(pathlib_movenode_goodnode)
841 entity path_build(entity next, vector where, entity prev, entity start)
846 if(buildpath_nodefilter)
847 if(buildpath_nodefilter(next.origin,where,prev.origin))
853 path.path_next = next;
855 setorigin(path,where);
858 path.classname = "path_end";
862 path.classname = "path_start";
864 path.classname = "path_node";
870 entity pathlib_astar(vector from,vector to)
872 entity path, start, end, open, n, ln;
873 float ptime, ftime, ctime;
875 ptime = gettime(GETTIME_REALTIME);
879 // Select water<->land capable node make/link
880 pathlib_makenode = pathlib_makenode_adaptive;
881 // Select XYZ cost estimate
882 pathlib_heuristic = pathlib_h_diagonal3;
883 // Select distance + waterfactor cost
884 pathlib_cost = pathlib_g_euclidean_water;
885 // Select star expander
886 pathlib_expandnode = pathlib_expandnode_star;
887 // Select walk simulation movement test
888 pathlib_movenode = pathlib_walknode;
889 // Filter final nodes by direction
890 buildpath_nodefilter = buildpath_nodefilter_directional;
892 // If the start is in water we need diffrent settings
895 // Select volumetric node expaner
896 pathlib_expandnode = pathlib_expandnode_box;
898 // Water movement test
899 pathlib_movenode = pathlib_swimnode;
906 closedlist = spawn();
911 pathlib_closed_cnt = 0;
912 pathlib_open_cnt = 0;
913 pathlib_made_cnt = 0;
914 pathlib_merge_cnt = 0;
915 pathlib_searched_cnt = 0;
916 pathlib_bestopen_seached = 0;
917 pathlib_bestcash_hits = 0;
918 pathlib_bestcash_saved = 0;
919 pathlib_recycle_cnt = 0;
921 pathlib_gridsize = 128;
922 pathlib_movecost = pathlib_gridsize;
923 pathlib_movecost_diag = vlen(('1 1 0' * pathlib_gridsize));
924 pathlib_movecost_waterfactor = 1.1;
925 pathlib_foundgoal = 0;
927 walknode_boxmax = self.maxs * 1.5;
928 walknode_boxmin = self.mins * 1.5;
930 pathlib_edge_check_size = (vlen(walknode_boxmin - walknode_boxmax) * 0.5);
932 walknode_boxup = '0 0 2' * self.maxs_z;
933 walknode_stepsize = 32;
934 walknode_stepup = '0 0 1' * walknode_stepsize;
935 walknode_maxdrop = '0 0 3' * walknode_stepsize;
937 from_x = fsnap(from_x,pathlib_gridsize);
938 from_y = fsnap(from_y,pathlib_gridsize);
940 to_x = fsnap(to_x,pathlib_gridsize);
941 to_y = fsnap(to_y,pathlib_gridsize);
943 dprint("AStar init. ", ftos(pathlib_scraplist_cnt), " nodes on scrap\n");
944 path = pathlib_mknode(from,world);
945 pathlib_close_node(path,to);
946 if(pathlib_foundgoal)
948 dprint("AStar: Goal found on first node!\n");
952 open.classname = "path_end";
953 setorigin(open,path.origin);
960 if(pathlib_expandnode(path,from,to) <= 0)
962 dprint("AStar path fail.\n");
968 best_open_node = pathlib_getbestopen();
970 pathlib_close_node(best_open_node,to);
971 if(inwater(n.origin))
972 pathlib_expandnode_box(n,from,to);
974 pathlib_expandnode_star(n,from,to);
976 while(pathlib_open_cnt)
978 best_open_node = pathlib_getbestopen();
980 pathlib_close_node(best_open_node,to);
982 if(inwater(n.origin))
983 pathlib_expandnode_box(n,from,to);
985 pathlib_expandnode(n,from,to);
987 if(pathlib_foundgoal)
989 dprint("Target found. Rebuilding and filtering path...\n");
990 ftime = gettime(GETTIME_REALTIME);
991 ptime = ftime - ptime;
993 start = path_build(world,path.origin,world,world);
994 end = path_build(world,goal_node.origin,world,start);
998 for(open = goal_node; open.path_prev != path; open = open.path_prev)
1000 n = path_build(ln,open.origin,open.path_prev,start);
1004 start.path_next = n;
1005 n.path_prev = start;
1006 ftime = gettime(GETTIME_REALTIME) - ftime;
1008 ctime = gettime(GETTIME_REALTIME);
1010 ctime = gettime(GETTIME_REALTIME) - ctime;
1014 pathlib_showpath2(start);
1016 dprint("Time used - pathfinding: ", ftos(ptime),"\n");
1017 dprint("Time used - rebuild & filter: ", ftos(ftime),"\n");
1018 dprint("Time used - cleanup: ", ftos(ctime),"\n");
1019 dprint("Time used - total: ", ftos(ptime + ftime + ctime),"\n");
1020 dprint("Time used - # frames: ", ftos(ceil((ptime + ftime + ctime) / sys_frametime)),"\n\n");
1021 dprint("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
1022 dprint("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
1023 dprint("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
1024 dprint("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
1025 dprint("Nodes - searched: ", ftos(pathlib_searched_cnt),"\n");
1027 if(pathlib_recycle_cnt)
1028 dprint("Nodes - make/reuse: ", ftos(pathlib_made_cnt / pathlib_recycle_cnt),"\n");
1029 if(pathlib_recycle_cnt)
1030 dprint("Nodes - reused: ", ftos(pathlib_recycle_cnt),"\n");
1032 dprint("Nodes bestopen searched: ", ftos(pathlib_bestopen_seached),"\n");
1033 dprint("Nodes bestcash - hits: ", ftos(pathlib_bestcash_hits),"\n");
1034 dprint("Nodes bestcash - save: ", ftos(pathlib_bestcash_saved),"\n");
1035 dprint("AStar done. ", ftos(pathlib_scraplist_cnt), " nodes on scrap\n\n");
1041 dprint("A* Faild to find a path! Try a smaller gridsize.\n");