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 #define MAX_CHASE_DISTANCE 700
50 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
52 if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
55 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
57 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
58 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
60 this.ignoregoal = this.goalentity;
61 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
68 void navigation_dynamicgoal_init(entity this, bool initially_static)
70 this.navigation_dynamicgoal = true;
71 this.bot_basewaypoint = this.nearestwaypoint;
73 this.nearestwaypointtimeout = -1;
75 this.nearestwaypointtimeout = time;
78 void navigation_dynamicgoal_set(entity this)
80 this.nearestwaypointtimeout = time;
81 if (this.nearestwaypoint)
82 this.nearestwaypointtimeout += 2;
85 void navigation_dynamicgoal_unset(entity this)
87 if(this.bot_basewaypoint)
88 this.nearestwaypoint = this.bot_basewaypoint;
89 this.nearestwaypointtimeout = -1;
92 // returns point of ent closer to org
93 vector get_closer_dest(entity ent, vector org)
95 vector dest = '0 0 0';
96 if ((ent.classname != "waypoint") || ent.wpisbox)
98 vector wm1 = ent.origin + ent.mins;
99 vector wm2 = ent.origin + ent.maxs;
100 dest.x = bound(wm1.x, org.x, wm2.x);
101 dest.y = bound(wm1.y, org.y, wm2.y);
102 dest.z = bound(wm1.z, org.z, wm2.z);
109 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
111 if ((ent.classname != "waypoint") || ent.wpisbox)
113 vector wm1 = ent.origin + ent.mins;
114 vector wm2 = ent.origin + ent.maxs;
115 if (IS_PLAYER(ent) || IS_MONSTER(ent))
117 // move destination point out of player bbox otherwise tracebox always fails
118 // (if bot_navigation_ignoreplayers is false)
119 wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
120 wm2 += vec2(PL_MAX_CONST) + '1 1 0';
122 // set destination point to x and y coords of ent that are closer to org
123 // z coord is set to ent's min height
124 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
125 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
126 tracewalk_dest.z = wm1.z;
127 tracewalk_dest_height = wm2.z - wm1.z; // destination height
131 tracewalk_dest = ent.origin;
132 tracewalk_dest_height = 0;
134 if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
136 // snap player to the ground
137 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
139 // bot is right under the player
140 tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
141 tracewalk_dest = trace_endpos;
142 tracewalk_dest_height = 0;
146 tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
147 if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
149 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
150 tracewalk_dest.z = trace_endpos.z;
156 // returns point of ent closer to org
157 vector set_tracewalk_dest_2(entity ent, vector org)
159 vector closer_dest = '0 0 0';
160 if ((ent.classname != "waypoint") || ent.wpisbox)
162 vector wm1 = ent.origin + ent.mins;
163 vector wm2 = ent.origin + ent.maxs;
164 closer_dest.x = bound(wm1.x, org.x, wm2.x);
165 closer_dest.y = bound(wm1.y, org.y, wm2.y);
166 closer_dest.z = bound(wm1.z, org.z, wm2.z);
167 // set destination point to x and y coords of ent that are closer to org
168 // z coord is set to ent's min height
169 tracewalk_dest.x = closer_dest.x;
170 tracewalk_dest.y = closer_dest.y;
171 tracewalk_dest.z = wm1.z;
172 tracewalk_dest_height = wm2.z - wm1.z; // destination height
176 closer_dest = ent.origin;
177 tracewalk_dest = closer_dest;
178 tracewalk_dest_height = 0;
183 bool navigation_check_submerged_state(entity ent, vector pos)
187 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
188 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
189 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
192 submerged = SUBMERGED(pos);
193 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
194 // is actually submerged because often they are inside some solid.
195 // That's why submerged state is saved now that we know current pos is
196 // not stuck in solid (previous tracewalk call to this pos was successfully)
197 if(!ent.navigation_dynamicgoal)
198 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
203 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
205 IL_EACH(g_ladders, it.classname == "func_ladder",
208 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
209 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
212 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
213 tracebox(org, m1, m2, top, movemode, e);
214 if(trace_fraction == 1)
221 vector resurface_limited(vector org, float lim, vector m1)
223 if (WETFEET(org + eZ * (lim - org.z)))
227 float RES_min_h = org.z;
228 float RES_max_h = lim;
230 org.z = 0.5 * (RES_min_h + RES_max_h);
235 } while (RES_max_h - RES_min_h >= 1);
240 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
243 #define NAV_SWIM_ONWATER 1
244 #define NAV_SWIM_UNDERWATER 2
246 // rough simulation of walking from one point to another to test if a path
247 // can be traveled, used for waypoint linking and havocbot
248 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
249 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
251 if(autocvar_bot_debug_tracewalk)
258 vector flatdir = end - start;
260 float flatdist = vlen(flatdir);
261 flatdir = normalize(flatdir);
263 bool ignorehazards = false;
266 // Analyze starting point
267 traceline(start, start, MOVE_NORMAL, e);
268 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
269 ignorehazards = true;
271 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
272 if (trace_startsolid)
275 if(autocvar_bot_debug_tracewalk)
276 debugnodestatus(start, DEBUG_NODE_FAIL);
278 //print("tracewalk: ", vtos(start), " is a bad start\n");
284 end2.z += end_height;
286 vector fixed_end = end;
289 if (flatdist > 0 && WETFEET(org))
292 nav_action = NAV_SWIM_UNDERWATER;
295 // tracebox down by player's height
296 // useful to know if water level is so low that bot can still walk
297 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
298 if (SUBMERGED(trace_endpos))
301 nav_action = NAV_SWIM_UNDERWATER;
304 nav_action = NAV_WALK;
308 nav_action = NAV_WALK;
316 if (org.z > end2.z + 1)
318 tracebox(org, m1, m2, end2, movemode, e);
320 if (org.z > end2.z + 1)
323 else if (org.z < end.z - 1)
325 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
326 if (SUBMERGED(trace_endpos))
328 vector v = trace_endpos;
329 tracebox(v, m1, m2, end, movemode, e);
330 if(trace_endpos.z >= end.z - 1)
332 RESURFACE_LIMITED(v, trace_endpos.z);
336 else if (trace_endpos.z > org.z - jumpheight_vec.z)
337 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
339 if (org.z < end.z - 1)
346 if(autocvar_bot_debug_tracewalk)
349 debugnodestatus(org, DEBUG_NODE_SUCCESS);
352 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
357 if(autocvar_bot_debug_tracewalk)
363 if (stepdist > flatdist)
365 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
367 // can't use movement direction here to calculate move because of
368 // precision errors especially when direction has a high enough z value
369 //water_dir = normalize(water_end - org);
370 //move = org + water_dir * stepdist;
371 fixed_end.z = bound(end.z, org.z, end2.z);
372 if (stepdist == flatdist) {
376 move = org + (fixed_end - org) * (stepdist / flatdist);
377 flatdist = vlen(vec2(fixed_end - move));
380 else // horiz. direction
382 flatdist -= stepdist;
383 move = org + flatdir * stepdist;
386 if(nav_action == NAV_SWIM_ONWATER)
388 tracebox(org, m1, m2, move, movemode, e); // swim
391 if (trace_fraction < 1)
394 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
396 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
399 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
401 if(autocvar_bot_debug_tracewalk)
404 debugnodestatus(org, DEBUG_NODE_SUCCESS);
407 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
411 if(autocvar_bot_debug_tracewalk)
412 debugnodestatus(org, DEBUG_NODE_FAIL);
415 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
426 if (org.z <= move.z) // going horiz.
428 tracebox(trace_endpos, m1, m2, move, movemode, e);
430 nav_action = NAV_WALK;
435 if (org.z <= move.z) // going horiz.
438 nav_action = NAV_SWIM_ONWATER;
444 nav_action = NAV_SWIM_UNDERWATER;
446 nav_action = NAV_SWIM_ONWATER;
449 else if(nav_action == NAV_SWIM_UNDERWATER)
451 if (move.z >= org.z) // swimming upwards or horiz.
453 tracebox(org, m1, m2, move, movemode, e); // swim
455 bool stepswum = false;
458 if (trace_fraction < 1)
461 vector stepswim_move = move + stepheightvec;
462 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
463 stepswim_move.z = end2.z;
465 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
468 if (trace_startsolid)
470 if(autocvar_bot_debug_tracewalk)
471 debugnodestatus(org, DEBUG_NODE_FAIL);
473 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
477 if (trace_fraction < 1)
479 float org_z_prev = org.z;
480 RESURFACE_LIMITED(org, end2.z);
481 if(org.z == org_z_prev)
483 if(autocvar_bot_debug_tracewalk)
484 debugnodestatus(org, DEBUG_NODE_FAIL);
486 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
490 nav_action = NAV_SWIM_UNDERWATER;
492 nav_action = NAV_SWIM_ONWATER;
494 // we didn't advance horiz. in this step, flatdist decrease should be reverted
495 // but we can't do it properly right now... apply this workaround instead
513 if (!WETFEET(trace_endpos))
515 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
516 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
517 if (!stepswum && SUBMERGED(trace_endpos))
519 RESURFACE_LIMITED(trace_endpos, end2.z);
521 nav_action = NAV_SWIM_ONWATER;
527 nav_action = NAV_WALK;
533 nav_action = NAV_SWIM_UNDERWATER;
536 else //if (move.z < org.z) // swimming downwards
538 tracebox(org, m1, m2, move, movemode, e); // swim
541 if (trace_fraction < 1)
544 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
547 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
549 if(autocvar_bot_debug_tracewalk)
550 debugnodestatus(move, DEBUG_NODE_FAIL);
552 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
564 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
566 // stepswim caused upwards direction
567 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
568 if (!SUBMERGED(trace_endpos))
571 nav_action = NAV_WALK;
578 nav_action = NAV_SWIM_UNDERWATER;
582 else if(nav_action == NAV_WALK)
585 tracebox(org, m1, m2, move, movemode, e);
587 if(autocvar_bot_debug_tracewalk)
588 debugnode(e, trace_endpos);
591 if (trace_fraction < 1)
593 // check if we can walk over this obstacle, possibly by jumpstepping
594 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
595 if (trace_fraction < 1 || trace_startsolid)
597 if (trace_startsolid) // hit ceiling above org
599 // reduce stepwalk height
600 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
601 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
603 else //if (trace_fraction < 1)
605 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
606 if (trace_startsolid) // hit ceiling above org
608 // reduce jumpstepwalk height
609 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
610 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
614 if (trace_fraction < 1)
616 vector v = trace_endpos;
617 v.z = org.z + jumpheight_vec.z;
618 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
620 if(autocvar_bot_debug_tracewalk)
623 debugnodestatus(v, DEBUG_NODE_SUCCESS);
626 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
630 if(autocvar_bot_debug_tracewalk)
631 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
633 traceline( org, move, movemode, e);
635 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
639 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
641 nextmove = move + (flatdir * stepdist);
642 traceline( move, nextmove, movemode, e);
645 flatdist = vlen(vec2(end - move));
649 if(autocvar_bot_debug_tracewalk)
650 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
652 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
653 //te_explosion(trace_endpos);
654 //print(ftos(e.dphitcontentsmask), "\n");
655 return false; // failed
667 // trace down from stepheight as far as possible and move there,
668 // if this starts in solid we try again without the stepup, and
669 // if that also fails we assume it is a wall
670 // (this is the same logic as the Quake walkmove function used)
671 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
679 if(autocvar_bot_debug_tracewalk)
681 debugnode(e, trace_endpos);
682 debugnodestatus(org, DEBUG_NODE_FAIL);
685 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
692 if(move.z >= end2.z && org.z < end2.z)
697 if(org.z > move.z - 1 || !SUBMERGED(org))
699 nav_action = NAV_WALK;
703 // ended up submerged while walking
704 if(autocvar_bot_debug_tracewalk)
707 RESURFACE_LIMITED(org, move.z);
708 nav_action = NAV_SWIM_ONWATER;
713 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
715 // moved but didn't arrive at the intended destination
716 if(autocvar_bot_debug_tracewalk)
717 debugnodestatus(org, DEBUG_NODE_FAIL);
722 /////////////////////////////////////////////////////////////////////////////
724 /////////////////////////////////////////////////////////////////////////////
726 // completely empty the goal stack, used when deciding where to go
727 void navigation_clearroute(entity this)
729 this.goalcurrent_prev = this.goalcurrent;
730 this.goalcurrent_distance_2d = FLOAT_MAX;
731 this.goalcurrent_distance_z = FLOAT_MAX;
732 this.goalcurrent_distance_time = 0;
733 this.goalentity_lock_timeout = 0;
734 this.goalentity = NULL;
735 this.goalcurrent = NULL;
736 this.goalstack01 = NULL;
737 this.goalstack02 = NULL;
738 this.goalstack03 = NULL;
739 this.goalstack04 = NULL;
740 this.goalstack05 = NULL;
741 this.goalstack06 = NULL;
742 this.goalstack07 = NULL;
743 this.goalstack08 = NULL;
744 this.goalstack09 = NULL;
745 this.goalstack10 = NULL;
746 this.goalstack11 = NULL;
747 this.goalstack12 = NULL;
748 this.goalstack13 = NULL;
749 this.goalstack14 = NULL;
750 this.goalstack15 = NULL;
751 this.goalstack16 = NULL;
752 this.goalstack17 = NULL;
753 this.goalstack18 = NULL;
754 this.goalstack19 = NULL;
755 this.goalstack20 = NULL;
756 this.goalstack21 = NULL;
757 this.goalstack22 = NULL;
758 this.goalstack23 = NULL;
759 this.goalstack24 = NULL;
760 this.goalstack25 = NULL;
761 this.goalstack26 = NULL;
762 this.goalstack27 = NULL;
763 this.goalstack28 = NULL;
764 this.goalstack29 = NULL;
765 this.goalstack30 = NULL;
766 this.goalstack31 = NULL;
769 // add a new goal at the beginning of the stack
770 // (in other words: add a new prerequisite before going to the later goals)
771 // NOTE: when a waypoint is added, the WP gets pushed first, then the
772 // next-closest WP on the shortest path to the WP
773 // That means, if the stack overflows, the bot will know how to do the FIRST 32
774 // steps to the goal, and then recalculate the path.
775 void navigation_pushroute(entity this, entity e)
777 this.goalcurrent_prev = this.goalcurrent;
778 this.goalcurrent_distance_2d = FLOAT_MAX;
779 this.goalcurrent_distance_z = FLOAT_MAX;
780 this.goalcurrent_distance_time = 0;
781 //print("bot ", etos(this), " push ", etos(e), "\n");
782 if(this.goalstack31 == this.goalentity)
783 this.goalentity = NULL;
784 this.goalstack31 = this.goalstack30;
785 this.goalstack30 = this.goalstack29;
786 this.goalstack29 = this.goalstack28;
787 this.goalstack28 = this.goalstack27;
788 this.goalstack27 = this.goalstack26;
789 this.goalstack26 = this.goalstack25;
790 this.goalstack25 = this.goalstack24;
791 this.goalstack24 = this.goalstack23;
792 this.goalstack23 = this.goalstack22;
793 this.goalstack22 = this.goalstack21;
794 this.goalstack21 = this.goalstack20;
795 this.goalstack20 = this.goalstack19;
796 this.goalstack19 = this.goalstack18;
797 this.goalstack18 = this.goalstack17;
798 this.goalstack17 = this.goalstack16;
799 this.goalstack16 = this.goalstack15;
800 this.goalstack15 = this.goalstack14;
801 this.goalstack14 = this.goalstack13;
802 this.goalstack13 = this.goalstack12;
803 this.goalstack12 = this.goalstack11;
804 this.goalstack11 = this.goalstack10;
805 this.goalstack10 = this.goalstack09;
806 this.goalstack09 = this.goalstack08;
807 this.goalstack08 = this.goalstack07;
808 this.goalstack07 = this.goalstack06;
809 this.goalstack06 = this.goalstack05;
810 this.goalstack05 = this.goalstack04;
811 this.goalstack04 = this.goalstack03;
812 this.goalstack03 = this.goalstack02;
813 this.goalstack02 = this.goalstack01;
814 this.goalstack01 = this.goalcurrent;
815 this.goalcurrent = e;
818 // remove first goal from stack
819 // (in other words: remove a prerequisite for reaching the later goals)
820 // (used when a spawnfunc_waypoint is reached)
821 void navigation_poproute(entity this)
823 this.goalcurrent_prev = this.goalcurrent;
824 this.goalcurrent_distance_2d = FLOAT_MAX;
825 this.goalcurrent_distance_z = FLOAT_MAX;
826 this.goalcurrent_distance_time = 0;
827 //print("bot ", etos(this), " pop\n");
828 if(this.goalcurrent == this.goalentity)
830 this.goalentity = NULL;
831 this.goalentity_lock_timeout = 0;
833 this.goalcurrent = this.goalstack01;
834 this.goalstack01 = this.goalstack02;
835 this.goalstack02 = this.goalstack03;
836 this.goalstack03 = this.goalstack04;
837 this.goalstack04 = this.goalstack05;
838 this.goalstack05 = this.goalstack06;
839 this.goalstack06 = this.goalstack07;
840 this.goalstack07 = this.goalstack08;
841 this.goalstack08 = this.goalstack09;
842 this.goalstack09 = this.goalstack10;
843 this.goalstack10 = this.goalstack11;
844 this.goalstack11 = this.goalstack12;
845 this.goalstack12 = this.goalstack13;
846 this.goalstack13 = this.goalstack14;
847 this.goalstack14 = this.goalstack15;
848 this.goalstack15 = this.goalstack16;
849 this.goalstack16 = this.goalstack17;
850 this.goalstack17 = this.goalstack18;
851 this.goalstack18 = this.goalstack19;
852 this.goalstack19 = this.goalstack20;
853 this.goalstack20 = this.goalstack21;
854 this.goalstack21 = this.goalstack22;
855 this.goalstack22 = this.goalstack23;
856 this.goalstack23 = this.goalstack24;
857 this.goalstack24 = this.goalstack25;
858 this.goalstack25 = this.goalstack26;
859 this.goalstack26 = this.goalstack27;
860 this.goalstack27 = this.goalstack28;
861 this.goalstack28 = this.goalstack29;
862 this.goalstack29 = this.goalstack30;
863 this.goalstack30 = this.goalstack31;
864 this.goalstack31 = NULL;
867 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
868 // waypoint destination coordinates instead of v (only useful for box waypoints)
869 // for normal waypoints v2 == v and v2_height == 0
870 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)
872 if (vdist(v - org, <, bestdist))
874 traceline(v, org, true, ent);
875 if (trace_fraction == 1)
879 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
884 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
892 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
893 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
896 ent = ent.tag_entity;
898 vector pm1 = ent.origin + ent.mins;
899 vector pm2 = ent.origin + ent.maxs;
901 // do two scans, because box test is cheaper
902 IL_EACH(g_waypoints, it != ent && it != except,
904 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
906 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
908 waypoint_clearlinks(ent); // initialize wpXXmincost fields
909 navigation_item_addlink(it, ent);
915 vector org = ent.origin;
916 if (navigation_testtracewalk)
922 if(ent.size && !IS_PLAYER(ent))
924 org += 0.5 * (ent.mins + ent.maxs);
925 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
928 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
930 waypoint_clearlinks(ent); // initialize wpXXmincost fields
931 IL_EACH(g_waypoints, it != ent,
933 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
936 set_tracewalk_dest(ent, it.origin, false);
937 if (vdist(tracewalk_dest - it.origin, <, 1050)
938 && tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST,
939 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
941 navigation_item_addlink(it, ent);
946 // box check failed, try walk
947 IL_EACH(g_waypoints, it != ent,
949 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
955 set_tracewalk_dest(ent, v, true);
956 if (trace_ent == ent)
964 set_tracewalk_dest(it, org, false);
966 if (navigation_waypoint_will_link(v, org, ent,
967 tracewalk_dest, tracewalk_dest_height,
968 tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
971 bestdist = vlen(tracewalk_dest - v);
973 bestdist = vlen(v - org);
977 if(!best && !ent.navigation_dynamicgoal)
979 int solid_save = ent.solid;
980 ent.solid = SOLID_BSP;
981 IL_EACH(g_jumppads, true,
983 if(trigger_push_test(it, ent))
985 best = it.nearestwaypoint;
989 ent.solid = solid_save;
993 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
995 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
996 if (autocvar_g_waypointeditor_auto)
998 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1000 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1005 // finds the waypoints near the bot initiating a navigation query
1006 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1008 //navigation_testtracewalk = true;
1010 IL_EACH(g_waypoints, !it.wpconsidered,
1012 set_tracewalk_dest(it, this.origin, false);
1014 vector diff = tracewalk_dest - this.origin;
1015 diff.z = max(0, diff.z);
1016 if(vdist(diff, <, maxdist))
1018 it.wpconsidered = true;
1019 if (tracewalk(this, this.origin, this.mins, this.maxs,
1020 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1022 it.wpnearestpoint = tracewalk_dest;
1023 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1030 //navigation_testtracewalk = false;
1034 // updates a path link if a spawnfunc_waypoint link is better than the current one
1035 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1041 m1 = wp.origin + wp.mins;
1042 m2 = wp.origin + wp.maxs;
1043 v.x = bound(m1_x, p.x, m2_x);
1044 v.y = bound(m1_y, p.y, m2_y);
1045 v.z = bound(m1_z, p.z, m2_z);
1049 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1050 cost += w.wp00mincost; // assuming teleport has exactly one destination
1052 cost += waypoint_gettravelcost(p, v, w, wp);
1053 if (wp.wpcost > cost)
1058 wp.wpnearestpoint = v;
1062 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1063 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1068 IL_EACH(g_waypoints, true,
1070 it.wpconsidered = false;
1071 it.wpnearestpoint = '0 0 0';
1072 it.wpcost = 10000000;
1077 if(fixed_source_waypoint)
1079 fixed_source_waypoint.wpconsidered = true;
1080 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1081 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1082 fixed_source_waypoint.wpfire = 1;
1083 fixed_source_waypoint.enemy = NULL;
1087 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1088 // as this search is expensive we will use lower values if the bot is on the air
1089 float increment, maxdistance;
1090 if(IS_ONGROUND(this))
1093 maxdistance = 50000;
1101 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1104 bool searching = true;
1108 IL_EACH(g_waypoints, it.wpfire,
1113 p = it.wpnearestpoint;
1115 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1116 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1117 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1118 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1119 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1120 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1121 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1122 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1123 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1124 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1125 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1126 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1127 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1128 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1129 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1130 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1131 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1132 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1133 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1134 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1135 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1152 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1153 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1157 IL_EACH(g_waypoints, true,
1159 it.wpconsidered = false;
1160 it.wpnearestpoint = '0 0 0';
1161 it.wpcost = 10000000;
1166 if(fixed_source_waypoint)
1168 fixed_source_waypoint.wpconsidered = true;
1169 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1170 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1171 fixed_source_waypoint.wpfire = 1;
1172 fixed_source_waypoint.enemy = NULL;
1176 error("need to start with a waypoint\n");
1179 bool searching = true;
1183 IL_EACH(g_waypoints, it.wpfire,
1187 cost = it.wpcost; // cost to walk from it to home
1188 p = it.wpnearestpoint;
1190 IL_EACH(g_waypoints, it != wp,
1192 if(!waypoint_islinked(it, wp))
1194 cost2 = cost + it.dmg;
1195 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1201 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1202 void navigation_routerating(entity this, entity e, float f, float rangebias)
1210 rangebias = waypoint_getlinearcost(rangebias);
1211 f = waypoint_getlinearcost(f);
1215 bool rate_wps = false;
1216 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1221 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1222 int t = pointcontents(trace_endpos + '0 0 1');
1223 if(t != CONTENT_SOLID )
1225 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1227 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1234 entity theEnemy = e;
1235 entity best_wp = NULL;
1236 float best_dist = 10000;
1237 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1238 && vdist(it.origin - this.origin, >, 100)
1239 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1241 float dist = vlen(it.origin - theEnemy.origin);
1242 if (dist < best_dist)
1254 vector goal_org = (e.absmin + e.absmax) * 0.5;
1256 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1258 // Evaluate path using jetpack
1260 if(this.items & IT_JETPACK)
1261 if(autocvar_bot_ai_navigation_jetpack)
1262 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1264 vector pointa, pointb;
1266 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1269 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1270 pointa = trace_endpos - '0 0 1';
1273 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1274 pointb = trace_endpos - '0 0 1';
1276 // Can I see these two points from the sky?
1277 traceline(pointa, pointb, MOVE_NORMAL, this);
1279 if(trace_fraction==1)
1281 LOG_DEBUG("jetpack ai: can bridge these two points");
1283 // Lower the altitude of these points as much as possible
1284 float zdistance, xydistance, cost, t, fuel;
1285 vector down, npa, npb;
1287 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1290 npa = pointa + down;
1291 npb = pointb + down;
1293 if(npa.z<=this.absmax.z)
1296 if(npb.z<=e.absmax.z)
1299 traceline(npa, npb, MOVE_NORMAL, this);
1300 if(trace_fraction==1)
1306 while(trace_fraction == 1);
1309 // Rough estimation of fuel consumption
1310 // (ignores acceleration and current xyz velocity)
1311 xydistance = vlen(pointa - pointb);
1312 zdistance = fabs(pointa.z - this.origin.z);
1314 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1315 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1316 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1318 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1321 if(this.ammo_fuel>fuel)
1324 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1325 // - between air and ground speeds)
1327 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1328 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1331 // Compare against other goals
1332 f = f * rangebias / (rangebias + cost);
1334 if (navigation_bestrating < f)
1336 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1337 navigation_bestrating = f;
1338 navigation_bestgoal = e;
1339 this.navigation_jetpack_goal = e;
1340 this.navigation_jetpack_point = pointb;
1348 //te_wizspike(e.origin);
1351 // update the cached spawnfunc_waypoint link on a dynamic item entity
1352 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1358 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1359 e.nearestwaypoint = NULL;
1361 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1362 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1364 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1365 e.nearestwaypoint = nwp = e.goalcurrent;
1367 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1370 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1372 if(!e.navigation_dynamicgoal)
1373 e.blacklisted = true;
1377 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1382 if(e.navigation_dynamicgoal)
1383 e.nearestwaypointtimeout = time + 2;
1384 else if(autocvar_g_waypointeditor)
1385 e.nearestwaypointtimeout = time + 3 + random() * 2;
1387 nwp = e.nearestwaypoint;
1390 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1391 if (nwp && nwp.wpcost < 10000000)
1393 //te_wizspike(nwp.wpnearestpoint);
1394 float nwptoitem_cost = 0;
1395 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1396 nwptoitem_cost = nwp.wp00mincost;
1398 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1399 float cost = nwp.wpcost + nwptoitem_cost;
1400 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1401 f = f * rangebias / (rangebias + cost);
1402 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1403 if (navigation_bestrating < f)
1405 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1406 navigation_bestrating = f;
1407 navigation_bestgoal = e;
1412 // adds an item to the the goal stack with the path to a given item
1413 bool navigation_routetogoal(entity this, entity e, vector startposition)
1415 // if there is no goal, just exit
1419 entity teleport_goal = NULL;
1421 this.goalentity = e;
1423 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1425 // force teleport destination as route destination
1427 navigation_pushroute(this, e.wp00);
1428 this.goalentity = e.wp00;
1431 // put the entity on the goal stack
1432 //print("routetogoal ", etos(e), "\n");
1433 navigation_pushroute(this, e);
1436 e = this.goalentity;
1438 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1440 this.wp_goal_prev1 = this.wp_goal_prev0;
1441 this.wp_goal_prev0 = e;
1445 if(e==this.navigation_jetpack_goal)
1448 // if it can reach the goal there is nothing more to do
1449 set_tracewalk_dest(e, startposition, true);
1450 if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1451 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1452 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1457 entity nearest_wp = NULL;
1458 // see if there are waypoints describing a path to the item
1459 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1461 e = e.nearestwaypoint;
1464 else if(teleport_goal)
1467 e = e.enemy; // we already have added it, so...
1472 if(nearest_wp && nearest_wp.enemy)
1474 // often path can be optimized by not adding the nearest waypoint
1475 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1477 if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget
1478 && vdist(vec2(this.goalentity.origin - nearest_wp.origin), >, 16))
1480 set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1481 if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1482 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1483 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1484 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1486 e = nearest_wp.enemy;
1490 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1491 e = nearest_wp.enemy;
1496 // add the spawnfunc_waypoint to the path
1497 navigation_pushroute(this, e);
1507 // shorten path by removing intermediate goals
1508 void navigation_shortenpath(entity this)
1510 if (!this.goalstack01 || wasfreed(this.goalstack01))
1512 if (this.bot_tracewalk_time > time)
1514 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1516 bool cut_allowed = false;
1517 entity next = this.goalentity;
1518 // evaluate whether bot can discard current route and chase directly a player, trying to
1519 // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1520 if (IS_MOVABLE(next))
1522 set_tracewalk_dest(next, this.origin, true);
1523 if (vdist(this.origin - tracewalk_dest, <, 200))
1525 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1526 && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1527 && vdist(this.origin - this.goalcurrent.origin, >, 100)
1528 && checkpvs(this.origin + this.view_ofs, next))
1530 if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1534 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1535 while (deviation.y < -180) deviation.y += 360;
1536 while (deviation.y > 180) deviation.y -= 360;
1537 if (fabs(deviation.y) > 25)
1543 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1544 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1546 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1549 navigation_poproute(this);
1551 while (this.goalcurrent != next);
1557 next = this.goalstack01;
1558 // if for some reason the bot is closer to the next goal, pop the current one
1559 if (!IS_MOVABLE(next) // already checked in the previous case
1560 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1561 && checkpvs(this.origin + this.view_ofs, next))
1563 set_tracewalk_dest(next, this.origin, true);
1569 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1570 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1572 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1573 navigation_poproute(this);
1578 // removes any currently touching waypoints from the goal stack
1579 // (this is how bots detect if they reached a goal)
1580 int navigation_poptouchedgoals(entity this)
1582 int removed_goals = 0;
1584 if(!this.goalcurrent)
1585 return removed_goals;
1587 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1589 // make sure jumppad is really hit, don't rely on distance based checks
1590 // as they may report a touch even if it didn't really happen
1591 if(this.lastteleporttime > 0
1592 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1594 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1595 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1597 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1598 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1600 navigation_poproute(this);
1601 this.lastteleporttime = 0;
1605 return removed_goals;
1608 // Loose goal touching check when running
1609 if(this.aistatus & AI_STATUS_RUNNING)
1610 if(this.goalcurrent.classname=="waypoint")
1611 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1613 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1615 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1616 if(trace_fraction==1)
1618 // Detect personal waypoints
1619 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1620 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1622 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1623 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1626 navigation_poproute(this);
1628 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1629 return removed_goals;
1634 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1636 vector gc_min = this.goalcurrent.absmin;
1637 vector gc_max = this.goalcurrent.absmax;
1638 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1640 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1641 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1643 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1646 // Detect personal waypoints
1647 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1648 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1650 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1651 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1654 navigation_poproute(this);
1656 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1657 return removed_goals;
1659 return removed_goals;
1662 entity navigation_get_really_close_waypoint(entity this)
1664 entity wp = this.goalcurrent;
1665 if(!wp || vdist(wp.origin - this.origin, >, 50))
1666 wp = this.goalcurrent_prev;
1669 if(wp.classname != "waypoint")
1671 wp = wp.nearestwaypoint;
1675 if(vdist(wp.origin - this.origin, >, 50))
1678 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1680 if(vdist(it.origin - this.origin, <, 50))
1689 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1692 set_tracewalk_dest(wp, this.origin, false);
1693 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1694 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1701 // begin a goal selection session (queries spawnfunc_waypoint network)
1702 void navigation_goalrating_start(entity this)
1704 if(this.aistatus & AI_STATUS_STUCK)
1707 this.navigation_jetpack_goal = NULL;
1708 navigation_bestrating = -1;
1709 entity wp = navigation_get_really_close_waypoint(this);
1710 navigation_clearroute(this);
1711 navigation_bestgoal = NULL;
1712 navigation_markroutes(this, wp);
1715 // ends a goal selection session (updates goal stack to the best goal)
1716 void navigation_goalrating_end(entity this)
1718 if(this.aistatus & AI_STATUS_STUCK)
1721 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1722 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1724 // If the bot got stuck then try to reach the farthest waypoint
1725 if (!this.goalentity && autocvar_bot_wander_enable)
1727 if (!(this.aistatus & AI_STATUS_STUCK))
1729 LOG_DEBUG(this.netname, " cannot walk to any goal");
1730 this.aistatus |= AI_STATUS_STUCK;
1735 void botframe_updatedangerousobjects(float maxupdate)
1737 vector m1, m2, v, o;
1741 IL_EACH(g_waypoints, true,
1747 IL_EACH(g_bot_dodge, it.bot_dodge,
1750 v.x = bound(m1_x, v.x, m2_x);
1751 v.y = bound(m1_y, v.y, m2_y);
1752 v.z = bound(m1_z, v.z, m2_z);
1753 o = (it.absmin + it.absmax) * 0.5;
1754 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1757 traceline(o, v, true, NULL);
1758 if (trace_fraction == 1)
1759 danger = danger + d;
1769 void navigation_unstuck(entity this)
1771 float search_radius = 1000;
1773 if (!autocvar_bot_wander_enable)
1776 if (!bot_waypoint_queue_owner)
1778 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1779 bot_waypoint_queue_owner = this;
1780 bot_waypoint_queue_bestgoal = NULL;
1781 bot_waypoint_queue_bestgoalrating = 0;
1784 if(bot_waypoint_queue_owner!=this)
1787 if (bot_waypoint_queue_goal)
1789 // evaluate the next goal on the queue
1790 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1791 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1792 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1793 if (tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1794 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1796 if( d > bot_waypoint_queue_bestgoalrating)
1798 bot_waypoint_queue_bestgoalrating = d;
1799 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1802 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1804 if (!bot_waypoint_queue_goal)
1806 if (bot_waypoint_queue_bestgoal)
1808 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1809 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1810 navigation_goalrating_timeout_set(this);
1811 this.aistatus &= ~AI_STATUS_STUCK;
1815 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1818 bot_waypoint_queue_owner = NULL;
1823 if(bot_strategytoken!=this)
1826 // build a new queue
1827 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1829 entity first = NULL;
1831 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1833 if(bot_waypoint_queue_goal)
1834 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1838 bot_waypoint_queue_goal = it;
1839 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1843 bot_waypoint_queue_goal = first;
1846 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1847 bot_waypoint_queue_owner = NULL;
1852 // Support for debugging tracewalk visually
1854 void debugresetnodes()
1856 debuglastnode = '0 0 0';
1859 void debugnode(entity this, vector node)
1861 if (!IS_PLAYER(this))
1864 if(debuglastnode=='0 0 0')
1866 debuglastnode = node;
1870 te_lightning2(NULL, node, debuglastnode);
1871 debuglastnode = node;
1874 void debugnodestatus(vector position, float status)
1880 case DEBUG_NODE_SUCCESS:
1883 case DEBUG_NODE_WARNING:
1886 case DEBUG_NODE_FAIL:
1893 te_customflash(position, 40, 2, c);
1896 // Support for debugging the goal stack visually
1899 .vector lastposition;
1901 // Debug the goal stack visually
1902 void debuggoalstack(entity this)
1907 if(this.goalcounter==0)goal=this.goalcurrent;
1908 else if(this.goalcounter==1)goal=this.goalstack01;
1909 else if(this.goalcounter==2)goal=this.goalstack02;
1910 else if(this.goalcounter==3)goal=this.goalstack03;
1911 else if(this.goalcounter==4)goal=this.goalstack04;
1912 else if(this.goalcounter==5)goal=this.goalstack05;
1913 else if(this.goalcounter==6)goal=this.goalstack06;
1914 else if(this.goalcounter==7)goal=this.goalstack07;
1915 else if(this.goalcounter==8)goal=this.goalstack08;
1916 else if(this.goalcounter==9)goal=this.goalstack09;
1917 else if(this.goalcounter==10)goal=this.goalstack10;
1918 else if(this.goalcounter==11)goal=this.goalstack11;
1919 else if(this.goalcounter==12)goal=this.goalstack12;
1920 else if(this.goalcounter==13)goal=this.goalstack13;
1921 else if(this.goalcounter==14)goal=this.goalstack14;
1922 else if(this.goalcounter==15)goal=this.goalstack15;
1923 else if(this.goalcounter==16)goal=this.goalstack16;
1924 else if(this.goalcounter==17)goal=this.goalstack17;
1925 else if(this.goalcounter==18)goal=this.goalstack18;
1926 else if(this.goalcounter==19)goal=this.goalstack19;
1927 else if(this.goalcounter==20)goal=this.goalstack20;
1928 else if(this.goalcounter==21)goal=this.goalstack21;
1929 else if(this.goalcounter==22)goal=this.goalstack22;
1930 else if(this.goalcounter==23)goal=this.goalstack23;
1931 else if(this.goalcounter==24)goal=this.goalstack24;
1932 else if(this.goalcounter==25)goal=this.goalstack25;
1933 else if(this.goalcounter==26)goal=this.goalstack26;
1934 else if(this.goalcounter==27)goal=this.goalstack27;
1935 else if(this.goalcounter==28)goal=this.goalstack28;
1936 else if(this.goalcounter==29)goal=this.goalstack29;
1937 else if(this.goalcounter==30)goal=this.goalstack30;
1938 else if(this.goalcounter==31)goal=this.goalstack31;
1943 this.goalcounter = 0;
1944 this.lastposition='0 0 0';
1948 if(this.lastposition=='0 0 0')
1951 org = this.lastposition;
1954 go = ( goal.absmin + goal.absmax ) * 0.5;
1955 te_lightning2(NULL, org, go);
1956 this.lastposition = go;