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