1 #include "navigation.qh"
5 #include "waypoints.qh"
7 #include "../t_items.qh"
9 #include "../../common/constants.qh"
10 #include "../../common/triggers/trigger/jumppads.qh"
12 void bot_debug(string input)
14 switch(autocvar_bot_debug)
16 case 1: LOG_TRACE(input); break;
17 case 2: LOG_INFO(input); break;
21 // rough simulation of walking from one point to another to test if a path
22 // can be traveled, used for waypoint linking and havocbot
24 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
36 if(autocvar_bot_debug_tracewalk)
45 dist = totaldist = vlen(move);
46 dir = normalize(move);
48 ignorehazards = false;
51 // Analyze starting point
52 traceline(start, start, MOVE_NORMAL, e);
53 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
57 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
58 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
64 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
68 if(autocvar_bot_debug_tracewalk)
69 debugnodestatus(start, DEBUG_NODE_FAIL);
71 //print("tracewalk: ", vtos(start), " is a bad start\n");
80 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
83 if(autocvar_bot_debug_tracewalk)
84 debugnodestatus(org, DEBUG_NODE_SUCCESS);
86 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
89 if(autocvar_bot_debug_tracewalk)
96 dist = dist - stepdist;
97 traceline(org, org, MOVE_NORMAL, e);
100 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
102 // hazards blocking path
103 if(autocvar_bot_debug_tracewalk)
104 debugnodestatus(org, DEBUG_NODE_FAIL);
106 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
110 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
112 move = normalize(end - org);
113 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
115 if(autocvar_bot_debug_tracewalk)
116 debugnode(trace_endpos);
118 if (trace_fraction < 1)
121 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
122 for (; org.z < end.z + self.maxs.z; org.z += stepdist)
124 if(autocvar_bot_debug_tracewalk)
127 if(pointcontents(org) == CONTENT_EMPTY)
131 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
133 if(autocvar_bot_debug_tracewalk)
134 debugnodestatus(org, DEBUG_NODE_FAIL);
137 //print("tracewalk: ", vtos(start), " failed under water\n");
147 move = dir * stepdist + org;
148 tracebox(org, m1, m2, move, movemode, e);
150 if(autocvar_bot_debug_tracewalk)
151 debugnode(trace_endpos);
154 if (trace_fraction < 1)
156 // check if we can walk over this obstacle, possibly by jumpstepping
157 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
158 if (trace_fraction < 1 || trace_startsolid)
160 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
161 if (trace_fraction < 1 || trace_startsolid)
163 if(autocvar_bot_debug_tracewalk)
164 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
167 traceline( org, move, movemode, e);
168 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
172 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
174 nextmove = move + (dir * stepdist);
175 traceline( move, nextmove, movemode, e);
181 if(autocvar_bot_debug_tracewalk)
182 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
184 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
185 //te_explosion(trace_endpos);
186 //print(ftos(e.dphitcontentsmask), "\n");
187 return false; // failed
199 // trace down from stepheight as far as possible and move there,
200 // if this starts in solid we try again without the stepup, and
201 // if that also fails we assume it is a wall
202 // (this is the same logic as the Quake walkmove function used)
203 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
205 // moved successfully
209 c = pointcontents(org + '0 0 1');
210 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
220 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
222 // moved but didn't arrive at the intended destination
223 if(autocvar_bot_debug_tracewalk)
224 debugnodestatus(org, DEBUG_NODE_FAIL);
229 /////////////////////////////////////////////////////////////////////////////
231 /////////////////////////////////////////////////////////////////////////////
233 // completely empty the goal stack, used when deciding where to go
234 void navigation_clearroute()
236 //print("bot ", etos(self), " clear\n");
237 self.navigation_hasgoals = false;
238 self.goalcurrent = world;
239 self.goalstack01 = world;
240 self.goalstack02 = world;
241 self.goalstack03 = world;
242 self.goalstack04 = world;
243 self.goalstack05 = world;
244 self.goalstack06 = world;
245 self.goalstack07 = world;
246 self.goalstack08 = world;
247 self.goalstack09 = world;
248 self.goalstack10 = world;
249 self.goalstack11 = world;
250 self.goalstack12 = world;
251 self.goalstack13 = world;
252 self.goalstack14 = world;
253 self.goalstack15 = world;
254 self.goalstack16 = world;
255 self.goalstack17 = world;
256 self.goalstack18 = world;
257 self.goalstack19 = world;
258 self.goalstack20 = world;
259 self.goalstack21 = world;
260 self.goalstack22 = world;
261 self.goalstack23 = world;
262 self.goalstack24 = world;
263 self.goalstack25 = world;
264 self.goalstack26 = world;
265 self.goalstack27 = world;
266 self.goalstack28 = world;
267 self.goalstack29 = world;
268 self.goalstack30 = world;
269 self.goalstack31 = world;
272 // add a new goal at the beginning of the stack
273 // (in other words: add a new prerequisite before going to the later goals)
274 // NOTE: when a waypoint is added, the WP gets pushed first, then the
275 // next-closest WP on the shortest path to the WP
276 // That means, if the stack overflows, the bot will know how to do the FIRST 32
277 // steps to the goal, and then recalculate the path.
278 void navigation_pushroute(entity e)
280 //print("bot ", etos(self), " push ", etos(e), "\n");
281 self.goalstack31 = self.goalstack30;
282 self.goalstack30 = self.goalstack29;
283 self.goalstack29 = self.goalstack28;
284 self.goalstack28 = self.goalstack27;
285 self.goalstack27 = self.goalstack26;
286 self.goalstack26 = self.goalstack25;
287 self.goalstack25 = self.goalstack24;
288 self.goalstack24 = self.goalstack23;
289 self.goalstack23 = self.goalstack22;
290 self.goalstack22 = self.goalstack21;
291 self.goalstack21 = self.goalstack20;
292 self.goalstack20 = self.goalstack19;
293 self.goalstack19 = self.goalstack18;
294 self.goalstack18 = self.goalstack17;
295 self.goalstack17 = self.goalstack16;
296 self.goalstack16 = self.goalstack15;
297 self.goalstack15 = self.goalstack14;
298 self.goalstack14 = self.goalstack13;
299 self.goalstack13 = self.goalstack12;
300 self.goalstack12 = self.goalstack11;
301 self.goalstack11 = self.goalstack10;
302 self.goalstack10 = self.goalstack09;
303 self.goalstack09 = self.goalstack08;
304 self.goalstack08 = self.goalstack07;
305 self.goalstack07 = self.goalstack06;
306 self.goalstack06 = self.goalstack05;
307 self.goalstack05 = self.goalstack04;
308 self.goalstack04 = self.goalstack03;
309 self.goalstack03 = self.goalstack02;
310 self.goalstack02 = self.goalstack01;
311 self.goalstack01 = self.goalcurrent;
312 self.goalcurrent = e;
315 // remove first goal from stack
316 // (in other words: remove a prerequisite for reaching the later goals)
317 // (used when a spawnfunc_waypoint is reached)
318 void navigation_poproute()
320 //print("bot ", etos(self), " pop\n");
321 self.goalcurrent = self.goalstack01;
322 self.goalstack01 = self.goalstack02;
323 self.goalstack02 = self.goalstack03;
324 self.goalstack03 = self.goalstack04;
325 self.goalstack04 = self.goalstack05;
326 self.goalstack05 = self.goalstack06;
327 self.goalstack06 = self.goalstack07;
328 self.goalstack07 = self.goalstack08;
329 self.goalstack08 = self.goalstack09;
330 self.goalstack09 = self.goalstack10;
331 self.goalstack10 = self.goalstack11;
332 self.goalstack11 = self.goalstack12;
333 self.goalstack12 = self.goalstack13;
334 self.goalstack13 = self.goalstack14;
335 self.goalstack14 = self.goalstack15;
336 self.goalstack15 = self.goalstack16;
337 self.goalstack16 = self.goalstack17;
338 self.goalstack17 = self.goalstack18;
339 self.goalstack18 = self.goalstack19;
340 self.goalstack19 = self.goalstack20;
341 self.goalstack20 = self.goalstack21;
342 self.goalstack21 = self.goalstack22;
343 self.goalstack22 = self.goalstack23;
344 self.goalstack23 = self.goalstack24;
345 self.goalstack24 = self.goalstack25;
346 self.goalstack25 = self.goalstack26;
347 self.goalstack26 = self.goalstack27;
348 self.goalstack27 = self.goalstack28;
349 self.goalstack28 = self.goalstack29;
350 self.goalstack29 = self.goalstack30;
351 self.goalstack30 = self.goalstack31;
352 self.goalstack31 = world;
355 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
358 dist = vlen(v - org);
361 traceline(v, org, true, ent);
362 if (trace_fraction == 1)
366 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
371 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
379 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
380 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
382 entity waylist, w, best;
383 vector v, org, pm1, pm2;
385 pm1 = ent.origin + ent.mins;
386 pm2 = ent.origin + ent.maxs;
387 waylist = findchain(classname, "waypoint");
389 // do two scans, because box test is cheaper
393 // if object is touching spawnfunc_waypoint
394 if(w != ent && w != except)
395 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
400 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
401 org.z = ent.origin.z + ent.mins.z - PL_MIN.z; // player height
402 // TODO possibly make other code have the same support for bboxes
404 org = org + ent.tag_entity.origin;
405 if (navigation_testtracewalk)
410 // box check failed, try walk
414 // if object can walk from spawnfunc_waypoint
420 wm1 = w.origin + w.mins;
421 wm2 = w.origin + w.maxs;
422 v.x = bound(wm1_x, org.x, wm2_x);
423 v.y = bound(wm1_y, org.y, wm2_y);
424 v.z = bound(wm1_z, org.z, wm2_z);
428 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
430 bestdist = vlen(v - org);
438 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
440 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, world);
441 if (autocvar_g_waypointeditor_auto)
443 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
445 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
450 // finds the waypoints near the bot initiating a navigation query
451 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
454 vector v, m1, m2, diff;
456 // navigation_testtracewalk = true;
461 if (!head.wpconsidered)
465 m1 = head.origin + head.mins;
466 m2 = head.origin + head.maxs;
468 v.x = bound(m1_x, v.x, m2_x);
469 v.y = bound(m1_y, v.y, m2_y);
470 v.z = bound(m1_z, v.z, m2_z);
474 diff = v - self.origin;
475 diff.z = max(0, diff.z);
476 if (vlen(diff) < maxdist)
478 head.wpconsidered = true;
479 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
481 head.wpnearestpoint = v;
482 head.wpcost = vlen(v - self.origin) + head.dmg;
491 //navigation_testtracewalk = false;
495 // updates a path link if a spawnfunc_waypoint link is better than the current one
496 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
505 v.x = bound(m1_x, p.x, m2_x);
506 v.y = bound(m1_y, p.y, m2_y);
507 v.z = bound(m1_z, p.z, m2_z);
511 cost2 = cost2 + vlen(v - p);
512 if (wp.wpcost > cost2)
517 wp.wpnearestpoint = v;
521 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
522 void navigation_markroutes(entity fixed_source_waypoint)
524 entity w, wp, waylist;
525 float searching, cost, cost2;
527 w = waylist = findchain(classname, "waypoint");
530 w.wpconsidered = false;
531 w.wpnearestpoint = '0 0 0';
538 if(fixed_source_waypoint)
540 fixed_source_waypoint.wpconsidered = true;
541 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
542 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
543 fixed_source_waypoint.wpfire = 1;
544 fixed_source_waypoint.enemy = world;
548 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
549 // as this search is expensive we will use lower values if the bot is on the air
550 float i, increment, maxdistance;
551 if(self.flags & FL_ONGROUND)
562 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
577 p = w.wpnearestpoint;
578 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
600 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
601 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
602 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
603 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
604 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
605 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
606 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
607 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
608 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
609 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
610 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
617 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
618 void navigation_markroutes_inverted(entity fixed_source_waypoint)
620 entity w, wp, waylist;
621 float searching, cost, cost2;
623 w = waylist = findchain(classname, "waypoint");
626 w.wpconsidered = false;
627 w.wpnearestpoint = '0 0 0';
634 if(fixed_source_waypoint)
636 fixed_source_waypoint.wpconsidered = true;
637 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
638 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
639 fixed_source_waypoint.wpfire = 1;
640 fixed_source_waypoint.enemy = world;
644 error("need to start with a waypoint\n");
658 cost = w.wpcost; // cost to walk from w to home
659 p = w.wpnearestpoint;
660 for(wp = waylist; wp; wp = wp.chain)
662 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
663 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
664 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
665 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
666 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
667 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
668 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
669 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
671 cost2 = cost + wp.dmg;
672 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
680 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
681 void navigation_routerating(entity e, float f, float rangebias)
691 o = (e.absmin + e.absmax) * 0.5;
693 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
695 // Evaluate path using jetpack
697 if(self.items & IT_JETPACK)
698 if(autocvar_bot_ai_navigation_jetpack)
699 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
701 vector pointa, pointb;
703 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
706 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
707 pointa = trace_endpos - '0 0 1';
710 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
711 pointb = trace_endpos - '0 0 1';
713 // Can I see these two points from the sky?
714 traceline(pointa, pointb, MOVE_NORMAL, self);
716 if(trace_fraction==1)
718 bot_debug("jetpack ai: can bridge these two points\n");
720 // Lower the altitude of these points as much as possible
721 float zdistance, xydistance, cost, t, fuel;
722 vector down, npa, npb;
724 down = '0 0 -1' * (PL_MAX.z - PL_MIN.z) * 10;
730 if(npa.z<=self.absmax.z)
733 if(npb.z<=e.absmax.z)
736 traceline(npa, npb, MOVE_NORMAL, self);
737 if(trace_fraction==1)
743 while(trace_fraction == 1);
746 // Rough estimation of fuel consumption
747 // (ignores acceleration and current xyz velocity)
748 xydistance = vlen(pointa - pointb);
749 zdistance = fabs(pointa.z - self.origin.z);
751 t = zdistance / autocvar_g_jetpack_maxspeed_up;
752 t += xydistance / autocvar_g_jetpack_maxspeed_side;
753 fuel = t * autocvar_g_jetpack_fuel * 0.8;
755 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
758 if(self.ammo_fuel>fuel)
761 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
762 // - between air and ground speeds)
764 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
765 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
768 // Compare against other goals
769 f = f * rangebias / (rangebias + cost);
771 if (navigation_bestrating < f)
773 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
774 navigation_bestrating = f;
775 navigation_bestgoal = e;
776 self.navigation_jetpack_goal = e;
777 self.navigation_jetpack_point = pointb;
784 //te_wizspike(e.origin);
787 // update the cached spawnfunc_waypoint link on a dynamic item entity
788 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
798 if(e.flags & FL_ITEM)
800 if (!(e.flags & FL_WEAPON))
801 if(e.nearestwaypoint)
804 else if (e.flags & FL_WEAPON)
806 if(e.classname != "droppedweapon")
807 if(e.nearestwaypoint)
812 if (time > e.nearestwaypointtimeout)
814 nwp = navigation_findnearestwaypoint(e, true);
816 e.nearestwaypoint = nwp;
819 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
821 if(e.flags & FL_ITEM)
822 e.blacklisted = true;
823 else if (e.flags & FL_WEAPON)
825 if(e.classname != "droppedweapon")
826 e.blacklisted = true;
831 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
836 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
837 if(e.classname=="item_flag_team")
838 e.nearestwaypointtimeout = time + 2;
840 e.nearestwaypointtimeout = time + random() * 3 + 5;
842 nwp = e.nearestwaypoint;
845 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
847 if (nwp.wpcost < 10000000)
849 //te_wizspike(nwp.wpnearestpoint);
850 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
851 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
852 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
853 if (navigation_bestrating < f)
855 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
856 navigation_bestrating = f;
857 navigation_bestgoal = e;
862 // adds an item to the the goal stack with the path to a given item
863 float navigation_routetogoal(entity e, vector startposition)
867 // if there is no goal, just exit
871 self.navigation_hasgoals = true;
873 // put the entity on the goal stack
874 //print("routetogoal ", etos(e), "\n");
875 navigation_pushroute(e);
878 if(e==self.navigation_jetpack_goal)
881 // if it can reach the goal there is nothing more to do
882 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
885 // see if there are waypoints describing a path to the item
886 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
887 e = e.nearestwaypoint;
889 e = e.enemy; // we already have added it, so...
896 // add the spawnfunc_waypoint to the path
897 navigation_pushroute(e);
907 // removes any currently touching waypoints from the goal stack
908 // (this is how bots detect if they reached a goal)
909 void navigation_poptouchedgoals()
913 m1 = org + self.mins;
914 m2 = org + self.maxs;
916 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
918 if(self.lastteleporttime>0)
919 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
921 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
922 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
924 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
925 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
927 navigation_poproute();
932 // If for some reason the bot is closer to the next goal, pop the current one
934 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
935 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
936 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
938 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
939 navigation_poproute();
940 // TODO this may also be a nice idea to do "early" (e.g. by
941 // manipulating the vlen() comparisons) to shorten paths in
942 // general - this would make bots walk more "on rails" than
943 // "zigzagging" which they currently do with sufficiently
944 // random-like waypoints, and thus can make a nice bot
945 // personality property
948 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
949 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
950 if(IS_PLAYER(self.goalcurrent))
951 navigation_poproute();
953 // aid for detecting jump pads better (distance based check fails sometimes)
954 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
955 navigation_poproute();
957 // Loose goal touching check when running
958 if(self.aistatus & AI_STATUS_RUNNING)
959 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
960 if(self.goalcurrent.classname=="waypoint")
962 if(vlen(self.origin - self.goalcurrent.origin)<150)
964 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, true, world);
965 if(trace_fraction==1)
967 // Detect personal waypoints
968 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
969 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
971 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
972 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
975 navigation_poproute();
980 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
982 // Detect personal waypoints
983 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
984 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
986 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
987 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
990 navigation_poproute();
994 // begin a goal selection session (queries spawnfunc_waypoint network)
995 void navigation_goalrating_start()
997 if(self.aistatus & AI_STATUS_STUCK)
1000 self.navigation_jetpack_goal = world;
1001 navigation_bestrating = -1;
1002 self.navigation_hasgoals = false;
1003 navigation_clearroute();
1004 navigation_bestgoal = world;
1005 navigation_markroutes(world);
1008 // ends a goal selection session (updates goal stack to the best goal)
1009 void navigation_goalrating_end()
1011 if(self.aistatus & AI_STATUS_STUCK)
1014 navigation_routetogoal(navigation_bestgoal, self.origin);
1015 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
1017 // If the bot got stuck then try to reach the farthest waypoint
1018 if (!self.navigation_hasgoals)
1019 if (autocvar_bot_wander_enable)
1021 if (!(self.aistatus & AI_STATUS_STUCK))
1023 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1024 self.aistatus |= AI_STATUS_STUCK;
1027 self.navigation_hasgoals = false; // Reset this value
1031 void botframe_updatedangerousobjects(float maxupdate)
1033 entity head, bot_dodgelist;
1034 vector m1, m2, v, o;
1037 bot_dodgelist = findchainfloat(bot_dodge, true);
1038 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1039 while (botframe_dangerwaypoint != world)
1042 m1 = botframe_dangerwaypoint.mins;
1043 m2 = botframe_dangerwaypoint.maxs;
1044 head = bot_dodgelist;
1048 v.x = bound(m1_x, v.x, m2_x);
1049 v.y = bound(m1_y, v.y, m2_y);
1050 v.z = bound(m1_z, v.z, m2_z);
1051 o = (head.absmin + head.absmax) * 0.5;
1052 d = head.bot_dodgerating - vlen(o - v);
1055 traceline(o, v, true, world);
1056 if (trace_fraction == 1)
1057 danger = danger + d;
1061 botframe_dangerwaypoint.dmg = danger;
1065 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1069 void navigation_unstuck()
1071 float search_radius = 1000;
1073 if (!autocvar_bot_wander_enable)
1076 if (!bot_waypoint_queue_owner)
1078 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1079 bot_waypoint_queue_owner = self;
1080 bot_waypoint_queue_bestgoal = world;
1081 bot_waypoint_queue_bestgoalrating = 0;
1084 if(bot_waypoint_queue_owner!=self)
1087 if (bot_waypoint_queue_goal)
1089 // evaluate the next goal on the queue
1090 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1091 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1092 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1094 if( d > bot_waypoint_queue_bestgoalrating)
1096 bot_waypoint_queue_bestgoalrating = d;
1097 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1100 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1102 if (!bot_waypoint_queue_goal)
1104 if (bot_waypoint_queue_bestgoal)
1106 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1107 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1108 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1109 self.aistatus &= ~AI_STATUS_STUCK;
1113 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1116 bot_waypoint_queue_owner = world;
1121 if(bot_strategytoken!=self)
1124 // build a new queue
1125 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1130 head = findradius(self.origin, search_radius);
1134 if(head.classname=="waypoint")
1135 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1137 if(bot_waypoint_queue_goal)
1138 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1142 bot_waypoint_queue_goal = head;
1143 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1150 bot_waypoint_queue_goal = first;
1153 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1154 bot_waypoint_queue_owner = world;
1159 // Support for debugging tracewalk visually
1161 void debugresetnodes()
1163 debuglastnode = '0 0 0';
1166 void debugnode(vector node)
1168 if (!IS_PLAYER(self))
1171 if(debuglastnode=='0 0 0')
1173 debuglastnode = node;
1177 te_lightning2(world, node, debuglastnode);
1178 debuglastnode = node;
1181 void debugnodestatus(vector position, float status)
1187 case DEBUG_NODE_SUCCESS:
1190 case DEBUG_NODE_WARNING:
1193 case DEBUG_NODE_FAIL:
1200 te_customflash(position, 40, 2, c);
1203 // Support for debugging the goal stack visually
1206 .vector lastposition;
1208 // Debug the goal stack visually
1209 void debuggoalstack()
1214 if(self.goalcounter==0)goal=self.goalcurrent;
1215 else if(self.goalcounter==1)goal=self.goalstack01;
1216 else if(self.goalcounter==2)goal=self.goalstack02;
1217 else if(self.goalcounter==3)goal=self.goalstack03;
1218 else if(self.goalcounter==4)goal=self.goalstack04;
1219 else if(self.goalcounter==5)goal=self.goalstack05;
1220 else if(self.goalcounter==6)goal=self.goalstack06;
1221 else if(self.goalcounter==7)goal=self.goalstack07;
1222 else if(self.goalcounter==8)goal=self.goalstack08;
1223 else if(self.goalcounter==9)goal=self.goalstack09;
1224 else if(self.goalcounter==10)goal=self.goalstack10;
1225 else if(self.goalcounter==11)goal=self.goalstack11;
1226 else if(self.goalcounter==12)goal=self.goalstack12;
1227 else if(self.goalcounter==13)goal=self.goalstack13;
1228 else if(self.goalcounter==14)goal=self.goalstack14;
1229 else if(self.goalcounter==15)goal=self.goalstack15;
1230 else if(self.goalcounter==16)goal=self.goalstack16;
1231 else if(self.goalcounter==17)goal=self.goalstack17;
1232 else if(self.goalcounter==18)goal=self.goalstack18;
1233 else if(self.goalcounter==19)goal=self.goalstack19;
1234 else if(self.goalcounter==20)goal=self.goalstack20;
1235 else if(self.goalcounter==21)goal=self.goalstack21;
1236 else if(self.goalcounter==22)goal=self.goalstack22;
1237 else if(self.goalcounter==23)goal=self.goalstack23;
1238 else if(self.goalcounter==24)goal=self.goalstack24;
1239 else if(self.goalcounter==25)goal=self.goalstack25;
1240 else if(self.goalcounter==26)goal=self.goalstack26;
1241 else if(self.goalcounter==27)goal=self.goalstack27;
1242 else if(self.goalcounter==28)goal=self.goalstack28;
1243 else if(self.goalcounter==29)goal=self.goalstack29;
1244 else if(self.goalcounter==30)goal=self.goalstack30;
1245 else if(self.goalcounter==31)goal=self.goalstack31;
1250 self.goalcounter = 0;
1251 self.lastposition='0 0 0';
1255 if(self.lastposition=='0 0 0')
1258 org = self.lastposition;
1261 go = ( goal.absmin + goal.absmax ) * 0.5;
1262 te_lightning2(world, org, go);
1263 self.lastposition = go;