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