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