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 void navigation_goalrating_timeout_force(entity this)
30 this.bot_strategytime = 0;
33 bool navigation_goalrating_timeout(entity this)
35 return this.bot_strategytime < time;
38 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
40 if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
43 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
45 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
46 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
48 this.ignoregoal = this.goalentity;
49 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
56 void navigation_dynamicgoal_init(entity this, bool initially_static)
58 this.navigation_dynamicgoal = true;
59 this.bot_basewaypoint = this.nearestwaypoint;
61 this.nearestwaypointtimeout = -1;
63 this.nearestwaypointtimeout = time;
66 void navigation_dynamicgoal_set(entity this)
68 this.nearestwaypointtimeout = time;
71 void navigation_dynamicgoal_unset(entity this)
73 if(this.bot_basewaypoint)
74 this.nearestwaypoint = this.bot_basewaypoint;
75 this.nearestwaypointtimeout = -1;
78 bool navigation_check_submerged_state(entity ent, vector pos)
82 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
83 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
84 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
87 submerged = SUBMERGED(pos);
88 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
89 // is actually submerged because often they are inside some solid.
90 // That's why submerged state is saved now that we know current pos is
91 // not stuck in solid (previous tracewalk call to this pos was successfully)
92 if(!ent.navigation_dynamicgoal)
93 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
98 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
100 IL_EACH(g_ladders, it.classname == "func_ladder",
103 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
104 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
107 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
108 tracebox(org, m1, m2, top, movemode, e);
109 if(trace_fraction == 1)
116 vector resurface_limited(vector org, float lim, vector m1)
118 if (WETFEET(org + eZ * (lim - org.z)))
122 float RES_min_h = org.z;
123 float RES_max_h = lim;
125 org.z = 0.5 * (RES_min_h + RES_max_h);
130 } while (RES_max_h - RES_min_h >= 1);
135 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
138 #define NAV_SWIM_ONWATER 1
139 #define NAV_SWIM_UNDERWATER 2
141 // rough simulation of walking from one point to another to test if a path
142 // can be traveled, used for waypoint linking and havocbot
143 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
144 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
146 if(autocvar_bot_debug_tracewalk)
153 vector flatdir = end - start;
155 float flatdist = vlen(flatdir);
156 flatdir = normalize(flatdir);
158 bool ignorehazards = false;
161 // Analyze starting point
162 traceline(start, start, MOVE_NORMAL, e);
163 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
164 ignorehazards = true;
166 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
167 if (trace_startsolid)
170 if(autocvar_bot_debug_tracewalk)
171 debugnodestatus(start, DEBUG_NODE_FAIL);
173 //print("tracewalk: ", vtos(start), " is a bad start\n");
179 end2.z += end_height;
181 vector fixed_end = end;
184 if (flatdist > 0 && WETFEET(org))
187 nav_action = NAV_SWIM_UNDERWATER;
190 // tracebox down by player's height
191 // useful to know if water level is so low that bot can still walk
192 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
193 if (SUBMERGED(trace_endpos))
196 nav_action = NAV_SWIM_UNDERWATER;
199 nav_action = NAV_WALK;
203 nav_action = NAV_WALK;
211 if (org.z > end2.z + 1)
213 tracebox(org, m1, m2, end2, movemode, e);
215 if (org.z > end2.z + 1)
218 else if (org.z < end.z - 1)
220 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
221 if (SUBMERGED(trace_endpos))
223 vector v = trace_endpos;
224 tracebox(v, m1, m2, end, movemode, e);
225 if(trace_endpos.z >= end.z - 1)
227 RESURFACE_LIMITED(v, trace_endpos.z);
231 else if (trace_endpos.z > org.z - jumpheight_vec.z)
232 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
234 if (org.z < end.z - 1)
241 if(autocvar_bot_debug_tracewalk)
244 debugnodestatus(org, DEBUG_NODE_SUCCESS);
247 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
252 if(autocvar_bot_debug_tracewalk)
258 if (stepdist > flatdist)
260 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
262 // can't use movement direction here to calculate move because of
263 // precision errors especially when direction has a high enough z value
264 //water_dir = normalize(water_end - org);
265 //move = org + water_dir * stepdist;
266 fixed_end.z = bound(end.z, org.z, end2.z);
267 if (stepdist == flatdist) {
271 move = org + (fixed_end - org) * (stepdist / flatdist);
272 flatdist = vlen(vec2(fixed_end - move));
275 else // horiz. direction
277 flatdist -= stepdist;
278 move = org + flatdir * stepdist;
281 if(nav_action == NAV_SWIM_ONWATER)
283 tracebox(org, m1, m2, move, movemode, e); // swim
286 if (trace_fraction < 1)
289 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
291 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
294 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
296 if(autocvar_bot_debug_tracewalk)
299 debugnodestatus(org, DEBUG_NODE_SUCCESS);
302 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
306 if(autocvar_bot_debug_tracewalk)
307 debugnodestatus(org, DEBUG_NODE_FAIL);
310 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
321 if (org.z <= move.z) // going horiz.
323 tracebox(trace_endpos, m1, m2, move, movemode, e);
325 nav_action = NAV_WALK;
330 if (org.z <= move.z) // going horiz.
333 nav_action = NAV_SWIM_ONWATER;
339 nav_action = NAV_SWIM_UNDERWATER;
341 nav_action = NAV_SWIM_ONWATER;
344 else if(nav_action == NAV_SWIM_UNDERWATER)
346 if (move.z >= org.z) // swimming upwards or horiz.
348 tracebox(org, m1, m2, move, movemode, e); // swim
350 bool stepswum = false;
353 if (trace_fraction < 1)
356 vector stepswim_move = move + stepheightvec;
357 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
358 stepswim_move.z = end2.z;
360 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
363 if (trace_startsolid)
365 if(autocvar_bot_debug_tracewalk)
366 debugnodestatus(org, DEBUG_NODE_FAIL);
368 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
372 if (trace_fraction < 1)
374 float org_z_prev = org.z;
375 RESURFACE_LIMITED(org, end2.z);
376 if(org.z == org_z_prev)
378 if(autocvar_bot_debug_tracewalk)
379 debugnodestatus(org, DEBUG_NODE_FAIL);
381 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
385 nav_action = NAV_SWIM_UNDERWATER;
387 nav_action = NAV_SWIM_ONWATER;
389 // we didn't advance horiz. in this step, flatdist decrease should be reverted
390 // but we can't do it properly right now... apply this workaround instead
408 if (!WETFEET(trace_endpos))
410 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
411 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
412 if (!stepswum && SUBMERGED(trace_endpos))
414 RESURFACE_LIMITED(trace_endpos, end2.z);
416 nav_action = NAV_SWIM_ONWATER;
422 nav_action = NAV_WALK;
428 nav_action = NAV_SWIM_UNDERWATER;
431 else //if (move.z < org.z) // swimming downwards
433 tracebox(org, m1, m2, move, movemode, e); // swim
436 if (trace_fraction < 1)
439 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
442 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
444 if(autocvar_bot_debug_tracewalk)
445 debugnodestatus(move, DEBUG_NODE_FAIL);
447 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
459 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
461 // stepswim caused upwards direction
462 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
463 if (!SUBMERGED(trace_endpos))
466 nav_action = NAV_WALK;
473 nav_action = NAV_SWIM_UNDERWATER;
477 else if(nav_action == NAV_WALK)
480 tracebox(org, m1, m2, move, movemode, e);
482 if(autocvar_bot_debug_tracewalk)
483 debugnode(e, trace_endpos);
486 if (trace_fraction < 1)
488 // check if we can walk over this obstacle, possibly by jumpstepping
489 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
490 if (trace_fraction < 1 || trace_startsolid)
492 if (trace_startsolid) // hit ceiling above org
494 // reduce stepwalk height
495 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
496 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
498 else //if (trace_fraction < 1)
500 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
501 if (trace_startsolid) // hit ceiling above org
503 // reduce jumpstepwalk height
504 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
505 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
509 if (trace_fraction < 1)
511 vector v = trace_endpos;
512 v.z = org.z + jumpheight_vec.z;
513 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
515 if(autocvar_bot_debug_tracewalk)
518 debugnodestatus(v, DEBUG_NODE_SUCCESS);
521 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
525 if(autocvar_bot_debug_tracewalk)
526 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
528 traceline( org, move, movemode, e);
530 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
534 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
536 nextmove = move + (flatdir * stepdist);
537 traceline( move, nextmove, movemode, e);
540 flatdist = vlen(vec2(end - move));
544 if(autocvar_bot_debug_tracewalk)
545 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
547 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
548 //te_explosion(trace_endpos);
549 //print(ftos(e.dphitcontentsmask), "\n");
550 return false; // failed
562 // trace down from stepheight as far as possible and move there,
563 // if this starts in solid we try again without the stepup, and
564 // if that also fails we assume it is a wall
565 // (this is the same logic as the Quake walkmove function used)
566 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
574 if(autocvar_bot_debug_tracewalk)
576 debugnode(e, trace_endpos);
577 debugnodestatus(org, DEBUG_NODE_FAIL);
580 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
587 if(move.z >= end2.z && org.z < end2.z)
592 if(org.z > move.z - 1 || !SUBMERGED(org))
594 nav_action = NAV_WALK;
598 // ended up submerged while walking
599 if(autocvar_bot_debug_tracewalk)
602 RESURFACE_LIMITED(org, move.z);
603 nav_action = NAV_SWIM_ONWATER;
608 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
610 // moved but didn't arrive at the intended destination
611 if(autocvar_bot_debug_tracewalk)
612 debugnodestatus(org, DEBUG_NODE_FAIL);
617 /////////////////////////////////////////////////////////////////////////////
619 /////////////////////////////////////////////////////////////////////////////
621 // completely empty the goal stack, used when deciding where to go
622 void navigation_clearroute(entity this)
624 this.goalcurrent_prev = this.goalcurrent;
625 this.goalcurrent_distance_2d = FLOAT_MAX;
626 this.goalcurrent_distance_z = FLOAT_MAX;
627 this.goalcurrent_distance_time = 0;
628 //print("bot ", etos(this), " clear\n");
629 this.goalentity = NULL;
630 this.goalcurrent = NULL;
631 this.goalstack01 = NULL;
632 this.goalstack02 = NULL;
633 this.goalstack03 = NULL;
634 this.goalstack04 = NULL;
635 this.goalstack05 = NULL;
636 this.goalstack06 = NULL;
637 this.goalstack07 = NULL;
638 this.goalstack08 = NULL;
639 this.goalstack09 = NULL;
640 this.goalstack10 = NULL;
641 this.goalstack11 = NULL;
642 this.goalstack12 = NULL;
643 this.goalstack13 = NULL;
644 this.goalstack14 = NULL;
645 this.goalstack15 = NULL;
646 this.goalstack16 = NULL;
647 this.goalstack17 = NULL;
648 this.goalstack18 = NULL;
649 this.goalstack19 = NULL;
650 this.goalstack20 = NULL;
651 this.goalstack21 = NULL;
652 this.goalstack22 = NULL;
653 this.goalstack23 = NULL;
654 this.goalstack24 = NULL;
655 this.goalstack25 = NULL;
656 this.goalstack26 = NULL;
657 this.goalstack27 = NULL;
658 this.goalstack28 = NULL;
659 this.goalstack29 = NULL;
660 this.goalstack30 = NULL;
661 this.goalstack31 = NULL;
664 // add a new goal at the beginning of the stack
665 // (in other words: add a new prerequisite before going to the later goals)
666 // NOTE: when a waypoint is added, the WP gets pushed first, then the
667 // next-closest WP on the shortest path to the WP
668 // That means, if the stack overflows, the bot will know how to do the FIRST 32
669 // steps to the goal, and then recalculate the path.
670 void navigation_pushroute(entity this, entity e)
672 this.goalcurrent_prev = this.goalcurrent;
673 this.goalcurrent_distance_2d = FLOAT_MAX;
674 this.goalcurrent_distance_z = FLOAT_MAX;
675 this.goalcurrent_distance_time = 0;
676 //print("bot ", etos(this), " push ", etos(e), "\n");
677 if(this.goalstack31 == this.goalentity)
678 this.goalentity = NULL;
679 this.goalstack31 = this.goalstack30;
680 this.goalstack30 = this.goalstack29;
681 this.goalstack29 = this.goalstack28;
682 this.goalstack28 = this.goalstack27;
683 this.goalstack27 = this.goalstack26;
684 this.goalstack26 = this.goalstack25;
685 this.goalstack25 = this.goalstack24;
686 this.goalstack24 = this.goalstack23;
687 this.goalstack23 = this.goalstack22;
688 this.goalstack22 = this.goalstack21;
689 this.goalstack21 = this.goalstack20;
690 this.goalstack20 = this.goalstack19;
691 this.goalstack19 = this.goalstack18;
692 this.goalstack18 = this.goalstack17;
693 this.goalstack17 = this.goalstack16;
694 this.goalstack16 = this.goalstack15;
695 this.goalstack15 = this.goalstack14;
696 this.goalstack14 = this.goalstack13;
697 this.goalstack13 = this.goalstack12;
698 this.goalstack12 = this.goalstack11;
699 this.goalstack11 = this.goalstack10;
700 this.goalstack10 = this.goalstack09;
701 this.goalstack09 = this.goalstack08;
702 this.goalstack08 = this.goalstack07;
703 this.goalstack07 = this.goalstack06;
704 this.goalstack06 = this.goalstack05;
705 this.goalstack05 = this.goalstack04;
706 this.goalstack04 = this.goalstack03;
707 this.goalstack03 = this.goalstack02;
708 this.goalstack02 = this.goalstack01;
709 this.goalstack01 = this.goalcurrent;
710 this.goalcurrent = e;
713 // remove first goal from stack
714 // (in other words: remove a prerequisite for reaching the later goals)
715 // (used when a spawnfunc_waypoint is reached)
716 void navigation_poproute(entity this)
718 this.goalcurrent_prev = this.goalcurrent;
719 this.goalcurrent_distance_2d = FLOAT_MAX;
720 this.goalcurrent_distance_z = FLOAT_MAX;
721 this.goalcurrent_distance_time = 0;
722 //print("bot ", etos(this), " pop\n");
723 if(this.goalcurrent == this.goalentity)
724 this.goalentity = NULL;
725 this.goalcurrent = this.goalstack01;
726 this.goalstack01 = this.goalstack02;
727 this.goalstack02 = this.goalstack03;
728 this.goalstack03 = this.goalstack04;
729 this.goalstack04 = this.goalstack05;
730 this.goalstack05 = this.goalstack06;
731 this.goalstack06 = this.goalstack07;
732 this.goalstack07 = this.goalstack08;
733 this.goalstack08 = this.goalstack09;
734 this.goalstack09 = this.goalstack10;
735 this.goalstack10 = this.goalstack11;
736 this.goalstack11 = this.goalstack12;
737 this.goalstack12 = this.goalstack13;
738 this.goalstack13 = this.goalstack14;
739 this.goalstack14 = this.goalstack15;
740 this.goalstack15 = this.goalstack16;
741 this.goalstack16 = this.goalstack17;
742 this.goalstack17 = this.goalstack18;
743 this.goalstack18 = this.goalstack19;
744 this.goalstack19 = this.goalstack20;
745 this.goalstack20 = this.goalstack21;
746 this.goalstack21 = this.goalstack22;
747 this.goalstack22 = this.goalstack23;
748 this.goalstack23 = this.goalstack24;
749 this.goalstack24 = this.goalstack25;
750 this.goalstack25 = this.goalstack26;
751 this.goalstack26 = this.goalstack27;
752 this.goalstack27 = this.goalstack28;
753 this.goalstack28 = this.goalstack29;
754 this.goalstack29 = this.goalstack30;
755 this.goalstack30 = this.goalstack31;
756 this.goalstack31 = NULL;
759 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
760 // waypoint destination coordinates instead of v (only useful for box waypoints)
761 // for normal waypoints v2 == v and v2_height == 0
762 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)
764 if (vdist(v - org, <, bestdist))
766 traceline(v, org, true, ent);
767 if (trace_fraction == 1)
771 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
776 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
784 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
785 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
788 ent = ent.tag_entity;
790 vector pm1 = ent.origin + ent.mins;
791 vector pm2 = ent.origin + ent.maxs;
793 // do two scans, because box test is cheaper
794 IL_EACH(g_waypoints, it != ent && it != except,
796 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
798 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
800 waypoint_clearlinks(ent); // initialize wpXXmincost fields
801 navigation_item_addlink(it, ent);
807 vector org = ent.origin;
808 if (navigation_testtracewalk)
812 vector v = '0 0 0', v2 = '0 0 0';
815 if(ent.size && !IS_PLAYER(ent))
817 org += 0.5 * (ent.mins + ent.maxs);
818 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
821 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
823 waypoint_clearlinks(ent); // initialize wpXXmincost fields
824 IL_EACH(g_waypoints, it != ent,
826 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
829 SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
830 if(vdist(v2 - it.origin, <, 1050))
831 if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
832 navigation_item_addlink(it, ent);
836 // box check failed, try walk
837 IL_EACH(g_waypoints, it != ent,
839 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
843 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
845 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
846 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
848 bestdist = vlen(v - org);
852 if(!best && !ent.navigation_dynamicgoal)
854 int solid_save = ent.solid;
855 ent.solid = SOLID_BSP;
856 IL_EACH(g_jumppads, true,
858 if(trigger_push_test(it, ent))
860 best = it.nearestwaypoint;
864 ent.solid = solid_save;
868 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
870 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
871 if (autocvar_g_waypointeditor_auto)
873 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
875 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
880 // finds the waypoints near the bot initiating a navigation query
881 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
884 //navigation_testtracewalk = true;
887 IL_EACH(g_waypoints, !it.wpconsidered,
889 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
891 vector diff = v - this.origin;
892 diff.z = max(0, diff.z);
893 if(vdist(diff, <, maxdist))
895 it.wpconsidered = true;
896 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
898 it.wpnearestpoint = v;
899 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
906 //navigation_testtracewalk = false;
910 // updates a path link if a spawnfunc_waypoint link is better than the current one
911 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
917 m1 = wp.origin + wp.mins;
918 m2 = wp.origin + wp.maxs;
919 v.x = bound(m1_x, p.x, m2_x);
920 v.y = bound(m1_y, p.y, m2_y);
921 v.z = bound(m1_z, p.z, m2_z);
925 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
926 cost += w.wp00mincost; // assuming teleport has exactly one destination
928 cost += waypoint_gettravelcost(p, v, w, wp);
929 if (wp.wpcost > cost)
934 wp.wpnearestpoint = v;
938 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
939 void navigation_markroutes(entity this, entity fixed_source_waypoint)
944 IL_EACH(g_waypoints, true,
946 it.wpconsidered = false;
947 it.wpnearestpoint = '0 0 0';
948 it.wpcost = 10000000;
953 if(fixed_source_waypoint)
955 fixed_source_waypoint.wpconsidered = true;
956 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
957 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
958 fixed_source_waypoint.wpfire = 1;
959 fixed_source_waypoint.enemy = NULL;
963 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
964 // as this search is expensive we will use lower values if the bot is on the air
965 float increment, maxdistance;
966 if(IS_ONGROUND(this))
977 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
980 bool searching = true;
984 IL_EACH(g_waypoints, it.wpfire,
989 p = it.wpnearestpoint;
991 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
992 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
993 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
994 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
995 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
996 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
997 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
998 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
999 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1000 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1001 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1002 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1003 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1004 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1005 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1028 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1029 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1033 IL_EACH(g_waypoints, true,
1035 it.wpconsidered = false;
1036 it.wpnearestpoint = '0 0 0';
1037 it.wpcost = 10000000;
1042 if(fixed_source_waypoint)
1044 fixed_source_waypoint.wpconsidered = true;
1045 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1046 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1047 fixed_source_waypoint.wpfire = 1;
1048 fixed_source_waypoint.enemy = NULL;
1052 error("need to start with a waypoint\n");
1055 bool searching = true;
1059 IL_EACH(g_waypoints, it.wpfire,
1063 cost = it.wpcost; // cost to walk from it to home
1064 p = it.wpnearestpoint;
1066 IL_EACH(g_waypoints, it != wp,
1068 if(!waypoint_islinked(it, wp))
1070 cost2 = cost + it.dmg;
1071 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1077 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1078 void navigation_routerating(entity this, entity e, float f, float rangebias)
1086 rangebias = waypoint_getlinearcost(rangebias);
1087 f = waypoint_getlinearcost(f);
1091 bool rate_wps = false;
1092 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1097 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1098 int t = pointcontents(trace_endpos + '0 0 1');
1099 if(t != CONTENT_SOLID )
1101 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1103 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1110 entity theEnemy = e;
1111 entity best_wp = NULL;
1112 float best_dist = 10000;
1113 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1114 && vdist(it.origin - this.origin, >, 100)
1115 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1117 float dist = vlen(it.origin - theEnemy.origin);
1118 if (dist < best_dist)
1130 vector goal_org = (e.absmin + e.absmax) * 0.5;
1132 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1134 // Evaluate path using jetpack
1136 if(this.items & IT_JETPACK)
1137 if(autocvar_bot_ai_navigation_jetpack)
1138 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1140 vector pointa, pointb;
1142 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1145 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1146 pointa = trace_endpos - '0 0 1';
1149 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1150 pointb = trace_endpos - '0 0 1';
1152 // Can I see these two points from the sky?
1153 traceline(pointa, pointb, MOVE_NORMAL, this);
1155 if(trace_fraction==1)
1157 LOG_DEBUG("jetpack ai: can bridge these two points");
1159 // Lower the altitude of these points as much as possible
1160 float zdistance, xydistance, cost, t, fuel;
1161 vector down, npa, npb;
1163 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1166 npa = pointa + down;
1167 npb = pointb + down;
1169 if(npa.z<=this.absmax.z)
1172 if(npb.z<=e.absmax.z)
1175 traceline(npa, npb, MOVE_NORMAL, this);
1176 if(trace_fraction==1)
1182 while(trace_fraction == 1);
1185 // Rough estimation of fuel consumption
1186 // (ignores acceleration and current xyz velocity)
1187 xydistance = vlen(pointa - pointb);
1188 zdistance = fabs(pointa.z - this.origin.z);
1190 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1191 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1192 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1194 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1197 if(this.ammo_fuel>fuel)
1200 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1201 // - between air and ground speeds)
1203 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1204 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1207 // Compare against other goals
1208 f = f * rangebias / (rangebias + cost);
1210 if (navigation_bestrating < f)
1212 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1213 navigation_bestrating = f;
1214 navigation_bestgoal = e;
1215 this.navigation_jetpack_goal = e;
1216 this.navigation_jetpack_point = pointb;
1224 //te_wizspike(e.origin);
1227 // update the cached spawnfunc_waypoint link on a dynamic item entity
1228 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1234 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1235 e.nearestwaypoint = NULL;
1237 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1238 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1240 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1243 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1245 if(!e.navigation_dynamicgoal)
1246 e.blacklisted = true;
1250 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1255 if(e.navigation_dynamicgoal)
1256 e.nearestwaypointtimeout = time + 2;
1257 else if(autocvar_g_waypointeditor)
1258 e.nearestwaypointtimeout = time + 3 + random() * 2;
1260 nwp = e.nearestwaypoint;
1263 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1265 if (nwp.wpcost < 10000000)
1267 //te_wizspike(nwp.wpnearestpoint);
1268 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1269 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1270 f = f * rangebias / (rangebias + cost);
1271 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1272 if (navigation_bestrating < f)
1274 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1275 navigation_bestrating = f;
1276 navigation_bestgoal = e;
1281 // adds an item to the the goal stack with the path to a given item
1282 bool navigation_routetogoal(entity this, entity e, vector startposition)
1284 // if there is no goal, just exit
1288 entity teleport_goal = NULL;
1290 this.goalentity = e;
1292 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1294 // force teleport destination as route destination
1296 navigation_pushroute(this, e.wp00);
1297 this.goalentity = e.wp00;
1300 // put the entity on the goal stack
1301 //print("routetogoal ", etos(e), "\n");
1302 navigation_pushroute(this, e);
1305 e = this.goalentity;
1307 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1309 this.wp_goal_prev1 = this.wp_goal_prev0;
1310 this.wp_goal_prev0 = e;
1314 if(e==this.navigation_jetpack_goal)
1317 // if it can reach the goal there is nothing more to do
1318 vector dest = '0 0 0';
1319 float dest_height = 0;
1320 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1321 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1324 entity nearest_wp = NULL;
1325 // see if there are waypoints describing a path to the item
1326 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1328 e = e.nearestwaypoint;
1331 else if(teleport_goal)
1334 e = e.enemy; // we already have added it, so...
1339 if(nearest_wp && nearest_wp.enemy)
1341 // often path can be optimized by not adding the nearest waypoint
1342 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1344 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1345 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1346 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1347 e = nearest_wp.enemy;
1349 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1350 e = nearest_wp.enemy;
1355 // add the spawnfunc_waypoint to the path
1356 navigation_pushroute(this, e);
1366 // removes any currently touching waypoints from the goal stack
1367 // (this is how bots detect if they reached a goal)
1368 int navigation_poptouchedgoals(entity this)
1370 int removed_goals = 0;
1372 if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1374 navigation_poproute(this);
1378 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1380 // make sure jumppad is really hit, don't rely on distance based checks
1381 // as they may report a touch even if it didn't really happen
1382 if(this.lastteleporttime > 0
1383 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1385 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1386 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1388 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1389 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1391 navigation_poproute(this);
1392 this.lastteleporttime = 0;
1396 return removed_goals;
1399 // If for some reason the bot is closer to the next goal, pop the current one
1400 if(this.goalstack01 && !wasfreed(this.goalstack01))
1401 if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1402 if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1403 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1405 vector dest = '0 0 0';
1406 float dest_height = 0;
1407 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1408 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1410 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1411 navigation_poproute(this);
1413 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1414 return removed_goals;
1415 // TODO this may also be a nice idea to do "early" (e.g. by
1416 // manipulating the vlen() comparisons) to shorten paths in
1417 // general - this would make bots walk more "on rails" than
1418 // "zigzagging" which they currently do with sufficiently
1419 // random-like waypoints, and thus can make a nice bot
1420 // personality property
1424 // Loose goal touching check when running
1425 if(this.aistatus & AI_STATUS_RUNNING)
1426 if(this.goalcurrent.classname=="waypoint")
1427 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1429 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1431 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1432 if(trace_fraction==1)
1434 // Detect personal waypoints
1435 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1436 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1438 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1439 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1442 navigation_poproute(this);
1444 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1445 return removed_goals;
1450 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1452 vector gc_min = this.goalcurrent.absmin;
1453 vector gc_max = this.goalcurrent.absmax;
1454 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1456 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1457 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1459 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1462 // Detect personal waypoints
1463 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1464 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1466 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1467 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1470 navigation_poproute(this);
1472 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1473 return removed_goals;
1475 return removed_goals;
1478 entity navigation_get_really_close_waypoint(entity this)
1480 entity wp = this.goalcurrent;
1481 if(!wp || vdist(wp.origin - this.origin, >, 50))
1482 wp = this.goalcurrent_prev;
1485 if(wp.classname != "waypoint")
1487 wp = wp.nearestwaypoint;
1491 if(vdist(wp.origin - this.origin, >, 50))
1493 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1495 if(vdist(it.origin - this.origin, <, 50))
1502 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1505 vector dest = '0 0 0';
1506 float dest_height = 0;
1507 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1508 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1513 // begin a goal selection session (queries spawnfunc_waypoint network)
1514 void navigation_goalrating_start(entity this)
1516 if(this.aistatus & AI_STATUS_STUCK)
1519 this.navigation_jetpack_goal = NULL;
1520 navigation_bestrating = -1;
1521 entity wp = navigation_get_really_close_waypoint(this);
1522 navigation_clearroute(this);
1523 navigation_bestgoal = NULL;
1524 navigation_markroutes(this, wp);
1527 // ends a goal selection session (updates goal stack to the best goal)
1528 void navigation_goalrating_end(entity this)
1530 if(this.aistatus & AI_STATUS_STUCK)
1533 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1534 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1536 // If the bot got stuck then try to reach the farthest waypoint
1537 if (!this.goalentity && autocvar_bot_wander_enable)
1539 if (!(this.aistatus & AI_STATUS_STUCK))
1541 LOG_DEBUG(this.netname, " cannot walk to any goal");
1542 this.aistatus |= AI_STATUS_STUCK;
1547 void botframe_updatedangerousobjects(float maxupdate)
1549 vector m1, m2, v, o;
1553 IL_EACH(g_waypoints, true,
1559 IL_EACH(g_bot_dodge, it.bot_dodge,
1562 v.x = bound(m1_x, v.x, m2_x);
1563 v.y = bound(m1_y, v.y, m2_y);
1564 v.z = bound(m1_z, v.z, m2_z);
1565 o = (it.absmin + it.absmax) * 0.5;
1566 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1569 traceline(o, v, true, NULL);
1570 if (trace_fraction == 1)
1571 danger = danger + d;
1581 void navigation_unstuck(entity this)
1583 float search_radius = 1000;
1585 if (!autocvar_bot_wander_enable)
1588 if (!bot_waypoint_queue_owner)
1590 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1591 bot_waypoint_queue_owner = this;
1592 bot_waypoint_queue_bestgoal = NULL;
1593 bot_waypoint_queue_bestgoalrating = 0;
1596 if(bot_waypoint_queue_owner!=this)
1599 if (bot_waypoint_queue_goal)
1601 // evaluate the next goal on the queue
1602 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1603 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1604 vector dest = '0 0 0';
1605 float dest_height = 0;
1606 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1607 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1609 if( d > bot_waypoint_queue_bestgoalrating)
1611 bot_waypoint_queue_bestgoalrating = d;
1612 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1615 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1617 if (!bot_waypoint_queue_goal)
1619 if (bot_waypoint_queue_bestgoal)
1621 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1622 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1623 navigation_goalrating_timeout_set(this);
1624 this.aistatus &= ~AI_STATUS_STUCK;
1628 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1631 bot_waypoint_queue_owner = NULL;
1636 if(bot_strategytoken!=this)
1639 // build a new queue
1640 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1642 entity first = NULL;
1644 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1646 if(bot_waypoint_queue_goal)
1647 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1651 bot_waypoint_queue_goal = it;
1652 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1656 bot_waypoint_queue_goal = first;
1659 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1660 bot_waypoint_queue_owner = NULL;
1665 // Support for debugging tracewalk visually
1667 void debugresetnodes()
1669 debuglastnode = '0 0 0';
1672 void debugnode(entity this, vector node)
1674 if (!IS_PLAYER(this))
1677 if(debuglastnode=='0 0 0')
1679 debuglastnode = node;
1683 te_lightning2(NULL, node, debuglastnode);
1684 debuglastnode = node;
1687 void debugnodestatus(vector position, float status)
1693 case DEBUG_NODE_SUCCESS:
1696 case DEBUG_NODE_WARNING:
1699 case DEBUG_NODE_FAIL:
1706 te_customflash(position, 40, 2, c);
1709 // Support for debugging the goal stack visually
1712 .vector lastposition;
1714 // Debug the goal stack visually
1715 void debuggoalstack(entity this)
1720 if(this.goalcounter==0)goal=this.goalcurrent;
1721 else if(this.goalcounter==1)goal=this.goalstack01;
1722 else if(this.goalcounter==2)goal=this.goalstack02;
1723 else if(this.goalcounter==3)goal=this.goalstack03;
1724 else if(this.goalcounter==4)goal=this.goalstack04;
1725 else if(this.goalcounter==5)goal=this.goalstack05;
1726 else if(this.goalcounter==6)goal=this.goalstack06;
1727 else if(this.goalcounter==7)goal=this.goalstack07;
1728 else if(this.goalcounter==8)goal=this.goalstack08;
1729 else if(this.goalcounter==9)goal=this.goalstack09;
1730 else if(this.goalcounter==10)goal=this.goalstack10;
1731 else if(this.goalcounter==11)goal=this.goalstack11;
1732 else if(this.goalcounter==12)goal=this.goalstack12;
1733 else if(this.goalcounter==13)goal=this.goalstack13;
1734 else if(this.goalcounter==14)goal=this.goalstack14;
1735 else if(this.goalcounter==15)goal=this.goalstack15;
1736 else if(this.goalcounter==16)goal=this.goalstack16;
1737 else if(this.goalcounter==17)goal=this.goalstack17;
1738 else if(this.goalcounter==18)goal=this.goalstack18;
1739 else if(this.goalcounter==19)goal=this.goalstack19;
1740 else if(this.goalcounter==20)goal=this.goalstack20;
1741 else if(this.goalcounter==21)goal=this.goalstack21;
1742 else if(this.goalcounter==22)goal=this.goalstack22;
1743 else if(this.goalcounter==23)goal=this.goalstack23;
1744 else if(this.goalcounter==24)goal=this.goalstack24;
1745 else if(this.goalcounter==25)goal=this.goalstack25;
1746 else if(this.goalcounter==26)goal=this.goalstack26;
1747 else if(this.goalcounter==27)goal=this.goalstack27;
1748 else if(this.goalcounter==28)goal=this.goalstack28;
1749 else if(this.goalcounter==29)goal=this.goalstack29;
1750 else if(this.goalcounter==30)goal=this.goalstack30;
1751 else if(this.goalcounter==31)goal=this.goalstack31;
1756 this.goalcounter = 0;
1757 this.lastposition='0 0 0';
1761 if(this.lastposition=='0 0 0')
1764 org = this.lastposition;
1767 go = ( goal.absmin + goal.absmax ) * 0.5;
1768 te_lightning2(NULL, org, go);
1769 this.lastposition = go;