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