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