1 void pathlib_deletepath(entity start)
5 e = findchainentity(owner, start);
14 //#define PATHLIB_NODEEXPIRE 0.05
15 #define PATHLIB_NODEEXPIRE 20
17 void dumpnode(entity n)
19 n.is_path_node = FALSE;
24 entity pathlib_mknode(vector where,entity parent)
28 node = pathlib_nodeatpoint(where);
37 node.think = SUB_Remove;
38 node.nextthink = time + PATHLIB_NODEEXPIRE;
39 node.is_path_node = TRUE;
40 node.owner = openlist;
41 node.path_prev = parent;
43 setmodel(node,"models/pathlib/square.md3");
44 setsize(node,'0 0 0','0 0 0');
45 node.colormod = randomvec() * 2;
47 node.scale = pathlib_gridsize / 512.001;
49 //pathlib_showsquare2(node,'1 1 1',0);//(node.medium & CONTENT_EMPTY));
50 setorigin(node, where);
51 node.medium = pointcontents(where);
54 //pathlib_showsquare(where,1,30);
63 float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost)
69 ++pathlib_searched_cnt;
71 if(inwater(parent.origin))
73 pathlib_expandnode = pathlib_expandnode_box;
74 pathlib_movenode = pathlib_swimnode;
80 pathlib_expandnode = pathlib_expandnode_box;
81 pathlib_movenode = pathlib_walknode;
85 //if(edge_check(parent.origin))
88 pathlib_expandnode = pathlib_expandnode_star;
89 pathlib_movenode = pathlib_walknode;
94 node = pathlib_nodeatpoint(to);
99 if(node.owner == openlist)
101 h = pathlib_heuristic(node.origin,goal);
102 g = pathlib_cost(parent,node.origin,cost);
105 if(node.pathlib_node_g > g)
107 node.pathlib_node_h = h;
108 node.pathlib_node_g = g;
109 node.pathlib_node_f = f;
111 node.path_prev = parent;
114 if not (best_open_node)
115 best_open_node = node;
116 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
117 best_open_node = node;
123 where = pathlib_movenode(parent.origin,to,0);
124 if not(pathlib_movenode_goodnode)
127 if(pathlib_nodeatpoint(where))
129 dprint("NAP WHERE :",vtos(where),"\n");
130 dprint("not NAP TO:",vtos(to),"\n");
131 dprint("NAP-NNAP:",ftos(vlen(to-where)),"\n\n");
136 if not (tile_check(where))
139 h = pathlib_heuristic(where,goal);
140 g = pathlib_cost(parent,where,cost);
145 node = findradius(where,pathlib_gridsize * 0.5);
148 if(node.is_path_node == TRUE)
151 if(node.owner == openlist)
153 if(node.pathlib_node_g > g)
155 //pathlib_movenode(where,node.origin,0);
156 //if(pathlib_movenode_goodnode)
158 //mark_error(node.origin + '0 0 128',30);
159 node.pathlib_node_h = h;
160 node.pathlib_node_g = g;
161 node.pathlib_node_f = f;
162 node.path_prev = parent;
166 if not (best_open_node)
167 best_open_node = node;
168 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
169 best_open_node = node;
178 node = pathlib_mknode(where,parent);
179 node.pathlib_node_h = h;
180 node.pathlib_node_g = g;
181 node.pathlib_node_f = f;
183 if not (best_open_node)
184 best_open_node = node;
185 else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
186 best_open_node = node;
191 entity pathlib_getbestopen()
198 ++pathlib_bestcash_hits;
199 pathlib_bestcash_saved += pathlib_open_cnt;
201 return best_open_node;
204 node = findchainentity(owner,openlist);
211 ++pathlib_bestopen_seached;
212 if(node.pathlib_node_f < bestnode.pathlib_node_f)
221 void pathlib_close_node(entity node,vector goal)
224 if(node.owner == closedlist)
226 dprint("Pathlib: Tried to close a closed node!\n");
230 if(node == best_open_node)
231 best_open_node = world;
233 ++pathlib_closed_cnt;
236 node.owner = closedlist;
238 if(vlen(node.origin - goal) <= pathlib_gridsize)
242 goalmove = pathlib_walknode(node.origin,goal,1);
243 if(pathlib_movenode_goodnode)
246 pathlib_foundgoal = TRUE;
251 void pathlib_cleanup()
253 best_open_node = world;
259 node = findfloat(world,is_path_node, TRUE);
263 node.owner = openlist;
264 node.pathlib_node_g = 0;
265 node.pathlib_node_h = 0;
266 node.pathlib_node_f = 0;
267 node.path_prev = world;
271 node = findfloat(node,is_path_node, TRUE);
285 float Cosine_Interpolate(float a, float b, float x)
290 f = (1 - cos(ft)) * 0.5;
292 return a*(1-f) + b*f;
295 float buildpath_nodefilter_directional(vector n,vector c,vector p)
299 d2 = normalize(p - c);
300 d1 = normalize(c - n);
302 if(vlen(d1-d2) < 0.25)
311 float buildpath_nodefilter_moveskip(vector n,vector c,vector p)
313 pathlib_walknode(p,n,1);
314 if(pathlib_movenode_goodnode)
320 entity path_build(entity next, vector where, entity prev, entity start)
325 if(buildpath_nodefilter)
326 if(buildpath_nodefilter(next.origin,where,prev.origin))
332 path.path_next = next;
334 setorigin(path,where);
337 path.classname = "path_end";
341 path.classname = "path_start";
343 path.classname = "path_node";
349 entity pathlib_astar(vector from,vector to)
351 entity path, start, end, open, n, ln;
352 float ptime, ftime, ctime;
354 ptime = gettime(GETTIME_REALTIME);
355 pathlib_starttime = ptime;
359 // Select water<->land capable node make/link
360 pathlib_makenode = pathlib_makenode_adaptive;
361 // Select XYZ cost estimate
362 //pathlib_heuristic = pathlib_h_diagonal3;
363 pathlib_heuristic = pathlib_h_diagonal;
364 // Select distance + waterfactor cost
365 pathlib_cost = pathlib_g_euclidean_water;
366 // Select star expander
367 pathlib_expandnode = pathlib_expandnode_star;
368 // Select walk simulation movement test
369 pathlib_movenode = pathlib_walknode;
370 // Filter final nodes by direction
371 buildpath_nodefilter = buildpath_nodefilter_directional;
372 // Filter tiles with cross pattern
373 tile_check = tile_check_cross;
375 // If the start is in water we need diffrent settings
378 // Select volumetric node expaner
379 pathlib_expandnode = pathlib_expandnode_box;
381 // Water movement test
382 pathlib_movenode = pathlib_swimnode;
389 closedlist = spawn();
391 pathlib_closed_cnt = 0;
392 pathlib_open_cnt = 0;
393 pathlib_made_cnt = 0;
394 pathlib_merge_cnt = 0;
395 pathlib_searched_cnt = 0;
396 pathlib_bestopen_seached = 0;
397 pathlib_bestcash_hits = 0;
398 pathlib_bestcash_saved = 0;
400 pathlib_gridsize = 128;
401 pathlib_movecost = pathlib_gridsize;
402 pathlib_movecost_diag = vlen(('1 1 0' * pathlib_gridsize));
403 pathlib_movecost_waterfactor = 1;
404 pathlib_foundgoal = 0;
406 movenode_boxmax = self.maxs * 1.25;
407 movenode_boxmin = self.mins * 1.25;
409 movenode_stepsize = 32;
411 tile_check_size = 65;
412 tile_check_up = '0 0 128';
413 tile_check_down = '0 0 128';
415 movenode_stepup = '0 0 36';
416 movenode_maxdrop = '0 0 128';
417 movenode_boxup = '0 0 72';
419 from_x = fsnap(from_x,pathlib_gridsize);
420 from_y = fsnap(from_y,pathlib_gridsize);
422 to_x = fsnap(to_x,pathlib_gridsize);
423 to_y = fsnap(to_y,pathlib_gridsize);
425 dprint("AStar init\n");
426 path = pathlib_mknode(from,world);
427 pathlib_close_node(path,to);
428 if(pathlib_foundgoal)
430 dprint("AStar: Goal found on first node!\n");
434 open.classname = "path_end";
435 setorigin(open,path.origin);
442 if(pathlib_expandnode(path,from,to) <= 0)
444 dprint("AStar path fail.\n");
450 best_open_node = pathlib_getbestopen();
452 pathlib_close_node(best_open_node,to);
453 if(inwater(n.origin))
454 pathlib_expandnode_box(n,from,to);
456 pathlib_expandnode_star(n,from,to);
458 while(pathlib_open_cnt)
460 if((gettime(GETTIME_REALTIME) - pathlib_starttime) > pathlib_maxtime)
462 dprint("Path took to long to compute!\n");
463 dprint("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
464 dprint("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
465 dprint("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
466 dprint("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
472 best_open_node = pathlib_getbestopen();
474 pathlib_close_node(best_open_node,to);
476 if(inwater(n.origin))
477 pathlib_expandnode_box(n,from,to);
479 pathlib_expandnode(n,from,to);
481 if(pathlib_foundgoal)
483 dprint("Target found. Rebuilding and filtering path...\n");
484 ftime = gettime(GETTIME_REALTIME);
485 ptime = ftime - ptime;
487 start = path_build(world,path.origin,world,world);
488 end = path_build(world,goal_node.origin,world,start);
492 for(open = goal_node; open.path_prev != path; open = open.path_prev)
494 n = path_build(ln,open.origin,open.path_prev,start);
500 ftime = gettime(GETTIME_REALTIME) - ftime;
502 ctime = gettime(GETTIME_REALTIME);
504 ctime = gettime(GETTIME_REALTIME) - ctime;
508 pathlib_showpath2(start);
510 dprint("Time used - pathfinding: ", ftos(ptime),"\n");
511 dprint("Time used - rebuild & filter: ", ftos(ftime),"\n");
512 dprint("Time used - cleanup: ", ftos(ctime),"\n");
513 dprint("Time used - total: ", ftos(ptime + ftime + ctime),"\n");
514 dprint("Time used - # frames: ", ftos(ceil((ptime + ftime + ctime) / sys_frametime)),"\n\n");
515 dprint("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
516 dprint("Nodes - open: ", ftos(pathlib_open_cnt),"\n");
517 dprint("Nodes - merged: ", ftos(pathlib_merge_cnt),"\n");
518 dprint("Nodes - closed: ", ftos(pathlib_closed_cnt),"\n");
519 dprint("Nodes - searched: ", ftos(pathlib_searched_cnt),"\n");
520 dprint("Nodes bestopen searched: ", ftos(pathlib_bestopen_seached),"\n");
521 dprint("Nodes bestcash - hits: ", ftos(pathlib_bestcash_hits),"\n");
522 dprint("Nodes bestcash - save: ", ftos(pathlib_bestcash_saved),"\n");
523 dprint("AStar done.\n");
529 dprint("A* Faild to find a path! Try a smaller gridsize.\n");