]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/navigation.qc
Bot AI: when assigning a bot frame to find a new goal prioritize bots that have just...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / navigation.qc
1 #include "navigation.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "cvars.qh"
6
7 #include "bot.qh"
8 #include "waypoints.qh"
9
10 #include <common/t_items.qh>
11
12 #include <common/items/_mod.qh>
13
14 #include <common/constants.qh>
15 #include <common/net_linked.qh>
16 #include <common/triggers/trigger/jumppads.qh>
17
18 .float speed;
19
20 void navigation_goalrating_timeout_set(entity this)
21 {
22         if(IS_MOVABLE(this.goalentity))
23                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval_movingtarget;
24         else
25                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
26 }
27
28 // use this when current goal must be discarded immediately
29 void navigation_goalrating_timeout_force(entity this)
30 {
31         navigation_goalrating_timeout_expire(this, 0);
32 }
33
34 // use this when current goal can be kept for a short while to increase the chance
35 // of bot touching a waypoint, which helps to find a new goal more efficiently
36 void navigation_goalrating_timeout_expire(entity this, float seconds)
37 {
38         if (seconds <= 0)
39                 this.bot_strategytime = 0;
40         else if (this.bot_strategytime > time + seconds)
41                 this.bot_strategytime = time + seconds;
42 }
43
44 bool navigation_goalrating_timeout(entity this)
45 {
46         return this.bot_strategytime < time;
47 }
48
49 bool navigation_goalrating_timeout_can_be_anticipated(entity this)
50 {
51         if(time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
52                 return true;
53
54         if (this.goalentity.bot_pickup && time > this.bot_strategytime - 5)
55         {
56                 vector gco = (this.goalentity.absmin + this.goalentity.absmax) * 0.5;
57                 if(!havocbot_goalrating_item_pickable_check_players(this, this.origin, this.goalentity, gco))
58                 {
59                         this.ignoregoal = this.goalentity;
60                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
61                         return true;
62                 }
63         }
64         return false;
65 }
66
67 void navigation_dynamicgoal_init(entity this, bool initially_static)
68 {
69         this.navigation_dynamicgoal = true;
70         this.bot_basewaypoint = this.nearestwaypoint;
71         if(initially_static)
72                 this.nearestwaypointtimeout = -1;
73         else
74                 this.nearestwaypointtimeout = time;
75 }
76
77 void navigation_dynamicgoal_set(entity this)
78 {
79         this.nearestwaypointtimeout = time;
80 }
81
82 void navigation_dynamicgoal_unset(entity this)
83 {
84         if(this.bot_basewaypoint)
85                 this.nearestwaypoint = this.bot_basewaypoint;
86         this.nearestwaypointtimeout = -1;
87 }
88
89 bool navigation_check_submerged_state(entity ent, vector pos)
90 {
91         bool submerged;
92         if(IS_PLAYER(ent))
93                 submerged = (ent.waterlevel == WATERLEVEL_SUBMERGED);
94         else if(ent.nav_submerged_state != SUBMERGED_UNDEFINED)
95                 submerged = (ent.nav_submerged_state == SUBMERGED_YES);
96         else
97         {
98                 submerged = SUBMERGED(pos);
99                 // NOTE: SUBMERGED check of box waypoint origin may fail even if origin
100                 //  is actually submerged because often they are inside some solid.
101                 //  That's why submerged state is saved now that we know current pos is
102                 //  not stuck in solid (previous tracewalk call to this pos was successfully)
103                 if(!ent.navigation_dynamicgoal)
104                         ent.nav_submerged_state = (submerged) ? SUBMERGED_YES : SUBMERGED_NO;
105         }
106         return submerged;
107 }
108
109 bool navigation_checkladders(entity e, vector org, vector m1, vector m2, vector end, vector end2, int movemode)
110 {
111         IL_EACH(g_ladders, it.classname == "func_ladder",
112         {
113                 if(it.bot_pickup)
114                 if(boxesoverlap(org + m1 + '-1 -1 -1', org + m2 + '1 1 1', it.absmin, it.absmax))
115                 if(boxesoverlap(end, end2, it.absmin + vec2(m1) + '-1 -1 0', it.absmax + vec2(m2) + '1 1 0'))
116                 {
117                         vector top = org;
118                         top.z = it.absmax.z + (PL_MAX_CONST.z - PL_MIN_CONST.z);
119                         tracebox(org, m1, m2, top, movemode, e);
120                         if(trace_fraction == 1)
121                                 return true;
122                 }
123         });
124         return false;
125 }
126
127 vector resurface_limited(vector org, float lim, vector m1)
128 {
129         if (WETFEET(org + eZ * (lim - org.z)))
130                 org.z = lim;
131         else
132         {
133                 float RES_min_h = org.z;
134                 float RES_max_h = lim;
135                 do {
136                         org.z = 0.5 * (RES_min_h + RES_max_h);
137                         if(WETFEET(org))
138                                 RES_min_h = org.z;
139                         else
140                                 RES_max_h = org.z;
141                 } while (RES_max_h - RES_min_h >= 1);
142                 org.z = RES_min_h;
143         }
144         return org;
145 }
146 #define RESURFACE_LIMITED(org, lim) org = resurface_limited(org, lim, m1)
147
148 #define NAV_WALK 0
149 #define NAV_SWIM_ONWATER 1
150 #define NAV_SWIM_UNDERWATER 2
151
152 // rough simulation of walking from one point to another to test if a path
153 // can be traveled, used for waypoint linking and havocbot
154 // if end_height is > 0 destination is any point in the vertical segment [end, end + end_height * eZ]
155 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float end_height, float movemode)
156 {
157         if(autocvar_bot_debug_tracewalk)
158         {
159                 debugresetnodes();
160                 debugnode(e, start);
161         }
162
163         vector org = start;
164         vector flatdir = end - start;
165         flatdir.z = 0;
166         float flatdist = vlen(flatdir);
167         flatdir = normalize(flatdir);
168         float stepdist = 32;
169         bool ignorehazards = false;
170         int nav_action;
171
172         // Analyze starting point
173         traceline(start, start, MOVE_NORMAL, e);
174         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
175                 ignorehazards = true;
176
177         tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
178         if (trace_startsolid)
179         {
180                 // Bad start
181                 if(autocvar_bot_debug_tracewalk)
182                         debugnodestatus(start, DEBUG_NODE_FAIL);
183
184                 //print("tracewalk: ", vtos(start), " is a bad start\n");
185                 return false;
186         }
187
188         vector end2 = end;
189         if(end_height)
190                 end2.z += end_height;
191
192         vector fixed_end = end;
193         vector move;
194
195         if (flatdist > 0 && WETFEET(org))
196         {
197                 if (SUBMERGED(org))
198                         nav_action = NAV_SWIM_UNDERWATER;
199                 else
200                 {
201                         // tracebox down by player's height
202                         // useful to know if water level is so low that bot can still walk
203                         tracebox(org, m1, m2, org - eZ * (m2.z - m1.z), movemode, e);
204                         if (SUBMERGED(trace_endpos))
205                         {
206                                 org = trace_endpos;
207                                 nav_action = NAV_SWIM_UNDERWATER;
208                         }
209                         else
210                                 nav_action = NAV_WALK;
211                 }
212         }
213         else
214                 nav_action =  NAV_WALK;
215
216         // Movement loop
217         while (true)
218         {
219                 if (flatdist <= 0)
220                 {
221                         bool success = true;
222                         if (org.z > end2.z + 1)
223                         {
224                                 tracebox(org, m1, m2, end2, movemode, e);
225                                 org = trace_endpos;
226                                 if (org.z > end2.z + 1)
227                                         success = false;
228                         }
229                         else if (org.z < end.z - 1)
230                         {
231                                 tracebox(org, m1, m2, org - jumpheight_vec, movemode, e);
232                                 if (SUBMERGED(trace_endpos))
233                                 {
234                                         vector v = trace_endpos;
235                                         tracebox(v, m1, m2, end, movemode, e);
236                                         if(trace_endpos.z >= end.z - 1)
237                                         {
238                                                 RESURFACE_LIMITED(v, trace_endpos.z);
239                                                 trace_endpos = v;
240                                         }
241                                 }
242                                 else if (trace_endpos.z > org.z - jumpheight_vec.z)
243                                         tracebox(trace_endpos, m1, m2, trace_endpos + jumpheight_vec, movemode, e);
244                                 org = trace_endpos;
245                                 if (org.z < end.z - 1)
246                                         success = false;
247                         }
248
249                         if (success)
250                         {
251                                 // Succeeded
252                                 if(autocvar_bot_debug_tracewalk)
253                                 {
254                                         debugnode(e, org);
255                                         debugnodestatus(org, DEBUG_NODE_SUCCESS);
256                                 }
257
258                                 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
259                                 return true;
260                         }
261                 }
262
263                 if(autocvar_bot_debug_tracewalk)
264                         debugnode(e, org);
265
266                 if (flatdist <= 0)
267                         break;
268
269                 if (stepdist > flatdist)
270                         stepdist = flatdist;
271                 if(nav_action == NAV_SWIM_UNDERWATER || (nav_action == NAV_SWIM_ONWATER && org.z > end2.z))
272                 {
273                         // can't use movement direction here to calculate move because of
274                         // precision errors especially when direction has a high enough z value
275                         //water_dir = normalize(water_end - org);
276                         //move = org + water_dir * stepdist;
277                         fixed_end.z = bound(end.z, org.z, end2.z);
278                         if (stepdist == flatdist) {
279                                 move = fixed_end;
280                                 flatdist = 0;
281                         } else {
282                                 move = org + (fixed_end - org) * (stepdist / flatdist);
283                                 flatdist = vlen(vec2(fixed_end - move));
284                         }
285                 }
286                 else // horiz. direction
287                 {
288                         flatdist -= stepdist;
289                         move = org + flatdir * stepdist;
290                 }
291
292                 if(nav_action == NAV_SWIM_ONWATER)
293                 {
294                         tracebox(org, m1, m2, move, movemode, e); // swim
295
296                         // hit something
297                         if (trace_fraction < 1)
298                         {
299                                 // stepswim
300                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
301
302                                 if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
303                                 {
304                                         org = trace_endpos;
305                                         if(navigation_checkladders(e, org, m1, m2, end, end2, movemode))
306                                         {
307                                                 if(autocvar_bot_debug_tracewalk)
308                                                 {
309                                                         debugnode(e, org);
310                                                         debugnodestatus(org, DEBUG_NODE_SUCCESS);
311                                                 }
312
313                                                 //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
314                                                 return true;
315                                         }
316
317                                         if(autocvar_bot_debug_tracewalk)
318                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
319
320                                         return false;
321                                         //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
322                                 }
323
324                                 //succesful stepswim
325
326                                 if (flatdist <= 0)
327                                 {
328                                         org = trace_endpos;
329                                         continue;
330                                 }
331
332                                 if (org.z <= move.z) // going horiz.
333                                 {
334                                         tracebox(trace_endpos, m1, m2, move, movemode, e);
335                                         org = trace_endpos;
336                                         nav_action = NAV_WALK;
337                                         continue;
338                                 }
339                         }
340
341                         if (org.z <= move.z) // going horiz.
342                         {
343                                 org = trace_endpos;
344                                 nav_action = NAV_SWIM_ONWATER;
345                         }
346                         else // going down
347                         {
348                                 org = trace_endpos;
349                                 if (SUBMERGED(org))
350                                         nav_action = NAV_SWIM_UNDERWATER;
351                                 else
352                                         nav_action = NAV_SWIM_ONWATER;
353                         }
354                 }
355                 else if(nav_action == NAV_SWIM_UNDERWATER)
356                 {
357                         if (move.z >= org.z) // swimming upwards or horiz.
358                         {
359                                 tracebox(org, m1, m2, move, movemode, e); // swim
360
361                                 bool stepswum = false;
362
363                                 // hit something
364                                 if (trace_fraction < 1)
365                                 {
366                                         // stepswim
367                                         vector stepswim_move = move + stepheightvec;
368                                         if (flatdist > 0 && stepswim_move.z > end2.z + stepheightvec.z) // don't allow stepswim to go higher than destination
369                                                 stepswim_move.z = end2.z;
370
371                                         tracebox(org + stepheightvec, m1, m2, stepswim_move, movemode, e);
372
373                                         // hit something
374                                         if (trace_startsolid)
375                                         {
376                                                 if(autocvar_bot_debug_tracewalk)
377                                                         debugnodestatus(org, DEBUG_NODE_FAIL);
378
379                                                 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
380                                                 return false;
381                                         }
382
383                                         if (trace_fraction < 1)
384                                         {
385                                                 float org_z_prev = org.z;
386                                                 RESURFACE_LIMITED(org, end2.z);
387                                                 if(org.z == org_z_prev)
388                                                 {
389                                                         if(autocvar_bot_debug_tracewalk)
390                                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
391
392                                                         //print("tracewalk: ", vtos(start), " can't reach ", vtos(end), "\n");
393                                                         return false;
394                                                 }
395                                                 if(SUBMERGED(org))
396                                                         nav_action = NAV_SWIM_UNDERWATER;
397                                                 else
398                                                         nav_action = NAV_SWIM_ONWATER;
399
400                                                 // we didn't advance horiz. in this step, flatdist decrease should be reverted
401                                                 // but we can't do it properly right now... apply this workaround instead
402                                                 if (flatdist <= 0)
403                                                         flatdist = 1;
404
405                                                 continue;
406                                         }
407
408                                         //succesful stepswim
409
410                                         if (flatdist <= 0)
411                                         {
412                                                 org = trace_endpos;
413                                                 continue;
414                                         }
415
416                                         stepswum = true;
417                                 }
418
419                                 if (!WETFEET(trace_endpos))
420                                 {
421                                         tracebox(trace_endpos, m1, m2, trace_endpos - eZ * (stepdist + (m2.z - m1.z)), movemode, e);
422                                         // if stepswum we'll land on the obstacle, avoid the SUBMERGED check
423                                         if (!stepswum && SUBMERGED(trace_endpos))
424                                         {
425                                                 RESURFACE_LIMITED(trace_endpos, end2.z);
426                                                 org = trace_endpos;
427                                                 nav_action = NAV_SWIM_ONWATER;
428                                                 continue;
429                                         }
430
431                                         // not submerged
432                                         org = trace_endpos;
433                                         nav_action = NAV_WALK;
434                                         continue;
435                                 }
436
437                                 // wetfeet
438                                 org = trace_endpos;
439                                 nav_action = NAV_SWIM_UNDERWATER;
440                                 continue;
441                         }
442                         else //if (move.z < org.z) // swimming downwards
443                         {
444                                 tracebox(org, m1, m2, move, movemode, e); // swim
445
446                                 // hit something
447                                 if (trace_fraction < 1)
448                                 {
449                                         // stepswim
450                                         tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
451
452                                         // hit something
453                                         if (trace_fraction < 1 || trace_startsolid) // can't jump obstacle out of water
454                                         {
455                                                 if(autocvar_bot_debug_tracewalk)
456                                                         debugnodestatus(move, DEBUG_NODE_FAIL);
457
458                                                 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
459                                                 return false;
460                                         }
461
462                                         //succesful stepswim
463
464                                         if (flatdist <= 0)
465                                         {
466                                                 org = trace_endpos;
467                                                 continue;
468                                         }
469
470                                         if (trace_endpos.z > org.z && !SUBMERGED(trace_endpos))
471                                         {
472                                                 // stepswim caused upwards direction
473                                                 tracebox(trace_endpos, m1, m2, trace_endpos - stepheightvec, movemode, e);
474                                                 if (!SUBMERGED(trace_endpos))
475                                                 {
476                                                         org = trace_endpos;
477                                                         nav_action = NAV_WALK;
478                                                         continue;
479                                                 }
480                                         }
481                                 }
482
483                                 org = trace_endpos;
484                                 nav_action = NAV_SWIM_UNDERWATER;
485                                 continue;
486                         }
487                 }
488                 else if(nav_action == NAV_WALK)
489                 {
490                         // walk
491                         tracebox(org, m1, m2, move, movemode, e);
492
493                         if(autocvar_bot_debug_tracewalk)
494                                 debugnode(e, trace_endpos);
495
496                         // hit something
497                         if (trace_fraction < 1)
498                         {
499                                 // check if we can walk over this obstacle, possibly by jumpstepping
500                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
501                                 if (trace_fraction < 1 || trace_startsolid)
502                                 {
503                                         if (trace_startsolid) // hit ceiling above org
504                                         {
505                                                 // reduce stepwalk height
506                                                 tracebox(org, m1, m2, org + stepheightvec, movemode, e);
507                                                 tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
508                                         }
509                                         else //if (trace_fraction < 1)
510                                         {
511                                                 tracebox(org + jumpstepheightvec, m1, m2, move + jumpstepheightvec, movemode, e);
512                                                 if (trace_startsolid) // hit ceiling above org
513                                                 {
514                                                         // reduce jumpstepwalk height
515                                                         tracebox(org, m1, m2, org + jumpstepheightvec, movemode, e);
516                                                         tracebox(trace_endpos, m1, m2, move + eZ * (trace_endpos.z - move.z), movemode, e);
517                                                 }
518                                         }
519
520                                         if (trace_fraction < 1)
521                                         {
522                                                 vector v = trace_endpos;
523                                                 v.z = org.z + jumpheight_vec.z;
524                                                 if(navigation_checkladders(e, v, m1, m2, end, end2, movemode))
525                                                 {
526                                                         if(autocvar_bot_debug_tracewalk)
527                                                         {
528                                                                 debugnode(e, v);
529                                                                 debugnodestatus(v, DEBUG_NODE_SUCCESS);
530                                                         }
531
532                                                         //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
533                                                         return true;
534                                                 }
535
536                                                 if(autocvar_bot_debug_tracewalk)
537                                                         debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
538
539                                                 traceline( org, move, movemode, e);
540
541                                                 if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
542                                                 {
543                                                         vector nextmove;
544                                                         move = trace_endpos;
545                                                         while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
546                                                         {
547                                                                 nextmove = move + (flatdir * stepdist);
548                                                                 traceline( move, nextmove, movemode, e);
549                                                                 move = nextmove;
550                                                         }
551                                                         flatdist = vlen(vec2(end - move));
552                                                 }
553                                                 else
554                                                 {
555                                                         if(autocvar_bot_debug_tracewalk)
556                                                                 debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
557
558                                                         //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
559                                                         //te_explosion(trace_endpos);
560                                                         //print(ftos(e.dphitcontentsmask), "\n");
561                                                         return false; // failed
562                                                 }
563                                         }
564                                         else
565                                                 move = trace_endpos;
566                                 }
567                                 else
568                                         move = trace_endpos;
569                         }
570                         else
571                                 move = trace_endpos;
572
573                         // trace down from stepheight as far as possible and move there,
574                         // if this starts in solid we try again without the stepup, and
575                         // if that also fails we assume it is a wall
576                         // (this is the same logic as the Quake walkmove function used)
577                         tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
578
579                         org = trace_endpos;
580
581                         if (!ignorehazards)
582                         {
583                                 if (IN_LAVA(org))
584                                 {
585                                         if(autocvar_bot_debug_tracewalk)
586                                         {
587                                                 debugnode(e, trace_endpos);
588                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
589                                         }
590
591                                         //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
592                                         return false;
593                                 }
594                         }
595
596                         if (flatdist <= 0)
597                         {
598                                 if(move.z >= end2.z && org.z < end2.z)
599                                         org.z = end2.z;
600                                 continue;
601                         }
602
603                         if(org.z > move.z - 1 || !SUBMERGED(org))
604                         {
605                                 nav_action = NAV_WALK;
606                                 continue;
607                         }
608
609                         // ended up submerged while walking
610                         if(autocvar_bot_debug_tracewalk)
611                                 debugnode(e, org);
612
613                         RESURFACE_LIMITED(org, move.z);
614                         nav_action = NAV_SWIM_ONWATER;
615                         continue;
616                 }
617         }
618
619         //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
620
621         // moved but didn't arrive at the intended destination
622         if(autocvar_bot_debug_tracewalk)
623                 debugnodestatus(org, DEBUG_NODE_FAIL);
624
625         return false;
626 }
627
628 /////////////////////////////////////////////////////////////////////////////
629 // goal stack
630 /////////////////////////////////////////////////////////////////////////////
631
632 // completely empty the goal stack, used when deciding where to go
633 void navigation_clearroute(entity this)
634 {
635         this.goalcurrent_prev = this.goalcurrent;
636         this.goalcurrent_distance_2d = FLOAT_MAX;
637         this.goalcurrent_distance_z = FLOAT_MAX;
638         this.goalcurrent_distance_time = 0;
639         this.goalentity_lock_timeout = 0;
640         this.goalentity = NULL;
641         this.goalcurrent = NULL;
642         this.goalstack01 = NULL;
643         this.goalstack02 = NULL;
644         this.goalstack03 = NULL;
645         this.goalstack04 = NULL;
646         this.goalstack05 = NULL;
647         this.goalstack06 = NULL;
648         this.goalstack07 = NULL;
649         this.goalstack08 = NULL;
650         this.goalstack09 = NULL;
651         this.goalstack10 = NULL;
652         this.goalstack11 = NULL;
653         this.goalstack12 = NULL;
654         this.goalstack13 = NULL;
655         this.goalstack14 = NULL;
656         this.goalstack15 = NULL;
657         this.goalstack16 = NULL;
658         this.goalstack17 = NULL;
659         this.goalstack18 = NULL;
660         this.goalstack19 = NULL;
661         this.goalstack20 = NULL;
662         this.goalstack21 = NULL;
663         this.goalstack22 = NULL;
664         this.goalstack23 = NULL;
665         this.goalstack24 = NULL;
666         this.goalstack25 = NULL;
667         this.goalstack26 = NULL;
668         this.goalstack27 = NULL;
669         this.goalstack28 = NULL;
670         this.goalstack29 = NULL;
671         this.goalstack30 = NULL;
672         this.goalstack31 = NULL;
673 }
674
675 // add a new goal at the beginning of the stack
676 // (in other words: add a new prerequisite before going to the later goals)
677 // NOTE: when a waypoint is added, the WP gets pushed first, then the
678 // next-closest WP on the shortest path to the WP
679 // That means, if the stack overflows, the bot will know how to do the FIRST 32
680 // steps to the goal, and then recalculate the path.
681 void navigation_pushroute(entity this, entity e)
682 {
683         this.goalcurrent_prev = this.goalcurrent;
684         this.goalcurrent_distance_2d = FLOAT_MAX;
685         this.goalcurrent_distance_z = FLOAT_MAX;
686         this.goalcurrent_distance_time = 0;
687         //print("bot ", etos(this), " push ", etos(e), "\n");
688         if(this.goalstack31 == this.goalentity)
689                 this.goalentity = NULL;
690         this.goalstack31 = this.goalstack30;
691         this.goalstack30 = this.goalstack29;
692         this.goalstack29 = this.goalstack28;
693         this.goalstack28 = this.goalstack27;
694         this.goalstack27 = this.goalstack26;
695         this.goalstack26 = this.goalstack25;
696         this.goalstack25 = this.goalstack24;
697         this.goalstack24 = this.goalstack23;
698         this.goalstack23 = this.goalstack22;
699         this.goalstack22 = this.goalstack21;
700         this.goalstack21 = this.goalstack20;
701         this.goalstack20 = this.goalstack19;
702         this.goalstack19 = this.goalstack18;
703         this.goalstack18 = this.goalstack17;
704         this.goalstack17 = this.goalstack16;
705         this.goalstack16 = this.goalstack15;
706         this.goalstack15 = this.goalstack14;
707         this.goalstack14 = this.goalstack13;
708         this.goalstack13 = this.goalstack12;
709         this.goalstack12 = this.goalstack11;
710         this.goalstack11 = this.goalstack10;
711         this.goalstack10 = this.goalstack09;
712         this.goalstack09 = this.goalstack08;
713         this.goalstack08 = this.goalstack07;
714         this.goalstack07 = this.goalstack06;
715         this.goalstack06 = this.goalstack05;
716         this.goalstack05 = this.goalstack04;
717         this.goalstack04 = this.goalstack03;
718         this.goalstack03 = this.goalstack02;
719         this.goalstack02 = this.goalstack01;
720         this.goalstack01 = this.goalcurrent;
721         this.goalcurrent = e;
722 }
723
724 // remove first goal from stack
725 // (in other words: remove a prerequisite for reaching the later goals)
726 // (used when a spawnfunc_waypoint is reached)
727 void navigation_poproute(entity this)
728 {
729         this.goalcurrent_prev = this.goalcurrent;
730         this.goalcurrent_distance_2d = FLOAT_MAX;
731         this.goalcurrent_distance_z = FLOAT_MAX;
732         this.goalcurrent_distance_time = 0;
733         //print("bot ", etos(this), " pop\n");
734         if(this.goalcurrent == this.goalentity)
735         {
736                 this.goalentity = NULL;
737                 this.goalentity_lock_timeout = 0;
738         }
739         this.goalcurrent = this.goalstack01;
740         this.goalstack01 = this.goalstack02;
741         this.goalstack02 = this.goalstack03;
742         this.goalstack03 = this.goalstack04;
743         this.goalstack04 = this.goalstack05;
744         this.goalstack05 = this.goalstack06;
745         this.goalstack06 = this.goalstack07;
746         this.goalstack07 = this.goalstack08;
747         this.goalstack08 = this.goalstack09;
748         this.goalstack09 = this.goalstack10;
749         this.goalstack10 = this.goalstack11;
750         this.goalstack11 = this.goalstack12;
751         this.goalstack12 = this.goalstack13;
752         this.goalstack13 = this.goalstack14;
753         this.goalstack14 = this.goalstack15;
754         this.goalstack15 = this.goalstack16;
755         this.goalstack16 = this.goalstack17;
756         this.goalstack17 = this.goalstack18;
757         this.goalstack18 = this.goalstack19;
758         this.goalstack19 = this.goalstack20;
759         this.goalstack20 = this.goalstack21;
760         this.goalstack21 = this.goalstack22;
761         this.goalstack22 = this.goalstack23;
762         this.goalstack23 = this.goalstack24;
763         this.goalstack24 = this.goalstack25;
764         this.goalstack25 = this.goalstack26;
765         this.goalstack26 = this.goalstack27;
766         this.goalstack27 = this.goalstack28;
767         this.goalstack28 = this.goalstack29;
768         this.goalstack29 = this.goalstack30;
769         this.goalstack30 = this.goalstack31;
770         this.goalstack31 = NULL;
771 }
772
773 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
774 // waypoint destination coordinates instead of v (only useful for box waypoints)
775 // for normal waypoints v2 == v and v2_height == 0
776 float navigation_waypoint_will_link(vector v, vector org, entity ent, vector v2, float v2_height, vector o2, float o2_height, float walkfromwp, float bestdist)
777 {
778         if (vdist(v - org, <, bestdist))
779         {
780                 traceline(v, org, true, ent);
781                 if (trace_fraction == 1)
782                 {
783                         if (walkfromwp)
784                         {
785                                 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
786                                         return true;
787                         }
788                         else
789                         {
790                                 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
791                                         return true;
792                         }
793                 }
794         }
795         return false;
796 }
797
798 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
799 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
800 {
801         if(ent.tag_entity)
802                 ent = ent.tag_entity;
803
804         vector pm1 = ent.origin + ent.mins;
805         vector pm2 = ent.origin + ent.maxs;
806
807         // do two scans, because box test is cheaper
808         IL_EACH(g_waypoints, it != ent && it != except,
809         {
810                 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
811                 {
812                         if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
813                         {
814                                 waypoint_clearlinks(ent); // initialize wpXXmincost fields
815                                 navigation_item_addlink(it, ent);
816                         }
817                         return it;
818                 }
819         });
820
821         vector org = ent.origin;
822         if (navigation_testtracewalk)
823                 te_plasmaburn(org);
824
825         entity best = NULL;
826         vector v = '0 0 0', v2 = '0 0 0';
827         float v2_height = 0;
828
829         if(ent.size && !IS_PLAYER(ent))
830         {
831                 org += 0.5 * (ent.mins + ent.maxs);
832                 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
833         }
834
835         if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
836         {
837                 waypoint_clearlinks(ent); // initialize wpXXmincost fields
838                 IL_EACH(g_waypoints, it != ent,
839                 {
840                         if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
841                                 continue;
842
843                         SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
844                         if(vdist(v2 - it.origin, <, 1050))
845                         if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
846                                 navigation_item_addlink(it, ent);
847                 });
848         }
849
850         // box check failed, try walk
851         IL_EACH(g_waypoints, it != ent,
852         {
853                 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
854                         continue;
855                 v = it.origin;
856                 if(walkfromwp)
857                         SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
858                 else
859                         SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
860                 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
861                 {
862                         bestdist = vlen(v - org);
863                         best = it;
864                 }
865         });
866         if(!best && !ent.navigation_dynamicgoal)
867         {
868                 int solid_save = ent.solid;
869                 ent.solid = SOLID_BSP;
870                 IL_EACH(g_jumppads, true,
871                 {
872                         if(trigger_push_test(it, ent))
873                         {
874                                 best = it.nearestwaypoint;
875                                 break;
876                         }
877                 });
878                 ent.solid = solid_save;
879         }
880         return best;
881 }
882 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
883 {
884         entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
885         if (autocvar_g_waypointeditor_auto)
886         {
887                 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
888                 if (wp && !wp2)
889                         wp.wpflags |= WAYPOINTFLAG_PROTECTED;
890         }
891         return wp;
892 }
893
894 // finds the waypoints near the bot initiating a navigation query
895 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
896 {
897         vector v = '0 0 0';
898         //navigation_testtracewalk = true;
899         int c = 0;
900         float v_height = 0;
901         IL_EACH(g_waypoints, !it.wpconsidered,
902         {
903                 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
904
905                 vector diff = v - this.origin;
906                 diff.z = max(0, diff.z);
907                 if(vdist(diff, <, maxdist))
908                 {
909                         it.wpconsidered = true;
910                         if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
911                         {
912                                 it.wpnearestpoint = v;
913                                 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
914                                 it.wpfire = 1;
915                                 it.enemy = NULL;
916                                 c = c + 1;
917                         }
918                 }
919         });
920         //navigation_testtracewalk = false;
921         return c;
922 }
923
924 // updates a path link if a spawnfunc_waypoint link is better than the current one
925 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
926 {
927         vector m1, m2;
928         vector v;
929         if (wp.wpisbox)
930         {
931                 m1 = wp.origin + wp.mins;
932                 m2 = wp.origin + wp.maxs;
933                 v.x = bound(m1_x, p.x, m2_x);
934                 v.y = bound(m1_y, p.y, m2_y);
935                 v.z = bound(m1_z, p.z, m2_z);
936         }
937         else
938                 v = wp.origin;
939         if (w.wpflags & WAYPOINTFLAG_TELEPORT)
940                 cost += w.wp00mincost; // assuming teleport has exactly one destination
941         else
942                 cost += waypoint_gettravelcost(p, v, w, wp);
943         if (wp.wpcost > cost)
944         {
945                 wp.wpcost = cost;
946                 wp.enemy = w;
947                 wp.wpfire = 1;
948                 wp.wpnearestpoint = v;
949         }
950 }
951
952 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
953 void navigation_markroutes(entity this, entity fixed_source_waypoint)
954 {
955         float cost, cost2;
956         vector p;
957
958         IL_EACH(g_waypoints, true,
959         {
960                 it.wpconsidered = false;
961                 it.wpnearestpoint = '0 0 0';
962                 it.wpcost = 10000000;
963                 it.wpfire = 0;
964                 it.enemy = NULL;
965         });
966
967         if(fixed_source_waypoint)
968         {
969                 fixed_source_waypoint.wpconsidered = true;
970                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
971                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
972                 fixed_source_waypoint.wpfire = 1;
973                 fixed_source_waypoint.enemy = NULL;
974         }
975         else
976         {
977                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
978                 // as this search is expensive we will use lower values if the bot is on the air
979                 float increment, maxdistance;
980                 if(IS_ONGROUND(this))
981                 {
982                         increment = 750;
983                         maxdistance = 50000;
984                 }
985                 else
986                 {
987                         increment = 500;
988                         maxdistance = 1500;
989                 }
990
991                 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
992         }
993
994         bool searching = true;
995         while (searching)
996         {
997                 searching = false;
998                 IL_EACH(g_waypoints, it.wpfire,
999                 {
1000                         searching = true;
1001                         it.wpfire = 0;
1002                         cost = it.wpcost;
1003                         p = it.wpnearestpoint;
1004                         entity wp;
1005                         wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006                         wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007                         wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008                         wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009                         wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010                         wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011                         wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012                         wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013                         wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014                         wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015                         wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016                         wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017                         wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018                         wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019                         wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020                         wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021                         wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022                         wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023                         wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1024                         wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1025                         wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1026                         wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1027                         wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1028                         wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1029                         wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1030                         wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1031                         wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1032                         wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1033                         wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1034                         wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1035                         wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1036                         wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1037                         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1038                 });
1039         }
1040 }
1041
1042 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1043 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1044 {
1045         float cost, cost2;
1046         vector p;
1047         IL_EACH(g_waypoints, true,
1048         {
1049                 it.wpconsidered = false;
1050                 it.wpnearestpoint = '0 0 0';
1051                 it.wpcost = 10000000;
1052                 it.wpfire = 0;
1053                 it.enemy = NULL;
1054         });
1055
1056         if(fixed_source_waypoint)
1057         {
1058                 fixed_source_waypoint.wpconsidered = true;
1059                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1060                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1061                 fixed_source_waypoint.wpfire = 1;
1062                 fixed_source_waypoint.enemy = NULL;
1063         }
1064         else
1065         {
1066                 error("need to start with a waypoint\n");
1067         }
1068
1069         bool searching = true;
1070         while (searching)
1071         {
1072                 searching = false;
1073                 IL_EACH(g_waypoints, it.wpfire,
1074                 {
1075                         searching = true;
1076                         it.wpfire = 0;
1077                         cost = it.wpcost; // cost to walk from it to home
1078                         p = it.wpnearestpoint;
1079                         entity wp = it;
1080                         IL_EACH(g_waypoints, it != wp,
1081                         {
1082                                 if(!waypoint_islinked(it, wp))
1083                                         continue;
1084                                 cost2 = cost + it.dmg;
1085                                 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1086                         });
1087                 });
1088         }
1089 }
1090
1091 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1092 void navigation_routerating(entity this, entity e, float f, float rangebias)
1093 {
1094         if (!e)
1095                 return;
1096
1097         if(e.blacklisted)
1098                 return;
1099
1100         rangebias = waypoint_getlinearcost(rangebias);
1101         f = waypoint_getlinearcost(f);
1102
1103         if (IS_PLAYER(e))
1104         {
1105                 bool rate_wps = false;
1106                 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1107                         rate_wps = true;
1108
1109                 if(!IS_ONGROUND(e))
1110                 {
1111                         traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1112                         int t = pointcontents(trace_endpos + '0 0 1');
1113                         if(t != CONTENT_SOLID )
1114                         {
1115                                 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1116                                         rate_wps = true;
1117                                 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1118                                         return;
1119                         }
1120                 }
1121
1122                 if(rate_wps)
1123                 {
1124                         entity theEnemy = e;
1125                         entity best_wp = NULL;
1126                         float best_dist = 10000;
1127                         IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1128                                 && vdist(it.origin - this.origin, >, 100)
1129                                 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1130                         {
1131                                 float dist = vlen(it.origin - theEnemy.origin);
1132                                 if (dist < best_dist)
1133                                 {
1134                                         best_wp = it;
1135                                         best_dist = dist;
1136                                 }
1137                         });
1138                         if (!best_wp)
1139                                 return;
1140                         e = best_wp;
1141                 }
1142         }
1143
1144         vector goal_org = (e.absmin + e.absmax) * 0.5;
1145
1146         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1147
1148         // Evaluate path using jetpack
1149         if(g_jetpack)
1150         if(this.items & IT_JETPACK)
1151         if(autocvar_bot_ai_navigation_jetpack)
1152         if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1153         {
1154                 vector pointa, pointb;
1155
1156                 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1157
1158                 // Point A
1159                 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1160                 pointa = trace_endpos - '0 0 1';
1161
1162                 // Point B
1163                 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1164                 pointb = trace_endpos - '0 0 1';
1165
1166                 // Can I see these two points from the sky?
1167                 traceline(pointa, pointb, MOVE_NORMAL, this);
1168
1169                 if(trace_fraction==1)
1170                 {
1171                         LOG_DEBUG("jetpack ai: can bridge these two points");
1172
1173                         // Lower the altitude of these points as much as possible
1174                         float zdistance, xydistance, cost, t, fuel;
1175                         vector down, npa, npb;
1176
1177                         down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1178
1179                         do{
1180                                 npa = pointa + down;
1181                                 npb = pointb + down;
1182
1183                                 if(npa.z<=this.absmax.z)
1184                                         break;
1185
1186                                 if(npb.z<=e.absmax.z)
1187                                         break;
1188
1189                                 traceline(npa, npb, MOVE_NORMAL, this);
1190                                 if(trace_fraction==1)
1191                                 {
1192                                         pointa = npa;
1193                                         pointb = npb;
1194                                 }
1195                         }
1196                         while(trace_fraction == 1);
1197
1198
1199                         // Rough estimation of fuel consumption
1200                         // (ignores acceleration and current xyz velocity)
1201                         xydistance = vlen(pointa - pointb);
1202                         zdistance = fabs(pointa.z - this.origin.z);
1203
1204                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
1205                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
1206                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
1207
1208                         LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1209
1210                         // enough fuel ?
1211                         if(this.ammo_fuel>fuel)
1212                         {
1213                                 // Estimate cost
1214                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1215                                 //  - between air and ground speeds)
1216
1217                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1218                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1219                                 cost *= 1.5;
1220
1221                                 // Compare against other goals
1222                                 f = f * rangebias / (rangebias + cost);
1223
1224                                 if (navigation_bestrating < f)
1225                                 {
1226                                         LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1227                                         navigation_bestrating = f;
1228                                         navigation_bestgoal = e;
1229                                         this.navigation_jetpack_goal = e;
1230                                         this.navigation_jetpack_point = pointb;
1231                                 }
1232                                 return;
1233                         }
1234                 }
1235         }
1236
1237         entity nwp;
1238         //te_wizspike(e.origin);
1239         //bprint(etos(e));
1240         //bprint("\n");
1241         // update the cached spawnfunc_waypoint link on a dynamic item entity
1242         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1243         {
1244                 nwp = e;
1245         }
1246         else
1247         {
1248                 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1249                         e.nearestwaypoint = NULL;
1250
1251                 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1252                         && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1253                 {
1254                         if(IS_BOT_CLIENT(e) && e.goalcurrent && e.goalcurrent.classname == "waypoint")
1255                                 e.nearestwaypoint = nwp = e.goalcurrent;
1256                         else
1257                                 e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1258                         if(!nwp)
1259                         {
1260                                 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1261
1262                                 if(!e.navigation_dynamicgoal)
1263                                         e.blacklisted = true;
1264
1265                                 if(e.blacklisted)
1266                                 {
1267                                         LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1268                                         return;
1269                                 }
1270                         }
1271
1272                         if(e.navigation_dynamicgoal)
1273                                 e.nearestwaypointtimeout = time + 2;
1274                         else if(autocvar_g_waypointeditor)
1275                                 e.nearestwaypointtimeout = time + 3 + random() * 2;
1276                 }
1277                 nwp = e.nearestwaypoint;
1278         }
1279
1280         LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1281         if (nwp && nwp.wpcost < 10000000)
1282         {
1283                 //te_wizspike(nwp.wpnearestpoint);
1284                 float nwptoitem_cost = 0;
1285                 if(nwp.wpflags & WAYPOINTFLAG_TELEPORT)
1286                         nwptoitem_cost = nwp.wp00mincost;
1287                 else
1288                         nwptoitem_cost = waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1289                 float cost = nwp.wpcost + nwptoitem_cost;
1290                 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1291                 f = f * rangebias / (rangebias + cost);
1292                 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1293                 if (navigation_bestrating < f)
1294                 {
1295                         LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1296                         navigation_bestrating = f;
1297                         navigation_bestgoal = e;
1298                 }
1299         }
1300 }
1301
1302 // adds an item to the the goal stack with the path to a given item
1303 bool navigation_routetogoal(entity this, entity e, vector startposition)
1304 {
1305         // if there is no goal, just exit
1306         if (!e)
1307                 return false;
1308
1309         entity teleport_goal = NULL;
1310
1311         this.goalentity = e;
1312
1313         if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1314         {
1315                 // force teleport destination as route destination
1316                 teleport_goal = e;
1317                 navigation_pushroute(this, e.wp00);
1318                 this.goalentity = e.wp00;
1319         }
1320
1321         // put the entity on the goal stack
1322         //print("routetogoal ", etos(e), "\n");
1323         navigation_pushroute(this, e);
1324
1325         if(teleport_goal)
1326                 e = this.goalentity;
1327
1328         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1329         {
1330                 this.wp_goal_prev1 = this.wp_goal_prev0;
1331                 this.wp_goal_prev0 = e;
1332         }
1333
1334         if(g_jetpack)
1335         if(e==this.navigation_jetpack_goal)
1336                 return true;
1337
1338         // if it can reach the goal there is nothing more to do
1339         vector dest = '0 0 0';
1340         float dest_height = 0;
1341         SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1342         if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1343                 return true;
1344
1345         entity nearest_wp = NULL;
1346         // see if there are waypoints describing a path to the item
1347         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1348         {
1349                 e = e.nearestwaypoint;
1350                 nearest_wp = e;
1351         }
1352         else if(teleport_goal)
1353                 e = teleport_goal;
1354         else
1355                 e = e.enemy; // we already have added it, so...
1356
1357         if(e == NULL)
1358                 return false;
1359
1360         if(nearest_wp && nearest_wp.enemy)
1361         {
1362                 // often path can be optimized by not adding the nearest waypoint
1363                 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1364                 {
1365                         SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1366                         if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1367                         if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1368                                 e = nearest_wp.enemy;
1369                 }
1370                 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1371                         e = nearest_wp.enemy;
1372         }
1373
1374         for (;;)
1375         {
1376                 // add the spawnfunc_waypoint to the path
1377                 navigation_pushroute(this, e);
1378                 e = e.enemy;
1379
1380                 if(e==NULL)
1381                         break;
1382         }
1383
1384         return false;
1385 }
1386
1387 // removes any currently touching waypoints from the goal stack
1388 // (this is how bots detect if they reached a goal)
1389 int navigation_poptouchedgoals(entity this)
1390 {
1391         int removed_goals = 0;
1392
1393         if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1394         {
1395                 navigation_poproute(this);
1396                 ++removed_goals;
1397         }
1398
1399         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1400         {
1401                 // make sure jumppad is really hit, don't rely on distance based checks
1402                 // as they may report a touch even if it didn't really happen
1403                 if(this.lastteleporttime > 0
1404                         && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1405                 {
1406                         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1407                         if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1408                         {
1409                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1410                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1411                         }
1412                         navigation_poproute(this);
1413                         this.lastteleporttime = 0;
1414                         ++removed_goals;
1415                 }
1416                 else
1417                         return removed_goals;
1418         }
1419
1420         // If for some reason the bot is closer to the next goal, pop the current one
1421         if(this.goalstack01 && !wasfreed(this.goalstack01))
1422         if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1423         if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1424         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1425         {
1426                 // this can happen when a player stands still or is frozen
1427                 // NOTE: this is only needed because tracewalk always fail when destination
1428                 // point is on a player and bot_navigation_ignoreplayers is false
1429                 if (this.goalentity.navigation_dynamicgoal
1430                         && boxesoverlap(this.absmin, this.absmax, this.goalentity.absmin, this.goalentity.absmax))
1431                 {
1432                         navigation_poproute(this);
1433                         ++removed_goals;
1434                         return removed_goals;
1435                 }
1436
1437                 vector dest = '0 0 0';
1438                 float dest_height = 0;
1439                 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1440                 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1441                 {
1442                         LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1443                         navigation_poproute(this);
1444                         ++removed_goals;
1445                         if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1446                                 return removed_goals;
1447                         // TODO this may also be a nice idea to do "early" (e.g. by
1448                         // manipulating the vlen() comparisons) to shorten paths in
1449                         // general - this would make bots walk more "on rails" than
1450                         // "zigzagging" which they currently do with sufficiently
1451                         // random-like waypoints, and thus can make a nice bot
1452                         // personality property
1453                 }
1454         }
1455
1456         // Loose goal touching check when running
1457         if(this.aistatus & AI_STATUS_RUNNING)
1458         if(this.goalcurrent.classname=="waypoint")
1459         if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1460         {
1461                 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1462                 {
1463                         traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1464                         if(trace_fraction==1)
1465                         {
1466                                 // Detect personal waypoints
1467                                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1468                                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1469                                 {
1470                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1471                                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1472                                 }
1473
1474                                 navigation_poproute(this);
1475                                 ++removed_goals;
1476                                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1477                                         return removed_goals;
1478                         }
1479                 }
1480         }
1481
1482         while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1483         {
1484                 vector gc_min = this.goalcurrent.absmin;
1485                 vector gc_max = this.goalcurrent.absmax;
1486                 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1487                 {
1488                         gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1489                         gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1490                 }
1491                 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1492                         break;
1493
1494                 // Detect personal waypoints
1495                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1496                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1497                 {
1498                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1499                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1500                 }
1501
1502                 navigation_poproute(this);
1503                 ++removed_goals;
1504                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1505                         return removed_goals;
1506         }
1507         return removed_goals;
1508 }
1509
1510 entity navigation_get_really_close_waypoint(entity this)
1511 {
1512         entity wp = this.goalcurrent;
1513         if(!wp || vdist(wp.origin - this.origin, >, 50))
1514                 wp = this.goalcurrent_prev;
1515         if(!wp)
1516                 return NULL;
1517         if(wp.classname != "waypoint")
1518         {
1519                 wp = wp.nearestwaypoint;
1520                 if(!wp)
1521                         return NULL;
1522         }
1523         if(vdist(wp.origin - this.origin, >, 50))
1524         {
1525                 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1526                 {
1527                         if(vdist(it.origin - this.origin, <, 50))
1528                         {
1529                                 wp = it;
1530                                 break;
1531                         }
1532                 });
1533         }
1534         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1535                 return NULL;
1536
1537         vector dest = '0 0 0';
1538         float dest_height = 0;
1539         SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1540         if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1541                 return NULL;
1542         return wp;
1543 }
1544
1545 // begin a goal selection session (queries spawnfunc_waypoint network)
1546 void navigation_goalrating_start(entity this)
1547 {
1548         if(this.aistatus & AI_STATUS_STUCK)
1549                 return;
1550
1551         this.navigation_jetpack_goal = NULL;
1552         navigation_bestrating = -1;
1553         entity wp = navigation_get_really_close_waypoint(this);
1554         navigation_clearroute(this);
1555         navigation_bestgoal = NULL;
1556         navigation_markroutes(this, wp);
1557 }
1558
1559 // ends a goal selection session (updates goal stack to the best goal)
1560 void navigation_goalrating_end(entity this)
1561 {
1562         if(this.aistatus & AI_STATUS_STUCK)
1563                 return;
1564
1565         navigation_routetogoal(this, navigation_bestgoal, this.origin);
1566         LOG_DEBUG("best goal ", this.goalcurrent.classname);
1567
1568         // If the bot got stuck then try to reach the farthest waypoint
1569         if (!this.goalentity && autocvar_bot_wander_enable)
1570         {
1571                 if (!(this.aistatus & AI_STATUS_STUCK))
1572                 {
1573                         LOG_DEBUG(this.netname, " cannot walk to any goal");
1574                         this.aistatus |= AI_STATUS_STUCK;
1575                 }
1576         }
1577 }
1578
1579 void botframe_updatedangerousobjects(float maxupdate)
1580 {
1581         vector m1, m2, v, o;
1582         float c, d, danger;
1583         c = 0;
1584         entity wp_cur;
1585         IL_EACH(g_waypoints, true,
1586         {
1587                 danger = 0;
1588                 m1 = it.absmin;
1589                 m2 = it.absmax;
1590                 wp_cur = it;
1591                 IL_EACH(g_bot_dodge, it.bot_dodge,
1592                 {
1593                         v = it.origin;
1594                         v.x = bound(m1_x, v.x, m2_x);
1595                         v.y = bound(m1_y, v.y, m2_y);
1596                         v.z = bound(m1_z, v.z, m2_z);
1597                         o = (it.absmin + it.absmax) * 0.5;
1598                         d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1599                         if (d > 0)
1600                         {
1601                                 traceline(o, v, true, NULL);
1602                                 if (trace_fraction == 1)
1603                                         danger = danger + d;
1604                         }
1605                 });
1606                 it.dmg = danger;
1607                 c = c + 1;
1608                 if (c >= maxupdate)
1609                         break;
1610         });
1611 }
1612
1613 void navigation_unstuck(entity this)
1614 {
1615         float search_radius = 1000;
1616
1617         if (!autocvar_bot_wander_enable)
1618                 return;
1619
1620         if (!bot_waypoint_queue_owner)
1621         {
1622                 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1623                 bot_waypoint_queue_owner = this;
1624                 bot_waypoint_queue_bestgoal = NULL;
1625                 bot_waypoint_queue_bestgoalrating = 0;
1626         }
1627
1628         if(bot_waypoint_queue_owner!=this)
1629                 return;
1630
1631         if (bot_waypoint_queue_goal)
1632         {
1633                 // evaluate the next goal on the queue
1634                 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1635                 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1636                 vector dest = '0 0 0';
1637                 float dest_height = 0;
1638                 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1639                 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1640                 {
1641                         if( d > bot_waypoint_queue_bestgoalrating)
1642                         {
1643                                 bot_waypoint_queue_bestgoalrating = d;
1644                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1645                         }
1646                 }
1647                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1648
1649                 if (!bot_waypoint_queue_goal)
1650                 {
1651                         if (bot_waypoint_queue_bestgoal)
1652                         {
1653                                 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1654                                 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1655                                 navigation_goalrating_timeout_set(this);
1656                                 this.aistatus &= ~AI_STATUS_STUCK;
1657                         }
1658                         else
1659                         {
1660                                 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1661                         }
1662
1663                         bot_waypoint_queue_owner = NULL;
1664                 }
1665         }
1666         else
1667         {
1668                 if(bot_strategytoken!=this)
1669                         return;
1670
1671                 // build a new queue
1672                 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1673
1674                 entity first = NULL;
1675
1676                 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1677                 {
1678                         if(bot_waypoint_queue_goal)
1679                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1680                         else
1681                                 first = it;
1682
1683                         bot_waypoint_queue_goal = it;
1684                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1685                 });
1686
1687                 if (first)
1688                         bot_waypoint_queue_goal = first;
1689                 else
1690                 {
1691                         LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1692                         bot_waypoint_queue_owner = NULL;
1693                 }
1694         }
1695 }
1696
1697 // Support for debugging tracewalk visually
1698
1699 void debugresetnodes()
1700 {
1701         debuglastnode = '0 0 0';
1702 }
1703
1704 void debugnode(entity this, vector node)
1705 {
1706         if (!IS_PLAYER(this))
1707                 return;
1708
1709         if(debuglastnode=='0 0 0')
1710         {
1711                 debuglastnode = node;
1712                 return;
1713         }
1714
1715         te_lightning2(NULL, node, debuglastnode);
1716         debuglastnode = node;
1717 }
1718
1719 void debugnodestatus(vector position, float status)
1720 {
1721         vector c;
1722
1723         switch (status)
1724         {
1725                 case DEBUG_NODE_SUCCESS:
1726                         c = '0 15 0';
1727                         break;
1728                 case DEBUG_NODE_WARNING:
1729                         c = '15 15 0';
1730                         break;
1731                 case DEBUG_NODE_FAIL:
1732                         c = '15 0 0';
1733                         break;
1734                 default:
1735                         c = '15 15 15';
1736         }
1737
1738         te_customflash(position, 40,  2, c);
1739 }
1740
1741 // Support for debugging the goal stack visually
1742
1743 .float goalcounter;
1744 .vector lastposition;
1745
1746 // Debug the goal stack visually
1747 void debuggoalstack(entity this)
1748 {
1749         entity goal;
1750         vector org, go;
1751
1752         if(this.goalcounter==0)goal=this.goalcurrent;
1753         else if(this.goalcounter==1)goal=this.goalstack01;
1754         else if(this.goalcounter==2)goal=this.goalstack02;
1755         else if(this.goalcounter==3)goal=this.goalstack03;
1756         else if(this.goalcounter==4)goal=this.goalstack04;
1757         else if(this.goalcounter==5)goal=this.goalstack05;
1758         else if(this.goalcounter==6)goal=this.goalstack06;
1759         else if(this.goalcounter==7)goal=this.goalstack07;
1760         else if(this.goalcounter==8)goal=this.goalstack08;
1761         else if(this.goalcounter==9)goal=this.goalstack09;
1762         else if(this.goalcounter==10)goal=this.goalstack10;
1763         else if(this.goalcounter==11)goal=this.goalstack11;
1764         else if(this.goalcounter==12)goal=this.goalstack12;
1765         else if(this.goalcounter==13)goal=this.goalstack13;
1766         else if(this.goalcounter==14)goal=this.goalstack14;
1767         else if(this.goalcounter==15)goal=this.goalstack15;
1768         else if(this.goalcounter==16)goal=this.goalstack16;
1769         else if(this.goalcounter==17)goal=this.goalstack17;
1770         else if(this.goalcounter==18)goal=this.goalstack18;
1771         else if(this.goalcounter==19)goal=this.goalstack19;
1772         else if(this.goalcounter==20)goal=this.goalstack20;
1773         else if(this.goalcounter==21)goal=this.goalstack21;
1774         else if(this.goalcounter==22)goal=this.goalstack22;
1775         else if(this.goalcounter==23)goal=this.goalstack23;
1776         else if(this.goalcounter==24)goal=this.goalstack24;
1777         else if(this.goalcounter==25)goal=this.goalstack25;
1778         else if(this.goalcounter==26)goal=this.goalstack26;
1779         else if(this.goalcounter==27)goal=this.goalstack27;
1780         else if(this.goalcounter==28)goal=this.goalstack28;
1781         else if(this.goalcounter==29)goal=this.goalstack29;
1782         else if(this.goalcounter==30)goal=this.goalstack30;
1783         else if(this.goalcounter==31)goal=this.goalstack31;
1784         else goal=NULL;
1785
1786         if(goal==NULL)
1787         {
1788                 this.goalcounter = 0;
1789                 this.lastposition='0 0 0';
1790                 return;
1791         }
1792
1793         if(this.lastposition=='0 0 0')
1794                 org = this.origin;
1795         else
1796                 org = this.lastposition;
1797
1798
1799         go = ( goal.absmin + goal.absmax ) * 0.5;
1800         te_lightning2(NULL, org, go);
1801         this.lastposition = go;
1802
1803         this.goalcounter++;
1804 }