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