1 #include "navigation.qh"
6 #include "waypoints.qh"
8 #include <common/t_items.qh>
10 #include <common/items/_mod.qh>
12 #include <common/constants.qh>
13 #include <common/net_linked.qh>
14 #include <common/triggers/trigger/jumppads.qh>
18 void navigation_dynamicgoal_init(entity this, bool initially_static)
20 this.navigation_dynamicgoal = true;
21 this.bot_basewaypoint = this.nearestwaypoint;
23 this.nearestwaypointtimeout = -1;
25 this.nearestwaypointtimeout = time;
28 void navigation_dynamicgoal_set(entity this)
30 this.nearestwaypointtimeout = time;
33 void navigation_dynamicgoal_unset(entity this)
35 if(this.bot_basewaypoint)
36 this.nearestwaypoint = this.bot_basewaypoint;
37 this.nearestwaypointtimeout = -1;
40 // rough simulation of walking from one point to another to test if a path
41 // can be traveled, used for waypoint linking and havocbot
43 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
54 if(autocvar_bot_debug_tracewalk)
63 dist = totaldist = vlen(move);
64 dir = normalize(move);
66 ignorehazards = false;
69 // Analyze starting point
70 traceline(start, start, MOVE_NORMAL, e);
71 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
75 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
76 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
82 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
86 if(autocvar_bot_debug_tracewalk)
87 debugnodestatus(start, DEBUG_NODE_FAIL);
89 //print("tracewalk: ", vtos(start), " is a bad start\n");
97 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
100 if(autocvar_bot_debug_tracewalk)
101 debugnodestatus(org, DEBUG_NODE_SUCCESS);
103 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
106 if(autocvar_bot_debug_tracewalk)
113 dist = dist - stepdist;
114 traceline(org, org, MOVE_NORMAL, e);
117 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
119 // hazards blocking path
120 if(autocvar_bot_debug_tracewalk)
121 debugnodestatus(org, DEBUG_NODE_FAIL);
123 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
127 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
129 move = normalize(end - org);
130 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
132 if(autocvar_bot_debug_tracewalk)
133 debugnode(e, trace_endpos);
135 if (trace_fraction < 1)
138 org = trace_endpos + normalize(org - trace_endpos) * stepdist;
139 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
141 if(autocvar_bot_debug_tracewalk)
144 if(pointcontents(org) == CONTENT_EMPTY)
148 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
150 if(autocvar_bot_debug_tracewalk)
151 debugnodestatus(org, DEBUG_NODE_FAIL);
154 //print("tracewalk: ", vtos(start), " failed under water\n");
164 move = dir * stepdist + org;
165 tracebox(org, m1, m2, move, movemode, e);
167 if(autocvar_bot_debug_tracewalk)
168 debugnode(e, trace_endpos);
171 if (trace_fraction < 1)
173 // check if we can walk over this obstacle, possibly by jumpstepping
174 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
175 if (trace_fraction < 1 || trace_startsolid)
177 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
178 if (trace_fraction < 1 || trace_startsolid)
180 bool ladder_found = false;
181 FOREACH_ENTITY_CLASS("func_ladder", boxesoverlap(trace_endpos + m1 + '-1 -1 -1', trace_endpos + m2 + '1 1 1', it.absmin, it.absmax),
183 if(boxesoverlap(end, end, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
184 ladder_found = true; // can't return here ("Loop mutex held by tracewalk" error)
188 if(autocvar_bot_debug_tracewalk)
189 debugnodestatus(org, DEBUG_NODE_SUCCESS);
191 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
195 if(autocvar_bot_debug_tracewalk)
196 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
198 traceline( org, move, movemode, e);
200 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
204 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
206 nextmove = move + (dir * stepdist);
207 traceline( move, nextmove, movemode, e);
213 if(autocvar_bot_debug_tracewalk)
214 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
216 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
217 //te_explosion(trace_endpos);
218 //print(ftos(e.dphitcontentsmask), "\n");
219 return false; // failed
231 // trace down from stepheight as far as possible and move there,
232 // if this starts in solid we try again without the stepup, and
233 // if that also fails we assume it is a wall
234 // (this is the same logic as the Quake walkmove function used)
235 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
237 // moved successfully
241 c = pointcontents(org + '0 0 1');
242 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
252 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
254 // moved but didn't arrive at the intended destination
255 if(autocvar_bot_debug_tracewalk)
256 debugnodestatus(org, DEBUG_NODE_FAIL);
261 /////////////////////////////////////////////////////////////////////////////
263 /////////////////////////////////////////////////////////////////////////////
265 // completely empty the goal stack, used when deciding where to go
266 void navigation_clearroute(entity this)
268 //print("bot ", etos(this), " clear\n");
269 this.goalentity = NULL;
270 this.goalcurrent = NULL;
271 this.goalstack01 = NULL;
272 this.goalstack02 = NULL;
273 this.goalstack03 = NULL;
274 this.goalstack04 = NULL;
275 this.goalstack05 = NULL;
276 this.goalstack06 = NULL;
277 this.goalstack07 = NULL;
278 this.goalstack08 = NULL;
279 this.goalstack09 = NULL;
280 this.goalstack10 = NULL;
281 this.goalstack11 = NULL;
282 this.goalstack12 = NULL;
283 this.goalstack13 = NULL;
284 this.goalstack14 = NULL;
285 this.goalstack15 = NULL;
286 this.goalstack16 = NULL;
287 this.goalstack17 = NULL;
288 this.goalstack18 = NULL;
289 this.goalstack19 = NULL;
290 this.goalstack20 = NULL;
291 this.goalstack21 = NULL;
292 this.goalstack22 = NULL;
293 this.goalstack23 = NULL;
294 this.goalstack24 = NULL;
295 this.goalstack25 = NULL;
296 this.goalstack26 = NULL;
297 this.goalstack27 = NULL;
298 this.goalstack28 = NULL;
299 this.goalstack29 = NULL;
300 this.goalstack30 = NULL;
301 this.goalstack31 = NULL;
304 // add a new goal at the beginning of the stack
305 // (in other words: add a new prerequisite before going to the later goals)
306 // NOTE: when a waypoint is added, the WP gets pushed first, then the
307 // next-closest WP on the shortest path to the WP
308 // That means, if the stack overflows, the bot will know how to do the FIRST 32
309 // steps to the goal, and then recalculate the path.
310 void navigation_pushroute(entity this, entity e)
312 //print("bot ", etos(this), " push ", etos(e), "\n");
313 if(this.goalstack31 == this.goalentity)
314 this.goalentity = NULL;
315 this.goalstack31 = this.goalstack30;
316 this.goalstack30 = this.goalstack29;
317 this.goalstack29 = this.goalstack28;
318 this.goalstack28 = this.goalstack27;
319 this.goalstack27 = this.goalstack26;
320 this.goalstack26 = this.goalstack25;
321 this.goalstack25 = this.goalstack24;
322 this.goalstack24 = this.goalstack23;
323 this.goalstack23 = this.goalstack22;
324 this.goalstack22 = this.goalstack21;
325 this.goalstack21 = this.goalstack20;
326 this.goalstack20 = this.goalstack19;
327 this.goalstack19 = this.goalstack18;
328 this.goalstack18 = this.goalstack17;
329 this.goalstack17 = this.goalstack16;
330 this.goalstack16 = this.goalstack15;
331 this.goalstack15 = this.goalstack14;
332 this.goalstack14 = this.goalstack13;
333 this.goalstack13 = this.goalstack12;
334 this.goalstack12 = this.goalstack11;
335 this.goalstack11 = this.goalstack10;
336 this.goalstack10 = this.goalstack09;
337 this.goalstack09 = this.goalstack08;
338 this.goalstack08 = this.goalstack07;
339 this.goalstack07 = this.goalstack06;
340 this.goalstack06 = this.goalstack05;
341 this.goalstack05 = this.goalstack04;
342 this.goalstack04 = this.goalstack03;
343 this.goalstack03 = this.goalstack02;
344 this.goalstack02 = this.goalstack01;
345 this.goalstack01 = this.goalcurrent;
346 this.goalcurrent = e;
349 // remove first goal from stack
350 // (in other words: remove a prerequisite for reaching the later goals)
351 // (used when a spawnfunc_waypoint is reached)
352 void navigation_poproute(entity this)
354 //print("bot ", etos(this), " pop\n");
355 if(this.goalcurrent == this.goalentity)
356 this.goalentity = NULL;
357 this.goalcurrent = this.goalstack01;
358 this.goalstack01 = this.goalstack02;
359 this.goalstack02 = this.goalstack03;
360 this.goalstack03 = this.goalstack04;
361 this.goalstack04 = this.goalstack05;
362 this.goalstack05 = this.goalstack06;
363 this.goalstack06 = this.goalstack07;
364 this.goalstack07 = this.goalstack08;
365 this.goalstack08 = this.goalstack09;
366 this.goalstack09 = this.goalstack10;
367 this.goalstack10 = this.goalstack11;
368 this.goalstack11 = this.goalstack12;
369 this.goalstack12 = this.goalstack13;
370 this.goalstack13 = this.goalstack14;
371 this.goalstack14 = this.goalstack15;
372 this.goalstack15 = this.goalstack16;
373 this.goalstack16 = this.goalstack17;
374 this.goalstack17 = this.goalstack18;
375 this.goalstack18 = this.goalstack19;
376 this.goalstack19 = this.goalstack20;
377 this.goalstack20 = this.goalstack21;
378 this.goalstack21 = this.goalstack22;
379 this.goalstack22 = this.goalstack23;
380 this.goalstack23 = this.goalstack24;
381 this.goalstack24 = this.goalstack25;
382 this.goalstack25 = this.goalstack26;
383 this.goalstack26 = this.goalstack27;
384 this.goalstack27 = this.goalstack28;
385 this.goalstack28 = this.goalstack29;
386 this.goalstack29 = this.goalstack30;
387 this.goalstack30 = this.goalstack31;
388 this.goalstack31 = NULL;
391 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
394 dist = vlen(v - org);
397 traceline(v, org, true, ent);
398 if (trace_fraction == 1)
402 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, bot_navigation_movemode))
407 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, bot_navigation_movemode))
415 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
416 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
418 vector pm1 = ent.origin + ent.mins;
419 vector pm2 = ent.origin + ent.maxs;
421 // do two scans, because box test is cheaper
422 IL_EACH(g_waypoints, it != ent && it != except,
424 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
428 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
429 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
430 // TODO possibly make other code have the same support for bboxes
432 org = org + ent.tag_entity.origin;
433 if (navigation_testtracewalk)
439 if(!autocvar_g_waypointeditor && !ent.navigation_dynamicgoal)
441 waypoint_clearlinks(ent); // initialize wpXXmincost fields
442 IL_EACH(g_waypoints, it != ent,
446 vector wm1 = it.absmin;
447 vector wm2 = it.absmax;
448 v.x = bound(wm1_x, org.x, wm2_x);
449 v.y = bound(wm1_y, org.y, wm2_y);
450 v.z = bound(wm1_z, org.z, wm2_z);
454 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, 1050))
455 navigation_item_addlink(it, ent);
459 // box check failed, try walk
460 IL_EACH(g_waypoints, it != ent,
464 vector wm1 = it.origin + it.mins;
465 vector wm2 = it.origin + it.maxs;
466 v.x = bound(wm1_x, org.x, wm2_x);
467 v.y = bound(wm1_y, org.y, wm2_y);
468 v.z = bound(wm1_z, org.z, wm2_z);
472 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
474 bestdist = vlen(v - org);
480 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
482 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
483 if (autocvar_g_waypointeditor_auto)
485 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
487 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
492 // finds the waypoints near the bot initiating a navigation query
493 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
496 // navigation_testtracewalk = true;
498 IL_EACH(g_waypoints, !it.wpconsidered,
502 m1 = it.origin + it.mins;
503 m2 = it.origin + it.maxs;
505 v.x = bound(m1_x, v.x, m2_x);
506 v.y = bound(m1_y, v.y, m2_y);
507 v.z = bound(m1_z, v.z, m2_z);
511 vector diff = v - this.origin;
512 diff.z = max(0, diff.z);
513 if(vdist(diff, <, maxdist))
515 it.wpconsidered = true;
516 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
518 it.wpnearestpoint = v;
519 it.wpcost = waypoint_getdistancecost(this.origin, v) + it.dmg;
526 //navigation_testtracewalk = false;
530 // updates a path link if a spawnfunc_waypoint link is better than the current one
531 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
540 v.x = bound(m1_x, p.x, m2_x);
541 v.y = bound(m1_y, p.y, m2_y);
542 v.z = bound(m1_z, p.z, m2_z);
546 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
547 cost += w.wp00mincost; // assuming teleport has exactly one destination
549 cost += waypoint_getdistancecost(p, v);
550 if (wp.wpcost > cost)
555 wp.wpnearestpoint = v;
559 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
560 void navigation_markroutes(entity this, entity fixed_source_waypoint)
565 IL_EACH(g_waypoints, true,
567 it.wpconsidered = false;
568 it.wpnearestpoint = '0 0 0';
569 it.wpcost = 10000000;
574 if(fixed_source_waypoint)
576 fixed_source_waypoint.wpconsidered = true;
577 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
578 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
579 fixed_source_waypoint.wpfire = 1;
580 fixed_source_waypoint.enemy = NULL;
584 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
585 // as this search is expensive we will use lower values if the bot is on the air
586 float increment, maxdistance;
587 if(IS_ONGROUND(this))
598 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
601 bool searching = true;
605 IL_EACH(g_waypoints, it.wpfire,
610 p = it.wpnearestpoint;
612 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
613 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
614 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
615 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
616 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
617 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
618 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
619 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
620 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
621 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
622 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
623 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
624 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
625 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
626 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
627 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
628 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
629 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
630 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
631 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
632 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
633 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
634 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
635 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
636 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
637 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
638 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
639 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
640 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
641 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
642 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
643 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
644 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
649 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
650 void navigation_markroutes_inverted(entity fixed_source_waypoint)
654 IL_EACH(g_waypoints, true,
656 it.wpconsidered = false;
657 it.wpnearestpoint = '0 0 0';
658 it.wpcost = 10000000;
663 if(fixed_source_waypoint)
665 fixed_source_waypoint.wpconsidered = true;
666 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
667 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
668 fixed_source_waypoint.wpfire = 1;
669 fixed_source_waypoint.enemy = NULL;
673 error("need to start with a waypoint\n");
676 bool searching = true;
680 IL_EACH(g_waypoints, it.wpfire,
684 cost = it.wpcost; // cost to walk from it to home
685 p = it.wpnearestpoint;
687 IL_EACH(g_waypoints, it != wp,
689 if(!waypoint_islinked(it, wp))
691 cost2 = cost + it.dmg;
692 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
698 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
699 void navigation_routerating(entity this, entity e, float f, float rangebias)
707 rangebias = waypoint_getdistancecost_simple(rangebias);
708 f = waypoint_getdistancecost_simple(f);
712 bool rate_wps = false;
713 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
718 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
719 int t = pointcontents(trace_endpos + '0 0 1');
720 if(t != CONTENT_SOLID )
722 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
724 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
732 entity best_wp = NULL;
733 float best_dist = 10000;
734 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
735 && vdist(it.origin - this.origin, >, 100)
736 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
738 float dist = vlen(it.origin - theEnemy.origin);
739 if (dist < best_dist)
751 vector goal_org = (e.absmin + e.absmax) * 0.5;
753 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
755 // Evaluate path using jetpack
757 if(this.items & IT_JETPACK)
758 if(autocvar_bot_ai_navigation_jetpack)
759 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
761 vector pointa, pointb;
763 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
766 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
767 pointa = trace_endpos - '0 0 1';
770 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
771 pointb = trace_endpos - '0 0 1';
773 // Can I see these two points from the sky?
774 traceline(pointa, pointb, MOVE_NORMAL, this);
776 if(trace_fraction==1)
778 LOG_DEBUG("jetpack ai: can bridge these two points");
780 // Lower the altitude of these points as much as possible
781 float zdistance, xydistance, cost, t, fuel;
782 vector down, npa, npb;
784 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
790 if(npa.z<=this.absmax.z)
793 if(npb.z<=e.absmax.z)
796 traceline(npa, npb, MOVE_NORMAL, this);
797 if(trace_fraction==1)
803 while(trace_fraction == 1);
806 // Rough estimation of fuel consumption
807 // (ignores acceleration and current xyz velocity)
808 xydistance = vlen(pointa - pointb);
809 zdistance = fabs(pointa.z - this.origin.z);
811 t = zdistance / autocvar_g_jetpack_maxspeed_up;
812 t += xydistance / autocvar_g_jetpack_maxspeed_side;
813 fuel = t * autocvar_g_jetpack_fuel * 0.8;
815 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
818 if(this.ammo_fuel>fuel)
821 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
822 // - between air and ground speeds)
824 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
825 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
828 // Compare against other goals
829 f = f * rangebias / (rangebias + cost);
831 if (navigation_bestrating < f)
833 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
834 navigation_bestrating = f;
835 navigation_bestgoal = e;
836 this.navigation_jetpack_goal = e;
837 this.navigation_jetpack_point = pointb;
845 //te_wizspike(e.origin);
848 // update the cached spawnfunc_waypoint link on a dynamic item entity
849 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
855 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
856 e.nearestwaypoint = NULL;
858 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
859 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
861 nwp = navigation_findnearestwaypoint(e, true);
864 e.nearestwaypoint = nwp;
866 vector m1 = nwp.absmin, m2 = nwp.absmax;
867 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
868 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
869 vector ve = (e.absmin - e.absmax) * 0.5;
870 ve.x = bound(m1.x, ve.x, m2.x);
871 ve.y = bound(m1.y, ve.y, m2.y);
872 ve.z = bound(m1.z, ve.z, m2.z);
874 m1 = e.absmin; m2 = e.absmax;
875 m1.x = e.origin.x; m1.y = e.origin.y;
876 m2.x = e.origin.x; m2.y = e.origin.y;
877 vector vnwp = nwp.origin;
878 vnwp.x = bound(m1.x, vnwp.x, m2.x);
879 vnwp.y = bound(m1.y, vnwp.y, m2.y);
880 vnwp.z = bound(m1.z, vnwp.z, m2.z);
881 e.nearestwaypoint_dist = vlen(ve - vnwp);
885 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
887 if(!e.navigation_dynamicgoal)
888 e.blacklisted = true;
892 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
897 if(e.navigation_dynamicgoal)
898 e.nearestwaypointtimeout = time + 2;
899 else if(autocvar_g_waypointeditor)
900 e.nearestwaypointtimeout = time + 3 + random() * 2;
902 nwp = e.nearestwaypoint;
905 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
907 if (nwp.wpcost < 10000000)
909 //te_wizspike(nwp.wpnearestpoint);
910 float cost = nwp.wpcost + waypoint_getdistancecost(nwp.wpnearestpoint, goal_org);
911 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
912 f = f * rangebias / (rangebias + cost);
913 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
914 if (navigation_bestrating < f)
916 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
917 navigation_bestrating = f;
918 navigation_bestgoal = e;
923 // adds an item to the the goal stack with the path to a given item
924 bool navigation_routetogoal(entity this, entity e, vector startposition)
926 // if there is no goal, just exit
932 // put the entity on the goal stack
933 //print("routetogoal ", etos(e), "\n");
934 navigation_pushroute(this, e);
936 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
938 this.wp_goal_prev1 = this.wp_goal_prev0;
939 this.wp_goal_prev0 = e;
943 if(e==this.navigation_jetpack_goal)
946 // if it can reach the goal there is nothing more to do
947 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
950 entity nearest_wp = NULL;
951 // see if there are waypoints describing a path to the item
952 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
954 e = e.nearestwaypoint;
958 e = e.enemy; // we already have added it, so...
963 if(nearest_wp && nearest_wp.enemy)
965 // often path can be optimized by not adding the nearest waypoint
966 if (this.goalentity.nearestwaypoint_dist < 8
967 || (!this.goalentity.navigation_dynamicgoal && navigation_item_islinked(nearest_wp.enemy, this.goalentity))
968 || (this.goalentity.navigation_dynamicgoal && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), (this.goalentity.absmin + this.goalentity.absmax) * 0.5, bot_navigation_movemode)))
969 e = nearest_wp.enemy;
974 // add the spawnfunc_waypoint to the path
975 navigation_pushroute(this, e);
985 // removes any currently touching waypoints from the goal stack
986 // (this is how bots detect if they reached a goal)
987 void navigation_poptouchedgoals(entity this)
991 m1 = org + this.mins;
992 m2 = org + this.maxs;
994 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
996 // make sure jumppad is really hit, don't rely on distance based checks
997 // as they may report a touch even if it didn't really happen
998 if(this.lastteleporttime>0)
999 if(time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1001 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1002 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1004 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1005 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1007 navigation_poproute(this);
1012 // If for some reason the bot is closer to the next goal, pop the current one
1013 if(this.goalstack01 && !wasfreed(this.goalstack01))
1014 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1015 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1016 if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
1018 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1019 navigation_poproute(this);
1020 // TODO this may also be a nice idea to do "early" (e.g. by
1021 // manipulating the vlen() comparisons) to shorten paths in
1022 // general - this would make bots walk more "on rails" than
1023 // "zigzagging" which they currently do with sufficiently
1024 // random-like waypoints, and thus can make a nice bot
1025 // personality property
1028 // Loose goal touching check when running
1029 if(this.aistatus & AI_STATUS_RUNNING)
1030 if(this.goalcurrent.classname=="waypoint")
1031 if(!(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1032 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
1034 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1036 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1037 if(trace_fraction==1)
1039 // Detect personal waypoints
1040 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1041 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1043 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1044 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1047 navigation_poproute(this);
1052 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1054 vector gc_min = this.goalcurrent.absmin;
1055 vector gc_max = this.goalcurrent.absmax;
1056 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1058 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1059 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1061 if(!boxesoverlap(m1, m2, gc_min, gc_max))
1064 if((this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1067 // Detect personal waypoints
1068 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1069 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1071 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1072 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1075 navigation_poproute(this);
1079 // begin a goal selection session (queries spawnfunc_waypoint network)
1080 void navigation_goalrating_start(entity this)
1082 if(this.aistatus & AI_STATUS_STUCK)
1085 this.navigation_jetpack_goal = NULL;
1086 navigation_bestrating = -1;
1087 navigation_clearroute(this);
1088 navigation_bestgoal = NULL;
1089 navigation_markroutes(this, NULL);
1092 // ends a goal selection session (updates goal stack to the best goal)
1093 void navigation_goalrating_end(entity this)
1095 if(this.aistatus & AI_STATUS_STUCK)
1098 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1099 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1101 // If the bot got stuck then try to reach the farthest waypoint
1102 if (!this.goalentity && autocvar_bot_wander_enable)
1104 if (!(this.aistatus & AI_STATUS_STUCK))
1106 LOG_DEBUG(this.netname, " cannot walk to any goal");
1107 this.aistatus |= AI_STATUS_STUCK;
1112 void botframe_updatedangerousobjects(float maxupdate)
1114 vector m1, m2, v, o;
1117 IL_EACH(g_waypoints, true,
1122 IL_EACH(g_bot_dodge, it.bot_dodge,
1125 v.x = bound(m1_x, v.x, m2_x);
1126 v.y = bound(m1_y, v.y, m2_y);
1127 v.z = bound(m1_z, v.z, m2_z);
1128 o = (it.absmin + it.absmax) * 0.5;
1129 d = waypoint_getdistancecost_simple(it.bot_dodgerating) - waypoint_getdistancecost(o, v);
1132 traceline(o, v, true, NULL);
1133 if (trace_fraction == 1)
1134 danger = danger + d;
1144 void navigation_unstuck(entity this)
1146 float search_radius = 1000;
1148 if (!autocvar_bot_wander_enable)
1151 if (!bot_waypoint_queue_owner)
1153 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1154 bot_waypoint_queue_owner = this;
1155 bot_waypoint_queue_bestgoal = NULL;
1156 bot_waypoint_queue_bestgoalrating = 0;
1159 if(bot_waypoint_queue_owner!=this)
1162 if (bot_waypoint_queue_goal)
1164 // evaluate the next goal on the queue
1165 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1166 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1167 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1169 if( d > bot_waypoint_queue_bestgoalrating)
1171 bot_waypoint_queue_bestgoalrating = d;
1172 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1175 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1177 if (!bot_waypoint_queue_goal)
1179 if (bot_waypoint_queue_bestgoal)
1181 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1182 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1183 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1184 this.aistatus &= ~AI_STATUS_STUCK;
1188 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1191 bot_waypoint_queue_owner = NULL;
1196 if(bot_strategytoken!=this)
1199 // build a new queue
1200 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1202 entity first = NULL;
1204 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1206 if(bot_waypoint_queue_goal)
1207 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1211 bot_waypoint_queue_goal = it;
1212 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1216 bot_waypoint_queue_goal = first;
1219 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1220 bot_waypoint_queue_owner = NULL;
1225 // Support for debugging tracewalk visually
1227 void debugresetnodes()
1229 debuglastnode = '0 0 0';
1232 void debugnode(entity this, vector node)
1234 if (!IS_PLAYER(this))
1237 if(debuglastnode=='0 0 0')
1239 debuglastnode = node;
1243 te_lightning2(NULL, node, debuglastnode);
1244 debuglastnode = node;
1247 void debugnodestatus(vector position, float status)
1253 case DEBUG_NODE_SUCCESS:
1256 case DEBUG_NODE_WARNING:
1259 case DEBUG_NODE_FAIL:
1266 te_customflash(position, 40, 2, c);
1269 // Support for debugging the goal stack visually
1272 .vector lastposition;
1274 // Debug the goal stack visually
1275 void debuggoalstack(entity this)
1280 if(this.goalcounter==0)goal=this.goalcurrent;
1281 else if(this.goalcounter==1)goal=this.goalstack01;
1282 else if(this.goalcounter==2)goal=this.goalstack02;
1283 else if(this.goalcounter==3)goal=this.goalstack03;
1284 else if(this.goalcounter==4)goal=this.goalstack04;
1285 else if(this.goalcounter==5)goal=this.goalstack05;
1286 else if(this.goalcounter==6)goal=this.goalstack06;
1287 else if(this.goalcounter==7)goal=this.goalstack07;
1288 else if(this.goalcounter==8)goal=this.goalstack08;
1289 else if(this.goalcounter==9)goal=this.goalstack09;
1290 else if(this.goalcounter==10)goal=this.goalstack10;
1291 else if(this.goalcounter==11)goal=this.goalstack11;
1292 else if(this.goalcounter==12)goal=this.goalstack12;
1293 else if(this.goalcounter==13)goal=this.goalstack13;
1294 else if(this.goalcounter==14)goal=this.goalstack14;
1295 else if(this.goalcounter==15)goal=this.goalstack15;
1296 else if(this.goalcounter==16)goal=this.goalstack16;
1297 else if(this.goalcounter==17)goal=this.goalstack17;
1298 else if(this.goalcounter==18)goal=this.goalstack18;
1299 else if(this.goalcounter==19)goal=this.goalstack19;
1300 else if(this.goalcounter==20)goal=this.goalstack20;
1301 else if(this.goalcounter==21)goal=this.goalstack21;
1302 else if(this.goalcounter==22)goal=this.goalstack22;
1303 else if(this.goalcounter==23)goal=this.goalstack23;
1304 else if(this.goalcounter==24)goal=this.goalstack24;
1305 else if(this.goalcounter==25)goal=this.goalstack25;
1306 else if(this.goalcounter==26)goal=this.goalstack26;
1307 else if(this.goalcounter==27)goal=this.goalstack27;
1308 else if(this.goalcounter==28)goal=this.goalstack28;
1309 else if(this.goalcounter==29)goal=this.goalstack29;
1310 else if(this.goalcounter==30)goal=this.goalstack30;
1311 else if(this.goalcounter==31)goal=this.goalstack31;
1316 this.goalcounter = 0;
1317 this.lastposition='0 0 0';
1321 if(this.lastposition=='0 0 0')
1324 org = this.lastposition;
1327 go = ( goal.absmin + goal.absmax ) * 0.5;
1328 te_lightning2(NULL, org, go);
1329 this.lastposition = go;