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