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 void navigation_goalrating_timeout_extend_if_needed(entity this, float seconds)
52 this.bot_strategytime = max(this.bot_strategytime, time + seconds);
55 #define MAX_CHASE_DISTANCE 700
56 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
58 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
59 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
60 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
65 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
67 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
69 this.ignoregoal = this.goalentity;
70 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
77 void navigation_dynamicgoal_init(entity this, bool initially_static)
79 this.navigation_dynamicgoal = true;
80 this.bot_basewaypoint = this.nearestwaypoint;
82 this.nearestwaypointtimeout = -1;
84 this.nearestwaypointtimeout = time;
87 void navigation_dynamicgoal_set(entity this, entity dropper)
89 this.nearestwaypointtimeout = time;
90 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
91 this.nearestwaypoint = dropper.nearestwaypoint;
92 if (this.nearestwaypoint)
93 this.nearestwaypointtimeout += 2;
96 void navigation_dynamicgoal_unset(entity this)
98 if(this.bot_basewaypoint)
99 this.nearestwaypoint = this.bot_basewaypoint;
100 this.nearestwaypointtimeout = -1;
103 // returns point of ent closer to org
104 vector get_closer_dest(entity ent, vector org)
106 vector dest = '0 0 0';
107 if ((ent.classname != "waypoint") || ent.wpisbox)
109 vector wm1 = ent.origin + ent.mins;
110 vector wm2 = ent.origin + ent.maxs;
111 dest.x = bound(wm1.x, org.x, wm2.x);
112 dest.y = bound(wm1.y, org.y, wm2.y);
113 dest.z = bound(wm1.z, org.z, wm2.z);
120 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
122 if ((ent.classname != "waypoint") || ent.wpisbox)
124 vector wm1 = ent.origin + ent.mins;
125 vector wm2 = ent.origin + ent.maxs;
126 if (IS_PLAYER(ent) || IS_MONSTER(ent))
128 // move destination point out of player bbox otherwise tracebox always fails
129 // (if bot_navigation_ignoreplayers is false)
130 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
131 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
133 // set destination point to x and y coords of ent that are closer to org
134 // z coord is set to ent's min height
135 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
136 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
137 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
138 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
140 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
141 tracewalk_dest_height = 0;
142 fix_player_dest = false;
146 tracewalk_dest.z = wm1.z;
147 tracewalk_dest_height = wm2.z - wm1.z;
152 tracewalk_dest = ent.origin;
153 tracewalk_dest_height = 0;
155 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
157 // snap player to the ground
158 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
160 // bot is right under the player
161 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
162 tracewalk_dest = trace_endpos;
163 tracewalk_dest_height = 0;
167 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
168 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
170 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
171 tracewalk_dest.z = trace_endpos.z;
177 // returns point of ent closer to org
178 vector set_tracewalk_dest_2(entity ent, vector org)
180 vector closer_dest = '0 0 0';
181 if ((ent.classname != "waypoint") || ent.wpisbox)
183 vector wm1 = ent.origin + ent.mins;
184 vector wm2 = ent.origin + ent.maxs;
185 closer_dest.x = bound(wm1.x, org.x, wm2.x);
186 closer_dest.y = bound(wm1.y, org.y, wm2.y);
187 closer_dest.z = bound(wm1.z, org.z, wm2.z);
188 // set destination point to x and y coords of ent that are closer to org
189 // z coord is set to ent's min height
190 tracewalk_dest.x = closer_dest.x;
191 tracewalk_dest.y = closer_dest.y;
192 tracewalk_dest.z = wm1.z;
193 tracewalk_dest_height = wm2.z - wm1.z; // destination height
197 closer_dest = ent.origin;
198 tracewalk_dest = closer_dest;
199 tracewalk_dest_height = 0;
204 bool navigation_check_submerged_state(entity ent, vector pos)
208 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
209 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
210 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
213 submerged = SUBMERGED(pos);
214 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
215 // is actually submerged because often they are inside some solid.
216 // That's why submerged state is saved now that we know current pos is
217 // not stuck in solid (previous tracewalk call to this pos was successfully)
218 if(!ent.navigation_dynamicgoal)
219 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
224 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
226 IL_EACH(g_ladders, it.classname == "func_ladder",
229 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
230 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
233 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
234 tracebox(org, m1, m2, top, movemode, e);
235 if(trace_fraction == 1)
242 vector resurface_limited(vector org, float lim, vector m1)
244 if (WETFEET(org + eZ * (lim - org.z)))
248 float RES_min_h = org.z;
249 float RES_max_h = lim;
251 org.z = 0.5 * (RES_min_h + RES_max_h);
256 } while (RES_max_h - RES_min_h >= 1);
261 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
264 #define NAV_SWIM_ONWATER 1
265 #define NAV_SWIM_UNDERWATER 2
267 // rough simulation of walking from one point to another to test if a path
268 // can be traveled, used for waypoint linking and havocbot
269 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
270 // INFO: the command sv_cmd trace walk is useful to test this function in game
271 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
273 if(autocvar_bot_debug_tracewalk)
280 vector flatdir = end - start;
282 float flatdist = vlen(flatdir);
283 flatdir = normalize(flatdir);
285 bool ignorehazards = false;
288 // Analyze starting point
290 ignorehazards = true;
292 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
293 if (trace_startsolid)
296 if(autocvar_bot_debug_tracewalk)
297 debugnodestatus(start, DEBUG_NODE_FAIL);
299 //print("tracewalk: ", vtos(start), " is a bad start\n");
305 end2.z += end_height;
307 vector fixed_end = end;
310 if (flatdist > 0 && WETFEET(org))
313 nav_action = NAV_SWIM_UNDERWATER;
316 // tracebox down by player's height
317 // useful to know if water level is so low that bot can still walk
318 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
319 if (SUBMERGED(trace_endpos))
322 nav_action = NAV_SWIM_UNDERWATER;
325 nav_action = NAV_WALK;
329 nav_action = NAV_WALK;
337 if (org.z > end2.z + 1)
339 tracebox(org, m1, m2, end2, movemode, e);
341 if (org.z > end2.z + 1)
344 else if (org.z < end.z - 1)
346 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
347 if (SUBMERGED(trace_endpos))
349 vector v = trace_endpos;
350 tracebox(v, m1, m2, end, movemode, e);
351 if(trace_endpos.z >= end.z - 1)
353 RESURFACE_LIMITED(v, trace_endpos.z);
357 else if (trace_endpos.z > org.z - jumpheight_vec.z)
358 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
360 if (org.z < end.z - 1)
367 if(autocvar_bot_debug_tracewalk)
370 debugnodestatus(org, DEBUG_NODE_SUCCESS);
373 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
378 if(autocvar_bot_debug_tracewalk)
384 if (stepdist > flatdist)
386 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
388 // can't use movement direction here to calculate move because of
389 // precision errors especially when direction has a high enough z value
390 //water_dir = normalize(water_end - org);
391 //move = org + water_dir * stepdist;
392 fixed_end.z = bound(end.z, org.z, end2.z);
393 if (stepdist == flatdist) {
397 move = org + (fixed_end - org) * (stepdist / flatdist);
398 flatdist = vlen(vec2(fixed_end - move));
401 else // horiz. direction
403 flatdist -= stepdist;
404 move = org + flatdir * stepdist;
407 if(nav_action == NAV_SWIM_ONWATER)
409 tracebox(org, m1, m2, move, movemode, e); // swim
412 if (trace_fraction < 1)
415 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
417 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
420 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
422 if(autocvar_bot_debug_tracewalk)
425 debugnodestatus(org, DEBUG_NODE_SUCCESS);
428 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
432 if(autocvar_bot_debug_tracewalk)
433 debugnodestatus(org, DEBUG_NODE_FAIL);
436 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
447 if (org.z <= move.z) // going horiz.
449 tracebox(trace_endpos, m1, m2, move, movemode, e);
451 nav_action = NAV_WALK;
456 if (org.z <= move.z) // going horiz.
459 nav_action = NAV_SWIM_ONWATER;
465 nav_action = NAV_SWIM_UNDERWATER;
467 nav_action = NAV_SWIM_ONWATER;
470 else if(nav_action == NAV_SWIM_UNDERWATER)
472 if (move.z >= org.z) // swimming upwards or horiz.
474 tracebox(org, m1, m2, move, movemode, e); // swim
476 bool stepswum = false;
479 if (trace_fraction < 1)
482 vector stepswim_move = move + stepheightvec;
483 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
484 stepswim_move.z = end2.z;
486 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
489 if (trace_startsolid)
491 if(autocvar_bot_debug_tracewalk)
492 debugnodestatus(org, DEBUG_NODE_FAIL);
494 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
498 if (trace_fraction < 1)
500 float org_z_prev = org.z;
501 RESURFACE_LIMITED(org, end2.z);
502 if(org.z == org_z_prev)
504 if(autocvar_bot_debug_tracewalk)
505 debugnodestatus(org, DEBUG_NODE_FAIL);
507 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
511 nav_action = NAV_SWIM_UNDERWATER;
513 nav_action = NAV_SWIM_ONWATER;
515 // we didn't advance horiz. in this step, flatdist decrease should be reverted
516 // but we can't do it properly right now... apply this workaround instead
534 if (!WETFEET(trace_endpos))
536 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
537 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
538 if (!stepswum && SUBMERGED(trace_endpos))
540 RESURFACE_LIMITED(trace_endpos, end2.z);
542 nav_action = NAV_SWIM_ONWATER;
548 nav_action = NAV_WALK;
554 nav_action = NAV_SWIM_UNDERWATER;
557 else //if (move.z < org.z) // swimming downwards
559 tracebox(org, m1, m2, move, movemode, e); // swim
562 if (trace_fraction < 1)
565 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
568 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
570 if(autocvar_bot_debug_tracewalk)
571 debugnodestatus(move, DEBUG_NODE_FAIL);
573 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
585 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
587 // stepswim caused upwards direction
588 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
589 if (!SUBMERGED(trace_endpos))
592 nav_action = NAV_WALK;
599 nav_action = NAV_SWIM_UNDERWATER;
603 else if(nav_action == NAV_WALK)
606 tracebox(org, m1, m2, move, movemode, e);
608 if(autocvar_bot_debug_tracewalk)
609 debugnode(e, trace_endpos);
612 if (trace_fraction < 1)
614 // check if we can walk over this obstacle, possibly by jumpstepping
615 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
616 if (trace_fraction < 1 || trace_startsolid)
618 if (trace_startsolid) // hit ceiling above org
620 // reduce stepwalk height
621 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
622 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
624 else //if (trace_fraction < 1)
626 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
627 if (trace_startsolid) // hit ceiling above org
629 // reduce jumpstepwalk height
630 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
631 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
635 if (trace_fraction < 1)
637 vector v = trace_endpos;
638 v.z = org.z + jumpheight_vec.z;
639 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
641 if(autocvar_bot_debug_tracewalk)
644 debugnodestatus(v, DEBUG_NODE_SUCCESS);
647 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
651 if(autocvar_bot_debug_tracewalk)
652 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
654 traceline( org, move, movemode, e);
656 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
660 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
662 nextmove = move + (flatdir * stepdist);
663 traceline( move, nextmove, movemode, e);
666 flatdist = vlen(vec2(end - move));
670 if(autocvar_bot_debug_tracewalk)
671 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
673 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
674 //te_explosion(trace_endpos);
675 //print(ftos(e.dphitcontentsmask), "\n");
676 return false; // failed
688 // trace down from stepheight as far as possible and move there,
689 // if this starts in solid we try again without the stepup, and
690 // if that also fails we assume it is a wall
691 // (this is the same logic as the Quake walkmove function used)
692 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
700 if(autocvar_bot_debug_tracewalk)
702 debugnode(e, trace_endpos);
703 debugnodestatus(org, DEBUG_NODE_FAIL);
706 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
713 if(move.z >= end2.z && org.z < end2.z)
718 if(org.z > move.z - 1 || !SUBMERGED(org))
720 nav_action = NAV_WALK;
724 // ended up submerged while walking
725 if(autocvar_bot_debug_tracewalk)
728 RESURFACE_LIMITED(org, move.z);
729 nav_action = NAV_SWIM_ONWATER;
734 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
736 // moved but didn't arrive at the intended destination
737 if(autocvar_bot_debug_tracewalk)
738 debugnodestatus(org, DEBUG_NODE_FAIL);
743 /////////////////////////////////////////////////////////////////////////////
745 /////////////////////////////////////////////////////////////////////////////
747 // completely empty the goal stack, used when deciding where to go
748 void navigation_clearroute(entity this)
750 this.lastteleporttime = 0;
751 this.goalcurrent_prev = this.goalcurrent;
752 this.goalcurrent_distance_2d = FLOAT_MAX;
753 this.goalcurrent_distance_z = FLOAT_MAX;
754 this.goalcurrent_distance_time = 0;
755 this.goalentity_lock_timeout = 0;
756 this.goalentity_shouldbefrozen = false;
757 this.goalentity = NULL;
758 this.goalcurrent = NULL;
759 this.goalstack01 = NULL;
760 this.goalstack02 = NULL;
761 this.goalstack03 = NULL;
762 this.goalstack04 = NULL;
763 this.goalstack05 = NULL;
764 this.goalstack06 = NULL;
765 this.goalstack07 = NULL;
766 this.goalstack08 = NULL;
767 this.goalstack09 = NULL;
768 this.goalstack10 = NULL;
769 this.goalstack11 = NULL;
770 this.goalstack12 = NULL;
771 this.goalstack13 = NULL;
772 this.goalstack14 = NULL;
773 this.goalstack15 = NULL;
774 this.goalstack16 = NULL;
775 this.goalstack17 = NULL;
776 this.goalstack18 = NULL;
777 this.goalstack19 = NULL;
778 this.goalstack20 = NULL;
779 this.goalstack21 = NULL;
780 this.goalstack22 = NULL;
781 this.goalstack23 = NULL;
782 this.goalstack24 = NULL;
783 this.goalstack25 = NULL;
784 this.goalstack26 = NULL;
785 this.goalstack27 = NULL;
786 this.goalstack28 = NULL;
787 this.goalstack29 = NULL;
788 this.goalstack30 = NULL;
789 this.goalstack31 = NULL;
792 // add a new goal at the beginning of the stack
793 // (in other words: add a new prerequisite before going to the later goals)
794 // NOTE: when a waypoint is added, the WP gets pushed first, then the
795 // next-closest WP on the shortest path to the WP
796 // That means, if the stack overflows, the bot will know how to do the FIRST 32
797 // steps to the goal, and then recalculate the path.
798 void navigation_pushroute(entity this, entity e)
800 this.goalcurrent_prev = this.goalcurrent;
801 this.goalcurrent_distance_2d = FLOAT_MAX;
802 this.goalcurrent_distance_z = FLOAT_MAX;
803 this.goalcurrent_distance_time = 0;
804 //print("bot ", etos(this), " push ", etos(e), "\n");
805 if(this.goalstack31 == this.goalentity)
806 this.goalentity = NULL;
807 this.goalstack31 = this.goalstack30;
808 this.goalstack30 = this.goalstack29;
809 this.goalstack29 = this.goalstack28;
810 this.goalstack28 = this.goalstack27;
811 this.goalstack27 = this.goalstack26;
812 this.goalstack26 = this.goalstack25;
813 this.goalstack25 = this.goalstack24;
814 this.goalstack24 = this.goalstack23;
815 this.goalstack23 = this.goalstack22;
816 this.goalstack22 = this.goalstack21;
817 this.goalstack21 = this.goalstack20;
818 this.goalstack20 = this.goalstack19;
819 this.goalstack19 = this.goalstack18;
820 this.goalstack18 = this.goalstack17;
821 this.goalstack17 = this.goalstack16;
822 this.goalstack16 = this.goalstack15;
823 this.goalstack15 = this.goalstack14;
824 this.goalstack14 = this.goalstack13;
825 this.goalstack13 = this.goalstack12;
826 this.goalstack12 = this.goalstack11;
827 this.goalstack11 = this.goalstack10;
828 this.goalstack10 = this.goalstack09;
829 this.goalstack09 = this.goalstack08;
830 this.goalstack08 = this.goalstack07;
831 this.goalstack07 = this.goalstack06;
832 this.goalstack06 = this.goalstack05;
833 this.goalstack05 = this.goalstack04;
834 this.goalstack04 = this.goalstack03;
835 this.goalstack03 = this.goalstack02;
836 this.goalstack02 = this.goalstack01;
837 this.goalstack01 = this.goalcurrent;
838 this.goalcurrent = e;
841 // remove first goal from stack
842 // (in other words: remove a prerequisite for reaching the later goals)
843 // (used when a spawnfunc_waypoint is reached)
844 void navigation_poproute(entity this)
846 this.goalcurrent_prev = this.goalcurrent;
847 this.goalcurrent_distance_2d = FLOAT_MAX;
848 this.goalcurrent_distance_z = FLOAT_MAX;
849 this.goalcurrent_distance_time = 0;
850 //print("bot ", etos(this), " pop\n");
851 if(this.goalcurrent == this.goalentity)
853 this.goalentity = NULL;
854 this.goalentity_lock_timeout = 0;
856 this.goalcurrent = this.goalstack01;
857 this.goalstack01 = this.goalstack02;
858 this.goalstack02 = this.goalstack03;
859 this.goalstack03 = this.goalstack04;
860 this.goalstack04 = this.goalstack05;
861 this.goalstack05 = this.goalstack06;
862 this.goalstack06 = this.goalstack07;
863 this.goalstack07 = this.goalstack08;
864 this.goalstack08 = this.goalstack09;
865 this.goalstack09 = this.goalstack10;
866 this.goalstack10 = this.goalstack11;
867 this.goalstack11 = this.goalstack12;
868 this.goalstack12 = this.goalstack13;
869 this.goalstack13 = this.goalstack14;
870 this.goalstack14 = this.goalstack15;
871 this.goalstack15 = this.goalstack16;
872 this.goalstack16 = this.goalstack17;
873 this.goalstack17 = this.goalstack18;
874 this.goalstack18 = this.goalstack19;
875 this.goalstack19 = this.goalstack20;
876 this.goalstack20 = this.goalstack21;
877 this.goalstack21 = this.goalstack22;
878 this.goalstack22 = this.goalstack23;
879 this.goalstack23 = this.goalstack24;
880 this.goalstack24 = this.goalstack25;
881 this.goalstack25 = this.goalstack26;
882 this.goalstack26 = this.goalstack27;
883 this.goalstack27 = this.goalstack28;
884 this.goalstack28 = this.goalstack29;
885 this.goalstack29 = this.goalstack30;
886 this.goalstack30 = this.goalstack31;
887 this.goalstack31 = NULL;
890 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
891 // waypoint destination coordinates instead of v (only useful for box waypoints)
892 // for normal waypoints v2 == v and v2_height == 0
893 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)
895 if (vdist(v - org, <, bestdist))
897 traceline(v, org, true, ent);
898 if (trace_fraction == 1)
902 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
907 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
915 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
916 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
919 ent = ent.tag_entity;
921 vector pm1 = ent.origin + ent.mins;
922 vector pm2 = ent.origin + ent.maxs;
924 // do two scans, because box test is cheaper
925 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
927 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
929 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
931 waypoint_clearlinks(ent); // initialize wpXXmincost fields
932 navigation_item_addlink(it, ent);
938 vector org = ent.origin;
939 if (navigation_testtracewalk)
945 if(ent.size && !IS_PLAYER(ent))
947 org += 0.5 * (ent.mins + ent.maxs);
948 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
951 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
953 waypoint_clearlinks(ent); // initialize wpXXmincost fields
954 IL_EACH(g_waypoints, it != ent,
956 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
959 set_tracewalk_dest(ent, it.origin, false);
960 if (vdist(tracewalk_dest - it.origin, <, 1050)
961 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
962 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
964 navigation_item_addlink(it, ent);
969 // box check failed, try walk
970 IL_EACH(g_waypoints, it != ent,
972 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
978 set_tracewalk_dest(ent, v, true);
979 if (trace_ent == ent)
987 set_tracewalk_dest(it, org, false);
989 if (navigation_waypoint_will_link(v, org, ent,
990 tracewalk_dest, tracewalk_dest_height,
991 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
994 bestdist = vlen(tracewalk_dest - v);
996 bestdist = vlen(v - org);
1000 if(!best && !ent.navigation_dynamicgoal)
1002 int solid_save = ent.solid;
1003 ent.solid = SOLID_BSP;
1004 IL_EACH(g_jumppads, true,
1006 if(trigger_push_test(it, ent))
1008 best = it.nearestwaypoint;
1012 ent.solid = solid_save;
1016 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1018 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1019 if (autocvar_g_waypointeditor_auto)
1021 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1023 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1028 // finds the waypoints near the bot initiating a navigation query
1029 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1031 //navigation_testtracewalk = true;
1033 IL_EACH(g_waypoints, !it.wpconsidered,
1035 set_tracewalk_dest(it, this.origin, false);
1037 vector diff = tracewalk_dest - this.origin;
1038 diff.z = max(0, diff.z);
1039 if(vdist(diff, <, maxdist))
1041 it.wpconsidered = true;
1042 if (tracewalk(this, this.origin, this.mins, this.maxs,
1043 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1045 it.wpnearestpoint = tracewalk_dest;
1046 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1053 //navigation_testtracewalk = false;
1057 // updates a path link if a spawnfunc_waypoint link is better than the current one
1058 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1064 m1 = wp.origin + wp.mins;
1065 m2 = wp.origin + wp.maxs;
1066 v.x = bound(m1_x, p.x, m2_x);
1067 v.y = bound(m1_y, p.y, m2_y);
1068 v.z = bound(m1_z, p.z, m2_z);
1072 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1073 cost += w.wp00mincost; // assuming teleport has exactly one destination
1075 cost += waypoint_gettravelcost(p, v, w, wp);
1076 if (wp.wpcost > cost)
1081 wp.wpnearestpoint = v;
1085 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1086 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1091 IL_EACH(g_waypoints, true,
1093 it.wpconsidered = false;
1094 it.wpnearestpoint = '0 0 0';
1095 it.wpcost = 10000000;
1100 if(fixed_source_waypoint)
1102 fixed_source_waypoint.wpconsidered = true;
1103 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1104 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1105 fixed_source_waypoint.wpfire = 1;
1106 fixed_source_waypoint.enemy = NULL;
1110 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1111 // as this search is expensive we will use lower values if the bot is on the air
1112 float increment, maxdistance;
1113 if(IS_ONGROUND(this))
1116 maxdistance = 50000;
1124 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1127 bool searching = true;
1131 IL_EACH(g_waypoints, it.wpfire,
1136 p = it.wpnearestpoint;
1138 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1166 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1167 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1168 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1169 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1170 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1175 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1176 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1180 IL_EACH(g_waypoints, true,
1182 it.wpconsidered = false;
1183 it.wpnearestpoint = '0 0 0';
1184 it.wpcost = 10000000;
1189 if(fixed_source_waypoint)
1191 fixed_source_waypoint.wpconsidered = true;
1192 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1193 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1194 fixed_source_waypoint.wpfire = 1;
1195 fixed_source_waypoint.enemy = NULL;
1199 error("need to start with a waypoint\n");
1202 bool searching = true;
1206 IL_EACH(g_waypoints, it.wpfire,
1210 cost = it.wpcost; // cost to walk from it to home
1211 p = it.wpnearestpoint;
1213 IL_EACH(g_waypoints, it != wp,
1215 if(!waypoint_islinked(it, wp))
1217 cost2 = cost + it.dmg;
1218 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1224 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1225 void navigation_routerating(entity this, entity e, float f, float rangebias)
1227 if (!e || e.blacklisted) { return; }
1229 rangebias = waypoint_getlinearcost(rangebias);
1230 f = waypoint_getlinearcost(f);
1234 bool rate_wps = false;
1235 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1236 || (e.flags & FL_PARTIALGROUND))
1243 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1244 int t = pointcontents(trace_endpos + '0 0 1');
1245 if(t != CONTENT_SOLID )
1247 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1249 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1256 entity theEnemy = e;
1257 entity best_wp = NULL;
1258 float best_dist = FLOAT_MAX;
1259 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1260 && vdist(it.origin - theEnemy.origin, <, 500)
1261 && vdist(it.origin - this.origin, >, 100)
1262 && vdist(it.origin - this.origin, <, 10000),
1264 float dist = vlen2(it.origin - theEnemy.origin);
1265 if (dist < best_dist)
1277 vector goal_org = (e.absmin + e.absmax) * 0.5;
1279 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1281 // Evaluate path using jetpack
1282 if(this.items & IT_JETPACK)
1283 if(autocvar_bot_ai_navigation_jetpack)
1284 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1286 vector pointa, pointb;
1288 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1291 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1292 pointa = trace_endpos - '0 0 1';
1295 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1296 pointb = trace_endpos - '0 0 1';
1298 // Can I see these two points from the sky?
1299 traceline(pointa, pointb, MOVE_NORMAL, this);
1301 if(trace_fraction==1)
1303 LOG_DEBUG("jetpack ai: can bridge these two points");
1305 // Lower the altitude of these points as much as possible
1306 float zdistance, xydistance, cost, t, fuel;
1307 vector down, npa, npb;
1309 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1312 npa = pointa + down;
1313 npb = pointb + down;
1315 if(npa.z<=this.absmax.z)
1318 if(npb.z<=e.absmax.z)
1321 traceline(npa, npb, MOVE_NORMAL, this);
1322 if(trace_fraction==1)
1328 while(trace_fraction == 1);
1331 // Rough estimation of fuel consumption
1332 // (ignores acceleration and current xyz velocity)
1333 xydistance = vlen(pointa - pointb);
1334 zdistance = fabs(pointa.z - this.origin.z);
1336 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1337 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1338 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1340 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResource(this, RES_FUEL)));
1343 if(GetResource(this, RES_FUEL) > fuel || (this.items & IT_UNLIMITED_AMMO))
1346 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1347 // - between air and ground speeds)
1349 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1350 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1353 // Compare against other goals
1354 f = f * rangebias / (rangebias + cost);
1356 if (navigation_bestrating < f)
1358 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1359 navigation_bestrating = f;
1360 navigation_bestgoal = e;
1361 this.navigation_jetpack_goal = e;
1362 this.navigation_jetpack_point = pointb;
1370 //te_wizspike(e.origin);
1373 // update the cached spawnfunc_waypoint link on a dynamic item entity
1374 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1380 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1381 e.nearestwaypoint = NULL;
1383 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1384 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1386 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1387 e.nearestwaypoint = nwp = e.goalcurrent;
1389 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1392 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1394 if(!e.navigation_dynamicgoal)
1395 e.blacklisted = true;
1399 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1404 if(e.navigation_dynamicgoal)
1405 e.nearestwaypointtimeout = time + 2;
1406 else if(autocvar_g_waypointeditor)
1407 e.nearestwaypointtimeout = time + 3 + random() * 2;
1409 nwp = e.nearestwaypoint;
1412 if (nwp && nwp.wpcost < 10000000)
1414 //te_wizspike(nwp.wpnearestpoint);
1415 float nwptoitem_cost = 0;
1416 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1417 nwptoitem_cost = nwp.wp00mincost;
1419 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1420 float cost = nwp.wpcost + nwptoitem_cost;
1421 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1422 f = f * rangebias / (rangebias + cost);
1423 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1424 if (navigation_bestrating < f)
1426 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1427 navigation_bestrating = f;
1428 navigation_bestgoal = e;
1433 // adds an item to the the goal stack with the path to a given item
1434 bool navigation_routetogoal(entity this, entity e, vector startposition)
1436 // if there is no goal, just exit
1440 entity teleport_goal = NULL;
1442 this.goalentity = e;
1444 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1446 // force teleport destination as route destination
1448 navigation_pushroute(this, e.wp00);
1449 this.goalentity = e.wp00;
1452 // put the entity on the goal stack
1453 //print("routetogoal ", etos(e), "\n");
1454 navigation_pushroute(this, e);
1457 e = this.goalentity;
1459 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1461 this.wp_goal_prev1 = this.wp_goal_prev0;
1462 this.wp_goal_prev0 = e;
1466 if(e==this.navigation_jetpack_goal)
1469 // if it can reach the goal there is nothing more to do
1470 set_tracewalk_dest(e, startposition, true);
1471 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1472 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1473 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1478 entity nearest_wp = NULL;
1479 // see if there are waypoints describing a path to the item
1480 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1482 e = e.nearestwaypoint;
1485 else if(teleport_goal)
1488 e = e.enemy; // we already have added it, so...
1493 if(nearest_wp && nearest_wp.enemy)
1495 // often path can be optimized by not adding the nearest waypoint
1496 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1498 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1500 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1501 e = nearest_wp.enemy;
1504 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1505 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1506 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1507 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1508 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1510 e = nearest_wp.enemy;
1515 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1516 e = nearest_wp.enemy;
1521 // add the spawnfunc_waypoint to the path
1522 navigation_pushroute(this, e);
1532 // shorten path by removing intermediate goals
1533 bool navigation_shortenpath(entity this)
1535 if (!this.goalstack01 || wasfreed(this.goalstack01))
1537 if (this.bot_tracewalk_time > time)
1539 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1541 bool cut_allowed = false;
1542 entity next = this.goalentity;
1543 // evaluate whether bot can discard current route and chase directly a player, trying to
1544 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1545 if (IS_MOVABLE(next))
1547 set_tracewalk_dest(next, this.origin, true);
1548 if (vdist(this.origin - tracewalk_dest, <, 200))
1550 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1551 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1552 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1553 && checkpvs(this.origin + this.view_ofs, next))
1555 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1559 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1560 while (deviation.y < -180) deviation.y += 360;
1561 while (deviation.y > 180) deviation.y -= 360;
1562 if (fabs(deviation.y) > 25)
1568 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1569 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1571 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1574 navigation_poproute(this);
1576 while (this.goalcurrent != next);
1583 next = this.goalstack01;
1584 // if for some reason the bot is closer to the next goal, pop the current one
1585 if (!IS_MOVABLE(next) && !(this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP))
1586 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1587 && checkpvs(this.origin + this.view_ofs, next))
1589 set_tracewalk_dest(next, this.origin, true);
1595 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1596 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1598 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1599 navigation_poproute(this);
1606 // removes any currently touching waypoints from the goal stack
1607 // (this is how bots detect if they reached a goal)
1608 int navigation_poptouchedgoals(entity this)
1610 int removed_goals = 0;
1612 if(!this.goalcurrent)
1613 return removed_goals;
1615 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1617 if (!this.goalcurrent.wpisbox // warpzone
1618 && vlen2(this.origin - this.goalstack01.origin) < vlen2(this.origin - this.goalcurrent.origin))
1620 // immediately remove origin and destination waypoints
1621 navigation_poproute(this);
1623 navigation_poproute(this);
1625 this.lastteleporttime = 0;
1628 // make sure jumppad is really hit, don't rely on distance based checks
1629 // as they may report a touch even if it didn't really happen
1630 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1632 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1633 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1635 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1636 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1638 if(this.jumppadcount)
1640 // remove jumppad waypoint after a random delay to prevent bots getting
1641 // stuck on certain jumppads that require an extra initial horizontal speed
1642 float max_delay = 0.1;
1643 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1645 if (time - this.lastteleporttime < random() * max_delay)
1646 return removed_goals;
1648 else if (this.goalcurrent.wpisbox) // teleport
1650 // immediately remove origin and destination waypoints
1651 navigation_poproute(this);
1654 navigation_poproute(this);
1655 this.lastteleporttime = 0;
1658 return removed_goals;
1660 else if (this.lastteleporttime > 0)
1662 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1663 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1664 // if so immediately fix bot path by removing skipped goals
1665 entity tele_ent = NULL;
1666 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1667 tele_ent = this.goalstack01;
1668 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1669 tele_ent = this.goalstack02;
1670 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1671 tele_ent = this.goalstack03;
1672 if (tele_ent && TELEPORT_USED(this, tele_ent))
1674 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1675 if (tele_ent.wpflags & WAYPOINTFLAG_PERSONAL && tele_ent.owner == this)
1677 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1678 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1680 while (this.goalcurrent != tele_ent)
1682 navigation_poproute(this);
1685 navigation_poproute(this);
1686 this.lastteleporttime = 0;
1688 return removed_goals;
1690 // reset of lastteleporttime can be overriden by a jumppad when it's set
1691 // in more than one frame: make sure it's reset
1692 this.lastteleporttime = 0;
1695 // Loose goal touching check when running
1696 if(this.aistatus & AI_STATUS_RUNNING)
1697 if(this.goalcurrent.classname=="waypoint")
1698 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1700 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1702 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1703 if(trace_fraction==1)
1705 // Detect personal waypoints
1706 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1707 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1709 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1710 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1713 navigation_poproute(this);
1715 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1716 return removed_goals;
1721 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1723 vector gc_min = this.goalcurrent.absmin;
1724 vector gc_max = this.goalcurrent.absmax;
1725 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1727 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1728 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1730 if (time < this.ladder_time)
1732 if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1737 if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1741 // Detect personal waypoints
1742 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1743 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1745 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1746 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1749 navigation_poproute(this);
1751 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1752 return removed_goals;
1754 return removed_goals;
1757 entity navigation_get_really_close_waypoint(entity this)
1759 entity wp = this.goalcurrent;
1761 wp = this.goalcurrent_prev;
1764 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, 50))
1766 wp = this.goalcurrent_prev;
1770 if(wp.classname != "waypoint")
1772 wp = wp.nearestwaypoint;
1776 if(vdist(wp.origin - this.origin, >, 50))
1779 IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
1781 if(vdist(it.origin - this.origin, <, 50))
1790 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1793 set_tracewalk_dest(wp, this.origin, false);
1794 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1795 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1802 // begin a goal selection session (queries spawnfunc_waypoint network)
1803 void navigation_goalrating_start(entity this)
1805 if(this.aistatus & AI_STATUS_STUCK)
1808 this.navigation_jetpack_goal = NULL;
1809 navigation_bestrating = -1;
1810 entity wp = navigation_get_really_close_waypoint(this);
1811 navigation_clearroute(this);
1812 navigation_bestgoal = NULL;
1813 navigation_markroutes(this, wp);
1814 this.goalstack31 = wp; // temporarly save the really close waypoint
1817 // ends a goal selection session (updates goal stack to the best goal)
1818 void navigation_goalrating_end(entity this)
1820 if(this.aistatus & AI_STATUS_STUCK)
1823 entity wp = this.goalstack31; // save to wp as this.goalstack31 is set by navigation_routetogoal
1824 this.goalstack31 = NULL;
1826 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1827 LOG_DEBUG("best goal ", navigation_bestgoal.classname);
1829 if (wp && this.goalcurrent == wp)
1830 navigation_poproute(this);
1832 // If the bot got stuck then try to reach the farthest waypoint
1833 if (!this.goalentity)
1835 if (autocvar_bot_wander_enable && !(this.aistatus & AI_STATUS_STUCK))
1837 LOG_DEBUG(this.netname, " cannot walk to any goal");
1838 this.aistatus |= AI_STATUS_STUCK;
1840 this.goalentity_shouldbefrozen = false;
1843 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1846 void botframe_updatedangerousobjects(float maxupdate)
1848 vector m1, m2, v, o;
1852 IL_EACH(g_waypoints, true,
1858 IL_EACH(g_bot_dodge, it.bot_dodge,
1861 v.x = bound(m1_x, v.x, m2_x);
1862 v.y = bound(m1_y, v.y, m2_y);
1863 v.z = bound(m1_z, v.z, m2_z);
1864 o = (it.absmin + it.absmax) * 0.5;
1865 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1868 traceline(o, v, true, NULL);
1869 if (trace_fraction == 1)
1870 danger = danger + d;
1880 void navigation_unstuck(entity this)
1882 if (!autocvar_bot_wander_enable)
1885 bool has_user_waypoints = false;
1886 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1888 has_user_waypoints = true;
1891 if (!has_user_waypoints)
1894 float search_radius = 1000;
1896 if (!bot_waypoint_queue_owner)
1898 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1899 bot_waypoint_queue_owner = this;
1900 bot_waypoint_queue_bestgoal = NULL;
1901 bot_waypoint_queue_bestgoalrating = 0;
1904 if(bot_waypoint_queue_owner!=this)
1907 if (bot_waypoint_queue_goal)
1909 // evaluate the next goal on the queue
1910 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1911 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with squared distance ", ftos(d));
1912 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1913 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1914 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1916 if( d > bot_waypoint_queue_bestgoalrating)
1918 bot_waypoint_queue_bestgoalrating = d;
1919 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1923 // move to a random waypoint while bot is searching for a walkable path;
1924 // this is usually sufficient to unstuck bots from bad spots or when other
1925 // bots of the same team block all their ways
1926 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1928 navigation_clearroute(this);
1929 navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1930 navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1933 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1935 if (!bot_waypoint_queue_goal)
1937 if (bot_waypoint_queue_bestgoal)
1939 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1940 navigation_clearroute(this);
1941 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1942 navigation_goalrating_timeout_set(this);
1943 this.aistatus &= ~AI_STATUS_STUCK;
1947 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1950 bot_waypoint_queue_owner = NULL;
1955 if(bot_strategytoken!=this)
1958 // build a new queue
1959 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1961 entity first = NULL;
1963 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1965 if(bot_waypoint_queue_goal)
1966 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1970 bot_waypoint_queue_goal = it;
1971 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1975 bot_waypoint_queue_goal = first;
1978 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1979 bot_waypoint_queue_owner = NULL;
1984 // Support for debugging tracewalk visually
1986 void debugresetnodes()
1988 debuglastnode = '0 0 0';
1991 void debugnode(entity this, vector node)
1993 if (!IS_PLAYER(this))
1996 if(debuglastnode=='0 0 0')
1998 debuglastnode = node;
2002 te_lightning2(NULL, node, debuglastnode);
2003 debuglastnode = node;
2006 void debugnodestatus(vector position, float status)
2012 case DEBUG_NODE_SUCCESS:
2015 case DEBUG_NODE_WARNING:
2018 case DEBUG_NODE_FAIL:
2025 te_customflash(position, 40, 2, c);
2028 // Support for debugging the goal stack visually
2031 .vector lastposition;
2033 // Debug the goal stack visually
2034 void debuggoalstack(entity this)
2039 if(this.goalcounter==0)goal=this.goalcurrent;
2040 else if(this.goalcounter==1)goal=this.goalstack01;
2041 else if(this.goalcounter==2)goal=this.goalstack02;
2042 else if(this.goalcounter==3)goal=this.goalstack03;
2043 else if(this.goalcounter==4)goal=this.goalstack04;
2044 else if(this.goalcounter==5)goal=this.goalstack05;
2045 else if(this.goalcounter==6)goal=this.goalstack06;
2046 else if(this.goalcounter==7)goal=this.goalstack07;
2047 else if(this.goalcounter==8)goal=this.goalstack08;
2048 else if(this.goalcounter==9)goal=this.goalstack09;
2049 else if(this.goalcounter==10)goal=this.goalstack10;
2050 else if(this.goalcounter==11)goal=this.goalstack11;
2051 else if(this.goalcounter==12)goal=this.goalstack12;
2052 else if(this.goalcounter==13)goal=this.goalstack13;
2053 else if(this.goalcounter==14)goal=this.goalstack14;
2054 else if(this.goalcounter==15)goal=this.goalstack15;
2055 else if(this.goalcounter==16)goal=this.goalstack16;
2056 else if(this.goalcounter==17)goal=this.goalstack17;
2057 else if(this.goalcounter==18)goal=this.goalstack18;
2058 else if(this.goalcounter==19)goal=this.goalstack19;
2059 else if(this.goalcounter==20)goal=this.goalstack20;
2060 else if(this.goalcounter==21)goal=this.goalstack21;
2061 else if(this.goalcounter==22)goal=this.goalstack22;
2062 else if(this.goalcounter==23)goal=this.goalstack23;
2063 else if(this.goalcounter==24)goal=this.goalstack24;
2064 else if(this.goalcounter==25)goal=this.goalstack25;
2065 else if(this.goalcounter==26)goal=this.goalstack26;
2066 else if(this.goalcounter==27)goal=this.goalstack27;
2067 else if(this.goalcounter==28)goal=this.goalstack28;
2068 else if(this.goalcounter==29)goal=this.goalstack29;
2069 else if(this.goalcounter==30)goal=this.goalstack30;
2070 else if(this.goalcounter==31)goal=this.goalstack31;
2075 this.goalcounter = 0;
2076 this.lastposition='0 0 0';
2080 if(this.lastposition=='0 0 0')
2083 org = this.lastposition;
2086 go = ( goal.absmin + goal.absmax ) * 0.5;
2087 te_lightning2(NULL, org, go);
2088 this.lastposition = go;