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