]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/navigation.qc
Start of uncrusticracy script, and kill trailing whitespace in all sources.
[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(entity ent, float walkfromwp, float bestdist)
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)
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         return navigation_findnearestwaypoint_withdist(ent, walkfromwp, 1050);
430 }
431
432 // finds the waypoints near the bot initiating a navigation query
433 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
434 {
435         entity head;
436         vector v, m1, m2, diff;
437         float c;
438 //      navigation_testtracewalk = TRUE;
439         c = 0;
440         head = waylist;
441         while (head)
442         {
443                 if (!head.wpconsidered)
444                 {
445                         if (head.wpisbox)
446                         {
447                                 m1 = head.origin + head.mins;
448                                 m2 = head.origin + head.maxs;
449                                 v = self.origin;
450                                 v_x = bound(m1_x, v_x, m2_x);
451                                 v_y = bound(m1_y, v_y, m2_y);
452                                 v_z = bound(m1_z, v_z, m2_z);
453                         }
454                         else
455                                 v = head.origin;
456                         diff = v - self.origin;
457                         diff_z = max(0, diff_z);
458                         if (vlen(diff) < maxdist)
459                         {
460                                 head.wpconsidered = TRUE;
461                                 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
462                                 {
463                                         head.wpnearestpoint = v;
464                                         head.wpcost = vlen(v - self.origin) + head.dmg;
465                                         head.wpfire = 1;
466                                         head.enemy = world;
467                                         c = c + 1;
468                                 }
469                         }
470                 }
471                 head = head.chain;
472         }
473         //navigation_testtracewalk = FALSE;
474         return c;
475 }
476
477 // updates a path link if a spawnfunc_waypoint link is better than the current one
478 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
479 {
480         vector m1;
481         vector m2;
482         vector v;
483         if (wp.wpisbox)
484         {
485                 m1 = wp.absmin;
486                 m2 = wp.absmax;
487                 v_x = bound(m1_x, p_x, m2_x);
488                 v_y = bound(m1_y, p_y, m2_y);
489                 v_z = bound(m1_z, p_z, m2_z);
490         }
491         else
492                 v = wp.origin;
493         cost2 = cost2 + vlen(v - p);
494         if (wp.wpcost > cost2)
495         {
496                 wp.wpcost = cost2;
497                 wp.enemy = w;
498                 wp.wpfire = 1;
499                 wp.wpnearestpoint = v;
500         }
501 }
502
503 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
504 void navigation_markroutes(entity fixed_source_waypoint)
505 {
506         entity w, wp, waylist;
507         float searching, cost, cost2;
508         vector p;
509         w = waylist = findchain(classname, "waypoint");
510         while (w)
511         {
512                 w.wpconsidered = FALSE;
513                 w.wpnearestpoint = '0 0 0';
514                 w.wpcost = 10000000;
515                 w.wpfire = 0;
516                 w.enemy = world;
517                 w = w.chain;
518         }
519
520         if(fixed_source_waypoint)
521         {
522                 fixed_source_waypoint.wpconsidered = TRUE;
523                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
524                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
525                 fixed_source_waypoint.wpfire = 1;
526                 fixed_source_waypoint.enemy = world;
527         }
528         else
529         {
530                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
531                 // as this search is expensive we will use lower values if the bot is on the air
532                 float i, increment, maxdistance;
533                 if(self.flags & FL_ONGROUND)
534                 {
535                         increment = 750;
536                         maxdistance = 50000;
537                 }
538                 else
539                 {
540                         increment = 500;
541                         maxdistance = 1500;
542                 }
543
544                 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
545         }
546
547         searching = TRUE;
548         while (searching)
549         {
550                 searching = FALSE;
551                 w = waylist;
552                 while (w)
553                 {
554                         if (w.wpfire)
555                         {
556                                 searching = TRUE;
557                                 w.wpfire = 0;
558                                 cost = w.wpcost;
559                                 p = w.wpnearestpoint;
560                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
561                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
562                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
563                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
564                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
565                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
566                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
567                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
593                         }
594                         w = w.chain;
595                 }
596         }
597 }
598
599 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
600 void navigation_markroutes_inverted(entity fixed_source_waypoint)
601 {
602         entity w, wp, waylist;
603         float searching, cost, cost2;
604         vector p;
605         w = waylist = findchain(classname, "waypoint");
606         while (w)
607         {
608                 w.wpconsidered = FALSE;
609                 w.wpnearestpoint = '0 0 0';
610                 w.wpcost = 10000000;
611                 w.wpfire = 0;
612                 w.enemy = world;
613                 w = w.chain;
614         }
615
616         if(fixed_source_waypoint)
617         {
618                 fixed_source_waypoint.wpconsidered = TRUE;
619                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
620                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
621                 fixed_source_waypoint.wpfire = 1;
622                 fixed_source_waypoint.enemy = world;
623         }
624         else
625         {
626                 error("need to start with a waypoint\n");
627         }
628
629         searching = TRUE;
630         while (searching)
631         {
632                 searching = FALSE;
633                 w = waylist;
634                 while (w)
635                 {
636                         if (w.wpfire)
637                         {
638                                 searching = TRUE;
639                                 w.wpfire = 0;
640                                 cost = w.wpcost; // cost to walk from w to home
641                                 p = w.wpnearestpoint;
642                                 for(wp = waylist; wp; wp = wp.chain)
643                                 {
644                                         if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
645                                         if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
646                                         if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
647                                         if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
648                                         if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
649                                         if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
650                                         if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
651                                         if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
652                                                 continue;
653                                         cost2 = cost + wp.dmg;
654                                         navigation_markroutes_checkwaypoint(w, wp, cost2, p);
655                                 }
656                         }
657                         w = w.chain;
658                 }
659         }
660 }
661
662 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
663 void navigation_routerating(entity e, float f, float rangebias)
664 {
665         entity nwp;
666         vector o;
667         if (!e)
668                 return;
669
670         if(e.blacklisted)
671                 return;
672
673         o = (e.absmin + e.absmax) * 0.5;
674
675         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
676
677         // Evaluate path using jetpack
678         if(g_jetpack)
679         if(self.items & IT_JETPACK)
680         if(autocvar_bot_ai_navigation_jetpack)
681         if(vlen(self.origin - o) > autocvar_bot_ai_navigation_jetpack_mindistance)
682         {
683                 vector pointa, pointb;
684
685                 bot_debug(strcat("jetpack ai: evaluating path for ", e.classname, "\n"));
686
687                 // Point A
688                 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
689                 pointa = trace_endpos - '0 0 1';
690
691                 // Point B
692                 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
693                 pointb = trace_endpos - '0 0 1';
694
695                 // Can I see these two points from the sky?
696                 traceline(pointa, pointb, MOVE_NORMAL, self);
697
698                 if(trace_fraction==1)
699                 {
700                         bot_debug("jetpack ai: can bridge these two points\n");
701
702                         // Lower the altitude of these points as much as possible
703                         float zdistance, xydistance, cost, t, fuel;
704                         vector down, npa, npb;
705
706                         down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
707
708                         do{
709                                 npa = pointa + down;
710                                 npb = pointb + down;
711
712                                 if(npa_z<=self.absmax_z)
713                                         break;
714
715                                 if(npb_z<=e.absmax_z)
716                                         break;
717
718                                 traceline(npa, npb, MOVE_NORMAL, self);
719                                 if(trace_fraction==1)
720                                 {
721                                         pointa = npa;
722                                         pointb = npb;
723                                 }
724                         }
725                         while(trace_fraction == 1);
726
727
728                         // Rough estimation of fuel consumption
729                         // (ignores acceleration and current xyz velocity)
730                         xydistance = vlen(pointa - pointb);
731                         zdistance = fabs(pointa_z - self.origin_z);
732
733                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
734                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
735                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
736
737                         bot_debug(strcat("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel), "\n"));
738
739                         // enough fuel ?
740                         if(self.ammo_fuel>fuel)
741                         {
742                                 // Estimate cost
743                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
744                                 //  - between air and ground speeds)
745
746                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
747                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
748                                 cost *= 1.5;
749
750                                 // Compare against other goals
751                                 f = f * rangebias / (rangebias + cost);
752
753                                 if (navigation_bestrating < f)
754                                 {
755                                         bot_debug(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
756                                         navigation_bestrating = f;
757                                         navigation_bestgoal = e;
758                                         self.navigation_jetpack_goal = e;
759                                         self.navigation_jetpack_point = pointb;
760                                 }
761                                 return;
762                         }
763                 }
764         }
765
766         //te_wizspike(e.origin);
767         //bprint(etos(e));
768         //bprint("\n");
769         // update the cached spawnfunc_waypoint link on a dynamic item entity
770         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
771         {
772                 nwp = e;
773         }
774         else
775         {
776                 float search;
777
778                 search = TRUE;
779
780                 if(e.flags & FL_ITEM)
781                 {
782                         if (!(e.flags & FL_WEAPON))
783                         if(e.nearestwaypoint)
784                                 search = FALSE;
785                 }
786                 else if (e.flags & FL_WEAPON)
787                 {
788                         if(e.classname != "droppedweapon")
789                         if(e.nearestwaypoint)
790                                 search = FALSE;
791                 }
792
793                 if(search)
794                 if (time > e.nearestwaypointtimeout)
795                 {
796                         nwp = navigation_findnearestwaypoint(e, TRUE);
797                         if(nwp)
798                                 e.nearestwaypoint = nwp;
799                         else
800                         {
801                                 bot_debug(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
802
803                                 if(e.flags & FL_ITEM)
804                                         e.blacklisted = TRUE;
805                                 else if (e.flags & FL_WEAPON)
806                                 {
807                                         if(e.classname != "droppedweapon")
808                                                 e.blacklisted = TRUE;
809                                 }
810
811                                 if(e.blacklisted)
812                                 {
813                                         bot_debug(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
814                                         return;
815                                 }
816                         }
817
818                         // TODO: Cleaner solution, probably handling this timeout from ctf.qc
819                         if(e.classname=="item_flag_team")
820                                 e.nearestwaypointtimeout = time + 2;
821                         else
822                                 e.nearestwaypointtimeout = time + random() * 3 + 5;
823                 }
824                 nwp = e.nearestwaypoint;
825         }
826
827         bot_debug(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
828         if (nwp)
829         if (nwp.wpcost < 10000000)
830         {
831                 //te_wizspike(nwp.wpnearestpoint);
832                 bot_debug(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
833                 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
834                 bot_debug(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
835                 if (navigation_bestrating < f)
836                 {
837                         bot_debug(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
838                         navigation_bestrating = f;
839                         navigation_bestgoal = e;
840                 }
841         }
842 }
843
844 // adds an item to the the goal stack with the path to a given item
845 float navigation_routetogoal(entity e, vector startposition)
846 {
847         self.goalentity = e;
848
849         // if there is no goal, just exit
850         if (!e)
851                 return FALSE;
852
853         self.navigation_hasgoals = TRUE;
854
855         // put the entity on the goal stack
856         //print("routetogoal ", etos(e), "\n");
857         navigation_pushroute(e);
858
859         if(g_jetpack)
860         if(e==self.navigation_jetpack_goal)
861                 return TRUE;
862
863         // if it can reach the goal there is nothing more to do
864         if (tracewalk(self, startposition, PL_MIN, PL_MAX, (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
865                 return TRUE;
866
867         // see if there are waypoints describing a path to the item
868         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
869                 e = e.nearestwaypoint;
870         else
871                 e = e.enemy; // we already have added it, so...
872
873         if(e == world)
874                 return FALSE;
875
876         for (;;)
877         {
878                 // add the spawnfunc_waypoint to the path
879                 navigation_pushroute(e);
880                 e = e.enemy;
881
882                 if(e==world)
883                         break;
884         }
885
886         return FALSE;
887 }
888
889 // removes any currently touching waypoints from the goal stack
890 // (this is how bots detect if they reached a goal)
891 void navigation_poptouchedgoals()
892 {
893         vector org, m1, m2;
894         org = self.origin;
895         m1 = org + self.mins;
896         m2 = org + self.maxs;
897
898         if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
899         {
900                 if(self.lastteleporttime>0)
901                 if(time-self.lastteleporttime<(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
902                 {
903                         if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
904                         if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
905                         {
906                                 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
907                                 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
908                         }
909                         navigation_poproute();
910                         return;
911                 }
912         }
913
914         // If for some reason the bot is closer to the next goal, pop the current one
915         if(self.goalstack01)
916         if(vlen(self.goalcurrent.origin - self.origin) > vlen(self.goalstack01.origin - self.origin))
917         if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
918         if(tracewalk(self, self.origin, self.mins, self.maxs, (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5, bot_navigation_movemode))
919         {
920                 bot_debug(strcat("path optimized for ", self.netname, ", removed a goal from the queue\n"));
921                 navigation_poproute();
922                 // TODO this may also be a nice idea to do "early" (e.g. by
923                 // manipulating the vlen() comparisons) to shorten paths in
924                 // general - this would make bots walk more "on rails" than
925                 // "zigzagging" which they currently do with sufficiently
926                 // random-like waypoints, and thus can make a nice bot
927                 // personality property
928         }
929
930         // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
931         // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
932         if(IS_PLAYER(self.goalcurrent))
933                 navigation_poproute();
934
935         // aid for detecting jump pads better (distance based check fails sometimes)
936         if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && self.jumppadcount > 0 )
937                 navigation_poproute();
938
939         // Loose goal touching check when running
940         if(self.aistatus & AI_STATUS_RUNNING)
941         if(self.speed >= autocvar_sv_maxspeed) // if -really- running
942         if(self.goalcurrent.classname=="waypoint")
943         {
944                 if(vlen(self.origin - self.goalcurrent.origin)<150)
945                 {
946                         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
947                         if(trace_fraction==1)
948                         {
949                                 // Detect personal waypoints
950                                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
951                                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
952                                 {
953                                         self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
954                                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
955                                 }
956
957                                 navigation_poproute();
958                         }
959                 }
960         }
961
962         while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
963         {
964                 // Detect personal waypoints
965                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
966                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
967                 {
968                         self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
969                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
970                 }
971
972                 navigation_poproute();
973         }
974 }
975
976 // begin a goal selection session (queries spawnfunc_waypoint network)
977 void navigation_goalrating_start()
978 {
979         if(self.aistatus & AI_STATUS_STUCK)
980                 return;
981
982         self.navigation_jetpack_goal = world;
983         navigation_bestrating = -1;
984         self.navigation_hasgoals = FALSE;
985         navigation_clearroute();
986         navigation_bestgoal = world;
987         navigation_markroutes(world);
988 }
989
990 // ends a goal selection session (updates goal stack to the best goal)
991 void navigation_goalrating_end()
992 {
993         if(self.aistatus & AI_STATUS_STUCK)
994                 return;
995
996         navigation_routetogoal(navigation_bestgoal, self.origin);
997         bot_debug(strcat("best goal ", self.goalcurrent.classname , "\n"));
998
999         // If the bot got stuck then try to reach the farthest waypoint
1000         if (!self.navigation_hasgoals)
1001         if (autocvar_bot_wander_enable)
1002         {
1003                 if (!(self.aistatus & AI_STATUS_STUCK))
1004                 {
1005                         bot_debug(strcat(self.netname, " cannot walk to any goal\n"));
1006                         self.aistatus |= AI_STATUS_STUCK;
1007                 }
1008
1009                 self.navigation_hasgoals = FALSE; // Reset this value
1010         }
1011 }
1012
1013 void botframe_updatedangerousobjects(float maxupdate)
1014 {
1015         entity head, bot_dodgelist;
1016         vector m1, m2, v, o;
1017         float c, d, danger;
1018         c = 0;
1019         bot_dodgelist = findchainfloat(bot_dodge, TRUE);
1020         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1021         while (botframe_dangerwaypoint != world)
1022         {
1023                 danger = 0;
1024                 m1 = botframe_dangerwaypoint.mins;
1025                 m2 = botframe_dangerwaypoint.maxs;
1026                 head = bot_dodgelist;
1027                 while (head)
1028                 {
1029                         v = head.origin;
1030                         v_x = bound(m1_x, v_x, m2_x);
1031                         v_y = bound(m1_y, v_y, m2_y);
1032                         v_z = bound(m1_z, v_z, m2_z);
1033                         o = (head.absmin + head.absmax) * 0.5;
1034                         d = head.bot_dodgerating - vlen(o - v);
1035                         if (d > 0)
1036                         {
1037                                 traceline(o, v, TRUE, world);
1038                                 if (trace_fraction == 1)
1039                                         danger = danger + d;
1040                         }
1041                         head = head.chain;
1042                 }
1043                 botframe_dangerwaypoint.dmg = danger;
1044                 c = c + 1;
1045                 if (c >= maxupdate)
1046                         break;
1047                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1048         }
1049 }
1050
1051 void navigation_unstuck()
1052 {
1053         float search_radius = 1000;
1054
1055         if (!autocvar_bot_wander_enable)
1056                 return;
1057
1058         if (!bot_waypoint_queue_owner)
1059         {
1060                 bot_debug(strcat(self.netname, " sutck, taking over the waypoints queue\n"));
1061                 bot_waypoint_queue_owner = self;
1062                 bot_waypoint_queue_bestgoal = world;
1063                 bot_waypoint_queue_bestgoalrating = 0;
1064         }
1065
1066         if(bot_waypoint_queue_owner!=self)
1067                 return;
1068
1069         if (bot_waypoint_queue_goal)
1070         {
1071                 // evaluate the next goal on the queue
1072                 float d = vlen(self.origin - bot_waypoint_queue_goal.origin);
1073                 bot_debug(strcat(self.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1074                 if(tracewalk(bot_waypoint_queue_goal, self.origin, PL_MIN, PL_MAX, bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1075                 {
1076                         if( d > bot_waypoint_queue_bestgoalrating)
1077                         {
1078                                 bot_waypoint_queue_bestgoalrating = d;
1079                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1080                         }
1081                 }
1082                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1083
1084                 if (!bot_waypoint_queue_goal)
1085                 {
1086                         if (bot_waypoint_queue_bestgoal)
1087                         {
1088                                 bot_debug(strcat(self.netname, " stuck, reachable waypoint found, heading to it\n"));
1089                                 navigation_routetogoal(bot_waypoint_queue_bestgoal, self.origin);
1090                                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1091                                 self.aistatus &= ~AI_STATUS_STUCK;
1092                         }
1093                         else
1094                         {
1095                                 bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1096                         }
1097
1098                         bot_waypoint_queue_owner = world;
1099                 }
1100         }
1101         else
1102         {
1103                 if(bot_strategytoken!=self)
1104                         return;
1105
1106                 // build a new queue
1107                 bot_debug(strcat(self.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1108
1109                 entity head, first;
1110
1111                 first = world;
1112                 head = findradius(self.origin, search_radius);
1113
1114                 while(head)
1115                 {
1116                         if(head.classname=="waypoint")
1117                 //      if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1118                         {
1119                                 if(bot_waypoint_queue_goal)
1120                                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1121                                 else
1122                                         first = head;
1123
1124                                 bot_waypoint_queue_goal = head;
1125                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = world;
1126                         }
1127
1128                         head = head.chain;
1129                 }
1130
1131                 if (first)
1132                         bot_waypoint_queue_goal = first;
1133                 else
1134                 {
1135                         bot_debug(strcat(self.netname, " stuck, cannot walk to any waypoint at all\n"));
1136                         bot_waypoint_queue_owner = world;
1137                 }
1138         }
1139 }
1140
1141 // Support for debugging tracewalk visually
1142
1143 void debugresetnodes()
1144 {
1145         debuglastnode = '0 0 0';
1146 }
1147
1148 void debugnode(vector node)
1149 {
1150         if (!IS_PLAYER(self))
1151                 return;
1152
1153         if(debuglastnode=='0 0 0')
1154         {
1155                 debuglastnode = node;
1156                 return;
1157         }
1158
1159         te_lightning2(world, node, debuglastnode);
1160         debuglastnode = node;
1161 }
1162
1163 void debugnodestatus(vector position, float status)
1164 {
1165         vector c;
1166
1167         switch (status)
1168         {
1169                 case DEBUG_NODE_SUCCESS:
1170                         c = '0 15 0';
1171                         break;
1172                 case DEBUG_NODE_WARNING:
1173                         c = '15 15 0';
1174                         break;
1175                 case DEBUG_NODE_FAIL:
1176                         c = '15 0 0';
1177                         break;
1178                 default:
1179                         c = '15 15 15';
1180         }
1181
1182         te_customflash(position, 40,  2, c);
1183 }
1184
1185 // Support for debugging the goal stack visually
1186
1187 .float goalcounter;
1188 .vector lastposition;
1189
1190 // Debug the goal stack visually
1191 void debuggoalstack()
1192 {
1193         entity goal;
1194         vector org, go;
1195
1196         if(self.goalcounter==0)goal=self.goalcurrent;
1197         else if(self.goalcounter==1)goal=self.goalstack01;
1198         else if(self.goalcounter==2)goal=self.goalstack02;
1199         else if(self.goalcounter==3)goal=self.goalstack03;
1200         else if(self.goalcounter==4)goal=self.goalstack04;
1201         else if(self.goalcounter==5)goal=self.goalstack05;
1202         else if(self.goalcounter==6)goal=self.goalstack06;
1203         else if(self.goalcounter==7)goal=self.goalstack07;
1204         else if(self.goalcounter==8)goal=self.goalstack08;
1205         else if(self.goalcounter==9)goal=self.goalstack09;
1206         else if(self.goalcounter==10)goal=self.goalstack10;
1207         else if(self.goalcounter==11)goal=self.goalstack11;
1208         else if(self.goalcounter==12)goal=self.goalstack12;
1209         else if(self.goalcounter==13)goal=self.goalstack13;
1210         else if(self.goalcounter==14)goal=self.goalstack14;
1211         else if(self.goalcounter==15)goal=self.goalstack15;
1212         else if(self.goalcounter==16)goal=self.goalstack16;
1213         else if(self.goalcounter==17)goal=self.goalstack17;
1214         else if(self.goalcounter==18)goal=self.goalstack18;
1215         else if(self.goalcounter==19)goal=self.goalstack19;
1216         else if(self.goalcounter==20)goal=self.goalstack20;
1217         else if(self.goalcounter==21)goal=self.goalstack21;
1218         else if(self.goalcounter==22)goal=self.goalstack22;
1219         else if(self.goalcounter==23)goal=self.goalstack23;
1220         else if(self.goalcounter==24)goal=self.goalstack24;
1221         else if(self.goalcounter==25)goal=self.goalstack25;
1222         else if(self.goalcounter==26)goal=self.goalstack26;
1223         else if(self.goalcounter==27)goal=self.goalstack27;
1224         else if(self.goalcounter==28)goal=self.goalstack28;
1225         else if(self.goalcounter==29)goal=self.goalstack29;
1226         else if(self.goalcounter==30)goal=self.goalstack30;
1227         else if(self.goalcounter==31)goal=self.goalstack31;
1228         else goal=world;
1229
1230         if(goal==world)
1231         {
1232                 self.goalcounter = 0;
1233                 self.lastposition='0 0 0';
1234                 return;
1235         }
1236
1237         if(self.lastposition=='0 0 0')
1238                 org = self.origin;
1239         else
1240                 org = self.lastposition;
1241
1242
1243         go = ( goal.absmin + goal.absmax ) * 0.5;
1244         te_lightning2(world, org, go);
1245         self.lastposition = go;
1246
1247         self.goalcounter++;
1248 }