1 #include "navigation.qh"
4 #include "waypoints.qh"
6 #include "../t_items.qh"
8 #include "../../common/items/all.qh"
10 #include "../../common/constants.qh"
11 #include "../../common/triggers/trigger/jumppads.qh"
15 void bot_debug(string input)
17 switch(autocvar_bot_debug)
19 case 1: LOG_TRACE(input); break;
20 case 2: LOG_INFO(input); break;
24 // rough simulation of walking from one point to another to test if a path
25 // can be traveled, used for waypoint linking and havocbot
27 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
39 if(autocvar_bot_debug_tracewalk)
48 dist = totaldist = vlen(move);
49 dir = normalize(move);
51 ignorehazards = false;
54 // Analyze starting point
55 traceline(start, start, MOVE_NORMAL, e);
56 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
60 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
61 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
67 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
71 if(autocvar_bot_debug_tracewalk)
72 debugnodestatus(start, DEBUG_NODE_FAIL);
74 //print("tracewalk: ", vtos(start), " is a bad start\n");
83 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
86 if(autocvar_bot_debug_tracewalk)
87 debugnodestatus(org, DEBUG_NODE_SUCCESS);
89 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
92 if(autocvar_bot_debug_tracewalk)
99 dist = dist - stepdist;
100 traceline(org, org, MOVE_NORMAL, e);
103 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
105 // hazards blocking path
106 if(autocvar_bot_debug_tracewalk)
107 debugnodestatus(org, DEBUG_NODE_FAIL);
109 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
113 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
115 move = normalize(end - org);
116 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
118 if(autocvar_bot_debug_tracewalk)
119 debugnode(trace_endpos);
121 if (trace_fraction < 1)
124 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
125 for (; org.z < end.z + self.maxs.z; org.z += stepdist)
127 if(autocvar_bot_debug_tracewalk)
130 if(pointcontents(org) == CONTENT_EMPTY)
134 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
136 if(autocvar_bot_debug_tracewalk)
137 debugnodestatus(org, DEBUG_NODE_FAIL);
140 //print("tracewalk: ", vtos(start), " failed under water\n");
150 move = dir * stepdist + org;
151 tracebox(org, m1, m2, move, movemode, e);
153 if(autocvar_bot_debug_tracewalk)
154 debugnode(trace_endpos);
157 if (trace_fraction < 1)
159 // check if we can walk over this obstacle, possibly by jumpstepping
160 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
161 if (trace_fraction < 1 || trace_startsolid)
163 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
164 if (trace_fraction < 1 || trace_startsolid)
166 if(autocvar_bot_debug_tracewalk)
167 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
170 traceline( org, move, movemode, e);
171 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
175 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
177 nextmove = move + (dir * stepdist);
178 traceline( move, nextmove, movemode, e);
184 if(autocvar_bot_debug_tracewalk)
185 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
187 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
188 //te_explosion(trace_endpos);
189 //print(ftos(e.dphitcontentsmask), "\n");
190 return false; // failed
202 // trace down from stepheight as far as possible and move there,
203 // if this starts in solid we try again without the stepup, and
204 // if that also fails we assume it is a wall
205 // (this is the same logic as the Quake walkmove function used)
206 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
208 // moved successfully
212 c = pointcontents(org + '0 0 1');
213 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
223 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
225 // moved but didn't arrive at the intended destination
226 if(autocvar_bot_debug_tracewalk)
227 debugnodestatus(org, DEBUG_NODE_FAIL);
232 /////////////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////////////
236 // completely empty the goal stack, used when deciding where to go
237 void navigation_clearroute()
239 //print("bot ", etos(self), " clear\n");
240 self.navigation_hasgoals = false;
241 self.goalcurrent = world;
242 self.goalstack01 = world;
243 self.goalstack02 = world;
244 self.goalstack03 = world;
245 self.goalstack04 = world;
246 self.goalstack05 = world;
247 self.goalstack06 = world;
248 self.goalstack07 = world;
249 self.goalstack08 = world;
250 self.goalstack09 = world;
251 self.goalstack10 = world;
252 self.goalstack11 = world;
253 self.goalstack12 = world;
254 self.goalstack13 = world;
255 self.goalstack14 = world;
256 self.goalstack15 = world;
257 self.goalstack16 = world;
258 self.goalstack17 = world;
259 self.goalstack18 = world;
260 self.goalstack19 = world;
261 self.goalstack20 = world;
262 self.goalstack21 = world;
263 self.goalstack22 = world;
264 self.goalstack23 = world;
265 self.goalstack24 = world;
266 self.goalstack25 = world;
267 self.goalstack26 = world;
268 self.goalstack27 = world;
269 self.goalstack28 = world;
270 self.goalstack29 = world;
271 self.goalstack30 = world;
272 self.goalstack31 = world;
275 // add a new goal at the beginning of the stack
276 // (in other words: add a new prerequisite before going to the later goals)
277 // NOTE: when a waypoint is added, the WP gets pushed first, then the
278 // next-closest WP on the shortest path to the WP
279 // That means, if the stack overflows, the bot will know how to do the FIRST 32
280 // steps to the goal, and then recalculate the path.
281 void navigation_pushroute(entity e)
283 //print("bot ", etos(self), " push ", etos(e), "\n");
284 self.goalstack31 = self.goalstack30;
285 self.goalstack30 = self.goalstack29;
286 self.goalstack29 = self.goalstack28;
287 self.goalstack28 = self.goalstack27;
288 self.goalstack27 = self.goalstack26;
289 self.goalstack26 = self.goalstack25;
290 self.goalstack25 = self.goalstack24;
291 self.goalstack24 = self.goalstack23;
292 self.goalstack23 = self.goalstack22;
293 self.goalstack22 = self.goalstack21;
294 self.goalstack21 = self.goalstack20;
295 self.goalstack20 = self.goalstack19;
296 self.goalstack19 = self.goalstack18;
297 self.goalstack18 = self.goalstack17;
298 self.goalstack17 = self.goalstack16;
299 self.goalstack16 = self.goalstack15;
300 self.goalstack15 = self.goalstack14;
301 self.goalstack14 = self.goalstack13;
302 self.goalstack13 = self.goalstack12;
303 self.goalstack12 = self.goalstack11;
304 self.goalstack11 = self.goalstack10;
305 self.goalstack10 = self.goalstack09;
306 self.goalstack09 = self.goalstack08;
307 self.goalstack08 = self.goalstack07;
308 self.goalstack07 = self.goalstack06;
309 self.goalstack06 = self.goalstack05;
310 self.goalstack05 = self.goalstack04;
311 self.goalstack04 = self.goalstack03;
312 self.goalstack03 = self.goalstack02;
313 self.goalstack02 = self.goalstack01;
314 self.goalstack01 = self.goalcurrent;
315 self.goalcurrent = e;
318 // remove first goal from stack
319 // (in other words: remove a prerequisite for reaching the later goals)
320 // (used when a spawnfunc_waypoint is reached)
321 void navigation_poproute()
323 //print("bot ", etos(self), " pop\n");
324 self.goalcurrent = self.goalstack01;
325 self.goalstack01 = self.goalstack02;
326 self.goalstack02 = self.goalstack03;
327 self.goalstack03 = self.goalstack04;
328 self.goalstack04 = self.goalstack05;
329 self.goalstack05 = self.goalstack06;
330 self.goalstack06 = self.goalstack07;
331 self.goalstack07 = self.goalstack08;
332 self.goalstack08 = self.goalstack09;
333 self.goalstack09 = self.goalstack10;
334 self.goalstack10 = self.goalstack11;
335 self.goalstack11 = self.goalstack12;
336 self.goalstack12 = self.goalstack13;
337 self.goalstack13 = self.goalstack14;
338 self.goalstack14 = self.goalstack15;
339 self.goalstack15 = self.goalstack16;
340 self.goalstack16 = self.goalstack17;
341 self.goalstack17 = self.goalstack18;
342 self.goalstack18 = self.goalstack19;
343 self.goalstack19 = self.goalstack20;
344 self.goalstack20 = self.goalstack21;
345 self.goalstack21 = self.goalstack22;
346 self.goalstack22 = self.goalstack23;
347 self.goalstack23 = self.goalstack24;
348 self.goalstack24 = self.goalstack25;
349 self.goalstack25 = self.goalstack26;
350 self.goalstack26 = self.goalstack27;
351 self.goalstack27 = self.goalstack28;
352 self.goalstack28 = self.goalstack29;
353 self.goalstack29 = self.goalstack30;
354 self.goalstack30 = self.goalstack31;
355 self.goalstack31 = world;
358 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
361 dist = vlen(v - org);
364 traceline(v, org, true, ent);
365 if (trace_fraction == 1)
369 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
374 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
382 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
383 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
385 entity waylist, w, best;
386 vector v, org, pm1, pm2;
388 pm1 = ent.origin + ent.mins;
389 pm2 = ent.origin + ent.maxs;
390 waylist = findchain(classname, "waypoint");
392 // do two scans, because box test is cheaper
396 // if object is touching spawnfunc_waypoint
397 if(w != ent && w != except)
398 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
403 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
404 org.z = ent.origin.z + ent.mins.z - PL_MIN.z; // player height
405 // TODO possibly make other code have the same support for bboxes
407 org = org + ent.tag_entity.origin;
408 if (navigation_testtracewalk)
413 // box check failed, try walk
417 // if object can walk from spawnfunc_waypoint
423 wm1 = w.origin + w.mins;
424 wm2 = w.origin + w.maxs;
425 v.x = bound(wm1_x, org.x, wm2_x);
426 v.y = bound(wm1_y, org.y, wm2_y);
427 v.z = bound(wm1_z, org.z, wm2_z);
431 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
433 bestdist = vlen(v - org);
441 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
443 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, world);
444 if (autocvar_g_waypointeditor_auto)
446 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
448 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
453 // finds the waypoints near the bot initiating a navigation query
454 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
457 vector v, m1, m2, diff;
459 // navigation_testtracewalk = true;
464 if (!head.wpconsidered)
468 m1 = head.origin + head.mins;
469 m2 = head.origin + head.maxs;
471 v.x = bound(m1_x, v.x, m2_x);
472 v.y = bound(m1_y, v.y, m2_y);
473 v.z = bound(m1_z, v.z, m2_z);
477 diff = v - self.origin;
478 diff.z = max(0, diff.z);
479 if (vlen(diff) < maxdist)
481 head.wpconsidered = true;
482 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
484 head.wpnearestpoint = v;
485 head.wpcost = vlen(v - self.origin) + head.dmg;
494 //navigation_testtracewalk = false;
498 // updates a path link if a spawnfunc_waypoint link is better than the current one
499 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
508 v.x = bound(m1_x, p.x, m2_x);
509 v.y = bound(m1_y, p.y, m2_y);
510 v.z = bound(m1_z, p.z, m2_z);
514 cost2 = cost2 + vlen(v - p);
515 if (wp.wpcost > cost2)
520 wp.wpnearestpoint = v;
524 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
525 void navigation_markroutes(entity fixed_source_waypoint)
527 entity w, wp, waylist;
528 float searching, cost, cost2;
530 w = waylist = findchain(classname, "waypoint");
533 w.wpconsidered = false;
534 w.wpnearestpoint = '0 0 0';
541 if(fixed_source_waypoint)
543 fixed_source_waypoint.wpconsidered = true;
544 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
545 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
546 fixed_source_waypoint.wpfire = 1;
547 fixed_source_waypoint.enemy = world;
551 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
552 // as this search is expensive we will use lower values if the bot is on the air
553 float i, increment, maxdistance;
554 if(self.flags & FL_ONGROUND)
565 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
580 p = w.wpnearestpoint;
581 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
600 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
601 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
602 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
603 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
604 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
605 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
606 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
607 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
608 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
609 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
610 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
611 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
612 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
613 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
620 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
621 void navigation_markroutes_inverted(entity fixed_source_waypoint)
623 entity w, wp, waylist;
624 float searching, cost, cost2;
626 w = waylist = findchain(classname, "waypoint");
629 w.wpconsidered = false;
630 w.wpnearestpoint = '0 0 0';
637 if(fixed_source_waypoint)
639 fixed_source_waypoint.wpconsidered = true;
640 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
641 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
642 fixed_source_waypoint.wpfire = 1;
643 fixed_source_waypoint.enemy = world;
647 error("need to start with a waypoint\n");
661 cost = w.wpcost; // cost to walk from w to home
662 p = w.wpnearestpoint;
663 for(wp = waylist; wp; wp = wp.chain)
665 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
666 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
667 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
668 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
669 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
670 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
671 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
672 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
674 cost2 = cost + wp.dmg;
675 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
683 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
684 void navigation_routerating(entity e, float f, float rangebias)
694 o = (e.absmin + e.absmax) * 0.5;
696 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
698 // Evaluate path using jetpack
700 if(self.items & IT_JETPACK)
701 if(autocvar_bot_ai_navigation_jetpack)
702 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
704 vector pointa, pointb;
706 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
709 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
710 pointa = trace_endpos - '0 0 1';
713 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
714 pointb = trace_endpos - '0 0 1';
716 // Can I see these two points from the sky?
717 traceline(pointa, pointb, MOVE_NORMAL, self);
719 if(trace_fraction==1)
721 bot_debug("jetpack ai: can bridge these two points\n");
723 // Lower the altitude of these points as much as possible
724 float zdistance, xydistance, cost, t, fuel;
725 vector down, npa, npb;
727 down = '0 0 -1' * (PL_MAX.z - PL_MIN.z) * 10;
733 if(npa.z<=self.absmax.z)
736 if(npb.z<=e.absmax.z)
739 traceline(npa, npb, MOVE_NORMAL, self);
740 if(trace_fraction==1)
746 while(trace_fraction == 1);
749 // Rough estimation of fuel consumption
750 // (ignores acceleration and current xyz velocity)
751 xydistance = vlen(pointa - pointb);
752 zdistance = fabs(pointa.z - self.origin.z);
754 t = zdistance / autocvar_g_jetpack_maxspeed_up;
755 t += xydistance / autocvar_g_jetpack_maxspeed_side;
756 fuel = t * autocvar_g_jetpack_fuel * 0.8;
758 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
761 if(self.ammo_fuel>fuel)
764 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
765 // - between air and ground speeds)
767 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
768 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
771 // Compare against other goals
772 f = f * rangebias / (rangebias + cost);
774 if (navigation_bestrating < f)
776 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
777 navigation_bestrating = f;
778 navigation_bestgoal = e;
779 self.navigation_jetpack_goal = e;
780 self.navigation_jetpack_point = pointb;
787 //te_wizspike(e.origin);
790 // update the cached spawnfunc_waypoint link on a dynamic item entity
791 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
801 if(e.flags & FL_ITEM)
803 if (!(e.flags & FL_WEAPON))
804 if(e.nearestwaypoint)
807 else if (e.flags & FL_WEAPON)
809 if(e.classname != "droppedweapon")
810 if(e.nearestwaypoint)
815 if (time > e.nearestwaypointtimeout)
817 nwp = navigation_findnearestwaypoint(e, true);
819 e.nearestwaypoint = nwp;
822 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
824 if(e.flags & FL_ITEM)
825 e.blacklisted = true;
826 else if (e.flags & FL_WEAPON)
828 if(e.classname != "droppedweapon")
829 e.blacklisted = true;
834 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
839 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
840 if(e.classname=="item_flag_team")
841 e.nearestwaypointtimeout = time + 2;
843 e.nearestwaypointtimeout = time + random() * 3 + 5;
845 nwp = e.nearestwaypoint;
848 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
850 if (nwp.wpcost < 10000000)
852 //te_wizspike(nwp.wpnearestpoint);
853 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
854 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
855 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
856 if (navigation_bestrating < f)
858 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
859 navigation_bestrating = f;
860 navigation_bestgoal = e;
865 // adds an item to the the goal stack with the path to a given item
866 float navigation_routetogoal(entity e, vector startposition)
870 // if there is no goal, just exit
874 self.navigation_hasgoals = true;
876 // put the entity on the goal stack
877 //print("routetogoal ", etos(e), "\n");
878 navigation_pushroute(e);
881 if(e==self.navigation_jetpack_goal)
884 // if it can reach the goal there is nothing more to do
885 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
888 // see if there are waypoints describing a path to the item
889 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
890 e = e.nearestwaypoint;
892 e = e.enemy; // we already have added it, so...
899 // add the spawnfunc_waypoint to the path
900 navigation_pushroute(e);
910 // removes any currently touching waypoints from the goal stack
911 // (this is how bots detect if they reached a goal)
912 void navigation_poptouchedgoals()
916 m1 = org + self.mins;
917 m2 = org + self.maxs;
919 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
921 if(self.lastteleporttime>0)
922 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
924 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
925 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
927 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
928 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
930 navigation_poproute();
935 // If for some reason the bot is closer to the next goal, pop the current one
937 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
938 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
939 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
941 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
942 navigation_poproute();
943 // TODO this may also be a nice idea to do "early" (e.g. by
944 // manipulating the vlen() comparisons) to shorten paths in
945 // general - this would make bots walk more "on rails" than
946 // "zigzagging" which they currently do with sufficiently
947 // random-like waypoints, and thus can make a nice bot
948 // personality property
951 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
952 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
953 if(IS_PLAYER(self.goalcurrent))
954 navigation_poproute();
956 // aid for detecting jump pads better (distance based check fails sometimes)
957 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
958 navigation_poproute();
960 // Loose goal touching check when running
961 if(self.aistatus & AI_STATUS_RUNNING)
962 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
963 if(self.goalcurrent.classname=="waypoint")
965 if(vlen(self.origin - self.goalcurrent.origin)<150)
967 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, true, world);
968 if(trace_fraction==1)
970 // Detect personal waypoints
971 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
972 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
974 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
975 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
978 navigation_poproute();
983 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
985 // Detect personal waypoints
986 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
987 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
989 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
990 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
993 navigation_poproute();
997 // begin a goal selection session (queries spawnfunc_waypoint network)
998 void navigation_goalrating_start()
1000 if(self.aistatus & AI_STATUS_STUCK)
1003 self.navigation_jetpack_goal = world;
1004 navigation_bestrating = -1;
1005 self.navigation_hasgoals = false;
1006 navigation_clearroute();
1007 navigation_bestgoal = world;
1008 navigation_markroutes(world);
1011 // ends a goal selection session (updates goal stack to the best goal)
1012 void navigation_goalrating_end()
1014 if(self.aistatus & AI_STATUS_STUCK)
1017 navigation_routetogoal(navigation_bestgoal, self.origin);
1018 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
1020 // If the bot got stuck then try to reach the farthest waypoint
1021 if (!self.navigation_hasgoals)
1022 if (autocvar_bot_wander_enable)
1024 if (!(self.aistatus & AI_STATUS_STUCK))
1026 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1027 self.aistatus |= AI_STATUS_STUCK;
1030 self.navigation_hasgoals = false; // Reset this value
1034 void botframe_updatedangerousobjects(float maxupdate)
1036 entity head, bot_dodgelist;
1037 vector m1, m2, v, o;
1040 bot_dodgelist = findchainfloat(bot_dodge, true);
1041 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1042 while (botframe_dangerwaypoint != world)
1045 m1 = botframe_dangerwaypoint.mins;
1046 m2 = botframe_dangerwaypoint.maxs;
1047 head = bot_dodgelist;
1051 v.x = bound(m1_x, v.x, m2_x);
1052 v.y = bound(m1_y, v.y, m2_y);
1053 v.z = bound(m1_z, v.z, m2_z);
1054 o = (head.absmin + head.absmax) * 0.5;
1055 d = head.bot_dodgerating - vlen(o - v);
1058 traceline(o, v, true, world);
1059 if (trace_fraction == 1)
1060 danger = danger + d;
1064 botframe_dangerwaypoint.dmg = danger;
1068 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1072 void navigation_unstuck()
1074 float search_radius = 1000;
1076 if (!autocvar_bot_wander_enable)
1079 if (!bot_waypoint_queue_owner)
1081 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1082 bot_waypoint_queue_owner = self;
1083 bot_waypoint_queue_bestgoal = world;
1084 bot_waypoint_queue_bestgoalrating = 0;
1087 if(bot_waypoint_queue_owner!=self)
1090 if (bot_waypoint_queue_goal)
1092 // evaluate the next goal on the queue
1093 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1094 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1095 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1097 if( d > bot_waypoint_queue_bestgoalrating)
1099 bot_waypoint_queue_bestgoalrating = d;
1100 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1103 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1105 if (!bot_waypoint_queue_goal)
1107 if (bot_waypoint_queue_bestgoal)
1109 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1110 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1111 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1112 self.aistatus &= ~AI_STATUS_STUCK;
1116 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1119 bot_waypoint_queue_owner = world;
1124 if(bot_strategytoken!=self)
1127 // build a new queue
1128 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1133 head = findradius(self.origin, search_radius);
1137 if(head.classname=="waypoint")
1138 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1140 if(bot_waypoint_queue_goal)
1141 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1145 bot_waypoint_queue_goal = head;
1146 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1153 bot_waypoint_queue_goal = first;
1156 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1157 bot_waypoint_queue_owner = world;
1162 // Support for debugging tracewalk visually
1164 void debugresetnodes()
1166 debuglastnode = '0 0 0';
1169 void debugnode(vector node)
1171 if (!IS_PLAYER(self))
1174 if(debuglastnode=='0 0 0')
1176 debuglastnode = node;
1180 te_lightning2(world, node, debuglastnode);
1181 debuglastnode = node;
1184 void debugnodestatus(vector position, float status)
1190 case DEBUG_NODE_SUCCESS:
1193 case DEBUG_NODE_WARNING:
1196 case DEBUG_NODE_FAIL:
1203 te_customflash(position, 40, 2, c);
1206 // Support for debugging the goal stack visually
1209 .vector lastposition;
1211 // Debug the goal stack visually
1212 void debuggoalstack()
1217 if(self.goalcounter==0)goal=self.goalcurrent;
1218 else if(self.goalcounter==1)goal=self.goalstack01;
1219 else if(self.goalcounter==2)goal=self.goalstack02;
1220 else if(self.goalcounter==3)goal=self.goalstack03;
1221 else if(self.goalcounter==4)goal=self.goalstack04;
1222 else if(self.goalcounter==5)goal=self.goalstack05;
1223 else if(self.goalcounter==6)goal=self.goalstack06;
1224 else if(self.goalcounter==7)goal=self.goalstack07;
1225 else if(self.goalcounter==8)goal=self.goalstack08;
1226 else if(self.goalcounter==9)goal=self.goalstack09;
1227 else if(self.goalcounter==10)goal=self.goalstack10;
1228 else if(self.goalcounter==11)goal=self.goalstack11;
1229 else if(self.goalcounter==12)goal=self.goalstack12;
1230 else if(self.goalcounter==13)goal=self.goalstack13;
1231 else if(self.goalcounter==14)goal=self.goalstack14;
1232 else if(self.goalcounter==15)goal=self.goalstack15;
1233 else if(self.goalcounter==16)goal=self.goalstack16;
1234 else if(self.goalcounter==17)goal=self.goalstack17;
1235 else if(self.goalcounter==18)goal=self.goalstack18;
1236 else if(self.goalcounter==19)goal=self.goalstack19;
1237 else if(self.goalcounter==20)goal=self.goalstack20;
1238 else if(self.goalcounter==21)goal=self.goalstack21;
1239 else if(self.goalcounter==22)goal=self.goalstack22;
1240 else if(self.goalcounter==23)goal=self.goalstack23;
1241 else if(self.goalcounter==24)goal=self.goalstack24;
1242 else if(self.goalcounter==25)goal=self.goalstack25;
1243 else if(self.goalcounter==26)goal=self.goalstack26;
1244 else if(self.goalcounter==27)goal=self.goalstack27;
1245 else if(self.goalcounter==28)goal=self.goalstack28;
1246 else if(self.goalcounter==29)goal=self.goalstack29;
1247 else if(self.goalcounter==30)goal=self.goalstack30;
1248 else if(self.goalcounter==31)goal=self.goalstack31;
1253 self.goalcounter = 0;
1254 self.lastposition='0 0 0';
1258 if(self.lastposition=='0 0 0')
1261 org = self.lastposition;
1264 go = ( goal.absmin + goal.absmax ) * 0.5;
1265 te_lightning2(world, org, go);
1266 self.lastposition = go;