]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/navigation.qc
Bot AI: make bots aware of items that can be reached by a standard jumppad trajectory...
[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         //print("bot ", etos(this), " clear\n");
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                 this.goalentity = NULL;
725         this.goalcurrent = this.goalstack01;
726         this.goalstack01 = this.goalstack02;
727         this.goalstack02 = this.goalstack03;
728         this.goalstack03 = this.goalstack04;
729         this.goalstack04 = this.goalstack05;
730         this.goalstack05 = this.goalstack06;
731         this.goalstack06 = this.goalstack07;
732         this.goalstack07 = this.goalstack08;
733         this.goalstack08 = this.goalstack09;
734         this.goalstack09 = this.goalstack10;
735         this.goalstack10 = this.goalstack11;
736         this.goalstack11 = this.goalstack12;
737         this.goalstack12 = this.goalstack13;
738         this.goalstack13 = this.goalstack14;
739         this.goalstack14 = this.goalstack15;
740         this.goalstack15 = this.goalstack16;
741         this.goalstack16 = this.goalstack17;
742         this.goalstack17 = this.goalstack18;
743         this.goalstack18 = this.goalstack19;
744         this.goalstack19 = this.goalstack20;
745         this.goalstack20 = this.goalstack21;
746         this.goalstack21 = this.goalstack22;
747         this.goalstack22 = this.goalstack23;
748         this.goalstack23 = this.goalstack24;
749         this.goalstack24 = this.goalstack25;
750         this.goalstack25 = this.goalstack26;
751         this.goalstack26 = this.goalstack27;
752         this.goalstack27 = this.goalstack28;
753         this.goalstack28 = this.goalstack29;
754         this.goalstack29 = this.goalstack30;
755         this.goalstack30 = this.goalstack31;
756         this.goalstack31 = NULL;
757 }
758
759 // walking to wp (walkfromwp == false) v2 and v2_height will be used as
760 // waypoint destination coordinates instead of v (only useful for box waypoints)
761 // for normal waypoints v2 == v and v2_height == 0
762 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)
763 {
764         if (vdist(v - org, <, bestdist))
765         {
766                 traceline(v, org, true, ent);
767                 if (trace_fraction == 1)
768                 {
769                         if (walkfromwp)
770                         {
771                                 if (tracewalk(ent, v, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
772                                         return true;
773                         }
774                         else
775                         {
776                                 if (tracewalk(ent, org, PL_MIN_CONST, PL_MAX_CONST, o2, o2_height, bot_navigation_movemode))
777                                         return true;
778                         }
779                 }
780         }
781         return false;
782 }
783
784 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
785 entity navigation_findnearestwaypoint_withdist_except(entity ent, float walkfromwp, float bestdist, entity except)
786 {
787         if(ent.tag_entity)
788                 ent = ent.tag_entity;
789
790         vector pm1 = ent.origin + ent.mins;
791         vector pm2 = ent.origin + ent.maxs;
792
793         // do two scans, because box test is cheaper
794         IL_EACH(g_waypoints, it != ent && it != except,
795         {
796                 if(boxesoverlap(pm1, pm2, it.absmin, it.absmax))
797                 {
798                         if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
799                         {
800                                 waypoint_clearlinks(ent); // initialize wpXXmincost fields
801                                 navigation_item_addlink(it, ent);
802                         }
803                         return it;
804                 }
805         });
806
807         vector org = ent.origin;
808         if (navigation_testtracewalk)
809                 te_plasmaburn(org);
810
811         entity best = NULL;
812         vector v = '0 0 0', v2 = '0 0 0';
813         float v2_height = 0;
814
815         if(ent.size && !IS_PLAYER(ent))
816         {
817                 org += 0.5 * (ent.mins + ent.maxs);
818                 org.z = ent.origin.z + ent.mins.z - PL_MIN_CONST.z; // player height
819         }
820
821         if(!autocvar_g_waypointeditor && walkfromwp && !ent.navigation_dynamicgoal)
822         {
823                 waypoint_clearlinks(ent); // initialize wpXXmincost fields
824                 IL_EACH(g_waypoints, it != ent,
825                 {
826                         if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
827                                 continue;
828
829                         SET_TRACEWALK_DESTCOORDS(ent, it.origin, v2, v2_height);
830                         if(vdist(v2 - it.origin, <, 1050))
831                         if(tracewalk(ent, it.origin, PL_MIN_CONST, PL_MAX_CONST, v2, v2_height, bot_navigation_movemode))
832                                 navigation_item_addlink(it, ent);
833                 });
834         }
835
836         // box check failed, try walk
837         IL_EACH(g_waypoints, it != ent,
838         {
839                 if(walkfromwp && (it.wpflags & WAYPOINTFLAG_NORELINK))
840                         continue;
841                 v = it.origin;
842                 if(walkfromwp)
843                         SET_TRACEWALK_DESTCOORDS(ent, v, v2, v2_height);
844                 else
845                         SET_TRACEWALK_DESTCOORDS(it, org, v2, v2_height);
846                 if(navigation_waypoint_will_link(v, org, ent, v2, v2_height, v2, v2_height, walkfromwp, bestdist))
847                 {
848                         bestdist = vlen(v - org);
849                         best = it;
850                 }
851         });
852         if(!best && !ent.navigation_dynamicgoal)
853         {
854                 int solid_save = ent.solid;
855                 ent.solid = SOLID_BSP;
856                 IL_EACH(g_jumppads, true,
857                 {
858                         if(trigger_push_test(it, ent))
859                         {
860                                 best = it.nearestwaypoint;
861                                 break;
862                         }
863                 });
864                 ent.solid = solid_save;
865         }
866         return best;
867 }
868 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
869 {
870         entity wp = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, NULL);
871         if (autocvar_g_waypointeditor_auto)
872         {
873                 entity wp2 = navigation_findnearestwaypoint_withdist_except(ent, walkfromwp, 1050, wp);
874                 if (wp && !wp2)
875                         wp.wpflags |= WAYPOINTFLAG_PROTECTED;
876         }
877         return wp;
878 }
879
880 // finds the waypoints near the bot initiating a navigation query
881 float navigation_markroutes_nearestwaypoints(entity this, float maxdist)
882 {
883         vector v = '0 0 0';
884         //navigation_testtracewalk = true;
885         int c = 0;
886         float v_height = 0;
887         IL_EACH(g_waypoints, !it.wpconsidered,
888         {
889                 SET_TRACEWALK_DESTCOORDS(it, this.origin, v, v_height);
890
891                 vector diff = v - this.origin;
892                 diff.z = max(0, diff.z);
893                 if(vdist(diff, <, maxdist))
894                 {
895                         it.wpconsidered = true;
896                         if (tracewalk(this, this.origin, this.mins, this.maxs, v, v_height, bot_navigation_movemode))
897                         {
898                                 it.wpnearestpoint = v;
899                                 it.wpcost = waypoint_gettravelcost(this.origin, v, this, it) + it.dmg;
900                                 it.wpfire = 1;
901                                 it.enemy = NULL;
902                                 c = c + 1;
903                         }
904                 }
905         });
906         //navigation_testtracewalk = false;
907         return c;
908 }
909
910 // updates a path link if a spawnfunc_waypoint link is better than the current one
911 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost, vector p)
912 {
913         vector m1, m2;
914         vector v;
915         if (wp.wpisbox)
916         {
917                 m1 = wp.origin + wp.mins;
918                 m2 = wp.origin + wp.maxs;
919                 v.x = bound(m1_x, p.x, m2_x);
920                 v.y = bound(m1_y, p.y, m2_y);
921                 v.z = bound(m1_z, p.z, m2_z);
922         }
923         else
924                 v = wp.origin;
925         if (w.wpflags & WAYPOINTFLAG_TELEPORT)
926                 cost += w.wp00mincost; // assuming teleport has exactly one destination
927         else
928                 cost += waypoint_gettravelcost(p, v, w, wp);
929         if (wp.wpcost > cost)
930         {
931                 wp.wpcost = cost;
932                 wp.enemy = w;
933                 wp.wpfire = 1;
934                 wp.wpnearestpoint = v;
935         }
936 }
937
938 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
939 void navigation_markroutes(entity this, entity fixed_source_waypoint)
940 {
941         float cost, cost2;
942         vector p;
943
944         IL_EACH(g_waypoints, true,
945         {
946                 it.wpconsidered = false;
947                 it.wpnearestpoint = '0 0 0';
948                 it.wpcost = 10000000;
949                 it.wpfire = 0;
950                 it.enemy = NULL;
951         });
952
953         if(fixed_source_waypoint)
954         {
955                 fixed_source_waypoint.wpconsidered = true;
956                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
957                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
958                 fixed_source_waypoint.wpfire = 1;
959                 fixed_source_waypoint.enemy = NULL;
960         }
961         else
962         {
963                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
964                 // as this search is expensive we will use lower values if the bot is on the air
965                 float increment, maxdistance;
966                 if(IS_ONGROUND(this))
967                 {
968                         increment = 750;
969                         maxdistance = 50000;
970                 }
971                 else
972                 {
973                         increment = 500;
974                         maxdistance = 1500;
975                 }
976
977                 for(int j = increment; !navigation_markroutes_nearestwaypoints(this, j) && j < maxdistance; j += increment);
978         }
979
980         bool searching = true;
981         while (searching)
982         {
983                 searching = false;
984                 IL_EACH(g_waypoints, it.wpfire,
985                 {
986                         searching = true;
987                         it.wpfire = 0;
988                         cost = it.wpcost;
989                         p = it.wpnearestpoint;
990                         entity wp;
991                         wp = it.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp00mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
992                         wp = it.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp01mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
993                         wp = it.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp02mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
994                         wp = it.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp03mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
995                         wp = it.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp04mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
996                         wp = it.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp05mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
997                         wp = it.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp06mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
998                         wp = it.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp07mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
999                         wp = it.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp08mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1000                         wp = it.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp09mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1001                         wp = it.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp10mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1002                         wp = it.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp11mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1003                         wp = it.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp12mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1004                         wp = it.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp13mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1005                         wp = it.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp14mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1006                         wp = it.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp15mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1007                         wp = it.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp16mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1008                         wp = it.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp17mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1009                         wp = it.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp18mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1010                         wp = it.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp19mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1011                         wp = it.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp20mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1012                         wp = it.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp21mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1013                         wp = it.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp22mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1014                         wp = it.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp23mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1015                         wp = it.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp24mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1016                         wp = it.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp25mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1017                         wp = it.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp26mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1018                         wp = it.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp27mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1019                         wp = it.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp28mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1020                         wp = it.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp29mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1021                         wp = it.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp30mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1022                         wp = it.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + it.wp31mincost) navigation_markroutes_checkwaypoint(it, wp, cost2, p);
1023                         }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1024                 });
1025         }
1026 }
1027
1028 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
1029 void navigation_markroutes_inverted(entity fixed_source_waypoint)
1030 {
1031         float cost, cost2;
1032         vector p;
1033         IL_EACH(g_waypoints, true,
1034         {
1035                 it.wpconsidered = false;
1036                 it.wpnearestpoint = '0 0 0';
1037                 it.wpcost = 10000000;
1038                 it.wpfire = 0;
1039                 it.enemy = NULL;
1040         });
1041
1042         if(fixed_source_waypoint)
1043         {
1044                 fixed_source_waypoint.wpconsidered = true;
1045                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
1046                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
1047                 fixed_source_waypoint.wpfire = 1;
1048                 fixed_source_waypoint.enemy = NULL;
1049         }
1050         else
1051         {
1052                 error("need to start with a waypoint\n");
1053         }
1054
1055         bool searching = true;
1056         while (searching)
1057         {
1058                 searching = false;
1059                 IL_EACH(g_waypoints, it.wpfire,
1060                 {
1061                         searching = true;
1062                         it.wpfire = 0;
1063                         cost = it.wpcost; // cost to walk from it to home
1064                         p = it.wpnearestpoint;
1065                         entity wp = it;
1066                         IL_EACH(g_waypoints, it != wp,
1067                         {
1068                                 if(!waypoint_islinked(it, wp))
1069                                         continue;
1070                                 cost2 = cost + it.dmg;
1071                                 navigation_markroutes_checkwaypoint(wp, it, cost2, p);
1072                         });
1073                 });
1074         }
1075 }
1076
1077 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1078 void navigation_routerating(entity this, entity e, float f, float rangebias)
1079 {
1080         if (!e)
1081                 return;
1082
1083         if(e.blacklisted)
1084                 return;
1085
1086         rangebias = waypoint_getlinearcost(rangebias);
1087         f = waypoint_getlinearcost(f);
1088
1089         if (IS_PLAYER(e))
1090         {
1091                 bool rate_wps = false;
1092                 if((e.flags & FL_INWATER) || (e.flags & FL_PARTIALGROUND))
1093                         rate_wps = true;
1094
1095                 if(!IS_ONGROUND(e))
1096                 {
1097                         traceline(e.origin, e.origin + '0 0 -1500', true, NULL);
1098                         int t = pointcontents(trace_endpos + '0 0 1');
1099                         if(t != CONTENT_SOLID )
1100                         {
1101                                 if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
1102                                         rate_wps = true;
1103                                 else if(tracebox_hits_trigger_hurt(e.origin, e.mins, e.maxs, trace_endpos))
1104                                         return;
1105                         }
1106                 }
1107
1108                 if(rate_wps)
1109                 {
1110                         entity theEnemy = e;
1111                         entity best_wp = NULL;
1112                         float best_dist = 10000;
1113                         IL_EACH(g_waypoints, vdist(it.origin - theEnemy.origin, <, 500)
1114                                 && vdist(it.origin - this.origin, >, 100)
1115                                 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1116                         {
1117                                 float dist = vlen(it.origin - theEnemy.origin);
1118                                 if (dist < best_dist)
1119                                 {
1120                                         best_wp = it;
1121                                         best_dist = dist;
1122                                 }
1123                         });
1124                         if (!best_wp)
1125                                 return;
1126                         e = best_wp;
1127                 }
1128         }
1129
1130         vector goal_org = (e.absmin + e.absmax) * 0.5;
1131
1132         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
1133
1134         // Evaluate path using jetpack
1135         if(g_jetpack)
1136         if(this.items & IT_JETPACK)
1137         if(autocvar_bot_ai_navigation_jetpack)
1138         if(vdist(this.origin - goal_org, >, autocvar_bot_ai_navigation_jetpack_mindistance))
1139         {
1140                 vector pointa, pointb;
1141
1142                 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname);
1143
1144                 // Point A
1145                 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
1146                 pointa = trace_endpos - '0 0 1';
1147
1148                 // Point B
1149                 traceline(goal_org, goal_org + '0 0 65535', MOVE_NORMAL, e);
1150                 pointb = trace_endpos - '0 0 1';
1151
1152                 // Can I see these two points from the sky?
1153                 traceline(pointa, pointb, MOVE_NORMAL, this);
1154
1155                 if(trace_fraction==1)
1156                 {
1157                         LOG_DEBUG("jetpack ai: can bridge these two points");
1158
1159                         // Lower the altitude of these points as much as possible
1160                         float zdistance, xydistance, cost, t, fuel;
1161                         vector down, npa, npb;
1162
1163                         down = '0 0 -1' * (STAT(PL_MAX, this).z - STAT(PL_MIN, this).z) * 10;
1164
1165                         do{
1166                                 npa = pointa + down;
1167                                 npb = pointb + down;
1168
1169                                 if(npa.z<=this.absmax.z)
1170                                         break;
1171
1172                                 if(npb.z<=e.absmax.z)
1173                                         break;
1174
1175                                 traceline(npa, npb, MOVE_NORMAL, this);
1176                                 if(trace_fraction==1)
1177                                 {
1178                                         pointa = npa;
1179                                         pointb = npb;
1180                                 }
1181                         }
1182                         while(trace_fraction == 1);
1183
1184
1185                         // Rough estimation of fuel consumption
1186                         // (ignores acceleration and current xyz velocity)
1187                         xydistance = vlen(pointa - pointb);
1188                         zdistance = fabs(pointa.z - this.origin.z);
1189
1190                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
1191                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
1192                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
1193
1194                         LOG_DEBUG("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel));
1195
1196                         // enough fuel ?
1197                         if(this.ammo_fuel>fuel)
1198                         {
1199                                 // Estimate cost
1200                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
1201                                 //  - between air and ground speeds)
1202
1203                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
1204                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
1205                                 cost *= 1.5;
1206
1207                                 // Compare against other goals
1208                                 f = f * rangebias / (rangebias + cost);
1209
1210                                 if (navigation_bestrating < f)
1211                                 {
1212                                         LOG_DEBUG("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1213                                         navigation_bestrating = f;
1214                                         navigation_bestgoal = e;
1215                                         this.navigation_jetpack_goal = e;
1216                                         this.navigation_jetpack_point = pointb;
1217                                 }
1218                                 return;
1219                         }
1220                 }
1221         }
1222
1223         entity nwp;
1224         //te_wizspike(e.origin);
1225         //bprint(etos(e));
1226         //bprint("\n");
1227         // update the cached spawnfunc_waypoint link on a dynamic item entity
1228         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1229         {
1230                 nwp = e;
1231         }
1232         else
1233         {
1234                 if(autocvar_g_waypointeditor && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1235                         e.nearestwaypoint = NULL;
1236
1237                 if ((!e.nearestwaypoint || e.navigation_dynamicgoal)
1238                         && e.nearestwaypointtimeout >= 0 && time > e.nearestwaypointtimeout)
1239                 {
1240                         e.nearestwaypoint = nwp = navigation_findnearestwaypoint(e, true);
1241                         if(!nwp)
1242                         {
1243                                 LOG_DEBUG("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e));
1244
1245                                 if(!e.navigation_dynamicgoal)
1246                                         e.blacklisted = true;
1247
1248                                 if(e.blacklisted)
1249                                 {
1250                                         LOG_DEBUG("The entity '", e.classname, "' is going to be excluded from path finding during this match");
1251                                         return;
1252                                 }
1253                         }
1254
1255                         if(e.navigation_dynamicgoal)
1256                                 e.nearestwaypointtimeout = time + 2;
1257                         else if(autocvar_g_waypointeditor)
1258                                 e.nearestwaypointtimeout = time + 3 + random() * 2;
1259                 }
1260                 nwp = e.nearestwaypoint;
1261         }
1262
1263         LOG_DEBUG("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")");
1264         if (nwp)
1265         if (nwp.wpcost < 10000000)
1266         {
1267                 //te_wizspike(nwp.wpnearestpoint);
1268                 float cost = nwp.wpcost + waypoint_gettravelcost(nwp.wpnearestpoint, goal_org, nwp, e);
1269                 LOG_DEBUG(e.classname, " ", ftos(f), "/(1+", ftos(cost), "/", ftos(rangebias), ") = ");
1270                 f = f * rangebias / (rangebias + cost);
1271                 LOG_DEBUG("considering ", e.classname, " (with rating ", ftos(f), ")");
1272                 if (navigation_bestrating < f)
1273                 {
1274                         LOG_DEBUG("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")");
1275                         navigation_bestrating = f;
1276                         navigation_bestgoal = e;
1277                 }
1278         }
1279 }
1280
1281 // adds an item to the the goal stack with the path to a given item
1282 bool navigation_routetogoal(entity this, entity e, vector startposition)
1283 {
1284         // if there is no goal, just exit
1285         if (!e)
1286                 return false;
1287
1288         entity teleport_goal = NULL;
1289
1290         this.goalentity = e;
1291
1292         if(e.wpflags & WAYPOINTFLAG_TELEPORT)
1293         {
1294                 // force teleport destination as route destination
1295                 teleport_goal = e;
1296                 navigation_pushroute(this, e.wp00);
1297                 this.goalentity = e.wp00;
1298         }
1299
1300         // put the entity on the goal stack
1301         //print("routetogoal ", etos(e), "\n");
1302         navigation_pushroute(this, e);
1303
1304         if(teleport_goal)
1305                 e = this.goalentity;
1306
1307         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
1308         {
1309                 this.wp_goal_prev1 = this.wp_goal_prev0;
1310                 this.wp_goal_prev0 = e;
1311         }
1312
1313         if(g_jetpack)
1314         if(e==this.navigation_jetpack_goal)
1315                 return true;
1316
1317         // if it can reach the goal there is nothing more to do
1318         vector dest = '0 0 0';
1319         float dest_height = 0;
1320         SET_TRACEWALK_DESTCOORDS(e, startposition, dest, dest_height);
1321         if (tracewalk(this, startposition, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1322                 return true;
1323
1324         entity nearest_wp = NULL;
1325         // see if there are waypoints describing a path to the item
1326         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
1327         {
1328                 e = e.nearestwaypoint;
1329                 nearest_wp = e;
1330         }
1331         else if(teleport_goal)
1332                 e = teleport_goal;
1333         else
1334                 e = e.enemy; // we already have added it, so...
1335
1336         if(e == NULL)
1337                 return false;
1338
1339         if(nearest_wp && nearest_wp.enemy)
1340         {
1341                 // often path can be optimized by not adding the nearest waypoint
1342                 if (this.goalentity.navigation_dynamicgoal || autocvar_g_waypointeditor)
1343                 {
1344                         SET_TRACEWALK_DESTCOORDS(this.goalentity, nearest_wp.enemy.origin, dest, dest_height);
1345                         if(vdist(dest - nearest_wp.enemy.origin, <, 1050))
1346                         if(tracewalk(this, nearest_wp.enemy.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1347                                 e = nearest_wp.enemy;
1348                 }
1349                 else if(navigation_item_islinked(nearest_wp.enemy, this.goalentity))
1350                         e = nearest_wp.enemy;
1351         }
1352
1353         for (;;)
1354         {
1355                 // add the spawnfunc_waypoint to the path
1356                 navigation_pushroute(this, e);
1357                 e = e.enemy;
1358
1359                 if(e==NULL)
1360                         break;
1361         }
1362
1363         return false;
1364 }
1365
1366 // removes any currently touching waypoints from the goal stack
1367 // (this is how bots detect if they reached a goal)
1368 int navigation_poptouchedgoals(entity this)
1369 {
1370         int removed_goals = 0;
1371
1372         if(IS_PLAYER(this.goalcurrent) && IS_DEAD(this.goalcurrent) && checkpvs(this.origin + this.view_ofs, this.goalcurrent))
1373         {
1374                 navigation_poproute(this);
1375                 ++removed_goals;
1376         }
1377
1378         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1379         {
1380                 // make sure jumppad is really hit, don't rely on distance based checks
1381                 // as they may report a touch even if it didn't really happen
1382                 if(this.lastteleporttime > 0
1383                         && time - this.lastteleporttime < ((this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL) ? 2 : 0.15))
1384                 {
1385                         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1386                         if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1387                         {
1388                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1389                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1390                         }
1391                         navigation_poproute(this);
1392                         this.lastteleporttime = 0;
1393                         ++removed_goals;
1394                 }
1395                 else
1396                         return removed_goals;
1397         }
1398
1399         // If for some reason the bot is closer to the next goal, pop the current one
1400         if(this.goalstack01 && !wasfreed(this.goalstack01))
1401         if(random() < 0.7) // randomness should help on certain hard paths with climbs and tight corners
1402         if(vlen2(this.goalcurrent.origin - this.goalstack01.origin) > vlen2(this.goalstack01.origin - this.origin))
1403         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
1404         {
1405                 vector dest = '0 0 0';
1406                 float dest_height = 0;
1407                 SET_TRACEWALK_DESTCOORDS(this.goalstack01, this.origin, dest, dest_height);
1408                 if(tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
1409                 {
1410                         LOG_DEBUG("path optimized for ", this.netname, ", removed a goal from the queue");
1411                         navigation_poproute(this);
1412                         ++removed_goals;
1413                         if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1414                                 return removed_goals;
1415                         // TODO this may also be a nice idea to do "early" (e.g. by
1416                         // manipulating the vlen() comparisons) to shorten paths in
1417                         // general - this would make bots walk more "on rails" than
1418                         // "zigzagging" which they currently do with sufficiently
1419                         // random-like waypoints, and thus can make a nice bot
1420                         // personality property
1421                 }
1422         }
1423
1424         // Loose goal touching check when running
1425         if(this.aistatus & AI_STATUS_RUNNING)
1426         if(this.goalcurrent.classname=="waypoint")
1427         if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
1428         {
1429                 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
1430                 {
1431                         traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
1432                         if(trace_fraction==1)
1433                         {
1434                                 // Detect personal waypoints
1435                                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1436                                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1437                                 {
1438                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1439                                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1440                                 }
1441
1442                                 navigation_poproute(this);
1443                                 ++removed_goals;
1444                                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1445                                         return removed_goals;
1446                         }
1447                 }
1448         }
1449
1450         while (this.goalcurrent && !IS_PLAYER(this.goalcurrent))
1451         {
1452                 vector gc_min = this.goalcurrent.absmin;
1453                 vector gc_max = this.goalcurrent.absmax;
1454                 if(this.goalcurrent.classname == "waypoint" && !this.goalcurrent.wpisbox)
1455                 {
1456                         gc_min = this.goalcurrent.origin - '1 1 1' * 12;
1457                         gc_max = this.goalcurrent.origin + '1 1 1' * 12;
1458                 }
1459                 if(!boxesoverlap(this.absmin, this.absmax, gc_min, gc_max))
1460                         break;
1461
1462                 // Detect personal waypoints
1463                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1464                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
1465                 {
1466                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1467                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1468                 }
1469
1470                 navigation_poproute(this);
1471                 ++removed_goals;
1472                 if(this.goalcurrent && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1473                         return removed_goals;
1474         }
1475         return removed_goals;
1476 }
1477
1478 entity navigation_get_really_close_waypoint(entity this)
1479 {
1480         entity wp = this.goalcurrent;
1481         if(!wp || vdist(wp.origin - this.origin, >, 50))
1482                 wp = this.goalcurrent_prev;
1483         if(!wp)
1484                 return NULL;
1485         if(wp.classname != "waypoint")
1486         {
1487                 wp = wp.nearestwaypoint;
1488                 if(!wp)
1489                         return NULL;
1490         }
1491         if(vdist(wp.origin - this.origin, >, 50))
1492         {
1493                 IL_EACH(g_waypoints, !(it.wpflags & WAYPOINTFLAG_TELEPORT),
1494                 {
1495                         if(vdist(it.origin - this.origin, <, 50))
1496                         {
1497                                 wp = it;
1498                                 break;
1499                         }
1500                 });
1501         }
1502         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1503                 return NULL;
1504
1505         vector dest = '0 0 0';
1506         float dest_height = 0;
1507         SET_TRACEWALK_DESTCOORDS(wp, this.origin, dest, dest_height);
1508         if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1509                 return NULL;
1510         return wp;
1511 }
1512
1513 // begin a goal selection session (queries spawnfunc_waypoint network)
1514 void navigation_goalrating_start(entity this)
1515 {
1516         if(this.aistatus & AI_STATUS_STUCK)
1517                 return;
1518
1519         this.navigation_jetpack_goal = NULL;
1520         navigation_bestrating = -1;
1521         entity wp = navigation_get_really_close_waypoint(this);
1522         navigation_clearroute(this);
1523         navigation_bestgoal = NULL;
1524         navigation_markroutes(this, wp);
1525 }
1526
1527 // ends a goal selection session (updates goal stack to the best goal)
1528 void navigation_goalrating_end(entity this)
1529 {
1530         if(this.aistatus & AI_STATUS_STUCK)
1531                 return;
1532
1533         navigation_routetogoal(this, navigation_bestgoal, this.origin);
1534         LOG_DEBUG("best goal ", this.goalcurrent.classname);
1535
1536         // If the bot got stuck then try to reach the farthest waypoint
1537         if (!this.goalentity && autocvar_bot_wander_enable)
1538         {
1539                 if (!(this.aistatus & AI_STATUS_STUCK))
1540                 {
1541                         LOG_DEBUG(this.netname, " cannot walk to any goal");
1542                         this.aistatus |= AI_STATUS_STUCK;
1543                 }
1544         }
1545 }
1546
1547 void botframe_updatedangerousobjects(float maxupdate)
1548 {
1549         vector m1, m2, v, o;
1550         float c, d, danger;
1551         c = 0;
1552         entity wp_cur;
1553         IL_EACH(g_waypoints, true,
1554         {
1555                 danger = 0;
1556                 m1 = it.absmin;
1557                 m2 = it.absmax;
1558                 wp_cur = it;
1559                 IL_EACH(g_bot_dodge, it.bot_dodge,
1560                 {
1561                         v = it.origin;
1562                         v.x = bound(m1_x, v.x, m2_x);
1563                         v.y = bound(m1_y, v.y, m2_y);
1564                         v.z = bound(m1_z, v.z, m2_z);
1565                         o = (it.absmin + it.absmax) * 0.5;
1566                         d = waypoint_getlinearcost(it.bot_dodgerating) - waypoint_gettravelcost(o, v, it, wp_cur);
1567                         if (d > 0)
1568                         {
1569                                 traceline(o, v, true, NULL);
1570                                 if (trace_fraction == 1)
1571                                         danger = danger + d;
1572                         }
1573                 });
1574                 it.dmg = danger;
1575                 c = c + 1;
1576                 if (c >= maxupdate)
1577                         break;
1578         });
1579 }
1580
1581 void navigation_unstuck(entity this)
1582 {
1583         float search_radius = 1000;
1584
1585         if (!autocvar_bot_wander_enable)
1586                 return;
1587
1588         if (!bot_waypoint_queue_owner)
1589         {
1590                 LOG_DEBUG(this.netname, " stuck, taking over the waypoints queue");
1591                 bot_waypoint_queue_owner = this;
1592                 bot_waypoint_queue_bestgoal = NULL;
1593                 bot_waypoint_queue_bestgoalrating = 0;
1594         }
1595
1596         if(bot_waypoint_queue_owner!=this)
1597                 return;
1598
1599         if (bot_waypoint_queue_goal)
1600         {
1601                 // evaluate the next goal on the queue
1602                 float d = vlen2(this.origin - bot_waypoint_queue_goal.origin);
1603                 LOG_DEBUG(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d));
1604                 vector dest = '0 0 0';
1605                 float dest_height = 0;
1606                 SET_TRACEWALK_DESTCOORDS(bot_waypoint_queue_goal, this.origin, dest, dest_height);
1607                 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this), dest, dest_height, bot_navigation_movemode))
1608                 {
1609                         if( d > bot_waypoint_queue_bestgoalrating)
1610                         {
1611                                 bot_waypoint_queue_bestgoalrating = d;
1612                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1613                         }
1614                 }
1615                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1616
1617                 if (!bot_waypoint_queue_goal)
1618                 {
1619                         if (bot_waypoint_queue_bestgoal)
1620                         {
1621                                 LOG_DEBUG(this.netname, " stuck, reachable waypoint found, heading to it");
1622                                 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1623                                 navigation_goalrating_timeout_set(this);
1624                                 this.aistatus &= ~AI_STATUS_STUCK;
1625                         }
1626                         else
1627                         {
1628                                 LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1629                         }
1630
1631                         bot_waypoint_queue_owner = NULL;
1632                 }
1633         }
1634         else
1635         {
1636                 if(bot_strategytoken!=this)
1637                         return;
1638
1639                 // build a new queue
1640                 LOG_DEBUG(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu");
1641
1642                 entity first = NULL;
1643
1644                 FOREACH_ENTITY_RADIUS(this.origin, search_radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
1645                 {
1646                         if(bot_waypoint_queue_goal)
1647                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = it;
1648                         else
1649                                 first = it;
1650
1651                         bot_waypoint_queue_goal = it;
1652                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1653                 });
1654
1655                 if (first)
1656                         bot_waypoint_queue_goal = first;
1657                 else
1658                 {
1659                         LOG_DEBUG(this.netname, " stuck, cannot walk to any waypoint at all");
1660                         bot_waypoint_queue_owner = NULL;
1661                 }
1662         }
1663 }
1664
1665 // Support for debugging tracewalk visually
1666
1667 void debugresetnodes()
1668 {
1669         debuglastnode = '0 0 0';
1670 }
1671
1672 void debugnode(entity this, vector node)
1673 {
1674         if (!IS_PLAYER(this))
1675                 return;
1676
1677         if(debuglastnode=='0 0 0')
1678         {
1679                 debuglastnode = node;
1680                 return;
1681         }
1682
1683         te_lightning2(NULL, node, debuglastnode);
1684         debuglastnode = node;
1685 }
1686
1687 void debugnodestatus(vector position, float status)
1688 {
1689         vector c;
1690
1691         switch (status)
1692         {
1693                 case DEBUG_NODE_SUCCESS:
1694                         c = '0 15 0';
1695                         break;
1696                 case DEBUG_NODE_WARNING:
1697                         c = '15 15 0';
1698                         break;
1699                 case DEBUG_NODE_FAIL:
1700                         c = '15 0 0';
1701                         break;
1702                 default:
1703                         c = '15 15 15';
1704         }
1705
1706         te_customflash(position, 40,  2, c);
1707 }
1708
1709 // Support for debugging the goal stack visually
1710
1711 .float goalcounter;
1712 .vector lastposition;
1713
1714 // Debug the goal stack visually
1715 void debuggoalstack(entity this)
1716 {
1717         entity goal;
1718         vector org, go;
1719
1720         if(this.goalcounter==0)goal=this.goalcurrent;
1721         else if(this.goalcounter==1)goal=this.goalstack01;
1722         else if(this.goalcounter==2)goal=this.goalstack02;
1723         else if(this.goalcounter==3)goal=this.goalstack03;
1724         else if(this.goalcounter==4)goal=this.goalstack04;
1725         else if(this.goalcounter==5)goal=this.goalstack05;
1726         else if(this.goalcounter==6)goal=this.goalstack06;
1727         else if(this.goalcounter==7)goal=this.goalstack07;
1728         else if(this.goalcounter==8)goal=this.goalstack08;
1729         else if(this.goalcounter==9)goal=this.goalstack09;
1730         else if(this.goalcounter==10)goal=this.goalstack10;
1731         else if(this.goalcounter==11)goal=this.goalstack11;
1732         else if(this.goalcounter==12)goal=this.goalstack12;
1733         else if(this.goalcounter==13)goal=this.goalstack13;
1734         else if(this.goalcounter==14)goal=this.goalstack14;
1735         else if(this.goalcounter==15)goal=this.goalstack15;
1736         else if(this.goalcounter==16)goal=this.goalstack16;
1737         else if(this.goalcounter==17)goal=this.goalstack17;
1738         else if(this.goalcounter==18)goal=this.goalstack18;
1739         else if(this.goalcounter==19)goal=this.goalstack19;
1740         else if(this.goalcounter==20)goal=this.goalstack20;
1741         else if(this.goalcounter==21)goal=this.goalstack21;
1742         else if(this.goalcounter==22)goal=this.goalstack22;
1743         else if(this.goalcounter==23)goal=this.goalstack23;
1744         else if(this.goalcounter==24)goal=this.goalstack24;
1745         else if(this.goalcounter==25)goal=this.goalstack25;
1746         else if(this.goalcounter==26)goal=this.goalstack26;
1747         else if(this.goalcounter==27)goal=this.goalstack27;
1748         else if(this.goalcounter==28)goal=this.goalstack28;
1749         else if(this.goalcounter==29)goal=this.goalstack29;
1750         else if(this.goalcounter==30)goal=this.goalstack30;
1751         else if(this.goalcounter==31)goal=this.goalstack31;
1752         else goal=NULL;
1753
1754         if(goal==NULL)
1755         {
1756                 this.goalcounter = 0;
1757                 this.lastposition='0 0 0';
1758                 return;
1759         }
1760
1761         if(this.lastposition=='0 0 0')
1762                 org = this.origin;
1763         else
1764                 org = this.lastposition;
1765
1766
1767         go = ( goal.absmin + goal.absmax ) * 0.5;
1768         te_lightning2(NULL, org, go);
1769         this.lastposition = go;
1770
1771         this.goalcounter++;
1772 }