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