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 this.goalentity_lock_timeout = 0;
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)
725 this.goalentity = NULL;
726 this.goalentity_lock_timeout = 0;
728 this.goalcurrent = this.goalstack01;
729 this.goalstack01 = this.goalstack02;
730 this.goalstack02 = this.goalstack03;
731 this.goalstack03 = this.goalstack04;
732 this.goalstack04 = this.goalstack05;
733 this.goalstack05 = this.goalstack06;
734 this.goalstack06 = this.goalstack07;
735 this.goalstack07 = this.goalstack08;
736 this.goalstack08 = this.goalstack09;
737 this.goalstack09 = this.goalstack10;
738 this.goalstack10 = this.goalstack11;
739 this.goalstack11 = this.goalstack12;
740 this.goalstack12 = this.goalstack13;
741 this.goalstack13 = this.goalstack14;
742 this.goalstack14 = this.goalstack15;
743 this.goalstack15 = this.goalstack16;
744 this.goalstack16 = this.goalstack17;
745 this.goalstack17 = this.goalstack18;
746 this.goalstack18 = this.goalstack19;
747 this.goalstack19 = this.goalstack20;
748 this.goalstack20 = this.goalstack21;
749 this.goalstack21 = this.goalstack22;
750 this.goalstack22 = this.goalstack23;
751 this.goalstack23 = this.goalstack24;
752 this.goalstack24 = this.goalstack25;
753 this.goalstack25 = this.goalstack26;
754 this.goalstack26 = this.goalstack27;
755 this.goalstack27 = this.goalstack28;
756 this.goalstack28 = this.goalstack29;
757 this.goalstack29 = this.goalstack30;
758 this.goalstack30 = this.goalstack31;
759 this.goalstack31 = NULL;
762 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
763 // waypoint destination coordinates instead of v (only useful for box waypoints)
764 // for normal waypoints v2 == v and v2_height == 0
765 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)
767 if (vdist(v - org, <, bestdist))
769 traceline(v, org, true, ent);
770 if (trace_fraction == 1)
774 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
779 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
787 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
788 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
791 ent = ent.tag_entity;
793 vector pm1 = ent.origin + ent.mins;
794 vector pm2 = ent.origin + ent.maxs;
796 // do two scans, because box test is cheaper
797 IL_EACH(g_waypoints, it != ent && it != except,
799 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
801 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
803 waypoint_clearlinks(ent); // initialize wpXXmincost fields
804 navigation_item_addlink(it, ent);
810 vector org = ent.origin;
811 if (navigation_testtracewalk)
815 vector v = '0 0 0', v2 = '0 0 0';
818 if(ent.size && !IS_PLAYER(ent))
820 org += 0.5 * (ent.mins + ent.maxs);
821 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
824 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
826 waypoint_clearlinks(ent); // initialize wpXXmincost fields
827 IL_EACH(g_waypoints, it != ent,
829 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
832 SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
833 if(vdist(v2 - it.origin, <, 1050))
834 if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
835 navigation_item_addlink(it, ent);
839 // box check failed, try walk
840 IL_EACH(g_waypoints, it != ent,
842 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
846 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
848 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
849 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
851 bestdist = vlen(v - org);
855 if(!best && !ent.navigation_dynamicgoal)
857 int solid_save = ent.solid;
858 ent.solid = SOLID_BSP;
859 IL_EACH(g_jumppads, true,
861 if(trigger_push_test(it, ent))
863 best = it.nearestwaypoint;
867 ent.solid = solid_save;
871 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
873 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
874 if (autocvar_g_waypointeditor_auto)
876 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
878 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
883 // finds the waypoints near the bot initiating a navigation query
884 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
887 //navigation_testtracewalk = true;
890 IL_EACH(g_waypoints, !it.wpconsidered,
892 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
894 vector diff = v - this.origin;
895 diff.z = max(0, diff.z);
896 if(vdist(diff, <, maxdist))
898 it.wpconsidered = true;
899 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
901 it.wpnearestpoint = v;
902 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
909 //navigation_testtracewalk = false;
913 // updates a path link if a spawnfunc_waypoint link is better than the current one
914 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
920 m1 = wp.origin + wp.mins;
921 m2 = wp.origin + wp.maxs;
922 v.x = bound(m1_x, p.x, m2_x);
923 v.y = bound(m1_y, p.y, m2_y);
924 v.z = bound(m1_z, p.z, m2_z);
928 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
929 cost += w.wp00mincost; // assuming teleport has exactly one destination
931 cost += waypoint_gettravelcost(p, v, w, wp);
932 if (wp.wpcost > cost)
937 wp.wpnearestpoint = v;
941 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
942 void navigation_markroutes(entity this, entity fixed_source_waypoint)
947 IL_EACH(g_waypoints, true,
949 it.wpconsidered = false;
950 it.wpnearestpoint = '0 0 0';
951 it.wpcost = 10000000;
956 if(fixed_source_waypoint)
958 fixed_source_waypoint.wpconsidered = true;
959 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
960 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
961 fixed_source_waypoint.wpfire = 1;
962 fixed_source_waypoint.enemy = NULL;
966 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
967 // as this search is expensive we will use lower values if the bot is on the air
968 float increment, maxdistance;
969 if(IS_ONGROUND(this))
980 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
983 bool searching = true;
987 IL_EACH(g_waypoints, it.wpfire,
992 p = it.wpnearestpoint;
994 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
995 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
996 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
997 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
998 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
999 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1000 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1001 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1002 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1003 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1004 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1005 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1024 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1025 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1026 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1031 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1032 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1036 IL_EACH(g_waypoints, true,
1038 it.wpconsidered = false;
1039 it.wpnearestpoint = '0 0 0';
1040 it.wpcost = 10000000;
1045 if(fixed_source_waypoint)
1047 fixed_source_waypoint.wpconsidered = true;
1048 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1049 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1050 fixed_source_waypoint.wpfire = 1;
1051 fixed_source_waypoint.enemy = NULL;
1055 error("need to start with a waypoint\n");
1058 bool searching = true;
1062 IL_EACH(g_waypoints, it.wpfire,
1066 cost = it.wpcost; // cost to walk from it to home
1067 p = it.wpnearestpoint;
1069 IL_EACH(g_waypoints, it != wp,
1071 if(!waypoint_islinked(it, wp))
1073 cost2 = cost + it.dmg;
1074 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1080 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1081 void navigation_routerating(entity this, entity e, float f, float rangebias)
1089 rangebias = waypoint_getlinearcost(rangebias);
1090 f = waypoint_getlinearcost(f);
1094 bool rate_wps = false;
1095 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1100 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1101 int t = pointcontents(trace_endpos + '0 0 1');
1102 if(t != CONTENT_SOLID )
1104 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1106 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1113 entity theEnemy = e;
1114 entity best_wp = NULL;
1115 float best_dist = 10000;
1116 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1117 && vdist(it.origin - this.origin, >, 100)
1118 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1120 float dist = vlen(it.origin - theEnemy.origin);
1121 if (dist < best_dist)
1133 vector goal_org = (e.absmin + e.absmax) * 0.5;
1135 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1137 // Evaluate path using jetpack
1139 if(this.items & IT_JETPACK)
1140 if(autocvar_bot_ai_navigation_jetpack)
1141 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1143 vector pointa, pointb;
1145 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1148 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1149 pointa = trace_endpos - '0 0 1';
1152 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1153 pointb = trace_endpos - '0 0 1';
1155 // Can I see these two points from the sky?
1156 traceline(pointa, pointb, MOVE_NORMAL, this);
1158 if(trace_fraction==1)
1160 LOG_DEBUG("jetpack ai: can bridge these two points");
1162 // Lower the altitude of these points as much as possible
1163 float zdistance, xydistance, cost, t, fuel;
1164 vector down, npa, npb;
1166 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1169 npa = pointa + down;
1170 npb = pointb + down;
1172 if(npa.z<=this.absmax.z)
1175 if(npb.z<=e.absmax.z)
1178 traceline(npa, npb, MOVE_NORMAL, this);
1179 if(trace_fraction==1)
1185 while(trace_fraction == 1);
1188 // Rough estimation of fuel consumption
1189 // (ignores acceleration and current xyz velocity)
1190 xydistance = vlen(pointa - pointb);
1191 zdistance = fabs(pointa.z - this.origin.z);
1193 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1194 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1195 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1197 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1200 if(this.ammo_fuel>fuel)
1203 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1204 // - between air and ground speeds)
1206 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1207 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1210 // Compare against other goals
1211 f = f * rangebias / (rangebias + cost);
1213 if (navigation_bestrating < f)
1215 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1216 navigation_bestrating = f;
1217 navigation_bestgoal = e;
1218 this.navigation_jetpack_goal = e;
1219 this.navigation_jetpack_point = pointb;
1227 //te_wizspike(e.origin);
1230 // update the cached spawnfunc_waypoint link on a dynamic item entity
1231 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1237 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1238 e.nearestwaypoint = NULL;
1240 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1241 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1243 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1244 e.nearestwaypoint = nwp = e.goalcurrent;
1246 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1249 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1251 if(!e.navigation_dynamicgoal)
1252 e.blacklisted = true;
1256 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1261 if(e.navigation_dynamicgoal)
1262 e.nearestwaypointtimeout = time + 2;
1263 else if(autocvar_g_waypointeditor)
1264 e.nearestwaypointtimeout = time + 3 + random() * 2;
1266 nwp = e.nearestwaypoint;
1269 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1270 if (nwp && nwp.wpcost < 10000000)
1272 //te_wizspike(nwp.wpnearestpoint);
1273 float nwptoitem_cost = 0;
1274 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1275 nwptoitem_cost = nwp.wp00mincost;
1277 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1278 float cost = nwp.wpcost + nwptoitem_cost;
1279 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1280 f = f * rangebias / (rangebias + cost);
1281 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1282 if (navigation_bestrating < f)
1284 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1285 navigation_bestrating = f;
1286 navigation_bestgoal = e;
1291 // adds an item to the the goal stack with the path to a given item
1292 bool navigation_routetogoal(entity this, entity e, vector startposition)
1294 // if there is no goal, just exit
1298 entity teleport_goal = NULL;
1300 this.goalentity = e;
1302 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1304 // force teleport destination as route destination
1306 navigation_pushroute(this, e.wp00);
1307 this.goalentity = e.wp00;
1310 // put the entity on the goal stack
1311 //print("routetogoal ", etos(e), "\n");
1312 navigation_pushroute(this, e);
1315 e = this.goalentity;
1317 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1319 this.wp_goal_prev1 = this.wp_goal_prev0;
1320 this.wp_goal_prev0 = e;
1324 if(e==this.navigation_jetpack_goal)
1327 // if it can reach the goal there is nothing more to do
1328 vector dest = '0 0 0';
1329 float dest_height = 0;
1330 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1331 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1334 entity nearest_wp = NULL;
1335 // see if there are waypoints describing a path to the item
1336 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1338 e = e.nearestwaypoint;
1341 else if(teleport_goal)
1344 e = e.enemy; // we already have added it, so...
1349 if(nearest_wp && nearest_wp.enemy)
1351 // often path can be optimized by not adding the nearest waypoint
1352 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1354 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1355 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1356 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1357 e = nearest_wp.enemy;
1359 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1360 e = nearest_wp.enemy;
1365 // add the spawnfunc_waypoint to the path
1366 navigation_pushroute(this, e);
1376 // removes any currently touching waypoints from the goal stack
1377 // (this is how bots detect if they reached a goal)
1378 int navigation_poptouchedgoals(entity this)
1380 int removed_goals = 0;
1382 if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1384 navigation_poproute(this);
1388 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1390 // make sure jumppad is really hit, don't rely on distance based checks
1391 // as they may report a touch even if it didn't really happen
1392 if(this.lastteleporttime > 0
1393 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1395 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1396 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1398 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1399 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1401 navigation_poproute(this);
1402 this.lastteleporttime = 0;
1406 return removed_goals;
1409 // If for some reason the bot is closer to the next goal, pop the current one
1410 if(this.goalstack01 && !wasfreed(this.goalstack01))
1411 if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1412 if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1413 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1415 vector dest = '0 0 0';
1416 float dest_height = 0;
1417 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1418 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1420 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1421 navigation_poproute(this);
1423 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1424 return removed_goals;
1425 // TODO this may also be a nice idea to do "early" (e.g. by
1426 // manipulating the vlen() comparisons) to shorten paths in
1427 // general - this would make bots walk more "on rails" than
1428 // "zigzagging" which they currently do with sufficiently
1429 // random-like waypoints, and thus can make a nice bot
1430 // personality property
1434 // Loose goal touching check when running
1435 if(this.aistatus & AI_STATUS_RUNNING)
1436 if(this.goalcurrent.classname=="waypoint")
1437 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1439 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1441 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1442 if(trace_fraction==1)
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;
1460 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1462 vector gc_min = this.goalcurrent.absmin;
1463 vector gc_max = this.goalcurrent.absmax;
1464 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1466 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1467 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1469 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1472 // Detect personal waypoints
1473 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1474 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1476 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1477 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1480 navigation_poproute(this);
1482 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1483 return removed_goals;
1485 return removed_goals;
1488 entity navigation_get_really_close_waypoint(entity this)
1490 entity wp = this.goalcurrent;
1491 if(!wp || vdist(wp.origin - this.origin, >, 50))
1492 wp = this.goalcurrent_prev;
1495 if(wp.classname != "waypoint")
1497 wp = wp.nearestwaypoint;
1501 if(vdist(wp.origin - this.origin, >, 50))
1503 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1505 if(vdist(it.origin - this.origin, <, 50))
1512 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1515 vector dest = '0 0 0';
1516 float dest_height = 0;
1517 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1518 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1523 // begin a goal selection session (queries spawnfunc_waypoint network)
1524 void navigation_goalrating_start(entity this)
1526 if(this.aistatus & AI_STATUS_STUCK)
1529 this.navigation_jetpack_goal = NULL;
1530 navigation_bestrating = -1;
1531 entity wp = navigation_get_really_close_waypoint(this);
1532 navigation_clearroute(this);
1533 navigation_bestgoal = NULL;
1534 navigation_markroutes(this, wp);
1537 // ends a goal selection session (updates goal stack to the best goal)
1538 void navigation_goalrating_end(entity this)
1540 if(this.aistatus & AI_STATUS_STUCK)
1543 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1544 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1546 // If the bot got stuck then try to reach the farthest waypoint
1547 if (!this.goalentity && autocvar_bot_wander_enable)
1549 if (!(this.aistatus & AI_STATUS_STUCK))
1551 LOG_DEBUG(this.netname, " cannot walk to any goal");
1552 this.aistatus |= AI_STATUS_STUCK;
1557 void botframe_updatedangerousobjects(float maxupdate)
1559 vector m1, m2, v, o;
1563 IL_EACH(g_waypoints, true,
1569 IL_EACH(g_bot_dodge, it.bot_dodge,
1572 v.x = bound(m1_x, v.x, m2_x);
1573 v.y = bound(m1_y, v.y, m2_y);
1574 v.z = bound(m1_z, v.z, m2_z);
1575 o = (it.absmin + it.absmax) * 0.5;
1576 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1579 traceline(o, v, true, NULL);
1580 if (trace_fraction == 1)
1581 danger = danger + d;
1591 void navigation_unstuck(entity this)
1593 float search_radius = 1000;
1595 if (!autocvar_bot_wander_enable)
1598 if (!bot_waypoint_queue_owner)
1600 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1601 bot_waypoint_queue_owner = this;
1602 bot_waypoint_queue_bestgoal = NULL;
1603 bot_waypoint_queue_bestgoalrating = 0;
1606 if(bot_waypoint_queue_owner!=this)
1609 if (bot_waypoint_queue_goal)
1611 // evaluate the next goal on the queue
1612 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1613 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1614 vector dest = '0 0 0';
1615 float dest_height = 0;
1616 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1617 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1619 if( d > bot_waypoint_queue_bestgoalrating)
1621 bot_waypoint_queue_bestgoalrating = d;
1622 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1625 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1627 if (!bot_waypoint_queue_goal)
1629 if (bot_waypoint_queue_bestgoal)
1631 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1632 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1633 navigation_goalrating_timeout_set(this);
1634 this.aistatus &= ~AI_STATUS_STUCK;
1638 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1641 bot_waypoint_queue_owner = NULL;
1646 if(bot_strategytoken!=this)
1649 // build a new queue
1650 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1652 entity first = NULL;
1654 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1656 if(bot_waypoint_queue_goal)
1657 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1661 bot_waypoint_queue_goal = it;
1662 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1666 bot_waypoint_queue_goal = first;
1669 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1670 bot_waypoint_queue_owner = NULL;
1675 // Support for debugging tracewalk visually
1677 void debugresetnodes()
1679 debuglastnode = '0 0 0';
1682 void debugnode(entity this, vector node)
1684 if (!IS_PLAYER(this))
1687 if(debuglastnode=='0 0 0')
1689 debuglastnode = node;
1693 te_lightning2(NULL, node, debuglastnode);
1694 debuglastnode = node;
1697 void debugnodestatus(vector position, float status)
1703 case DEBUG_NODE_SUCCESS:
1706 case DEBUG_NODE_WARNING:
1709 case DEBUG_NODE_FAIL:
1716 te_customflash(position, 40, 2, c);
1719 // Support for debugging the goal stack visually
1722 .vector lastposition;
1724 // Debug the goal stack visually
1725 void debuggoalstack(entity this)
1730 if(this.goalcounter==0)goal=this.goalcurrent;
1731 else if(this.goalcounter==1)goal=this.goalstack01;
1732 else if(this.goalcounter==2)goal=this.goalstack02;
1733 else if(this.goalcounter==3)goal=this.goalstack03;
1734 else if(this.goalcounter==4)goal=this.goalstack04;
1735 else if(this.goalcounter==5)goal=this.goalstack05;
1736 else if(this.goalcounter==6)goal=this.goalstack06;
1737 else if(this.goalcounter==7)goal=this.goalstack07;
1738 else if(this.goalcounter==8)goal=this.goalstack08;
1739 else if(this.goalcounter==9)goal=this.goalstack09;
1740 else if(this.goalcounter==10)goal=this.goalstack10;
1741 else if(this.goalcounter==11)goal=this.goalstack11;
1742 else if(this.goalcounter==12)goal=this.goalstack12;
1743 else if(this.goalcounter==13)goal=this.goalstack13;
1744 else if(this.goalcounter==14)goal=this.goalstack14;
1745 else if(this.goalcounter==15)goal=this.goalstack15;
1746 else if(this.goalcounter==16)goal=this.goalstack16;
1747 else if(this.goalcounter==17)goal=this.goalstack17;
1748 else if(this.goalcounter==18)goal=this.goalstack18;
1749 else if(this.goalcounter==19)goal=this.goalstack19;
1750 else if(this.goalcounter==20)goal=this.goalstack20;
1751 else if(this.goalcounter==21)goal=this.goalstack21;
1752 else if(this.goalcounter==22)goal=this.goalstack22;
1753 else if(this.goalcounter==23)goal=this.goalstack23;
1754 else if(this.goalcounter==24)goal=this.goalstack24;
1755 else if(this.goalcounter==25)goal=this.goalstack25;
1756 else if(this.goalcounter==26)goal=this.goalstack26;
1757 else if(this.goalcounter==27)goal=this.goalstack27;
1758 else if(this.goalcounter==28)goal=this.goalstack28;
1759 else if(this.goalcounter==29)goal=this.goalstack29;
1760 else if(this.goalcounter==30)goal=this.goalstack30;
1761 else if(this.goalcounter==31)goal=this.goalstack31;
1766 this.goalcounter = 0;
1767 this.lastposition='0 0 0';
1771 if(this.lastposition=='0 0 0')
1774 org = this.lastposition;
1777 go = ( goal.absmin + goal.absmax ) * 0.5;
1778 te_lightning2(NULL, org, go);
1779 this.lastposition = go;