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