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 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
40 if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
43 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
45 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
46 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
48 this.ignoregoal = this.goalentity;
49 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
56 void navigation_dynamicgoal_init(entity this, bool initially_static)
58 this.navigation_dynamicgoal = true;
59 this.bot_basewaypoint = this.nearestwaypoint;
61 this.nearestwaypointtimeout = -1;
63 this.nearestwaypointtimeout = time;
66 void navigation_dynamicgoal_set(entity this)
68 this.nearestwaypointtimeout = time;
71 void navigation_dynamicgoal_unset(entity this)
73 if(this.bot_basewaypoint)
74 this.nearestwaypoint = this.bot_basewaypoint;
75 this.nearestwaypointtimeout = -1;
78 bool navigation_check_submerged_state(entity ent, vector pos)
82 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
83 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
84 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
87 submerged = SUBMERGED(pos);
88 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
89 // is actually submerged because often they are inside some solid.
90 // That's why submerged state is saved now that we know current pos is
91 // not stuck in solid (previous tracewalk call to this pos was successfully)
92 if(!ent.navigation_dynamicgoal)
93 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
98 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
100 IL_EACH(g_ladders, it.classname == "func_ladder",
103 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
104 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
107 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
108 tracebox(org, m1, m2, top, movemode, e);
109 if(trace_fraction == 1)
116 vector resurface_limited(vector org, float lim, vector m1)
118 if (WETFEET(org + eZ * (lim - org.z)))
122 float RES_min_h = org.z;
123 float RES_max_h = lim;
125 org.z = 0.5 * (RES_min_h + RES_max_h);
130 } while (RES_max_h - RES_min_h >= 1);
135 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
138 #define NAV_SWIM_ONWATER 1
139 #define NAV_SWIM_UNDERWATER 2
141 // rough simulation of walking from one point to another to test if a path
142 // can be traveled, used for waypoint linking and havocbot
143 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
144 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
146 if(autocvar_bot_debug_tracewalk)
153 vector flatdir = end - start;
155 float flatdist = vlen(flatdir);
156 flatdir = normalize(flatdir);
158 bool ignorehazards = false;
161 // Analyze starting point
162 traceline(start, start, MOVE_NORMAL, e);
163 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
164 ignorehazards = true;
166 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
167 if (trace_startsolid)
170 if(autocvar_bot_debug_tracewalk)
171 debugnodestatus(start, DEBUG_NODE_FAIL);
173 //print("tracewalk: ", vtos(start), " is a bad start\n");
179 end2.z += end_height;
181 vector fixed_end = end;
184 if (flatdist > 0 && WETFEET(org))
187 nav_action = NAV_SWIM_UNDERWATER;
190 // tracebox down by player's height
191 // useful to know if water level is so low that bot can still walk
192 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
193 if (SUBMERGED(trace_endpos))
196 nav_action = NAV_SWIM_UNDERWATER;
199 nav_action = NAV_WALK;
203 nav_action = NAV_WALK;
211 if (org.z > end2.z + 1)
213 tracebox(org, m1, m2, end2, movemode, e);
215 if (org.z > end2.z + 1)
218 else if (org.z < end.z - 1)
220 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
221 if (SUBMERGED(trace_endpos))
223 vector v = trace_endpos;
224 tracebox(v, m1, m2, end, movemode, e);
225 if(trace_endpos.z >= end.z - 1)
227 RESURFACE_LIMITED(v, trace_endpos.z);
231 else if (trace_endpos.z > org.z - jumpheight_vec.z)
232 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
234 if (org.z < end.z - 1)
241 if(autocvar_bot_debug_tracewalk)
244 debugnodestatus(org, DEBUG_NODE_SUCCESS);
247 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
252 if(autocvar_bot_debug_tracewalk)
258 if (stepdist > flatdist)
260 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
262 // can't use movement direction here to calculate move because of
263 // precision errors especially when direction has a high enough z value
264 //water_dir = normalize(water_end - org);
265 //move = org + water_dir * stepdist;
266 fixed_end.z = bound(end.z, org.z, end2.z);
267 if (stepdist == flatdist) {
271 move = org + (fixed_end - org) * (stepdist / flatdist);
272 flatdist = vlen(vec2(fixed_end - move));
275 else // horiz. direction
277 flatdist -= stepdist;
278 move = org + flatdir * stepdist;
281 if(nav_action == NAV_SWIM_ONWATER)
283 tracebox(org, m1, m2, move, movemode, e); // swim
286 if (trace_fraction < 1)
289 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
291 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
294 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
296 if(autocvar_bot_debug_tracewalk)
299 debugnodestatus(org, DEBUG_NODE_SUCCESS);
302 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
306 if(autocvar_bot_debug_tracewalk)
307 debugnodestatus(org, DEBUG_NODE_FAIL);
310 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
321 if (org.z <= move.z) // going horiz.
323 tracebox(trace_endpos, m1, m2, move, movemode, e);
325 nav_action = NAV_WALK;
330 if (org.z <= move.z) // going horiz.
333 nav_action = NAV_SWIM_ONWATER;
339 nav_action = NAV_SWIM_UNDERWATER;
341 nav_action = NAV_SWIM_ONWATER;
344 else if(nav_action == NAV_SWIM_UNDERWATER)
346 if (move.z >= org.z) // swimming upwards or horiz.
348 tracebox(org, m1, m2, move, movemode, e); // swim
350 bool stepswum = false;
353 if (trace_fraction < 1)
356 vector stepswim_move = move + stepheightvec;
357 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
358 stepswim_move.z = end2.z;
360 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
363 if (trace_startsolid)
365 if(autocvar_bot_debug_tracewalk)
366 debugnodestatus(org, DEBUG_NODE_FAIL);
368 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
372 if (trace_fraction < 1)
374 float org_z_prev = org.z;
375 RESURFACE_LIMITED(org, end2.z);
376 if(org.z == org_z_prev)
378 if(autocvar_bot_debug_tracewalk)
379 debugnodestatus(org, DEBUG_NODE_FAIL);
381 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
385 nav_action = NAV_SWIM_UNDERWATER;
387 nav_action = NAV_SWIM_ONWATER;
389 // we didn't advance horiz. in this step, flatdist decrease should be reverted
390 // but we can't do it properly right now... apply this workaround instead
408 if (!WETFEET(trace_endpos))
410 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
411 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
412 if (!stepswum && SUBMERGED(trace_endpos))
414 RESURFACE_LIMITED(trace_endpos, end2.z);
416 nav_action = NAV_SWIM_ONWATER;
422 nav_action = NAV_WALK;
428 nav_action = NAV_SWIM_UNDERWATER;
431 else //if (move.z < org.z) // swimming downwards
433 tracebox(org, m1, m2, move, movemode, e); // swim
436 if (trace_fraction < 1)
439 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
442 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
444 if(autocvar_bot_debug_tracewalk)
445 debugnodestatus(move, DEBUG_NODE_FAIL);
447 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
459 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
461 // stepswim caused upwards direction
462 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
463 if (!SUBMERGED(trace_endpos))
466 nav_action = NAV_WALK;
473 nav_action = NAV_SWIM_UNDERWATER;
477 else if(nav_action == NAV_WALK)
480 tracebox(org, m1, m2, move, movemode, e);
482 if(autocvar_bot_debug_tracewalk)
483 debugnode(e, trace_endpos);
486 if (trace_fraction < 1)
488 // check if we can walk over this obstacle, possibly by jumpstepping
489 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
490 if (trace_fraction < 1 || trace_startsolid)
492 if (trace_startsolid) // hit ceiling above org
494 // reduce stepwalk height
495 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
496 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
498 else //if (trace_fraction < 1)
500 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
501 if (trace_startsolid) // hit ceiling above org
503 // reduce jumpstepwalk height
504 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
505 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
509 if (trace_fraction < 1)
511 vector v = trace_endpos;
512 v.z = org.z + jumpheight_vec.z;
513 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
515 if(autocvar_bot_debug_tracewalk)
518 debugnodestatus(v, DEBUG_NODE_SUCCESS);
521 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
525 if(autocvar_bot_debug_tracewalk)
526 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
528 traceline( org, move, movemode, e);
530 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
534 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
536 nextmove = move + (flatdir * stepdist);
537 traceline( move, nextmove, movemode, e);
540 flatdist = vlen(vec2(end - move));
544 if(autocvar_bot_debug_tracewalk)
545 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
547 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
548 //te_explosion(trace_endpos);
549 //print(ftos(e.dphitcontentsmask), "\n");
550 return false; // failed
562 // trace down from stepheight as far as possible and move there,
563 // if this starts in solid we try again without the stepup, and
564 // if that also fails we assume it is a wall
565 // (this is the same logic as the Quake walkmove function used)
566 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
574 if(autocvar_bot_debug_tracewalk)
576 debugnode(e, trace_endpos);
577 debugnodestatus(org, DEBUG_NODE_FAIL);
580 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
587 if(move.z >= end2.z && org.z < end2.z)
592 if(org.z > move.z - 1 || !SUBMERGED(org))
594 nav_action = NAV_WALK;
598 // ended up submerged while walking
599 if(autocvar_bot_debug_tracewalk)
602 RESURFACE_LIMITED(org, move.z);
603 nav_action = NAV_SWIM_ONWATER;
608 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
610 // moved but didn't arrive at the intended destination
611 if(autocvar_bot_debug_tracewalk)
612 debugnodestatus(org, DEBUG_NODE_FAIL);
617 /////////////////////////////////////////////////////////////////////////////
619 /////////////////////////////////////////////////////////////////////////////
621 // completely empty the goal stack, used when deciding where to go
622 void navigation_clearroute(entity this)
624 this.goalcurrent_prev = this.goalcurrent;
625 this.goalcurrent_distance_2d = FLOAT_MAX;
626 this.goalcurrent_distance_z = FLOAT_MAX;
627 this.goalcurrent_distance_time = 0;
628 //print("bot ", etos(this), " clear\n");
629 this.goalentity = NULL;
630 this.goalcurrent = NULL;
631 this.goalstack01 = NULL;
632 this.goalstack02 = NULL;
633 this.goalstack03 = NULL;
634 this.goalstack04 = NULL;
635 this.goalstack05 = NULL;
636 this.goalstack06 = NULL;
637 this.goalstack07 = NULL;
638 this.goalstack08 = NULL;
639 this.goalstack09 = NULL;
640 this.goalstack10 = NULL;
641 this.goalstack11 = NULL;
642 this.goalstack12 = NULL;
643 this.goalstack13 = NULL;
644 this.goalstack14 = NULL;
645 this.goalstack15 = NULL;
646 this.goalstack16 = NULL;
647 this.goalstack17 = NULL;
648 this.goalstack18 = NULL;
649 this.goalstack19 = NULL;
650 this.goalstack20 = NULL;
651 this.goalstack21 = NULL;
652 this.goalstack22 = NULL;
653 this.goalstack23 = NULL;
654 this.goalstack24 = NULL;
655 this.goalstack25 = NULL;
656 this.goalstack26 = NULL;
657 this.goalstack27 = NULL;
658 this.goalstack28 = NULL;
659 this.goalstack29 = NULL;
660 this.goalstack30 = NULL;
661 this.goalstack31 = NULL;
664 // add a new goal at the beginning of the stack
665 // (in other words: add a new prerequisite before going to the later goals)
666 // NOTE: when a waypoint is added, the WP gets pushed first, then the
667 // next-closest WP on the shortest path to the WP
668 // That means, if the stack overflows, the bot will know how to do the FIRST 32
669 // steps to the goal, and then recalculate the path.
670 void navigation_pushroute(entity this, entity e)
672 this.goalcurrent_prev = this.goalcurrent;
673 this.goalcurrent_distance_2d = FLOAT_MAX;
674 this.goalcurrent_distance_z = FLOAT_MAX;
675 this.goalcurrent_distance_time = 0;
676 //print("bot ", etos(this), " push ", etos(e), "\n");
677 if(this.goalstack31 == this.goalentity)
678 this.goalentity = NULL;
679 this.goalstack31 = this.goalstack30;
680 this.goalstack30 = this.goalstack29;
681 this.goalstack29 = this.goalstack28;
682 this.goalstack28 = this.goalstack27;
683 this.goalstack27 = this.goalstack26;
684 this.goalstack26 = this.goalstack25;
685 this.goalstack25 = this.goalstack24;
686 this.goalstack24 = this.goalstack23;
687 this.goalstack23 = this.goalstack22;
688 this.goalstack22 = this.goalstack21;
689 this.goalstack21 = this.goalstack20;
690 this.goalstack20 = this.goalstack19;
691 this.goalstack19 = this.goalstack18;
692 this.goalstack18 = this.goalstack17;
693 this.goalstack17 = this.goalstack16;
694 this.goalstack16 = this.goalstack15;
695 this.goalstack15 = this.goalstack14;
696 this.goalstack14 = this.goalstack13;
697 this.goalstack13 = this.goalstack12;
698 this.goalstack12 = this.goalstack11;
699 this.goalstack11 = this.goalstack10;
700 this.goalstack10 = this.goalstack09;
701 this.goalstack09 = this.goalstack08;
702 this.goalstack08 = this.goalstack07;
703 this.goalstack07 = this.goalstack06;
704 this.goalstack06 = this.goalstack05;
705 this.goalstack05 = this.goalstack04;
706 this.goalstack04 = this.goalstack03;
707 this.goalstack03 = this.goalstack02;
708 this.goalstack02 = this.goalstack01;
709 this.goalstack01 = this.goalcurrent;
710 this.goalcurrent = e;
713 // remove first goal from stack
714 // (in other words: remove a prerequisite for reaching the later goals)
715 // (used when a spawnfunc_waypoint is reached)
716 void navigation_poproute(entity this)
718 this.goalcurrent_prev = this.goalcurrent;
719 this.goalcurrent_distance_2d = FLOAT_MAX;
720 this.goalcurrent_distance_z = FLOAT_MAX;
721 this.goalcurrent_distance_time = 0;
722 //print("bot ", etos(this), " pop\n");
723 if(this.goalcurrent == this.goalentity)
724 this.goalentity = NULL;
725 this.goalcurrent = this.goalstack01;
726 this.goalstack01 = this.goalstack02;
727 this.goalstack02 = this.goalstack03;
728 this.goalstack03 = this.goalstack04;
729 this.goalstack04 = this.goalstack05;
730 this.goalstack05 = this.goalstack06;
731 this.goalstack06 = this.goalstack07;
732 this.goalstack07 = this.goalstack08;
733 this.goalstack08 = this.goalstack09;
734 this.goalstack09 = this.goalstack10;
735 this.goalstack10 = this.goalstack11;
736 this.goalstack11 = this.goalstack12;
737 this.goalstack12 = this.goalstack13;
738 this.goalstack13 = this.goalstack14;
739 this.goalstack14 = this.goalstack15;
740 this.goalstack15 = this.goalstack16;
741 this.goalstack16 = this.goalstack17;
742 this.goalstack17 = this.goalstack18;
743 this.goalstack18 = this.goalstack19;
744 this.goalstack19 = this.goalstack20;
745 this.goalstack20 = this.goalstack21;
746 this.goalstack21 = this.goalstack22;
747 this.goalstack22 = this.goalstack23;
748 this.goalstack23 = this.goalstack24;
749 this.goalstack24 = this.goalstack25;
750 this.goalstack25 = this.goalstack26;
751 this.goalstack26 = this.goalstack27;
752 this.goalstack27 = this.goalstack28;
753 this.goalstack28 = this.goalstack29;
754 this.goalstack29 = this.goalstack30;
755 this.goalstack30 = this.goalstack31;
756 this.goalstack31 = NULL;
759 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
760 // waypoint destination coordinates instead of v (only useful for box waypoints)
761 // for normal waypoints v2 == v and v2_height == 0
762 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)
764 if (vdist(v - org, <, bestdist))
766 traceline(v, org, true, ent);
767 if (trace_fraction == 1)
771 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
776 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
784 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
785 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
788 ent = ent.tag_entity;
790 vector pm1 = ent.origin + ent.mins;
791 vector pm2 = ent.origin + ent.maxs;
793 // do two scans, because box test is cheaper
794 IL_EACH(g_waypoints, it != ent && it != except,
796 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
798 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
800 waypoint_clearlinks(ent); // initialize wpXXmincost fields
801 navigation_item_addlink(it, ent);
807 vector org = ent.origin;
808 if (navigation_testtracewalk)
812 vector v = '0 0 0', v2 = '0 0 0';
815 if(ent.size && !IS_PLAYER(ent))
817 org += 0.5 * (ent.mins + ent.maxs);
818 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
821 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
823 waypoint_clearlinks(ent); // initialize wpXXmincost fields
824 IL_EACH(g_waypoints, it != ent,
826 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
829 SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
830 if(vdist(v2 - it.origin, <, 1050))
831 if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
832 navigation_item_addlink(it, ent);
836 // box check failed, try walk
837 IL_EACH(g_waypoints, it != ent,
839 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
843 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
845 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
846 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
848 bestdist = vlen(v - org);
854 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
856 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
857 if (autocvar_g_waypointeditor_auto)
859 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
861 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
866 // finds the waypoints near the bot initiating a navigation query
867 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
870 //navigation_testtracewalk = true;
873 IL_EACH(g_waypoints, !it.wpconsidered,
875 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
877 vector diff = v - this.origin;
878 diff.z = max(0, diff.z);
879 if(vdist(diff, <, maxdist))
881 it.wpconsidered = true;
882 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
884 it.wpnearestpoint = v;
885 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
892 //navigation_testtracewalk = false;
896 // updates a path link if a spawnfunc_waypoint link is better than the current one
897 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
903 m1 = wp.origin + wp.mins;
904 m2 = wp.origin + wp.maxs;
905 v.x = bound(m1_x, p.x, m2_x);
906 v.y = bound(m1_y, p.y, m2_y);
907 v.z = bound(m1_z, p.z, m2_z);
911 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
912 cost += w.wp00mincost; // assuming teleport has exactly one destination
914 cost += waypoint_gettravelcost(p, v, w, wp);
915 if (wp.wpcost > cost)
920 wp.wpnearestpoint = v;
924 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
925 void navigation_markroutes(entity this, entity fixed_source_waypoint)
930 IL_EACH(g_waypoints, true,
932 it.wpconsidered = false;
933 it.wpnearestpoint = '0 0 0';
934 it.wpcost = 10000000;
939 if(fixed_source_waypoint)
941 fixed_source_waypoint.wpconsidered = true;
942 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
943 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
944 fixed_source_waypoint.wpfire = 1;
945 fixed_source_waypoint.enemy = NULL;
949 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
950 // as this search is expensive we will use lower values if the bot is on the air
951 float increment, maxdistance;
952 if(IS_ONGROUND(this))
963 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
966 bool searching = true;
970 IL_EACH(g_waypoints, it.wpfire,
975 p = it.wpnearestpoint;
977 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
978 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
979 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
980 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
981 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
982 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
983 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
984 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
985 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
986 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
987 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
988 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
989 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
990 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
991 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
992 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
993 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
994 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
995 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
996 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
997 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
998 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
999 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1000 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1001 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1002 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1003 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1004 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1005 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1014 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1015 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1019 IL_EACH(g_waypoints, true,
1021 it.wpconsidered = false;
1022 it.wpnearestpoint = '0 0 0';
1023 it.wpcost = 10000000;
1028 if(fixed_source_waypoint)
1030 fixed_source_waypoint.wpconsidered = true;
1031 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1032 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1033 fixed_source_waypoint.wpfire = 1;
1034 fixed_source_waypoint.enemy = NULL;
1038 error("need to start with a waypoint\n");
1041 bool searching = true;
1045 IL_EACH(g_waypoints, it.wpfire,
1049 cost = it.wpcost; // cost to walk from it to home
1050 p = it.wpnearestpoint;
1052 IL_EACH(g_waypoints, it != wp,
1054 if(!waypoint_islinked(it, wp))
1056 cost2 = cost + it.dmg;
1057 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1063 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1064 void navigation_routerating(entity this, entity e, float f, float rangebias)
1072 rangebias = waypoint_getlinearcost(rangebias);
1073 f = waypoint_getlinearcost(f);
1077 bool rate_wps = false;
1078 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1083 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1084 int t = pointcontents(trace_endpos + '0 0 1');
1085 if(t != CONTENT_SOLID )
1087 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1089 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1096 entity theEnemy = e;
1097 entity best_wp = NULL;
1098 float best_dist = 10000;
1099 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1100 && vdist(it.origin - this.origin, >, 100)
1101 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1103 float dist = vlen(it.origin - theEnemy.origin);
1104 if (dist < best_dist)
1116 vector goal_org = (e.absmin + e.absmax) * 0.5;
1118 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1120 // Evaluate path using jetpack
1122 if(this.items & IT_JETPACK)
1123 if(autocvar_bot_ai_navigation_jetpack)
1124 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1126 vector pointa, pointb;
1128 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1131 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1132 pointa = trace_endpos - '0 0 1';
1135 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1136 pointb = trace_endpos - '0 0 1';
1138 // Can I see these two points from the sky?
1139 traceline(pointa, pointb, MOVE_NORMAL, this);
1141 if(trace_fraction==1)
1143 LOG_DEBUG("jetpack ai: can bridge these two points");
1145 // Lower the altitude of these points as much as possible
1146 float zdistance, xydistance, cost, t, fuel;
1147 vector down, npa, npb;
1149 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1152 npa = pointa + down;
1153 npb = pointb + down;
1155 if(npa.z<=this.absmax.z)
1158 if(npb.z<=e.absmax.z)
1161 traceline(npa, npb, MOVE_NORMAL, this);
1162 if(trace_fraction==1)
1168 while(trace_fraction == 1);
1171 // Rough estimation of fuel consumption
1172 // (ignores acceleration and current xyz velocity)
1173 xydistance = vlen(pointa - pointb);
1174 zdistance = fabs(pointa.z - this.origin.z);
1176 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1177 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1178 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1180 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1183 if(this.ammo_fuel>fuel)
1186 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1187 // - between air and ground speeds)
1189 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1190 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1193 // Compare against other goals
1194 f = f * rangebias / (rangebias + cost);
1196 if (navigation_bestrating < f)
1198 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1199 navigation_bestrating = f;
1200 navigation_bestgoal = e;
1201 this.navigation_jetpack_goal = e;
1202 this.navigation_jetpack_point = pointb;
1210 //te_wizspike(e.origin);
1213 // update the cached spawnfunc_waypoint link on a dynamic item entity
1214 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1220 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1221 e.nearestwaypoint = NULL;
1223 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1224 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1226 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
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.navigation_dynamicgoal || autocvar_g_waypointeditor)
1330 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1331 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1332 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1333 e = nearest_wp.enemy;
1335 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1336 e = nearest_wp.enemy;
1341 // add the spawnfunc_waypoint to the path
1342 navigation_pushroute(this, e);
1352 // removes any currently touching waypoints from the goal stack
1353 // (this is how bots detect if they reached a goal)
1354 int navigation_poptouchedgoals(entity this)
1356 int removed_goals = 0;
1358 if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1360 navigation_poproute(this);
1364 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1366 // make sure jumppad is really hit, don't rely on distance based checks
1367 // as they may report a touch even if it didn't really happen
1368 if(this.lastteleporttime > 0
1369 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1371 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1372 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1374 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1375 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1377 navigation_poproute(this);
1378 this.lastteleporttime = 0;
1382 return removed_goals;
1385 // If for some reason the bot is closer to the next goal, pop the current one
1386 if(this.goalstack01 && !wasfreed(this.goalstack01))
1387 if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1388 if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1389 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1391 vector dest = '0 0 0';
1392 float dest_height = 0;
1393 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1394 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1396 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1397 navigation_poproute(this);
1399 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1400 return removed_goals;
1401 // TODO this may also be a nice idea to do "early" (e.g. by
1402 // manipulating the vlen() comparisons) to shorten paths in
1403 // general - this would make bots walk more "on rails" than
1404 // "zigzagging" which they currently do with sufficiently
1405 // random-like waypoints, and thus can make a nice bot
1406 // personality property
1410 // Loose goal touching check when running
1411 if(this.aistatus & AI_STATUS_RUNNING)
1412 if(this.goalcurrent.classname=="waypoint")
1413 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1415 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1417 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1418 if(trace_fraction==1)
1420 // Detect personal waypoints
1421 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1422 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1424 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1425 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1428 navigation_poproute(this);
1430 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1431 return removed_goals;
1436 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1438 vector gc_min = this.goalcurrent.absmin;
1439 vector gc_max = this.goalcurrent.absmax;
1440 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1442 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1443 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1445 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1448 // Detect personal waypoints
1449 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1450 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1452 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1453 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1456 navigation_poproute(this);
1458 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1459 return removed_goals;
1461 return removed_goals;
1464 entity navigation_get_really_close_waypoint(entity this)
1466 entity wp = this.goalcurrent;
1467 if(!wp || vdist(wp.origin - this.origin, >, 50))
1468 wp = this.goalcurrent_prev;
1471 if(wp.classname != "waypoint")
1473 wp = wp.nearestwaypoint;
1477 if(vdist(wp.origin - this.origin, >, 50))
1479 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1481 if(vdist(it.origin - this.origin, <, 50))
1488 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1491 vector dest = '0 0 0';
1492 float dest_height = 0;
1493 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1494 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1499 // begin a goal selection session (queries spawnfunc_waypoint network)
1500 void navigation_goalrating_start(entity this)
1502 if(this.aistatus & AI_STATUS_STUCK)
1505 this.navigation_jetpack_goal = NULL;
1506 navigation_bestrating = -1;
1507 entity wp = navigation_get_really_close_waypoint(this);
1508 navigation_clearroute(this);
1509 navigation_bestgoal = NULL;
1510 navigation_markroutes(this, wp);
1513 // ends a goal selection session (updates goal stack to the best goal)
1514 void navigation_goalrating_end(entity this)
1516 if(this.aistatus & AI_STATUS_STUCK)
1519 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1520 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1522 // If the bot got stuck then try to reach the farthest waypoint
1523 if (!this.goalentity && autocvar_bot_wander_enable)
1525 if (!(this.aistatus & AI_STATUS_STUCK))
1527 LOG_DEBUG(this.netname, " cannot walk to any goal");
1528 this.aistatus |= AI_STATUS_STUCK;
1533 void botframe_updatedangerousobjects(float maxupdate)
1535 vector m1, m2, v, o;
1539 IL_EACH(g_waypoints, true,
1545 IL_EACH(g_bot_dodge, it.bot_dodge,
1548 v.x = bound(m1_x, v.x, m2_x);
1549 v.y = bound(m1_y, v.y, m2_y);
1550 v.z = bound(m1_z, v.z, m2_z);
1551 o = (it.absmin + it.absmax) * 0.5;
1552 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1555 traceline(o, v, true, NULL);
1556 if (trace_fraction == 1)
1557 danger = danger + d;
1567 void navigation_unstuck(entity this)
1569 float search_radius = 1000;
1571 if (!autocvar_bot_wander_enable)
1574 if (!bot_waypoint_queue_owner)
1576 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1577 bot_waypoint_queue_owner = this;
1578 bot_waypoint_queue_bestgoal = NULL;
1579 bot_waypoint_queue_bestgoalrating = 0;
1582 if(bot_waypoint_queue_owner!=this)
1585 if (bot_waypoint_queue_goal)
1587 // evaluate the next goal on the queue
1588 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1589 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1590 vector dest = '0 0 0';
1591 float dest_height = 0;
1592 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1593 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1595 if( d > bot_waypoint_queue_bestgoalrating)
1597 bot_waypoint_queue_bestgoalrating = d;
1598 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1601 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1603 if (!bot_waypoint_queue_goal)
1605 if (bot_waypoint_queue_bestgoal)
1607 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1608 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1609 navigation_goalrating_timeout_set(this);
1610 this.aistatus &= ~AI_STATUS_STUCK;
1614 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1617 bot_waypoint_queue_owner = NULL;
1622 if(bot_strategytoken!=this)
1625 // build a new queue
1626 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1628 entity first = NULL;
1630 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1632 if(bot_waypoint_queue_goal)
1633 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1637 bot_waypoint_queue_goal = it;
1638 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1642 bot_waypoint_queue_goal = first;
1645 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1646 bot_waypoint_queue_owner = NULL;
1651 // Support for debugging tracewalk visually
1653 void debugresetnodes()
1655 debuglastnode = '0 0 0';
1658 void debugnode(entity this, vector node)
1660 if (!IS_PLAYER(this))
1663 if(debuglastnode=='0 0 0')
1665 debuglastnode = node;
1669 te_lightning2(NULL, node, debuglastnode);
1670 debuglastnode = node;
1673 void debugnodestatus(vector position, float status)
1679 case DEBUG_NODE_SUCCESS:
1682 case DEBUG_NODE_WARNING:
1685 case DEBUG_NODE_FAIL:
1692 te_customflash(position, 40, 2, c);
1695 // Support for debugging the goal stack visually
1698 .vector lastposition;
1700 // Debug the goal stack visually
1701 void debuggoalstack(entity this)
1706 if(this.goalcounter==0)goal=this.goalcurrent;
1707 else if(this.goalcounter==1)goal=this.goalstack01;
1708 else if(this.goalcounter==2)goal=this.goalstack02;
1709 else if(this.goalcounter==3)goal=this.goalstack03;
1710 else if(this.goalcounter==4)goal=this.goalstack04;
1711 else if(this.goalcounter==5)goal=this.goalstack05;
1712 else if(this.goalcounter==6)goal=this.goalstack06;
1713 else if(this.goalcounter==7)goal=this.goalstack07;
1714 else if(this.goalcounter==8)goal=this.goalstack08;
1715 else if(this.goalcounter==9)goal=this.goalstack09;
1716 else if(this.goalcounter==10)goal=this.goalstack10;
1717 else if(this.goalcounter==11)goal=this.goalstack11;
1718 else if(this.goalcounter==12)goal=this.goalstack12;
1719 else if(this.goalcounter==13)goal=this.goalstack13;
1720 else if(this.goalcounter==14)goal=this.goalstack14;
1721 else if(this.goalcounter==15)goal=this.goalstack15;
1722 else if(this.goalcounter==16)goal=this.goalstack16;
1723 else if(this.goalcounter==17)goal=this.goalstack17;
1724 else if(this.goalcounter==18)goal=this.goalstack18;
1725 else if(this.goalcounter==19)goal=this.goalstack19;
1726 else if(this.goalcounter==20)goal=this.goalstack20;
1727 else if(this.goalcounter==21)goal=this.goalstack21;
1728 else if(this.goalcounter==22)goal=this.goalstack22;
1729 else if(this.goalcounter==23)goal=this.goalstack23;
1730 else if(this.goalcounter==24)goal=this.goalstack24;
1731 else if(this.goalcounter==25)goal=this.goalstack25;
1732 else if(this.goalcounter==26)goal=this.goalstack26;
1733 else if(this.goalcounter==27)goal=this.goalstack27;
1734 else if(this.goalcounter==28)goal=this.goalstack28;
1735 else if(this.goalcounter==29)goal=this.goalstack29;
1736 else if(this.goalcounter==30)goal=this.goalstack30;
1737 else if(this.goalcounter==31)goal=this.goalstack31;
1742 this.goalcounter = 0;
1743 this.lastposition='0 0 0';
1747 if(this.lastposition=='0 0 0')
1750 org = this.lastposition;
1753 go = ( goal.absmin + goal.absmax ) * 0.5;
1754 te_lightning2(NULL, org, go);
1755 this.lastposition = go;