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