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