1 #include "navigation.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
8 #include "waypoints.qh"
10 #include <common/t_items.qh>
12 #include <common/items/_mod.qh>
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/triggers/trigger/jumppads.qh>
20 void navigation_goalrating_timeout_set(entity this)
22 if(IS_MOVABLE(this.goalentity))
23 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
25 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
28 // use this when current goal must be discarded immediately
29 void navigation_goalrating_timeout_force(entity this)
31 navigation_goalrating_timeout_expire(this, 0);
34 // use this when current goal can be kept for a short while to increase the chance
35 // of bot touching a waypoint, which helps to find a new goal more efficiently
36 void navigation_goalrating_timeout_expire(entity this, float seconds)
39 this.bot_strategytime = 0;
40 else if (this.bot_strategytime > time + seconds)
41 this.bot_strategytime = time + seconds;
44 bool navigation_goalrating_timeout(entity this)
46 return this.bot_strategytime < time;
49 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
51 if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
54 if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
56 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
57 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
59 this.ignoregoal = this.goalentity;
60 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
67 void navigation_dynamicgoal_init(entity this, bool initially_static)
69 this.navigation_dynamicgoal = true;
70 this.bot_basewaypoint = this.nearestwaypoint;
72 this.nearestwaypointtimeout = -1;
74 this.nearestwaypointtimeout = time;
77 void navigation_dynamicgoal_set(entity this)
79 this.nearestwaypointtimeout = time;
80 if (this.nearestwaypoint)
81 this.nearestwaypointtimeout += 2;
84 void navigation_dynamicgoal_unset(entity this)
86 if(this.bot_basewaypoint)
87 this.nearestwaypoint = this.bot_basewaypoint;
88 this.nearestwaypointtimeout = -1;
91 bool navigation_check_submerged_state(entity ent, vector pos)
95 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
96 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
97 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
100 submerged = SUBMERGED(pos);
101 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
102 // is actually submerged because often they are inside some solid.
103 // That's why submerged state is saved now that we know current pos is
104 // not stuck in solid (previous tracewalk call to this pos was successfully)
105 if(!ent.navigation_dynamicgoal)
106 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
111 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
113 IL_EACH(g_ladders, it.classname == "func_ladder",
116 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
117 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
120 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
121 tracebox(org, m1, m2, top, movemode, e);
122 if(trace_fraction == 1)
129 vector resurface_limited(vector org, float lim, vector m1)
131 if (WETFEET(org + eZ * (lim - org.z)))
135 float RES_min_h = org.z;
136 float RES_max_h = lim;
138 org.z = 0.5 * (RES_min_h + RES_max_h);
143 } while (RES_max_h - RES_min_h >= 1);
148 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
151 #define NAV_SWIM_ONWATER 1
152 #define NAV_SWIM_UNDERWATER 2
154 // rough simulation of walking from one point to another to test if a path
155 // can be traveled, used for waypoint linking and havocbot
156 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
157 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
159 if(autocvar_bot_debug_tracewalk)
166 vector flatdir = end - start;
168 float flatdist = vlen(flatdir);
169 flatdir = normalize(flatdir);
171 bool ignorehazards = false;
174 // Analyze starting point
175 traceline(start, start, MOVE_NORMAL, e);
176 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
177 ignorehazards = true;
179 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
180 if (trace_startsolid)
183 if(autocvar_bot_debug_tracewalk)
184 debugnodestatus(start, DEBUG_NODE_FAIL);
186 //print("tracewalk: ", vtos(start), " is a bad start\n");
192 end2.z += end_height;
194 vector fixed_end = end;
197 if (flatdist > 0 && WETFEET(org))
200 nav_action = NAV_SWIM_UNDERWATER;
203 // tracebox down by player's height
204 // useful to know if water level is so low that bot can still walk
205 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
206 if (SUBMERGED(trace_endpos))
209 nav_action = NAV_SWIM_UNDERWATER;
212 nav_action = NAV_WALK;
216 nav_action = NAV_WALK;
224 if (org.z > end2.z + 1)
226 tracebox(org, m1, m2, end2, movemode, e);
228 if (org.z > end2.z + 1)
231 else if (org.z < end.z - 1)
233 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
234 if (SUBMERGED(trace_endpos))
236 vector v = trace_endpos;
237 tracebox(v, m1, m2, end, movemode, e);
238 if(trace_endpos.z >= end.z - 1)
240 RESURFACE_LIMITED(v, trace_endpos.z);
244 else if (trace_endpos.z > org.z - jumpheight_vec.z)
245 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
247 if (org.z < end.z - 1)
254 if(autocvar_bot_debug_tracewalk)
257 debugnodestatus(org, DEBUG_NODE_SUCCESS);
260 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
265 if(autocvar_bot_debug_tracewalk)
271 if (stepdist > flatdist)
273 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
275 // can't use movement direction here to calculate move because of
276 // precision errors especially when direction has a high enough z value
277 //water_dir = normalize(water_end - org);
278 //move = org + water_dir * stepdist;
279 fixed_end.z = bound(end.z, org.z, end2.z);
280 if (stepdist == flatdist) {
284 move = org + (fixed_end - org) * (stepdist / flatdist);
285 flatdist = vlen(vec2(fixed_end - move));
288 else // horiz. direction
290 flatdist -= stepdist;
291 move = org + flatdir * stepdist;
294 if(nav_action == NAV_SWIM_ONWATER)
296 tracebox(org, m1, m2, move, movemode, e); // swim
299 if (trace_fraction < 1)
302 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
304 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
307 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
309 if(autocvar_bot_debug_tracewalk)
312 debugnodestatus(org, DEBUG_NODE_SUCCESS);
315 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
319 if(autocvar_bot_debug_tracewalk)
320 debugnodestatus(org, DEBUG_NODE_FAIL);
323 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
334 if (org.z <= move.z) // going horiz.
336 tracebox(trace_endpos, m1, m2, move, movemode, e);
338 nav_action = NAV_WALK;
343 if (org.z <= move.z) // going horiz.
346 nav_action = NAV_SWIM_ONWATER;
352 nav_action = NAV_SWIM_UNDERWATER;
354 nav_action = NAV_SWIM_ONWATER;
357 else if(nav_action == NAV_SWIM_UNDERWATER)
359 if (move.z >= org.z) // swimming upwards or horiz.
361 tracebox(org, m1, m2, move, movemode, e); // swim
363 bool stepswum = false;
366 if (trace_fraction < 1)
369 vector stepswim_move = move + stepheightvec;
370 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
371 stepswim_move.z = end2.z;
373 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
376 if (trace_startsolid)
378 if(autocvar_bot_debug_tracewalk)
379 debugnodestatus(org, DEBUG_NODE_FAIL);
381 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
385 if (trace_fraction < 1)
387 float org_z_prev = org.z;
388 RESURFACE_LIMITED(org, end2.z);
389 if(org.z == org_z_prev)
391 if(autocvar_bot_debug_tracewalk)
392 debugnodestatus(org, DEBUG_NODE_FAIL);
394 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
398 nav_action = NAV_SWIM_UNDERWATER;
400 nav_action = NAV_SWIM_ONWATER;
402 // we didn't advance horiz. in this step, flatdist decrease should be reverted
403 // but we can't do it properly right now... apply this workaround instead
421 if (!WETFEET(trace_endpos))
423 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
424 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
425 if (!stepswum && SUBMERGED(trace_endpos))
427 RESURFACE_LIMITED(trace_endpos, end2.z);
429 nav_action = NAV_SWIM_ONWATER;
435 nav_action = NAV_WALK;
441 nav_action = NAV_SWIM_UNDERWATER;
444 else //if (move.z < org.z) // swimming downwards
446 tracebox(org, m1, m2, move, movemode, e); // swim
449 if (trace_fraction < 1)
452 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
455 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
457 if(autocvar_bot_debug_tracewalk)
458 debugnodestatus(move, DEBUG_NODE_FAIL);
460 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
472 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
474 // stepswim caused upwards direction
475 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
476 if (!SUBMERGED(trace_endpos))
479 nav_action = NAV_WALK;
486 nav_action = NAV_SWIM_UNDERWATER;
490 else if(nav_action == NAV_WALK)
493 tracebox(org, m1, m2, move, movemode, e);
495 if(autocvar_bot_debug_tracewalk)
496 debugnode(e, trace_endpos);
499 if (trace_fraction < 1)
501 // check if we can walk over this obstacle, possibly by jumpstepping
502 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
503 if (trace_fraction < 1 || trace_startsolid)
505 if (trace_startsolid) // hit ceiling above org
507 // reduce stepwalk height
508 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
509 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
511 else //if (trace_fraction < 1)
513 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
514 if (trace_startsolid) // hit ceiling above org
516 // reduce jumpstepwalk height
517 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
518 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
522 if (trace_fraction < 1)
524 vector v = trace_endpos;
525 v.z = org.z + jumpheight_vec.z;
526 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
528 if(autocvar_bot_debug_tracewalk)
531 debugnodestatus(v, DEBUG_NODE_SUCCESS);
534 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
538 if(autocvar_bot_debug_tracewalk)
539 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
541 traceline( org, move, movemode, e);
543 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
547 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
549 nextmove = move + (flatdir * stepdist);
550 traceline( move, nextmove, movemode, e);
553 flatdist = vlen(vec2(end - move));
557 if(autocvar_bot_debug_tracewalk)
558 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
560 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
561 //te_explosion(trace_endpos);
562 //print(ftos(e.dphitcontentsmask), "\n");
563 return false; // failed
575 // trace down from stepheight as far as possible and move there,
576 // if this starts in solid we try again without the stepup, and
577 // if that also fails we assume it is a wall
578 // (this is the same logic as the Quake walkmove function used)
579 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
587 if(autocvar_bot_debug_tracewalk)
589 debugnode(e, trace_endpos);
590 debugnodestatus(org, DEBUG_NODE_FAIL);
593 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
600 if(move.z >= end2.z && org.z < end2.z)
605 if(org.z > move.z - 1 || !SUBMERGED(org))
607 nav_action = NAV_WALK;
611 // ended up submerged while walking
612 if(autocvar_bot_debug_tracewalk)
615 RESURFACE_LIMITED(org, move.z);
616 nav_action = NAV_SWIM_ONWATER;
621 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
623 // moved but didn't arrive at the intended destination
624 if(autocvar_bot_debug_tracewalk)
625 debugnodestatus(org, DEBUG_NODE_FAIL);
630 /////////////////////////////////////////////////////////////////////////////
632 /////////////////////////////////////////////////////////////////////////////
634 // completely empty the goal stack, used when deciding where to go
635 void navigation_clearroute(entity this)
637 this.goalcurrent_prev = this.goalcurrent;
638 this.goalcurrent_distance_2d = FLOAT_MAX;
639 this.goalcurrent_distance_z = FLOAT_MAX;
640 this.goalcurrent_distance_time = 0;
641 this.goalentity_lock_timeout = 0;
642 this.goalentity = NULL;
643 this.goalcurrent = NULL;
644 this.goalstack01 = NULL;
645 this.goalstack02 = NULL;
646 this.goalstack03 = NULL;
647 this.goalstack04 = NULL;
648 this.goalstack05 = NULL;
649 this.goalstack06 = NULL;
650 this.goalstack07 = NULL;
651 this.goalstack08 = NULL;
652 this.goalstack09 = NULL;
653 this.goalstack10 = NULL;
654 this.goalstack11 = NULL;
655 this.goalstack12 = NULL;
656 this.goalstack13 = NULL;
657 this.goalstack14 = NULL;
658 this.goalstack15 = NULL;
659 this.goalstack16 = NULL;
660 this.goalstack17 = NULL;
661 this.goalstack18 = NULL;
662 this.goalstack19 = NULL;
663 this.goalstack20 = NULL;
664 this.goalstack21 = NULL;
665 this.goalstack22 = NULL;
666 this.goalstack23 = NULL;
667 this.goalstack24 = NULL;
668 this.goalstack25 = NULL;
669 this.goalstack26 = NULL;
670 this.goalstack27 = NULL;
671 this.goalstack28 = NULL;
672 this.goalstack29 = NULL;
673 this.goalstack30 = NULL;
674 this.goalstack31 = NULL;
677 // add a new goal at the beginning of the stack
678 // (in other words: add a new prerequisite before going to the later goals)
679 // NOTE: when a waypoint is added, the WP gets pushed first, then the
680 // next-closest WP on the shortest path to the WP
681 // That means, if the stack overflows, the bot will know how to do the FIRST 32
682 // steps to the goal, and then recalculate the path.
683 void navigation_pushroute(entity this, entity e)
685 this.goalcurrent_prev = this.goalcurrent;
686 this.goalcurrent_distance_2d = FLOAT_MAX;
687 this.goalcurrent_distance_z = FLOAT_MAX;
688 this.goalcurrent_distance_time = 0;
689 //print("bot ", etos(this), " push ", etos(e), "\n");
690 if(this.goalstack31 == this.goalentity)
691 this.goalentity = NULL;
692 this.goalstack31 = this.goalstack30;
693 this.goalstack30 = this.goalstack29;
694 this.goalstack29 = this.goalstack28;
695 this.goalstack28 = this.goalstack27;
696 this.goalstack27 = this.goalstack26;
697 this.goalstack26 = this.goalstack25;
698 this.goalstack25 = this.goalstack24;
699 this.goalstack24 = this.goalstack23;
700 this.goalstack23 = this.goalstack22;
701 this.goalstack22 = this.goalstack21;
702 this.goalstack21 = this.goalstack20;
703 this.goalstack20 = this.goalstack19;
704 this.goalstack19 = this.goalstack18;
705 this.goalstack18 = this.goalstack17;
706 this.goalstack17 = this.goalstack16;
707 this.goalstack16 = this.goalstack15;
708 this.goalstack15 = this.goalstack14;
709 this.goalstack14 = this.goalstack13;
710 this.goalstack13 = this.goalstack12;
711 this.goalstack12 = this.goalstack11;
712 this.goalstack11 = this.goalstack10;
713 this.goalstack10 = this.goalstack09;
714 this.goalstack09 = this.goalstack08;
715 this.goalstack08 = this.goalstack07;
716 this.goalstack07 = this.goalstack06;
717 this.goalstack06 = this.goalstack05;
718 this.goalstack05 = this.goalstack04;
719 this.goalstack04 = this.goalstack03;
720 this.goalstack03 = this.goalstack02;
721 this.goalstack02 = this.goalstack01;
722 this.goalstack01 = this.goalcurrent;
723 this.goalcurrent = e;
726 // remove first goal from stack
727 // (in other words: remove a prerequisite for reaching the later goals)
728 // (used when a spawnfunc_waypoint is reached)
729 void navigation_poproute(entity this)
731 this.goalcurrent_prev = this.goalcurrent;
732 this.goalcurrent_distance_2d = FLOAT_MAX;
733 this.goalcurrent_distance_z = FLOAT_MAX;
734 this.goalcurrent_distance_time = 0;
735 //print("bot ", etos(this), " pop\n");
736 if(this.goalcurrent == this.goalentity)
738 this.goalentity = NULL;
739 this.goalentity_lock_timeout = 0;
741 this.goalcurrent = this.goalstack01;
742 this.goalstack01 = this.goalstack02;
743 this.goalstack02 = this.goalstack03;
744 this.goalstack03 = this.goalstack04;
745 this.goalstack04 = this.goalstack05;
746 this.goalstack05 = this.goalstack06;
747 this.goalstack06 = this.goalstack07;
748 this.goalstack07 = this.goalstack08;
749 this.goalstack08 = this.goalstack09;
750 this.goalstack09 = this.goalstack10;
751 this.goalstack10 = this.goalstack11;
752 this.goalstack11 = this.goalstack12;
753 this.goalstack12 = this.goalstack13;
754 this.goalstack13 = this.goalstack14;
755 this.goalstack14 = this.goalstack15;
756 this.goalstack15 = this.goalstack16;
757 this.goalstack16 = this.goalstack17;
758 this.goalstack17 = this.goalstack18;
759 this.goalstack18 = this.goalstack19;
760 this.goalstack19 = this.goalstack20;
761 this.goalstack20 = this.goalstack21;
762 this.goalstack21 = this.goalstack22;
763 this.goalstack22 = this.goalstack23;
764 this.goalstack23 = this.goalstack24;
765 this.goalstack24 = this.goalstack25;
766 this.goalstack25 = this.goalstack26;
767 this.goalstack26 = this.goalstack27;
768 this.goalstack27 = this.goalstack28;
769 this.goalstack28 = this.goalstack29;
770 this.goalstack29 = this.goalstack30;
771 this.goalstack30 = this.goalstack31;
772 this.goalstack31 = NULL;
775 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
776 // waypoint destination coordinates instead of v (only useful for box waypoints)
777 // for normal waypoints v2 == v and v2_height == 0
778 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)
780 if (vdist(v - org, <, bestdist))
782 traceline(v, org, true, ent);
783 if (trace_fraction == 1)
787 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
792 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
800 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
801 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
804 ent = ent.tag_entity;
806 vector pm1 = ent.origin + ent.mins;
807 vector pm2 = ent.origin + ent.maxs;
809 // do two scans, because box test is cheaper
810 IL_EACH(g_waypoints, it != ent && it != except,
812 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
814 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
816 waypoint_clearlinks(ent); // initialize wpXXmincost fields
817 navigation_item_addlink(it, ent);
823 vector org = ent.origin;
824 if (navigation_testtracewalk)
828 vector v = '0 0 0', v2 = '0 0 0';
831 if(ent.size && !IS_PLAYER(ent))
833 org += 0.5 * (ent.mins + ent.maxs);
834 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
837 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
839 waypoint_clearlinks(ent); // initialize wpXXmincost fields
840 IL_EACH(g_waypoints, it != ent,
842 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
845 SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
846 if(vdist(v2 - it.origin, <, 1050))
847 if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
848 navigation_item_addlink(it, ent);
852 // box check failed, try walk
853 IL_EACH(g_waypoints, it != ent,
855 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
859 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
861 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
862 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
864 bestdist = vlen(v - org);
868 if(!best && !ent.navigation_dynamicgoal)
870 int solid_save = ent.solid;
871 ent.solid = SOLID_BSP;
872 IL_EACH(g_jumppads, true,
874 if(trigger_push_test(it, ent))
876 best = it.nearestwaypoint;
880 ent.solid = solid_save;
884 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
886 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
887 if (autocvar_g_waypointeditor_auto)
889 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
891 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
896 // finds the waypoints near the bot initiating a navigation query
897 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
900 //navigation_testtracewalk = true;
903 IL_EACH(g_waypoints, !it.wpconsidered,
905 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
907 vector diff = v - this.origin;
908 diff.z = max(0, diff.z);
909 if(vdist(diff, <, maxdist))
911 it.wpconsidered = true;
912 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
914 it.wpnearestpoint = v;
915 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
922 //navigation_testtracewalk = false;
926 // updates a path link if a spawnfunc_waypoint link is better than the current one
927 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
933 m1 = wp.origin + wp.mins;
934 m2 = wp.origin + wp.maxs;
935 v.x = bound(m1_x, p.x, m2_x);
936 v.y = bound(m1_y, p.y, m2_y);
937 v.z = bound(m1_z, p.z, m2_z);
941 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
942 cost += w.wp00mincost; // assuming teleport has exactly one destination
944 cost += waypoint_gettravelcost(p, v, w, wp);
945 if (wp.wpcost > cost)
950 wp.wpnearestpoint = v;
954 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
955 void navigation_markroutes(entity this, entity fixed_source_waypoint)
960 IL_EACH(g_waypoints, true,
962 it.wpconsidered = false;
963 it.wpnearestpoint = '0 0 0';
964 it.wpcost = 10000000;
969 if(fixed_source_waypoint)
971 fixed_source_waypoint.wpconsidered = true;
972 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
973 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
974 fixed_source_waypoint.wpfire = 1;
975 fixed_source_waypoint.enemy = NULL;
979 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
980 // as this search is expensive we will use lower values if the bot is on the air
981 float increment, maxdistance;
982 if(IS_ONGROUND(this))
993 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
996 bool searching = true;
1000 IL_EACH(g_waypoints, it.wpfire,
1005 p = it.wpnearestpoint;
1007 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1024 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1025 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1026 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1027 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1028 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1029 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1030 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1031 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1032 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1033 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1034 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1035 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1036 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1037 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1038 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1039 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1044 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1045 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1049 IL_EACH(g_waypoints, true,
1051 it.wpconsidered = false;
1052 it.wpnearestpoint = '0 0 0';
1053 it.wpcost = 10000000;
1058 if(fixed_source_waypoint)
1060 fixed_source_waypoint.wpconsidered = true;
1061 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1062 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1063 fixed_source_waypoint.wpfire = 1;
1064 fixed_source_waypoint.enemy = NULL;
1068 error("need to start with a waypoint\n");
1071 bool searching = true;
1075 IL_EACH(g_waypoints, it.wpfire,
1079 cost = it.wpcost; // cost to walk from it to home
1080 p = it.wpnearestpoint;
1082 IL_EACH(g_waypoints, it != wp,
1084 if(!waypoint_islinked(it, wp))
1086 cost2 = cost + it.dmg;
1087 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1093 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1094 void navigation_routerating(entity this, entity e, float f, float rangebias)
1102 rangebias = waypoint_getlinearcost(rangebias);
1103 f = waypoint_getlinearcost(f);
1107 bool rate_wps = false;
1108 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1113 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1114 int t = pointcontents(trace_endpos + '0 0 1');
1115 if(t != CONTENT_SOLID )
1117 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1119 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1126 entity theEnemy = e;
1127 entity best_wp = NULL;
1128 float best_dist = 10000;
1129 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1130 && vdist(it.origin - this.origin, >, 100)
1131 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1133 float dist = vlen(it.origin - theEnemy.origin);
1134 if (dist < best_dist)
1146 vector goal_org = (e.absmin + e.absmax) * 0.5;
1148 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1150 // Evaluate path using jetpack
1152 if(this.items & IT_JETPACK)
1153 if(autocvar_bot_ai_navigation_jetpack)
1154 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1156 vector pointa, pointb;
1158 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1161 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1162 pointa = trace_endpos - '0 0 1';
1165 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1166 pointb = trace_endpos - '0 0 1';
1168 // Can I see these two points from the sky?
1169 traceline(pointa, pointb, MOVE_NORMAL, this);
1171 if(trace_fraction==1)
1173 LOG_DEBUG("jetpack ai: can bridge these two points");
1175 // Lower the altitude of these points as much as possible
1176 float zdistance, xydistance, cost, t, fuel;
1177 vector down, npa, npb;
1179 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1182 npa = pointa + down;
1183 npb = pointb + down;
1185 if(npa.z<=this.absmax.z)
1188 if(npb.z<=e.absmax.z)
1191 traceline(npa, npb, MOVE_NORMAL, this);
1192 if(trace_fraction==1)
1198 while(trace_fraction == 1);
1201 // Rough estimation of fuel consumption
1202 // (ignores acceleration and current xyz velocity)
1203 xydistance = vlen(pointa - pointb);
1204 zdistance = fabs(pointa.z - this.origin.z);
1206 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1207 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1208 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1210 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1213 if(this.ammo_fuel>fuel)
1216 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1217 // - between air and ground speeds)
1219 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1220 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1223 // Compare against other goals
1224 f = f * rangebias / (rangebias + cost);
1226 if (navigation_bestrating < f)
1228 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1229 navigation_bestrating = f;
1230 navigation_bestgoal = e;
1231 this.navigation_jetpack_goal = e;
1232 this.navigation_jetpack_point = pointb;
1240 //te_wizspike(e.origin);
1243 // update the cached spawnfunc_waypoint link on a dynamic item entity
1244 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1250 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1251 e.nearestwaypoint = NULL;
1253 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1254 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1256 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1257 e.nearestwaypoint = nwp = e.goalcurrent;
1259 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1262 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1264 if(!e.navigation_dynamicgoal)
1265 e.blacklisted = true;
1269 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1274 if(e.navigation_dynamicgoal)
1275 e.nearestwaypointtimeout = time + 2;
1276 else if(autocvar_g_waypointeditor)
1277 e.nearestwaypointtimeout = time + 3 + random() * 2;
1279 nwp = e.nearestwaypoint;
1282 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1283 if (nwp && nwp.wpcost < 10000000)
1285 //te_wizspike(nwp.wpnearestpoint);
1286 float nwptoitem_cost = 0;
1287 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1288 nwptoitem_cost = nwp.wp00mincost;
1290 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1291 float cost = nwp.wpcost + nwptoitem_cost;
1292 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1293 f = f * rangebias / (rangebias + cost);
1294 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1295 if (navigation_bestrating < f)
1297 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1298 navigation_bestrating = f;
1299 navigation_bestgoal = e;
1304 // adds an item to the the goal stack with the path to a given item
1305 bool navigation_routetogoal(entity this, entity e, vector startposition)
1307 // if there is no goal, just exit
1311 entity teleport_goal = NULL;
1313 this.goalentity = e;
1315 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1317 // force teleport destination as route destination
1319 navigation_pushroute(this, e.wp00);
1320 this.goalentity = e.wp00;
1323 // put the entity on the goal stack
1324 //print("routetogoal ", etos(e), "\n");
1325 navigation_pushroute(this, e);
1328 e = this.goalentity;
1330 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1332 this.wp_goal_prev1 = this.wp_goal_prev0;
1333 this.wp_goal_prev0 = e;
1337 if(e==this.navigation_jetpack_goal)
1340 // if it can reach the goal there is nothing more to do
1341 vector dest = '0 0 0';
1342 float dest_height = 0;
1343 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1344 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1347 entity nearest_wp = NULL;
1348 // see if there are waypoints describing a path to the item
1349 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1351 e = e.nearestwaypoint;
1354 else if(teleport_goal)
1357 e = e.enemy; // we already have added it, so...
1362 if(nearest_wp && nearest_wp.enemy)
1364 // often path can be optimized by not adding the nearest waypoint
1365 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1367 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1368 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1369 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1370 e = nearest_wp.enemy;
1372 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1373 e = nearest_wp.enemy;
1378 // add the spawnfunc_waypoint to the path
1379 navigation_pushroute(this, e);
1389 // removes any currently touching waypoints from the goal stack
1390 // (this is how bots detect if they reached a goal)
1391 int navigation_poptouchedgoals(entity this)
1393 int removed_goals = 0;
1395 if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1397 navigation_poproute(this);
1401 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1403 // make sure jumppad is really hit, don't rely on distance based checks
1404 // as they may report a touch even if it didn't really happen
1405 if(this.lastteleporttime > 0
1406 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1408 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1409 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1411 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1412 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1414 navigation_poproute(this);
1415 this.lastteleporttime = 0;
1419 return removed_goals;
1422 // If for some reason the bot is closer to the next goal, pop the current one
1423 if(this.goalstack01 && !wasfreed(this.goalstack01))
1424 if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1425 if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1426 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1428 // this can happen when a player stands still or is frozen
1429 // NOTE: this is only needed because tracewalk always fail when destination
1430 // point is on a player and bot_navigation_ignoreplayers is false
1431 if (this.goalentity.navigation_dynamicgoal
1432 && boxesoverlap(this.absmin, this.absmax, this.goalentity.absmin, this.goalentity.absmax))
1434 navigation_poproute(this);
1436 return removed_goals;
1439 vector dest = '0 0 0';
1440 float dest_height = 0;
1441 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1442 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1444 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1445 navigation_poproute(this);
1447 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1448 return removed_goals;
1449 // TODO this may also be a nice idea to do "early" (e.g. by
1450 // manipulating the vlen() comparisons) to shorten paths in
1451 // general - this would make bots walk more "on rails" than
1452 // "zigzagging" which they currently do with sufficiently
1453 // random-like waypoints, and thus can make a nice bot
1454 // personality property
1458 // Loose goal touching check when running
1459 if(this.aistatus & AI_STATUS_RUNNING)
1460 if(this.goalcurrent.classname=="waypoint")
1461 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1463 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1465 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1466 if(trace_fraction==1)
1468 // Detect personal waypoints
1469 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1470 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1472 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1473 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1476 navigation_poproute(this);
1478 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1479 return removed_goals;
1484 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1486 vector gc_min = this.goalcurrent.absmin;
1487 vector gc_max = this.goalcurrent.absmax;
1488 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1490 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1491 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1493 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1496 // Detect personal waypoints
1497 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1498 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1500 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1501 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1504 navigation_poproute(this);
1506 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1507 return removed_goals;
1509 return removed_goals;
1512 entity navigation_get_really_close_waypoint(entity this)
1514 entity wp = this.goalcurrent;
1515 if(!wp || vdist(wp.origin - this.origin, >, 50))
1516 wp = this.goalcurrent_prev;
1519 if(wp.classname != "waypoint")
1521 wp = wp.nearestwaypoint;
1525 if(vdist(wp.origin - this.origin, >, 50))
1527 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1529 if(vdist(it.origin - this.origin, <, 50))
1536 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1539 vector dest = '0 0 0';
1540 float dest_height = 0;
1541 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1542 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1547 // begin a goal selection session (queries spawnfunc_waypoint network)
1548 void navigation_goalrating_start(entity this)
1550 if(this.aistatus & AI_STATUS_STUCK)
1553 this.navigation_jetpack_goal = NULL;
1554 navigation_bestrating = -1;
1555 entity wp = navigation_get_really_close_waypoint(this);
1556 navigation_clearroute(this);
1557 navigation_bestgoal = NULL;
1558 navigation_markroutes(this, wp);
1561 // ends a goal selection session (updates goal stack to the best goal)
1562 void navigation_goalrating_end(entity this)
1564 if(this.aistatus & AI_STATUS_STUCK)
1567 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1568 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1570 // If the bot got stuck then try to reach the farthest waypoint
1571 if (!this.goalentity && autocvar_bot_wander_enable)
1573 if (!(this.aistatus & AI_STATUS_STUCK))
1575 LOG_DEBUG(this.netname, " cannot walk to any goal");
1576 this.aistatus |= AI_STATUS_STUCK;
1581 void botframe_updatedangerousobjects(float maxupdate)
1583 vector m1, m2, v, o;
1587 IL_EACH(g_waypoints, true,
1593 IL_EACH(g_bot_dodge, it.bot_dodge,
1596 v.x = bound(m1_x, v.x, m2_x);
1597 v.y = bound(m1_y, v.y, m2_y);
1598 v.z = bound(m1_z, v.z, m2_z);
1599 o = (it.absmin + it.absmax) * 0.5;
1600 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1603 traceline(o, v, true, NULL);
1604 if (trace_fraction == 1)
1605 danger = danger + d;
1615 void navigation_unstuck(entity this)
1617 float search_radius = 1000;
1619 if (!autocvar_bot_wander_enable)
1622 if (!bot_waypoint_queue_owner)
1624 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1625 bot_waypoint_queue_owner = this;
1626 bot_waypoint_queue_bestgoal = NULL;
1627 bot_waypoint_queue_bestgoalrating = 0;
1630 if(bot_waypoint_queue_owner!=this)
1633 if (bot_waypoint_queue_goal)
1635 // evaluate the next goal on the queue
1636 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1637 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1638 vector dest = '0 0 0';
1639 float dest_height = 0;
1640 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1641 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1643 if( d > bot_waypoint_queue_bestgoalrating)
1645 bot_waypoint_queue_bestgoalrating = d;
1646 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1649 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1651 if (!bot_waypoint_queue_goal)
1653 if (bot_waypoint_queue_bestgoal)
1655 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1656 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1657 navigation_goalrating_timeout_set(this);
1658 this.aistatus &= ~AI_STATUS_STUCK;
1662 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1665 bot_waypoint_queue_owner = NULL;
1670 if(bot_strategytoken!=this)
1673 // build a new queue
1674 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1676 entity first = NULL;
1678 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1680 if(bot_waypoint_queue_goal)
1681 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1685 bot_waypoint_queue_goal = it;
1686 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1690 bot_waypoint_queue_goal = first;
1693 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1694 bot_waypoint_queue_owner = NULL;
1699 // Support for debugging tracewalk visually
1701 void debugresetnodes()
1703 debuglastnode = '0 0 0';
1706 void debugnode(entity this, vector node)
1708 if (!IS_PLAYER(this))
1711 if(debuglastnode=='0 0 0')
1713 debuglastnode = node;
1717 te_lightning2(NULL, node, debuglastnode);
1718 debuglastnode = node;
1721 void debugnodestatus(vector position, float status)
1727 case DEBUG_NODE_SUCCESS:
1730 case DEBUG_NODE_WARNING:
1733 case DEBUG_NODE_FAIL:
1740 te_customflash(position, 40, 2, c);
1743 // Support for debugging the goal stack visually
1746 .vector lastposition;
1748 // Debug the goal stack visually
1749 void debuggoalstack(entity this)
1754 if(this.goalcounter==0)goal=this.goalcurrent;
1755 else if(this.goalcounter==1)goal=this.goalstack01;
1756 else if(this.goalcounter==2)goal=this.goalstack02;
1757 else if(this.goalcounter==3)goal=this.goalstack03;
1758 else if(this.goalcounter==4)goal=this.goalstack04;
1759 else if(this.goalcounter==5)goal=this.goalstack05;
1760 else if(this.goalcounter==6)goal=this.goalstack06;
1761 else if(this.goalcounter==7)goal=this.goalstack07;
1762 else if(this.goalcounter==8)goal=this.goalstack08;
1763 else if(this.goalcounter==9)goal=this.goalstack09;
1764 else if(this.goalcounter==10)goal=this.goalstack10;
1765 else if(this.goalcounter==11)goal=this.goalstack11;
1766 else if(this.goalcounter==12)goal=this.goalstack12;
1767 else if(this.goalcounter==13)goal=this.goalstack13;
1768 else if(this.goalcounter==14)goal=this.goalstack14;
1769 else if(this.goalcounter==15)goal=this.goalstack15;
1770 else if(this.goalcounter==16)goal=this.goalstack16;
1771 else if(this.goalcounter==17)goal=this.goalstack17;
1772 else if(this.goalcounter==18)goal=this.goalstack18;
1773 else if(this.goalcounter==19)goal=this.goalstack19;
1774 else if(this.goalcounter==20)goal=this.goalstack20;
1775 else if(this.goalcounter==21)goal=this.goalstack21;
1776 else if(this.goalcounter==22)goal=this.goalstack22;
1777 else if(this.goalcounter==23)goal=this.goalstack23;
1778 else if(this.goalcounter==24)goal=this.goalstack24;
1779 else if(this.goalcounter==25)goal=this.goalstack25;
1780 else if(this.goalcounter==26)goal=this.goalstack26;
1781 else if(this.goalcounter==27)goal=this.goalstack27;
1782 else if(this.goalcounter==28)goal=this.goalstack28;
1783 else if(this.goalcounter==29)goal=this.goalstack29;
1784 else if(this.goalcounter==30)goal=this.goalstack30;
1785 else if(this.goalcounter==31)goal=this.goalstack31;
1790 this.goalcounter = 0;
1791 this.lastposition='0 0 0';
1795 if(this.lastposition=='0 0 0')
1798 org = this.lastposition;
1801 go = ( goal.absmin + goal.absmax ) * 0.5;
1802 te_lightning2(NULL, org, go);
1803 this.lastposition = go;