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