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