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