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