1 #include "navigation.qh"
6 #include "waypoints.qh"
8 #include <common/t_items.qh>
10 #include <common/items/_mod.qh>
12 #include <common/constants.qh>
13 #include <common/net_linked.qh>
14 #include <common/triggers/trigger/jumppads.qh>
18 void navigation_dynamicgoal_init(entity this, bool initially_static)
20 this.navigation_dynamicgoal = true;
21 this.bot_basewaypoint = this.nearestwaypoint;
23 this.nearestwaypointtimeout = -1;
25 this.nearestwaypointtimeout = time;
28 void navigation_dynamicgoal_set(entity this)
30 this.nearestwaypointtimeout = time;
33 void navigation_dynamicgoal_unset(entity this)
35 if(this.bot_basewaypoint)
36 this.nearestwaypoint = this.bot_basewaypoint;
37 this.nearestwaypointtimeout = -1;
40 // rough simulation of walking from one point to another to test if a path
41 // can be traveled, used for waypoint linking and havocbot
43 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
54 if(autocvar_bot_debug_tracewalk)
63 dist = totaldist = vlen(move);
64 dir = normalize(move);
66 ignorehazards = false;
69 // Analyze starting point
70 traceline(start, start, MOVE_NORMAL, e);
71 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
75 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
76 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
82 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
86 if(autocvar_bot_debug_tracewalk)
87 debugnodestatus(start, DEBUG_NODE_FAIL);
89 //print("tracewalk: ", vtos(start), " is a bad start\n");
97 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
100 if(autocvar_bot_debug_tracewalk)
101 debugnodestatus(org, DEBUG_NODE_SUCCESS);
103 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
106 if(autocvar_bot_debug_tracewalk)
113 dist = dist - stepdist;
114 traceline(org, org, MOVE_NORMAL, e);
117 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
119 // hazards blocking path
120 if(autocvar_bot_debug_tracewalk)
121 debugnodestatus(org, DEBUG_NODE_FAIL);
123 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
127 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
129 move = normalize(end - org);
130 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
132 if(autocvar_bot_debug_tracewalk)
133 debugnode(e, trace_endpos);
135 if (trace_fraction < 1)
138 org = trace_endpos + normalize(org - trace_endpos) * stepdist;
139 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
141 if(autocvar_bot_debug_tracewalk)
144 if(pointcontents(org) == CONTENT_EMPTY)
148 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
150 if(autocvar_bot_debug_tracewalk)
151 debugnodestatus(org, DEBUG_NODE_FAIL);
154 //print("tracewalk: ", vtos(start), " failed under water\n");
164 move = dir * stepdist + org;
165 tracebox(org, m1, m2, move, movemode, e);
167 if(autocvar_bot_debug_tracewalk)
168 debugnode(e, trace_endpos);
171 if (trace_fraction < 1)
173 // check if we can walk over this obstacle, possibly by jumpstepping
174 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
175 if (trace_fraction < 1 || trace_startsolid)
177 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
178 if (trace_fraction < 1 || trace_startsolid)
180 bool ladder_found = false;
181 FOREACH_ENTITY_CLASS("func_ladder", true,
183 if(boxesoverlap(org + jumpheight_vec + m1 + '-1 -1 -1', org + jumpheight_vec + m2 + '1 1 1', it.absmin, it.absmax))
184 if(boxesoverlap(end, end, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
185 ladder_found = true; // can't return here ("Loop mutex held by tracewalk" error)
189 if(autocvar_bot_debug_tracewalk)
190 debugnodestatus(end, DEBUG_NODE_SUCCESS);
192 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
196 if(autocvar_bot_debug_tracewalk)
197 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
199 traceline( org, move, movemode, e);
201 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
205 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
207 nextmove = move + (dir * stepdist);
208 traceline( move, nextmove, movemode, e);
214 if(autocvar_bot_debug_tracewalk)
215 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
217 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
218 //te_explosion(trace_endpos);
219 //print(ftos(e.dphitcontentsmask), "\n");
220 return false; // failed
232 // trace down from stepheight as far as possible and move there,
233 // if this starts in solid we try again without the stepup, and
234 // if that also fails we assume it is a wall
235 // (this is the same logic as the Quake walkmove function used)
236 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
238 // moved successfully
242 c = pointcontents(org + '0 0 1');
243 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
253 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
255 // moved but didn't arrive at the intended destination
256 if(autocvar_bot_debug_tracewalk)
257 debugnodestatus(org, DEBUG_NODE_FAIL);
262 /////////////////////////////////////////////////////////////////////////////
264 /////////////////////////////////////////////////////////////////////////////
266 // completely empty the goal stack, used when deciding where to go
267 void navigation_clearroute(entity this)
269 //print("bot ", etos(this), " clear\n");
270 this.goalentity = NULL;
271 this.goalcurrent = NULL;
272 this.goalstack01 = NULL;
273 this.goalstack02 = NULL;
274 this.goalstack03 = NULL;
275 this.goalstack04 = NULL;
276 this.goalstack05 = NULL;
277 this.goalstack06 = NULL;
278 this.goalstack07 = NULL;
279 this.goalstack08 = NULL;
280 this.goalstack09 = NULL;
281 this.goalstack10 = NULL;
282 this.goalstack11 = NULL;
283 this.goalstack12 = NULL;
284 this.goalstack13 = NULL;
285 this.goalstack14 = NULL;
286 this.goalstack15 = NULL;
287 this.goalstack16 = NULL;
288 this.goalstack17 = NULL;
289 this.goalstack18 = NULL;
290 this.goalstack19 = NULL;
291 this.goalstack20 = NULL;
292 this.goalstack21 = NULL;
293 this.goalstack22 = NULL;
294 this.goalstack23 = NULL;
295 this.goalstack24 = NULL;
296 this.goalstack25 = NULL;
297 this.goalstack26 = NULL;
298 this.goalstack27 = NULL;
299 this.goalstack28 = NULL;
300 this.goalstack29 = NULL;
301 this.goalstack30 = NULL;
302 this.goalstack31 = NULL;
305 // add a new goal at the beginning of the stack
306 // (in other words: add a new prerequisite before going to the later goals)
307 // NOTE: when a waypoint is added, the WP gets pushed first, then the
308 // next-closest WP on the shortest path to the WP
309 // That means, if the stack overflows, the bot will know how to do the FIRST 32
310 // steps to the goal, and then recalculate the path.
311 void navigation_pushroute(entity this, entity e)
313 //print("bot ", etos(this), " push ", etos(e), "\n");
314 if(this.goalstack31 == this.goalentity)
315 this.goalentity = NULL;
316 this.goalstack31 = this.goalstack30;
317 this.goalstack30 = this.goalstack29;
318 this.goalstack29 = this.goalstack28;
319 this.goalstack28 = this.goalstack27;
320 this.goalstack27 = this.goalstack26;
321 this.goalstack26 = this.goalstack25;
322 this.goalstack25 = this.goalstack24;
323 this.goalstack24 = this.goalstack23;
324 this.goalstack23 = this.goalstack22;
325 this.goalstack22 = this.goalstack21;
326 this.goalstack21 = this.goalstack20;
327 this.goalstack20 = this.goalstack19;
328 this.goalstack19 = this.goalstack18;
329 this.goalstack18 = this.goalstack17;
330 this.goalstack17 = this.goalstack16;
331 this.goalstack16 = this.goalstack15;
332 this.goalstack15 = this.goalstack14;
333 this.goalstack14 = this.goalstack13;
334 this.goalstack13 = this.goalstack12;
335 this.goalstack12 = this.goalstack11;
336 this.goalstack11 = this.goalstack10;
337 this.goalstack10 = this.goalstack09;
338 this.goalstack09 = this.goalstack08;
339 this.goalstack08 = this.goalstack07;
340 this.goalstack07 = this.goalstack06;
341 this.goalstack06 = this.goalstack05;
342 this.goalstack05 = this.goalstack04;
343 this.goalstack04 = this.goalstack03;
344 this.goalstack03 = this.goalstack02;
345 this.goalstack02 = this.goalstack01;
346 this.goalstack01 = this.goalcurrent;
347 this.goalcurrent = e;
350 // remove first goal from stack
351 // (in other words: remove a prerequisite for reaching the later goals)
352 // (used when a spawnfunc_waypoint is reached)
353 void navigation_poproute(entity this)
355 //print("bot ", etos(this), " pop\n");
356 if(this.goalcurrent == this.goalentity)
357 this.goalentity = NULL;
358 this.goalcurrent = this.goalstack01;
359 this.goalstack01 = this.goalstack02;
360 this.goalstack02 = this.goalstack03;
361 this.goalstack03 = this.goalstack04;
362 this.goalstack04 = this.goalstack05;
363 this.goalstack05 = this.goalstack06;
364 this.goalstack06 = this.goalstack07;
365 this.goalstack07 = this.goalstack08;
366 this.goalstack08 = this.goalstack09;
367 this.goalstack09 = this.goalstack10;
368 this.goalstack10 = this.goalstack11;
369 this.goalstack11 = this.goalstack12;
370 this.goalstack12 = this.goalstack13;
371 this.goalstack13 = this.goalstack14;
372 this.goalstack14 = this.goalstack15;
373 this.goalstack15 = this.goalstack16;
374 this.goalstack16 = this.goalstack17;
375 this.goalstack17 = this.goalstack18;
376 this.goalstack18 = this.goalstack19;
377 this.goalstack19 = this.goalstack20;
378 this.goalstack20 = this.goalstack21;
379 this.goalstack21 = this.goalstack22;
380 this.goalstack22 = this.goalstack23;
381 this.goalstack23 = this.goalstack24;
382 this.goalstack24 = this.goalstack25;
383 this.goalstack25 = this.goalstack26;
384 this.goalstack26 = this.goalstack27;
385 this.goalstack27 = this.goalstack28;
386 this.goalstack28 = this.goalstack29;
387 this.goalstack29 = this.goalstack30;
388 this.goalstack30 = this.goalstack31;
389 this.goalstack31 = NULL;
392 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
395 dist = vlen(v - org);
398 traceline(v, org, true, ent);
399 if (trace_fraction == 1)
403 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, bot_navigation_movemode))
408 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, bot_navigation_movemode))
416 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
417 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
419 vector pm1 = ent.origin + ent.mins;
420 vector pm2 = ent.origin + ent.maxs;
422 // do two scans, because box test is cheaper
423 IL_EACH(g_waypoints, it != ent && it != except,
425 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
429 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
430 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
431 // TODO possibly make other code have the same support for bboxes
433 org = org + ent.tag_entity.origin;
434 if (navigation_testtracewalk)
440 if(!autocvar_g_waypointeditor && !ent.navigation_dynamicgoal)
442 waypoint_clearlinks(ent); // initialize wpXXmincost fields
443 IL_EACH(g_waypoints, it != ent,
447 vector wm1 = it.absmin;
448 vector wm2 = it.absmax;
449 v.x = bound(wm1_x, org.x, wm2_x);
450 v.y = bound(wm1_y, org.y, wm2_y);
451 v.z = bound(wm1_z, org.z, wm2_z);
455 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, 1050))
456 navigation_item_addlink(it, ent);
460 // box check failed, try walk
461 IL_EACH(g_waypoints, it != ent,
465 vector wm1 = it.origin + it.mins;
466 vector wm2 = it.origin + it.maxs;
467 v.x = bound(wm1_x, org.x, wm2_x);
468 v.y = bound(wm1_y, org.y, wm2_y);
469 v.z = bound(wm1_z, org.z, wm2_z);
473 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
475 bestdist = vlen(v - org);
481 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
483 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
484 if (autocvar_g_waypointeditor_auto)
486 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
488 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
493 // finds the waypoints near the bot initiating a navigation query
494 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
497 // navigation_testtracewalk = true;
499 IL_EACH(g_waypoints, !it.wpconsidered,
503 m1 = it.origin + it.mins;
504 m2 = it.origin + it.maxs;
506 v.x = bound(m1_x, v.x, m2_x);
507 v.y = bound(m1_y, v.y, m2_y);
508 v.z = bound(m1_z, v.z, m2_z);
512 vector diff = v - this.origin;
513 diff.z = max(0, diff.z);
514 if(vdist(diff, <, maxdist))
516 it.wpconsidered = true;
517 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
519 it.wpnearestpoint = v;
520 it.wpcost = waypoint_getdistancecost(this.origin, v) + it.dmg;
527 //navigation_testtracewalk = false;
531 // updates a path link if a spawnfunc_waypoint link is better than the current one
532 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
541 v.x = bound(m1_x, p.x, m2_x);
542 v.y = bound(m1_y, p.y, m2_y);
543 v.z = bound(m1_z, p.z, m2_z);
547 if (w.wpflags & WAYPOINTFLAG_TELEPORT)
548 cost += w.wp00mincost; // assuming teleport has exactly one destination
550 cost += waypoint_getdistancecost(p, v);
551 if (wp.wpcost > cost)
556 wp.wpnearestpoint = v;
560 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
561 void navigation_markroutes(entity this, entity fixed_source_waypoint)
566 IL_EACH(g_waypoints, true,
568 it.wpconsidered = false;
569 it.wpnearestpoint = '0 0 0';
570 it.wpcost = 10000000;
575 if(fixed_source_waypoint)
577 fixed_source_waypoint.wpconsidered = true;
578 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
579 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
580 fixed_source_waypoint.wpfire = 1;
581 fixed_source_waypoint.enemy = NULL;
585 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
586 // as this search is expensive we will use lower values if the bot is on the air
587 float increment, maxdistance;
588 if(IS_ONGROUND(this))
599 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
602 bool searching = true;
606 IL_EACH(g_waypoints, it.wpfire,
611 p = it.wpnearestpoint;
613 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
614 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
615 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
616 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
617 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
618 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
619 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
620 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
621 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
622 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
623 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
624 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
625 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
626 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
627 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
628 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
629 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
630 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
631 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
632 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
633 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
634 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
635 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
636 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
637 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
638 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
639 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
640 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
641 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
642 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
643 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
644 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
645 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
650 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
651 void navigation_markroutes_inverted(entity fixed_source_waypoint)
655 IL_EACH(g_waypoints, true,
657 it.wpconsidered = false;
658 it.wpnearestpoint = '0 0 0';
659 it.wpcost = 10000000;
664 if(fixed_source_waypoint)
666 fixed_source_waypoint.wpconsidered = true;
667 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
668 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
669 fixed_source_waypoint.wpfire = 1;
670 fixed_source_waypoint.enemy = NULL;
674 error("need to start with a waypoint\n");
677 bool searching = true;
681 IL_EACH(g_waypoints, it.wpfire,
685 cost = it.wpcost; // cost to walk from it to home
686 p = it.wpnearestpoint;
688 IL_EACH(g_waypoints, it != wp,
690 if(!waypoint_islinked(it, wp))
692 cost2 = cost + it.dmg;
693 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
699 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
700 void navigation_routerating(entity this, entity e, float f, float rangebias)
708 rangebias = waypoint_getdistancecost_simple(rangebias);
709 f = waypoint_getdistancecost_simple(f);
713 bool rate_wps = false;
714 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
719 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
720 int t = pointcontents(trace_endpos + '0 0 1');
721 if(t != CONTENT_SOLID )
723 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
725 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
733 entity best_wp = NULL;
734 float best_dist = 10000;
735 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
736 && vdist(it.origin - this.origin, >, 100)
737 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
739 float dist = vlen(it.origin - theEnemy.origin);
740 if (dist < best_dist)
752 vector goal_org = (e.absmin + e.absmax) * 0.5;
754 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
756 // Evaluate path using jetpack
758 if(this.items & IT_JETPACK)
759 if(autocvar_bot_ai_navigation_jetpack)
760 if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
762 vector pointa, pointb;
764 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
767 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
768 pointa = trace_endpos - '0 0 1';
771 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
772 pointb = trace_endpos - '0 0 1';
774 // Can I see these two points from the sky?
775 traceline(pointa, pointb, MOVE_NORMAL, this);
777 if(trace_fraction==1)
779 LOG_DEBUG("jetpack ai: can bridge these two points");
781 // Lower the altitude of these points as much as possible
782 float zdistance, xydistance, cost, t, fuel;
783 vector down, npa, npb;
785 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
791 if(npa.z<=this.absmax.z)
794 if(npb.z<=e.absmax.z)
797 traceline(npa, npb, MOVE_NORMAL, this);
798 if(trace_fraction==1)
804 while(trace_fraction == 1);
807 // Rough estimation of fuel consumption
808 // (ignores acceleration and current xyz velocity)
809 xydistance = vlen(pointa - pointb);
810 zdistance = fabs(pointa.z - this.origin.z);
812 t = zdistance / autocvar_g_jetpack_maxspeed_up;
813 t += xydistance / autocvar_g_jetpack_maxspeed_side;
814 fuel = t * autocvar_g_jetpack_fuel * 0.8;
816 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
819 if(this.ammo_fuel>fuel)
822 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
823 // - between air and ground speeds)
825 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
826 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
829 // Compare against other goals
830 f = f * rangebias / (rangebias + cost);
832 if (navigation_bestrating < f)
834 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
835 navigation_bestrating = f;
836 navigation_bestgoal = e;
837 this.navigation_jetpack_goal = e;
838 this.navigation_jetpack_point = pointb;
846 //te_wizspike(e.origin);
849 // update the cached spawnfunc_waypoint link on a dynamic item entity
850 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
856 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
857 e.nearestwaypoint = NULL;
859 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
860 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
862 nwp = navigation_findnearestwaypoint(e, true);
865 e.nearestwaypoint = nwp;
867 vector m1 = nwp.absmin, m2 = nwp.absmax;
868 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
869 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
870 vector ve = (e.absmin - e.absmax) * 0.5;
871 ve.x = bound(m1.x, ve.x, m2.x);
872 ve.y = bound(m1.y, ve.y, m2.y);
873 ve.z = bound(m1.z, ve.z, m2.z);
875 m1 = e.absmin; m2 = e.absmax;
876 m1.x = e.origin.x; m1.y = e.origin.y;
877 m2.x = e.origin.x; m2.y = e.origin.y;
878 vector vnwp = nwp.origin;
879 vnwp.x = bound(m1.x, vnwp.x, m2.x);
880 vnwp.y = bound(m1.y, vnwp.y, m2.y);
881 vnwp.z = bound(m1.z, vnwp.z, m2.z);
882 e.nearestwaypoint_dist = vlen(ve - vnwp);
886 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
888 if(!e.navigation_dynamicgoal)
889 e.blacklisted = true;
893 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
898 if(e.navigation_dynamicgoal)
899 e.nearestwaypointtimeout = time + 2;
900 else if(autocvar_g_waypointeditor)
901 e.nearestwaypointtimeout = time + 3 + random() * 2;
903 nwp = e.nearestwaypoint;
906 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
908 if (nwp.wpcost < 10000000)
910 //te_wizspike(nwp.wpnearestpoint);
911 float cost = nwp.wpcost + waypoint_getdistancecost(nwp.wpnearestpoint, goal_org);
912 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
913 f = f * rangebias / (rangebias + cost);
914 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
915 if (navigation_bestrating < f)
917 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
918 navigation_bestrating = f;
919 navigation_bestgoal = e;
924 // adds an item to the the goal stack with the path to a given item
925 bool navigation_routetogoal(entity this, entity e, vector startposition)
927 // if there is no goal, just exit
933 // put the entity on the goal stack
934 //print("routetogoal ", etos(e), "\n");
935 navigation_pushroute(this, e);
937 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
939 this.wp_goal_prev1 = this.wp_goal_prev0;
940 this.wp_goal_prev0 = e;
944 if(e==this.navigation_jetpack_goal)
947 // if it can reach the goal there is nothing more to do
948 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
951 entity nearest_wp = NULL;
952 // see if there are waypoints describing a path to the item
953 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
955 e = e.nearestwaypoint;
959 e = e.enemy; // we already have added it, so...
964 if(nearest_wp && nearest_wp.enemy)
966 // often path can be optimized by not adding the nearest waypoint
967 if (this.goalentity.nearestwaypoint_dist < 8
968 || (!this.goalentity.navigation_dynamicgoal && navigation_item_islinked(nearest_wp.enemy, this.goalentity))
969 || (this.goalentity.navigation_dynamicgoal && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), (this.goalentity.absmin + this.goalentity.absmax) * 0.5, bot_navigation_movemode)))
970 e = nearest_wp.enemy;
975 // add the spawnfunc_waypoint to the path
976 navigation_pushroute(this, e);
986 // removes any currently touching waypoints from the goal stack
987 // (this is how bots detect if they reached a goal)
988 void navigation_poptouchedgoals(entity this)
992 m1 = org + this.mins;
993 m2 = org + this.maxs;
995 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
997 // make sure jumppad is really hit, don't rely on distance based checks
998 // as they may report a touch even if it didn't really happen
999 if(this.lastteleporttime>0)
1000 if(time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1002 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1003 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1005 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1006 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1008 navigation_poproute(this);
1013 // If for some reason the bot is closer to the next goal, pop the current one
1014 if(this.goalstack01 && !wasfreed(this.goalstack01))
1015 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
1016 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1017 if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
1019 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1020 navigation_poproute(this);
1021 // TODO this may also be a nice idea to do "early" (e.g. by
1022 // manipulating the vlen() comparisons) to shorten paths in
1023 // general - this would make bots walk more "on rails" than
1024 // "zigzagging" which they currently do with sufficiently
1025 // random-like waypoints, and thus can make a nice bot
1026 // personality property
1029 // Loose goal touching check when running
1030 if(this.aistatus & AI_STATUS_RUNNING)
1031 if(this.goalcurrent.classname=="waypoint")
1032 if(!(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1033 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
1035 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1037 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1038 if(trace_fraction==1)
1040 // Detect personal waypoints
1041 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1042 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1044 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1045 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1048 navigation_poproute(this);
1053 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1055 vector gc_min = this.goalcurrent.absmin;
1056 vector gc_max = this.goalcurrent.absmax;
1057 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1059 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1060 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1062 if(!boxesoverlap(m1, m2, gc_min, gc_max))
1065 if((this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1068 // Detect personal waypoints
1069 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1070 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1072 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1073 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1076 navigation_poproute(this);
1080 // begin a goal selection session (queries spawnfunc_waypoint network)
1081 void navigation_goalrating_start(entity this)
1083 if(this.aistatus & AI_STATUS_STUCK)
1086 this.navigation_jetpack_goal = NULL;
1087 navigation_bestrating = -1;
1088 navigation_clearroute(this);
1089 navigation_bestgoal = NULL;
1090 navigation_markroutes(this, NULL);
1093 // ends a goal selection session (updates goal stack to the best goal)
1094 void navigation_goalrating_end(entity this)
1096 if(this.aistatus & AI_STATUS_STUCK)
1099 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1100 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1102 // If the bot got stuck then try to reach the farthest waypoint
1103 if (!this.goalentity && autocvar_bot_wander_enable)
1105 if (!(this.aistatus & AI_STATUS_STUCK))
1107 LOG_DEBUG(this.netname, " cannot walk to any goal");
1108 this.aistatus |= AI_STATUS_STUCK;
1113 void botframe_updatedangerousobjects(float maxupdate)
1115 vector m1, m2, v, o;
1118 IL_EACH(g_waypoints, true,
1123 IL_EACH(g_bot_dodge, it.bot_dodge,
1126 v.x = bound(m1_x, v.x, m2_x);
1127 v.y = bound(m1_y, v.y, m2_y);
1128 v.z = bound(m1_z, v.z, m2_z);
1129 o = (it.absmin + it.absmax) * 0.5;
1130 d = waypoint_getdistancecost_simple(it.bot_dodgerating) - waypoint_getdistancecost(o, v);
1133 traceline(o, v, true, NULL);
1134 if (trace_fraction == 1)
1135 danger = danger + d;
1145 void navigation_unstuck(entity this)
1147 float search_radius = 1000;
1149 if (!autocvar_bot_wander_enable)
1152 if (!bot_waypoint_queue_owner)
1154 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1155 bot_waypoint_queue_owner = this;
1156 bot_waypoint_queue_bestgoal = NULL;
1157 bot_waypoint_queue_bestgoalrating = 0;
1160 if(bot_waypoint_queue_owner!=this)
1163 if (bot_waypoint_queue_goal)
1165 // evaluate the next goal on the queue
1166 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1167 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1168 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1170 if( d > bot_waypoint_queue_bestgoalrating)
1172 bot_waypoint_queue_bestgoalrating = d;
1173 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1176 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1178 if (!bot_waypoint_queue_goal)
1180 if (bot_waypoint_queue_bestgoal)
1182 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1183 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1184 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1185 this.aistatus &= ~AI_STATUS_STUCK;
1189 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1192 bot_waypoint_queue_owner = NULL;
1197 if(bot_strategytoken!=this)
1200 // build a new queue
1201 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1203 entity first = NULL;
1205 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1207 if(bot_waypoint_queue_goal)
1208 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1212 bot_waypoint_queue_goal = it;
1213 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1217 bot_waypoint_queue_goal = first;
1220 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1221 bot_waypoint_queue_owner = NULL;
1226 // Support for debugging tracewalk visually
1228 void debugresetnodes()
1230 debuglastnode = '0 0 0';
1233 void debugnode(entity this, vector node)
1235 if (!IS_PLAYER(this))
1238 if(debuglastnode=='0 0 0')
1240 debuglastnode = node;
1244 te_lightning2(NULL, node, debuglastnode);
1245 debuglastnode = node;
1248 void debugnodestatus(vector position, float status)
1254 case DEBUG_NODE_SUCCESS:
1257 case DEBUG_NODE_WARNING:
1260 case DEBUG_NODE_FAIL:
1267 te_customflash(position, 40, 2, c);
1270 // Support for debugging the goal stack visually
1273 .vector lastposition;
1275 // Debug the goal stack visually
1276 void debuggoalstack(entity this)
1281 if(this.goalcounter==0)goal=this.goalcurrent;
1282 else if(this.goalcounter==1)goal=this.goalstack01;
1283 else if(this.goalcounter==2)goal=this.goalstack02;
1284 else if(this.goalcounter==3)goal=this.goalstack03;
1285 else if(this.goalcounter==4)goal=this.goalstack04;
1286 else if(this.goalcounter==5)goal=this.goalstack05;
1287 else if(this.goalcounter==6)goal=this.goalstack06;
1288 else if(this.goalcounter==7)goal=this.goalstack07;
1289 else if(this.goalcounter==8)goal=this.goalstack08;
1290 else if(this.goalcounter==9)goal=this.goalstack09;
1291 else if(this.goalcounter==10)goal=this.goalstack10;
1292 else if(this.goalcounter==11)goal=this.goalstack11;
1293 else if(this.goalcounter==12)goal=this.goalstack12;
1294 else if(this.goalcounter==13)goal=this.goalstack13;
1295 else if(this.goalcounter==14)goal=this.goalstack14;
1296 else if(this.goalcounter==15)goal=this.goalstack15;
1297 else if(this.goalcounter==16)goal=this.goalstack16;
1298 else if(this.goalcounter==17)goal=this.goalstack17;
1299 else if(this.goalcounter==18)goal=this.goalstack18;
1300 else if(this.goalcounter==19)goal=this.goalstack19;
1301 else if(this.goalcounter==20)goal=this.goalstack20;
1302 else if(this.goalcounter==21)goal=this.goalstack21;
1303 else if(this.goalcounter==22)goal=this.goalstack22;
1304 else if(this.goalcounter==23)goal=this.goalstack23;
1305 else if(this.goalcounter==24)goal=this.goalstack24;
1306 else if(this.goalcounter==25)goal=this.goalstack25;
1307 else if(this.goalcounter==26)goal=this.goalstack26;
1308 else if(this.goalcounter==27)goal=this.goalstack27;
1309 else if(this.goalcounter==28)goal=this.goalstack28;
1310 else if(this.goalcounter==29)goal=this.goalstack29;
1311 else if(this.goalcounter==30)goal=this.goalstack30;
1312 else if(this.goalcounter==31)goal=this.goalstack31;
1317 this.goalcounter = 0;
1318 this.lastposition='0 0 0';
1322 if(this.lastposition=='0 0 0')
1325 org = this.lastposition;
1328 go = ( goal.absmin + goal.absmax ) * 0.5;
1329 te_lightning2(NULL, org, go);
1330 this.lastposition = go;