4 void pathlib_deletepath(entity start)
8 e = findchainentity(owner, start);
17 //#define PATHLIB_NODEEXPIRE 0.05
18 const float PATHLIB_NODEEXPIRE = 20;
20 void dumpnode(entity n)
22 n.is_path_node = false;
27 entity pathlib_mknode(vector where,entity parent)
31 node = pathlib_nodeatpoint(where);
35 mark_error(where, 60);
42 node.think = SUB_Remove;
43 node.nextthink = time + PATHLIB_NODEEXPIRE;
44 node.is_path_node = true;
45 node.owner = openlist;
46 node.path_prev = parent;
49 setsize(node, '0 0 0', '0 0 0');
51 setorigin(node, where);
52 node.medium = pointcontents(where);
53 pathlib_showsquare(where, 1 ,15);
61 float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost)
64 float h,g,f,doedge = 0;
67 ++pathlib_searched_cnt;
69 if(inwater(parent.origin))
71 LOG_TRACE("FromWater\n");
72 pathlib_expandnode = pathlib_expandnode_box;
73 pathlib_movenode = pathlib_swimnode;
79 LOG_TRACE("ToWater\n");
80 pathlib_expandnode = pathlib_expandnode_box;
81 pathlib_movenode = pathlib_walknode;
85 LOG_TRACE("LandToLoand\n");
86 //if(edge_check(parent.origin))
89 pathlib_expandnode = pathlib_expandnode_star;
90 pathlib_movenode = pathlib_walknode;
95 node = pathlib_nodeatpoint(to);
98 LOG_TRACE("NodeAtPoint\n");
101 if(node.owner == openlist)
103 h = pathlib_heuristic(node.origin,goal);
104 g = pathlib_cost(parent,node.origin,cost);
107 if(node.pathlib_node_g > g)
109 node.pathlib_node_h = h;
110 node.pathlib_node_g = g;
111 node.pathlib_node_f = f;
113 node.path_prev = parent;
117 best_open_node = node;
118 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
119 best_open_node = node;
125 where = pathlib_movenode(parent.origin, to, 0);
127 if (!pathlib_movenode_goodnode)
129 //pathlib_showsquare(where, 0 ,30);
130 //pathlib_showsquare(parent.origin, 1 ,30);
131 LOG_TRACE("pathlib_movenode_goodnode = 0\n");
135 //pathlib_showsquare(where, 1 ,30);
137 if(pathlib_nodeatpoint(where))
139 LOG_TRACE("NAP WHERE :",vtos(where),"\n");
140 LOG_TRACE("not NAP TO:",vtos(to),"\n");
141 LOG_TRACE("NAP-NNAP:",ftos(vlen(to-where)),"\n\n");
147 if (!tile_check(where))
149 LOG_TRACE("tile_check fail\n");
150 pathlib_showsquare(where, 0 ,30);
155 h = pathlib_heuristic(where,goal);
156 g = pathlib_cost(parent,where,cost);
160 node = findradius(where,pathlib_gridsize * 0.5);
163 if(node.is_path_node == true)
166 if(node.owner == openlist)
168 if(node.pathlib_node_g > g)
170 //pathlib_movenode(where,node.origin,0);
171 //if(pathlib_movenode_goodnode)
173 //mark_error(node.origin + '0 0 128',30);
174 node.pathlib_node_h = h;
175 node.pathlib_node_g = g;
176 node.pathlib_node_f = f;
177 node.path_prev = parent;
182 best_open_node = node;
183 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
184 best_open_node = node;
193 node = pathlib_mknode(where, parent);
194 node.pathlib_node_h = h;
195 node.pathlib_node_g = g;
196 node.pathlib_node_f = f;
199 best_open_node = node;
200 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
201 best_open_node = node;
206 entity pathlib_getbestopen()
213 ++pathlib_bestcash_hits;
214 pathlib_bestcash_saved += pathlib_open_cnt;
216 return best_open_node;
219 node = findchainentity(owner,openlist);
226 ++pathlib_bestopen_seached;
227 if(node.pathlib_node_f < bestnode.pathlib_node_f)
236 void pathlib_close_node(entity node,vector goal)
239 if(node.owner == closedlist)
241 LOG_TRACE("Pathlib: Tried to close a closed node!\n");
245 if(node == best_open_node)
246 best_open_node = world;
248 ++pathlib_closed_cnt;
251 node.owner = closedlist;
253 if(vlen(node.origin - goal) <= pathlib_gridsize)
257 goalmove = pathlib_walknode(node.origin,goal,1);
258 if(pathlib_movenode_goodnode)
261 pathlib_foundgoal = true;
266 void pathlib_cleanup()
268 best_open_node = world;
274 node = findfloat(world,is_path_node, true);
278 node.owner = openlist;
279 node.pathlib_node_g = 0;
280 node.pathlib_node_h = 0;
281 node.pathlib_node_f = 0;
282 node.path_prev = world;
286 node = findfloat(node,is_path_node, true);
300 float Cosine_Interpolate(float a, float b, float x)
305 f = (1 - cos(ft)) * 0.5;
307 return a*(1-f) + b*f;
310 float buildpath_nodefilter_directional(vector n,vector c,vector p)
314 d2 = normalize(p - c);
315 d1 = normalize(c - n);
317 if(vlen(d1-d2) < 0.25)
326 float buildpath_nodefilter_moveskip(vector n,vector c,vector p)
328 pathlib_walknode(p,n,1);
329 if(pathlib_movenode_goodnode)
335 float buildpath_nodefilter_none(vector n,vector c,vector p)
340 entity path_build(entity next, vector where, entity prev, entity start)
345 if(buildpath_nodefilter)
346 if(buildpath_nodefilter(next.origin,where,prev.origin))
352 path.path_next = next;
354 setorigin(path,where);
357 path.classname = "path_end";
361 path.classname = "path_start";
363 path.classname = "path_node";
369 entity pathlib_astar(vector from,vector to)
371 entity path, start, end, open, n, ln;
372 float ptime, ftime, ctime;
374 ptime = gettime(GETTIME_REALTIME);
375 pathlib_starttime = ptime;
379 // Select water<->land capable node make/link
380 pathlib_makenode = pathlib_makenode_adaptive;
382 // Select XYZ cost estimate
383 //pathlib_heuristic = pathlib_h_diagonal3;
384 pathlib_heuristic = pathlib_h_diagonal;
386 // Select distance + waterfactor cost
387 pathlib_cost = pathlib_g_euclidean_water;
389 // Select star expander
390 pathlib_expandnode = pathlib_expandnode_star;
392 // Select walk simulation movement test
393 pathlib_movenode = pathlib_walknode;
395 // Filter final nodes by direction
396 buildpath_nodefilter = buildpath_nodefilter_directional;
398 // Filter tiles with cross pattern
399 tile_check = tile_check_cross;
401 // If the start is in water we need diffrent settings
404 // Select volumetric node expaner
405 pathlib_expandnode = pathlib_expandnode_box;
407 // Water movement test
408 pathlib_movenode = pathlib_swimnode;
415 closedlist = spawn();
417 pathlib_closed_cnt = 0;
418 pathlib_open_cnt = 0;
419 pathlib_made_cnt = 0;
420 pathlib_merge_cnt = 0;
421 pathlib_searched_cnt = 0;
422 pathlib_bestopen_seached = 0;
423 pathlib_bestcash_hits = 0;
424 pathlib_bestcash_saved = 0;
426 pathlib_gridsize = 128;
427 pathlib_movecost = pathlib_gridsize;
428 pathlib_movecost_diag = vlen(('1 1 0' * pathlib_gridsize));
429 pathlib_movecost_waterfactor = 25000000;
430 pathlib_foundgoal = 0;
432 movenode_boxmax = self.maxs * 1.1;
433 movenode_boxmin = self.mins * 1.1;
435 movenode_stepsize = pathlib_gridsize * 0.25;
437 tile_check_size = pathlib_gridsize * 0.5;
438 tile_check_up = '0 0 2' * pathlib_gridsize;
439 tile_check_up = '0 0 128';
440 tile_check_down = '0 0 3' * pathlib_gridsize;
441 tile_check_down = '0 0 256';
443 //movenode_stepup = '0 0 128';
444 movenode_stepup = '0 0 36';
445 movenode_maxdrop = '0 0 512';
446 //movenode_maxdrop = '0 0 512';
447 movenode_boxup = '0 0 72';
449 from.x = fsnap(from.x, pathlib_gridsize);
450 from.y = fsnap(from.y, pathlib_gridsize);
453 to.x = fsnap(to.x, pathlib_gridsize);
454 to.y = fsnap(to.y, pathlib_gridsize);
457 LOG_TRACE("AStar init\n");
458 path = pathlib_mknode(from, world);
459 pathlib_close_node(path, to);
460 if(pathlib_foundgoal)
462 LOG_TRACE("AStar: Goal found on first node!\n");
466 open.classname = "path_end";
467 setorigin(open,path.origin);
474 if(pathlib_expandnode(path, from, to) <= 0)
476 LOG_TRACE("AStar path fail.\n");
482 best_open_node = pathlib_getbestopen();
484 pathlib_close_node(best_open_node, to);
485 if(inwater(n.origin))
486 pathlib_expandnode_box(n, from, to);
488 pathlib_expandnode_star(n, from, to);
490 while(pathlib_open_cnt)
492 if((gettime(GETTIME_REALTIME) - pathlib_starttime) > pathlib_maxtime)
494 LOG_TRACE("Path took to long to compute!\n");
495 LOG_TRACE("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
496 LOG_TRACE("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
497 LOG_TRACE("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
498 LOG_TRACE("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
504 best_open_node = pathlib_getbestopen();
506 pathlib_close_node(best_open_node,to);
508 if(inwater(n.origin))
509 pathlib_expandnode_box(n,from,to);
511 pathlib_expandnode(n,from,to);
513 if(pathlib_foundgoal)
515 LOG_TRACE("Target found. Rebuilding and filtering path...\n");
516 ftime = gettime(GETTIME_REALTIME);
517 ptime = ftime - ptime;
519 start = path_build(world,path.origin,world,world);
520 end = path_build(world,goal_node.origin,world,start);
524 for(open = goal_node; open.path_prev != path; open = open.path_prev)
526 n = path_build(ln,open.origin,open.path_prev,start);
532 ftime = gettime(GETTIME_REALTIME) - ftime;
534 ctime = gettime(GETTIME_REALTIME);
536 ctime = gettime(GETTIME_REALTIME) - ctime;
540 pathlib_showpath2(start);
542 LOG_TRACE("Time used - pathfinding: ", ftos(ptime),"\n");
543 LOG_TRACE("Time used - rebuild & filter: ", ftos(ftime),"\n");
544 LOG_TRACE("Time used - cleanup: ", ftos(ctime),"\n");
545 LOG_TRACE("Time used - total: ", ftos(ptime + ftime + ctime),"\n");
546 LOG_TRACE("Time used - # frames: ", ftos(ceil((ptime + ftime + ctime) / sys_frametime)),"\n\n");
547 LOG_TRACE("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
548 LOG_TRACE("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
549 LOG_TRACE("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
550 LOG_TRACE("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
551 LOG_TRACE("Nodes - searched: ", ftos(pathlib_searched_cnt),"\n");
552 LOG_TRACE("Nodes bestopen searched: ", ftos(pathlib_bestopen_seached),"\n");
553 LOG_TRACE("Nodes bestcash - hits: ", ftos(pathlib_bestcash_hits),"\n");
554 LOG_TRACE("Nodes bestcash - save: ", ftos(pathlib_bestcash_saved),"\n");
555 LOG_TRACE("AStar done.\n");
561 LOG_TRACE("A* Faild to find a path! Try a smaller gridsize.\n");