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