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