1 #include "navigation.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
8 #include "waypoints.qh"
10 #include <common/t_items.qh>
12 #include <common/items/_mod.qh>
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/triggers/trigger/jumppads.qh>
20 void navigation_goalrating_timeout_set(entity this)
22 if(IS_MOVABLE(this.goalentity))
23 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
25 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
28 void navigation_goalrating_timeout_force(entity this)
30 this.bot_strategytime = 0;
33 bool navigation_goalrating_timeout(entity this)
35 return this.bot_strategytime < time;
38 void navigation_dynamicgoal_init(entity this, bool initially_static)
40 this.navigation_dynamicgoal = true;
41 this.bot_basewaypoint = this.nearestwaypoint;
43 this.nearestwaypointtimeout = -1;
45 this.nearestwaypointtimeout = time;
48 void navigation_dynamicgoal_set(entity this)
50 this.nearestwaypointtimeout = time;
53 void navigation_dynamicgoal_unset(entity this)
55 if(this.bot_basewaypoint)
56 this.nearestwaypoint = this.bot_basewaypoint;
57 this.nearestwaypointtimeout = -1;
60 bool navigation_check_submerged_state(entity ent, vector pos)
64 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
65 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
66 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
69 submerged = SUBMERGED(pos);
70 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
71 // is actually submerged because often they are inside some solid.
72 // That's why submerged state is saved now that we know current pos is
73 // not stuck in solid (previous tracewalk call to this pos was successfully)
74 if(!ent.navigation_dynamicgoal)
75 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
80 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
82 IL_EACH(g_ladders, it.classname == "func_ladder",
85 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
86 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
89 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
90 tracebox(org, m1, m2, top, movemode, e);
91 if(trace_fraction == 1)
98 vector resurface_limited(vector org, float lim, vector m1)
100 if (WETFEET(org + eZ * (lim - org.z)))
104 float RES_min_h = org.z;
105 float RES_max_h = lim;
107 org.z = 0.5 * (RES_min_h + RES_max_h);
112 } while (RES_max_h - RES_min_h >= 1);
117 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
120 #define NAV_SWIM_ONWATER 1
121 #define NAV_SWIM_UNDERWATER 2
123 // rough simulation of walking from one point to another to test if a path
124 // can be traveled, used for waypoint linking and havocbot
125 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
126 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
128 if(autocvar_bot_debug_tracewalk)
135 vector flatdir = end - start;
137 float flatdist = vlen(flatdir);
138 flatdir = normalize(flatdir);
140 bool ignorehazards = false;
143 // Analyze starting point
144 traceline(start, start, MOVE_NORMAL, e);
145 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
146 ignorehazards = true;
148 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
149 if (trace_startsolid)
152 if(autocvar_bot_debug_tracewalk)
153 debugnodestatus(start, DEBUG_NODE_FAIL);
155 //print("tracewalk: ", vtos(start), " is a bad start\n");
161 end2.z += end_height;
163 vector fixed_end = end;
166 if (flatdist > 0 && WETFEET(org))
169 nav_action = NAV_SWIM_UNDERWATER;
172 // tracebox down by player's height
173 // useful to know if water level is so low that bot can still walk
174 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
175 if (SUBMERGED(trace_endpos))
178 nav_action = NAV_SWIM_UNDERWATER;
181 nav_action = NAV_WALK;
185 nav_action = NAV_WALK;
193 if (org.z > end2.z + 1)
195 tracebox(org, m1, m2, end2, movemode, e);
197 if (org.z > end2.z + 1)
200 else if (org.z < end.z - 1)
202 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
203 if (SUBMERGED(trace_endpos))
205 vector v = trace_endpos;
206 tracebox(v, m1, m2, end, movemode, e);
207 if(trace_endpos.z >= end.z - 1)
209 RESURFACE_LIMITED(v, trace_endpos.z);
213 else if (trace_endpos.z > org.z - jumpheight_vec.z)
214 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
216 if (org.z < end.z - 1)
223 if(autocvar_bot_debug_tracewalk)
226 debugnodestatus(org, DEBUG_NODE_SUCCESS);
229 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
234 if(autocvar_bot_debug_tracewalk)
240 if (stepdist > flatdist)
242 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
244 // can't use movement direction here to calculate move because of
245 // precision errors especially when direction has a high enough z value
246 //water_dir = normalize(water_end - org);
247 //move = org + water_dir * stepdist;
248 fixed_end.z = bound(end.z, org.z, end2.z);
249 if (stepdist == flatdist) {
253 move = org + (fixed_end - org) * (stepdist / flatdist);
254 flatdist = vlen(vec2(fixed_end - move));
257 else // horiz. direction
259 flatdist -= stepdist;
260 move = org + flatdir * stepdist;
263 if(nav_action == NAV_SWIM_ONWATER)
265 tracebox(org, m1, m2, move, movemode, e); // swim
268 if (trace_fraction < 1)
271 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
273 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
276 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
278 if(autocvar_bot_debug_tracewalk)
281 debugnodestatus(org, DEBUG_NODE_SUCCESS);
284 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
288 if(autocvar_bot_debug_tracewalk)
289 debugnodestatus(org, DEBUG_NODE_FAIL);
292 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
303 if (org.z <= move.z) // going horiz.
305 tracebox(trace_endpos, m1, m2, move, movemode, e);
307 nav_action = NAV_WALK;
312 if (org.z <= move.z) // going horiz.
315 nav_action = NAV_SWIM_ONWATER;
321 nav_action = NAV_SWIM_UNDERWATER;
323 nav_action = NAV_SWIM_ONWATER;
326 else if(nav_action == NAV_SWIM_UNDERWATER)
328 if (move.z >= org.z) // swimming upwards or horiz.
330 tracebox(org, m1, m2, move, movemode, e); // swim
332 bool stepswum = false;
335 if (trace_fraction < 1)
338 vector stepswim_move = move + stepheightvec;
339 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
340 stepswim_move.z = end2.z;
342 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
345 if (trace_startsolid)
347 if(autocvar_bot_debug_tracewalk)
348 debugnodestatus(org, DEBUG_NODE_FAIL);
350 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
354 if (trace_fraction < 1)
356 float org_z_prev = org.z;
357 RESURFACE_LIMITED(org, end2.z);
358 if(org.z == org_z_prev)
360 if(autocvar_bot_debug_tracewalk)
361 debugnodestatus(org, DEBUG_NODE_FAIL);
363 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
367 nav_action = NAV_SWIM_UNDERWATER;
369 nav_action = NAV_SWIM_ONWATER;
371 // we didn't advance horiz. in this step, flatdist decrease should be reverted
372 // but we can't do it properly right now... apply this workaround instead
390 if (!WETFEET(trace_endpos))
392 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
393 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
394 if (!stepswum && SUBMERGED(trace_endpos))
396 RESURFACE_LIMITED(trace_endpos, end2.z);
398 nav_action = NAV_SWIM_ONWATER;
404 nav_action = NAV_WALK;
410 nav_action = NAV_SWIM_UNDERWATER;
413 else //if (move.z < org.z) // swimming downwards
415 tracebox(org, m1, m2, move, movemode, e); // swim
418 if (trace_fraction < 1)
421 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
424 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
426 if(autocvar_bot_debug_tracewalk)
427 debugnodestatus(move, DEBUG_NODE_FAIL);
429 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
441 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
443 // stepswim caused upwards direction
444 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
445 if (!SUBMERGED(trace_endpos))
448 nav_action = NAV_WALK;
455 nav_action = NAV_SWIM_UNDERWATER;
459 else if(nav_action == NAV_WALK)
462 tracebox(org, m1, m2, move, movemode, e);
464 if(autocvar_bot_debug_tracewalk)
465 debugnode(e, trace_endpos);
468 if (trace_fraction < 1)
470 // check if we can walk over this obstacle, possibly by jumpstepping
471 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
472 if (trace_fraction < 1 || trace_startsolid)
474 if (trace_startsolid) // hit ceiling above org
476 // reduce stepwalk height
477 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
478 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
480 else //if (trace_fraction < 1)
482 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
483 if (trace_startsolid) // hit ceiling above org
485 // reduce jumpstepwalk height
486 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
487 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
491 if (trace_fraction < 1)
493 vector v = trace_endpos;
494 v.z = org.z + jumpheight_vec.z;
495 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
497 if(autocvar_bot_debug_tracewalk)
500 debugnodestatus(v, DEBUG_NODE_SUCCESS);
503 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
507 if(autocvar_bot_debug_tracewalk)
508 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
510 traceline( org, move, movemode, e);
512 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
516 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
518 nextmove = move + (flatdir * stepdist);
519 traceline( move, nextmove, movemode, e);
522 flatdist = vlen(vec2(end - move));
526 if(autocvar_bot_debug_tracewalk)
527 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
529 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
530 //te_explosion(trace_endpos);
531 //print(ftos(e.dphitcontentsmask), "\n");
532 return false; // failed
544 // trace down from stepheight as far as possible and move there,
545 // if this starts in solid we try again without the stepup, and
546 // if that also fails we assume it is a wall
547 // (this is the same logic as the Quake walkmove function used)
548 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
556 if(autocvar_bot_debug_tracewalk)
558 debugnode(e, trace_endpos);
559 debugnodestatus(org, DEBUG_NODE_FAIL);
562 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
569 if(move.z >= end2.z && org.z < end2.z)
574 if(org.z > move.z - 1 || !SUBMERGED(org))
576 nav_action = NAV_WALK;
580 // ended up submerged while walking
581 if(autocvar_bot_debug_tracewalk)
584 RESURFACE_LIMITED(org, move.z);
585 nav_action = NAV_SWIM_ONWATER;
590 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
592 // moved but didn't arrive at the intended destination
593 if(autocvar_bot_debug_tracewalk)
594 debugnodestatus(org, DEBUG_NODE_FAIL);
599 /////////////////////////////////////////////////////////////////////////////
601 /////////////////////////////////////////////////////////////////////////////
603 // completely empty the goal stack, used when deciding where to go
604 void navigation_clearroute(entity this)
606 this.goalcurrent_prev = this.goalcurrent;
607 this.goalcurrent_distance = 10000000;
608 this.goalcurrent_distance_time = 0;
609 //print("bot ", etos(this), " clear\n");
610 this.goalentity = NULL;
611 this.goalcurrent = NULL;
612 this.goalstack01 = NULL;
613 this.goalstack02 = NULL;
614 this.goalstack03 = NULL;
615 this.goalstack04 = NULL;
616 this.goalstack05 = NULL;
617 this.goalstack06 = NULL;
618 this.goalstack07 = NULL;
619 this.goalstack08 = NULL;
620 this.goalstack09 = NULL;
621 this.goalstack10 = NULL;
622 this.goalstack11 = NULL;
623 this.goalstack12 = NULL;
624 this.goalstack13 = NULL;
625 this.goalstack14 = NULL;
626 this.goalstack15 = NULL;
627 this.goalstack16 = NULL;
628 this.goalstack17 = NULL;
629 this.goalstack18 = NULL;
630 this.goalstack19 = NULL;
631 this.goalstack20 = NULL;
632 this.goalstack21 = NULL;
633 this.goalstack22 = NULL;
634 this.goalstack23 = NULL;
635 this.goalstack24 = NULL;
636 this.goalstack25 = NULL;
637 this.goalstack26 = NULL;
638 this.goalstack27 = NULL;
639 this.goalstack28 = NULL;
640 this.goalstack29 = NULL;
641 this.goalstack30 = NULL;
642 this.goalstack31 = NULL;
645 // add a new goal at the beginning of the stack
646 // (in other words: add a new prerequisite before going to the later goals)
647 // NOTE: when a waypoint is added, the WP gets pushed first, then the
648 // next-closest WP on the shortest path to the WP
649 // That means, if the stack overflows, the bot will know how to do the FIRST 32
650 // steps to the goal, and then recalculate the path.
651 void navigation_pushroute(entity this, entity e)
653 this.goalcurrent_prev = this.goalcurrent;
654 this.goalcurrent_distance = 10000000;
655 this.goalcurrent_distance_time = 0;
656 //print("bot ", etos(this), " push ", etos(e), "\n");
657 if(this.goalstack31 == this.goalentity)
658 this.goalentity = NULL;
659 this.goalstack31 = this.goalstack30;
660 this.goalstack30 = this.goalstack29;
661 this.goalstack29 = this.goalstack28;
662 this.goalstack28 = this.goalstack27;
663 this.goalstack27 = this.goalstack26;
664 this.goalstack26 = this.goalstack25;
665 this.goalstack25 = this.goalstack24;
666 this.goalstack24 = this.goalstack23;
667 this.goalstack23 = this.goalstack22;
668 this.goalstack22 = this.goalstack21;
669 this.goalstack21 = this.goalstack20;
670 this.goalstack20 = this.goalstack19;
671 this.goalstack19 = this.goalstack18;
672 this.goalstack18 = this.goalstack17;
673 this.goalstack17 = this.goalstack16;
674 this.goalstack16 = this.goalstack15;
675 this.goalstack15 = this.goalstack14;
676 this.goalstack14 = this.goalstack13;
677 this.goalstack13 = this.goalstack12;
678 this.goalstack12 = this.goalstack11;
679 this.goalstack11 = this.goalstack10;
680 this.goalstack10 = this.goalstack09;
681 this.goalstack09 = this.goalstack08;
682 this.goalstack08 = this.goalstack07;
683 this.goalstack07 = this.goalstack06;
684 this.goalstack06 = this.goalstack05;
685 this.goalstack05 = this.goalstack04;
686 this.goalstack04 = this.goalstack03;
687 this.goalstack03 = this.goalstack02;
688 this.goalstack02 = this.goalstack01;
689 this.goalstack01 = this.goalcurrent;
690 this.goalcurrent = e;
693 // remove first goal from stack
694 // (in other words: remove a prerequisite for reaching the later goals)
695 // (used when a spawnfunc_waypoint is reached)
696 void navigation_poproute(entity this)
698 this.goalcurrent_prev = this.goalcurrent;
699 this.goalcurrent_distance = 10000000;
700 this.goalcurrent_distance_time = 0;
701 //print("bot ", etos(this), " pop\n");
702 if(this.goalcurrent == this.goalentity)
703 this.goalentity = NULL;
704 this.goalcurrent = this.goalstack01;
705 this.goalstack01 = this.goalstack02;
706 this.goalstack02 = this.goalstack03;
707 this.goalstack03 = this.goalstack04;
708 this.goalstack04 = this.goalstack05;
709 this.goalstack05 = this.goalstack06;
710 this.goalstack06 = this.goalstack07;
711 this.goalstack07 = this.goalstack08;
712 this.goalstack08 = this.goalstack09;
713 this.goalstack09 = this.goalstack10;
714 this.goalstack10 = this.goalstack11;
715 this.goalstack11 = this.goalstack12;
716 this.goalstack12 = this.goalstack13;
717 this.goalstack13 = this.goalstack14;
718 this.goalstack14 = this.goalstack15;
719 this.goalstack15 = this.goalstack16;
720 this.goalstack16 = this.goalstack17;
721 this.goalstack17 = this.goalstack18;
722 this.goalstack18 = this.goalstack19;
723 this.goalstack19 = this.goalstack20;
724 this.goalstack20 = this.goalstack21;
725 this.goalstack21 = this.goalstack22;
726 this.goalstack22 = this.goalstack23;
727 this.goalstack23 = this.goalstack24;
728 this.goalstack24 = this.goalstack25;
729 this.goalstack25 = this.goalstack26;
730 this.goalstack26 = this.goalstack27;
731 this.goalstack27 = this.goalstack28;
732 this.goalstack28 = this.goalstack29;
733 this.goalstack29 = this.goalstack30;
734 this.goalstack30 = this.goalstack31;
735 this.goalstack31 = NULL;
738 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
739 // waypoint destination coordinates instead of v (only useful for box waypoints)
740 // for normal waypoints v2 == v and v2_height == 0
741 float navigation_waypoint_will_link(vector v, vector org, entity ent, vector v2, float v2_height, vector o2, float o2_height, float walkfromwp, float bestdist)
743 if (vdist(v - org, <, bestdist))
745 traceline(v, org, true, ent);
746 if (trace_fraction == 1)
750 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
755 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
763 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
764 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
767 ent = ent.tag_entity;
769 vector pm1 = ent.origin + ent.mins;
770 vector pm2 = ent.origin + ent.maxs;
772 // do two scans, because box test is cheaper
773 IL_EACH(g_waypoints, it != ent && it != except,
775 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
777 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
779 waypoint_clearlinks(ent); // initialize wpXXmincost fields
780 navigation_item_addlink(it, ent);
786 vector org = ent.origin;
787 if (navigation_testtracewalk)
791 vector v = '0 0 0', v2 = '0 0 0';
794 if(ent.size && !IS_PLAYER(ent))
796 org += 0.5 * (ent.mins + ent.maxs);
797 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
800 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
802 waypoint_clearlinks(ent); // initialize wpXXmincost fields
803 IL_EACH(g_waypoints, it != ent,
805 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
808 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
809 if(vdist(v2 - org, <, 1050))
810 if(tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
811 navigation_item_addlink(it, ent);
815 // box check failed, try walk
816 IL_EACH(g_waypoints, it != ent,
818 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
822 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
824 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
825 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
827 bestdist = vlen(v - org);
833 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
835 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
836 if (autocvar_g_waypointeditor_auto)
838 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
840 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
845 // finds the waypoints near the bot initiating a navigation query
846 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
849 //navigation_testtracewalk = true;
852 IL_EACH(g_waypoints, !it.wpconsidered,
854 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
856 vector diff = v - this.origin;
857 diff.z = max(0, diff.z);
858 if(vdist(diff, <, maxdist))
860 it.wpconsidered = true;
861 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
863 it.wpnearestpoint = v;
864 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
871 //navigation_testtracewalk = false;
875 // updates a path link if a spawnfunc_waypoint link is better than the current one
876 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
882 m1 = wp.origin + wp.mins;
883 m2 = wp.origin + wp.maxs;
884 v.x = bound(m1_x, p.x, m2_x);
885 v.y = bound(m1_y, p.y, m2_y);
886 v.z = bound(m1_z, p.z, m2_z);
890 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
891 cost += w.wp00mincost; // assuming teleport has exactly one destination
893 cost += waypoint_gettravelcost(p, v, w, wp);
894 if (wp.wpcost > cost)
899 wp.wpnearestpoint = v;
903 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
904 void navigation_markroutes(entity this, entity fixed_source_waypoint)
909 IL_EACH(g_waypoints, true,
911 it.wpconsidered = false;
912 it.wpnearestpoint = '0 0 0';
913 it.wpcost = 10000000;
918 if(fixed_source_waypoint)
920 fixed_source_waypoint.wpconsidered = true;
921 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
922 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
923 fixed_source_waypoint.wpfire = 1;
924 fixed_source_waypoint.enemy = NULL;
928 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
929 // as this search is expensive we will use lower values if the bot is on the air
930 float increment, maxdistance;
931 if(IS_ONGROUND(this))
942 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
945 bool searching = true;
949 IL_EACH(g_waypoints, it.wpfire,
954 p = it.wpnearestpoint;
956 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
957 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
958 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
959 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
960 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
961 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
962 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
963 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
964 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
965 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
966 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
967 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
968 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
969 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
970 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
971 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
972 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
973 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
974 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
975 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
976 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
977 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
978 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
979 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
980 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
981 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
982 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
983 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
984 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
985 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
986 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
987 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
988 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
993 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
994 void navigation_markroutes_inverted(entity fixed_source_waypoint)
998 IL_EACH(g_waypoints, true,
1000 it.wpconsidered = false;
1001 it.wpnearestpoint = '0 0 0';
1002 it.wpcost = 10000000;
1007 if(fixed_source_waypoint)
1009 fixed_source_waypoint.wpconsidered = true;
1010 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1011 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1012 fixed_source_waypoint.wpfire = 1;
1013 fixed_source_waypoint.enemy = NULL;
1017 error("need to start with a waypoint\n");
1020 bool searching = true;
1024 IL_EACH(g_waypoints, it.wpfire,
1028 cost = it.wpcost; // cost to walk from it to home
1029 p = it.wpnearestpoint;
1031 IL_EACH(g_waypoints, it != wp,
1033 if(!waypoint_islinked(it, wp))
1035 cost2 = cost + it.dmg;
1036 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1042 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1043 void navigation_routerating(entity this, entity e, float f, float rangebias)
1051 rangebias = waypoint_getlinearcost(rangebias);
1052 f = waypoint_getlinearcost(f);
1056 bool rate_wps = false;
1057 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1062 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1063 int t = pointcontents(trace_endpos + '0 0 1');
1064 if(t != CONTENT_SOLID )
1066 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1068 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1075 entity theEnemy = e;
1076 entity best_wp = NULL;
1077 float best_dist = 10000;
1078 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1079 && vdist(it.origin - this.origin, >, 100)
1080 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1082 float dist = vlen(it.origin - theEnemy.origin);
1083 if (dist < best_dist)
1095 vector goal_org = (e.absmin + e.absmax) * 0.5;
1097 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1099 // Evaluate path using jetpack
1101 if(this.items & IT_JETPACK)
1102 if(autocvar_bot_ai_navigation_jetpack)
1103 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1105 vector pointa, pointb;
1107 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1110 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1111 pointa = trace_endpos - '0 0 1';
1114 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1115 pointb = trace_endpos - '0 0 1';
1117 // Can I see these two points from the sky?
1118 traceline(pointa, pointb, MOVE_NORMAL, this);
1120 if(trace_fraction==1)
1122 LOG_DEBUG("jetpack ai: can bridge these two points");
1124 // Lower the altitude of these points as much as possible
1125 float zdistance, xydistance, cost, t, fuel;
1126 vector down, npa, npb;
1128 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1131 npa = pointa + down;
1132 npb = pointb + down;
1134 if(npa.z<=this.absmax.z)
1137 if(npb.z<=e.absmax.z)
1140 traceline(npa, npb, MOVE_NORMAL, this);
1141 if(trace_fraction==1)
1147 while(trace_fraction == 1);
1150 // Rough estimation of fuel consumption
1151 // (ignores acceleration and current xyz velocity)
1152 xydistance = vlen(pointa - pointb);
1153 zdistance = fabs(pointa.z - this.origin.z);
1155 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1156 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1157 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1159 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1162 if(this.ammo_fuel>fuel)
1165 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1166 // - between air and ground speeds)
1168 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1169 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1172 // Compare against other goals
1173 f = f * rangebias / (rangebias + cost);
1175 if (navigation_bestrating < f)
1177 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1178 navigation_bestrating = f;
1179 navigation_bestgoal = e;
1180 this.navigation_jetpack_goal = e;
1181 this.navigation_jetpack_point = pointb;
1189 //te_wizspike(e.origin);
1192 // update the cached spawnfunc_waypoint link on a dynamic item entity
1193 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1199 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1200 e.nearestwaypoint = NULL;
1202 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1203 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1205 nwp = navigation_findnearestwaypoint(e, true);
1208 e.nearestwaypoint = nwp;
1210 vector m1 = nwp.absmin, m2 = nwp.absmax;
1211 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
1212 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
1213 vector ve = (e.absmin - e.absmax) * 0.5;
1214 ve.x = bound(m1.x, ve.x, m2.x);
1215 ve.y = bound(m1.y, ve.y, m2.y);
1216 ve.z = bound(m1.z, ve.z, m2.z);
1218 m1 = e.absmin; m2 = e.absmax;
1219 m1.x = e.origin.x; m1.y = e.origin.y;
1220 m2.x = e.origin.x; m2.y = e.origin.y;
1221 vector vnwp = nwp.origin;
1222 vnwp.x = bound(m1.x, vnwp.x, m2.x);
1223 vnwp.y = bound(m1.y, vnwp.y, m2.y);
1224 vnwp.z = bound(m1.z, vnwp.z, m2.z);
1225 e.nearestwaypoint_dist = vlen(ve - vnwp);
1229 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1231 if(!e.navigation_dynamicgoal)
1232 e.blacklisted = true;
1236 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1241 if(e.navigation_dynamicgoal)
1242 e.nearestwaypointtimeout = time + 2;
1243 else if(autocvar_g_waypointeditor)
1244 e.nearestwaypointtimeout = time + 3 + random() * 2;
1246 nwp = e.nearestwaypoint;
1249 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1251 if (nwp.wpcost < 10000000)
1253 //te_wizspike(nwp.wpnearestpoint);
1254 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1255 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1256 f = f * rangebias / (rangebias + cost);
1257 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1258 if (navigation_bestrating < f)
1260 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1261 navigation_bestrating = f;
1262 navigation_bestgoal = e;
1267 // adds an item to the the goal stack with the path to a given item
1268 bool navigation_routetogoal(entity this, entity e, vector startposition)
1270 // if there is no goal, just exit
1274 entity teleport_goal = NULL;
1276 this.goalentity = e;
1278 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1280 // force teleport destination as route destination
1282 navigation_pushroute(this, e.wp00);
1283 this.goalentity = e.wp00;
1286 // put the entity on the goal stack
1287 //print("routetogoal ", etos(e), "\n");
1288 navigation_pushroute(this, e);
1291 e = this.goalentity;
1293 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1295 this.wp_goal_prev1 = this.wp_goal_prev0;
1296 this.wp_goal_prev0 = e;
1300 if(e==this.navigation_jetpack_goal)
1303 // if it can reach the goal there is nothing more to do
1304 vector dest = '0 0 0';
1305 float dest_height = 0;
1306 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1307 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1310 entity nearest_wp = NULL;
1311 // see if there are waypoints describing a path to the item
1312 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1314 e = e.nearestwaypoint;
1317 else if(teleport_goal)
1320 e = e.enemy; // we already have added it, so...
1325 if(nearest_wp && nearest_wp.enemy)
1327 // often path can be optimized by not adding the nearest waypoint
1328 if (this.goalentity.nearestwaypoint_dist < 8)
1329 e = nearest_wp.enemy;
1332 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1334 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1335 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1336 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1337 e = nearest_wp.enemy;
1339 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1340 e = nearest_wp.enemy;
1346 // add the spawnfunc_waypoint to the path
1347 navigation_pushroute(this, e);
1357 // removes any currently touching waypoints from the goal stack
1358 // (this is how bots detect if they reached a goal)
1359 int navigation_poptouchedgoals(entity this)
1361 int removed_goals = 0;
1362 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1364 // make sure jumppad is really hit, don't rely on distance based checks
1365 // as they may report a touch even if it didn't really happen
1366 if(this.lastteleporttime > 0
1367 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1369 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1370 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1372 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1373 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1375 navigation_poproute(this);
1379 return removed_goals;
1382 // If for some reason the bot is closer to the next goal, pop the current one
1383 if(this.goalstack01 && !wasfreed(this.goalstack01))
1384 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1385 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1387 vector dest = '0 0 0';
1388 float dest_height = 0;
1389 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1390 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1392 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1393 navigation_poproute(this);
1395 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1396 return removed_goals;
1397 // TODO this may also be a nice idea to do "early" (e.g. by
1398 // manipulating the vlen() comparisons) to shorten paths in
1399 // general - this would make bots walk more "on rails" than
1400 // "zigzagging" which they currently do with sufficiently
1401 // random-like waypoints, and thus can make a nice bot
1402 // personality property
1406 // Loose goal touching check when running
1407 if(this.aistatus & AI_STATUS_RUNNING)
1408 if(this.goalcurrent.classname=="waypoint")
1409 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1411 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1413 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1414 if(trace_fraction==1)
1416 // Detect personal waypoints
1417 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1418 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1420 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1421 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1424 navigation_poproute(this);
1426 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1427 return removed_goals;
1432 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1434 vector gc_min = this.goalcurrent.absmin;
1435 vector gc_max = this.goalcurrent.absmax;
1436 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1438 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1439 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1441 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1444 // Detect personal waypoints
1445 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1446 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1448 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1449 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1452 navigation_poproute(this);
1454 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1455 return removed_goals;
1457 return removed_goals;
1460 entity navigation_get_really_close_waypoint(entity this)
1462 entity wp = this.goalcurrent;
1463 if(!wp || vdist(wp.origin - this.origin, >, 50))
1464 wp = this.goalcurrent_prev;
1467 if(wp.classname != "waypoint")
1469 wp = wp.nearestwaypoint;
1473 if(vdist(wp.origin - this.origin, >, 50))
1475 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1477 if(vdist(it.origin - this.origin, <, 50))
1484 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1487 vector dest = '0 0 0';
1488 float dest_height = 0;
1489 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1490 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1495 // begin a goal selection session (queries spawnfunc_waypoint network)
1496 void navigation_goalrating_start(entity this)
1498 if(this.aistatus & AI_STATUS_STUCK)
1501 this.navigation_jetpack_goal = NULL;
1502 navigation_bestrating = -1;
1503 entity wp = navigation_get_really_close_waypoint(this);
1504 navigation_clearroute(this);
1505 navigation_bestgoal = NULL;
1506 navigation_markroutes(this, wp);
1509 // ends a goal selection session (updates goal stack to the best goal)
1510 void navigation_goalrating_end(entity this)
1512 if(this.aistatus & AI_STATUS_STUCK)
1515 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1516 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1518 // If the bot got stuck then try to reach the farthest waypoint
1519 if (!this.goalentity && autocvar_bot_wander_enable)
1521 if (!(this.aistatus & AI_STATUS_STUCK))
1523 LOG_DEBUG(this.netname, " cannot walk to any goal");
1524 this.aistatus |= AI_STATUS_STUCK;
1529 void botframe_updatedangerousobjects(float maxupdate)
1531 vector m1, m2, v, o;
1535 IL_EACH(g_waypoints, true,
1541 IL_EACH(g_bot_dodge, it.bot_dodge,
1544 v.x = bound(m1_x, v.x, m2_x);
1545 v.y = bound(m1_y, v.y, m2_y);
1546 v.z = bound(m1_z, v.z, m2_z);
1547 o = (it.absmin + it.absmax) * 0.5;
1548 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1551 traceline(o, v, true, NULL);
1552 if (trace_fraction == 1)
1553 danger = danger + d;
1563 void navigation_unstuck(entity this)
1565 float search_radius = 1000;
1567 if (!autocvar_bot_wander_enable)
1570 if (!bot_waypoint_queue_owner)
1572 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1573 bot_waypoint_queue_owner = this;
1574 bot_waypoint_queue_bestgoal = NULL;
1575 bot_waypoint_queue_bestgoalrating = 0;
1578 if(bot_waypoint_queue_owner!=this)
1581 if (bot_waypoint_queue_goal)
1583 // evaluate the next goal on the queue
1584 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1585 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1586 vector dest = '0 0 0';
1587 float dest_height = 0;
1588 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1589 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1591 if( d > bot_waypoint_queue_bestgoalrating)
1593 bot_waypoint_queue_bestgoalrating = d;
1594 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1597 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1599 if (!bot_waypoint_queue_goal)
1601 if (bot_waypoint_queue_bestgoal)
1603 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1604 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1605 navigation_goalrating_timeout_set(this);
1606 this.aistatus &= ~AI_STATUS_STUCK;
1610 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1613 bot_waypoint_queue_owner = NULL;
1618 if(bot_strategytoken!=this)
1621 // build a new queue
1622 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1624 entity first = NULL;
1626 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1628 if(bot_waypoint_queue_goal)
1629 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1633 bot_waypoint_queue_goal = it;
1634 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1638 bot_waypoint_queue_goal = first;
1641 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1642 bot_waypoint_queue_owner = NULL;
1647 // Support for debugging tracewalk visually
1649 void debugresetnodes()
1651 debuglastnode = '0 0 0';
1654 void debugnode(entity this, vector node)
1656 if (!IS_PLAYER(this))
1659 if(debuglastnode=='0 0 0')
1661 debuglastnode = node;
1665 te_lightning2(NULL, node, debuglastnode);
1666 debuglastnode = node;
1669 void debugnodestatus(vector position, float status)
1675 case DEBUG_NODE_SUCCESS:
1678 case DEBUG_NODE_WARNING:
1681 case DEBUG_NODE_FAIL:
1688 te_customflash(position, 40, 2, c);
1691 // Support for debugging the goal stack visually
1694 .vector lastposition;
1696 // Debug the goal stack visually
1697 void debuggoalstack(entity this)
1702 if(this.goalcounter==0)goal=this.goalcurrent;
1703 else if(this.goalcounter==1)goal=this.goalstack01;
1704 else if(this.goalcounter==2)goal=this.goalstack02;
1705 else if(this.goalcounter==3)goal=this.goalstack03;
1706 else if(this.goalcounter==4)goal=this.goalstack04;
1707 else if(this.goalcounter==5)goal=this.goalstack05;
1708 else if(this.goalcounter==6)goal=this.goalstack06;
1709 else if(this.goalcounter==7)goal=this.goalstack07;
1710 else if(this.goalcounter==8)goal=this.goalstack08;
1711 else if(this.goalcounter==9)goal=this.goalstack09;
1712 else if(this.goalcounter==10)goal=this.goalstack10;
1713 else if(this.goalcounter==11)goal=this.goalstack11;
1714 else if(this.goalcounter==12)goal=this.goalstack12;
1715 else if(this.goalcounter==13)goal=this.goalstack13;
1716 else if(this.goalcounter==14)goal=this.goalstack14;
1717 else if(this.goalcounter==15)goal=this.goalstack15;
1718 else if(this.goalcounter==16)goal=this.goalstack16;
1719 else if(this.goalcounter==17)goal=this.goalstack17;
1720 else if(this.goalcounter==18)goal=this.goalstack18;
1721 else if(this.goalcounter==19)goal=this.goalstack19;
1722 else if(this.goalcounter==20)goal=this.goalstack20;
1723 else if(this.goalcounter==21)goal=this.goalstack21;
1724 else if(this.goalcounter==22)goal=this.goalstack22;
1725 else if(this.goalcounter==23)goal=this.goalstack23;
1726 else if(this.goalcounter==24)goal=this.goalstack24;
1727 else if(this.goalcounter==25)goal=this.goalstack25;
1728 else if(this.goalcounter==26)goal=this.goalstack26;
1729 else if(this.goalcounter==27)goal=this.goalstack27;
1730 else if(this.goalcounter==28)goal=this.goalstack28;
1731 else if(this.goalcounter==29)goal=this.goalstack29;
1732 else if(this.goalcounter==30)goal=this.goalstack30;
1733 else if(this.goalcounter==31)goal=this.goalstack31;
1738 this.goalcounter = 0;
1739 this.lastposition='0 0 0';
1743 if(this.lastposition=='0 0 0')
1746 org = this.lastposition;
1749 go = ( goal.absmin + goal.absmax ) * 0.5;
1750 te_lightning2(NULL, org, go);
1751 this.lastposition = go;