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)
83 this.nearestwaypointtimeout = time;
84 if (this.nearestwaypoint)
85 this.nearestwaypointtimeout += 2;
88 void navigation_dynamicgoal_unset(entity this)
90 if(this.bot_basewaypoint)
91 this.nearestwaypoint = this.bot_basewaypoint;
92 this.nearestwaypointtimeout = -1;
95 // returns point of ent closer to org
96 vector get_closer_dest(entity ent, vector org)
98 vector dest = '0 0 0';
99 if ((ent.classname != "waypoint") || ent.wpisbox)
101 vector wm1 = ent.origin + ent.mins;
102 vector wm2 = ent.origin + ent.maxs;
103 dest.x = bound(wm1.x, org.x, wm2.x);
104 dest.y = bound(wm1.y, org.y, wm2.y);
105 dest.z = bound(wm1.z, org.z, wm2.z);
112 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
114 if ((ent.classname != "waypoint") || ent.wpisbox)
116 vector wm1 = ent.origin + ent.mins;
117 vector wm2 = ent.origin + ent.maxs;
118 if (IS_PLAYER(ent) || IS_MONSTER(ent))
120 // move destination point out of player bbox otherwise tracebox always fails
121 // (if bot_navigation_ignoreplayers is false)
122 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
123 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
125 // set destination point to x and y coords of ent that are closer to org
126 // z coord is set to ent's min height
127 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
128 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
129 tracewalk_dest.z = wm1.z;
130 tracewalk_dest_height = wm2.z - wm1.z; // destination height
134 tracewalk_dest = ent.origin;
135 tracewalk_dest_height = 0;
137 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
139 // snap player to the ground
140 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
142 // bot is right under the player
143 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
144 tracewalk_dest = trace_endpos;
145 tracewalk_dest_height = 0;
149 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
150 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
152 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
153 tracewalk_dest.z = trace_endpos.z;
159 // returns point of ent closer to org
160 vector set_tracewalk_dest_2(entity ent, vector org)
162 vector closer_dest = '0 0 0';
163 if ((ent.classname != "waypoint") || ent.wpisbox)
165 vector wm1 = ent.origin + ent.mins;
166 vector wm2 = ent.origin + ent.maxs;
167 closer_dest.x = bound(wm1.x, org.x, wm2.x);
168 closer_dest.y = bound(wm1.y, org.y, wm2.y);
169 closer_dest.z = bound(wm1.z, org.z, wm2.z);
170 // set destination point to x and y coords of ent that are closer to org
171 // z coord is set to ent's min height
172 tracewalk_dest.x = closer_dest.x;
173 tracewalk_dest.y = closer_dest.y;
174 tracewalk_dest.z = wm1.z;
175 tracewalk_dest_height = wm2.z - wm1.z; // destination height
179 closer_dest = ent.origin;
180 tracewalk_dest = closer_dest;
181 tracewalk_dest_height = 0;
186 bool navigation_check_submerged_state(entity ent, vector pos)
190 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
191 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
192 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
195 submerged = SUBMERGED(pos);
196 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
197 // is actually submerged because often they are inside some solid.
198 // That's why submerged state is saved now that we know current pos is
199 // not stuck in solid (previous tracewalk call to this pos was successfully)
200 if(!ent.navigation_dynamicgoal)
201 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
206 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
208 IL_EACH(g_ladders, it.classname == "func_ladder",
211 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
212 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
215 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
216 tracebox(org, m1, m2, top, movemode, e);
217 if(trace_fraction == 1)
224 vector resurface_limited(vector org, float lim, vector m1)
226 if (WETFEET(org + eZ * (lim - org.z)))
230 float RES_min_h = org.z;
231 float RES_max_h = lim;
233 org.z = 0.5 * (RES_min_h + RES_max_h);
238 } while (RES_max_h - RES_min_h >= 1);
243 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
246 #define NAV_SWIM_ONWATER 1
247 #define NAV_SWIM_UNDERWATER 2
249 // rough simulation of walking from one point to another to test if a path
250 // can be traveled, used for waypoint linking and havocbot
251 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
252 // INFO: the command sv_cmd trace walk is useful to test this function in game
253 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
255 if(autocvar_bot_debug_tracewalk)
262 vector flatdir = end - start;
264 float flatdist = vlen(flatdir);
265 flatdir = normalize(flatdir);
267 bool ignorehazards = false;
270 // Analyze starting point
272 ignorehazards = true;
274 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
275 if (trace_startsolid)
278 if(autocvar_bot_debug_tracewalk)
279 debugnodestatus(start, DEBUG_NODE_FAIL);
281 //print("tracewalk: ", vtos(start), " is a bad start\n");
287 end2.z += end_height;
289 vector fixed_end = end;
292 if (flatdist > 0 && WETFEET(org))
295 nav_action = NAV_SWIM_UNDERWATER;
298 // tracebox down by player's height
299 // useful to know if water level is so low that bot can still walk
300 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
301 if (SUBMERGED(trace_endpos))
304 nav_action = NAV_SWIM_UNDERWATER;
307 nav_action = NAV_WALK;
311 nav_action = NAV_WALK;
319 if (org.z > end2.z + 1)
321 tracebox(org, m1, m2, end2, movemode, e);
323 if (org.z > end2.z + 1)
326 else if (org.z < end.z - 1)
328 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
329 if (SUBMERGED(trace_endpos))
331 vector v = trace_endpos;
332 tracebox(v, m1, m2, end, movemode, e);
333 if(trace_endpos.z >= end.z - 1)
335 RESURFACE_LIMITED(v, trace_endpos.z);
339 else if (trace_endpos.z > org.z - jumpheight_vec.z)
340 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
342 if (org.z < end.z - 1)
349 if(autocvar_bot_debug_tracewalk)
352 debugnodestatus(org, DEBUG_NODE_SUCCESS);
355 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
360 if(autocvar_bot_debug_tracewalk)
366 if (stepdist > flatdist)
368 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
370 // can't use movement direction here to calculate move because of
371 // precision errors especially when direction has a high enough z value
372 //water_dir = normalize(water_end - org);
373 //move = org + water_dir * stepdist;
374 fixed_end.z = bound(end.z, org.z, end2.z);
375 if (stepdist == flatdist) {
379 move = org + (fixed_end - org) * (stepdist / flatdist);
380 flatdist = vlen(vec2(fixed_end - move));
383 else // horiz. direction
385 flatdist -= stepdist;
386 move = org + flatdir * stepdist;
389 if(nav_action == NAV_SWIM_ONWATER)
391 tracebox(org, m1, m2, move, movemode, e); // swim
394 if (trace_fraction < 1)
397 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
399 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
402 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
404 if(autocvar_bot_debug_tracewalk)
407 debugnodestatus(org, DEBUG_NODE_SUCCESS);
410 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
414 if(autocvar_bot_debug_tracewalk)
415 debugnodestatus(org, DEBUG_NODE_FAIL);
418 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
429 if (org.z <= move.z) // going horiz.
431 tracebox(trace_endpos, m1, m2, move, movemode, e);
433 nav_action = NAV_WALK;
438 if (org.z <= move.z) // going horiz.
441 nav_action = NAV_SWIM_ONWATER;
447 nav_action = NAV_SWIM_UNDERWATER;
449 nav_action = NAV_SWIM_ONWATER;
452 else if(nav_action == NAV_SWIM_UNDERWATER)
454 if (move.z >= org.z) // swimming upwards or horiz.
456 tracebox(org, m1, m2, move, movemode, e); // swim
458 bool stepswum = false;
461 if (trace_fraction < 1)
464 vector stepswim_move = move + stepheightvec;
465 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
466 stepswim_move.z = end2.z;
468 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
471 if (trace_startsolid)
473 if(autocvar_bot_debug_tracewalk)
474 debugnodestatus(org, DEBUG_NODE_FAIL);
476 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
480 if (trace_fraction < 1)
482 float org_z_prev = org.z;
483 RESURFACE_LIMITED(org, end2.z);
484 if(org.z == org_z_prev)
486 if(autocvar_bot_debug_tracewalk)
487 debugnodestatus(org, DEBUG_NODE_FAIL);
489 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
493 nav_action = NAV_SWIM_UNDERWATER;
495 nav_action = NAV_SWIM_ONWATER;
497 // we didn't advance horiz. in this step, flatdist decrease should be reverted
498 // but we can't do it properly right now... apply this workaround instead
516 if (!WETFEET(trace_endpos))
518 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
519 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
520 if (!stepswum && SUBMERGED(trace_endpos))
522 RESURFACE_LIMITED(trace_endpos, end2.z);
524 nav_action = NAV_SWIM_ONWATER;
530 nav_action = NAV_WALK;
536 nav_action = NAV_SWIM_UNDERWATER;
539 else //if (move.z < org.z) // swimming downwards
541 tracebox(org, m1, m2, move, movemode, e); // swim
544 if (trace_fraction < 1)
547 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
550 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
552 if(autocvar_bot_debug_tracewalk)
553 debugnodestatus(move, DEBUG_NODE_FAIL);
555 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
567 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
569 // stepswim caused upwards direction
570 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
571 if (!SUBMERGED(trace_endpos))
574 nav_action = NAV_WALK;
581 nav_action = NAV_SWIM_UNDERWATER;
585 else if(nav_action == NAV_WALK)
588 tracebox(org, m1, m2, move, movemode, e);
590 if(autocvar_bot_debug_tracewalk)
591 debugnode(e, trace_endpos);
594 if (trace_fraction < 1)
596 // check if we can walk over this obstacle, possibly by jumpstepping
597 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
598 if (trace_fraction < 1 || trace_startsolid)
600 if (trace_startsolid) // hit ceiling above org
602 // reduce stepwalk height
603 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
604 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
606 else //if (trace_fraction < 1)
608 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
609 if (trace_startsolid) // hit ceiling above org
611 // reduce jumpstepwalk height
612 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
613 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
617 if (trace_fraction < 1)
619 vector v = trace_endpos;
620 v.z = org.z + jumpheight_vec.z;
621 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
623 if(autocvar_bot_debug_tracewalk)
626 debugnodestatus(v, DEBUG_NODE_SUCCESS);
629 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
633 if(autocvar_bot_debug_tracewalk)
634 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
636 traceline( org, move, movemode, e);
638 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
642 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
644 nextmove = move + (flatdir * stepdist);
645 traceline( move, nextmove, movemode, e);
648 flatdist = vlen(vec2(end - move));
652 if(autocvar_bot_debug_tracewalk)
653 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
655 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
656 //te_explosion(trace_endpos);
657 //print(ftos(e.dphitcontentsmask), "\n");
658 return false; // failed
670 // trace down from stepheight as far as possible and move there,
671 // if this starts in solid we try again without the stepup, and
672 // if that also fails we assume it is a wall
673 // (this is the same logic as the Quake walkmove function used)
674 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
682 if(autocvar_bot_debug_tracewalk)
684 debugnode(e, trace_endpos);
685 debugnodestatus(org, DEBUG_NODE_FAIL);
688 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
695 if(move.z >= end2.z && org.z < end2.z)
700 if(org.z > move.z - 1 || !SUBMERGED(org))
702 nav_action = NAV_WALK;
706 // ended up submerged while walking
707 if(autocvar_bot_debug_tracewalk)
710 RESURFACE_LIMITED(org, move.z);
711 nav_action = NAV_SWIM_ONWATER;
716 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
718 // moved but didn't arrive at the intended destination
719 if(autocvar_bot_debug_tracewalk)
720 debugnodestatus(org, DEBUG_NODE_FAIL);
725 /////////////////////////////////////////////////////////////////////////////
727 /////////////////////////////////////////////////////////////////////////////
729 // completely empty the goal stack, used when deciding where to go
730 void navigation_clearroute(entity this)
732 this.goalcurrent_prev = this.goalcurrent;
733 this.goalcurrent_distance_2d = FLOAT_MAX;
734 this.goalcurrent_distance_z = FLOAT_MAX;
735 this.goalcurrent_distance_time = 0;
736 this.goalentity_lock_timeout = 0;
737 this.goalentity_shouldbefrozen = false;
738 this.goalentity = NULL;
739 this.goalcurrent = NULL;
740 this.goalstack01 = NULL;
741 this.goalstack02 = NULL;
742 this.goalstack03 = NULL;
743 this.goalstack04 = NULL;
744 this.goalstack05 = NULL;
745 this.goalstack06 = NULL;
746 this.goalstack07 = NULL;
747 this.goalstack08 = NULL;
748 this.goalstack09 = NULL;
749 this.goalstack10 = NULL;
750 this.goalstack11 = NULL;
751 this.goalstack12 = NULL;
752 this.goalstack13 = NULL;
753 this.goalstack14 = NULL;
754 this.goalstack15 = NULL;
755 this.goalstack16 = NULL;
756 this.goalstack17 = NULL;
757 this.goalstack18 = NULL;
758 this.goalstack19 = NULL;
759 this.goalstack20 = NULL;
760 this.goalstack21 = NULL;
761 this.goalstack22 = NULL;
762 this.goalstack23 = NULL;
763 this.goalstack24 = NULL;
764 this.goalstack25 = NULL;
765 this.goalstack26 = NULL;
766 this.goalstack27 = NULL;
767 this.goalstack28 = NULL;
768 this.goalstack29 = NULL;
769 this.goalstack30 = NULL;
770 this.goalstack31 = NULL;
773 // add a new goal at the beginning of the stack
774 // (in other words: add a new prerequisite before going to the later goals)
775 // NOTE: when a waypoint is added, the WP gets pushed first, then the
776 // next-closest WP on the shortest path to the WP
777 // That means, if the stack overflows, the bot will know how to do the FIRST 32
778 // steps to the goal, and then recalculate the path.
779 void navigation_pushroute(entity this, entity e)
781 this.goalcurrent_prev = this.goalcurrent;
782 this.goalcurrent_distance_2d = FLOAT_MAX;
783 this.goalcurrent_distance_z = FLOAT_MAX;
784 this.goalcurrent_distance_time = 0;
785 //print("bot ", etos(this), " push ", etos(e), "\n");
786 if(this.goalstack31 == this.goalentity)
787 this.goalentity = NULL;
788 this.goalstack31 = this.goalstack30;
789 this.goalstack30 = this.goalstack29;
790 this.goalstack29 = this.goalstack28;
791 this.goalstack28 = this.goalstack27;
792 this.goalstack27 = this.goalstack26;
793 this.goalstack26 = this.goalstack25;
794 this.goalstack25 = this.goalstack24;
795 this.goalstack24 = this.goalstack23;
796 this.goalstack23 = this.goalstack22;
797 this.goalstack22 = this.goalstack21;
798 this.goalstack21 = this.goalstack20;
799 this.goalstack20 = this.goalstack19;
800 this.goalstack19 = this.goalstack18;
801 this.goalstack18 = this.goalstack17;
802 this.goalstack17 = this.goalstack16;
803 this.goalstack16 = this.goalstack15;
804 this.goalstack15 = this.goalstack14;
805 this.goalstack14 = this.goalstack13;
806 this.goalstack13 = this.goalstack12;
807 this.goalstack12 = this.goalstack11;
808 this.goalstack11 = this.goalstack10;
809 this.goalstack10 = this.goalstack09;
810 this.goalstack09 = this.goalstack08;
811 this.goalstack08 = this.goalstack07;
812 this.goalstack07 = this.goalstack06;
813 this.goalstack06 = this.goalstack05;
814 this.goalstack05 = this.goalstack04;
815 this.goalstack04 = this.goalstack03;
816 this.goalstack03 = this.goalstack02;
817 this.goalstack02 = this.goalstack01;
818 this.goalstack01 = this.goalcurrent;
819 this.goalcurrent = e;
822 // remove first goal from stack
823 // (in other words: remove a prerequisite for reaching the later goals)
824 // (used when a spawnfunc_waypoint is reached)
825 void navigation_poproute(entity this)
827 this.goalcurrent_prev = this.goalcurrent;
828 this.goalcurrent_distance_2d = FLOAT_MAX;
829 this.goalcurrent_distance_z = FLOAT_MAX;
830 this.goalcurrent_distance_time = 0;
831 //print("bot ", etos(this), " pop\n");
832 if(this.goalcurrent == this.goalentity)
834 this.goalentity = NULL;
835 this.goalentity_lock_timeout = 0;
837 this.goalcurrent = this.goalstack01;
838 this.goalstack01 = this.goalstack02;
839 this.goalstack02 = this.goalstack03;
840 this.goalstack03 = this.goalstack04;
841 this.goalstack04 = this.goalstack05;
842 this.goalstack05 = this.goalstack06;
843 this.goalstack06 = this.goalstack07;
844 this.goalstack07 = this.goalstack08;
845 this.goalstack08 = this.goalstack09;
846 this.goalstack09 = this.goalstack10;
847 this.goalstack10 = this.goalstack11;
848 this.goalstack11 = this.goalstack12;
849 this.goalstack12 = this.goalstack13;
850 this.goalstack13 = this.goalstack14;
851 this.goalstack14 = this.goalstack15;
852 this.goalstack15 = this.goalstack16;
853 this.goalstack16 = this.goalstack17;
854 this.goalstack17 = this.goalstack18;
855 this.goalstack18 = this.goalstack19;
856 this.goalstack19 = this.goalstack20;
857 this.goalstack20 = this.goalstack21;
858 this.goalstack21 = this.goalstack22;
859 this.goalstack22 = this.goalstack23;
860 this.goalstack23 = this.goalstack24;
861 this.goalstack24 = this.goalstack25;
862 this.goalstack25 = this.goalstack26;
863 this.goalstack26 = this.goalstack27;
864 this.goalstack27 = this.goalstack28;
865 this.goalstack28 = this.goalstack29;
866 this.goalstack29 = this.goalstack30;
867 this.goalstack30 = this.goalstack31;
868 this.goalstack31 = NULL;
871 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
872 // waypoint destination coordinates instead of v (only useful for box waypoints)
873 // for normal waypoints v2 == v and v2_height == 0
874 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)
876 if (vdist(v - org, <, bestdist))
878 traceline(v, org, true, ent);
879 if (trace_fraction == 1)
883 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
888 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
896 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
897 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
900 ent = ent.tag_entity;
902 vector pm1 = ent.origin + ent.mins;
903 vector pm2 = ent.origin + ent.maxs;
905 // do two scans, because box test is cheaper
906 IL_EACH(g_waypoints, it != ent && it != except,
908 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
910 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
912 waypoint_clearlinks(ent); // initialize wpXXmincost fields
913 navigation_item_addlink(it, ent);
919 vector org = ent.origin;
920 if (navigation_testtracewalk)
926 if(ent.size && !IS_PLAYER(ent))
928 org += 0.5 * (ent.mins + ent.maxs);
929 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
932 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
934 waypoint_clearlinks(ent); // initialize wpXXmincost fields
935 IL_EACH(g_waypoints, it != ent,
937 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
940 set_tracewalk_dest(ent, it.origin, false);
941 if (vdist(tracewalk_dest - it.origin, <, 1050)
942 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
943 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
945 navigation_item_addlink(it, ent);
950 // box check failed, try walk
951 IL_EACH(g_waypoints, it != ent,
953 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
959 set_tracewalk_dest(ent, v, true);
960 if (trace_ent == ent)
968 set_tracewalk_dest(it, org, false);
970 if (navigation_waypoint_will_link(v, org, ent,
971 tracewalk_dest, tracewalk_dest_height,
972 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
975 bestdist = vlen(tracewalk_dest - v);
977 bestdist = vlen(v - org);
981 if(!best && !ent.navigation_dynamicgoal)
983 int solid_save = ent.solid;
984 ent.solid = SOLID_BSP;
985 IL_EACH(g_jumppads, true,
987 if(trigger_push_test(it, ent))
989 best = it.nearestwaypoint;
993 ent.solid = solid_save;
997 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
999 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1000 if (autocvar_g_waypointeditor_auto)
1002 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1004 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1009 // finds the waypoints near the bot initiating a navigation query
1010 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1012 //navigation_testtracewalk = true;
1014 IL_EACH(g_waypoints, !it.wpconsidered,
1016 set_tracewalk_dest(it, this.origin, false);
1018 vector diff = tracewalk_dest - this.origin;
1019 diff.z = max(0, diff.z);
1020 if(vdist(diff, <, maxdist))
1022 it.wpconsidered = true;
1023 if (tracewalk(this, this.origin, this.mins, this.maxs,
1024 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1026 it.wpnearestpoint = tracewalk_dest;
1027 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1034 //navigation_testtracewalk = false;
1038 // updates a path link if a spawnfunc_waypoint link is better than the current one
1039 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1045 m1 = wp.origin + wp.mins;
1046 m2 = wp.origin + wp.maxs;
1047 v.x = bound(m1_x, p.x, m2_x);
1048 v.y = bound(m1_y, p.y, m2_y);
1049 v.z = bound(m1_z, p.z, m2_z);
1053 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1054 cost += w.wp00mincost; // assuming teleport has exactly one destination
1056 cost += waypoint_gettravelcost(p, v, w, wp);
1057 if (wp.wpcost > cost)
1062 wp.wpnearestpoint = v;
1066 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1067 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1072 IL_EACH(g_waypoints, true,
1074 it.wpconsidered = false;
1075 it.wpnearestpoint = '0 0 0';
1076 it.wpcost = 10000000;
1081 if(fixed_source_waypoint)
1083 fixed_source_waypoint.wpconsidered = true;
1084 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1085 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1086 fixed_source_waypoint.wpfire = 1;
1087 fixed_source_waypoint.enemy = NULL;
1091 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1092 // as this search is expensive we will use lower values if the bot is on the air
1093 float increment, maxdistance;
1094 if(IS_ONGROUND(this))
1097 maxdistance = 50000;
1105 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1108 bool searching = true;
1112 IL_EACH(g_waypoints, it.wpfire,
1117 p = it.wpnearestpoint;
1119 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1120 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1121 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1122 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1123 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1124 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1125 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1126 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1127 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1128 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1129 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1130 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1131 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1132 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1133 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1156 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1157 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1161 IL_EACH(g_waypoints, true,
1163 it.wpconsidered = false;
1164 it.wpnearestpoint = '0 0 0';
1165 it.wpcost = 10000000;
1170 if(fixed_source_waypoint)
1172 fixed_source_waypoint.wpconsidered = true;
1173 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1174 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1175 fixed_source_waypoint.wpfire = 1;
1176 fixed_source_waypoint.enemy = NULL;
1180 error("need to start with a waypoint\n");
1183 bool searching = true;
1187 IL_EACH(g_waypoints, it.wpfire,
1191 cost = it.wpcost; // cost to walk from it to home
1192 p = it.wpnearestpoint;
1194 IL_EACH(g_waypoints, it != wp,
1196 if(!waypoint_islinked(it, wp))
1198 cost2 = cost + it.dmg;
1199 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1205 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1206 void navigation_routerating(entity this, entity e, float f, float rangebias)
1214 rangebias = waypoint_getlinearcost(rangebias);
1215 f = waypoint_getlinearcost(f);
1219 bool rate_wps = false;
1220 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1221 || (e.flags & FL_PARTIALGROUND))
1228 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1229 int t = pointcontents(trace_endpos + '0 0 1');
1230 if(t != CONTENT_SOLID )
1232 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1234 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1241 entity theEnemy = e;
1242 entity best_wp = NULL;
1243 float best_dist = FLOAT_MAX;
1244 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1245 && vdist(it.origin - theEnemy.origin, <, 500)
1246 && vdist(it.origin - this.origin, >, 100)
1247 && vdist(it.origin - this.origin, <, 10000),
1249 float dist = vlen2(it.origin - theEnemy.origin);
1250 if (dist < best_dist)
1262 vector goal_org = (e.absmin + e.absmax) * 0.5;
1264 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1266 // Evaluate path using jetpack
1267 if(this.items & IT_JETPACK)
1268 if(autocvar_bot_ai_navigation_jetpack)
1269 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1271 vector pointa, pointb;
1273 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1276 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1277 pointa = trace_endpos - '0 0 1';
1280 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1281 pointb = trace_endpos - '0 0 1';
1283 // Can I see these two points from the sky?
1284 traceline(pointa, pointb, MOVE_NORMAL, this);
1286 if(trace_fraction==1)
1288 LOG_DEBUG("jetpack ai: can bridge these two points");
1290 // Lower the altitude of these points as much as possible
1291 float zdistance, xydistance, cost, t, fuel;
1292 vector down, npa, npb;
1294 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1297 npa = pointa + down;
1298 npb = pointb + down;
1300 if(npa.z<=this.absmax.z)
1303 if(npb.z<=e.absmax.z)
1306 traceline(npa, npb, MOVE_NORMAL, this);
1307 if(trace_fraction==1)
1313 while(trace_fraction == 1);
1316 // Rough estimation of fuel consumption
1317 // (ignores acceleration and current xyz velocity)
1318 xydistance = vlen(pointa - pointb);
1319 zdistance = fabs(pointa.z - this.origin.z);
1321 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1322 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1323 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1325 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResourceAmount(this, RESOURCE_FUEL)));
1328 if(GetResourceAmount(this, RESOURCE_FUEL) > fuel || (this.items & IT_UNLIMITED_WEAPON_AMMO))
1331 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1332 // - between air and ground speeds)
1334 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1335 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1338 // Compare against other goals
1339 f = f * rangebias / (rangebias + cost);
1341 if (navigation_bestrating < f)
1343 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1344 navigation_bestrating = f;
1345 navigation_bestgoal = e;
1346 this.navigation_jetpack_goal = e;
1347 this.navigation_jetpack_point = pointb;
1355 //te_wizspike(e.origin);
1358 // update the cached spawnfunc_waypoint link on a dynamic item entity
1359 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1365 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1366 e.nearestwaypoint = NULL;
1368 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1369 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1371 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1372 e.nearestwaypoint = nwp = e.goalcurrent;
1374 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1377 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1379 if(!e.navigation_dynamicgoal)
1380 e.blacklisted = true;
1384 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1389 if(e.navigation_dynamicgoal)
1390 e.nearestwaypointtimeout = time + 2;
1391 else if(autocvar_g_waypointeditor)
1392 e.nearestwaypointtimeout = time + 3 + random() * 2;
1394 nwp = e.nearestwaypoint;
1397 if (nwp && nwp.wpcost < 10000000)
1399 //te_wizspike(nwp.wpnearestpoint);
1400 float nwptoitem_cost = 0;
1401 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1402 nwptoitem_cost = nwp.wp00mincost;
1404 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1405 float cost = nwp.wpcost + nwptoitem_cost;
1406 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1407 f = f * rangebias / (rangebias + cost);
1408 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1409 if (navigation_bestrating < f)
1411 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1412 navigation_bestrating = f;
1413 navigation_bestgoal = e;
1418 // adds an item to the the goal stack with the path to a given item
1419 bool navigation_routetogoal(entity this, entity e, vector startposition)
1421 // if there is no goal, just exit
1425 entity teleport_goal = NULL;
1427 this.goalentity = e;
1429 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1431 // force teleport destination as route destination
1433 navigation_pushroute(this, e.wp00);
1434 this.goalentity = e.wp00;
1437 // put the entity on the goal stack
1438 //print("routetogoal ", etos(e), "\n");
1439 navigation_pushroute(this, e);
1442 e = this.goalentity;
1444 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1446 this.wp_goal_prev1 = this.wp_goal_prev0;
1447 this.wp_goal_prev0 = e;
1451 if(e==this.navigation_jetpack_goal)
1454 // if it can reach the goal there is nothing more to do
1455 set_tracewalk_dest(e, startposition, true);
1456 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1457 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1458 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1463 entity nearest_wp = NULL;
1464 // see if there are waypoints describing a path to the item
1465 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1467 e = e.nearestwaypoint;
1470 else if(teleport_goal)
1473 e = e.enemy; // we already have added it, so...
1478 if(nearest_wp && nearest_wp.enemy)
1480 // often path can be optimized by not adding the nearest waypoint
1481 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1483 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1485 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1486 e = nearest_wp.enemy;
1489 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1490 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1491 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1492 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1493 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1495 e = nearest_wp.enemy;
1500 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1501 e = nearest_wp.enemy;
1506 // add the spawnfunc_waypoint to the path
1507 navigation_pushroute(this, e);
1517 // shorten path by removing intermediate goals
1518 bool navigation_shortenpath(entity this)
1520 if (!this.goalstack01 || wasfreed(this.goalstack01))
1522 if (this.bot_tracewalk_time > time)
1524 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1526 bool cut_allowed = false;
1527 entity next = this.goalentity;
1528 // evaluate whether bot can discard current route and chase directly a player, trying to
1529 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1530 if (IS_MOVABLE(next))
1532 set_tracewalk_dest(next, this.origin, true);
1533 if (vdist(this.origin - tracewalk_dest, <, 200))
1535 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1536 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1537 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1538 && checkpvs(this.origin + this.view_ofs, next))
1540 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1544 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1545 while (deviation.y < -180) deviation.y += 360;
1546 while (deviation.y > 180) deviation.y -= 360;
1547 if (fabs(deviation.y) > 25)
1553 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1554 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1556 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1559 navigation_poproute(this);
1561 while (this.goalcurrent != next);
1568 next = this.goalstack01;
1569 // if for some reason the bot is closer to the next goal, pop the current one
1570 if (!IS_MOVABLE(next) // already checked in the previous case
1571 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1572 && checkpvs(this.origin + this.view_ofs, next))
1574 set_tracewalk_dest(next, this.origin, true);
1580 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1581 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1583 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1584 navigation_poproute(this);
1591 // removes any currently touching waypoints from the goal stack
1592 // (this is how bots detect if they reached a goal)
1593 int navigation_poptouchedgoals(entity this)
1595 int removed_goals = 0;
1597 if(!this.goalcurrent)
1598 return removed_goals;
1600 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1602 // make sure jumppad is really hit, don't rely on distance based checks
1603 // as they may report a touch even if it didn't really happen
1604 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1606 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1607 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1609 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1610 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1612 if(this.jumppadcount)
1614 // remove jumppad waypoint after a random delay to prevent bots getting
1615 // stuck on certain jumppads that require an extra initial horizontal speed
1616 float max_delay = 0.1;
1617 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1619 if (time - this.lastteleporttime < random() * max_delay)
1620 return removed_goals;
1622 navigation_poproute(this);
1623 this.lastteleporttime = 0;
1627 return removed_goals;
1629 else if (this.lastteleporttime > 0)
1631 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1632 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1633 // if so immediately fix bot path by removing skipped goals
1634 entity tele_ent = NULL;
1635 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1636 tele_ent = this.goalstack01;
1637 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1638 tele_ent = this.goalstack02;
1639 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1640 tele_ent = this.goalstack03;
1641 if (tele_ent && TELEPORT_USED(this, tele_ent))
1643 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1644 if (tele_ent.wpflags & WAYPOINTFLAG_PERSONAL && tele_ent.owner == this)
1646 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1647 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1649 while (this.goalcurrent != tele_ent)
1651 navigation_poproute(this);
1654 navigation_poproute(this);
1655 this.lastteleporttime = 0;
1657 return removed_goals;
1661 // Loose goal touching check when running
1662 if(this.aistatus & AI_STATUS_RUNNING)
1663 if(this.goalcurrent.classname=="waypoint")
1664 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1666 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1668 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1669 if(trace_fraction==1)
1671 // Detect personal waypoints
1672 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1673 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1675 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1676 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1679 navigation_poproute(this);
1681 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1682 return removed_goals;
1687 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1689 vector gc_min = this.goalcurrent.absmin;
1690 vector gc_max = this.goalcurrent.absmax;
1691 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1693 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1694 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1696 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1699 // Detect personal waypoints
1700 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1701 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1703 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1704 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1707 navigation_poproute(this);
1709 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1710 return removed_goals;
1712 return removed_goals;
1715 entity navigation_get_really_close_waypoint(entity this)
1717 entity wp = this.goalcurrent;
1719 wp = this.goalcurrent_prev;
1722 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, 50))
1724 wp = this.goalcurrent_prev;
1728 if(wp.classname != "waypoint")
1730 wp = wp.nearestwaypoint;
1734 if(vdist(wp.origin - this.origin, >, 50))
1737 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1739 if(vdist(it.origin - this.origin, <, 50))
1748 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1751 set_tracewalk_dest(wp, this.origin, false);
1752 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1753 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1760 // begin a goal selection session (queries spawnfunc_waypoint network)
1761 void navigation_goalrating_start(entity this)
1763 if(this.aistatus & AI_STATUS_STUCK)
1766 this.navigation_jetpack_goal = NULL;
1767 navigation_bestrating = -1;
1768 entity wp = navigation_get_really_close_waypoint(this);
1769 navigation_clearroute(this);
1770 navigation_bestgoal = NULL;
1771 navigation_markroutes(this, wp);
1774 // ends a goal selection session (updates goal stack to the best goal)
1775 void navigation_goalrating_end(entity this)
1777 if(this.aistatus & AI_STATUS_STUCK)
1780 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1781 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1783 // If the bot got stuck then try to reach the farthest waypoint
1784 if (!this.goalentity && autocvar_bot_wander_enable)
1786 if (!(this.aistatus & AI_STATUS_STUCK))
1788 LOG_DEBUG(this.netname, " cannot walk to any goal");
1789 this.aistatus |= AI_STATUS_STUCK;
1792 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1795 void botframe_updatedangerousobjects(float maxupdate)
1797 vector m1, m2, v, o;
1801 IL_EACH(g_waypoints, true,
1807 IL_EACH(g_bot_dodge, it.bot_dodge,
1810 v.x = bound(m1_x, v.x, m2_x);
1811 v.y = bound(m1_y, v.y, m2_y);
1812 v.z = bound(m1_z, v.z, m2_z);
1813 o = (it.absmin + it.absmax) * 0.5;
1814 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1817 traceline(o, v, true, NULL);
1818 if (trace_fraction == 1)
1819 danger = danger + d;
1829 void navigation_unstuck(entity this)
1831 if (!autocvar_bot_wander_enable)
1834 bool has_user_waypoints = false;
1835 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1837 has_user_waypoints = true;
1840 if (!has_user_waypoints)
1843 float search_radius = 1000;
1845 if (!bot_waypoint_queue_owner)
1847 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1848 bot_waypoint_queue_owner = this;
1849 bot_waypoint_queue_bestgoal = NULL;
1850 bot_waypoint_queue_bestgoalrating = 0;
1853 if(bot_waypoint_queue_owner!=this)
1856 if (bot_waypoint_queue_goal)
1858 // evaluate the next goal on the queue
1859 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1860 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1861 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1862 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1863 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1865 if( d > bot_waypoint_queue_bestgoalrating)
1867 bot_waypoint_queue_bestgoalrating = d;
1868 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1871 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1873 if (!bot_waypoint_queue_goal)
1875 if (bot_waypoint_queue_bestgoal)
1877 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1878 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1879 navigation_goalrating_timeout_set(this);
1880 this.aistatus &= ~AI_STATUS_STUCK;
1884 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1887 bot_waypoint_queue_owner = NULL;
1892 if(bot_strategytoken!=this)
1895 // build a new queue
1896 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1898 entity first = NULL;
1900 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1902 if(bot_waypoint_queue_goal)
1903 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1907 bot_waypoint_queue_goal = it;
1908 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1912 bot_waypoint_queue_goal = first;
1915 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1916 bot_waypoint_queue_owner = NULL;
1921 // Support for debugging tracewalk visually
1923 void debugresetnodes()
1925 debuglastnode = '0 0 0';
1928 void debugnode(entity this, vector node)
1930 if (!IS_PLAYER(this))
1933 if(debuglastnode=='0 0 0')
1935 debuglastnode = node;
1939 te_lightning2(NULL, node, debuglastnode);
1940 debuglastnode = node;
1943 void debugnodestatus(vector position, float status)
1949 case DEBUG_NODE_SUCCESS:
1952 case DEBUG_NODE_WARNING:
1955 case DEBUG_NODE_FAIL:
1962 te_customflash(position, 40, 2, c);
1965 // Support for debugging the goal stack visually
1968 .vector lastposition;
1970 // Debug the goal stack visually
1971 void debuggoalstack(entity this)
1976 if(this.goalcounter==0)goal=this.goalcurrent;
1977 else if(this.goalcounter==1)goal=this.goalstack01;
1978 else if(this.goalcounter==2)goal=this.goalstack02;
1979 else if(this.goalcounter==3)goal=this.goalstack03;
1980 else if(this.goalcounter==4)goal=this.goalstack04;
1981 else if(this.goalcounter==5)goal=this.goalstack05;
1982 else if(this.goalcounter==6)goal=this.goalstack06;
1983 else if(this.goalcounter==7)goal=this.goalstack07;
1984 else if(this.goalcounter==8)goal=this.goalstack08;
1985 else if(this.goalcounter==9)goal=this.goalstack09;
1986 else if(this.goalcounter==10)goal=this.goalstack10;
1987 else if(this.goalcounter==11)goal=this.goalstack11;
1988 else if(this.goalcounter==12)goal=this.goalstack12;
1989 else if(this.goalcounter==13)goal=this.goalstack13;
1990 else if(this.goalcounter==14)goal=this.goalstack14;
1991 else if(this.goalcounter==15)goal=this.goalstack15;
1992 else if(this.goalcounter==16)goal=this.goalstack16;
1993 else if(this.goalcounter==17)goal=this.goalstack17;
1994 else if(this.goalcounter==18)goal=this.goalstack18;
1995 else if(this.goalcounter==19)goal=this.goalstack19;
1996 else if(this.goalcounter==20)goal=this.goalstack20;
1997 else if(this.goalcounter==21)goal=this.goalstack21;
1998 else if(this.goalcounter==22)goal=this.goalstack22;
1999 else if(this.goalcounter==23)goal=this.goalstack23;
2000 else if(this.goalcounter==24)goal=this.goalstack24;
2001 else if(this.goalcounter==25)goal=this.goalstack25;
2002 else if(this.goalcounter==26)goal=this.goalstack26;
2003 else if(this.goalcounter==27)goal=this.goalstack27;
2004 else if(this.goalcounter==28)goal=this.goalstack28;
2005 else if(this.goalcounter==29)goal=this.goalstack29;
2006 else if(this.goalcounter==30)goal=this.goalstack30;
2007 else if(this.goalcounter==31)goal=this.goalstack31;
2012 this.goalcounter = 0;
2013 this.lastposition='0 0 0';
2017 if(this.lastposition=='0 0 0')
2020 org = this.lastposition;
2023 go = ( goal.absmin + goal.absmax ) * 0.5;
2024 te_lightning2(NULL, org, go);
2025 this.lastposition = go;