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/triggers/trigger/jumppads.qh>
17 // rough simulation of walking from one point to another to test if a path
18 // can be traveled, used for waypoint linking and havocbot
20 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
32 if(autocvar_bot_debug_tracewalk)
41 dist = totaldist = vlen(move);
42 dir = normalize(move);
44 ignorehazards = false;
47 // Analyze starting point
48 traceline(start, start, MOVE_NORMAL, e);
49 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
53 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
54 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
60 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
64 if(autocvar_bot_debug_tracewalk)
65 debugnodestatus(start, DEBUG_NODE_FAIL);
67 //print("tracewalk: ", vtos(start), " is a bad start\n");
76 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
79 if(autocvar_bot_debug_tracewalk)
80 debugnodestatus(org, DEBUG_NODE_SUCCESS);
82 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
85 if(autocvar_bot_debug_tracewalk)
92 dist = dist - stepdist;
93 traceline(org, org, MOVE_NORMAL, e);
96 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
98 // hazards blocking path
99 if(autocvar_bot_debug_tracewalk)
100 debugnodestatus(org, DEBUG_NODE_FAIL);
102 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
106 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
108 move = normalize(end - org);
109 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
111 if(autocvar_bot_debug_tracewalk)
112 debugnode(e, trace_endpos);
114 if (trace_fraction < 1)
117 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
118 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
120 if(autocvar_bot_debug_tracewalk)
123 if(pointcontents(org) == CONTENT_EMPTY)
127 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
129 if(autocvar_bot_debug_tracewalk)
130 debugnodestatus(org, DEBUG_NODE_FAIL);
133 //print("tracewalk: ", vtos(start), " failed under water\n");
143 move = dir * stepdist + org;
144 tracebox(org, m1, m2, move, movemode, e);
146 if(autocvar_bot_debug_tracewalk)
147 debugnode(e, trace_endpos);
150 if (trace_fraction < 1)
152 // check if we can walk over this obstacle, possibly by jumpstepping
153 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
154 if (trace_fraction < 1 || trace_startsolid)
156 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
157 if (trace_fraction < 1 || trace_startsolid)
159 if(autocvar_bot_debug_tracewalk)
160 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
163 traceline( org, move, movemode, e);
164 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
168 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
170 nextmove = move + (dir * stepdist);
171 traceline( move, nextmove, movemode, e);
177 if(autocvar_bot_debug_tracewalk)
178 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
180 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
181 //te_explosion(trace_endpos);
182 //print(ftos(e.dphitcontentsmask), "\n");
183 return false; // failed
195 // trace down from stepheight as far as possible and move there,
196 // if this starts in solid we try again without the stepup, and
197 // if that also fails we assume it is a wall
198 // (this is the same logic as the Quake walkmove function used)
199 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
201 // moved successfully
205 c = pointcontents(org + '0 0 1');
206 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
216 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
218 // moved but didn't arrive at the intended destination
219 if(autocvar_bot_debug_tracewalk)
220 debugnodestatus(org, DEBUG_NODE_FAIL);
225 /////////////////////////////////////////////////////////////////////////////
227 /////////////////////////////////////////////////////////////////////////////
229 // completely empty the goal stack, used when deciding where to go
230 void navigation_clearroute(entity this)
232 //print("bot ", etos(this), " clear\n");
233 this.navigation_hasgoals = false;
234 this.goalcurrent = NULL;
235 this.goalstack01 = NULL;
236 this.goalstack02 = NULL;
237 this.goalstack03 = NULL;
238 this.goalstack04 = NULL;
239 this.goalstack05 = NULL;
240 this.goalstack06 = NULL;
241 this.goalstack07 = NULL;
242 this.goalstack08 = NULL;
243 this.goalstack09 = NULL;
244 this.goalstack10 = NULL;
245 this.goalstack11 = NULL;
246 this.goalstack12 = NULL;
247 this.goalstack13 = NULL;
248 this.goalstack14 = NULL;
249 this.goalstack15 = NULL;
250 this.goalstack16 = NULL;
251 this.goalstack17 = NULL;
252 this.goalstack18 = NULL;
253 this.goalstack19 = NULL;
254 this.goalstack20 = NULL;
255 this.goalstack21 = NULL;
256 this.goalstack22 = NULL;
257 this.goalstack23 = NULL;
258 this.goalstack24 = NULL;
259 this.goalstack25 = NULL;
260 this.goalstack26 = NULL;
261 this.goalstack27 = NULL;
262 this.goalstack28 = NULL;
263 this.goalstack29 = NULL;
264 this.goalstack30 = NULL;
265 this.goalstack31 = NULL;
268 // add a new goal at the beginning of the stack
269 // (in other words: add a new prerequisite before going to the later goals)
270 // NOTE: when a waypoint is added, the WP gets pushed first, then the
271 // next-closest WP on the shortest path to the WP
272 // That means, if the stack overflows, the bot will know how to do the FIRST 32
273 // steps to the goal, and then recalculate the path.
274 void navigation_pushroute(entity this, entity e)
276 //print("bot ", etos(this), " push ", etos(e), "\n");
277 this.goalstack31 = this.goalstack30;
278 this.goalstack30 = this.goalstack29;
279 this.goalstack29 = this.goalstack28;
280 this.goalstack28 = this.goalstack27;
281 this.goalstack27 = this.goalstack26;
282 this.goalstack26 = this.goalstack25;
283 this.goalstack25 = this.goalstack24;
284 this.goalstack24 = this.goalstack23;
285 this.goalstack23 = this.goalstack22;
286 this.goalstack22 = this.goalstack21;
287 this.goalstack21 = this.goalstack20;
288 this.goalstack20 = this.goalstack19;
289 this.goalstack19 = this.goalstack18;
290 this.goalstack18 = this.goalstack17;
291 this.goalstack17 = this.goalstack16;
292 this.goalstack16 = this.goalstack15;
293 this.goalstack15 = this.goalstack14;
294 this.goalstack14 = this.goalstack13;
295 this.goalstack13 = this.goalstack12;
296 this.goalstack12 = this.goalstack11;
297 this.goalstack11 = this.goalstack10;
298 this.goalstack10 = this.goalstack09;
299 this.goalstack09 = this.goalstack08;
300 this.goalstack08 = this.goalstack07;
301 this.goalstack07 = this.goalstack06;
302 this.goalstack06 = this.goalstack05;
303 this.goalstack05 = this.goalstack04;
304 this.goalstack04 = this.goalstack03;
305 this.goalstack03 = this.goalstack02;
306 this.goalstack02 = this.goalstack01;
307 this.goalstack01 = this.goalcurrent;
308 this.goalcurrent = e;
311 // remove first goal from stack
312 // (in other words: remove a prerequisite for reaching the later goals)
313 // (used when a spawnfunc_waypoint is reached)
314 void navigation_poproute(entity this)
316 //print("bot ", etos(this), " pop\n");
317 this.goalcurrent = this.goalstack01;
318 this.goalstack01 = this.goalstack02;
319 this.goalstack02 = this.goalstack03;
320 this.goalstack03 = this.goalstack04;
321 this.goalstack04 = this.goalstack05;
322 this.goalstack05 = this.goalstack06;
323 this.goalstack06 = this.goalstack07;
324 this.goalstack07 = this.goalstack08;
325 this.goalstack08 = this.goalstack09;
326 this.goalstack09 = this.goalstack10;
327 this.goalstack10 = this.goalstack11;
328 this.goalstack11 = this.goalstack12;
329 this.goalstack12 = this.goalstack13;
330 this.goalstack13 = this.goalstack14;
331 this.goalstack14 = this.goalstack15;
332 this.goalstack15 = this.goalstack16;
333 this.goalstack16 = this.goalstack17;
334 this.goalstack17 = this.goalstack18;
335 this.goalstack18 = this.goalstack19;
336 this.goalstack19 = this.goalstack20;
337 this.goalstack20 = this.goalstack21;
338 this.goalstack21 = this.goalstack22;
339 this.goalstack22 = this.goalstack23;
340 this.goalstack23 = this.goalstack24;
341 this.goalstack24 = this.goalstack25;
342 this.goalstack25 = this.goalstack26;
343 this.goalstack26 = this.goalstack27;
344 this.goalstack27 = this.goalstack28;
345 this.goalstack28 = this.goalstack29;
346 this.goalstack29 = this.goalstack30;
347 this.goalstack30 = this.goalstack31;
348 this.goalstack31 = NULL;
351 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
354 dist = vlen(v - org);
357 traceline(v, org, true, ent);
358 if (trace_fraction == 1)
362 if (tracewalk(ent, v, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), org, bot_navigation_movemode))
367 if (tracewalk(ent, org, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, bot_navigation_movemode))
375 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
376 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
378 vector pm1 = ent.origin + ent.mins;
379 vector pm2 = ent.origin + ent.maxs;
381 // do two scans, because box test is cheaper
382 IL_EACH(g_waypoints, it != ent && it != except,
384 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
388 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
389 org.z = ent.origin.z + ent.mins.z - STAT(PL_MIN, NULL).z; // player height
390 // TODO possibly make other code have the same support for bboxes
392 org = org + ent.tag_entity.origin;
393 if (navigation_testtracewalk)
399 // box check failed, try walk
400 IL_EACH(g_waypoints, it != ent,
404 vector wm1 = it.origin + it.mins;
405 vector wm2 = it.origin + it.maxs;
406 v.x = bound(wm1_x, org.x, wm2_x);
407 v.y = bound(wm1_y, org.y, wm2_y);
408 v.z = bound(wm1_z, org.z, wm2_z);
412 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
414 bestdist = vlen(v - org);
420 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
422 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
423 if (autocvar_g_waypointeditor_auto)
425 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
427 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
432 // finds the waypoints near the bot initiating a navigation query
433 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
436 // navigation_testtracewalk = true;
438 IL_EACH(g_waypoints, !it.wpconsidered,
442 m1 = it.origin + it.mins;
443 m2 = it.origin + it.maxs;
445 v.x = bound(m1_x, v.x, m2_x);
446 v.y = bound(m1_y, v.y, m2_y);
447 v.z = bound(m1_z, v.z, m2_z);
451 vector diff = v - this.origin;
452 diff.z = max(0, diff.z);
453 if(vdist(diff, <, maxdist))
455 it.wpconsidered = true;
456 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
458 it.wpnearestpoint = v;
459 it.wpcost = vlen(v - this.origin) + it.dmg;
466 //navigation_testtracewalk = false;
470 // updates a path link if a spawnfunc_waypoint link is better than the current one
471 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
480 v.x = bound(m1_x, p.x, m2_x);
481 v.y = bound(m1_y, p.y, m2_y);
482 v.z = bound(m1_z, p.z, m2_z);
486 cost2 = cost2 + vlen(v - p);
487 if (wp.wpcost > cost2)
492 wp.wpnearestpoint = v;
496 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
497 void navigation_markroutes(entity this, entity fixed_source_waypoint)
502 IL_EACH(g_waypoints, true,
504 it.wpconsidered = false;
505 it.wpnearestpoint = '0 0 0';
506 it.wpcost = 10000000;
511 if(fixed_source_waypoint)
513 fixed_source_waypoint.wpconsidered = true;
514 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
515 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
516 fixed_source_waypoint.wpfire = 1;
517 fixed_source_waypoint.enemy = NULL;
521 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
522 // as this search is expensive we will use lower values if the bot is on the air
523 float increment, maxdistance;
524 if(IS_ONGROUND(this))
535 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
538 bool searching = true;
542 IL_EACH(g_waypoints, it.wpfire,
547 p = it.wpnearestpoint;
549 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
550 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
551 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
552 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
553 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
554 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
555 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
556 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
557 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
558 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
559 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
560 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
561 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
562 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
563 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
564 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
565 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
566 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
567 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
568 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
569 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
570 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
571 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
572 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
573 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
574 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
575 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
576 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
577 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
578 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
579 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
580 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
581 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
586 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
587 void navigation_markroutes_inverted(entity fixed_source_waypoint)
591 IL_EACH(g_waypoints, true,
593 it.wpconsidered = false;
594 it.wpnearestpoint = '0 0 0';
595 it.wpcost = 10000000;
600 if(fixed_source_waypoint)
602 fixed_source_waypoint.wpconsidered = true;
603 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
604 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
605 fixed_source_waypoint.wpfire = 1;
606 fixed_source_waypoint.enemy = NULL;
610 error("need to start with a waypoint\n");
613 bool searching = true;
617 IL_EACH(g_waypoints, it.wpfire,
621 cost = it.wpcost; // cost to walk from it to home
622 p = it.wpnearestpoint;
624 IL_EACH(g_waypoints, true,
626 if(wp != it.wp00) if(wp != it.wp01) if(wp != it.wp02) if(wp != it.wp03)
627 if(wp != it.wp04) if(wp != it.wp05) if(wp != it.wp06) if(wp != it.wp07)
628 if(wp != it.wp08) if(wp != it.wp09) if(wp != it.wp10) if(wp != it.wp11)
629 if(wp != it.wp12) if(wp != it.wp13) if(wp != it.wp14) if(wp != it.wp15)
630 if(wp != it.wp16) if(wp != it.wp17) if(wp != it.wp18) if(wp != it.wp19)
631 if(wp != it.wp20) if(wp != it.wp21) if(wp != it.wp22) if(wp != it.wp23)
632 if(wp != it.wp24) if(wp != it.wp25) if(wp != it.wp26) if(wp != it.wp27)
633 if(wp != it.wp28) if(wp != it.wp29) if(wp != it.wp30) if(wp != it.wp31)
635 cost2 = cost + it.dmg;
636 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
642 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
643 void navigation_routerating(entity this, entity e, float f, float rangebias)
653 o = (e.absmin + e.absmax) * 0.5;
655 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
657 // Evaluate path using jetpack
659 if(this.items & IT_JETPACK)
660 if(autocvar_bot_ai_navigation_jetpack)
661 if(vdist(this.origin - o, >, autocvar_bot_ai_navigation_jetpack_mindistance))
663 vector pointa, pointb;
665 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
668 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
669 pointa = trace_endpos - '0 0 1';
672 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
673 pointb = trace_endpos - '0 0 1';
675 // Can I see these two points from the sky?
676 traceline(pointa, pointb, MOVE_NORMAL, this);
678 if(trace_fraction==1)
680 LOG_DEBUG("jetpack ai: can bridge these two points");
682 // Lower the altitude of these points as much as possible
683 float zdistance, xydistance, cost, t, fuel;
684 vector down, npa, npb;
686 down = '0 0 -1' * (STAT(PL_MAX, NULL).z - STAT(PL_MIN, NULL).z) * 10;
692 if(npa.z<=this.absmax.z)
695 if(npb.z<=e.absmax.z)
698 traceline(npa, npb, MOVE_NORMAL, this);
699 if(trace_fraction==1)
705 while(trace_fraction == 1);
708 // Rough estimation of fuel consumption
709 // (ignores acceleration and current xyz velocity)
710 xydistance = vlen(pointa - pointb);
711 zdistance = fabs(pointa.z - this.origin.z);
713 t = zdistance / autocvar_g_jetpack_maxspeed_up;
714 t += xydistance / autocvar_g_jetpack_maxspeed_side;
715 fuel = t * autocvar_g_jetpack_fuel * 0.8;
717 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
720 if(this.ammo_fuel>fuel)
723 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
724 // - between air and ground speeds)
726 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
727 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
730 // Compare against other goals
731 f = f * rangebias / (rangebias + cost);
733 if (navigation_bestrating < f)
735 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
736 navigation_bestrating = f;
737 navigation_bestgoal = e;
738 this.navigation_jetpack_goal = e;
739 this.navigation_jetpack_point = pointb;
746 //te_wizspike(e.origin);
749 // update the cached spawnfunc_waypoint link on a dynamic item entity
750 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
760 if(e.flags & FL_ITEM)
762 if (!(e.flags & FL_WEAPON))
763 if(e.nearestwaypoint)
766 else if (e.flags & FL_WEAPON)
768 if(e.classname != "droppedweapon")
769 if(e.nearestwaypoint)
774 if (time > e.nearestwaypointtimeout)
776 nwp = navigation_findnearestwaypoint(e, true);
778 e.nearestwaypoint = nwp;
781 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
783 if(e.flags & FL_ITEM)
784 e.blacklisted = true;
785 else if (e.flags & FL_WEAPON)
787 if(e.classname != "droppedweapon")
788 e.blacklisted = true;
793 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
798 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
799 if(e.classname=="item_flag_team")
800 e.nearestwaypointtimeout = time + 2;
802 e.nearestwaypointtimeout = time + random() * 3 + 5;
804 nwp = e.nearestwaypoint;
807 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
809 if (nwp.wpcost < 10000000)
811 //te_wizspike(nwp.wpnearestpoint);
812 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
813 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
814 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
815 if (navigation_bestrating < f)
817 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
818 navigation_bestrating = f;
819 navigation_bestgoal = e;
824 // adds an item to the the goal stack with the path to a given item
825 bool navigation_routetogoal(entity this, entity e, vector startposition)
829 // if there is no goal, just exit
833 this.navigation_hasgoals = true;
835 // put the entity on the goal stack
836 //print("routetogoal ", etos(e), "\n");
837 navigation_pushroute(this, e);
840 if(e==this.navigation_jetpack_goal)
843 // if it can reach the goal there is nothing more to do
844 if (tracewalk(this, startposition, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
847 // see if there are waypoints describing a path to the item
848 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
849 e = e.nearestwaypoint;
851 e = e.enemy; // we already have added it, so...
858 // add the spawnfunc_waypoint to the path
859 navigation_pushroute(this, e);
869 // removes any currently touching waypoints from the goal stack
870 // (this is how bots detect if they reached a goal)
871 void navigation_poptouchedgoals(entity this)
875 m1 = org + this.mins;
876 m2 = org + this.maxs;
878 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
880 if(this.lastteleporttime>0)
881 if(time-this.lastteleporttime<(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
883 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
884 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
886 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
887 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
889 navigation_poproute(this);
894 // If for some reason the bot is closer to the next goal, pop the current one
896 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
897 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
898 if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
900 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
901 navigation_poproute(this);
902 // TODO this may also be a nice idea to do "early" (e.g. by
903 // manipulating the vlen() comparisons) to shorten paths in
904 // general - this would make bots walk more "on rails" than
905 // "zigzagging" which they currently do with sufficiently
906 // random-like waypoints, and thus can make a nice bot
907 // personality property
910 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
911 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
912 if(IS_PLAYER(this.goalcurrent))
913 navigation_poproute(this);
915 // aid for detecting jump pads better (distance based check fails sometimes)
916 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && this.jumppadcount > 0 )
917 navigation_poproute(this);
919 // Loose goal touching check when running
920 if(this.aistatus & AI_STATUS_RUNNING)
921 if(this.speed >= autocvar_sv_maxspeed) // if -really- running
922 if(this.goalcurrent.classname=="waypoint")
924 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
926 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
927 if(trace_fraction==1)
929 // Detect personal waypoints
930 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
931 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
933 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
934 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
937 navigation_poproute(this);
942 while (this.goalcurrent && boxesoverlap(m1, m2, this.goalcurrent.absmin, this.goalcurrent.absmax))
944 // Detect personal waypoints
945 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
946 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
948 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
949 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
952 navigation_poproute(this);
956 // begin a goal selection session (queries spawnfunc_waypoint network)
957 void navigation_goalrating_start(entity this)
959 if(this.aistatus & AI_STATUS_STUCK)
962 this.navigation_jetpack_goal = NULL;
963 navigation_bestrating = -1;
964 this.navigation_hasgoals = false;
965 navigation_clearroute(this);
966 navigation_bestgoal = NULL;
967 navigation_markroutes(this, NULL);
970 // ends a goal selection session (updates goal stack to the best goal)
971 void navigation_goalrating_end(entity this)
973 if(this.aistatus & AI_STATUS_STUCK)
976 navigation_routetogoal(this, navigation_bestgoal, this.origin);
977 LOG_DEBUG("best goal ", this.goalcurrent.classname);
979 // If the bot got stuck then try to reach the farthest waypoint
980 if (!this.navigation_hasgoals)
981 if (autocvar_bot_wander_enable)
983 if (!(this.aistatus & AI_STATUS_STUCK))
985 LOG_DEBUG(this.netname, " cannot walk to any goal");
986 this.aistatus |= AI_STATUS_STUCK;
989 this.navigation_hasgoals = false; // Reset this value
993 void botframe_updatedangerousobjects(float maxupdate)
998 IL_EACH(g_waypoints, true,
1003 IL_EACH(g_bot_dodge, it.bot_dodge,
1006 v.x = bound(m1_x, v.x, m2_x);
1007 v.y = bound(m1_y, v.y, m2_y);
1008 v.z = bound(m1_z, v.z, m2_z);
1009 o = (it.absmin + it.absmax) * 0.5;
1010 d = it.bot_dodgerating - vlen(o - v);
1013 traceline(o, v, true, NULL);
1014 if (trace_fraction == 1)
1015 danger = danger + d;
1025 void navigation_unstuck(entity this)
1027 float search_radius = 1000;
1029 if (!autocvar_bot_wander_enable)
1032 if (!bot_waypoint_queue_owner)
1034 LOG_DEBUG(this.netname, " sutck, taking over the waypoints queue");
1035 bot_waypoint_queue_owner = this;
1036 bot_waypoint_queue_bestgoal = NULL;
1037 bot_waypoint_queue_bestgoalrating = 0;
1040 if(bot_waypoint_queue_owner!=this)
1043 if (bot_waypoint_queue_goal)
1045 // evaluate the next goal on the queue
1046 float d = vlen(this.origin - bot_waypoint_queue_goal.origin);
1047 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1048 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1050 if( d > bot_waypoint_queue_bestgoalrating)
1052 bot_waypoint_queue_bestgoalrating = d;
1053 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1056 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1058 if (!bot_waypoint_queue_goal)
1060 if (bot_waypoint_queue_bestgoal)
1062 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1063 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1064 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1065 this.aistatus &= ~AI_STATUS_STUCK;
1069 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1072 bot_waypoint_queue_owner = NULL;
1077 if(bot_strategytoken!=this)
1080 // build a new queue
1081 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1083 entity first = NULL;
1085 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1087 if(bot_waypoint_queue_goal)
1088 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1092 bot_waypoint_queue_goal = it;
1093 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1097 bot_waypoint_queue_goal = first;
1100 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1101 bot_waypoint_queue_owner = NULL;
1106 // Support for debugging tracewalk visually
1108 void debugresetnodes()
1110 debuglastnode = '0 0 0';
1113 void debugnode(entity this, vector node)
1115 if (!IS_PLAYER(this))
1118 if(debuglastnode=='0 0 0')
1120 debuglastnode = node;
1124 te_lightning2(NULL, node, debuglastnode);
1125 debuglastnode = node;
1128 void debugnodestatus(vector position, float status)
1134 case DEBUG_NODE_SUCCESS:
1137 case DEBUG_NODE_WARNING:
1140 case DEBUG_NODE_FAIL:
1147 te_customflash(position, 40, 2, c);
1150 // Support for debugging the goal stack visually
1153 .vector lastposition;
1155 // Debug the goal stack visually
1156 void debuggoalstack(entity this)
1161 if(this.goalcounter==0)goal=this.goalcurrent;
1162 else if(this.goalcounter==1)goal=this.goalstack01;
1163 else if(this.goalcounter==2)goal=this.goalstack02;
1164 else if(this.goalcounter==3)goal=this.goalstack03;
1165 else if(this.goalcounter==4)goal=this.goalstack04;
1166 else if(this.goalcounter==5)goal=this.goalstack05;
1167 else if(this.goalcounter==6)goal=this.goalstack06;
1168 else if(this.goalcounter==7)goal=this.goalstack07;
1169 else if(this.goalcounter==8)goal=this.goalstack08;
1170 else if(this.goalcounter==9)goal=this.goalstack09;
1171 else if(this.goalcounter==10)goal=this.goalstack10;
1172 else if(this.goalcounter==11)goal=this.goalstack11;
1173 else if(this.goalcounter==12)goal=this.goalstack12;
1174 else if(this.goalcounter==13)goal=this.goalstack13;
1175 else if(this.goalcounter==14)goal=this.goalstack14;
1176 else if(this.goalcounter==15)goal=this.goalstack15;
1177 else if(this.goalcounter==16)goal=this.goalstack16;
1178 else if(this.goalcounter==17)goal=this.goalstack17;
1179 else if(this.goalcounter==18)goal=this.goalstack18;
1180 else if(this.goalcounter==19)goal=this.goalstack19;
1181 else if(this.goalcounter==20)goal=this.goalstack20;
1182 else if(this.goalcounter==21)goal=this.goalstack21;
1183 else if(this.goalcounter==22)goal=this.goalstack22;
1184 else if(this.goalcounter==23)goal=this.goalstack23;
1185 else if(this.goalcounter==24)goal=this.goalstack24;
1186 else if(this.goalcounter==25)goal=this.goalstack25;
1187 else if(this.goalcounter==26)goal=this.goalstack26;
1188 else if(this.goalcounter==27)goal=this.goalstack27;
1189 else if(this.goalcounter==28)goal=this.goalstack28;
1190 else if(this.goalcounter==29)goal=this.goalstack29;
1191 else if(this.goalcounter==30)goal=this.goalstack30;
1192 else if(this.goalcounter==31)goal=this.goalstack31;
1197 this.goalcounter = 0;
1198 this.lastposition='0 0 0';
1202 if(this.lastposition=='0 0 0')
1205 org = this.lastposition;
1208 go = ( goal.absmin + goal.absmax ) * 0.5;
1209 te_lightning2(NULL, org, go);
1210 this.lastposition = go;