]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/navigation.qc
Fix some cases of missing parenthesis in bitflag checks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / navigation.qc
1 #include "navigation.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "cvars.qh"
6
7 #include "bot.qh"
8 #include "waypoints.qh"
9
10 #include <common/t_items.qh>
11
12 #include <common/items/_mod.qh>
13
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/mapobjects/func/ladder.qh>
17 #include <common/mapobjects/trigger/jumppads.qh>
18
19 .float speed;
20
21 void navigation_goalrating_timeout_set(entity this)
22 {
23         if(IS_MOVABLE(this.goalentity))
24                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
25         else
26                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
27 }
28
29 // use this when current goal must be discarded immediately
30 void navigation_goalrating_timeout_force(entity this)
31 {
32         navigation_goalrating_timeout_expire(this, 0);
33 }
34
35 // use this when current goal can be kept for a short while to increase the chance
36 // of bot touching a waypoint, which helps to find a new goal more efficiently
37 void navigation_goalrating_timeout_expire(entity this, float seconds)
38 {
39         if (seconds <= 0)
40                 this.bot_strategytime = 0;
41         else if (this.bot_strategytime > time + seconds)
42                 this.bot_strategytime = time + seconds;
43 }
44
45 bool navigation_goalrating_timeout(entity this)
46 {
47         return this.bot_strategytime < time;
48 }
49
50 ERASEABLE
51 void navigation_goalrating_timeout_extend_if_needed(entity this, float seconds)
52 {
53         this.bot_strategytime = max(this.bot_strategytime, time + seconds);
54 }
55
56 #define MAX_CHASE_DISTANCE 700
57 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
58 {
59         vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
60         if (vdist(gco - this.origin, >, autocvar_sv_maxspeed * 1.5)
61                 && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
62         {
63                 return true;
64         }
65
66         if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
67         {
68                 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
69                 {
70                         this.ignoregoal = this.goalentity;
71                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
72                         return true;
73                 }
74         }
75         return false;
76 }
77
78 void navigation_dynamicgoal_init(entity this, bool initially_static)
79 {
80         this.navigation_dynamicgoal = true;
81         this.bot_basewaypoint = this.nearestwaypoint;
82         if(initially_static)
83                 this.nearestwaypointtimeout = -1;
84         else
85                 this.nearestwaypointtimeout = time;
86 }
87
88 void navigation_dynamicgoal_set(entity this, entity dropper)
89 {
90         this.nearestwaypointtimeout = time;
91         if (dropper && dropper.nearestwaypointtimeout && dropper.nearestwaypointtimeout < time + 2)
92                 this.nearestwaypoint = dropper.nearestwaypoint;
93         if (this.nearestwaypoint)
94                 this.nearestwaypointtimeout += 2;
95 }
96
97 void navigation_dynamicgoal_unset(entity this)
98 {
99         if(this.bot_basewaypoint)
100                 this.nearestwaypoint = this.bot_basewaypoint;
101         this.nearestwaypointtimeout = -1;
102 }
103
104 // returns point of ent closer to org
105 vector get_closer_dest(entity ent, vector org)
106 {
107         vector dest = '0 0 0';
108         if ((ent.classname != "waypoint") || ent.wpisbox)
109         {
110                 vector wm1 = ent.origin + ent.mins;
111                 vector wm2 = ent.origin + ent.maxs;
112                 dest.x = bound(wm1.x, org.x, wm2.x);
113                 dest.y = bound(wm1.y, org.y, wm2.y);
114                 dest.z = bound(wm1.z, org.z, wm2.z);
115         }
116         else
117                 dest = ent.origin;
118         return dest;
119 }
120
121 void set_tracewalk_dest(entity ent, vector org, bool fix_player_dest)
122 {
123         if ((ent.classname != "waypoint") || ent.wpisbox)
124         {
125                 vector wm1 = ent.origin + ent.mins;
126                 vector wm2 = ent.origin + ent.maxs;
127                 if (IS_PLAYER(ent) || IS_MONSTER(ent))
128                 {
129                         // move destination point out of player bbox otherwise tracebox always fails
130                         // (if bot_navigation_ignoreplayers is false)
131                         wm1 += vec2(PL_MIN_CONST) + '-1 -1 0';
132                         wm2 += vec2(PL_MAX_CONST) + '1 1 0';
133                 }
134                 // set destination point to x and y coords of ent that are closer to org
135                 // z coord is set to ent's min height
136                 tracewalk_dest.x = bound(wm1.x, org.x, wm2.x);
137                 tracewalk_dest.y = bound(wm1.y, org.y, wm2.y);
138                 if ((IS_PLAYER(ent) || IS_MONSTER(ent))
139                         && org.x == tracewalk_dest.x && org.y == tracewalk_dest.y && org.z > tracewalk_dest.z)
140                 {
141                         tracewalk_dest.z = wm2.z - PL_MIN_CONST.z;
142                         tracewalk_dest_height = 0;
143                         fix_player_dest = false;
144                 }
145                 else
146                 {
147                         tracewalk_dest.z = wm1.z;
148                         tracewalk_dest_height = wm2.z - wm1.z;
149                 }
150         }
151         else
152         {
153                 tracewalk_dest = ent.origin;
154                 tracewalk_dest_height = 0;
155         }
156         if (fix_player_dest && IS_PLAYER(ent) && !IS_ONGROUND(ent))
157         {
158                 // snap player to the ground
159                 if (org.x == tracewalk_dest.x && org.y == tracewalk_dest.y)
160                 {
161                         // bot is right under the player
162                         tracebox(ent.origin, ent.mins, ent.maxs, ent.origin - '0 0 700', MOVE_NORMAL, ent);
163                         tracewalk_dest = trace_endpos;
164                         tracewalk_dest_height = 0;
165                 }
166                 else
167                 {
168                         tracebox(tracewalk_dest, ent.mins, ent.maxs, tracewalk_dest - '0 0 700', MOVE_NORMAL, ent);
169                         if (!trace_startsolid && tracewalk_dest.z - trace_endpos.z > 0)
170                         {
171                                 tracewalk_dest_height = tracewalk_dest.z - trace_endpos.z;
172                                 tracewalk_dest.z = trace_endpos.z;
173                         }
174                 }
175         }
176 }
177
178 // returns point of ent closer to org
179 vector set_tracewalk_dest_2(entity ent, vector org)
180 {
181         vector closer_dest = '0 0 0';
182         if ((ent.classname != "waypoint") || ent.wpisbox)
183         {
184                 vector wm1 = ent.origin + ent.mins;
185                 vector wm2 = ent.origin + ent.maxs;
186                 closer_dest.x = bound(wm1.x, org.x, wm2.x);
187                 closer_dest.y = bound(wm1.y, org.y, wm2.y);
188                 closer_dest.z = bound(wm1.z, org.z, wm2.z);
189                 // set destination point to x and y coords of ent that are closer to org
190                 // z coord is set to ent's min height
191                 tracewalk_dest.x = closer_dest.x;
192                 tracewalk_dest.y = closer_dest.y;
193                 tracewalk_dest.z = wm1.z;
194                 tracewalk_dest_height = wm2.z - wm1.z; // destination height
195         }
196         else
197         {
198                 closer_dest = ent.origin;
199                 tracewalk_dest = closer_dest;
200                 tracewalk_dest_height = 0;
201         }
202         return closer_dest;
203 }
204
205 bool navigation_check_submerged_state(entity ent, vector pos)
206 {
207         bool submerged;
208         if(IS_PLAYER(ent))
209                 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
210         else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
211                 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
212         else
213         {
214                 submerged = SUBMERGED(pos);
215                 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
216                 //  is actually submerged because often they are inside some solid.
217                 //  That's why submerged state is saved now that we know current pos is
218                 //  not stuck in solid (previous tracewalk call to this pos was successfully)
219                 if(!ent.navigation_dynamicgoal)
220                         ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
221         }
222         return submerged;
223 }
224
225 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
226 {
227         IL_EACH(g_ladders, it.classname == "func_ladder",
228         {
229                 if(it.bot_pickup)
230                 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
231                 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
232                 {
233                         vector top = org;
234                         top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
235                         tracebox(org, m1, m2, top, movemode, e);
236                         if(trace_fraction == 1)
237                                 return true;
238                 }
239         });
240         return false;
241 }
242
243 // Unfortuntely we can't use trace_inwater since it doesn't hold the fraction of the total
244 // distance that was traveled before impact as the description in the engine (collision.h) says.
245 // It would have helped to speed up tracewalk underwater
246 vector resurface_limited(vector org, float lim, vector m1)
247 {
248         if (WETFEET(org + eZ * (lim - org.z)))
249                 org.z = lim;
250         else
251         {
252                 float RES_min_h = org.z;
253                 float RES_max_h = lim;
254                 do {
255                         org.z = 0.5 * (RES_min_h + RES_max_h);
256                         if(WETFEET(org))
257                                 RES_min_h = org.z;
258                         else
259                                 RES_max_h = org.z;
260                 } while (RES_max_h - RES_min_h >= 1);
261                 org.z = RES_min_h;
262         }
263         return org;
264 }
265 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
266
267 #define NAV_WALK 0
268 #define NAV_SWIM_ONWATER 1
269 #define NAV_SWIM_UNDERWATER 2
270
271 // rough simulation of walking from one point to another to test if a path
272 // can be traveled, used for waypoint linking and havocbot
273 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
274 // INFO: the command sv_cmd trace walk is useful to test this function in game
275 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
276 {
277         if(autocvar_bot_debug_tracewalk)
278         {
279                 debugresetnodes();
280                 debugnode(e, start);
281         }
282
283         vector org = start;
284         vector flatdir = end - start;
285         flatdir.z = 0;
286         float flatdist = vlen(flatdir);
287         flatdir = normalize(flatdir);
288         float stepdist = 32;
289         bool ignorehazards = false;
290         int nav_action;
291
292         // Analyze starting point
293         if (IN_LAVA(start))
294                 ignorehazards = true;
295
296         tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
297         if (trace_startsolid)
298         {
299                 // Bad start
300                 if(autocvar_bot_debug_tracewalk)
301                         debugnodestatus(start, DEBUG_NODE_FAIL);
302
303                 //print("tracewalk: ", vtos(start), " is a bad start\n");
304                 return false;
305         }
306
307         vector end2 = end;
308         if(end_height)
309                 end2.z += end_height;
310
311         vector fixed_end = end;
312         vector move;
313
314         if (flatdist > 0 && WETFEET(org))
315         {
316                 if (SUBMERGED(org))
317                         nav_action = NAV_SWIM_UNDERWATER;
318                 else
319                 {
320                         // tracebox down by player's height
321                         // useful to know if water level is so low that bot can still walk
322                         tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
323                         if (SUBMERGED(trace_endpos))
324                         {
325                                 org = trace_endpos;
326                                 nav_action = NAV_SWIM_UNDERWATER;
327                         }
328                         else
329                                 nav_action = NAV_WALK;
330                 }
331         }
332         else
333                 nav_action =  NAV_WALK;
334
335         // Movement loop
336         while (true)
337         {
338                 if (flatdist <= 0)
339                 {
340                         bool success = true;
341                         if (org.z > end2.z + 1)
342                         {
343                                 tracebox(org, m1, m2, end2, movemode, e);
344                                 org = trace_endpos;
345                                 if (org.z > end2.z + 1)
346                                         success = false;
347                         }
348                         else if (org.z < end.z - 1)
349                         {
350                                 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
351                                 if (SUBMERGED(trace_endpos))
352                                 {
353                                         vector v = trace_endpos;
354                                         tracebox(v, m1, m2, end, movemode, e);
355                                         if(trace_endpos.z >= end.z - 1)
356                                         {
357                                                 RESURFACE_LIMITED(v, trace_endpos.z);
358                                                 trace_endpos = v;
359                                         }
360                                 }
361                                 else if (trace_endpos.z > org.z - jumpheight_vec.z)
362                                         tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
363                                 org = trace_endpos;
364                                 if (org.z < end.z - 1)
365                                         success = false;
366                         }
367
368                         if (success)
369                         {
370                                 // Succeeded
371                                 if(autocvar_bot_debug_tracewalk)
372                                 {
373                                         debugnode(e, org);
374                                         debugnodestatus(org, DEBUG_NODE_SUCCESS);
375                                 }
376
377                                 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
378                                 return true;
379                         }
380                 }
381
382                 if(autocvar_bot_debug_tracewalk)
383                         debugnode(e, org);
384
385                 if (flatdist <= 0)
386                         break;
387
388                 if (stepdist > flatdist)
389                         stepdist = flatdist;
390                 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
391                 {
392                         // can't use movement direction here to calculate move because of
393                         // precision errors especially when direction has a high enough z value
394                         //water_dir = normalize(water_end - org);
395                         //move = org + water_dir * stepdist;
396                         fixed_end.z = bound(end.z, org.z, end2.z);
397                         if (stepdist == flatdist) {
398                                 move = fixed_end;
399                                 flatdist = 0;
400                         } else {
401                                 move = org + (fixed_end - org) * (stepdist / flatdist);
402                                 flatdist = vlen(vec2(fixed_end - move));
403                         }
404                 }
405                 else // horiz. direction
406                 {
407                         flatdist -= stepdist;
408                         move = org + flatdir * stepdist;
409                 }
410
411                 if(nav_action == NAV_SWIM_ONWATER)
412                 {
413                         tracebox(org, m1, m2, move, movemode, e); // swim
414
415                         // hit something
416                         if (trace_fraction < 1)
417                         {
418                                 // stepswim
419                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
420
421                                 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
422                                 {
423                                         org = trace_endpos;
424                                         if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
425                                         {
426                                                 if(autocvar_bot_debug_tracewalk)
427                                                 {
428                                                         debugnode(e, org);
429                                                         debugnodestatus(org, DEBUG_NODE_SUCCESS);
430                                                 }
431
432                                                 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
433                                                 return true;
434                                         }
435
436                                         if(autocvar_bot_debug_tracewalk)
437                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
438
439                                         return false;
440                                         //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
441                                 }
442
443                                 //succesful stepswim
444
445                                 if (flatdist <= 0)
446                                 {
447                                         org = trace_endpos;
448                                         continue;
449                                 }
450
451                                 if (org.z <= move.z) // going horiz.
452                                 {
453                                         tracebox(trace_endpos, m1, m2, move, movemode, e);
454                                         org = trace_endpos;
455                                         nav_action = NAV_WALK;
456                                         continue;
457                                 }
458                         }
459
460                         if (org.z <= move.z) // going horiz.
461                         {
462                                 org = trace_endpos;
463                                 nav_action = NAV_SWIM_ONWATER;
464                         }
465                         else // going down
466                         {
467                                 org = trace_endpos;
468                                 if (SUBMERGED(org))
469                                         nav_action = NAV_SWIM_UNDERWATER;
470                                 else
471                                         nav_action = NAV_SWIM_ONWATER;
472                         }
473                 }
474                 else if(nav_action == NAV_SWIM_UNDERWATER)
475                 {
476                         if (move.z >= org.z) // swimming upwards or horiz.
477                         {
478                                 tracebox(org, m1, m2, move, movemode, e); // swim
479
480                                 bool stepswum = false;
481
482                                 // hit something
483                                 if (trace_fraction < 1)
484                                 {
485                                         // stepswim
486                                         vector stepswim_move = move + stepheightvec;
487                                         if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
488                                                 stepswim_move.z = end2.z;
489
490                                         tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
491
492                                         // hit something
493                                         if (trace_startsolid)
494                                         {
495                                                 if(autocvar_bot_debug_tracewalk)
496                                                         debugnodestatus(org, DEBUG_NODE_FAIL);
497
498                                                 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
499                                                 return false;
500                                         }
501
502                                         if (trace_fraction < 1)
503                                         {
504                                                 float org_z_prev = org.z;
505                                                 RESURFACE_LIMITED(org, end2.z);
506                                                 if(org.z == org_z_prev)
507                                                 {
508                                                         if(autocvar_bot_debug_tracewalk)
509                                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
510
511                                                         //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
512                                                         return false;
513                                                 }
514                                                 if(SUBMERGED(org))
515                                                         nav_action = NAV_SWIM_UNDERWATER;
516                                                 else
517                                                         nav_action = NAV_SWIM_ONWATER;
518
519                                                 // we didn't advance horiz. in this step, flatdist decrease should be reverted
520                                                 // but we can't do it properly right now... apply this workaround instead
521                                                 if (flatdist <= 0)
522                                                         flatdist = 1;
523
524                                                 continue;
525                                         }
526
527                                         //succesful stepswim
528
529                                         if (flatdist <= 0)
530                                         {
531                                                 org = trace_endpos;
532                                                 continue;
533                                         }
534
535                                         stepswum = true;
536                                 }
537
538                                 if (!WETFEET(trace_endpos))
539                                 {
540                                         tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
541                                         // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
542                                         if (!stepswum && SUBMERGED(trace_endpos))
543                                         {
544                                                 RESURFACE_LIMITED(trace_endpos, end2.z);
545                                                 org = trace_endpos;
546                                                 nav_action = NAV_SWIM_ONWATER;
547                                                 continue;
548                                         }
549
550                                         // not submerged
551                                         org = trace_endpos;
552                                         nav_action = NAV_WALK;
553                                         continue;
554                                 }
555
556                                 // wetfeet
557                                 org = trace_endpos;
558                                 nav_action = NAV_SWIM_UNDERWATER;
559                                 continue;
560                         }
561                         else //if (move.z < org.z) // swimming downwards
562                         {
563                                 tracebox(org, m1, m2, move, movemode, e); // swim
564
565                                 // hit something
566                                 if (trace_fraction < 1)
567                                 {
568                                         // stepswim
569                                         tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
570
571                                         // hit something
572                                         if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
573                                         {
574                                                 if(autocvar_bot_debug_tracewalk)
575                                                         debugnodestatus(move, DEBUG_NODE_FAIL);
576
577                                                 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
578                                                 return false;
579                                         }
580
581                                         //succesful stepswim
582
583                                         if (flatdist <= 0)
584                                         {
585                                                 org = trace_endpos;
586                                                 continue;
587                                         }
588
589                                         if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
590                                         {
591                                                 // stepswim caused upwards direction
592                                                 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
593                                                 if (!SUBMERGED(trace_endpos))
594                                                 {
595                                                         org = trace_endpos;
596                                                         nav_action = NAV_WALK;
597                                                         continue;
598                                                 }
599                                         }
600                                 }
601
602                                 org = trace_endpos;
603                                 nav_action = NAV_SWIM_UNDERWATER;
604                                 continue;
605                         }
606                 }
607                 else if(nav_action == NAV_WALK)
608                 {
609                         // walk
610                         tracebox(org, m1, m2, move, movemode, e);
611
612                         if(autocvar_bot_debug_tracewalk)
613                                 debugnode(e, trace_endpos);
614
615                         // hit something
616                         if (trace_fraction < 1)
617                         {
618                                 // check if we can walk over this obstacle, possibly by jumpstepping
619                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
620                                 if (trace_fraction < 1 || trace_startsolid)
621                                 {
622                                         if (trace_startsolid) // hit ceiling above org
623                                         {
624                                                 // reduce stepwalk height
625                                                 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
626                                                 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
627                                         }
628                                         else //if (trace_fraction < 1)
629                                         {
630                                                 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
631                                                 if (trace_startsolid) // hit ceiling above org
632                                                 {
633                                                         // reduce jumpstepwalk height
634                                                         tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
635                                                         tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
636                                                 }
637                                         }
638
639                                         if (trace_fraction < 1)
640                                         {
641                                                 vector v = trace_endpos;
642                                                 v.z = org.z + jumpheight_vec.z;
643                                                 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
644                                                 {
645                                                         if(autocvar_bot_debug_tracewalk)
646                                                         {
647                                                                 debugnode(e, v);
648                                                                 debugnodestatus(v, DEBUG_NODE_SUCCESS);
649                                                         }
650
651                                                         //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
652                                                         return true;
653                                                 }
654
655                                                 if(autocvar_bot_debug_tracewalk)
656                                                         debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
657
658                                                 traceline( org, move, movemode, e);
659
660                                                 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
661                                                 {
662                                                         vector nextmove;
663                                                         move = trace_endpos;
664                                                         while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
665                                                         {
666                                                                 nextmove = move + (flatdir * stepdist);
667                                                                 traceline( move, nextmove, movemode, e);
668                                                                 move = nextmove;
669                                                         }
670                                                         flatdist = vlen(vec2(end - move));
671                                                 }
672                                                 else
673                                                 {
674                                                         if(autocvar_bot_debug_tracewalk)
675                                                                 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
676
677                                                         //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
678                                                         //te_explosion(trace_endpos);
679                                                         //print(ftos(e.dphitcontentsmask), "\n");
680                                                         return false; // failed
681                                                 }
682                                         }
683                                         else
684                                                 move = trace_endpos;
685                                 }
686                                 else
687                                         move = trace_endpos;
688                         }
689                         else
690                                 move = trace_endpos;
691
692                         // trace down from stepheight as far as possible and move there,
693                         // if this starts in solid we try again without the stepup, and
694                         // if that also fails we assume it is a wall
695                         // (this is the same logic as the Quake walkmove function used)
696                         tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
697
698                         org = trace_endpos;
699
700                         if (!ignorehazards)
701                         {
702                                 if (IN_LAVA(org))
703                                 {
704                                         if(autocvar_bot_debug_tracewalk)
705                                         {
706                                                 debugnode(e, trace_endpos);
707                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
708                                         }
709
710                                         //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
711                                         return false;
712                                 }
713                         }
714
715                         if (flatdist <= 0)
716                         {
717                                 if(move.z >= end2.z && org.z < end2.z)
718                                         org.z = end2.z;
719                                 continue;
720                         }
721
722                         if(org.z > move.z - 1 || !SUBMERGED(org))
723                         {
724                                 nav_action = NAV_WALK;
725                                 continue;
726                         }
727
728                         // ended up submerged while walking
729                         if(autocvar_bot_debug_tracewalk)
730                                 debugnode(e, org);
731
732                         RESURFACE_LIMITED(org, move.z);
733                         nav_action = NAV_SWIM_ONWATER;
734                         continue;
735                 }
736         }
737
738         //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
739
740         // moved but didn't arrive at the intended destination
741         if(autocvar_bot_debug_tracewalk)
742                 debugnodestatus(org, DEBUG_NODE_FAIL);
743
744         return false;
745 }
746
747 /////////////////////////////////////////////////////////////////////////////
748 // goal stack
749 /////////////////////////////////////////////////////////////////////////////
750
751 // completely empty the goal stack, used when deciding where to go
752 void navigation_clearroute(entity this)
753 {
754         this.lastteleporttime = 0;
755         this.goalcurrent_prev = this.goalcurrent;
756         this.goalcurrent_distance_2d = FLOAT_MAX;
757         this.goalcurrent_distance_z = FLOAT_MAX;
758         this.goalcurrent_distance_time = 0;
759         this.goalentity_lock_timeout = 0;
760         this.goalentity_shouldbefrozen = false;
761         this.goalentity = NULL;
762         this.goalcurrent = NULL;
763         this.goalstack01 = NULL;
764         this.goalstack02 = NULL;
765         this.goalstack03 = NULL;
766         this.goalstack04 = NULL;
767         this.goalstack05 = NULL;
768         this.goalstack06 = NULL;
769         this.goalstack07 = NULL;
770         this.goalstack08 = NULL;
771         this.goalstack09 = NULL;
772         this.goalstack10 = NULL;
773         this.goalstack11 = NULL;
774         this.goalstack12 = NULL;
775         this.goalstack13 = NULL;
776         this.goalstack14 = NULL;
777         this.goalstack15 = NULL;
778         this.goalstack16 = NULL;
779         this.goalstack17 = NULL;
780         this.goalstack18 = NULL;
781         this.goalstack19 = NULL;
782         this.goalstack20 = NULL;
783         this.goalstack21 = NULL;
784         this.goalstack22 = NULL;
785         this.goalstack23 = NULL;
786         this.goalstack24 = NULL;
787         this.goalstack25 = NULL;
788         this.goalstack26 = NULL;
789         this.goalstack27 = NULL;
790         this.goalstack28 = NULL;
791         this.goalstack29 = NULL;
792         this.goalstack30 = NULL;
793         this.goalstack31 = NULL;
794 }
795
796 // add a new goal at the beginning of the stack
797 // (in other words: add a new prerequisite before going to the later goals)
798 // NOTE: when a waypoint is added, the WP gets pushed first, then the
799 // next-closest WP on the shortest path to the WP
800 // That means, if the stack overflows, the bot will know how to do the FIRST 32
801 // steps to the goal, and then recalculate the path.
802 void navigation_pushroute(entity this, entity e)
803 {
804         this.goalcurrent_prev = this.goalcurrent;
805         this.goalcurrent_distance_2d = FLOAT_MAX;
806         this.goalcurrent_distance_z = FLOAT_MAX;
807         this.goalcurrent_distance_time = 0;
808         //print("bot ", etos(this), " push ", etos(e), "\n");
809         if(this.goalstack31 == this.goalentity)
810                 this.goalentity = NULL;
811         this.goalstack31 = this.goalstack30;
812         this.goalstack30 = this.goalstack29;
813         this.goalstack29 = this.goalstack28;
814         this.goalstack28 = this.goalstack27;
815         this.goalstack27 = this.goalstack26;
816         this.goalstack26 = this.goalstack25;
817         this.goalstack25 = this.goalstack24;
818         this.goalstack24 = this.goalstack23;
819         this.goalstack23 = this.goalstack22;
820         this.goalstack22 = this.goalstack21;
821         this.goalstack21 = this.goalstack20;
822         this.goalstack20 = this.goalstack19;
823         this.goalstack19 = this.goalstack18;
824         this.goalstack18 = this.goalstack17;
825         this.goalstack17 = this.goalstack16;
826         this.goalstack16 = this.goalstack15;
827         this.goalstack15 = this.goalstack14;
828         this.goalstack14 = this.goalstack13;
829         this.goalstack13 = this.goalstack12;
830         this.goalstack12 = this.goalstack11;
831         this.goalstack11 = this.goalstack10;
832         this.goalstack10 = this.goalstack09;
833         this.goalstack09 = this.goalstack08;
834         this.goalstack08 = this.goalstack07;
835         this.goalstack07 = this.goalstack06;
836         this.goalstack06 = this.goalstack05;
837         this.goalstack05 = this.goalstack04;
838         this.goalstack04 = this.goalstack03;
839         this.goalstack03 = this.goalstack02;
840         this.goalstack02 = this.goalstack01;
841         this.goalstack01 = this.goalcurrent;
842         this.goalcurrent = e;
843 }
844
845 // remove first goal from stack
846 // (in other words: remove a prerequisite for reaching the later goals)
847 // (used when a spawnfunc_waypoint is reached)
848 void navigation_poproute(entity this)
849 {
850         this.goalcurrent_prev = this.goalcurrent;
851         this.goalcurrent_distance_2d = FLOAT_MAX;
852         this.goalcurrent_distance_z = FLOAT_MAX;
853         this.goalcurrent_distance_time = 0;
854         //print("bot ", etos(this), " pop\n");
855         if(this.goalcurrent == this.goalentity)
856         {
857                 this.goalentity = NULL;
858                 this.goalentity_lock_timeout = 0;
859         }
860         this.goalcurrent = this.goalstack01;
861         this.goalstack01 = this.goalstack02;
862         this.goalstack02 = this.goalstack03;
863         this.goalstack03 = this.goalstack04;
864         this.goalstack04 = this.goalstack05;
865         this.goalstack05 = this.goalstack06;
866         this.goalstack06 = this.goalstack07;
867         this.goalstack07 = this.goalstack08;
868         this.goalstack08 = this.goalstack09;
869         this.goalstack09 = this.goalstack10;
870         this.goalstack10 = this.goalstack11;
871         this.goalstack11 = this.goalstack12;
872         this.goalstack12 = this.goalstack13;
873         this.goalstack13 = this.goalstack14;
874         this.goalstack14 = this.goalstack15;
875         this.goalstack15 = this.goalstack16;
876         this.goalstack16 = this.goalstack17;
877         this.goalstack17 = this.goalstack18;
878         this.goalstack18 = this.goalstack19;
879         this.goalstack19 = this.goalstack20;
880         this.goalstack20 = this.goalstack21;
881         this.goalstack21 = this.goalstack22;
882         this.goalstack22 = this.goalstack23;
883         this.goalstack23 = this.goalstack24;
884         this.goalstack24 = this.goalstack25;
885         this.goalstack25 = this.goalstack26;
886         this.goalstack26 = this.goalstack27;
887         this.goalstack27 = this.goalstack28;
888         this.goalstack28 = this.goalstack29;
889         this.goalstack29 = this.goalstack30;
890         this.goalstack30 = this.goalstack31;
891         this.goalstack31 = NULL;
892 }
893
894 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
895 // waypoint destination coordinates instead of v (only useful for box waypoints)
896 // for normal waypoints v2 == v and v2_height == 0
897 float navigation_waypoint_will_link(vector v, vector org, entity ent, vector v2, float v2_height, vector o2, float o2_height, float walkfromwp, float bestdist)
898 {
899         if (vdist(v - org, <, bestdist))
900         {
901                 traceline(v, org, true, ent);
902                 if (trace_fraction == 1)
903                 {
904                         if (walkfromwp)
905                         {
906                                 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
907                                         return true;
908                         }
909                         else
910                         {
911                                 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
912                                         return true;
913                         }
914                 }
915         }
916         return false;
917 }
918
919 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
920 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
921 {
922         if(ent.tag_entity)
923                 ent = ent.tag_entity;
924
925         vector pm1 = ent.origin + ent.mins;
926         vector pm2 = ent.origin + ent.maxs;
927
928         if (autocvar_g_waypointeditor && !IS_BOT_CLIENT(ent))
929         {
930                 // this code allows removing waypoints in the air and seeing jumppad/telepport waypoint links
931                 // FIXME it causes a bug where a waypoint spawned really close to another one (max 16 qu)
932                 // isn't detected as the nearest waypoint
933                 IL_EACH(g_waypoints, it != ent && it != except,
934                 {
935                         if (boxesoverlap(pm1, pm2, it.absmin, it.absmax))
936                                 return it;
937                 });
938         }
939         else
940         {
941                 // do two scans, because box test is cheaper
942                 IL_EACH(g_waypoints, it != ent && it != except && !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
943                 {
944                         if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
945                         {
946                                 if(walkfromwp && !ent.navigation_dynamicgoal)
947                                         waypoint_clearlinks(ent); // initialize wpXXmincost fields
948                                 return it;
949                         }
950                 });
951         }
952
953         vector org = ent.origin;
954         if (navigation_testtracewalk)
955                 te_plasmaburn(org);
956
957         entity best = NULL;
958         vector v = '0 0 0';
959
960         if(ent.size && !IS_PLAYER(ent))
961         {
962                 org += 0.5 * (ent.mins + ent.maxs);
963                 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
964         }
965
966         // box check failed, try walk
967         IL_EACH(g_waypoints, it != ent,
968         {
969                 if (walkfromwp && (it.wpflags & WPFLAGMASK_NORELINK))
970                         continue;
971                 v = it.origin;
972
973                 if (walkfromwp)
974                 {
975                         set_tracewalk_dest(ent, v, true);
976                         if (trace_ent == ent)
977                         {
978                                 bestdist = 0;
979                                 best = it;
980                                 break;
981                         }
982                 }
983                 else
984                         set_tracewalk_dest(it, org, false);
985
986                 if (navigation_waypoint_will_link(v, org, ent,
987                         tracewalk_dest, tracewalk_dest_height,
988                         tracewalk_dest, tracewalk_dest_height, walkfromwp, bestdist))
989                 {
990                         if (walkfromwp)
991                                 bestdist = vlen(tracewalk_dest - v);
992                         else
993                                 bestdist = vlen(v - org);
994                         best = it;
995                 }
996         });
997         if(!best && !ent.navigation_dynamicgoal)
998         {
999                 int solid_save = ent.solid;
1000                 ent.solid = SOLID_BSP;
1001                 IL_EACH(g_jumppads, true,
1002                 {
1003                         if(trigger_push_test(it, ent))
1004                         {
1005                                 best = it.nearestwaypoint;
1006                                 break;
1007                         }
1008                 });
1009                 ent.solid = solid_save;
1010         }
1011         return best;
1012 }
1013 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
1014 {
1015         entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
1016         if (autocvar_g_waypointeditor_auto)
1017         {
1018                 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
1019                 if (wp && !wp2)
1020                         wp.wpflags |= WAYPOINTFLAG_PROTECTED;
1021         }
1022         return wp;
1023 }
1024
1025 // finds the waypoints near the bot initiating a navigation query
1026 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
1027 {
1028         //navigation_testtracewalk = true;
1029         int c = 0;
1030         IL_EACH(g_waypoints, !it.wpconsidered,
1031         {
1032                 set_tracewalk_dest(it, this.origin, false);
1033
1034                 vector diff = tracewalk_dest - this.origin;
1035                 diff.z = max(0, diff.z);
1036                 if(vdist(diff, <, maxdist))
1037                 {
1038                         it.wpconsidered = true;
1039                         if (tracewalk(this, this.origin, this.mins, this.maxs,
1040                                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1041                         {
1042                                 it.wpnearestpoint = tracewalk_dest;
1043                                 it.wpcost = waypoint_gettravelcost(this.origin, tracewalk_dest, this, it) + it.dmg;
1044                                 it.wpfire = 1;
1045                                 it.enemy = NULL;
1046                                 c = c + 1;
1047                         }
1048                 }
1049         });
1050         //navigation_testtracewalk = false;
1051         return c;
1052 }
1053
1054 // updates a path link if a spawnfunc_waypoint link is better than the current one
1055 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
1056 {
1057         vector m1, m2;
1058         vector v;
1059         if (wp.wpisbox)
1060         {
1061                 m1 = wp.origin + wp.mins;
1062                 m2 = wp.origin + wp.maxs;
1063                 v.x = bound(m1_x, p.x, m2_x);
1064                 v.y = bound(m1_y, p.y, m2_y);
1065                 v.z = bound(m1_z, p.z, m2_z);
1066         }
1067         else
1068                 v = wp.origin;
1069         if (w.wpflags & WAYPOINTFLAG_TELEPORT)
1070                 cost += w.wp00mincost; // assuming teleport has exactly one destination
1071         else
1072                 cost += waypoint_gettravelcost(p, v, w, wp);
1073         if (wp.wpcost > cost)
1074         {
1075                 wp.wpcost = cost;
1076                 wp.enemy = w;
1077                 wp.wpfire = 1;
1078                 wp.wpnearestpoint = v;
1079         }
1080 }
1081
1082 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1083 void navigation_markroutes(entity this, entity fixed_source_waypoint)
1084 {
1085         float cost, cost2;
1086         vector p;
1087
1088         IL_EACH(g_waypoints, true,
1089         {
1090                 it.wpconsidered = false;
1091                 it.wpnearestpoint = '0 0 0';
1092                 it.wpcost = 10000000;
1093                 it.wpfire = 0;
1094                 it.enemy = NULL;
1095         });
1096
1097         if(fixed_source_waypoint)
1098         {
1099                 fixed_source_waypoint.wpconsidered = true;
1100                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1101                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
1102                 fixed_source_waypoint.wpfire = 1;
1103                 fixed_source_waypoint.enemy = NULL;
1104         }
1105         else
1106         {
1107                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1108                 // as this search is expensive we will use lower values if the bot is on the air
1109                 float increment, maxdistance;
1110                 if(IS_ONGROUND(this))
1111                 {
1112                         increment = 750;
1113                         maxdistance = 50000;
1114                 }
1115                 else
1116                 {
1117                         increment = 500;
1118                         maxdistance = 1500;
1119                 }
1120
1121                 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
1122         }
1123
1124         bool searching = true;
1125         while (searching)
1126         {
1127                 searching = false;
1128                 IL_EACH(g_waypoints, it.wpfire,
1129                 {
1130                         searching = true;
1131                         it.wpfire = 0;
1132                         cost = it.wpcost;
1133                         p = it.wpnearestpoint;
1134                         entity wp;
1135                         wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1136                         wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1137                         wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1138                         wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1139                         wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1140                         wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1141                         wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1142                         wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1143                         wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1144                         wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1145                         wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1146                         wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1147                         wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1148                         wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1149                         wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1150                         wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1151                         wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1152                         wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1153                         wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1154                         wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1155                         wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1156                         wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1157                         wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1158                         wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1159                         wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1160                         wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1161                         wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1162                         wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1163                         wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1164                         wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1165                         wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1166                         wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1167                         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1168                 });
1169         }
1170 }
1171
1172 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1173 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1174 {
1175         float cost, cost2;
1176         vector p;
1177         IL_EACH(g_waypoints, true,
1178         {
1179                 it.wpconsidered = false;
1180                 it.wpnearestpoint = '0 0 0';
1181                 it.wpcost = 10000000;
1182                 it.wpfire = 0;
1183                 it.enemy = NULL;
1184         });
1185
1186         if(fixed_source_waypoint)
1187         {
1188                 fixed_source_waypoint.wpconsidered = true;
1189                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1190                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1191                 fixed_source_waypoint.wpfire = 1;
1192                 fixed_source_waypoint.enemy = NULL;
1193         }
1194         else
1195         {
1196                 error("need to start with a waypoint\n");
1197         }
1198
1199         bool searching = true;
1200         while (searching)
1201         {
1202                 searching = false;
1203                 IL_EACH(g_waypoints, it.wpfire,
1204                 {
1205                         searching = true;
1206                         it.wpfire = 0;
1207                         cost = it.wpcost; // cost to walk from it to home
1208                         p = it.wpnearestpoint;
1209                         entity wp = it;
1210                         IL_EACH(g_waypoints, it != wp,
1211                         {
1212                                 if(!waypoint_islinked(it, wp))
1213                                         continue;
1214                                 cost2 = cost + it.dmg;
1215                                 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1216                         });
1217                 });
1218         }
1219 }
1220
1221 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1222 void navigation_routerating(entity this, entity e, float f, float rangebias)
1223 {
1224         if (!e || e.blacklisted) { return; }
1225
1226         rangebias = waypoint_getlinearcost(rangebias);
1227         f = waypoint_getlinearcost(f);
1228
1229         if (IS_PLAYER(e))
1230         {
1231                 bool rate_wps = false;
1232                 if (e.watertype < CONTENT_WATER || (e.waterlevel > WATERLEVEL_WETFEET && !STAT(FROZEN, e))
1233                         || (e.flags & FL_PARTIALGROUND))
1234                 {
1235                         rate_wps = true;
1236                 }
1237
1238                 if(!IS_ONGROUND(e))
1239                 {
1240                         traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1241                         int t = pointcontents(trace_endpos + '0 0 1');
1242                         if(t != CONTENT_SOLID )
1243                         {
1244                                 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1245                                         rate_wps = true;
1246                                 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1247                                         return;
1248                         }
1249                 }
1250
1251                 if(rate_wps)
1252                 {
1253                         entity theEnemy = e;
1254                         entity best_wp = NULL;
1255                         float best_dist = FLOAT_MAX;
1256                         IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT)
1257                                 && vdist(it.origin - theEnemy.origin, <, 500)
1258                                 && vdist(it.origin - this.origin, >, 100)
1259                                 && vdist(it.origin - this.origin, <, 10000),
1260                         {
1261                                 float dist = vlen2(it.origin - theEnemy.origin);
1262                                 if (dist < best_dist)
1263                                 {
1264                                         best_wp = it;
1265                                         best_dist = dist;
1266                                 }
1267                         });
1268                         if (!best_wp)
1269                                 return;
1270                         e = best_wp;
1271                 }
1272         }
1273
1274         vector goal_org = (e.absmin + e.absmax) * 0.5;
1275
1276         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1277
1278         // Evaluate path using jetpack
1279         if(this.items & IT_JETPACK)
1280         if(autocvar_bot_ai_navigation_jetpack)
1281         if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1282         {
1283                 vector pointa, pointb;
1284
1285                 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1286
1287                 // Point A
1288                 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1289                 pointa = trace_endpos - '0 0 1';
1290
1291                 // Point B
1292                 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1293                 pointb = trace_endpos - '0 0 1';
1294
1295                 // Can I see these two points from the sky?
1296                 traceline(pointa, pointb, MOVE_NORMAL, this);
1297
1298                 if(trace_fraction==1)
1299                 {
1300                         LOG_DEBUG("jetpack ai: can bridge these two points");
1301
1302                         // Lower the altitude of these points as much as possible
1303                         float zdistance, xydistance, cost, t, fuel;
1304                         vector down, npa, npb;
1305
1306                         down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1307
1308                         do{
1309                                 npa = pointa + down;
1310                                 npb = pointb + down;
1311
1312                                 if(npa.z<=this.absmax.z)
1313                                         break;
1314
1315                                 if(npb.z<=e.absmax.z)
1316                                         break;
1317
1318                                 traceline(npa, npb, MOVE_NORMAL, this);
1319                                 if(trace_fraction==1)
1320                                 {
1321                                         pointa = npa;
1322                                         pointb = npb;
1323                                 }
1324                         }
1325                         while(trace_fraction == 1);
1326
1327
1328                         // Rough estimation of fuel consumption
1329                         // (ignores acceleration and current xyz velocity)
1330                         xydistance = vlen(pointa - pointb);
1331                         zdistance = fabs(pointa.z - this.origin.z);
1332
1333                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
1334                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
1335                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
1336
1337                         LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), ", have ", ftos(GetResource(this, RES_FUEL)));
1338
1339                         // enough fuel ?
1340                         if(GetResource(this, RES_FUEL) > fuel || (this.items & IT_UNLIMITED_AMMO))
1341                         {
1342                                 // Estimate cost
1343                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1344                                 //  - between air and ground speeds)
1345
1346                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1347                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1348                                 cost *= 1.5;
1349
1350                                 // Compare against other goals
1351                                 f = f * rangebias / (rangebias + cost);
1352
1353                                 if (navigation_bestrating < f)
1354                                 {
1355                                         LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1356                                         navigation_bestrating = f;
1357                                         navigation_bestgoal = e;
1358                                         this.navigation_jetpack_goal = e;
1359                                         this.navigation_jetpack_point = pointb;
1360                                 }
1361                                 return;
1362                         }
1363                 }
1364         }
1365
1366         entity nwp;
1367         //te_wizspike(e.origin);
1368         //bprint(etos(e));
1369         //bprint("\n");
1370         // update the cached spawnfunc_waypoint link on a dynamic item entity
1371         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1372         {
1373                 nwp = e;
1374         }
1375         else
1376         {
1377                 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1378                         e.nearestwaypoint = NULL;
1379
1380                 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1381                         && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1382                 {
1383                         if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1384                                 e.nearestwaypoint = nwp = e.goalcurrent;
1385                         else
1386                                 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1387                         if(!nwp)
1388                         {
1389                                 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1390
1391                                 if(!e.navigation_dynamicgoal)
1392                                         e.blacklisted = true;
1393
1394                                 if(e.blacklisted)
1395                                 {
1396                                         LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1397                                         return;
1398                                 }
1399                         }
1400
1401                         if(e.navigation_dynamicgoal)
1402                                 e.nearestwaypointtimeout = time + 2;
1403                         else if(autocvar_g_waypointeditor)
1404                                 e.nearestwaypointtimeout = time + 3 + random() * 2;
1405                 }
1406                 nwp = e.nearestwaypoint;
1407         }
1408
1409         if (nwp && nwp.wpcost < 10000000)
1410         {
1411                 //te_wizspike(nwp.wpnearestpoint);
1412                 float nwptoitem_cost = 0;
1413                 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1414                         nwptoitem_cost = nwp.wp00mincost;
1415                 else
1416                         nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1417                 float cost = nwp.wpcost + nwptoitem_cost;
1418                 LOG_DEBUG("checking ^5", e.classname, "^7 with base rating ^xf04", ftos(f), "^7 and rangebias ^xf40", ftos(rangebias));
1419                 f = f * rangebias / (rangebias + cost);
1420                 LOG_DEBUG("         ^5", e.classname, "^7 with cost ^6", ftos(cost), "^7 and final rating ^2", ftos(f));
1421                 if (navigation_bestrating < f)
1422                 {
1423                         LOG_DEBUG(" ground path: ^3added goal ^5", e.classname);
1424                         navigation_bestrating = f;
1425                         navigation_bestgoal = e;
1426                 }
1427         }
1428 }
1429
1430 // adds an item to the the goal stack with the path to a given item
1431 bool navigation_routetogoal(entity this, entity e, vector startposition)
1432 {
1433         // if there is no goal, just exit
1434         if (!e)
1435                 return false;
1436
1437         entity teleport_goal = NULL;
1438
1439         this.goalentity = e;
1440
1441         if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1442         {
1443                 // force teleport destination as route destination
1444                 teleport_goal = e;
1445                 navigation_pushroute(this, e.wp00);
1446                 this.goalentity = e.wp00;
1447         }
1448
1449         // put the entity on the goal stack
1450         //print("routetogoal ", etos(e), "\n");
1451         navigation_pushroute(this, e);
1452
1453         if(teleport_goal)
1454                 e = this.goalentity;
1455
1456         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1457         {
1458                 this.wp_goal_prev1 = this.wp_goal_prev0;
1459                 this.wp_goal_prev0 = e;
1460         }
1461
1462         if(g_jetpack)
1463         if(e==this.navigation_jetpack_goal)
1464                 return true;
1465
1466         // if it can reach the goal there is nothing more to do
1467         set_tracewalk_dest(e, startposition, true);
1468         if ((!IS_MOVABLE(this.goalcurrent) || vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE))
1469                 && (trace_ent == this || tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this),
1470                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1471         {
1472                 return true;
1473         }
1474
1475         entity nearest_wp = NULL;
1476         // see if there are waypoints describing a path to the item
1477         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1478         {
1479                 e = e.nearestwaypoint;
1480                 nearest_wp = e;
1481         }
1482         else if(teleport_goal)
1483                 e = teleport_goal;
1484         else
1485                 e = e.enemy; // we already have added it, so...
1486
1487         if(e == NULL)
1488                 return false;
1489
1490         if(nearest_wp && nearest_wp.enemy && !(nearest_wp.enemy.wpflags & WPFLAGMASK_NORELINK))
1491         {
1492                 // often path can be optimized by not adding the nearest waypoint
1493                 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1494                 {
1495                         if (nearest_wp.enemy.wpcost < autocvar_bot_ai_strategyinterval_movingtarget)
1496                         {
1497                                 if (vdist(vec2(this.goalentity.origin - nearest_wp.origin), <, 32))
1498                                         e = nearest_wp.enemy;
1499                                 else
1500                                 {
1501                                         set_tracewalk_dest(this.goalentity, nearest_wp.enemy.origin, true);
1502                                         if (trace_ent == this || (vdist(tracewalk_dest - nearest_wp.enemy.origin, <, 1050)
1503                                                 && vlen2(tracewalk_dest - nearest_wp.enemy.origin) < vlen2(nearest_wp.origin - nearest_wp.enemy.origin)
1504                                                 && tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1505                                                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
1506                                         {
1507                                                 e = nearest_wp.enemy;
1508                                         }
1509                                 }
1510                         }
1511                 }
1512                 else
1513                 {
1514                         // NOTE unlike waypoints, items hold incoming links
1515                         navigation_item_initlinks_ifneeded(this.goalentity);
1516                         int link_num = navigation_item_getlinknum(this.goalentity, nearest_wp.enemy);
1517                         if (link_num >= 0)
1518                         {
1519                                 if (navigation_item_iswalkablelink(this.goalentity, link_num))
1520                                         e = nearest_wp.enemy;
1521                         }
1522                         else // untested link
1523                         {
1524                                 entity wp = nearest_wp.enemy;
1525                                 entity goal = this.goalentity;
1526                                 bool walkable = false;
1527                                 if (checkpvs(wp.origin, goal))
1528                                 {
1529                                         set_tracewalk_dest(goal, wp.origin, false);
1530                                         if (vdist(tracewalk_dest - wp.origin, <, 1050)
1531                                                 && tracewalk(goal, wp.origin, PL_MIN_CONST, PL_MAX_CONST,
1532                                                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1533                                         {
1534                                                 walkable = true;
1535                                                 e = nearest_wp.enemy;
1536                                         }
1537                                 }
1538                                 navigation_item_add_link(wp, goal, walkable);
1539                         }
1540                 }
1541         }
1542
1543         for (;;)
1544         {
1545                 // add the spawnfunc_waypoint to the path
1546                 navigation_pushroute(this, e);
1547                 e = e.enemy;
1548
1549                 if(e==NULL)
1550                         break;
1551         }
1552
1553         return false;
1554 }
1555
1556 // shorten path by removing intermediate goals
1557 bool navigation_shortenpath(entity this)
1558 {
1559         if (!this.goalstack01 || wasfreed(this.goalstack01))
1560                 return false;
1561         if (this.bot_tracewalk_time > time)
1562                 return false;
1563         this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
1564
1565         bool cut_allowed = false;
1566         entity next = this.goalentity;
1567         // evaluate whether bot can discard current route and chase directly a player, trying to
1568         // keep waypoint route as long as possible, as it is safer and faster (bot can bunnyhop)
1569         if (IS_MOVABLE(next))
1570         {
1571                 set_tracewalk_dest(next, this.origin, true);
1572                 if (vdist(this.origin - tracewalk_dest, <, 200))
1573                         cut_allowed = true;
1574                 else if (vdist(tracewalk_dest - this.origin, <, MAX_CHASE_DISTANCE)
1575                         && vdist(tracewalk_dest - this.goalcurrent.origin, >, 200)
1576                         && vdist(this.origin - this.goalcurrent.origin, >, 100)
1577                         && checkpvs(this.origin + this.view_ofs, next))
1578                 {
1579                         if (vlen2(next.origin - this.origin) < vlen2(this.goalcurrent.origin - this.origin))
1580                                 cut_allowed = true;
1581                         else
1582                         {
1583                                 vector deviation = vectoangles(this.goalcurrent.origin - this.origin) - vectoangles(next.origin - this.origin);
1584                                 while (deviation.y < -180) deviation.y += 360;
1585                                 while (deviation.y > 180) deviation.y -= 360;
1586                                 if (fabs(deviation.y) > 25)
1587                                         cut_allowed = true;
1588                         }
1589                 }
1590                 if (cut_allowed)
1591                 {
1592                         if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1593                                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1594                         {
1595                                 LOG_DEBUG("path optimized for ", this.netname, ", route cleared");
1596                                 do
1597                                 {
1598                                         navigation_poproute(this);
1599                                 }
1600                                 while (this.goalcurrent != next);
1601                                 return true;
1602                         }
1603                         return false;
1604                 }
1605         }
1606
1607         next = this.goalstack01;
1608         // if for some reason the bot is closer to the next goal, pop the current one
1609         if (!IS_MOVABLE(next) && !(this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP))
1610                 && vlen2(this.goalcurrent.origin - next.origin) > vlen2(next.origin - this.origin)
1611                 && checkpvs(this.origin + this.view_ofs, next))
1612         {
1613                 set_tracewalk_dest(next, this.origin, true);
1614                 cut_allowed = true;
1615         }
1616
1617         if (cut_allowed)
1618         {
1619                 if (trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
1620                         tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1621                 {
1622                         LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1623                         navigation_poproute(this);
1624                         return true;
1625                 }
1626         }
1627         return false;
1628 }
1629
1630 // removes any currently touching waypoints from the goal stack
1631 // (this is how bots detect if they reached a goal)
1632 int navigation_poptouchedgoals(entity this)
1633 {
1634         int removed_goals = 0;
1635
1636         if(!this.goalcurrent)
1637                 return removed_goals;
1638
1639         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1640         {
1641                 if (!this.goalcurrent.wpisbox // warpzone
1642                         && vlen2(this.origin - this.goalstack01.origin) < vlen2(this.origin - this.goalcurrent.origin))
1643                 {
1644                         // immediately remove origin and destination waypoints
1645                         navigation_poproute(this);
1646                         ++removed_goals;
1647                         navigation_poproute(this);
1648                         ++removed_goals;
1649                         this.lastteleporttime = 0;
1650                 }
1651
1652                 // make sure jumppad is really hit, don't rely on distance based checks
1653                 // as they may report a touch even if it didn't really happen
1654                 if(this.lastteleporttime > 0 && TELEPORT_USED(this, this.goalcurrent))
1655                 {
1656                         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1657                         if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1658                         {
1659                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1660                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1661                         }
1662                         if(this.jumppadcount)
1663                         {
1664                                 // remove jumppad waypoint after a random delay to prevent bots getting
1665                                 // stuck on certain jumppads that require an extra initial horizontal speed
1666                                 float max_delay = 0.1;
1667                                 if (vdist(vec2(this.velocity), >, 2 * autocvar_sv_maxspeed))
1668                                         max_delay = 0.05;
1669                                 if (time - this.lastteleporttime < random() * max_delay)
1670                                         return removed_goals;
1671                         }
1672                         else if (this.goalcurrent.wpisbox) // teleport
1673                         {
1674                                 // immediately remove origin and destination waypoints
1675                                 navigation_poproute(this);
1676                                 ++removed_goals;
1677                         }
1678                         navigation_poproute(this);
1679                         this.lastteleporttime = 0;
1680                         ++removed_goals;
1681                 }
1682                 return removed_goals;
1683         }
1684         else if (this.lastteleporttime > 0)
1685         {
1686                 // sometimes bot is pushed so hard (by a jumppad or a shot) that ends up touching the next
1687                 // teleport / jumppad / warpzone present in its path skipping check of one or more goals
1688                 // if so immediately fix bot path by removing skipped goals
1689                 entity tele_ent = NULL;
1690                 if (this.goalstack01 && (this.goalstack01.wpflags & WAYPOINTFLAG_TELEPORT))
1691                         tele_ent = this.goalstack01;
1692                 else if (this.goalstack02 && (this.goalstack02.wpflags & WAYPOINTFLAG_TELEPORT))
1693                         tele_ent = this.goalstack02;
1694                 else if (this.goalstack03 && (this.goalstack03.wpflags & WAYPOINTFLAG_TELEPORT))
1695                         tele_ent = this.goalstack03;
1696                 if (tele_ent && TELEPORT_USED(this, tele_ent))
1697                 {
1698                         if (this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1699                         if ((tele_ent.wpflags & WAYPOINTFLAG_PERSONAL) && tele_ent.owner == this)
1700                         {
1701                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1702                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1703                         }
1704                         while (this.goalcurrent != tele_ent)
1705                         {
1706                                 navigation_poproute(this);
1707                                 ++removed_goals;
1708                         }
1709                         navigation_poproute(this);
1710                         this.lastteleporttime = 0;
1711                         ++removed_goals;
1712                         return removed_goals;
1713                 }
1714                 // reset of lastteleporttime can be overriden by a jumppad when it's set
1715                 // in more than one frame: make sure it's reset
1716                 this.lastteleporttime = 0;
1717         }
1718
1719         // Loose goal touching check when running
1720         // check goalstack01 to make sure waypoint isn't the final goal
1721         if((this.aistatus & AI_STATUS_RUNNING) && this.goalcurrent.classname == "waypoint" && !(this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP)
1722                 && this.goalstack01 && !wasfreed(this.goalstack01) && vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed))
1723         {
1724                 vector gco = this.goalcurrent.origin;
1725                 float min_dist = BOT_BUNNYHOP_WP_DETECTION_RANGE;
1726                 // also detect waypoints when bot is way above them but with a narrower horizontal range
1727                 // so to increase chances bot ends up in the standard range (optimizes nearest waypoint finding)
1728                 if(vdist(this.origin - gco, <, min_dist)
1729                         || (vdist(vec2(this.origin - gco), <, min_dist * 0.5) && vdist(this.origin - eZ * 1.5 * min_dist - gco, <, min_dist)))
1730                 {
1731                         traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1732                         if(trace_fraction==1)
1733                         {
1734                                 // Detect personal waypoints
1735                                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1736                                 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1737                                 {
1738                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1739                                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1740                                 }
1741
1742                                 navigation_poproute(this);
1743                                 ++removed_goals;
1744                                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1745                                         return removed_goals;
1746                         }
1747                 }
1748         }
1749
1750         while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1751         {
1752                 vector gc_min = this.goalcurrent.absmin;
1753                 vector gc_max = this.goalcurrent.absmax;
1754                 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1755                 {
1756                         gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1757                         gc_max = this.goalcurrent.origin + '1 1 1' * 12 + eZ * (jumpheight_vec.z + STAT(PL_MIN, this).z);
1758                 }
1759                 if (time < this.ladder_time)
1760                 {
1761                         if (!boxesoverlap(this.absmin, this.absmax - eZ * STAT(PL_MAX, this).z, gc_min, gc_max))
1762                                 break;
1763                 }
1764                 else
1765                 {
1766                         if (!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1767                                 break;
1768                 }
1769
1770                 // Detect personal waypoints
1771                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1772                 if((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) && this.goalcurrent.owner==this)
1773                 {
1774                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1775                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1776                 }
1777
1778                 navigation_poproute(this);
1779                 ++removed_goals;
1780                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1781                         return removed_goals;
1782         }
1783         return removed_goals;
1784 }
1785
1786 entity navigation_get_really_close_waypoint(entity this)
1787 {
1788         entity wp = this.goalcurrent;
1789         if(!wp)
1790                 wp = this.goalcurrent_prev;
1791         if(!wp)
1792                 return NULL;
1793         float min_dist = ((this.aistatus & AI_STATUS_RUNNING) ? BOT_BUNNYHOP_WP_DETECTION_RANGE : 50);
1794         if(wp != this.goalcurrent_prev && vdist(wp.origin - this.origin, >, min_dist))
1795         {
1796                 wp = this.goalcurrent_prev;
1797                 if(!wp)
1798                         return NULL;
1799         }
1800         if(wp.classname != "waypoint")
1801         {
1802                 wp = wp.nearestwaypoint;
1803                 if(!wp)
1804                         return NULL;
1805         }
1806         if(vdist(wp.origin - this.origin, >, min_dist))
1807         {
1808                 wp = NULL;
1809                 IL_EACH(g_waypoints, !(it.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_JUMP)),
1810                 {
1811                         if(vdist(it.origin - this.origin, <, min_dist))
1812                         {
1813                                 wp = it;
1814                                 break;
1815                         }
1816                 });
1817                 if(!wp)
1818                         return NULL;
1819         }
1820         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1821                 return NULL;
1822
1823         set_tracewalk_dest(wp, this.origin, false);
1824         if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1825                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1826         {
1827                 return NULL;
1828         }
1829         return wp;
1830 }
1831
1832 // begin a goal selection session (queries spawnfunc_waypoint network)
1833 void navigation_goalrating_start(entity this)
1834 {
1835         if(this.aistatus & AI_STATUS_STUCK)
1836                 return;
1837
1838         this.navigation_jetpack_goal = NULL;
1839         navigation_bestrating = -1;
1840         entity wp = navigation_get_really_close_waypoint(this);
1841         navigation_clearroute(this);
1842         navigation_bestgoal = NULL;
1843         navigation_markroutes(this, wp);
1844         this.goalstack31 = wp; // temporarly save the really close waypoint
1845 }
1846
1847 // ends a goal selection session (updates goal stack to the best goal)
1848 void navigation_goalrating_end(entity this)
1849 {
1850         if(this.aistatus & AI_STATUS_STUCK)
1851                 return;
1852
1853         entity wp = this.goalstack31; // save to wp as this.goalstack31 is set by navigation_routetogoal
1854         this.goalstack31 = NULL;
1855
1856         navigation_routetogoal(this, navigation_bestgoal, this.origin);
1857         LOG_DEBUG("best goal ", navigation_bestgoal.classname);
1858
1859         if (wp && this.goalcurrent == wp)
1860                 navigation_poproute(this);
1861
1862         // If the bot got stuck then try to reach the farthest waypoint
1863         if (!this.goalentity)
1864         {
1865                 if (autocvar_bot_wander_enable && !(this.aistatus & AI_STATUS_STUCK))
1866                 {
1867                         LOG_DEBUG(this.netname, " cannot walk to any goal");
1868                         this.aistatus |= AI_STATUS_STUCK;
1869                 }
1870                 this.goalentity_shouldbefrozen = false;
1871         }
1872         else
1873                 this.goalentity_shouldbefrozen = boolean(STAT(FROZEN, this.goalentity));
1874 }
1875
1876 void botframe_updatedangerousobjects(float maxupdate)
1877 {
1878         vector m1, m2, v, o;
1879         float c, d, danger;
1880         c = 0;
1881         entity wp_cur;
1882         IL_EACH(g_waypoints, true,
1883         {
1884                 danger = 0;
1885                 m1 = it.absmin;
1886                 m2 = it.absmax;
1887                 wp_cur = it;
1888                 IL_EACH(g_bot_dodge, it.bot_dodge,
1889                 {
1890                         v = it.origin;
1891                         v.x = bound(m1_x, v.x, m2_x);
1892                         v.y = bound(m1_y, v.y, m2_y);
1893                         v.z = bound(m1_z, v.z, m2_z);
1894                         o = (it.absmin + it.absmax) * 0.5;
1895                         d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1896                         if (d > 0)
1897                         {
1898                                 traceline(o, v, true, NULL);
1899                                 if (trace_fraction == 1)
1900                                         danger = danger + d;
1901                         }
1902                 });
1903                 it.dmg = danger;
1904                 c = c + 1;
1905                 if (c >= maxupdate)
1906                         break;
1907         });
1908 }
1909
1910 void navigation_unstuck(entity this)
1911 {
1912         if (!autocvar_bot_wander_enable)
1913                 return;
1914
1915         bool has_user_waypoints = false;
1916         IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_GENERATED),
1917         {
1918                 has_user_waypoints = true;
1919                 break;
1920         });
1921         if (!has_user_waypoints)
1922                 return;
1923
1924         float search_radius = 1000;
1925
1926         if (!bot_waypoint_queue_owner)
1927         {
1928                 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1929                 bot_waypoint_queue_owner = this;
1930                 bot_waypoint_queue_bestgoal = NULL;
1931                 bot_waypoint_queue_bestgoalrating = 0;
1932         }
1933
1934         if(bot_waypoint_queue_owner!=this)
1935                 return;
1936
1937         if (bot_waypoint_queue_goal)
1938         {
1939                 // evaluate the next goal on the queue
1940                 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1941                 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with squared distance ", ftos(d));
1942                 set_tracewalk_dest(bot_waypoint_queue_goal, this.origin, false);
1943                 if (tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
1944                         tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1945                 {
1946                         if( d > bot_waypoint_queue_bestgoalrating)
1947                         {
1948                                 bot_waypoint_queue_bestgoalrating = d;
1949                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1950                         }
1951                 }
1952
1953                 // move to a random waypoint while bot is searching for a walkable path;
1954                 // this is usually sufficient to unstuck bots from bad spots or when other
1955                 // bots of the same team block all their ways
1956                 if (!bot_waypoint_queue_bestgoal && (!this.goalentity || random() < 0.1))
1957                 {
1958                         navigation_clearroute(this);
1959                         navigation_routetogoal(this, bot_waypoint_queue_goal, this.origin);
1960                         navigation_goalrating_timeout_expire(this, 1 + random() * 2);
1961                 }
1962
1963                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1964
1965                 if (!bot_waypoint_queue_goal)
1966                 {
1967                         if (bot_waypoint_queue_bestgoal)
1968                         {
1969                                 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1970                                 navigation_clearroute(this);
1971                                 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1972                                 navigation_goalrating_timeout_set(this);
1973                                 this.aistatus &= ~AI_STATUS_STUCK;
1974                         }
1975                         else
1976                         {
1977                                 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1978                         }
1979
1980                         bot_waypoint_queue_owner = NULL;
1981                 }
1982         }
1983         else
1984         {
1985                 if(bot_strategytoken!=this)
1986                         return;
1987
1988                 // build a new queue
1989                 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1990
1991                 entity first = NULL;
1992
1993                 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1994                 {
1995                         if(bot_waypoint_queue_goal)
1996                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1997                         else
1998                                 first = it;
1999
2000                         bot_waypoint_queue_goal = it;
2001                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
2002                 });
2003
2004                 if (first)
2005                         bot_waypoint_queue_goal = first;
2006                 else
2007                 {
2008                         LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
2009                         bot_waypoint_queue_owner = NULL;
2010                 }
2011         }
2012 }
2013
2014 // Support for debugging tracewalk visually
2015
2016 void debugresetnodes()
2017 {
2018         debuglastnode = '0 0 0';
2019 }
2020
2021 void debugnode(entity this, vector node)
2022 {
2023         if (!IS_PLAYER(this))
2024                 return;
2025
2026         if(debuglastnode=='0 0 0')
2027         {
2028                 debuglastnode = node;
2029                 return;
2030         }
2031
2032         te_lightning2(NULL, node, debuglastnode);
2033         debuglastnode = node;
2034 }
2035
2036 void debugnodestatus(vector position, float status)
2037 {
2038         vector c;
2039
2040         switch (status)
2041         {
2042                 case DEBUG_NODE_SUCCESS:
2043                         c = '0 15 0';
2044                         break;
2045                 case DEBUG_NODE_WARNING:
2046                         c = '15 15 0';
2047                         break;
2048                 case DEBUG_NODE_FAIL:
2049                         c = '15 0 0';
2050                         break;
2051                 default:
2052                         c = '15 15 15';
2053         }
2054
2055         te_customflash(position, 40,  2, c);
2056 }
2057
2058 // Support for debugging the goal stack visually
2059
2060 .float goalcounter;
2061 .vector lastposition;
2062
2063 // Debug the goal stack visually
2064 void debuggoalstack(entity this)
2065 {
2066         entity goal;
2067         vector org, go;
2068
2069         if(this.goalcounter==0)goal=this.goalcurrent;
2070         else if(this.goalcounter==1)goal=this.goalstack01;
2071         else if(this.goalcounter==2)goal=this.goalstack02;
2072         else if(this.goalcounter==3)goal=this.goalstack03;
2073         else if(this.goalcounter==4)goal=this.goalstack04;
2074         else if(this.goalcounter==5)goal=this.goalstack05;
2075         else if(this.goalcounter==6)goal=this.goalstack06;
2076         else if(this.goalcounter==7)goal=this.goalstack07;
2077         else if(this.goalcounter==8)goal=this.goalstack08;
2078         else if(this.goalcounter==9)goal=this.goalstack09;
2079         else if(this.goalcounter==10)goal=this.goalstack10;
2080         else if(this.goalcounter==11)goal=this.goalstack11;
2081         else if(this.goalcounter==12)goal=this.goalstack12;
2082         else if(this.goalcounter==13)goal=this.goalstack13;
2083         else if(this.goalcounter==14)goal=this.goalstack14;
2084         else if(this.goalcounter==15)goal=this.goalstack15;
2085         else if(this.goalcounter==16)goal=this.goalstack16;
2086         else if(this.goalcounter==17)goal=this.goalstack17;
2087         else if(this.goalcounter==18)goal=this.goalstack18;
2088         else if(this.goalcounter==19)goal=this.goalstack19;
2089         else if(this.goalcounter==20)goal=this.goalstack20;
2090         else if(this.goalcounter==21)goal=this.goalstack21;
2091         else if(this.goalcounter==22)goal=this.goalstack22;
2092         else if(this.goalcounter==23)goal=this.goalstack23;
2093         else if(this.goalcounter==24)goal=this.goalstack24;
2094         else if(this.goalcounter==25)goal=this.goalstack25;
2095         else if(this.goalcounter==26)goal=this.goalstack26;
2096         else if(this.goalcounter==27)goal=this.goalstack27;
2097         else if(this.goalcounter==28)goal=this.goalstack28;
2098         else if(this.goalcounter==29)goal=this.goalstack29;
2099         else if(this.goalcounter==30)goal=this.goalstack30;
2100         else if(this.goalcounter==31)goal=this.goalstack31;
2101         else goal=NULL;
2102
2103         if(goal==NULL)
2104         {
2105                 this.goalcounter = 0;
2106                 this.lastposition='0 0 0';
2107                 return;
2108         }
2109
2110         if(this.lastposition=='0 0 0')
2111                 org = this.origin;
2112         else
2113                 org = this.lastposition;
2114
2115
2116         go = ( goal.absmin + goal.absmax ) * 0.5;
2117         te_lightning2(NULL, org, go);
2118         this.lastposition = go;
2119
2120         this.goalcounter++;
2121 }