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