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