1 #include "navigation.qh"
3 #include <server/bot/api.qh>
4 #include <common/weapons/_all.qh>
5 #include <common/stats.qh>
9 #include "waypoints.qh"
11 #include <server/items/items.qh>
13 #include <common/items/_mod.qh>
15 #include <common/constants.qh>
16 #include <common/net_linked.qh>
17 #include <common/mapobjects/func/ladder.qh>
18 #include <common/mapobjects/trigger/hurt.qh>
19 #include <common/mapobjects/trigger/jumppads.qh>
23 void navigation_goalrating_timeout_set(entity this)
25 if(IS_MOVABLE(this.goalentity))
26 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
28 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
31 // use this when current goal must be discarded immediately
32 void navigation_goalrating_timeout_force(entity this)
34 navigation_goalrating_timeout_expire(this, 0);
37 // use this when current goal can be kept for a short while to increase the chance
38 // of bot touching a waypoint, which helps to find a new goal more efficiently
39 void navigation_goalrating_timeout_expire(entity this, float seconds)
42 this.bot_strategytime = 0;
43 else if (this.bot_strategytime > time + seconds)
44 this.bot_strategytime = time + seconds;
47 bool navigation_goalrating_timeout(entity this)
49 return this.bot_strategytime < time;
53 void navigation_goalrating_timeout_extend_if_needed(entity this, float seconds)
55 this.bot_strategytime = max(this.bot_strategytime, time + seconds);
58 #define MAX_CHASE_DISTANCE 700
59 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
61 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
62 if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
63 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
68 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
70 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
72 this.ignoregoal = this.goalentity;
73 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
80 void navigation_dynamicgoal_init(entity this, bool initially_static)
82 this.navigation_dynamicgoal = true;
83 this.bot_basewaypoint = this.nearestwaypoint;
85 this.nearestwaypointtimeout = -1;
87 this.nearestwaypointtimeout = time;
90 void navigation_dynamicgoal_set(entity this, entity dropper)
92 this.nearestwaypointtimeout = time;
93 if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
94 this.nearestwaypoint = dropper.nearestwaypoint;
95 if (this.nearestwaypoint)
96 this.nearestwaypointtimeout += 2;
99 void navigation_dynamicgoal_unset(entity this)
101 if(this.bot_basewaypoint)
102 this.nearestwaypoint = this.bot_basewaypoint;
103 this.nearestwaypointtimeout = -1;
106 // returns point of ent closer to org
107 vector get_closer_dest(entity ent, vector org)
109 vector dest = '0 0 0';
110 if ((ent.classname != "waypoint") || ent.wpisbox)
112 vector wm1 = ent.origin + ent.mins;
113 vector wm2 = ent.origin + ent.maxs;
114 dest.x = bound(wm1.x, org.x, wm2.x);
115 dest.y = bound(wm1.y, org.y, wm2.y);
116 dest.z = bound(wm1.z, org.z, wm2.z);
123 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
125 if ((ent.classname != "waypoint") || ent.wpisbox)
127 vector wm1 = ent.origin + ent.mins;
128 vector wm2 = ent.origin + ent.maxs;
129 if (IS_PLAYER(ent) || IS_MONSTER(ent))
131 // move destination point out of player bbox otherwise tracebox always fails
132 // (if bot_navigation_ignoreplayers is false)
133 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
134 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
136 // set destination point to x and y coords of ent that are closer to org
137 // z coord is set to ent's min height
138 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
139 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
140 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
141 && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
143 tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
144 tracewalk_dest_height = 0;
145 fix_player_dest = false;
149 tracewalk_dest.z = wm1.z;
150 tracewalk_dest_height = wm2.z - wm1.z;
155 tracewalk_dest = ent.origin;
156 tracewalk_dest_height = 0;
158 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
160 // snap player to the ground
161 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
163 // bot is right under the player
164 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
165 tracewalk_dest = trace_endpos;
166 tracewalk_dest_height = 0;
170 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
171 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
173 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
174 tracewalk_dest.z = trace_endpos.z;
180 // returns point of ent closer to org
181 vector set_tracewalk_dest_2(entity ent, vector org)
183 vector closer_dest = '0 0 0';
184 if ((ent.classname != "waypoint") || ent.wpisbox)
186 vector wm1 = ent.origin + ent.mins;
187 vector wm2 = ent.origin + ent.maxs;
188 closer_dest.x = bound(wm1.x, org.x, wm2.x);
189 closer_dest.y = bound(wm1.y, org.y, wm2.y);
190 closer_dest.z = bound(wm1.z, org.z, wm2.z);
191 // set destination point to x and y coords of ent that are closer to org
192 // z coord is set to ent's min height
193 tracewalk_dest.x = closer_dest.x;
194 tracewalk_dest.y = closer_dest.y;
195 tracewalk_dest.z = wm1.z;
196 tracewalk_dest_height = wm2.z - wm1.z; // destination height
200 closer_dest = ent.origin;
201 tracewalk_dest = closer_dest;
202 tracewalk_dest_height = 0;
207 bool navigation_check_submerged_state(entity ent, vector pos)
211 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
212 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
213 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
216 submerged = SUBMERGED(pos);
217 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
218 // is actually submerged because often they are inside some solid.
219 // That's why submerged state is saved now that we know current pos is
220 // not stuck in solid (previous tracewalk call to this pos was successfully)
221 if(!ent.navigation_dynamicgoal)
222 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
227 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
229 IL_EACH(g_ladders, it.classname == "func_ladder",
232 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
233 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
236 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
237 tracebox(org, m1, m2, top, movemode, e);
238 if(trace_fraction == 1)
245 // Unfortuntely we can't use trace_inwater since it doesn't hold the fraction of the total
246 // distance that was traveled before impact as the description in the engine (collision.h) says.
247 // It would have helped to speed up tracewalk underwater
248 vector resurface_limited(vector org, float lim, vector m1)
250 if (WETFEET(org + eZ * (lim - org.z)))
254 float RES_min_h = org.z;
255 float RES_max_h = lim;
257 org.z = 0.5 * (RES_min_h + RES_max_h);
262 } while (RES_max_h - RES_min_h >= 1);
267 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
270 #define NAV_SWIM_ONWATER 1
271 #define NAV_SWIM_UNDERWATER 2
273 // rough simulation of walking from one point to another to test if a path
274 // can be traveled, used for waypoint linking and havocbot
275 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
276 // INFO: the command sv_cmd trace walk is useful to test this function in game
277 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
279 if(autocvar_bot_debug_tracewalk)
286 vector flatdir = end - start;
288 float flatdist = vlen(flatdir);
289 flatdir = normalize(flatdir);
291 bool ignorehazards = false;
294 // Analyze starting point
296 ignorehazards = true;
298 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
299 if (trace_startsolid)
302 if(autocvar_bot_debug_tracewalk)
303 debugnodestatus(start, DEBUG_NODE_FAIL);
305 //print("tracewalk: ", vtos(start), " is a bad start\n");
311 end2.z += end_height;
313 vector fixed_end = end;
316 if (flatdist > 0 && WETFEET(org))
319 nav_action = NAV_SWIM_UNDERWATER;
322 // tracebox down by player's height
323 // useful to know if water level is so low that bot can still walk
324 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
325 if (SUBMERGED(trace_endpos))
328 nav_action = NAV_SWIM_UNDERWATER;
331 nav_action = NAV_WALK;
335 nav_action = NAV_WALK;
343 if (org.z > end2.z + 1)
345 tracebox(org, m1, m2, end2, movemode, e);
347 if (org.z > end2.z + 1)
350 else if (org.z < end.z - 1)
352 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
353 if (SUBMERGED(trace_endpos))
355 vector v = trace_endpos;
356 tracebox(v, m1, m2, end, movemode, e);
357 if(trace_endpos.z >= end.z - 1)
359 RESURFACE_LIMITED(v, trace_endpos.z);
363 else if (trace_endpos.z > org.z - jumpheight_vec.z)
364 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
366 if (org.z < end.z - 1)
373 if(autocvar_bot_debug_tracewalk)
376 debugnodestatus(org, DEBUG_NODE_SUCCESS);
379 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
384 if(autocvar_bot_debug_tracewalk)
390 if (stepdist > flatdist)
392 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
394 // can't use movement direction here to calculate move because of
395 // precision errors especially when direction has a high enough z value
396 //water_dir = normalize(water_end - org);
397 //move = org + water_dir * stepdist;
398 fixed_end.z = bound(end.z, org.z, end2.z);
399 if (stepdist == flatdist) {
403 move = org + (fixed_end - org) * (stepdist / flatdist);
404 flatdist = vlen(vec2(fixed_end - move));
407 else // horiz. direction
409 flatdist -= stepdist;
410 move = org + flatdir * stepdist;
413 if(nav_action == NAV_SWIM_ONWATER)
415 tracebox(org, m1, m2, move, movemode, e); // swim
418 if (trace_fraction < 1)
421 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
423 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
426 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
428 if(autocvar_bot_debug_tracewalk)
431 debugnodestatus(org, DEBUG_NODE_SUCCESS);
434 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
438 if(autocvar_bot_debug_tracewalk)
439 debugnodestatus(org, DEBUG_NODE_FAIL);
442 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
453 if (org.z <= move.z) // going horiz.
455 tracebox(trace_endpos, m1, m2, move, movemode, e);
457 nav_action = NAV_WALK;
462 if (org.z <= move.z) // going horiz.
465 nav_action = NAV_SWIM_ONWATER;
471 nav_action = NAV_SWIM_UNDERWATER;
473 nav_action = NAV_SWIM_ONWATER;
476 else if(nav_action == NAV_SWIM_UNDERWATER)
478 if (move.z >= org.z) // swimming upwards or horiz.
480 tracebox(org, m1, m2, move, movemode, e); // swim
482 bool stepswum = false;
485 if (trace_fraction < 1)
488 vector stepswim_move = move + stepheightvec;
489 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
490 stepswim_move.z = end2.z;
492 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
495 if (trace_startsolid)
497 if(autocvar_bot_debug_tracewalk)
498 debugnodestatus(org, DEBUG_NODE_FAIL);
500 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
504 if (trace_fraction < 1)
506 float org_z_prev = org.z;
507 RESURFACE_LIMITED(org, end2.z);
508 if(org.z == org_z_prev)
510 if(autocvar_bot_debug_tracewalk)
511 debugnodestatus(org, DEBUG_NODE_FAIL);
513 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
517 nav_action = NAV_SWIM_UNDERWATER;
519 nav_action = NAV_SWIM_ONWATER;
521 // we didn't advance horiz. in this step, flatdist decrease should be reverted
522 // but we can't do it properly right now... apply this workaround instead
540 if (!WETFEET(trace_endpos))
542 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
543 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
544 if (!stepswum && SUBMERGED(trace_endpos))
546 RESURFACE_LIMITED(trace_endpos, end2.z);
548 nav_action = NAV_SWIM_ONWATER;
554 nav_action = NAV_WALK;
560 nav_action = NAV_SWIM_UNDERWATER;
563 else //if (move.z < org.z) // swimming downwards
565 tracebox(org, m1, m2, move, movemode, e); // swim
568 if (trace_fraction < 1)
571 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
574 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
576 if(autocvar_bot_debug_tracewalk)
577 debugnodestatus(move, DEBUG_NODE_FAIL);
579 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
591 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
593 // stepswim caused upwards direction
594 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
595 if (!SUBMERGED(trace_endpos))
598 nav_action = NAV_WALK;
605 nav_action = NAV_SWIM_UNDERWATER;
609 else if(nav_action == NAV_WALK)
612 tracebox(org, m1, m2, move, movemode, e);
614 if(autocvar_bot_debug_tracewalk)
615 debugnode(e, trace_endpos);
618 if (trace_fraction < 1)
620 // check if we can walk over this obstacle, possibly by jumpstepping
621 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
622 if (trace_fraction < 1 || trace_startsolid)
624 if (trace_startsolid) // hit ceiling above org
626 // reduce stepwalk height
627 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
628 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
630 else //if (trace_fraction < 1)
632 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
633 if (trace_startsolid) // hit ceiling above org
635 // reduce jumpstepwalk height
636 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
637 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
641 if (trace_fraction < 1)
643 vector v = trace_endpos;
644 v.z = org.z + jumpheight_vec.z;
645 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
647 if(autocvar_bot_debug_tracewalk)
650 debugnodestatus(v, DEBUG_NODE_SUCCESS);
653 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
657 if(autocvar_bot_debug_tracewalk)
658 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
660 traceline( org, move, movemode, e);
662 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
666 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
668 nextmove = move + (flatdir * stepdist);
669 traceline( move, nextmove, movemode, e);
672 flatdist = vlen(vec2(end - move));
676 if(autocvar_bot_debug_tracewalk)
677 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
679 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
680 //te_explosion(trace_endpos);
681 //print(ftos(e.dphitcontentsmask), "\n");
682 return false; // failed
694 // trace down from stepheight as far as possible and move there,
695 // if this starts in solid we try again without the stepup, and
696 // if that also fails we assume it is a wall
697 // (this is the same logic as the Quake walkmove function used)
698 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
706 if(autocvar_bot_debug_tracewalk)
708 debugnode(e, trace_endpos);
709 debugnodestatus(org, DEBUG_NODE_FAIL);
712 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
719 if(move.z >= end2.z && org.z < end2.z)
724 if(org.z > move.z - 1 || !SUBMERGED(org))
726 nav_action = NAV_WALK;
730 // ended up submerged while walking
731 if(autocvar_bot_debug_tracewalk)
734 RESURFACE_LIMITED(org, move.z);
735 nav_action = NAV_SWIM_ONWATER;
740 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
742 // moved but didn't arrive at the intended destination
743 if(autocvar_bot_debug_tracewalk)
744 debugnodestatus(org, DEBUG_NODE_FAIL);
749 /////////////////////////////////////////////////////////////////////////////
751 /////////////////////////////////////////////////////////////////////////////
753 // completely empty the goal stack, used when deciding where to go
754 void navigation_clearroute(entity this)
756 this.lastteleporttime = 0;
757 this.goalcurrent_prev = this.goalcurrent;
758 this.goalcurrent_distance_2d = FLOAT_MAX;
759 this.goalcurrent_distance_z = FLOAT_MAX;
760 this.goalcurrent_distance_time = 0;
761 this.goalentity_lock_timeout = 0;
762 this.goalentity_shouldbefrozen = false;
763 this.goalentity = NULL;
764 this.goalcurrent = NULL;
765 this.goalstack01 = NULL;
766 this.goalstack02 = NULL;
767 this.goalstack03 = NULL;
768 this.goalstack04 = NULL;
769 this.goalstack05 = NULL;
770 this.goalstack06 = NULL;
771 this.goalstack07 = NULL;
772 this.goalstack08 = NULL;
773 this.goalstack09 = NULL;
774 this.goalstack10 = NULL;
775 this.goalstack11 = NULL;
776 this.goalstack12 = NULL;
777 this.goalstack13 = NULL;
778 this.goalstack14 = NULL;
779 this.goalstack15 = NULL;
780 this.goalstack16 = NULL;
781 this.goalstack17 = NULL;
782 this.goalstack18 = NULL;
783 this.goalstack19 = NULL;
784 this.goalstack20 = NULL;
785 this.goalstack21 = NULL;
786 this.goalstack22 = NULL;
787 this.goalstack23 = NULL;
788 this.goalstack24 = NULL;
789 this.goalstack25 = NULL;
790 this.goalstack26 = NULL;
791 this.goalstack27 = NULL;
792 this.goalstack28 = NULL;
793 this.goalstack29 = NULL;
794 this.goalstack30 = NULL;
795 this.goalstack31 = NULL;
798 // add a new goal at the beginning of the stack
799 // (in other words: add a new prerequisite before going to the later goals)
800 // NOTE: when a waypoint is added, the WP gets pushed first, then the
801 // next-closest WP on the shortest path to the WP
802 // That means, if the stack overflows, the bot will know how to do the FIRST 32
803 // steps to the goal, and then recalculate the path.
804 void navigation_pushroute(entity this, entity e)
806 this.goalcurrent_prev = this.goalcurrent;
807 this.goalcurrent_distance_2d = FLOAT_MAX;
808 this.goalcurrent_distance_z = FLOAT_MAX;
809 this.goalcurrent_distance_time = 0;
810 //print("bot ", etos(this), " push ", etos(e), "\n");
811 if(this.goalstack31 == this.goalentity)
812 this.goalentity = NULL;
813 this.goalstack31 = this.goalstack30;
814 this.goalstack30 = this.goalstack29;
815 this.goalstack29 = this.goalstack28;
816 this.goalstack28 = this.goalstack27;
817 this.goalstack27 = this.goalstack26;
818 this.goalstack26 = this.goalstack25;
819 this.goalstack25 = this.goalstack24;
820 this.goalstack24 = this.goalstack23;
821 this.goalstack23 = this.goalstack22;
822 this.goalstack22 = this.goalstack21;
823 this.goalstack21 = this.goalstack20;
824 this.goalstack20 = this.goalstack19;
825 this.goalstack19 = this.goalstack18;
826 this.goalstack18 = this.goalstack17;
827 this.goalstack17 = this.goalstack16;
828 this.goalstack16 = this.goalstack15;
829 this.goalstack15 = this.goalstack14;
830 this.goalstack14 = this.goalstack13;
831 this.goalstack13 = this.goalstack12;
832 this.goalstack12 = this.goalstack11;
833 this.goalstack11 = this.goalstack10;
834 this.goalstack10 = this.goalstack09;
835 this.goalstack09 = this.goalstack08;
836 this.goalstack08 = this.goalstack07;
837 this.goalstack07 = this.goalstack06;
838 this.goalstack06 = this.goalstack05;
839 this.goalstack05 = this.goalstack04;
840 this.goalstack04 = this.goalstack03;
841 this.goalstack03 = this.goalstack02;
842 this.goalstack02 = this.goalstack01;
843 this.goalstack01 = this.goalcurrent;
844 this.goalcurrent = e;
847 // remove first goal from stack
848 // (in other words: remove a prerequisite for reaching the later goals)
849 // (used when a spawnfunc_waypoint is reached)
850 void navigation_poproute(entity this)
852 this.goalcurrent_prev = this.goalcurrent;
853 this.goalcurrent_distance_2d = FLOAT_MAX;
854 this.goalcurrent_distance_z = FLOAT_MAX;
855 this.goalcurrent_distance_time = 0;
856 //print("bot ", etos(this), " pop\n");
857 if(this.goalcurrent == this.goalentity)
859 this.goalentity = NULL;
860 this.goalentity_lock_timeout = 0;
862 this.goalcurrent = this.goalstack01;
863 this.goalstack01 = this.goalstack02;
864 this.goalstack02 = this.goalstack03;
865 this.goalstack03 = this.goalstack04;
866 this.goalstack04 = this.goalstack05;
867 this.goalstack05 = this.goalstack06;
868 this.goalstack06 = this.goalstack07;
869 this.goalstack07 = this.goalstack08;
870 this.goalstack08 = this.goalstack09;
871 this.goalstack09 = this.goalstack10;
872 this.goalstack10 = this.goalstack11;
873 this.goalstack11 = this.goalstack12;
874 this.goalstack12 = this.goalstack13;
875 this.goalstack13 = this.goalstack14;
876 this.goalstack14 = this.goalstack15;
877 this.goalstack15 = this.goalstack16;
878 this.goalstack16 = this.goalstack17;
879 this.goalstack17 = this.goalstack18;
880 this.goalstack18 = this.goalstack19;
881 this.goalstack19 = this.goalstack20;
882 this.goalstack20 = this.goalstack21;
883 this.goalstack21 = this.goalstack22;
884 this.goalstack22 = this.goalstack23;
885 this.goalstack23 = this.goalstack24;
886 this.goalstack24 = this.goalstack25;
887 this.goalstack25 = this.goalstack26;
888 this.goalstack26 = this.goalstack27;
889 this.goalstack27 = this.goalstack28;
890 this.goalstack28 = this.goalstack29;
891 this.goalstack29 = this.goalstack30;
892 this.goalstack30 = this.goalstack31;
893 this.goalstack31 = NULL;
896 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
897 // waypoint destination coordinates instead of v (only useful for box waypoints)
898 // for normal waypoints v2 == v and v2_height == 0
899 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)
901 if (vdist(v - org, <, bestdist))
903 traceline(v, org, true, ent);
904 if (trace_fraction == 1)
908 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
913 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
921 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
922 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
925 ent = ent.tag_entity;
927 vector pm1 = ent.origin + ent.mins;
928 vector pm2 = ent.origin + ent.maxs;
930 if (autocvar_g_waypointeditor && !IS_BOT_CLIENT(ent))
932 // this code allows removing waypoints in the air and seeing jumppad/telepport waypoint links
933 // FIXME it causes a bug where a waypoint spawned really close to another one (max 16 qu)
934 // isn't detected as the nearest waypoint
935 IL_EACH(g_waypoints, it != ent && it != except,
937 if (boxesoverlap(pm1, pm2, it.absmin, it.absmax))
943 // do two scans, because box test is cheaper
944 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
946 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
948 if(walkfromwp && !ent.navigation_dynamicgoal)
949 waypoint_clearlinks(ent); // initialize wpXXmincost fields
955 vector org = ent.origin;
956 if (navigation_testtracewalk)
962 if(ent.size && !IS_PLAYER(ent))
964 org += 0.5 * (ent.mins + ent.maxs);
965 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
968 // box check failed, try walk
969 IL_EACH(g_waypoints, it != ent,
971 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
977 set_tracewalk_dest(ent, v, true);
978 if (trace_ent == ent)
986 set_tracewalk_dest(it, org, false);
988 if (navigation_waypoint_will_link(v, org, ent,
989 tracewalk_dest, tracewalk_dest_height,
990 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
993 bestdist = vlen(tracewalk_dest - v);
995 bestdist = vlen(v - org);
999 if(!best && !ent.navigation_dynamicgoal)
1001 int solid_save = ent.solid;
1002 ent.solid = SOLID_BSP;
1003 IL_EACH(g_jumppads, true,
1005 if(trigger_push_test(it, ent))
1007 best = it.nearestwaypoint;
1011 ent.solid = solid_save;
1015 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1017 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1018 if (autocvar_g_waypointeditor_auto)
1020 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1022 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1027 // finds the waypoints near the bot initiating a navigation query
1028 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1030 //navigation_testtracewalk = true;
1032 IL_EACH(g_waypoints, !it.wpconsidered,
1034 set_tracewalk_dest(it, this.origin, false);
1036 vector diff = tracewalk_dest - this.origin;
1037 diff.z = max(0, diff.z);
1038 if(vdist(diff, <, maxdist))
1040 it.wpconsidered = true;
1041 if (tracewalk(this, this.origin, this.mins, this.maxs,
1042 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1044 it.wpnearestpoint = tracewalk_dest;
1045 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1052 //navigation_testtracewalk = false;
1056 // updates a path link if a spawnfunc_waypoint link is better than the current one
1057 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1063 m1 = wp.origin + wp.mins;
1064 m2 = wp.origin + wp.maxs;
1065 v.x = bound(m1_x, p.x, m2_x);
1066 v.y = bound(m1_y, p.y, m2_y);
1067 v.z = bound(m1_z, p.z, m2_z);
1071 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1072 cost += w.wp00mincost; // assuming teleport has exactly one destination
1074 cost += waypoint_gettravelcost(p, v, w, wp);
1075 if (wp.wpcost > cost)
1080 wp.wpnearestpoint = v;
1084 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1085 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1090 IL_EACH(g_waypoints, true,
1092 it.wpconsidered = false;
1093 it.wpnearestpoint = '0 0 0';
1094 it.wpcost = 10000000;
1099 if(fixed_source_waypoint)
1101 fixed_source_waypoint.wpconsidered = true;
1102 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1103 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1104 fixed_source_waypoint.wpfire = 1;
1105 fixed_source_waypoint.enemy = NULL;
1109 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1110 // as this search is expensive we will use lower values if the bot is on the air
1111 float increment, maxdistance;
1112 if(IS_ONGROUND(this))
1115 maxdistance = 50000;
1123 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1126 bool searching = true;
1130 IL_EACH(g_waypoints, it.wpfire,
1135 p = it.wpnearestpoint;
1137 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1166 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1167 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1168 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1169 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1174 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1175 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1179 IL_EACH(g_waypoints, true,
1181 it.wpconsidered = false;
1182 it.wpnearestpoint = '0 0 0';
1183 it.wpcost = 10000000;
1188 if(fixed_source_waypoint)
1190 fixed_source_waypoint.wpconsidered = true;
1191 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1192 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1193 fixed_source_waypoint.wpfire = 1;
1194 fixed_source_waypoint.enemy = NULL;
1198 error("need to start with a waypoint\n");
1201 bool searching = true;
1205 IL_EACH(g_waypoints, it.wpfire,
1209 cost = it.wpcost; // cost to walk from it to home
1210 p = it.wpnearestpoint;
1212 IL_EACH(g_waypoints, it != wp,
1214 if(!waypoint_islinked(it, wp))
1216 cost2 = cost + it.dmg;
1217 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1223 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1224 void navigation_routerating(entity this, entity e, float f, float rangebias)
1226 if (!e || e.blacklisted) { return; }
1228 rangebias = waypoint_getlinearcost(rangebias);
1229 f = waypoint_getlinearcost(f);
1233 bool rate_wps = false;
1234 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1235 || (e.flags & FL_PARTIALGROUND))
1242 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1243 int t = pointcontents(trace_endpos + '0 0 1');
1244 if(t != CONTENT_SOLID )
1246 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1248 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1255 entity theEnemy = e;
1256 entity best_wp = NULL;
1257 float best_dist = FLOAT_MAX;
1258 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1259 && vdist(it.origin - theEnemy.origin, <, 500)
1260 && vdist(it.origin - this.origin, >, 100)
1261 && vdist(it.origin - this.origin, <, 10000),
1263 float dist = vlen2(it.origin - theEnemy.origin);
1264 if (dist < best_dist)
1276 vector goal_org = (e.absmin + e.absmax) * 0.5;
1278 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1280 // Evaluate path using jetpack
1281 if(this.items & IT_JETPACK)
1282 if(autocvar_bot_ai_navigation_jetpack)
1283 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1285 vector pointa, pointb;
1287 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1290 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1291 pointa = trace_endpos - '0 0 1';
1294 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1295 pointb = trace_endpos - '0 0 1';
1297 // Can I see these two points from the sky?
1298 traceline(pointa, pointb, MOVE_NORMAL, this);
1300 if(trace_fraction==1)
1302 LOG_DEBUG("jetpack ai: can bridge these two points");
1304 // Lower the altitude of these points as much as possible
1305 float zdistance, xydistance, cost, t, fuel;
1306 vector down, npa, npb;
1308 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1311 npa = pointa + down;
1312 npb = pointb + down;
1314 if(npa.z<=this.absmax.z)
1317 if(npb.z<=e.absmax.z)
1320 traceline(npa, npb, MOVE_NORMAL, this);
1321 if(trace_fraction==1)
1327 while(trace_fraction == 1);
1330 // Rough estimation of fuel consumption
1331 // (ignores acceleration and current xyz velocity)
1332 xydistance = vlen(pointa - pointb);
1333 zdistance = fabs(pointa.z - this.origin.z);
1335 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1336 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1337 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1339 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResource(this, RES_FUEL)));
1342 if(GetResource(this, RES_FUEL) > fuel || (this.items & IT_UNLIMITED_AMMO))
1345 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1346 // - between air and ground speeds)
1348 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1349 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1352 // Compare against other goals
1353 f = f * rangebias / (rangebias + cost);
1355 if (navigation_bestrating < f)
1357 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1358 navigation_bestrating = f;
1359 navigation_bestgoal = e;
1360 this.navigation_jetpack_goal = e;
1361 this.navigation_jetpack_point = pointb;
1369 //te_wizspike(e.origin);
1372 // update the cached spawnfunc_waypoint link on a dynamic item entity
1373 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1379 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1380 e.nearestwaypoint = NULL;
1382 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1383 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1385 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1386 e.nearestwaypoint = nwp = e.goalcurrent;
1388 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1391 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1393 if(!e.navigation_dynamicgoal)
1394 e.blacklisted = true;
1398 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1403 if(e.navigation_dynamicgoal)
1404 e.nearestwaypointtimeout = time + 2;
1405 else if(autocvar_g_waypointeditor)
1406 e.nearestwaypointtimeout = time + 3 + random() * 2;
1408 nwp = e.nearestwaypoint;
1411 if (nwp && nwp.wpcost < 10000000)
1413 //te_wizspike(nwp.wpnearestpoint);
1414 float nwptoitem_cost = 0;
1415 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1416 nwptoitem_cost = nwp.wp00mincost;
1418 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1419 float cost = nwp.wpcost + nwptoitem_cost;
1420 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1421 f = f * rangebias / (rangebias + cost);
1422 LOG_DEBUG(" ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1423 if (navigation_bestrating < f)
1425 LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1426 navigation_bestrating = f;
1427 navigation_bestgoal = e;
1432 // adds an item to the the goal stack with the path to a given item
1433 bool navigation_routetogoal(entity this, entity e, vector startposition)
1435 // if there is no goal, just exit
1439 entity teleport_goal = NULL;
1441 this.goalentity = e;
1443 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1445 // force teleport destination as route destination
1447 navigation_pushroute(this, e.wp00);
1448 this.goalentity = e.wp00;
1451 // put the entity on the goal stack
1452 //print("routetogoal ", etos(e), "\n");
1453 navigation_pushroute(this, e);
1456 e = this.goalentity;
1458 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1460 this.wp_goal_prev1 = this.wp_goal_prev0;
1461 this.wp_goal_prev0 = e;
1464 if((this.items & IT_JETPACK) && 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;