1 #include "navigation.qh"
5 #include "waypoints.qh"
7 #include "../t_items.qh"
9 #include "../../common/constants.qh"
11 void bot_debug(string input)
13 switch(autocvar_bot_debug)
15 case 1: dprint(input); break;
16 case 2: print(input); break;
20 // rough simulation of walking from one point to another to test if a path
21 // can be traveled, used for waypoint linking and havocbot
23 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
35 if(autocvar_bot_debug_tracewalk)
44 dist = totaldist = vlen(move);
45 dir = normalize(move);
47 ignorehazards = false;
50 // Analyze starting point
51 traceline(start, start, MOVE_NORMAL, e);
52 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
56 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
57 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
63 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
67 if(autocvar_bot_debug_tracewalk)
68 debugnodestatus(start, DEBUG_NODE_FAIL);
70 //print("tracewalk: ", vtos(start), " is a bad start\n");
79 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
82 if(autocvar_bot_debug_tracewalk)
83 debugnodestatus(org, DEBUG_NODE_SUCCESS);
85 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
88 if(autocvar_bot_debug_tracewalk)
95 dist = dist - stepdist;
96 traceline(org, org, MOVE_NORMAL, e);
99 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
101 // hazards blocking path
102 if(autocvar_bot_debug_tracewalk)
103 debugnodestatus(org, DEBUG_NODE_FAIL);
105 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
109 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
111 move = normalize(end - org);
112 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
114 if(autocvar_bot_debug_tracewalk)
115 debugnode(trace_endpos);
117 if (trace_fraction < 1)
120 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
121 for (; org.z < end.z + self.maxs.z; org.z += stepdist)
123 if(autocvar_bot_debug_tracewalk)
126 if(pointcontents(org) == CONTENT_EMPTY)
130 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
132 if(autocvar_bot_debug_tracewalk)
133 debugnodestatus(org, DEBUG_NODE_FAIL);
136 //print("tracewalk: ", vtos(start), " failed under water\n");
146 move = dir * stepdist + org;
147 tracebox(org, m1, m2, move, movemode, e);
149 if(autocvar_bot_debug_tracewalk)
150 debugnode(trace_endpos);
153 if (trace_fraction < 1)
155 // check if we can walk over this obstacle, possibly by jumpstepping
156 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
157 if (trace_fraction < 1 || trace_startsolid)
159 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
160 if (trace_fraction < 1 || trace_startsolid)
162 if(autocvar_bot_debug_tracewalk)
163 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
166 traceline( org, move, movemode, e);
167 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
171 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
173 nextmove = move + (dir * stepdist);
174 traceline( move, nextmove, movemode, e);
180 if(autocvar_bot_debug_tracewalk)
181 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
183 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
184 //te_explosion(trace_endpos);
185 //print(ftos(e.dphitcontentsmask), "\n");
186 return false; // failed
198 // trace down from stepheight as far as possible and move there,
199 // if this starts in solid we try again without the stepup, and
200 // if that also fails we assume it is a wall
201 // (this is the same logic as the Quake walkmove function used)
202 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
204 // moved successfully
208 c = pointcontents(org + '0 0 1');
209 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
219 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
221 // moved but didn't arrive at the intended destination
222 if(autocvar_bot_debug_tracewalk)
223 debugnodestatus(org, DEBUG_NODE_FAIL);
228 /////////////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////////////
232 // completely empty the goal stack, used when deciding where to go
233 void navigation_clearroute()
235 //print("bot ", etos(self), " clear\n");
236 self.navigation_hasgoals = false;
237 self.goalcurrent = world;
238 self.goalstack01 = world;
239 self.goalstack02 = world;
240 self.goalstack03 = world;
241 self.goalstack04 = world;
242 self.goalstack05 = world;
243 self.goalstack06 = world;
244 self.goalstack07 = world;
245 self.goalstack08 = world;
246 self.goalstack09 = world;
247 self.goalstack10 = world;
248 self.goalstack11 = world;
249 self.goalstack12 = world;
250 self.goalstack13 = world;
251 self.goalstack14 = world;
252 self.goalstack15 = world;
253 self.goalstack16 = world;
254 self.goalstack17 = world;
255 self.goalstack18 = world;
256 self.goalstack19 = world;
257 self.goalstack20 = world;
258 self.goalstack21 = world;
259 self.goalstack22 = world;
260 self.goalstack23 = world;
261 self.goalstack24 = world;
262 self.goalstack25 = world;
263 self.goalstack26 = world;
264 self.goalstack27 = world;
265 self.goalstack28 = world;
266 self.goalstack29 = world;
267 self.goalstack30 = world;
268 self.goalstack31 = world;
271 // add a new goal at the beginning of the stack
272 // (in other words: add a new prerequisite before going to the later goals)
273 // NOTE: when a waypoint is added, the WP gets pushed first, then the
274 // next-closest WP on the shortest path to the WP
275 // That means, if the stack overflows, the bot will know how to do the FIRST 32
276 // steps to the goal, and then recalculate the path.
277 void navigation_pushroute(entity e)
279 //print("bot ", etos(self), " push ", etos(e), "\n");
280 self.goalstack31 = self.goalstack30;
281 self.goalstack30 = self.goalstack29;
282 self.goalstack29 = self.goalstack28;
283 self.goalstack28 = self.goalstack27;
284 self.goalstack27 = self.goalstack26;
285 self.goalstack26 = self.goalstack25;
286 self.goalstack25 = self.goalstack24;
287 self.goalstack24 = self.goalstack23;
288 self.goalstack23 = self.goalstack22;
289 self.goalstack22 = self.goalstack21;
290 self.goalstack21 = self.goalstack20;
291 self.goalstack20 = self.goalstack19;
292 self.goalstack19 = self.goalstack18;
293 self.goalstack18 = self.goalstack17;
294 self.goalstack17 = self.goalstack16;
295 self.goalstack16 = self.goalstack15;
296 self.goalstack15 = self.goalstack14;
297 self.goalstack14 = self.goalstack13;
298 self.goalstack13 = self.goalstack12;
299 self.goalstack12 = self.goalstack11;
300 self.goalstack11 = self.goalstack10;
301 self.goalstack10 = self.goalstack09;
302 self.goalstack09 = self.goalstack08;
303 self.goalstack08 = self.goalstack07;
304 self.goalstack07 = self.goalstack06;
305 self.goalstack06 = self.goalstack05;
306 self.goalstack05 = self.goalstack04;
307 self.goalstack04 = self.goalstack03;
308 self.goalstack03 = self.goalstack02;
309 self.goalstack02 = self.goalstack01;
310 self.goalstack01 = self.goalcurrent;
311 self.goalcurrent = e;
314 // remove first goal from stack
315 // (in other words: remove a prerequisite for reaching the later goals)
316 // (used when a spawnfunc_waypoint is reached)
317 void navigation_poproute()
319 //print("bot ", etos(self), " pop\n");
320 self.goalcurrent = self.goalstack01;
321 self.goalstack01 = self.goalstack02;
322 self.goalstack02 = self.goalstack03;
323 self.goalstack03 = self.goalstack04;
324 self.goalstack04 = self.goalstack05;
325 self.goalstack05 = self.goalstack06;
326 self.goalstack06 = self.goalstack07;
327 self.goalstack07 = self.goalstack08;
328 self.goalstack08 = self.goalstack09;
329 self.goalstack09 = self.goalstack10;
330 self.goalstack10 = self.goalstack11;
331 self.goalstack11 = self.goalstack12;
332 self.goalstack12 = self.goalstack13;
333 self.goalstack13 = self.goalstack14;
334 self.goalstack14 = self.goalstack15;
335 self.goalstack15 = self.goalstack16;
336 self.goalstack16 = self.goalstack17;
337 self.goalstack17 = self.goalstack18;
338 self.goalstack18 = self.goalstack19;
339 self.goalstack19 = self.goalstack20;
340 self.goalstack20 = self.goalstack21;
341 self.goalstack21 = self.goalstack22;
342 self.goalstack22 = self.goalstack23;
343 self.goalstack23 = self.goalstack24;
344 self.goalstack24 = self.goalstack25;
345 self.goalstack25 = self.goalstack26;
346 self.goalstack26 = self.goalstack27;
347 self.goalstack27 = self.goalstack28;
348 self.goalstack28 = self.goalstack29;
349 self.goalstack29 = self.goalstack30;
350 self.goalstack30 = self.goalstack31;
351 self.goalstack31 = world;
354 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
357 dist = vlen(v - org);
360 traceline(v, org, true, ent);
361 if (trace_fraction == 1)
365 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
370 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
378 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
379 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
381 entity waylist, w, best;
382 vector v, org, pm1, pm2;
384 pm1 = ent.origin + ent.mins;
385 pm2 = ent.origin + ent.maxs;
386 waylist = findchain(classname, "waypoint");
388 // do two scans, because box test is cheaper
392 // if object is touching spawnfunc_waypoint
393 if(w != ent && w != except)
394 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
399 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
400 org.z = ent.origin.z + ent.mins.z - PL_MIN_z; // player height
401 // TODO possibly make other code have the same support for bboxes
403 org = org + ent.tag_entity.origin;
404 if (navigation_testtracewalk)
409 // box check failed, try walk
413 // if object can walk from spawnfunc_waypoint
419 wm1 = w.origin + w.mins;
420 wm2 = w.origin + w.maxs;
421 v.x = bound(wm1_x, org.x, wm2_x);
422 v.y = bound(wm1_y, org.y, wm2_y);
423 v.z = bound(wm1_z, org.z, wm2_z);
427 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
429 bestdist = vlen(v - org);
437 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
439 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, world);
440 if (autocvar_g_waypointeditor_auto)
442 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
444 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
449 // finds the waypoints near the bot initiating a navigation query
450 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
453 vector v, m1, m2, diff;
455 // navigation_testtracewalk = true;
460 if (!head.wpconsidered)
464 m1 = head.origin + head.mins;
465 m2 = head.origin + head.maxs;
467 v.x = bound(m1_x, v.x, m2_x);
468 v.y = bound(m1_y, v.y, m2_y);
469 v.z = bound(m1_z, v.z, m2_z);
473 diff = v - self.origin;
474 diff.z = max(0, diff.z);
475 if (vlen(diff) < maxdist)
477 head.wpconsidered = true;
478 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
480 head.wpnearestpoint = v;
481 head.wpcost = vlen(v - self.origin) + head.dmg;
490 //navigation_testtracewalk = false;
494 // updates a path link if a spawnfunc_waypoint link is better than the current one
495 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
504 v.x = bound(m1_x, p.x, m2_x);
505 v.y = bound(m1_y, p.y, m2_y);
506 v.z = bound(m1_z, p.z, m2_z);
510 cost2 = cost2 + vlen(v - p);
511 if (wp.wpcost > cost2)
516 wp.wpnearestpoint = v;
520 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
521 void navigation_markroutes(entity fixed_source_waypoint)
523 entity w, wp, waylist;
524 float searching, cost, cost2;
526 w = waylist = findchain(classname, "waypoint");
529 w.wpconsidered = false;
530 w.wpnearestpoint = '0 0 0';
537 if(fixed_source_waypoint)
539 fixed_source_waypoint.wpconsidered = true;
540 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
541 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
542 fixed_source_waypoint.wpfire = 1;
543 fixed_source_waypoint.enemy = world;
547 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
548 // as this search is expensive we will use lower values if the bot is on the air
549 float i, increment, maxdistance;
550 if(self.flags & FL_ONGROUND)
561 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
576 p = w.wpnearestpoint;
577 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
600 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
601 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
602 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
603 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
604 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
605 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
606 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
607 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
608 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
609 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
616 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
617 void navigation_markroutes_inverted(entity fixed_source_waypoint)
619 entity w, wp, waylist;
620 float searching, cost, cost2;
622 w = waylist = findchain(classname, "waypoint");
625 w.wpconsidered = false;
626 w.wpnearestpoint = '0 0 0';
633 if(fixed_source_waypoint)
635 fixed_source_waypoint.wpconsidered = true;
636 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
637 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
638 fixed_source_waypoint.wpfire = 1;
639 fixed_source_waypoint.enemy = world;
643 error("need to start with a waypoint\n");
657 cost = w.wpcost; // cost to walk from w to home
658 p = w.wpnearestpoint;
659 for(wp = waylist; wp; wp = wp.chain)
661 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
662 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
663 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
664 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
665 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
666 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
667 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
668 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
670 cost2 = cost + wp.dmg;
671 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
679 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
680 void navigation_routerating(entity e, float f, float rangebias)
690 o = (e.absmin + e.absmax) * 0.5;
692 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
694 // Evaluate path using jetpack
696 if(self.items & IT_JETPACK)
697 if(autocvar_bot_ai_navigation_jetpack)
698 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
700 vector pointa, pointb;
702 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
705 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
706 pointa = trace_endpos - '0 0 1';
709 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
710 pointb = trace_endpos - '0 0 1';
712 // Can I see these two points from the sky?
713 traceline(pointa, pointb, MOVE_NORMAL, self);
715 if(trace_fraction==1)
717 bot_debug("jetpack ai: can bridge these two points\n");
719 // Lower the altitude of these points as much as possible
720 float zdistance, xydistance, cost, t, fuel;
721 vector down, npa, npb;
723 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
729 if(npa.z<=self.absmax.z)
732 if(npb.z<=e.absmax.z)
735 traceline(npa, npb, MOVE_NORMAL, self);
736 if(trace_fraction==1)
742 while(trace_fraction == 1);
745 // Rough estimation of fuel consumption
746 // (ignores acceleration and current xyz velocity)
747 xydistance = vlen(pointa - pointb);
748 zdistance = fabs(pointa.z - self.origin.z);
750 t = zdistance / autocvar_g_jetpack_maxspeed_up;
751 t += xydistance / autocvar_g_jetpack_maxspeed_side;
752 fuel = t * autocvar_g_jetpack_fuel * 0.8;
754 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
757 if(self.ammo_fuel>fuel)
760 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
761 // - between air and ground speeds)
763 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
764 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
767 // Compare against other goals
768 f = f * rangebias / (rangebias + cost);
770 if (navigation_bestrating < f)
772 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
773 navigation_bestrating = f;
774 navigation_bestgoal = e;
775 self.navigation_jetpack_goal = e;
776 self.navigation_jetpack_point = pointb;
783 //te_wizspike(e.origin);
786 // update the cached spawnfunc_waypoint link on a dynamic item entity
787 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
797 if(e.flags & FL_ITEM)
799 if (!(e.flags & FL_WEAPON))
800 if(e.nearestwaypoint)
803 else if (e.flags & FL_WEAPON)
805 if(e.classname != "droppedweapon")
806 if(e.nearestwaypoint)
811 if (time > e.nearestwaypointtimeout)
813 nwp = navigation_findnearestwaypoint(e, true);
815 e.nearestwaypoint = nwp;
818 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
820 if(e.flags & FL_ITEM)
821 e.blacklisted = true;
822 else if (e.flags & FL_WEAPON)
824 if(e.classname != "droppedweapon")
825 e.blacklisted = true;
830 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
835 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
836 if(e.classname=="item_flag_team")
837 e.nearestwaypointtimeout = time + 2;
839 e.nearestwaypointtimeout = time + random() * 3 + 5;
841 nwp = e.nearestwaypoint;
844 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
846 if (nwp.wpcost < 10000000)
848 //te_wizspike(nwp.wpnearestpoint);
849 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
850 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
851 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
852 if (navigation_bestrating < f)
854 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
855 navigation_bestrating = f;
856 navigation_bestgoal = e;
861 // adds an item to the the goal stack with the path to a given item
862 float navigation_routetogoal(entity e, vector startposition)
866 // if there is no goal, just exit
870 self.navigation_hasgoals = true;
872 // put the entity on the goal stack
873 //print("routetogoal ", etos(e), "\n");
874 navigation_pushroute(e);
877 if(e==self.navigation_jetpack_goal)
880 // if it can reach the goal there is nothing more to do
881 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
884 // see if there are waypoints describing a path to the item
885 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
886 e = e.nearestwaypoint;
888 e = e.enemy; // we already have added it, so...
895 // add the spawnfunc_waypoint to the path
896 navigation_pushroute(e);
906 // removes any currently touching waypoints from the goal stack
907 // (this is how bots detect if they reached a goal)
908 void navigation_poptouchedgoals()
912 m1 = org + self.mins;
913 m2 = org + self.maxs;
915 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
917 if(self.lastteleporttime>0)
918 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
920 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
921 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
923 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
924 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
926 navigation_poproute();
931 // If for some reason the bot is closer to the next goal, pop the current one
933 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
934 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
935 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
937 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
938 navigation_poproute();
939 // TODO this may also be a nice idea to do "early" (e.g. by
940 // manipulating the vlen() comparisons) to shorten paths in
941 // general - this would make bots walk more "on rails" than
942 // "zigzagging" which they currently do with sufficiently
943 // random-like waypoints, and thus can make a nice bot
944 // personality property
947 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
948 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
949 if(IS_PLAYER(self.goalcurrent))
950 navigation_poproute();
952 // aid for detecting jump pads better (distance based check fails sometimes)
953 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
954 navigation_poproute();
956 // Loose goal touching check when running
957 if(self.aistatus & AI_STATUS_RUNNING)
958 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
959 if(self.goalcurrent.classname=="waypoint")
961 if(vlen(self.origin - self.goalcurrent.origin)<150)
963 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, true, world);
964 if(trace_fraction==1)
966 // Detect personal waypoints
967 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
968 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
970 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
971 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
974 navigation_poproute();
979 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
981 // Detect personal waypoints
982 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
983 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
985 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
986 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
989 navigation_poproute();
993 // begin a goal selection session (queries spawnfunc_waypoint network)
994 void navigation_goalrating_start()
996 if(self.aistatus & AI_STATUS_STUCK)
999 self.navigation_jetpack_goal = world;
1000 navigation_bestrating = -1;
1001 self.navigation_hasgoals = false;
1002 navigation_clearroute();
1003 navigation_bestgoal = world;
1004 navigation_markroutes(world);
1007 // ends a goal selection session (updates goal stack to the best goal)
1008 void navigation_goalrating_end()
1010 if(self.aistatus & AI_STATUS_STUCK)
1013 navigation_routetogoal(navigation_bestgoal, self.origin);
1014 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
1016 // If the bot got stuck then try to reach the farthest waypoint
1017 if (!self.navigation_hasgoals)
1018 if (autocvar_bot_wander_enable)
1020 if (!(self.aistatus & AI_STATUS_STUCK))
1022 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1023 self.aistatus |= AI_STATUS_STUCK;
1026 self.navigation_hasgoals = false; // Reset this value
1030 void botframe_updatedangerousobjects(float maxupdate)
1032 entity head, bot_dodgelist;
1033 vector m1, m2, v, o;
1036 bot_dodgelist = findchainfloat(bot_dodge, true);
1037 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1038 while (botframe_dangerwaypoint != world)
1041 m1 = botframe_dangerwaypoint.mins;
1042 m2 = botframe_dangerwaypoint.maxs;
1043 head = bot_dodgelist;
1047 v.x = bound(m1_x, v.x, m2_x);
1048 v.y = bound(m1_y, v.y, m2_y);
1049 v.z = bound(m1_z, v.z, m2_z);
1050 o = (head.absmin + head.absmax) * 0.5;
1051 d = head.bot_dodgerating - vlen(o - v);
1054 traceline(o, v, true, world);
1055 if (trace_fraction == 1)
1056 danger = danger + d;
1060 botframe_dangerwaypoint.dmg = danger;
1064 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1068 void navigation_unstuck()
1070 float search_radius = 1000;
1072 if (!autocvar_bot_wander_enable)
1075 if (!bot_waypoint_queue_owner)
1077 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1078 bot_waypoint_queue_owner = self;
1079 bot_waypoint_queue_bestgoal = world;
1080 bot_waypoint_queue_bestgoalrating = 0;
1083 if(bot_waypoint_queue_owner!=self)
1086 if (bot_waypoint_queue_goal)
1088 // evaluate the next goal on the queue
1089 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1090 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1091 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1093 if( d > bot_waypoint_queue_bestgoalrating)
1095 bot_waypoint_queue_bestgoalrating = d;
1096 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1099 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1101 if (!bot_waypoint_queue_goal)
1103 if (bot_waypoint_queue_bestgoal)
1105 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1106 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1107 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1108 self.aistatus &= ~AI_STATUS_STUCK;
1112 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1115 bot_waypoint_queue_owner = world;
1120 if(bot_strategytoken!=self)
1123 // build a new queue
1124 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1129 head = findradius(self.origin, search_radius);
1133 if(head.classname=="waypoint")
1134 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1136 if(bot_waypoint_queue_goal)
1137 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1141 bot_waypoint_queue_goal = head;
1142 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1149 bot_waypoint_queue_goal = first;
1152 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1153 bot_waypoint_queue_owner = world;
1158 // Support for debugging tracewalk visually
1160 void debugresetnodes()
1162 debuglastnode = '0 0 0';
1165 void debugnode(vector node)
1167 if (!IS_PLAYER(self))
1170 if(debuglastnode=='0 0 0')
1172 debuglastnode = node;
1176 te_lightning2(world, node, debuglastnode);
1177 debuglastnode = node;
1180 void debugnodestatus(vector position, float status)
1186 case DEBUG_NODE_SUCCESS:
1189 case DEBUG_NODE_WARNING:
1192 case DEBUG_NODE_FAIL:
1199 te_customflash(position, 40, 2, c);
1202 // Support for debugging the goal stack visually
1205 .vector lastposition;
1207 // Debug the goal stack visually
1208 void debuggoalstack()
1213 if(self.goalcounter==0)goal=self.goalcurrent;
1214 else if(self.goalcounter==1)goal=self.goalstack01;
1215 else if(self.goalcounter==2)goal=self.goalstack02;
1216 else if(self.goalcounter==3)goal=self.goalstack03;
1217 else if(self.goalcounter==4)goal=self.goalstack04;
1218 else if(self.goalcounter==5)goal=self.goalstack05;
1219 else if(self.goalcounter==6)goal=self.goalstack06;
1220 else if(self.goalcounter==7)goal=self.goalstack07;
1221 else if(self.goalcounter==8)goal=self.goalstack08;
1222 else if(self.goalcounter==9)goal=self.goalstack09;
1223 else if(self.goalcounter==10)goal=self.goalstack10;
1224 else if(self.goalcounter==11)goal=self.goalstack11;
1225 else if(self.goalcounter==12)goal=self.goalstack12;
1226 else if(self.goalcounter==13)goal=self.goalstack13;
1227 else if(self.goalcounter==14)goal=self.goalstack14;
1228 else if(self.goalcounter==15)goal=self.goalstack15;
1229 else if(self.goalcounter==16)goal=self.goalstack16;
1230 else if(self.goalcounter==17)goal=self.goalstack17;
1231 else if(self.goalcounter==18)goal=self.goalstack18;
1232 else if(self.goalcounter==19)goal=self.goalstack19;
1233 else if(self.goalcounter==20)goal=self.goalstack20;
1234 else if(self.goalcounter==21)goal=self.goalstack21;
1235 else if(self.goalcounter==22)goal=self.goalstack22;
1236 else if(self.goalcounter==23)goal=self.goalstack23;
1237 else if(self.goalcounter==24)goal=self.goalstack24;
1238 else if(self.goalcounter==25)goal=self.goalstack25;
1239 else if(self.goalcounter==26)goal=self.goalstack26;
1240 else if(self.goalcounter==27)goal=self.goalstack27;
1241 else if(self.goalcounter==28)goal=self.goalstack28;
1242 else if(self.goalcounter==29)goal=self.goalstack29;
1243 else if(self.goalcounter==30)goal=self.goalstack30;
1244 else if(self.goalcounter==31)goal=self.goalstack31;
1249 self.goalcounter = 0;
1250 self.lastposition='0 0 0';
1254 if(self.lastposition=='0 0 0')
1257 org = self.lastposition;
1260 go = ( goal.absmin + goal.absmax ) * 0.5;
1261 te_lightning2(world, org, go);
1262 self.lastposition = go;