]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/navigation.qc
8deefdb39716c527be0bc1e36512cccfb07471cf
[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 bool tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
19 {
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(e, 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(e, 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(e, 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 + e.maxs.z; org.z += stepdist)
117                                 {
118                                                 if(autocvar_bot_debug_tracewalk)
119                                                         debugnode(e, 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(e, 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 = NULL;
233         this.goalstack01 = NULL;
234         this.goalstack02 = NULL;
235         this.goalstack03 = NULL;
236         this.goalstack04 = NULL;
237         this.goalstack05 = NULL;
238         this.goalstack06 = NULL;
239         this.goalstack07 = NULL;
240         this.goalstack08 = NULL;
241         this.goalstack09 = NULL;
242         this.goalstack10 = NULL;
243         this.goalstack11 = NULL;
244         this.goalstack12 = NULL;
245         this.goalstack13 = NULL;
246         this.goalstack14 = NULL;
247         this.goalstack15 = NULL;
248         this.goalstack16 = NULL;
249         this.goalstack17 = NULL;
250         this.goalstack18 = NULL;
251         this.goalstack19 = NULL;
252         this.goalstack20 = NULL;
253         this.goalstack21 = NULL;
254         this.goalstack22 = NULL;
255         this.goalstack23 = NULL;
256         this.goalstack24 = NULL;
257         this.goalstack25 = NULL;
258         this.goalstack26 = NULL;
259         this.goalstack27 = NULL;
260         this.goalstack28 = NULL;
261         this.goalstack29 = NULL;
262         this.goalstack30 = NULL;
263         this.goalstack31 = NULL;
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 = NULL;
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 = NULL;
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, NULL);
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                                 if (tracewalk(this, this.origin, this.mins, this.maxs, v, bot_navigation_movemode))
474                                 {
475                                         head.wpnearestpoint = v;
476                                         head.wpcost = vlen(v - this.origin) + head.dmg;
477                                         head.wpfire = 1;
478                                         head.enemy = NULL;
479                                         c = c + 1;
480                                 }
481                         }
482                 }
483                 head = head.chain;
484         }
485         //navigation_testtracewalk = false;
486         return c;
487 }
488
489 // updates a path link if a spawnfunc_waypoint link is better than the current one
490 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
491 {
492         vector m1;
493         vector m2;
494         vector v;
495         if (wp.wpisbox)
496         {
497                 m1 = wp.absmin;
498                 m2 = wp.absmax;
499                 v.x = bound(m1_x, p.x, m2_x);
500                 v.y = bound(m1_y, p.y, m2_y);
501                 v.z = bound(m1_z, p.z, m2_z);
502         }
503         else
504                 v = wp.origin;
505         cost2 = cost2 + vlen(v - p);
506         if (wp.wpcost > cost2)
507         {
508                 wp.wpcost = cost2;
509                 wp.enemy = w;
510                 wp.wpfire = 1;
511                 wp.wpnearestpoint = v;
512         }
513 }
514
515 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
516 void navigation_markroutes(entity this, entity fixed_source_waypoint)
517 {
518         entity w, wp, waylist;
519         float searching, cost, cost2;
520         vector p;
521         w = waylist = findchain(classname, "waypoint");
522         while (w)
523         {
524                 w.wpconsidered = false;
525                 w.wpnearestpoint = '0 0 0';
526                 w.wpcost = 10000000;
527                 w.wpfire = 0;
528                 w.enemy = NULL;
529                 w = w.chain;
530         }
531
532         if(fixed_source_waypoint)
533         {
534                 fixed_source_waypoint.wpconsidered = true;
535                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
536                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
537                 fixed_source_waypoint.wpfire = 1;
538                 fixed_source_waypoint.enemy = NULL;
539         }
540         else
541         {
542                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
543                 // as this search is expensive we will use lower values if the bot is on the air
544                 float i, increment, maxdistance;
545                 if(IS_ONGROUND(this))
546                 {
547                         increment = 750;
548                         maxdistance = 50000;
549                 }
550                 else
551                 {
552                         increment = 500;
553                         maxdistance = 1500;
554                 }
555
556                 for(i=increment;!navigation_markroutes_nearestwaypoints(this, waylist, i)&&i<maxdistance;i+=increment);
557         }
558
559         searching = true;
560         while (searching)
561         {
562                 searching = false;
563                 w = waylist;
564                 while (w)
565                 {
566                         if (w.wpfire)
567                         {
568                                 searching = true;
569                                 w.wpfire = 0;
570                                 cost = w.wpcost;
571                                 p = w.wpnearestpoint;
572                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
575                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
576                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
577                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
578                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
579                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
580                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
581                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
582                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
583                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
584                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
585                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
586                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
587                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
588                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
589                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
590                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
591                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
592                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
593                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
594                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
595                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
596                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
597                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
598                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
599                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
600                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
601                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
602                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
603                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
604                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
605                         }
606                         w = w.chain;
607                 }
608         }
609 }
610
611 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
612 void navigation_markroutes_inverted(entity fixed_source_waypoint)
613 {
614         entity w, wp, waylist;
615         float searching, cost, cost2;
616         vector p;
617         w = waylist = findchain(classname, "waypoint");
618         while (w)
619         {
620                 w.wpconsidered = false;
621                 w.wpnearestpoint = '0 0 0';
622                 w.wpcost = 10000000;
623                 w.wpfire = 0;
624                 w.enemy = NULL;
625                 w = w.chain;
626         }
627
628         if(fixed_source_waypoint)
629         {
630                 fixed_source_waypoint.wpconsidered = true;
631                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
632                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
633                 fixed_source_waypoint.wpfire = 1;
634                 fixed_source_waypoint.enemy = NULL;
635         }
636         else
637         {
638                 error("need to start with a waypoint\n");
639         }
640
641         searching = true;
642         while (searching)
643         {
644                 searching = false;
645                 w = waylist;
646                 while (w)
647                 {
648                         if (w.wpfire)
649                         {
650                                 searching = true;
651                                 w.wpfire = 0;
652                                 cost = w.wpcost; // cost to walk from w to home
653                                 p = w.wpnearestpoint;
654                                 for(wp = waylist; wp; wp = wp.chain)
655                                 {
656                                         if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
657                                         if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
658                                         if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
659                                         if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
660                                         if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
661                                         if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
662                                         if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
663                                         if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
664                                                 continue;
665                                         cost2 = cost + wp.dmg;
666                                         navigation_markroutes_checkwaypoint(w, wp, cost2, p);
667                                 }
668                         }
669                         w = w.chain;
670                 }
671         }
672 }
673
674 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
675 void navigation_routerating(entity this, entity e, float f, float rangebias)
676 {
677         entity nwp;
678         vector o;
679         if (!e)
680                 return;
681
682         if(e.blacklisted)
683                 return;
684
685         o = (e.absmin + e.absmax) * 0.5;
686
687         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
688
689         // Evaluate path using jetpack
690         if(g_jetpack)
691         if(this.items & IT_JETPACK)
692         if(autocvar_bot_ai_navigation_jetpack)
693         if(vdist(this.origin - o, >, autocvar_bot_ai_navigation_jetpack_mindistance))
694         {
695                 vector pointa, pointb;
696
697                 LOG_DEBUG("jetpack ai: evaluating path for ", e.classname, "\n");
698
699                 // Point A
700                 traceline(this.origin, this.origin + '0 0 65535', MOVE_NORMAL, this);
701                 pointa = trace_endpos - '0 0 1';
702
703                 // Point B
704                 traceline(o, o + '0 0 65535', MOVE_NORMAL, e);
705                 pointb = trace_endpos - '0 0 1';
706
707                 // Can I see these two points from the sky?
708                 traceline(pointa, pointb, MOVE_NORMAL, this);
709
710                 if(trace_fraction==1)
711                 {
712                         LOG_DEBUG("jetpack ai: can bridge these two points\n");
713
714                         // Lower the altitude of these points as much as possible
715                         float zdistance, xydistance, cost, t, fuel;
716                         vector down, npa, npb;
717
718                         down = '0 0 -1' * (STAT(PL_MAX, NULL).z - STAT(PL_MIN, NULL).z) * 10;
719
720                         do{
721                                 npa = pointa + down;
722                                 npb = pointb + down;
723
724                                 if(npa.z<=this.absmax.z)
725                                         break;
726
727                                 if(npb.z<=e.absmax.z)
728                                         break;
729
730                                 traceline(npa, npb, MOVE_NORMAL, this);
731                                 if(trace_fraction==1)
732                                 {
733                                         pointa = npa;
734                                         pointb = npb;
735                                 }
736                         }
737                         while(trace_fraction == 1);
738
739
740                         // Rough estimation of fuel consumption
741                         // (ignores acceleration and current xyz velocity)
742                         xydistance = vlen(pointa - pointb);
743                         zdistance = fabs(pointa.z - this.origin.z);
744
745                         t = zdistance / autocvar_g_jetpack_maxspeed_up;
746                         t += xydistance / autocvar_g_jetpack_maxspeed_side;
747                         fuel = t * autocvar_g_jetpack_fuel * 0.8;
748
749                         LOG_DEBUG(strcat("jetpack ai: required fuel ", ftos(fuel), " this.ammo_fuel ", ftos(this.ammo_fuel), "\n"));
750
751                         // enough fuel ?
752                         if(this.ammo_fuel>fuel)
753                         {
754                                 // Estimate cost
755                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
756                                 //  - between air and ground speeds)
757
758                                 cost = xydistance / (autocvar_g_jetpack_maxspeed_side/autocvar_sv_maxspeed);
759                                 cost += zdistance / (autocvar_g_jetpack_maxspeed_up/autocvar_sv_maxspeed);
760                                 cost *= 1.5;
761
762                                 // Compare against other goals
763                                 f = f * rangebias / (rangebias + cost);
764
765                                 if (navigation_bestrating < f)
766                                 {
767                                         LOG_DEBUG(strcat("jetpack path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
768                                         navigation_bestrating = f;
769                                         navigation_bestgoal = e;
770                                         this.navigation_jetpack_goal = e;
771                                         this.navigation_jetpack_point = pointb;
772                                 }
773                                 return;
774                         }
775                 }
776         }
777
778         //te_wizspike(e.origin);
779         //bprint(etos(e));
780         //bprint("\n");
781         // update the cached spawnfunc_waypoint link on a dynamic item entity
782         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
783         {
784                 nwp = e;
785         }
786         else
787         {
788                 float search;
789
790                 search = true;
791
792                 if(e.flags & FL_ITEM)
793                 {
794                         if (!(e.flags & FL_WEAPON))
795                         if(e.nearestwaypoint)
796                                 search = false;
797                 }
798                 else if (e.flags & FL_WEAPON)
799                 {
800                         if(e.classname != "droppedweapon")
801                         if(e.nearestwaypoint)
802                                 search = false;
803                 }
804
805                 if(search)
806                 if (time > e.nearestwaypointtimeout)
807                 {
808                         nwp = navigation_findnearestwaypoint(e, true);
809                         if(nwp)
810                                 e.nearestwaypoint = nwp;
811                         else
812                         {
813                                 LOG_DEBUG(strcat("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n"));
814
815                                 if(e.flags & FL_ITEM)
816                                         e.blacklisted = true;
817                                 else if (e.flags & FL_WEAPON)
818                                 {
819                                         if(e.classname != "droppedweapon")
820                                                 e.blacklisted = true;
821                                 }
822
823                                 if(e.blacklisted)
824                                 {
825                                         LOG_DEBUG(strcat("The entity '", e.classname, "' is going to be excluded from path finding during this match\n"));
826                                         return;
827                                 }
828                         }
829
830                         // TODO: Cleaner solution, probably handling this timeout from ctf.qc
831                         if(e.classname=="item_flag_team")
832                                 e.nearestwaypointtimeout = time + 2;
833                         else
834                                 e.nearestwaypointtimeout = time + random() * 3 + 5;
835                 }
836                 nwp = e.nearestwaypoint;
837         }
838
839         LOG_DEBUG(strcat("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n"));
840         if (nwp)
841         if (nwp.wpcost < 10000000)
842         {
843                 //te_wizspike(nwp.wpnearestpoint);
844                 LOG_DEBUG(strcat(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = "));
845                 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(o - nwp.wpnearestpoint)));
846                 LOG_DEBUG(strcat("considering ", e.classname, " (with rating ", ftos(f), ")\n"));
847                 if (navigation_bestrating < f)
848                 {
849                         LOG_DEBUG(strcat("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n"));
850                         navigation_bestrating = f;
851                         navigation_bestgoal = e;
852                 }
853         }
854 }
855
856 // adds an item to the the goal stack with the path to a given item
857 bool navigation_routetogoal(entity this, entity e, vector startposition)
858 {
859         this.goalentity = e;
860
861         // if there is no goal, just exit
862         if (!e)
863                 return false;
864
865         this.navigation_hasgoals = true;
866
867         // put the entity on the goal stack
868         //print("routetogoal ", etos(e), "\n");
869         navigation_pushroute(this, e);
870
871         if(g_jetpack)
872         if(e==this.navigation_jetpack_goal)
873                 return true;
874
875         // if it can reach the goal there is nothing more to do
876         if (tracewalk(this, startposition, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), (e.absmin + e.absmax) * 0.5, bot_navigation_movemode))
877                 return true;
878
879         // see if there are waypoints describing a path to the item
880         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
881                 e = e.nearestwaypoint;
882         else
883                 e = e.enemy; // we already have added it, so...
884
885         if(e == NULL)
886                 return false;
887
888         for (;;)
889         {
890                 // add the spawnfunc_waypoint to the path
891                 navigation_pushroute(this, e);
892                 e = e.enemy;
893
894                 if(e==NULL)
895                         break;
896         }
897
898         return false;
899 }
900
901 // removes any currently touching waypoints from the goal stack
902 // (this is how bots detect if they reached a goal)
903 void navigation_poptouchedgoals(entity this)
904 {
905         vector org, m1, m2;
906         org = this.origin;
907         m1 = org + this.mins;
908         m2 = org + this.maxs;
909
910         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
911         {
912                 if(this.lastteleporttime>0)
913                 if(time-this.lastteleporttime<(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)?2:0.15)
914                 {
915                         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
916                         if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
917                         {
918                                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
919                                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
920                         }
921                         navigation_poproute(this);
922                         return;
923                 }
924         }
925
926         // If for some reason the bot is closer to the next goal, pop the current one
927         if(this.goalstack01)
928         if(vlen2(this.goalcurrent.origin - this.origin) > vlen2(this.goalstack01.origin - this.origin))
929         if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
930         if(tracewalk(this, this.origin, this.mins, this.maxs, (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5, bot_navigation_movemode))
931         {
932                 LOG_DEBUG(strcat("path optimized for ", this.netname, ", removed a goal from the queue\n"));
933                 navigation_poproute(this);
934                 // TODO this may also be a nice idea to do "early" (e.g. by
935                 // manipulating the vlen() comparisons) to shorten paths in
936                 // general - this would make bots walk more "on rails" than
937                 // "zigzagging" which they currently do with sufficiently
938                 // random-like waypoints, and thus can make a nice bot
939                 // personality property
940         }
941
942         // HACK: remove players/bots as goals, they can lead a bot to unexpected places (cliffs, lava, etc)
943         // TODO: rate waypoints near the targetted player at that moment, instead of the player itthis
944         if(IS_PLAYER(this.goalcurrent))
945                 navigation_poproute(this);
946
947         // aid for detecting jump pads better (distance based check fails sometimes)
948         if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT && this.jumppadcount > 0 )
949                 navigation_poproute(this);
950
951         // Loose goal touching check when running
952         if(this.aistatus & AI_STATUS_RUNNING)
953         if(this.speed >= autocvar_sv_maxspeed) // if -really- running
954         if(this.goalcurrent.classname=="waypoint")
955         {
956                 if(vdist(this.origin - this.goalcurrent.origin, <, 150))
957                 {
958                         traceline(this.origin + this.view_ofs , this.goalcurrent.origin, true, NULL);
959                         if(trace_fraction==1)
960                         {
961                                 // Detect personal waypoints
962                                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
963                                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
964                                 {
965                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
966                                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
967                                 }
968
969                                 navigation_poproute(this);
970                         }
971                 }
972         }
973
974         while (this.goalcurrent && boxesoverlap(m1, m2, this.goalcurrent.absmin, this.goalcurrent.absmax))
975         {
976                 // Detect personal waypoints
977                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
978                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && this.goalcurrent.owner==this)
979                 {
980                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
981                         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
982                 }
983
984                 navigation_poproute(this);
985         }
986 }
987
988 // begin a goal selection session (queries spawnfunc_waypoint network)
989 void navigation_goalrating_start(entity this)
990 {
991         if(this.aistatus & AI_STATUS_STUCK)
992                 return;
993
994         this.navigation_jetpack_goal = NULL;
995         navigation_bestrating = -1;
996         this.navigation_hasgoals = false;
997         navigation_clearroute(this);
998         navigation_bestgoal = NULL;
999         navigation_markroutes(this, NULL);
1000 }
1001
1002 // ends a goal selection session (updates goal stack to the best goal)
1003 void navigation_goalrating_end(entity this)
1004 {
1005         if(this.aistatus & AI_STATUS_STUCK)
1006                 return;
1007
1008         navigation_routetogoal(this, navigation_bestgoal, this.origin);
1009         LOG_DEBUG(strcat("best goal ", this.goalcurrent.classname , "\n"));
1010
1011         // If the bot got stuck then try to reach the farthest waypoint
1012         if (!this.navigation_hasgoals)
1013         if (autocvar_bot_wander_enable)
1014         {
1015                 if (!(this.aistatus & AI_STATUS_STUCK))
1016                 {
1017                         LOG_DEBUG(strcat(this.netname, " cannot walk to any goal\n"));
1018                         this.aistatus |= AI_STATUS_STUCK;
1019                 }
1020
1021                 this.navigation_hasgoals = false; // Reset this value
1022         }
1023 }
1024
1025 void botframe_updatedangerousobjects(float maxupdate)
1026 {
1027         entity head, bot_dodgelist;
1028         vector m1, m2, v, o;
1029         float c, d, danger;
1030         c = 0;
1031         bot_dodgelist = findchainfloat(bot_dodge, true);
1032         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1033         while (botframe_dangerwaypoint != NULL)
1034         {
1035                 danger = 0;
1036                 m1 = botframe_dangerwaypoint.mins;
1037                 m2 = botframe_dangerwaypoint.maxs;
1038                 head = bot_dodgelist;
1039                 while (head)
1040                 {
1041                         v = head.origin;
1042                         v.x = bound(m1_x, v.x, m2_x);
1043                         v.y = bound(m1_y, v.y, m2_y);
1044                         v.z = bound(m1_z, v.z, m2_z);
1045                         o = (head.absmin + head.absmax) * 0.5;
1046                         d = head.bot_dodgerating - vlen(o - v);
1047                         if (d > 0)
1048                         {
1049                                 traceline(o, v, true, NULL);
1050                                 if (trace_fraction == 1)
1051                                         danger = danger + d;
1052                         }
1053                         head = head.chain;
1054                 }
1055                 botframe_dangerwaypoint.dmg = danger;
1056                 c = c + 1;
1057                 if (c >= maxupdate)
1058                         break;
1059                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1060         }
1061 }
1062
1063 void navigation_unstuck(entity this)
1064 {
1065         float search_radius = 1000;
1066
1067         if (!autocvar_bot_wander_enable)
1068                 return;
1069
1070         if (!bot_waypoint_queue_owner)
1071         {
1072                 LOG_DEBUG(strcat(this.netname, " sutck, taking over the waypoints queue\n"));
1073                 bot_waypoint_queue_owner = this;
1074                 bot_waypoint_queue_bestgoal = NULL;
1075                 bot_waypoint_queue_bestgoalrating = 0;
1076         }
1077
1078         if(bot_waypoint_queue_owner!=this)
1079                 return;
1080
1081         if (bot_waypoint_queue_goal)
1082         {
1083                 // evaluate the next goal on the queue
1084                 float d = vlen(this.origin - bot_waypoint_queue_goal.origin);
1085                 LOG_DEBUG(strcat(this.netname, " evaluating ", bot_waypoint_queue_goal.classname, " with distance ", ftos(d), "\n"));
1086                 if(tracewalk(bot_waypoint_queue_goal, this.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), bot_waypoint_queue_goal.origin, bot_navigation_movemode))
1087                 {
1088                         if( d > bot_waypoint_queue_bestgoalrating)
1089                         {
1090                                 bot_waypoint_queue_bestgoalrating = d;
1091                                 bot_waypoint_queue_bestgoal = bot_waypoint_queue_goal;
1092                         }
1093                 }
1094                 bot_waypoint_queue_goal = bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal;
1095
1096                 if (!bot_waypoint_queue_goal)
1097                 {
1098                         if (bot_waypoint_queue_bestgoal)
1099                         {
1100                                 LOG_DEBUG(strcat(this.netname, " stuck, reachable waypoint found, heading to it\n"));
1101                                 navigation_routetogoal(this, bot_waypoint_queue_bestgoal, this.origin);
1102                                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
1103                                 this.aistatus &= ~AI_STATUS_STUCK;
1104                         }
1105                         else
1106                         {
1107                                 LOG_DEBUG(strcat(this.netname, " stuck, cannot walk to any waypoint at all\n"));
1108                         }
1109
1110                         bot_waypoint_queue_owner = NULL;
1111                 }
1112         }
1113         else
1114         {
1115                 if(bot_strategytoken!=this)
1116                         return;
1117
1118                 // build a new queue
1119                 LOG_DEBUG(strcat(this.netname, " stuck, scanning reachable waypoints within ", ftos(search_radius)," qu\n"));
1120
1121                 entity head, first;
1122
1123                 first = NULL;
1124                 head = findradius(this.origin, search_radius);
1125
1126                 while(head)
1127                 {
1128                         if(head.classname=="waypoint")
1129                 //      if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
1130                         {
1131                                 if(bot_waypoint_queue_goal)
1132                                         bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = head;
1133                                 else
1134                                         first = head;
1135
1136                                 bot_waypoint_queue_goal = head;
1137                                 bot_waypoint_queue_goal.bot_waypoint_queue_nextgoal = NULL;
1138                         }
1139
1140                         head = head.chain;
1141                 }
1142
1143                 if (first)
1144                         bot_waypoint_queue_goal = first;
1145                 else
1146                 {
1147                         LOG_DEBUG(strcat(this.netname, " stuck, cannot walk to any waypoint at all\n"));
1148                         bot_waypoint_queue_owner = NULL;
1149                 }
1150         }
1151 }
1152
1153 // Support for debugging tracewalk visually
1154
1155 void debugresetnodes()
1156 {
1157         debuglastnode = '0 0 0';
1158 }
1159
1160 void debugnode(entity this, vector node)
1161 {
1162         if (!IS_PLAYER(this))
1163                 return;
1164
1165         if(debuglastnode=='0 0 0')
1166         {
1167                 debuglastnode = node;
1168                 return;
1169         }
1170
1171         te_lightning2(NULL, node, debuglastnode);
1172         debuglastnode = node;
1173 }
1174
1175 void debugnodestatus(vector position, float status)
1176 {
1177         vector c;
1178
1179         switch (status)
1180         {
1181                 case DEBUG_NODE_SUCCESS:
1182                         c = '0 15 0';
1183                         break;
1184                 case DEBUG_NODE_WARNING:
1185                         c = '15 15 0';
1186                         break;
1187                 case DEBUG_NODE_FAIL:
1188                         c = '15 0 0';
1189                         break;
1190                 default:
1191                         c = '15 15 15';
1192         }
1193
1194         te_customflash(position, 40,  2, c);
1195 }
1196
1197 // Support for debugging the goal stack visually
1198
1199 .float goalcounter;
1200 .vector lastposition;
1201
1202 // Debug the goal stack visually
1203 void debuggoalstack(entity this)
1204 {
1205         entity goal;
1206         vector org, go;
1207
1208         if(this.goalcounter==0)goal=this.goalcurrent;
1209         else if(this.goalcounter==1)goal=this.goalstack01;
1210         else if(this.goalcounter==2)goal=this.goalstack02;
1211         else if(this.goalcounter==3)goal=this.goalstack03;
1212         else if(this.goalcounter==4)goal=this.goalstack04;
1213         else if(this.goalcounter==5)goal=this.goalstack05;
1214         else if(this.goalcounter==6)goal=this.goalstack06;
1215         else if(this.goalcounter==7)goal=this.goalstack07;
1216         else if(this.goalcounter==8)goal=this.goalstack08;
1217         else if(this.goalcounter==9)goal=this.goalstack09;
1218         else if(this.goalcounter==10)goal=this.goalstack10;
1219         else if(this.goalcounter==11)goal=this.goalstack11;
1220         else if(this.goalcounter==12)goal=this.goalstack12;
1221         else if(this.goalcounter==13)goal=this.goalstack13;
1222         else if(this.goalcounter==14)goal=this.goalstack14;
1223         else if(this.goalcounter==15)goal=this.goalstack15;
1224         else if(this.goalcounter==16)goal=this.goalstack16;
1225         else if(this.goalcounter==17)goal=this.goalstack17;
1226         else if(this.goalcounter==18)goal=this.goalstack18;
1227         else if(this.goalcounter==19)goal=this.goalstack19;
1228         else if(this.goalcounter==20)goal=this.goalstack20;
1229         else if(this.goalcounter==21)goal=this.goalstack21;
1230         else if(this.goalcounter==22)goal=this.goalstack22;
1231         else if(this.goalcounter==23)goal=this.goalstack23;
1232         else if(this.goalcounter==24)goal=this.goalstack24;
1233         else if(this.goalcounter==25)goal=this.goalstack25;
1234         else if(this.goalcounter==26)goal=this.goalstack26;
1235         else if(this.goalcounter==27)goal=this.goalstack27;
1236         else if(this.goalcounter==28)goal=this.goalstack28;
1237         else if(this.goalcounter==29)goal=this.goalstack29;
1238         else if(this.goalcounter==30)goal=this.goalstack30;
1239         else if(this.goalcounter==31)goal=this.goalstack31;
1240         else goal=NULL;
1241
1242         if(goal==NULL)
1243         {
1244                 this.goalcounter = 0;
1245                 this.lastposition='0 0 0';
1246                 return;
1247         }
1248
1249         if(this.lastposition=='0 0 0')
1250                 org = this.origin;
1251         else
1252                 org = this.lastposition;
1253
1254
1255         go = ( goal.absmin + goal.absmax ) * 0.5;
1256         te_lightning2(NULL, org, go);
1257         this.lastposition = go;
1258
1259         this.goalcounter++;
1260 }