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;
82 void navigation_dynamicgoal_unset(entity this)
84 if(this.bot_basewaypoint)
85 this.nearestwaypoint = this.bot_basewaypoint;
86 this.nearestwaypointtimeout = -1;
89 bool navigation_check_submerged_state(entity ent, vector pos)
93 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
94 else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
95 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
98 submerged = SUBMERGED(pos);
99 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
100 // is actually submerged because often they are inside some solid.
101 // That's why submerged state is saved now that we know current pos is
102 // not stuck in solid (previous tracewalk call to this pos was successfully)
103 if(!ent.navigation_dynamicgoal)
104 ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
109 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
111 IL_EACH(g_ladders, it.classname == "func_ladder",
114 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
115 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
118 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
119 tracebox(org, m1, m2, top, movemode, e);
120 if(trace_fraction == 1)
127 vector resurface_limited(vector org, float lim, vector m1)
129 if (WETFEET(org + eZ * (lim - org.z)))
133 float RES_min_h = org.z;
134 float RES_max_h = lim;
136 org.z = 0.5 * (RES_min_h + RES_max_h);
141 } while (RES_max_h - RES_min_h >= 1);
146 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
149 #define NAV_SWIM_ONWATER 1
150 #define NAV_SWIM_UNDERWATER 2
152 // rough simulation of walking from one point to another to test if a path
153 // can be traveled, used for waypoint linking and havocbot
154 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
155 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
157 if(autocvar_bot_debug_tracewalk)
164 vector flatdir = end - start;
166 float flatdist = vlen(flatdir);
167 flatdir = normalize(flatdir);
169 bool ignorehazards = false;
172 // Analyze starting point
173 traceline(start, start, MOVE_NORMAL, e);
174 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
175 ignorehazards = true;
177 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
178 if (trace_startsolid)
181 if(autocvar_bot_debug_tracewalk)
182 debugnodestatus(start, DEBUG_NODE_FAIL);
184 //print("tracewalk: ", vtos(start), " is a bad start\n");
190 end2.z += end_height;
192 vector fixed_end = end;
195 if (flatdist > 0 && WETFEET(org))
198 nav_action = NAV_SWIM_UNDERWATER;
201 // tracebox down by player's height
202 // useful to know if water level is so low that bot can still walk
203 tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
204 if (SUBMERGED(trace_endpos))
207 nav_action = NAV_SWIM_UNDERWATER;
210 nav_action = NAV_WALK;
214 nav_action = NAV_WALK;
222 if (org.z > end2.z + 1)
224 tracebox(org, m1, m2, end2, movemode, e);
226 if (org.z > end2.z + 1)
229 else if (org.z < end.z - 1)
231 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
232 if (SUBMERGED(trace_endpos))
234 vector v = trace_endpos;
235 tracebox(v, m1, m2, end, movemode, e);
236 if(trace_endpos.z >= end.z - 1)
238 RESURFACE_LIMITED(v, trace_endpos.z);
242 else if (trace_endpos.z > org.z - jumpheight_vec.z)
243 tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
245 if (org.z < end.z - 1)
252 if(autocvar_bot_debug_tracewalk)
255 debugnodestatus(org, DEBUG_NODE_SUCCESS);
258 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
263 if(autocvar_bot_debug_tracewalk)
269 if (stepdist > flatdist)
271 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
273 // can't use movement direction here to calculate move because of
274 // precision errors especially when direction has a high enough z value
275 //water_dir = normalize(water_end - org);
276 //move = org + water_dir * stepdist;
277 fixed_end.z = bound(end.z, org.z, end2.z);
278 if (stepdist == flatdist) {
282 move = org + (fixed_end - org) * (stepdist / flatdist);
283 flatdist = vlen(vec2(fixed_end - move));
286 else // horiz. direction
288 flatdist -= stepdist;
289 move = org + flatdir * stepdist;
292 if(nav_action == NAV_SWIM_ONWATER)
294 tracebox(org, m1, m2, move, movemode, e); // swim
297 if (trace_fraction < 1)
300 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
302 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
305 if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
307 if(autocvar_bot_debug_tracewalk)
310 debugnodestatus(org, DEBUG_NODE_SUCCESS);
313 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
317 if(autocvar_bot_debug_tracewalk)
318 debugnodestatus(org, DEBUG_NODE_FAIL);
321 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
332 if (org.z <= move.z) // going horiz.
334 tracebox(trace_endpos, m1, m2, move, movemode, e);
336 nav_action = NAV_WALK;
341 if (org.z <= move.z) // going horiz.
344 nav_action = NAV_SWIM_ONWATER;
350 nav_action = NAV_SWIM_UNDERWATER;
352 nav_action = NAV_SWIM_ONWATER;
355 else if(nav_action == NAV_SWIM_UNDERWATER)
357 if (move.z >= org.z) // swimming upwards or horiz.
359 tracebox(org, m1, m2, move, movemode, e); // swim
361 bool stepswum = false;
364 if (trace_fraction < 1)
367 vector stepswim_move = move + stepheightvec;
368 if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
369 stepswim_move.z = end2.z;
371 tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
374 if (trace_startsolid)
376 if(autocvar_bot_debug_tracewalk)
377 debugnodestatus(org, DEBUG_NODE_FAIL);
379 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
383 if (trace_fraction < 1)
385 float org_z_prev = org.z;
386 RESURFACE_LIMITED(org, end2.z);
387 if(org.z == org_z_prev)
389 if(autocvar_bot_debug_tracewalk)
390 debugnodestatus(org, DEBUG_NODE_FAIL);
392 //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
396 nav_action = NAV_SWIM_UNDERWATER;
398 nav_action = NAV_SWIM_ONWATER;
400 // we didn't advance horiz. in this step, flatdist decrease should be reverted
401 // but we can't do it properly right now... apply this workaround instead
419 if (!WETFEET(trace_endpos))
421 tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
422 // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
423 if (!stepswum && SUBMERGED(trace_endpos))
425 RESURFACE_LIMITED(trace_endpos, end2.z);
427 nav_action = NAV_SWIM_ONWATER;
433 nav_action = NAV_WALK;
439 nav_action = NAV_SWIM_UNDERWATER;
442 else //if (move.z < org.z) // swimming downwards
444 tracebox(org, m1, m2, move, movemode, e); // swim
447 if (trace_fraction < 1)
450 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
453 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
455 if(autocvar_bot_debug_tracewalk)
456 debugnodestatus(move, DEBUG_NODE_FAIL);
458 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
470 if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
472 // stepswim caused upwards direction
473 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
474 if (!SUBMERGED(trace_endpos))
477 nav_action = NAV_WALK;
484 nav_action = NAV_SWIM_UNDERWATER;
488 else if(nav_action == NAV_WALK)
491 tracebox(org, m1, m2, move, movemode, e);
493 if(autocvar_bot_debug_tracewalk)
494 debugnode(e, trace_endpos);
497 if (trace_fraction < 1)
499 // check if we can walk over this obstacle, possibly by jumpstepping
500 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
501 if (trace_fraction < 1 || trace_startsolid)
503 if (trace_startsolid) // hit ceiling above org
505 // reduce stepwalk height
506 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
507 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
509 else //if (trace_fraction < 1)
511 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
512 if (trace_startsolid) // hit ceiling above org
514 // reduce jumpstepwalk height
515 tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
516 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
520 if (trace_fraction < 1)
522 vector v = trace_endpos;
523 v.z = org.z + jumpheight_vec.z;
524 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
526 if(autocvar_bot_debug_tracewalk)
529 debugnodestatus(v, DEBUG_NODE_SUCCESS);
532 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
536 if(autocvar_bot_debug_tracewalk)
537 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
539 traceline( org, move, movemode, e);
541 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
545 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
547 nextmove = move + (flatdir * stepdist);
548 traceline( move, nextmove, movemode, e);
551 flatdist = vlen(vec2(end - move));
555 if(autocvar_bot_debug_tracewalk)
556 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
558 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
559 //te_explosion(trace_endpos);
560 //print(ftos(e.dphitcontentsmask), "\n");
561 return false; // failed
573 // trace down from stepheight as far as possible and move there,
574 // if this starts in solid we try again without the stepup, and
575 // if that also fails we assume it is a wall
576 // (this is the same logic as the Quake walkmove function used)
577 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
585 if(autocvar_bot_debug_tracewalk)
587 debugnode(e, trace_endpos);
588 debugnodestatus(org, DEBUG_NODE_FAIL);
591 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
598 if(move.z >= end2.z && org.z < end2.z)
603 if(org.z > move.z - 1 || !SUBMERGED(org))
605 nav_action = NAV_WALK;
609 // ended up submerged while walking
610 if(autocvar_bot_debug_tracewalk)
613 RESURFACE_LIMITED(org, move.z);
614 nav_action = NAV_SWIM_ONWATER;
619 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
621 // moved but didn't arrive at the intended destination
622 if(autocvar_bot_debug_tracewalk)
623 debugnodestatus(org, DEBUG_NODE_FAIL);
628 /////////////////////////////////////////////////////////////////////////////
630 /////////////////////////////////////////////////////////////////////////////
632 // completely empty the goal stack, used when deciding where to go
633 void navigation_clearroute(entity this)
635 this.goalcurrent_prev = this.goalcurrent;
636 this.goalcurrent_distance_2d = FLOAT_MAX;
637 this.goalcurrent_distance_z = FLOAT_MAX;
638 this.goalcurrent_distance_time = 0;
639 this.goalentity_lock_timeout = 0;
640 this.goalentity = NULL;
641 this.goalcurrent = NULL;
642 this.goalstack01 = NULL;
643 this.goalstack02 = NULL;
644 this.goalstack03 = NULL;
645 this.goalstack04 = NULL;
646 this.goalstack05 = NULL;
647 this.goalstack06 = NULL;
648 this.goalstack07 = NULL;
649 this.goalstack08 = NULL;
650 this.goalstack09 = NULL;
651 this.goalstack10 = NULL;
652 this.goalstack11 = NULL;
653 this.goalstack12 = NULL;
654 this.goalstack13 = NULL;
655 this.goalstack14 = NULL;
656 this.goalstack15 = NULL;
657 this.goalstack16 = NULL;
658 this.goalstack17 = NULL;
659 this.goalstack18 = NULL;
660 this.goalstack19 = NULL;
661 this.goalstack20 = NULL;
662 this.goalstack21 = NULL;
663 this.goalstack22 = NULL;
664 this.goalstack23 = NULL;
665 this.goalstack24 = NULL;
666 this.goalstack25 = NULL;
667 this.goalstack26 = NULL;
668 this.goalstack27 = NULL;
669 this.goalstack28 = NULL;
670 this.goalstack29 = NULL;
671 this.goalstack30 = NULL;
672 this.goalstack31 = NULL;
675 // add a new goal at the beginning of the stack
676 // (in other words: add a new prerequisite before going to the later goals)
677 // NOTE: when a waypoint is added, the WP gets pushed first, then the
678 // next-closest WP on the shortest path to the WP
679 // That means, if the stack overflows, the bot will know how to do the FIRST 32
680 // steps to the goal, and then recalculate the path.
681 void navigation_pushroute(entity this, entity e)
683 this.goalcurrent_prev = this.goalcurrent;
684 this.goalcurrent_distance_2d = FLOAT_MAX;
685 this.goalcurrent_distance_z = FLOAT_MAX;
686 this.goalcurrent_distance_time = 0;
687 //print("bot ", etos(this), " push ", etos(e), "\n");
688 if(this.goalstack31 == this.goalentity)
689 this.goalentity = NULL;
690 this.goalstack31 = this.goalstack30;
691 this.goalstack30 = this.goalstack29;
692 this.goalstack29 = this.goalstack28;
693 this.goalstack28 = this.goalstack27;
694 this.goalstack27 = this.goalstack26;
695 this.goalstack26 = this.goalstack25;
696 this.goalstack25 = this.goalstack24;
697 this.goalstack24 = this.goalstack23;
698 this.goalstack23 = this.goalstack22;
699 this.goalstack22 = this.goalstack21;
700 this.goalstack21 = this.goalstack20;
701 this.goalstack20 = this.goalstack19;
702 this.goalstack19 = this.goalstack18;
703 this.goalstack18 = this.goalstack17;
704 this.goalstack17 = this.goalstack16;
705 this.goalstack16 = this.goalstack15;
706 this.goalstack15 = this.goalstack14;
707 this.goalstack14 = this.goalstack13;
708 this.goalstack13 = this.goalstack12;
709 this.goalstack12 = this.goalstack11;
710 this.goalstack11 = this.goalstack10;
711 this.goalstack10 = this.goalstack09;
712 this.goalstack09 = this.goalstack08;
713 this.goalstack08 = this.goalstack07;
714 this.goalstack07 = this.goalstack06;
715 this.goalstack06 = this.goalstack05;
716 this.goalstack05 = this.goalstack04;
717 this.goalstack04 = this.goalstack03;
718 this.goalstack03 = this.goalstack02;
719 this.goalstack02 = this.goalstack01;
720 this.goalstack01 = this.goalcurrent;
721 this.goalcurrent = e;
724 // remove first goal from stack
725 // (in other words: remove a prerequisite for reaching the later goals)
726 // (used when a spawnfunc_waypoint is reached)
727 void navigation_poproute(entity this)
729 this.goalcurrent_prev = this.goalcurrent;
730 this.goalcurrent_distance_2d = FLOAT_MAX;
731 this.goalcurrent_distance_z = FLOAT_MAX;
732 this.goalcurrent_distance_time = 0;
733 //print("bot ", etos(this), " pop\n");
734 if(this.goalcurrent == this.goalentity)
736 this.goalentity = NULL;
737 this.goalentity_lock_timeout = 0;
739 this.goalcurrent = this.goalstack01;
740 this.goalstack01 = this.goalstack02;
741 this.goalstack02 = this.goalstack03;
742 this.goalstack03 = this.goalstack04;
743 this.goalstack04 = this.goalstack05;
744 this.goalstack05 = this.goalstack06;
745 this.goalstack06 = this.goalstack07;
746 this.goalstack07 = this.goalstack08;
747 this.goalstack08 = this.goalstack09;
748 this.goalstack09 = this.goalstack10;
749 this.goalstack10 = this.goalstack11;
750 this.goalstack11 = this.goalstack12;
751 this.goalstack12 = this.goalstack13;
752 this.goalstack13 = this.goalstack14;
753 this.goalstack14 = this.goalstack15;
754 this.goalstack15 = this.goalstack16;
755 this.goalstack16 = this.goalstack17;
756 this.goalstack17 = this.goalstack18;
757 this.goalstack18 = this.goalstack19;
758 this.goalstack19 = this.goalstack20;
759 this.goalstack20 = this.goalstack21;
760 this.goalstack21 = this.goalstack22;
761 this.goalstack22 = this.goalstack23;
762 this.goalstack23 = this.goalstack24;
763 this.goalstack24 = this.goalstack25;
764 this.goalstack25 = this.goalstack26;
765 this.goalstack26 = this.goalstack27;
766 this.goalstack27 = this.goalstack28;
767 this.goalstack28 = this.goalstack29;
768 this.goalstack29 = this.goalstack30;
769 this.goalstack30 = this.goalstack31;
770 this.goalstack31 = NULL;
773 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
774 // waypoint destination coordinates instead of v (only useful for box waypoints)
775 // for normal waypoints v2 == v and v2_height == 0
776 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)
778 if (vdist(v - org, <, bestdist))
780 traceline(v, org, true, ent);
781 if (trace_fraction == 1)
785 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
790 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
798 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
799 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
802 ent = ent.tag_entity;
804 vector pm1 = ent.origin + ent.mins;
805 vector pm2 = ent.origin + ent.maxs;
807 // do two scans, because box test is cheaper
808 IL_EACH(g_waypoints, it != ent && it != except,
810 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
812 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
814 waypoint_clearlinks(ent); // initialize wpXXmincost fields
815 navigation_item_addlink(it, ent);
821 vector org = ent.origin;
822 if (navigation_testtracewalk)
826 vector v = '0 0 0', v2 = '0 0 0';
829 if(ent.size && !IS_PLAYER(ent))
831 org += 0.5 * (ent.mins + ent.maxs);
832 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
835 if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
837 waypoint_clearlinks(ent); // initialize wpXXmincost fields
838 IL_EACH(g_waypoints, it != ent,
840 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
843 SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
844 if(vdist(v2 - it.origin, <, 1050))
845 if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
846 navigation_item_addlink(it, ent);
850 // box check failed, try walk
851 IL_EACH(g_waypoints, it != ent,
853 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
857 SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
859 SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
860 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
862 bestdist = vlen(v - org);
866 if(!best && !ent.navigation_dynamicgoal)
868 int solid_save = ent.solid;
869 ent.solid = SOLID_BSP;
870 IL_EACH(g_jumppads, true,
872 if(trigger_push_test(it, ent))
874 best = it.nearestwaypoint;
878 ent.solid = solid_save;
882 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
884 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
885 if (autocvar_g_waypointeditor_auto)
887 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
889 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
894 // finds the waypoints near the bot initiating a navigation query
895 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
898 //navigation_testtracewalk = true;
901 IL_EACH(g_waypoints, !it.wpconsidered,
903 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
905 vector diff = v - this.origin;
906 diff.z = max(0, diff.z);
907 if(vdist(diff, <, maxdist))
909 it.wpconsidered = true;
910 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
912 it.wpnearestpoint = v;
913 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
920 //navigation_testtracewalk = false;
924 // updates a path link if a spawnfunc_waypoint link is better than the current one
925 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
931 m1 = wp.origin + wp.mins;
932 m2 = wp.origin + wp.maxs;
933 v.x = bound(m1_x, p.x, m2_x);
934 v.y = bound(m1_y, p.y, m2_y);
935 v.z = bound(m1_z, p.z, m2_z);
939 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
940 cost += w.wp00mincost; // assuming teleport has exactly one destination
942 cost += waypoint_gettravelcost(p, v, w, wp);
943 if (wp.wpcost > cost)
948 wp.wpnearestpoint = v;
952 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
953 void navigation_markroutes(entity this, entity fixed_source_waypoint)
958 IL_EACH(g_waypoints, true,
960 it.wpconsidered = false;
961 it.wpnearestpoint = '0 0 0';
962 it.wpcost = 10000000;
967 if(fixed_source_waypoint)
969 fixed_source_waypoint.wpconsidered = true;
970 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
971 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
972 fixed_source_waypoint.wpfire = 1;
973 fixed_source_waypoint.enemy = NULL;
977 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
978 // as this search is expensive we will use lower values if the bot is on the air
979 float increment, maxdistance;
980 if(IS_ONGROUND(this))
991 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
994 bool searching = true;
998 IL_EACH(g_waypoints, it.wpfire,
1003 p = it.wpnearestpoint;
1005 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1024 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1025 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1026 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1027 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1028 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1029 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1030 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1031 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1032 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1033 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1034 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1035 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1036 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1037 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1042 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1043 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1047 IL_EACH(g_waypoints, true,
1049 it.wpconsidered = false;
1050 it.wpnearestpoint = '0 0 0';
1051 it.wpcost = 10000000;
1056 if(fixed_source_waypoint)
1058 fixed_source_waypoint.wpconsidered = true;
1059 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1060 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1061 fixed_source_waypoint.wpfire = 1;
1062 fixed_source_waypoint.enemy = NULL;
1066 error("need to start with a waypoint\n");
1069 bool searching = true;
1073 IL_EACH(g_waypoints, it.wpfire,
1077 cost = it.wpcost; // cost to walk from it to home
1078 p = it.wpnearestpoint;
1080 IL_EACH(g_waypoints, it != wp,
1082 if(!waypoint_islinked(it, wp))
1084 cost2 = cost + it.dmg;
1085 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1091 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1092 void navigation_routerating(entity this, entity e, float f, float rangebias)
1100 rangebias = waypoint_getlinearcost(rangebias);
1101 f = waypoint_getlinearcost(f);
1105 bool rate_wps = false;
1106 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1111 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1112 int t = pointcontents(trace_endpos + '0 0 1');
1113 if(t != CONTENT_SOLID )
1115 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1117 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1124 entity theEnemy = e;
1125 entity best_wp = NULL;
1126 float best_dist = 10000;
1127 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1128 && vdist(it.origin - this.origin, >, 100)
1129 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1131 float dist = vlen(it.origin - theEnemy.origin);
1132 if (dist < best_dist)
1144 vector goal_org = (e.absmin + e.absmax) * 0.5;
1146 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1148 // Evaluate path using jetpack
1150 if(this.items & IT_JETPACK)
1151 if(autocvar_bot_ai_navigation_jetpack)
1152 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1154 vector pointa, pointb;
1156 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1159 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1160 pointa = trace_endpos - '0 0 1';
1163 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1164 pointb = trace_endpos - '0 0 1';
1166 // Can I see these two points from the sky?
1167 traceline(pointa, pointb, MOVE_NORMAL, this);
1169 if(trace_fraction==1)
1171 LOG_DEBUG("jetpack ai: can bridge these two points");
1173 // Lower the altitude of these points as much as possible
1174 float zdistance, xydistance, cost, t, fuel;
1175 vector down, npa, npb;
1177 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1180 npa = pointa + down;
1181 npb = pointb + down;
1183 if(npa.z<=this.absmax.z)
1186 if(npb.z<=e.absmax.z)
1189 traceline(npa, npb, MOVE_NORMAL, this);
1190 if(trace_fraction==1)
1196 while(trace_fraction == 1);
1199 // Rough estimation of fuel consumption
1200 // (ignores acceleration and current xyz velocity)
1201 xydistance = vlen(pointa - pointb);
1202 zdistance = fabs(pointa.z - this.origin.z);
1204 t = zdistance / autocvar_g_jetpack_maxspeed_up;
1205 t += xydistance / autocvar_g_jetpack_maxspeed_side;
1206 fuel = t * autocvar_g_jetpack_fuel * 0.8;
1208 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1211 if(this.ammo_fuel>fuel)
1214 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1215 // - between air and ground speeds)
1217 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1218 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1221 // Compare against other goals
1222 f = f * rangebias / (rangebias + cost);
1224 if (navigation_bestrating < f)
1226 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1227 navigation_bestrating = f;
1228 navigation_bestgoal = e;
1229 this.navigation_jetpack_goal = e;
1230 this.navigation_jetpack_point = pointb;
1238 //te_wizspike(e.origin);
1241 // update the cached spawnfunc_waypoint link on a dynamic item entity
1242 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1248 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1249 e.nearestwaypoint = NULL;
1251 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1252 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1254 if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1255 e.nearestwaypoint = nwp = e.goalcurrent;
1257 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1260 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1262 if(!e.navigation_dynamicgoal)
1263 e.blacklisted = true;
1267 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1272 if(e.navigation_dynamicgoal)
1273 e.nearestwaypointtimeout = time + 2;
1274 else if(autocvar_g_waypointeditor)
1275 e.nearestwaypointtimeout = time + 3 + random() * 2;
1277 nwp = e.nearestwaypoint;
1280 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1281 if (nwp && nwp.wpcost < 10000000)
1283 //te_wizspike(nwp.wpnearestpoint);
1284 float nwptoitem_cost = 0;
1285 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1286 nwptoitem_cost = nwp.wp00mincost;
1288 nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1289 float cost = nwp.wpcost + nwptoitem_cost;
1290 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1291 f = f * rangebias / (rangebias + cost);
1292 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1293 if (navigation_bestrating < f)
1295 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1296 navigation_bestrating = f;
1297 navigation_bestgoal = e;
1302 // adds an item to the the goal stack with the path to a given item
1303 bool navigation_routetogoal(entity this, entity e, vector startposition)
1305 // if there is no goal, just exit
1309 entity teleport_goal = NULL;
1311 this.goalentity = e;
1313 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1315 // force teleport destination as route destination
1317 navigation_pushroute(this, e.wp00);
1318 this.goalentity = e.wp00;
1321 // put the entity on the goal stack
1322 //print("routetogoal ", etos(e), "\n");
1323 navigation_pushroute(this, e);
1326 e = this.goalentity;
1328 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1330 this.wp_goal_prev1 = this.wp_goal_prev0;
1331 this.wp_goal_prev0 = e;
1335 if(e==this.navigation_jetpack_goal)
1338 // if it can reach the goal there is nothing more to do
1339 vector dest = '0 0 0';
1340 float dest_height = 0;
1341 SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1342 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1345 entity nearest_wp = NULL;
1346 // see if there are waypoints describing a path to the item
1347 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1349 e = e.nearestwaypoint;
1352 else if(teleport_goal)
1355 e = e.enemy; // we already have added it, so...
1360 if(nearest_wp && nearest_wp.enemy)
1362 // often path can be optimized by not adding the nearest waypoint
1363 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1365 SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1366 if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1367 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1368 e = nearest_wp.enemy;
1370 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1371 e = nearest_wp.enemy;
1376 // add the spawnfunc_waypoint to the path
1377 navigation_pushroute(this, e);
1387 // removes any currently touching waypoints from the goal stack
1388 // (this is how bots detect if they reached a goal)
1389 int navigation_poptouchedgoals(entity this)
1391 int removed_goals = 0;
1393 if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1395 navigation_poproute(this);
1399 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1401 // make sure jumppad is really hit, don't rely on distance based checks
1402 // as they may report a touch even if it didn't really happen
1403 if(this.lastteleporttime > 0
1404 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1406 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1407 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1409 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1410 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1412 navigation_poproute(this);
1413 this.lastteleporttime = 0;
1417 return removed_goals;
1420 // If for some reason the bot is closer to the next goal, pop the current one
1421 if(this.goalstack01 && !wasfreed(this.goalstack01))
1422 if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1423 if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1424 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1426 // this can happen when a player stands still or is frozen
1427 // NOTE: this is only needed because tracewalk always fail when destination
1428 // point is on a player and bot_navigation_ignoreplayers is false
1429 if (this.goalentity.navigation_dynamicgoal
1430 && boxesoverlap(this.absmin, this.absmax, this.goalentity.absmin, this.goalentity.absmax))
1432 navigation_poproute(this);
1434 return removed_goals;
1437 vector dest = '0 0 0';
1438 float dest_height = 0;
1439 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1440 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1442 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1443 navigation_poproute(this);
1445 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1446 return removed_goals;
1447 // TODO this may also be a nice idea to do "early" (e.g. by
1448 // manipulating the vlen() comparisons) to shorten paths in
1449 // general - this would make bots walk more "on rails" than
1450 // "zigzagging" which they currently do with sufficiently
1451 // random-like waypoints, and thus can make a nice bot
1452 // personality property
1456 // Loose goal touching check when running
1457 if(this.aistatus & AI_STATUS_RUNNING)
1458 if(this.goalcurrent.classname=="waypoint")
1459 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1461 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1463 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1464 if(trace_fraction==1)
1466 // Detect personal waypoints
1467 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1468 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1470 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1471 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1474 navigation_poproute(this);
1476 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1477 return removed_goals;
1482 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1484 vector gc_min = this.goalcurrent.absmin;
1485 vector gc_max = this.goalcurrent.absmax;
1486 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1488 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1489 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1491 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1494 // Detect personal waypoints
1495 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1496 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1498 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1499 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1502 navigation_poproute(this);
1504 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1505 return removed_goals;
1507 return removed_goals;
1510 entity navigation_get_really_close_waypoint(entity this)
1512 entity wp = this.goalcurrent;
1513 if(!wp || vdist(wp.origin - this.origin, >, 50))
1514 wp = this.goalcurrent_prev;
1517 if(wp.classname != "waypoint")
1519 wp = wp.nearestwaypoint;
1523 if(vdist(wp.origin - this.origin, >, 50))
1525 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1527 if(vdist(it.origin - this.origin, <, 50))
1534 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1537 vector dest = '0 0 0';
1538 float dest_height = 0;
1539 SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1540 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1545 // begin a goal selection session (queries spawnfunc_waypoint network)
1546 void navigation_goalrating_start(entity this)
1548 if(this.aistatus & AI_STATUS_STUCK)
1551 this.navigation_jetpack_goal = NULL;
1552 navigation_bestrating = -1;
1553 entity wp = navigation_get_really_close_waypoint(this);
1554 navigation_clearroute(this);
1555 navigation_bestgoal = NULL;
1556 navigation_markroutes(this, wp);
1559 // ends a goal selection session (updates goal stack to the best goal)
1560 void navigation_goalrating_end(entity this)
1562 if(this.aistatus & AI_STATUS_STUCK)
1565 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1566 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1568 // If the bot got stuck then try to reach the farthest waypoint
1569 if (!this.goalentity && autocvar_bot_wander_enable)
1571 if (!(this.aistatus & AI_STATUS_STUCK))
1573 LOG_DEBUG(this.netname, " cannot walk to any goal");
1574 this.aistatus |= AI_STATUS_STUCK;
1579 void botframe_updatedangerousobjects(float maxupdate)
1581 vector m1, m2, v, o;
1585 IL_EACH(g_waypoints, true,
1591 IL_EACH(g_bot_dodge, it.bot_dodge,
1594 v.x = bound(m1_x, v.x, m2_x);
1595 v.y = bound(m1_y, v.y, m2_y);
1596 v.z = bound(m1_z, v.z, m2_z);
1597 o = (it.absmin + it.absmax) * 0.5;
1598 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1601 traceline(o, v, true, NULL);
1602 if (trace_fraction == 1)
1603 danger = danger + d;
1613 void navigation_unstuck(entity this)
1615 float search_radius = 1000;
1617 if (!autocvar_bot_wander_enable)
1620 if (!bot_waypoint_queue_owner)
1622 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1623 bot_waypoint_queue_owner = this;
1624 bot_waypoint_queue_bestgoal = NULL;
1625 bot_waypoint_queue_bestgoalrating = 0;
1628 if(bot_waypoint_queue_owner!=this)
1631 if (bot_waypoint_queue_goal)
1633 // evaluate the next goal on the queue
1634 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1635 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1636 vector dest = '0 0 0';
1637 float dest_height = 0;
1638 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1639 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1641 if( d > bot_waypoint_queue_bestgoalrating)
1643 bot_waypoint_queue_bestgoalrating = d;
1644 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1647 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1649 if (!bot_waypoint_queue_goal)
1651 if (bot_waypoint_queue_bestgoal)
1653 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1654 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1655 navigation_goalrating_timeout_set(this);
1656 this.aistatus &= ~AI_STATUS_STUCK;
1660 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1663 bot_waypoint_queue_owner = NULL;
1668 if(bot_strategytoken!=this)
1671 // build a new queue
1672 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1674 entity first = NULL;
1676 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1678 if(bot_waypoint_queue_goal)
1679 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1683 bot_waypoint_queue_goal = it;
1684 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1688 bot_waypoint_queue_goal = first;
1691 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1692 bot_waypoint_queue_owner = NULL;
1697 // Support for debugging tracewalk visually
1699 void debugresetnodes()
1701 debuglastnode = '0 0 0';
1704 void debugnode(entity this, vector node)
1706 if (!IS_PLAYER(this))
1709 if(debuglastnode=='0 0 0')
1711 debuglastnode = node;
1715 te_lightning2(NULL, node, debuglastnode);
1716 debuglastnode = node;
1719 void debugnodestatus(vector position, float status)
1725 case DEBUG_NODE_SUCCESS:
1728 case DEBUG_NODE_WARNING:
1731 case DEBUG_NODE_FAIL:
1738 te_customflash(position, 40, 2, c);
1741 // Support for debugging the goal stack visually
1744 .vector lastposition;
1746 // Debug the goal stack visually
1747 void debuggoalstack(entity this)
1752 if(this.goalcounter==0)goal=this.goalcurrent;
1753 else if(this.goalcounter==1)goal=this.goalstack01;
1754 else if(this.goalcounter==2)goal=this.goalstack02;
1755 else if(this.goalcounter==3)goal=this.goalstack03;
1756 else if(this.goalcounter==4)goal=this.goalstack04;
1757 else if(this.goalcounter==5)goal=this.goalstack05;
1758 else if(this.goalcounter==6)goal=this.goalstack06;
1759 else if(this.goalcounter==7)goal=this.goalstack07;
1760 else if(this.goalcounter==8)goal=this.goalstack08;
1761 else if(this.goalcounter==9)goal=this.goalstack09;
1762 else if(this.goalcounter==10)goal=this.goalstack10;
1763 else if(this.goalcounter==11)goal=this.goalstack11;
1764 else if(this.goalcounter==12)goal=this.goalstack12;
1765 else if(this.goalcounter==13)goal=this.goalstack13;
1766 else if(this.goalcounter==14)goal=this.goalstack14;
1767 else if(this.goalcounter==15)goal=this.goalstack15;
1768 else if(this.goalcounter==16)goal=this.goalstack16;
1769 else if(this.goalcounter==17)goal=this.goalstack17;
1770 else if(this.goalcounter==18)goal=this.goalstack18;
1771 else if(this.goalcounter==19)goal=this.goalstack19;
1772 else if(this.goalcounter==20)goal=this.goalstack20;
1773 else if(this.goalcounter==21)goal=this.goalstack21;
1774 else if(this.goalcounter==22)goal=this.goalstack22;
1775 else if(this.goalcounter==23)goal=this.goalstack23;
1776 else if(this.goalcounter==24)goal=this.goalstack24;
1777 else if(this.goalcounter==25)goal=this.goalstack25;
1778 else if(this.goalcounter==26)goal=this.goalstack26;
1779 else if(this.goalcounter==27)goal=this.goalstack27;
1780 else if(this.goalcounter==28)goal=this.goalstack28;
1781 else if(this.goalcounter==29)goal=this.goalstack29;
1782 else if(this.goalcounter==30)goal=this.goalstack30;
1783 else if(this.goalcounter==31)goal=this.goalstack31;
1788 this.goalcounter = 0;
1789 this.lastposition='0 0 0';
1793 if(this.lastposition=='0 0 0')
1796 org = this.lastposition;
1799 go = ( goal.absmin + goal.absmax ) * 0.5;
1800 te_lightning2(NULL, org, go);
1801 this.lastposition = go;