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