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