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