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