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