]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/bots.qc
Reverted change commited accidentaly
[divverent/nexuiz.git] / data / qcsrc / server / bots.qc
1 .float aistatus;
2 float AI_STATUS_ROAMING                         = 1;    // Bot is just crawling the map. No enemies at sight
3 float AI_STATUS_ATTACKING                       = 2;    // There are enemies at sight
4 float AI_STATUS_RUNNING                         = 4;    // Bot is bunny hopping
5 float AI_STATUS_DANGER_AHEAD                    = 8;    // There is lava/slime/trigger_hurt ahead
6 float AI_STATUS_OUT_JUMPPAD                     = 16;   // Trying to get out of a "vertical" jump pad
7 float AI_STATUS_OUT_WATER                       = 32;   // Trying to get out of water
8 float AI_STATUS_WAYPOINT_PERSONAL_GOING         = 64;   // Going to a personal waypoint
9 float AI_STATUS_WAYPOINT_PERSONAL_REACHED       = 128;  // Personal waypoint reached
10
11 // utilities for path debugging
12 #ifdef DEBUG_TRACEWALK
13
14 float DEBUG_NODE_SUCCESS        = 1;
15 float DEBUG_NODE_WARNING        = 2;
16 float DEBUG_NODE_FAIL           = 3;
17
18 vector debuglastnode;
19
20 void debugresetnodes()
21 {
22         debuglastnode = '0 0 0';
23 }
24
25 void debugnode(vector node)
26 {
27         if not(self.classname=="player")
28                 return;
29
30         if(debuglastnode=='0 0 0')
31         {
32                 debuglastnode = node;
33                 return;
34         }
35
36         te_lightning2(world, node, debuglastnode);
37         debuglastnode = node;
38 }
39
40 void debugnodestatus(vector position, float status)
41 {
42         vector color;
43
44         switch (status)
45         {
46                 case DEBUG_NODE_SUCCESS:
47                         color = '0 15 0';
48                         break;
49                 case DEBUG_NODE_WARNING:
50                         color = '15 15 0';
51                         break;
52                 case DEBUG_NODE_FAIL:
53                         color = '15 0 0';
54                         break;
55                 default:
56                         color = '15 15 15';
57         }
58
59         te_customflash(position, 40,  2, color);
60 }
61
62 #endif
63
64 // rough simulation of walking from one point to another to test if a path
65 // can be traveled, used for waypoint linking and havocbot
66
67 vector stepheightvec;
68 float navigation_testtracewalk;
69 float tracewalk(entity e, vector start, vector m1, vector m2, vector end, float movemode)
70 {
71         local vector org;
72         local vector move;
73         local vector dir;
74         local float dist;
75         local float totaldist;
76         local float stepdist;
77         local float yaw;
78         local float ignorehazards;
79         local float swimming;
80
81         #ifdef DEBUG_TRACEWALK
82                 debugresetnodes();
83                 debugnode(start);
84         #endif
85
86         move = end - start;
87         move_z = 0;
88         org = start;
89         dist = totaldist = vlen(move);
90         dir = normalize(move);
91         stepdist = 32;
92         ignorehazards = FALSE;
93
94         // Analyze starting point
95         traceline(start, start, MOVE_NORMAL, e);
96         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
97                 ignorehazards = TRUE;
98         else
99         {
100                 traceline( start, start + '0 0 -65536', MOVE_NORMAL, e);
101                 if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
102                 {
103                         ignorehazards = TRUE;
104                         swimming = TRUE;
105                 }
106         }
107         tracebox(start, m1, m2, start, MOVE_NOMONSTERS, e);
108         if (trace_startsolid)
109         {
110                 // Bad start
111                 #ifdef DEBUG_TRACEWALK
112                         debugnodestatus(start, DEBUG_NODE_FAIL);
113                 #endif
114                 return FALSE;
115         }
116
117         // Movement loop
118         yaw = vectoyaw(move);
119         move = end - org;
120         for (;;)
121         {
122                 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
123                 {
124                         // Succeeded
125                         #ifdef DEBUG_TRACEWALK
126                                 debugnodestatus(org, DEBUG_NODE_SUCCESS);
127                         #endif
128                         return TRUE;
129                 }
130                 #ifdef DEBUG_TRACEWALK
131                         debugnode(org);
132                 #endif
133
134                 if (dist <= 0)
135                         break;
136                 if (stepdist > dist)
137                         stepdist = dist;
138                 dist = dist - stepdist;
139                 traceline(org, org, MOVE_NORMAL, e);
140                 if (!ignorehazards)
141                 {
142                         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
143                         {
144                                 // hazards blocking path
145                                 #ifdef DEBUG_TRACEWALK
146                                         debugnodestatus(org, DEBUG_NODE_FAIL);
147                                 #endif
148                                 return FALSE;
149                         }
150                 }
151                 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
152                 {
153                         move = normalize(end - org);
154                         tracebox(org, m1, m2, org + move * stepdist, movemode, e);
155
156                         #ifdef DEBUG_TRACEWALK
157                                 debugnode(trace_endpos);
158                         #endif
159
160                         if (trace_fraction < 1)
161                         {
162                                 swimming = TRUE;
163                                 org = trace_endpos - normalize(org - trace_endpos) * stepdist;
164                                 for(; org_z < end_z + self.maxs_z; org_z += stepdist)
165                                 {
166                                                 #ifdef DEBUG_TRACEWALK
167                                                         debugnode(org);
168                                                 #endif
169                                         if(pointcontents(org) == CONTENT_EMPTY)
170                                                         break;
171                                 }
172
173                                 if not (pointcontents(org + '0 0 1') == CONTENT_EMPTY)
174                                 {
175                                         #ifdef DEBUG_TRACEWALK
176                                                 debugnodestatus(org, DEBUG_NODE_FAIL);
177                                         #endif
178                                         return FALSE;
179                                 }
180                                 continue;
181
182                         }
183                         else
184                                 org = trace_endpos;
185                 }
186                 else
187                 {
188                         move = dir * stepdist + org;
189                         tracebox(org, m1, m2, move, movemode, e);
190
191                         #ifdef DEBUG_TRACEWALK
192                                 debugnode(trace_endpos);
193                         #endif
194
195                         // hit something
196                         if (trace_fraction < 1)
197                         {
198                                 // check if we can walk over this obstacle
199                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, movemode, e);
200                                 if (trace_fraction < 1 || trace_startsolid)
201                                 {
202                                         #ifdef DEBUG_TRACEWALK
203                                                 debugnodestatus(trace_endpos, DEBUG_NODE_WARNING);
204                                         #endif
205
206                                         // check for doors
207                                         traceline( org, move, movemode, e);
208                                         if ( trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
209                                         {
210                                                 local vector nextmove;
211                                                 move = trace_endpos;
212                                                 while(trace_ent.classname == "door_rotating" || trace_ent.classname == "door")
213                                                 {
214                                                         nextmove = move + (dir * stepdist);
215                                                         traceline( move, nextmove, movemode, e);
216                                                         move = nextmove;
217                                                 }
218                                         }
219                                         else
220                                         {
221                                                 #ifdef DEBUG_TRACEWALK
222                                                         debugnodestatus(trace_endpos, DEBUG_NODE_FAIL);
223                                                 #endif
224                                                 return FALSE; // failed
225                                         }
226                                 }
227                                 else
228                                         move = trace_endpos;
229                         }
230                         else
231                                 move = trace_endpos;
232
233                         // trace down from stepheight as far as possible and move there,
234                         // if this starts in solid we try again without the stepup, and
235                         // if that also fails we assume it is a wall
236                         // (this is the same logic as the Quake walkmove function used)
237                         tracebox(move, m1, m2, move + '0 0 -65536', movemode, e);
238
239                         // moved successfully
240                         if(swimming)
241                         {
242                                 local float c;
243                                 c = pointcontents(org + '0 0 1');
244                                 if not(c == CONTENT_WATER || c == CONTENT_LAVA || c == CONTENT_SLIME)
245                                         swimming = FALSE;
246                                 else
247                                         continue;
248                         }
249
250                         org = trace_endpos;
251                 }
252         }
253
254         // moved but didn't arrive at the intended destination
255         #ifdef DEBUG_TRACEWALK
256                 debugnodestatus(org, DEBUG_NODE_FAIL);
257         #endif
258
259         return FALSE;
260 };
261
262
263 // grenade tracing to decide the best pitch to fire at
264
265 entity tracetossent;
266 entity tracetossfaketarget;
267
268 // traces multiple trajectories to find one that will impact the target
269 // 'end' vector is the place it aims for,
270 // returns TRUE only if it hit targ (don't target non-solid entities)
271 vector findtrajectory_velocity;
272 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
273 {
274         local float c, savesolid, shottime;
275         local vector dir, end, v;
276         if (shotspeed < 1)
277                 return FALSE; // could cause division by zero if calculated
278         if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
279                 return FALSE; // could never hit it
280         if (!tracetossent)
281                 tracetossent = spawn();
282         tracetossent.owner = ignore;
283         setsize(tracetossent, m1, m2);
284         savesolid = targ.solid;
285         targ.solid = SOLID_NOT;
286         shottime = ((vlen(targ.origin - org) / shotspeed) + shotdelay);
287         v = targ.velocity * shottime + targ.origin;
288         tracebox(targ.origin, targ.mins, targ.maxs, v, FALSE, targ);
289         v = trace_endpos;
290         end = v + (targ.mins + targ.maxs) * 0.5;
291         if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
292         {
293                 // out of range
294                 targ.solid = savesolid;
295                 return FALSE;
296         }
297
298         if (!tracetossfaketarget)
299                 tracetossfaketarget = spawn();
300         tracetossfaketarget.solid = savesolid;
301         tracetossfaketarget.movetype = targ.movetype;
302         setmodel(tracetossfaketarget, targ.model); // no low precision
303         tracetossfaketarget.model = targ.model;
304         tracetossfaketarget.modelindex = targ.modelindex;
305         setsize(tracetossfaketarget, targ.mins, targ.maxs);
306         setorigin(tracetossfaketarget, v);
307
308         c = 0;
309         dir = normalize(end - org);
310         while (c < 10) // 10 traces
311         {
312                 setorigin(tracetossent, org); // reset
313                 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
314                 tracetoss(tracetossent, ignore); // love builtin functions...
315                 if (trace_ent == tracetossfaketarget) // done
316                 {
317                         targ.solid = savesolid;
318
319                         // make it disappear
320                         tracetossfaketarget.solid = SOLID_NOT;
321                         tracetossfaketarget.movetype = MOVETYPE_NONE;
322                         tracetossfaketarget.model = "";
323                         tracetossfaketarget.modelindex = 0;
324                         // relink to remove it from physics considerations
325                         setorigin(tracetossfaketarget, v);
326
327                         return TRUE;
328                 }
329                 dir_z = dir_z + 0.1; // aim up a little more
330                 c = c + 1;
331         }
332         targ.solid = savesolid;
333
334         // make it disappear
335         tracetossfaketarget.solid = SOLID_NOT;
336         tracetossfaketarget.movetype = MOVETYPE_NONE;
337         tracetossfaketarget.model = "";
338         tracetossfaketarget.modelindex = 0;
339         // relink to remove it from physics considerations
340         setorigin(tracetossfaketarget, v);
341
342         // leave a valid one even if it won't reach
343         findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
344         return FALSE;
345 };
346
347
348
349 // lag simulation
350
351 .void(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) lag_func;
352
353 // upto 5 queued messages
354 .float lag1_time;
355 .float lag1_float1;
356 .float lag1_float2;
357 .entity lag1_entity1;
358 .vector lag1_vec1;
359 .vector lag1_vec2;
360 .vector lag1_vec3;
361 .vector lag1_vec4;
362
363 .float lag2_time;
364 .float lag2_float1;
365 .float lag2_float2;
366 .entity lag2_entity1;
367 .vector lag2_vec1;
368 .vector lag2_vec2;
369 .vector lag2_vec3;
370 .vector lag2_vec4;
371
372 .float lag3_time;
373 .float lag3_float1;
374 .float lag3_float2;
375 .entity lag3_entity1;
376 .vector lag3_vec1;
377 .vector lag3_vec2;
378 .vector lag3_vec3;
379 .vector lag3_vec4;
380
381 .float lag4_time;
382 .float lag4_float1;
383 .float lag4_float2;
384 .entity lag4_entity1;
385 .vector lag4_vec1;
386 .vector lag4_vec2;
387 .vector lag4_vec3;
388 .vector lag4_vec4;
389
390 .float lag5_time;
391 .float lag5_float1;
392 .float lag5_float2;
393 .entity lag5_entity1;
394 .vector lag5_vec1;
395 .vector lag5_vec2;
396 .vector lag5_vec3;
397 .vector lag5_vec4;
398
399 void lag_update()
400 {
401         if (self.lag1_time) if (time > self.lag1_time) {self.lag_func(self.lag1_time, self.lag1_float1, self.lag1_float2, self.lag1_entity1, self.lag1_vec1, self.lag1_vec2, self.lag1_vec3, self.lag1_vec4);self.lag1_time = 0;}
402         if (self.lag2_time) if (time > self.lag2_time) {self.lag_func(self.lag2_time, self.lag2_float1, self.lag2_float2, self.lag2_entity1, self.lag2_vec1, self.lag2_vec2, self.lag2_vec3, self.lag2_vec4);self.lag2_time = 0;}
403         if (self.lag3_time) if (time > self.lag3_time) {self.lag_func(self.lag3_time, self.lag3_float1, self.lag3_float2, self.lag3_entity1, self.lag3_vec1, self.lag3_vec2, self.lag3_vec3, self.lag3_vec4);self.lag3_time = 0;}
404         if (self.lag4_time) if (time > self.lag4_time) {self.lag_func(self.lag4_time, self.lag4_float1, self.lag4_float2, self.lag4_entity1, self.lag4_vec1, self.lag4_vec2, self.lag4_vec3, self.lag4_vec4);self.lag4_time = 0;}
405         if (self.lag5_time) if (time > self.lag5_time) {self.lag_func(self.lag5_time, self.lag5_float1, self.lag5_float2, self.lag5_entity1, self.lag5_vec1, self.lag5_vec2, self.lag5_vec3, self.lag5_vec4);self.lag5_time = 0;}
406 };
407
408 float lag_additem(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
409 {
410         if (self.lag1_time == 0) {self.lag1_time = t;self.lag1_float1 = f1;self.lag1_float2 = f2;self.lag1_entity1 = e1;self.lag1_vec1 = v1;self.lag1_vec2 = v2;self.lag1_vec3 = v3;self.lag1_vec4 = v4;return TRUE;}
411         if (self.lag2_time == 0) {self.lag2_time = t;self.lag2_float1 = f1;self.lag2_float2 = f2;self.lag2_entity1 = e1;self.lag2_vec1 = v1;self.lag2_vec2 = v2;self.lag2_vec3 = v3;self.lag2_vec4 = v4;return TRUE;}
412         if (self.lag3_time == 0) {self.lag3_time = t;self.lag3_float1 = f1;self.lag3_float2 = f2;self.lag3_entity1 = e1;self.lag3_vec1 = v1;self.lag3_vec2 = v2;self.lag3_vec3 = v3;self.lag3_vec4 = v4;return TRUE;}
413         if (self.lag4_time == 0) {self.lag4_time = t;self.lag4_float1 = f1;self.lag4_float2 = f2;self.lag4_entity1 = e1;self.lag4_vec1 = v1;self.lag4_vec2 = v2;self.lag4_vec3 = v3;self.lag4_vec4 = v4;return TRUE;}
414         if (self.lag5_time == 0) {self.lag5_time = t;self.lag5_float1 = f1;self.lag5_float2 = f2;self.lag5_entity1 = e1;self.lag5_vec1 = v1;self.lag5_vec2 = v2;self.lag5_vec3 = v3;self.lag5_vec4 = v4;return TRUE;}
415         // no room for it (what is the best thing to do here??)
416         return FALSE;
417 };
418
419
420 // Random skill system
421 .float bot_thinkskill;
422 .float bot_mouseskill;
423 .float bot_predictionskill;
424 .float bot_offsetskill;
425
426
427 // spawnfunc_waypoint navigation system
428
429 // itemscore = (howmuchmoreIwant / howmuchIcanwant) / itemdistance
430 // waypointscore = 0.7 / waypointdistance
431
432 .entity wp00, wp01, wp02, wp03, wp04, wp05, wp06, wp07;
433 .entity wp08, wp09, wp10, wp11, wp12, wp13, wp14, wp15;
434 .entity wp16, wp17, wp18, wp19, wp20, wp21, wp22, wp23, wp24, wp25, wp26, wp27, wp28, wp29, wp30, wp31;
435 .float wp00mincost, wp01mincost, wp02mincost, wp03mincost, wp04mincost, wp05mincost, wp06mincost, wp07mincost;
436 .float wp08mincost, wp09mincost, wp10mincost, wp11mincost, wp12mincost, wp13mincost, wp14mincost, wp15mincost;
437 .float wp16mincost, wp17mincost, wp18mincost, wp19mincost, wp20mincost, wp21mincost, wp22mincost, wp23mincost, wp24mincost, wp25mincost, wp26mincost, wp27mincost, wp28mincost, wp29mincost, wp30mincost, wp31mincost;
438 .float wpfire, wpcost, wpconsidered;
439 .float wpisbox;
440 .float wpflags;
441 .vector wpnearestpoint;
442
443 // stack of current goals (the last one of which may be an item or other
444 // desirable object, the rest are typically waypoints to reach it)
445 .entity goalcurrent, goalstack01, goalstack02, goalstack03;
446 .entity goalstack04, goalstack05, goalstack06, goalstack07;
447 .entity goalstack08, goalstack09, goalstack10, goalstack11;
448 .entity goalstack12, goalstack13, goalstack14, goalstack15;
449 .entity goalstack16, goalstack17, goalstack18, goalstack19;
450 .entity goalstack20, goalstack21, goalstack22, goalstack23;
451 .entity goalstack24, goalstack25, goalstack26, goalstack27;
452 .entity goalstack28, goalstack29, goalstack30, goalstack31;
453
454 .entity nearestwaypoint;
455 .float nearestwaypointtimeout;
456
457 // used during navigation_goalrating_begin/end sessions
458 #define MAX_BESTGOALS 3
459 float bestgoalswindex;
460 float bestgoalsrindex;
461 float navigation_bestrating;
462 entity navigation_bestgoals[MAX_BESTGOALS];
463 .float navigation_hasgoals;
464
465 /////////////////////////////////////////////////////////////////////////////
466 // spawnfunc_waypoint management
467 /////////////////////////////////////////////////////////////////////////////
468
469 // waypoints with this flag are not saved, they are automatically generated
470 // waypoints like jump pads, teleporters, and items
471 float WAYPOINTFLAG_GENERATED = 8388608;
472 float WAYPOINTFLAG_ITEM = 4194304;
473 float WAYPOINTFLAG_TELEPORT = 2097152;
474 float WAYPOINTFLAG_NORELINK = 1048576;
475 float WAYPOINTFLAG_PERSONAL = 524288;
476
477 // add a new link to the spawnfunc_waypoint, replacing the furthest link it already has
478 void waypoint_addlink(entity from, entity to)
479 {
480         local float c;
481
482         if (from == to)
483                 return;
484         if (from.wpflags & WAYPOINTFLAG_NORELINK)
485                 return;
486
487         if (from.wp00 == to) return;if (from.wp01 == to) return;if (from.wp02 == to) return;if (from.wp03 == to) return;
488         if (from.wp04 == to) return;if (from.wp05 == to) return;if (from.wp06 == to) return;if (from.wp07 == to) return;
489         if (from.wp08 == to) return;if (from.wp09 == to) return;if (from.wp10 == to) return;if (from.wp11 == to) return;
490         if (from.wp12 == to) return;if (from.wp13 == to) return;if (from.wp14 == to) return;if (from.wp15 == to) return;
491         if (from.wp16 == to) return;if (from.wp17 == to) return;if (from.wp18 == to) return;if (from.wp19 == to) return;
492         if (from.wp20 == to) return;if (from.wp21 == to) return;if (from.wp22 == to) return;if (from.wp23 == to) return;
493         if (from.wp24 == to) return;if (from.wp25 == to) return;if (from.wp26 == to) return;if (from.wp27 == to) return;
494         if (from.wp28 == to) return;if (from.wp29 == to) return;if (from.wp30 == to) return;if (from.wp31 == to) return;
495
496         if (to.wpisbox || from.wpisbox)
497         {
498                 // if either is a box we have to find the nearest points on them to
499                 // calculate the distance properly
500                 local vector v1, v2, m1, m2;
501                 v1 = from.origin;
502                 m1 = to.absmin;
503                 m2 = to.absmax;
504                 v1_x = bound(m1_x, v1_x, m2_x);
505                 v1_y = bound(m1_y, v1_y, m2_y);
506                 v1_z = bound(m1_z, v1_z, m2_z);
507                 v2 = to.origin;
508                 m1 = from.absmin;
509                 m2 = from.absmax;
510                 v2_x = bound(m1_x, v2_x, m2_x);
511                 v2_y = bound(m1_y, v2_y, m2_y);
512                 v2_z = bound(m1_z, v2_z, m2_z);
513                 v2 = to.origin;
514                 c = vlen(v2 - v1);
515         }
516         else
517                 c = vlen(to.origin - from.origin);
518
519         if (from.wp31mincost < c) return;
520         if (from.wp30mincost < c) {from.wp31 = to;from.wp31mincost = c;return;} from.wp31 = from.wp30;from.wp31mincost = from.wp30mincost;
521         if (from.wp29mincost < c) {from.wp30 = to;from.wp30mincost = c;return;} from.wp30 = from.wp29;from.wp30mincost = from.wp29mincost;
522         if (from.wp28mincost < c) {from.wp29 = to;from.wp29mincost = c;return;} from.wp29 = from.wp28;from.wp29mincost = from.wp28mincost;
523         if (from.wp27mincost < c) {from.wp28 = to;from.wp28mincost = c;return;} from.wp28 = from.wp27;from.wp28mincost = from.wp27mincost;
524         if (from.wp26mincost < c) {from.wp27 = to;from.wp27mincost = c;return;} from.wp27 = from.wp26;from.wp27mincost = from.wp26mincost;
525         if (from.wp25mincost < c) {from.wp26 = to;from.wp26mincost = c;return;} from.wp26 = from.wp25;from.wp26mincost = from.wp25mincost;
526         if (from.wp24mincost < c) {from.wp25 = to;from.wp25mincost = c;return;} from.wp25 = from.wp24;from.wp25mincost = from.wp24mincost;
527         if (from.wp23mincost < c) {from.wp24 = to;from.wp24mincost = c;return;} from.wp24 = from.wp23;from.wp24mincost = from.wp23mincost;
528         if (from.wp22mincost < c) {from.wp23 = to;from.wp23mincost = c;return;} from.wp23 = from.wp22;from.wp23mincost = from.wp22mincost;
529         if (from.wp21mincost < c) {from.wp22 = to;from.wp22mincost = c;return;} from.wp22 = from.wp21;from.wp22mincost = from.wp21mincost;
530         if (from.wp20mincost < c) {from.wp21 = to;from.wp21mincost = c;return;} from.wp21 = from.wp20;from.wp21mincost = from.wp20mincost;
531         if (from.wp19mincost < c) {from.wp20 = to;from.wp20mincost = c;return;} from.wp20 = from.wp19;from.wp20mincost = from.wp19mincost;
532         if (from.wp18mincost < c) {from.wp19 = to;from.wp19mincost = c;return;} from.wp19 = from.wp18;from.wp19mincost = from.wp18mincost;
533         if (from.wp17mincost < c) {from.wp18 = to;from.wp18mincost = c;return;} from.wp18 = from.wp17;from.wp18mincost = from.wp17mincost;
534         if (from.wp16mincost < c) {from.wp17 = to;from.wp17mincost = c;return;} from.wp17 = from.wp16;from.wp17mincost = from.wp16mincost;
535         if (from.wp15mincost < c) {from.wp16 = to;from.wp16mincost = c;return;} from.wp16 = from.wp15;from.wp16mincost = from.wp15mincost;
536         if (from.wp14mincost < c) {from.wp15 = to;from.wp15mincost = c;return;} from.wp15 = from.wp14;from.wp15mincost = from.wp14mincost;
537         if (from.wp13mincost < c) {from.wp14 = to;from.wp14mincost = c;return;} from.wp14 = from.wp13;from.wp14mincost = from.wp13mincost;
538         if (from.wp12mincost < c) {from.wp13 = to;from.wp13mincost = c;return;} from.wp13 = from.wp12;from.wp13mincost = from.wp12mincost;
539         if (from.wp11mincost < c) {from.wp12 = to;from.wp12mincost = c;return;} from.wp12 = from.wp11;from.wp12mincost = from.wp11mincost;
540         if (from.wp10mincost < c) {from.wp11 = to;from.wp11mincost = c;return;} from.wp11 = from.wp10;from.wp11mincost = from.wp10mincost;
541         if (from.wp09mincost < c) {from.wp10 = to;from.wp10mincost = c;return;} from.wp10 = from.wp09;from.wp10mincost = from.wp09mincost;
542         if (from.wp08mincost < c) {from.wp09 = to;from.wp09mincost = c;return;} from.wp09 = from.wp08;from.wp09mincost = from.wp08mincost;
543         if (from.wp07mincost < c) {from.wp08 = to;from.wp08mincost = c;return;} from.wp08 = from.wp07;from.wp08mincost = from.wp07mincost;
544         if (from.wp06mincost < c) {from.wp07 = to;from.wp07mincost = c;return;} from.wp07 = from.wp06;from.wp07mincost = from.wp06mincost;
545         if (from.wp05mincost < c) {from.wp06 = to;from.wp06mincost = c;return;} from.wp06 = from.wp05;from.wp06mincost = from.wp05mincost;
546         if (from.wp04mincost < c) {from.wp05 = to;from.wp05mincost = c;return;} from.wp05 = from.wp04;from.wp05mincost = from.wp04mincost;
547         if (from.wp03mincost < c) {from.wp04 = to;from.wp04mincost = c;return;} from.wp04 = from.wp03;from.wp04mincost = from.wp03mincost;
548         if (from.wp02mincost < c) {from.wp03 = to;from.wp03mincost = c;return;} from.wp03 = from.wp02;from.wp03mincost = from.wp02mincost;
549         if (from.wp01mincost < c) {from.wp02 = to;from.wp02mincost = c;return;} from.wp02 = from.wp01;from.wp02mincost = from.wp01mincost;
550         if (from.wp00mincost < c) {from.wp01 = to;from.wp01mincost = c;return;} from.wp01 = from.wp00;from.wp01mincost = from.wp00mincost;
551         from.wp00 = to;from.wp00mincost = c;return;
552 };
553
554 // fields you can query using prvm_global server to get some statistics about waypoint linking culling
555 float relink_total, relink_walkculled, relink_pvsculled, relink_lengthculled;
556
557 // relink this spawnfunc_waypoint
558 // (precompile a list of all reachable waypoints from this spawnfunc_waypoint)
559 // (SLOW!)
560 void waypoint_think()
561 {
562         local entity e;
563         local vector sv, sm1, sm2, ev, em1, em2, dv;
564         //dprint("waypoint_think wpisbox = ", ftos(self.wpisbox), "\n");
565         sm1 = self.origin + self.mins;
566         sm2 = self.origin + self.maxs;
567         stepheightvec = cvar("sv_stepheight") * '0 0 1';
568         for(e = world; (e = find(e, classname, "waypoint")); )
569         {
570                 if (boxesoverlap(self.absmin, self.absmax, e.absmin, e.absmax))
571                 {
572                         waypoint_addlink(self, e);
573                         waypoint_addlink(e, self);
574                 }
575                 else
576                 {
577                         ++relink_total;
578                         if(!checkpvs(self.origin, e))
579                         {
580                                 ++relink_pvsculled;
581                                 continue;
582                         }
583                         sv = e.origin;
584                         sv_x = bound(sm1_x, sv_x, sm2_x);
585                         sv_y = bound(sm1_y, sv_y, sm2_y);
586                         sv_z = bound(sm1_z, sv_z, sm2_z);
587                         ev = self.origin;
588                         em1 = e.origin + e.mins;
589                         em2 = e.origin + e.maxs;
590                         ev_x = bound(em1_x, ev_x, em2_x);
591                         ev_y = bound(em1_y, ev_y, em2_y);
592                         ev_z = bound(em1_z, ev_z, em2_z);
593                         dv = ev - sv;
594                         dv_z = 0;
595                         if (vlen(dv) >= 1050) // max search distance in XY
596                         {
597                                 ++relink_lengthculled;
598                                 continue;
599                         }
600                         navigation_testtracewalk = 0;
601                         if (!self.wpisbox)
602                         {
603                                 tracebox(sv - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, sv, FALSE, self);
604                                 if (!trace_startsolid)
605                                 {
606                                         //dprint("sv deviation", vtos(trace_endpos - sv), "\n");
607                                         sv = trace_endpos + '0 0 1';
608                                 }
609                         }
610                         if (!e.wpisbox)
611                         {
612                                 tracebox(ev - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, ev, FALSE, e);
613                                 if (!trace_startsolid)
614                                 {
615                                         //dprint("ev deviation", vtos(trace_endpos - ev), "\n");
616                                         ev = trace_endpos + '0 0 1';
617                                 }
618                         }
619                         //traceline(self.origin, e.origin, FALSE, world);
620                         //if (trace_fraction == 1)
621                         if (!self.wpisbox && tracewalk(self, sv, PL_MIN, PL_MAX, ev, MOVE_NOMONSTERS))
622                                 waypoint_addlink(self, e);
623                         else
624                                 relink_walkculled += 0.5;
625                         if (!e.wpisbox && tracewalk(e, ev, PL_MIN, PL_MAX, sv, MOVE_NOMONSTERS))
626                                 waypoint_addlink(e, self);
627                         else
628                                 relink_walkculled += 0.5;
629                 }
630         }
631         navigation_testtracewalk = 0;
632 };
633
634 void waypoint_clearlinks(entity wp)
635 {
636         // clear links to other waypoints
637         local float f;
638         f = 10000000;
639         wp.wp00 = wp.wp01 = wp.wp02 = wp.wp03 = wp.wp04 = wp.wp05 = wp.wp06 = wp.wp07 = world;wp.wp00mincost = wp.wp01mincost = wp.wp02mincost = wp.wp03mincost = wp.wp04mincost = wp.wp05mincost = wp.wp06mincost = wp.wp07mincost = f;
640         wp.wp08 = wp.wp09 = wp.wp10 = wp.wp11 = wp.wp12 = wp.wp13 = wp.wp14 = wp.wp15 = world;wp.wp08mincost = wp.wp09mincost = wp.wp10mincost = wp.wp11mincost = wp.wp12mincost = wp.wp13mincost = wp.wp14mincost = wp.wp15mincost = f;
641         wp.wp16 = wp.wp17 = wp.wp18 = wp.wp19 = wp.wp20 = wp.wp21 = wp.wp22 = wp.wp23 = world;wp.wp16mincost = wp.wp17mincost = wp.wp18mincost = wp.wp19mincost = wp.wp20mincost = wp.wp21mincost = wp.wp22mincost = wp.wp23mincost = f;
642         wp.wp24 = wp.wp25 = wp.wp26 = wp.wp27 = wp.wp28 = wp.wp29 = wp.wp30 = wp.wp31 = world;wp.wp24mincost = wp.wp25mincost = wp.wp26mincost = wp.wp27mincost = wp.wp28mincost = wp.wp29mincost = wp.wp30mincost = wp.wp31mincost = f;
643 };
644
645 // tell a spawnfunc_waypoint to relink
646 void waypoint_schedulerelink(entity wp)
647 {
648         if (wp == world)
649                 return;
650         // TODO: add some sort of visible box in edit mode for box waypoints
651         if (cvar("g_waypointeditor"))
652         {
653                 local vector m1, m2;
654                 m1 = wp.mins;
655                 m2 = wp.maxs;
656                 setmodel(wp, "models/runematch/rune.mdl"); wp.effects = EF_LOWPRECISION;
657                 setsize(wp, m1, m2);
658                 if (wp.wpflags & WAYPOINTFLAG_ITEM)
659                         wp.colormod = '1 0 0';
660                 else if (wp.wpflags & WAYPOINTFLAG_GENERATED)
661                         wp.colormod = '1 1 0';
662                 else
663                         wp.colormod = '1 1 1';
664         }
665         else
666                 wp.model = "";
667         wp.wpisbox = vlen(wp.size) > 0;
668         wp.enemy = world;
669         wp.owner = world;
670         if (!(wp.wpflags & WAYPOINTFLAG_NORELINK))
671                 waypoint_clearlinks(wp);
672         // schedule an actual relink on next frame
673         wp.think = waypoint_think;
674         wp.nextthink = time;
675         wp.effects = EF_LOWPRECISION;
676 }
677
678 // create a new spawnfunc_waypoint and automatically link it to other waypoints, and link
679 // them back to it as well
680 // (suitable for spawnfunc_waypoint editor)
681 entity waypoint_spawn(vector m1, vector m2, float f)
682 {
683         local entity w;
684         local vector org;
685         w = find(world, classname, "waypoint");
686
687         if not(f & WAYPOINTFLAG_PERSONAL)
688         while (w)
689         {
690                 // if a matching spawnfunc_waypoint already exists, don't add a duplicate
691                 if (boxesoverlap(m1, m2, w.absmin, w.absmax))
692                         return w;
693                 w = find(w, classname, "waypoint");
694         }
695
696         w = spawn();
697         w.classname = "waypoint";
698         w.wpflags = f;
699         setorigin(w, (m1 + m2) * 0.5);
700         setsize(w, m1 - w.origin, m2 - w.origin);
701         if (vlen(w.size) > 0)
702                 w.wpisbox = TRUE;
703
704         if(!(f & WAYPOINTFLAG_GENERATED))
705         if(!w.wpisbox)
706         {
707                 traceline(w.origin + '0 0 1', w.origin + PL_MIN_z * '0 0 1' - '0 0 1', MOVE_NOMONSTERS, w);
708                 if (trace_fraction < 1)
709                         setorigin(w, trace_endpos - PL_MIN_z * '0 0 1');
710
711                 // check if the start position is stuck
712                 tracebox(w.origin, PL_MIN + '-1 -1 -1', PL_MAX + '1 1 1', w.origin, MOVE_NOMONSTERS, w);
713                 if (trace_startsolid)
714                 {
715                         org = w.origin + '0 0 26';
716                         tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
717                         if(trace_startsolid)
718                         {
719                                 org = w.origin + '2 2 2';
720                                 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
721                                 if(trace_startsolid)
722                                 {
723                                         org = w.origin + '-2 -2 2';
724                                         tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
725                                         if(trace_startsolid)
726                                         {
727                                                 org = w.origin + '-2 2 2';
728                                                 tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
729                                                 if(trace_startsolid)
730                                                 {
731                                                         org = w.origin + '2 -2 2';
732                                                         tracebox(org, PL_MIN, PL_MAX, w.origin, MOVE_WORLDONLY, w);
733                                                         if(trace_startsolid)
734                                                         {
735                                                                 // this WP is in solid, refuse it
736                                                                 dprint("Killed a waypoint that was stuck in solid at ", vtos(org), "\n");
737                                                                 remove(w);
738                                                                 return world;
739                                                         }
740                                                 }
741                                         }
742                                 }
743                         }
744                         setorigin(w, org * 0.05 + trace_endpos * 0.95); // don't trust the trace fully
745                 }
746
747                 tracebox(w.origin, PL_MIN, PL_MAX, w.origin - '0 0 128', MOVE_WORLDONLY, w);
748                 if(trace_startsolid)
749                 {
750                         dprint("Killed a waypoint that was stuck in solid ", vtos(w.origin), "\n");
751                         remove(w);
752                         return world;
753                 }
754                 if (!trace_inwater)
755                 {
756                         if(trace_fraction == 1)
757                         {
758                                 dprint("Killed a waypoint that was stuck in air at ", vtos(w.origin), "\n");
759                                 remove(w);
760                                 return world;
761                         }
762                         trace_endpos_z += 0.1; // don't trust the trace fully
763 //                      dprint("Moved waypoint at ", vtos(w.origin), " by ", ftos(vlen(w.origin - trace_endpos)));
764 //                      dprint(" direction: ", vtos((trace_endpos - w.origin)), "\n");
765                         setorigin(w, trace_endpos);
766                 }
767         }
768
769         waypoint_clearlinks(w);
770         //waypoint_schedulerelink(w);
771         return w;
772 };
773
774 // spawnfunc_waypoint map entity
775 void spawnfunc_waypoint()
776 {
777         setorigin(self, self.origin);
778         // schedule a relink after other waypoints have had a chance to spawn
779         waypoint_clearlinks(self);
780         //waypoint_schedulerelink(self);
781 };
782
783 // remove a spawnfunc_waypoint, and schedule all neighbors to relink
784 void waypoint_remove(entity e)
785 {
786         // tell all linked waypoints that they need to relink
787         waypoint_schedulerelink(e.wp00);
788         waypoint_schedulerelink(e.wp01);
789         waypoint_schedulerelink(e.wp02);
790         waypoint_schedulerelink(e.wp03);
791         waypoint_schedulerelink(e.wp04);
792         waypoint_schedulerelink(e.wp05);
793         waypoint_schedulerelink(e.wp06);
794         waypoint_schedulerelink(e.wp07);
795         waypoint_schedulerelink(e.wp08);
796         waypoint_schedulerelink(e.wp09);
797         waypoint_schedulerelink(e.wp10);
798         waypoint_schedulerelink(e.wp11);
799         waypoint_schedulerelink(e.wp12);
800         waypoint_schedulerelink(e.wp13);
801         waypoint_schedulerelink(e.wp14);
802         waypoint_schedulerelink(e.wp15);
803         waypoint_schedulerelink(e.wp16);
804         waypoint_schedulerelink(e.wp17);
805         waypoint_schedulerelink(e.wp18);
806         waypoint_schedulerelink(e.wp19);
807         waypoint_schedulerelink(e.wp20);
808         waypoint_schedulerelink(e.wp21);
809         waypoint_schedulerelink(e.wp22);
810         waypoint_schedulerelink(e.wp23);
811         waypoint_schedulerelink(e.wp24);
812         waypoint_schedulerelink(e.wp25);
813         waypoint_schedulerelink(e.wp26);
814         waypoint_schedulerelink(e.wp27);
815         waypoint_schedulerelink(e.wp28);
816         waypoint_schedulerelink(e.wp29);
817         waypoint_schedulerelink(e.wp30);
818         waypoint_schedulerelink(e.wp31);
819         // and now remove the spawnfunc_waypoint
820         remove(e);
821 };
822
823 // empties the map of waypoints
824 void waypoint_removeall()
825 {
826         local entity head, next;
827         head = findchain(classname, "waypoint");
828         while (head)
829         {
830                 next = head.chain;
831                 remove(head);
832                 head = next;
833         }
834 };
835
836 // tell all waypoints to relink
837 // (is this useful at all?)
838 void waypoint_schedulerelinkall()
839 {
840         local entity head;
841         relink_total = relink_walkculled = relink_pvsculled = relink_lengthculled = 0;
842         head = findchain(classname, "waypoint");
843         while (head)
844         {
845                 waypoint_schedulerelink(head);
846                 head = head.chain;
847         }
848 };
849
850 // Load waypoint links from file
851 float botframe_cachedwaypointlinks;
852 float waypoint_load_links()
853 {
854         local string filename, s;
855         local float file, tokens, c, found;
856         local entity wp_from, wp_to;
857         local vector wp_to_pos, wp_from_pos;
858         filename = strcat("maps/", mapname);
859         filename = strcat(filename, ".waypoints.cache");
860         file = fopen(filename, FILE_READ);
861
862         if (file < 0)
863         {
864                 dprint("waypoint links load from ");
865                 dprint(filename);
866                 dprint(" failed\n");
867                 return FALSE;
868         }
869
870         while (1)
871         {
872                 s = fgets(file);
873                 if (!s)
874                         break;
875
876                 tokens = tokenizebyseparator(s, "*");
877
878                 if (tokens!=2)
879                 {
880                         // bad file format
881                         fclose(file);
882                         return FALSE;
883                 }
884
885                 wp_from_pos     = stov(argv(0));
886                 wp_to_pos       = stov(argv(1));
887
888                 // Search "from" waypoint
889                 if(wp_from.origin!=wp_from_pos)
890                 {
891                         wp_from = findradius(wp_from_pos, 1);
892                         found = FALSE;
893                         while(wp_from)
894                         {
895                                 if(vlen(wp_from.origin-wp_from_pos)<1)
896                                 if(wp_from.classname == "waypoint")
897                                 {
898                                         found = TRUE;
899                                         break;
900                                 }
901                                 wp_from = wp_from.chain;
902                         }
903
904                         if(!found)
905                         {
906                                 // can't find that waypoint
907                                 fclose(file);
908                                 return FALSE;
909                         }
910                 }
911
912                 // Search "to" waypoint
913                 wp_to = findradius(wp_to_pos, 1);
914                 found = FALSE;
915                 while(wp_to)
916                 {
917                         if(vlen(wp_to.origin-wp_to_pos)<1)
918                         if(wp_to.classname == "waypoint")
919                         {
920                                 found = TRUE;
921                                 break;
922                         }
923                         wp_to = wp_to.chain;
924                 }
925
926                 if(!found)
927                 {
928                         // can't find that waypoint
929                         fclose(file);
930                         return FALSE;
931                 }
932
933                 ++c;
934                 waypoint_addlink(wp_from, wp_to);
935         }
936
937         fclose(file);
938
939         dprint("loaded ");
940         dprint(ftos(c));
941         dprint(" waypoint links from maps/");
942         dprint(mapname);
943         dprint(".waypoints.cache\n");
944
945         botframe_cachedwaypointlinks = TRUE;
946         return TRUE;
947 };
948
949 float botframe_loadedforcedlinks;
950 void waypoint_load_links_hardwired()
951 {
952         local string filename, s;
953         local float file, tokens, c, found;
954         local entity wp_from, wp_to;
955         local vector wp_to_pos, wp_from_pos;
956         filename = strcat("maps/", mapname);
957         filename = strcat(filename, ".waypoints.hardwired");
958         file = fopen(filename, FILE_READ);
959
960         botframe_loadedforcedlinks = TRUE;
961
962         if (file < 0)
963         {
964                 dprint("waypoint links load from ");
965                 dprint(filename);
966                 dprint(" failed\n");
967                 return;
968         }
969
970         for (;;)
971         {
972                 s = fgets(file);
973                 if (!s)
974                         break;
975
976                 if(substring(s, 0, 2)=="//")
977                         continue;
978
979                 if(substring(s, 0, 1)=="#")
980                         continue;
981
982                 tokens = tokenizebyseparator(s, "*");
983
984                 if (tokens!=2)
985                         continue;
986
987                 wp_from_pos     = stov(argv(0));
988                 wp_to_pos       = stov(argv(1));
989
990                 // Search "from" waypoint
991                 if(wp_from.origin!=wp_from_pos)
992                 {
993                         wp_from = findradius(wp_from_pos, 1);
994                         found = FALSE;
995                         while(wp_from)
996                         {
997                                 if(vlen(wp_from.origin-wp_from_pos)<1)
998                                 if(wp_from.classname == "waypoint")
999                                 {
1000                                         found = TRUE;
1001                                         break;
1002                                 }
1003                                 wp_from = wp_from.chain;
1004                         }
1005
1006                         if(!found)
1007                         {
1008                                 print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_from_pos), ". Path skipped\n"));
1009                                 continue;
1010                         }
1011                 }
1012
1013                 // Search "to" waypoint
1014                 wp_to = findradius(wp_to_pos, 1);
1015                 found = FALSE;
1016                 while(wp_to)
1017                 {
1018                         if(vlen(wp_to.origin-wp_to_pos)<1)
1019                         if(wp_to.classname == "waypoint")
1020                         {
1021                                 found = TRUE;
1022                                 break;
1023                         }
1024                         wp_to = wp_to.chain;
1025                 }
1026
1027                 if(!found)
1028                 {
1029                         print(strcat("NOTICE: Can not find waypoint at ", vtos(wp_to_pos), ". Path skipped\n"));
1030                         continue;
1031                 }
1032
1033                 ++c;
1034                 waypoint_addlink(wp_from, wp_to);
1035         }
1036
1037         fclose(file);
1038
1039         dprint("loaded ");
1040         dprint(ftos(c));
1041         dprint(" waypoint links from maps/");
1042         dprint(mapname);
1043         dprint(".waypoints.hardwired\n");
1044 };
1045
1046
1047 // Save all waypoint links to a file
1048 void waypoint_save_links()
1049 {
1050         local string filename, s;
1051         local float file, c, i;
1052         local entity w, link;
1053         filename = strcat("maps/", mapname);
1054         filename = strcat(filename, ".waypoints.cache");
1055         file = fopen(filename, FILE_WRITE);
1056         if (file < 0)
1057         {
1058                 print("waypoint links save to ");
1059                 print(filename);
1060                 print(" failed\n");
1061         }
1062         c = 0;
1063         w = findchain(classname, "waypoint");
1064         while (w)
1065         {
1066                 for(i=0;i<32;++i)
1067                 {
1068                         // :S
1069                         switch(i)
1070                         {
1071                                 //      for i in $(seq -w 0 31); do echo "case $i:link = w.wp$i; break;"; done;
1072                                 case 00:link = w.wp00; break;
1073                                 case 01:link = w.wp01; break;
1074                                 case 02:link = w.wp02; break;
1075                                 case 03:link = w.wp03; break;
1076                                 case 04:link = w.wp04; break;
1077                                 case 05:link = w.wp05; break;
1078                                 case 06:link = w.wp06; break;
1079                                 case 07:link = w.wp07; break;
1080                                 case 08:link = w.wp08; break;
1081                                 case 09:link = w.wp09; break;
1082                                 case 10:link = w.wp10; break;
1083                                 case 11:link = w.wp11; break;
1084                                 case 12:link = w.wp12; break;
1085                                 case 13:link = w.wp13; break;
1086                                 case 14:link = w.wp14; break;
1087                                 case 15:link = w.wp15; break;
1088                                 case 16:link = w.wp16; break;
1089                                 case 17:link = w.wp17; break;
1090                                 case 18:link = w.wp18; break;
1091                                 case 19:link = w.wp19; break;
1092                                 case 20:link = w.wp20; break;
1093                                 case 21:link = w.wp21; break;
1094                                 case 22:link = w.wp22; break;
1095                                 case 23:link = w.wp23; break;
1096                                 case 24:link = w.wp24; break;
1097                                 case 25:link = w.wp25; break;
1098                                 case 26:link = w.wp26; break;
1099                                 case 27:link = w.wp27; break;
1100                                 case 28:link = w.wp28; break;
1101                                 case 29:link = w.wp29; break;
1102                                 case 30:link = w.wp30; break;
1103                                 case 31:link = w.wp31; break;
1104                         }
1105
1106                         if(link==world)
1107                                 continue;
1108
1109                         s = strcat(vtos(w.origin), "*", vtos(link.origin), "\n");
1110                         fputs(file, s);
1111                         ++c;
1112                 }
1113                 w = w.chain;
1114         }
1115         fclose(file);
1116         botframe_cachedwaypointlinks = TRUE;
1117
1118         print("saved ");
1119         print(ftos(c));
1120         print(" waypoints links to maps/");
1121         print(mapname);
1122         print(".waypoints.cache\n");
1123 };
1124
1125 // save waypoints to gamedir/data/maps/mapname.waypoints
1126 void waypoint_saveall()
1127 {
1128         local string filename, s;
1129         local float file, c;
1130         local entity w;
1131         filename = strcat("maps/", mapname);
1132         filename = strcat(filename, ".waypoints");
1133         file = fopen(filename, FILE_WRITE);
1134         if (file >= 0)
1135         {
1136                 c = 0;
1137                 w = findchain(classname, "waypoint");
1138                 while (w)
1139                 {
1140                         if (!(w.wpflags & WAYPOINTFLAG_GENERATED))
1141                         {
1142                                 s = strcat(vtos(w.origin + w.mins), "\n");
1143                                 s = strcat(s, vtos(w.origin + w.maxs));
1144                                 s = strcat(s, "\n");
1145                                 s = strcat(s, ftos(w.wpflags));
1146                                 s = strcat(s, "\n");
1147                                 fputs(file, s);
1148                                 c = c + 1;
1149                         }
1150                         w = w.chain;
1151                 }
1152                 fclose(file);
1153                 bprint("saved ");
1154                 bprint(ftos(c));
1155                 bprint(" waypoints to maps/");
1156                 bprint(mapname);
1157                 bprint(".waypoints\n");
1158         }
1159         else
1160         {
1161                 bprint("waypoint save to ");
1162                 bprint(filename);
1163                 bprint(" failed\n");
1164         }
1165         waypoint_save_links();
1166         botframe_loadedforcedlinks = FALSE;
1167 };
1168
1169 // load waypoints from file
1170 float waypoint_loadall()
1171 {
1172         local string filename, s;
1173         local float file, cwp, cwb, fl;
1174         local vector m1, m2;
1175         cwp = 0;
1176         cwb = 0;
1177         filename = strcat("maps/", mapname);
1178         filename = strcat(filename, ".waypoints");
1179         file = fopen(filename, FILE_READ);
1180         if (file >= 0)
1181         {
1182                 while (1)
1183                 {
1184                         s = fgets(file);
1185                         if (!s)
1186                                 break;
1187                         m1 = stov(s);
1188                         s = fgets(file);
1189                         if (!s)
1190                                 break;
1191                         m2 = stov(s);
1192                         s = fgets(file);
1193                         if (!s)
1194                                 break;
1195                         fl = stof(s);
1196                         waypoint_spawn(m1, m2, fl);
1197                         if (m1 == m2)
1198                                 cwp = cwp + 1;
1199                         else
1200                                 cwb = cwb + 1;
1201                 }
1202                 fclose(file);
1203                 dprint("loaded ");
1204                 dprint(ftos(cwp));
1205                 dprint(" waypoints and ");
1206                 dprint(ftos(cwb));
1207                 dprint(" wayboxes from maps/");
1208                 dprint(mapname);
1209                 dprint(".waypoints\n");
1210         }
1211         else
1212         {
1213                 dprint("waypoint load from ");
1214                 dprint(filename);
1215                 dprint(" failed\n");
1216         }
1217         return cwp + cwb;
1218 };
1219
1220 void waypoint_spawnforitem(entity e)
1221 {
1222         local entity w;
1223         local vector org;
1224
1225         if(!bot_waypoints_for_items)
1226                 return;
1227
1228         // Center of entity
1229         org = e.origin + (e.mins + e.maxs) * 0.5;
1230
1231         // Calculate the altitude where the waypoint will be placed
1232         traceline(e.origin, e.origin + '0 0 -65535', TRUE, e);
1233         if(e.origin_z + e.mins_z - trace_endpos_z < PL_MAX_z - PL_MIN_z + 10)
1234                 org_z = trace_endpos_z + PL_MAX_z - PL_MIN_z;
1235         else
1236                 org_z = e.origin_z;
1237
1238         e.nearestwaypointtimeout = time + 1000000000;
1239         // don't spawn an item spawnfunc_waypoint if it already exists
1240         w = findchain(classname, "waypoint");
1241         while (w)
1242         {
1243                 if (w.wpisbox)
1244                 {
1245                         if (boxesoverlap(org, org, w.absmin, w.absmax))
1246                         {
1247                                 e.nearestwaypoint = w;
1248                                 return;
1249                         }
1250                 }
1251                 else
1252                 {
1253                         if (vlen(w.origin - org) < 16)
1254                         {
1255                                 e.nearestwaypoint = w;
1256                                 return;
1257                         }
1258                 }
1259                 w = w.chain;
1260         }
1261         e.nearestwaypoint = waypoint_spawn(org, org, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_ITEM);
1262 };
1263
1264 void waypoint_spawnforteleporter(entity e, vector destination, float timetaken)
1265 {
1266         local entity w;
1267         local entity dw;
1268         w = waypoint_spawn(e.absmin, e.absmax, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_NORELINK);
1269         dw = waypoint_spawn(destination, destination, WAYPOINTFLAG_GENERATED);
1270         // one way link to the destination
1271         w.wp00 = dw;
1272         w.wp00mincost = timetaken; // this is just for jump pads
1273         // the teleporter's nearest spawnfunc_waypoint is this one
1274         // (teleporters are not goals, so this is probably useless)
1275         e.nearestwaypoint = w;
1276         e.nearestwaypointtimeout = time + 1000000000;
1277 };
1278
1279 entity waypoint_spawnpersonal(vector position)
1280 {
1281         entity w;
1282
1283         w = waypoint_spawn(position, position, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_PERSONAL);
1284         w.nearestwaypoint = world;
1285         w.nearestwaypointtimeout = 0;
1286         w.owner = self;
1287
1288         return w;
1289 };
1290
1291 /////////////////////////////////////////////////////////////////////////////
1292 // goal stack
1293 /////////////////////////////////////////////////////////////////////////////
1294
1295 // completely empty the goal stack, used when deciding where to go
1296 void navigation_clearroute()
1297 {
1298         self.navigation_hasgoals = FALSE;
1299         self.goalcurrent = world;
1300         self.goalstack01 = world;
1301         self.goalstack02 = world;
1302         self.goalstack03 = world;
1303         self.goalstack04 = world;
1304         self.goalstack05 = world;
1305         self.goalstack06 = world;
1306         self.goalstack07 = world;
1307         self.goalstack08 = world;
1308         self.goalstack09 = world;
1309         self.goalstack10 = world;
1310         self.goalstack11 = world;
1311         self.goalstack12 = world;
1312         self.goalstack13 = world;
1313         self.goalstack14 = world;
1314         self.goalstack15 = world;
1315         self.goalstack16 = world;
1316         self.goalstack17 = world;
1317         self.goalstack18 = world;
1318         self.goalstack19 = world;
1319         self.goalstack20 = world;
1320         self.goalstack21 = world;
1321         self.goalstack22 = world;
1322         self.goalstack23 = world;
1323         self.goalstack24 = world;
1324         self.goalstack25 = world;
1325         self.goalstack26 = world;
1326         self.goalstack27 = world;
1327         self.goalstack28 = world;
1328         self.goalstack29 = world;
1329         self.goalstack30 = world;
1330         self.goalstack31 = world;
1331 };
1332
1333 // add a new goal at the beginning of the stack
1334 // (in other words: add a new prerequisite before going to the later goals)
1335 void navigation_pushroute(entity e)
1336 {
1337         self.goalstack31 = self.goalstack30;
1338         self.goalstack30 = self.goalstack29;
1339         self.goalstack29 = self.goalstack28;
1340         self.goalstack28 = self.goalstack27;
1341         self.goalstack27 = self.goalstack26;
1342         self.goalstack26 = self.goalstack25;
1343         self.goalstack25 = self.goalstack24;
1344         self.goalstack24 = self.goalstack23;
1345         self.goalstack23 = self.goalstack22;
1346         self.goalstack22 = self.goalstack21;
1347         self.goalstack21 = self.goalstack20;
1348         self.goalstack20 = self.goalstack19;
1349         self.goalstack19 = self.goalstack18;
1350         self.goalstack18 = self.goalstack17;
1351         self.goalstack17 = self.goalstack16;
1352         self.goalstack16 = self.goalstack15;
1353         self.goalstack15 = self.goalstack14;
1354         self.goalstack14 = self.goalstack13;
1355         self.goalstack13 = self.goalstack12;
1356         self.goalstack12 = self.goalstack11;
1357         self.goalstack11 = self.goalstack10;
1358         self.goalstack10 = self.goalstack09;
1359         self.goalstack09 = self.goalstack08;
1360         self.goalstack08 = self.goalstack07;
1361         self.goalstack07 = self.goalstack06;
1362         self.goalstack06 = self.goalstack05;
1363         self.goalstack05 = self.goalstack04;
1364         self.goalstack04 = self.goalstack03;
1365         self.goalstack03 = self.goalstack02;
1366         self.goalstack02 = self.goalstack01;
1367         self.goalstack01 = self.goalcurrent;
1368         self.goalcurrent = e;
1369 };
1370
1371 // remove first goal from stack
1372 // (in other words: remove a prerequisite for reaching the later goals)
1373 // (used when a spawnfunc_waypoint is reached)
1374 void navigation_poproute()
1375 {
1376         if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1377                 remove(self.goalcurrent);
1378
1379         self.goalcurrent = self.goalstack01;
1380         self.goalstack01 = self.goalstack02;
1381         self.goalstack02 = self.goalstack03;
1382         self.goalstack03 = self.goalstack04;
1383         self.goalstack04 = self.goalstack05;
1384         self.goalstack05 = self.goalstack06;
1385         self.goalstack06 = self.goalstack07;
1386         self.goalstack07 = self.goalstack08;
1387         self.goalstack08 = self.goalstack09;
1388         self.goalstack09 = self.goalstack10;
1389         self.goalstack10 = self.goalstack11;
1390         self.goalstack11 = self.goalstack12;
1391         self.goalstack12 = self.goalstack13;
1392         self.goalstack13 = self.goalstack14;
1393         self.goalstack14 = self.goalstack15;
1394         self.goalstack15 = self.goalstack16;
1395         self.goalstack16 = self.goalstack17;
1396         self.goalstack17 = self.goalstack18;
1397         self.goalstack18 = self.goalstack19;
1398         self.goalstack19 = self.goalstack20;
1399         self.goalstack20 = self.goalstack21;
1400         self.goalstack21 = self.goalstack22;
1401         self.goalstack22 = self.goalstack23;
1402         self.goalstack23 = self.goalstack24;
1403         self.goalstack24 = self.goalstack25;
1404         self.goalstack25 = self.goalstack26;
1405         self.goalstack26 = self.goalstack27;
1406         self.goalstack27 = self.goalstack28;
1407         self.goalstack28 = self.goalstack29;
1408         self.goalstack29 = self.goalstack30;
1409         self.goalstack30 = self.goalstack31;
1410         self.goalstack31 = world;
1411 };
1412
1413 // find the spawnfunc_waypoint near a dynamic goal such as a dropped weapon
1414 entity navigation_findnearestwaypoint(entity player, float walkfromwp)
1415 {
1416         local entity waylist, w, best;
1417         local float dist, bestdist;
1418         local vector v, org, pm1, pm2;
1419         pm1 = player.origin + PL_MIN;
1420         pm2 = player.origin + PL_MAX;
1421         waylist = findchain(classname, "waypoint");
1422         // do two scans, because box test is cheaper
1423         w = waylist;
1424         while (w)
1425         {
1426                 // if object is touching spawnfunc_waypoint
1427                 if (boxesoverlap(pm1, pm2, w.absmin, w.absmax))
1428                         return w;
1429                 w = w.chain;
1430         }
1431
1432         org = player.origin + (player.mins_z - PL_MIN_z) * '0 0 1';
1433         if(player.tag_entity)
1434                 org = org + player.tag_entity.origin;
1435         if (navigation_testtracewalk)
1436                 te_plasmaburn(org);
1437         best = world;
1438         bestdist = 1050;
1439
1440         // box check failed, try walk
1441         w = waylist;
1442         while (w)
1443         {
1444                 // if object can walk from spawnfunc_waypoint
1445                 if (w.wpisbox)
1446                 {
1447                         local vector wm1, wm2;
1448                         wm1 = w.origin + w.mins;
1449                         wm2 = w.origin + w.maxs;
1450                         v_x = bound(wm1_x, org_x, wm2_x);
1451                         v_y = bound(wm1_y, org_y, wm2_y);
1452                         v_z = bound(wm1_z, org_z, wm2_z);
1453                 }
1454                 else
1455                         v = w.origin;
1456                 dist = vlen(v - org);
1457                 if (bestdist > dist)
1458                 {
1459                         if (walkfromwp)
1460                         {
1461                                 traceline(v, org, TRUE, player);
1462                                 if (trace_fraction == 1)
1463                                 if (tracewalk(player, v, PL_MIN, PL_MAX, org, MOVE_NORMAL))
1464                                 {
1465                                         bestdist = dist;
1466                                         best = w;
1467                                 }
1468                         }
1469                         else
1470                         {
1471                                 traceline(v, org, TRUE, player);
1472                                 if (trace_fraction == 1)
1473                                 if (tracewalk(player, org, PL_MIN, PL_MAX, v, MOVE_NORMAL))
1474                                 {
1475                                         bestdist = dist;
1476                                         best = w;
1477                                 }
1478                         }
1479                 }
1480                 w = w.chain;
1481         }
1482         return best;
1483 }
1484
1485 // finds the waypoints near the bot initiating a navigation query
1486 float navigation_markroutes_nearestwaypoints(entity waylist, float maxdist)
1487 {
1488         local entity head;
1489         local vector v, m1, m2, diff;
1490         local float c;
1491 //      navigation_testtracewalk = TRUE;
1492         c = 0;
1493         head = waylist;
1494         while (head)
1495         {
1496                 if (!head.wpconsidered)
1497                 {
1498                         if (head.wpisbox)
1499                         {
1500                                 m1 = head.origin + head.mins;
1501                                 m2 = head.origin + head.maxs;
1502                                 v = self.origin;
1503                                 v_x = bound(m1_x, v_x, m2_x);
1504                                 v_y = bound(m1_y, v_y, m2_y);
1505                                 v_z = bound(m1_z, v_z, m2_z);
1506                         }
1507                         else
1508                                 v = head.origin;
1509                         diff = v - self.origin;
1510                         diff_z = max(0, diff_z);
1511                         if (vlen(diff) < maxdist)
1512                         {
1513                                 head.wpconsidered = TRUE;
1514                                 if (tracewalk(self, self.origin, self.mins, self.maxs, v, MOVE_NORMAL))
1515                                 {
1516                                         head.wpnearestpoint = v;
1517                                         head.wpcost = vlen(v - self.origin) + head.dmg;
1518                                         head.wpfire = 1;
1519                                         head.enemy = world;
1520                                         c = c + 1;
1521                                 }
1522                         }
1523                 }
1524                 head = head.chain;
1525         }
1526         //navigation_testtracewalk = FALSE;
1527         return c;
1528 }
1529
1530 // updates a path link if a spawnfunc_waypoint link is better than the current one
1531 void navigation_markroutes_checkwaypoint(entity w, entity wp, float cost2, vector p)
1532 {
1533         local vector m1;
1534         local vector m2;
1535         local vector v;
1536         if (wp.wpisbox)
1537         {
1538                 m1 = wp.absmin;
1539                 m2 = wp.absmax;
1540                 v_x = bound(m1_x, p_x, m2_x);
1541                 v_y = bound(m1_y, p_y, m2_y);
1542                 v_z = bound(m1_z, p_z, m2_z);
1543         }
1544         else
1545                 v = wp.origin;
1546         cost2 = cost2 + vlen(v);
1547         if (wp.wpcost > cost2)
1548         {
1549                 wp.wpcost = cost2;
1550                 wp.enemy = w;
1551                 wp.wpfire = 1;
1552                 wp.wpnearestpoint = v;
1553         }
1554 };
1555
1556 // queries the entire spawnfunc_waypoint network for pathes leading away from the bot
1557 void navigation_markroutes()
1558 {
1559         local entity w, wp, waylist;
1560         local float searching, cost, cost2;
1561         local vector p;
1562         w = waylist = findchain(classname, "waypoint");
1563         while (w)
1564         {
1565                 w.wpconsidered = FALSE;
1566                 w.wpnearestpoint = '0 0 0';
1567                 w.wpcost = 10000000;
1568                 w.wpfire = 0;
1569                 w.enemy = world;
1570                 w = w.chain;
1571         }
1572
1573         // try a short range search for the nearest waypoints, and expand the search repeatedly if none are found
1574         // as this search is expensive we will use lower values if the bot is on the air
1575         local float i, increment, maxdistance;
1576         if(self.flags & FL_ONGROUND)
1577         {
1578                 increment = 750;
1579                 maxdistance = 50000;
1580         }
1581         else
1582         {
1583                 increment = 500;
1584                 maxdistance = 1500;
1585         }
1586
1587         for(i=increment;!navigation_markroutes_nearestwaypoints(waylist, i)&&i<maxdistance;i+=increment);
1588
1589         searching = TRUE;
1590         while (searching)
1591         {
1592                 searching = FALSE;
1593                 w = waylist;
1594                 while (w)
1595                 {
1596                         if (w.wpfire)
1597                         {
1598                                 searching = TRUE;
1599                                 w.wpfire = 0;
1600                                 cost = w.wpcost;
1601                                 p = w.wpnearestpoint;
1602                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1603                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1604                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1605                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1606                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1607                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1608                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1609                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1610                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1611                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1612                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1613                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1614                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1615                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1616                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1617                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1618                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1619                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1620                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1621                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1622                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1623                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1624                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1625                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1626                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1627                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1628                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1629                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1630                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1631                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1632                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1633                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1634                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1635                         }
1636                         w = w.chain;
1637                 }
1638         }
1639 };
1640
1641 void navigation_bestgoals_reset()
1642 {
1643         local float i;
1644
1645         bestgoalswindex = 0;
1646         bestgoalsrindex = 0;
1647
1648         for(i=0;i>MAX_BESTGOALS-1;++i)
1649         {
1650                 navigation_bestgoals[i] = world;
1651         }
1652 }
1653
1654 void navigation_add_bestgoal(entity goal)
1655 {
1656         if(bestgoalsrindex>0)
1657         {
1658                 ++bestgoalsrindex;
1659
1660                 if(bestgoalsrindex==MAX_BESTGOALS)
1661                         bestgoalsrindex = 0;
1662         }
1663
1664         if(bestgoalswindex==MAX_BESTGOALS)
1665         {
1666                 bestgoalswindex=0;
1667                 if(bestgoalsrindex==0)
1668                         bestgoalsrindex=1;
1669         }
1670
1671         navigation_bestgoals[bestgoalswindex] = goal;
1672
1673         ++bestgoalswindex;
1674 }
1675
1676 entity navigation_get_bestgoal()
1677 {
1678         local entity ent;
1679
1680         ent = navigation_bestgoals[bestgoalsrindex];
1681         navigation_bestgoals[bestgoalsrindex] = world;
1682
1683         ++bestgoalsrindex;
1684
1685         if(bestgoalsrindex==MAX_BESTGOALS)
1686                 bestgoalsrindex = 0;
1687
1688         return ent;
1689 }
1690
1691 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1692 .void() havocbot_role;
1693 void() havocbot_role_ctf_offense;
1694 void navigation_routerating(entity e, float f, float rangebias)
1695 {
1696         if (!e)
1697                 return;
1698         //te_wizspike(e.origin);
1699         //bprint(etos(e));
1700         //bprint("\n");
1701         // update the cached spawnfunc_waypoint link on a dynamic item entity
1702         if (time > e.nearestwaypointtimeout)
1703         {
1704                 e.nearestwaypoint = navigation_findnearestwaypoint(e, TRUE);
1705                 e.nearestwaypointtimeout = time + random() * 3 + 5;
1706         }
1707         //dprint(e.classname, " ", ftos(f));
1708         //dprint("-- checking ", e.classname, " (with cost ", ftos(e.nearestwaypoint.wpcost), ")\n");
1709         if (e.nearestwaypoint)
1710         if (e.nearestwaypoint.wpcost < 10000000)
1711         {
1712                 //te_wizspike(e.nearestwaypoint.wpnearestpoint);
1713                 //dprint(e.classname, " ", ftos(f), "/(1+", ftos((e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint))), "/", ftos(rangebias), ") = ");
1714                 f = f * rangebias / (rangebias + (e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint)));
1715                 //if (self.havocbot_role == havocbot_role_ctf_offense)
1716                 //      dprint("-- considering ", e.classname, " (with rating ", ftos(f), ")\n");
1717                 //dprint(ftos(f));
1718                 if (navigation_bestrating < f)
1719                 {
1720                         navigation_bestrating = f;
1721                         navigation_add_bestgoal(e);
1722                 }
1723         }
1724         //dprint("\n");
1725 };
1726
1727 // adds an item to the the goal stack with the path to a given item
1728 float navigation_routetogoal(entity e, vector startposition)
1729 {
1730         self.goalentity = e;
1731
1732         // if there is no goal, just exit
1733         if (!e)
1734                 return FALSE;
1735
1736         self.navigation_hasgoals = TRUE;
1737
1738         // put the entity on the goal stack
1739         navigation_pushroute(e);
1740
1741         // if it can reach the goal there is nothing more to do
1742         if (tracewalk(self, startposition, PL_MIN, PL_MAX, e.origin, MOVE_NORMAL))
1743                 return TRUE;
1744
1745         // see if there are waypoints describing a path to the item
1746         e = e.nearestwaypoint;
1747         if(e == world)
1748                 return FALSE;
1749
1750         for (;;)
1751         {
1752                 // add the spawnfunc_waypoint to the path
1753                 navigation_pushroute(e);
1754                 e = e.enemy;
1755
1756                 if(e==world)
1757                         break;
1758         }
1759
1760         return FALSE;
1761 };
1762
1763 void navigation_routetogoals()
1764 {
1765         entity g1, g2;
1766
1767         navigation_clearroute();
1768
1769         g1 = navigation_get_bestgoal();
1770         for(;;)
1771         {
1772                 if(g2==world)
1773                         g2 = navigation_get_bestgoal();
1774
1775                 if(g2==world)
1776                 {
1777                         navigation_routetogoal(g1, self.origin);
1778                         return;
1779                 }
1780
1781                 if(navigation_routetogoal(g1, g2.origin))
1782                 {
1783                         g1 = g2;
1784                         g2 = world;
1785                         continue;
1786                 }
1787
1788                 navigation_clearroute();
1789                 g1 = g2;
1790                 g2 = world;
1791         }
1792 }
1793
1794 // removes any currently touching waypoints from the goal stack
1795 // (this is how bots detect if they have reached a goal)
1796 .float lastteleporttime;
1797
1798 void navigation_poptouchedgoals()
1799 {
1800         local vector org, m1, m2;
1801         org = self.origin;
1802         m1 = org + self.mins;
1803         m2 = org + self.maxs;
1804
1805         if(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1806         {
1807                 if(self.lastteleporttime>0)
1808                 if(time-self.lastteleporttime<0.2)
1809                 {
1810                         navigation_poproute();
1811                         return;
1812                 }
1813         }
1814
1815         // Loose goal touching check when running
1816         if(self.aistatus & AI_STATUS_RUNNING)
1817         if(self.goalcurrent.classname=="waypoint")
1818         {
1819                 if(vlen(self.origin - self.goalcurrent.origin)<150)
1820                 {
1821                         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
1822                         if(trace_fraction==1)
1823                         {
1824                                 // Detect personal waypoints
1825                                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1826                                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1827                                 {
1828                                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1829                                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1830                                 }
1831
1832                                 navigation_poproute();
1833                         }
1834                 }
1835         }
1836
1837         while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
1838         {
1839                 // Detect personal waypoints
1840                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1841                 if(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL && self.goalcurrent.owner==self)
1842                 {
1843                         self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1844                         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1845                 }
1846
1847                 navigation_poproute();
1848         }
1849 }
1850
1851 // begin a goal selection session (queries spawnfunc_waypoint network)
1852 void navigation_goalrating_start()
1853 {
1854         navigation_bestrating = -1;
1855         self.navigation_hasgoals = FALSE;
1856         navigation_bestgoals_reset();
1857         navigation_markroutes();
1858 };
1859
1860 // ends a goal selection session (updates goal stack to the best goal)
1861 void navigation_goalrating_end()
1862 {
1863         navigation_routetogoals();
1864 };
1865
1866
1867 //////////////////////////////////////////////////////////////////////////////
1868 // general bot functions
1869 //////////////////////////////////////////////////////////////////////////////
1870
1871 .float isbot; // true if this client is actually a bot
1872
1873 float skill;
1874 entity bot_list;
1875 .entity nextbot;
1876 .string netname_freeme;
1877 .string playermodel_freeme;
1878 .string playerskin_freeme;
1879
1880 float sv_maxspeed;
1881 .float createdtime;
1882
1883 .float bot_preferredcolors;
1884
1885 .float bot_attack;
1886 .float bot_dodge;
1887 .float bot_dodgerating;
1888
1889 //.float bot_painintensity;
1890 .float bot_firetimer;
1891 //.float bot_oldhealth;
1892 .void() bot_ai;
1893
1894 .entity bot_aimtarg;
1895 .float bot_aimlatency;
1896 .vector bot_aimselforigin;
1897 .vector bot_aimselfvelocity;
1898 .vector bot_aimtargorigin;
1899 .vector bot_aimtargvelocity;
1900
1901 .float bot_pickup;
1902 .float(entity player, entity item) bot_pickupevalfunc;
1903 .float bot_pickupbasevalue;
1904 .float bot_canfire;
1905 .float bot_strategytime;
1906
1907 // used for aiming currently
1908 // FIXME: make the weapon code use these and then replace the calculations here with a call to the weapon code
1909 vector shotorg;
1910 vector shotdir;
1911
1912 .float bot_forced_team;
1913 .float bot_config_loaded;
1914
1915 void bot_setnameandstuff()
1916 {
1917         local string readfile, s;
1918         local float file, tokens;
1919
1920         local string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
1921         local string name, prefix, suffix;
1922
1923         file = fopen(cvar_string("bot_config_file"), FILE_READ);
1924
1925         if(file < 0)
1926                 print(strcat("Error: Can not open the bot configuration file '",cvar_string("bot_config_file"),"'\n"));
1927         else
1928         {
1929                 RandomSelection_Init();
1930                 for(;;)
1931                 {
1932                         s = fgets(file);
1933                         if(!s)
1934                                 break;
1935                         if(substring(s, 0, 2) == "//")
1936                                 continue;
1937                         if(substring(s, 0, 1) == "#")
1938                                 continue;
1939                         RandomSelection_Add(world, 0, s, 1, 0);
1940                         readfile = RandomSelection_chosen_string;
1941                 }
1942                 fclose(file);
1943         }
1944
1945         tokens = tokenizebyseparator(readfile, "\t");
1946         if(argv(0) != "") bot_name = argv(0);
1947         else bot_name = "Bot";
1948
1949         if(argv(1) != "") bot_model = argv(1);
1950         else bot_model = "marine";
1951
1952         if(argv(2) != "") bot_skin = argv(2);
1953         else bot_skin = "0";
1954
1955         if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
1956         else bot_shirt = ftos(floor(random() * 15));
1957
1958         if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
1959         else bot_pants = ftos(floor(random() * 15));
1960
1961         self.bot_forced_team = stof(argv(5));
1962         self.bot_config_loaded = TRUE;
1963         prefix = cvar_string("bot_prefix");
1964         suffix = cvar_string("bot_suffix");
1965
1966         // this is really only a default, JoinBestTeam is called later
1967         setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
1968         self.bot_preferredcolors = self.clientcolors;
1969
1970         // pick the name
1971         if (cvar("bot_usemodelnames"))
1972                 name = bot_model;
1973         else
1974                 name = bot_name;
1975
1976         // pick the model and skin
1977         self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model, ".zym"));
1978         self.playerskin = self.playerskin_freeme = strzone(bot_skin);
1979
1980         if(!cvar("g_campaign"))
1981                 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
1982         else
1983                 self.netname = self.netname_freeme = strzone(name);
1984 };
1985
1986 float bot_custom_weapon;
1987 float bot_distance_far;
1988 float bot_distance_close;
1989
1990 float bot_weapons_far[WEP_LAST];
1991 float bot_weapons_mid[WEP_LAST];
1992 float bot_weapons_close[WEP_LAST];
1993
1994 void bot_custom_weapon_priority_setup()
1995 {
1996         local float tokens, i, c, w;
1997
1998         bot_custom_weapon = FALSE;
1999
2000         if(     cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
2001                 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
2002                 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
2003                 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
2004         )
2005                 return;
2006
2007         // Parse distances
2008         tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
2009
2010         if (tokens!=2)
2011                 return;
2012
2013         bot_distance_far = stof(argv(0));
2014         bot_distance_close = stof(argv(1));
2015
2016         if(bot_distance_far < bot_distance_close){
2017                 bot_distance_far = stof(argv(1));
2018                 bot_distance_close = stof(argv(0));
2019         }
2020
2021         // Initialize list of weapons
2022         bot_weapons_far[0] = -1;
2023         bot_weapons_mid[0] = -1;
2024         bot_weapons_close[0] = -1;
2025
2026         // Parse far distance weapon priorities
2027         tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_far")," ");
2028
2029         c = 0;
2030         for(i=0; i < tokens && c < WEP_COUNT; ++i){
2031                 w = stof(argv(i));
2032                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2033                         bot_weapons_far[c] = w;
2034                         ++c;
2035                 }
2036         }
2037         if(c < WEP_COUNT)
2038                 bot_weapons_far[c] = -1;
2039
2040         // Parse mid distance weapon priorities
2041         tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_mid")," ");
2042
2043         c = 0;
2044         for(i=0; i < tokens && c < WEP_COUNT; ++i){
2045                 w = stof(argv(i));
2046                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2047                         bot_weapons_mid[c] = w;
2048                         ++c;
2049                 }
2050         }
2051         if(c < WEP_COUNT)
2052                 bot_weapons_mid[c] = -1;
2053
2054         // Parse close distance weapon priorities
2055         tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_close")," ");
2056
2057         c = 0;
2058         for(i=0; i < tokens && i < WEP_COUNT; ++i){
2059                 w = stof(argv(i));
2060                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
2061                         bot_weapons_close[c] = w;
2062                         ++c;
2063                 }
2064         }
2065         if(c < WEP_COUNT)
2066                 bot_weapons_close[c] = -1;
2067
2068         bot_custom_weapon = TRUE;
2069 };
2070
2071
2072 void bot_endgame()
2073 {
2074         local entity e;
2075         //dprint("bot_endgame\n");
2076         e = bot_list;
2077         while (e)
2078         {
2079                 setcolor(e, e.bot_preferredcolors);
2080                 e = e.nextbot;
2081         }
2082         // if dynamic waypoints are ever implemented, save them here
2083 };
2084
2085 float bot_ignore_bots; // let bots not attack other bots (only works in non-teamplay)
2086 float bot_shouldattack(entity e)
2087 {
2088         if (e.team == self.team)
2089         {
2090                 if (e == self)
2091                         return FALSE;
2092                 if (teams_matter)
2093                 if (e.team != 0)
2094                         return FALSE;
2095         }
2096         if(!teams_matter)
2097                 if(bot_ignore_bots)
2098                         if(clienttype(e) == CLIENTTYPE_BOT)
2099                                 return FALSE;
2100         if (!e.takedamage)
2101                 return FALSE;
2102         if (e.deadflag)
2103                 return FALSE;
2104         if (e.BUTTON_CHAT)
2105                 return FALSE;
2106         if(g_minstagib)
2107         if(e.items & IT_STRENGTH)
2108                 return FALSE;
2109         if(e.flags & FL_NOTARGET)
2110                 return FALSE;
2111         return TRUE;
2112 };
2113
2114 void bot_lagfunc(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
2115 {
2116         if(self.flags & FL_INWATER)
2117         {
2118                 self.bot_aimtarg = world;
2119                 return;
2120         }
2121         self.bot_aimtarg = e1;
2122         self.bot_aimlatency = self.ping; // FIXME?  Shouldn't this be in the lag item?
2123         self.bot_aimselforigin = v1;
2124         self.bot_aimselfvelocity = v2;
2125         self.bot_aimtargorigin = v3;
2126         self.bot_aimtargvelocity = v4;
2127         if(skill <= 0)
2128                 self.bot_canfire = (random() < 0.8);
2129         else if(skill <= 1)
2130                 self.bot_canfire = (random() < 0.9);
2131         else if(skill <= 2)
2132                 self.bot_canfire = (random() < 0.95);
2133         else
2134                 self.bot_canfire = 1;
2135 };
2136
2137 .float bot_nextthink;
2138 .float bot_badaimtime;
2139 .float bot_aimthinktime;
2140 .float bot_prevaimtime;
2141 .vector bot_mouseaim;
2142 .vector bot_badaimoffset;
2143 .vector bot_1st_order_aimfilter;
2144 .vector bot_2nd_order_aimfilter;
2145 .vector bot_3th_order_aimfilter;
2146 .vector bot_4th_order_aimfilter;
2147 .vector bot_5th_order_aimfilter;
2148 .vector bot_olddesiredang;
2149 float bot_aimdir(vector v, float maxfiredeviation)
2150 {
2151         local float dist, delta_t, blend;
2152         local vector desiredang, diffang;
2153
2154         //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
2155         // make sure v_angle is sane first
2156         self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2157         self.v_angle_z = 0;
2158
2159         // get the desired angles to aim at
2160         //dprint(" at:", vtos(v));
2161         v = normalize(v);
2162         //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
2163         if (time >= self.bot_badaimtime)
2164         {
2165                 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
2166                 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5) * cvar("bot_ai_aimskill_offset");
2167         }
2168         desiredang = vectoangles(v) + self.bot_badaimoffset;
2169         //dprint(" desired:", vtos(desiredang));
2170         if (desiredang_x >= 180)
2171                 desiredang_x = desiredang_x - 360;
2172         desiredang_x = bound(-90, 0 - desiredang_x, 90);
2173         desiredang_z = self.v_angle_z;
2174         //dprint(" / ", vtos(desiredang));
2175
2176         //// pain throws off aim
2177         //if (self.bot_painintensity)
2178         //{
2179         //      // shake from pain
2180         //      desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
2181         //}
2182
2183         // calculate turn angles
2184         diffang = (desiredang - self.bot_olddesiredang);
2185         // wrap yaw turn
2186         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2187         if (diffang_y >= 180)
2188                 diffang_y = diffang_y - 360;
2189         self.bot_olddesiredang = desiredang;
2190         //dprint(" diff:", vtos(diffang));
2191
2192         delta_t = time-self.bot_prevaimtime;
2193         self.bot_prevaimtime = time;
2194         // Here we will try to anticipate the comming aiming direction
2195         self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
2196                 + (diffang * (1 / delta_t)    - self.bot_1st_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_1st"),1);
2197         self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
2198                 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_2nd"),1);
2199         self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
2200                 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_3th"),1);
2201         self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
2202                 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_4th"),1);
2203         self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
2204                 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_5th"),1);
2205
2206         //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
2207         blend = bound(0,skill,10)*0.1;
2208         desiredang = desiredang + blend *
2209         (
2210                   self.bot_1st_order_aimfilter * cvar("bot_ai_aimskill_order_mix_1st")
2211                 + self.bot_2nd_order_aimfilter * cvar("bot_ai_aimskill_order_mix_2nd")
2212                 + self.bot_3th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_3th")
2213                 + self.bot_4th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_4th")
2214                 + self.bot_5th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_5th")
2215         );
2216
2217         // calculate turn angles
2218         diffang = desiredang - self.bot_mouseaim;
2219         // wrap yaw turn
2220         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2221         if (diffang_y >= 180)
2222                 diffang_y = diffang_y - 360;
2223         //dprint(" diff:", vtos(diffang));
2224
2225         if (time >= self.bot_aimthinktime)
2226         {
2227                 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
2228                 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-skill,10));
2229         }
2230
2231         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2232
2233         diffang = self.bot_mouseaim - desiredang;
2234         // wrap yaw turn
2235         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2236         if (diffang_y >= 180)
2237                 diffang_y = diffang_y - 360;
2238         desiredang = desiredang + diffang * bound(0,cvar("bot_ai_aimskill_think"),1);
2239
2240         // calculate turn angles
2241         diffang = desiredang - self.v_angle;
2242         // wrap yaw turn
2243         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2244         if (diffang_y >= 180)
2245                 diffang_y = diffang_y - 360;
2246         //dprint(" diff:", vtos(diffang));
2247
2248         // jitter tracking
2249         dist = vlen(diffang);
2250         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
2251
2252         // turn
2253         local float r, fixedrate, blendrate;
2254         fixedrate = cvar("bot_ai_aimskill_fixedrate") / bound(1,dist,1000);
2255         blendrate = cvar("bot_ai_aimskill_blendrate");
2256         r = max(fixedrate, blendrate);
2257         //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
2258         self.v_angle = self.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
2259         self.v_angle = self.v_angle * bound(0,cvar("bot_ai_aimskill_mouse"),1) + desiredang * bound(0,(1-cvar("bot_ai_aimskill_mouse")),1);
2260         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
2261         //self.v_angle = self.v_angle + diffang * (1/ blendrate);
2262         self.v_angle_z = 0;
2263         self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
2264         //dprint(" turn:", vtos(self.v_angle));
2265
2266         makevectors(self.v_angle);
2267         shotorg = self.origin + self.view_ofs;
2268         shotdir = v_forward;
2269
2270         //dprint(" dir:", vtos(v_forward));
2271         //te_lightning2(world, shotorg, shotorg + shotdir * 100);
2272
2273         // calculate turn angles again
2274         //diffang = desiredang - self.v_angle;
2275         //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
2276         //if (diffang_y >= 180)
2277         //      diffang_y = diffang_y - 360;
2278
2279         //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
2280
2281         // decide whether to fire this time
2282         // note the maxfiredeviation is in degrees so this has to convert to radians first
2283         //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2284         if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
2285         if (vlen(trace_endpos-shotorg) < 500+500*bound(0, skill, 10) || random()*random()>bound(0,skill*0.05,1))
2286                 self.bot_firetimer = time + bound(0.1, 0.5-skill*0.05, 0.5);
2287         //traceline(shotorg,shotorg+shotdir*1000,FALSE,world);
2288         //dprint(ftos(maxfiredeviation),"\n");
2289         //dprint(" diff:", vtos(diffang), "\n");
2290
2291         return self.bot_canfire && (time < self.bot_firetimer);
2292 };
2293
2294 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
2295 {
2296         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
2297         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
2298 };
2299
2300 float bot_aim(float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
2301 {
2302         local float f, r;
2303         local vector v;
2304         /*
2305         eprint(self);
2306         dprint("bot_aim(", ftos(shotspeed));
2307         dprint(", ", ftos(shotspeedupward));
2308         dprint(", ", ftos(maxshottime));
2309         dprint(", ", ftos(applygravity));
2310         dprint(");\n");
2311         */
2312         if (!shotspeed)
2313         {
2314                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " shotspeed is zero!\n");
2315                 shotspeed = 1000000;
2316         }
2317         if (!maxshottime)
2318         {
2319                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " maxshottime is zero!\n");
2320                 maxshottime = 1;
2321         }
2322         makevectors(self.v_angle);
2323         shotorg = self.origin + self.view_ofs;
2324         shotdir = v_forward;
2325         v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
2326         local float distanceratio;
2327         distanceratio =sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/cvar("bot_ai_aimskill_firetolerance_distdegrees");
2328         distanceratio = bound(0,distanceratio,1);
2329         r =  (cvar("bot_ai_aimskill_firetolerance_maxdegrees")-cvar("bot_ai_aimskill_firetolerance_mindegrees"))
2330                 * (1-distanceratio) + cvar("bot_ai_aimskill_firetolerance_mindegrees");
2331         if (applygravity && self.bot_aimtarg)
2332         {
2333                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
2334                         return FALSE;
2335                 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
2336         }
2337         else
2338         {
2339                 f = bot_aimdir(v - shotorg, r);
2340                 //dprint("AIM: ");dprint(vtos(self.bot_aimtargorigin));dprint(" + ");dprint(vtos(self.bot_aimtargvelocity));dprint(" * ");dprint(ftos(self.bot_aimlatency + vlen(self.bot_aimtargorigin - shotorg) / shotspeed));dprint(" = ");dprint(vtos(v));dprint(" : aimdir = ");dprint(vtos(normalize(v - shotorg)));dprint(" : ");dprint(vtos(shotdir));dprint("\n");
2341                 traceline(shotorg, shotorg + shotdir * 10000, FALSE, self);
2342                 if (trace_ent.takedamage)
2343                 if (trace_fraction < 1)
2344                 if (!bot_shouldattack(trace_ent))
2345                         return FALSE;
2346                 traceline(shotorg, self.bot_aimtargorigin, FALSE, self);
2347                 if (trace_fraction < 1)
2348                 if (trace_ent != self.enemy)
2349                 if (!bot_shouldattack(trace_ent))
2350                         return FALSE;
2351         }
2352         if (r > maxshottime * shotspeed)
2353                 return FALSE;
2354         return f;
2355 };
2356
2357 // TODO: move this painintensity code to the player damage code
2358 void bot_think()
2359 {
2360         if (self.bot_nextthink > time)
2361                 return;
2362         self.bot_nextthink = self.bot_nextthink + cvar("bot_ai_thinkinterval");
2363         //if (self.bot_painintensity > 0)
2364         //      self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
2365
2366         //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
2367         //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
2368
2369         if(time < game_starttime || ((cvar("g_campaign") && !campaign_bots_may_start)))
2370         {
2371                 self.nextthink = time + 0.5;
2372                 return;
2373         }
2374
2375         if (self.fixangle)
2376         {
2377                 self.v_angle = self.angles;
2378                 self.v_angle_z = 0;
2379                 self.fixangle = FALSE;
2380         }
2381
2382         self.dmg_take = 0;
2383         self.dmg_save = 0;
2384         self.dmg_inflictor = world;
2385
2386         // calculate an aiming latency based on the skill setting
2387         // (simulated network latency + naturally delayed reflexes)
2388         //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
2389         // minimum ping 20+10 random
2390         self.ping = bound(0,0.07 - bound(0, skill * 0.005,0.05)+random()*0.01,0.65); // Now holds real lag to server, and higer skill players take a less laggy server
2391         // skill 10 = ping 0.2 (adrenaline)
2392         // skill 0 = ping 0.7 (slightly drunk)
2393
2394         // clear buttons
2395         self.BUTTON_ATCK = 0;
2396         self.button1 = 0;
2397         self.BUTTON_JUMP = 0;
2398         self.BUTTON_ATCK2 = 0;
2399         self.BUTTON_ZOOM = 0;
2400         self.BUTTON_CROUCH = 0;
2401         self.BUTTON_HOOK = 0;
2402         self.BUTTON_INFO = 0;
2403         self.button8 = 0;
2404         self.BUTTON_CHAT = 0;
2405         self.BUTTON_USE = 0;
2406
2407         // if dead, just wait until we can respawn
2408         if (self.deadflag)
2409         {
2410                 if (self.deadflag == DEAD_DEAD)
2411                 {
2412                         self.BUTTON_JUMP = 1; // press jump to respawn
2413                         self.bot_strategytime = 0;
2414                 }
2415                 return;
2416         }
2417
2418         // now call the current bot AI (havocbot for example)
2419         self.bot_ai();
2420 };
2421
2422 entity bot_strategytoken;
2423 float bot_strategytoken_taken;
2424 entity player_list;
2425 .entity nextplayer;
2426 void bot_relinkplayerlist()
2427 {
2428         local entity e;
2429         local entity prevbot;
2430         player_count = 0;
2431         currentbots = 0;
2432         player_list = e = findchainflags(flags, FL_CLIENT);
2433         bot_list = world;
2434         prevbot = world;
2435         while (e)
2436         {
2437                 player_count = player_count + 1;
2438                 e.nextplayer = e.chain;
2439                 if (clienttype(e) == CLIENTTYPE_BOT)
2440                 {
2441                         if (prevbot)
2442                                 prevbot.nextbot = e;
2443                         else
2444                                 bot_list = e;
2445                         prevbot = e;
2446                         currentbots = currentbots + 1;
2447                 }
2448                 e = e.chain;
2449         }
2450         dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
2451         bot_strategytoken = bot_list;
2452         bot_strategytoken_taken = TRUE;
2453 };
2454
2455 void() havocbot_setupbot;
2456 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam);
2457
2458 void bot_clientdisconnect()
2459 {
2460         if (clienttype(self) != CLIENTTYPE_BOT)
2461                 return;
2462         if(self.netname_freeme)
2463                 strunzone(self.netname_freeme);
2464         if(self.playermodel_freeme)
2465                 strunzone(self.playermodel_freeme);
2466         if(self.playerskin_freeme)
2467                 strunzone(self.playerskin_freeme);
2468         self.netname_freeme = string_null;
2469         self.playermodel_freeme = string_null;
2470         self.playerskin_freeme = string_null;
2471 }
2472
2473 void bot_clientconnect()
2474 {
2475         if (clienttype(self) != CLIENTTYPE_BOT)
2476                 return;
2477         self.bot_preferredcolors = self.clientcolors;
2478         self.bot_nextthink = time - random();
2479         self.lag_func = bot_lagfunc;
2480         self.isbot = TRUE;
2481         self.createdtime = self.nextthink;
2482
2483         if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
2484                 bot_setnameandstuff();
2485
2486         if(self.bot_forced_team==1)
2487                 self.team = COLOR_TEAM1;
2488         else if(self.bot_forced_team==2)
2489                 self.team = COLOR_TEAM2;
2490         else if(self.bot_forced_team==3)
2491                 self.team = COLOR_TEAM3;
2492         else if(self.bot_forced_team==4)
2493                 self.team = COLOR_TEAM4;
2494         else
2495                 JoinBestTeam(self, FALSE, TRUE);
2496
2497         havocbot_setupbot();
2498         self.bot_mouseskill=random()-0.5;
2499         self.bot_thinkskill=random()-0.5;
2500         self.bot_predictionskill=random()-0.5;
2501         self.bot_offsetskill=random()-0.5;
2502 };
2503
2504 entity bot_spawn()
2505 {
2506         local entity oldself, bot;
2507         bot = spawnclient();
2508         if (bot)
2509         {
2510                 currentbots = currentbots + 1;
2511                 oldself = self;
2512                 self = bot;
2513                 bot_setnameandstuff();
2514                 ClientConnect();
2515                 PutClientInServer();
2516                 self = oldself;
2517         }
2518         return bot;
2519 };
2520
2521 void CheckAllowedTeams(entity for_whom); void GetTeamCounts(entity other); float c1, c2, c3, c4;
2522 void bot_removefromlargestteam()
2523 {
2524         local float besttime, bestcount, thiscount;
2525         local entity best, head;
2526         CheckAllowedTeams(world);
2527         GetTeamCounts(world);
2528         head = findchainfloat(isbot, TRUE);
2529         if (!head)
2530                 return;
2531         best = head;
2532         besttime = head.createdtime;
2533         bestcount = 0;
2534         while (head)
2535         {
2536                 if(head.team == COLOR_TEAM1)
2537                         thiscount = c1;
2538                 else if(head.team == COLOR_TEAM2)
2539                         thiscount = c2;
2540                 else if(head.team == COLOR_TEAM3)
2541                         thiscount = c3;
2542                 else if(head.team == COLOR_TEAM4)
2543                         thiscount = c4;
2544                 else
2545                         thiscount = 0;
2546                 if (thiscount > bestcount)
2547                 {
2548                         bestcount = thiscount;
2549                         besttime = head.createdtime;
2550                         best = head;
2551                 }
2552                 else if (thiscount == bestcount && besttime < head.createdtime)
2553                 {
2554                         besttime = head.createdtime;
2555                         best = head;
2556                 }
2557                 head = head.chain;
2558         }
2559         currentbots = currentbots - 1;
2560         dropclient(best);
2561 };
2562
2563 void bot_removenewest()
2564 {
2565         local float besttime;
2566         local entity best, head;
2567
2568         if(teams_matter)
2569         {
2570                 bot_removefromlargestteam();
2571                 return;
2572         }
2573
2574         head = findchainfloat(isbot, TRUE);
2575         if (!head)
2576                 return;
2577         best = head;
2578         besttime = head.createdtime;
2579         while (head)
2580         {
2581                 if (besttime < head.createdtime)
2582                 {
2583                         besttime = head.createdtime;
2584                         best = head;
2585                 }
2586                 head = head.chain;
2587         }
2588         currentbots = currentbots - 1;
2589         dropclient(best);
2590 };
2591
2592 float botframe_waypointeditorlightningtime;
2593 void botframe_showwaypointlinks()
2594 {
2595         local entity player, head, w;
2596         if (time < botframe_waypointeditorlightningtime)
2597                 return;
2598         botframe_waypointeditorlightningtime = time + 0.5;
2599         player = find(world, classname, "player");
2600         while (player)
2601         {
2602                 if (!player.isbot)
2603                 if (player.flags & FL_ONGROUND || player.waterlevel > WATERLEVEL_NONE)
2604                 {
2605                         //navigation_testtracewalk = TRUE;
2606                         head = navigation_findnearestwaypoint(player, FALSE);
2607                         //navigation_testtracewalk = FALSE;
2608                         if (head)
2609                         {
2610                                 w = head     ;if (w) te_lightning2(world, w.origin, player.origin);
2611                                 w = head.wp00;if (w) te_lightning2(world, w.origin, head.origin);
2612                                 w = head.wp01;if (w) te_lightning2(world, w.origin, head.origin);
2613                                 w = head.wp02;if (w) te_lightning2(world, w.origin, head.origin);
2614                                 w = head.wp03;if (w) te_lightning2(world, w.origin, head.origin);
2615                                 w = head.wp04;if (w) te_lightning2(world, w.origin, head.origin);
2616                                 w = head.wp05;if (w) te_lightning2(world, w.origin, head.origin);
2617                                 w = head.wp06;if (w) te_lightning2(world, w.origin, head.origin);
2618                                 w = head.wp07;if (w) te_lightning2(world, w.origin, head.origin);
2619                                 w = head.wp08;if (w) te_lightning2(world, w.origin, head.origin);
2620                                 w = head.wp09;if (w) te_lightning2(world, w.origin, head.origin);
2621                                 w = head.wp10;if (w) te_lightning2(world, w.origin, head.origin);
2622                                 w = head.wp11;if (w) te_lightning2(world, w.origin, head.origin);
2623                                 w = head.wp12;if (w) te_lightning2(world, w.origin, head.origin);
2624                                 w = head.wp13;if (w) te_lightning2(world, w.origin, head.origin);
2625                                 w = head.wp14;if (w) te_lightning2(world, w.origin, head.origin);
2626                                 w = head.wp15;if (w) te_lightning2(world, w.origin, head.origin);
2627                                 w = head.wp16;if (w) te_lightning2(world, w.origin, head.origin);
2628                                 w = head.wp17;if (w) te_lightning2(world, w.origin, head.origin);
2629                                 w = head.wp18;if (w) te_lightning2(world, w.origin, head.origin);
2630                                 w = head.wp19;if (w) te_lightning2(world, w.origin, head.origin);
2631                                 w = head.wp20;if (w) te_lightning2(world, w.origin, head.origin);
2632                                 w = head.wp21;if (w) te_lightning2(world, w.origin, head.origin);
2633                                 w = head.wp22;if (w) te_lightning2(world, w.origin, head.origin);
2634                                 w = head.wp23;if (w) te_lightning2(world, w.origin, head.origin);
2635                                 w = head.wp24;if (w) te_lightning2(world, w.origin, head.origin);
2636                                 w = head.wp25;if (w) te_lightning2(world, w.origin, head.origin);
2637                                 w = head.wp26;if (w) te_lightning2(world, w.origin, head.origin);
2638                                 w = head.wp27;if (w) te_lightning2(world, w.origin, head.origin);
2639                                 w = head.wp28;if (w) te_lightning2(world, w.origin, head.origin);
2640                                 w = head.wp29;if (w) te_lightning2(world, w.origin, head.origin);
2641                                 w = head.wp30;if (w) te_lightning2(world, w.origin, head.origin);
2642                                 w = head.wp31;if (w) te_lightning2(world, w.origin, head.origin);
2643                         }
2644                 }
2645                 player = find(player, classname, "player");
2646         }
2647 };
2648
2649 entity botframe_dangerwaypoint;
2650 void botframe_updatedangerousobjects(float maxupdate)
2651 {
2652         local entity head, bot_dodgelist;
2653         local vector m1, m2, v;
2654         local float c, d, danger;
2655         c = 0;
2656         bot_dodgelist = findchainfloat(bot_dodge, TRUE);
2657         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2658         while (botframe_dangerwaypoint != world)
2659         {
2660                 danger = 0;
2661                 m1 = botframe_dangerwaypoint.mins;
2662                 m2 = botframe_dangerwaypoint.maxs;
2663                 head = bot_dodgelist;
2664                 while (head)
2665                 {
2666                         v = head.origin;
2667                         v_x = bound(m1_x, v_x, m2_x);
2668                         v_y = bound(m1_y, v_y, m2_y);
2669                         v_z = bound(m1_z, v_z, m2_z);
2670                         d = head.bot_dodgerating - vlen(head.origin - v);
2671                         if (d > 0)
2672                         {
2673                                 traceline(head.origin, v, TRUE, world);
2674                                 if (trace_fraction == 1)
2675                                         danger = danger + d;
2676                         }
2677                         head = head.chain;
2678                 }
2679                 botframe_dangerwaypoint.dmg = danger;
2680                 c = c + 1;
2681                 if (c >= maxupdate)
2682                         break;
2683                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
2684         }
2685 };
2686
2687
2688 float botframe_spawnedwaypoints;
2689 float botframe_nextthink;
2690 float botframe_nextdangertime;
2691
2692 float autoskill_nextthink;
2693 .float totalfrags_lastcheck;
2694 void autoskill(float factor)
2695 {
2696         float bestbot;
2697         float bestplayer;
2698         entity head;
2699
2700         bestbot = -1;
2701         bestplayer = -1;
2702         FOR_EACH_PLAYER(head)
2703         {
2704                 if(clienttype(head) == CLIENTTYPE_REAL)
2705                         bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
2706                 else
2707                         bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
2708         }
2709
2710         dprint("autoskill: best player got ", ftos(bestplayer), ", ");
2711         dprint("best bot got ", ftos(bestbot), "; ");
2712         if(bestbot < 0 || bestplayer < 0)
2713         {
2714                 dprint("not doing anything\n");
2715                 // don't return, let it reset all counters below
2716         }
2717         else if(bestbot <= bestplayer * factor - 2)
2718         {
2719                 if(cvar("skill") < 17)
2720                 {
2721                         dprint("2 frags difference, increasing skill\n");
2722                         cvar_set("skill", ftos(cvar("skill") + 1));
2723                         bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
2724                 }
2725         }
2726         else if(bestbot >= bestplayer * factor + 2)
2727         {
2728                 if(cvar("skill") > 0)
2729                 {
2730                         dprint("2 frags difference, decreasing skill\n");
2731                         cvar_set("skill", ftos(cvar("skill") - 1));
2732                         bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
2733                 }
2734         }
2735         else
2736         {
2737                 dprint("not doing anything\n");
2738                 return;
2739                 // don't reset counters, wait for them to accumulate
2740         }
2741
2742         FOR_EACH_PLAYER(head)
2743                 head.totalfrags_lastcheck = head.totalfrags;
2744 }
2745
2746 float bot_cvar_nextthink;
2747 void bot_serverframe()
2748 {
2749         float realplayers, bots, activerealplayers;
2750         entity head;
2751
2752         if (intermission_running)
2753                 return;
2754
2755         if (time < 2)
2756                 return;
2757
2758         if(time > autoskill_nextthink)
2759         {
2760                 float a;
2761                 a = cvar("skill_auto");
2762                 if(a)
2763                         autoskill(a);
2764                 autoskill_nextthink = time + 5;
2765         }
2766
2767         activerealplayers = 0;
2768         realplayers = 0;
2769
2770         FOR_EACH_REALCLIENT(head)
2771         {
2772                 if(head.classname == "player" || g_lms || g_arena)
2773                         ++activerealplayers;
2774                 ++realplayers;
2775         }
2776
2777         // add/remove bots if needed to make sure there are at least
2778         // minplayers+bot_number, or remove all bots if no one is playing
2779         // But don't remove bots immediately on level change, as the real players
2780         // usually haven't rejoined yet
2781         bots_would_leave = FALSE;
2782         if (realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5))
2783         {
2784                 float realminplayers, minplayers;
2785                 realminplayers = cvar("minplayers");
2786                 minplayers = max(0, floor(realminplayers));
2787
2788                 float realminbots, minbots;
2789                 if(cvar("bot_vs_human"))
2790                         realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
2791                 else
2792                         realminbots = cvar("bot_number");
2793                 minbots = max(0, floor(realminbots));
2794
2795                 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
2796                 if(bots > minbots)
2797                         bots_would_leave = TRUE;
2798         }
2799         else
2800         {
2801                 // if there are no players, remove bots
2802                 bots = 0;
2803         }
2804
2805         bot_ignore_bots = cvar("bot_ignore_bots");
2806
2807         // only add one bot per frame to avoid utter chaos
2808         if(time > botframe_nextthink)
2809         {
2810                 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
2811                 while (currentbots < bots)
2812                 {
2813                         if (bot_spawn() == world)
2814                         {
2815                                 bprint("Can not add bot, server full.\n");
2816                                 botframe_nextthink = time + 10;
2817                                 break;
2818                         }
2819                 }
2820                 while (currentbots > bots)
2821                         bot_removenewest();
2822         }
2823
2824         if(botframe_spawnedwaypoints)
2825         {
2826                 if(cvar("waypoint_benchmark"))
2827                         localcmd("quit\n");
2828         }
2829
2830         if (currentbots > 0 || cvar("g_waypointeditor"))
2831         if (botframe_spawnedwaypoints)
2832         {
2833                 if(botframe_cachedwaypointlinks)
2834                 {
2835                         if(!botframe_loadedforcedlinks)
2836                                 waypoint_load_links_hardwired();
2837                 }
2838                 else
2839                 {
2840                         // TODO: Make this check cleaner
2841                         local entity wp = findchain(classname, "waypoint");
2842                         if(time - wp.nextthink > 10)
2843                                 waypoint_save_links();
2844                 }
2845         }
2846         else
2847         {
2848                 botframe_spawnedwaypoints = TRUE;
2849                 waypoint_loadall();
2850                 if(!waypoint_load_links())
2851                         waypoint_schedulerelinkall();
2852         }
2853
2854         if (bot_list)
2855         {
2856                 // cycle the goal token from one bot to the next each frame
2857                 // (this prevents them from all doing spawnfunc_waypoint searches on the same
2858                 //  frame, which causes choppy framerates)
2859                 if (bot_strategytoken_taken)
2860                 {
2861                         bot_strategytoken_taken = FALSE;
2862                         if (bot_strategytoken)
2863                                 bot_strategytoken = bot_strategytoken.nextbot;
2864                         if (!bot_strategytoken)
2865                                 bot_strategytoken = bot_list;
2866                 }
2867
2868                 if (botframe_nextdangertime < time)
2869                 {
2870                         local float interval;
2871                         interval = cvar("bot_ai_dangerdetectioninterval");
2872                         if (botframe_nextdangertime < time - interval * 1.5)
2873                                 botframe_nextdangertime = time;
2874                         botframe_nextdangertime = botframe_nextdangertime + interval;
2875                         botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
2876                 }
2877         }
2878
2879         if (cvar("g_waypointeditor"))
2880                 botframe_showwaypointlinks();
2881
2882         if(time > bot_cvar_nextthink)
2883         {
2884                 if(currentbots>1)
2885                         bot_custom_weapon_priority_setup();
2886                 bot_cvar_nextthink = time + 5;
2887         }
2888 };