2 // rough simulation of walking from one point to another to test if a path
3 // can be traveled, used for waypoint linking and havocbot
5 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
11 local float totaldist;
14 local float ignorehazards;
17 #ifdef DEBUG_TRACEWALK
25 dist = totaldist = vlen(move);
26 dir = normalize(move);
28 ignorehazards = FALSE;
30 // Analyze starting point
31 traceline(start, start, MOVE_NORMAL, e);
32 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
36 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
37 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
43 tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
47 #ifdef DEBUG_TRACEWALK
48 debugnodestatus(start, DEBUG_NODE_FAIL);
50 //print("tracewalk: ", vtos(start), " is a bad start\n");
59 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
62 #ifdef DEBUG_TRACEWALK
63 debugnodestatus(org, DEBUG_NODE_SUCCESS);
65 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
68 #ifdef DEBUG_TRACEWALK
76 dist = dist - stepdist;
77 traceline(org, org, MOVE_NORMAL, e);
80 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
82 // hazards blocking path
83 #ifdef DEBUG_TRACEWALK
84 debugnodestatus(org, DEBUG_NODE_FAIL);
86 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
90 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
92 move = normalize(end - org);
93 tracebox(org, m1, m2, org + move * stepdist, movemode, e);
95 #ifdef DEBUG_TRACEWALK
96 debugnode(trace_endpos);
99 if (trace_fraction < 1)
102 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
103 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
105 #ifdef DEBUG_TRACEWALK
108 if(pointcontents(org) == CONTENT_EMPTY)
112 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
114 #ifdef DEBUG_TRACEWALK
115 debugnodestatus(org, DEBUG_NODE_FAIL);
118 //print("tracewalk: ", vtos(start), " failed under water\n");
128 move = dir * stepdist + org;
129 tracebox(org, m1, m2, move, movemode, e);
131 #ifdef DEBUG_TRACEWALK
132 debugnode(trace_endpos);
136 if (trace_fraction < 1)
138 // check if we can walk over this obstacle
139 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
140 if (trace_fraction < 1 || trace_startsolid)
142 #ifdef DEBUG_TRACEWALK
143 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
147 traceline( org, move, movemode, e);
148 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
150 local vector nextmove;
152 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
154 nextmove = move + (dir * stepdist);
155 traceline( move, nextmove, movemode, e);
161 #ifdef DEBUG_TRACEWALK
162 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
164 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
165 //te_explosion(trace_endpos);
166 //print(ftos(e.dphitcontentsmask), "\n");
167 return FALSE; // failed
176 // trace down from stepheight as far as possible and move there,
177 // if this starts in solid we try again without the stepup, and
178 // if that also fails we assume it is a wall
179 // (this is the same logic as the Quake walkmove function used)
180 tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
182 // moved successfully
186 c = pointcontents(org + '0 0 1');
187 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
197 //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
199 // moved but didn't arrive at the intended destination
200 #ifdef DEBUG_TRACEWALK
201 debugnodestatus(org, DEBUG_NODE_FAIL);
207 /////////////////////////////////////////////////////////////////////////////
209 /////////////////////////////////////////////////////////////////////////////
211 // completely empty the goal stack, used when deciding where to go
212 void navigation_clearroute()
214 //print("bot ", etos(self), " clear\n");
215 self.navigation_hasgoals = FALSE;
216 self.goalcurrent = world;
217 self.goalstack01 = world;
218 self.goalstack02 = world;
219 self.goalstack03 = world;
220 self.goalstack04 = world;
221 self.goalstack05 = world;
222 self.goalstack06 = world;
223 self.goalstack07 = world;
224 self.goalstack08 = world;
225 self.goalstack09 = world;
226 self.goalstack10 = world;
227 self.goalstack11 = world;
228 self.goalstack12 = world;
229 self.goalstack13 = world;
230 self.goalstack14 = world;
231 self.goalstack15 = world;
232 self.goalstack16 = world;
233 self.goalstack17 = world;
234 self.goalstack18 = world;
235 self.goalstack19 = world;
236 self.goalstack20 = world;
237 self.goalstack21 = world;
238 self.goalstack22 = world;
239 self.goalstack23 = world;
240 self.goalstack24 = world;
241 self.goalstack25 = world;
242 self.goalstack26 = world;
243 self.goalstack27 = world;
244 self.goalstack28 = world;
245 self.goalstack29 = world;
246 self.goalstack30 = world;
247 self.goalstack31 = world;
250 // add a new goal at the beginning of the stack
251 // (in other words: add a new prerequisite before going to the later goals)
252 // NOTE: when a waypoint is added, the WP gets pushed first, then the
253 // next-closest WP on the shortest path to the WP
254 // That means, if the stack overflows, the bot will know how to do the FIRST 32
255 // steps to the goal, and then recalculate the path.
256 void navigation_pushroute(entity e)
258 //print("bot ", etos(self), " push ", etos(e), "\n");
259 self.goalstack31 = self.goalstack30;
260 self.goalstack30 = self.goalstack29;
261 self.goalstack29 = self.goalstack28;
262 self.goalstack28 = self.goalstack27;
263 self.goalstack27 = self.goalstack26;
264 self.goalstack26 = self.goalstack25;
265 self.goalstack25 = self.goalstack24;
266 self.goalstack24 = self.goalstack23;
267 self.goalstack23 = self.goalstack22;
268 self.goalstack22 = self.goalstack21;
269 self.goalstack21 = self.goalstack20;
270 self.goalstack20 = self.goalstack19;
271 self.goalstack19 = self.goalstack18;
272 self.goalstack18 = self.goalstack17;
273 self.goalstack17 = self.goalstack16;
274 self.goalstack16 = self.goalstack15;
275 self.goalstack15 = self.goalstack14;
276 self.goalstack14 = self.goalstack13;
277 self.goalstack13 = self.goalstack12;
278 self.goalstack12 = self.goalstack11;
279 self.goalstack11 = self.goalstack10;
280 self.goalstack10 = self.goalstack09;
281 self.goalstack09 = self.goalstack08;
282 self.goalstack08 = self.goalstack07;
283 self.goalstack07 = self.goalstack06;
284 self.goalstack06 = self.goalstack05;
285 self.goalstack05 = self.goalstack04;
286 self.goalstack04 = self.goalstack03;
287 self.goalstack03 = self.goalstack02;
288 self.goalstack02 = self.goalstack01;
289 self.goalstack01 = self.goalcurrent;
290 self.goalcurrent = e;
293 // remove first goal from stack
294 // (in other words: remove a prerequisite for reaching the later goals)
295 // (used when a spawnfunc_waypoint is reached)
296 void navigation_poproute()
298 //print("bot ", etos(self), " pop\n");
299 self.goalcurrent = self.goalstack01;
300 self.goalstack01 = self.goalstack02;
301 self.goalstack02 = self.goalstack03;
302 self.goalstack03 = self.goalstack04;
303 self.goalstack04 = self.goalstack05;
304 self.goalstack05 = self.goalstack06;
305 self.goalstack06 = self.goalstack07;
306 self.goalstack07 = self.goalstack08;
307 self.goalstack08 = self.goalstack09;
308 self.goalstack09 = self.goalstack10;
309 self.goalstack10 = self.goalstack11;
310 self.goalstack11 = self.goalstack12;
311 self.goalstack12 = self.goalstack13;
312 self.goalstack13 = self.goalstack14;
313 self.goalstack14 = self.goalstack15;
314 self.goalstack15 = self.goalstack16;
315 self.goalstack16 = self.goalstack17;
316 self.goalstack17 = self.goalstack18;
317 self.goalstack18 = self.goalstack19;
318 self.goalstack19 = self.goalstack20;
319 self.goalstack20 = self.goalstack21;
320 self.goalstack21 = self.goalstack22;
321 self.goalstack22 = self.goalstack23;
322 self.goalstack23 = self.goalstack24;
323 self.goalstack24 = self.goalstack25;
324 self.goalstack25 = self.goalstack26;
325 self.goalstack26 = self.goalstack27;
326 self.goalstack27 = self.goalstack28;
327 self.goalstack28 = self.goalstack29;
328 self.goalstack29 = self.goalstack30;
329 self.goalstack30 = self.goalstack31;
330 self.goalstack31 = world;
333 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
334 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
336 local entity waylist, w, best;
337 local float dist, bestdist;
338 local vector v, org, pm1, pm2;
339 pm1 = ent.origin + ent.mins;
340 pm2 = ent.origin + ent.maxs;
341 waylist = findchain(classname, "waypoint");
343 // do two scans, because box test is cheaper
347 // if object is touching spawnfunc_waypoint
349 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
354 org = ent.origin + 0.5 * (ent.mins + ent.maxs);
355 org_z = ent.origin_z + ent.mins_z - PL_MIN_z; // player height
356 // TODO possibly make other code have the same support for bboxes
358 org = org + ent.tag_entity.origin;
359 if (navigation_testtracewalk)
365 // box check failed, try walk
369 // if object can walk from spawnfunc_waypoint
374 local vector wm1, wm2;
375 wm1 = w.origin + w.mins;
376 wm2 = w.origin + w.maxs;
377 v_x = bound(wm1_x, org_x, wm2_x);
378 v_y = bound(wm1_y, org_y, wm2_y);
379 v_z = bound(wm1_z, org_z, wm2_z);
383 dist = vlen(v - org);
386 traceline(v, org, TRUE, ent);
387 if (trace_fraction == 1)
391 //print("^1can I reach ", vtos(org), " from ", vtos(v), "?\n");
392 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
400 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
414 // finds the waypoints near the bot initiating a navigation query
415 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
418 local vector v, m1, m2, diff;
420 // navigation_testtracewalk = TRUE;
425 if (!head.wpconsidered)
429 m1 = head.origin + head.mins;
430 m2 = head.origin + head.maxs;
432 v_x = bound(m1_x, v_x, m2_x);
433 v_y = bound(m1_y, v_y, m2_y);
434 v_z = bound(m1_z, v_z, m2_z);
438 diff = v - self.origin;
439 diff_z = max(0, diff_z);
440 if (vlen(diff) < maxdist)
442 head.wpconsidered = TRUE;
443 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
445 head.wpnearestpoint = v;
446 head.wpcost = vlen(v - self.origin) + head.dmg;
455 //navigation_testtracewalk = FALSE;
459 // updates a path link if a spawnfunc_waypoint link is better than the current one
460 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
469 v_x = bound(m1_x, p_x, m2_x);
470 v_y = bound(m1_y, p_y, m2_y);
471 v_z = bound(m1_z, p_z, m2_z);
475 cost2 = cost2 + vlen(v - p);
476 if (wp.wpcost > cost2)
481 wp.wpnearestpoint = v;
485 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
486 void navigation_markroutes(entity fixed_source_waypoint)
488 local entity w, wp, waylist;
489 local float searching, cost, cost2;
491 w = waylist = findchain(classname, "waypoint");
494 w.wpconsidered = FALSE;
495 w.wpnearestpoint = '0 0 0';
502 if(fixed_source_waypoint)
504 fixed_source_waypoint.wpconsidered = TRUE;
505 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
506 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
507 fixed_source_waypoint.wpfire = 1;
508 fixed_source_waypoint.enemy = world;
512 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
513 // as this search is expensive we will use lower values if the bot is on the air
514 local float i, increment, maxdistance;
515 if(self.flags & FL_ONGROUND)
526 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
541 p = w.wpnearestpoint;
542 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
543 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
544 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
545 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
546 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
547 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
548 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
549 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
550 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
551 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
552 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
553 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
554 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
555 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
556 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
557 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
558 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
559 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
560 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
561 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
562 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
563 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
564 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
565 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
566 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
567 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
581 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
582 void navigation_markroutes_inverted(entity fixed_source_waypoint)
584 local entity w, wp, waylist;
585 local float searching, cost, cost2;
587 w = waylist = findchain(classname, "waypoint");
590 w.wpconsidered = FALSE;
591 w.wpnearestpoint = '0 0 0';
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 = world;
608 error("need to start with a waypoint\n");
622 cost = w.wpcost; // cost to walk from w to home
623 p = w.wpnearestpoint;
624 for(wp = waylist; wp; wp = wp.chain)
626 if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
627 if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
628 if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
629 if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
630 if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
631 if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
632 if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
633 if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
635 cost2 = cost + wp.dmg;
636 navigation_markroutes_checkwaypoint(w, wp, cost2, p);
644 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
645 void navigation_routerating(entity e, float f, float rangebias)
651 //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
653 // Evaluate path using jetpack
655 if(self.items & IT_JETPACK)
656 if(cvar("bot_ai_navigation_jetpack"))
657 if(vlen(self.origin - e.origin) > cvar("bot_ai_navigation_jetpack_mindistance"))
659 vector pointa, pointb;
661 // dprint("jetpack ai: evaluating path for ", e.classname,"\n");
664 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
665 pointa = trace_endpos - '0 0 1';
668 traceline(e.origin, e.origin + '0 0 65535', MOVE_NORMAL, e);
669 pointb = trace_endpos - '0 0 1';
671 // Can I see these two points from the sky?
672 traceline(pointa, pointb, MOVE_NORMAL, self);
674 if(trace_fraction==1)
676 // dprint("jetpack ai: can bridge these two points\n");
678 // Lower the altitude of these points as much as possible
679 local float zdistance, xydistance, cost, t, fuel;
680 local vector down, npa, npb;
682 down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
688 if(npa_z<=self.absmax_z)
691 if(npb_z<=e.absmax_z)
694 traceline(npa, npb, MOVE_NORMAL, self);
695 if(trace_fraction==1)
701 while(trace_fraction == 1);
704 // Rough estimation of fuel consumption
705 // (ignores acceleration and current xyz velocity)
706 xydistance = vlen(pointa - pointb);
707 zdistance = fabs(pointa_z - self.origin_z);
709 t = zdistance / cvar("g_jetpack_maxspeed_up");
710 t += xydistance / cvar("g_jetpack_maxspeed_side");
711 fuel = t * cvar("g_jetpack_fuel") * 0.8;
713 // dprint("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel),"\n");
716 if(self.ammo_fuel>fuel)
719 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
720 // - between air and ground speeds)
722 cost = xydistance / (cvar("g_jetpack_maxspeed_side")/cvar("sv_maxspeed"));
723 cost += zdistance / (cvar("g_jetpack_maxspeed_up")/cvar("sv_maxspeed"));
726 // Compare against other goals
727 f = f * rangebias / (rangebias + cost);
729 if (navigation_bestrating < f)
731 // dprint("jetpack path: added goal", e.classname, " (with rating ", ftos(f), ")\n");
732 navigation_bestrating = f;
733 navigation_bestgoal = e;
734 self.navigation_jetpack_goal = e;
735 self.navigation_jetpack_point = pointb;
742 //te_wizspike(e.origin);
745 // update the cached spawnfunc_waypoint link on a dynamic item entity
746 if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
752 if (time > e.nearestwaypointtimeout)
754 nwp = navigation_findnearestwaypoint(e, TRUE);
756 e.nearestwaypoint = nwp;
758 dprint("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n");
760 // TODO: Cleaner solution, probably handling this timeout from ctf.qc
761 if(e.classname=="item_flag_team")
762 e.nearestwaypointtimeout = time + 2;
764 e.nearestwaypointtimeout = time + random() * 3 + 5;
766 nwp = e.nearestwaypoint;
769 //dprint("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n");
771 if (nwp.wpcost < 10000000)
773 //te_wizspike(nwp.wpnearestpoint);
774 // dprint(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
775 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint)));
776 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
778 if (navigation_bestrating < f)
780 // dprint("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n");
781 navigation_bestrating = f;
782 navigation_bestgoal = e;
788 // adds an item to the the goal stack with the path to a given item
789 float navigation_routetogoal(entity e, vector startposition)
793 // if there is no goal, just exit
797 self.navigation_hasgoals = TRUE;
799 // put the entity on the goal stack
800 //print("routetogoal ", etos(e), "\n");
801 navigation_pushroute(e);
804 if(e==self.navigation_jetpack_goal)
807 // if it can reach the goal there is nothing more to do
808 if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, bot_navigation_movemode))
811 // see if there are waypoints describing a path to the item
812 if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
813 e = e.nearestwaypoint;
815 e = e.enemy; // we already have added it, so...
822 // add the spawnfunc_waypoint to the path
823 navigation_pushroute(e);
833 // removes any currently touching waypoints from the goal stack
834 // (this is how bots detect if they reached a goal)
835 void navigation_poptouchedgoals()
837 local vector org, m1, m2;
839 m1 = org + self.mins;
840 m2 = org + self.maxs;
842 if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
844 if(self.lastteleporttime>0)
845 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
847 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
848 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
850 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
851 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
853 navigation_poproute();
858 // Loose goal touching check when running
859 if(self.aistatus & AI_STATUS_RUNNING)
860 if(self.goalcurrent.classname=="waypoint")
862 if(vlen(self.origin - self.goalcurrent.origin)<150)
864 traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
865 if(trace_fraction==1)
867 // Detect personal waypoints
868 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
869 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
871 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
872 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
875 navigation_poproute();
880 while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
882 // Detect personal waypoints
883 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
884 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
886 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
887 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
890 navigation_poproute();
894 // begin a goal selection session (queries spawnfunc_waypoint network)
895 void navigation_goalrating_start()
897 self.navigation_jetpack_goal = world;
898 navigation_bestrating = -1;
899 self.navigation_hasgoals = FALSE;
900 navigation_bestgoal = world;
901 navigation_markroutes(world);
904 // ends a goal selection session (updates goal stack to the best goal)
905 void navigation_goalrating_end()
907 navigation_routetogoal(navigation_bestgoal, self.origin);
908 // dprint("best goal ", self.goalcurrent.classname , "\n");
910 // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
911 if not (self.navigation_hasgoals)
913 dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
917 RandomSelection_Init();
918 head = findradius(self.origin,1000);
921 if(head.classname=="waypoint")
922 if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
923 if(vlen(self.origin-head.origin)>100)
924 if(checkpvs(self.view_ofs,head))
925 RandomSelection_Add(head, 0, string_null, 1 + (vlen(self.origin-head.origin)<500), 0);
928 if(RandomSelection_chosen_ent)
929 navigation_routetogoal(RandomSelection_chosen_ent, self.origin);
931 self.navigation_hasgoals = FALSE; // Reset this value
935 void botframe_updatedangerousobjects(float maxupdate)
937 local entity head, bot_dodgelist;
938 local vector m1, m2, v;
939 local float c, d, danger;
941 bot_dodgelist = findchainfloat(bot_dodge, TRUE);
942 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
943 while (botframe_dangerwaypoint != world)
946 m1 = botframe_dangerwaypoint.mins;
947 m2 = botframe_dangerwaypoint.maxs;
948 head = bot_dodgelist;
952 v_x = bound(m1_x, v_x, m2_x);
953 v_y = bound(m1_y, v_y, m2_y);
954 v_z = bound(m1_z, v_z, m2_z);
955 d = head.bot_dodgerating - vlen(head.origin - v);
958 traceline(head.origin, v, TRUE, world);
959 if (trace_fraction == 1)
964 botframe_dangerwaypoint.dmg = danger;
968 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
972 #ifdef DEBUG_TRACEWALK
974 void debugresetnodes()
976 debuglastnode = '0 0 0';
979 void debugnode(vector node)
981 if not(self.classname=="player")
984 if(debuglastnode=='0 0 0')
986 debuglastnode = node;
990 te_lightning2(world, node, debuglastnode);
991 debuglastnode = node;
994 void debugnodestatus(vector position, float status)
1000 case DEBUG_NODE_SUCCESS:
1003 case DEBUG_NODE_WARNING:
1006 case DEBUG_NODE_FAIL:
1013 te_customflash(position, 40, 2, color);
1018 #ifdef DEBUG_BOT_GOALSTACK
1021 .vector lastposition;
1023 // Debug the goal stack visually
1024 void debuggoalstack()
1026 local entity target;
1029 if(self.goalcounter==0)target=self.goalcurrent;
1030 else if(self.goalcounter==1)target=self.goalstack01;
1031 else if(self.goalcounter==2)target=self.goalstack02;
1032 else if(self.goalcounter==3)target=self.goalstack03;
1033 else if(self.goalcounter==4)target=self.goalstack04;
1034 else if(self.goalcounter==5)target=self.goalstack05;
1035 else if(self.goalcounter==6)target=self.goalstack06;
1036 else if(self.goalcounter==7)target=self.goalstack07;
1037 else if(self.goalcounter==8)target=self.goalstack08;
1038 else if(self.goalcounter==9)target=self.goalstack09;
1039 else if(self.goalcounter==10)target=self.goalstack10;
1040 else if(self.goalcounter==11)target=self.goalstack11;
1041 else if(self.goalcounter==12)target=self.goalstack12;
1042 else if(self.goalcounter==13)target=self.goalstack13;
1043 else if(self.goalcounter==14)target=self.goalstack14;
1044 else if(self.goalcounter==15)target=self.goalstack15;
1045 else if(self.goalcounter==16)target=self.goalstack16;
1046 else if(self.goalcounter==17)target=self.goalstack17;
1047 else if(self.goalcounter==18)target=self.goalstack18;
1048 else if(self.goalcounter==19)target=self.goalstack19;
1049 else if(self.goalcounter==20)target=self.goalstack20;
1050 else if(self.goalcounter==21)target=self.goalstack21;
1051 else if(self.goalcounter==22)target=self.goalstack22;
1052 else if(self.goalcounter==23)target=self.goalstack23;
1053 else if(self.goalcounter==24)target=self.goalstack24;
1054 else if(self.goalcounter==25)target=self.goalstack25;
1055 else if(self.goalcounter==26)target=self.goalstack26;
1056 else if(self.goalcounter==27)target=self.goalstack27;
1057 else if(self.goalcounter==28)target=self.goalstack28;
1058 else if(self.goalcounter==29)target=self.goalstack29;
1059 else if(self.goalcounter==30)target=self.goalstack30;
1060 else if(self.goalcounter==31)target=self.goalstack31;
1064 self.goalcounter = 0;
1065 self.lastposition='0 0 0';
1069 if(self.lastposition=='0 0 0')
1072 org = self.lastposition;
1075 te_lightning2(world, org, target.origin);
1076 self.lastposition = target.origin;