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