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