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
42 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
43 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, 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");
99 if (boxesoverlap(end, end2, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
102 if(autocvar_bot_debug_tracewalk)
103 debugnodestatus(org, DEBUG_NODE_SUCCESS);
105 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
108 if(autocvar_bot_debug_tracewalk)
115 dist = dist - stepdist;
116 traceline(org, org, MOVE_NORMAL, e);
119 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
121 // hazards blocking path
122 if(autocvar_bot_debug_tracewalk)
123 debugnodestatus(org, DEBUG_NODE_FAIL);
125 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
129 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
131 move = normalize(end - org);
132 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
134 if(autocvar_bot_debug_tracewalk)
135 debugnode(e, trace_endpos);
137 if (trace_fraction < 1)
140 org = trace_endpos + normalize(org - trace_endpos) * stepdist;
141 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
143 if(autocvar_bot_debug_tracewalk)
146 if(pointcontents(org) == CONTENT_EMPTY)
150 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
152 if(autocvar_bot_debug_tracewalk)
153 debugnodestatus(org, DEBUG_NODE_FAIL);
156 //print("tracewalk: ", vtos(start), " failed under water\n");
166 move = dir * stepdist + org;
167 tracebox(org, m1, m2, move, movemode, e);
169 if(autocvar_bot_debug_tracewalk)
170 debugnode(e, trace_endpos);
173 if (trace_fraction < 1)
175 // check if we can walk over this obstacle, possibly by jumpstepping
176 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
177 if (trace_fraction < 1 || trace_startsolid)
179 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
180 if (trace_fraction < 1 || trace_startsolid)
182 bool ladder_found = false;
183 IL_EACH(g_ladders, it.classname == "func_ladder",
186 if(boxesoverlap(org + jumpheight_vec + m1 + '-1 -1 -1', org + jumpheight_vec + m2 + '1 1 1', it.absmin, it.absmax))
187 if(boxesoverlap(end, end2, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
188 ladder_found = true; // can't return here ("Loop mutex held by tracewalk" error)
192 if(autocvar_bot_debug_tracewalk)
193 debugnodestatus(end, DEBUG_NODE_SUCCESS);
195 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
199 if(autocvar_bot_debug_tracewalk)
200 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
202 traceline( org, move, movemode, e);
204 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
208 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
210 nextmove = move + (dir * stepdist);
211 traceline( move, nextmove, movemode, e);
217 if(autocvar_bot_debug_tracewalk)
218 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
220 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
221 //te_explosion(trace_endpos);
222 //print(ftos(e.dphitcontentsmask), "\n");
223 return false; // failed
235 // trace down from stepheight as far as possible and move there,
236 // if this starts in solid we try again without the stepup, and
237 // if that also fails we assume it is a wall
238 // (this is the same logic as the Quake walkmove function used)
239 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
241 // moved successfully
245 c = pointcontents(org + '0 0 1');
246 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
256 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
258 // moved but didn't arrive at the intended destination
259 if(autocvar_bot_debug_tracewalk)
260 debugnodestatus(org, DEBUG_NODE_FAIL);
265 /////////////////////////////////////////////////////////////////////////////
267 /////////////////////////////////////////////////////////////////////////////
269 // completely empty the goal stack, used when deciding where to go
270 void navigation_clearroute(entity this)
272 this.goalcurrent_prev = this.goalcurrent;
273 this.goalcurrent_distance = 10000000;
274 this.goalcurrent_distance_time = 0;
275 //print("bot ", etos(this), " clear\n");
276 this.goalentity = NULL;
277 this.goalcurrent = NULL;
278 this.goalstack01 = NULL;
279 this.goalstack02 = NULL;
280 this.goalstack03 = NULL;
281 this.goalstack04 = NULL;
282 this.goalstack05 = NULL;
283 this.goalstack06 = NULL;
284 this.goalstack07 = NULL;
285 this.goalstack08 = NULL;
286 this.goalstack09 = NULL;
287 this.goalstack10 = NULL;
288 this.goalstack11 = NULL;
289 this.goalstack12 = NULL;
290 this.goalstack13 = NULL;
291 this.goalstack14 = NULL;
292 this.goalstack15 = NULL;
293 this.goalstack16 = NULL;
294 this.goalstack17 = NULL;
295 this.goalstack18 = NULL;
296 this.goalstack19 = NULL;
297 this.goalstack20 = NULL;
298 this.goalstack21 = NULL;
299 this.goalstack22 = NULL;
300 this.goalstack23 = NULL;
301 this.goalstack24 = NULL;
302 this.goalstack25 = NULL;
303 this.goalstack26 = NULL;
304 this.goalstack27 = NULL;
305 this.goalstack28 = NULL;
306 this.goalstack29 = NULL;
307 this.goalstack30 = NULL;
308 this.goalstack31 = NULL;
311 // add a new goal at the beginning of the stack
312 // (in other words: add a new prerequisite before going to the later goals)
313 // NOTE: when a waypoint is added, the WP gets pushed first, then the
314 // next-closest WP on the shortest path to the WP
315 // That means, if the stack overflows, the bot will know how to do the FIRST 32
316 // steps to the goal, and then recalculate the path.
317 void navigation_pushroute(entity this, entity e)
319 this.goalcurrent_prev = this.goalcurrent;
320 this.goalcurrent_distance = 10000000;
321 this.goalcurrent_distance_time = 0;
322 //print("bot ", etos(this), " push ", etos(e), "\n");
323 if(this.goalstack31 == this.goalentity)
324 this.goalentity = NULL;
325 this.goalstack31 = this.goalstack30;
326 this.goalstack30 = this.goalstack29;
327 this.goalstack29 = this.goalstack28;
328 this.goalstack28 = this.goalstack27;
329 this.goalstack27 = this.goalstack26;
330 this.goalstack26 = this.goalstack25;
331 this.goalstack25 = this.goalstack24;
332 this.goalstack24 = this.goalstack23;
333 this.goalstack23 = this.goalstack22;
334 this.goalstack22 = this.goalstack21;
335 this.goalstack21 = this.goalstack20;
336 this.goalstack20 = this.goalstack19;
337 this.goalstack19 = this.goalstack18;
338 this.goalstack18 = this.goalstack17;
339 this.goalstack17 = this.goalstack16;
340 this.goalstack16 = this.goalstack15;
341 this.goalstack15 = this.goalstack14;
342 this.goalstack14 = this.goalstack13;
343 this.goalstack13 = this.goalstack12;
344 this.goalstack12 = this.goalstack11;
345 this.goalstack11 = this.goalstack10;
346 this.goalstack10 = this.goalstack09;
347 this.goalstack09 = this.goalstack08;
348 this.goalstack08 = this.goalstack07;
349 this.goalstack07 = this.goalstack06;
350 this.goalstack06 = this.goalstack05;
351 this.goalstack05 = this.goalstack04;
352 this.goalstack04 = this.goalstack03;
353 this.goalstack03 = this.goalstack02;
354 this.goalstack02 = this.goalstack01;
355 this.goalstack01 = this.goalcurrent;
356 this.goalcurrent = e;
359 // remove first goal from stack
360 // (in other words: remove a prerequisite for reaching the later goals)
361 // (used when a spawnfunc_waypoint is reached)
362 void navigation_poproute(entity this)
364 this.goalcurrent_prev = this.goalcurrent;
365 this.goalcurrent_distance = 10000000;
366 this.goalcurrent_distance_time = 0;
367 //print("bot ", etos(this), " pop\n");
368 if(this.goalcurrent == this.goalentity)
369 this.goalentity = NULL;
370 this.goalcurrent = this.goalstack01;
371 this.goalstack01 = this.goalstack02;
372 this.goalstack02 = this.goalstack03;
373 this.goalstack03 = this.goalstack04;
374 this.goalstack04 = this.goalstack05;
375 this.goalstack05 = this.goalstack06;
376 this.goalstack06 = this.goalstack07;
377 this.goalstack07 = this.goalstack08;
378 this.goalstack08 = this.goalstack09;
379 this.goalstack09 = this.goalstack10;
380 this.goalstack10 = this.goalstack11;
381 this.goalstack11 = this.goalstack12;
382 this.goalstack12 = this.goalstack13;
383 this.goalstack13 = this.goalstack14;
384 this.goalstack14 = this.goalstack15;
385 this.goalstack15 = this.goalstack16;
386 this.goalstack16 = this.goalstack17;
387 this.goalstack17 = this.goalstack18;
388 this.goalstack18 = this.goalstack19;
389 this.goalstack19 = this.goalstack20;
390 this.goalstack20 = this.goalstack21;
391 this.goalstack21 = this.goalstack22;
392 this.goalstack22 = this.goalstack23;
393 this.goalstack23 = this.goalstack24;
394 this.goalstack24 = this.goalstack25;
395 this.goalstack25 = this.goalstack26;
396 this.goalstack26 = this.goalstack27;
397 this.goalstack27 = this.goalstack28;
398 this.goalstack28 = this.goalstack29;
399 this.goalstack29 = this.goalstack30;
400 this.goalstack30 = this.goalstack31;
401 this.goalstack31 = NULL;
404 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
407 dist = vlen(v - org);
410 traceline(v, org, true, ent);
411 if (trace_fraction == 1)
415 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, 0, bot_navigation_movemode))
420 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, 0, bot_navigation_movemode))
428 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
429 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
431 vector pm1 = ent.origin + ent.mins;
432 vector pm2 = ent.origin + ent.maxs;
434 // do two scans, because box test is cheaper
435 IL_EACH(g_waypoints, it != ent && it != except,
437 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
441 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
442 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
443 // TODO possibly make other code have the same support for bboxes
445 org = org + ent.tag_entity.origin;
446 if (navigation_testtracewalk)
452 if(!autocvar_g_waypointeditor && !ent.navigation_dynamicgoal)
454 waypoint_clearlinks(ent); // initialize wpXXmincost fields
455 IL_EACH(g_waypoints, it != ent,
459 vector wm1 = it.absmin;
460 vector wm2 = it.absmax;
461 v.x = bound(wm1_x, org.x, wm2_x);
462 v.y = bound(wm1_y, org.y, wm2_y);
463 v.z = bound(wm1_z, org.z, wm2_z);
467 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, 1050))
468 navigation_item_addlink(it, ent);
472 // box check failed, try walk
473 IL_EACH(g_waypoints, it != ent,
477 vector wm1 = it.origin + it.mins;
478 vector wm2 = it.origin + it.maxs;
479 v.x = bound(wm1_x, org.x, wm2_x);
480 v.y = bound(wm1_y, org.y, wm2_y);
481 v.z = bound(wm1_z, org.z, wm2_z);
485 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
487 bestdist = vlen(v - org);
493 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
495 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
496 if (autocvar_g_waypointeditor_auto)
498 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
500 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
505 // finds the waypoints near the bot initiating a navigation query
506 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
509 //navigation_testtracewalk = true;
512 IL_EACH(g_waypoints, !it.wpconsidered,
516 m1 = it.origin + it.mins;
517 m2 = it.origin + it.maxs;
519 v.x = bound(m1_x, v.x, m2_x);
520 v.y = bound(m1_y, v.y, m2_y);
522 v_height = m2.z - m1.z;
529 vector diff = v - this.origin;
530 diff.z = max(0, diff.z);
531 if(vdist(diff, <, maxdist))
533 it.wpconsidered = true;
534 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
536 it.wpnearestpoint = v;
537 it.wpcost = waypoint_gettravelcost(this.origin, v) + it.dmg;
544 //navigation_testtracewalk = false;
548 // updates a path link if a spawnfunc_waypoint link is better than the current one
549 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
558 v.x = bound(m1_x, p.x, m2_x);
559 v.y = bound(m1_y, p.y, m2_y);
560 v.z = bound(m1_z, p.z, m2_z);
564 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
565 cost += w.wp00mincost; // assuming teleport has exactly one destination
567 cost += waypoint_gettravelcost(p, v);
568 if (wp.wpcost > cost)
573 wp.wpnearestpoint = v;
577 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
578 void navigation_markroutes(entity this, entity fixed_source_waypoint)
583 IL_EACH(g_waypoints, true,
585 it.wpconsidered = false;
586 it.wpnearestpoint = '0 0 0';
587 it.wpcost = 10000000;
592 if(fixed_source_waypoint)
594 fixed_source_waypoint.wpconsidered = true;
595 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
596 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
597 fixed_source_waypoint.wpfire = 1;
598 fixed_source_waypoint.enemy = NULL;
602 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
603 // as this search is expensive we will use lower values if the bot is on the air
604 float increment, maxdistance;
605 if(IS_ONGROUND(this))
616 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
619 bool searching = true;
623 IL_EACH(g_waypoints, it.wpfire,
628 p = it.wpnearestpoint;
630 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
631 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
632 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
633 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
634 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
635 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
636 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
637 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
638 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
639 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
640 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
641 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
642 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
643 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
644 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
645 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
646 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
647 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
648 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
649 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
650 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
651 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
652 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
653 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
654 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
655 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
656 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
657 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
658 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
659 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
660 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
661 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
662 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
667 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
668 void navigation_markroutes_inverted(entity fixed_source_waypoint)
672 IL_EACH(g_waypoints, true,
674 it.wpconsidered = false;
675 it.wpnearestpoint = '0 0 0';
676 it.wpcost = 10000000;
681 if(fixed_source_waypoint)
683 fixed_source_waypoint.wpconsidered = true;
684 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
685 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
686 fixed_source_waypoint.wpfire = 1;
687 fixed_source_waypoint.enemy = NULL;
691 error("need to start with a waypoint\n");
694 bool searching = true;
698 IL_EACH(g_waypoints, it.wpfire,
702 cost = it.wpcost; // cost to walk from it to home
703 p = it.wpnearestpoint;
705 IL_EACH(g_waypoints, it != wp,
707 if(!waypoint_islinked(it, wp))
709 cost2 = cost + it.dmg;
710 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
716 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
717 void navigation_routerating(entity this, entity e, float f, float rangebias)
725 rangebias = waypoint_getlinearcost(rangebias);
726 f = waypoint_getlinearcost(f);
730 bool rate_wps = false;
731 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
736 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
737 int t = pointcontents(trace_endpos + '0 0 1');
738 if(t != CONTENT_SOLID )
740 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
742 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
750 entity best_wp = NULL;
751 float best_dist = 10000;
752 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
753 && vdist(it.origin - this.origin, >, 100)
754 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
756 float dist = vlen(it.origin - theEnemy.origin);
757 if (dist < best_dist)
769 vector goal_org = (e.absmin + e.absmax) * 0.5;
771 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
773 // Evaluate path using jetpack
775 if(this.items & IT_JETPACK)
776 if(autocvar_bot_ai_navigation_jetpack)
777 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
779 vector pointa, pointb;
781 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
784 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
785 pointa = trace_endpos - '0 0 1';
788 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
789 pointb = trace_endpos - '0 0 1';
791 // Can I see these two points from the sky?
792 traceline(pointa, pointb, MOVE_NORMAL, this);
794 if(trace_fraction==1)
796 LOG_DEBUG("jetpack ai: can bridge these two points");
798 // Lower the altitude of these points as much as possible
799 float zdistance, xydistance, cost, t, fuel;
800 vector down, npa, npb;
802 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
808 if(npa.z<=this.absmax.z)
811 if(npb.z<=e.absmax.z)
814 traceline(npa, npb, MOVE_NORMAL, this);
815 if(trace_fraction==1)
821 while(trace_fraction == 1);
824 // Rough estimation of fuel consumption
825 // (ignores acceleration and current xyz velocity)
826 xydistance = vlen(pointa - pointb);
827 zdistance = fabs(pointa.z - this.origin.z);
829 t = zdistance / autocvar_g_jetpack_maxspeed_up;
830 t += xydistance / autocvar_g_jetpack_maxspeed_side;
831 fuel = t * autocvar_g_jetpack_fuel * 0.8;
833 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
836 if(this.ammo_fuel>fuel)
839 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
840 // - between air and ground speeds)
842 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
843 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
846 // Compare against other goals
847 f = f * rangebias / (rangebias + cost);
849 if (navigation_bestrating < f)
851 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
852 navigation_bestrating = f;
853 navigation_bestgoal = e;
854 this.navigation_jetpack_goal = e;
855 this.navigation_jetpack_point = pointb;
863 //te_wizspike(e.origin);
866 // update the cached spawnfunc_waypoint link on a dynamic item entity
867 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
873 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
874 e.nearestwaypoint = NULL;
876 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
877 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
879 nwp = navigation_findnearestwaypoint(e, true);
882 e.nearestwaypoint = nwp;
884 vector m1 = nwp.absmin, m2 = nwp.absmax;
885 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
886 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
887 vector ve = (e.absmin - e.absmax) * 0.5;
888 ve.x = bound(m1.x, ve.x, m2.x);
889 ve.y = bound(m1.y, ve.y, m2.y);
890 ve.z = bound(m1.z, ve.z, m2.z);
892 m1 = e.absmin; m2 = e.absmax;
893 m1.x = e.origin.x; m1.y = e.origin.y;
894 m2.x = e.origin.x; m2.y = e.origin.y;
895 vector vnwp = nwp.origin;
896 vnwp.x = bound(m1.x, vnwp.x, m2.x);
897 vnwp.y = bound(m1.y, vnwp.y, m2.y);
898 vnwp.z = bound(m1.z, vnwp.z, m2.z);
899 e.nearestwaypoint_dist = vlen(ve - vnwp);
903 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
905 if(!e.navigation_dynamicgoal)
906 e.blacklisted = true;
910 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
915 if(e.navigation_dynamicgoal)
916 e.nearestwaypointtimeout = time + 2;
917 else if(autocvar_g_waypointeditor)
918 e.nearestwaypointtimeout = time + 3 + random() * 2;
920 nwp = e.nearestwaypoint;
923 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
925 if (nwp.wpcost < 10000000)
927 //te_wizspike(nwp.wpnearestpoint);
928 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org);
929 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
930 f = f * rangebias / (rangebias + cost);
931 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
932 if (navigation_bestrating < f)
934 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
935 navigation_bestrating = f;
936 navigation_bestgoal = e;
941 // adds an item to the the goal stack with the path to a given item
942 bool navigation_routetogoal(entity this, entity e, vector startposition)
944 // if there is no goal, just exit
948 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
950 // force teleport destination as route destination
957 // put the entity on the goal stack
958 //print("routetogoal ", etos(e), "\n");
959 navigation_pushroute(this, e);
961 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
963 this.wp_goal_prev1 = this.wp_goal_prev0;
964 this.wp_goal_prev0 = e;
968 if(e==this.navigation_jetpack_goal)
971 // if it can reach the goal there is nothing more to do
972 vector dest = (e.absmin + e.absmax) * 0.5;
974 float dest_height = e.absmax.z - e.absmin.z;
975 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
978 entity nearest_wp = NULL;
979 // see if there are waypoints describing a path to the item
980 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
982 e = e.nearestwaypoint;
986 e = e.enemy; // we already have added it, so...
991 if(nearest_wp && nearest_wp.enemy)
993 // often path can be optimized by not adding the nearest waypoint
994 if (this.goalentity.nearestwaypoint_dist < 8)
995 e = nearest_wp.enemy;
998 if (this.goalentity.navigation_dynamicgoal)
1000 vector dest = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
1001 dest.z = this.goalentity.absmin.z;
1002 float dest_height = this.goalentity.absmax.z - this.goalentity.absmin.z;
1003 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1004 e = nearest_wp.enemy;
1006 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1007 e = nearest_wp.enemy;
1013 // add the spawnfunc_waypoint to the path
1014 navigation_pushroute(this, e);
1024 // removes any currently touching waypoints from the goal stack
1025 // (this is how bots detect if they reached a goal)
1026 void navigation_poptouchedgoals(entity this)
1028 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1030 // make sure jumppad is really hit, don't rely on distance based checks
1031 // as they may report a touch even if it didn't really happen
1032 if(this.lastteleporttime > 0
1033 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1035 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1036 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1038 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1039 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1041 navigation_poproute(this);
1047 // If for some reason the bot is closer to the next goal, pop the current one
1048 if(this.goalstack01 && !wasfreed(this.goalstack01))
1049 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1050 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1052 vector dest = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
1053 dest.z = this.goalstack01.absmin.z;
1054 float dest_height = this.goalstack01.absmax.z - this.goalstack01.absmin.z;
1055 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1057 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1058 navigation_poproute(this);
1059 // TODO this may also be a nice idea to do "early" (e.g. by
1060 // manipulating the vlen() comparisons) to shorten paths in
1061 // general - this would make bots walk more "on rails" than
1062 // "zigzagging" which they currently do with sufficiently
1063 // random-like waypoints, and thus can make a nice bot
1064 // personality property
1068 // Loose goal touching check when running
1069 if(this.aistatus & AI_STATUS_RUNNING)
1070 if(this.goalcurrent.classname=="waypoint")
1071 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
1073 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1075 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1076 if(trace_fraction==1)
1078 // Detect personal waypoints
1079 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1080 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1082 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1083 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1086 navigation_poproute(this);
1091 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1093 vector gc_min = this.goalcurrent.absmin;
1094 vector gc_max = this.goalcurrent.absmax;
1095 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1097 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1098 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1100 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1103 // Detect personal waypoints
1104 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1105 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1107 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1108 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1111 navigation_poproute(this);
1115 // begin a goal selection session (queries spawnfunc_waypoint network)
1116 void navigation_goalrating_start(entity this)
1118 if(this.aistatus & AI_STATUS_STUCK)
1121 this.navigation_jetpack_goal = NULL;
1122 navigation_bestrating = -1;
1123 navigation_clearroute(this);
1124 navigation_bestgoal = NULL;
1125 navigation_markroutes(this, NULL);
1128 // ends a goal selection session (updates goal stack to the best goal)
1129 void navigation_goalrating_end(entity this)
1131 if(this.aistatus & AI_STATUS_STUCK)
1134 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1135 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1137 // If the bot got stuck then try to reach the farthest waypoint
1138 if (!this.goalentity && autocvar_bot_wander_enable)
1140 if (!(this.aistatus & AI_STATUS_STUCK))
1142 LOG_DEBUG(this.netname, " cannot walk to any goal");
1143 this.aistatus |= AI_STATUS_STUCK;
1148 void botframe_updatedangerousobjects(float maxupdate)
1150 vector m1, m2, v, o;
1153 IL_EACH(g_waypoints, true,
1158 IL_EACH(g_bot_dodge, it.bot_dodge,
1161 v.x = bound(m1_x, v.x, m2_x);
1162 v.y = bound(m1_y, v.y, m2_y);
1163 v.z = bound(m1_z, v.z, m2_z);
1164 o = (it.absmin + it.absmax) * 0.5;
1165 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v);
1168 traceline(o, v, true, NULL);
1169 if (trace_fraction == 1)
1170 danger = danger + d;
1180 void navigation_unstuck(entity this)
1182 float search_radius = 1000;
1184 if (!autocvar_bot_wander_enable)
1187 if (!bot_waypoint_queue_owner)
1189 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1190 bot_waypoint_queue_owner = this;
1191 bot_waypoint_queue_bestgoal = NULL;
1192 bot_waypoint_queue_bestgoalrating = 0;
1195 if(bot_waypoint_queue_owner!=this)
1198 if (bot_waypoint_queue_goal)
1200 // evaluate the next goal on the queue
1201 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1202 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1203 vector dest = (bot_waypoint_queue_goal.absmin + bot_waypoint_queue_goal.absmax) * 0.5;
1204 dest.z = bot_waypoint_queue_goal.absmin.z;
1205 float dest_height = bot_waypoint_queue_goal.absmax.z - bot_waypoint_queue_goal.absmin.z;
1206 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1208 if( d > bot_waypoint_queue_bestgoalrating)
1210 bot_waypoint_queue_bestgoalrating = d;
1211 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1214 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1216 if (!bot_waypoint_queue_goal)
1218 if (bot_waypoint_queue_bestgoal)
1220 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1221 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1222 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1223 this.aistatus &= ~AI_STATUS_STUCK;
1227 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1230 bot_waypoint_queue_owner = NULL;
1235 if(bot_strategytoken!=this)
1238 // build a new queue
1239 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1241 entity first = NULL;
1243 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1245 if(bot_waypoint_queue_goal)
1246 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1250 bot_waypoint_queue_goal = it;
1251 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1255 bot_waypoint_queue_goal = first;
1258 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1259 bot_waypoint_queue_owner = NULL;
1264 // Support for debugging tracewalk visually
1266 void debugresetnodes()
1268 debuglastnode = '0 0 0';
1271 void debugnode(entity this, vector node)
1273 if (!IS_PLAYER(this))
1276 if(debuglastnode=='0 0 0')
1278 debuglastnode = node;
1282 te_lightning2(NULL, node, debuglastnode);
1283 debuglastnode = node;
1286 void debugnodestatus(vector position, float status)
1292 case DEBUG_NODE_SUCCESS:
1295 case DEBUG_NODE_WARNING:
1298 case DEBUG_NODE_FAIL:
1305 te_customflash(position, 40, 2, c);
1308 // Support for debugging the goal stack visually
1311 .vector lastposition;
1313 // Debug the goal stack visually
1314 void debuggoalstack(entity this)
1319 if(this.goalcounter==0)goal=this.goalcurrent;
1320 else if(this.goalcounter==1)goal=this.goalstack01;
1321 else if(this.goalcounter==2)goal=this.goalstack02;
1322 else if(this.goalcounter==3)goal=this.goalstack03;
1323 else if(this.goalcounter==4)goal=this.goalstack04;
1324 else if(this.goalcounter==5)goal=this.goalstack05;
1325 else if(this.goalcounter==6)goal=this.goalstack06;
1326 else if(this.goalcounter==7)goal=this.goalstack07;
1327 else if(this.goalcounter==8)goal=this.goalstack08;
1328 else if(this.goalcounter==9)goal=this.goalstack09;
1329 else if(this.goalcounter==10)goal=this.goalstack10;
1330 else if(this.goalcounter==11)goal=this.goalstack11;
1331 else if(this.goalcounter==12)goal=this.goalstack12;
1332 else if(this.goalcounter==13)goal=this.goalstack13;
1333 else if(this.goalcounter==14)goal=this.goalstack14;
1334 else if(this.goalcounter==15)goal=this.goalstack15;
1335 else if(this.goalcounter==16)goal=this.goalstack16;
1336 else if(this.goalcounter==17)goal=this.goalstack17;
1337 else if(this.goalcounter==18)goal=this.goalstack18;
1338 else if(this.goalcounter==19)goal=this.goalstack19;
1339 else if(this.goalcounter==20)goal=this.goalstack20;
1340 else if(this.goalcounter==21)goal=this.goalstack21;
1341 else if(this.goalcounter==22)goal=this.goalstack22;
1342 else if(this.goalcounter==23)goal=this.goalstack23;
1343 else if(this.goalcounter==24)goal=this.goalstack24;
1344 else if(this.goalcounter==25)goal=this.goalstack25;
1345 else if(this.goalcounter==26)goal=this.goalstack26;
1346 else if(this.goalcounter==27)goal=this.goalstack27;
1347 else if(this.goalcounter==28)goal=this.goalstack28;
1348 else if(this.goalcounter==29)goal=this.goalstack29;
1349 else if(this.goalcounter==30)goal=this.goalstack30;
1350 else if(this.goalcounter==31)goal=this.goalstack31;
1355 this.goalcounter = 0;
1356 this.lastposition='0 0 0';
1360 if(this.lastposition=='0 0 0')
1363 org = this.lastposition;
1366 go = ( goal.absmin + goal.absmax ) * 0.5;
1367 te_lightning2(NULL, org, go);
1368 this.lastposition = go;