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/mapobjects/func/ladder.qh>
17 #include <common/mapobjects/trigger/jumppads.qh>
21 void navigation_goalrating_timeout_set(entity this)
23 if(IS_MOVABLE(this.goalentity))
24 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
26 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
29 // use this when current goal must be discarded immediately
30 void navigation_goalrating_timeout_force(entity this)
32 navigation_goalrating_timeout_expire(this, 0);
35 // use this when current goal can be kept for a short while to increase the chance
36 // of bot touching a waypoint, which helps to find a new goal more efficiently
37 void navigation_goalrating_timeout_expire(entity this, float seconds)
40 this.bot_strategytime = 0;
41 else if (this.bot_strategytime > time + seconds)
42 this.bot_strategytime = time + seconds;
45 bool navigation_goalrating_timeout(entity this)
47 return this.bot_strategytime < time;
50 #define MAX_CHASE_DISTANCE 700
51 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
53 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
54 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
55 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
60 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
62 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
64 this.ignoregoal = this.goalentity;
65 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
72 void navigation_dynamicgoal_init(entity this, bool initially_static)
74 this.navigation_dynamicgoal = true;
75 this.bot_basewaypoint = this.nearestwaypoint;
77 this.nearestwaypointtimeout = -1;
79 this.nearestwaypointtimeout = time;
82 void navigation_dynamicgoal_set(entity this, entity dropper)
84 this.nearestwaypointtimeout = time;
85 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
86 this.nearestwaypoint = dropper.nearestwaypoint;
87 if (this.nearestwaypoint)
88 this.nearestwaypointtimeout += 2;
91 void navigation_dynamicgoal_unset(entity this)
93 if(this.bot_basewaypoint)
94 this.nearestwaypoint = this.bot_basewaypoint;
95 this.nearestwaypointtimeout = -1;
98 // returns point of ent closer to org
99 vector get_closer_dest(entity ent, vector org)
101 vector dest = '0 0 0';
102 if ((ent.classname != "waypoint") || ent.wpisbox)
104 vector wm1 = ent.origin + ent.mins;
105 vector wm2 = ent.origin + ent.maxs;
106 dest.x = bound(wm1.x, org.x, wm2.x);
107 dest.y = bound(wm1.y, org.y, wm2.y);
108 dest.z = bound(wm1.z, org.z, wm2.z);
115 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
117 if ((ent.classname != "waypoint") || ent.wpisbox)
119 vector wm1 = ent.origin + ent.mins;
120 vector wm2 = ent.origin + ent.maxs;
121 if (IS_PLAYER(ent) || IS_MONSTER(ent))
123 // move destination point out of player bbox otherwise tracebox always fails
124 // (if bot_navigation_ignoreplayers is false)
125 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
126 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
128 // set destination point to x and y coords of ent that are closer to org
129 // z coord is set to ent's min height
130 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
131 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
132 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
133 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
135 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
136 tracewalk_dest_height = 0;
137 fix_player_dest = false;
141 tracewalk_dest.z = wm1.z;
142 tracewalk_dest_height = wm2.z - wm1.z;
147 tracewalk_dest = ent.origin;
148 tracewalk_dest_height = 0;
150 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
152 // snap player to the ground
153 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
155 // bot is right under the player
156 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
157 tracewalk_dest = trace_endpos;
158 tracewalk_dest_height = 0;
162 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
163 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
165 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
166 tracewalk_dest.z = trace_endpos.z;
172 // returns point of ent closer to org
173 vector set_tracewalk_dest_2(entity ent, vector org)
175 vector closer_dest = '0 0 0';
176 if ((ent.classname != "waypoint") || ent.wpisbox)
178 vector wm1 = ent.origin + ent.mins;
179 vector wm2 = ent.origin + ent.maxs;
180 closer_dest.x = bound(wm1.x, org.x, wm2.x);
181 closer_dest.y = bound(wm1.y, org.y, wm2.y);
182 closer_dest.z = bound(wm1.z, org.z, wm2.z);
183 // set destination point to x and y coords of ent that are closer to org
184 // z coord is set to ent's min height
185 tracewalk_dest.x = closer_dest.x;
186 tracewalk_dest.y = closer_dest.y;
187 tracewalk_dest.z = wm1.z;
188 tracewalk_dest_height = wm2.z - wm1.z; // destination height
192 closer_dest = ent.origin;
193 tracewalk_dest = closer_dest;
194 tracewalk_dest_height = 0;
199 bool navigation_check_submerged_state(entity ent, vector pos)
203 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
204 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
205 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
208 submerged = SUBMERGED(pos);
209 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
210 // is actually submerged because often they are inside some solid.
211 // That's why submerged state is saved now that we know current pos is
212 // not stuck in solid (previous tracewalk call to this pos was successfully)
213 if(!ent.navigation_dynamicgoal)
214 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
219 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
221 IL_EACH(g_ladders, it.classname == "func_ladder",
224 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
225 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
228 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
229 tracebox(org, m1, m2, top, movemode, e);
230 if(trace_fraction == 1)
237 vector resurface_limited(vector org, float lim, vector m1)
239 if (WETFEET(org + eZ * (lim - org.z)))
243 float RES_min_h = org.z;
244 float RES_max_h = lim;
246 org.z = 0.5 * (RES_min_h + RES_max_h);
251 } while (RES_max_h - RES_min_h >= 1);
256 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
259 #define NAV_SWIM_ONWATER 1
260 #define NAV_SWIM_UNDERWATER 2
262 // rough simulation of walking from one point to another to test if a path
263 // can be traveled, used for waypoint linking and havocbot
264 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
265 // INFO: the command sv_cmd trace walk is useful to test this function in game
266 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
268 if(autocvar_bot_debug_tracewalk)
275 vector flatdir = end - start;
277 float flatdist = vlen(flatdir);
278 flatdir = normalize(flatdir);
280 bool ignorehazards = false;
283 // Analyze starting point
285 ignorehazards = true;
287 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
288 if (trace_startsolid)
291 if(autocvar_bot_debug_tracewalk)
292 debugnodestatus(start, DEBUG_NODE_FAIL);
294 //print("tracewalk: ", vtos(start), " is a bad start\n");
300 end2.z += end_height;
302 vector fixed_end = end;
305 if (flatdist > 0 && WETFEET(org))
308 nav_action = NAV_SWIM_UNDERWATER;
311 // tracebox down by player's height
312 // useful to know if water level is so low that bot can still walk
313 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
314 if (SUBMERGED(trace_endpos))
317 nav_action = NAV_SWIM_UNDERWATER;
320 nav_action = NAV_WALK;
324 nav_action = NAV_WALK;
332 if (org.z > end2.z + 1)
334 tracebox(org, m1, m2, end2, movemode, e);
336 if (org.z > end2.z + 1)
339 else if (org.z < end.z - 1)
341 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
342 if (SUBMERGED(trace_endpos))
344 vector v = trace_endpos;
345 tracebox(v, m1, m2, end, movemode, e);
346 if(trace_endpos.z >= end.z - 1)
348 RESURFACE_LIMITED(v, trace_endpos.z);
352 else if (trace_endpos.z > org.z - jumpheight_vec.z)
353 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
355 if (org.z < end.z - 1)
362 if(autocvar_bot_debug_tracewalk)
365 debugnodestatus(org, DEBUG_NODE_SUCCESS);
368 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
373 if(autocvar_bot_debug_tracewalk)
379 if (stepdist > flatdist)
381 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
383 // can't use movement direction here to calculate move because of
384 // precision errors especially when direction has a high enough z value
385 //water_dir = normalize(water_end - org);
386 //move = org + water_dir * stepdist;
387 fixed_end.z = bound(end.z, org.z, end2.z);
388 if (stepdist == flatdist) {
392 move = org + (fixed_end - org) * (stepdist / flatdist);
393 flatdist = vlen(vec2(fixed_end - move));
396 else // horiz. direction
398 flatdist -= stepdist;
399 move = org + flatdir * stepdist;
402 if(nav_action == NAV_SWIM_ONWATER)
404 tracebox(org, m1, m2, move, movemode, e); // swim
407 if (trace_fraction < 1)
410 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
412 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
415 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
417 if(autocvar_bot_debug_tracewalk)
420 debugnodestatus(org, DEBUG_NODE_SUCCESS);
423 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
427 if(autocvar_bot_debug_tracewalk)
428 debugnodestatus(org, DEBUG_NODE_FAIL);
431 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
442 if (org.z <= move.z) // going horiz.
444 tracebox(trace_endpos, m1, m2, move, movemode, e);
446 nav_action = NAV_WALK;
451 if (org.z <= move.z) // going horiz.
454 nav_action = NAV_SWIM_ONWATER;
460 nav_action = NAV_SWIM_UNDERWATER;
462 nav_action = NAV_SWIM_ONWATER;
465 else if(nav_action == NAV_SWIM_UNDERWATER)
467 if (move.z >= org.z) // swimming upwards or horiz.
469 tracebox(org, m1, m2, move, movemode, e); // swim
471 bool stepswum = false;
474 if (trace_fraction < 1)
477 vector stepswim_move = move + stepheightvec;
478 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
479 stepswim_move.z = end2.z;
481 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
484 if (trace_startsolid)
486 if(autocvar_bot_debug_tracewalk)
487 debugnodestatus(org, DEBUG_NODE_FAIL);
489 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
493 if (trace_fraction < 1)
495 float org_z_prev = org.z;
496 RESURFACE_LIMITED(org, end2.z);
497 if(org.z == org_z_prev)
499 if(autocvar_bot_debug_tracewalk)
500 debugnodestatus(org, DEBUG_NODE_FAIL);
502 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
506 nav_action = NAV_SWIM_UNDERWATER;
508 nav_action = NAV_SWIM_ONWATER;
510 // we didn't advance horiz. in this step, flatdist decrease should be reverted
511 // but we can't do it properly right now... apply this workaround instead
529 if (!WETFEET(trace_endpos))
531 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
532 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
533 if (!stepswum && SUBMERGED(trace_endpos))
535 RESURFACE_LIMITED(trace_endpos, end2.z);
537 nav_action = NAV_SWIM_ONWATER;
543 nav_action = NAV_WALK;
549 nav_action = NAV_SWIM_UNDERWATER;
552 else //if (move.z < org.z) // swimming downwards
554 tracebox(org, m1, m2, move, movemode, e); // swim
557 if (trace_fraction < 1)
560 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
563 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
565 if(autocvar_bot_debug_tracewalk)
566 debugnodestatus(move, DEBUG_NODE_FAIL);
568 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
580 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
582 // stepswim caused upwards direction
583 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
584 if (!SUBMERGED(trace_endpos))
587 nav_action = NAV_WALK;
594 nav_action = NAV_SWIM_UNDERWATER;
598 else if(nav_action == NAV_WALK)
601 tracebox(org, m1, m2, move, movemode, e);
603 if(autocvar_bot_debug_tracewalk)
604 debugnode(e, trace_endpos);
607 if (trace_fraction < 1)
609 // check if we can walk over this obstacle, possibly by jumpstepping
610 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
611 if (trace_fraction < 1 || trace_startsolid)
613 if (trace_startsolid) // hit ceiling above org
615 // reduce stepwalk height
616 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
617 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
619 else //if (trace_fraction < 1)
621 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
622 if (trace_startsolid) // hit ceiling above org
624 // reduce jumpstepwalk height
625 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
626 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
630 if (trace_fraction < 1)
632 vector v = trace_endpos;
633 v.z = org.z + jumpheight_vec.z;
634 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
636 if(autocvar_bot_debug_tracewalk)
639 debugnodestatus(v, DEBUG_NODE_SUCCESS);
642 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
646 if(autocvar_bot_debug_tracewalk)
647 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
649 traceline( org, move, movemode, e);
651 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
655 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
657 nextmove = move + (flatdir * stepdist);
658 traceline( move, nextmove, movemode, e);
661 flatdist = vlen(vec2(end - move));
665 if(autocvar_bot_debug_tracewalk)
666 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
668 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
669 //te_explosion(trace_endpos);
670 //print(ftos(e.dphitcontentsmask), "\n");
671 return false; // failed
683 // trace down from stepheight as far as possible and move there,
684 // if this starts in solid we try again without the stepup, and
685 // if that also fails we assume it is a wall
686 // (this is the same logic as the Quake walkmove function used)
687 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
695 if(autocvar_bot_debug_tracewalk)
697 debugnode(e, trace_endpos);
698 debugnodestatus(org, DEBUG_NODE_FAIL);
701 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
708 if(move.z >= end2.z && org.z < end2.z)
713 if(org.z > move.z - 1 || !SUBMERGED(org))
715 nav_action = NAV_WALK;
719 // ended up submerged while walking
720 if(autocvar_bot_debug_tracewalk)
723 RESURFACE_LIMITED(org, move.z);
724 nav_action = NAV_SWIM_ONWATER;
729 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
731 // moved but didn't arrive at the intended destination
732 if(autocvar_bot_debug_tracewalk)
733 debugnodestatus(org, DEBUG_NODE_FAIL);
738 /////////////////////////////////////////////////////////////////////////////
740 /////////////////////////////////////////////////////////////////////////////
742 // completely empty the goal stack, used when deciding where to go
743 void navigation_clearroute(entity this)
745 this.lastteleporttime = 0;
746 this.goalcurrent_prev = this.goalcurrent;
747 this.goalcurrent_distance_2d = FLOAT_MAX;
748 this.goalcurrent_distance_z = FLOAT_MAX;
749 this.goalcurrent_distance_time = 0;
750 this.goalentity_lock_timeout = 0;
751 this.goalentity_shouldbefrozen = false;
752 this.goalentity = NULL;
753 this.goalcurrent = NULL;
754 this.goalstack01 = NULL;
755 this.goalstack02 = NULL;
756 this.goalstack03 = NULL;
757 this.goalstack04 = NULL;
758 this.goalstack05 = NULL;
759 this.goalstack06 = NULL;
760 this.goalstack07 = NULL;
761 this.goalstack08 = NULL;
762 this.goalstack09 = NULL;
763 this.goalstack10 = NULL;
764 this.goalstack11 = NULL;
765 this.goalstack12 = NULL;
766 this.goalstack13 = NULL;
767 this.goalstack14 = NULL;
768 this.goalstack15 = NULL;
769 this.goalstack16 = NULL;
770 this.goalstack17 = NULL;
771 this.goalstack18 = NULL;
772 this.goalstack19 = NULL;
773 this.goalstack20 = NULL;
774 this.goalstack21 = NULL;
775 this.goalstack22 = NULL;
776 this.goalstack23 = NULL;
777 this.goalstack24 = NULL;
778 this.goalstack25 = NULL;
779 this.goalstack26 = NULL;
780 this.goalstack27 = NULL;
781 this.goalstack28 = NULL;
782 this.goalstack29 = NULL;
783 this.goalstack30 = NULL;
784 this.goalstack31 = NULL;
787 // add a new goal at the beginning of the stack
788 // (in other words: add a new prerequisite before going to the later goals)
789 // NOTE: when a waypoint is added, the WP gets pushed first, then the
790 // next-closest WP on the shortest path to the WP
791 // That means, if the stack overflows, the bot will know how to do the FIRST 32
792 // steps to the goal, and then recalculate the path.
793 void navigation_pushroute(entity this, entity e)
795 this.goalcurrent_prev = this.goalcurrent;
796 this.goalcurrent_distance_2d = FLOAT_MAX;
797 this.goalcurrent_distance_z = FLOAT_MAX;
798 this.goalcurrent_distance_time = 0;
799 //print("bot ", etos(this), " push ", etos(e), "\n");
800 if(this.goalstack31 == this.goalentity)
801 this.goalentity = NULL;
802 this.goalstack31 = this.goalstack30;
803 this.goalstack30 = this.goalstack29;
804 this.goalstack29 = this.goalstack28;
805 this.goalstack28 = this.goalstack27;
806 this.goalstack27 = this.goalstack26;
807 this.goalstack26 = this.goalstack25;
808 this.goalstack25 = this.goalstack24;
809 this.goalstack24 = this.goalstack23;
810 this.goalstack23 = this.goalstack22;
811 this.goalstack22 = this.goalstack21;
812 this.goalstack21 = this.goalstack20;
813 this.goalstack20 = this.goalstack19;
814 this.goalstack19 = this.goalstack18;
815 this.goalstack18 = this.goalstack17;
816 this.goalstack17 = this.goalstack16;
817 this.goalstack16 = this.goalstack15;
818 this.goalstack15 = this.goalstack14;
819 this.goalstack14 = this.goalstack13;
820 this.goalstack13 = this.goalstack12;
821 this.goalstack12 = this.goalstack11;
822 this.goalstack11 = this.goalstack10;
823 this.goalstack10 = this.goalstack09;
824 this.goalstack09 = this.goalstack08;
825 this.goalstack08 = this.goalstack07;
826 this.goalstack07 = this.goalstack06;
827 this.goalstack06 = this.goalstack05;
828 this.goalstack05 = this.goalstack04;
829 this.goalstack04 = this.goalstack03;
830 this.goalstack03 = this.goalstack02;
831 this.goalstack02 = this.goalstack01;
832 this.goalstack01 = this.goalcurrent;
833 this.goalcurrent = e;
836 // remove first goal from stack
837 // (in other words: remove a prerequisite for reaching the later goals)
838 // (used when a spawnfunc_waypoint is reached)
839 void navigation_poproute(entity this)
841 this.goalcurrent_prev = this.goalcurrent;
842 this.goalcurrent_distance_2d = FLOAT_MAX;
843 this.goalcurrent_distance_z = FLOAT_MAX;
844 this.goalcurrent_distance_time = 0;
845 //print("bot ", etos(this), " pop\n");
846 if(this.goalcurrent == this.goalentity)
848 this.goalentity = NULL;
849 this.goalentity_lock_timeout = 0;
851 this.goalcurrent = this.goalstack01;
852 this.goalstack01 = this.goalstack02;
853 this.goalstack02 = this.goalstack03;
854 this.goalstack03 = this.goalstack04;
855 this.goalstack04 = this.goalstack05;
856 this.goalstack05 = this.goalstack06;
857 this.goalstack06 = this.goalstack07;
858 this.goalstack07 = this.goalstack08;
859 this.goalstack08 = this.goalstack09;
860 this.goalstack09 = this.goalstack10;
861 this.goalstack10 = this.goalstack11;
862 this.goalstack11 = this.goalstack12;
863 this.goalstack12 = this.goalstack13;
864 this.goalstack13 = this.goalstack14;
865 this.goalstack14 = this.goalstack15;
866 this.goalstack15 = this.goalstack16;
867 this.goalstack16 = this.goalstack17;
868 this.goalstack17 = this.goalstack18;
869 this.goalstack18 = this.goalstack19;
870 this.goalstack19 = this.goalstack20;
871 this.goalstack20 = this.goalstack21;
872 this.goalstack21 = this.goalstack22;
873 this.goalstack22 = this.goalstack23;
874 this.goalstack23 = this.goalstack24;
875 this.goalstack24 = this.goalstack25;
876 this.goalstack25 = this.goalstack26;
877 this.goalstack26 = this.goalstack27;
878 this.goalstack27 = this.goalstack28;
879 this.goalstack28 = this.goalstack29;
880 this.goalstack29 = this.goalstack30;
881 this.goalstack30 = this.goalstack31;
882 this.goalstack31 = NULL;
885 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
886 // waypoint destination coordinates instead of v (only useful for box waypoints)
887 // for normal waypoints v2 == v and v2_height == 0
888 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)
890 if (vdist(v - org, <, bestdist))
892 traceline(v, org, true, ent);
893 if (trace_fraction == 1)
897 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
902 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
910 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
911 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
914 ent = ent.tag_entity;
916 vector pm1 = ent.origin + ent.mins;
917 vector pm2 = ent.origin + ent.maxs;
919 // do two scans, because box test is cheaper
920 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
922 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
924 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
926 waypoint_clearlinks(ent); // initialize wpXXmincost fields
927 navigation_item_addlink(it, ent);
933 vector org = ent.origin;
934 if (navigation_testtracewalk)
940 if(ent.size && !IS_PLAYER(ent))
942 org += 0.5 * (ent.mins + ent.maxs);
943 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
946 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
948 waypoint_clearlinks(ent); // initialize wpXXmincost fields
949 IL_EACH(g_waypoints, it != ent,
951 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
954 set_tracewalk_dest(ent, it.origin, false);
955 if (vdist(tracewalk_dest - it.origin, <, 1050)
956 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
957 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
959 navigation_item_addlink(it, ent);
964 // box check failed, try walk
965 IL_EACH(g_waypoints, it != ent,
967 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
973 set_tracewalk_dest(ent, v, true);
974 if (trace_ent == ent)
982 set_tracewalk_dest(it, org, false);
984 if (navigation_waypoint_will_link(v, org, ent,
985 tracewalk_dest, tracewalk_dest_height,
986 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
989 bestdist = vlen(tracewalk_dest - v);
991 bestdist = vlen(v - org);
995 if(!best && !ent.navigation_dynamicgoal)
997 int solid_save = ent.solid;
998 ent.solid = SOLID_BSP;
999 IL_EACH(g_jumppads, true,
1001 if(trigger_push_test(it, ent))
1003 best = it.nearestwaypoint;
1007 ent.solid = solid_save;
1011 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1013 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1014 if (autocvar_g_waypointeditor_auto)
1016 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1018 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1023 // finds the waypoints near the bot initiating a navigation query
1024 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1026 //navigation_testtracewalk = true;
1028 IL_EACH(g_waypoints, !it.wpconsidered,
1030 set_tracewalk_dest(it, this.origin, false);
1032 vector diff = tracewalk_dest - this.origin;
1033 diff.z = max(0, diff.z);
1034 if(vdist(diff, <, maxdist))
1036 it.wpconsidered = true;
1037 if (tracewalk(this, this.origin, this.mins, this.maxs,
1038 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1040 it.wpnearestpoint = tracewalk_dest;
1041 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1048 //navigation_testtracewalk = false;
1052 // updates a path link if a spawnfunc_waypoint link is better than the current one
1053 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1059 m1 = wp.origin + wp.mins;
1060 m2 = wp.origin + wp.maxs;
1061 v.x = bound(m1_x, p.x, m2_x);
1062 v.y = bound(m1_y, p.y, m2_y);
1063 v.z = bound(m1_z, p.z, m2_z);
1067 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1068 cost += w.wp00mincost; // assuming teleport has exactly one destination
1070 cost += waypoint_gettravelcost(p, v, w, wp);
1071 if (wp.wpcost > cost)
1076 wp.wpnearestpoint = v;
1080 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1081 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1086 IL_EACH(g_waypoints, true,
1088 it.wpconsidered = false;
1089 it.wpnearestpoint = '0 0 0';
1090 it.wpcost = 10000000;
1095 if(fixed_source_waypoint)
1097 fixed_source_waypoint.wpconsidered = true;
1098 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1099 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1100 fixed_source_waypoint.wpfire = 1;
1101 fixed_source_waypoint.enemy = NULL;
1105 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1106 // as this search is expensive we will use lower values if the bot is on the air
1107 float increment, maxdistance;
1108 if(IS_ONGROUND(this))
1111 maxdistance = 50000;
1119 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1122 bool searching = true;
1126 IL_EACH(g_waypoints, it.wpfire,
1131 p = it.wpnearestpoint;
1133 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1170 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1171 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1175 IL_EACH(g_waypoints, true,
1177 it.wpconsidered = false;
1178 it.wpnearestpoint = '0 0 0';
1179 it.wpcost = 10000000;
1184 if(fixed_source_waypoint)
1186 fixed_source_waypoint.wpconsidered = true;
1187 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1188 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1189 fixed_source_waypoint.wpfire = 1;
1190 fixed_source_waypoint.enemy = NULL;
1194 error("need to start with a waypoint\n");
1197 bool searching = true;
1201 IL_EACH(g_waypoints, it.wpfire,
1205 cost = it.wpcost; // cost to walk from it to home
1206 p = it.wpnearestpoint;
1208 IL_EACH(g_waypoints, it != wp,
1210 if(!waypoint_islinked(it, wp))
1212 cost2 = cost + it.dmg;
1213 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1219 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1220 void navigation_routerating(entity this, entity e, float f, float rangebias)
1222 if (!e || e.blacklisted) { return; }
1224 rangebias = waypoint_getlinearcost(rangebias);
1225 f = waypoint_getlinearcost(f);
1229 bool rate_wps = false;
1230 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1231 || (e.flags & FL_PARTIALGROUND))
1238 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1239 int t = pointcontents(trace_endpos + '0 0 1');
1240 if(t != CONTENT_SOLID )
1242 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1244 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1251 entity theEnemy = e;
1252 entity best_wp = NULL;
1253 float best_dist = FLOAT_MAX;
1254 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1255 && vdist(it.origin - theEnemy.origin, <, 500)
1256 && vdist(it.origin - this.origin, >, 100)
1257 && vdist(it.origin - this.origin, <, 10000),
1259 float dist = vlen2(it.origin - theEnemy.origin);
1260 if (dist < best_dist)
1272 vector goal_org = (e.absmin + e.absmax) * 0.5;
1274 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1276 // Evaluate path using jetpack
1277 if(this.items & IT_JETPACK)
1278 if(autocvar_bot_ai_navigation_jetpack)
1279 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1281 vector pointa, pointb;
1283 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1286 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1287 pointa = trace_endpos - '0 0 1';
1290 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1291 pointb = trace_endpos - '0 0 1';
1293 // Can I see these two points from the sky?
1294 traceline(pointa, pointb, MOVE_NORMAL, this);
1296 if(trace_fraction==1)
1298 LOG_DEBUG("jetpack ai: can bridge these two points");
1300 // Lower the altitude of these points as much as possible
1301 float zdistance, xydistance, cost, t, fuel;
1302 vector down, npa, npb;
1304 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1307 npa = pointa + down;
1308 npb = pointb + down;
1310 if(npa.z<=this.absmax.z)
1313 if(npb.z<=e.absmax.z)
1316 traceline(npa, npb, MOVE_NORMAL, this);
1317 if(trace_fraction==1)
1323 while(trace_fraction == 1);
1326 // Rough estimation of fuel consumption
1327 // (ignores acceleration and current xyz velocity)
1328 xydistance = vlen(pointa - pointb);
1329 zdistance = fabs(pointa.z - this.origin.z);
1331 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1332 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1333 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1335 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResourceAmount(this, RESOURCE_FUEL)));
1338 if(GetResourceAmount(this, RESOURCE_FUEL) > fuel || (this.items & IT_UNLIMITED_WEAPON_AMMO))
1341 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1342 // - between air and ground speeds)
1344 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1345 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1348 // Compare against other goals
1349 f = f * rangebias / (rangebias + cost);
1351 if (navigation_bestrating < f)
1353 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1354 navigation_bestrating = f;
1355 navigation_bestgoal = e;
1356 this.navigation_jetpack_goal = e;
1357 this.navigation_jetpack_point = pointb;
1365 //te_wizspike(e.origin);
1368 // update the cached spawnfunc_waypoint link on a dynamic item entity
1369 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1375 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1376 e.nearestwaypoint = NULL;
1378 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1379 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1381 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1382 e.nearestwaypoint = nwp = e.goalcurrent;
1384 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1387 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1389 if(!e.navigation_dynamicgoal)
1390 e.blacklisted = true;
1394 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1399 if(e.navigation_dynamicgoal)
1400 e.nearestwaypointtimeout = time + 2;
1401 else if(autocvar_g_waypointeditor)
1402 e.nearestwaypointtimeout = time + 3 + random() * 2;
1404 nwp = e.nearestwaypoint;
1407 if (nwp && nwp.wpcost < 10000000)
1409 //te_wizspike(nwp.wpnearestpoint);
1410 float nwptoitem_cost = 0;
1411 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1412 nwptoitem_cost = nwp.wp00mincost;
1414 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1415 float cost = nwp.wpcost + nwptoitem_cost;
1416 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1417 f = f * rangebias / (rangebias + cost);
1418 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1419 if (navigation_bestrating < f)
1421 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1422 navigation_bestrating = f;
1423 navigation_bestgoal = e;
1428 // adds an item to the the goal stack with the path to a given item
1429 bool navigation_routetogoal(entity this, entity e, vector startposition)
1431 // if there is no goal, just exit
1435 entity teleport_goal = NULL;
1437 this.goalentity = e;
1439 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1441 // force teleport destination as route destination
1443 navigation_pushroute(this, e.wp00);
1444 this.goalentity = e.wp00;
1447 // put the entity on the goal stack
1448 //print("routetogoal ", etos(e), "\n");
1449 navigation_pushroute(this, e);
1452 e = this.goalentity;
1454 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1456 this.wp_goal_prev1 = this.wp_goal_prev0;
1457 this.wp_goal_prev0 = e;
1461 if(e==this.navigation_jetpack_goal)
1464 // if it can reach the goal there is nothing more to do
1465 set_tracewalk_dest(e, startposition, true);
1466 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1467 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1468 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1473 entity nearest_wp = NULL;
1474 // see if there are waypoints describing a path to the item
1475 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1477 e = e.nearestwaypoint;
1480 else if(teleport_goal)
1483 e = e.enemy; // we already have added it, so...
1488 if(nearest_wp && nearest_wp.enemy)
1490 // often path can be optimized by not adding the nearest waypoint
1491 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1493 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1495 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1496 e = nearest_wp.enemy;
1499 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1500 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1501 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1502 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1503 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1505 e = nearest_wp.enemy;
1510 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1511 e = nearest_wp.enemy;
1516 // add the spawnfunc_waypoint to the path
1517 navigation_pushroute(this, e);
1527 // shorten path by removing intermediate goals
1528 bool navigation_shortenpath(entity this)
1530 if (!this.goalstack01 || wasfreed(this.goalstack01))
1532 if (this.bot_tracewalk_time > time)
1534 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1536 bool cut_allowed = false;
1537 entity next = this.goalentity;
1538 // evaluate whether bot can discard current route and chase directly a player, trying to
1539 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1540 if (IS_MOVABLE(next))
1542 set_tracewalk_dest(next, this.origin, true);
1543 if (vdist(this.origin - tracewalk_dest, <, 200))
1545 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1546 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1547 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1548 && checkpvs(this.origin + this.view_ofs, next))
1550 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1554 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1555 while (deviation.y < -180) deviation.y += 360;
1556 while (deviation.y > 180) deviation.y -= 360;
1557 if (fabs(deviation.y) > 25)
1563 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1564 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1566 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1569 navigation_poproute(this);
1571 while (this.goalcurrent != next);
1578 next = this.goalstack01;
1579 // if for some reason the bot is closer to the next goal, pop the current one
1580 if (!IS_MOVABLE(next) // already checked in the previous case
1581 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1582 && checkpvs(this.origin + this.view_ofs, next))
1584 set_tracewalk_dest(next, this.origin, true);
1590 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1591 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1593 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1594 navigation_poproute(this);
1601 // removes any currently touching waypoints from the goal stack
1602 // (this is how bots detect if they reached a goal)
1603 int navigation_poptouchedgoals(entity this)
1605 int removed_goals = 0;
1607 if(!this.goalcurrent)
1608 return removed_goals;
1610 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1612 if (!this.goalcurrent.wpisbox // warpzone
1613 && vlen2(this.origin - this.goalstack01.origin) < vlen2(this.origin - this.goalcurrent.origin))
1615 // immediately remove origin and destination waypoints
1616 navigation_poproute(this);
1618 navigation_poproute(this);
1620 this.lastteleporttime = 0;
1623 // make sure jumppad is really hit, don't rely on distance based checks
1624 // as they may report a touch even if it didn't really happen
1625 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1627 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1628 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1630 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1631 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1633 if(this.jumppadcount)
1635 // remove jumppad waypoint after a random delay to prevent bots getting
1636 // stuck on certain jumppads that require an extra initial horizontal speed
1637 float max_delay = 0.1;
1638 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1640 if (time - this.lastteleporttime < random() * max_delay)
1641 return removed_goals;
1643 else if (this.goalcurrent.wpisbox) // teleport
1645 // immediately remove origin and destination waypoints
1646 navigation_poproute(this);
1649 navigation_poproute(this);
1650 this.lastteleporttime = 0;
1653 return removed_goals;
1655 else if (this.lastteleporttime > 0)
1657 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1658 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1659 // if so immediately fix bot path by removing skipped goals
1660 entity tele_ent = NULL;
1661 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1662 tele_ent = this.goalstack01;
1663 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1664 tele_ent = this.goalstack02;
1665 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1666 tele_ent = this.goalstack03;
1667 if (tele_ent && TELEPORT_USED(this, tele_ent))
1669 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1670 if (tele_ent.wpflags & WAYPOINTFLAG_PERSONAL && tele_ent.owner == this)
1672 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1673 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1675 while (this.goalcurrent != tele_ent)
1677 navigation_poproute(this);
1680 navigation_poproute(this);
1681 this.lastteleporttime = 0;
1683 return removed_goals;
1685 // reset of lastteleporttime can be overriden by a jumppad when it's set
1686 // in more than one frame: make sure it's reset
1687 this.lastteleporttime = 0;
1690 // Loose goal touching check when running
1691 if(this.aistatus & AI_STATUS_RUNNING)
1692 if(this.goalcurrent.classname=="waypoint")
1693 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1695 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1697 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1698 if(trace_fraction==1)
1700 // Detect personal waypoints
1701 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1702 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1704 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1705 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1708 navigation_poproute(this);
1710 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1711 return removed_goals;
1716 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1718 vector gc_min = this.goalcurrent.absmin;
1719 vector gc_max = this.goalcurrent.absmax;
1720 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1722 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1723 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1725 if (time < this.ladder_time)
1727 if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1732 if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1736 // Detect personal waypoints
1737 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1738 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1740 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1741 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1744 navigation_poproute(this);
1746 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1747 return removed_goals;
1749 return removed_goals;
1752 entity navigation_get_really_close_waypoint(entity this)
1754 entity wp = this.goalcurrent;
1756 wp = this.goalcurrent_prev;
1759 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, 50))
1761 wp = this.goalcurrent_prev;
1765 if(wp.classname != "waypoint")
1767 wp = wp.nearestwaypoint;
1771 if(vdist(wp.origin - this.origin, >, 50))
1774 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1776 if(vdist(it.origin - this.origin, <, 50))
1785 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1788 set_tracewalk_dest(wp, this.origin, false);
1789 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1790 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1797 // begin a goal selection session (queries spawnfunc_waypoint network)
1798 void navigation_goalrating_start(entity this)
1800 if(this.aistatus & AI_STATUS_STUCK)
1803 this.navigation_jetpack_goal = NULL;
1804 navigation_bestrating = -1;
1805 entity wp = navigation_get_really_close_waypoint(this);
1806 navigation_clearroute(this);
1807 navigation_bestgoal = NULL;
1808 navigation_markroutes(this, wp);
1809 this.goalstack31 = wp; // temporarly save the really close waypoint
1812 // ends a goal selection session (updates goal stack to the best goal)
1813 void navigation_goalrating_end(entity this)
1815 if(this.aistatus & AI_STATUS_STUCK)
1818 entity wp = this.goalstack31; // save to wp as this.goalstack31 is set by navigation_routetogoal
1819 this.goalstack31 = NULL;
1821 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1822 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1824 if (wp && this.goalcurrent == wp)
1825 navigation_poproute(this);
1827 // If the bot got stuck then try to reach the farthest waypoint
1828 if (!this.goalentity)
1830 if (autocvar_bot_wander_enable && !(this.aistatus & AI_STATUS_STUCK))
1832 LOG_DEBUG(this.netname, " cannot walk to any goal");
1833 this.aistatus |= AI_STATUS_STUCK;
1835 this.goalentity_shouldbefrozen = false;
1838 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1841 void botframe_updatedangerousobjects(float maxupdate)
1843 vector m1, m2, v, o;
1847 IL_EACH(g_waypoints, true,
1853 IL_EACH(g_bot_dodge, it.bot_dodge,
1856 v.x = bound(m1_x, v.x, m2_x);
1857 v.y = bound(m1_y, v.y, m2_y);
1858 v.z = bound(m1_z, v.z, m2_z);
1859 o = (it.absmin + it.absmax) * 0.5;
1860 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1863 traceline(o, v, true, NULL);
1864 if (trace_fraction == 1)
1865 danger = danger + d;
1875 void navigation_unstuck(entity this)
1877 if (!autocvar_bot_wander_enable)
1880 bool has_user_waypoints = false;
1881 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1883 has_user_waypoints = true;
1886 if (!has_user_waypoints)
1889 float search_radius = 1000;
1891 if (!bot_waypoint_queue_owner)
1893 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1894 bot_waypoint_queue_owner = this;
1895 bot_waypoint_queue_bestgoal = NULL;
1896 bot_waypoint_queue_bestgoalrating = 0;
1899 if(bot_waypoint_queue_owner!=this)
1902 if (bot_waypoint_queue_goal)
1904 // evaluate the next goal on the queue
1905 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1906 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1907 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1908 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1909 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1911 if( d > bot_waypoint_queue_bestgoalrating)
1913 bot_waypoint_queue_bestgoalrating = d;
1914 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1918 // move to a random waypoint while bot is searching for a walkable path;
1919 // this is usually sufficient to unstuck bots from bad spots or when other
1920 // bots of the same team block all their ways
1921 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1923 navigation_clearroute(this);
1924 navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1925 navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1928 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1930 if (!bot_waypoint_queue_goal)
1932 if (bot_waypoint_queue_bestgoal)
1934 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1935 navigation_clearroute(this);
1936 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1937 navigation_goalrating_timeout_set(this);
1938 this.aistatus &= ~AI_STATUS_STUCK;
1942 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1945 bot_waypoint_queue_owner = NULL;
1950 if(bot_strategytoken!=this)
1953 // build a new queue
1954 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1956 entity first = NULL;
1958 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1960 if(bot_waypoint_queue_goal)
1961 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1965 bot_waypoint_queue_goal = it;
1966 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1970 bot_waypoint_queue_goal = first;
1973 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1974 bot_waypoint_queue_owner = NULL;
1979 // Support for debugging tracewalk visually
1981 void debugresetnodes()
1983 debuglastnode = '0 0 0';
1986 void debugnode(entity this, vector node)
1988 if (!IS_PLAYER(this))
1991 if(debuglastnode=='0 0 0')
1993 debuglastnode = node;
1997 te_lightning2(NULL, node, debuglastnode);
1998 debuglastnode = node;
2001 void debugnodestatus(vector position, float status)
2007 case DEBUG_NODE_SUCCESS:
2010 case DEBUG_NODE_WARNING:
2013 case DEBUG_NODE_FAIL:
2020 te_customflash(position, 40, 2, c);
2023 // Support for debugging the goal stack visually
2026 .vector lastposition;
2028 // Debug the goal stack visually
2029 void debuggoalstack(entity this)
2034 if(this.goalcounter==0)goal=this.goalcurrent;
2035 else if(this.goalcounter==1)goal=this.goalstack01;
2036 else if(this.goalcounter==2)goal=this.goalstack02;
2037 else if(this.goalcounter==3)goal=this.goalstack03;
2038 else if(this.goalcounter==4)goal=this.goalstack04;
2039 else if(this.goalcounter==5)goal=this.goalstack05;
2040 else if(this.goalcounter==6)goal=this.goalstack06;
2041 else if(this.goalcounter==7)goal=this.goalstack07;
2042 else if(this.goalcounter==8)goal=this.goalstack08;
2043 else if(this.goalcounter==9)goal=this.goalstack09;
2044 else if(this.goalcounter==10)goal=this.goalstack10;
2045 else if(this.goalcounter==11)goal=this.goalstack11;
2046 else if(this.goalcounter==12)goal=this.goalstack12;
2047 else if(this.goalcounter==13)goal=this.goalstack13;
2048 else if(this.goalcounter==14)goal=this.goalstack14;
2049 else if(this.goalcounter==15)goal=this.goalstack15;
2050 else if(this.goalcounter==16)goal=this.goalstack16;
2051 else if(this.goalcounter==17)goal=this.goalstack17;
2052 else if(this.goalcounter==18)goal=this.goalstack18;
2053 else if(this.goalcounter==19)goal=this.goalstack19;
2054 else if(this.goalcounter==20)goal=this.goalstack20;
2055 else if(this.goalcounter==21)goal=this.goalstack21;
2056 else if(this.goalcounter==22)goal=this.goalstack22;
2057 else if(this.goalcounter==23)goal=this.goalstack23;
2058 else if(this.goalcounter==24)goal=this.goalstack24;
2059 else if(this.goalcounter==25)goal=this.goalstack25;
2060 else if(this.goalcounter==26)goal=this.goalstack26;
2061 else if(this.goalcounter==27)goal=this.goalstack27;
2062 else if(this.goalcounter==28)goal=this.goalstack28;
2063 else if(this.goalcounter==29)goal=this.goalstack29;
2064 else if(this.goalcounter==30)goal=this.goalstack30;
2065 else if(this.goalcounter==31)goal=this.goalstack31;
2070 this.goalcounter = 0;
2071 this.lastposition='0 0 0';
2075 if(this.lastposition=='0 0 0')
2078 org = this.lastposition;
2081 go = ( goal.absmin + goal.absmax ) * 0.5;
2082 te_lightning2(NULL, org, go);
2083 this.lastposition = go;