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