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