1 #include "navigation.qh"
6 #include "waypoints.qh"
8 #include <common/t_items.qh>
10 #include <common/items/_mod.qh>
12 #include <common/constants.qh>
13 #include <common/net_linked.qh>
14 #include <common/triggers/trigger/jumppads.qh>
18 void navigation_dynamicgoal_init(entity this, bool initially_static)
20 this.navigation_dynamicgoal = true;
21 this.bot_basewaypoint = this.nearestwaypoint;
23 this.nearestwaypointtimeout = -1;
25 this.nearestwaypointtimeout = time;
28 void navigation_dynamicgoal_set(entity this)
30 this.nearestwaypointtimeout = time;
33 void navigation_dynamicgoal_unset(entity this)
35 if(this.bot_basewaypoint)
36 this.nearestwaypoint = this.bot_basewaypoint;
37 this.nearestwaypointtimeout = -1;
40 bool navigation_checkladders(entity e, vector org, vector move, vector m1, vector m2, vector end, vector end2, int movemode)
42 IL_EACH(g_ladders, it.classname == "func_ladder",
45 if(boxesoverlap(move + m1 + '-1 -1 -1', move + m2 + '1 1 1', it.absmin, it.absmax))
46 if(boxesoverlap(end, end2, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
49 top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
50 tracebox(org, m1, m2, top, movemode, e);
51 if(trace_fraction == 1)
58 // rough simulation of walking from one point to another to test if a path
59 // can be traveled, used for waypoint linking and havocbot
60 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
61 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
63 if(autocvar_bot_debug_tracewalk)
70 vector dir = end - start;
72 float dist = vlen(dir);
75 bool ignorehazards = false;
76 bool swimming = false;
78 // Analyze starting point
79 traceline(start, start, MOVE_NORMAL, e);
80 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
84 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
85 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
91 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
95 if(autocvar_bot_debug_tracewalk)
96 debugnodestatus(start, DEBUG_NODE_FAIL);
98 //print("tracewalk: ", vtos(start), " is a bad start\n");
104 end2.z += end_height;
108 if (boxesoverlap(end, end2, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
111 if(autocvar_bot_debug_tracewalk)
112 debugnodestatus(org, DEBUG_NODE_SUCCESS);
114 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
117 if(autocvar_bot_debug_tracewalk)
124 dist = dist - stepdist;
125 traceline(org, org, MOVE_NORMAL, e);
128 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
130 // hazards blocking path
131 if(autocvar_bot_debug_tracewalk)
132 debugnodestatus(org, DEBUG_NODE_FAIL);
134 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
138 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
140 vector water_end = end;
141 water_end.z = bound(end.z, org.z, end2.z);
142 vector water_dir = normalize(water_end - org);
143 vector move = org + water_dir * stepdist;
144 tracebox(org, m1, m2, move, movemode, e);
146 if(autocvar_bot_debug_tracewalk)
147 debugnode(e, trace_endpos);
149 if (trace_fraction < 1)
152 org = trace_endpos - water_dir * (stepdist / 2);
153 for (; org.z < end2.z + e.maxs.z; org.z += stepdist)
155 if(autocvar_bot_debug_tracewalk)
158 if(pointcontents(org) == CONTENT_EMPTY)
162 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
164 if(autocvar_bot_debug_tracewalk)
165 debugnodestatus(org, DEBUG_NODE_FAIL);
168 //print("tracewalk: ", vtos(start), " failed under water\n");
171 if(navigation_checkladders(e, org, move, m1, m2, end, end2, movemode))
173 if(autocvar_bot_debug_tracewalk)
174 debugnodestatus(trace_endpos, DEBUG_NODE_SUCCESS);
176 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
187 vector move = org + dir * stepdist;
188 tracebox(org, m1, m2, move, movemode, e);
190 if(autocvar_bot_debug_tracewalk)
191 debugnode(e, trace_endpos);
194 if (trace_fraction < 1)
196 // check if we can walk over this obstacle, possibly by jumpstepping
197 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
198 if (trace_fraction < 1 || trace_startsolid)
200 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
201 if (trace_fraction < 1 || trace_startsolid)
203 if(navigation_checkladders(e, trace_endpos - dir * (stepdist / 2),
204 move + jumpheight_vec, m1, m2, end, end2, movemode))
206 if(autocvar_bot_debug_tracewalk)
207 debugnodestatus(trace_endpos, DEBUG_NODE_SUCCESS);
209 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
213 if(autocvar_bot_debug_tracewalk)
214 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
216 traceline( org, move, movemode, e);
218 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
222 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
224 nextmove = move + (dir * stepdist);
225 traceline( move, nextmove, movemode, e);
231 if(autocvar_bot_debug_tracewalk)
232 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
234 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
235 //te_explosion(trace_endpos);
236 //print(ftos(e.dphitcontentsmask), "\n");
237 return false; // failed
249 // trace down from stepheight as far as possible and move there,
250 // if this starts in solid we try again without the stepup, and
251 // if that also fails we assume it is a wall
252 // (this is the same logic as the Quake walkmove function used)
253 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
255 // moved successfully
259 c = pointcontents(org + '0 0 1');
260 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
270 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
272 // moved but didn't arrive at the intended destination
273 if(autocvar_bot_debug_tracewalk)
274 debugnodestatus(org, DEBUG_NODE_FAIL);
279 /////////////////////////////////////////////////////////////////////////////
281 /////////////////////////////////////////////////////////////////////////////
283 // completely empty the goal stack, used when deciding where to go
284 void navigation_clearroute(entity this)
286 this.goalcurrent_prev = this.goalcurrent;
287 this.goalcurrent_distance = 10000000;
288 this.goalcurrent_distance_time = 0;
289 //print("bot ", etos(this), " clear\n");
290 this.goalentity = NULL;
291 this.goalcurrent = NULL;
292 this.goalstack01 = NULL;
293 this.goalstack02 = NULL;
294 this.goalstack03 = NULL;
295 this.goalstack04 = NULL;
296 this.goalstack05 = NULL;
297 this.goalstack06 = NULL;
298 this.goalstack07 = NULL;
299 this.goalstack08 = NULL;
300 this.goalstack09 = NULL;
301 this.goalstack10 = NULL;
302 this.goalstack11 = NULL;
303 this.goalstack12 = NULL;
304 this.goalstack13 = NULL;
305 this.goalstack14 = NULL;
306 this.goalstack15 = NULL;
307 this.goalstack16 = NULL;
308 this.goalstack17 = NULL;
309 this.goalstack18 = NULL;
310 this.goalstack19 = NULL;
311 this.goalstack20 = NULL;
312 this.goalstack21 = NULL;
313 this.goalstack22 = NULL;
314 this.goalstack23 = NULL;
315 this.goalstack24 = NULL;
316 this.goalstack25 = NULL;
317 this.goalstack26 = NULL;
318 this.goalstack27 = NULL;
319 this.goalstack28 = NULL;
320 this.goalstack29 = NULL;
321 this.goalstack30 = NULL;
322 this.goalstack31 = NULL;
325 // add a new goal at the beginning of the stack
326 // (in other words: add a new prerequisite before going to the later goals)
327 // NOTE: when a waypoint is added, the WP gets pushed first, then the
328 // next-closest WP on the shortest path to the WP
329 // That means, if the stack overflows, the bot will know how to do the FIRST 32
330 // steps to the goal, and then recalculate the path.
331 void navigation_pushroute(entity this, entity e)
333 this.goalcurrent_prev = this.goalcurrent;
334 this.goalcurrent_distance = 10000000;
335 this.goalcurrent_distance_time = 0;
336 //print("bot ", etos(this), " push ", etos(e), "\n");
337 if(this.goalstack31 == this.goalentity)
338 this.goalentity = NULL;
339 this.goalstack31 = this.goalstack30;
340 this.goalstack30 = this.goalstack29;
341 this.goalstack29 = this.goalstack28;
342 this.goalstack28 = this.goalstack27;
343 this.goalstack27 = this.goalstack26;
344 this.goalstack26 = this.goalstack25;
345 this.goalstack25 = this.goalstack24;
346 this.goalstack24 = this.goalstack23;
347 this.goalstack23 = this.goalstack22;
348 this.goalstack22 = this.goalstack21;
349 this.goalstack21 = this.goalstack20;
350 this.goalstack20 = this.goalstack19;
351 this.goalstack19 = this.goalstack18;
352 this.goalstack18 = this.goalstack17;
353 this.goalstack17 = this.goalstack16;
354 this.goalstack16 = this.goalstack15;
355 this.goalstack15 = this.goalstack14;
356 this.goalstack14 = this.goalstack13;
357 this.goalstack13 = this.goalstack12;
358 this.goalstack12 = this.goalstack11;
359 this.goalstack11 = this.goalstack10;
360 this.goalstack10 = this.goalstack09;
361 this.goalstack09 = this.goalstack08;
362 this.goalstack08 = this.goalstack07;
363 this.goalstack07 = this.goalstack06;
364 this.goalstack06 = this.goalstack05;
365 this.goalstack05 = this.goalstack04;
366 this.goalstack04 = this.goalstack03;
367 this.goalstack03 = this.goalstack02;
368 this.goalstack02 = this.goalstack01;
369 this.goalstack01 = this.goalcurrent;
370 this.goalcurrent = e;
373 // remove first goal from stack
374 // (in other words: remove a prerequisite for reaching the later goals)
375 // (used when a spawnfunc_waypoint is reached)
376 void navigation_poproute(entity this)
378 this.goalcurrent_prev = this.goalcurrent;
379 this.goalcurrent_distance = 10000000;
380 this.goalcurrent_distance_time = 0;
381 //print("bot ", etos(this), " pop\n");
382 if(this.goalcurrent == this.goalentity)
383 this.goalentity = NULL;
384 this.goalcurrent = this.goalstack01;
385 this.goalstack01 = this.goalstack02;
386 this.goalstack02 = this.goalstack03;
387 this.goalstack03 = this.goalstack04;
388 this.goalstack04 = this.goalstack05;
389 this.goalstack05 = this.goalstack06;
390 this.goalstack06 = this.goalstack07;
391 this.goalstack07 = this.goalstack08;
392 this.goalstack08 = this.goalstack09;
393 this.goalstack09 = this.goalstack10;
394 this.goalstack10 = this.goalstack11;
395 this.goalstack11 = this.goalstack12;
396 this.goalstack12 = this.goalstack13;
397 this.goalstack13 = this.goalstack14;
398 this.goalstack14 = this.goalstack15;
399 this.goalstack15 = this.goalstack16;
400 this.goalstack16 = this.goalstack17;
401 this.goalstack17 = this.goalstack18;
402 this.goalstack18 = this.goalstack19;
403 this.goalstack19 = this.goalstack20;
404 this.goalstack20 = this.goalstack21;
405 this.goalstack21 = this.goalstack22;
406 this.goalstack22 = this.goalstack23;
407 this.goalstack23 = this.goalstack24;
408 this.goalstack24 = this.goalstack25;
409 this.goalstack25 = this.goalstack26;
410 this.goalstack26 = this.goalstack27;
411 this.goalstack27 = this.goalstack28;
412 this.goalstack28 = this.goalstack29;
413 this.goalstack29 = this.goalstack30;
414 this.goalstack30 = this.goalstack31;
415 this.goalstack31 = NULL;
418 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
421 dist = vlen(v - org);
424 traceline(v, org, true, ent);
425 if (trace_fraction == 1)
429 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, 0, bot_navigation_movemode))
434 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, 0, bot_navigation_movemode))
442 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
443 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
445 vector pm1 = ent.origin + ent.mins;
446 vector pm2 = ent.origin + ent.maxs;
448 // do two scans, because box test is cheaper
449 IL_EACH(g_waypoints, it != ent && it != except,
451 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
455 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
456 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
457 // TODO possibly make other code have the same support for bboxes
459 org = org + ent.tag_entity.origin;
460 if (navigation_testtracewalk)
466 if(!autocvar_g_waypointeditor && !ent.navigation_dynamicgoal)
468 waypoint_clearlinks(ent); // initialize wpXXmincost fields
469 IL_EACH(g_waypoints, it != ent,
471 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
476 vector wm1 = it.absmin;
477 vector wm2 = it.absmax;
478 v.x = bound(wm1_x, org.x, wm2_x);
479 v.y = bound(wm1_y, org.y, wm2_y);
480 v.z = bound(wm1_z, org.z, wm2_z);
484 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, 1050))
485 navigation_item_addlink(it, ent);
489 // box check failed, try walk
490 IL_EACH(g_waypoints, it != ent,
492 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
497 vector wm1 = it.origin + it.mins;
498 vector wm2 = it.origin + it.maxs;
499 v.x = bound(wm1_x, org.x, wm2_x);
500 v.y = bound(wm1_y, org.y, wm2_y);
501 v.z = bound(wm1_z, org.z, wm2_z);
505 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
507 bestdist = vlen(v - org);
513 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
515 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
516 if (autocvar_g_waypointeditor_auto)
518 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
520 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
525 // finds the waypoints near the bot initiating a navigation query
526 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
529 //navigation_testtracewalk = true;
532 IL_EACH(g_waypoints, !it.wpconsidered,
536 m1 = it.origin + it.mins;
537 m2 = it.origin + it.maxs;
539 v.x = bound(m1_x, v.x, m2_x);
540 v.y = bound(m1_y, v.y, m2_y);
542 v_height = m2.z - m1.z;
549 vector diff = v - this.origin;
550 diff.z = max(0, diff.z);
551 if(vdist(diff, <, maxdist))
553 it.wpconsidered = true;
554 if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
556 it.wpnearestpoint = v;
557 it.wpcost = waypoint_gettravelcost(this.origin, v) + it.dmg;
564 //navigation_testtracewalk = false;
568 // updates a path link if a spawnfunc_waypoint link is better than the current one
569 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
578 v.x = bound(m1_x, p.x, m2_x);
579 v.y = bound(m1_y, p.y, m2_y);
580 v.z = bound(m1_z, p.z, m2_z);
584 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
585 cost += w.wp00mincost; // assuming teleport has exactly one destination
587 cost += waypoint_gettravelcost(p, v);
588 if (wp.wpcost > cost)
593 wp.wpnearestpoint = v;
597 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
598 void navigation_markroutes(entity this, entity fixed_source_waypoint)
603 IL_EACH(g_waypoints, true,
605 it.wpconsidered = false;
606 it.wpnearestpoint = '0 0 0';
607 it.wpcost = 10000000;
612 if(fixed_source_waypoint)
614 fixed_source_waypoint.wpconsidered = true;
615 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
616 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
617 fixed_source_waypoint.wpfire = 1;
618 fixed_source_waypoint.enemy = NULL;
622 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
623 // as this search is expensive we will use lower values if the bot is on the air
624 float increment, maxdistance;
625 if(IS_ONGROUND(this))
636 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
639 bool searching = true;
643 IL_EACH(g_waypoints, it.wpfire,
648 p = it.wpnearestpoint;
650 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
651 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
652 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
653 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
654 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
655 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
656 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
657 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
658 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
659 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
660 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
661 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
662 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
663 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
664 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
665 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
666 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
667 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
668 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
669 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
670 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
671 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
672 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
673 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
674 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
675 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
676 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
677 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
678 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
679 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
680 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
681 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
682 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
687 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
688 void navigation_markroutes_inverted(entity fixed_source_waypoint)
692 IL_EACH(g_waypoints, true,
694 it.wpconsidered = false;
695 it.wpnearestpoint = '0 0 0';
696 it.wpcost = 10000000;
701 if(fixed_source_waypoint)
703 fixed_source_waypoint.wpconsidered = true;
704 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
705 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
706 fixed_source_waypoint.wpfire = 1;
707 fixed_source_waypoint.enemy = NULL;
711 error("need to start with a waypoint\n");
714 bool searching = true;
718 IL_EACH(g_waypoints, it.wpfire,
722 cost = it.wpcost; // cost to walk from it to home
723 p = it.wpnearestpoint;
725 IL_EACH(g_waypoints, it != wp,
727 if(!waypoint_islinked(it, wp))
729 cost2 = cost + it.dmg;
730 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
736 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
737 void navigation_routerating(entity this, entity e, float f, float rangebias)
745 rangebias = waypoint_getlinearcost(rangebias);
746 f = waypoint_getlinearcost(f);
750 bool rate_wps = false;
751 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
756 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
757 int t = pointcontents(trace_endpos + '0 0 1');
758 if(t != CONTENT_SOLID )
760 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
762 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
770 entity best_wp = NULL;
771 float best_dist = 10000;
772 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
773 && vdist(it.origin - this.origin, >, 100)
774 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
776 float dist = vlen(it.origin - theEnemy.origin);
777 if (dist < best_dist)
789 vector goal_org = (e.absmin + e.absmax) * 0.5;
791 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
793 // Evaluate path using jetpack
795 if(this.items & IT_JETPACK)
796 if(autocvar_bot_ai_navigation_jetpack)
797 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
799 vector pointa, pointb;
801 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
804 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
805 pointa = trace_endpos - '0 0 1';
808 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
809 pointb = trace_endpos - '0 0 1';
811 // Can I see these two points from the sky?
812 traceline(pointa, pointb, MOVE_NORMAL, this);
814 if(trace_fraction==1)
816 LOG_DEBUG("jetpack ai: can bridge these two points");
818 // Lower the altitude of these points as much as possible
819 float zdistance, xydistance, cost, t, fuel;
820 vector down, npa, npb;
822 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
828 if(npa.z<=this.absmax.z)
831 if(npb.z<=e.absmax.z)
834 traceline(npa, npb, MOVE_NORMAL, this);
835 if(trace_fraction==1)
841 while(trace_fraction == 1);
844 // Rough estimation of fuel consumption
845 // (ignores acceleration and current xyz velocity)
846 xydistance = vlen(pointa - pointb);
847 zdistance = fabs(pointa.z - this.origin.z);
849 t = zdistance / autocvar_g_jetpack_maxspeed_up;
850 t += xydistance / autocvar_g_jetpack_maxspeed_side;
851 fuel = t * autocvar_g_jetpack_fuel * 0.8;
853 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
856 if(this.ammo_fuel>fuel)
859 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
860 // - between air and ground speeds)
862 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
863 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
866 // Compare against other goals
867 f = f * rangebias / (rangebias + cost);
869 if (navigation_bestrating < f)
871 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
872 navigation_bestrating = f;
873 navigation_bestgoal = e;
874 this.navigation_jetpack_goal = e;
875 this.navigation_jetpack_point = pointb;
883 //te_wizspike(e.origin);
886 // update the cached spawnfunc_waypoint link on a dynamic item entity
887 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
893 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
894 e.nearestwaypoint = NULL;
896 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
897 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
899 nwp = navigation_findnearestwaypoint(e, true);
902 e.nearestwaypoint = nwp;
904 vector m1 = nwp.absmin, m2 = nwp.absmax;
905 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
906 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
907 vector ve = (e.absmin - e.absmax) * 0.5;
908 ve.x = bound(m1.x, ve.x, m2.x);
909 ve.y = bound(m1.y, ve.y, m2.y);
910 ve.z = bound(m1.z, ve.z, m2.z);
912 m1 = e.absmin; m2 = e.absmax;
913 m1.x = e.origin.x; m1.y = e.origin.y;
914 m2.x = e.origin.x; m2.y = e.origin.y;
915 vector vnwp = nwp.origin;
916 vnwp.x = bound(m1.x, vnwp.x, m2.x);
917 vnwp.y = bound(m1.y, vnwp.y, m2.y);
918 vnwp.z = bound(m1.z, vnwp.z, m2.z);
919 e.nearestwaypoint_dist = vlen(ve - vnwp);
923 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
925 if(!e.navigation_dynamicgoal)
926 e.blacklisted = true;
930 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
935 if(e.navigation_dynamicgoal)
936 e.nearestwaypointtimeout = time + 2;
937 else if(autocvar_g_waypointeditor)
938 e.nearestwaypointtimeout = time + 3 + random() * 2;
940 nwp = e.nearestwaypoint;
943 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
945 if (nwp.wpcost < 10000000)
947 //te_wizspike(nwp.wpnearestpoint);
948 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org);
949 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
950 f = f * rangebias / (rangebias + cost);
951 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
952 if (navigation_bestrating < f)
954 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
955 navigation_bestrating = f;
956 navigation_bestgoal = e;
961 // adds an item to the the goal stack with the path to a given item
962 bool navigation_routetogoal(entity this, entity e, vector startposition)
964 // if there is no goal, just exit
968 if(e.wpflags & WAYPOINTFLAG_TELEPORT)
970 // force teleport destination as route destination
977 // put the entity on the goal stack
978 //print("routetogoal ", etos(e), "\n");
979 navigation_pushroute(this, e);
981 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
983 this.wp_goal_prev1 = this.wp_goal_prev0;
984 this.wp_goal_prev0 = e;
988 if(e==this.navigation_jetpack_goal)
991 // if it can reach the goal there is nothing more to do
992 vector dest = (e.absmin + e.absmax) * 0.5;
994 float dest_height = e.absmax.z - e.absmin.z;
995 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
998 entity nearest_wp = NULL;
999 // see if there are waypoints describing a path to the item
1000 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1002 e = e.nearestwaypoint;
1006 e = e.enemy; // we already have added it, so...
1011 if(nearest_wp && nearest_wp.enemy)
1013 // often path can be optimized by not adding the nearest waypoint
1014 if (this.goalentity.nearestwaypoint_dist < 8)
1015 e = nearest_wp.enemy;
1018 if (this.goalentity.navigation_dynamicgoal)
1020 vector dest = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
1021 dest.z = this.goalentity.absmin.z;
1022 float dest_height = this.goalentity.absmax.z - this.goalentity.absmin.z;
1023 if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1024 e = nearest_wp.enemy;
1026 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1027 e = nearest_wp.enemy;
1033 // add the spawnfunc_waypoint to the path
1034 navigation_pushroute(this, e);
1044 // removes any currently touching waypoints from the goal stack
1045 // (this is how bots detect if they reached a goal)
1046 void navigation_poptouchedgoals(entity this)
1048 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1050 // make sure jumppad is really hit, don't rely on distance based checks
1051 // as they may report a touch even if it didn't really happen
1052 if(this.lastteleporttime > 0
1053 && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1055 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1056 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1058 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1059 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1061 navigation_poproute(this);
1067 // If for some reason the bot is closer to the next goal, pop the current one
1068 if(this.goalstack01 && !wasfreed(this.goalstack01))
1069 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1070 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1072 vector dest = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
1073 dest.z = this.goalstack01.absmin.z;
1074 float dest_height = this.goalstack01.absmax.z - this.goalstack01.absmin.z;
1075 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1077 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1078 navigation_poproute(this);
1079 // TODO this may also be a nice idea to do "early" (e.g. by
1080 // manipulating the vlen() comparisons) to shorten paths in
1081 // general - this would make bots walk more "on rails" than
1082 // "zigzagging" which they currently do with sufficiently
1083 // random-like waypoints, and thus can make a nice bot
1084 // personality property
1088 // Loose goal touching check when running
1089 if(this.aistatus & AI_STATUS_RUNNING)
1090 if(this.goalcurrent.classname=="waypoint")
1091 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1093 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1095 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1096 if(trace_fraction==1)
1098 // Detect personal waypoints
1099 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1100 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1102 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1103 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1106 navigation_poproute(this);
1111 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1113 vector gc_min = this.goalcurrent.absmin;
1114 vector gc_max = this.goalcurrent.absmax;
1115 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1117 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1118 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1120 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1123 // Detect personal waypoints
1124 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1125 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1127 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1128 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1131 navigation_poproute(this);
1135 // begin a goal selection session (queries spawnfunc_waypoint network)
1136 void navigation_goalrating_start(entity this)
1138 if(this.aistatus & AI_STATUS_STUCK)
1141 this.navigation_jetpack_goal = NULL;
1142 navigation_bestrating = -1;
1143 navigation_clearroute(this);
1144 navigation_bestgoal = NULL;
1145 navigation_markroutes(this, NULL);
1148 // ends a goal selection session (updates goal stack to the best goal)
1149 void navigation_goalrating_end(entity this)
1151 if(this.aistatus & AI_STATUS_STUCK)
1154 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1155 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1157 // If the bot got stuck then try to reach the farthest waypoint
1158 if (!this.goalentity && autocvar_bot_wander_enable)
1160 if (!(this.aistatus & AI_STATUS_STUCK))
1162 LOG_DEBUG(this.netname, " cannot walk to any goal");
1163 this.aistatus |= AI_STATUS_STUCK;
1168 void botframe_updatedangerousobjects(float maxupdate)
1170 vector m1, m2, v, o;
1173 IL_EACH(g_waypoints, true,
1178 IL_EACH(g_bot_dodge, it.bot_dodge,
1181 v.x = bound(m1_x, v.x, m2_x);
1182 v.y = bound(m1_y, v.y, m2_y);
1183 v.z = bound(m1_z, v.z, m2_z);
1184 o = (it.absmin + it.absmax) * 0.5;
1185 d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v);
1188 traceline(o, v, true, NULL);
1189 if (trace_fraction == 1)
1190 danger = danger + d;
1200 void navigation_unstuck(entity this)
1202 float search_radius = 1000;
1204 if (!autocvar_bot_wander_enable)
1207 if (!bot_waypoint_queue_owner)
1209 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1210 bot_waypoint_queue_owner = this;
1211 bot_waypoint_queue_bestgoal = NULL;
1212 bot_waypoint_queue_bestgoalrating = 0;
1215 if(bot_waypoint_queue_owner!=this)
1218 if (bot_waypoint_queue_goal)
1220 // evaluate the next goal on the queue
1221 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1222 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1223 vector dest = (bot_waypoint_queue_goal.absmin + bot_waypoint_queue_goal.absmax) * 0.5;
1224 dest.z = bot_waypoint_queue_goal.absmin.z;
1225 float dest_height = bot_waypoint_queue_goal.absmax.z - bot_waypoint_queue_goal.absmin.z;
1226 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1228 if( d > bot_waypoint_queue_bestgoalrating)
1230 bot_waypoint_queue_bestgoalrating = d;
1231 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1234 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1236 if (!bot_waypoint_queue_goal)
1238 if (bot_waypoint_queue_bestgoal)
1240 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1241 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1242 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1243 this.aistatus &= ~AI_STATUS_STUCK;
1247 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1250 bot_waypoint_queue_owner = NULL;
1255 if(bot_strategytoken!=this)
1258 // build a new queue
1259 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1261 entity first = NULL;
1263 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1265 if(bot_waypoint_queue_goal)
1266 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1270 bot_waypoint_queue_goal = it;
1271 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1275 bot_waypoint_queue_goal = first;
1278 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1279 bot_waypoint_queue_owner = NULL;
1284 // Support for debugging tracewalk visually
1286 void debugresetnodes()
1288 debuglastnode = '0 0 0';
1291 void debugnode(entity this, vector node)
1293 if (!IS_PLAYER(this))
1296 if(debuglastnode=='0 0 0')
1298 debuglastnode = node;
1302 te_lightning2(NULL, node, debuglastnode);
1303 debuglastnode = node;
1306 void debugnodestatus(vector position, float status)
1312 case DEBUG_NODE_SUCCESS:
1315 case DEBUG_NODE_WARNING:
1318 case DEBUG_NODE_FAIL:
1325 te_customflash(position, 40, 2, c);
1328 // Support for debugging the goal stack visually
1331 .vector lastposition;
1333 // Debug the goal stack visually
1334 void debuggoalstack(entity this)
1339 if(this.goalcounter==0)goal=this.goalcurrent;
1340 else if(this.goalcounter==1)goal=this.goalstack01;
1341 else if(this.goalcounter==2)goal=this.goalstack02;
1342 else if(this.goalcounter==3)goal=this.goalstack03;
1343 else if(this.goalcounter==4)goal=this.goalstack04;
1344 else if(this.goalcounter==5)goal=this.goalstack05;
1345 else if(this.goalcounter==6)goal=this.goalstack06;
1346 else if(this.goalcounter==7)goal=this.goalstack07;
1347 else if(this.goalcounter==8)goal=this.goalstack08;
1348 else if(this.goalcounter==9)goal=this.goalstack09;
1349 else if(this.goalcounter==10)goal=this.goalstack10;
1350 else if(this.goalcounter==11)goal=this.goalstack11;
1351 else if(this.goalcounter==12)goal=this.goalstack12;
1352 else if(this.goalcounter==13)goal=this.goalstack13;
1353 else if(this.goalcounter==14)goal=this.goalstack14;
1354 else if(this.goalcounter==15)goal=this.goalstack15;
1355 else if(this.goalcounter==16)goal=this.goalstack16;
1356 else if(this.goalcounter==17)goal=this.goalstack17;
1357 else if(this.goalcounter==18)goal=this.goalstack18;
1358 else if(this.goalcounter==19)goal=this.goalstack19;
1359 else if(this.goalcounter==20)goal=this.goalstack20;
1360 else if(this.goalcounter==21)goal=this.goalstack21;
1361 else if(this.goalcounter==22)goal=this.goalstack22;
1362 else if(this.goalcounter==23)goal=this.goalstack23;
1363 else if(this.goalcounter==24)goal=this.goalstack24;
1364 else if(this.goalcounter==25)goal=this.goalstack25;
1365 else if(this.goalcounter==26)goal=this.goalstack26;
1366 else if(this.goalcounter==27)goal=this.goalstack27;
1367 else if(this.goalcounter==28)goal=this.goalstack28;
1368 else if(this.goalcounter==29)goal=this.goalstack29;
1369 else if(this.goalcounter==30)goal=this.goalstack30;
1370 else if(this.goalcounter==31)goal=this.goalstack31;
1375 this.goalcounter = 0;
1376 this.lastposition='0 0 0';
1380 if(this.lastposition=='0 0 0')
1383 org = this.lastposition;
1386 go = ( goal.absmin + goal.absmax ) * 0.5;
1387 te_lightning2(NULL, org, go);
1388 this.lastposition = go;