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