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/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 // use this when current goal must be discarded immediately
29 void navigation_goalrating_timeout_force(entity this)
31 navigation_goalrating_timeout_expire(this, 0);
34 // use this when current goal can be kept for a short while to increase the chance
35 // of bot touching a waypoint, which helps to find a new goal more efficiently
36 void navigation_goalrating_timeout_expire(entity this, float seconds)
39 this.bot_strategytime = 0;
40 else if (this.bot_strategytime > time + seconds)
41 this.bot_strategytime = time + seconds;
44 bool navigation_goalrating_timeout(entity this)
46 return this.bot_strategytime < time;
49 #define MAX_CHASE_DISTANCE 700
50 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
52 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
53 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
54 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
59 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
61 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
63 this.ignoregoal = this.goalentity;
64 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
71 void navigation_dynamicgoal_init(entity this, bool initially_static)
73 this.navigation_dynamicgoal = true;
74 this.bot_basewaypoint = this.nearestwaypoint;
76 this.nearestwaypointtimeout = -1;
78 this.nearestwaypointtimeout = time;
81 void navigation_dynamicgoal_set(entity this, entity dropper)
83 this.nearestwaypointtimeout = time;
84 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
85 this.nearestwaypoint = dropper.nearestwaypoint;
86 if (this.nearestwaypoint)
87 this.nearestwaypointtimeout += 2;
90 void navigation_dynamicgoal_unset(entity this)
92 if(this.bot_basewaypoint)
93 this.nearestwaypoint = this.bot_basewaypoint;
94 this.nearestwaypointtimeout = -1;
97 // returns point of ent closer to org
98 vector get_closer_dest(entity ent, vector org)
100 vector dest = '0 0 0';
101 if ((ent.classname != "waypoint") || ent.wpisbox)
103 vector wm1 = ent.origin + ent.mins;
104 vector wm2 = ent.origin + ent.maxs;
105 dest.x = bound(wm1.x, org.x, wm2.x);
106 dest.y = bound(wm1.y, org.y, wm2.y);
107 dest.z = bound(wm1.z, org.z, wm2.z);
114 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
116 if ((ent.classname != "waypoint") || ent.wpisbox)
118 vector wm1 = ent.origin + ent.mins;
119 vector wm2 = ent.origin + ent.maxs;
120 if (IS_PLAYER(ent) || IS_MONSTER(ent))
122 // move destination point out of player bbox otherwise tracebox always fails
123 // (if bot_navigation_ignoreplayers is false)
124 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
125 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
127 // set destination point to x and y coords of ent that are closer to org
128 // z coord is set to ent's min height
129 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
130 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
131 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
132 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
134 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
135 tracewalk_dest_height = 0;
136 fix_player_dest = false;
140 tracewalk_dest.z = wm1.z;
141 tracewalk_dest_height = wm2.z - wm1.z;
146 tracewalk_dest = ent.origin;
147 tracewalk_dest_height = 0;
149 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
151 // snap player to the ground
152 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
154 // bot is right under the player
155 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
156 tracewalk_dest = trace_endpos;
157 tracewalk_dest_height = 0;
161 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
162 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
164 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
165 tracewalk_dest.z = trace_endpos.z;
171 // returns point of ent closer to org
172 vector set_tracewalk_dest_2(entity ent, vector org)
174 vector closer_dest = '0 0 0';
175 if ((ent.classname != "waypoint") || ent.wpisbox)
177 vector wm1 = ent.origin + ent.mins;
178 vector wm2 = ent.origin + ent.maxs;
179 closer_dest.x = bound(wm1.x, org.x, wm2.x);
180 closer_dest.y = bound(wm1.y, org.y, wm2.y);
181 closer_dest.z = bound(wm1.z, org.z, wm2.z);
182 // set destination point to x and y coords of ent that are closer to org
183 // z coord is set to ent's min height
184 tracewalk_dest.x = closer_dest.x;
185 tracewalk_dest.y = closer_dest.y;
186 tracewalk_dest.z = wm1.z;
187 tracewalk_dest_height = wm2.z - wm1.z; // destination height
191 closer_dest = ent.origin;
192 tracewalk_dest = closer_dest;
193 tracewalk_dest_height = 0;
198 bool navigation_check_submerged_state(entity ent, vector pos)
202 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
203 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
204 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
207 submerged = SUBMERGED(pos);
208 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
209 // is actually submerged because often they are inside some solid.
210 // That's why submerged state is saved now that we know current pos is
211 // not stuck in solid (previous tracewalk call to this pos was successfully)
212 if(!ent.navigation_dynamicgoal)
213 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
218 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
220 IL_EACH(g_ladders, it.classname == "func_ladder",
223 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
224 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
227 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
228 tracebox(org, m1, m2, top, movemode, e);
229 if(trace_fraction == 1)
236 vector resurface_limited(vector org, float lim, vector m1)
238 if (WETFEET(org + eZ * (lim - org.z)))
242 float RES_min_h = org.z;
243 float RES_max_h = lim;
245 org.z = 0.5 * (RES_min_h + RES_max_h);
250 } while (RES_max_h - RES_min_h >= 1);
255 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
258 #define NAV_SWIM_ONWATER 1
259 #define NAV_SWIM_UNDERWATER 2
261 // rough simulation of walking from one point to another to test if a path
262 // can be traveled, used for waypoint linking and havocbot
263 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
264 // INFO: the command sv_cmd trace walk is useful to test this function in game
265 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
267 if(autocvar_bot_debug_tracewalk)
274 vector flatdir = end - start;
276 float flatdist = vlen(flatdir);
277 flatdir = normalize(flatdir);
279 bool ignorehazards = false;
282 // Analyze starting point
284 ignorehazards = true;
286 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
287 if (trace_startsolid)
290 if(autocvar_bot_debug_tracewalk)
291 debugnodestatus(start, DEBUG_NODE_FAIL);
293 //print("tracewalk: ", vtos(start), " is a bad start\n");
299 end2.z += end_height;
301 vector fixed_end = end;
304 if (flatdist > 0 && WETFEET(org))
307 nav_action = NAV_SWIM_UNDERWATER;
310 // tracebox down by player's height
311 // useful to know if water level is so low that bot can still walk
312 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
313 if (SUBMERGED(trace_endpos))
316 nav_action = NAV_SWIM_UNDERWATER;
319 nav_action = NAV_WALK;
323 nav_action = NAV_WALK;
331 if (org.z > end2.z + 1)
333 tracebox(org, m1, m2, end2, movemode, e);
335 if (org.z > end2.z + 1)
338 else if (org.z < end.z - 1)
340 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
341 if (SUBMERGED(trace_endpos))
343 vector v = trace_endpos;
344 tracebox(v, m1, m2, end, movemode, e);
345 if(trace_endpos.z >= end.z - 1)
347 RESURFACE_LIMITED(v, trace_endpos.z);
351 else if (trace_endpos.z > org.z - jumpheight_vec.z)
352 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
354 if (org.z < end.z - 1)
361 if(autocvar_bot_debug_tracewalk)
364 debugnodestatus(org, DEBUG_NODE_SUCCESS);
367 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
372 if(autocvar_bot_debug_tracewalk)
378 if (stepdist > flatdist)
380 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
382 // can't use movement direction here to calculate move because of
383 // precision errors especially when direction has a high enough z value
384 //water_dir = normalize(water_end - org);
385 //move = org + water_dir * stepdist;
386 fixed_end.z = bound(end.z, org.z, end2.z);
387 if (stepdist == flatdist) {
391 move = org + (fixed_end - org) * (stepdist / flatdist);
392 flatdist = vlen(vec2(fixed_end - move));
395 else // horiz. direction
397 flatdist -= stepdist;
398 move = org + flatdir * stepdist;
401 if(nav_action == NAV_SWIM_ONWATER)
403 tracebox(org, m1, m2, move, movemode, e); // swim
406 if (trace_fraction < 1)
409 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
411 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
414 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
416 if(autocvar_bot_debug_tracewalk)
419 debugnodestatus(org, DEBUG_NODE_SUCCESS);
422 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
426 if(autocvar_bot_debug_tracewalk)
427 debugnodestatus(org, DEBUG_NODE_FAIL);
430 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
441 if (org.z <= move.z) // going horiz.
443 tracebox(trace_endpos, m1, m2, move, movemode, e);
445 nav_action = NAV_WALK;
450 if (org.z <= move.z) // going horiz.
453 nav_action = NAV_SWIM_ONWATER;
459 nav_action = NAV_SWIM_UNDERWATER;
461 nav_action = NAV_SWIM_ONWATER;
464 else if(nav_action == NAV_SWIM_UNDERWATER)
466 if (move.z >= org.z) // swimming upwards or horiz.
468 tracebox(org, m1, m2, move, movemode, e); // swim
470 bool stepswum = false;
473 if (trace_fraction < 1)
476 vector stepswim_move = move + stepheightvec;
477 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
478 stepswim_move.z = end2.z;
480 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
483 if (trace_startsolid)
485 if(autocvar_bot_debug_tracewalk)
486 debugnodestatus(org, DEBUG_NODE_FAIL);
488 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
492 if (trace_fraction < 1)
494 float org_z_prev = org.z;
495 RESURFACE_LIMITED(org, end2.z);
496 if(org.z == org_z_prev)
498 if(autocvar_bot_debug_tracewalk)
499 debugnodestatus(org, DEBUG_NODE_FAIL);
501 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
505 nav_action = NAV_SWIM_UNDERWATER;
507 nav_action = NAV_SWIM_ONWATER;
509 // we didn't advance horiz. in this step, flatdist decrease should be reverted
510 // but we can't do it properly right now... apply this workaround instead
528 if (!WETFEET(trace_endpos))
530 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
531 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
532 if (!stepswum && SUBMERGED(trace_endpos))
534 RESURFACE_LIMITED(trace_endpos, end2.z);
536 nav_action = NAV_SWIM_ONWATER;
542 nav_action = NAV_WALK;
548 nav_action = NAV_SWIM_UNDERWATER;
551 else //if (move.z < org.z) // swimming downwards
553 tracebox(org, m1, m2, move, movemode, e); // swim
556 if (trace_fraction < 1)
559 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
562 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
564 if(autocvar_bot_debug_tracewalk)
565 debugnodestatus(move, DEBUG_NODE_FAIL);
567 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
579 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
581 // stepswim caused upwards direction
582 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
583 if (!SUBMERGED(trace_endpos))
586 nav_action = NAV_WALK;
593 nav_action = NAV_SWIM_UNDERWATER;
597 else if(nav_action == NAV_WALK)
600 tracebox(org, m1, m2, move, movemode, e);
602 if(autocvar_bot_debug_tracewalk)
603 debugnode(e, trace_endpos);
606 if (trace_fraction < 1)
608 // check if we can walk over this obstacle, possibly by jumpstepping
609 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
610 if (trace_fraction < 1 || trace_startsolid)
612 if (trace_startsolid) // hit ceiling above org
614 // reduce stepwalk height
615 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
616 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
618 else //if (trace_fraction < 1)
620 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
621 if (trace_startsolid) // hit ceiling above org
623 // reduce jumpstepwalk height
624 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
625 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
629 if (trace_fraction < 1)
631 vector v = trace_endpos;
632 v.z = org.z + jumpheight_vec.z;
633 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
635 if(autocvar_bot_debug_tracewalk)
638 debugnodestatus(v, DEBUG_NODE_SUCCESS);
641 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
645 if(autocvar_bot_debug_tracewalk)
646 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
648 traceline( org, move, movemode, e);
650 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
654 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
656 nextmove = move + (flatdir * stepdist);
657 traceline( move, nextmove, movemode, e);
660 flatdist = vlen(vec2(end - move));
664 if(autocvar_bot_debug_tracewalk)
665 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
667 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
668 //te_explosion(trace_endpos);
669 //print(ftos(e.dphitcontentsmask), "\n");
670 return false; // failed
682 // trace down from stepheight as far as possible and move there,
683 // if this starts in solid we try again without the stepup, and
684 // if that also fails we assume it is a wall
685 // (this is the same logic as the Quake walkmove function used)
686 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
694 if(autocvar_bot_debug_tracewalk)
696 debugnode(e, trace_endpos);
697 debugnodestatus(org, DEBUG_NODE_FAIL);
700 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
707 if(move.z >= end2.z && org.z < end2.z)
712 if(org.z > move.z - 1 || !SUBMERGED(org))
714 nav_action = NAV_WALK;
718 // ended up submerged while walking
719 if(autocvar_bot_debug_tracewalk)
722 RESURFACE_LIMITED(org, move.z);
723 nav_action = NAV_SWIM_ONWATER;
728 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
730 // moved but didn't arrive at the intended destination
731 if(autocvar_bot_debug_tracewalk)
732 debugnodestatus(org, DEBUG_NODE_FAIL);
737 /////////////////////////////////////////////////////////////////////////////
739 /////////////////////////////////////////////////////////////////////////////
741 // completely empty the goal stack, used when deciding where to go
742 void navigation_clearroute(entity this)
744 this.goalcurrent_prev = this.goalcurrent;
745 this.goalcurrent_distance_2d = FLOAT_MAX;
746 this.goalcurrent_distance_z = FLOAT_MAX;
747 this.goalcurrent_distance_time = 0;
748 this.goalentity_lock_timeout = 0;
749 this.goalentity_shouldbefrozen = false;
750 this.goalentity = NULL;
751 this.goalcurrent = NULL;
752 this.goalstack01 = NULL;
753 this.goalstack02 = NULL;
754 this.goalstack03 = NULL;
755 this.goalstack04 = NULL;
756 this.goalstack05 = NULL;
757 this.goalstack06 = NULL;
758 this.goalstack07 = NULL;
759 this.goalstack08 = NULL;
760 this.goalstack09 = NULL;
761 this.goalstack10 = NULL;
762 this.goalstack11 = NULL;
763 this.goalstack12 = NULL;
764 this.goalstack13 = NULL;
765 this.goalstack14 = NULL;
766 this.goalstack15 = NULL;
767 this.goalstack16 = NULL;
768 this.goalstack17 = NULL;
769 this.goalstack18 = NULL;
770 this.goalstack19 = NULL;
771 this.goalstack20 = NULL;
772 this.goalstack21 = NULL;
773 this.goalstack22 = NULL;
774 this.goalstack23 = NULL;
775 this.goalstack24 = NULL;
776 this.goalstack25 = NULL;
777 this.goalstack26 = NULL;
778 this.goalstack27 = NULL;
779 this.goalstack28 = NULL;
780 this.goalstack29 = NULL;
781 this.goalstack30 = NULL;
782 this.goalstack31 = NULL;
785 // add a new goal at the beginning of the stack
786 // (in other words: add a new prerequisite before going to the later goals)
787 // NOTE: when a waypoint is added, the WP gets pushed first, then the
788 // next-closest WP on the shortest path to the WP
789 // That means, if the stack overflows, the bot will know how to do the FIRST 32
790 // steps to the goal, and then recalculate the path.
791 void navigation_pushroute(entity this, entity e)
793 this.goalcurrent_prev = this.goalcurrent;
794 this.goalcurrent_distance_2d = FLOAT_MAX;
795 this.goalcurrent_distance_z = FLOAT_MAX;
796 this.goalcurrent_distance_time = 0;
797 //print("bot ", etos(this), " push ", etos(e), "\n");
798 if(this.goalstack31 == this.goalentity)
799 this.goalentity = NULL;
800 this.goalstack31 = this.goalstack30;
801 this.goalstack30 = this.goalstack29;
802 this.goalstack29 = this.goalstack28;
803 this.goalstack28 = this.goalstack27;
804 this.goalstack27 = this.goalstack26;
805 this.goalstack26 = this.goalstack25;
806 this.goalstack25 = this.goalstack24;
807 this.goalstack24 = this.goalstack23;
808 this.goalstack23 = this.goalstack22;
809 this.goalstack22 = this.goalstack21;
810 this.goalstack21 = this.goalstack20;
811 this.goalstack20 = this.goalstack19;
812 this.goalstack19 = this.goalstack18;
813 this.goalstack18 = this.goalstack17;
814 this.goalstack17 = this.goalstack16;
815 this.goalstack16 = this.goalstack15;
816 this.goalstack15 = this.goalstack14;
817 this.goalstack14 = this.goalstack13;
818 this.goalstack13 = this.goalstack12;
819 this.goalstack12 = this.goalstack11;
820 this.goalstack11 = this.goalstack10;
821 this.goalstack10 = this.goalstack09;
822 this.goalstack09 = this.goalstack08;
823 this.goalstack08 = this.goalstack07;
824 this.goalstack07 = this.goalstack06;
825 this.goalstack06 = this.goalstack05;
826 this.goalstack05 = this.goalstack04;
827 this.goalstack04 = this.goalstack03;
828 this.goalstack03 = this.goalstack02;
829 this.goalstack02 = this.goalstack01;
830 this.goalstack01 = this.goalcurrent;
831 this.goalcurrent = e;
834 // remove first goal from stack
835 // (in other words: remove a prerequisite for reaching the later goals)
836 // (used when a spawnfunc_waypoint is reached)
837 void navigation_poproute(entity this)
839 this.goalcurrent_prev = this.goalcurrent;
840 this.goalcurrent_distance_2d = FLOAT_MAX;
841 this.goalcurrent_distance_z = FLOAT_MAX;
842 this.goalcurrent_distance_time = 0;
843 //print("bot ", etos(this), " pop\n");
844 if(this.goalcurrent == this.goalentity)
846 this.goalentity = NULL;
847 this.goalentity_lock_timeout = 0;
849 this.goalcurrent = this.goalstack01;
850 this.goalstack01 = this.goalstack02;
851 this.goalstack02 = this.goalstack03;
852 this.goalstack03 = this.goalstack04;
853 this.goalstack04 = this.goalstack05;
854 this.goalstack05 = this.goalstack06;
855 this.goalstack06 = this.goalstack07;
856 this.goalstack07 = this.goalstack08;
857 this.goalstack08 = this.goalstack09;
858 this.goalstack09 = this.goalstack10;
859 this.goalstack10 = this.goalstack11;
860 this.goalstack11 = this.goalstack12;
861 this.goalstack12 = this.goalstack13;
862 this.goalstack13 = this.goalstack14;
863 this.goalstack14 = this.goalstack15;
864 this.goalstack15 = this.goalstack16;
865 this.goalstack16 = this.goalstack17;
866 this.goalstack17 = this.goalstack18;
867 this.goalstack18 = this.goalstack19;
868 this.goalstack19 = this.goalstack20;
869 this.goalstack20 = this.goalstack21;
870 this.goalstack21 = this.goalstack22;
871 this.goalstack22 = this.goalstack23;
872 this.goalstack23 = this.goalstack24;
873 this.goalstack24 = this.goalstack25;
874 this.goalstack25 = this.goalstack26;
875 this.goalstack26 = this.goalstack27;
876 this.goalstack27 = this.goalstack28;
877 this.goalstack28 = this.goalstack29;
878 this.goalstack29 = this.goalstack30;
879 this.goalstack30 = this.goalstack31;
880 this.goalstack31 = NULL;
883 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
884 // waypoint destination coordinates instead of v (only useful for box waypoints)
885 // for normal waypoints v2 == v and v2_height == 0
886 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)
888 if (vdist(v - org, <, bestdist))
890 traceline(v, org, true, ent);
891 if (trace_fraction == 1)
895 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
900 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
908 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
909 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
912 ent = ent.tag_entity;
914 vector pm1 = ent.origin + ent.mins;
915 vector pm2 = ent.origin + ent.maxs;
917 // do two scans, because box test is cheaper
918 IL_EACH(g_waypoints, it != ent && it != except,
920 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
922 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
924 waypoint_clearlinks(ent); // initialize wpXXmincost fields
925 navigation_item_addlink(it, ent);
931 vector org = ent.origin;
932 if (navigation_testtracewalk)
938 if(ent.size && !IS_PLAYER(ent))
940 org += 0.5 * (ent.mins + ent.maxs);
941 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
944 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
946 waypoint_clearlinks(ent); // initialize wpXXmincost fields
947 IL_EACH(g_waypoints, it != ent,
949 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
952 set_tracewalk_dest(ent, it.origin, false);
953 if (vdist(tracewalk_dest - it.origin, <, 1050)
954 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
955 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
957 navigation_item_addlink(it, ent);
962 // box check failed, try walk
963 IL_EACH(g_waypoints, it != ent,
965 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
971 set_tracewalk_dest(ent, v, true);
972 if (trace_ent == ent)
980 set_tracewalk_dest(it, org, false);
982 if (navigation_waypoint_will_link(v, org, ent,
983 tracewalk_dest, tracewalk_dest_height,
984 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
987 bestdist = vlen(tracewalk_dest - v);
989 bestdist = vlen(v - org);
993 if(!best && !ent.navigation_dynamicgoal)
995 int solid_save = ent.solid;
996 ent.solid = SOLID_BSP;
997 IL_EACH(g_jumppads, true,
999 if(trigger_push_test(it, ent))
1001 best = it.nearestwaypoint;
1005 ent.solid = solid_save;
1009 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1011 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1012 if (autocvar_g_waypointeditor_auto)
1014 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1016 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1021 // finds the waypoints near the bot initiating a navigation query
1022 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1024 //navigation_testtracewalk = true;
1026 IL_EACH(g_waypoints, !it.wpconsidered,
1028 set_tracewalk_dest(it, this.origin, false);
1030 vector diff = tracewalk_dest - this.origin;
1031 diff.z = max(0, diff.z);
1032 if(vdist(diff, <, maxdist))
1034 it.wpconsidered = true;
1035 if (tracewalk(this, this.origin, this.mins, this.maxs,
1036 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1038 it.wpnearestpoint = tracewalk_dest;
1039 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1046 //navigation_testtracewalk = false;
1050 // updates a path link if a spawnfunc_waypoint link is better than the current one
1051 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1057 m1 = wp.origin + wp.mins;
1058 m2 = wp.origin + wp.maxs;
1059 v.x = bound(m1_x, p.x, m2_x);
1060 v.y = bound(m1_y, p.y, m2_y);
1061 v.z = bound(m1_z, p.z, m2_z);
1065 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1066 cost += w.wp00mincost; // assuming teleport has exactly one destination
1068 cost += waypoint_gettravelcost(p, v, w, wp);
1069 if (wp.wpcost > cost)
1074 wp.wpnearestpoint = v;
1078 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1079 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1084 IL_EACH(g_waypoints, true,
1086 it.wpconsidered = false;
1087 it.wpnearestpoint = '0 0 0';
1088 it.wpcost = 10000000;
1093 if(fixed_source_waypoint)
1095 fixed_source_waypoint.wpconsidered = true;
1096 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1097 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1098 fixed_source_waypoint.wpfire = 1;
1099 fixed_source_waypoint.enemy = NULL;
1103 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1104 // as this search is expensive we will use lower values if the bot is on the air
1105 float increment, maxdistance;
1106 if(IS_ONGROUND(this))
1109 maxdistance = 50000;
1117 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1120 bool searching = true;
1124 IL_EACH(g_waypoints, it.wpfire,
1129 p = it.wpnearestpoint;
1131 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1132 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1133 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1168 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1169 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1173 IL_EACH(g_waypoints, true,
1175 it.wpconsidered = false;
1176 it.wpnearestpoint = '0 0 0';
1177 it.wpcost = 10000000;
1182 if(fixed_source_waypoint)
1184 fixed_source_waypoint.wpconsidered = true;
1185 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1186 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1187 fixed_source_waypoint.wpfire = 1;
1188 fixed_source_waypoint.enemy = NULL;
1192 error("need to start with a waypoint\n");
1195 bool searching = true;
1199 IL_EACH(g_waypoints, it.wpfire,
1203 cost = it.wpcost; // cost to walk from it to home
1204 p = it.wpnearestpoint;
1206 IL_EACH(g_waypoints, it != wp,
1208 if(!waypoint_islinked(it, wp))
1210 cost2 = cost + it.dmg;
1211 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1217 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1218 void navigation_routerating(entity this, entity e, float f, float rangebias)
1220 if (!e || e.blacklisted) { return; }
1222 rangebias = waypoint_getlinearcost(rangebias);
1223 f = waypoint_getlinearcost(f);
1227 bool rate_wps = false;
1228 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1229 || (e.flags & FL_PARTIALGROUND))
1236 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1237 int t = pointcontents(trace_endpos + '0 0 1');
1238 if(t != CONTENT_SOLID )
1240 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1242 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1249 entity theEnemy = e;
1250 entity best_wp = NULL;
1251 float best_dist = FLOAT_MAX;
1252 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1253 && vdist(it.origin - theEnemy.origin, <, 500)
1254 && vdist(it.origin - this.origin, >, 100)
1255 && vdist(it.origin - this.origin, <, 10000),
1257 float dist = vlen2(it.origin - theEnemy.origin);
1258 if (dist < best_dist)
1270 vector goal_org = (e.absmin + e.absmax) * 0.5;
1272 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1274 // Evaluate path using jetpack
1275 if(this.items & IT_JETPACK)
1276 if(autocvar_bot_ai_navigation_jetpack)
1277 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1279 vector pointa, pointb;
1281 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1284 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1285 pointa = trace_endpos - '0 0 1';
1288 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1289 pointb = trace_endpos - '0 0 1';
1291 // Can I see these two points from the sky?
1292 traceline(pointa, pointb, MOVE_NORMAL, this);
1294 if(trace_fraction==1)
1296 LOG_DEBUG("jetpack ai: can bridge these two points");
1298 // Lower the altitude of these points as much as possible
1299 float zdistance, xydistance, cost, t, fuel;
1300 vector down, npa, npb;
1302 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1305 npa = pointa + down;
1306 npb = pointb + down;
1308 if(npa.z<=this.absmax.z)
1311 if(npb.z<=e.absmax.z)
1314 traceline(npa, npb, MOVE_NORMAL, this);
1315 if(trace_fraction==1)
1321 while(trace_fraction == 1);
1324 // Rough estimation of fuel consumption
1325 // (ignores acceleration and current xyz velocity)
1326 xydistance = vlen(pointa - pointb);
1327 zdistance = fabs(pointa.z - this.origin.z);
1329 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1330 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1331 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1333 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResourceAmount(this, RESOURCE_FUEL)));
1336 if(GetResourceAmount(this, RESOURCE_FUEL) > fuel || (this.items & IT_UNLIMITED_WEAPON_AMMO))
1339 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1340 // - between air and ground speeds)
1342 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1343 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1346 // Compare against other goals
1347 f = f * rangebias / (rangebias + cost);
1349 if (navigation_bestrating < f)
1351 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1352 navigation_bestrating = f;
1353 navigation_bestgoal = e;
1354 this.navigation_jetpack_goal = e;
1355 this.navigation_jetpack_point = pointb;
1363 //te_wizspike(e.origin);
1366 // update the cached spawnfunc_waypoint link on a dynamic item entity
1367 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1373 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1374 e.nearestwaypoint = NULL;
1376 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1377 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1379 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1380 e.nearestwaypoint = nwp = e.goalcurrent;
1382 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1385 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1387 if(!e.navigation_dynamicgoal)
1388 e.blacklisted = true;
1392 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1397 if(e.navigation_dynamicgoal)
1398 e.nearestwaypointtimeout = time + 2;
1399 else if(autocvar_g_waypointeditor)
1400 e.nearestwaypointtimeout = time + 3 + random() * 2;
1402 nwp = e.nearestwaypoint;
1405 if (nwp && nwp.wpcost < 10000000)
1407 //te_wizspike(nwp.wpnearestpoint);
1408 float nwptoitem_cost = 0;
1409 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1410 nwptoitem_cost = nwp.wp00mincost;
1412 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1413 float cost = nwp.wpcost + nwptoitem_cost;
1414 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1415 f = f * rangebias / (rangebias + cost);
1416 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1417 if (navigation_bestrating < f)
1419 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1420 navigation_bestrating = f;
1421 navigation_bestgoal = e;
1426 // adds an item to the the goal stack with the path to a given item
1427 bool navigation_routetogoal(entity this, entity e, vector startposition)
1429 // if there is no goal, just exit
1433 entity teleport_goal = NULL;
1435 this.goalentity = e;
1437 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1439 // force teleport destination as route destination
1441 navigation_pushroute(this, e.wp00);
1442 this.goalentity = e.wp00;
1445 // put the entity on the goal stack
1446 //print("routetogoal ", etos(e), "\n");
1447 navigation_pushroute(this, e);
1450 e = this.goalentity;
1452 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1454 this.wp_goal_prev1 = this.wp_goal_prev0;
1455 this.wp_goal_prev0 = e;
1459 if(e==this.navigation_jetpack_goal)
1462 // if it can reach the goal there is nothing more to do
1463 set_tracewalk_dest(e, startposition, true);
1464 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1465 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1466 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1471 entity nearest_wp = NULL;
1472 // see if there are waypoints describing a path to the item
1473 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1475 e = e.nearestwaypoint;
1478 else if(teleport_goal)
1481 e = e.enemy; // we already have added it, so...
1486 if(nearest_wp && nearest_wp.enemy)
1488 // often path can be optimized by not adding the nearest waypoint
1489 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1491 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1493 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1494 e = nearest_wp.enemy;
1497 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1498 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1499 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1500 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1501 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1503 e = nearest_wp.enemy;
1508 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1509 e = nearest_wp.enemy;
1514 // add the spawnfunc_waypoint to the path
1515 navigation_pushroute(this, e);
1525 // shorten path by removing intermediate goals
1526 bool navigation_shortenpath(entity this)
1528 if (!this.goalstack01 || wasfreed(this.goalstack01))
1530 if (this.bot_tracewalk_time > time)
1532 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1534 bool cut_allowed = false;
1535 entity next = this.goalentity;
1536 // evaluate whether bot can discard current route and chase directly a player, trying to
1537 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1538 if (IS_MOVABLE(next))
1540 set_tracewalk_dest(next, this.origin, true);
1541 if (vdist(this.origin - tracewalk_dest, <, 200))
1543 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1544 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1545 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1546 && checkpvs(this.origin + this.view_ofs, next))
1548 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1552 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1553 while (deviation.y < -180) deviation.y += 360;
1554 while (deviation.y > 180) deviation.y -= 360;
1555 if (fabs(deviation.y) > 25)
1561 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1562 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1564 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1567 navigation_poproute(this);
1569 while (this.goalcurrent != next);
1576 next = this.goalstack01;
1577 // if for some reason the bot is closer to the next goal, pop the current one
1578 if (!IS_MOVABLE(next) // already checked in the previous case
1579 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1580 && checkpvs(this.origin + this.view_ofs, next))
1582 set_tracewalk_dest(next, this.origin, true);
1588 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1589 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1591 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1592 navigation_poproute(this);
1599 // removes any currently touching waypoints from the goal stack
1600 // (this is how bots detect if they reached a goal)
1601 int navigation_poptouchedgoals(entity this)
1603 int removed_goals = 0;
1605 if(!this.goalcurrent)
1606 return removed_goals;
1608 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1610 // make sure jumppad is really hit, don't rely on distance based checks
1611 // as they may report a touch even if it didn't really happen
1612 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1614 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1615 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1617 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1618 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1620 if(this.jumppadcount)
1622 // remove jumppad waypoint after a random delay to prevent bots getting
1623 // stuck on certain jumppads that require an extra initial horizontal speed
1624 float max_delay = 0.1;
1625 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1627 if (time - this.lastteleporttime < random() * max_delay)
1628 return removed_goals;
1630 navigation_poproute(this);
1631 this.lastteleporttime = 0;
1635 return removed_goals;
1637 else if (this.lastteleporttime > 0)
1639 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1640 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1641 // if so immediately fix bot path by removing skipped goals
1642 entity tele_ent = NULL;
1643 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1644 tele_ent = this.goalstack01;
1645 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1646 tele_ent = this.goalstack02;
1647 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1648 tele_ent = this.goalstack03;
1649 if (tele_ent && TELEPORT_USED(this, tele_ent))
1651 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1652 if (tele_ent.wpflags & WAYPOINTFLAG_PERSONAL && tele_ent.owner == this)
1654 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1655 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1657 while (this.goalcurrent != tele_ent)
1659 navigation_poproute(this);
1662 navigation_poproute(this);
1663 this.lastteleporttime = 0;
1665 return removed_goals;
1669 // Loose goal touching check when running
1670 if(this.aistatus & AI_STATUS_RUNNING)
1671 if(this.goalcurrent.classname=="waypoint")
1672 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1674 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1676 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1677 if(trace_fraction==1)
1679 // Detect personal waypoints
1680 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1681 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1683 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1684 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1687 navigation_poproute(this);
1689 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1690 return removed_goals;
1695 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1697 vector gc_min = this.goalcurrent.absmin;
1698 vector gc_max = this.goalcurrent.absmax;
1699 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1701 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1702 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1704 if (time < this.ladder_time)
1706 if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1711 if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1715 // Detect personal waypoints
1716 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1717 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1719 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1720 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1723 navigation_poproute(this);
1725 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1726 return removed_goals;
1728 return removed_goals;
1731 entity navigation_get_really_close_waypoint(entity this)
1733 entity wp = this.goalcurrent;
1735 wp = this.goalcurrent_prev;
1738 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, 50))
1740 wp = this.goalcurrent_prev;
1744 if(wp.classname != "waypoint")
1746 wp = wp.nearestwaypoint;
1750 if(vdist(wp.origin - this.origin, >, 50))
1753 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1755 if(vdist(it.origin - this.origin, <, 50))
1764 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1767 set_tracewalk_dest(wp, this.origin, false);
1768 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1769 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1776 // begin a goal selection session (queries spawnfunc_waypoint network)
1777 void navigation_goalrating_start(entity this)
1779 if(this.aistatus & AI_STATUS_STUCK)
1782 this.navigation_jetpack_goal = NULL;
1783 navigation_bestrating = -1;
1784 entity wp = navigation_get_really_close_waypoint(this);
1785 navigation_clearroute(this);
1786 navigation_bestgoal = NULL;
1787 navigation_markroutes(this, wp);
1790 // ends a goal selection session (updates goal stack to the best goal)
1791 void navigation_goalrating_end(entity this)
1793 if(this.aistatus & AI_STATUS_STUCK)
1796 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1797 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1799 // If the bot got stuck then try to reach the farthest waypoint
1800 if (!this.goalentity && autocvar_bot_wander_enable)
1802 if (!(this.aistatus & AI_STATUS_STUCK))
1804 LOG_DEBUG(this.netname, " cannot walk to any goal");
1805 this.aistatus |= AI_STATUS_STUCK;
1808 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1811 void botframe_updatedangerousobjects(float maxupdate)
1813 vector m1, m2, v, o;
1817 IL_EACH(g_waypoints, true,
1823 IL_EACH(g_bot_dodge, it.bot_dodge,
1826 v.x = bound(m1_x, v.x, m2_x);
1827 v.y = bound(m1_y, v.y, m2_y);
1828 v.z = bound(m1_z, v.z, m2_z);
1829 o = (it.absmin + it.absmax) * 0.5;
1830 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1833 traceline(o, v, true, NULL);
1834 if (trace_fraction == 1)
1835 danger = danger + d;
1845 void navigation_unstuck(entity this)
1847 if (!autocvar_bot_wander_enable)
1850 bool has_user_waypoints = false;
1851 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1853 has_user_waypoints = true;
1856 if (!has_user_waypoints)
1859 float search_radius = 1000;
1861 if (!bot_waypoint_queue_owner)
1863 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1864 bot_waypoint_queue_owner = this;
1865 bot_waypoint_queue_bestgoal = NULL;
1866 bot_waypoint_queue_bestgoalrating = 0;
1869 if(bot_waypoint_queue_owner!=this)
1872 if (bot_waypoint_queue_goal)
1874 // evaluate the next goal on the queue
1875 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1876 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1877 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1878 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1879 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1881 if( d > bot_waypoint_queue_bestgoalrating)
1883 bot_waypoint_queue_bestgoalrating = d;
1884 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1888 // move to a random waypoint while bot is searching for a walkable path;
1889 // this is usually sufficient to unstuck bots from bad spots or when other
1890 // bots of the same team block all their ways
1891 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1893 navigation_clearroute(this);
1894 navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1895 navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1898 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1900 if (!bot_waypoint_queue_goal)
1902 if (bot_waypoint_queue_bestgoal)
1904 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1905 navigation_clearroute(this);
1906 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1907 navigation_goalrating_timeout_set(this);
1908 this.aistatus &= ~AI_STATUS_STUCK;
1912 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1915 bot_waypoint_queue_owner = NULL;
1920 if(bot_strategytoken!=this)
1923 // build a new queue
1924 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1926 entity first = NULL;
1928 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1930 if(bot_waypoint_queue_goal)
1931 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1935 bot_waypoint_queue_goal = it;
1936 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1940 bot_waypoint_queue_goal = first;
1943 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1944 bot_waypoint_queue_owner = NULL;
1949 // Support for debugging tracewalk visually
1951 void debugresetnodes()
1953 debuglastnode = '0 0 0';
1956 void debugnode(entity this, vector node)
1958 if (!IS_PLAYER(this))
1961 if(debuglastnode=='0 0 0')
1963 debuglastnode = node;
1967 te_lightning2(NULL, node, debuglastnode);
1968 debuglastnode = node;
1971 void debugnodestatus(vector position, float status)
1977 case DEBUG_NODE_SUCCESS:
1980 case DEBUG_NODE_WARNING:
1983 case DEBUG_NODE_FAIL:
1990 te_customflash(position, 40, 2, c);
1993 // Support for debugging the goal stack visually
1996 .vector lastposition;
1998 // Debug the goal stack visually
1999 void debuggoalstack(entity this)
2004 if(this.goalcounter==0)goal=this.goalcurrent;
2005 else if(this.goalcounter==1)goal=this.goalstack01;
2006 else if(this.goalcounter==2)goal=this.goalstack02;
2007 else if(this.goalcounter==3)goal=this.goalstack03;
2008 else if(this.goalcounter==4)goal=this.goalstack04;
2009 else if(this.goalcounter==5)goal=this.goalstack05;
2010 else if(this.goalcounter==6)goal=this.goalstack06;
2011 else if(this.goalcounter==7)goal=this.goalstack07;
2012 else if(this.goalcounter==8)goal=this.goalstack08;
2013 else if(this.goalcounter==9)goal=this.goalstack09;
2014 else if(this.goalcounter==10)goal=this.goalstack10;
2015 else if(this.goalcounter==11)goal=this.goalstack11;
2016 else if(this.goalcounter==12)goal=this.goalstack12;
2017 else if(this.goalcounter==13)goal=this.goalstack13;
2018 else if(this.goalcounter==14)goal=this.goalstack14;
2019 else if(this.goalcounter==15)goal=this.goalstack15;
2020 else if(this.goalcounter==16)goal=this.goalstack16;
2021 else if(this.goalcounter==17)goal=this.goalstack17;
2022 else if(this.goalcounter==18)goal=this.goalstack18;
2023 else if(this.goalcounter==19)goal=this.goalstack19;
2024 else if(this.goalcounter==20)goal=this.goalstack20;
2025 else if(this.goalcounter==21)goal=this.goalstack21;
2026 else if(this.goalcounter==22)goal=this.goalstack22;
2027 else if(this.goalcounter==23)goal=this.goalstack23;
2028 else if(this.goalcounter==24)goal=this.goalstack24;
2029 else if(this.goalcounter==25)goal=this.goalstack25;
2030 else if(this.goalcounter==26)goal=this.goalstack26;
2031 else if(this.goalcounter==27)goal=this.goalstack27;
2032 else if(this.goalcounter==28)goal=this.goalstack28;
2033 else if(this.goalcounter==29)goal=this.goalstack29;
2034 else if(this.goalcounter==30)goal=this.goalstack30;
2035 else if(this.goalcounter==31)goal=this.goalstack31;
2040 this.goalcounter = 0;
2041 this.lastposition='0 0 0';
2045 if(this.lastposition=='0 0 0')
2048 org = this.lastposition;
2051 go = ( goal.absmin + goal.absmax ) * 0.5;
2052 te_lightning2(NULL, org, go);
2053 this.lastposition = go;