1 #include "navigation.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
8 #include "waypoints.qh"
10 #include <common/t_items.qh>
12 #include <common/items/_mod.qh>
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/triggers/trigger/jumppads.qh>
20 void navigation_goalrating_timeout_set(entity this)
22 if(IS_MOVABLE(this.goalentity))
23 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
25 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
28 // use this when current goal must be discarded immediately
29 void navigation_goalrating_timeout_force(entity this)
31 navigation_goalrating_timeout_expire(this, 0);
34 // use this when current goal can be kept for a short while to increase the chance
35 // of bot touching a waypoint, which helps to find a new goal more efficiently
36 void navigation_goalrating_timeout_expire(entity this, float seconds)
39 this.bot_strategytime = 0;
40 else if (this.bot_strategytime > time + seconds)
41 this.bot_strategytime = time + seconds;
44 bool navigation_goalrating_timeout(entity this)
46 return this.bot_strategytime < time;
49 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
51 if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
54 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
56 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
57 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
59 this.ignoregoal = this.goalentity;
60 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
67 void navigation_dynamicgoal_init(entity this, bool initially_static)
69 this.navigation_dynamicgoal = true;
70 this.bot_basewaypoint = this.nearestwaypoint;
72 this.nearestwaypointtimeout = -1;
74 this.nearestwaypointtimeout = time;
77 void navigation_dynamicgoal_set(entity this)
79 this.nearestwaypointtimeout = time;
80 if (this.nearestwaypoint)
81 this.nearestwaypointtimeout += 2;
84 void navigation_dynamicgoal_unset(entity this)
86 if(this.bot_basewaypoint)
87 this.nearestwaypoint = this.bot_basewaypoint;
88 this.nearestwaypointtimeout = -1;
91 // returns point of ent closer to org
92 vector get_closer_dest(entity ent, vector org)
94 vector dest = '0 0 0';
95 if ((ent.classname != "waypoint") || ent.wpisbox)
97 vector wm1 = ent.origin + ent.mins - eZ * (PL_MAX_CONST.z - 1);
98 vector wm2 = ent.origin + ent.maxs - eZ * (PL_MIN_CONST.z + 1);
99 dest.x = bound(wm1.x, org.x, wm2.x);
100 dest.y = bound(wm1.y, org.y, wm2.y);
101 dest.z = bound(wm1.z, org.z, wm2.z);
108 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
110 if ((ent.classname != "waypoint") || ent.wpisbox)
112 vector wm1 = ent.origin + ent.mins - eZ * (PL_MAX_CONST.z - 1);
113 vector wm2 = ent.origin + ent.maxs - eZ * (PL_MIN_CONST.z + 1);
114 if (IS_PLAYER(ent) || IS_MONSTER(ent))
116 // move destination point out of player bbox otherwise tracebox always fails
117 // (if bot_navigation_ignoreplayers is false)
118 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
119 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
121 // set destination point to x and y coords of ent that are closer to org
122 // z coord is set to ent's min height
123 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
124 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
125 tracewalk_dest.z = wm1.z;
126 tracewalk_dest_height = wm2.z - wm1.z; // destination height
130 tracewalk_dest = ent.origin;
131 tracewalk_dest_height = 0;
133 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
135 // snap player to the ground
136 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
138 // bot is right under the player
139 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
140 tracewalk_dest = trace_endpos;
141 tracewalk_dest_height = 0;
145 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
146 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
148 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
149 tracewalk_dest.z = trace_endpos.z;
155 // returns point of ent closer to org
156 vector set_tracewalk_dest_2(entity ent, vector org)
158 vector closer_dest = '0 0 0';
159 if ((ent.classname != "waypoint") || ent.wpisbox)
161 vector wm1 = ent.origin + ent.mins - eZ * (PL_MAX_CONST.z - 1);
162 vector wm2 = ent.origin + ent.maxs - eZ * (PL_MIN_CONST.z + 1);
163 closer_dest.x = bound(wm1.x, org.x, wm2.x);
164 closer_dest.y = bound(wm1.y, org.y, wm2.y);
165 closer_dest.z = bound(wm1.z, org.z, wm2.z);
166 // set destination point to x and y coords of ent that are closer to org
167 // z coord is set to ent's min height
168 tracewalk_dest.x = closer_dest.x;
169 tracewalk_dest.y = closer_dest.y;
170 tracewalk_dest.z = wm1.z;
171 tracewalk_dest_height = wm2.z - wm1.z; // destination height
175 closer_dest = ent.origin;
176 tracewalk_dest = closer_dest;
177 tracewalk_dest_height = 0;
182 bool navigation_check_submerged_state(entity ent, vector pos)
186 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
187 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
188 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
191 submerged = SUBMERGED(pos);
192 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
193 // is actually submerged because often they are inside some solid.
194 // That's why submerged state is saved now that we know current pos is
195 // not stuck in solid (previous tracewalk call to this pos was successfully)
196 if(!ent.navigation_dynamicgoal)
197 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
202 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
204 IL_EACH(g_ladders, it.classname == "func_ladder",
207 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
208 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
211 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
212 tracebox(org, m1, m2, top, movemode, e);
213 if(trace_fraction == 1)
220 vector resurface_limited(vector org, float lim, vector m1)
222 if (WETFEET(org + eZ * (lim - org.z)))
226 float RES_min_h = org.z;
227 float RES_max_h = lim;
229 org.z = 0.5 * (RES_min_h + RES_max_h);
234 } while (RES_max_h - RES_min_h >= 1);
239 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
242 #define NAV_SWIM_ONWATER 1
243 #define NAV_SWIM_UNDERWATER 2
245 // rough simulation of walking from one point to another to test if a path
246 // can be traveled, used for waypoint linking and havocbot
247 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
248 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
250 if(autocvar_bot_debug_tracewalk)
257 vector flatdir = end - start;
259 float flatdist = vlen(flatdir);
260 flatdir = normalize(flatdir);
262 bool ignorehazards = false;
265 // Analyze starting point
266 traceline(start, start, MOVE_NORMAL, e);
267 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
268 ignorehazards = true;
270 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
271 if (trace_startsolid)
274 if(autocvar_bot_debug_tracewalk)
275 debugnodestatus(start, DEBUG_NODE_FAIL);
277 //print("tracewalk: ", vtos(start), " is a bad start\n");
283 end2.z += end_height;
285 vector fixed_end = end;
288 if (flatdist > 0 && WETFEET(org))
291 nav_action = NAV_SWIM_UNDERWATER;
294 // tracebox down by player's height
295 // useful to know if water level is so low that bot can still walk
296 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
297 if (SUBMERGED(trace_endpos))
300 nav_action = NAV_SWIM_UNDERWATER;
303 nav_action = NAV_WALK;
307 nav_action = NAV_WALK;
315 if (org.z > end2.z + 1)
317 tracebox(org, m1, m2, end2, movemode, e);
319 if (org.z > end2.z + 1)
322 else if (org.z < end.z - 1)
324 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
325 if (SUBMERGED(trace_endpos))
327 vector v = trace_endpos;
328 tracebox(v, m1, m2, end, movemode, e);
329 if(trace_endpos.z >= end.z - 1)
331 RESURFACE_LIMITED(v, trace_endpos.z);
335 else if (trace_endpos.z > org.z - jumpheight_vec.z)
336 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
338 if (org.z < end.z - 1)
345 if(autocvar_bot_debug_tracewalk)
348 debugnodestatus(org, DEBUG_NODE_SUCCESS);
351 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
356 if(autocvar_bot_debug_tracewalk)
362 if (stepdist > flatdist)
364 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
366 // can't use movement direction here to calculate move because of
367 // precision errors especially when direction has a high enough z value
368 //water_dir = normalize(water_end - org);
369 //move = org + water_dir * stepdist;
370 fixed_end.z = bound(end.z, org.z, end2.z);
371 if (stepdist == flatdist) {
375 move = org + (fixed_end - org) * (stepdist / flatdist);
376 flatdist = vlen(vec2(fixed_end - move));
379 else // horiz. direction
381 flatdist -= stepdist;
382 move = org + flatdir * stepdist;
385 if(nav_action == NAV_SWIM_ONWATER)
387 tracebox(org, m1, m2, move, movemode, e); // swim
390 if (trace_fraction < 1)
393 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
395 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
398 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
400 if(autocvar_bot_debug_tracewalk)
403 debugnodestatus(org, DEBUG_NODE_SUCCESS);
406 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
410 if(autocvar_bot_debug_tracewalk)
411 debugnodestatus(org, DEBUG_NODE_FAIL);
414 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
425 if (org.z <= move.z) // going horiz.
427 tracebox(trace_endpos, m1, m2, move, movemode, e);
429 nav_action = NAV_WALK;
434 if (org.z <= move.z) // going horiz.
437 nav_action = NAV_SWIM_ONWATER;
443 nav_action = NAV_SWIM_UNDERWATER;
445 nav_action = NAV_SWIM_ONWATER;
448 else if(nav_action == NAV_SWIM_UNDERWATER)
450 if (move.z >= org.z) // swimming upwards or horiz.
452 tracebox(org, m1, m2, move, movemode, e); // swim
454 bool stepswum = false;
457 if (trace_fraction < 1)
460 vector stepswim_move = move + stepheightvec;
461 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
462 stepswim_move.z = end2.z;
464 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
467 if (trace_startsolid)
469 if(autocvar_bot_debug_tracewalk)
470 debugnodestatus(org, DEBUG_NODE_FAIL);
472 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
476 if (trace_fraction < 1)
478 float org_z_prev = org.z;
479 RESURFACE_LIMITED(org, end2.z);
480 if(org.z == org_z_prev)
482 if(autocvar_bot_debug_tracewalk)
483 debugnodestatus(org, DEBUG_NODE_FAIL);
485 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
489 nav_action = NAV_SWIM_UNDERWATER;
491 nav_action = NAV_SWIM_ONWATER;
493 // we didn't advance horiz. in this step, flatdist decrease should be reverted
494 // but we can't do it properly right now... apply this workaround instead
512 if (!WETFEET(trace_endpos))
514 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
515 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
516 if (!stepswum && SUBMERGED(trace_endpos))
518 RESURFACE_LIMITED(trace_endpos, end2.z);
520 nav_action = NAV_SWIM_ONWATER;
526 nav_action = NAV_WALK;
532 nav_action = NAV_SWIM_UNDERWATER;
535 else //if (move.z < org.z) // swimming downwards
537 tracebox(org, m1, m2, move, movemode, e); // swim
540 if (trace_fraction < 1)
543 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
546 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
548 if(autocvar_bot_debug_tracewalk)
549 debugnodestatus(move, DEBUG_NODE_FAIL);
551 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
563 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
565 // stepswim caused upwards direction
566 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
567 if (!SUBMERGED(trace_endpos))
570 nav_action = NAV_WALK;
577 nav_action = NAV_SWIM_UNDERWATER;
581 else if(nav_action == NAV_WALK)
584 tracebox(org, m1, m2, move, movemode, e);
586 if(autocvar_bot_debug_tracewalk)
587 debugnode(e, trace_endpos);
590 if (trace_fraction < 1)
592 // check if we can walk over this obstacle, possibly by jumpstepping
593 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
594 if (trace_fraction < 1 || trace_startsolid)
596 if (trace_startsolid) // hit ceiling above org
598 // reduce stepwalk height
599 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
600 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
602 else //if (trace_fraction < 1)
604 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
605 if (trace_startsolid) // hit ceiling above org
607 // reduce jumpstepwalk height
608 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
609 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
613 if (trace_fraction < 1)
615 vector v = trace_endpos;
616 v.z = org.z + jumpheight_vec.z;
617 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
619 if(autocvar_bot_debug_tracewalk)
622 debugnodestatus(v, DEBUG_NODE_SUCCESS);
625 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
629 if(autocvar_bot_debug_tracewalk)
630 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
632 traceline( org, move, movemode, e);
634 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
638 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
640 nextmove = move + (flatdir * stepdist);
641 traceline( move, nextmove, movemode, e);
644 flatdist = vlen(vec2(end - move));
648 if(autocvar_bot_debug_tracewalk)
649 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
651 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
652 //te_explosion(trace_endpos);
653 //print(ftos(e.dphitcontentsmask), "\n");
654 return false; // failed
666 // trace down from stepheight as far as possible and move there,
667 // if this starts in solid we try again without the stepup, and
668 // if that also fails we assume it is a wall
669 // (this is the same logic as the Quake walkmove function used)
670 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
678 if(autocvar_bot_debug_tracewalk)
680 debugnode(e, trace_endpos);
681 debugnodestatus(org, DEBUG_NODE_FAIL);
684 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
691 if(move.z >= end2.z && org.z < end2.z)
696 if(org.z > move.z - 1 || !SUBMERGED(org))
698 nav_action = NAV_WALK;
702 // ended up submerged while walking
703 if(autocvar_bot_debug_tracewalk)
706 RESURFACE_LIMITED(org, move.z);
707 nav_action = NAV_SWIM_ONWATER;
712 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
714 // moved but didn't arrive at the intended destination
715 if(autocvar_bot_debug_tracewalk)
716 debugnodestatus(org, DEBUG_NODE_FAIL);
721 /////////////////////////////////////////////////////////////////////////////
723 /////////////////////////////////////////////////////////////////////////////
725 // completely empty the goal stack, used when deciding where to go
726 void navigation_clearroute(entity this)
728 this.goalcurrent_prev = this.goalcurrent;
729 this.goalcurrent_distance_2d = FLOAT_MAX;
730 this.goalcurrent_distance_z = FLOAT_MAX;
731 this.goalcurrent_distance_time = 0;
732 this.goalentity_lock_timeout = 0;
733 this.goalentity = NULL;
734 this.goalcurrent = NULL;
735 this.goalstack01 = NULL;
736 this.goalstack02 = NULL;
737 this.goalstack03 = NULL;
738 this.goalstack04 = NULL;
739 this.goalstack05 = NULL;
740 this.goalstack06 = NULL;
741 this.goalstack07 = NULL;
742 this.goalstack08 = NULL;
743 this.goalstack09 = NULL;
744 this.goalstack10 = NULL;
745 this.goalstack11 = NULL;
746 this.goalstack12 = NULL;
747 this.goalstack13 = NULL;
748 this.goalstack14 = NULL;
749 this.goalstack15 = NULL;
750 this.goalstack16 = NULL;
751 this.goalstack17 = NULL;
752 this.goalstack18 = NULL;
753 this.goalstack19 = NULL;
754 this.goalstack20 = NULL;
755 this.goalstack21 = NULL;
756 this.goalstack22 = NULL;
757 this.goalstack23 = NULL;
758 this.goalstack24 = NULL;
759 this.goalstack25 = NULL;
760 this.goalstack26 = NULL;
761 this.goalstack27 = NULL;
762 this.goalstack28 = NULL;
763 this.goalstack29 = NULL;
764 this.goalstack30 = NULL;
765 this.goalstack31 = NULL;
768 // add a new goal at the beginning of the stack
769 // (in other words: add a new prerequisite before going to the later goals)
770 // NOTE: when a waypoint is added, the WP gets pushed first, then the
771 // next-closest WP on the shortest path to the WP
772 // That means, if the stack overflows, the bot will know how to do the FIRST 32
773 // steps to the goal, and then recalculate the path.
774 void navigation_pushroute(entity this, entity e)
776 this.goalcurrent_prev = this.goalcurrent;
777 this.goalcurrent_distance_2d = FLOAT_MAX;
778 this.goalcurrent_distance_z = FLOAT_MAX;
779 this.goalcurrent_distance_time = 0;
780 //print("bot ", etos(this), " push ", etos(e), "\n");
781 if(this.goalstack31 == this.goalentity)
782 this.goalentity = NULL;
783 this.goalstack31 = this.goalstack30;
784 this.goalstack30 = this.goalstack29;
785 this.goalstack29 = this.goalstack28;
786 this.goalstack28 = this.goalstack27;
787 this.goalstack27 = this.goalstack26;
788 this.goalstack26 = this.goalstack25;
789 this.goalstack25 = this.goalstack24;
790 this.goalstack24 = this.goalstack23;
791 this.goalstack23 = this.goalstack22;
792 this.goalstack22 = this.goalstack21;
793 this.goalstack21 = this.goalstack20;
794 this.goalstack20 = this.goalstack19;
795 this.goalstack19 = this.goalstack18;
796 this.goalstack18 = this.goalstack17;
797 this.goalstack17 = this.goalstack16;
798 this.goalstack16 = this.goalstack15;
799 this.goalstack15 = this.goalstack14;
800 this.goalstack14 = this.goalstack13;
801 this.goalstack13 = this.goalstack12;
802 this.goalstack12 = this.goalstack11;
803 this.goalstack11 = this.goalstack10;
804 this.goalstack10 = this.goalstack09;
805 this.goalstack09 = this.goalstack08;
806 this.goalstack08 = this.goalstack07;
807 this.goalstack07 = this.goalstack06;
808 this.goalstack06 = this.goalstack05;
809 this.goalstack05 = this.goalstack04;
810 this.goalstack04 = this.goalstack03;
811 this.goalstack03 = this.goalstack02;
812 this.goalstack02 = this.goalstack01;
813 this.goalstack01 = this.goalcurrent;
814 this.goalcurrent = e;
817 // remove first goal from stack
818 // (in other words: remove a prerequisite for reaching the later goals)
819 // (used when a spawnfunc_waypoint is reached)
820 void navigation_poproute(entity this)
822 this.goalcurrent_prev = this.goalcurrent;
823 this.goalcurrent_distance_2d = FLOAT_MAX;
824 this.goalcurrent_distance_z = FLOAT_MAX;
825 this.goalcurrent_distance_time = 0;
826 //print("bot ", etos(this), " pop\n");
827 if(this.goalcurrent == this.goalentity)
829 this.goalentity = NULL;
830 this.goalentity_lock_timeout = 0;
832 this.goalcurrent = this.goalstack01;
833 this.goalstack01 = this.goalstack02;
834 this.goalstack02 = this.goalstack03;
835 this.goalstack03 = this.goalstack04;
836 this.goalstack04 = this.goalstack05;
837 this.goalstack05 = this.goalstack06;
838 this.goalstack06 = this.goalstack07;
839 this.goalstack07 = this.goalstack08;
840 this.goalstack08 = this.goalstack09;
841 this.goalstack09 = this.goalstack10;
842 this.goalstack10 = this.goalstack11;
843 this.goalstack11 = this.goalstack12;
844 this.goalstack12 = this.goalstack13;
845 this.goalstack13 = this.goalstack14;
846 this.goalstack14 = this.goalstack15;
847 this.goalstack15 = this.goalstack16;
848 this.goalstack16 = this.goalstack17;
849 this.goalstack17 = this.goalstack18;
850 this.goalstack18 = this.goalstack19;
851 this.goalstack19 = this.goalstack20;
852 this.goalstack20 = this.goalstack21;
853 this.goalstack21 = this.goalstack22;
854 this.goalstack22 = this.goalstack23;
855 this.goalstack23 = this.goalstack24;
856 this.goalstack24 = this.goalstack25;
857 this.goalstack25 = this.goalstack26;
858 this.goalstack26 = this.goalstack27;
859 this.goalstack27 = this.goalstack28;
860 this.goalstack28 = this.goalstack29;
861 this.goalstack29 = this.goalstack30;
862 this.goalstack30 = this.goalstack31;
863 this.goalstack31 = NULL;
866 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
867 // waypoint destination coordinates instead of v (only useful for box waypoints)
868 // for normal waypoints v2 == v and v2_height == 0
869 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)
871 if (vdist(v - org, <, bestdist))
873 traceline(v, org, true, ent);
874 if (trace_fraction == 1)
878 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
883 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
891 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
892 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
895 ent = ent.tag_entity;
897 vector pm1 = ent.origin + ent.mins;
898 vector pm2 = ent.origin + ent.maxs;
900 // do two scans, because box test is cheaper
901 IL_EACH(g_waypoints, it != ent && it != except,
903 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
905 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
907 waypoint_clearlinks(ent); // initialize wpXXmincost fields
908 navigation_item_addlink(it, ent);
914 vector org = ent.origin;
915 if (navigation_testtracewalk)
921 if(ent.size && !IS_PLAYER(ent))
923 org += 0.5 * (ent.mins + ent.maxs);
924 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
927 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
929 waypoint_clearlinks(ent); // initialize wpXXmincost fields
930 IL_EACH(g_waypoints, it != ent,
932 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
935 set_tracewalk_dest(ent, it.origin, false);
936 if (vdist(tracewalk_dest - it.origin, <, 1050)
937 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
938 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
940 navigation_item_addlink(it, ent);
945 // box check failed, try walk
946 IL_EACH(g_waypoints, it != ent,
948 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
954 set_tracewalk_dest(ent, v, true);
955 if (trace_ent == ent)
963 set_tracewalk_dest(it, org, false);
965 if (navigation_waypoint_will_link(v, org, ent,
966 tracewalk_dest, tracewalk_dest_height,
967 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
970 bestdist = vlen(tracewalk_dest - org);
972 bestdist = vlen(v - org);
976 if(!best && !ent.navigation_dynamicgoal)
978 int solid_save = ent.solid;
979 ent.solid = SOLID_BSP;
980 IL_EACH(g_jumppads, true,
982 if(trigger_push_test(it, ent))
984 best = it.nearestwaypoint;
988 ent.solid = solid_save;
992 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
994 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
995 if (autocvar_g_waypointeditor_auto)
997 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
999 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1004 // finds the waypoints near the bot initiating a navigation query
1005 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1007 //navigation_testtracewalk = true;
1009 IL_EACH(g_waypoints, !it.wpconsidered,
1011 set_tracewalk_dest(it, this.origin, false);
1013 vector diff = tracewalk_dest - this.origin;
1014 diff.z = max(0, diff.z);
1015 if(vdist(diff, <, maxdist))
1017 it.wpconsidered = true;
1018 if (tracewalk(this, this.origin, this.mins, this.maxs,
1019 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1021 it.wpnearestpoint = tracewalk_dest;
1022 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1029 //navigation_testtracewalk = false;
1033 // updates a path link if a spawnfunc_waypoint link is better than the current one
1034 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1040 m1 = wp.origin + wp.mins;
1041 m2 = wp.origin + wp.maxs;
1042 v.x = bound(m1_x, p.x, m2_x);
1043 v.y = bound(m1_y, p.y, m2_y);
1044 v.z = bound(m1_z, p.z, m2_z);
1048 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1049 cost += w.wp00mincost; // assuming teleport has exactly one destination
1051 cost += waypoint_gettravelcost(p, v, w, wp);
1052 if (wp.wpcost > cost)
1057 wp.wpnearestpoint = v;
1061 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1062 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1067 IL_EACH(g_waypoints, true,
1069 it.wpconsidered = false;
1070 it.wpnearestpoint = '0 0 0';
1071 it.wpcost = 10000000;
1076 if(fixed_source_waypoint)
1078 fixed_source_waypoint.wpconsidered = true;
1079 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1080 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1081 fixed_source_waypoint.wpfire = 1;
1082 fixed_source_waypoint.enemy = NULL;
1086 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1087 // as this search is expensive we will use lower values if the bot is on the air
1088 float increment, maxdistance;
1089 if(IS_ONGROUND(this))
1092 maxdistance = 50000;
1100 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1103 bool searching = true;
1107 IL_EACH(g_waypoints, it.wpfire,
1112 p = it.wpnearestpoint;
1114 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1115 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1116 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1117 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1118 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1119 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1120 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1121 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1122 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1123 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1124 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1125 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1126 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1127 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1128 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1129 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1130 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1131 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1132 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1133 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1151 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1152 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1156 IL_EACH(g_waypoints, true,
1158 it.wpconsidered = false;
1159 it.wpnearestpoint = '0 0 0';
1160 it.wpcost = 10000000;
1165 if(fixed_source_waypoint)
1167 fixed_source_waypoint.wpconsidered = true;
1168 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1169 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1170 fixed_source_waypoint.wpfire = 1;
1171 fixed_source_waypoint.enemy = NULL;
1175 error("need to start with a waypoint\n");
1178 bool searching = true;
1182 IL_EACH(g_waypoints, it.wpfire,
1186 cost = it.wpcost; // cost to walk from it to home
1187 p = it.wpnearestpoint;
1189 IL_EACH(g_waypoints, it != wp,
1191 if(!waypoint_islinked(it, wp))
1193 cost2 = cost + it.dmg;
1194 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1200 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1201 void navigation_routerating(entity this, entity e, float f, float rangebias)
1209 rangebias = waypoint_getlinearcost(rangebias);
1210 f = waypoint_getlinearcost(f);
1214 bool rate_wps = false;
1215 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1220 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1221 int t = pointcontents(trace_endpos + '0 0 1');
1222 if(t != CONTENT_SOLID )
1224 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1226 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1233 entity theEnemy = e;
1234 entity best_wp = NULL;
1235 float best_dist = 10000;
1236 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1237 && vdist(it.origin - this.origin, >, 100)
1238 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1240 float dist = vlen(it.origin - theEnemy.origin);
1241 if (dist < best_dist)
1253 vector goal_org = (e.absmin + e.absmax) * 0.5;
1255 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1257 // Evaluate path using jetpack
1259 if(this.items & IT_JETPACK)
1260 if(autocvar_bot_ai_navigation_jetpack)
1261 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1263 vector pointa, pointb;
1265 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1268 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1269 pointa = trace_endpos - '0 0 1';
1272 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1273 pointb = trace_endpos - '0 0 1';
1275 // Can I see these two points from the sky?
1276 traceline(pointa, pointb, MOVE_NORMAL, this);
1278 if(trace_fraction==1)
1280 LOG_DEBUG("jetpack ai: can bridge these two points");
1282 // Lower the altitude of these points as much as possible
1283 float zdistance, xydistance, cost, t, fuel;
1284 vector down, npa, npb;
1286 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1289 npa = pointa + down;
1290 npb = pointb + down;
1292 if(npa.z<=this.absmax.z)
1295 if(npb.z<=e.absmax.z)
1298 traceline(npa, npb, MOVE_NORMAL, this);
1299 if(trace_fraction==1)
1305 while(trace_fraction == 1);
1308 // Rough estimation of fuel consumption
1309 // (ignores acceleration and current xyz velocity)
1310 xydistance = vlen(pointa - pointb);
1311 zdistance = fabs(pointa.z - this.origin.z);
1313 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1314 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1315 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1317 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1320 if(this.ammo_fuel>fuel)
1323 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1324 // - between air and ground speeds)
1326 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1327 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1330 // Compare against other goals
1331 f = f * rangebias / (rangebias + cost);
1333 if (navigation_bestrating < f)
1335 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1336 navigation_bestrating = f;
1337 navigation_bestgoal = e;
1338 this.navigation_jetpack_goal = e;
1339 this.navigation_jetpack_point = pointb;
1347 //te_wizspike(e.origin);
1350 // update the cached spawnfunc_waypoint link on a dynamic item entity
1351 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1357 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1358 e.nearestwaypoint = NULL;
1360 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1361 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1363 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1364 e.nearestwaypoint = nwp = e.goalcurrent;
1366 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1369 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1371 if(!e.navigation_dynamicgoal)
1372 e.blacklisted = true;
1376 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1381 if(e.navigation_dynamicgoal)
1382 e.nearestwaypointtimeout = time + 2;
1383 else if(autocvar_g_waypointeditor)
1384 e.nearestwaypointtimeout = time + 3 + random() * 2;
1386 nwp = e.nearestwaypoint;
1389 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1390 if (nwp && nwp.wpcost < 10000000)
1392 //te_wizspike(nwp.wpnearestpoint);
1393 float nwptoitem_cost = 0;
1394 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1395 nwptoitem_cost = nwp.wp00mincost;
1397 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1398 float cost = nwp.wpcost + nwptoitem_cost;
1399 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1400 f = f * rangebias / (rangebias + cost);
1401 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1402 if (navigation_bestrating < f)
1404 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1405 navigation_bestrating = f;
1406 navigation_bestgoal = e;
1411 // adds an item to the the goal stack with the path to a given item
1412 bool navigation_routetogoal(entity this, entity e, vector startposition)
1414 // if there is no goal, just exit
1418 entity teleport_goal = NULL;
1420 this.goalentity = e;
1422 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1424 // force teleport destination as route destination
1426 navigation_pushroute(this, e.wp00);
1427 this.goalentity = e.wp00;
1430 // put the entity on the goal stack
1431 //print("routetogoal ", etos(e), "\n");
1432 navigation_pushroute(this, e);
1435 e = this.goalentity;
1437 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1439 this.wp_goal_prev1 = this.wp_goal_prev0;
1440 this.wp_goal_prev0 = e;
1444 if(e==this.navigation_jetpack_goal)
1447 // if it can reach the goal there is nothing more to do
1448 set_tracewalk_dest(e, startposition, true);
1449 if (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1450 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1455 entity nearest_wp = NULL;
1456 // see if there are waypoints describing a path to the item
1457 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1459 e = e.nearestwaypoint;
1462 else if(teleport_goal)
1465 e = e.enemy; // we already have added it, so...
1470 if(nearest_wp && nearest_wp.enemy)
1472 // often path can be optimized by not adding the nearest waypoint
1473 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1475 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1476 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1477 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1478 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1480 e = nearest_wp.enemy;
1483 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1484 e = nearest_wp.enemy;
1489 // add the spawnfunc_waypoint to the path
1490 navigation_pushroute(this, e);
1500 // removes any currently touching waypoints from the goal stack
1501 // (this is how bots detect if they reached a goal)
1502 int navigation_poptouchedgoals(entity this)
1504 int removed_goals = 0;
1506 if(!this.goalcurrent)
1507 return removed_goals;
1509 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1511 // make sure jumppad is really hit, don't rely on distance based checks
1512 // as they may report a touch even if it didn't really happen
1513 if(this.lastteleporttime > 0
1514 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1516 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1517 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1519 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1520 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1522 navigation_poproute(this);
1523 this.lastteleporttime = 0;
1527 return removed_goals;
1530 // If for some reason the bot is closer to the next goal, pop the current one
1531 // randomness should help to get unstuck bot on certain hard paths with climbs and tight corners
1532 if (this.goalstack01 && !wasfreed(this.goalstack01) && random() < 0.7)
1534 entity next = IS_PLAYER(this.goalentity) ? this.goalentity : this.goalstack01;
1535 if (vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1536 && checkpvs(this.origin + this.view_ofs, next))
1538 set_tracewalk_dest(next, this.origin, true);
1539 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1540 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1542 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1545 // loop clears the whole route if next is a player
1546 navigation_poproute(this);
1549 while (this.goalcurrent == next);
1550 if (this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1551 return removed_goals;
1552 // TODO this may also be a nice idea to do "early" (e.g. by
1553 // manipulating the vlen() comparisons) to shorten paths in
1554 // general - this would make bots walk more "on rails" than
1555 // "zigzagging" which they currently do with sufficiently
1556 // random-like waypoints, and thus can make a nice bot
1557 // personality property
1562 // Loose goal touching check when running
1563 if(this.aistatus & AI_STATUS_RUNNING)
1564 if(this.goalcurrent.classname=="waypoint")
1565 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1567 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1569 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1570 if(trace_fraction==1)
1572 // Detect personal waypoints
1573 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1574 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1576 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1577 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1580 navigation_poproute(this);
1582 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1583 return removed_goals;
1588 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1590 vector gc_min = this.goalcurrent.absmin;
1591 vector gc_max = this.goalcurrent.absmax;
1592 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1594 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1595 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1597 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1600 // Detect personal waypoints
1601 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1602 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1604 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1605 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1608 navigation_poproute(this);
1610 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1611 return removed_goals;
1613 return removed_goals;
1616 entity navigation_get_really_close_waypoint(entity this)
1618 entity wp = this.goalcurrent;
1619 if(!wp || vdist(wp.origin - this.origin, >, 50))
1620 wp = this.goalcurrent_prev;
1623 if(wp.classname != "waypoint")
1625 wp = wp.nearestwaypoint;
1629 if(vdist(wp.origin - this.origin, >, 50))
1631 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1633 if(vdist(it.origin - this.origin, <, 50))
1640 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1643 set_tracewalk_dest(wp, this.origin, false);
1644 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1645 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1652 // begin a goal selection session (queries spawnfunc_waypoint network)
1653 void navigation_goalrating_start(entity this)
1655 if(this.aistatus & AI_STATUS_STUCK)
1658 this.navigation_jetpack_goal = NULL;
1659 navigation_bestrating = -1;
1660 entity wp = navigation_get_really_close_waypoint(this);
1661 navigation_clearroute(this);
1662 navigation_bestgoal = NULL;
1663 navigation_markroutes(this, wp);
1666 // ends a goal selection session (updates goal stack to the best goal)
1667 void navigation_goalrating_end(entity this)
1669 if(this.aistatus & AI_STATUS_STUCK)
1672 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1673 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1675 // If the bot got stuck then try to reach the farthest waypoint
1676 if (!this.goalentity && autocvar_bot_wander_enable)
1678 if (!(this.aistatus & AI_STATUS_STUCK))
1680 LOG_DEBUG(this.netname, " cannot walk to any goal");
1681 this.aistatus |= AI_STATUS_STUCK;
1686 void botframe_updatedangerousobjects(float maxupdate)
1688 vector m1, m2, v, o;
1692 IL_EACH(g_waypoints, true,
1698 IL_EACH(g_bot_dodge, it.bot_dodge,
1701 v.x = bound(m1_x, v.x, m2_x);
1702 v.y = bound(m1_y, v.y, m2_y);
1703 v.z = bound(m1_z, v.z, m2_z);
1704 o = (it.absmin + it.absmax) * 0.5;
1705 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1708 traceline(o, v, true, NULL);
1709 if (trace_fraction == 1)
1710 danger = danger + d;
1720 void navigation_unstuck(entity this)
1722 float search_radius = 1000;
1724 if (!autocvar_bot_wander_enable)
1727 if (!bot_waypoint_queue_owner)
1729 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1730 bot_waypoint_queue_owner = this;
1731 bot_waypoint_queue_bestgoal = NULL;
1732 bot_waypoint_queue_bestgoalrating = 0;
1735 if(bot_waypoint_queue_owner!=this)
1738 if (bot_waypoint_queue_goal)
1740 // evaluate the next goal on the queue
1741 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1742 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1743 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1744 if (tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1745 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1747 if( d > bot_waypoint_queue_bestgoalrating)
1749 bot_waypoint_queue_bestgoalrating = d;
1750 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1753 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1755 if (!bot_waypoint_queue_goal)
1757 if (bot_waypoint_queue_bestgoal)
1759 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1760 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1761 navigation_goalrating_timeout_set(this);
1762 this.aistatus &= ~AI_STATUS_STUCK;
1766 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1769 bot_waypoint_queue_owner = NULL;
1774 if(bot_strategytoken!=this)
1777 // build a new queue
1778 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1780 entity first = NULL;
1782 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1784 if(bot_waypoint_queue_goal)
1785 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1789 bot_waypoint_queue_goal = it;
1790 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1794 bot_waypoint_queue_goal = first;
1797 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1798 bot_waypoint_queue_owner = NULL;
1803 // Support for debugging tracewalk visually
1805 void debugresetnodes()
1807 debuglastnode = '0 0 0';
1810 void debugnode(entity this, vector node)
1812 if (!IS_PLAYER(this))
1815 if(debuglastnode=='0 0 0')
1817 debuglastnode = node;
1821 te_lightning2(NULL, node, debuglastnode);
1822 debuglastnode = node;
1825 void debugnodestatus(vector position, float status)
1831 case DEBUG_NODE_SUCCESS:
1834 case DEBUG_NODE_WARNING:
1837 case DEBUG_NODE_FAIL:
1844 te_customflash(position, 40, 2, c);
1847 // Support for debugging the goal stack visually
1850 .vector lastposition;
1852 // Debug the goal stack visually
1853 void debuggoalstack(entity this)
1858 if(this.goalcounter==0)goal=this.goalcurrent;
1859 else if(this.goalcounter==1)goal=this.goalstack01;
1860 else if(this.goalcounter==2)goal=this.goalstack02;
1861 else if(this.goalcounter==3)goal=this.goalstack03;
1862 else if(this.goalcounter==4)goal=this.goalstack04;
1863 else if(this.goalcounter==5)goal=this.goalstack05;
1864 else if(this.goalcounter==6)goal=this.goalstack06;
1865 else if(this.goalcounter==7)goal=this.goalstack07;
1866 else if(this.goalcounter==8)goal=this.goalstack08;
1867 else if(this.goalcounter==9)goal=this.goalstack09;
1868 else if(this.goalcounter==10)goal=this.goalstack10;
1869 else if(this.goalcounter==11)goal=this.goalstack11;
1870 else if(this.goalcounter==12)goal=this.goalstack12;
1871 else if(this.goalcounter==13)goal=this.goalstack13;
1872 else if(this.goalcounter==14)goal=this.goalstack14;
1873 else if(this.goalcounter==15)goal=this.goalstack15;
1874 else if(this.goalcounter==16)goal=this.goalstack16;
1875 else if(this.goalcounter==17)goal=this.goalstack17;
1876 else if(this.goalcounter==18)goal=this.goalstack18;
1877 else if(this.goalcounter==19)goal=this.goalstack19;
1878 else if(this.goalcounter==20)goal=this.goalstack20;
1879 else if(this.goalcounter==21)goal=this.goalstack21;
1880 else if(this.goalcounter==22)goal=this.goalstack22;
1881 else if(this.goalcounter==23)goal=this.goalstack23;
1882 else if(this.goalcounter==24)goal=this.goalstack24;
1883 else if(this.goalcounter==25)goal=this.goalstack25;
1884 else if(this.goalcounter==26)goal=this.goalstack26;
1885 else if(this.goalcounter==27)goal=this.goalstack27;
1886 else if(this.goalcounter==28)goal=this.goalstack28;
1887 else if(this.goalcounter==29)goal=this.goalstack29;
1888 else if(this.goalcounter==30)goal=this.goalstack30;
1889 else if(this.goalcounter==31)goal=this.goalstack31;
1894 this.goalcounter = 0;
1895 this.lastposition='0 0 0';
1899 if(this.lastposition=='0 0 0')
1902 org = this.lastposition;
1905 go = ( goal.absmin + goal.absmax ) * 0.5;
1906 te_lightning2(NULL, org, go);
1907 this.lastposition = go;