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