1 #include "navigation.qh"
3 #include <common/constants.qh>
4 #include <common/items/_mod.qh>
5 #include <common/mapobjects/func/ladder.qh>
6 #include <common/mapobjects/trigger/hurt.qh>
7 #include <common/mapobjects/trigger/jumppads.qh>
8 #include <common/net_linked.qh>
9 #include <common/stats.qh>
10 #include <common/weapons/_all.qh>
11 #include <server/bot/api.qh>
12 #include <server/bot/default/bot.qh>
13 #include <server/bot/default/cvars.qh>
14 #include <server/bot/default/waypoints.qh>
15 #include <server/items/items.qh>
19 void navigation_goalrating_timeout_set(entity this)
21 if(IS_MOVABLE(this.goalentity))
22 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
24 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
27 // use this when current goal must be discarded immediately
28 void navigation_goalrating_timeout_force(entity this)
30 navigation_goalrating_timeout_expire(this, 0);
33 // use this when current goal can be kept for a short while to increase the chance
34 // of bot touching a waypoint, which helps to find a new goal more efficiently
35 void navigation_goalrating_timeout_expire(entity this, float seconds)
38 this.bot_strategytime = 0;
39 else if (this.bot_strategytime > time + seconds)
40 this.bot_strategytime = time + seconds;
43 bool navigation_goalrating_timeout(entity this)
45 return this.bot_strategytime < time;
49 void navigation_goalrating_timeout_extend_if_needed(entity this, float seconds)
51 this.bot_strategytime = max(this.bot_strategytime, time + seconds);
54 #define MAX_CHASE_DISTANCE 700
55 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
57 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
58 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
59 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
64 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
66 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
68 this.ignoregoal = this.goalentity;
69 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
76 void navigation_dynamicgoal_init(entity this, bool initially_static)
78 this.navigation_dynamicgoal = true;
79 this.bot_basewaypoint = this.nearestwaypoint;
81 this.nearestwaypointtimeout = -1;
83 this.nearestwaypointtimeout = time;
86 void navigation_dynamicgoal_set(entity this, entity dropper)
88 this.nearestwaypointtimeout = time;
89 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
90 this.nearestwaypoint = dropper.nearestwaypoint;
91 if (this.nearestwaypoint)
92 this.nearestwaypointtimeout += 2;
95 void navigation_dynamicgoal_unset(entity this)
97 if(this.bot_basewaypoint)
98 this.nearestwaypoint = this.bot_basewaypoint;
99 this.nearestwaypointtimeout = -1;
102 // returns point of ent closer to org
103 vector get_closer_dest(entity ent, vector org)
105 vector dest = '0 0 0';
106 if ((ent.classname != "waypoint") || ent.wpisbox)
108 vector wm1 = ent.origin + ent.mins;
109 vector wm2 = ent.origin + ent.maxs;
110 dest.x = bound(wm1.x, org.x, wm2.x);
111 dest.y = bound(wm1.y, org.y, wm2.y);
112 dest.z = bound(wm1.z, org.z, wm2.z);
119 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
121 if ((ent.classname != "waypoint") || ent.wpisbox)
123 vector wm1 = ent.origin + ent.mins;
124 vector wm2 = ent.origin + ent.maxs;
125 if (IS_PLAYER(ent) || IS_MONSTER(ent))
127 // move destination point out of player bbox otherwise tracebox always fails
128 // (if bot_navigation_ignoreplayers is false)
129 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
130 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
132 // set destination point to x and y coords of ent that are closer to org
133 // z coord is set to ent's min height
134 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
135 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
136 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
137 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
139 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
140 tracewalk_dest_height = 0;
141 fix_player_dest = false;
145 tracewalk_dest.z = wm1.z;
146 tracewalk_dest_height = wm2.z - wm1.z;
151 tracewalk_dest = ent.origin;
152 tracewalk_dest_height = 0;
154 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
156 // snap player to the ground
157 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
159 // bot is right under the player
160 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
161 tracewalk_dest = trace_endpos;
162 tracewalk_dest_height = 0;
166 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
167 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
169 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
170 tracewalk_dest.z = trace_endpos.z;
176 // returns point of ent closer to org
177 vector set_tracewalk_dest_2(entity ent, vector org)
179 vector closer_dest = '0 0 0';
180 if ((ent.classname != "waypoint") || ent.wpisbox)
182 vector wm1 = ent.origin + ent.mins;
183 vector wm2 = ent.origin + ent.maxs;
184 closer_dest.x = bound(wm1.x, org.x, wm2.x);
185 closer_dest.y = bound(wm1.y, org.y, wm2.y);
186 closer_dest.z = bound(wm1.z, org.z, wm2.z);
187 // set destination point to x and y coords of ent that are closer to org
188 // z coord is set to ent's min height
189 tracewalk_dest.x = closer_dest.x;
190 tracewalk_dest.y = closer_dest.y;
191 tracewalk_dest.z = wm1.z;
192 tracewalk_dest_height = wm2.z - wm1.z; // destination height
196 closer_dest = ent.origin;
197 tracewalk_dest = closer_dest;
198 tracewalk_dest_height = 0;
203 bool navigation_check_submerged_state(entity ent, vector pos)
207 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
208 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
209 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
212 submerged = SUBMERGED(pos);
213 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
214 // is actually submerged because often they are inside some solid.
215 // That's why submerged state is saved now that we know current pos is
216 // not stuck in solid (previous tracewalk call to this pos was successfully)
217 if(!ent.navigation_dynamicgoal)
218 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
223 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
225 IL_EACH(g_ladders, true,
228 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
229 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
232 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
233 tracebox(org, m1, m2, top, movemode, e);
234 if(trace_fraction == 1)
241 // Unfortuntely we can't use trace_inwater since it doesn't hold the fraction of the total
242 // distance that was traveled before impact as the description in the engine (collision.h) says.
243 // It would have helped to speed up tracewalk underwater
244 vector resurface_limited(vector org, float lim, vector m1)
246 if (WETFEET(org + eZ * (lim - org.z)))
250 float RES_min_h = org.z;
251 float RES_max_h = lim;
253 org.z = 0.5 * (RES_min_h + RES_max_h);
258 } while (RES_max_h - RES_min_h >= 1);
263 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
266 #define NAV_SWIM_ONWATER 1
267 #define NAV_SWIM_UNDERWATER 2
269 // rough simulation of walking from one point to another to test if a path
270 // can be traveled, used for waypoint linking and havocbot
271 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
272 // INFO: the command sv_cmd trace walk is useful to test this function in game
273 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
275 if(autocvar_bot_debug_tracewalk)
282 vector flatdir = end - start;
284 float flatdist = vlen(flatdir);
285 flatdir = normalize(flatdir);
287 bool ignorehazards = false;
290 // Analyze starting point
292 ignorehazards = true;
294 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
295 if (trace_startsolid)
298 if(autocvar_bot_debug_tracewalk)
299 debugnodestatus(start, DEBUG_NODE_FAIL);
301 //print("tracewalk: ", vtos(start), " is a bad start\n");
307 end2.z += end_height;
309 vector fixed_end = end;
312 if (flatdist > 0 && WETFEET(org))
315 nav_action = NAV_SWIM_UNDERWATER;
318 // tracebox down by player's height
319 // useful to know if water level is so low that bot can still walk
320 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
321 if (SUBMERGED(trace_endpos))
324 nav_action = NAV_SWIM_UNDERWATER;
327 nav_action = NAV_WALK;
331 nav_action = NAV_WALK;
339 if (org.z > end2.z + 1)
341 tracebox(org, m1, m2, end2, movemode, e);
343 if (org.z > end2.z + 1)
346 else if (org.z < end.z - 1)
348 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
349 if (SUBMERGED(trace_endpos))
351 vector v = trace_endpos;
352 tracebox(v, m1, m2, end, movemode, e);
353 if(trace_endpos.z >= end.z - 1)
355 RESURFACE_LIMITED(v, trace_endpos.z);
359 else if (trace_endpos.z > org.z - jumpheight_vec.z)
360 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
362 if (org.z < end.z - 1)
369 if(autocvar_bot_debug_tracewalk)
372 debugnodestatus(org, DEBUG_NODE_SUCCESS);
375 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
380 if(autocvar_bot_debug_tracewalk)
386 if (stepdist > flatdist)
388 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
390 // can't use movement direction here to calculate move because of
391 // precision errors especially when direction has a high enough z value
392 //water_dir = normalize(water_end - org);
393 //move = org + water_dir * stepdist;
394 fixed_end.z = bound(end.z, org.z, end2.z);
395 if (stepdist == flatdist) {
399 move = org + (fixed_end - org) * (stepdist / flatdist);
400 flatdist = vlen(vec2(fixed_end - move));
403 else // horiz. direction
405 flatdist -= stepdist;
406 move = org + flatdir * stepdist;
409 if(nav_action == NAV_SWIM_ONWATER)
411 tracebox(org, m1, m2, move, movemode, e); // swim
414 if (trace_fraction < 1)
417 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
419 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
422 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
424 if(autocvar_bot_debug_tracewalk)
427 debugnodestatus(org, DEBUG_NODE_SUCCESS);
430 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
434 if(autocvar_bot_debug_tracewalk)
435 debugnodestatus(org, DEBUG_NODE_FAIL);
438 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
449 if (org.z <= move.z) // going horiz.
451 tracebox(trace_endpos, m1, m2, move, movemode, e);
453 nav_action = NAV_WALK;
458 if (org.z <= move.z) // going horiz.
461 nav_action = NAV_SWIM_ONWATER;
467 nav_action = NAV_SWIM_UNDERWATER;
469 nav_action = NAV_SWIM_ONWATER;
472 else if(nav_action == NAV_SWIM_UNDERWATER)
474 if (move.z >= org.z) // swimming upwards or horiz.
476 tracebox(org, m1, m2, move, movemode, e); // swim
478 bool stepswum = false;
481 if (trace_fraction < 1)
484 vector stepswim_move = move + stepheightvec;
485 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
486 stepswim_move.z = end2.z;
488 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
491 if (trace_startsolid)
493 if(autocvar_bot_debug_tracewalk)
494 debugnodestatus(org, DEBUG_NODE_FAIL);
496 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
500 if (trace_fraction < 1)
502 float org_z_prev = org.z;
503 RESURFACE_LIMITED(org, end2.z);
504 if(org.z == org_z_prev)
506 if(autocvar_bot_debug_tracewalk)
507 debugnodestatus(org, DEBUG_NODE_FAIL);
509 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
513 nav_action = NAV_SWIM_UNDERWATER;
515 nav_action = NAV_SWIM_ONWATER;
517 // we didn't advance horiz. in this step, flatdist decrease should be reverted
518 // but we can't do it properly right now... apply this workaround instead
536 if (!WETFEET(trace_endpos))
538 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
539 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
540 if (!stepswum && SUBMERGED(trace_endpos))
542 RESURFACE_LIMITED(trace_endpos, end2.z);
544 nav_action = NAV_SWIM_ONWATER;
550 nav_action = NAV_WALK;
556 nav_action = NAV_SWIM_UNDERWATER;
559 else //if (move.z < org.z) // swimming downwards
561 tracebox(org, m1, m2, move, movemode, e); // swim
564 if (trace_fraction < 1)
567 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
570 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
572 if(autocvar_bot_debug_tracewalk)
573 debugnodestatus(move, DEBUG_NODE_FAIL);
575 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
587 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
589 // stepswim caused upwards direction
590 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
591 if (!SUBMERGED(trace_endpos))
594 nav_action = NAV_WALK;
601 nav_action = NAV_SWIM_UNDERWATER;
605 else if(nav_action == NAV_WALK)
608 tracebox(org, m1, m2, move, movemode, e);
610 if(autocvar_bot_debug_tracewalk)
611 debugnode(e, trace_endpos);
614 if (trace_fraction < 1)
616 // check if we can walk over this obstacle, possibly by jumpstepping
617 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
618 if (trace_fraction < 1 || trace_startsolid)
620 if (trace_startsolid) // hit ceiling above org
622 // reduce stepwalk height
623 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
624 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
626 else //if (trace_fraction < 1)
628 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
629 if (trace_startsolid) // hit ceiling above org
631 // reduce jumpstepwalk height
632 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
633 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
637 if (trace_fraction < 1)
639 vector v = trace_endpos;
640 v.z = org.z + jumpheight_vec.z;
641 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
643 if(autocvar_bot_debug_tracewalk)
646 debugnodestatus(v, DEBUG_NODE_SUCCESS);
649 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
653 if(autocvar_bot_debug_tracewalk)
654 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
656 traceline( org, move, movemode, e);
658 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
662 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
664 nextmove = move + (flatdir * stepdist);
665 traceline( move, nextmove, movemode, e);
668 flatdist = vlen(vec2(end - move));
672 if(autocvar_bot_debug_tracewalk)
673 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
675 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
676 //te_explosion(trace_endpos);
677 //print(ftos(e.dphitcontentsmask), "\n");
678 return false; // failed
690 // trace down from stepheight as far as possible and move there,
691 // if this starts in solid we try again without the stepup, and
692 // if that also fails we assume it is a wall
693 // (this is the same logic as the Quake walkmove function used)
694 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
702 if(autocvar_bot_debug_tracewalk)
704 debugnode(e, trace_endpos);
705 debugnodestatus(org, DEBUG_NODE_FAIL);
708 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
715 if(move.z >= end2.z && org.z < end2.z)
720 if(org.z > move.z - 1 || !SUBMERGED(org))
722 nav_action = NAV_WALK;
726 // ended up submerged while walking
727 if(autocvar_bot_debug_tracewalk)
730 RESURFACE_LIMITED(org, move.z);
731 nav_action = NAV_SWIM_ONWATER;
736 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
738 // moved but didn't arrive at the intended destination
739 if(autocvar_bot_debug_tracewalk)
740 debugnodestatus(org, DEBUG_NODE_FAIL);
745 /////////////////////////////////////////////////////////////////////////////
747 /////////////////////////////////////////////////////////////////////////////
749 // completely empty the goal stack, used when deciding where to go
750 void navigation_clearroute(entity this)
752 this.lastteleporttime = 0;
753 this.goalcurrent_prev = this.goalcurrent;
754 this.goalcurrent_distance_2d = FLOAT_MAX;
755 this.goalcurrent_distance_z = FLOAT_MAX;
756 this.goalcurrent_distance_time = 0;
757 this.goalentity_lock_timeout = 0;
758 this.goalentity_shouldbefrozen = false;
759 this.goalentity = NULL;
760 this.goalcurrent = NULL;
761 this.goalstack01 = NULL;
762 this.goalstack02 = NULL;
763 this.goalstack03 = NULL;
764 this.goalstack04 = NULL;
765 this.goalstack05 = NULL;
766 this.goalstack06 = NULL;
767 this.goalstack07 = NULL;
768 this.goalstack08 = NULL;
769 this.goalstack09 = NULL;
770 this.goalstack10 = NULL;
771 this.goalstack11 = NULL;
772 this.goalstack12 = NULL;
773 this.goalstack13 = NULL;
774 this.goalstack14 = NULL;
775 this.goalstack15 = NULL;
776 this.goalstack16 = NULL;
777 this.goalstack17 = NULL;
778 this.goalstack18 = NULL;
779 this.goalstack19 = NULL;
780 this.goalstack20 = NULL;
781 this.goalstack21 = NULL;
782 this.goalstack22 = NULL;
783 this.goalstack23 = NULL;
784 this.goalstack24 = NULL;
785 this.goalstack25 = NULL;
786 this.goalstack26 = NULL;
787 this.goalstack27 = NULL;
788 this.goalstack28 = NULL;
789 this.goalstack29 = NULL;
790 this.goalstack30 = NULL;
791 this.goalstack31 = NULL;
794 // add a new goal at the beginning of the stack
795 // (in other words: add a new prerequisite before going to the later goals)
796 // NOTE: when a waypoint is added, the WP gets pushed first, then the
797 // next-closest WP on the shortest path to the WP
798 // That means, if the stack overflows, the bot will know how to do the FIRST 32
799 // steps to the goal, and then recalculate the path.
800 void navigation_pushroute(entity this, entity e)
802 this.goalcurrent_prev = this.goalcurrent;
803 this.goalcurrent_distance_2d = FLOAT_MAX;
804 this.goalcurrent_distance_z = FLOAT_MAX;
805 this.goalcurrent_distance_time = 0;
806 //print("bot ", etos(this), " push ", etos(e), "\n");
807 if(this.goalstack31 == this.goalentity)
808 this.goalentity = NULL;
809 this.goalstack31 = this.goalstack30;
810 this.goalstack30 = this.goalstack29;
811 this.goalstack29 = this.goalstack28;
812 this.goalstack28 = this.goalstack27;
813 this.goalstack27 = this.goalstack26;
814 this.goalstack26 = this.goalstack25;
815 this.goalstack25 = this.goalstack24;
816 this.goalstack24 = this.goalstack23;
817 this.goalstack23 = this.goalstack22;
818 this.goalstack22 = this.goalstack21;
819 this.goalstack21 = this.goalstack20;
820 this.goalstack20 = this.goalstack19;
821 this.goalstack19 = this.goalstack18;
822 this.goalstack18 = this.goalstack17;
823 this.goalstack17 = this.goalstack16;
824 this.goalstack16 = this.goalstack15;
825 this.goalstack15 = this.goalstack14;
826 this.goalstack14 = this.goalstack13;
827 this.goalstack13 = this.goalstack12;
828 this.goalstack12 = this.goalstack11;
829 this.goalstack11 = this.goalstack10;
830 this.goalstack10 = this.goalstack09;
831 this.goalstack09 = this.goalstack08;
832 this.goalstack08 = this.goalstack07;
833 this.goalstack07 = this.goalstack06;
834 this.goalstack06 = this.goalstack05;
835 this.goalstack05 = this.goalstack04;
836 this.goalstack04 = this.goalstack03;
837 this.goalstack03 = this.goalstack02;
838 this.goalstack02 = this.goalstack01;
839 this.goalstack01 = this.goalcurrent;
840 this.goalcurrent = e;
843 // remove first goal from stack
844 // (in other words: remove a prerequisite for reaching the later goals)
845 // (used when a spawnfunc_waypoint is reached)
846 void navigation_poproute(entity this)
848 this.goalcurrent_prev = this.goalcurrent;
849 this.goalcurrent_distance_2d = FLOAT_MAX;
850 this.goalcurrent_distance_z = FLOAT_MAX;
851 this.goalcurrent_distance_time = 0;
852 //print("bot ", etos(this), " pop\n");
853 if(this.goalcurrent == this.goalentity)
855 this.goalentity = NULL;
856 this.goalentity_lock_timeout = 0;
858 this.goalcurrent = this.goalstack01;
859 this.goalstack01 = this.goalstack02;
860 this.goalstack02 = this.goalstack03;
861 this.goalstack03 = this.goalstack04;
862 this.goalstack04 = this.goalstack05;
863 this.goalstack05 = this.goalstack06;
864 this.goalstack06 = this.goalstack07;
865 this.goalstack07 = this.goalstack08;
866 this.goalstack08 = this.goalstack09;
867 this.goalstack09 = this.goalstack10;
868 this.goalstack10 = this.goalstack11;
869 this.goalstack11 = this.goalstack12;
870 this.goalstack12 = this.goalstack13;
871 this.goalstack13 = this.goalstack14;
872 this.goalstack14 = this.goalstack15;
873 this.goalstack15 = this.goalstack16;
874 this.goalstack16 = this.goalstack17;
875 this.goalstack17 = this.goalstack18;
876 this.goalstack18 = this.goalstack19;
877 this.goalstack19 = this.goalstack20;
878 this.goalstack20 = this.goalstack21;
879 this.goalstack21 = this.goalstack22;
880 this.goalstack22 = this.goalstack23;
881 this.goalstack23 = this.goalstack24;
882 this.goalstack24 = this.goalstack25;
883 this.goalstack25 = this.goalstack26;
884 this.goalstack26 = this.goalstack27;
885 this.goalstack27 = this.goalstack28;
886 this.goalstack28 = this.goalstack29;
887 this.goalstack29 = this.goalstack30;
888 this.goalstack30 = this.goalstack31;
889 this.goalstack31 = NULL;
892 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
893 // waypoint destination coordinates instead of v (only useful for box waypoints)
894 // for normal waypoints v2 == v and v2_height == 0
895 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)
897 if (vdist(v - org, <, bestdist))
899 traceline(v, org, true, ent);
900 if (trace_fraction == 1)
904 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
909 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
917 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
918 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
921 ent = ent.tag_entity;
923 vector pm1 = ent.origin + ent.mins;
924 vector pm2 = ent.origin + ent.maxs;
926 if (waypointeditor_enabled && !IS_BOT_CLIENT(ent))
928 // this code allows removing waypoints in the air and seeing jumppad/telepport waypoint links
929 // FIXME it causes a bug where a waypoint spawned really close to another one (max 16 qu)
930 // isn't detected as the nearest waypoint
931 IL_EACH(g_waypoints, it != ent && it != except,
933 if (boxesoverlap(pm1, pm2, it.absmin, it.absmax))
939 // do two scans, because box test is cheaper
940 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
942 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
944 if(walkfromwp && !ent.navigation_dynamicgoal)
945 waypoint_clearlinks(ent); // initialize wpXXmincost fields
951 vector org = ent.origin;
952 if (navigation_testtracewalk)
958 if(ent.size && !IS_PLAYER(ent))
960 org += 0.5 * (ent.mins + ent.maxs);
961 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
964 // box check failed, try walk
965 IL_EACH(g_waypoints, it != ent,
967 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
973 set_tracewalk_dest(ent, v, true);
974 if (trace_ent == ent)
982 set_tracewalk_dest(it, org, false);
984 if (navigation_waypoint_will_link(v, org, ent,
985 tracewalk_dest, tracewalk_dest_height,
986 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
989 bestdist = vlen(tracewalk_dest - v);
991 bestdist = vlen(v - org);
995 if(!best && !ent.navigation_dynamicgoal)
997 int solid_save = ent.solid;
998 ent.solid = SOLID_BSP;
999 IL_EACH(g_jumppads, true,
1001 if(trigger_push_test(it, ent))
1003 best = it.nearestwaypoint;
1007 ent.solid = solid_save;
1011 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1013 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1014 if (autocvar_g_waypointeditor_auto)
1016 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1018 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1023 // finds the waypoints near the bot initiating a navigation query
1024 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1026 //navigation_testtracewalk = true;
1028 IL_EACH(g_waypoints, !it.wpconsidered,
1030 set_tracewalk_dest(it, this.origin, false);
1032 vector diff = tracewalk_dest - this.origin;
1033 diff.z = max(0, diff.z);
1034 if(vdist(diff, <, maxdist))
1036 it.wpconsidered = true;
1037 if (tracewalk(this, this.origin, this.mins, this.maxs,
1038 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1040 it.wpnearestpoint = tracewalk_dest;
1041 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1048 //navigation_testtracewalk = false;
1052 // updates a path link if a spawnfunc_waypoint link is better than the current one
1053 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1059 m1 = wp.origin + wp.mins;
1060 m2 = wp.origin + wp.maxs;
1061 v.x = bound(m1_x, p.x, m2_x);
1062 v.y = bound(m1_y, p.y, m2_y);
1063 v.z = bound(m1_z, p.z, m2_z);
1067 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1068 cost += w.wp00mincost; // assuming teleport has exactly one destination
1070 cost += waypoint_gettravelcost(p, v, w, wp);
1071 if (wp.wpcost > cost)
1076 wp.wpnearestpoint = v;
1080 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1081 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1086 IL_EACH(g_waypoints, true,
1088 it.wpconsidered = false;
1089 it.wpnearestpoint = '0 0 0';
1090 it.wpcost = 10000000;
1095 if(fixed_source_waypoint)
1097 fixed_source_waypoint.wpconsidered = true;
1098 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1099 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1100 fixed_source_waypoint.wpfire = 1;
1101 fixed_source_waypoint.enemy = NULL;
1105 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1106 // as this search is expensive we will use lower values if the bot is on the air
1107 float increment, maxdistance;
1108 if(IS_ONGROUND(this))
1111 maxdistance = 50000;
1119 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1122 bool searching = true;
1126 IL_EACH(g_waypoints, it.wpfire,
1131 p = it.wpnearestpoint;
1133 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1170 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1171 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1175 IL_EACH(g_waypoints, true,
1177 it.wpconsidered = false;
1178 it.wpnearestpoint = '0 0 0';
1179 it.wpcost = 10000000;
1184 if(fixed_source_waypoint)
1186 fixed_source_waypoint.wpconsidered = true;
1187 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1188 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1189 fixed_source_waypoint.wpfire = 1;
1190 fixed_source_waypoint.enemy = NULL;
1194 error("need to start with a waypoint\n");
1197 bool searching = true;
1201 IL_EACH(g_waypoints, it.wpfire,
1205 cost = it.wpcost; // cost to walk from it to home
1206 p = it.wpnearestpoint;
1208 IL_EACH(g_waypoints, it != wp,
1210 if(!waypoint_islinked(it, wp))
1212 cost2 = cost + it.dmg;
1213 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1219 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1220 void navigation_routerating(entity this, entity e, float f, float rangebias)
1222 if (!e || e.blacklisted) { return; }
1224 rangebias = waypoint_getlinearcost(rangebias);
1225 f = waypoint_getlinearcost(f);
1229 bool rate_wps = false;
1230 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1231 || (e.flags & FL_PARTIALGROUND))
1238 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1239 int t = pointcontents(trace_endpos + '0 0 1');
1240 if(t != CONTENT_SOLID )
1242 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1244 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1251 entity theEnemy = e;
1252 entity best_wp = NULL;
1253 float best_dist = FLOAT_MAX;
1254 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1255 && vdist(it.origin - theEnemy.origin, <, 500)
1256 && vdist(it.origin - this.origin, >, 100)
1257 && vdist(it.origin - this.origin, <, 10000),
1259 float dist = vlen2(it.origin - theEnemy.origin);
1260 if (dist < best_dist)
1272 vector goal_org = (e.absmin + e.absmax) * 0.5;
1274 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1276 // Evaluate path using jetpack
1277 if(this.items & IT_JETPACK)
1278 if(autocvar_bot_ai_navigation_jetpack)
1279 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1281 vector pointa, pointb;
1283 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1286 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1287 pointa = trace_endpos - '0 0 1';
1290 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1291 pointb = trace_endpos - '0 0 1';
1293 // Can I see these two points from the sky?
1294 traceline(pointa, pointb, MOVE_NORMAL, this);
1296 if(trace_fraction==1)
1298 LOG_DEBUG("jetpack ai: can bridge these two points");
1300 // Lower the altitude of these points as much as possible
1301 float zdistance, xydistance, cost, t, fuel;
1302 vector down, npa, npb;
1304 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1307 npa = pointa + down;
1308 npb = pointb + down;
1310 if(npa.z<=this.absmax.z)
1313 if(npb.z<=e.absmax.z)
1316 traceline(npa, npb, MOVE_NORMAL, this);
1317 if(trace_fraction==1)
1323 while(trace_fraction == 1);
1326 // Rough estimation of fuel consumption
1327 // (ignores acceleration and current xyz velocity)
1328 xydistance = vlen(pointa - pointb);
1329 zdistance = fabs(pointa.z - this.origin.z);
1331 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1332 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1333 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1335 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResource(this, RES_FUEL)));
1338 if(GetResource(this, RES_FUEL) > fuel || (this.items & IT_UNLIMITED_AMMO))
1341 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1342 // - between air and ground speeds)
1344 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1345 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1348 // Compare against other goals
1349 f = f * rangebias / (rangebias + cost);
1351 if (navigation_bestrating < f)
1353 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1354 navigation_bestrating = f;
1355 navigation_bestgoal = e;
1356 this.navigation_jetpack_goal = e;
1357 this.navigation_jetpack_point = pointb;
1365 //te_wizspike(e.origin);
1368 // update the cached spawnfunc_waypoint link on a dynamic item entity
1369 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1375 if(waypointeditor_enabled && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1376 e.nearestwaypoint = NULL;
1378 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1379 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1381 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1382 e.nearestwaypoint = nwp = e.goalcurrent;
1384 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1387 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1389 if(!e.navigation_dynamicgoal)
1390 e.blacklisted = true;
1394 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1399 if(e.navigation_dynamicgoal)
1400 e.nearestwaypointtimeout = time + 2;
1401 else if(waypointeditor_enabled)
1402 e.nearestwaypointtimeout = time + 3 + random() * 2;
1404 nwp = e.nearestwaypoint;
1407 if (nwp && nwp.wpcost < 10000000)
1409 //te_wizspike(nwp.wpnearestpoint);
1410 float nwptoitem_cost = 0;
1411 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1412 nwptoitem_cost = nwp.wp00mincost;
1414 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1415 float cost = nwp.wpcost + nwptoitem_cost;
1416 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1417 f = f * rangebias / (rangebias + cost);
1418 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1419 if (navigation_bestrating < f)
1421 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1422 navigation_bestrating = f;
1423 navigation_bestgoal = e;
1428 // adds an item to the the goal stack with the path to a given item
1429 bool navigation_routetogoal(entity this, entity e, vector startposition)
1431 // if there is no goal, just exit
1435 entity teleport_goal = NULL;
1437 this.goalentity = e;
1439 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1441 // force teleport destination as route destination
1443 navigation_pushroute(this, e.wp00);
1444 this.goalentity = e.wp00;
1447 // put the entity on the goal stack
1448 //print("routetogoal ", etos(e), "\n");
1449 navigation_pushroute(this, e);
1452 e = this.goalentity;
1454 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1456 this.wp_goal_prev1 = this.wp_goal_prev0;
1457 this.wp_goal_prev0 = e;
1460 if((this.items & IT_JETPACK) && e == this.navigation_jetpack_goal)
1463 // if it can reach the goal there is nothing more to do
1464 set_tracewalk_dest(e, startposition, true);
1465 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1466 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1467 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1472 entity nearest_wp = NULL;
1473 // see if there are waypoints describing a path to the item
1474 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1476 e = e.nearestwaypoint;
1479 else if(teleport_goal)
1482 e = e.enemy; // we already have added it, so...
1487 if(nearest_wp && nearest_wp.enemy && !(nearest_wp.enemy.wpflags & WPFLAGMASK_NORELINK))
1489 // often path can be optimized by not adding the nearest waypoint
1490 if (this.goalentity.navigation_dynamicgoal || waypointeditor_enabled)
1492 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1494 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1495 e = nearest_wp.enemy;
1498 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1499 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1500 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1501 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1502 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1504 e = nearest_wp.enemy;
1511 // NOTE unlike waypoints, items hold incoming links
1512 navigation_item_initlinks_ifneeded(this.goalentity);
1513 int link_num = navigation_item_getlinknum(this.goalentity, nearest_wp.enemy);
1516 if (navigation_item_iswalkablelink(this.goalentity, link_num))
1517 e = nearest_wp.enemy;
1519 else // untested link
1521 entity wp = nearest_wp.enemy;
1522 entity goal = this.goalentity;
1523 bool walkable = false;
1524 if (checkpvs(wp.origin, goal))
1526 set_tracewalk_dest(goal, wp.origin, false);
1527 if (vdist(tracewalk_dest - wp.origin, <, 1050)
1528 && tracewalk(goal, wp.origin, PL_MIN_CONST, PL_MAX_CONST,
1529 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1532 e = nearest_wp.enemy;
1535 navigation_item_add_link(wp, goal, walkable);
1542 // add the spawnfunc_waypoint to the path
1543 navigation_pushroute(this, e);
1553 // shorten path by removing intermediate goals
1554 bool navigation_shortenpath(entity this)
1556 if (!this.goalstack01 || wasfreed(this.goalstack01))
1558 if (this.bot_tracewalk_time > time)
1560 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1562 bool cut_allowed = false;
1563 entity next = this.goalentity;
1564 // evaluate whether bot can discard current route and chase directly a player, trying to
1565 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1566 if (IS_MOVABLE(next))
1568 set_tracewalk_dest(next, this.origin, true);
1569 if (vdist(this.origin - tracewalk_dest, <, 200))
1571 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1572 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1573 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1574 && checkpvs(this.origin + this.view_ofs, next))
1576 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1580 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1581 while (deviation.y < -180) deviation.y += 360;
1582 while (deviation.y > 180) deviation.y -= 360;
1583 if (fabs(deviation.y) > 25)
1589 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1590 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1592 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1595 navigation_poproute(this);
1597 while (this.goalcurrent != next);
1604 next = this.goalstack01;
1605 // if for some reason the bot is closer to the next goal, pop the current one
1606 if (!IS_MOVABLE(next) && !(this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP))
1607 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1608 && checkpvs(this.origin + this.view_ofs, next))
1610 set_tracewalk_dest(next, this.origin, true);
1616 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1617 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1619 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1620 navigation_poproute(this);
1627 // removes any currently touching waypoints from the goal stack
1628 // (this is how bots detect if they reached a goal)
1629 int navigation_poptouchedgoals(entity this)
1631 int removed_goals = 0;
1633 if(!this.goalcurrent)
1634 return removed_goals;
1636 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1638 if (!this.goalcurrent.wpisbox // warpzone
1639 && vlen2(this.origin - this.goalstack01.origin) < vlen2(this.origin - this.goalcurrent.origin))
1641 // immediately remove origin and destination waypoints
1642 navigation_poproute(this);
1644 navigation_poproute(this);
1646 this.lastteleporttime = 0;
1649 // make sure jumppad is really hit, don't rely on distance based checks
1650 // as they may report a touch even if it didn't really happen
1651 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1653 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1654 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1656 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1657 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1659 if(this.jumppadcount)
1661 // remove jumppad waypoint after a random delay to prevent bots getting
1662 // stuck on certain jumppads that require an extra initial horizontal speed
1663 float max_delay = 0.1;
1664 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1666 if (time - this.lastteleporttime < random() * max_delay)
1667 return removed_goals;
1669 else if (this.goalcurrent.wpisbox) // teleport
1671 // immediately remove origin and destination waypoints
1672 navigation_poproute(this);
1675 navigation_poproute(this);
1676 this.lastteleporttime = 0;
1679 return removed_goals;
1681 else if (this.lastteleporttime > 0)
1683 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1684 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1685 // if so immediately fix bot path by removing skipped goals
1686 entity tele_ent = NULL;
1687 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1688 tele_ent = this.goalstack01;
1689 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1690 tele_ent = this.goalstack02;
1691 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1692 tele_ent = this.goalstack03;
1693 if (tele_ent && TELEPORT_USED(this, tele_ent))
1695 if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1696 if ((tele_ent.wpflags & WAYPOINTFLAG_PERSONAL) && tele_ent.owner == this)
1698 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1699 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1701 while (this.goalcurrent != tele_ent)
1703 navigation_poproute(this);
1706 navigation_poproute(this);
1707 this.lastteleporttime = 0;
1709 return removed_goals;
1711 // reset of lastteleporttime can be overriden by a jumppad when it's set
1712 // in more than one frame: make sure it's reset
1713 this.lastteleporttime = 0;
1716 // Loose goal touching check when running
1717 // check goalstack01 to make sure waypoint isn't the final goal
1718 if((this.aistatus & AI_STATUS_RUNNING) && this.goalcurrent.classname == "waypoint" && !(this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP)
1719 && this.goalstack01 && !wasfreed(this.goalstack01) && vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed))
1721 vector gco = this.goalcurrent.origin;
1722 float min_dist = BOT_BUNNYHOP_WP_DETECTION_RANGE;
1723 // also detect waypoints when bot is way above them but with a narrower horizontal range
1724 // so to increase chances bot ends up in the standard range (optimizes nearest waypoint finding)
1725 if(vdist(this.origin - gco, <, min_dist)
1726 || (vdist(vec2(this.origin - gco), <, min_dist * 0.5) && vdist(this.origin - eZ * 1.5 * min_dist - gco, <, min_dist)))
1728 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1729 if(trace_fraction==1)
1731 // Detect personal waypoints
1732 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1733 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1735 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1736 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1739 navigation_poproute(this);
1741 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1742 return removed_goals;
1747 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1749 vector gc_min = this.goalcurrent.absmin;
1750 vector gc_max = this.goalcurrent.absmax;
1751 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1753 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1754 gc_max = this.goalcurrent.origin + '1 1 1' * 12 + eZ * (jumpheight_vec.z + STAT(PL_MIN, this).z);
1756 if (this.ladder_entity)
1758 if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1763 if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1767 // Detect personal waypoints
1768 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1769 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1771 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1772 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1775 navigation_poproute(this);
1777 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1778 return removed_goals;
1780 return removed_goals;
1783 entity navigation_get_really_close_waypoint(entity this)
1785 entity wp = this.goalcurrent;
1787 wp = this.goalcurrent_prev;
1790 float min_dist = ((this.aistatus & AI_STATUS_RUNNING) ? BOT_BUNNYHOP_WP_DETECTION_RANGE : 50);
1791 if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, min_dist))
1793 wp = this.goalcurrent_prev;
1797 if(wp.classname != "waypoint")
1799 wp = wp.nearestwaypoint;
1803 if(vdist(wp.origin - this.origin, >, min_dist))
1806 IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
1808 if(vdist(it.origin - this.origin, <, min_dist))
1817 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1820 set_tracewalk_dest(wp, this.origin, false);
1821 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1822 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1829 // begin a goal selection session (queries spawnfunc_waypoint network)
1830 void navigation_goalrating_start(entity this)
1832 if(this.aistatus & AI_STATUS_STUCK)
1835 this.navigation_jetpack_goal = NULL;
1836 navigation_bestrating = -1;
1837 entity wp = navigation_get_really_close_waypoint(this);
1838 navigation_clearroute(this);
1839 navigation_bestgoal = NULL;
1840 navigation_markroutes(this, wp);
1841 this.goalstack31 = wp; // temporarly save the really close waypoint
1844 // ends a goal selection session (updates goal stack to the best goal)
1845 void navigation_goalrating_end(entity this)
1847 if(this.aistatus & AI_STATUS_STUCK)
1850 entity wp = this.goalstack31; // save to wp as this.goalstack31 is set by navigation_routetogoal
1851 this.goalstack31 = NULL;
1853 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1854 LOG_DEBUG("best goal ", navigation_bestgoal.classname);
1856 if (wp && this.goalcurrent == wp)
1857 navigation_poproute(this);
1859 // If the bot got stuck then try to reach the farthest waypoint
1860 if (!this.goalentity)
1862 if (autocvar_bot_wander_enable && !(this.aistatus & AI_STATUS_STUCK))
1864 LOG_DEBUG(this.netname, " cannot walk to any goal");
1865 this.aistatus |= AI_STATUS_STUCK;
1867 this.goalentity_shouldbefrozen = false;
1870 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1873 void botframe_updatedangerousobjects(float maxupdate)
1875 vector m1, m2, v, o;
1879 IL_EACH(g_waypoints, true,
1885 IL_EACH(g_bot_dodge, it.bot_dodge,
1888 v.x = bound(m1_x, v.x, m2_x);
1889 v.y = bound(m1_y, v.y, m2_y);
1890 v.z = bound(m1_z, v.z, m2_z);
1891 o = (it.absmin + it.absmax) * 0.5;
1892 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1895 traceline(o, v, true, NULL);
1896 if (trace_fraction == 1)
1897 danger = danger + d;
1907 void navigation_unstuck(entity this)
1909 if (!autocvar_bot_wander_enable)
1912 bool has_user_waypoints = false;
1913 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1915 has_user_waypoints = true;
1918 if (!has_user_waypoints)
1921 float search_radius = 1000;
1923 if (!bot_waypoint_queue_owner)
1925 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1926 bot_waypoint_queue_owner = this;
1927 bot_waypoint_queue_bestgoal = NULL;
1928 bot_waypoint_queue_bestgoalrating = 0;
1931 if(bot_waypoint_queue_owner!=this)
1934 if (bot_waypoint_queue_goal)
1936 // evaluate the next goal on the queue
1937 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1938 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with squared distance ", ftos(d));
1939 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1940 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1941 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1943 if( d > bot_waypoint_queue_bestgoalrating)
1945 bot_waypoint_queue_bestgoalrating = d;
1946 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1950 // move to a random waypoint while bot is searching for a walkable path;
1951 // this is usually sufficient to unstuck bots from bad spots or when other
1952 // bots of the same team block all their ways
1953 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1955 navigation_clearroute(this);
1956 navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1957 navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1960 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1962 if (!bot_waypoint_queue_goal)
1964 if (bot_waypoint_queue_bestgoal)
1966 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1967 navigation_clearroute(this);
1968 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1969 navigation_goalrating_timeout_set(this);
1970 this.aistatus &= ~AI_STATUS_STUCK;
1974 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1977 bot_waypoint_queue_owner = NULL;
1982 if(bot_strategytoken!=this)
1985 // build a new queue
1986 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1988 entity first = NULL;
1990 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1992 if(bot_waypoint_queue_goal)
1993 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1997 bot_waypoint_queue_goal = it;
1998 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
2002 bot_waypoint_queue_goal = first;
2005 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
2006 bot_waypoint_queue_owner = NULL;
2011 // Support for debugging tracewalk visually
2013 void debugresetnodes()
2015 debuglastnode = '0 0 0';
2018 void debugnode(entity this, vector node)
2020 if (!IS_PLAYER(this))
2023 if(debuglastnode=='0 0 0')
2025 debuglastnode = node;
2029 te_lightning2(NULL, node, debuglastnode);
2030 debuglastnode = node;
2033 void debugnodestatus(vector position, float status)
2039 case DEBUG_NODE_SUCCESS:
2042 case DEBUG_NODE_WARNING:
2045 case DEBUG_NODE_FAIL:
2052 te_customflash(position, 40, 2, c);
2055 // Support for debugging the goal stack visually
2058 .vector lastposition;
2060 // Debug the goal stack visually
2061 void debuggoalstack(entity this)
2066 if(this.goalcounter==0)goal=this.goalcurrent;
2067 else if(this.goalcounter==1)goal=this.goalstack01;
2068 else if(this.goalcounter==2)goal=this.goalstack02;
2069 else if(this.goalcounter==3)goal=this.goalstack03;
2070 else if(this.goalcounter==4)goal=this.goalstack04;
2071 else if(this.goalcounter==5)goal=this.goalstack05;
2072 else if(this.goalcounter==6)goal=this.goalstack06;
2073 else if(this.goalcounter==7)goal=this.goalstack07;
2074 else if(this.goalcounter==8)goal=this.goalstack08;
2075 else if(this.goalcounter==9)goal=this.goalstack09;
2076 else if(this.goalcounter==10)goal=this.goalstack10;
2077 else if(this.goalcounter==11)goal=this.goalstack11;
2078 else if(this.goalcounter==12)goal=this.goalstack12;
2079 else if(this.goalcounter==13)goal=this.goalstack13;
2080 else if(this.goalcounter==14)goal=this.goalstack14;
2081 else if(this.goalcounter==15)goal=this.goalstack15;
2082 else if(this.goalcounter==16)goal=this.goalstack16;
2083 else if(this.goalcounter==17)goal=this.goalstack17;
2084 else if(this.goalcounter==18)goal=this.goalstack18;
2085 else if(this.goalcounter==19)goal=this.goalstack19;
2086 else if(this.goalcounter==20)goal=this.goalstack20;
2087 else if(this.goalcounter==21)goal=this.goalstack21;
2088 else if(this.goalcounter==22)goal=this.goalstack22;
2089 else if(this.goalcounter==23)goal=this.goalstack23;
2090 else if(this.goalcounter==24)goal=this.goalstack24;
2091 else if(this.goalcounter==25)goal=this.goalstack25;
2092 else if(this.goalcounter==26)goal=this.goalstack26;
2093 else if(this.goalcounter==27)goal=this.goalstack27;
2094 else if(this.goalcounter==28)goal=this.goalstack28;
2095 else if(this.goalcounter==29)goal=this.goalstack29;
2096 else if(this.goalcounter==30)goal=this.goalstack30;
2097 else if(this.goalcounter==31)goal=this.goalstack31;
2102 this.goalcounter = 0;
2103 this.lastposition='0 0 0';
2107 if(this.lastposition=='0 0 0')
2110 org = this.lastposition;
2113 go = ( goal.absmin + goal.absmax ) * 0.5;
2114 te_lightning2(NULL, org, go);
2115 this.lastposition = go;