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