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