1 #include "navigation.qh"
4 #include "waypoints.qh"
6 #include <common/t_items.qh>
8 #include <common/items/all.qh>
10 #include <common/constants.qh>
11 #include <common/triggers/trigger/jumppads.qh>
15 // rough simulation of walking from one point to another to test if a path
16 // can be traveled, used for waypoint linking and havocbot
18 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
30 if(autocvar_bot_debug_tracewalk)
39 dist = totaldist = vlen(move);
40 dir = normalize(move);
42 ignorehazards = false;
45 // Analyze starting point
46 traceline(start, start, MOVE_NORMAL, e);
47 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
51 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
52 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
58 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
62 if(autocvar_bot_debug_tracewalk)
63 debugnodestatus(start, DEBUG_NODE_FAIL);
65 //print("tracewalk: ", vtos(start), " is a bad start\n");
74 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
77 if(autocvar_bot_debug_tracewalk)
78 debugnodestatus(org, DEBUG_NODE_SUCCESS);
80 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
83 if(autocvar_bot_debug_tracewalk)
90 dist = dist - stepdist;
91 traceline(org, org, MOVE_NORMAL, e);
94 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
96 // hazards blocking path
97 if(autocvar_bot_debug_tracewalk)
98 debugnodestatus(org, DEBUG_NODE_FAIL);
100 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
104 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
106 move = normalize(end - org);
107 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
109 if(autocvar_bot_debug_tracewalk)
110 debugnode(e, trace_endpos);
112 if (trace_fraction < 1)
115 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
116 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
118 if(autocvar_bot_debug_tracewalk)
121 if(pointcontents(org) == CONTENT_EMPTY)
125 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
127 if(autocvar_bot_debug_tracewalk)
128 debugnodestatus(org, DEBUG_NODE_FAIL);
131 //print("tracewalk: ", vtos(start), " failed under water\n");
141 move = dir * stepdist + org;
142 tracebox(org, m1, m2, move, movemode, e);
144 if(autocvar_bot_debug_tracewalk)
145 debugnode(e, trace_endpos);
148 if (trace_fraction < 1)
150 // check if we can walk over this obstacle, possibly by jumpstepping
151 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
152 if (trace_fraction < 1 || trace_startsolid)
154 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
155 if (trace_fraction < 1 || trace_startsolid)
157 if(autocvar_bot_debug_tracewalk)
158 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
161 traceline( org, move, movemode, e);
162 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
166 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
168 nextmove = move + (dir * stepdist);
169 traceline( move, nextmove, movemode, e);
175 if(autocvar_bot_debug_tracewalk)
176 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
178 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
179 //te_explosion(trace_endpos);
180 //print(ftos(e.dphitcontentsmask), "\n");
181 return false; // failed
193 // trace down from stepheight as far as possible and move there,
194 // if this starts in solid we try again without the stepup, and
195 // if that also fails we assume it is a wall
196 // (this is the same logic as the Quake walkmove function used)
197 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
199 // moved successfully
203 c = pointcontents(org + '0 0 1');
204 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
214 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
216 // moved but didn't arrive at the intended destination
217 if(autocvar_bot_debug_tracewalk)
218 debugnodestatus(org, DEBUG_NODE_FAIL);
223 /////////////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////////////
227 // completely empty the goal stack, used when deciding where to go
228 void navigation_clearroute(entity this)
230 //print("bot ", etos(this), " clear\n");
231 this.navigation_hasgoals = false;
232 this.goalcurrent = NULL;
233 this.goalstack01 = NULL;
234 this.goalstack02 = NULL;
235 this.goalstack03 = NULL;
236 this.goalstack04 = NULL;
237 this.goalstack05 = NULL;
238 this.goalstack06 = NULL;
239 this.goalstack07 = NULL;
240 this.goalstack08 = NULL;
241 this.goalstack09 = NULL;
242 this.goalstack10 = NULL;
243 this.goalstack11 = NULL;
244 this.goalstack12 = NULL;
245 this.goalstack13 = NULL;
246 this.goalstack14 = NULL;
247 this.goalstack15 = NULL;
248 this.goalstack16 = NULL;
249 this.goalstack17 = NULL;
250 this.goalstack18 = NULL;
251 this.goalstack19 = NULL;
252 this.goalstack20 = NULL;
253 this.goalstack21 = NULL;
254 this.goalstack22 = NULL;
255 this.goalstack23 = NULL;
256 this.goalstack24 = NULL;
257 this.goalstack25 = NULL;
258 this.goalstack26 = NULL;
259 this.goalstack27 = NULL;
260 this.goalstack28 = NULL;
261 this.goalstack29 = NULL;
262 this.goalstack30 = NULL;
263 this.goalstack31 = NULL;
266 // add a new goal at the beginning of the stack
267 // (in other words: add a new prerequisite before going to the later goals)
268 // NOTE: when a waypoint is added, the WP gets pushed first, then the
269 // next-closest WP on the shortest path to the WP
270 // That means, if the stack overflows, the bot will know how to do the FIRST 32
271 // steps to the goal, and then recalculate the path.
272 void navigation_pushroute(entity this, entity e)
274 //print("bot ", etos(this), " push ", etos(e), "\n");
275 this.goalstack31 = this.goalstack30;
276 this.goalstack30 = this.goalstack29;
277 this.goalstack29 = this.goalstack28;
278 this.goalstack28 = this.goalstack27;
279 this.goalstack27 = this.goalstack26;
280 this.goalstack26 = this.goalstack25;
281 this.goalstack25 = this.goalstack24;
282 this.goalstack24 = this.goalstack23;
283 this.goalstack23 = this.goalstack22;
284 this.goalstack22 = this.goalstack21;
285 this.goalstack21 = this.goalstack20;
286 this.goalstack20 = this.goalstack19;
287 this.goalstack19 = this.goalstack18;
288 this.goalstack18 = this.goalstack17;
289 this.goalstack17 = this.goalstack16;
290 this.goalstack16 = this.goalstack15;
291 this.goalstack15 = this.goalstack14;
292 this.goalstack14 = this.goalstack13;
293 this.goalstack13 = this.goalstack12;
294 this.goalstack12 = this.goalstack11;
295 this.goalstack11 = this.goalstack10;
296 this.goalstack10 = this.goalstack09;
297 this.goalstack09 = this.goalstack08;
298 this.goalstack08 = this.goalstack07;
299 this.goalstack07 = this.goalstack06;
300 this.goalstack06 = this.goalstack05;
301 this.goalstack05 = this.goalstack04;
302 this.goalstack04 = this.goalstack03;
303 this.goalstack03 = this.goalstack02;
304 this.goalstack02 = this.goalstack01;
305 this.goalstack01 = this.goalcurrent;
306 this.goalcurrent = e;
309 // remove first goal from stack
310 // (in other words: remove a prerequisite for reaching the later goals)
311 // (used when a spawnfunc_waypoint is reached)
312 void navigation_poproute(entity this)
314 //print("bot ", etos(this), " pop\n");
315 this.goalcurrent = this.goalstack01;
316 this.goalstack01 = this.goalstack02;
317 this.goalstack02 = this.goalstack03;
318 this.goalstack03 = this.goalstack04;
319 this.goalstack04 = this.goalstack05;
320 this.goalstack05 = this.goalstack06;
321 this.goalstack06 = this.goalstack07;
322 this.goalstack07 = this.goalstack08;
323 this.goalstack08 = this.goalstack09;
324 this.goalstack09 = this.goalstack10;
325 this.goalstack10 = this.goalstack11;
326 this.goalstack11 = this.goalstack12;
327 this.goalstack12 = this.goalstack13;
328 this.goalstack13 = this.goalstack14;
329 this.goalstack14 = this.goalstack15;
330 this.goalstack15 = this.goalstack16;
331 this.goalstack16 = this.goalstack17;
332 this.goalstack17 = this.goalstack18;
333 this.goalstack18 = this.goalstack19;
334 this.goalstack19 = this.goalstack20;
335 this.goalstack20 = this.goalstack21;
336 this.goalstack21 = this.goalstack22;
337 this.goalstack22 = this.goalstack23;
338 this.goalstack23 = this.goalstack24;
339 this.goalstack24 = this.goalstack25;
340 this.goalstack25 = this.goalstack26;
341 this.goalstack26 = this.goalstack27;
342 this.goalstack27 = this.goalstack28;
343 this.goalstack28 = this.goalstack29;
344 this.goalstack29 = this.goalstack30;
345 this.goalstack30 = this.goalstack31;
346 this.goalstack31 = NULL;
349 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
352 dist = vlen(v - org);
355 traceline(v, org, true, ent);
356 if (trace_fraction == 1)
360 if (tracewalk(ent, v, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), org, bot_navigation_movemode))
365 if (tracewalk(ent, org, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), v, bot_navigation_movemode))
373 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
374 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
376 vector pm1 = ent.origin + ent.mins;
377 vector pm2 = ent.origin + ent.maxs;
379 // do two scans, because box test is cheaper
380 IL_EACH(g_waypoints, it != ent && it != except,
382 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
386 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
387 org.z = ent.origin.z + ent.mins.z - STAT(PL_MIN, NULL).z; // player height
388 // TODO possibly make other code have the same support for bboxes
390 org = org + ent.tag_entity.origin;
391 if (navigation_testtracewalk)
397 // box check failed, try walk
398 IL_EACH(g_waypoints, it != ent,
402 vector wm1 = it.origin + it.mins;
403 vector wm2 = it.origin + it.maxs;
404 v.x = bound(wm1_x, org.x, wm2_x);
405 v.y = bound(wm1_y, org.y, wm2_y);
406 v.z = bound(wm1_z, org.z, wm2_z);
410 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
412 bestdist = vlen(v - org);
418 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
420 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
421 if (autocvar_g_waypointeditor_auto)
423 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
425 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
430 // finds the waypoints near the bot initiating a navigation query
431 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
434 // navigation_testtracewalk = true;
436 IL_EACH(g_waypoints, !it.wpconsidered,
440 m1 = it.origin + it.mins;
441 m2 = it.origin + it.maxs;
443 v.x = bound(m1_x, v.x, m2_x);
444 v.y = bound(m1_y, v.y, m2_y);
445 v.z = bound(m1_z, v.z, m2_z);
449 vector diff = v - this.origin;
450 diff.z = max(0, diff.z);
451 if(vdist(diff, <, maxdist))
453 it.wpconsidered = true;
454 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
456 it.wpnearestpoint = v;
457 it.wpcost = vlen(v - this.origin) + it.dmg;
464 //navigation_testtracewalk = false;
468 // updates a path link if a spawnfunc_waypoint link is better than the current one
469 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
478 v.x = bound(m1_x, p.x, m2_x);
479 v.y = bound(m1_y, p.y, m2_y);
480 v.z = bound(m1_z, p.z, m2_z);
484 cost2 = cost2 + vlen(v - p);
485 if (wp.wpcost > cost2)
490 wp.wpnearestpoint = v;
494 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
495 void navigation_markroutes(entity this, entity fixed_source_waypoint)
500 IL_EACH(g_waypoints, true,
502 it.wpconsidered = false;
503 it.wpnearestpoint = '0 0 0';
504 it.wpcost = 10000000;
509 if(fixed_source_waypoint)
511 fixed_source_waypoint.wpconsidered = true;
512 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
513 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
514 fixed_source_waypoint.wpfire = 1;
515 fixed_source_waypoint.enemy = NULL;
519 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
520 // as this search is expensive we will use lower values if the bot is on the air
521 float increment, maxdistance;
522 if(IS_ONGROUND(this))
533 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
536 bool searching = true;
540 IL_EACH(g_waypoints, it.wpfire,
545 p = it.wpnearestpoint;
547 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
548 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
549 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
550 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
551 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
552 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
553 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
554 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
555 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
556 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
557 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
558 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
559 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
560 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
561 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
562 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
563 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
564 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
565 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
566 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
567 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
568 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
569 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
570 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
571 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
572 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
573 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
574 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
575 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
576 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
577 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
578 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
579 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
584 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
585 void navigation_markroutes_inverted(entity fixed_source_waypoint)
589 IL_EACH(g_waypoints, true,
591 it.wpconsidered = false;
592 it.wpnearestpoint = '0 0 0';
593 it.wpcost = 10000000;
598 if(fixed_source_waypoint)
600 fixed_source_waypoint.wpconsidered = true;
601 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
602 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
603 fixed_source_waypoint.wpfire = 1;
604 fixed_source_waypoint.enemy = NULL;
608 error("need to start with a waypoint\n");
611 bool searching = true;
615 IL_EACH(g_waypoints, it.wpfire,
619 cost = it.wpcost; // cost to walk from it to home
620 p = it.wpnearestpoint;
622 IL_EACH(g_waypoints, true,
624 if(wp != it.wp00) if(wp != it.wp01) if(wp != it.wp02) if(wp != it.wp03)
625 if(wp != it.wp04) if(wp != it.wp05) if(wp != it.wp06) if(wp != it.wp07)
626 if(wp != it.wp08) if(wp != it.wp09) if(wp != it.wp10) if(wp != it.wp11)
627 if(wp != it.wp12) if(wp != it.wp13) if(wp != it.wp14) if(wp != it.wp15)
628 if(wp != it.wp16) if(wp != it.wp17) if(wp != it.wp18) if(wp != it.wp19)
629 if(wp != it.wp20) if(wp != it.wp21) if(wp != it.wp22) if(wp != it.wp23)
630 if(wp != it.wp24) if(wp != it.wp25) if(wp != it.wp26) if(wp != it.wp27)
631 if(wp != it.wp28) if(wp != it.wp29) if(wp != it.wp30) if(wp != it.wp31)
633 cost2 = cost + it.dmg;
634 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
640 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
641 void navigation_routerating(entity this, entity e, float f, float rangebias)
651 o = (e.absmin + e.absmax) * 0.5;
653 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
655 // Evaluate path using jetpack
657 if(this.items & IT_JETPACK)
658 if(autocvar_bot_ai_navigation_jetpack)
659 if(vdist(this.origin - o, >, autocvar_bot_ai_navigation_jetpack_mindistance))
661 vector pointa, pointb;
663 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname, "\n");
666 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
667 pointa = trace_endpos - '0 0 1';
670 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
671 pointb = trace_endpos - '0 0 1';
673 // Can I see these two points from the sky?
674 traceline(pointa, pointb, MOVE_NORMAL, this);
676 if(trace_fraction==1)
678 LOG_DEBUG("jetpack ai: can bridge these two points\n");
680 // Lower the altitude of these points as much as possible
681 float zdistance, xydistance, cost, t, fuel;
682 vector down, npa, npb;
684 down = '0 0 -1' * (STAT(PL_MAX, NULL).z - STAT(PL_MIN, NULL).z) * 10;
690 if(npa.z<=this.absmax.z)
693 if(npb.z<=e.absmax.z)
696 traceline(npa, npb, MOVE_NORMAL, this);
697 if(trace_fraction==1)
703 while(trace_fraction == 1);
706 // Rough estimation of fuel consumption
707 // (ignores acceleration and current xyz velocity)
708 xydistance = vlen(pointa - pointb);
709 zdistance = fabs(pointa.z - this.origin.z);
711 t = zdistance / autocvar_g_jetpack_maxspeed_up;
712 t += xydistance / autocvar_g_jetpack_maxspeed_side;
713 fuel = t * autocvar_g_jetpack_fuel * 0.8;
715 LOG_DEBUG(strcat("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel), "\n"));
718 if(this.ammo_fuel>fuel)
721 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
722 // - between air and ground speeds)
724 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
725 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
728 // Compare against other goals
729 f = f * rangebias / (rangebias + cost);
731 if (navigation_bestrating < f)
733 LOG_DEBUG(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
734 navigation_bestrating = f;
735 navigation_bestgoal = e;
736 this.navigation_jetpack_goal = e;
737 this.navigation_jetpack_point = pointb;
744 //te_wizspike(e.origin);
747 // update the cached spawnfunc_waypoint link on a dynamic item entity
748 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
758 if(e.flags & FL_ITEM)
760 if (!(e.flags & FL_WEAPON))
761 if(e.nearestwaypoint)
764 else if (e.flags & FL_WEAPON)
766 if(e.classname != "droppedweapon")
767 if(e.nearestwaypoint)
772 if (time > e.nearestwaypointtimeout)
774 nwp = navigation_findnearestwaypoint(e, true);
776 e.nearestwaypoint = nwp;
779 LOG_DEBUG(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
781 if(e.flags & FL_ITEM)
782 e.blacklisted = true;
783 else if (e.flags & FL_WEAPON)
785 if(e.classname != "droppedweapon")
786 e.blacklisted = true;
791 LOG_DEBUG(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
796 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
797 if(e.classname=="item_flag_team")
798 e.nearestwaypointtimeout = time + 2;
800 e.nearestwaypointtimeout = time + random() * 3 + 5;
802 nwp = e.nearestwaypoint;
805 LOG_DEBUG(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
807 if (nwp.wpcost < 10000000)
809 //te_wizspike(nwp.wpnearestpoint);
810 LOG_DEBUG(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
811 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
812 LOG_DEBUG(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
813 if (navigation_bestrating < f)
815 LOG_DEBUG(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
816 navigation_bestrating = f;
817 navigation_bestgoal = e;
822 // adds an item to the the goal stack with the path to a given item
823 bool navigation_routetogoal(entity this, entity e, vector startposition)
827 // if there is no goal, just exit
831 this.navigation_hasgoals = true;
833 // put the entity on the goal stack
834 //print("routetogoal ", etos(e), "\n");
835 navigation_pushroute(this, e);
838 if(e==this.navigation_jetpack_goal)
841 // if it can reach the goal there is nothing more to do
842 if (tracewalk(this, startposition, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
845 // see if there are waypoints describing a path to the item
846 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
847 e = e.nearestwaypoint;
849 e = e.enemy; // we already have added it, so...
856 // add the spawnfunc_waypoint to the path
857 navigation_pushroute(this, e);
867 // removes any currently touching waypoints from the goal stack
868 // (this is how bots detect if they reached a goal)
869 void navigation_poptouchedgoals(entity this)
873 m1 = org + this.mins;
874 m2 = org + this.maxs;
876 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
878 if(this.lastteleporttime>0)
879 if(time-this.lastteleporttime<(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
881 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
882 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
884 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
885 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
887 navigation_poproute(this);
892 // If for some reason the bot is closer to the next goal, pop the current one
894 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
895 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
896 if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
898 LOG_DEBUG(strcat("path optimized for ", this.netname, ", removed a goal from the queue\n"));
899 navigation_poproute(this);
900 // TODO this may also be a nice idea to do "early" (e.g. by
901 // manipulating the vlen() comparisons) to shorten paths in
902 // general - this would make bots walk more "on rails" than
903 // "zigzagging" which they currently do with sufficiently
904 // random-like waypoints, and thus can make a nice bot
905 // personality property
908 // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
909 // TODO: rate waypoints near the targetted player at that moment, instead of the player itthis
910 if(IS_PLAYER(this.goalcurrent))
911 navigation_poproute(this);
913 // aid for detecting jump pads better (distance based check fails sometimes)
914 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && this.jumppadcount > 0 )
915 navigation_poproute(this);
917 // Loose goal touching check when running
918 if(this.aistatus & AI_STATUS_RUNNING)
919 if(this.speed >= autocvar_sv_maxspeed) // if -really- running
920 if(this.goalcurrent.classname=="waypoint")
922 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
924 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
925 if(trace_fraction==1)
927 // Detect personal waypoints
928 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
929 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
931 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
932 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
935 navigation_poproute(this);
940 while (this.goalcurrent && boxesoverlap(m1, m2, this.goalcurrent.absmin, this.goalcurrent.absmax))
942 // Detect personal waypoints
943 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
944 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
946 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
947 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
950 navigation_poproute(this);
954 // begin a goal selection session (queries spawnfunc_waypoint network)
955 void navigation_goalrating_start(entity this)
957 if(this.aistatus & AI_STATUS_STUCK)
960 this.navigation_jetpack_goal = NULL;
961 navigation_bestrating = -1;
962 this.navigation_hasgoals = false;
963 navigation_clearroute(this);
964 navigation_bestgoal = NULL;
965 navigation_markroutes(this, NULL);
968 // ends a goal selection session (updates goal stack to the best goal)
969 void navigation_goalrating_end(entity this)
971 if(this.aistatus & AI_STATUS_STUCK)
974 navigation_routetogoal(this, navigation_bestgoal, this.origin);
975 LOG_DEBUG(strcat("best goal ", this.goalcurrent.classname , "\n"));
977 // If the bot got stuck then try to reach the farthest waypoint
978 if (!this.navigation_hasgoals)
979 if (autocvar_bot_wander_enable)
981 if (!(this.aistatus & AI_STATUS_STUCK))
983 LOG_DEBUG(strcat(this.netname, " cannot walk to any goal\n"));
984 this.aistatus |= AI_STATUS_STUCK;
987 this.navigation_hasgoals = false; // Reset this value
991 void botframe_updatedangerousobjects(float maxupdate)
996 IL_EACH(g_waypoints, true,
1001 FOREACH_ENTITY_FLOAT(bot_dodge, true,
1004 v.x = bound(m1_x, v.x, m2_x);
1005 v.y = bound(m1_y, v.y, m2_y);
1006 v.z = bound(m1_z, v.z, m2_z);
1007 o = (it.absmin + it.absmax) * 0.5;
1008 d = it.bot_dodgerating - vlen(o - v);
1011 traceline(o, v, true, NULL);
1012 if (trace_fraction == 1)
1013 danger = danger + d;
1023 void navigation_unstuck(entity this)
1025 float search_radius = 1000;
1027 if (!autocvar_bot_wander_enable)
1030 if (!bot_waypoint_queue_owner)
1032 LOG_DEBUG(strcat(this.netname, " sutck, taking over the waypoints queue\n"));
1033 bot_waypoint_queue_owner = this;
1034 bot_waypoint_queue_bestgoal = NULL;
1035 bot_waypoint_queue_bestgoalrating = 0;
1038 if(bot_waypoint_queue_owner!=this)
1041 if (bot_waypoint_queue_goal)
1043 // evaluate the next goal on the queue
1044 float d = vlen(this.origin - bot_waypoint_queue_goal.origin);
1045 LOG_DEBUG(strcat(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1046 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1048 if( d > bot_waypoint_queue_bestgoalrating)
1050 bot_waypoint_queue_bestgoalrating = d;
1051 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1054 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1056 if (!bot_waypoint_queue_goal)
1058 if (bot_waypoint_queue_bestgoal)
1060 LOG_DEBUG(strcat(this.netname, " stuck, reachable waypoint found, heading to it\n"));
1061 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1062 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1063 this.aistatus &= ~AI_STATUS_STUCK;
1067 LOG_DEBUG(strcat(this.netname, " stuck, cannot walk to any waypoint at all\n"));
1070 bot_waypoint_queue_owner = NULL;
1075 if(bot_strategytoken!=this)
1078 // build a new queue
1079 LOG_DEBUG(strcat(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1081 entity first = NULL;
1083 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1085 if(bot_waypoint_queue_goal)
1086 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1090 bot_waypoint_queue_goal = it;
1091 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1095 bot_waypoint_queue_goal = first;
1098 LOG_DEBUG(strcat(this.netname, " stuck, cannot walk to any waypoint at all\n"));
1099 bot_waypoint_queue_owner = NULL;
1104 // Support for debugging tracewalk visually
1106 void debugresetnodes()
1108 debuglastnode = '0 0 0';
1111 void debugnode(entity this, vector node)
1113 if (!IS_PLAYER(this))
1116 if(debuglastnode=='0 0 0')
1118 debuglastnode = node;
1122 te_lightning2(NULL, node, debuglastnode);
1123 debuglastnode = node;
1126 void debugnodestatus(vector position, float status)
1132 case DEBUG_NODE_SUCCESS:
1135 case DEBUG_NODE_WARNING:
1138 case DEBUG_NODE_FAIL:
1145 te_customflash(position, 40, 2, c);
1148 // Support for debugging the goal stack visually
1151 .vector lastposition;
1153 // Debug the goal stack visually
1154 void debuggoalstack(entity this)
1159 if(this.goalcounter==0)goal=this.goalcurrent;
1160 else if(this.goalcounter==1)goal=this.goalstack01;
1161 else if(this.goalcounter==2)goal=this.goalstack02;
1162 else if(this.goalcounter==3)goal=this.goalstack03;
1163 else if(this.goalcounter==4)goal=this.goalstack04;
1164 else if(this.goalcounter==5)goal=this.goalstack05;
1165 else if(this.goalcounter==6)goal=this.goalstack06;
1166 else if(this.goalcounter==7)goal=this.goalstack07;
1167 else if(this.goalcounter==8)goal=this.goalstack08;
1168 else if(this.goalcounter==9)goal=this.goalstack09;
1169 else if(this.goalcounter==10)goal=this.goalstack10;
1170 else if(this.goalcounter==11)goal=this.goalstack11;
1171 else if(this.goalcounter==12)goal=this.goalstack12;
1172 else if(this.goalcounter==13)goal=this.goalstack13;
1173 else if(this.goalcounter==14)goal=this.goalstack14;
1174 else if(this.goalcounter==15)goal=this.goalstack15;
1175 else if(this.goalcounter==16)goal=this.goalstack16;
1176 else if(this.goalcounter==17)goal=this.goalstack17;
1177 else if(this.goalcounter==18)goal=this.goalstack18;
1178 else if(this.goalcounter==19)goal=this.goalstack19;
1179 else if(this.goalcounter==20)goal=this.goalstack20;
1180 else if(this.goalcounter==21)goal=this.goalstack21;
1181 else if(this.goalcounter==22)goal=this.goalstack22;
1182 else if(this.goalcounter==23)goal=this.goalstack23;
1183 else if(this.goalcounter==24)goal=this.goalstack24;
1184 else if(this.goalcounter==25)goal=this.goalstack25;
1185 else if(this.goalcounter==26)goal=this.goalstack26;
1186 else if(this.goalcounter==27)goal=this.goalstack27;
1187 else if(this.goalcounter==28)goal=this.goalstack28;
1188 else if(this.goalcounter==29)goal=this.goalstack29;
1189 else if(this.goalcounter==30)goal=this.goalstack30;
1190 else if(this.goalcounter==31)goal=this.goalstack31;
1195 this.goalcounter = 0;
1196 this.lastposition='0 0 0';
1200 if(this.lastposition=='0 0 0')
1203 org = this.lastposition;
1206 go = ( goal.absmin + goal.absmax ) * 0.5;
1207 te_lightning2(NULL, org, go);
1208 this.lastposition = go;