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