1 #include "navigation.qh"
3 #include "../../../common/triggers/trigger/jumppads.qh"
5 void bot_debug(string input)
7 switch(autocvar_bot_debug)
9 case 1: LOG_TRACE(input); break;
10 case 2: LOG_INFO(input); break;
14 // rough simulation of walking from one point to another to test if a path
15 // can be traveled, used for waypoint linking and havocbot
17 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
29 if(autocvar_bot_debug_tracewalk)
38 dist = totaldist = vlen(move);
39 dir = normalize(move);
41 ignorehazards = false;
44 // Analyze starting point
45 traceline(start, start, MOVE_NORMAL, e);
46 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
50 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
51 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
57 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
61 if(autocvar_bot_debug_tracewalk)
62 debugnodestatus(start, DEBUG_NODE_FAIL);
64 //print("tracewalk: ", vtos(start), " is a bad start\n");
73 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
76 if(autocvar_bot_debug_tracewalk)
77 debugnodestatus(org, DEBUG_NODE_SUCCESS);
79 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
82 if(autocvar_bot_debug_tracewalk)
89 dist = dist - stepdist;
90 traceline(org, org, MOVE_NORMAL, e);
93 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
95 // hazards blocking path
96 if(autocvar_bot_debug_tracewalk)
97 debugnodestatus(org, DEBUG_NODE_FAIL);
99 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
103 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
105 move = normalize(end - org);
106 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
108 if(autocvar_bot_debug_tracewalk)
109 debugnode(trace_endpos);
111 if (trace_fraction < 1)
114 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
115 for (; org.z < end.z + self.maxs.z; org.z += stepdist)
117 if(autocvar_bot_debug_tracewalk)
120 if(pointcontents(org) == CONTENT_EMPTY)
124 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
126 if(autocvar_bot_debug_tracewalk)
127 debugnodestatus(org, DEBUG_NODE_FAIL);
130 //print("tracewalk: ", vtos(start), " failed under water\n");
140 move = dir * stepdist + org;
141 tracebox(org, m1, m2, move, movemode, e);
143 if(autocvar_bot_debug_tracewalk)
144 debugnode(trace_endpos);
147 if (trace_fraction < 1)
149 // check if we can walk over this obstacle, possibly by jumpstepping
150 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
151 if (trace_fraction < 1 || trace_startsolid)
153 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
154 if (trace_fraction < 1 || trace_startsolid)
156 if(autocvar_bot_debug_tracewalk)
157 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
160 traceline( org, move, movemode, e);
161 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
165 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
167 nextmove = move + (dir * stepdist);
168 traceline( move, nextmove, movemode, e);
174 if(autocvar_bot_debug_tracewalk)
175 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
177 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
178 //te_explosion(trace_endpos);
179 //print(ftos(e.dphitcontentsmask), "\n");
180 return false; // failed
192 // trace down from stepheight as far as possible and move there,
193 // if this starts in solid we try again without the stepup, and
194 // if that also fails we assume it is a wall
195 // (this is the same logic as the Quake walkmove function used)
196 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
198 // moved successfully
202 c = pointcontents(org + '0 0 1');
203 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
213 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
215 // moved but didn't arrive at the intended destination
216 if(autocvar_bot_debug_tracewalk)
217 debugnodestatus(org, DEBUG_NODE_FAIL);
222 /////////////////////////////////////////////////////////////////////////////
224 /////////////////////////////////////////////////////////////////////////////
226 // completely empty the goal stack, used when deciding where to go
227 void navigation_clearroute()
229 //print("bot ", etos(self), " clear\n");
230 self.navigation_hasgoals = false;
231 self.goalcurrent = world;
232 self.goalstack01 = world;
233 self.goalstack02 = world;
234 self.goalstack03 = world;
235 self.goalstack04 = world;
236 self.goalstack05 = world;
237 self.goalstack06 = world;
238 self.goalstack07 = world;
239 self.goalstack08 = world;
240 self.goalstack09 = world;
241 self.goalstack10 = world;
242 self.goalstack11 = world;
243 self.goalstack12 = world;
244 self.goalstack13 = world;
245 self.goalstack14 = world;
246 self.goalstack15 = world;
247 self.goalstack16 = world;
248 self.goalstack17 = world;
249 self.goalstack18 = world;
250 self.goalstack19 = world;
251 self.goalstack20 = world;
252 self.goalstack21 = world;
253 self.goalstack22 = world;
254 self.goalstack23 = world;
255 self.goalstack24 = world;
256 self.goalstack25 = world;
257 self.goalstack26 = world;
258 self.goalstack27 = world;
259 self.goalstack28 = world;
260 self.goalstack29 = world;
261 self.goalstack30 = world;
262 self.goalstack31 = world;
265 // add a new goal at the beginning of the stack
266 // (in other words: add a new prerequisite before going to the later goals)
267 // NOTE: when a waypoint is added, the WP gets pushed first, then the
268 // next-closest WP on the shortest path to the WP
269 // That means, if the stack overflows, the bot will know how to do the FIRST 32
270 // steps to the goal, and then recalculate the path.
271 void navigation_pushroute(entity e)
273 //print("bot ", etos(self), " push ", etos(e), "\n");
274 self.goalstack31 = self.goalstack30;
275 self.goalstack30 = self.goalstack29;
276 self.goalstack29 = self.goalstack28;
277 self.goalstack28 = self.goalstack27;
278 self.goalstack27 = self.goalstack26;
279 self.goalstack26 = self.goalstack25;
280 self.goalstack25 = self.goalstack24;
281 self.goalstack24 = self.goalstack23;
282 self.goalstack23 = self.goalstack22;
283 self.goalstack22 = self.goalstack21;
284 self.goalstack21 = self.goalstack20;
285 self.goalstack20 = self.goalstack19;
286 self.goalstack19 = self.goalstack18;
287 self.goalstack18 = self.goalstack17;
288 self.goalstack17 = self.goalstack16;
289 self.goalstack16 = self.goalstack15;
290 self.goalstack15 = self.goalstack14;
291 self.goalstack14 = self.goalstack13;
292 self.goalstack13 = self.goalstack12;
293 self.goalstack12 = self.goalstack11;
294 self.goalstack11 = self.goalstack10;
295 self.goalstack10 = self.goalstack09;
296 self.goalstack09 = self.goalstack08;
297 self.goalstack08 = self.goalstack07;
298 self.goalstack07 = self.goalstack06;
299 self.goalstack06 = self.goalstack05;
300 self.goalstack05 = self.goalstack04;
301 self.goalstack04 = self.goalstack03;
302 self.goalstack03 = self.goalstack02;
303 self.goalstack02 = self.goalstack01;
304 self.goalstack01 = self.goalcurrent;
305 self.goalcurrent = e;
308 // remove first goal from stack
309 // (in other words: remove a prerequisite for reaching the later goals)
310 // (used when a spawnfunc_waypoint is reached)
311 void navigation_poproute()
313 //print("bot ", etos(self), " pop\n");
314 self.goalcurrent = self.goalstack01;
315 self.goalstack01 = self.goalstack02;
316 self.goalstack02 = self.goalstack03;
317 self.goalstack03 = self.goalstack04;
318 self.goalstack04 = self.goalstack05;
319 self.goalstack05 = self.goalstack06;
320 self.goalstack06 = self.goalstack07;
321 self.goalstack07 = self.goalstack08;
322 self.goalstack08 = self.goalstack09;
323 self.goalstack09 = self.goalstack10;
324 self.goalstack10 = self.goalstack11;
325 self.goalstack11 = self.goalstack12;
326 self.goalstack12 = self.goalstack13;
327 self.goalstack13 = self.goalstack14;
328 self.goalstack14 = self.goalstack15;
329 self.goalstack15 = self.goalstack16;
330 self.goalstack16 = self.goalstack17;
331 self.goalstack17 = self.goalstack18;
332 self.goalstack18 = self.goalstack19;
333 self.goalstack19 = self.goalstack20;
334 self.goalstack20 = self.goalstack21;
335 self.goalstack21 = self.goalstack22;
336 self.goalstack22 = self.goalstack23;
337 self.goalstack23 = self.goalstack24;
338 self.goalstack24 = self.goalstack25;
339 self.goalstack25 = self.goalstack26;
340 self.goalstack26 = self.goalstack27;
341 self.goalstack27 = self.goalstack28;
342 self.goalstack28 = self.goalstack29;
343 self.goalstack29 = self.goalstack30;
344 self.goalstack30 = self.goalstack31;
345 self.goalstack31 = world;
348 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
351 dist = vlen(v - org);
354 traceline(v, org, true, ent);
355 if (trace_fraction == 1)
359 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
364 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
372 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
373 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
375 entity waylist, w, best;
376 vector v, org, pm1, pm2;
378 pm1 = ent.origin + ent.mins;
379 pm2 = ent.origin + ent.maxs;
380 waylist = findchain(classname, "waypoint");
382 // do two scans, because box test is cheaper
386 // if object is touching spawnfunc_waypoint
387 if(w != ent && w != except)
388 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
393 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
394 org.z = ent.origin.z + ent.mins.z - PL_MIN.z; // player height
395 // TODO possibly make other code have the same support for bboxes
397 org = org + ent.tag_entity.origin;
398 if (navigation_testtracewalk)
403 // box check failed, try walk
407 // if object can walk from spawnfunc_waypoint
413 wm1 = w.origin + w.mins;
414 wm2 = w.origin + w.maxs;
415 v.x = bound(wm1_x, org.x, wm2_x);
416 v.y = bound(wm1_y, org.y, wm2_y);
417 v.z = bound(wm1_z, org.z, wm2_z);
421 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
423 bestdist = vlen(v - org);
431 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
433 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, world);
434 if (autocvar_g_waypointeditor_auto)
436 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
438 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
443 // finds the waypoints near the bot initiating a navigation query
444 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
447 vector v, m1, m2, diff;
449 // navigation_testtracewalk = true;
454 if (!head.wpconsidered)
458 m1 = head.origin + head.mins;
459 m2 = head.origin + head.maxs;
461 v.x = bound(m1_x, v.x, m2_x);
462 v.y = bound(m1_y, v.y, m2_y);
463 v.z = bound(m1_z, v.z, m2_z);
467 diff = v - self.origin;
468 diff.z = max(0, diff.z);
469 if (vlen(diff) < maxdist)
471 head.wpconsidered = true;
472 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
474 head.wpnearestpoint = v;
475 head.wpcost = vlen(v - self.origin) + head.dmg;
484 //navigation_testtracewalk = false;
488 // updates a path link if a spawnfunc_waypoint link is better than the current one
489 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
498 v.x = bound(m1_x, p.x, m2_x);
499 v.y = bound(m1_y, p.y, m2_y);
500 v.z = bound(m1_z, p.z, m2_z);
504 cost2 = cost2 + vlen(v - p);
505 if (wp.wpcost > cost2)
510 wp.wpnearestpoint = v;
514 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
515 void navigation_markroutes(entity fixed_source_waypoint)
517 entity w, wp, waylist;
518 float searching, cost, cost2;
520 w = waylist = findchain(classname, "waypoint");
523 w.wpconsidered = false;
524 w.wpnearestpoint = '0 0 0';
531 if(fixed_source_waypoint)
533 fixed_source_waypoint.wpconsidered = true;
534 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
535 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
536 fixed_source_waypoint.wpfire = 1;
537 fixed_source_waypoint.enemy = world;
541 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
542 // as this search is expensive we will use lower values if the bot is on the air
543 float i, increment, maxdistance;
544 if(self.flags & FL_ONGROUND)
555 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
570 p = w.wpnearestpoint;
571 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
600 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
601 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
602 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
603 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
610 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
611 void navigation_markroutes_inverted(entity fixed_source_waypoint)
613 entity w, wp, waylist;
614 float searching, cost, cost2;
616 w = waylist = findchain(classname, "waypoint");
619 w.wpconsidered = false;
620 w.wpnearestpoint = '0 0 0';
627 if(fixed_source_waypoint)
629 fixed_source_waypoint.wpconsidered = true;
630 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
631 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
632 fixed_source_waypoint.wpfire = 1;
633 fixed_source_waypoint.enemy = world;
637 error("need to start with a waypoint\n");
651 cost = w.wpcost; // cost to walk from w to home
652 p = w.wpnearestpoint;
653 for(wp = waylist; wp; wp = wp.chain)
655 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
656 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
657 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
658 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
659 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
660 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
661 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
662 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
664 cost2 = cost + wp.dmg;
665 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
673 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
674 void navigation_routerating(entity e, float f, float rangebias)
684 o = (e.absmin + e.absmax) * 0.5;
686 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
688 // Evaluate path using jetpack
690 if(self.items & IT_JETPACK)
691 if(autocvar_bot_ai_navigation_jetpack)
692 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
694 vector pointa, pointb;
696 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
699 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
700 pointa = trace_endpos - '0 0 1';
703 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
704 pointb = trace_endpos - '0 0 1';
706 // Can I see these two points from the sky?
707 traceline(pointa, pointb, MOVE_NORMAL, self);
709 if(trace_fraction==1)
711 bot_debug("jetpack ai: can bridge these two points\n");
713 // Lower the altitude of these points as much as possible
714 float zdistance, xydistance, cost, t, fuel;
715 vector down, npa, npb;
717 down = '0 0 -1' * (PL_MAX.z - PL_MIN.z) * 10;
723 if(npa.z<=self.absmax.z)
726 if(npb.z<=e.absmax.z)
729 traceline(npa, npb, MOVE_NORMAL, self);
730 if(trace_fraction==1)
736 while(trace_fraction == 1);
739 // Rough estimation of fuel consumption
740 // (ignores acceleration and current xyz velocity)
741 xydistance = vlen(pointa - pointb);
742 zdistance = fabs(pointa.z - self.origin.z);
744 t = zdistance / autocvar_g_jetpack_maxspeed_up;
745 t += xydistance / autocvar_g_jetpack_maxspeed_side;
746 fuel = t * autocvar_g_jetpack_fuel * 0.8;
748 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
751 if(self.ammo_fuel>fuel)
754 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
755 // - between air and ground speeds)
757 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
758 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
761 // Compare against other goals
762 f = f * rangebias / (rangebias + cost);
764 if (navigation_bestrating < f)
766 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
767 navigation_bestrating = f;
768 navigation_bestgoal = e;
769 self.navigation_jetpack_goal = e;
770 self.navigation_jetpack_point = pointb;
777 //te_wizspike(e.origin);
780 // update the cached spawnfunc_waypoint link on a dynamic item entity
781 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
791 if(e.flags & FL_ITEM)
793 if (!(e.flags & FL_WEAPON))
794 if(e.nearestwaypoint)
797 else if (e.flags & FL_WEAPON)
799 if(e.classname != "droppedweapon")
800 if(e.nearestwaypoint)
805 if (time > e.nearestwaypointtimeout)
807 nwp = navigation_findnearestwaypoint(e, true);
809 e.nearestwaypoint = nwp;
812 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
814 if(e.flags & FL_ITEM)
815 e.blacklisted = true;
816 else if (e.flags & FL_WEAPON)
818 if(e.classname != "droppedweapon")
819 e.blacklisted = true;
824 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
829 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
830 if(e.classname=="item_flag_team")
831 e.nearestwaypointtimeout = time + 2;
833 e.nearestwaypointtimeout = time + random() * 3 + 5;
835 nwp = e.nearestwaypoint;
838 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
840 if (nwp.wpcost < 10000000)
842 //te_wizspike(nwp.wpnearestpoint);
843 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
844 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
845 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
846 if (navigation_bestrating < f)
848 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
849 navigation_bestrating = f;
850 navigation_bestgoal = e;
855 // adds an item to the the goal stack with the path to a given item
856 float navigation_routetogoal(entity e, vector startposition)
860 // if there is no goal, just exit
864 self.navigation_hasgoals = true;
866 // put the entity on the goal stack
867 //print("routetogoal ", etos(e), "\n");
868 navigation_pushroute(e);
871 if(e==self.navigation_jetpack_goal)
874 // if it can reach the goal there is nothing more to do
875 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
878 // see if there are waypoints describing a path to the item
879 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
880 e = e.nearestwaypoint;
882 e = e.enemy; // we already have added it, so...
889 // add the spawnfunc_waypoint to the path
890 navigation_pushroute(e);
900 // removes any currently touching waypoints from the goal stack
901 // (this is how bots detect if they reached a goal)
902 void navigation_poptouchedgoals()
906 m1 = org + self.mins;
907 m2 = org + self.maxs;
909 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
911 if(self.lastteleporttime>0)
912 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
914 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
915 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
917 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
918 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
920 navigation_poproute();
925 // If for some reason the bot is closer to the next goal, pop the current one
927 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
928 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
929 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
931 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
932 navigation_poproute();
933 // TODO this may also be a nice idea to do "early" (e.g. by
934 // manipulating the vlen() comparisons) to shorten paths in
935 // general - this would make bots walk more "on rails" than
936 // "zigzagging" which they currently do with sufficiently
937 // random-like waypoints, and thus can make a nice bot
938 // personality property
941 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
942 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
943 if(IS_PLAYER(self.goalcurrent))
944 navigation_poproute();
946 // aid for detecting jump pads better (distance based check fails sometimes)
947 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
948 navigation_poproute();
950 // Loose goal touching check when running
951 if(self.aistatus & AI_STATUS_RUNNING)
952 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
953 if(self.goalcurrent.classname=="waypoint")
955 if(vlen(self.origin - self.goalcurrent.origin)<150)
957 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, true, world);
958 if(trace_fraction==1)
960 // Detect personal waypoints
961 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
962 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
964 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
965 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
968 navigation_poproute();
973 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
975 // Detect personal waypoints
976 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
977 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
979 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
980 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
983 navigation_poproute();
987 // begin a goal selection session (queries spawnfunc_waypoint network)
988 void navigation_goalrating_start()
990 if(self.aistatus & AI_STATUS_STUCK)
993 self.navigation_jetpack_goal = world;
994 navigation_bestrating = -1;
995 self.navigation_hasgoals = false;
996 navigation_clearroute();
997 navigation_bestgoal = world;
998 navigation_markroutes(world);
1001 // ends a goal selection session (updates goal stack to the best goal)
1002 void navigation_goalrating_end()
1004 if(self.aistatus & AI_STATUS_STUCK)
1007 navigation_routetogoal(navigation_bestgoal, self.origin);
1008 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
1010 // If the bot got stuck then try to reach the farthest waypoint
1011 if (!self.navigation_hasgoals)
1012 if (autocvar_bot_wander_enable)
1014 if (!(self.aistatus & AI_STATUS_STUCK))
1016 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1017 self.aistatus |= AI_STATUS_STUCK;
1020 self.navigation_hasgoals = false; // Reset this value
1024 void botframe_updatedangerousobjects(float maxupdate)
1026 entity head, bot_dodgelist;
1027 vector m1, m2, v, o;
1030 bot_dodgelist = findchainfloat(bot_dodge, true);
1031 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1032 while (botframe_dangerwaypoint != world)
1035 m1 = botframe_dangerwaypoint.mins;
1036 m2 = botframe_dangerwaypoint.maxs;
1037 head = bot_dodgelist;
1041 v.x = bound(m1_x, v.x, m2_x);
1042 v.y = bound(m1_y, v.y, m2_y);
1043 v.z = bound(m1_z, v.z, m2_z);
1044 o = (head.absmin + head.absmax) * 0.5;
1045 d = head.bot_dodgerating - vlen(o - v);
1048 traceline(o, v, true, world);
1049 if (trace_fraction == 1)
1050 danger = danger + d;
1054 botframe_dangerwaypoint.dmg = danger;
1058 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1062 void navigation_unstuck()
1064 float search_radius = 1000;
1066 if (!autocvar_bot_wander_enable)
1069 if (!bot_waypoint_queue_owner)
1071 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1072 bot_waypoint_queue_owner = self;
1073 bot_waypoint_queue_bestgoal = world;
1074 bot_waypoint_queue_bestgoalrating = 0;
1077 if(bot_waypoint_queue_owner!=self)
1080 if (bot_waypoint_queue_goal)
1082 // evaluate the next goal on the queue
1083 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1084 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1085 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1087 if( d > bot_waypoint_queue_bestgoalrating)
1089 bot_waypoint_queue_bestgoalrating = d;
1090 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1093 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1095 if (!bot_waypoint_queue_goal)
1097 if (bot_waypoint_queue_bestgoal)
1099 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1100 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1101 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1102 self.aistatus &= ~AI_STATUS_STUCK;
1106 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1109 bot_waypoint_queue_owner = world;
1114 if(bot_strategytoken!=self)
1117 // build a new queue
1118 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1123 head = findradius(self.origin, search_radius);
1127 if(head.classname=="waypoint")
1128 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1130 if(bot_waypoint_queue_goal)
1131 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1135 bot_waypoint_queue_goal = head;
1136 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1143 bot_waypoint_queue_goal = first;
1146 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1147 bot_waypoint_queue_owner = world;
1152 // Support for debugging tracewalk visually
1154 void debugresetnodes()
1156 debuglastnode = '0 0 0';
1159 void debugnode(vector node)
1161 if (!IS_PLAYER(self))
1164 if(debuglastnode=='0 0 0')
1166 debuglastnode = node;
1170 te_lightning2(world, node, debuglastnode);
1171 debuglastnode = node;
1174 void debugnodestatus(vector position, float status)
1180 case DEBUG_NODE_SUCCESS:
1183 case DEBUG_NODE_WARNING:
1186 case DEBUG_NODE_FAIL:
1193 te_customflash(position, 40, 2, c);
1196 // Support for debugging the goal stack visually
1199 .vector lastposition;
1201 // Debug the goal stack visually
1202 void debuggoalstack()
1207 if(self.goalcounter==0)goal=self.goalcurrent;
1208 else if(self.goalcounter==1)goal=self.goalstack01;
1209 else if(self.goalcounter==2)goal=self.goalstack02;
1210 else if(self.goalcounter==3)goal=self.goalstack03;
1211 else if(self.goalcounter==4)goal=self.goalstack04;
1212 else if(self.goalcounter==5)goal=self.goalstack05;
1213 else if(self.goalcounter==6)goal=self.goalstack06;
1214 else if(self.goalcounter==7)goal=self.goalstack07;
1215 else if(self.goalcounter==8)goal=self.goalstack08;
1216 else if(self.goalcounter==9)goal=self.goalstack09;
1217 else if(self.goalcounter==10)goal=self.goalstack10;
1218 else if(self.goalcounter==11)goal=self.goalstack11;
1219 else if(self.goalcounter==12)goal=self.goalstack12;
1220 else if(self.goalcounter==13)goal=self.goalstack13;
1221 else if(self.goalcounter==14)goal=self.goalstack14;
1222 else if(self.goalcounter==15)goal=self.goalstack15;
1223 else if(self.goalcounter==16)goal=self.goalstack16;
1224 else if(self.goalcounter==17)goal=self.goalstack17;
1225 else if(self.goalcounter==18)goal=self.goalstack18;
1226 else if(self.goalcounter==19)goal=self.goalstack19;
1227 else if(self.goalcounter==20)goal=self.goalstack20;
1228 else if(self.goalcounter==21)goal=self.goalstack21;
1229 else if(self.goalcounter==22)goal=self.goalstack22;
1230 else if(self.goalcounter==23)goal=self.goalstack23;
1231 else if(self.goalcounter==24)goal=self.goalstack24;
1232 else if(self.goalcounter==25)goal=self.goalstack25;
1233 else if(self.goalcounter==26)goal=self.goalstack26;
1234 else if(self.goalcounter==27)goal=self.goalstack27;
1235 else if(self.goalcounter==28)goal=self.goalstack28;
1236 else if(self.goalcounter==29)goal=self.goalstack29;
1237 else if(self.goalcounter==30)goal=self.goalstack30;
1238 else if(self.goalcounter==31)goal=self.goalstack31;
1243 self.goalcounter = 0;
1244 self.lastposition='0 0 0';
1248 if(self.lastposition=='0 0 0')
1251 org = self.lastposition;
1254 go = ( goal.absmin + goal.absmax ) * 0.5;
1255 te_lightning2(world, org, go);
1256 self.lastposition = go;