]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/bot/navigation.qc
s/print/dprint/g + update headers
[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 // NOTE: when a waypoint is added, the WP gets pushed first, then the
253 // next-closest WP on the shortest path to the WP
254 // That means, if the stack overflows, the bot will know how to do the FIRST 32
255 // steps to the goal, and then recalculate the path.
256 void navigation_pushroute(entity e)
257 {
258         //print("bot ", etos(self), " push ", etos(e), "\n");
259         self.goalstack31 = self.goalstack30;
260         self.goalstack30 = self.goalstack29;
261         self.goalstack29 = self.goalstack28;
262         self.goalstack28 = self.goalstack27;
263         self.goalstack27 = self.goalstack26;
264         self.goalstack26 = self.goalstack25;
265         self.goalstack25 = self.goalstack24;
266         self.goalstack24 = self.goalstack23;
267         self.goalstack23 = self.goalstack22;
268         self.goalstack22 = self.goalstack21;
269         self.goalstack21 = self.goalstack20;
270         self.goalstack20 = self.goalstack19;
271         self.goalstack19 = self.goalstack18;
272         self.goalstack18 = self.goalstack17;
273         self.goalstack17 = self.goalstack16;
274         self.goalstack16 = self.goalstack15;
275         self.goalstack15 = self.goalstack14;
276         self.goalstack14 = self.goalstack13;
277         self.goalstack13 = self.goalstack12;
278         self.goalstack12 = self.goalstack11;
279         self.goalstack11 = self.goalstack10;
280         self.goalstack10 = self.goalstack09;
281         self.goalstack09 = self.goalstack08;
282         self.goalstack08 = self.goalstack07;
283         self.goalstack07 = self.goalstack06;
284         self.goalstack06 = self.goalstack05;
285         self.goalstack05 = self.goalstack04;
286         self.goalstack04 = self.goalstack03;
287         self.goalstack03 = self.goalstack02;
288         self.goalstack02 = self.goalstack01;
289         self.goalstack01 = self.goalcurrent;
290         self.goalcurrent = e;
291 };
292
293 // remove first goal from stack
294 // (in other words: remove a prerequisite for reaching the later goals)
295 // (used when a spawnfunc_waypoint is reached)
296 void navigation_poproute()
297 {
298         //print("bot ", etos(self), " pop\n");
299         self.goalcurrent = self.goalstack01;
300         self.goalstack01 = self.goalstack02;
301         self.goalstack02 = self.goalstack03;
302         self.goalstack03 = self.goalstack04;
303         self.goalstack04 = self.goalstack05;
304         self.goalstack05 = self.goalstack06;
305         self.goalstack06 = self.goalstack07;
306         self.goalstack07 = self.goalstack08;
307         self.goalstack08 = self.goalstack09;
308         self.goalstack09 = self.goalstack10;
309         self.goalstack10 = self.goalstack11;
310         self.goalstack11 = self.goalstack12;
311         self.goalstack12 = self.goalstack13;
312         self.goalstack13 = self.goalstack14;
313         self.goalstack14 = self.goalstack15;
314         self.goalstack15 = self.goalstack16;
315         self.goalstack16 = self.goalstack17;
316         self.goalstack17 = self.goalstack18;
317         self.goalstack18 = self.goalstack19;
318         self.goalstack19 = self.goalstack20;
319         self.goalstack20 = self.goalstack21;
320         self.goalstack21 = self.goalstack22;
321         self.goalstack22 = self.goalstack23;
322         self.goalstack23 = self.goalstack24;
323         self.goalstack24 = self.goalstack25;
324         self.goalstack25 = self.goalstack26;
325         self.goalstack26 = self.goalstack27;
326         self.goalstack27 = self.goalstack28;
327         self.goalstack28 = self.goalstack29;
328         self.goalstack29 = self.goalstack30;
329         self.goalstack30 = self.goalstack31;
330         self.goalstack31 = world;
331 };
332
333 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
334 entity navigation_findnearestwaypoint(entity ent, float walkfromwp)
335 {
336         local entity waylist, w, best;
337         local float dist, bestdist;
338         local vector v, org, pm1, pm2;
339         pm1 = ent.origin + ent.mins;
340         pm2 = ent.origin + ent.maxs;
341         waylist = findchain(classname, "waypoint");
342
343         // do two scans, because box test is cheaper
344         w = waylist;
345         while (w)
346         {
347                 // if object is touching spawnfunc_waypoint
348                 if(w != ent)
349                         if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
350                                 return w;
351                 w = w.chain;
352         }
353
354         org = ent.origin + 0.5 * (ent.mins + ent.maxs);
355         org_z = ent.origin_z + ent.mins_z - PL_MIN_z; // player height
356         // TODO possibly make other code have the same support for bboxes
357         if(ent.tag_entity)
358                 org = org + ent.tag_entity.origin;
359         if (navigation_testtracewalk)
360                 te_plasmaburn(org);
361
362         best = world;
363         bestdist = 1050;
364
365         // box check failed, try walk
366         w = waylist;
367         while (w)
368         {
369                 // if object can walk from spawnfunc_waypoint
370                 if(w != ent)
371                 {
372                         if (w.wpisbox)
373                         {
374                                 local vector wm1, wm2;
375                                 wm1 = w.origin + w.mins;
376                                 wm2 = w.origin + w.maxs;
377                                 v_x = bound(wm1_x, org_x, wm2_x);
378                                 v_y = bound(wm1_y, org_y, wm2_y);
379                                 v_z = bound(wm1_z, org_z, wm2_z);
380                         }
381                         else
382                                 v = w.origin;
383                         dist = vlen(v - org);
384                         if (bestdist > dist)
385                         {
386                                 traceline(v, org, TRUE, ent);
387                                 if (trace_fraction == 1)
388                                 {
389                                         if (walkfromwp)
390                                         {
391                                                 //print("^1can I reach ", vtos(org), " from ", vtos(v), "?\n");
392                                                 if (tracewalk(ent, v, PL_MIN, PL_MAX, org, bot_navigation_movemode))
393                                                 {
394                                                         bestdist = dist;
395                                                         best = w;
396                                                 }
397                                         }
398                                         else
399                                         {
400                                                 if (tracewalk(ent, org, PL_MIN, PL_MAX, v, bot_navigation_movemode))
401                                                 {
402                                                         bestdist = dist;
403                                                         best = w;
404                                                 }
405                                         }
406                                 }
407                         }
408                 }
409                 w = w.chain;
410         }
411         return best;
412 }
413
414 // finds the waypoints near the bot initiating a navigation query
415 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
416 {
417         local entity head;
418         local vector v, m1, m2, diff;
419         local float c;
420 //      navigation_testtracewalk = TRUE;
421         c = 0;
422         head = waylist;
423         while (head)
424         {
425                 if (!head.wpconsidered)
426                 {
427                         if (head.wpisbox)
428                         {
429                                 m1 = head.origin + head.mins;
430                                 m2 = head.origin + head.maxs;
431                                 v = self.origin;
432                                 v_x = bound(m1_x, v_x, m2_x);
433                                 v_y = bound(m1_y, v_y, m2_y);
434                                 v_z = bound(m1_z, v_z, m2_z);
435                         }
436                         else
437                                 v = head.origin;
438                         diff = v - self.origin;
439                         diff_z = max(0, diff_z);
440                         if (vlen(diff) < maxdist)
441                         {
442                                 head.wpconsidered = TRUE;
443                                 if (tracewalk(self, self.origin, self.mins, self.maxs, v, bot_navigation_movemode))
444                                 {
445                                         head.wpnearestpoint = v;
446                                         head.wpcost = vlen(v - self.origin) + head.dmg;
447                                         head.wpfire = 1;
448                                         head.enemy = world;
449                                         c = c + 1;
450                                 }
451                         }
452                 }
453                 head = head.chain;
454         }
455         //navigation_testtracewalk = FALSE;
456         return c;
457 }
458
459 // updates a path link if a spawnfunc_waypoint link is better than the current one
460 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
461 {
462         local vector m1;
463         local vector m2;
464         local vector v;
465         if (wp.wpisbox)
466         {
467                 m1 = wp.absmin;
468                 m2 = wp.absmax;
469                 v_x = bound(m1_x, p_x, m2_x);
470                 v_y = bound(m1_y, p_y, m2_y);
471                 v_z = bound(m1_z, p_z, m2_z);
472         }
473         else
474                 v = wp.origin;
475         cost2 = cost2 + vlen(v - p);
476         if (wp.wpcost > cost2)
477         {
478                 wp.wpcost = cost2;
479                 wp.enemy = w;
480                 wp.wpfire = 1;
481                 wp.wpnearestpoint = v;
482         }
483 };
484
485 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
486 void navigation_markroutes(entity fixed_source_waypoint)
487 {
488         local entity w, wp, waylist;
489         local float searching, cost, cost2;
490         local vector p;
491         w = waylist = findchain(classname, "waypoint");
492         while (w)
493         {
494                 w.wpconsidered = FALSE;
495                 w.wpnearestpoint = '0 0 0';
496                 w.wpcost = 10000000;
497                 w.wpfire = 0;
498                 w.enemy = world;
499                 w = w.chain;
500         }
501
502         if(fixed_source_waypoint)
503         {
504                 fixed_source_waypoint.wpconsidered = TRUE;
505                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
506                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg;
507                 fixed_source_waypoint.wpfire = 1;
508                 fixed_source_waypoint.enemy = world;
509         }
510         else
511         {
512                 // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
513                 // as this search is expensive we will use lower values if the bot is on the air
514                 local float i, increment, maxdistance;
515                 if(self.flags & FL_ONGROUND)
516                 {
517                         increment = 750;
518                         maxdistance = 50000;
519                 }
520                 else
521                 {
522                         increment = 500;
523                         maxdistance = 1500;
524                 }
525
526                 for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
527         }
528
529         searching = TRUE;
530         while (searching)
531         {
532                 searching = FALSE;
533                 w = waylist;
534                 while (w)
535                 {
536                         if (w.wpfire)
537                         {
538                                 searching = TRUE;
539                                 w.wpfire = 0;
540                                 cost = w.wpcost;
541                                 p = w.wpnearestpoint;
542                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
543                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
544                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
545                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
546                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
547                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
548                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
549                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
550                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
551                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
552                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
553                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
554                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
555                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
556                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
557                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
558                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
559                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
560                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
561                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
562                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
563                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
564                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
565                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
566                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
567                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
568                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
569                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
570                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
571                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
572                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
573                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + w.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
574                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
575                         }
576                         w = w.chain;
577                 }
578         }
579 };
580
581 // queries the entire spawnfunc_waypoint network for pathes leading to the bot
582 void navigation_markroutes_inverted(entity fixed_source_waypoint)
583 {
584         local entity w, wp, waylist;
585         local float searching, cost, cost2;
586         local vector p;
587         w = waylist = findchain(classname, "waypoint");
588         while (w)
589         {
590                 w.wpconsidered = FALSE;
591                 w.wpnearestpoint = '0 0 0';
592                 w.wpcost = 10000000;
593                 w.wpfire = 0;
594                 w.enemy = world;
595                 w = w.chain;
596         }
597
598         if(fixed_source_waypoint)
599         {
600                 fixed_source_waypoint.wpconsidered = TRUE;
601                 fixed_source_waypoint.wpnearestpoint = fixed_source_waypoint.origin + 0.5 * (fixed_source_waypoint.mins + fixed_source_waypoint.maxs);
602                 fixed_source_waypoint.wpcost = fixed_source_waypoint.dmg; // the cost to get from X to fixed_source_waypoint
603                 fixed_source_waypoint.wpfire = 1;
604                 fixed_source_waypoint.enemy = world;
605         }
606         else
607         {
608                 error("need to start with a waypoint\n");
609         }
610
611         searching = TRUE;
612         while (searching)
613         {
614                 searching = FALSE;
615                 w = waylist;
616                 while (w)
617                 {
618                         if (w.wpfire)
619                         {
620                                 searching = TRUE;
621                                 w.wpfire = 0;
622                                 cost = w.wpcost; // cost to walk from w to home
623                                 p = w.wpnearestpoint;
624                                 for(wp = waylist; wp; wp = wp.chain)
625                                 {
626                                         if(w != wp.wp00) if(w != wp.wp01) if(w != wp.wp02) if(w != wp.wp03)
627                                         if(w != wp.wp04) if(w != wp.wp05) if(w != wp.wp06) if(w != wp.wp07)
628                                         if(w != wp.wp08) if(w != wp.wp09) if(w != wp.wp10) if(w != wp.wp11)
629                                         if(w != wp.wp12) if(w != wp.wp13) if(w != wp.wp14) if(w != wp.wp15)
630                                         if(w != wp.wp16) if(w != wp.wp17) if(w != wp.wp18) if(w != wp.wp19)
631                                         if(w != wp.wp20) if(w != wp.wp21) if(w != wp.wp22) if(w != wp.wp23)
632                                         if(w != wp.wp24) if(w != wp.wp25) if(w != wp.wp26) if(w != wp.wp27)
633                                         if(w != wp.wp28) if(w != wp.wp29) if(w != wp.wp30) if(w != wp.wp31)
634                                                 continue;
635                                         cost2 = cost + wp.dmg;
636                                         navigation_markroutes_checkwaypoint(w, wp, cost2, p);
637                                 }
638                         }
639                         w = w.chain;
640                 }
641         }
642 };
643
644 void navigation_bestgoals_reset()
645 {
646         local float i;
647
648         bestgoalswindex = 0;
649         bestgoalsrindex = 0;
650
651         for(i=0;i>MAX_BESTGOALS-1;++i)
652         {
653                 navigation_bestgoals[i] = world;
654         }
655 }
656
657 void navigation_add_bestgoal(entity goal)
658 {
659         if(bestgoalsrindex>0)
660         {
661                 ++bestgoalsrindex;
662
663                 if(bestgoalsrindex==MAX_BESTGOALS)
664                         bestgoalsrindex = 0;
665         }
666
667         if(bestgoalswindex==MAX_BESTGOALS)
668         {
669                 bestgoalswindex=0;
670                 if(bestgoalsrindex==0)
671                         bestgoalsrindex=1;
672         }
673
674         navigation_bestgoals[bestgoalswindex] = goal;
675
676         ++bestgoalswindex;
677 }
678
679 entity navigation_get_bestgoal()
680 {
681         local entity ent;
682
683         ent = navigation_bestgoals[bestgoalsrindex];
684         navigation_bestgoals[bestgoalsrindex] = world;
685
686         ++bestgoalsrindex;
687
688         if(bestgoalsrindex==MAX_BESTGOALS)
689                 bestgoalsrindex = 0;
690
691         return ent;
692 }
693
694 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
695 void navigation_routerating(entity e, float f, float rangebias)
696 {
697         entity nwp;
698         if (!e)
699                 return;
700
701         //print("routerating ", etos(e), " = ", ftos(f), " - ", ftos(rangebias), "\n");
702
703         // Evaluate path using jetpack
704         if(g_jetpack)
705         if(self.items & IT_JETPACK)
706         if(cvar("bot_ai_navigation_jetpack"))
707         if(vlen(self.origin - e.origin) > cvar("bot_ai_navigation_jetpack_mindistance"))
708         {
709                 vector pointa, pointb;
710
711         //      dprint("jetpack ai: evaluating path for ", e.classname,"\n");
712
713                 // Point A
714                 traceline(self.origin, self.origin + '0 0 65535', MOVE_NORMAL, self);
715                 pointa = trace_endpos - '0 0 1';
716
717                 // Point B
718                 traceline(e.origin, e.origin + '0 0 65535', MOVE_NORMAL, e);
719                 pointb = trace_endpos - '0 0 1';
720
721                 // Can I see these two points from the sky?
722                 traceline(pointa, pointb, MOVE_NORMAL, self);
723
724                 if(trace_fraction==1)
725                 {
726                 //      dprint("jetpack ai: can bridge these two points\n");
727
728                         // Lower the altitude of these points as much as possible
729                         local float zdistance, xydistance, cost, t, fuel;
730                         local vector down, npa, npb;
731
732                         down = '0 0 -1' * (PL_MAX_z - PL_MIN_z) * 10;
733
734                         do{
735                                 npa = pointa + down;
736                                 npb = pointb + down;
737
738                                 if(npa_z<=self.absmax_z)
739                                         break;
740
741                                 if(npb_z<=e.absmax_z)
742                                         break;
743
744                                 traceline(npa, npb, MOVE_NORMAL, self);
745                                 if(trace_fraction==1)
746                                 {
747                                         pointa = npa;
748                                         pointb = npb;
749                                 }
750                         }
751                         while(trace_fraction == 1);
752
753
754                         // Rough estimation of fuel consumption
755                         // (ignores acceleration and current xyz velocity)
756                         xydistance = vlen(pointa - pointb);
757                         zdistance = fabs(pointa_z - self.origin_z);
758
759                         t = zdistance / cvar("g_jetpack_maxspeed_up");
760                         t += xydistance / cvar("g_jetpack_maxspeed_side");
761                         fuel = t * cvar("g_jetpack_fuel") * 0.8;
762
763                 //      dprint("jetpack ai: required fuel ", ftos(fuel), " self.ammo_fuel ", ftos(self.ammo_fuel),"\n");
764
765                         // enough fuel ?
766                         if(self.ammo_fuel>fuel)
767                         {
768                                 // Estimate cost
769                                 // (as onground costs calculation is mostly based on distances, here we do the same establishing some relationship
770                                 //  - between air and ground speeds)
771
772                                 cost = xydistance / (cvar("g_jetpack_maxspeed_side")/cvar("sv_maxspeed"));
773                                 cost += zdistance / (cvar("g_jetpack_maxspeed_up")/cvar("sv_maxspeed"));
774                                 cost *= 1.5;
775
776                                 // Compare against other goals
777                                 f = f * rangebias / (rangebias + cost);
778
779                                 if (navigation_bestrating < f)
780                                 {
781                         //              dprint("jetpack path: added goal", e.classname, " (with rating ", ftos(f), ")\n");
782                                         navigation_bestrating = f;
783                                         navigation_add_bestgoal(e);
784                                         self.navigation_jetpack_goal = e;
785                                         self.navigation_jetpack_point = pointb;
786                                 }
787                                 return;
788                         }
789                 }
790         }
791
792         //te_wizspike(e.origin);
793         //bprint(etos(e));
794         //bprint("\n");
795         // update the cached spawnfunc_waypoint link on a dynamic item entity
796         if(e.classname == "waypoint" && !(e.wpflags & WAYPOINTFLAG_PERSONAL))
797         {
798                 nwp = e;
799         }
800         else
801         {
802                 if (time > e.nearestwaypointtimeout)
803                 {
804                         nwp = navigation_findnearestwaypoint(e, TRUE);
805                         if(nwp)
806                                 e.nearestwaypoint = nwp;
807                         else
808                                 dprint("FAILED to find a nearest waypoint to '", e.classname, "' #", etos(e), "\n");
809
810                         // TODO: Cleaner solution, probably handling this timeout from ctf.qc
811                         if(e.classname=="item_flag_team")
812                                 e.nearestwaypointtimeout = time + 2;
813                         else
814                                 e.nearestwaypointtimeout = time + random() * 3 + 5;
815                 }
816                 nwp = e.nearestwaypoint;
817         }
818
819         //dprint("-- checking ", e.classname, " (with cost ", ftos(nwp.wpcost), ")\n");
820         if (nwp)
821         if (nwp.wpcost < 10000000)
822         {
823                 //te_wizspike(nwp.wpnearestpoint);
824         //      dprint(e.classname, " ", ftos(f), "/(1+", ftos((nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint))), "/", ftos(rangebias), ") = ");
825                 f = f * rangebias / (rangebias + (nwp.wpcost + vlen(e.origin - nwp.wpnearestpoint)));
826                 //dprint("considering ", e.classname, " (with rating ", ftos(f), ")\n");
827                 //dprint(ftos(f));
828                 if (navigation_bestrating < f)
829                 {
830                 //      dprint("ground path: added goal ", e.classname, " (with rating ", ftos(f), ")\n");
831                         navigation_bestrating = f;
832                         navigation_add_bestgoal(e);
833                 }
834         }
835         //dprint("\n");
836 };
837
838 // adds an item to the the goal stack with the path to a given item
839 float navigation_routetogoal(entity e, vector startposition)
840 {
841         self.goalentity = e;
842
843         // if there is no goal, just exit
844         if (!e)
845                 return FALSE;
846
847         self.navigation_hasgoals = TRUE;
848
849         // put the entity on the goal stack
850         //print("routetogoal ", etos(e), "\n");
851         navigation_pushroute(e);
852
853         if(g_jetpack)
854         if(e==self.navigation_jetpack_goal)
855                 return TRUE;
856
857         // if it can reach the goal there is nothing more to do
858         if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, bot_navigation_movemode))
859                 return TRUE;
860
861         // see if there are waypoints describing a path to the item
862         if(e.classname != "waypoint" || (e.wpflags & WAYPOINTFLAG_PERSONAL))
863                 e = e.nearestwaypoint;
864         else
865                 e = e.enemy; // we already have added it, so...
866
867         if(e == world)
868                 return FALSE;
869
870         for (;;)
871         {
872                 // add the spawnfunc_waypoint to the path
873                 navigation_pushroute(e);
874                 e = e.enemy;
875
876                 if(e==world)
877                         break;
878         }
879
880         return FALSE;
881 };
882
883 void navigation_routetogoals()
884 {
885         entity g1, g2;
886
887         navigation_clearroute();
888
889         g1 = navigation_get_bestgoal();
890         for(;;)
891         {
892                 if(g2==world)
893                         g2 = navigation_get_bestgoal();
894
895                 if(g2==world)
896                 {
897                         navigation_routetogoal(g1, self.origin);
898                         return;
899                 }
900
901                 if(navigation_routetogoal(g1, g2.origin))
902                 {
903                         g1 = g2;
904                         g2 = world;
905                         continue;
906                 }
907
908                 navigation_clearroute();
909                 g1 = g2;
910                 g2 = world;
911         }
912 }
913
914 // removes any currently touching waypoints from the goal stack
915 // (this is how bots detect if they reached a goal)
916 void navigation_poptouchedgoals()
917 {
918         local vector org, m1, m2;
919         org = self.origin;
920         m1 = org + self.mins;
921         m2 = org + self.maxs;
922
923         if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
924         {
925                 if(self.lastteleporttime>0)
926                 if(time-self.lastteleporttime<0.15)
927                 {
928                         navigation_poproute();
929                         return;
930                 }
931         }
932
933         // Loose goal touching check when running
934         if(self.aistatus & AI_STATUS_RUNNING)
935         if(self.goalcurrent.classname=="waypoint")
936         {
937                 if(vlen(self.origin - self.goalcurrent.origin)<150)
938                 {
939                         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
940                         if(trace_fraction==1)
941                         {
942                                 // Detect personal waypoints
943                                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
944                                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
945                                 {
946                                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
947                                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
948                                 }
949
950                                 navigation_poproute();
951                         }
952                 }
953         }
954
955         while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
956         {
957                 // Detect personal waypoints
958                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
959                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
960                 {
961                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
962                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
963                 }
964
965                 navigation_poproute();
966         }
967 }
968
969 // begin a goal selection session (queries spawnfunc_waypoint network)
970 void navigation_goalrating_start()
971 {
972         self.navigation_jetpack_goal = world;
973         navigation_bestrating = -1;
974         self.navigation_hasgoals = FALSE;
975         navigation_bestgoals_reset();
976         navigation_markroutes(world);
977 };
978
979 // ends a goal selection session (updates goal stack to the best goal)
980 void navigation_goalrating_end()
981 {
982         navigation_routetogoals();
983 //      dprint("best goal ", self.goalcurrent.classname , "\n");
984
985         // Hack: if it can't walk to any goal just move blindly to the first visible waypoint
986         if not (self.navigation_hasgoals)
987         {
988                 dprint(self.netname, " can't walk to any goal, going to a near waypoint\n");
989
990                 entity head;
991
992                 RandomSelection_Init();
993                 head = findradius(self.origin,1000);
994                 while(head)
995                 {
996                         if(head.classname=="waypoint")
997                         if(!(head.wpflags & WAYPOINTFLAG_GENERATED))
998                         if(vlen(self.origin-head.origin)>100)
999                         if(checkpvs(self.view_ofs,head))
1000                                 RandomSelection_Add(head, 0, string_null, 1 + (vlen(self.origin-head.origin)<500), 0);
1001                         head = head.chain;
1002                 }
1003                 if(RandomSelection_chosen_ent)
1004                         navigation_routetogoal(RandomSelection_chosen_ent, self.origin);
1005
1006                 self.navigation_hasgoals = FALSE; // Reset this value
1007         }
1008 };
1009
1010 void botframe_updatedangerousobjects(float maxupdate)
1011 {
1012         local entity head, bot_dodgelist;
1013         local vector m1, m2, v;
1014         local float c, d, danger;
1015         c = 0;
1016         bot_dodgelist = findchainfloat(bot_dodge, TRUE);
1017         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1018         while (botframe_dangerwaypoint != world)
1019         {
1020                 danger = 0;
1021                 m1 = botframe_dangerwaypoint.mins;
1022                 m2 = botframe_dangerwaypoint.maxs;
1023                 head = bot_dodgelist;
1024                 while (head)
1025                 {
1026                         v = head.origin;
1027                         v_x = bound(m1_x, v_x, m2_x);
1028                         v_y = bound(m1_y, v_y, m2_y);
1029                         v_z = bound(m1_z, v_z, m2_z);
1030                         d = head.bot_dodgerating - vlen(head.origin - v);
1031                         if (d > 0)
1032                         {
1033                                 traceline(head.origin, v, TRUE, world);
1034                                 if (trace_fraction == 1)
1035                                         danger = danger + d;
1036                         }
1037                         head = head.chain;
1038                 }
1039                 botframe_dangerwaypoint.dmg = danger;
1040                 c = c + 1;
1041                 if (c >= maxupdate)
1042                         break;
1043                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1044         }
1045 };
1046
1047 #ifdef DEBUG_TRACEWALK
1048
1049 void debugresetnodes()
1050 {
1051         debuglastnode = '0 0 0';
1052 }
1053
1054 void debugnode(vector node)
1055 {
1056         if not(self.classname=="player")
1057                 return;
1058
1059         if(debuglastnode=='0 0 0')
1060         {
1061                 debuglastnode = node;
1062                 return;
1063         }
1064
1065         te_lightning2(world, node, debuglastnode);
1066         debuglastnode = node;
1067 }
1068
1069 void debugnodestatus(vector position, float status)
1070 {
1071         vector color;
1072
1073         switch (status)
1074         {
1075                 case DEBUG_NODE_SUCCESS:
1076                         color = '0 15 0';
1077                         break;
1078                 case DEBUG_NODE_WARNING:
1079                         color = '15 15 0';
1080                         break;
1081                 case DEBUG_NODE_FAIL:
1082                         color = '15 0 0';
1083                         break;
1084                 default:
1085                         color = '15 15 15';
1086         }
1087
1088         te_customflash(position, 40,  2, color);
1089 }
1090
1091 #endif
1092
1093 #ifdef DEBUG_BOT_GOALSTACK
1094
1095 .float goalcounter;
1096 .vector lastposition;
1097
1098 // Debug the goal stack visually
1099 void debuggoalstack()
1100 {
1101         local entity target;
1102         local vector org;
1103
1104         if(self.goalcounter==0)target=self.goalcurrent;
1105         else if(self.goalcounter==1)target=self.goalstack01;
1106         else if(self.goalcounter==2)target=self.goalstack02;
1107         else if(self.goalcounter==3)target=self.goalstack03;
1108         else if(self.goalcounter==4)target=self.goalstack04;
1109         else if(self.goalcounter==5)target=self.goalstack05;
1110         else if(self.goalcounter==6)target=self.goalstack06;
1111         else if(self.goalcounter==7)target=self.goalstack07;
1112         else if(self.goalcounter==8)target=self.goalstack08;
1113         else if(self.goalcounter==9)target=self.goalstack09;
1114         else if(self.goalcounter==10)target=self.goalstack10;
1115         else if(self.goalcounter==11)target=self.goalstack11;
1116         else if(self.goalcounter==12)target=self.goalstack12;
1117         else if(self.goalcounter==13)target=self.goalstack13;
1118         else if(self.goalcounter==14)target=self.goalstack14;
1119         else if(self.goalcounter==15)target=self.goalstack15;
1120         else if(self.goalcounter==16)target=self.goalstack16;
1121         else if(self.goalcounter==17)target=self.goalstack17;
1122         else if(self.goalcounter==18)target=self.goalstack18;
1123         else if(self.goalcounter==19)target=self.goalstack19;
1124         else if(self.goalcounter==20)target=self.goalstack20;
1125         else if(self.goalcounter==21)target=self.goalstack21;
1126         else if(self.goalcounter==22)target=self.goalstack22;
1127         else if(self.goalcounter==23)target=self.goalstack23;
1128         else if(self.goalcounter==24)target=self.goalstack24;
1129         else if(self.goalcounter==25)target=self.goalstack25;
1130         else if(self.goalcounter==26)target=self.goalstack26;
1131         else if(self.goalcounter==27)target=self.goalstack27;
1132         else if(self.goalcounter==28)target=self.goalstack28;
1133         else if(self.goalcounter==29)target=self.goalstack29;
1134         else if(self.goalcounter==30)target=self.goalstack30;
1135         else if(self.goalcounter==31)target=self.goalstack31;
1136
1137         if(target==world)
1138         {
1139                 self.goalcounter = 0;
1140                 self.lastposition='0 0 0';
1141                 return;
1142         }
1143
1144         if(self.lastposition=='0 0 0')
1145                 org = self.origin;
1146         else
1147                 org = self.lastposition;
1148
1149
1150         te_lightning2(world, org, target.origin);
1151         self.lastposition = target.origin;
1152
1153         self.goalcounter++;
1154 }
1155
1156 #endif