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