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