]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/bot/navigation.qc
fix bug with guiding accelerated rockets
[divverent/nexuiz.git] / data / qcsrc / server / bot / navigation.qc
1
2 // rough simulation of walking from one point to another to test if a path
3 // can be traveled, used for waypoint linking and havocbot
4
5 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
6 {
7         local vector org;
8         local vector move;
9         local vector dir;
10         local float dist;
11         local float totaldist;
12         local float stepdist;
13         local float yaw;
14         local float ignorehazards;
15         local float swimming;
16
17         #ifdef DEBUG_TRACEWALK
18                 debugresetnodes();
19                 debugnode(start);
20         #endif
21
22         move = end - start;
23         move_z = 0;
24         org = start;
25         dist = totaldist = vlen(move);
26         dir = normalize(move);
27         stepdist = 32;
28         ignorehazards = FALSE;
29
30         // Analyze starting point
31         traceline(start, start, MOVE_NORMAL, e);
32         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
33                 ignorehazards = TRUE;
34         else
35         {
36                 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
37                 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
38                 {
39                         ignorehazards = TRUE;
40                         swimming = TRUE;
41                 }
42         }
43         tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
44         if (trace_startsolid)
45         {
46                 // Bad start
47                 #ifdef DEBUG_TRACEWALK
48                         debugnodestatus(start, DEBUG_NODE_FAIL);
49                 #endif
50                 //print("tracewalk: ", vtos(start), " is a bad start\n");
51                 return FALSE;
52         }
53
54         // Movement loop
55         yaw = vectoyaw(move);
56         move = end - org;
57         for (;;)
58         {
59                 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
60                 {
61                         // Succeeded
62                         #ifdef DEBUG_TRACEWALK
63                                 debugnodestatus(org, DEBUG_NODE_SUCCESS);
64                         #endif
65                         //print("tracewalk: ", vtos(start), " can reach ", vtos(end), "\n");
66                         return TRUE;
67                 }
68                 #ifdef DEBUG_TRACEWALK
69                         debugnode(org);
70                 #endif
71
72                 if (dist <= 0)
73                         break;
74                 if (stepdist > dist)
75                         stepdist = dist;
76                 dist = dist - stepdist;
77                 traceline(org, org, MOVE_NORMAL, e);
78                 if (!ignorehazards)
79                 {
80                         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
81                         {
82                                 // hazards blocking path
83                                 #ifdef DEBUG_TRACEWALK
84                                         debugnodestatus(org, DEBUG_NODE_FAIL);
85                                 #endif
86                                 //print("tracewalk: ", vtos(start), " hits a hazard when trying to reach ", vtos(end), "\n");
87                                 return FALSE;
88                         }
89                 }
90                 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
91                 {
92                         move = normalize(end - org);
93                         tracebox(org, m1, m2, org + move * stepdist, movemode, e);
94
95                         #ifdef DEBUG_TRACEWALK
96                                 debugnode(trace_endpos);
97                         #endif
98
99                         if (trace_fraction < 1)
100                         {
101                                 swimming = TRUE;
102                                 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
103                                 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
104                                 {
105                                                 #ifdef DEBUG_TRACEWALK
106                                                         debugnode(org);
107                                                 #endif
108                                         if(pointcontents(org) == CONTENT_EMPTY)
109                                                         break;
110                                 }
111
112                                 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
113                                 {
114                                         #ifdef DEBUG_TRACEWALK
115                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
116                                         #endif
117                                         return FALSE;
118                                         //print("tracewalk: ", vtos(start), " failed under water\n");
119                                 }
120                                 continue;
121
122                         }
123                         else
124                                 org = trace_endpos;
125                 }
126                 else
127                 {
128                         move = dir * stepdist + org;
129                         tracebox(org, m1, m2, move, movemode, e);
130
131                         #ifdef DEBUG_TRACEWALK
132                                 debugnode(trace_endpos);
133                         #endif
134
135                         // hit something
136                         if (trace_fraction < 1)
137                         {
138                                 // check if we can walk over this obstacle
139                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
140                                 if (trace_fraction < 1 || trace_startsolid)
141                                 {
142                                         #ifdef DEBUG_TRACEWALK
143                                                 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
144                                         #endif
145
146                                         // check for doors
147                                         traceline( org, move, movemode, e);
148                                         if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
149                                         {
150                                                 local vector nextmove;
151                                                 move = trace_endpos;
152                                                 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
153                                                 {
154                                                         nextmove = move + (dir * stepdist);
155                                                         traceline( move, nextmove, movemode, e);
156                                                         move = nextmove;
157                                                 }
158                                         }
159                                         else
160                                         {
161                                                 #ifdef DEBUG_TRACEWALK
162                                                         debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
163                                                 #endif
164                                                 //print("tracewalk: ", vtos(start), " hit something when trying to reach ", vtos(end), "\n");
165                                                 //te_explosion(trace_endpos);
166                                                 //print(ftos(e.dphitcontentsmask), "\n");
167                                                 return FALSE; // failed
168                                         }
169                                 }
170                                 else
171                                         move = trace_endpos;
172                         }
173                         else
174                                 move = trace_endpos;
175
176                         // trace down from stepheight as far as possible and move there,
177                         // if this starts in solid we try again without the stepup, and
178                         // if that also fails we assume it is a wall
179                         // (this is the same logic as the Quake walkmove function used)
180                         tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
181
182                         // moved successfully
183                         if(swimming)
184                         {
185                                 local float c;
186                                 c = pointcontents(org + '0 0 1');
187                                 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
188                                         swimming = FALSE;
189                                 else
190                                         continue;
191                         }
192
193                         org = trace_endpos;
194                 }
195         }
196
197         //print("tracewalk: ", vtos(start), " did not arrive at ", vtos(end), " but at ", vtos(org), "\n");
198
199         // moved but didn't arrive at the intended destination
200         #ifdef DEBUG_TRACEWALK
201                 debugnodestatus(org, DEBUG_NODE_FAIL);
202         #endif
203
204         return FALSE;
205 };
206
207 /////////////////////////////////////////////////////////////////////////////
208 // goal stack
209 /////////////////////////////////////////////////////////////////////////////
210
211 // completely empty the goal stack, used when deciding where to go
212 void navigation_clearroute()
213 {
214         //print("bot ", etos(self), " clear\n");
215         self.navigation_hasgoals = FALSE;
216         self.goalcurrent = world;
217         self.goalstack01 = world;
218         self.goalstack02 = world;
219         self.goalstack03 = world;
220         self.goalstack04 = world;
221         self.goalstack05 = world;
222         self.goalstack06 = world;
223         self.goalstack07 = world;
224         self.goalstack08 = world;
225         self.goalstack09 = world;
226         self.goalstack10 = world;
227         self.goalstack11 = world;
228         self.goalstack12 = world;
229         self.goalstack13 = world;
230         self.goalstack14 = world;
231         self.goalstack15 = world;
232         self.goalstack16 = world;
233         self.goalstack17 = world;
234         self.goalstack18 = world;
235         self.goalstack19 = world;
236         self.goalstack20 = world;
237         self.goalstack21 = world;
238         self.goalstack22 = world;
239         self.goalstack23 = world;
240         self.goalstack24 = world;
241         self.goalstack25 = world;
242         self.goalstack26 = world;
243         self.goalstack27 = world;
244         self.goalstack28 = world;
245         self.goalstack29 = world;
246         self.goalstack30 = world;
247         self.goalstack31 = world;
248 };
249
250 // add a new goal at the beginning of the stack
251 // (in other words: add a new prerequisite before going to the later goals)
252 void navigation_pushroute(entity e)
253 {
254         //print("bot ", etos(self), " push ", etos(e), "\n");
255         self.goalstack31 = self.goalstack30;
256         self.goalstack30 = self.goalstack29;
257         self.goalstack29 = self.goalstack28;
258         self.goalstack28 = self.goalstack27;
259         self.goalstack27 = self.goalstack26;
260         self.goalstack26 = self.goalstack25;
261         self.goalstack25 = self.goalstack24;
262         self.goalstack24 = self.goalstack23;
263         self.goalstack23 = self.goalstack22;
264         self.goalstack22 = self.goalstack21;
265         self.goalstack21 = self.goalstack20;
266         self.goalstack20 = self.goalstack19;
267         self.goalstack19 = self.goalstack18;
268         self.goalstack18 = self.goalstack17;
269         self.goalstack17 = self.goalstack16;
270         self.goalstack16 = self.goalstack15;
271         self.goalstack15 = self.goalstack14;
272         self.goalstack14 = self.goalstack13;
273         self.goalstack13 = self.goalstack12;
274         self.goalstack12 = self.goalstack11;
275         self.goalstack11 = self.goalstack10;
276         self.goalstack10 = self.goalstack09;
277         self.goalstack09 = self.goalstack08;
278         self.goalstack08 = self.goalstack07;
279         self.goalstack07 = self.goalstack06;
280         self.goalstack06 = self.goalstack05;
281         self.goalstack05 = self.goalstack04;
282         self.goalstack04 = self.goalstack03;
283         self.goalstack03 = self.goalstack02;
284         self.goalstack02 = self.goalstack01;
285         self.goalstack01 = self.goalcurrent;
286         self.goalcurrent = e;
287 };
288
289 // remove first goal from stack
290 // (in other words: remove a prerequisite for reaching the later goals)
291 // (used when a spawnfunc_waypoint is reached)
292 void navigation_poproute()
293 {
294         //print("bot ", etos(self), " pop\n");
295         self.goalcurrent = self.goalstack01;
296         self.goalstack01 = self.goalstack02;
297         self.goalstack02 = self.goalstack03;
298         self.goalstack03 = self.goalstack04;
299         self.goalstack04 = self.goalstack05;
300         self.goalstack05 = self.goalstack06;
301         self.goalstack06 = self.goalstack07;
302         self.goalstack07 = self.goalstack08;
303         self.goalstack08 = self.goalstack09;
304         self.goalstack09 = self.goalstack10;
305         self.goalstack10 = self.goalstack11;
306         self.goalstack11 = self.goalstack12;
307         self.goalstack12 = self.goalstack13;
308         self.goalstack13 = self.goalstack14;
309         self.goalstack14 = self.goalstack15;
310         self.goalstack15 = self.goalstack16;
311         self.goalstack16 = self.goalstack17;
312         self.goalstack17 = self.goalstack18;
313         self.goalstack18 = self.goalstack19;
314         self.goalstack19 = self.goalstack20;
315         self.goalstack20 = self.goalstack21;
316         self.goalstack21 = self.goalstack22;
317         self.goalstack22 = self.goalstack23;
318         self.goalstack23 = self.goalstack24;
319         self.goalstack24 = self.goalstack25;
320         self.goalstack25 = self.goalstack26;
321         self.goalstack26 = self.goalstack27;
322         self.goalstack27 = self.goalstack28;
323         self.goalstack28 = self.goalstack29;
324         self.goalstack29 = self.goalstack30;
325         self.goalstack30 = self.goalstack31;
326         self.goalstack31 = world;
327 };
328
329 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
330 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
331 {
332         local entity waylist, w, best;
333         local float dist, bestdist;
334         local vector v, org, pm1, pm2;
335         pm1 = ent.origin + PL_MIN;
336         pm2 = ent.origin + PL_MAX;
337         waylist = findchain(classname, "waypoint");
338
339         // do two scans, because box test is cheaper
340         w = waylist;
341         while (w)
342         {
343                 // if object is touching spawnfunc_waypoint
344                 if(w != ent)
345                         if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
346                                 return w;
347                 w = w.chain;
348         }
349
350         org = ent.origin + (ent.mins_z - PL_MIN_z) * '0 0 1';
351         if(ent.tag_entity)
352                 org = org + ent.tag_entity.origin;
353         if (navigation_testtracewalk)
354                 te_plasmaburn(org);
355
356         best = world;
357         bestdist = 1050;
358
359         // box check failed, try walk
360         w = waylist;
361         while (w)
362         {
363                 // if object can walk from spawnfunc_waypoint
364                 if(w != ent)
365                 {
366                         if (w.wpisbox)
367                         {
368                                 local vector wm1, wm2;
369                                 wm1 = w.origin + w.mins;
370                                 wm2 = w.origin + w.maxs;
371                                 v_x = bound(wm1_x, org_x, wm2_x);
372                                 v_y = bound(wm1_y, org_y, wm2_y);
373                                 v_z = bound(wm1_z, org_z, wm2_z);
374                         }
375                         else
376                                 v = w.origin;
377                         dist = vlen(v - org);
378                         if (bestdist > dist)
379                         {
380                                 traceline(v, org, TRUE, ent);
381                                 if (trace_fraction == 1)
382                                 {
383                                         if (walkfromwp)
384                                         {
385                                                 //print("^1can I reach ", vtos(org), " from ", vtos(v), "?\n");
386                                                 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
387                                                 {
388                                                         bestdist = dist;
389                                                         best = w;
390                                                 }
391                                         }
392                                         else
393                                         {
394                                                 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
395                                                 {
396                                                         bestdist = dist;
397                                                         best = w;
398                                                 }
399                                         }
400                                 }
401                         }
402                 }
403                 w = w.chain;
404         }
405         return best;
406 }
407
408 // finds the waypoints near the bot initiating a navigation query
409 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
410 {
411         local entity head;
412         local vector v, m1, m2, diff;
413         local float c;
414 //      navigation_testtracewalk = TRUE;
415         c = 0;
416         head = waylist;
417         while (head)
418         {
419                 if (!head.wpconsidered)
420                 {
421                         if (head.wpisbox)
422                         {
423                                 m1 = head.origin + head.mins;
424                                 m2 = head.origin + head.maxs;
425                                 v = self.origin;
426                                 v_x = bound(m1_x, v_x, m2_x);
427                                 v_y = bound(m1_y, v_y, m2_y);
428                                 v_z = bound(m1_z, v_z, m2_z);
429                         }
430                         else
431                                 v = head.origin;
432                         diff = v - self.origin;
433                         diff_z = max(0, diff_z);
434                         if (vlen(diff) < maxdist)
435                         {
436                                 head.wpconsidered = TRUE;
437                                 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
438                                 {
439                                         head.wpnearestpoint = v;
440                                         head.wpcost = vlen(v - self.origin) + head.dmg;
441                                         head.wpfire = 1;
442                                         head.enemy = world;
443                                         c = c + 1;
444                                 }
445                         }
446                 }
447                 head = head.chain;
448         }
449         //navigation_testtracewalk = FALSE;
450         return c;
451 }
452
453 // updates a path link if a spawnfunc_waypoint link is better than the current one
454 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
455 {
456         local vector m1;
457         local vector m2;
458         local vector v;
459         if (wp.wpisbox)
460         {
461                 m1 = wp.absmin;
462                 m2 = wp.absmax;
463                 v_x = bound(m1_x, p_x, m2_x);
464                 v_y = bound(m1_y, p_y, m2_y);
465                 v_z = bound(m1_z, p_z, m2_z);
466         }
467         else
468                 v = wp.origin;
469         cost2 = cost2 + vlen(v - p);
470         if (wp.wpcost > cost2)
471         {
472                 wp.wpcost = cost2;
473                 wp.enemy = w;
474                 wp.wpfire = 1;
475                 wp.wpnearestpoint = v;
476         }
477 };
478
479 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
480 void navigation_markroutes()
481 {
482         local entity w, wp, waylist;
483         local float searching, cost, cost2;
484         local vector p;
485         w = waylist = findchain(classname, "waypoint");
486         while (w)
487         {
488                 w.wpconsidered = FALSE;
489                 w.wpnearestpoint = '0 0 0';
490                 w.wpcost = 10000000;
491                 w.wpfire = 0;
492                 w.enemy = world;
493                 w = w.chain;
494         }
495
496         // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
497         // as this search is expensive we will use lower values if the bot is on the air
498         local float i, increment, maxdistance;
499         if(self.flags & FL_ONGROUND)
500         {
501                 increment = 750;
502                 maxdistance = 50000;
503         }
504         else
505         {
506                 increment = 500;
507                 maxdistance = 1500;
508         }
509
510         for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
511
512         searching = TRUE;
513         while (searching)
514         {
515                 searching = FALSE;
516                 w = waylist;
517                 while (w)
518                 {
519                         if (w.wpfire)
520                         {
521                                 searching = TRUE;
522                                 w.wpfire = 0;
523                                 cost = w.wpcost;
524                                 p = w.wpnearestpoint;
525                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
526                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
527                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
528                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
529                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
530                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
531                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
532                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
533                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
534                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
535                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
536                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
537                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
538                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
539                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
540                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
541                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
542                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
543                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
544                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
545                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
546                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
547                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
548                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
549                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
550                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
551                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
552                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
553                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
554                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
555                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
556                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
557                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
558                         }
559                         w = w.chain;
560                 }
561         }
562 };
563
564 void navigation_bestgoals_reset()
565 {
566         local float i;
567
568         bestgoalswindex = 0;
569         bestgoalsrindex = 0;
570
571         for(i=0;i>MAX_BESTGOALS-1;++i)
572         {
573                 navigation_bestgoals[i] = world;
574         }
575 }
576
577 void navigation_add_bestgoal(entity goal)
578 {
579         if(bestgoalsrindex>0)
580         {
581                 ++bestgoalsrindex;
582
583                 if(bestgoalsrindex==MAX_BESTGOALS)
584                         bestgoalsrindex = 0;
585         }
586
587         if(bestgoalswindex==MAX_BESTGOALS)
588         {
589                 bestgoalswindex=0;
590                 if(bestgoalsrindex==0)
591                         bestgoalsrindex=1;
592         }
593
594         navigation_bestgoals[bestgoalswindex] = goal;
595
596         ++bestgoalswindex;
597 }
598
599 entity navigation_get_bestgoal()
600 {
601         local entity ent;
602
603         ent = navigation_bestgoals[bestgoalsrindex];
604         navigation_bestgoals[bestgoalsrindex] = world;
605
606         ++bestgoalsrindex;
607
608         if(bestgoalsrindex==MAX_BESTGOALS)
609                 bestgoalsrindex = 0;
610
611         return ent;
612 }
613
614 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
615 void navigation_routerating(entity e, float f, float rangebias)
616 {
617         entity nwp;
618         if (!e)
619                 return;
620
621         // Evaluate path using jetpack
622         if(g_jetpack)
623         if(self.items & IT_JETPACK)
624         if(cvar("bot_ai_navigation_jetpack"))
625         if(vlen(self.origin - e.origin) > cvar("bot_ai_navigation_jetpack_mindistance"))
626         {
627                 vector pointa, pointb;
628
629         //      dprint("jetpack ai: evaluating path for ", e.classname,"\n");
630
631                 // Point A
632                 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
633                 pointa = trace_endpos - '0 0 1';
634
635                 // Point B
636                 traceline(e.origin, e.origin + '0 0 65535', MOVE_NORMAL, e);
637                 pointb = trace_endpos - '0 0 1';
638
639                 // Can I see these two points from the sky?
640                 traceline(pointa, pointb, MOVE_NORMAL, self);
641
642                 if(trace_fraction==1)
643                 {
644                 //      dprint("jetpack ai: can bridge these two points\n");
645
646                         // Lower the altitude of these points as much as possible
647                         local float zdistance, xydistance, cost, t, fuel;
648                         local vector down, npa, npb;
649
650                         down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
651
652                         do{
653                                 npa = pointa + down;
654                                 npb = pointb + down;
655
656                                 if(npa_z<=self.absmax_z)
657                                         break;
658
659                                 if(npb_z<=e.absmax_z)
660                                         break;
661
662                                 traceline(npa, npb, MOVE_NORMAL, self);
663                                 if(trace_fraction==1)
664                                 {
665                                         pointa = npa;
666                                         pointb = npb;
667                                 }
668                         }
669                         while(trace_fraction == 1);
670
671
672                         // Rough estimation of fuel consumption
673                         // (ignores acceleration and current xyz velocity)
674                         xydistance = vlen(pointa - pointb);
675                         zdistance = fabs(pointa_z - self.origin_z);
676
677                         t = zdistance / cvar("g_jetpack_maxspeed_up");
678                         t += xydistance / cvar("g_jetpack_maxspeed_side");
679                         fuel = t * cvar("g_jetpack_fuel") * 0.8;
680
681                 //      dprint("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel),"\n");
682
683                         // enough fuel ?
684                         if(self.ammo_fuel>fuel)
685                         {
686                                 // Estimate cost
687                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
688                                 //  - between air and ground speeds)
689
690                                 cost = xydistance / (cvar("g_jetpack_maxspeed_side")/cvar("sv_maxspeed"));
691                                 cost += zdistance / (cvar("g_jetpack_maxspeed_up")/cvar("sv_maxspeed"));
692                                 cost *= 1.5;
693
694                                 // Compare against other goals
695                                 f = f * rangebias / (rangebias + cost);
696
697                                 if (navigation_bestrating < f)
698                                 {
699                         //              dprint("jetpack path: added goal", e.classname, " (with rating ", ftos(f), ")\n");
700                                         navigation_bestrating = f;
701                                         navigation_add_bestgoal(e);
702                                         self.navigation_jetpack_goal = e;
703                                         self.navigation_jetpack_point = pointb;
704                                 }
705                                 return;
706                         }
707                 }
708         }
709
710         //te_wizspike(e.origin);
711         //bprint(etos(e));
712         //bprint("\n");
713         // update the cached spawnfunc_waypoint link on a dynamic item entity
714         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
715         {
716                 nwp = e;
717         }
718         else
719         {
720                 if (time > e.nearestwaypointtimeout)
721                 {
722                         e.nearestwaypoint = navigation_findnearestwaypoint(e, TRUE);
723
724                         // TODO: Cleaner solution, probably handling this timeout from ctf.qc
725                         if(e.classname=="item_flag_team")
726                                 e.nearestwaypointtimeout = time + 2;
727                         else
728                                 e.nearestwaypointtimeout = time + random() * 3 + 5;
729                 }
730                 nwp = e.nearestwaypoint;
731         }
732
733         //dprint("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n");
734         if (nwp)
735         if (nwp.wpcost < 10000000)
736         {
737                 //te_wizspike(nwp.wpnearestpoint);
738         //      dprint(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
739                 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint)));
740                 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
741                 //dprint(ftos(f));
742                 if (navigation_bestrating < f)
743                 {
744                 //      dprint("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n");
745                         navigation_bestrating = f;
746                         navigation_add_bestgoal(e);
747                 }
748         }
749         //dprint("\n");
750 };
751
752 // adds an item to the the goal stack with the path to a given item
753 float navigation_routetogoal(entity e, vector startposition)
754 {
755         self.goalentity = e;
756
757         // if there is no goal, just exit
758         if (!e)
759                 return FALSE;
760
761         self.navigation_hasgoals = TRUE;
762
763         // put the entity on the goal stack
764         navigation_pushroute(e);
765
766         if(g_jetpack)
767         if(e==self.navigation_jetpack_goal)
768                 return TRUE;
769
770         // if it can reach the goal there is nothing more to do
771         if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, bot_navigation_movemode))
772                 return TRUE;
773
774         // see if there are waypoints describing a path to the item
775         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
776                 e = e.nearestwaypoint;
777         else
778                 e = e.enemy; // we already have added it, so...
779
780         if(e == world)
781                 return FALSE;
782
783         for (;;)
784         {
785                 // add the spawnfunc_waypoint to the path
786                 navigation_pushroute(e);
787                 e = e.enemy;
788
789                 if(e==world)
790                         break;
791         }
792
793         return FALSE;
794 };
795
796 void navigation_routetogoals()
797 {
798         entity g1, g2;
799
800         navigation_clearroute();
801
802         g1 = navigation_get_bestgoal();
803         for(;;)
804         {
805                 if(g2==world)
806                         g2 = navigation_get_bestgoal();
807
808                 if(g2==world)
809                 {
810                         navigation_routetogoal(g1, self.origin);
811                         return;
812                 }
813
814                 if(navigation_routetogoal(g1, g2.origin))
815                 {
816                         g1 = g2;
817                         g2 = world;
818                         continue;
819                 }
820
821                 navigation_clearroute();
822                 g1 = g2;
823                 g2 = world;
824         }
825 }
826
827 // removes any currently touching waypoints from the goal stack
828 // (this is how bots detect if they reached a goal)
829 void navigation_poptouchedgoals()
830 {
831         local vector org, m1, m2;
832         org = self.origin;
833         m1 = org + self.mins;
834         m2 = org + self.maxs;
835
836         if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
837         {
838                 if(self.lastteleporttime>0)
839                 if(time-self.lastteleporttime<0.15)
840                 {
841                         navigation_poproute();
842                         return;
843                 }
844         }
845
846         // Loose goal touching check when running
847         if(self.aistatus & AI_STATUS_RUNNING)
848         if(self.goalcurrent.classname=="waypoint")
849         {
850                 if(vlen(self.origin - self.goalcurrent.origin)<150)
851                 {
852                         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
853                         if(trace_fraction==1)
854                         {
855                                 // Detect personal waypoints
856                                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
857                                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
858                                 {
859                                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
860                                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
861                                 }
862
863                                 navigation_poproute();
864                         }
865                 }
866         }
867
868         while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
869         {
870                 // Detect personal waypoints
871                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
872                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
873                 {
874                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
875                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
876                 }
877
878                 navigation_poproute();
879         }
880 }
881
882 // begin a goal selection session (queries spawnfunc_waypoint network)
883 void navigation_goalrating_start()
884 {
885         self.navigation_jetpack_goal = world;
886         navigation_bestrating = -1;
887         self.navigation_hasgoals = FALSE;
888         navigation_bestgoals_reset();
889         navigation_markroutes();
890 };
891
892 // ends a goal selection session (updates goal stack to the best goal)
893 void navigation_goalrating_end()
894 {
895         navigation_routetogoals();
896 //      dprint("best goal ", self.goalcurrent.classname , "\n");
897
898         // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
899         if not (self.navigation_hasgoals)
900         {
901                 dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
902
903                 entity head;
904
905                 RandomSelection_Init();
906                 head = findradius(self.origin,1000);
907                 while(head)
908                 {
909                         if(head.classname=="waypoint")
910                         if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
911                         if(vlen(self.origin-head.origin)>100)
912                         if(checkpvs(self.view_ofs,head))
913                                 RandomSelection_Add(head, 0, string_null, 1 + (vlen(self.origin-head.origin)<500), 0);
914                         head = head.chain;
915                 }
916                 if(RandomSelection_chosen_ent)
917                         navigation_routetogoal(RandomSelection_chosen_ent, self.origin);
918
919                 self.navigation_hasgoals = FALSE; // Reset this value
920         }
921 };
922
923 void botframe_updatedangerousobjects(float maxupdate)
924 {
925         local entity head, bot_dodgelist;
926         local vector m1, m2, v;
927         local float c, d, danger;
928         c = 0;
929         bot_dodgelist = findchainfloat(bot_dodge, TRUE);
930         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
931         while (botframe_dangerwaypoint != world)
932         {
933                 danger = 0;
934                 m1 = botframe_dangerwaypoint.mins;
935                 m2 = botframe_dangerwaypoint.maxs;
936                 head = bot_dodgelist;
937                 while (head)
938                 {
939                         v = head.origin;
940                         v_x = bound(m1_x, v_x, m2_x);
941                         v_y = bound(m1_y, v_y, m2_y);
942                         v_z = bound(m1_z, v_z, m2_z);
943                         d = head.bot_dodgerating - vlen(head.origin - v);
944                         if (d > 0)
945                         {
946                                 traceline(head.origin, v, TRUE, world);
947                                 if (trace_fraction == 1)
948                                         danger = danger + d;
949                         }
950                         head = head.chain;
951                 }
952                 botframe_dangerwaypoint.dmg = danger;
953                 c = c + 1;
954                 if (c >= maxupdate)
955                         break;
956                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
957         }
958 };
959
960 #ifdef DEBUG_TRACEWALK
961
962 void debugresetnodes()
963 {
964         debuglastnode = '0 0 0';
965 }
966
967 void debugnode(vector node)
968 {
969         if not(self.classname=="player")
970                 return;
971
972         if(debuglastnode=='0 0 0')
973         {
974                 debuglastnode = node;
975                 return;
976         }
977
978         te_lightning2(world, node, debuglastnode);
979         debuglastnode = node;
980 }
981
982 void debugnodestatus(vector position, float status)
983 {
984         vector color;
985
986         switch (status)
987         {
988                 case DEBUG_NODE_SUCCESS:
989                         color = '0 15 0';
990                         break;
991                 case DEBUG_NODE_WARNING:
992                         color = '15 15 0';
993                         break;
994                 case DEBUG_NODE_FAIL:
995                         color = '15 0 0';
996                         break;
997                 default:
998                         color = '15 15 15';
999         }
1000
1001         te_customflash(position, 40,  2, color);
1002 }
1003
1004 #endif
1005
1006 #ifdef DEBUG_BOT_GOALSTACK
1007
1008 .float goalcounter;
1009 .vector lastposition;
1010
1011 // Debug the goal stack visually
1012 void debuggoalstack()
1013 {
1014         local entity target;
1015         local vector org;
1016
1017         if(self.goalcounter==0)target=self.goalcurrent;
1018         else if(self.goalcounter==1)target=self.goalstack01;
1019         else if(self.goalcounter==2)target=self.goalstack02;
1020         else if(self.goalcounter==3)target=self.goalstack03;
1021         else if(self.goalcounter==4)target=self.goalstack04;
1022         else if(self.goalcounter==5)target=self.goalstack05;
1023         else if(self.goalcounter==6)target=self.goalstack06;
1024         else if(self.goalcounter==7)target=self.goalstack07;
1025         else if(self.goalcounter==8)target=self.goalstack08;
1026         else if(self.goalcounter==9)target=self.goalstack09;
1027         else if(self.goalcounter==10)target=self.goalstack10;
1028         else if(self.goalcounter==11)target=self.goalstack11;
1029         else if(self.goalcounter==12)target=self.goalstack12;
1030         else if(self.goalcounter==13)target=self.goalstack13;
1031         else if(self.goalcounter==14)target=self.goalstack14;
1032         else if(self.goalcounter==15)target=self.goalstack15;
1033         else if(self.goalcounter==16)target=self.goalstack16;
1034         else if(self.goalcounter==17)target=self.goalstack17;
1035         else if(self.goalcounter==18)target=self.goalstack18;
1036         else if(self.goalcounter==19)target=self.goalstack19;
1037         else if(self.goalcounter==20)target=self.goalstack20;
1038         else if(self.goalcounter==21)target=self.goalstack21;
1039         else if(self.goalcounter==22)target=self.goalstack22;
1040         else if(self.goalcounter==23)target=self.goalstack23;
1041         else if(self.goalcounter==24)target=self.goalstack24;
1042         else if(self.goalcounter==25)target=self.goalstack25;
1043         else if(self.goalcounter==26)target=self.goalstack26;
1044         else if(self.goalcounter==27)target=self.goalstack27;
1045         else if(self.goalcounter==28)target=self.goalstack28;
1046         else if(self.goalcounter==29)target=self.goalstack29;
1047         else if(self.goalcounter==30)target=self.goalstack30;
1048         else if(self.goalcounter==31)target=self.goalstack31;
1049
1050         if(target==world)
1051         {
1052                 self.goalcounter = 0;
1053                 self.lastposition='0 0 0';
1054                 return;
1055         }
1056
1057         if(self.lastposition=='0 0 0')
1058                 org = self.origin;
1059         else
1060                 org = self.lastposition;
1061
1062
1063         te_lightning2(world, org, target.origin);
1064         self.lastposition = target.origin;
1065
1066         self.goalcounter++;
1067 }
1068
1069 #endif