]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/navigation.qc
Assume bot can walk from wp A to item X if wp A is linked to wp B and wp B origin...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / navigation.qc
1 #include "navigation.qh"
2
3 #include "cvars.qh"
4
5 #include "bot.qh"
6 #include "waypoints.qh"
7
8 #include <common/t_items.qh>
9
10 #include <common/items/_mod.qh>
11
12 #include <common/constants.qh>
13 #include <common/net_linked.qh>
14 #include <common/triggers/trigger/jumppads.qh>
15
16 .float speed;
17
18 void navigation_dynamicgoal_init(entity this, bool initially_static)
19 {
20         this.navigation_dynamicgoal = true;
21         this.bot_basewaypoint = this.nearestwaypoint;
22         if(initially_static)
23                 this.nearestwaypointtimeout = -1;
24         else
25                 this.nearestwaypointtimeout = time;
26 }
27
28 void navigation_dynamicgoal_set(entity this)
29 {
30         this.nearestwaypointtimeout = time;
31 }
32
33 void navigation_dynamicgoal_unset(entity this)
34 {
35         if(this.bot_basewaypoint)
36                 this.nearestwaypoint = this.bot_basewaypoint;
37         this.nearestwaypointtimeout = -1;
38 }
39
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
42
43 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
44 {
45         vector org;
46         vector move;
47         vector dir;
48         float dist;
49         float totaldist;
50         float stepdist;
51         float ignorehazards;
52         float swimming;
53
54         if(autocvar_bot_debug_tracewalk)
55         {
56                 debugresetnodes();
57                 debugnode(e, start);
58         }
59
60         move = end - start;
61         move.z = 0;
62         org = start;
63         dist = totaldist = vlen(move);
64         dir = normalize(move);
65         stepdist = 32;
66         ignorehazards = false;
67         swimming = false;
68
69         // Analyze starting point
70         traceline(start, start, MOVE_NORMAL, e);
71         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
72                 ignorehazards = true;
73         else
74         {
75                 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
76                 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
77                 {
78                         ignorehazards = true;
79                         swimming = true;
80                 }
81         }
82         tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
83         if (trace_startsolid)
84         {
85                 // Bad start
86                 if(autocvar_bot_debug_tracewalk)
87                         debugnodestatus(start, DEBUG_NODE_FAIL);
88
89                 //print("tracewalk: ", vtos(start), " is a bad start\n");
90                 return false;
91         }
92
93         // Movement loop
94         move = end - org;
95         for (;;)
96         {
97                 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
98                 {
99                         // Succeeded
100                         if(autocvar_bot_debug_tracewalk)
101                                 debugnodestatus(org, DEBUG_NODE_SUCCESS);
102
103                         //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
104                         return true;
105                 }
106                 if(autocvar_bot_debug_tracewalk)
107                         debugnode(e, org);
108
109                 if (dist <= 0)
110                         break;
111                 if (stepdist > dist)
112                         stepdist = dist;
113                 dist = dist - stepdist;
114                 traceline(org, org, MOVE_NORMAL, e);
115                 if (!ignorehazards)
116                 {
117                         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
118                         {
119                                 // hazards blocking path
120                                 if(autocvar_bot_debug_tracewalk)
121                                         debugnodestatus(org, DEBUG_NODE_FAIL);
122
123                                 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
124                                 return false;
125                         }
126                 }
127                 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
128                 {
129                         move = normalize(end - org);
130                         tracebox(org, m1, m2, org + move * stepdist, movemode, e);
131
132                         if(autocvar_bot_debug_tracewalk)
133                                 debugnode(e, trace_endpos);
134
135                         if (trace_fraction < 1)
136                         {
137                                 swimming = true;
138                                 org = trace_endpos + normalize(org - trace_endpos) * stepdist;
139                                 for (; org.z < end.z + e.maxs.z; org.z += stepdist)
140                                 {
141                                         if(autocvar_bot_debug_tracewalk)
142                                                 debugnode(e, org);
143
144                                         if(pointcontents(org) == CONTENT_EMPTY)
145                                                 break;
146                                 }
147
148                                 if(pointcontents(org + '0 0 1') != CONTENT_EMPTY)
149                                 {
150                                         if(autocvar_bot_debug_tracewalk)
151                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
152
153                                         return false;
154                                         //print("tracewalk: ", vtos(start), " failed under water\n");
155                                 }
156                                 continue;
157
158                         }
159                         else
160                                 org = trace_endpos;
161                 }
162                 else
163                 {
164                         move = dir * stepdist + org;
165                         tracebox(org, m1, m2, move, movemode, e);
166
167                         if(autocvar_bot_debug_tracewalk)
168                                 debugnode(e, trace_endpos);
169
170                         // hit something
171                         if (trace_fraction < 1)
172                         {
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)
176                                 {
177                                         tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
178                                         if (trace_fraction < 1 || trace_startsolid)
179                                         {
180                                                 bool ladder_found = false;
181                                                 FOREACH_ENTITY_CLASS("func_ladder", boxesoverlap(trace_endpos + m1 + '-1 -1 -1', trace_endpos + m2 + '1 1 1', it.absmin, it.absmax),
182                                                 {
183                                                         if(boxesoverlap(end, end, it.absmin + (m1 - eZ * m1.z - '1 1 0'), it.absmax + (m2 - eZ * m2.z + '1 1 0')))
184                                                                 ladder_found = true; // can't return here ("Loop mutex held by tracewalk" error)
185                                                 });
186                                                 if(ladder_found)
187                                                 {
188                                                         if(autocvar_bot_debug_tracewalk)
189                                                                 debugnodestatus(org, DEBUG_NODE_SUCCESS);
190
191                                                         //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
192                                                         return true;
193                                                 }
194
195                                                 if(autocvar_bot_debug_tracewalk)
196                                                         debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
197
198                                                 traceline( org, move, movemode, e);
199
200                                                 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
201                                                 {
202                                                         vector nextmove;
203                                                         move = trace_endpos;
204                                                         while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
205                                                         {
206                                                                 nextmove = move + (dir * stepdist);
207                                                                 traceline( move, nextmove, movemode, e);
208                                                                 move = nextmove;
209                                                         }
210                                                 }
211                                                 else
212                                                 {
213                                                         if(autocvar_bot_debug_tracewalk)
214                                                                 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
215
216                                                         //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
217                                                         //te_explosion(trace_endpos);
218                                                         //print(ftos(e.dphitcontentsmask), "\n");
219                                                         return false; // failed
220                                                 }
221                                         }
222                                         else
223                                                 move = trace_endpos;
224                                 }
225                                 else
226                                         move = trace_endpos;
227                         }
228                         else
229                                 move = trace_endpos;
230
231                         // trace down from stepheight as far as possible and move there,
232                         // if this starts in solid we try again without the stepup, and
233                         // if that also fails we assume it is a wall
234                         // (this is the same logic as the Quake walkmove function used)
235                         tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
236
237                         // moved successfully
238                         if(swimming)
239                         {
240                                 float c;
241                                 c = pointcontents(org + '0 0 1');
242                                 if (!(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME))
243                                         swimming = false;
244                                 else
245                                         continue;
246                         }
247
248                         org = trace_endpos;
249                 }
250         }
251
252         //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
253
254         // moved but didn't arrive at the intended destination
255         if(autocvar_bot_debug_tracewalk)
256                 debugnodestatus(org, DEBUG_NODE_FAIL);
257
258         return false;
259 }
260
261 /////////////////////////////////////////////////////////////////////////////
262 // goal stack
263 /////////////////////////////////////////////////////////////////////////////
264
265 // completely empty the goal stack, used when deciding where to go
266 void navigation_clearroute(entity this)
267 {
268         //print("bot ", etos(this), " clear\n");
269         this.goalentity = NULL;
270         this.goalcurrent = NULL;
271         this.goalstack01 = NULL;
272         this.goalstack02 = NULL;
273         this.goalstack03 = NULL;
274         this.goalstack04 = NULL;
275         this.goalstack05 = NULL;
276         this.goalstack06 = NULL;
277         this.goalstack07 = NULL;
278         this.goalstack08 = NULL;
279         this.goalstack09 = NULL;
280         this.goalstack10 = NULL;
281         this.goalstack11 = NULL;
282         this.goalstack12 = NULL;
283         this.goalstack13 = NULL;
284         this.goalstack14 = NULL;
285         this.goalstack15 = NULL;
286         this.goalstack16 = NULL;
287         this.goalstack17 = NULL;
288         this.goalstack18 = NULL;
289         this.goalstack19 = NULL;
290         this.goalstack20 = NULL;
291         this.goalstack21 = NULL;
292         this.goalstack22 = NULL;
293         this.goalstack23 = NULL;
294         this.goalstack24 = NULL;
295         this.goalstack25 = NULL;
296         this.goalstack26 = NULL;
297         this.goalstack27 = NULL;
298         this.goalstack28 = NULL;
299         this.goalstack29 = NULL;
300         this.goalstack30 = NULL;
301         this.goalstack31 = NULL;
302 }
303
304 // add a new goal at the beginning of the stack
305 // (in other words: add a new prerequisite before going to the later goals)
306 // NOTE: when a waypoint is added, the WP gets pushed first, then the
307 // next-closest WP on the shortest path to the WP
308 // That means, if the stack overflows, the bot will know how to do the FIRST 32
309 // steps to the goal, and then recalculate the path.
310 void navigation_pushroute(entity this, entity e)
311 {
312         //print("bot ", etos(this), " push ", etos(e), "\n");
313         if(this.goalstack31 == this.goalentity)
314                 this.goalentity = NULL;
315         this.goalstack31 = this.goalstack30;
316         this.goalstack30 = this.goalstack29;
317         this.goalstack29 = this.goalstack28;
318         this.goalstack28 = this.goalstack27;
319         this.goalstack27 = this.goalstack26;
320         this.goalstack26 = this.goalstack25;
321         this.goalstack25 = this.goalstack24;
322         this.goalstack24 = this.goalstack23;
323         this.goalstack23 = this.goalstack22;
324         this.goalstack22 = this.goalstack21;
325         this.goalstack21 = this.goalstack20;
326         this.goalstack20 = this.goalstack19;
327         this.goalstack19 = this.goalstack18;
328         this.goalstack18 = this.goalstack17;
329         this.goalstack17 = this.goalstack16;
330         this.goalstack16 = this.goalstack15;
331         this.goalstack15 = this.goalstack14;
332         this.goalstack14 = this.goalstack13;
333         this.goalstack13 = this.goalstack12;
334         this.goalstack12 = this.goalstack11;
335         this.goalstack11 = this.goalstack10;
336         this.goalstack10 = this.goalstack09;
337         this.goalstack09 = this.goalstack08;
338         this.goalstack08 = this.goalstack07;
339         this.goalstack07 = this.goalstack06;
340         this.goalstack06 = this.goalstack05;
341         this.goalstack05 = this.goalstack04;
342         this.goalstack04 = this.goalstack03;
343         this.goalstack03 = this.goalstack02;
344         this.goalstack02 = this.goalstack01;
345         this.goalstack01 = this.goalcurrent;
346         this.goalcurrent = e;
347 }
348
349 // remove first goal from stack
350 // (in other words: remove a prerequisite for reaching the later goals)
351 // (used when a spawnfunc_waypoint is reached)
352 void navigation_poproute(entity this)
353 {
354         //print("bot ", etos(this), " pop\n");
355         if(this.goalcurrent == this.goalentity)
356                 this.goalentity = NULL;
357         this.goalcurrent = this.goalstack01;
358         this.goalstack01 = this.goalstack02;
359         this.goalstack02 = this.goalstack03;
360         this.goalstack03 = this.goalstack04;
361         this.goalstack04 = this.goalstack05;
362         this.goalstack05 = this.goalstack06;
363         this.goalstack06 = this.goalstack07;
364         this.goalstack07 = this.goalstack08;
365         this.goalstack08 = this.goalstack09;
366         this.goalstack09 = this.goalstack10;
367         this.goalstack10 = this.goalstack11;
368         this.goalstack11 = this.goalstack12;
369         this.goalstack12 = this.goalstack13;
370         this.goalstack13 = this.goalstack14;
371         this.goalstack14 = this.goalstack15;
372         this.goalstack15 = this.goalstack16;
373         this.goalstack16 = this.goalstack17;
374         this.goalstack17 = this.goalstack18;
375         this.goalstack18 = this.goalstack19;
376         this.goalstack19 = this.goalstack20;
377         this.goalstack20 = this.goalstack21;
378         this.goalstack21 = this.goalstack22;
379         this.goalstack22 = this.goalstack23;
380         this.goalstack23 = this.goalstack24;
381         this.goalstack24 = this.goalstack25;
382         this.goalstack25 = this.goalstack26;
383         this.goalstack26 = this.goalstack27;
384         this.goalstack27 = this.goalstack28;
385         this.goalstack28 = this.goalstack29;
386         this.goalstack29 = this.goalstack30;
387         this.goalstack30 = this.goalstack31;
388         this.goalstack31 = NULL;
389 }
390
391 float navigation_waypoint_will_link(vector v, vector org, entity ent, float walkfromwp, float bestdist)
392 {
393         float dist;
394         dist = vlen(v - org);
395         if (bestdist > dist)
396         {
397                 traceline(v, org, true, ent);
398                 if (trace_fraction == 1)
399                 {
400                         if (walkfromwp)
401                         {
402                                 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, org, bot_navigation_movemode))
403                                         return true;
404                         }
405                         else
406                         {
407                                 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, v, bot_navigation_movemode))
408                                         return true;
409                         }
410                 }
411         }
412         return false;
413 }
414
415 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
416 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
417 {
418         vector pm1 = ent.origin + ent.mins;
419         vector pm2 = ent.origin + ent.maxs;
420
421         // do two scans, because box test is cheaper
422         IL_EACH(g_waypoints, it != ent && it != except,
423         {
424                 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
425                         return it;
426         });
427
428         vector org = ent.origin + 0.5 * (ent.mins + ent.maxs);
429         org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
430         // TODO possibly make other code have the same support for bboxes
431         if(ent.tag_entity)
432                 org = org + ent.tag_entity.origin;
433         if (navigation_testtracewalk)
434                 te_plasmaburn(org);
435
436         entity best = NULL;
437         vector v;
438
439         // box check failed, try walk
440         IL_EACH(g_waypoints, it != ent,
441         {
442                 if(it.wpisbox)
443                 {
444                         vector wm1 = it.origin + it.mins;
445                         vector wm2 = it.origin + it.maxs;
446                         v.x = bound(wm1_x, org.x, wm2_x);
447                         v.y = bound(wm1_y, org.y, wm2_y);
448                         v.z = bound(wm1_z, org.z, wm2_z);
449                 }
450                 else
451                         v = it.origin;
452                 if(navigation_waypoint_will_link(v, org, ent, walkfromwp, bestdist))
453                 {
454                         bestdist = vlen(v - org);
455                         best = it;
456                 }
457         });
458         return best;
459 }
460 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
461 {
462         entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
463         if (autocvar_g_waypointeditor_auto)
464         {
465                 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
466                 if (wp && !wp2)
467                         wp.wpflags |= WAYPOINTFLAG_PROTECTED;
468         }
469         return wp;
470 }
471
472 // finds the waypoints near the bot initiating a navigation query
473 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
474 {
475         vector v, m1, m2;
476 //      navigation_testtracewalk = true;
477         int c = 0;
478         IL_EACH(g_waypoints, !it.wpconsidered,
479         {
480                 if (it.wpisbox)
481                 {
482                         m1 = it.origin + it.mins;
483                         m2 = it.origin + it.maxs;
484                         v = this.origin;
485                         v.x = bound(m1_x, v.x, m2_x);
486                         v.y = bound(m1_y, v.y, m2_y);
487                         v.z = bound(m1_z, v.z, m2_z);
488                 }
489                 else
490                         v = it.origin;
491                 vector diff = v - this.origin;
492                 diff.z = max(0, diff.z);
493                 if(vdist(diff, <, maxdist))
494                 {
495                         it.wpconsidered = true;
496                         if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
497                         {
498                                 it.wpnearestpoint = v;
499                                 it.wpcost = waypoint_getdistancecost(this.origin, v) + it.dmg;
500                                 it.wpfire = 1;
501                                 it.enemy = NULL;
502                                 c = c + 1;
503                         }
504                 }
505         });
506         //navigation_testtracewalk = false;
507         return c;
508 }
509
510 // updates a path link if a spawnfunc_waypoint link is better than the current one
511 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
512 {
513         vector m1;
514         vector m2;
515         vector v;
516         if (wp.wpisbox)
517         {
518                 m1 = wp.absmin;
519                 m2 = wp.absmax;
520                 v.x = bound(m1_x, p.x, m2_x);
521                 v.y = bound(m1_y, p.y, m2_y);
522                 v.z = bound(m1_z, p.z, m2_z);
523         }
524         else
525                 v = wp.origin;
526         cost2 += waypoint_getdistancecost(p, v);
527         if (wp.wpcost > cost2)
528         {
529                 wp.wpcost = cost2;
530                 wp.enemy = w;
531                 wp.wpfire = 1;
532                 wp.wpnearestpoint = v;
533         }
534 }
535
536 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
537 void navigation_markroutes(entity this, entity fixed_source_waypoint)
538 {
539         float cost, cost2;
540         vector p;
541
542         IL_EACH(g_waypoints, true,
543         {
544                 it.wpconsidered = false;
545                 it.wpnearestpoint = '0 0 0';
546                 it.wpcost = 10000000;
547                 it.wpfire = 0;
548                 it.enemy = NULL;
549         });
550
551         if(fixed_source_waypoint)
552         {
553                 fixed_source_waypoint.wpconsidered = true;
554                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
555                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
556                 fixed_source_waypoint.wpfire = 1;
557                 fixed_source_waypoint.enemy = NULL;
558         }
559         else
560         {
561                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
562                 // as this search is expensive we will use lower values if the bot is on the air
563                 float increment, maxdistance;
564                 if(IS_ONGROUND(this))
565                 {
566                         increment = 750;
567                         maxdistance = 50000;
568                 }
569                 else
570                 {
571                         increment = 500;
572                         maxdistance = 1500;
573                 }
574
575                 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
576         }
577
578         bool searching = true;
579         while (searching)
580         {
581                 searching = false;
582                 IL_EACH(g_waypoints, it.wpfire,
583                 {
584                         searching = true;
585                         it.wpfire = 0;
586                         cost = it.wpcost;
587                         p = it.wpnearestpoint;
588                         entity wp;
589                         wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
590                         wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
591                         wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
592                         wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
593                         wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
594                         wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
595                         wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
596                         wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
597                         wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
598                         wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
599                         wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
600                         wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
601                         wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
602                         wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
603                         wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
604                         wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
605                         wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
606                         wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
607                         wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
608                         wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
609                         wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
610                         wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
611                         wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
612                         wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
613                         wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
614                         wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
615                         wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
616                         wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
617                         wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
618                         wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
619                         wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
620                         wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
621                         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
622                 });
623         }
624 }
625
626 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
627 void navigation_markroutes_inverted(entity fixed_source_waypoint)
628 {
629         float cost, cost2;
630         vector p;
631         IL_EACH(g_waypoints, true,
632         {
633                 it.wpconsidered = false;
634                 it.wpnearestpoint = '0 0 0';
635                 it.wpcost = 10000000;
636                 it.wpfire = 0;
637                 it.enemy = NULL;
638         });
639
640         if(fixed_source_waypoint)
641         {
642                 fixed_source_waypoint.wpconsidered = true;
643                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
644                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
645                 fixed_source_waypoint.wpfire = 1;
646                 fixed_source_waypoint.enemy = NULL;
647         }
648         else
649         {
650                 error("need to start with a waypoint\n");
651         }
652
653         bool searching = true;
654         while (searching)
655         {
656                 searching = false;
657                 IL_EACH(g_waypoints, it.wpfire,
658                 {
659                         searching = true;
660                         it.wpfire = 0;
661                         cost = it.wpcost; // cost to walk from it to home
662                         p = it.wpnearestpoint;
663                         entity wp = it;
664                         IL_EACH(g_waypoints, it != wp,
665                         {
666                                 if(!waypoint_islinked(it, wp))
667                                         continue;
668                                 cost2 = cost + it.dmg;
669                                 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
670                         });
671                 });
672         }
673 }
674
675 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
676 void navigation_routerating(entity this, entity e, float f, float rangebias)
677 {
678         if (!e)
679                 return;
680
681         if(e.blacklisted)
682                 return;
683
684         rangebias = waypoint_getdistancecost_simple(rangebias);
685         f = waypoint_getdistancecost_simple(f);
686
687         if (IS_PLAYER(e))
688         {
689                 bool rate_wps = false;
690                 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
691                         rate_wps = true;
692
693                 if(!IS_ONGROUND(e))
694                 {
695                         traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
696                         int t = pointcontents(trace_endpos + '0 0 1');
697                         if(t != CONTENT_SOLID )
698                         {
699                                 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
700                                         rate_wps = true;
701                                 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
702                                         return;
703                         }
704                 }
705
706                 if(rate_wps)
707                 {
708                         entity theEnemy = e;
709                         entity best_wp = NULL;
710                         float best_dist = 10000;
711                         IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
712                                 && vdist(it.origin - this.origin, >, 100)
713                                 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
714                         {
715                                 float dist = vlen(it.origin - theEnemy.origin);
716                                 if (dist < best_dist)
717                                 {
718                                         best_wp = it;
719                                         best_dist = dist;
720                                 }
721                         });
722                         if (!best_wp)
723                                 return;
724                         e = best_wp;
725                 }
726         }
727
728         vector o = (e.absmin + e.absmax) * 0.5;
729
730         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
731
732         // Evaluate path using jetpack
733         if(g_jetpack)
734         if(this.items & IT_JETPACK)
735         if(autocvar_bot_ai_navigation_jetpack)
736         if(vdist(this.origin - o, >, autocvar_bot_ai_navigation_jetpack_mindistance))
737         {
738                 vector pointa, pointb;
739
740                 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
741
742                 // Point A
743                 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
744                 pointa = trace_endpos - '0 0 1';
745
746                 // Point B
747                 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
748                 pointb = trace_endpos - '0 0 1';
749
750                 // Can I see these two points from the sky?
751                 traceline(pointa, pointb, MOVE_NORMAL, this);
752
753                 if(trace_fraction==1)
754                 {
755                         LOG_DEBUG("jetpack ai: can bridge these two points");
756
757                         // Lower the altitude of these points as much as possible
758                         float zdistance, xydistance, cost, t, fuel;
759                         vector down, npa, npb;
760
761                         down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
762
763                         do{
764                                 npa = pointa + down;
765                                 npb = pointb + down;
766
767                                 if(npa.z<=this.absmax.z)
768                                         break;
769
770                                 if(npb.z<=e.absmax.z)
771                                         break;
772
773                                 traceline(npa, npb, MOVE_NORMAL, this);
774                                 if(trace_fraction==1)
775                                 {
776                                         pointa = npa;
777                                         pointb = npb;
778                                 }
779                         }
780                         while(trace_fraction == 1);
781
782
783                         // Rough estimation of fuel consumption
784                         // (ignores acceleration and current xyz velocity)
785                         xydistance = vlen(pointa - pointb);
786                         zdistance = fabs(pointa.z - this.origin.z);
787
788                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
789                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
790                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
791
792                         LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
793
794                         // enough fuel ?
795                         if(this.ammo_fuel>fuel)
796                         {
797                                 // Estimate cost
798                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
799                                 //  - between air and ground speeds)
800
801                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
802                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
803                                 cost *= 1.5;
804
805                                 // Compare against other goals
806                                 f = f * rangebias / (rangebias + cost);
807
808                                 if (navigation_bestrating < f)
809                                 {
810                                         LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
811                                         navigation_bestrating = f;
812                                         navigation_bestgoal = e;
813                                         this.navigation_jetpack_goal = e;
814                                         this.navigation_jetpack_point = pointb;
815                                 }
816                                 return;
817                         }
818                 }
819         }
820
821         entity nwp;
822         //te_wizspike(e.origin);
823         //bprint(etos(e));
824         //bprint("\n");
825         // update the cached spawnfunc_waypoint link on a dynamic item entity
826         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
827         {
828                 nwp = e;
829         }
830         else
831         {
832                 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
833                         e.nearestwaypoint = NULL;
834
835                 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
836                         && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
837                 {
838                         nwp = navigation_findnearestwaypoint(e, true);
839                         if(nwp)
840                         {
841                                 e.nearestwaypoint = nwp;
842
843                                 vector m1 = nwp.absmin, m2 = nwp.absmax;
844                                 m1.x = nwp.origin.x; m1.y = nwp.origin.y;
845                                 m2.x = nwp.origin.x; m2.y = nwp.origin.y;
846                                 vector ve = (e.absmin - e.absmax) * 0.5;
847                                 ve.x = bound(m1.x, ve.x, m2.x);
848                                 ve.y = bound(m1.y, ve.y, m2.y);
849                                 ve.z = bound(m1.z, ve.z, m2.z);
850
851                                 m1 = e.absmin; m2 = e.absmax;
852                                 m1.x = e.origin.x; m1.y = e.origin.y;
853                                 m2.x = e.origin.x; m2.y = e.origin.y;
854                                 vector vnwp = nwp.origin;
855                                 vnwp.x = bound(m1.x, vnwp.x, m2.x);
856                                 vnwp.y = bound(m1.y, vnwp.y, m2.y);
857                                 vnwp.z = bound(m1.z, vnwp.z, m2.z);
858                                 e.nearestwaypoint_dist = vlen(ve - vnwp);
859                         }
860                         else
861                         {
862                                 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
863
864                                 if(!e.navigation_dynamicgoal)
865                                         e.blacklisted = true;
866
867                                 if(e.blacklisted)
868                                 {
869                                         LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
870                                         return;
871                                 }
872                         }
873
874                         if(e.navigation_dynamicgoal)
875                                 e.nearestwaypointtimeout = time + 2;
876                         else if(autocvar_g_waypointeditor)
877                                 e.nearestwaypointtimeout = time + 3 + random() * 2;
878                 }
879                 nwp = e.nearestwaypoint;
880         }
881
882         LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
883         if (nwp)
884         if (nwp.wpcost < 10000000)
885         {
886                 //te_wizspike(nwp.wpnearestpoint);
887                 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
888                 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
889                 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
890                 if (navigation_bestrating < f)
891                 {
892                         LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
893                         navigation_bestrating = f;
894                         navigation_bestgoal = e;
895                 }
896         }
897 }
898
899 // adds an item to the the goal stack with the path to a given item
900 bool navigation_routetogoal(entity this, entity e, vector startposition)
901 {
902         // if there is no goal, just exit
903         if (!e)
904                 return false;
905
906         this.goalentity = e;
907
908         // put the entity on the goal stack
909         //print("routetogoal ", etos(e), "\n");
910         navigation_pushroute(this, e);
911
912         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
913         {
914                 this.wp_goal_prev1 = this.wp_goal_prev0;
915                 this.wp_goal_prev0 = e;
916         }
917
918         if(g_jetpack)
919         if(e==this.navigation_jetpack_goal)
920                 return true;
921
922         // if it can reach the goal there is nothing more to do
923         if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
924                 return true;
925
926         entity nearest_wp = NULL;
927         // see if there are waypoints describing a path to the item
928         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
929         {
930                 e = e.nearestwaypoint;
931                 nearest_wp = e;
932         }
933         else
934                 e = e.enemy; // we already have added it, so...
935
936         if(e == NULL)
937                 return false;
938
939         if(nearest_wp && nearest_wp.enemy)
940         {
941                 // often path can be optimized by not adding the nearest waypoint
942                 if (this.goalentity.nearestwaypoint_dist < 8
943                         || 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))
944                         e = nearest_wp.enemy;
945         }
946
947         for (;;)
948         {
949                 // add the spawnfunc_waypoint to the path
950                 navigation_pushroute(this, e);
951                 e = e.enemy;
952
953                 if(e==NULL)
954                         break;
955         }
956
957         return false;
958 }
959
960 // removes any currently touching waypoints from the goal stack
961 // (this is how bots detect if they reached a goal)
962 void navigation_poptouchedgoals(entity this)
963 {
964         vector org, m1, m2;
965         org = this.origin;
966         m1 = org + this.mins;
967         m2 = org + this.maxs;
968
969         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
970         {
971                 // make sure jumppad is really hit, don't rely on distance based checks
972                 // as they may report a touch even if it didn't really happen
973                 if(this.lastteleporttime>0)
974                 if(time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
975                 {
976                         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
977                         if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
978                         {
979                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
980                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
981                         }
982                         navigation_poproute(this);
983                         return;
984                 }
985         }
986
987         // If for some reason the bot is closer to the next goal, pop the current one
988         if(this.goalstack01 && !wasfreed(this.goalstack01))
989         if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
990         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
991         if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
992         {
993                 LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
994                 navigation_poproute(this);
995                 // TODO this may also be a nice idea to do "early" (e.g. by
996                 // manipulating the vlen() comparisons) to shorten paths in
997                 // general - this would make bots walk more "on rails" than
998                 // "zigzagging" which they currently do with sufficiently
999                 // random-like waypoints, and thus can make a nice bot
1000                 // personality property
1001         }
1002
1003         // Loose goal touching check when running
1004         if(this.aistatus & AI_STATUS_RUNNING)
1005         if(this.goalcurrent.classname=="waypoint")
1006         if(!(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1007         if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
1008         {
1009                 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1010                 {
1011                         traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1012                         if(trace_fraction==1)
1013                         {
1014                                 // Detect personal waypoints
1015                                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1016                                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1017                                 {
1018                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1019                                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1020                                 }
1021
1022                                 navigation_poproute(this);
1023                         }
1024                 }
1025         }
1026
1027         while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1028         {
1029                 vector gc_min = this.goalcurrent.absmin;
1030                 vector gc_max = this.goalcurrent.absmax;
1031                 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1032                 {
1033                         gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1034                         gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1035                 }
1036                 if(!boxesoverlap(m1, m2, gc_min, gc_max))
1037                         break;
1038
1039                 if((this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT))
1040                         break;
1041
1042                 // Detect personal waypoints
1043                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1044                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1045                 {
1046                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1047                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1048                 }
1049
1050                 navigation_poproute(this);
1051         }
1052 }
1053
1054 // begin a goal selection session (queries spawnfunc_waypoint network)
1055 void navigation_goalrating_start(entity this)
1056 {
1057         if(this.aistatus & AI_STATUS_STUCK)
1058                 return;
1059
1060         this.navigation_jetpack_goal = NULL;
1061         navigation_bestrating = -1;
1062         navigation_clearroute(this);
1063         navigation_bestgoal = NULL;
1064         navigation_markroutes(this, NULL);
1065 }
1066
1067 // ends a goal selection session (updates goal stack to the best goal)
1068 void navigation_goalrating_end(entity this)
1069 {
1070         if(this.aistatus & AI_STATUS_STUCK)
1071                 return;
1072
1073         navigation_routetogoal(this, navigation_bestgoal, this.origin);
1074         LOG_DEBUG("best goal ", this.goalcurrent.classname);
1075
1076         // If the bot got stuck then try to reach the farthest waypoint
1077         if (!this.goalentity && autocvar_bot_wander_enable)
1078         {
1079                 if (!(this.aistatus & AI_STATUS_STUCK))
1080                 {
1081                         LOG_DEBUG(this.netname, " cannot walk to any goal");
1082                         this.aistatus |= AI_STATUS_STUCK;
1083                 }
1084         }
1085 }
1086
1087 void botframe_updatedangerousobjects(float maxupdate)
1088 {
1089         vector m1, m2, v, o;
1090         float c, d, danger;
1091         c = 0;
1092         IL_EACH(g_waypoints, true,
1093         {
1094                 danger = 0;
1095                 m1 = it.absmin;
1096                 m2 = it.absmax;
1097                 IL_EACH(g_bot_dodge, it.bot_dodge,
1098                 {
1099                         v = it.origin;
1100                         v.x = bound(m1_x, v.x, m2_x);
1101                         v.y = bound(m1_y, v.y, m2_y);
1102                         v.z = bound(m1_z, v.z, m2_z);
1103                         o = (it.absmin + it.absmax) * 0.5;
1104                         d = waypoint_getdistancecost_simple(it.bot_dodgerating) - waypoint_getdistancecost(o, v);
1105                         if (d > 0)
1106                         {
1107                                 traceline(o, v, true, NULL);
1108                                 if (trace_fraction == 1)
1109                                         danger = danger + d;
1110                         }
1111                 });
1112                 it.dmg = danger;
1113                 c = c + 1;
1114                 if (c >= maxupdate)
1115                         break;
1116         });
1117 }
1118
1119 void navigation_unstuck(entity this)
1120 {
1121         float search_radius = 1000;
1122
1123         if (!autocvar_bot_wander_enable)
1124                 return;
1125
1126         if (!bot_waypoint_queue_owner)
1127         {
1128                 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1129                 bot_waypoint_queue_owner = this;
1130                 bot_waypoint_queue_bestgoal = NULL;
1131                 bot_waypoint_queue_bestgoalrating = 0;
1132         }
1133
1134         if(bot_waypoint_queue_owner!=this)
1135                 return;
1136
1137         if (bot_waypoint_queue_goal)
1138         {
1139                 // evaluate the next goal on the queue
1140                 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1141                 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1142                 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1143                 {
1144                         if( d > bot_waypoint_queue_bestgoalrating)
1145                         {
1146                                 bot_waypoint_queue_bestgoalrating = d;
1147                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1148                         }
1149                 }
1150                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1151
1152                 if (!bot_waypoint_queue_goal)
1153                 {
1154                         if (bot_waypoint_queue_bestgoal)
1155                         {
1156                                 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1157                                 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1158                                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1159                                 this.aistatus &= ~AI_STATUS_STUCK;
1160                         }
1161                         else
1162                         {
1163                                 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1164                         }
1165
1166                         bot_waypoint_queue_owner = NULL;
1167                 }
1168         }
1169         else
1170         {
1171                 if(bot_strategytoken!=this)
1172                         return;
1173
1174                 // build a new queue
1175                 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1176
1177                 entity first = NULL;
1178
1179                 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1180                 {
1181                         if(bot_waypoint_queue_goal)
1182                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1183                         else
1184                                 first = it;
1185
1186                         bot_waypoint_queue_goal = it;
1187                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1188                 });
1189
1190                 if (first)
1191                         bot_waypoint_queue_goal = first;
1192                 else
1193                 {
1194                         LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1195                         bot_waypoint_queue_owner = NULL;
1196                 }
1197         }
1198 }
1199
1200 // Support for debugging tracewalk visually
1201
1202 void debugresetnodes()
1203 {
1204         debuglastnode = '0 0 0';
1205 }
1206
1207 void debugnode(entity this, vector node)
1208 {
1209         if (!IS_PLAYER(this))
1210                 return;
1211
1212         if(debuglastnode=='0 0 0')
1213         {
1214                 debuglastnode = node;
1215                 return;
1216         }
1217
1218         te_lightning2(NULL, node, debuglastnode);
1219         debuglastnode = node;
1220 }
1221
1222 void debugnodestatus(vector position, float status)
1223 {
1224         vector c;
1225
1226         switch (status)
1227         {
1228                 case DEBUG_NODE_SUCCESS:
1229                         c = '0 15 0';
1230                         break;
1231                 case DEBUG_NODE_WARNING:
1232                         c = '15 15 0';
1233                         break;
1234                 case DEBUG_NODE_FAIL:
1235                         c = '15 0 0';
1236                         break;
1237                 default:
1238                         c = '15 15 15';
1239         }
1240
1241         te_customflash(position, 40,  2, c);
1242 }
1243
1244 // Support for debugging the goal stack visually
1245
1246 .float goalcounter;
1247 .vector lastposition;
1248
1249 // Debug the goal stack visually
1250 void debuggoalstack(entity this)
1251 {
1252         entity goal;
1253         vector org, go;
1254
1255         if(this.goalcounter==0)goal=this.goalcurrent;
1256         else if(this.goalcounter==1)goal=this.goalstack01;
1257         else if(this.goalcounter==2)goal=this.goalstack02;
1258         else if(this.goalcounter==3)goal=this.goalstack03;
1259         else if(this.goalcounter==4)goal=this.goalstack04;
1260         else if(this.goalcounter==5)goal=this.goalstack05;
1261         else if(this.goalcounter==6)goal=this.goalstack06;
1262         else if(this.goalcounter==7)goal=this.goalstack07;
1263         else if(this.goalcounter==8)goal=this.goalstack08;
1264         else if(this.goalcounter==9)goal=this.goalstack09;
1265         else if(this.goalcounter==10)goal=this.goalstack10;
1266         else if(this.goalcounter==11)goal=this.goalstack11;
1267         else if(this.goalcounter==12)goal=this.goalstack12;
1268         else if(this.goalcounter==13)goal=this.goalstack13;
1269         else if(this.goalcounter==14)goal=this.goalstack14;
1270         else if(this.goalcounter==15)goal=this.goalstack15;
1271         else if(this.goalcounter==16)goal=this.goalstack16;
1272         else if(this.goalcounter==17)goal=this.goalstack17;
1273         else if(this.goalcounter==18)goal=this.goalstack18;
1274         else if(this.goalcounter==19)goal=this.goalstack19;
1275         else if(this.goalcounter==20)goal=this.goalstack20;
1276         else if(this.goalcounter==21)goal=this.goalstack21;
1277         else if(this.goalcounter==22)goal=this.goalstack22;
1278         else if(this.goalcounter==23)goal=this.goalstack23;
1279         else if(this.goalcounter==24)goal=this.goalstack24;
1280         else if(this.goalcounter==25)goal=this.goalstack25;
1281         else if(this.goalcounter==26)goal=this.goalstack26;
1282         else if(this.goalcounter==27)goal=this.goalstack27;
1283         else if(this.goalcounter==28)goal=this.goalstack28;
1284         else if(this.goalcounter==29)goal=this.goalstack29;
1285         else if(this.goalcounter==30)goal=this.goalstack30;
1286         else if(this.goalcounter==31)goal=this.goalstack31;
1287         else goal=NULL;
1288
1289         if(goal==NULL)
1290         {
1291                 this.goalcounter = 0;
1292                 this.lastposition='0 0 0';
1293                 return;
1294         }
1295
1296         if(this.lastposition=='0 0 0')
1297                 org = this.origin;
1298         else
1299                 org = this.lastposition;
1300
1301
1302         go = ( goal.absmin + goal.absmax ) * 0.5;
1303         te_lightning2(NULL, org, go);
1304         this.lastposition = go;
1305
1306         this.goalcounter++;
1307 }