1 #include "navigation.qh"
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
8 #include "waypoints.qh"
10 #include <common/t_items.qh>
12 #include <common/items/_mod.qh>
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/triggers/trigger/jumppads.qh>
20 void navigation_dynamicgoal_init(entity this, bool initially_static)
22 this.navigation_dynamicgoal = true;
23 this.bot_basewaypoint = this.nearestwaypoint;
25 this.nearestwaypointtimeout = -1;
27 this.nearestwaypointtimeout = time;
30 void navigation_dynamicgoal_set(entity this)
32 this.nearestwaypointtimeout = time;
35 void navigation_dynamicgoal_unset(entity this)
37 if(this.bot_basewaypoint)
38 this.nearestwaypoint = this.bot_basewaypoint;
39 this.nearestwaypointtimeout = -1;
42 // rough simulation of walking from one point to another to test if a path
43 // can be traveled, used for waypoint linking and havocbot
45 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
55 entity tw_ladder = NULL;
57 if(autocvar_bot_debug_tracewalk)
66 dist = totaldist = vlen(move);
67 dir = normalize(move);
69 ignorehazards = false;
72 // Analyze starting point
73 traceline(start, start, MOVE_NORMAL, e);
74 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
78 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
79 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
85 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
89 if(autocvar_bot_debug_tracewalk)
90 debugnodestatus(start, DEBUG_NODE_FAIL);
92 //print("tracewalk: ", vtos(start), " is a bad start\n");
100 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
103 if(autocvar_bot_debug_tracewalk)
104 debugnodestatus(org, DEBUG_NODE_SUCCESS);
106 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
109 if(autocvar_bot_debug_tracewalk)
116 dist = dist - stepdist;
117 traceline(org, org, MOVE_NORMAL, e);
120 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
122 // hazards blocking path
123 if(autocvar_bot_debug_tracewalk)
124 debugnodestatus(org, DEBUG_NODE_FAIL);
126 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
130 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
132 move = normalize(end - org);
133 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
135 if(autocvar_bot_debug_tracewalk)
136 debugnode(e, trace_endpos);
138 if (trace_fraction < 1)
141 org = trace_endpos + normalize(org - trace_endpos) * stepdist;
142 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
144 if(autocvar_bot_debug_tracewalk)
147 if(pointcontents(org) == CONTENT_EMPTY)
151 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
153 if(autocvar_bot_debug_tracewalk)
154 debugnodestatus(org, DEBUG_NODE_FAIL);
157 //print("tracewalk: ", vtos(start), " failed under water\n");
167 move = dir * stepdist + org;
168 tracebox(org, m1, m2, move, movemode, e);
170 if(autocvar_bot_debug_tracewalk)
171 debugnode(e, trace_endpos);
174 if (trace_fraction < 1)
176 // check if we can walk over this obstacle, possibly by jumpstepping
177 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
178 if (trace_fraction < 1 || trace_startsolid)
180 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
181 if (trace_fraction < 1 || trace_startsolid)
183 if(autocvar_bot_debug_tracewalk)
184 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
186 IL_EACH(g_ladders, it.classname == "func_ladder",
187 { it.solid = SOLID_BSP; });
189 traceline( org, move, movemode, e);
191 IL_EACH(g_ladders, it.classname == "func_ladder",
192 { it.solid = SOLID_TRIGGER; });
194 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
198 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
200 nextmove = move + (dir * stepdist);
201 traceline( move, nextmove, movemode, e);
205 else if (trace_ent.classname == "func_ladder")
207 tw_ladder = trace_ent;
208 vector ladder_bottom = trace_endpos - dir * m2.x;
209 vector ladder_top = ladder_bottom;
210 ladder_top.z = trace_ent.absmax.z + (-m1.z + 1);
211 tracebox(ladder_bottom, m1, m2, ladder_top, movemode, e);
212 if (trace_fraction < 1 || trace_startsolid)
214 if(autocvar_bot_debug_tracewalk)
215 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
217 return false; // failed
219 org = ladder_top + dir * m2.x;
220 move = org + dir * stepdist;
225 if(autocvar_bot_debug_tracewalk)
226 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
228 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
229 //te_explosion(trace_endpos);
230 //print(ftos(e.dphitcontentsmask), "\n");
231 return false; // failed
243 // trace down from stepheight as far as possible and move there,
244 // if this starts in solid we try again without the stepup, and
245 // if that also fails we assume it is a wall
246 // (this is the same logic as the Quake walkmove function used)
247 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
249 // moved successfully
253 c = pointcontents(org + '0 0 1');
254 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
263 if(tw_ladder && org.z < tw_ladder.absmax.z)
265 // stop tracewalk if destination height is lower than the top of the ladder
266 // otherwise bot can't easily figure out climbing direction
267 if(autocvar_bot_debug_tracewalk)
268 debugnodestatus(org, DEBUG_NODE_FAIL);
274 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
276 // moved but didn't arrive at the intended destination
277 if(autocvar_bot_debug_tracewalk)
278 debugnodestatus(org, DEBUG_NODE_FAIL);
283 /////////////////////////////////////////////////////////////////////////////
285 /////////////////////////////////////////////////////////////////////////////
287 // completely empty the goal stack, used when deciding where to go
288 void navigation_clearroute(entity this)
290 //print("bot ", etos(this), " clear\n");
291 this.goalentity = NULL;
292 this.goalcurrent = NULL;
293 this.goalstack01 = NULL;
294 this.goalstack02 = NULL;
295 this.goalstack03 = NULL;
296 this.goalstack04 = NULL;
297 this.goalstack05 = NULL;
298 this.goalstack06 = NULL;
299 this.goalstack07 = NULL;
300 this.goalstack08 = NULL;
301 this.goalstack09 = NULL;
302 this.goalstack10 = NULL;
303 this.goalstack11 = NULL;
304 this.goalstack12 = NULL;
305 this.goalstack13 = NULL;
306 this.goalstack14 = NULL;
307 this.goalstack15 = NULL;
308 this.goalstack16 = NULL;
309 this.goalstack17 = NULL;
310 this.goalstack18 = NULL;
311 this.goalstack19 = NULL;
312 this.goalstack20 = NULL;
313 this.goalstack21 = NULL;
314 this.goalstack22 = NULL;
315 this.goalstack23 = NULL;
316 this.goalstack24 = NULL;
317 this.goalstack25 = NULL;
318 this.goalstack26 = NULL;
319 this.goalstack27 = NULL;
320 this.goalstack28 = NULL;
321 this.goalstack29 = NULL;
322 this.goalstack30 = NULL;
323 this.goalstack31 = NULL;
326 // add a new goal at the beginning of the stack
327 // (in other words: add a new prerequisite before going to the later goals)
328 // NOTE: when a waypoint is added, the WP gets pushed first, then the
329 // next-closest WP on the shortest path to the WP
330 // That means, if the stack overflows, the bot will know how to do the FIRST 32
331 // steps to the goal, and then recalculate the path.
332 void navigation_pushroute(entity this, entity e)
334 //print("bot ", etos(this), " push ", etos(e), "\n");
335 if(this.goalstack31 == this.goalentity)
336 this.goalentity = NULL;
337 this.goalstack31 = this.goalstack30;
338 this.goalstack30 = this.goalstack29;
339 this.goalstack29 = this.goalstack28;
340 this.goalstack28 = this.goalstack27;
341 this.goalstack27 = this.goalstack26;
342 this.goalstack26 = this.goalstack25;
343 this.goalstack25 = this.goalstack24;
344 this.goalstack24 = this.goalstack23;
345 this.goalstack23 = this.goalstack22;
346 this.goalstack22 = this.goalstack21;
347 this.goalstack21 = this.goalstack20;
348 this.goalstack20 = this.goalstack19;
349 this.goalstack19 = this.goalstack18;
350 this.goalstack18 = this.goalstack17;
351 this.goalstack17 = this.goalstack16;
352 this.goalstack16 = this.goalstack15;
353 this.goalstack15 = this.goalstack14;
354 this.goalstack14 = this.goalstack13;
355 this.goalstack13 = this.goalstack12;
356 this.goalstack12 = this.goalstack11;
357 this.goalstack11 = this.goalstack10;
358 this.goalstack10 = this.goalstack09;
359 this.goalstack09 = this.goalstack08;
360 this.goalstack08 = this.goalstack07;
361 this.goalstack07 = this.goalstack06;
362 this.goalstack06 = this.goalstack05;
363 this.goalstack05 = this.goalstack04;
364 this.goalstack04 = this.goalstack03;
365 this.goalstack03 = this.goalstack02;
366 this.goalstack02 = this.goalstack01;
367 this.goalstack01 = this.goalcurrent;
368 this.goalcurrent = e;
371 // remove first goal from stack
372 // (in other words: remove a prerequisite for reaching the later goals)
373 // (used when a spawnfunc_waypoint is reached)
374 void navigation_poproute(entity this)
376 //print("bot ", etos(this), " pop\n");
377 if(this.goalcurrent == this.goalentity)
378 this.goalentity = NULL;
379 this.goalcurrent = this.goalstack01;
380 this.goalstack01 = this.goalstack02;
381 this.goalstack02 = this.goalstack03;
382 this.goalstack03 = this.goalstack04;
383 this.goalstack04 = this.goalstack05;
384 this.goalstack05 = this.goalstack06;
385 this.goalstack06 = this.goalstack07;
386 this.goalstack07 = this.goalstack08;
387 this.goalstack08 = this.goalstack09;
388 this.goalstack09 = this.goalstack10;
389 this.goalstack10 = this.goalstack11;
390 this.goalstack11 = this.goalstack12;
391 this.goalstack12 = this.goalstack13;
392 this.goalstack13 = this.goalstack14;
393 this.goalstack14 = this.goalstack15;
394 this.goalstack15 = this.goalstack16;
395 this.goalstack16 = this.goalstack17;
396 this.goalstack17 = this.goalstack18;
397 this.goalstack18 = this.goalstack19;
398 this.goalstack19 = this.goalstack20;
399 this.goalstack20 = this.goalstack21;
400 this.goalstack21 = this.goalstack22;
401 this.goalstack22 = this.goalstack23;
402 this.goalstack23 = this.goalstack24;
403 this.goalstack24 = this.goalstack25;
404 this.goalstack25 = this.goalstack26;
405 this.goalstack26 = this.goalstack27;
406 this.goalstack27 = this.goalstack28;
407 this.goalstack28 = this.goalstack29;
408 this.goalstack29 = this.goalstack30;
409 this.goalstack30 = this.goalstack31;
410 this.goalstack31 = NULL;
413 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
416 dist = vlen(v - org);
419 traceline(v, org, true, ent);
420 if (trace_fraction == 1)
424 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, bot_navigation_movemode))
429 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, bot_navigation_movemode))
437 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
438 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
440 vector pm1 = ent.origin + ent.mins;
441 vector pm2 = ent.origin + ent.maxs;
443 // do two scans, because box test is cheaper
444 IL_EACH(g_waypoints, it != ent && it != except,
446 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
450 vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
451 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
452 // TODO possibly make other code have the same support for bboxes
454 org = org + ent.tag_entity.origin;
455 if (navigation_testtracewalk)
461 // box check failed, try walk
462 IL_EACH(g_waypoints, it != ent,
466 vector wm1 = it.origin + it.mins;
467 vector wm2 = it.origin + it.maxs;
468 v.x = bound(wm1_x, org.x, wm2_x);
469 v.y = bound(wm1_y, org.y, wm2_y);
470 v.z = bound(wm1_z, org.z, wm2_z);
474 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
476 bestdist = vlen(v - org);
482 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
484 entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
485 if (autocvar_g_waypointeditor_auto)
487 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
489 wp.wpflags |= WAYPOINTFLAG_PROTECTED;
494 // finds the waypoints near the bot initiating a navigation query
495 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
498 // navigation_testtracewalk = true;
500 IL_EACH(g_waypoints, !it.wpconsidered,
504 m1 = it.origin + it.mins;
505 m2 = it.origin + it.maxs;
507 v.x = bound(m1_x, v.x, m2_x);
508 v.y = bound(m1_y, v.y, m2_y);
509 v.z = bound(m1_z, v.z, m2_z);
513 vector diff = v - this.origin;
514 diff.z = max(0, diff.z);
515 if(vdist(diff, <, maxdist))
517 it.wpconsidered = true;
518 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
520 it.wpnearestpoint = v;
521 it.wpcost = vlen(v - this.origin) + it.dmg;
528 //navigation_testtracewalk = false;
532 // updates a path link if a spawnfunc_waypoint link is better than the current one
533 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
542 v.x = bound(m1_x, p.x, m2_x);
543 v.y = bound(m1_y, p.y, m2_y);
544 v.z = bound(m1_z, p.z, m2_z);
548 cost2 = cost2 + vlen(v - p);
549 if (wp.wpcost > cost2)
554 wp.wpnearestpoint = v;
558 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
559 void navigation_markroutes(entity this, entity fixed_source_waypoint)
564 IL_EACH(g_waypoints, true,
566 it.wpconsidered = false;
567 it.wpnearestpoint = '0 0 0';
568 it.wpcost = 10000000;
573 if(fixed_source_waypoint)
575 fixed_source_waypoint.wpconsidered = true;
576 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
577 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
578 fixed_source_waypoint.wpfire = 1;
579 fixed_source_waypoint.enemy = NULL;
583 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
584 // as this search is expensive we will use lower values if the bot is on the air
585 float increment, maxdistance;
586 if(IS_ONGROUND(this))
597 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
600 bool searching = true;
604 IL_EACH(g_waypoints, it.wpfire,
609 p = it.wpnearestpoint;
611 wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
612 wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
613 wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
614 wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
615 wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
616 wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
617 wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
618 wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
619 wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
620 wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
621 wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
622 wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
623 wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
624 wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
625 wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
626 wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
627 wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
628 wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
629 wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
630 wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
631 wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
632 wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
633 wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
634 wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
635 wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
636 wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
637 wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
638 wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
639 wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
640 wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
641 wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
642 wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
643 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
648 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
649 void navigation_markroutes_inverted(entity fixed_source_waypoint)
653 IL_EACH(g_waypoints, true,
655 it.wpconsidered = false;
656 it.wpnearestpoint = '0 0 0';
657 it.wpcost = 10000000;
662 if(fixed_source_waypoint)
664 fixed_source_waypoint.wpconsidered = true;
665 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
666 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
667 fixed_source_waypoint.wpfire = 1;
668 fixed_source_waypoint.enemy = NULL;
672 error("need to start with a waypoint\n");
675 bool searching = true;
679 IL_EACH(g_waypoints, it.wpfire,
683 cost = it.wpcost; // cost to walk from it to home
684 p = it.wpnearestpoint;
686 IL_EACH(g_waypoints, true,
688 if(wp != it.wp00) if(wp != it.wp01) if(wp != it.wp02) if(wp != it.wp03)
689 if(wp != it.wp04) if(wp != it.wp05) if(wp != it.wp06) if(wp != it.wp07)
690 if(wp != it.wp08) if(wp != it.wp09) if(wp != it.wp10) if(wp != it.wp11)
691 if(wp != it.wp12) if(wp != it.wp13) if(wp != it.wp14) if(wp != it.wp15)
692 if(wp != it.wp16) if(wp != it.wp17) if(wp != it.wp18) if(wp != it.wp19)
693 if(wp != it.wp20) if(wp != it.wp21) if(wp != it.wp22) if(wp != it.wp23)
694 if(wp != it.wp24) if(wp != it.wp25) if(wp != it.wp26) if(wp != it.wp27)
695 if(wp != it.wp28) if(wp != it.wp29) if(wp != it.wp30) if(wp != it.wp31)
697 cost2 = cost + it.dmg;
698 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
704 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
705 void navigation_routerating(entity this, entity e, float f, float rangebias)
715 bool rate_wps = false;
716 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
721 traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
722 int t = pointcontents(trace_endpos + '0 0 1');
723 if(t != CONTENT_SOLID )
725 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
727 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
735 entity best_wp = NULL;
736 float best_dist = 10000;
737 IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
738 && vdist(it.origin - this.origin, >, 100)
739 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
741 float dist = vlen(it.origin - theEnemy.origin);
742 if (dist < best_dist)
754 vector o = (e.absmin + e.absmax) * 0.5;
756 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
758 // Evaluate path using jetpack
760 if(this.items & IT_JETPACK)
761 if(autocvar_bot_ai_navigation_jetpack)
762 if(vdist(this.origin - o, >, autocvar_bot_ai_navigation_jetpack_mindistance))
764 vector pointa, pointb;
766 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
769 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
770 pointa = trace_endpos - '0 0 1';
773 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
774 pointb = trace_endpos - '0 0 1';
776 // Can I see these two points from the sky?
777 traceline(pointa, pointb, MOVE_NORMAL, this);
779 if(trace_fraction==1)
781 LOG_DEBUG("jetpack ai: can bridge these two points");
783 // Lower the altitude of these points as much as possible
784 float zdistance, xydistance, cost, t, fuel;
785 vector down, npa, npb;
787 down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
793 if(npa.z<=this.absmax.z)
796 if(npb.z<=e.absmax.z)
799 traceline(npa, npb, MOVE_NORMAL, this);
800 if(trace_fraction==1)
806 while(trace_fraction == 1);
809 // Rough estimation of fuel consumption
810 // (ignores acceleration and current xyz velocity)
811 xydistance = vlen(pointa - pointb);
812 zdistance = fabs(pointa.z - this.origin.z);
814 t = zdistance / autocvar_g_jetpack_maxspeed_up;
815 t += xydistance / autocvar_g_jetpack_maxspeed_side;
816 fuel = t * autocvar_g_jetpack_fuel * 0.8;
818 LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
821 if(this.ammo_fuel>fuel)
824 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
825 // - between air and ground speeds)
827 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
828 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
831 // Compare against other goals
832 f = f * rangebias / (rangebias + cost);
834 if (navigation_bestrating < f)
836 LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
837 navigation_bestrating = f;
838 navigation_bestgoal = e;
839 this.navigation_jetpack_goal = e;
840 this.navigation_jetpack_point = pointb;
848 //te_wizspike(e.origin);
851 // update the cached spawnfunc_waypoint link on a dynamic item entity
852 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
858 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
859 e.nearestwaypoint = NULL;
861 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
862 && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
864 nwp = navigation_findnearestwaypoint(e, true);
866 e.nearestwaypoint = nwp;
869 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
871 if(!e.navigation_dynamicgoal)
872 e.blacklisted = true;
876 LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
881 if(e.navigation_dynamicgoal)
882 e.nearestwaypointtimeout = time + 2;
883 else if(autocvar_g_waypointeditor)
884 e.nearestwaypointtimeout = time + 3 + random() * 2;
886 nwp = e.nearestwaypoint;
889 LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
891 if (nwp.wpcost < 10000000)
893 //te_wizspike(nwp.wpnearestpoint);
894 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
895 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
896 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
897 if (navigation_bestrating < f)
899 LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
900 navigation_bestrating = f;
901 navigation_bestgoal = e;
906 // adds an item to the the goal stack with the path to a given item
907 bool navigation_routetogoal(entity this, entity e, vector startposition)
909 // if there is no goal, just exit
915 // put the entity on the goal stack
916 //print("routetogoal ", etos(e), "\n");
917 navigation_pushroute(this, e);
919 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
921 this.wp_goal_prev1 = this.wp_goal_prev0;
922 this.wp_goal_prev0 = e;
926 if(e==this.navigation_jetpack_goal)
929 // if it can reach the goal there is nothing more to do
930 if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
933 entity nearest_wp = NULL;
934 // see if there are waypoints describing a path to the item
935 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
937 e = e.nearestwaypoint;
941 e = e.enemy; // we already have added it, so...
946 if(nearest_wp && nearest_wp.enemy)
948 // often path can be optimized by not adding the nearest waypoint
949 if(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))
950 e = nearest_wp.enemy;
955 // add the spawnfunc_waypoint to the path
956 navigation_pushroute(this, e);
966 // removes any currently touching waypoints from the goal stack
967 // (this is how bots detect if they reached a goal)
968 void navigation_poptouchedgoals(entity this)
972 m1 = org + this.mins;
973 m2 = org + this.maxs;
975 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
977 // make sure jumppad is really hit, don't rely on distance based checks
978 // as they may report a touch even if it didn't really happen
979 if(this.lastteleporttime>0)
980 if(time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
982 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
983 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
985 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
986 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
988 navigation_poproute(this);
993 // If for some reason the bot is closer to the next goal, pop the current one
994 if(this.goalstack01 && !wasfreed(this.goalstack01))
995 if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
996 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
997 if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
999 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1000 navigation_poproute(this);
1001 // TODO this may also be a nice idea to do "early" (e.g. by
1002 // manipulating the vlen() comparisons) to shorten paths in
1003 // general - this would make bots walk more "on rails" than
1004 // "zigzagging" which they currently do with sufficiently
1005 // random-like waypoints, and thus can make a nice bot
1006 // personality property
1009 // Loose goal touching check when running
1010 if(this.aistatus & AI_STATUS_RUNNING)
1011 if(this.goalcurrent.classname=="waypoint")
1012 if(!(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1013 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
1015 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1017 traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1018 if(trace_fraction==1)
1020 // Detect personal waypoints
1021 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1022 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1024 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1025 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1028 navigation_poproute(this);
1033 while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1035 vector gc_min = this.goalcurrent.absmin;
1036 vector gc_max = this.goalcurrent.absmax;
1037 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1039 gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1040 gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1042 if(!boxesoverlap(m1, m2, gc_min, gc_max))
1045 if((this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1048 // Detect personal waypoints
1049 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1050 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1052 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1053 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1056 navigation_poproute(this);
1060 // begin a goal selection session (queries spawnfunc_waypoint network)
1061 void navigation_goalrating_start(entity this)
1063 if(this.aistatus & AI_STATUS_STUCK)
1066 this.navigation_jetpack_goal = NULL;
1067 navigation_bestrating = -1;
1068 navigation_clearroute(this);
1069 navigation_bestgoal = NULL;
1070 navigation_markroutes(this, NULL);
1073 // ends a goal selection session (updates goal stack to the best goal)
1074 void navigation_goalrating_end(entity this)
1076 if(this.aistatus & AI_STATUS_STUCK)
1079 navigation_routetogoal(this, navigation_bestgoal, this.origin);
1080 LOG_DEBUG("best goal ", this.goalcurrent.classname);
1082 // If the bot got stuck then try to reach the farthest waypoint
1083 if (!this.goalentity && autocvar_bot_wander_enable)
1085 if (!(this.aistatus & AI_STATUS_STUCK))
1087 LOG_DEBUG(this.netname, " cannot walk to any goal");
1088 this.aistatus |= AI_STATUS_STUCK;
1093 void botframe_updatedangerousobjects(float maxupdate)
1095 vector m1, m2, v, o;
1098 IL_EACH(g_waypoints, true,
1103 IL_EACH(g_bot_dodge, it.bot_dodge,
1106 v.x = bound(m1_x, v.x, m2_x);
1107 v.y = bound(m1_y, v.y, m2_y);
1108 v.z = bound(m1_z, v.z, m2_z);
1109 o = (it.absmin + it.absmax) * 0.5;
1110 d = it.bot_dodgerating - vlen(o - v);
1113 traceline(o, v, true, NULL);
1114 if (trace_fraction == 1)
1115 danger = danger + d;
1125 void navigation_unstuck(entity this)
1127 float search_radius = 1000;
1129 if (!autocvar_bot_wander_enable)
1132 if (!bot_waypoint_queue_owner)
1134 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1135 bot_waypoint_queue_owner = this;
1136 bot_waypoint_queue_bestgoal = NULL;
1137 bot_waypoint_queue_bestgoalrating = 0;
1140 if(bot_waypoint_queue_owner!=this)
1143 if (bot_waypoint_queue_goal)
1145 // evaluate the next goal on the queue
1146 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1147 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1148 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1150 if( d > bot_waypoint_queue_bestgoalrating)
1152 bot_waypoint_queue_bestgoalrating = d;
1153 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1156 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1158 if (!bot_waypoint_queue_goal)
1160 if (bot_waypoint_queue_bestgoal)
1162 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1163 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1164 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1165 this.aistatus &= ~AI_STATUS_STUCK;
1169 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1172 bot_waypoint_queue_owner = NULL;
1177 if(bot_strategytoken!=this)
1180 // build a new queue
1181 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1183 entity first = NULL;
1185 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1187 if(bot_waypoint_queue_goal)
1188 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1192 bot_waypoint_queue_goal = it;
1193 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1197 bot_waypoint_queue_goal = first;
1200 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1201 bot_waypoint_queue_owner = NULL;
1206 // Support for debugging tracewalk visually
1208 void debugresetnodes()
1210 debuglastnode = '0 0 0';
1213 void debugnode(entity this, vector node)
1215 if (!IS_PLAYER(this))
1218 if(debuglastnode=='0 0 0')
1220 debuglastnode = node;
1224 te_lightning2(NULL, node, debuglastnode);
1225 debuglastnode = node;
1228 void debugnodestatus(vector position, float status)
1234 case DEBUG_NODE_SUCCESS:
1237 case DEBUG_NODE_WARNING:
1240 case DEBUG_NODE_FAIL:
1247 te_customflash(position, 40, 2, c);
1250 // Support for debugging the goal stack visually
1253 .vector lastposition;
1255 // Debug the goal stack visually
1256 void debuggoalstack(entity this)
1261 if(this.goalcounter==0)goal=this.goalcurrent;
1262 else if(this.goalcounter==1)goal=this.goalstack01;
1263 else if(this.goalcounter==2)goal=this.goalstack02;
1264 else if(this.goalcounter==3)goal=this.goalstack03;
1265 else if(this.goalcounter==4)goal=this.goalstack04;
1266 else if(this.goalcounter==5)goal=this.goalstack05;
1267 else if(this.goalcounter==6)goal=this.goalstack06;
1268 else if(this.goalcounter==7)goal=this.goalstack07;
1269 else if(this.goalcounter==8)goal=this.goalstack08;
1270 else if(this.goalcounter==9)goal=this.goalstack09;
1271 else if(this.goalcounter==10)goal=this.goalstack10;
1272 else if(this.goalcounter==11)goal=this.goalstack11;
1273 else if(this.goalcounter==12)goal=this.goalstack12;
1274 else if(this.goalcounter==13)goal=this.goalstack13;
1275 else if(this.goalcounter==14)goal=this.goalstack14;
1276 else if(this.goalcounter==15)goal=this.goalstack15;
1277 else if(this.goalcounter==16)goal=this.goalstack16;
1278 else if(this.goalcounter==17)goal=this.goalstack17;
1279 else if(this.goalcounter==18)goal=this.goalstack18;
1280 else if(this.goalcounter==19)goal=this.goalstack19;
1281 else if(this.goalcounter==20)goal=this.goalstack20;
1282 else if(this.goalcounter==21)goal=this.goalstack21;
1283 else if(this.goalcounter==22)goal=this.goalstack22;
1284 else if(this.goalcounter==23)goal=this.goalstack23;
1285 else if(this.goalcounter==24)goal=this.goalstack24;
1286 else if(this.goalcounter==25)goal=this.goalstack25;
1287 else if(this.goalcounter==26)goal=this.goalstack26;
1288 else if(this.goalcounter==27)goal=this.goalstack27;
1289 else if(this.goalcounter==28)goal=this.goalstack28;
1290 else if(this.goalcounter==29)goal=this.goalstack29;
1291 else if(this.goalcounter==30)goal=this.goalstack30;
1292 else if(this.goalcounter==31)goal=this.goalstack31;
1297 this.goalcounter = 0;
1298 this.lastposition='0 0 0';
1302 if(this.lastposition=='0 0 0')
1305 org = this.lastposition;
1308 go = ( goal.absmin + goal.absmax ) * 0.5;
1309 te_lightning2(NULL, org, go);
1310 this.lastposition = go;