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