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