1 void bot_debug(string input)
3 switch(autocvar_bot_debug)
5 case 1: dprint(input); break;
6 case 2: print(input); break;
10 // rough simulation of walking from one point to another to test if a path
11 // can be traveled, used for waypoint linking and havocbot
13 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
25 if(autocvar_bot_debug_tracewalk)
34 dist = totaldist = vlen(move);
35 dir = normalize(move);
37 ignorehazards = false;
40 // Analyze starting point
41 traceline(start, start, MOVE_NORMAL, e);
42 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
46 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
47 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
53 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
57 if(autocvar_bot_debug_tracewalk)
58 debugnodestatus(start, DEBUG_NODE_FAIL);
60 //print("tracewalk: ", vtos(start), " is a bad start\n");
69 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
72 if(autocvar_bot_debug_tracewalk)
73 debugnodestatus(org, DEBUG_NODE_SUCCESS);
75 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
78 if(autocvar_bot_debug_tracewalk)
85 dist = dist - stepdist;
86 traceline(org, org, MOVE_NORMAL, e);
89 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
91 // hazards blocking path
92 if(autocvar_bot_debug_tracewalk)
93 debugnodestatus(org, DEBUG_NODE_FAIL);
95 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
99 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
101 move = normalize(end - org);
102 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
104 if(autocvar_bot_debug_tracewalk)
105 debugnode(trace_endpos);
107 if (trace_fraction < 1)
110 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
111 for (; org.z < end.z + self.maxs.z; org.z += stepdist)
113 if(autocvar_bot_debug_tracewalk)
116 if(pointcontents(org) == CONTENT_EMPTY)
120 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
122 if(autocvar_bot_debug_tracewalk)
123 debugnodestatus(org, DEBUG_NODE_FAIL);
126 //print("tracewalk: ", vtos(start), " failed under water\n");
136 move = dir * stepdist + org;
137 tracebox(org, m1, m2, move, movemode, e);
139 if(autocvar_bot_debug_tracewalk)
140 debugnode(trace_endpos);
143 if (trace_fraction < 1)
145 // check if we can walk over this obstacle, possibly by jumpstepping
146 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
147 if (trace_fraction < 1 || trace_startsolid)
149 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
150 if (trace_fraction < 1 || trace_startsolid)
152 if(autocvar_bot_debug_tracewalk)
153 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
156 traceline( org, move, movemode, e);
157 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
161 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
163 nextmove = move + (dir * stepdist);
164 traceline( move, nextmove, movemode, e);
170 if(autocvar_bot_debug_tracewalk)
171 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
173 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
174 //te_explosion(trace_endpos);
175 //print(ftos(e.dphitcontentsmask), "\n");
176 return false; // failed
188 // trace down from stepheight as far as possible and move there,
189 // if this starts in solid we try again without the stepup, and
190 // if that also fails we assume it is a wall
191 // (this is the same logic as the Quake walkmove function used)
192 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
194 // moved successfully
198 c = pointcontents(org + '0 0 1');
199 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
209 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
211 // moved but didn't arrive at the intended destination
212 if(autocvar_bot_debug_tracewalk)
213 debugnodestatus(org, DEBUG_NODE_FAIL);
218 /////////////////////////////////////////////////////////////////////////////
220 /////////////////////////////////////////////////////////////////////////////
222 // completely empty the goal stack, used when deciding where to go
223 void navigation_clearroute()
225 //print("bot ", etos(self), " clear\n");
226 self.navigation_hasgoals = false;
227 self.goalcurrent = world;
228 self.goalstack01 = world;
229 self.goalstack02 = world;
230 self.goalstack03 = world;
231 self.goalstack04 = world;
232 self.goalstack05 = world;
233 self.goalstack06 = world;
234 self.goalstack07 = world;
235 self.goalstack08 = world;
236 self.goalstack09 = world;
237 self.goalstack10 = world;
238 self.goalstack11 = world;
239 self.goalstack12 = world;
240 self.goalstack13 = world;
241 self.goalstack14 = world;
242 self.goalstack15 = world;
243 self.goalstack16 = world;
244 self.goalstack17 = world;
245 self.goalstack18 = world;
246 self.goalstack19 = world;
247 self.goalstack20 = world;
248 self.goalstack21 = world;
249 self.goalstack22 = world;
250 self.goalstack23 = world;
251 self.goalstack24 = world;
252 self.goalstack25 = world;
253 self.goalstack26 = world;
254 self.goalstack27 = world;
255 self.goalstack28 = world;
256 self.goalstack29 = world;
257 self.goalstack30 = world;
258 self.goalstack31 = world;
261 // add a new goal at the beginning of the stack
262 // (in other words: add a new prerequisite before going to the later goals)
263 // NOTE: when a waypoint is added, the WP gets pushed first, then the
264 // next-closest WP on the shortest path to the WP
265 // That means, if the stack overflows, the bot will know how to do the FIRST 32
266 // steps to the goal, and then recalculate the path.
267 void navigation_pushroute(entity e)
269 //print("bot ", etos(self), " push ", etos(e), "\n");
270 self.goalstack31 = self.goalstack30;
271 self.goalstack30 = self.goalstack29;
272 self.goalstack29 = self.goalstack28;
273 self.goalstack28 = self.goalstack27;
274 self.goalstack27 = self.goalstack26;
275 self.goalstack26 = self.goalstack25;
276 self.goalstack25 = self.goalstack24;
277 self.goalstack24 = self.goalstack23;
278 self.goalstack23 = self.goalstack22;
279 self.goalstack22 = self.goalstack21;
280 self.goalstack21 = self.goalstack20;
281 self.goalstack20 = self.goalstack19;
282 self.goalstack19 = self.goalstack18;
283 self.goalstack18 = self.goalstack17;
284 self.goalstack17 = self.goalstack16;
285 self.goalstack16 = self.goalstack15;
286 self.goalstack15 = self.goalstack14;
287 self.goalstack14 = self.goalstack13;
288 self.goalstack13 = self.goalstack12;
289 self.goalstack12 = self.goalstack11;
290 self.goalstack11 = self.goalstack10;
291 self.goalstack10 = self.goalstack09;
292 self.goalstack09 = self.goalstack08;
293 self.goalstack08 = self.goalstack07;
294 self.goalstack07 = self.goalstack06;
295 self.goalstack06 = self.goalstack05;
296 self.goalstack05 = self.goalstack04;
297 self.goalstack04 = self.goalstack03;
298 self.goalstack03 = self.goalstack02;
299 self.goalstack02 = self.goalstack01;
300 self.goalstack01 = self.goalcurrent;
301 self.goalcurrent = e;
304 // remove first goal from stack
305 // (in other words: remove a prerequisite for reaching the later goals)
306 // (used when a spawnfunc_waypoint is reached)
307 void navigation_poproute()
309 //print("bot ", etos(self), " pop\n");
310 self.goalcurrent = self.goalstack01;
311 self.goalstack01 = self.goalstack02;
312 self.goalstack02 = self.goalstack03;
313 self.goalstack03 = self.goalstack04;
314 self.goalstack04 = self.goalstack05;
315 self.goalstack05 = self.goalstack06;
316 self.goalstack06 = self.goalstack07;
317 self.goalstack07 = self.goalstack08;
318 self.goalstack08 = self.goalstack09;
319 self.goalstack09 = self.goalstack10;
320 self.goalstack10 = self.goalstack11;
321 self.goalstack11 = self.goalstack12;
322 self.goalstack12 = self.goalstack13;
323 self.goalstack13 = self.goalstack14;
324 self.goalstack14 = self.goalstack15;
325 self.goalstack15 = self.goalstack16;
326 self.goalstack16 = self.goalstack17;
327 self.goalstack17 = self.goalstack18;
328 self.goalstack18 = self.goalstack19;
329 self.goalstack19 = self.goalstack20;
330 self.goalstack20 = self.goalstack21;
331 self.goalstack21 = self.goalstack22;
332 self.goalstack22 = self.goalstack23;
333 self.goalstack23 = self.goalstack24;
334 self.goalstack24 = self.goalstack25;
335 self.goalstack25 = self.goalstack26;
336 self.goalstack26 = self.goalstack27;
337 self.goalstack27 = self.goalstack28;
338 self.goalstack28 = self.goalstack29;
339 self.goalstack29 = self.goalstack30;
340 self.goalstack30 = self.goalstack31;
341 self.goalstack31 = world;
344 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
347 dist = vlen(v - org);
350 traceline(v, org, true, ent);
351 if (trace_fraction == 1)
355 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
360 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
368 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
369 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
371 entity waylist, w, best;
372 vector v, org, pm1, pm2;
374 pm1 = ent.origin + ent.mins;
375 pm2 = ent.origin + ent.maxs;
376 waylist = findchain(classname, "waypoint");
378 // do two scans, because box test is cheaper
382 // if object is touching spawnfunc_waypoint
383 if(w != ent && w != except)
384 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
389 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
390 org.z = ent.origin.z + ent.mins.z - PL_MIN_z; // player height
391 // TODO possibly make other code have the same support for bboxes
393 org = org + ent.tag_entity.origin;
394 if (navigation_testtracewalk)
399 // box check failed, try walk
403 // if object can walk from spawnfunc_waypoint
409 wm1 = w.origin + w.mins;
410 wm2 = w.origin + w.maxs;
411 v.x = bound(wm1_x, org.x, wm2_x);
412 v.y = bound(wm1_y, org.y, wm2_y);
413 v.z = bound(wm1_z, org.z, wm2_z);
417 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
419 bestdist = vlen(v - org);
427 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
429 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, world);
430 if (autocvar_g_waypointeditor_auto)
432 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
434 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
439 // finds the waypoints near the bot initiating a navigation query
440 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
443 vector v, m1, m2, diff;
445 // navigation_testtracewalk = true;
450 if (!head.wpconsidered)
454 m1 = head.origin + head.mins;
455 m2 = head.origin + head.maxs;
457 v.x = bound(m1_x, v.x, m2_x);
458 v.y = bound(m1_y, v.y, m2_y);
459 v.z = bound(m1_z, v.z, m2_z);
463 diff = v - self.origin;
464 diff.z = max(0, diff.z);
465 if (vlen(diff) < maxdist)
467 head.wpconsidered = true;
468 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
470 head.wpnearestpoint = v;
471 head.wpcost = vlen(v - self.origin) + head.dmg;
480 //navigation_testtracewalk = false;
484 // updates a path link if a spawnfunc_waypoint link is better than the current one
485 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
494 v.x = bound(m1_x, p.x, m2_x);
495 v.y = bound(m1_y, p.y, m2_y);
496 v.z = bound(m1_z, p.z, m2_z);
500 cost2 = cost2 + vlen(v - p);
501 if (wp.wpcost > cost2)
506 wp.wpnearestpoint = v;
510 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
511 void navigation_markroutes(entity fixed_source_waypoint)
513 entity w, wp, waylist;
514 float searching, cost, cost2;
516 w = waylist = findchain(classname, "waypoint");
519 w.wpconsidered = false;
520 w.wpnearestpoint = '0 0 0';
527 if(fixed_source_waypoint)
529 fixed_source_waypoint.wpconsidered = true;
530 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
531 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
532 fixed_source_waypoint.wpfire = 1;
533 fixed_source_waypoint.enemy = world;
537 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
538 // as this search is expensive we will use lower values if the bot is on the air
539 float i, increment, maxdistance;
540 if(self.flags & FL_ONGROUND)
551 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
566 p = w.wpnearestpoint;
567 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
606 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
607 void navigation_markroutes_inverted(entity fixed_source_waypoint)
609 entity w, wp, waylist;
610 float searching, cost, cost2;
612 w = waylist = findchain(classname, "waypoint");
615 w.wpconsidered = false;
616 w.wpnearestpoint = '0 0 0';
623 if(fixed_source_waypoint)
625 fixed_source_waypoint.wpconsidered = true;
626 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
627 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
628 fixed_source_waypoint.wpfire = 1;
629 fixed_source_waypoint.enemy = world;
633 error("need to start with a waypoint\n");
647 cost = w.wpcost; // cost to walk from w to home
648 p = w.wpnearestpoint;
649 for(wp = waylist; wp; wp = wp.chain)
651 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
652 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
653 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
654 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
655 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
656 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
657 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
658 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
660 cost2 = cost + wp.dmg;
661 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
669 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
670 void navigation_routerating(entity e, float f, float rangebias)
680 o = (e.absmin + e.absmax) * 0.5;
682 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
684 // Evaluate path using jetpack
686 if(self.items & IT_JETPACK)
687 if(autocvar_bot_ai_navigation_jetpack)
688 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
690 vector pointa, pointb;
692 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
695 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
696 pointa = trace_endpos - '0 0 1';
699 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
700 pointb = trace_endpos - '0 0 1';
702 // Can I see these two points from the sky?
703 traceline(pointa, pointb, MOVE_NORMAL, self);
705 if(trace_fraction==1)
707 bot_debug("jetpack ai: can bridge these two points\n");
709 // Lower the altitude of these points as much as possible
710 float zdistance, xydistance, cost, t, fuel;
711 vector down, npa, npb;
713 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
719 if(npa.z<=self.absmax.z)
722 if(npb.z<=e.absmax.z)
725 traceline(npa, npb, MOVE_NORMAL, self);
726 if(trace_fraction==1)
732 while(trace_fraction == 1);
735 // Rough estimation of fuel consumption
736 // (ignores acceleration and current xyz velocity)
737 xydistance = vlen(pointa - pointb);
738 zdistance = fabs(pointa.z - self.origin.z);
740 t = zdistance / autocvar_g_jetpack_maxspeed_up;
741 t += xydistance / autocvar_g_jetpack_maxspeed_side;
742 fuel = t * autocvar_g_jetpack_fuel * 0.8;
744 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
747 if(self.ammo_fuel>fuel)
750 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
751 // - between air and ground speeds)
753 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
754 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
757 // Compare against other goals
758 f = f * rangebias / (rangebias + cost);
760 if (navigation_bestrating < f)
762 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
763 navigation_bestrating = f;
764 navigation_bestgoal = e;
765 self.navigation_jetpack_goal = e;
766 self.navigation_jetpack_point = pointb;
773 //te_wizspike(e.origin);
776 // update the cached spawnfunc_waypoint link on a dynamic item entity
777 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
787 if(e.flags & FL_ITEM)
789 if (!(e.flags & FL_WEAPON))
790 if(e.nearestwaypoint)
793 else if (e.flags & FL_WEAPON)
795 if(e.classname != "droppedweapon")
796 if(e.nearestwaypoint)
801 if (time > e.nearestwaypointtimeout)
803 nwp = navigation_findnearestwaypoint(e, true);
805 e.nearestwaypoint = nwp;
808 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
810 if(e.flags & FL_ITEM)
811 e.blacklisted = true;
812 else if (e.flags & FL_WEAPON)
814 if(e.classname != "droppedweapon")
815 e.blacklisted = true;
820 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
825 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
826 if(e.classname=="item_flag_team")
827 e.nearestwaypointtimeout = time + 2;
829 e.nearestwaypointtimeout = time + random() * 3 + 5;
831 nwp = e.nearestwaypoint;
834 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
836 if (nwp.wpcost < 10000000)
838 //te_wizspike(nwp.wpnearestpoint);
839 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
840 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
841 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
842 if (navigation_bestrating < f)
844 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
845 navigation_bestrating = f;
846 navigation_bestgoal = e;
851 // adds an item to the the goal stack with the path to a given item
852 float navigation_routetogoal(entity e, vector startposition)
856 // if there is no goal, just exit
860 self.navigation_hasgoals = true;
862 // put the entity on the goal stack
863 //print("routetogoal ", etos(e), "\n");
864 navigation_pushroute(e);
867 if(e==self.navigation_jetpack_goal)
870 // if it can reach the goal there is nothing more to do
871 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
874 // see if there are waypoints describing a path to the item
875 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
876 e = e.nearestwaypoint;
878 e = e.enemy; // we already have added it, so...
885 // add the spawnfunc_waypoint to the path
886 navigation_pushroute(e);
896 // removes any currently touching waypoints from the goal stack
897 // (this is how bots detect if they reached a goal)
898 void navigation_poptouchedgoals()
902 m1 = org + self.mins;
903 m2 = org + self.maxs;
905 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
907 if(self.lastteleporttime>0)
908 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
910 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
911 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
913 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
914 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
916 navigation_poproute();
921 // If for some reason the bot is closer to the next goal, pop the current one
923 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
924 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
925 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
927 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
928 navigation_poproute();
929 // TODO this may also be a nice idea to do "early" (e.g. by
930 // manipulating the vlen() comparisons) to shorten paths in
931 // general - this would make bots walk more "on rails" than
932 // "zigzagging" which they currently do with sufficiently
933 // random-like waypoints, and thus can make a nice bot
934 // personality property
937 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
938 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
939 if(IS_PLAYER(self.goalcurrent))
940 navigation_poproute();
942 // aid for detecting jump pads better (distance based check fails sometimes)
943 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
944 navigation_poproute();
946 // Loose goal touching check when running
947 if(self.aistatus & AI_STATUS_RUNNING)
948 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
949 if(self.goalcurrent.classname=="waypoint")
951 if(vlen(self.origin - self.goalcurrent.origin)<150)
953 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, true, world);
954 if(trace_fraction==1)
956 // Detect personal waypoints
957 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
958 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
960 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
961 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
964 navigation_poproute();
969 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
971 // Detect personal waypoints
972 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
973 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
975 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
976 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
979 navigation_poproute();
983 // begin a goal selection session (queries spawnfunc_waypoint network)
984 void navigation_goalrating_start()
986 if(self.aistatus & AI_STATUS_STUCK)
989 self.navigation_jetpack_goal = world;
990 navigation_bestrating = -1;
991 self.navigation_hasgoals = false;
992 navigation_clearroute();
993 navigation_bestgoal = world;
994 navigation_markroutes(world);
997 // ends a goal selection session (updates goal stack to the best goal)
998 void navigation_goalrating_end()
1000 if(self.aistatus & AI_STATUS_STUCK)
1003 navigation_routetogoal(navigation_bestgoal, self.origin);
1004 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
1006 // If the bot got stuck then try to reach the farthest waypoint
1007 if (!self.navigation_hasgoals)
1008 if (autocvar_bot_wander_enable)
1010 if (!(self.aistatus & AI_STATUS_STUCK))
1012 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1013 self.aistatus |= AI_STATUS_STUCK;
1016 self.navigation_hasgoals = false; // Reset this value
1020 void botframe_updatedangerousobjects(float maxupdate)
1022 entity head, bot_dodgelist;
1023 vector m1, m2, v, o;
1026 bot_dodgelist = findchainfloat(bot_dodge, true);
1027 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1028 while (botframe_dangerwaypoint != world)
1031 m1 = botframe_dangerwaypoint.mins;
1032 m2 = botframe_dangerwaypoint.maxs;
1033 head = bot_dodgelist;
1037 v.x = bound(m1_x, v.x, m2_x);
1038 v.y = bound(m1_y, v.y, m2_y);
1039 v.z = bound(m1_z, v.z, m2_z);
1040 o = (head.absmin + head.absmax) * 0.5;
1041 d = head.bot_dodgerating - vlen(o - v);
1044 traceline(o, v, true, world);
1045 if (trace_fraction == 1)
1046 danger = danger + d;
1050 botframe_dangerwaypoint.dmg = danger;
1054 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1058 void navigation_unstuck()
1060 float search_radius = 1000;
1062 if (!autocvar_bot_wander_enable)
1065 if (!bot_waypoint_queue_owner)
1067 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1068 bot_waypoint_queue_owner = self;
1069 bot_waypoint_queue_bestgoal = world;
1070 bot_waypoint_queue_bestgoalrating = 0;
1073 if(bot_waypoint_queue_owner!=self)
1076 if (bot_waypoint_queue_goal)
1078 // evaluate the next goal on the queue
1079 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1080 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1081 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1083 if( d > bot_waypoint_queue_bestgoalrating)
1085 bot_waypoint_queue_bestgoalrating = d;
1086 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1089 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1091 if (!bot_waypoint_queue_goal)
1093 if (bot_waypoint_queue_bestgoal)
1095 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1096 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1097 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1098 self.aistatus &= ~AI_STATUS_STUCK;
1102 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1105 bot_waypoint_queue_owner = world;
1110 if(bot_strategytoken!=self)
1113 // build a new queue
1114 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1119 head = findradius(self.origin, search_radius);
1123 if(head.classname=="waypoint")
1124 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1126 if(bot_waypoint_queue_goal)
1127 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1131 bot_waypoint_queue_goal = head;
1132 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1139 bot_waypoint_queue_goal = first;
1142 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1143 bot_waypoint_queue_owner = world;
1148 // Support for debugging tracewalk visually
1150 void debugresetnodes()
1152 debuglastnode = '0 0 0';
1155 void debugnode(vector node)
1157 if (!IS_PLAYER(self))
1160 if(debuglastnode=='0 0 0')
1162 debuglastnode = node;
1166 te_lightning2(world, node, debuglastnode);
1167 debuglastnode = node;
1170 void debugnodestatus(vector position, float status)
1176 case DEBUG_NODE_SUCCESS:
1179 case DEBUG_NODE_WARNING:
1182 case DEBUG_NODE_FAIL:
1189 te_customflash(position, 40, 2, c);
1192 // Support for debugging the goal stack visually
1195 .vector lastposition;
1197 // Debug the goal stack visually
1198 void debuggoalstack()
1203 if(self.goalcounter==0)goal=self.goalcurrent;
1204 else if(self.goalcounter==1)goal=self.goalstack01;
1205 else if(self.goalcounter==2)goal=self.goalstack02;
1206 else if(self.goalcounter==3)goal=self.goalstack03;
1207 else if(self.goalcounter==4)goal=self.goalstack04;
1208 else if(self.goalcounter==5)goal=self.goalstack05;
1209 else if(self.goalcounter==6)goal=self.goalstack06;
1210 else if(self.goalcounter==7)goal=self.goalstack07;
1211 else if(self.goalcounter==8)goal=self.goalstack08;
1212 else if(self.goalcounter==9)goal=self.goalstack09;
1213 else if(self.goalcounter==10)goal=self.goalstack10;
1214 else if(self.goalcounter==11)goal=self.goalstack11;
1215 else if(self.goalcounter==12)goal=self.goalstack12;
1216 else if(self.goalcounter==13)goal=self.goalstack13;
1217 else if(self.goalcounter==14)goal=self.goalstack14;
1218 else if(self.goalcounter==15)goal=self.goalstack15;
1219 else if(self.goalcounter==16)goal=self.goalstack16;
1220 else if(self.goalcounter==17)goal=self.goalstack17;
1221 else if(self.goalcounter==18)goal=self.goalstack18;
1222 else if(self.goalcounter==19)goal=self.goalstack19;
1223 else if(self.goalcounter==20)goal=self.goalstack20;
1224 else if(self.goalcounter==21)goal=self.goalstack21;
1225 else if(self.goalcounter==22)goal=self.goalstack22;
1226 else if(self.goalcounter==23)goal=self.goalstack23;
1227 else if(self.goalcounter==24)goal=self.goalstack24;
1228 else if(self.goalcounter==25)goal=self.goalstack25;
1229 else if(self.goalcounter==26)goal=self.goalstack26;
1230 else if(self.goalcounter==27)goal=self.goalstack27;
1231 else if(self.goalcounter==28)goal=self.goalstack28;
1232 else if(self.goalcounter==29)goal=self.goalstack29;
1233 else if(self.goalcounter==30)goal=self.goalstack30;
1234 else if(self.goalcounter==31)goal=self.goalstack31;
1239 self.goalcounter = 0;
1240 self.lastposition='0 0 0';
1244 if(self.lastposition=='0 0 0')
1247 org = self.lastposition;
1250 go = ( goal.absmin + goal.absmax ) * 0.5;
1251 te_lightning2(world, org, go);
1252 self.lastposition = go;