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