2 // rough simulation of walking from one point to another to test if a path
3 // can be traveled, used for waypoint linking and havocbot
5 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
17 if(autocvar_bot_debug_tracewalk)
26 dist = totaldist = vlen(move);
27 dir = normalize(move);
29 ignorehazards = FALSE;
31 // Analyze starting point
32 traceline(start, start, MOVE_NORMAL, e);
33 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
37 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
38 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
44 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
48 if(autocvar_bot_debug_tracewalk)
49 debugnodestatus(start, DEBUG_NODE_FAIL);
51 //print("tracewalk: ", vtos(start), " is a bad start\n");
60 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
63 if(autocvar_bot_debug_tracewalk)
64 debugnodestatus(org, DEBUG_NODE_SUCCESS);
66 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
69 if(autocvar_bot_debug_tracewalk)
76 dist = dist - stepdist;
77 traceline(org, org, MOVE_NORMAL, e);
80 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
82 // hazards blocking path
83 if(autocvar_bot_debug_tracewalk)
84 debugnodestatus(org, DEBUG_NODE_FAIL);
86 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
90 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
92 move = normalize(end - org);
93 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
95 if(autocvar_bot_debug_tracewalk)
96 debugnode(trace_endpos);
98 if (trace_fraction < 1)
101 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
102 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
104 if(autocvar_bot_debug_tracewalk)
107 if(pointcontents(org) == CONTENT_EMPTY)
111 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
113 if(autocvar_bot_debug_tracewalk)
114 debugnodestatus(org, DEBUG_NODE_FAIL);
117 //print("tracewalk: ", vtos(start), " failed under water\n");
127 move = dir * stepdist + org;
128 tracebox(org, m1, m2, move, movemode, e);
130 if(autocvar_bot_debug_tracewalk)
131 debugnode(trace_endpos);
134 if (trace_fraction < 1)
136 // check if we can walk over this obstacle, possibly by jumpstepping
137 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
138 if (trace_fraction < 1 || trace_startsolid)
140 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
141 if (trace_fraction < 1 || trace_startsolid)
143 if(autocvar_bot_debug_tracewalk)
144 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
147 traceline( org, move, movemode, e);
148 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
152 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
154 nextmove = move + (dir * stepdist);
155 traceline( move, nextmove, movemode, e);
161 if(autocvar_bot_debug_tracewalk)
162 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
164 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
165 //te_explosion(trace_endpos);
166 //print(ftos(e.dphitcontentsmask), "\n");
167 return FALSE; // failed
179 // trace down from stepheight as far as possible and move there,
180 // if this starts in solid we try again without the stepup, and
181 // if that also fails we assume it is a wall
182 // (this is the same logic as the Quake walkmove function used)
183 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
185 // moved successfully
189 c = pointcontents(org + '0 0 1');
190 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
200 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
202 // moved but didn't arrive at the intended destination
203 if(autocvar_bot_debug_tracewalk)
204 debugnodestatus(org, DEBUG_NODE_FAIL);
209 /////////////////////////////////////////////////////////////////////////////
211 /////////////////////////////////////////////////////////////////////////////
213 // completely empty the goal stack, used when deciding where to go
214 void navigation_clearroute()
216 //print("bot ", etos(self), " clear\n");
217 self.navigation_hasgoals = FALSE;
218 self.goalcurrent = world;
219 self.goalstack01 = world;
220 self.goalstack02 = world;
221 self.goalstack03 = world;
222 self.goalstack04 = world;
223 self.goalstack05 = world;
224 self.goalstack06 = world;
225 self.goalstack07 = world;
226 self.goalstack08 = world;
227 self.goalstack09 = world;
228 self.goalstack10 = world;
229 self.goalstack11 = world;
230 self.goalstack12 = world;
231 self.goalstack13 = world;
232 self.goalstack14 = world;
233 self.goalstack15 = world;
234 self.goalstack16 = world;
235 self.goalstack17 = world;
236 self.goalstack18 = world;
237 self.goalstack19 = world;
238 self.goalstack20 = world;
239 self.goalstack21 = world;
240 self.goalstack22 = world;
241 self.goalstack23 = world;
242 self.goalstack24 = world;
243 self.goalstack25 = world;
244 self.goalstack26 = world;
245 self.goalstack27 = world;
246 self.goalstack28 = world;
247 self.goalstack29 = world;
248 self.goalstack30 = world;
249 self.goalstack31 = world;
252 // add a new goal at the beginning of the stack
253 // (in other words: add a new prerequisite before going to the later goals)
254 // NOTE: when a waypoint is added, the WP gets pushed first, then the
255 // next-closest WP on the shortest path to the WP
256 // That means, if the stack overflows, the bot will know how to do the FIRST 32
257 // steps to the goal, and then recalculate the path.
258 void navigation_pushroute(entity e)
260 //print("bot ", etos(self), " push ", etos(e), "\n");
261 self.goalstack31 = self.goalstack30;
262 self.goalstack30 = self.goalstack29;
263 self.goalstack29 = self.goalstack28;
264 self.goalstack28 = self.goalstack27;
265 self.goalstack27 = self.goalstack26;
266 self.goalstack26 = self.goalstack25;
267 self.goalstack25 = self.goalstack24;
268 self.goalstack24 = self.goalstack23;
269 self.goalstack23 = self.goalstack22;
270 self.goalstack22 = self.goalstack21;
271 self.goalstack21 = self.goalstack20;
272 self.goalstack20 = self.goalstack19;
273 self.goalstack19 = self.goalstack18;
274 self.goalstack18 = self.goalstack17;
275 self.goalstack17 = self.goalstack16;
276 self.goalstack16 = self.goalstack15;
277 self.goalstack15 = self.goalstack14;
278 self.goalstack14 = self.goalstack13;
279 self.goalstack13 = self.goalstack12;
280 self.goalstack12 = self.goalstack11;
281 self.goalstack11 = self.goalstack10;
282 self.goalstack10 = self.goalstack09;
283 self.goalstack09 = self.goalstack08;
284 self.goalstack08 = self.goalstack07;
285 self.goalstack07 = self.goalstack06;
286 self.goalstack06 = self.goalstack05;
287 self.goalstack05 = self.goalstack04;
288 self.goalstack04 = self.goalstack03;
289 self.goalstack03 = self.goalstack02;
290 self.goalstack02 = self.goalstack01;
291 self.goalstack01 = self.goalcurrent;
292 self.goalcurrent = e;
295 // remove first goal from stack
296 // (in other words: remove a prerequisite for reaching the later goals)
297 // (used when a spawnfunc_waypoint is reached)
298 void navigation_poproute()
300 //print("bot ", etos(self), " pop\n");
301 self.goalcurrent = self.goalstack01;
302 self.goalstack01 = self.goalstack02;
303 self.goalstack02 = self.goalstack03;
304 self.goalstack03 = self.goalstack04;
305 self.goalstack04 = self.goalstack05;
306 self.goalstack05 = self.goalstack06;
307 self.goalstack06 = self.goalstack07;
308 self.goalstack07 = self.goalstack08;
309 self.goalstack08 = self.goalstack09;
310 self.goalstack09 = self.goalstack10;
311 self.goalstack10 = self.goalstack11;
312 self.goalstack11 = self.goalstack12;
313 self.goalstack12 = self.goalstack13;
314 self.goalstack13 = self.goalstack14;
315 self.goalstack14 = self.goalstack15;
316 self.goalstack15 = self.goalstack16;
317 self.goalstack16 = self.goalstack17;
318 self.goalstack17 = self.goalstack18;
319 self.goalstack18 = self.goalstack19;
320 self.goalstack19 = self.goalstack20;
321 self.goalstack20 = self.goalstack21;
322 self.goalstack21 = self.goalstack22;
323 self.goalstack22 = self.goalstack23;
324 self.goalstack23 = self.goalstack24;
325 self.goalstack24 = self.goalstack25;
326 self.goalstack25 = self.goalstack26;
327 self.goalstack26 = self.goalstack27;
328 self.goalstack27 = self.goalstack28;
329 self.goalstack28 = self.goalstack29;
330 self.goalstack29 = self.goalstack30;
331 self.goalstack30 = self.goalstack31;
332 self.goalstack31 = world;
335 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
338 dist = vlen(v - org);
341 traceline(v, org, TRUE, ent);
342 if (trace_fraction == 1)
346 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
351 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
359 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
360 entity navigation_findnearestwaypoint_withdist(entity ent, float walkfromwp, float bestdist)
362 entity waylist, w, best;
364 vector v, org, pm1, pm2;
366 pm1 = ent.origin + ent.mins;
367 pm2 = ent.origin + ent.maxs;
368 waylist = findchain(classname, "waypoint");
370 // do two scans, because box test is cheaper
374 // if object is touching spawnfunc_waypoint
376 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
381 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
382 org_z = ent.origin_z + ent.mins_z - PL_MIN_z; // player height
383 // TODO possibly make other code have the same support for bboxes
385 org = org + ent.tag_entity.origin;
386 if (navigation_testtracewalk)
391 // box check failed, try walk
395 // if object can walk from spawnfunc_waypoint
401 wm1 = w.origin + w.mins;
402 wm2 = w.origin + w.maxs;
403 v_x = bound(wm1_x, org_x, wm2_x);
404 v_y = bound(wm1_y, org_y, wm2_y);
405 v_z = bound(wm1_z, org_z, wm2_z);
409 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
411 bestdist = vlen(v - org);
419 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
421 return navigation_findnearestwaypoint_withdist(ent, walkfromwp, 1050);
424 // finds the waypoints near the bot initiating a navigation query
425 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
428 vector v, m1, m2, diff;
430 // navigation_testtracewalk = TRUE;
435 if (!head.wpconsidered)
439 m1 = head.origin + head.mins;
440 m2 = head.origin + head.maxs;
442 v_x = bound(m1_x, v_x, m2_x);
443 v_y = bound(m1_y, v_y, m2_y);
444 v_z = bound(m1_z, v_z, m2_z);
448 diff = v - self.origin;
449 diff_z = max(0, diff_z);
450 if (vlen(diff) < maxdist)
452 head.wpconsidered = TRUE;
453 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
455 head.wpnearestpoint = v;
456 head.wpcost = vlen(v - self.origin) + head.dmg;
465 //navigation_testtracewalk = FALSE;
469 // updates a path link if a spawnfunc_waypoint link is better than the current one
470 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
479 v_x = bound(m1_x, p_x, m2_x);
480 v_y = bound(m1_y, p_y, m2_y);
481 v_z = bound(m1_z, p_z, m2_z);
485 cost2 = cost2 + vlen(v - p);
486 if (wp.wpcost > cost2)
491 wp.wpnearestpoint = v;
495 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
496 void navigation_markroutes(entity fixed_source_waypoint)
498 entity w, wp, waylist;
499 float searching, cost, cost2;
501 w = waylist = findchain(classname, "waypoint");
504 w.wpconsidered = FALSE;
505 w.wpnearestpoint = '0 0 0';
512 if(fixed_source_waypoint)
514 fixed_source_waypoint.wpconsidered = TRUE;
515 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
516 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
517 fixed_source_waypoint.wpfire = 1;
518 fixed_source_waypoint.enemy = world;
522 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
523 // as this search is expensive we will use lower values if the bot is on the air
524 float i, increment, maxdistance;
525 if(self.flags & FL_ONGROUND)
536 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
551 p = w.wpnearestpoint;
552 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
553 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
554 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
555 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
556 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
557 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
558 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
559 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
560 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
561 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
562 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
563 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
564 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
565 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
566 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
567 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
591 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
592 void navigation_markroutes_inverted(entity fixed_source_waypoint)
594 entity w, wp, waylist;
595 float searching, cost, cost2;
597 w = waylist = findchain(classname, "waypoint");
600 w.wpconsidered = FALSE;
601 w.wpnearestpoint = '0 0 0';
608 if(fixed_source_waypoint)
610 fixed_source_waypoint.wpconsidered = TRUE;
611 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
612 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
613 fixed_source_waypoint.wpfire = 1;
614 fixed_source_waypoint.enemy = world;
618 error("need to start with a waypoint\n");
632 cost = w.wpcost; // cost to walk from w to home
633 p = w.wpnearestpoint;
634 for(wp = waylist; wp; wp = wp.chain)
636 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
637 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
638 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
639 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
640 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
641 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
642 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
643 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
645 cost2 = cost + wp.dmg;
646 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
654 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
655 void navigation_routerating(entity e, float f, float rangebias)
665 o = (e.absmin + e.absmax) * 0.5;
667 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
669 // Evaluate path using jetpack
671 if(self.items & IT_JETPACK)
672 if(autocvar_bot_ai_navigation_jetpack)
673 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
675 vector pointa, pointb;
677 // dprint("jetpack ai: evaluating path for ", e.classname,"\n");
680 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
681 pointa = trace_endpos - '0 0 1';
684 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
685 pointb = trace_endpos - '0 0 1';
687 // Can I see these two points from the sky?
688 traceline(pointa, pointb, MOVE_NORMAL, self);
690 if(trace_fraction==1)
692 // dprint("jetpack ai: can bridge these two points\n");
694 // Lower the altitude of these points as much as possible
695 float zdistance, xydistance, cost, t, fuel;
696 vector down, npa, npb;
698 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
704 if(npa_z<=self.absmax_z)
707 if(npb_z<=e.absmax_z)
710 traceline(npa, npb, MOVE_NORMAL, self);
711 if(trace_fraction==1)
717 while(trace_fraction == 1);
720 // Rough estimation of fuel consumption
721 // (ignores acceleration and current xyz velocity)
722 xydistance = vlen(pointa - pointb);
723 zdistance = fabs(pointa_z - self.origin_z);
725 t = zdistance / autocvar_g_jetpack_maxspeed_up;
726 t += xydistance / autocvar_g_jetpack_maxspeed_side;
727 fuel = t * autocvar_g_jetpack_fuel * 0.8;
729 // dprint("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel),"\n");
732 if(self.ammo_fuel>fuel)
735 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
736 // - between air and ground speeds)
738 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
739 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
742 // Compare against other goals
743 f = f * rangebias / (rangebias + cost);
745 if (navigation_bestrating < f)
747 // dprint("jetpack path: added goal", e.classname, " (with rating ", ftos(f), ")\n");
748 navigation_bestrating = f;
749 navigation_bestgoal = e;
750 self.navigation_jetpack_goal = e;
751 self.navigation_jetpack_point = pointb;
758 //te_wizspike(e.origin);
761 // update the cached spawnfunc_waypoint link on a dynamic item entity
762 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
772 if(e.flags & FL_ITEM)
774 if not(e.flags & FL_WEAPON)
775 if(e.nearestwaypoint)
778 else if (e.flags & FL_WEAPON)
780 if(e.classname != "droppedweapon")
781 if(e.nearestwaypoint)
786 if (time > e.nearestwaypointtimeout)
788 nwp = navigation_findnearestwaypoint(e, TRUE);
790 e.nearestwaypoint = nwp;
793 dprint("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n");
795 if(e.flags & FL_ITEM)
796 e.blacklisted = TRUE;
797 else if (e.flags & FL_WEAPON)
799 if(e.classname != "droppedweapon")
800 e.blacklisted = TRUE;
805 dprint("The entity '", e.classname, "' is going to be excluded from path finding during this match\n");
810 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
811 if(e.classname=="item_flag_team")
812 e.nearestwaypointtimeout = time + 2;
814 e.nearestwaypointtimeout = time + random() * 3 + 5;
816 nwp = e.nearestwaypoint;
819 //dprint("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n");
821 if (nwp.wpcost < 10000000)
823 //te_wizspike(nwp.wpnearestpoint);
824 // dprint(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
825 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
826 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
828 if (navigation_bestrating < f)
830 // dprint("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n");
831 navigation_bestrating = f;
832 navigation_bestgoal = e;
838 // adds an item to the the goal stack with the path to a given item
839 float navigation_routetogoal(entity e, vector startposition)
843 // if there is no goal, just exit
847 self.navigation_hasgoals = TRUE;
849 // put the entity on the goal stack
850 //print("routetogoal ", etos(e), "\n");
851 navigation_pushroute(e);
854 if(e==self.navigation_jetpack_goal)
857 // if it can reach the goal there is nothing more to do
858 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
861 // see if there are waypoints describing a path to the item
862 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
863 e = e.nearestwaypoint;
865 e = e.enemy; // we already have added it, so...
872 // add the spawnfunc_waypoint to the path
873 navigation_pushroute(e);
883 // removes any currently touching waypoints from the goal stack
884 // (this is how bots detect if they reached a goal)
885 void navigation_poptouchedgoals()
889 m1 = org + self.mins;
890 m2 = org + self.maxs;
892 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
894 if(self.lastteleporttime>0)
895 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
897 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
898 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
900 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
901 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
903 navigation_poproute();
908 // If for some reason the bot is closer to the next goal, pop the current one
910 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
911 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
912 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
914 /// dprint("path optimized for ", self.netname, ", removed a goal from the queue\n");
915 navigation_poproute();
916 // TODO this may also be a nice idea to do "early" (e.g. by
917 // manipulating the vlen() comparisons) to shorten paths in
918 // general - this would make bots walk more "on rails" than
919 // "zigzagging" which they currently do with sufficiently
920 // random-like waypoints, and thus can make a nice bot
921 // personality property
924 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
925 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
926 if(self.goalcurrent.classname=="player")
927 navigation_poproute();
929 // aid for detecting jump pads better (distance based check fails sometimes)
930 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
931 navigation_poproute();
933 // Loose goal touching check when running
934 if(self.aistatus & AI_STATUS_RUNNING)
935 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
936 if(self.goalcurrent.classname=="waypoint")
938 if(vlen(self.origin - self.goalcurrent.origin)<150)
940 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
941 if(trace_fraction==1)
943 // Detect personal waypoints
944 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
945 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
947 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
948 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
951 navigation_poproute();
956 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
958 // Detect personal waypoints
959 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
960 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
962 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
963 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
966 navigation_poproute();
970 // begin a goal selection session (queries spawnfunc_waypoint network)
971 void navigation_goalrating_start()
973 if(self.aistatus & AI_STATUS_STUCK)
976 self.navigation_jetpack_goal = world;
977 navigation_bestrating = -1;
978 self.navigation_hasgoals = FALSE;
979 navigation_clearroute();
980 navigation_bestgoal = world;
981 navigation_markroutes(world);
984 // ends a goal selection session (updates goal stack to the best goal)
985 void navigation_goalrating_end()
987 if(self.aistatus & AI_STATUS_STUCK)
990 navigation_routetogoal(navigation_bestgoal, self.origin);
991 // dprint("best goal ", self.goalcurrent.classname , "\n");
993 // If the bot got stuck then try to reach the farthest waypoint
994 if not (self.navigation_hasgoals)
995 if (autocvar_bot_wander_enable)
997 if not(self.aistatus & AI_STATUS_STUCK)
999 dprint(self.netname, " cannot walk to any goal\n");
1000 self.aistatus |= AI_STATUS_STUCK;
1003 self.navigation_hasgoals = FALSE; // Reset this value
1007 void botframe_updatedangerousobjects(float maxupdate)
1009 entity head, bot_dodgelist;
1010 vector m1, m2, v, o;
1013 bot_dodgelist = findchainfloat(bot_dodge, TRUE);
1014 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1015 while (botframe_dangerwaypoint != world)
1018 m1 = botframe_dangerwaypoint.mins;
1019 m2 = botframe_dangerwaypoint.maxs;
1020 head = bot_dodgelist;
1024 v_x = bound(m1_x, v_x, m2_x);
1025 v_y = bound(m1_y, v_y, m2_y);
1026 v_z = bound(m1_z, v_z, m2_z);
1027 o = (head.absmin + head.absmax) * 0.5;
1028 d = head.bot_dodgerating - vlen(o - v);
1031 traceline(o, v, TRUE, world);
1032 if (trace_fraction == 1)
1033 danger = danger + d;
1037 botframe_dangerwaypoint.dmg = danger;
1041 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1045 void navigation_unstuck()
1047 float search_radius = 1000;
1049 if not(autocvar_bot_wander_enable)
1052 if not(bot_waypoint_queue_owner)
1054 // dprint(self.netname, " sutck, taking over the waypoints queue\n");
1055 bot_waypoint_queue_owner = self;
1056 bot_waypoint_queue_bestgoal = world;
1057 bot_waypoint_queue_bestgoalrating = 0;
1060 if(bot_waypoint_queue_owner!=self)
1063 if (bot_waypoint_queue_goal)
1065 // evaluate the next goal on the queue
1066 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1067 // dprint(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n");
1068 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1070 if( d > bot_waypoint_queue_bestgoalrating)
1072 bot_waypoint_queue_bestgoalrating = d;
1073 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1076 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1078 if not(bot_waypoint_queue_goal)
1080 if (bot_waypoint_queue_bestgoal)
1082 dprint(self.netname, " stuck, reachable waypoint found, heading to it\n");
1083 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1084 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1085 self.aistatus &~= AI_STATUS_STUCK;
1089 dprint(self.netname, " stuck, cannot walk to any waypoint at all\n");
1092 bot_waypoint_queue_owner = world;
1097 if(bot_strategytoken!=self)
1100 // build a new queue
1101 dprint(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n");
1106 head = findradius(self.origin, search_radius);
1110 if(head.classname=="waypoint")
1111 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1113 if(bot_waypoint_queue_goal)
1114 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1118 bot_waypoint_queue_goal = head;
1119 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1126 bot_waypoint_queue_goal = first;
1129 dprint(self.netname, " stuck, cannot walk to any waypoint at all\n");
1130 bot_waypoint_queue_owner = world;
1135 // Support for debugging tracewalk visually
1137 void debugresetnodes()
1139 debuglastnode = '0 0 0';
1142 void debugnode(vector node)
1144 if not(self.classname=="player")
1147 if(debuglastnode=='0 0 0')
1149 debuglastnode = node;
1153 te_lightning2(world, node, debuglastnode);
1154 debuglastnode = node;
1157 void debugnodestatus(vector position, float status)
1163 case DEBUG_NODE_SUCCESS:
1166 case DEBUG_NODE_WARNING:
1169 case DEBUG_NODE_FAIL:
1176 te_customflash(position, 40, 2, c);
1179 // Support for debugging the goal stack visually
1182 .vector lastposition;
1184 // Debug the goal stack visually
1185 void debuggoalstack()
1190 if(self.goalcounter==0)goal=self.goalcurrent;
1191 else if(self.goalcounter==1)goal=self.goalstack01;
1192 else if(self.goalcounter==2)goal=self.goalstack02;
1193 else if(self.goalcounter==3)goal=self.goalstack03;
1194 else if(self.goalcounter==4)goal=self.goalstack04;
1195 else if(self.goalcounter==5)goal=self.goalstack05;
1196 else if(self.goalcounter==6)goal=self.goalstack06;
1197 else if(self.goalcounter==7)goal=self.goalstack07;
1198 else if(self.goalcounter==8)goal=self.goalstack08;
1199 else if(self.goalcounter==9)goal=self.goalstack09;
1200 else if(self.goalcounter==10)goal=self.goalstack10;
1201 else if(self.goalcounter==11)goal=self.goalstack11;
1202 else if(self.goalcounter==12)goal=self.goalstack12;
1203 else if(self.goalcounter==13)goal=self.goalstack13;
1204 else if(self.goalcounter==14)goal=self.goalstack14;
1205 else if(self.goalcounter==15)goal=self.goalstack15;
1206 else if(self.goalcounter==16)goal=self.goalstack16;
1207 else if(self.goalcounter==17)goal=self.goalstack17;
1208 else if(self.goalcounter==18)goal=self.goalstack18;
1209 else if(self.goalcounter==19)goal=self.goalstack19;
1210 else if(self.goalcounter==20)goal=self.goalstack20;
1211 else if(self.goalcounter==21)goal=self.goalstack21;
1212 else if(self.goalcounter==22)goal=self.goalstack22;
1213 else if(self.goalcounter==23)goal=self.goalstack23;
1214 else if(self.goalcounter==24)goal=self.goalstack24;
1215 else if(self.goalcounter==25)goal=self.goalstack25;
1216 else if(self.goalcounter==26)goal=self.goalstack26;
1217 else if(self.goalcounter==27)goal=self.goalstack27;
1218 else if(self.goalcounter==28)goal=self.goalstack28;
1219 else if(self.goalcounter==29)goal=self.goalstack29;
1220 else if(self.goalcounter==30)goal=self.goalstack30;
1221 else if(self.goalcounter==31)goal=self.goalstack31;
1225 self.goalcounter = 0;
1226 self.lastposition='0 0 0';
1230 if(self.lastposition=='0 0 0')
1233 org = self.lastposition;
1236 go = ( goal.absmin + goal.absmax ) * 0.5;
1237 te_lightning2(world, org, go);
1238 self.lastposition = go;