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