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(entity ent, float walkfromwp, float bestdist)
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
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 return navigation_findnearestwaypoint_withdist(ent, walkfromwp, 1050);
432 // finds the waypoints near the bot initiating a navigation query
433 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
436 vector v, m1, m2, diff;
438 // navigation_testtracewalk = TRUE;
443 if (!head.wpconsidered)
447 m1 = head.origin + head.mins;
448 m2 = head.origin + head.maxs;
450 v_x = bound(m1_x, v_x, m2_x);
451 v_y = bound(m1_y, v_y, m2_y);
452 v_z = bound(m1_z, v_z, m2_z);
456 diff = v - self.origin;
457 diff_z = max(0, diff_z);
458 if (vlen(diff) < maxdist)
460 head.wpconsidered = TRUE;
461 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
463 head.wpnearestpoint = v;
464 head.wpcost = vlen(v - self.origin) + head.dmg;
473 //navigation_testtracewalk = FALSE;
477 // updates a path link if a spawnfunc_waypoint link is better than the current one
478 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
487 v_x = bound(m1_x, p_x, m2_x);
488 v_y = bound(m1_y, p_y, m2_y);
489 v_z = bound(m1_z, p_z, m2_z);
493 cost2 = cost2 + vlen(v - p);
494 if (wp.wpcost > cost2)
499 wp.wpnearestpoint = v;
503 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
504 void navigation_markroutes(entity fixed_source_waypoint)
506 entity w, wp, waylist;
507 float searching, cost, cost2;
509 w = waylist = findchain(classname, "waypoint");
512 w.wpconsidered = FALSE;
513 w.wpnearestpoint = '0 0 0';
520 if(fixed_source_waypoint)
522 fixed_source_waypoint.wpconsidered = TRUE;
523 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
524 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
525 fixed_source_waypoint.wpfire = 1;
526 fixed_source_waypoint.enemy = world;
530 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
531 // as this search is expensive we will use lower values if the bot is on the air
532 float i, increment, maxdistance;
533 if(self.flags & FL_ONGROUND)
544 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
559 p = w.wpnearestpoint;
560 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
561 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
562 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
563 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
564 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
565 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
566 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
567 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
599 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
600 void navigation_markroutes_inverted(entity fixed_source_waypoint)
602 entity w, wp, waylist;
603 float searching, cost, cost2;
605 w = waylist = findchain(classname, "waypoint");
608 w.wpconsidered = FALSE;
609 w.wpnearestpoint = '0 0 0';
616 if(fixed_source_waypoint)
618 fixed_source_waypoint.wpconsidered = TRUE;
619 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
620 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
621 fixed_source_waypoint.wpfire = 1;
622 fixed_source_waypoint.enemy = world;
626 error("need to start with a waypoint\n");
640 cost = w.wpcost; // cost to walk from w to home
641 p = w.wpnearestpoint;
642 for(wp = waylist; wp; wp = wp.chain)
644 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
645 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
646 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
647 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
648 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
649 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
650 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
651 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
653 cost2 = cost + wp.dmg;
654 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
662 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
663 void navigation_routerating(entity e, float f, float rangebias)
673 o = (e.absmin + e.absmax) * 0.5;
675 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
677 // Evaluate path using jetpack
679 if(self.items & IT_JETPACK)
680 if(autocvar_bot_ai_navigation_jetpack)
681 if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
683 vector pointa, pointb;
685 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
688 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
689 pointa = trace_endpos - '0 0 1';
692 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
693 pointb = trace_endpos - '0 0 1';
695 // Can I see these two points from the sky?
696 traceline(pointa, pointb, MOVE_NORMAL, self);
698 if(trace_fraction==1)
700 bot_debug("jetpack ai: can bridge these two points\n");
702 // Lower the altitude of these points as much as possible
703 float zdistance, xydistance, cost, t, fuel;
704 vector down, npa, npb;
706 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
712 if(npa_z<=self.absmax_z)
715 if(npb_z<=e.absmax_z)
718 traceline(npa, npb, MOVE_NORMAL, self);
719 if(trace_fraction==1)
725 while(trace_fraction == 1);
728 // Rough estimation of fuel consumption
729 // (ignores acceleration and current xyz velocity)
730 xydistance = vlen(pointa - pointb);
731 zdistance = fabs(pointa_z - self.origin_z);
733 t = zdistance / autocvar_g_jetpack_maxspeed_up;
734 t += xydistance / autocvar_g_jetpack_maxspeed_side;
735 fuel = t * autocvar_g_jetpack_fuel * 0.8;
737 bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
740 if(self.ammo_fuel>fuel)
743 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
744 // - between air and ground speeds)
746 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
747 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
750 // Compare against other goals
751 f = f * rangebias / (rangebias + cost);
753 if (navigation_bestrating < f)
755 bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
756 navigation_bestrating = f;
757 navigation_bestgoal = e;
758 self.navigation_jetpack_goal = e;
759 self.navigation_jetpack_point = pointb;
766 //te_wizspike(e.origin);
769 // update the cached spawnfunc_waypoint link on a dynamic item entity
770 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
780 if(e.flags & FL_ITEM)
782 if (!(e.flags & FL_WEAPON))
783 if(e.nearestwaypoint)
786 else if (e.flags & FL_WEAPON)
788 if(e.classname != "droppedweapon")
789 if(e.nearestwaypoint)
794 if (time > e.nearestwaypointtimeout)
796 nwp = navigation_findnearestwaypoint(e, TRUE);
798 e.nearestwaypoint = nwp;
801 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
803 if(e.flags & FL_ITEM)
804 e.blacklisted = TRUE;
805 else if (e.flags & FL_WEAPON)
807 if(e.classname != "droppedweapon")
808 e.blacklisted = TRUE;
813 bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
818 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
819 if(e.classname=="item_flag_team")
820 e.nearestwaypointtimeout = time + 2;
822 e.nearestwaypointtimeout = time + random() * 3 + 5;
824 nwp = e.nearestwaypoint;
827 bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
829 if (nwp.wpcost < 10000000)
831 //te_wizspike(nwp.wpnearestpoint);
832 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
833 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
834 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
835 if (navigation_bestrating < f)
837 bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
838 navigation_bestrating = f;
839 navigation_bestgoal = e;
844 // adds an item to the the goal stack with the path to a given item
845 float navigation_routetogoal(entity e, vector startposition)
849 // if there is no goal, just exit
853 self.navigation_hasgoals = TRUE;
855 // put the entity on the goal stack
856 //print("routetogoal ", etos(e), "\n");
857 navigation_pushroute(e);
860 if(e==self.navigation_jetpack_goal)
863 // if it can reach the goal there is nothing more to do
864 if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
867 // see if there are waypoints describing a path to the item
868 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
869 e = e.nearestwaypoint;
871 e = e.enemy; // we already have added it, so...
878 // add the spawnfunc_waypoint to the path
879 navigation_pushroute(e);
889 // removes any currently touching waypoints from the goal stack
890 // (this is how bots detect if they reached a goal)
891 void navigation_poptouchedgoals()
895 m1 = org + self.mins;
896 m2 = org + self.maxs;
898 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
900 if(self.lastteleporttime>0)
901 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
903 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
904 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
906 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
907 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
909 navigation_poproute();
914 // If for some reason the bot is closer to the next goal, pop the current one
916 if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
917 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
918 if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
920 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
921 navigation_poproute();
922 // TODO this may also be a nice idea to do "early" (e.g. by
923 // manipulating the vlen() comparisons) to shorten paths in
924 // general - this would make bots walk more "on rails" than
925 // "zigzagging" which they currently do with sufficiently
926 // random-like waypoints, and thus can make a nice bot
927 // personality property
930 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
931 // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
932 if(IS_PLAYER(self.goalcurrent))
933 navigation_poproute();
935 // aid for detecting jump pads better (distance based check fails sometimes)
936 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
937 navigation_poproute();
939 // Loose goal touching check when running
940 if(self.aistatus & AI_STATUS_RUNNING)
941 if(self.speed >= autocvar_sv_maxspeed) // if -really- running
942 if(self.goalcurrent.classname=="waypoint")
944 if(vlen(self.origin - self.goalcurrent.origin)<150)
946 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
947 if(trace_fraction==1)
949 // Detect personal waypoints
950 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
951 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
953 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
954 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
957 navigation_poproute();
962 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
964 // Detect personal waypoints
965 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
966 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
968 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
969 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
972 navigation_poproute();
976 // begin a goal selection session (queries spawnfunc_waypoint network)
977 void navigation_goalrating_start()
979 if(self.aistatus & AI_STATUS_STUCK)
982 self.navigation_jetpack_goal = world;
983 navigation_bestrating = -1;
984 self.navigation_hasgoals = FALSE;
985 navigation_clearroute();
986 navigation_bestgoal = world;
987 navigation_markroutes(world);
990 // ends a goal selection session (updates goal stack to the best goal)
991 void navigation_goalrating_end()
993 if(self.aistatus & AI_STATUS_STUCK)
996 navigation_routetogoal(navigation_bestgoal, self.origin);
997 bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
999 // If the bot got stuck then try to reach the farthest waypoint
1000 if (!self.navigation_hasgoals)
1001 if (autocvar_bot_wander_enable)
1003 if (!(self.aistatus & AI_STATUS_STUCK))
1005 bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1006 self.aistatus |= AI_STATUS_STUCK;
1009 self.navigation_hasgoals = FALSE; // Reset this value
1013 void botframe_updatedangerousobjects(float maxupdate)
1015 entity head, bot_dodgelist;
1016 vector m1, m2, v, o;
1019 bot_dodgelist = findchainfloat(bot_dodge, TRUE);
1020 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1021 while (botframe_dangerwaypoint != world)
1024 m1 = botframe_dangerwaypoint.mins;
1025 m2 = botframe_dangerwaypoint.maxs;
1026 head = bot_dodgelist;
1030 v_x = bound(m1_x, v_x, m2_x);
1031 v_y = bound(m1_y, v_y, m2_y);
1032 v_z = bound(m1_z, v_z, m2_z);
1033 o = (head.absmin + head.absmax) * 0.5;
1034 d = head.bot_dodgerating - vlen(o - v);
1037 traceline(o, v, TRUE, world);
1038 if (trace_fraction == 1)
1039 danger = danger + d;
1043 botframe_dangerwaypoint.dmg = danger;
1047 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1051 void navigation_unstuck()
1053 float search_radius = 1000;
1055 if (!autocvar_bot_wander_enable)
1058 if (!bot_waypoint_queue_owner)
1060 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1061 bot_waypoint_queue_owner = self;
1062 bot_waypoint_queue_bestgoal = world;
1063 bot_waypoint_queue_bestgoalrating = 0;
1066 if(bot_waypoint_queue_owner!=self)
1069 if (bot_waypoint_queue_goal)
1071 // evaluate the next goal on the queue
1072 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1073 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1074 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1076 if( d > bot_waypoint_queue_bestgoalrating)
1078 bot_waypoint_queue_bestgoalrating = d;
1079 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1082 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1084 if (!bot_waypoint_queue_goal)
1086 if (bot_waypoint_queue_bestgoal)
1088 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1089 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1090 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1091 self.aistatus &= ~AI_STATUS_STUCK;
1095 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1098 bot_waypoint_queue_owner = world;
1103 if(bot_strategytoken!=self)
1106 // build a new queue
1107 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1112 head = findradius(self.origin, search_radius);
1116 if(head.classname=="waypoint")
1117 // if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1119 if(bot_waypoint_queue_goal)
1120 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1124 bot_waypoint_queue_goal = head;
1125 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1132 bot_waypoint_queue_goal = first;
1135 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1136 bot_waypoint_queue_owner = world;
1141 // Support for debugging tracewalk visually
1143 void debugresetnodes()
1145 debuglastnode = '0 0 0';
1148 void debugnode(vector node)
1150 if (!IS_PLAYER(self))
1153 if(debuglastnode=='0 0 0')
1155 debuglastnode = node;
1159 te_lightning2(world, node, debuglastnode);
1160 debuglastnode = node;
1163 void debugnodestatus(vector position, float status)
1169 case DEBUG_NODE_SUCCESS:
1172 case DEBUG_NODE_WARNING:
1175 case DEBUG_NODE_FAIL:
1182 te_customflash(position, 40, 2, c);
1185 // Support for debugging the goal stack visually
1188 .vector lastposition;
1190 // Debug the goal stack visually
1191 void debuggoalstack()
1196 if(self.goalcounter==0)goal=self.goalcurrent;
1197 else if(self.goalcounter==1)goal=self.goalstack01;
1198 else if(self.goalcounter==2)goal=self.goalstack02;
1199 else if(self.goalcounter==3)goal=self.goalstack03;
1200 else if(self.goalcounter==4)goal=self.goalstack04;
1201 else if(self.goalcounter==5)goal=self.goalstack05;
1202 else if(self.goalcounter==6)goal=self.goalstack06;
1203 else if(self.goalcounter==7)goal=self.goalstack07;
1204 else if(self.goalcounter==8)goal=self.goalstack08;
1205 else if(self.goalcounter==9)goal=self.goalstack09;
1206 else if(self.goalcounter==10)goal=self.goalstack10;
1207 else if(self.goalcounter==11)goal=self.goalstack11;
1208 else if(self.goalcounter==12)goal=self.goalstack12;
1209 else if(self.goalcounter==13)goal=self.goalstack13;
1210 else if(self.goalcounter==14)goal=self.goalstack14;
1211 else if(self.goalcounter==15)goal=self.goalstack15;
1212 else if(self.goalcounter==16)goal=self.goalstack16;
1213 else if(self.goalcounter==17)goal=self.goalstack17;
1214 else if(self.goalcounter==18)goal=self.goalstack18;
1215 else if(self.goalcounter==19)goal=self.goalstack19;
1216 else if(self.goalcounter==20)goal=self.goalstack20;
1217 else if(self.goalcounter==21)goal=self.goalstack21;
1218 else if(self.goalcounter==22)goal=self.goalstack22;
1219 else if(self.goalcounter==23)goal=self.goalstack23;
1220 else if(self.goalcounter==24)goal=self.goalstack24;
1221 else if(self.goalcounter==25)goal=self.goalstack25;
1222 else if(self.goalcounter==26)goal=self.goalstack26;
1223 else if(self.goalcounter==27)goal=self.goalstack27;
1224 else if(self.goalcounter==28)goal=self.goalstack28;
1225 else if(self.goalcounter==29)goal=self.goalstack29;
1226 else if(self.goalcounter==30)goal=self.goalstack30;
1227 else if(self.goalcounter==31)goal=self.goalstack31;
1232 self.goalcounter = 0;
1233 self.lastposition='0 0 0';
1237 if(self.lastposition=='0 0 0')
1240 org = self.lastposition;
1243 go = ( goal.absmin + goal.absmax ) * 0.5;
1244 te_lightning2(world, org, go);
1245 self.lastposition = go;