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