1 #include "navigation.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
8 #include "waypoints.qh"
10 #include <server/items/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/hurt.qh>
18 #include <common/mapobjects/trigger/jumppads.qh>
22 void navigation_goalrating_timeout_set(entity this)
24 if(IS_MOVABLE(this.goalentity))
25 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
27 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
30 // use this when current goal must be discarded immediately
31 void navigation_goalrating_timeout_force(entity this)
33 navigation_goalrating_timeout_expire(this, 0);
36 // use this when current goal can be kept for a short while to increase the chance
37 // of bot touching a waypoint, which helps to find a new goal more efficiently
38 void navigation_goalrating_timeout_expire(entity this, float seconds)
41 this.bot_strategytime = 0;
42 else if (this.bot_strategytime > time + seconds)
43 this.bot_strategytime = time + seconds;
46 bool navigation_goalrating_timeout(entity this)
48 return this.bot_strategytime < time;
52 void navigation_goalrating_timeout_extend_if_needed(entity this, float seconds)
54 this.bot_strategytime = max(this.bot_strategytime, time + seconds);
57 #define MAX_CHASE_DISTANCE 700
58 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
60 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
61 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
62 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
67 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
69 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
71 this.ignoregoal = this.goalentity;
72 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
79 void navigation_dynamicgoal_init(entity this, bool initially_static)
81 this.navigation_dynamicgoal = true;
82 this.bot_basewaypoint = this.nearestwaypoint;
84 this.nearestwaypointtimeout = -1;
86 this.nearestwaypointtimeout = time;
89 void navigation_dynamicgoal_set(entity this, entity dropper)
91 this.nearestwaypointtimeout = time;
92 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
93 this.nearestwaypoint = dropper.nearestwaypoint;
94 if (this.nearestwaypoint)
95 this.nearestwaypointtimeout += 2;
98 void navigation_dynamicgoal_unset(entity this)
100 if(this.bot_basewaypoint)
101 this.nearestwaypoint = this.bot_basewaypoint;
102 this.nearestwaypointtimeout = -1;
105 // returns point of ent closer to org
106 vector get_closer_dest(entity ent, vector org)
108 vector dest = '0 0 0';
109 if ((ent.classname != "waypoint") || ent.wpisbox)
111 vector wm1 = ent.origin + ent.mins;
112 vector wm2 = ent.origin + ent.maxs;
113 dest.x = bound(wm1.x, org.x, wm2.x);
114 dest.y = bound(wm1.y, org.y, wm2.y);
115 dest.z = bound(wm1.z, org.z, wm2.z);
122 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
124 if ((ent.classname != "waypoint") || ent.wpisbox)
126 vector wm1 = ent.origin + ent.mins;
127 vector wm2 = ent.origin + ent.maxs;
128 if (IS_PLAYER(ent) || IS_MONSTER(ent))
130 // move destination point out of player bbox otherwise tracebox always fails
131 // (if bot_navigation_ignoreplayers is false)
132 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
133 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
135 // set destination point to x and y coords of ent that are closer to org
136 // z coord is set to ent's min height
137 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
138 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
139 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
140 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
142 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
143 tracewalk_dest_height = 0;
144 fix_player_dest = false;
148 tracewalk_dest.z = wm1.z;
149 tracewalk_dest_height = wm2.z - wm1.z;
154 tracewalk_dest = ent.origin;
155 tracewalk_dest_height = 0;
157 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
159 // snap player to the ground
160 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
162 // bot is right under the player
163 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
164 tracewalk_dest = trace_endpos;
165 tracewalk_dest_height = 0;
169 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
170 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
172 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
173 tracewalk_dest.z = trace_endpos.z;
179 // returns point of ent closer to org
180 vector set_tracewalk_dest_2(entity ent, vector org)
182 vector closer_dest = '0 0 0';
183 if ((ent.classname != "waypoint") || ent.wpisbox)
185 vector wm1 = ent.origin + ent.mins;
186 vector wm2 = ent.origin + ent.maxs;
187 closer_dest.x = bound(wm1.x, org.x, wm2.x);
188 closer_dest.y = bound(wm1.y, org.y, wm2.y);
189 closer_dest.z = bound(wm1.z, org.z, wm2.z);
190 // set destination point to x and y coords of ent that are closer to org
191 // z coord is set to ent's min height
192 tracewalk_dest.x = closer_dest.x;
193 tracewalk_dest.y = closer_dest.y;
194 tracewalk_dest.z = wm1.z;
195 tracewalk_dest_height = wm2.z - wm1.z; // destination height
199 closer_dest = ent.origin;
200 tracewalk_dest = closer_dest;
201 tracewalk_dest_height = 0;
206 bool navigation_check_submerged_state(entity ent, vector pos)
210 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
211 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
212 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
215 submerged = SUBMERGED(pos);
216 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
217 // is actually submerged because often they are inside some solid.
218 // That's why submerged state is saved now that we know current pos is
219 // not stuck in solid (previous tracewalk call to this pos was successfully)
220 if(!ent.navigation_dynamicgoal)
221 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
226 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
228 IL_EACH(g_ladders, it.classname == "func_ladder",
231 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
232 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
235 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
236 tracebox(org, m1, m2, top, movemode, e);
237 if(trace_fraction == 1)
244 // Unfortuntely we can't use trace_inwater since it doesn't hold the fraction of the total
245 // distance that was traveled before impact as the description in the engine (collision.h) says.
246 // It would have helped to speed up tracewalk underwater
247 vector resurface_limited(vector org, float lim, vector m1)
249 if (WETFEET(org + eZ * (lim - org.z)))
253 float RES_min_h = org.z;
254 float RES_max_h = lim;
256 org.z = 0.5 * (RES_min_h + RES_max_h);
261 } while (RES_max_h - RES_min_h >= 1);
266 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
269 #define NAV_SWIM_ONWATER 1
270 #define NAV_SWIM_UNDERWATER 2
272 // rough simulation of walking from one point to another to test if a path
273 // can be traveled, used for waypoint linking and havocbot
274 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
275 // INFO: the command sv_cmd trace walk is useful to test this function in game
276 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
278 if(autocvar_bot_debug_tracewalk)
285 vector flatdir = end - start;
287 float flatdist = vlen(flatdir);
288 flatdir = normalize(flatdir);
290 bool ignorehazards = false;
293 // Analyze starting point
295 ignorehazards = true;
297 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
298 if (trace_startsolid)
301 if(autocvar_bot_debug_tracewalk)
302 debugnodestatus(start, DEBUG_NODE_FAIL);
304 //print("tracewalk: ", vtos(start), " is a bad start\n");
310 end2.z += end_height;
312 vector fixed_end = end;
315 if (flatdist > 0 && WETFEET(org))
318 nav_action = NAV_SWIM_UNDERWATER;
321 // tracebox down by player's height
322 // useful to know if water level is so low that bot can still walk
323 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
324 if (SUBMERGED(trace_endpos))
327 nav_action = NAV_SWIM_UNDERWATER;
330 nav_action = NAV_WALK;
334 nav_action = NAV_WALK;
342 if (org.z > end2.z + 1)
344 tracebox(org, m1, m2, end2, movemode, e);
346 if (org.z > end2.z + 1)
349 else if (org.z < end.z - 1)
351 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
352 if (SUBMERGED(trace_endpos))
354 vector v = trace_endpos;
355 tracebox(v, m1, m2, end, movemode, e);
356 if(trace_endpos.z >= end.z - 1)
358 RESURFACE_LIMITED(v, trace_endpos.z);
362 else if (trace_endpos.z > org.z - jumpheight_vec.z)
363 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
365 if (org.z < end.z - 1)
372 if(autocvar_bot_debug_tracewalk)
375 debugnodestatus(org, DEBUG_NODE_SUCCESS);
378 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
383 if(autocvar_bot_debug_tracewalk)
389 if (stepdist > flatdist)
391 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
393 // can't use movement direction here to calculate move because of
394 // precision errors especially when direction has a high enough z value
395 //water_dir = normalize(water_end - org);
396 //move = org + water_dir * stepdist;
397 fixed_end.z = bound(end.z, org.z, end2.z);
398 if (stepdist == flatdist) {
402 move = org + (fixed_end - org) * (stepdist / flatdist);
403 flatdist = vlen(vec2(fixed_end - move));
406 else // horiz. direction
408 flatdist -= stepdist;
409 move = org + flatdir * stepdist;
412 if(nav_action == NAV_SWIM_ONWATER)
414 tracebox(org, m1, m2, move, movemode, e); // swim
417 if (trace_fraction < 1)
420 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
422 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
425 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
427 if(autocvar_bot_debug_tracewalk)
430 debugnodestatus(org, DEBUG_NODE_SUCCESS);
433 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
437 if(autocvar_bot_debug_tracewalk)
438 debugnodestatus(org, DEBUG_NODE_FAIL);
441 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
452 if (org.z <= move.z) // going horiz.
454 tracebox(trace_endpos, m1, m2, move, movemode, e);
456 nav_action = NAV_WALK;
461 if (org.z <= move.z) // going horiz.
464 nav_action = NAV_SWIM_ONWATER;
470 nav_action = NAV_SWIM_UNDERWATER;
472 nav_action = NAV_SWIM_ONWATER;
475 else if(nav_action == NAV_SWIM_UNDERWATER)
477 if (move.z >= org.z) // swimming upwards or horiz.
479 tracebox(org, m1, m2, move, movemode, e); // swim
481 bool stepswum = false;
484 if (trace_fraction < 1)
487 vector stepswim_move = move + stepheightvec;
488 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
489 stepswim_move.z = end2.z;
491 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
494 if (trace_startsolid)
496 if(autocvar_bot_debug_tracewalk)
497 debugnodestatus(org, DEBUG_NODE_FAIL);
499 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
503 if (trace_fraction < 1)
505 float org_z_prev = org.z;
506 RESURFACE_LIMITED(org, end2.z);
507 if(org.z == org_z_prev)
509 if(autocvar_bot_debug_tracewalk)
510 debugnodestatus(org, DEBUG_NODE_FAIL);
512 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
516 nav_action = NAV_SWIM_UNDERWATER;
518 nav_action = NAV_SWIM_ONWATER;
520 // we didn't advance horiz. in this step, flatdist decrease should be reverted
521 // but we can't do it properly right now... apply this workaround instead
539 if (!WETFEET(trace_endpos))
541 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
542 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
543 if (!stepswum && SUBMERGED(trace_endpos))
545 RESURFACE_LIMITED(trace_endpos, end2.z);
547 nav_action = NAV_SWIM_ONWATER;
553 nav_action = NAV_WALK;
559 nav_action = NAV_SWIM_UNDERWATER;
562 else //if (move.z < org.z) // swimming downwards
564 tracebox(org, m1, m2, move, movemode, e); // swim
567 if (trace_fraction < 1)
570 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
573 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
575 if(autocvar_bot_debug_tracewalk)
576 debugnodestatus(move, DEBUG_NODE_FAIL);
578 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
590 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
592 // stepswim caused upwards direction
593 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
594 if (!SUBMERGED(trace_endpos))
597 nav_action = NAV_WALK;
604 nav_action = NAV_SWIM_UNDERWATER;
608 else if(nav_action == NAV_WALK)
611 tracebox(org, m1, m2, move, movemode, e);
613 if(autocvar_bot_debug_tracewalk)
614 debugnode(e, trace_endpos);
617 if (trace_fraction < 1)
619 // check if we can walk over this obstacle, possibly by jumpstepping
620 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
621 if (trace_fraction < 1 || trace_startsolid)
623 if (trace_startsolid) // hit ceiling above org
625 // reduce stepwalk height
626 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
627 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
629 else //if (trace_fraction < 1)
631 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
632 if (trace_startsolid) // hit ceiling above org
634 // reduce jumpstepwalk height
635 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
636 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
640 if (trace_fraction < 1)
642 vector v = trace_endpos;
643 v.z = org.z + jumpheight_vec.z;
644 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
646 if(autocvar_bot_debug_tracewalk)
649 debugnodestatus(v, DEBUG_NODE_SUCCESS);
652 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
656 if(autocvar_bot_debug_tracewalk)
657 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
659 traceline( org, move, movemode, e);
661 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
665 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
667 nextmove = move + (flatdir * stepdist);
668 traceline( move, nextmove, movemode, e);
671 flatdist = vlen(vec2(end - move));
675 if(autocvar_bot_debug_tracewalk)
676 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
678 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
679 //te_explosion(trace_endpos);
680 //print(ftos(e.dphitcontentsmask), "\n");
681 return false; // failed
693 // trace down from stepheight as far as possible and move there,
694 // if this starts in solid we try again without the stepup, and
695 // if that also fails we assume it is a wall
696 // (this is the same logic as the Quake walkmove function used)
697 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
705 if(autocvar_bot_debug_tracewalk)
707 debugnode(e, trace_endpos);
708 debugnodestatus(org, DEBUG_NODE_FAIL);
711 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
718 if(move.z >= end2.z && org.z < end2.z)
723 if(org.z > move.z - 1 || !SUBMERGED(org))
725 nav_action = NAV_WALK;
729 // ended up submerged while walking
730 if(autocvar_bot_debug_tracewalk)
733 RESURFACE_LIMITED(org, move.z);
734 nav_action = NAV_SWIM_ONWATER;
739 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
741 // moved but didn't arrive at the intended destination
742 if(autocvar_bot_debug_tracewalk)
743 debugnodestatus(org, DEBUG_NODE_FAIL);
748 /////////////////////////////////////////////////////////////////////////////
750 /////////////////////////////////////////////////////////////////////////////
752 // completely empty the goal stack, used when deciding where to go
753 void navigation_clearroute(entity this)
755 this.lastteleporttime = 0;
756 this.goalcurrent_prev = this.goalcurrent;
757 this.goalcurrent_distance_2d = FLOAT_MAX;
758 this.goalcurrent_distance_z = FLOAT_MAX;
759 this.goalcurrent_distance_time = 0;
760 this.goalentity_lock_timeout = 0;
761 this.goalentity_shouldbefrozen = false;
762 this.goalentity = NULL;
763 this.goalcurrent = NULL;
764 this.goalstack01 = NULL;
765 this.goalstack02 = NULL;
766 this.goalstack03 = NULL;
767 this.goalstack04 = NULL;
768 this.goalstack05 = NULL;
769 this.goalstack06 = NULL;
770 this.goalstack07 = NULL;
771 this.goalstack08 = NULL;
772 this.goalstack09 = NULL;
773 this.goalstack10 = NULL;
774 this.goalstack11 = NULL;
775 this.goalstack12 = NULL;
776 this.goalstack13 = NULL;
777 this.goalstack14 = NULL;
778 this.goalstack15 = NULL;
779 this.goalstack16 = NULL;
780 this.goalstack17 = NULL;
781 this.goalstack18 = NULL;
782 this.goalstack19 = NULL;
783 this.goalstack20 = NULL;
784 this.goalstack21 = NULL;
785 this.goalstack22 = NULL;
786 this.goalstack23 = NULL;
787 this.goalstack24 = NULL;
788 this.goalstack25 = NULL;
789 this.goalstack26 = NULL;
790 this.goalstack27 = NULL;
791 this.goalstack28 = NULL;
792 this.goalstack29 = NULL;
793 this.goalstack30 = NULL;
794 this.goalstack31 = NULL;
797 // add a new goal at the beginning of the stack
798 // (in other words: add a new prerequisite before going to the later goals)
799 // NOTE: when a waypoint is added, the WP gets pushed first, then the
800 // next-closest WP on the shortest path to the WP
801 // That means, if the stack overflows, the bot will know how to do the FIRST 32
802 // steps to the goal, and then recalculate the path.
803 void navigation_pushroute(entity this, entity e)
805 this.goalcurrent_prev = this.goalcurrent;
806 this.goalcurrent_distance_2d = FLOAT_MAX;
807 this.goalcurrent_distance_z = FLOAT_MAX;
808 this.goalcurrent_distance_time = 0;
809 //print("bot ", etos(this), " push ", etos(e), "\n");
810 if(this.goalstack31 == this.goalentity)
811 this.goalentity = NULL;
812 this.goalstack31 = this.goalstack30;
813 this.goalstack30 = this.goalstack29;
814 this.goalstack29 = this.goalstack28;
815 this.goalstack28 = this.goalstack27;
816 this.goalstack27 = this.goalstack26;
817 this.goalstack26 = this.goalstack25;
818 this.goalstack25 = this.goalstack24;
819 this.goalstack24 = this.goalstack23;
820 this.goalstack23 = this.goalstack22;
821 this.goalstack22 = this.goalstack21;
822 this.goalstack21 = this.goalstack20;
823 this.goalstack20 = this.goalstack19;
824 this.goalstack19 = this.goalstack18;
825 this.goalstack18 = this.goalstack17;
826 this.goalstack17 = this.goalstack16;
827 this.goalstack16 = this.goalstack15;
828 this.goalstack15 = this.goalstack14;
829 this.goalstack14 = this.goalstack13;
830 this.goalstack13 = this.goalstack12;
831 this.goalstack12 = this.goalstack11;
832 this.goalstack11 = this.goalstack10;
833 this.goalstack10 = this.goalstack09;
834 this.goalstack09 = this.goalstack08;
835 this.goalstack08 = this.goalstack07;
836 this.goalstack07 = this.goalstack06;
837 this.goalstack06 = this.goalstack05;
838 this.goalstack05 = this.goalstack04;
839 this.goalstack04 = this.goalstack03;
840 this.goalstack03 = this.goalstack02;
841 this.goalstack02 = this.goalstack01;
842 this.goalstack01 = this.goalcurrent;
843 this.goalcurrent = e;
846 // remove first goal from stack
847 // (in other words: remove a prerequisite for reaching the later goals)
848 // (used when a spawnfunc_waypoint is reached)
849 void navigation_poproute(entity this)
851 this.goalcurrent_prev = this.goalcurrent;
852 this.goalcurrent_distance_2d = FLOAT_MAX;
853 this.goalcurrent_distance_z = FLOAT_MAX;
854 this.goalcurrent_distance_time = 0;
855 //print("bot ", etos(this), " pop\n");
856 if(this.goalcurrent == this.goalentity)
858 this.goalentity = NULL;
859 this.goalentity_lock_timeout = 0;
861 this.goalcurrent = this.goalstack01;
862 this.goalstack01 = this.goalstack02;
863 this.goalstack02 = this.goalstack03;
864 this.goalstack03 = this.goalstack04;
865 this.goalstack04 = this.goalstack05;
866 this.goalstack05 = this.goalstack06;
867 this.goalstack06 = this.goalstack07;
868 this.goalstack07 = this.goalstack08;
869 this.goalstack08 = this.goalstack09;
870 this.goalstack09 = this.goalstack10;
871 this.goalstack10 = this.goalstack11;
872 this.goalstack11 = this.goalstack12;
873 this.goalstack12 = this.goalstack13;
874 this.goalstack13 = this.goalstack14;
875 this.goalstack14 = this.goalstack15;
876 this.goalstack15 = this.goalstack16;
877 this.goalstack16 = this.goalstack17;
878 this.goalstack17 = this.goalstack18;
879 this.goalstack18 = this.goalstack19;
880 this.goalstack19 = this.goalstack20;
881 this.goalstack20 = this.goalstack21;
882 this.goalstack21 = this.goalstack22;
883 this.goalstack22 = this.goalstack23;
884 this.goalstack23 = this.goalstack24;
885 this.goalstack24 = this.goalstack25;
886 this.goalstack25 = this.goalstack26;
887 this.goalstack26 = this.goalstack27;
888 this.goalstack27 = this.goalstack28;
889 this.goalstack28 = this.goalstack29;
890 this.goalstack29 = this.goalstack30;
891 this.goalstack30 = this.goalstack31;
892 this.goalstack31 = NULL;
895 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
896 // waypoint destination coordinates instead of v (only useful for box waypoints)
897 // for normal waypoints v2 == v and v2_height == 0
898 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)
900 if (vdist(v - org, <, bestdist))
902 traceline(v, org, true, ent);
903 if (trace_fraction == 1)
907 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
912 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
920 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
921 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
924 ent = ent.tag_entity;
926 vector pm1 = ent.origin + ent.mins;
927 vector pm2 = ent.origin + ent.maxs;
929 if (autocvar_g_waypointeditor && !IS_BOT_CLIENT(ent))
931 // this code allows removing waypoints in the air and seeing jumppad/telepport waypoint links
932 // FIXME it causes a bug where a waypoint spawned really close to another one (max 16 qu)
933 // isn't detected as the nearest waypoint
934 IL_EACH(g_waypoints, it != ent && it != except,
936 if (boxesoverlap(pm1, pm2, it.absmin, it.absmax))
942 // do two scans, because box test is cheaper
943 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
945 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
947 if(walkfromwp && !ent.navigation_dynamicgoal)
948 waypoint_clearlinks(ent); // initialize wpXXmincost fields
954 vector org = ent.origin;
955 if (navigation_testtracewalk)
961 if(ent.size && !IS_PLAYER(ent))
963 org += 0.5 * (ent.mins + ent.maxs);
964 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
967 // box check failed, try walk
968 IL_EACH(g_waypoints, it != ent,
970 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
976 set_tracewalk_dest(ent, v, true);
977 if (trace_ent == ent)
985 set_tracewalk_dest(it, org, false);
987 if (navigation_waypoint_will_link(v, org, ent,
988 tracewalk_dest, tracewalk_dest_height,
989 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
992 bestdist = vlen(tracewalk_dest - v);
994 bestdist = vlen(v - org);
998 if(!best && !ent.navigation_dynamicgoal)
1000 int solid_save = ent.solid;
1001 ent.solid = SOLID_BSP;
1002 IL_EACH(g_jumppads, true,
1004 if(trigger_push_test(it, ent))
1006 best = it.nearestwaypoint;
1010 ent.solid = solid_save;
1014 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1016 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1017 if (autocvar_g_waypointeditor_auto)
1019 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1021 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1026 // finds the waypoints near the bot initiating a navigation query
1027 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1029 //navigation_testtracewalk = true;
1031 IL_EACH(g_waypoints, !it.wpconsidered,
1033 set_tracewalk_dest(it, this.origin, false);
1035 vector diff = tracewalk_dest - this.origin;
1036 diff.z = max(0, diff.z);
1037 if(vdist(diff, <, maxdist))
1039 it.wpconsidered = true;
1040 if (tracewalk(this, this.origin, this.mins, this.maxs,
1041 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1043 it.wpnearestpoint = tracewalk_dest;
1044 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1051 //navigation_testtracewalk = false;
1055 // updates a path link if a spawnfunc_waypoint link is better than the current one
1056 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1062 m1 = wp.origin + wp.mins;
1063 m2 = wp.origin + wp.maxs;
1064 v.x = bound(m1_x, p.x, m2_x);
1065 v.y = bound(m1_y, p.y, m2_y);
1066 v.z = bound(m1_z, p.z, m2_z);
1070 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1071 cost += w.wp00mincost; // assuming teleport has exactly one destination
1073 cost += waypoint_gettravelcost(p, v, w, wp);
1074 if (wp.wpcost > cost)
1079 wp.wpnearestpoint = v;
1083 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1084 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1089 IL_EACH(g_waypoints, true,
1091 it.wpconsidered = false;
1092 it.wpnearestpoint = '0 0 0';
1093 it.wpcost = 10000000;
1098 if(fixed_source_waypoint)
1100 fixed_source_waypoint.wpconsidered = true;
1101 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1102 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1103 fixed_source_waypoint.wpfire = 1;
1104 fixed_source_waypoint.enemy = NULL;
1108 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1109 // as this search is expensive we will use lower values if the bot is on the air
1110 float increment, maxdistance;
1111 if(IS_ONGROUND(this))
1114 maxdistance = 50000;
1122 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1125 bool searching = true;
1129 IL_EACH(g_waypoints, it.wpfire,
1134 p = it.wpnearestpoint;
1136 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1166 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1167 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1168 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1173 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1174 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1178 IL_EACH(g_waypoints, true,
1180 it.wpconsidered = false;
1181 it.wpnearestpoint = '0 0 0';
1182 it.wpcost = 10000000;
1187 if(fixed_source_waypoint)
1189 fixed_source_waypoint.wpconsidered = true;
1190 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1191 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1192 fixed_source_waypoint.wpfire = 1;
1193 fixed_source_waypoint.enemy = NULL;
1197 error("need to start with a waypoint\n");
1200 bool searching = true;
1204 IL_EACH(g_waypoints, it.wpfire,
1208 cost = it.wpcost; // cost to walk from it to home
1209 p = it.wpnearestpoint;
1211 IL_EACH(g_waypoints, it != wp,
1213 if(!waypoint_islinked(it, wp))
1215 cost2 = cost + it.dmg;
1216 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1222 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1223 void navigation_routerating(entity this, entity e, float f, float rangebias)
1225 if (!e || e.blacklisted) { return; }
1227 rangebias = waypoint_getlinearcost(rangebias);
1228 f = waypoint_getlinearcost(f);
1232 bool rate_wps = false;
1233 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1234 || (e.flags & FL_PARTIALGROUND))
1241 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1242 int t = pointcontents(trace_endpos + '0 0 1');
1243 if(t != CONTENT_SOLID )
1245 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1247 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1254 entity theEnemy = e;
1255 entity best_wp = NULL;
1256 float best_dist = FLOAT_MAX;
1257 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1258 && vdist(it.origin - theEnemy.origin, <, 500)
1259 && vdist(it.origin - this.origin, >, 100)
1260 && vdist(it.origin - this.origin, <, 10000),
1262 float dist = vlen2(it.origin - theEnemy.origin);
1263 if (dist < best_dist)
1275 vector goal_org = (e.absmin + e.absmax) * 0.5;
1277 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1279 // Evaluate path using jetpack
1280 if(this.items & IT_JETPACK)
1281 if(autocvar_bot_ai_navigation_jetpack)
1282 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1284 vector pointa, pointb;
1286 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1289 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1290 pointa = trace_endpos - '0 0 1';
1293 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1294 pointb = trace_endpos - '0 0 1';
1296 // Can I see these two points from the sky?
1297 traceline(pointa, pointb, MOVE_NORMAL, this);
1299 if(trace_fraction==1)
1301 LOG_DEBUG("jetpack ai: can bridge these two points");
1303 // Lower the altitude of these points as much as possible
1304 float zdistance, xydistance, cost, t, fuel;
1305 vector down, npa, npb;
1307 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1310 npa = pointa + down;
1311 npb = pointb + down;
1313 if(npa.z<=this.absmax.z)
1316 if(npb.z<=e.absmax.z)
1319 traceline(npa, npb, MOVE_NORMAL, this);
1320 if(trace_fraction==1)
1326 while(trace_fraction == 1);
1329 // Rough estimation of fuel consumption
1330 // (ignores acceleration and current xyz velocity)
1331 xydistance = vlen(pointa - pointb);
1332 zdistance = fabs(pointa.z - this.origin.z);
1334 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1335 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1336 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1338 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResource(this, RES_FUEL)));
1341 if(GetResource(this, RES_FUEL) > fuel || (this.items & IT_UNLIMITED_AMMO))
1344 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1345 // - between air and ground speeds)
1347 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1348 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1351 // Compare against other goals
1352 f = f * rangebias / (rangebias + cost);
1354 if (navigation_bestrating < f)
1356 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1357 navigation_bestrating = f;
1358 navigation_bestgoal = e;
1359 this.navigation_jetpack_goal = e;
1360 this.navigation_jetpack_point = pointb;
1368 //te_wizspike(e.origin);
1371 // update the cached spawnfunc_waypoint link on a dynamic item entity
1372 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1378 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1379 e.nearestwaypoint = NULL;
1381 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1382 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1384 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1385 e.nearestwaypoint = nwp = e.goalcurrent;
1387 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1390 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1392 if(!e.navigation_dynamicgoal)
1393 e.blacklisted = true;
1397 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1402 if(e.navigation_dynamicgoal)
1403 e.nearestwaypointtimeout = time + 2;
1404 else if(autocvar_g_waypointeditor)
1405 e.nearestwaypointtimeout = time + 3 + random() * 2;
1407 nwp = e.nearestwaypoint;
1410 if (nwp && nwp.wpcost < 10000000)
1412 //te_wizspike(nwp.wpnearestpoint);
1413 float nwptoitem_cost = 0;
1414 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1415 nwptoitem_cost = nwp.wp00mincost;
1417 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1418 float cost = nwp.wpcost + nwptoitem_cost;
1419 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1420 f = f * rangebias / (rangebias + cost);
1421 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1422 if (navigation_bestrating < f)
1424 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1425 navigation_bestrating = f;
1426 navigation_bestgoal = e;
1431 // adds an item to the the goal stack with the path to a given item
1432 bool navigation_routetogoal(entity this, entity e, vector startposition)
1434 // if there is no goal, just exit
1438 entity teleport_goal = NULL;
1440 this.goalentity = e;
1442 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1444 // force teleport destination as route destination
1446 navigation_pushroute(this, e.wp00);
1447 this.goalentity = e.wp00;
1450 // put the entity on the goal stack
1451 //print("routetogoal ", etos(e), "\n");
1452 navigation_pushroute(this, e);
1455 e = this.goalentity;
1457 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1459 this.wp_goal_prev1 = this.wp_goal_prev0;
1460 this.wp_goal_prev0 = e;
1464 if(e==this.navigation_jetpack_goal)
1467 // if it can reach the goal there is nothing more to do
1468 set_tracewalk_dest(e, startposition, true);
1469 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1470 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1471 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1476 entity nearest_wp = NULL;
1477 // see if there are waypoints describing a path to the item
1478 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1480 e = e.nearestwaypoint;
1483 else if(teleport_goal)
1486 e = e.enemy; // we already have added it, so...
1491 if(nearest_wp && nearest_wp.enemy && !(nearest_wp.enemy.wpflags & WPFLAGMASK_NORELINK))
1493 // often path can be optimized by not adding the nearest waypoint
1494 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1496 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1498 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1499 e = nearest_wp.enemy;
1502 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1503 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1504 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1505 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1506 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1508 e = nearest_wp.enemy;
1515 // NOTE unlike waypoints, items hold incoming links
1516 navigation_item_initlinks_ifneeded(this.goalentity);
1517 int link_num = navigation_item_getlinknum(this.goalentity, nearest_wp.enemy);
1520 if (navigation_item_iswalkablelink(this.goalentity, link_num))
1521 e = nearest_wp.enemy;
1523 else // untested link
1525 entity wp = nearest_wp.enemy;
1526 entity goal = this.goalentity;
1527 bool walkable = false;
1528 if (checkpvs(wp.origin, goal))
1530 set_tracewalk_dest(goal, wp.origin, false);
1531 if (vdist(tracewalk_dest - wp.origin, <, 1050)
1532 && tracewalk(goal, wp.origin, PL_MIN_CONST, PL_MAX_CONST,
1533 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1536 e = nearest_wp.enemy;
1539 navigation_item_add_link(wp, goal, walkable);
1546 // add the spawnfunc_waypoint to the path
1547 navigation_pushroute(this, e);
1557 // shorten path by removing intermediate goals
1558 bool navigation_shortenpath(entity this)
1560 if (!this.goalstack01 || wasfreed(this.goalstack01))
1562 if (this.bot_tracewalk_time > time)
1564 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1566 bool cut_allowed = false;
1567 entity next = this.goalentity;
1568 // evaluate whether bot can discard current route and chase directly a player, trying to
1569 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1570 if (IS_MOVABLE(next))
1572 set_tracewalk_dest(next, this.origin, true);
1573 if (vdist(this.origin - tracewalk_dest, <, 200))
1575 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1576 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1577 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1578 && checkpvs(this.origin + this.view_ofs, next))
1580 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1584 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1585 while (deviation.y < -180) deviation.y += 360;
1586 while (deviation.y > 180) deviation.y -= 360;
1587 if (fabs(deviation.y) > 25)
1593 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1594 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1596 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1599 navigation_poproute(this);
1601 while (this.goalcurrent != next);
1608 next = this.goalstack01;
1609 // if for some reason the bot is closer to the next goal, pop the current one
1610 if (!IS_MOVABLE(next) && !(this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP))
1611 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1612 && checkpvs(this.origin + this.view_ofs, next))
1614 set_tracewalk_dest(next, this.origin, true);
1620 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1621 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1623 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1624 navigation_poproute(this);
1631 // removes any currently touching waypoints from the goal stack
1632 // (this is how bots detect if they reached a goal)
1633 int navigation_poptouchedgoals(entity this)
1635 int removed_goals = 0;
1637 if(!this.goalcurrent)
1638 return removed_goals;
1640 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1642 if (!this.goalcurrent.wpisbox // warpzone
1643 && vlen2(this.origin - this.goalstack01.origin) < vlen2(this.origin - this.goalcurrent.origin))
1645 // immediately remove origin and destination waypoints
1646 navigation_poproute(this);
1648 navigation_poproute(this);
1650 this.lastteleporttime = 0;
1653 // make sure jumppad is really hit, don't rely on distance based checks
1654 // as they may report a touch even if it didn't really happen
1655 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1657 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1658 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1660 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1661 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1663 if(this.jumppadcount)
1665 // remove jumppad waypoint after a random delay to prevent bots getting
1666 // stuck on certain jumppads that require an extra initial horizontal speed
1667 float max_delay = 0.1;
1668 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1670 if (time - this.lastteleporttime < random() * max_delay)
1671 return removed_goals;
1673 else if (this.goalcurrent.wpisbox) // teleport
1675 // immediately remove origin and destination waypoints
1676 navigation_poproute(this);
1679 navigation_poproute(this);
1680 this.lastteleporttime = 0;
1683 return removed_goals;
1685 else if (this.lastteleporttime > 0)
1687 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1688 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1689 // if so immediately fix bot path by removing skipped goals
1690 entity tele_ent = NULL;
1691 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1692 tele_ent = this.goalstack01;
1693 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1694 tele_ent = this.goalstack02;
1695 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1696 tele_ent = this.goalstack03;
1697 if (tele_ent && TELEPORT_USED(this, tele_ent))
1699 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1700 if ((tele_ent.wpflags & WAYPOINTFLAG_PERSONAL) && tele_ent.owner == this)
1702 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1703 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1705 while (this.goalcurrent != tele_ent)
1707 navigation_poproute(this);
1710 navigation_poproute(this);
1711 this.lastteleporttime = 0;
1713 return removed_goals;
1715 // reset of lastteleporttime can be overriden by a jumppad when it's set
1716 // in more than one frame: make sure it's reset
1717 this.lastteleporttime = 0;
1720 // Loose goal touching check when running
1721 // check goalstack01 to make sure waypoint isn't the final goal
1722 if((this.aistatus & AI_STATUS_RUNNING) && this.goalcurrent.classname == "waypoint" && !(this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP)
1723 && this.goalstack01 && !wasfreed(this.goalstack01) && vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed))
1725 vector gco = this.goalcurrent.origin;
1726 float min_dist = BOT_BUNNYHOP_WP_DETECTION_RANGE;
1727 // also detect waypoints when bot is way above them but with a narrower horizontal range
1728 // so to increase chances bot ends up in the standard range (optimizes nearest waypoint finding)
1729 if(vdist(this.origin - gco, <, min_dist)
1730 || (vdist(vec2(this.origin - gco), <, min_dist * 0.5) && vdist(this.origin - eZ * 1.5 * min_dist - gco, <, min_dist)))
1732 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1733 if(trace_fraction==1)
1735 // Detect personal waypoints
1736 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1737 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1739 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1740 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1743 navigation_poproute(this);
1745 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1746 return removed_goals;
1751 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1753 vector gc_min = this.goalcurrent.absmin;
1754 vector gc_max = this.goalcurrent.absmax;
1755 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1757 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1758 gc_max = this.goalcurrent.origin + '1 1 1' * 12 + eZ * (jumpheight_vec.z + STAT(PL_MIN, this).z);
1760 if (this.ladder_entity)
1762 if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1767 if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1771 // Detect personal waypoints
1772 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1773 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1775 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1776 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1779 navigation_poproute(this);
1781 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1782 return removed_goals;
1784 return removed_goals;
1787 entity navigation_get_really_close_waypoint(entity this)
1789 entity wp = this.goalcurrent;
1791 wp = this.goalcurrent_prev;
1794 float min_dist = ((this.aistatus & AI_STATUS_RUNNING) ? BOT_BUNNYHOP_WP_DETECTION_RANGE : 50);
1795 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, min_dist))
1797 wp = this.goalcurrent_prev;
1801 if(wp.classname != "waypoint")
1803 wp = wp.nearestwaypoint;
1807 if(vdist(wp.origin - this.origin, >, min_dist))
1810 IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
1812 if(vdist(it.origin - this.origin, <, min_dist))
1821 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1824 set_tracewalk_dest(wp, this.origin, false);
1825 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1826 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1833 // begin a goal selection session (queries spawnfunc_waypoint network)
1834 void navigation_goalrating_start(entity this)
1836 if(this.aistatus & AI_STATUS_STUCK)
1839 this.navigation_jetpack_goal = NULL;
1840 navigation_bestrating = -1;
1841 entity wp = navigation_get_really_close_waypoint(this);
1842 navigation_clearroute(this);
1843 navigation_bestgoal = NULL;
1844 navigation_markroutes(this, wp);
1845 this.goalstack31 = wp; // temporarly save the really close waypoint
1848 // ends a goal selection session (updates goal stack to the best goal)
1849 void navigation_goalrating_end(entity this)
1851 if(this.aistatus & AI_STATUS_STUCK)
1854 entity wp = this.goalstack31; // save to wp as this.goalstack31 is set by navigation_routetogoal
1855 this.goalstack31 = NULL;
1857 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1858 LOG_DEBUG("best goal ", navigation_bestgoal.classname);
1860 if (wp && this.goalcurrent == wp)
1861 navigation_poproute(this);
1863 // If the bot got stuck then try to reach the farthest waypoint
1864 if (!this.goalentity)
1866 if (autocvar_bot_wander_enable && !(this.aistatus & AI_STATUS_STUCK))
1868 LOG_DEBUG(this.netname, " cannot walk to any goal");
1869 this.aistatus |= AI_STATUS_STUCK;
1871 this.goalentity_shouldbefrozen = false;
1874 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1877 void botframe_updatedangerousobjects(float maxupdate)
1879 vector m1, m2, v, o;
1883 IL_EACH(g_waypoints, true,
1889 IL_EACH(g_bot_dodge, it.bot_dodge,
1892 v.x = bound(m1_x, v.x, m2_x);
1893 v.y = bound(m1_y, v.y, m2_y);
1894 v.z = bound(m1_z, v.z, m2_z);
1895 o = (it.absmin + it.absmax) * 0.5;
1896 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1899 traceline(o, v, true, NULL);
1900 if (trace_fraction == 1)
1901 danger = danger + d;
1911 void navigation_unstuck(entity this)
1913 if (!autocvar_bot_wander_enable)
1916 bool has_user_waypoints = false;
1917 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1919 has_user_waypoints = true;
1922 if (!has_user_waypoints)
1925 float search_radius = 1000;
1927 if (!bot_waypoint_queue_owner)
1929 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1930 bot_waypoint_queue_owner = this;
1931 bot_waypoint_queue_bestgoal = NULL;
1932 bot_waypoint_queue_bestgoalrating = 0;
1935 if(bot_waypoint_queue_owner!=this)
1938 if (bot_waypoint_queue_goal)
1940 // evaluate the next goal on the queue
1941 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1942 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with squared distance ", ftos(d));
1943 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1944 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1945 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1947 if( d > bot_waypoint_queue_bestgoalrating)
1949 bot_waypoint_queue_bestgoalrating = d;
1950 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1954 // move to a random waypoint while bot is searching for a walkable path;
1955 // this is usually sufficient to unstuck bots from bad spots or when other
1956 // bots of the same team block all their ways
1957 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1959 navigation_clearroute(this);
1960 navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1961 navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1964 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1966 if (!bot_waypoint_queue_goal)
1968 if (bot_waypoint_queue_bestgoal)
1970 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1971 navigation_clearroute(this);
1972 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1973 navigation_goalrating_timeout_set(this);
1974 this.aistatus &= ~AI_STATUS_STUCK;
1978 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1981 bot_waypoint_queue_owner = NULL;
1986 if(bot_strategytoken!=this)
1989 // build a new queue
1990 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1992 entity first = NULL;
1994 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1996 if(bot_waypoint_queue_goal)
1997 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
2001 bot_waypoint_queue_goal = it;
2002 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
2006 bot_waypoint_queue_goal = first;
2009 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
2010 bot_waypoint_queue_owner = NULL;
2015 // Support for debugging tracewalk visually
2017 void debugresetnodes()
2019 debuglastnode = '0 0 0';
2022 void debugnode(entity this, vector node)
2024 if (!IS_PLAYER(this))
2027 if(debuglastnode=='0 0 0')
2029 debuglastnode = node;
2033 te_lightning2(NULL, node, debuglastnode);
2034 debuglastnode = node;
2037 void debugnodestatus(vector position, float status)
2043 case DEBUG_NODE_SUCCESS:
2046 case DEBUG_NODE_WARNING:
2049 case DEBUG_NODE_FAIL:
2056 te_customflash(position, 40, 2, c);
2059 // Support for debugging the goal stack visually
2062 .vector lastposition;
2064 // Debug the goal stack visually
2065 void debuggoalstack(entity this)
2070 if(this.goalcounter==0)goal=this.goalcurrent;
2071 else if(this.goalcounter==1)goal=this.goalstack01;
2072 else if(this.goalcounter==2)goal=this.goalstack02;
2073 else if(this.goalcounter==3)goal=this.goalstack03;
2074 else if(this.goalcounter==4)goal=this.goalstack04;
2075 else if(this.goalcounter==5)goal=this.goalstack05;
2076 else if(this.goalcounter==6)goal=this.goalstack06;
2077 else if(this.goalcounter==7)goal=this.goalstack07;
2078 else if(this.goalcounter==8)goal=this.goalstack08;
2079 else if(this.goalcounter==9)goal=this.goalstack09;
2080 else if(this.goalcounter==10)goal=this.goalstack10;
2081 else if(this.goalcounter==11)goal=this.goalstack11;
2082 else if(this.goalcounter==12)goal=this.goalstack12;
2083 else if(this.goalcounter==13)goal=this.goalstack13;
2084 else if(this.goalcounter==14)goal=this.goalstack14;
2085 else if(this.goalcounter==15)goal=this.goalstack15;
2086 else if(this.goalcounter==16)goal=this.goalstack16;
2087 else if(this.goalcounter==17)goal=this.goalstack17;
2088 else if(this.goalcounter==18)goal=this.goalstack18;
2089 else if(this.goalcounter==19)goal=this.goalstack19;
2090 else if(this.goalcounter==20)goal=this.goalstack20;
2091 else if(this.goalcounter==21)goal=this.goalstack21;
2092 else if(this.goalcounter==22)goal=this.goalstack22;
2093 else if(this.goalcounter==23)goal=this.goalstack23;
2094 else if(this.goalcounter==24)goal=this.goalstack24;
2095 else if(this.goalcounter==25)goal=this.goalstack25;
2096 else if(this.goalcounter==26)goal=this.goalstack26;
2097 else if(this.goalcounter==27)goal=this.goalstack27;
2098 else if(this.goalcounter==28)goal=this.goalstack28;
2099 else if(this.goalcounter==29)goal=this.goalstack29;
2100 else if(this.goalcounter==30)goal=this.goalstack30;
2101 else if(this.goalcounter==31)goal=this.goalstack31;
2106 this.goalcounter = 0;
2107 this.lastposition='0 0 0';
2111 if(this.lastposition=='0 0 0')
2114 org = this.lastposition;
2117 go = ( goal.absmin + goal.absmax ) * 0.5;
2118 te_lightning2(NULL, org, go);
2119 this.lastposition = go;