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_dynamicgoal_init(entity this, bool initially_static)
22 this.navigation_dynamicgoal = true;
23 this.bot_basewaypoint = this.nearestwaypoint;
25 this.nearestwaypointtimeout = -1;
27 this.nearestwaypointtimeout = time;
30 void navigation_dynamicgoal_set(entity this)
32 this.nearestwaypointtimeout = time;
35 void navigation_dynamicgoal_unset(entity this)
37 if(this.bot_basewaypoint)
38 this.nearestwaypoint = this.bot_basewaypoint;
39 this.nearestwaypointtimeout = -1;
42 bool navigation_check_submerged_state(entity ent, vector pos)
46 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
47 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
48 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
51 submerged = SUBMERGED(pos);
52 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
53 // is actually submerged because often they are inside some solid.
54 // That's why submerged state is saved now that we know current pos is
55 // not stuck in solid (previous tracewalk call to this pos was successfully)
56 if(!ent.navigation_dynamicgoal)
57 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
62 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
64 IL_EACH(g_ladders, it.classname == "func_ladder",
67 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
68 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
71 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
72 tracebox(org, m1, m2, top, movemode, e);
73 if(trace_fraction == 1)
80 vector resurface_limited(vector org, float lim, vector m1)
82 if (WETFEET(org + eZ * (lim - org.z)))
86 float RES_min_h = org.z;
87 float RES_max_h = lim;
89 org.z = 0.5 * (RES_min_h + RES_max_h);
94 } while (RES_max_h - RES_min_h >= 1);
99 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
102 #define NAV_SWIM_ONWATER 1
103 #define NAV_SWIM_UNDERWATER 2
105 // rough simulation of walking from one point to another to test if a path
106 // can be traveled, used for waypoint linking and havocbot
107 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
108 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
110 if(autocvar_bot_debug_tracewalk)
117 vector flatdir = end - start;
119 float flatdist = vlen(flatdir);
120 flatdir = normalize(flatdir);
122 bool ignorehazards = false;
125 // Analyze starting point
126 traceline(start, start, MOVE_NORMAL, e);
127 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
128 ignorehazards = true;
130 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
131 if (trace_startsolid)
134 if(autocvar_bot_debug_tracewalk)
135 debugnodestatus(start, DEBUG_NODE_FAIL);
137 //print("tracewalk: ", vtos(start), " is a bad start\n");
143 end2.z += end_height;
145 vector fixed_end = end;
148 if (flatdist > 0 && WETFEET(org))
151 nav_action = NAV_SWIM_UNDERWATER;
154 // tracebox down by player's height
155 // useful to know if water level is so low that bot can still walk
156 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
157 if (SUBMERGED(trace_endpos))
160 nav_action = NAV_SWIM_UNDERWATER;
163 nav_action = NAV_WALK;
167 nav_action = NAV_WALK;
175 if (org.z > end2.z + 1)
177 tracebox(org, m1, m2, end2, movemode, e);
179 if (org.z > end2.z + 1)
182 else if (org.z < end.z - 1)
184 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
185 if (SUBMERGED(trace_endpos))
187 vector v = trace_endpos;
188 tracebox(v, m1, m2, end, movemode, e);
189 if(trace_endpos.z >= end.z - 1)
191 RESURFACE_LIMITED(v, trace_endpos.z);
195 else if (trace_endpos.z > org.z - jumpheight_vec.z)
196 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
198 if (org.z < end.z - 1)
205 if(autocvar_bot_debug_tracewalk)
208 debugnodestatus(org, DEBUG_NODE_SUCCESS);
211 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
216 if(autocvar_bot_debug_tracewalk)
222 if (stepdist > flatdist)
224 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
226 // can't use movement direction here to calculate move because of
227 // precision errors especially when direction has a high enough z value
228 //water_dir = normalize(water_end - org);
229 //move = org + water_dir * stepdist;
230 fixed_end.z = bound(end.z, org.z, end2.z);
231 if (stepdist == flatdist) {
235 move = org + (fixed_end - org) * (stepdist / flatdist);
236 flatdist = vlen(vec2(fixed_end - move));
239 else // horiz. direction
241 flatdist -= stepdist;
242 move = org + flatdir * stepdist;
245 if(nav_action == NAV_SWIM_ONWATER)
247 tracebox(org, m1, m2, move, movemode, e); // swim
250 if (trace_fraction < 1)
253 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
255 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
258 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
260 if(autocvar_bot_debug_tracewalk)
263 debugnodestatus(org, DEBUG_NODE_SUCCESS);
266 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
270 if(autocvar_bot_debug_tracewalk)
271 debugnodestatus(org, DEBUG_NODE_FAIL);
274 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
285 if (org.z <= move.z) // going horiz.
287 tracebox(trace_endpos, m1, m2, move, movemode, e);
289 nav_action = NAV_WALK;
294 if (org.z <= move.z) // going horiz.
297 nav_action = NAV_SWIM_ONWATER;
303 nav_action = NAV_SWIM_UNDERWATER;
305 nav_action = NAV_SWIM_ONWATER;
308 else if(nav_action == NAV_SWIM_UNDERWATER)
310 if (move.z >= org.z) // swimming upwards or horiz.
312 tracebox(org, m1, m2, move, movemode, e); // swim
314 bool stepswum = false;
317 if (trace_fraction < 1)
320 vector stepswim_move = move + stepheightvec;
321 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
322 stepswim_move.z = end2.z;
324 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
327 if (trace_startsolid)
329 if(autocvar_bot_debug_tracewalk)
330 debugnodestatus(org, DEBUG_NODE_FAIL);
332 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
336 if (trace_fraction < 1)
338 float org_z_prev = org.z;
339 RESURFACE_LIMITED(org, end2.z);
340 if(org.z == org_z_prev)
342 if(autocvar_bot_debug_tracewalk)
343 debugnodestatus(org, DEBUG_NODE_FAIL);
345 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
349 nav_action = NAV_SWIM_UNDERWATER;
351 nav_action = NAV_SWIM_ONWATER;
353 // we didn't advance horiz. in this step, flatdist decrease should be reverted
354 // but we can't do it properly right now... apply this workaround instead
372 if (!WETFEET(trace_endpos))
374 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
375 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
376 if (!stepswum && SUBMERGED(trace_endpos))
378 RESURFACE_LIMITED(trace_endpos, end2.z);
380 nav_action = NAV_SWIM_ONWATER;
386 nav_action = NAV_WALK;
392 nav_action = NAV_SWIM_UNDERWATER;
395 else //if (move.z < org.z) // swimming downwards
397 tracebox(org, m1, m2, move, movemode, e); // swim
400 if (trace_fraction < 1)
403 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
406 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
408 if(autocvar_bot_debug_tracewalk)
409 debugnodestatus(move, DEBUG_NODE_FAIL);
411 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
423 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
425 // stepswim caused upwards direction
426 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
427 if (!SUBMERGED(trace_endpos))
430 nav_action = NAV_WALK;
437 nav_action = NAV_SWIM_UNDERWATER;
441 else if(nav_action == NAV_WALK)
444 tracebox(org, m1, m2, move, movemode, e);
446 if(autocvar_bot_debug_tracewalk)
447 debugnode(e, trace_endpos);
450 if (trace_fraction < 1)
452 // check if we can walk over this obstacle, possibly by jumpstepping
453 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
454 if (trace_fraction < 1 || trace_startsolid)
456 if (trace_startsolid) // hit ceiling above org
458 // reduce stepwalk height
459 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
460 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
462 else //if (trace_fraction < 1)
464 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
465 if (trace_startsolid) // hit ceiling above org
467 // reduce jumpstepwalk height
468 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
469 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
473 if (trace_fraction < 1)
475 vector v = trace_endpos;
476 v.z = org.z + jumpheight_vec.z;
477 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
479 if(autocvar_bot_debug_tracewalk)
482 debugnodestatus(v, DEBUG_NODE_SUCCESS);
485 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
489 if(autocvar_bot_debug_tracewalk)
490 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
492 traceline( org, move, movemode, e);
494 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
498 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
500 nextmove = move + (flatdir * stepdist);
501 traceline( move, nextmove, movemode, e);
504 flatdist = vlen(vec2(end - move));
508 if(autocvar_bot_debug_tracewalk)
509 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
511 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
512 //te_explosion(trace_endpos);
513 //print(ftos(e.dphitcontentsmask), "\n");
514 return false; // failed
526 // trace down from stepheight as far as possible and move there,
527 // if this starts in solid we try again without the stepup, and
528 // if that also fails we assume it is a wall
529 // (this is the same logic as the Quake walkmove function used)
530 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
538 if(autocvar_bot_debug_tracewalk)
540 debugnode(e, trace_endpos);
541 debugnodestatus(org, DEBUG_NODE_FAIL);
544 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
551 if(move.z >= end2.z && org.z < end2.z)
556 if(org.z > move.z - 1 || !SUBMERGED(org))
558 nav_action = NAV_WALK;
562 // ended up submerged while walking
563 if(autocvar_bot_debug_tracewalk)
566 RESURFACE_LIMITED(org, move.z);
567 nav_action = NAV_SWIM_ONWATER;
572 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
574 // moved but didn't arrive at the intended destination
575 if(autocvar_bot_debug_tracewalk)
576 debugnodestatus(org, DEBUG_NODE_FAIL);
581 /////////////////////////////////////////////////////////////////////////////
583 /////////////////////////////////////////////////////////////////////////////
585 // completely empty the goal stack, used when deciding where to go
586 void navigation_clearroute(entity this)
588 this.goalcurrent_prev = this.goalcurrent;
589 this.goalcurrent_distance = 10000000;
590 this.goalcurrent_distance_time = 0;
591 //print("bot ", etos(this), " clear\n");
592 this.goalentity = NULL;
593 this.goalcurrent = NULL;
594 this.goalstack01 = NULL;
595 this.goalstack02 = NULL;
596 this.goalstack03 = NULL;
597 this.goalstack04 = NULL;
598 this.goalstack05 = NULL;
599 this.goalstack06 = NULL;
600 this.goalstack07 = NULL;
601 this.goalstack08 = NULL;
602 this.goalstack09 = NULL;
603 this.goalstack10 = NULL;
604 this.goalstack11 = NULL;
605 this.goalstack12 = NULL;
606 this.goalstack13 = NULL;
607 this.goalstack14 = NULL;
608 this.goalstack15 = NULL;
609 this.goalstack16 = NULL;
610 this.goalstack17 = NULL;
611 this.goalstack18 = NULL;
612 this.goalstack19 = NULL;
613 this.goalstack20 = NULL;
614 this.goalstack21 = NULL;
615 this.goalstack22 = NULL;
616 this.goalstack23 = NULL;
617 this.goalstack24 = NULL;
618 this.goalstack25 = NULL;
619 this.goalstack26 = NULL;
620 this.goalstack27 = NULL;
621 this.goalstack28 = NULL;
622 this.goalstack29 = NULL;
623 this.goalstack30 = NULL;
624 this.goalstack31 = NULL;
627 // add a new goal at the beginning of the stack
628 // (in other words: add a new prerequisite before going to the later goals)
629 // NOTE: when a waypoint is added, the WP gets pushed first, then the
630 // next-closest WP on the shortest path to the WP
631 // That means, if the stack overflows, the bot will know how to do the FIRST 32
632 // steps to the goal, and then recalculate the path.
633 void navigation_pushroute(entity this, entity e)
635 this.goalcurrent_prev = this.goalcurrent;
636 this.goalcurrent_distance = 10000000;
637 this.goalcurrent_distance_time = 0;
638 //print("bot ", etos(this), " push ", etos(e), "\n");
639 if(this.goalstack31 == this.goalentity)
640 this.goalentity = NULL;
641 this.goalstack31 = this.goalstack30;
642 this.goalstack30 = this.goalstack29;
643 this.goalstack29 = this.goalstack28;
644 this.goalstack28 = this.goalstack27;
645 this.goalstack27 = this.goalstack26;
646 this.goalstack26 = this.goalstack25;
647 this.goalstack25 = this.goalstack24;
648 this.goalstack24 = this.goalstack23;
649 this.goalstack23 = this.goalstack22;
650 this.goalstack22 = this.goalstack21;
651 this.goalstack21 = this.goalstack20;
652 this.goalstack20 = this.goalstack19;
653 this.goalstack19 = this.goalstack18;
654 this.goalstack18 = this.goalstack17;
655 this.goalstack17 = this.goalstack16;
656 this.goalstack16 = this.goalstack15;
657 this.goalstack15 = this.goalstack14;
658 this.goalstack14 = this.goalstack13;
659 this.goalstack13 = this.goalstack12;
660 this.goalstack12 = this.goalstack11;
661 this.goalstack11 = this.goalstack10;
662 this.goalstack10 = this.goalstack09;
663 this.goalstack09 = this.goalstack08;
664 this.goalstack08 = this.goalstack07;
665 this.goalstack07 = this.goalstack06;
666 this.goalstack06 = this.goalstack05;
667 this.goalstack05 = this.goalstack04;
668 this.goalstack04 = this.goalstack03;
669 this.goalstack03 = this.goalstack02;
670 this.goalstack02 = this.goalstack01;
671 this.goalstack01 = this.goalcurrent;
672 this.goalcurrent = e;
675 // remove first goal from stack
676 // (in other words: remove a prerequisite for reaching the later goals)
677 // (used when a spawnfunc_waypoint is reached)
678 void navigation_poproute(entity this)
680 this.goalcurrent_prev = this.goalcurrent;
681 this.goalcurrent_distance = 10000000;
682 this.goalcurrent_distance_time = 0;
683 //print("bot ", etos(this), " pop\n");
684 if(this.goalcurrent == this.goalentity)
685 this.goalentity = NULL;
686 this.goalcurrent = this.goalstack01;
687 this.goalstack01 = this.goalstack02;
688 this.goalstack02 = this.goalstack03;
689 this.goalstack03 = this.goalstack04;
690 this.goalstack04 = this.goalstack05;
691 this.goalstack05 = this.goalstack06;
692 this.goalstack06 = this.goalstack07;
693 this.goalstack07 = this.goalstack08;
694 this.goalstack08 = this.goalstack09;
695 this.goalstack09 = this.goalstack10;
696 this.goalstack10 = this.goalstack11;
697 this.goalstack11 = this.goalstack12;
698 this.goalstack12 = this.goalstack13;
699 this.goalstack13 = this.goalstack14;
700 this.goalstack14 = this.goalstack15;
701 this.goalstack15 = this.goalstack16;
702 this.goalstack16 = this.goalstack17;
703 this.goalstack17 = this.goalstack18;
704 this.goalstack18 = this.goalstack19;
705 this.goalstack19 = this.goalstack20;
706 this.goalstack20 = this.goalstack21;
707 this.goalstack21 = this.goalstack22;
708 this.goalstack22 = this.goalstack23;
709 this.goalstack23 = this.goalstack24;
710 this.goalstack24 = this.goalstack25;
711 this.goalstack25 = this.goalstack26;
712 this.goalstack26 = this.goalstack27;
713 this.goalstack27 = this.goalstack28;
714 this.goalstack28 = this.goalstack29;
715 this.goalstack29 = this.goalstack30;
716 this.goalstack30 = this.goalstack31;
717 this.goalstack31 = NULL;
720 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
721 // waypoint destination coordinates instead of v (only useful for box waypoints)
722 // for normal waypoints v2 == v and v2_height == 0
723 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)
725 if (vdist(v - org, <, bestdist))
727 traceline(v, org, true, ent);
728 if (trace_fraction == 1)
732 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
737 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
745 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
746 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
749 ent = ent.tag_entity;
751 vector pm1 = ent.origin + ent.mins;
752 vector pm2 = ent.origin + ent.maxs;
754 // do two scans, because box test is cheaper
755 IL_EACH(g_waypoints, it != ent && it != except,
757 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
761 vector org = ent.origin;
762 if (navigation_testtracewalk)
766 vector v = '0 0 0', v2 = '0 0 0';
769 if(ent.size && !IS_PLAYER(ent))
771 org += 0.5 * (ent.mins + ent.maxs);
772 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
775 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
777 waypoint_clearlinks(ent); // initialize wpXXmincost fields
778 IL_EACH(g_waypoints, it != ent,
780 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
783 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
784 if(vdist(v2 - org, <, 1050))
785 if(tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
786 navigation_item_addlink(it, ent);
790 // box check failed, try walk
791 IL_EACH(g_waypoints, it != ent,
793 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
797 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
799 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
800 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
802 bestdist = vlen(v - org);
808 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
810 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
811 if (autocvar_g_waypointeditor_auto)
813 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
815 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
820 // finds the waypoints near the bot initiating a navigation query
821 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
824 //navigation_testtracewalk = true;
827 IL_EACH(g_waypoints, !it.wpconsidered,
829 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
831 vector diff = v - this.origin;
832 diff.z = max(0, diff.z);
833 if(vdist(diff, <, maxdist))
835 it.wpconsidered = true;
836 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
838 it.wpnearestpoint = v;
839 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
846 //navigation_testtracewalk = false;
850 // updates a path link if a spawnfunc_waypoint link is better than the current one
851 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
857 m1 = wp.origin + wp.mins;
858 m2 = wp.origin + wp.maxs;
859 v.x = bound(m1_x, p.x, m2_x);
860 v.y = bound(m1_y, p.y, m2_y);
861 v.z = bound(m1_z, p.z, m2_z);
865 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
866 cost += w.wp00mincost; // assuming teleport has exactly one destination
868 cost += waypoint_gettravelcost(p, v, w, wp);
869 if (wp.wpcost > cost)
874 wp.wpnearestpoint = v;
878 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
879 void navigation_markroutes(entity this, entity fixed_source_waypoint)
884 IL_EACH(g_waypoints, true,
886 it.wpconsidered = false;
887 it.wpnearestpoint = '0 0 0';
888 it.wpcost = 10000000;
893 if(fixed_source_waypoint)
895 fixed_source_waypoint.wpconsidered = true;
896 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
897 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
898 fixed_source_waypoint.wpfire = 1;
899 fixed_source_waypoint.enemy = NULL;
903 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
904 // as this search is expensive we will use lower values if the bot is on the air
905 float increment, maxdistance;
906 if(IS_ONGROUND(this))
917 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
920 bool searching = true;
924 IL_EACH(g_waypoints, it.wpfire,
929 p = it.wpnearestpoint;
931 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
932 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
933 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
934 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
935 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
936 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
937 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
938 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
939 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
940 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
941 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
942 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
943 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
944 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
945 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
946 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
947 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
948 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
949 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
950 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
951 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
952 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
953 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
954 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
955 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
956 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
957 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
958 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
959 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
960 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
961 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
962 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
963 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
968 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
969 void navigation_markroutes_inverted(entity fixed_source_waypoint)
973 IL_EACH(g_waypoints, true,
975 it.wpconsidered = false;
976 it.wpnearestpoint = '0 0 0';
977 it.wpcost = 10000000;
982 if(fixed_source_waypoint)
984 fixed_source_waypoint.wpconsidered = true;
985 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
986 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
987 fixed_source_waypoint.wpfire = 1;
988 fixed_source_waypoint.enemy = NULL;
992 error("need to start with a waypoint\n");
995 bool searching = true;
999 IL_EACH(g_waypoints, it.wpfire,
1003 cost = it.wpcost; // cost to walk from it to home
1004 p = it.wpnearestpoint;
1006 IL_EACH(g_waypoints, it != wp,
1008 if(!waypoint_islinked(it, wp))
1010 cost2 = cost + it.dmg;
1011 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1017 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1018 void navigation_routerating(entity this, entity e, float f, float rangebias)
1026 rangebias = waypoint_getlinearcost(rangebias);
1027 f = waypoint_getlinearcost(f);
1031 bool rate_wps = false;
1032 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1037 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1038 int t = pointcontents(trace_endpos + '0 0 1');
1039 if(t != CONTENT_SOLID )
1041 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1043 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1050 entity theEnemy = e;
1051 entity best_wp = NULL;
1052 float best_dist = 10000;
1053 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1054 && vdist(it.origin - this.origin, >, 100)
1055 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1057 float dist = vlen(it.origin - theEnemy.origin);
1058 if (dist < best_dist)
1070 vector goal_org = (e.absmin + e.absmax) * 0.5;
1072 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1074 // Evaluate path using jetpack
1076 if(this.items & IT_JETPACK)
1077 if(autocvar_bot_ai_navigation_jetpack)
1078 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1080 vector pointa, pointb;
1082 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1085 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1086 pointa = trace_endpos - '0 0 1';
1089 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1090 pointb = trace_endpos - '0 0 1';
1092 // Can I see these two points from the sky?
1093 traceline(pointa, pointb, MOVE_NORMAL, this);
1095 if(trace_fraction==1)
1097 LOG_DEBUG("jetpack ai: can bridge these two points");
1099 // Lower the altitude of these points as much as possible
1100 float zdistance, xydistance, cost, t, fuel;
1101 vector down, npa, npb;
1103 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1106 npa = pointa + down;
1107 npb = pointb + down;
1109 if(npa.z<=this.absmax.z)
1112 if(npb.z<=e.absmax.z)
1115 traceline(npa, npb, MOVE_NORMAL, this);
1116 if(trace_fraction==1)
1122 while(trace_fraction == 1);
1125 // Rough estimation of fuel consumption
1126 // (ignores acceleration and current xyz velocity)
1127 xydistance = vlen(pointa - pointb);
1128 zdistance = fabs(pointa.z - this.origin.z);
1130 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1131 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1132 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1134 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1137 if(this.ammo_fuel>fuel)
1140 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1141 // - between air and ground speeds)
1143 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1144 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1147 // Compare against other goals
1148 f = f * rangebias / (rangebias + cost);
1150 if (navigation_bestrating < f)
1152 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1153 navigation_bestrating = f;
1154 navigation_bestgoal = e;
1155 this.navigation_jetpack_goal = e;
1156 this.navigation_jetpack_point = pointb;
1164 //te_wizspike(e.origin);
1167 // update the cached spawnfunc_waypoint link on a dynamic item entity
1168 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1174 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1175 e.nearestwaypoint = NULL;
1177 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1178 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1180 nwp = navigation_findnearestwaypoint(e, true);
1183 e.nearestwaypoint = nwp;
1185 vector m1 = nwp.absmin, m2 = nwp.absmax;
1186 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
1187 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
1188 vector ve = (e.absmin - e.absmax) * 0.5;
1189 ve.x = bound(m1.x, ve.x, m2.x);
1190 ve.y = bound(m1.y, ve.y, m2.y);
1191 ve.z = bound(m1.z, ve.z, m2.z);
1193 m1 = e.absmin; m2 = e.absmax;
1194 m1.x = e.origin.x; m1.y = e.origin.y;
1195 m2.x = e.origin.x; m2.y = e.origin.y;
1196 vector vnwp = nwp.origin;
1197 vnwp.x = bound(m1.x, vnwp.x, m2.x);
1198 vnwp.y = bound(m1.y, vnwp.y, m2.y);
1199 vnwp.z = bound(m1.z, vnwp.z, m2.z);
1200 e.nearestwaypoint_dist = vlen(ve - vnwp);
1204 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1206 if(!e.navigation_dynamicgoal)
1207 e.blacklisted = true;
1211 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1216 if(e.navigation_dynamicgoal)
1217 e.nearestwaypointtimeout = time + 2;
1218 else if(autocvar_g_waypointeditor)
1219 e.nearestwaypointtimeout = time + 3 + random() * 2;
1221 nwp = e.nearestwaypoint;
1224 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1226 if (nwp.wpcost < 10000000)
1228 //te_wizspike(nwp.wpnearestpoint);
1229 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1230 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1231 f = f * rangebias / (rangebias + cost);
1232 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1233 if (navigation_bestrating < f)
1235 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1236 navigation_bestrating = f;
1237 navigation_bestgoal = e;
1242 // adds an item to the the goal stack with the path to a given item
1243 bool navigation_routetogoal(entity this, entity e, vector startposition)
1245 // if there is no goal, just exit
1249 entity teleport_goal = NULL;
1251 this.goalentity = e;
1253 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1255 // force teleport destination as route destination
1257 navigation_pushroute(this, e.wp00);
1258 this.goalentity = e.wp00;
1261 // put the entity on the goal stack
1262 //print("routetogoal ", etos(e), "\n");
1263 navigation_pushroute(this, e);
1266 e = this.goalentity;
1268 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1270 this.wp_goal_prev1 = this.wp_goal_prev0;
1271 this.wp_goal_prev0 = e;
1275 if(e==this.navigation_jetpack_goal)
1278 // if it can reach the goal there is nothing more to do
1279 vector dest = '0 0 0';
1280 float dest_height = 0;
1281 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1282 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1285 entity nearest_wp = NULL;
1286 // see if there are waypoints describing a path to the item
1287 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1289 e = e.nearestwaypoint;
1292 else if(teleport_goal)
1295 e = e.enemy; // we already have added it, so...
1300 if(nearest_wp && nearest_wp.enemy)
1302 // often path can be optimized by not adding the nearest waypoint
1303 if (this.goalentity.nearestwaypoint_dist < 8)
1304 e = nearest_wp.enemy;
1307 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1309 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1310 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1311 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1312 e = nearest_wp.enemy;
1314 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1315 e = nearest_wp.enemy;
1321 // add the spawnfunc_waypoint to the path
1322 navigation_pushroute(this, e);
1332 // removes any currently touching waypoints from the goal stack
1333 // (this is how bots detect if they reached a goal)
1334 void navigation_poptouchedgoals(entity this)
1336 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1338 // make sure jumppad is really hit, don't rely on distance based checks
1339 // as they may report a touch even if it didn't really happen
1340 if(this.lastteleporttime > 0
1341 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1343 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1344 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1346 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1347 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1349 navigation_poproute(this);
1355 // If for some reason the bot is closer to the next goal, pop the current one
1356 if(this.goalstack01 && !wasfreed(this.goalstack01))
1357 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1358 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1360 vector dest = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
1361 dest.z = this.goalstack01.absmin.z;
1362 float dest_height = this.goalstack01.absmax.z - this.goalstack01.absmin.z;
1363 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1365 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1366 navigation_poproute(this);
1367 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1369 // TODO this may also be a nice idea to do "early" (e.g. by
1370 // manipulating the vlen() comparisons) to shorten paths in
1371 // general - this would make bots walk more "on rails" than
1372 // "zigzagging" which they currently do with sufficiently
1373 // random-like waypoints, and thus can make a nice bot
1374 // personality property
1378 // Loose goal touching check when running
1379 if(this.aistatus & AI_STATUS_RUNNING)
1380 if(this.goalcurrent.classname=="waypoint")
1381 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1383 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1385 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1386 if(trace_fraction==1)
1388 // Detect personal waypoints
1389 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1390 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1392 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1393 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1396 navigation_poproute(this);
1397 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1403 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1405 vector gc_min = this.goalcurrent.absmin;
1406 vector gc_max = this.goalcurrent.absmax;
1407 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1409 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1410 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1412 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1415 // Detect personal waypoints
1416 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1417 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1419 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1420 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1423 navigation_poproute(this);
1424 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1429 // begin a goal selection session (queries spawnfunc_waypoint network)
1430 void navigation_goalrating_start(entity this)
1432 if(this.aistatus & AI_STATUS_STUCK)
1435 this.navigation_jetpack_goal = NULL;
1436 navigation_bestrating = -1;
1437 navigation_clearroute(this);
1438 navigation_bestgoal = NULL;
1439 navigation_markroutes(this, NULL);
1442 // ends a goal selection session (updates goal stack to the best goal)
1443 void navigation_goalrating_end(entity this)
1445 if(this.aistatus & AI_STATUS_STUCK)
1448 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1449 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1451 // If the bot got stuck then try to reach the farthest waypoint
1452 if (!this.goalentity && autocvar_bot_wander_enable)
1454 if (!(this.aistatus & AI_STATUS_STUCK))
1456 LOG_DEBUG(this.netname, " cannot walk to any goal");
1457 this.aistatus |= AI_STATUS_STUCK;
1462 void botframe_updatedangerousobjects(float maxupdate)
1464 vector m1, m2, v, o;
1468 IL_EACH(g_waypoints, true,
1474 IL_EACH(g_bot_dodge, it.bot_dodge,
1477 v.x = bound(m1_x, v.x, m2_x);
1478 v.y = bound(m1_y, v.y, m2_y);
1479 v.z = bound(m1_z, v.z, m2_z);
1480 o = (it.absmin + it.absmax) * 0.5;
1481 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1484 traceline(o, v, true, NULL);
1485 if (trace_fraction == 1)
1486 danger = danger + d;
1496 void navigation_unstuck(entity this)
1498 float search_radius = 1000;
1500 if (!autocvar_bot_wander_enable)
1503 if (!bot_waypoint_queue_owner)
1505 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1506 bot_waypoint_queue_owner = this;
1507 bot_waypoint_queue_bestgoal = NULL;
1508 bot_waypoint_queue_bestgoalrating = 0;
1511 if(bot_waypoint_queue_owner!=this)
1514 if (bot_waypoint_queue_goal)
1516 // evaluate the next goal on the queue
1517 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1518 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1519 vector dest = (bot_waypoint_queue_goal.absmin + bot_waypoint_queue_goal.absmax) * 0.5;
1520 dest.z = bot_waypoint_queue_goal.absmin.z;
1521 float dest_height = bot_waypoint_queue_goal.absmax.z - bot_waypoint_queue_goal.absmin.z;
1522 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1524 if( d > bot_waypoint_queue_bestgoalrating)
1526 bot_waypoint_queue_bestgoalrating = d;
1527 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1530 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1532 if (!bot_waypoint_queue_goal)
1534 if (bot_waypoint_queue_bestgoal)
1536 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1537 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1538 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1539 this.aistatus &= ~AI_STATUS_STUCK;
1543 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1546 bot_waypoint_queue_owner = NULL;
1551 if(bot_strategytoken!=this)
1554 // build a new queue
1555 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1557 entity first = NULL;
1559 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1561 if(bot_waypoint_queue_goal)
1562 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1566 bot_waypoint_queue_goal = it;
1567 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1571 bot_waypoint_queue_goal = first;
1574 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1575 bot_waypoint_queue_owner = NULL;
1580 // Support for debugging tracewalk visually
1582 void debugresetnodes()
1584 debuglastnode = '0 0 0';
1587 void debugnode(entity this, vector node)
1589 if (!IS_PLAYER(this))
1592 if(debuglastnode=='0 0 0')
1594 debuglastnode = node;
1598 te_lightning2(NULL, node, debuglastnode);
1599 debuglastnode = node;
1602 void debugnodestatus(vector position, float status)
1608 case DEBUG_NODE_SUCCESS:
1611 case DEBUG_NODE_WARNING:
1614 case DEBUG_NODE_FAIL:
1621 te_customflash(position, 40, 2, c);
1624 // Support for debugging the goal stack visually
1627 .vector lastposition;
1629 // Debug the goal stack visually
1630 void debuggoalstack(entity this)
1635 if(this.goalcounter==0)goal=this.goalcurrent;
1636 else if(this.goalcounter==1)goal=this.goalstack01;
1637 else if(this.goalcounter==2)goal=this.goalstack02;
1638 else if(this.goalcounter==3)goal=this.goalstack03;
1639 else if(this.goalcounter==4)goal=this.goalstack04;
1640 else if(this.goalcounter==5)goal=this.goalstack05;
1641 else if(this.goalcounter==6)goal=this.goalstack06;
1642 else if(this.goalcounter==7)goal=this.goalstack07;
1643 else if(this.goalcounter==8)goal=this.goalstack08;
1644 else if(this.goalcounter==9)goal=this.goalstack09;
1645 else if(this.goalcounter==10)goal=this.goalstack10;
1646 else if(this.goalcounter==11)goal=this.goalstack11;
1647 else if(this.goalcounter==12)goal=this.goalstack12;
1648 else if(this.goalcounter==13)goal=this.goalstack13;
1649 else if(this.goalcounter==14)goal=this.goalstack14;
1650 else if(this.goalcounter==15)goal=this.goalstack15;
1651 else if(this.goalcounter==16)goal=this.goalstack16;
1652 else if(this.goalcounter==17)goal=this.goalstack17;
1653 else if(this.goalcounter==18)goal=this.goalstack18;
1654 else if(this.goalcounter==19)goal=this.goalstack19;
1655 else if(this.goalcounter==20)goal=this.goalstack20;
1656 else if(this.goalcounter==21)goal=this.goalstack21;
1657 else if(this.goalcounter==22)goal=this.goalstack22;
1658 else if(this.goalcounter==23)goal=this.goalstack23;
1659 else if(this.goalcounter==24)goal=this.goalstack24;
1660 else if(this.goalcounter==25)goal=this.goalstack25;
1661 else if(this.goalcounter==26)goal=this.goalstack26;
1662 else if(this.goalcounter==27)goal=this.goalstack27;
1663 else if(this.goalcounter==28)goal=this.goalstack28;
1664 else if(this.goalcounter==29)goal=this.goalstack29;
1665 else if(this.goalcounter==30)goal=this.goalstack30;
1666 else if(this.goalcounter==31)goal=this.goalstack31;
1671 this.goalcounter = 0;
1672 this.lastposition='0 0 0';
1676 if(this.lastposition=='0 0 0')
1679 org = this.lastposition;
1682 go = ( goal.absmin + goal.absmax ) * 0.5;
1683 te_lightning2(NULL, org, go);
1684 this.lastposition = go;