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