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