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