]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/bots.qc
set lowprecision whereever possible
[divverent/nexuiz.git] / data / qcsrc / server / bots.qc
1 // rough simulation of walking from one point to another to test if a path
2 // can be traveled, used by havocbot
3
4
5 vector stepheightvec;
6 float navigation_testtracewalk;
7 float(entity e, vector start, vector m1, vector m2, vector end) tracewalk =
8 {
9         local vector org;
10         local vector move;
11         local vector dir;
12         local float dist;
13         local float totaldist;
14         local float stepdist;
15         local float yaw;
16         local float ignorehazards;
17         if (navigation_testtracewalk)
18         {
19                 if (navigation_testtracewalk > 1)
20                         dprint("tracewalk: ");
21                 //te_wizspike(start);
22                 //te_knightspike(end);
23                 //te_lightning2(world, start, end);
24         }
25         move = end - start;
26         move_z = 0;
27         org = start;
28         dist = totaldist = vlen(move);
29         dir = normalize(move);
30         stepdist = 32;
31         ignorehazards = FALSE;
32         //self.angles = vectoangles(dir);
33         traceline(start, start, FALSE, e);
34         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
35                 ignorehazards = TRUE;
36         tracebox(start, m1, m2, start, TRUE, e);
37         if (trace_startsolid)
38         {
39                 // failed - bad start
40                 if (navigation_testtracewalk)
41                 {
42                         if (navigation_testtracewalk > 1)
43                                 dprint("bad-start\n");
44                         te_knightspike(start);
45                 }
46                 return 0;
47         }
48         yaw = vectoyaw(move);
49         move = end - org;
50         while (1)
51         {
52                 if (boxesoverlap(end, end, org + m1 + '-1 -1 -1', org + m2 + '1 1 1'))
53                 {
54                         if (navigation_testtracewalk)
55                         {
56                                 if (navigation_testtracewalk > 1)
57                                         dprint("success\n");
58                                 te_wizspike(org);
59                         }
60                         // succeeded
61                         return 1;
62                 }
63                 if (dist <= 0)
64                         break;
65                 if (navigation_testtracewalk)
66                 {
67                         //dprint("trying ");
68                         //te_gunshot(org);
69                         particle(org, '0 0 0', 104, 8);
70                 }
71                 if (stepdist > dist)
72                         stepdist = dist;
73                 dist = dist - stepdist;
74                 traceline(org, org, FALSE, e);
75                 if (!ignorehazards)
76                 {
77                         if (trace_dpstartcontents & (DPCONTENTS_SLIME | DPCONTENTS_LAVA))
78                         {
79                                 if (navigation_testtracewalk)
80                                 {
81                                         if (navigation_testtracewalk > 1)
82                                                 dprint("hazard\n");
83                                         te_gunshot(org);
84                                 }
85                                 // hazards blocking path
86                                 return 0;
87                         }
88                 }
89                 if (trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
90                 {
91                         move = normalize(end - org);
92                         tracebox(org, m1, m2, org + move * stepdist, MOVE_WORLDONLY, e);
93                         if (trace_fraction < 1)
94                         {
95                                 if (navigation_testtracewalk)
96                                 {
97                                         if (navigation_testtracewalk > 1)
98                                                 dprint("swimming-hit-something\n");
99                                         //particle(org, move * 64, 104, 4);
100                                         te_gunshot(org);
101                                 }
102                                 // failed
103                                 return 0;
104                         }
105                         org = trace_endpos;
106                 }
107                 else
108                 {
109                         move = dir * stepdist + org;
110                         tracebox(org, m1, m2, move, MOVE_WORLDONLY, e);
111                         if (trace_fraction < 1)
112                         {
113                                 tracebox(org + stepheightvec, m1, m2, move + stepheightvec, MOVE_WORLDONLY, e);
114                                 if (trace_fraction < 1 || trace_startsolid)
115                                 {
116                                         if (navigation_testtracewalk)
117                                         {
118                                                 if (navigation_testtracewalk > 1)
119                                                         dprint("hit-something\n");
120                                                 //move = normalize(end - org);
121                                                 //particle(org, move * 64, 104, 4);
122                                                 te_gunshot(org);
123                                         }
124                                         // failed
125                                         return 0;
126                                 }
127                         }
128                         move = trace_endpos;
129                         tracebox(move, m1, m2, move + '0 0 -65536', FALSE, e);
130                         /*
131                         // trace down from stepheight as far as possible and move there,
132                         // if this starts in solid we try again without the stepup, and
133                         // if that also fails we assume it is a wall
134                         // (this is the same logic as the Quake walkmove function used)
135                         tracebox(move + '0 0 18', m1, m2, move + '0 0 -65536', FALSE, e);
136                         if (trace_startsolid)
137                         {
138                                 tracebox(move, m1, m2, move + '0 0 -65536', FALSE, e);
139                                 if (trace_startsolid)
140                                 {
141                                         if (navigation_testtracewalk)
142                                         {
143                                                 if (navigation_testtracewalk > 1)
144                                                         dprint("hit-something\n");
145                                                 //move = normalize(end - org);
146                                                 //particle(org, move * 64, 104, 4);
147                                                 te_knightspike(org);
148                                         }
149                                         // failed
150                                         return 0;
151                                 }
152                         }
153                         */
154                         // moved successfully
155                         if (navigation_testtracewalk > 1)
156                                 dprint("moved ");
157                         org = trace_endpos;
158                 }
159         }
160         if (navigation_testtracewalk)
161         {
162                 if (navigation_testtracewalk > 1)
163                         dprint("wrong-place\n");
164                 te_knightspike(org);
165                 //te_gunshot(end);
166         }
167         // moved but didn't arrive at the intended destination
168         return 0;
169 };
170
171
172 // grenade tracing to decide the best pitch to fire at
173
174 entity tracetossent;
175 entity tracetossfaketarget;
176
177 // traces multiple trajectories to find one that will impact the target
178 // 'end' vector is the place it aims for,
179 // returns TRUE only if it hit targ (don't target non-solid entities)
180 vector findtrajectory_velocity;
181 float(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore) findtrajectorywithleading =
182 {
183         local float c, savesolid, shottime;
184         local vector dir, end, v;
185         if (shotspeed < 1)
186                 return FALSE; // could cause division by zero if calculated
187         if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
188                 return FALSE; // could never hit it
189         if (!tracetossent)
190                 tracetossent = spawn();
191         tracetossent.owner = ignore;
192         setsize(tracetossent, m1, m2);
193         savesolid = targ.solid;
194         targ.solid = SOLID_NOT;
195         shottime = ((vlen(targ.origin - org) / shotspeed) + shotdelay);
196         v = targ.velocity * shottime + targ.origin;
197         tracebox(targ.origin, targ.mins, targ.maxs, v, FALSE, targ);
198         v = trace_endpos;
199         end = v + (targ.mins + targ.maxs) * 0.5;
200         if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
201         {
202                 // out of range
203                 targ.solid = savesolid;
204                 return FALSE;
205         }
206
207         if (!tracetossfaketarget)
208                 tracetossfaketarget = spawn();
209         tracetossfaketarget.solid = savesolid;
210         tracetossfaketarget.movetype = targ.movetype;
211         setmodel(tracetossfaketarget, targ.model); // no low precision
212         tracetossfaketarget.model = targ.model;
213         tracetossfaketarget.modelindex = targ.modelindex;
214         setsize(tracetossfaketarget, targ.mins, targ.maxs);
215         setorigin(tracetossfaketarget, v);
216
217         c = 0;
218         dir = normalize(end - org);
219         while (c < 10) // 10 traces
220         {
221                 setorigin(tracetossent, org); // reset
222                 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
223                 tracetoss(tracetossent, ignore); // love builtin functions...
224                 if (trace_ent == tracetossfaketarget) // done
225                 {
226                         targ.solid = savesolid;
227
228                         // make it disappear
229                         tracetossfaketarget.solid = SOLID_NOT;
230                         tracetossfaketarget.movetype = MOVETYPE_NONE;
231                         tracetossfaketarget.model = "";
232                         tracetossfaketarget.modelindex = 0;
233                         // relink to remove it from physics considerations
234                         setorigin(tracetossfaketarget, v);
235
236                         return TRUE;
237                 }
238                 dir_z = dir_z + 0.1; // aim up a little more
239                 c = c + 1;
240         }
241         targ.solid = savesolid;
242
243         // make it disappear
244         tracetossfaketarget.solid = SOLID_NOT;
245         tracetossfaketarget.movetype = MOVETYPE_NONE;
246         tracetossfaketarget.model = "";
247         tracetossfaketarget.modelindex = 0;
248         // relink to remove it from physics considerations
249         setorigin(tracetossfaketarget, v);
250
251         // leave a valid one even if it won't reach
252         findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
253         return FALSE;
254 };
255
256
257
258 // lag simulation
259
260 .void(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) lag_func;
261
262 // upto 5 queued messages
263 .float lag1_time;
264 .float lag1_float1;
265 .float lag1_float2;
266 .entity lag1_entity1;
267 .vector lag1_vec1;
268 .vector lag1_vec2;
269 .vector lag1_vec3;
270 .vector lag1_vec4;
271
272 .float lag2_time;
273 .float lag2_float1;
274 .float lag2_float2;
275 .entity lag2_entity1;
276 .vector lag2_vec1;
277 .vector lag2_vec2;
278 .vector lag2_vec3;
279 .vector lag2_vec4;
280
281 .float lag3_time;
282 .float lag3_float1;
283 .float lag3_float2;
284 .entity lag3_entity1;
285 .vector lag3_vec1;
286 .vector lag3_vec2;
287 .vector lag3_vec3;
288 .vector lag3_vec4;
289
290 .float lag4_time;
291 .float lag4_float1;
292 .float lag4_float2;
293 .entity lag4_entity1;
294 .vector lag4_vec1;
295 .vector lag4_vec2;
296 .vector lag4_vec3;
297 .vector lag4_vec4;
298
299 .float lag5_time;
300 .float lag5_float1;
301 .float lag5_float2;
302 .entity lag5_entity1;
303 .vector lag5_vec1;
304 .vector lag5_vec2;
305 .vector lag5_vec3;
306 .vector lag5_vec4;
307
308 void() lag_update =
309 {
310         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;}
311         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;}
312         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;}
313         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;}
314         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;}
315 };
316
317 float(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) lag_additem =
318 {
319         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;}
320         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;}
321         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;}
322         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;}
323         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;}
324         // no room for it (what is the best thing to do here??)
325         return FALSE;
326 };
327
328
329 // Random skill system
330 .float bot_thinkskill;
331 .float bot_mouseskill;
332 .float bot_predictionskill;
333 .float bot_offsetskill;
334
335
336 // waypoint navigation system
337
338 // itemscore = (howmuchmoreIwant / howmuchIcanwant) / itemdistance
339 // waypointscore = 0.7 / waypointdistance
340
341 .entity wp00, wp01, wp02, wp03, wp04, wp05, wp06, wp07;
342 .entity wp08, wp09, wp10, wp11, wp12, wp13, wp14, wp15;
343 .entity wp16, wp17, wp18, wp19, wp20, wp21, wp22, wp23, wp24, wp25, wp26, wp27, wp28, wp29, wp30, wp31;
344 .float wp00mincost, wp01mincost, wp02mincost, wp03mincost, wp04mincost, wp05mincost, wp06mincost, wp07mincost;
345 .float wp08mincost, wp09mincost, wp10mincost, wp11mincost, wp12mincost, wp13mincost, wp14mincost, wp15mincost;
346 .float wp16mincost, wp17mincost, wp18mincost, wp19mincost, wp20mincost, wp21mincost, wp22mincost, wp23mincost, wp24mincost, wp25mincost, wp26mincost, wp27mincost, wp28mincost, wp29mincost, wp30mincost, wp31mincost;
347 .float wpfire, wpcost;
348 .float wpisbox;
349 .float wpflags;
350 .vector wpnearestpoint;
351
352 // stack of current goals (the last one of which may be an item or other
353 // desirable object, the rest are typically waypoints to reach it)
354 .entity goalcurrent, goalstack01, goalstack02, goalstack03;
355 .entity goalstack04, goalstack05, goalstack06, goalstack07;
356 .entity goalstack08, goalstack09, goalstack10, goalstack11;
357 .entity goalstack12, goalstack13, goalstack14, goalstack15;
358 .entity goalstack16, goalstack17, goalstack18, goalstack19;
359 .entity goalstack20, goalstack21, goalstack22, goalstack23;
360 .entity goalstack24, goalstack25, goalstack26, goalstack27;
361 .entity goalstack28, goalstack29, goalstack30, goalstack31;
362
363 .entity nearestwaypoint;
364 .float nearestwaypointtimeout;
365
366 // used during navigation_goalrating_begin/end sessions
367 float navigation_bestrating;
368 entity navigation_bestgoal;
369
370
371
372
373
374 /////////////////////////////////////////////////////////////////////////////
375 // waypoint management
376 /////////////////////////////////////////////////////////////////////////////
377
378 // waypoints with this flag are not saved, they are automatically generated
379 // waypoints like jump pads, teleporters, and items
380 float WAYPOINTFLAG_GENERATED = 8388608;
381 float WAYPOINTFLAG_ITEM = 4194304;
382 float WAYPOINTFLAG_TELEPORT = 2097152;
383 float WAYPOINTFLAG_NORELINK = 1048576;
384
385 // add a new link to the waypoint, replacing the furthest link it already has
386 void(entity from, entity to) waypoint_addlink =
387 {
388         local float c;
389
390         if (from == to)
391                 return;
392         if (from.wpflags & WAYPOINTFLAG_NORELINK)
393                 return;
394
395         if (from.wp00 == to) return;if (from.wp01 == to) return;if (from.wp02 == to) return;if (from.wp03 == to) return;
396         if (from.wp04 == to) return;if (from.wp05 == to) return;if (from.wp06 == to) return;if (from.wp07 == to) return;
397         if (from.wp08 == to) return;if (from.wp09 == to) return;if (from.wp10 == to) return;if (from.wp11 == to) return;
398         if (from.wp12 == to) return;if (from.wp13 == to) return;if (from.wp14 == to) return;if (from.wp15 == to) return;
399         if (from.wp16 == to) return;if (from.wp17 == to) return;if (from.wp18 == to) return;if (from.wp19 == to) return;
400         if (from.wp20 == to) return;if (from.wp21 == to) return;if (from.wp22 == to) return;if (from.wp23 == to) return;
401         if (from.wp24 == to) return;if (from.wp25 == to) return;if (from.wp26 == to) return;if (from.wp27 == to) return;
402         if (from.wp28 == to) return;if (from.wp29 == to) return;if (from.wp30 == to) return;if (from.wp31 == to) return;
403
404         if (to.wpisbox || from.wpisbox)
405         {
406                 // if either is a box we have to find the nearest points on them to
407                 // calculate the distance properly
408                 local vector v1, v2, m1, m2;
409                 v1 = from.origin;
410                 m1 = to.absmin;
411                 m2 = to.absmax;
412                 v1_x = bound(m1_x, v1_x, m2_x);
413                 v1_y = bound(m1_y, v1_y, m2_y);
414                 v1_z = bound(m1_z, v1_z, m2_z);
415                 v2 = to.origin;
416                 m1 = from.absmin;
417                 m2 = from.absmax;
418                 v2_x = bound(m1_x, v2_x, m2_x);
419                 v2_y = bound(m1_y, v2_y, m2_y);
420                 v2_z = bound(m1_z, v2_z, m2_z);
421                 v2 = to.origin;
422                 c = vlen(v2 - v1);
423         }
424         else
425                 c = vlen(to.origin - from.origin);
426
427         if (from.wp31mincost < c) return;
428         if (from.wp30mincost < c) {from.wp31 = to;from.wp31mincost = c;return;} from.wp31 = from.wp30;from.wp31mincost = from.wp30mincost;
429         if (from.wp29mincost < c) {from.wp30 = to;from.wp30mincost = c;return;} from.wp30 = from.wp29;from.wp30mincost = from.wp29mincost;
430         if (from.wp28mincost < c) {from.wp29 = to;from.wp29mincost = c;return;} from.wp29 = from.wp28;from.wp29mincost = from.wp28mincost;
431         if (from.wp27mincost < c) {from.wp28 = to;from.wp28mincost = c;return;} from.wp28 = from.wp27;from.wp28mincost = from.wp27mincost;
432         if (from.wp26mincost < c) {from.wp27 = to;from.wp27mincost = c;return;} from.wp27 = from.wp26;from.wp27mincost = from.wp26mincost;
433         if (from.wp25mincost < c) {from.wp26 = to;from.wp26mincost = c;return;} from.wp26 = from.wp25;from.wp26mincost = from.wp25mincost;
434         if (from.wp24mincost < c) {from.wp25 = to;from.wp25mincost = c;return;} from.wp25 = from.wp24;from.wp25mincost = from.wp24mincost;
435         if (from.wp23mincost < c) {from.wp24 = to;from.wp24mincost = c;return;} from.wp24 = from.wp23;from.wp24mincost = from.wp23mincost;
436         if (from.wp22mincost < c) {from.wp23 = to;from.wp23mincost = c;return;} from.wp23 = from.wp22;from.wp23mincost = from.wp22mincost;
437         if (from.wp21mincost < c) {from.wp22 = to;from.wp22mincost = c;return;} from.wp22 = from.wp21;from.wp22mincost = from.wp21mincost;
438         if (from.wp20mincost < c) {from.wp21 = to;from.wp21mincost = c;return;} from.wp21 = from.wp20;from.wp21mincost = from.wp20mincost;
439         if (from.wp19mincost < c) {from.wp20 = to;from.wp20mincost = c;return;} from.wp20 = from.wp19;from.wp20mincost = from.wp19mincost;
440         if (from.wp18mincost < c) {from.wp19 = to;from.wp19mincost = c;return;} from.wp19 = from.wp18;from.wp19mincost = from.wp18mincost;
441         if (from.wp17mincost < c) {from.wp18 = to;from.wp18mincost = c;return;} from.wp18 = from.wp17;from.wp18mincost = from.wp17mincost;
442         if (from.wp16mincost < c) {from.wp17 = to;from.wp17mincost = c;return;} from.wp17 = from.wp16;from.wp17mincost = from.wp16mincost;
443         if (from.wp15mincost < c) {from.wp16 = to;from.wp16mincost = c;return;} from.wp16 = from.wp15;from.wp16mincost = from.wp15mincost;
444         if (from.wp14mincost < c) {from.wp15 = to;from.wp15mincost = c;return;} from.wp15 = from.wp14;from.wp15mincost = from.wp14mincost;
445         if (from.wp13mincost < c) {from.wp14 = to;from.wp14mincost = c;return;} from.wp14 = from.wp13;from.wp14mincost = from.wp13mincost;
446         if (from.wp12mincost < c) {from.wp13 = to;from.wp13mincost = c;return;} from.wp13 = from.wp12;from.wp13mincost = from.wp12mincost;
447         if (from.wp11mincost < c) {from.wp12 = to;from.wp12mincost = c;return;} from.wp12 = from.wp11;from.wp12mincost = from.wp11mincost;
448         if (from.wp10mincost < c) {from.wp11 = to;from.wp11mincost = c;return;} from.wp11 = from.wp10;from.wp11mincost = from.wp10mincost;
449         if (from.wp09mincost < c) {from.wp10 = to;from.wp10mincost = c;return;} from.wp10 = from.wp09;from.wp10mincost = from.wp09mincost;
450         if (from.wp08mincost < c) {from.wp09 = to;from.wp09mincost = c;return;} from.wp09 = from.wp08;from.wp09mincost = from.wp08mincost;
451         if (from.wp07mincost < c) {from.wp08 = to;from.wp08mincost = c;return;} from.wp08 = from.wp07;from.wp08mincost = from.wp07mincost;
452         if (from.wp06mincost < c) {from.wp07 = to;from.wp07mincost = c;return;} from.wp07 = from.wp06;from.wp07mincost = from.wp06mincost;
453         if (from.wp05mincost < c) {from.wp06 = to;from.wp06mincost = c;return;} from.wp06 = from.wp05;from.wp06mincost = from.wp05mincost;
454         if (from.wp04mincost < c) {from.wp05 = to;from.wp05mincost = c;return;} from.wp05 = from.wp04;from.wp05mincost = from.wp04mincost;
455         if (from.wp03mincost < c) {from.wp04 = to;from.wp04mincost = c;return;} from.wp04 = from.wp03;from.wp04mincost = from.wp03mincost;
456         if (from.wp02mincost < c) {from.wp03 = to;from.wp03mincost = c;return;} from.wp03 = from.wp02;from.wp03mincost = from.wp02mincost;
457         if (from.wp01mincost < c) {from.wp02 = to;from.wp02mincost = c;return;} from.wp02 = from.wp01;from.wp02mincost = from.wp01mincost;
458         if (from.wp00mincost < c) {from.wp01 = to;from.wp01mincost = c;return;} from.wp01 = from.wp00;from.wp01mincost = from.wp00mincost;
459         from.wp00 = to;from.wp00mincost = c;return;
460 };
461
462 // relink this waypoint
463 // (precompile a list of all reachable waypoints from this waypoint)
464 // (SLOW!)
465 void() waypoint_think =
466 {
467         local entity e;
468         local vector sv, sm1, sm2, ev, em1, em2;
469         //dprint("waypoint_think wpisbox = ", ftos(self.wpisbox), "\n");
470         sm1 = self.origin + self.mins;
471         sm2 = self.origin + self.maxs;
472         e = find(world, classname, "waypoint");
473         stepheightvec = cvar("sv_stepheight") * '0 0 1';
474         while (e)
475         {
476                 if (boxesoverlap(self.absmin, self.absmax, e.absmin, e.absmax))
477                 {
478                         waypoint_addlink(self, e);
479                         waypoint_addlink(e, self);
480                 }
481                 else
482                 {
483                         sv = e.origin;
484                         sv_x = bound(sm1_x, sv_x, sm2_x);
485                         sv_y = bound(sm1_y, sv_y, sm2_y);
486                         sv_z = bound(sm1_z, sv_z, sm2_z);
487                         ev = self.origin;
488                         em1 = e.origin + e.mins;
489                         em2 = e.origin + e.maxs;
490                         ev_x = bound(em1_x, ev_x, em2_x);
491                         ev_y = bound(em1_y, ev_y, em2_y);
492                         ev_z = bound(em1_z, ev_z, em2_z);
493                         if (vlen(ev - sv) < self.wp31mincost)
494                         {
495                                 navigation_testtracewalk = 0;
496                                 if (!self.wpisbox)
497                                 {
498                                         tracebox(sv - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, sv, FALSE, self);
499                                         if (!trace_startsolid)
500                                         {
501                                                 //dprint("sv deviation", vtos(trace_endpos - sv), "\n");
502                                                 sv = trace_endpos + '0 0 1';
503                                         }
504                                 }
505                                 if (!e.wpisbox)
506                                 {
507                                         tracebox(ev - PL_MIN_z * '0 0 1', PL_MIN, PL_MAX, ev, FALSE, e);
508                                         if (!trace_startsolid)
509                                         {
510                                                 //dprint("ev deviation", vtos(trace_endpos - ev), "\n");
511                                                 ev = trace_endpos + '0 0 1';
512                                         }
513                                 }
514                                 //traceline(self.origin, e.origin, FALSE, world);
515                                 //if (trace_fraction == 1)
516                                 if (!self.wpisbox)
517                                 if (tracewalk(self, sv, PL_MIN, PL_MAX, ev))
518                                         waypoint_addlink(self, e);
519                                 if (!e.wpisbox)
520                                 if (tracewalk(e, ev, PL_MIN, PL_MAX, sv))
521                                         waypoint_addlink(e, self);
522                         }
523                 }
524                 e = find(e, classname, "waypoint");
525         }
526         navigation_testtracewalk = 0;
527 };
528
529 void(entity wp) waypoint_clearlinks =
530 {
531         // clear links to other waypoints
532         local float f;
533         f = 1050; // maximum search range for other waypoints
534         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;
535         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;
536         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;
537         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;
538 };
539
540 // tell a waypoint to relink
541 void(entity wp) waypoint_schedulerelink =
542 {
543         if (wp == world)
544                 return;
545         // TODO: add some sort of visible box in edit mode for box waypoints
546         if (cvar("g_waypointeditor"))
547         {
548                 local vector m1, m2;
549                 m1 = wp.mins;
550                 m2 = wp.maxs;
551                 setmodel(wp, "models/runematch/rune.mdl"); wp.effects = EF_LOWPRECISION;
552                 setsize(wp, m1, m2);
553         }
554         else
555                 wp.model = "";
556         wp.wpisbox = vlen(wp.size) > 0;
557         wp.enemy = world;
558         wp.owner = world;
559         if (!(wp.wpflags & WAYPOINTFLAG_NORELINK))
560                 waypoint_clearlinks(wp);
561         // schedule an actual relink on next frame
562         wp.think = waypoint_think;
563         wp.nextthink = time;
564         wp.effects = EF_LOWPRECISION;
565 }
566
567 // create a new waypoint and automatically link it to other waypoints, and link
568 // them back to it as well
569 // (suitable for waypoint editor)
570 entity(vector m1, vector m2, float f) waypoint_spawn =
571 {
572         local entity w;
573         local vector org;
574         org = (m1 + m2) * 0.5;
575         m1 = m1 - org;
576         m2 = m2 - org;
577         w = find(world, classname, "waypoint");
578         while (w)
579         {
580                 // if a matching waypoint already exists, don't add a duplicate
581                 if (w.origin == org && w.mins == m1 && w.maxs == m2)
582                         return w;
583                 w = find(w, classname, "waypoint");
584         }
585         w = spawn();
586         w.classname = "waypoint";
587         w.wpflags = f;
588         setorigin(w, org);
589         setsize(w, m1, m2);
590         waypoint_clearlinks(w);
591         //waypoint_schedulerelink(w);
592         return w;
593 };
594
595 // waypoint map entity
596 void() waypoint =
597 {
598         setorigin(self, self.origin);
599         // schedule a relink after other waypoints have had a chance to spawn
600         waypoint_clearlinks(self);
601         //waypoint_schedulerelink(self);
602 };
603
604 // remove a waypoint, and schedule all neighbors to relink
605 void(entity e) waypoint_remove =
606 {
607         // tell all linked waypoints that they need to relink
608         waypoint_schedulerelink(e.wp00);
609         waypoint_schedulerelink(e.wp01);
610         waypoint_schedulerelink(e.wp02);
611         waypoint_schedulerelink(e.wp03);
612         waypoint_schedulerelink(e.wp04);
613         waypoint_schedulerelink(e.wp05);
614         waypoint_schedulerelink(e.wp06);
615         waypoint_schedulerelink(e.wp07);
616         waypoint_schedulerelink(e.wp08);
617         waypoint_schedulerelink(e.wp09);
618         waypoint_schedulerelink(e.wp10);
619         waypoint_schedulerelink(e.wp11);
620         waypoint_schedulerelink(e.wp12);
621         waypoint_schedulerelink(e.wp13);
622         waypoint_schedulerelink(e.wp14);
623         waypoint_schedulerelink(e.wp15);
624         waypoint_schedulerelink(e.wp16);
625         waypoint_schedulerelink(e.wp17);
626         waypoint_schedulerelink(e.wp18);
627         waypoint_schedulerelink(e.wp19);
628         waypoint_schedulerelink(e.wp20);
629         waypoint_schedulerelink(e.wp21);
630         waypoint_schedulerelink(e.wp22);
631         waypoint_schedulerelink(e.wp23);
632         waypoint_schedulerelink(e.wp24);
633         waypoint_schedulerelink(e.wp25);
634         waypoint_schedulerelink(e.wp26);
635         waypoint_schedulerelink(e.wp27);
636         waypoint_schedulerelink(e.wp28);
637         waypoint_schedulerelink(e.wp29);
638         waypoint_schedulerelink(e.wp30);
639         waypoint_schedulerelink(e.wp31);
640         // and now remove the waypoint
641         remove(e);
642 };
643
644 // empties the map of waypoints
645 void() waypoint_removeall =
646 {
647         local entity head, next;
648         head = findchain(classname, "waypoint");
649         while (head)
650         {
651                 next = head.chain;
652                 remove(head);
653                 head = next;
654         }
655 };
656
657 // tell all waypoints to relink
658 // (is this useful at all?)
659 void() waypoint_schedulerelinkall =
660 {
661         local entity head;
662         head = findchain(classname, "waypoint");
663         while (head)
664         {
665                 waypoint_schedulerelink(head);
666                 head = head.chain;
667         }
668 };
669
670 // save waypoints to gamedir/data/maps/mapname.waypoints
671 // TODO: support saving wayboxes
672 void() waypoint_saveall =
673 {
674         local string filename, s;
675         local float file, c;
676         local entity w;
677         filename = strcat("maps/", mapname);
678         filename = strcat(filename, ".waypoints");
679         file = fopen(filename, FILE_WRITE);
680         if (file >= 0)
681         {
682                 c = 0;
683                 w = findchain(classname, "waypoint");
684                 while (w)
685                 {
686                         if (!(w.wpflags & WAYPOINTFLAG_GENERATED))
687                         {
688                                 s = strcat(vtos(w.origin + w.mins), "\n");
689                                 s = strcat(s, vtos(w.origin + w.maxs));
690                                 s = strcat(s, "\n");
691                                 s = strcat(s, ftos(w.wpflags));
692                                 s = strcat(s, "\n");
693                                 fputs(file, s);
694                                 c = c + 1;
695                         }
696                         w = w.chain;
697                 }
698                 fclose(file);
699                 bprint("saved ");
700                 bprint(ftos(c));
701                 bprint(" waypoints to maps/");
702                 bprint(mapname);
703                 bprint(".waypoints\n");
704         }
705         else
706         {
707                 bprint("waypoint save to ");
708                 bprint(filename);
709                 bprint(" failed\n");
710         }
711 };
712
713 // load waypoints from file
714 float() waypoint_loadall =
715 {
716         local string filename, s;
717         local float file, cwp, cwb, fl;
718         local vector m1, m2;
719         cwp = 0;
720         cwb = 0;
721         filename = strcat("maps/", mapname);
722         filename = strcat(filename, ".waypoints");
723         file = fopen(filename, FILE_READ);
724         if (file >= 0)
725         {
726                 while (1)
727                 {
728                         s = fgets(file);
729                         if (!s)
730                                 break;
731                         m1 = stov(s);
732                         s = fgets(file);
733                         if (!s)
734                                 break;
735                         m2 = stov(s);
736                         s = fgets(file);
737                         if (!s)
738                                 break;
739                         fl = stof(s);
740                         waypoint_spawn(m1, m2, fl);
741                         if (m1 == m2)
742                                 cwp = cwp + 1;
743                         else
744                                 cwb = cwb + 1;
745                 }
746                 fclose(file);
747                 dprint("loaded ");
748                 dprint(ftos(cwp));
749                 dprint(" waypoints and ");
750                 dprint(ftos(cwb));
751                 dprint(" wayboxes from maps/");
752                 dprint(mapname);
753                 dprint(".waypoints\n");
754         }
755         else
756         {
757                 dprint("waypoint load from ");
758                 dprint(filename);
759                 dprint(" failed\n");
760         }
761         return cwp + cwb;
762 };
763
764 void(entity e) waypoint_spawnforitem =
765 {
766         local entity w;
767         local vector org;
768
769         if(!bot_waypoints_for_items)
770                 return;
771
772         org = e.origin + (e.mins + e.maxs) * 0.5;
773         org_z = e.origin_z + e.mins_z - PL_MIN_z + 1;
774         e.nearestwaypointtimeout = time + 1000000000;
775         // don't spawn an item waypoint if it already exists
776         w = findchain(classname, "waypoint");
777         while (w)
778         {
779                 if (vlen(w.size) > 1)
780                 {
781                         if (boxesoverlap(org, org, w.absmin, w.absmax))
782                         {
783                                 e.nearestwaypoint = w;
784                                 return;
785                         }
786                 }
787                 else
788                 {
789                         if (vlen(w.origin - org) < 16)
790                         {
791                                 e.nearestwaypoint = w;
792                                 return;
793                         }
794                 }
795                 w = w.chain;
796         }
797         e.nearestwaypoint = waypoint_spawn(org, org, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_ITEM);
798 };
799
800 void(entity e, vector destination, float timetaken) waypoint_spawnforteleporter =
801 {
802         local entity w;
803         local entity dw;
804         w = waypoint_spawn(e.absmin, e.absmax, WAYPOINTFLAG_GENERATED | WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_NORELINK);
805         dw = waypoint_spawn(destination, destination, WAYPOINTFLAG_GENERATED);
806         // one way link to the destination
807         w.wp00 = dw;
808         w.wp00mincost = timetaken; // this is just for jump pads
809         // the teleporter's nearest waypoint is this one
810         // (teleporters are not goals, so this is probably useless)
811         e.nearestwaypoint = w;
812         e.nearestwaypointtimeout = time + 1000000000;
813 };
814
815
816
817
818
819 /////////////////////////////////////////////////////////////////////////////
820 // goal stack
821 /////////////////////////////////////////////////////////////////////////////
822
823 // completely empty the goal stack, used when deciding where to go
824 void() navigation_clearroute =
825 {
826         self.goalcurrent = world;
827         self.goalstack01 = world;
828         self.goalstack02 = world;
829         self.goalstack03 = world;
830         self.goalstack04 = world;
831         self.goalstack05 = world;
832         self.goalstack06 = world;
833         self.goalstack07 = world;
834         self.goalstack08 = world;
835         self.goalstack09 = world;
836         self.goalstack10 = world;
837         self.goalstack11 = world;
838         self.goalstack12 = world;
839         self.goalstack13 = world;
840         self.goalstack14 = world;
841         self.goalstack15 = world;
842         self.goalstack16 = world;
843         self.goalstack17 = world;
844         self.goalstack18 = world;
845         self.goalstack19 = world;
846         self.goalstack20 = world;
847         self.goalstack21 = world;
848         self.goalstack22 = world;
849         self.goalstack23 = world;
850         self.goalstack24 = world;
851         self.goalstack25 = world;
852         self.goalstack26 = world;
853         self.goalstack27 = world;
854         self.goalstack28 = world;
855         self.goalstack29 = world;
856         self.goalstack30 = world;
857         self.goalstack31 = world;
858 };
859
860 // add a new goal at the beginning of the stack
861 // (in other words: add a new prerequisite before going to the later goals)
862 void(entity e) navigation_pushroute =
863 {
864         self.goalstack31 = self.goalstack30;
865         self.goalstack30 = self.goalstack29;
866         self.goalstack29 = self.goalstack28;
867         self.goalstack28 = self.goalstack27;
868         self.goalstack27 = self.goalstack26;
869         self.goalstack26 = self.goalstack25;
870         self.goalstack25 = self.goalstack24;
871         self.goalstack24 = self.goalstack23;
872         self.goalstack23 = self.goalstack22;
873         self.goalstack22 = self.goalstack21;
874         self.goalstack21 = self.goalstack20;
875         self.goalstack20 = self.goalstack19;
876         self.goalstack19 = self.goalstack18;
877         self.goalstack18 = self.goalstack17;
878         self.goalstack17 = self.goalstack16;
879         self.goalstack16 = self.goalstack15;
880         self.goalstack15 = self.goalstack14;
881         self.goalstack14 = self.goalstack13;
882         self.goalstack13 = self.goalstack12;
883         self.goalstack12 = self.goalstack11;
884         self.goalstack11 = self.goalstack10;
885         self.goalstack10 = self.goalstack09;
886         self.goalstack09 = self.goalstack08;
887         self.goalstack08 = self.goalstack07;
888         self.goalstack07 = self.goalstack06;
889         self.goalstack06 = self.goalstack05;
890         self.goalstack05 = self.goalstack04;
891         self.goalstack04 = self.goalstack03;
892         self.goalstack03 = self.goalstack02;
893         self.goalstack02 = self.goalstack01;
894         self.goalstack01 = self.goalcurrent;
895         self.goalcurrent = e;
896 };
897
898 // remove first goal from stack
899 // (in other words: remove a prerequisite for reaching the later goals)
900 // (used when a waypoint is reached)
901 void() navigation_poproute =
902 {
903         self.goalcurrent = self.goalstack01;
904         self.goalstack01 = self.goalstack02;
905         self.goalstack02 = self.goalstack03;
906         self.goalstack03 = self.goalstack04;
907         self.goalstack04 = self.goalstack05;
908         self.goalstack05 = self.goalstack06;
909         self.goalstack06 = self.goalstack07;
910         self.goalstack07 = self.goalstack08;
911         self.goalstack08 = self.goalstack09;
912         self.goalstack09 = self.goalstack10;
913         self.goalstack10 = self.goalstack11;
914         self.goalstack11 = self.goalstack12;
915         self.goalstack12 = self.goalstack13;
916         self.goalstack13 = self.goalstack14;
917         self.goalstack14 = self.goalstack15;
918         self.goalstack15 = self.goalstack16;
919         self.goalstack16 = self.goalstack17;
920         self.goalstack17 = self.goalstack18;
921         self.goalstack18 = self.goalstack19;
922         self.goalstack19 = self.goalstack20;
923         self.goalstack20 = self.goalstack21;
924         self.goalstack21 = self.goalstack22;
925         self.goalstack22 = self.goalstack23;
926         self.goalstack23 = self.goalstack24;
927         self.goalstack24 = self.goalstack25;
928         self.goalstack25 = self.goalstack26;
929         self.goalstack26 = self.goalstack27;
930         self.goalstack27 = self.goalstack28;
931         self.goalstack28 = self.goalstack29;
932         self.goalstack29 = self.goalstack30;
933         self.goalstack30 = self.goalstack31;
934         self.goalstack31 = world;
935 };
936
937 // find the waypoint near a dynamic goal such as a dropped weapon
938 entity(entity player, float walkfromwp) navigation_findnearestwaypoint =
939 {
940         local entity waylist, w, best;
941         local float dist, bestdist;
942         local vector v, org;
943         org = player.origin + (player.mins_z - PL_MIN_z) * '0 0 1';
944         if (navigation_testtracewalk)
945                 te_plasmaburn(org);
946         best = world;
947         bestdist = 1000;
948         waylist = findchain(classname, "waypoint");
949         // do two scans, because box test is cheaper
950         w = waylist;
951         while (w)
952         {
953                 // if object is touching waypoint
954                 if (boxesoverlap(org, org, w.absmin, w.absmax))
955                         return w;
956                 w = w.chain;
957         }
958         // box check failed, try walk
959         w = waylist;
960         while (w)
961         {
962                 // if object can walk from waypoint
963                 if (w.wpisbox)
964                 {
965                         local vector wm1, wm2;
966                         wm1 = w.origin + w.mins;
967                         wm2 = w.origin + w.maxs;
968                         v_x = bound(wm1_x, org_x, wm2_x);
969                         v_y = bound(wm1_y, org_y, wm2_y);
970                         v_z = bound(wm1_z, org_z, wm2_z);
971                 }
972                 else
973                         v = w.origin;
974                 dist = vlen(v - org);
975                 if (bestdist > dist)
976                 {
977                         if (walkfromwp)
978                         {
979                                 traceline(v, org, TRUE, player);
980                                 if (trace_fraction == 1)
981                                 if (tracewalk(player, v, PL_MIN, PL_MAX, org))
982                                 {
983                                         bestdist = dist;
984                                         best = w;
985                                 }
986                         }
987                         else
988                         {
989                                 traceline(v, org, TRUE, player);
990                                 if (trace_fraction == 1)
991                                 if (tracewalk(player, org, PL_MIN, PL_MAX, v))
992                                 {
993                                         bestdist = dist;
994                                         best = w;
995                                 }
996                         }
997                 }
998                 w = w.chain;
999         }
1000         return best;
1001 }
1002
1003 // finds the waypoints near the bot initiating a navigation query
1004 void() navigation_markroutes_nearestwaypoints =
1005 {
1006         local entity head;
1007         local float dist;
1008         local vector v, m1, m2;
1009         //navigation_testtracewalk = TRUE;
1010         head = findchain(classname, "waypoint");
1011         while (head)
1012         {
1013                 m1 = head.origin + head.mins;
1014                 m2 = head.origin + head.maxs;
1015                 v = self.origin;
1016                 v_x = bound(m1_x, v_x, m2_x);
1017                 v_y = bound(m1_y, v_y, m2_y);
1018                 v_z = bound(m1_z, v_z, m2_z);
1019                 dist = vlen(v - self.origin);
1020                 if (dist < 1000)
1021                 {
1022                         if (dist < 4)
1023                         {
1024                                 // if bot is very near a waypoint don't bother doing a trace
1025                                 head.wpnearestpoint = v;
1026                                 head.wpcost = dist + head.dmg;
1027                                 head.wpfire = 1;
1028                                 head.enemy = world;
1029                         }
1030                         else
1031                         {
1032                                 tracebox(self.origin, '-16 -16 0', '16 16 0', v, TRUE, self);
1033                                 if (trace_fraction == 1)
1034                                 {
1035                                         if (tracewalk(self, self.origin, self.mins, self.maxs, v))
1036                                         {
1037                                                 head.wpnearestpoint = v;
1038                                                 head.wpcost = dist + head.dmg;
1039                                                 head.wpfire = 1;
1040                                                 head.enemy = world;
1041                                         }
1042                                 }
1043                         }
1044                 }
1045                 head = head.chain;
1046         }
1047         //navigation_testtracewalk = FALSE;
1048 }
1049
1050 // updates a path link if a waypoint link is better than the current one
1051 void(entity w, entity wp, float cost2, vector p) navigation_markroutes_checkwaypoint =
1052 {
1053         local vector m1;
1054         local vector m2;
1055         local vector v;
1056         if (wp.wpisbox)
1057         {
1058                 m1 = wp.absmin;
1059                 m2 = wp.absmax;
1060                 v_x = bound(m1_x, p_x, m2_x);
1061                 v_y = bound(m1_y, p_y, m2_y);
1062                 v_z = bound(m1_z, p_z, m2_z);
1063         }
1064         else
1065                 v = wp.origin;
1066         cost2 = cost2 + vlen(v);
1067         if (wp.wpcost > cost2)
1068         {
1069                 wp.wpcost = cost2;
1070                 wp.enemy = w;
1071                 wp.wpfire = 1;
1072                 wp.wpnearestpoint = v;
1073         }
1074 };
1075
1076 // queries the entire waypoint network for pathes leading away from the bot
1077 void() navigation_markroutes =
1078 {
1079         local entity w, wp, waylist;
1080         local float searching, cost, cost2;
1081         local vector p;
1082         w = waylist = findchain(classname, "waypoint");
1083         while (w)
1084         {
1085                 w.wpnearestpoint = '0 0 0';
1086                 w.wpcost = 10000000;
1087                 w.wpfire = 0;
1088                 w.enemy = world;
1089                 w = w.chain;
1090         }
1091         navigation_markroutes_nearestwaypoints();
1092         searching = TRUE;
1093         while (searching)
1094         {
1095                 searching = FALSE;
1096                 w = waylist;
1097                 while (w)
1098                 {
1099                         if (w.wpfire)
1100                         {
1101                                 searching = TRUE;
1102                                 w.wpfire = 0;
1103                                 cost = w.wpcost;
1104                                 p = w.wpnearestpoint;
1105                                 wp = w.wp00;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp00mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1106                                 wp = w.wp01;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp01mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1107                                 wp = w.wp02;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp02mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1108                                 wp = w.wp03;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp03mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1109                                 wp = w.wp04;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp04mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1110                                 wp = w.wp05;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp05mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1111                                 wp = w.wp06;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp06mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1112                                 wp = w.wp07;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp07mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1113                                 wp = w.wp08;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp08mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1114                                 wp = w.wp09;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp09mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1115                                 wp = w.wp10;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp10mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1116                                 wp = w.wp11;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp11mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1117                                 wp = w.wp12;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp12mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1118                                 wp = w.wp13;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp13mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1119                                 wp = w.wp14;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp14mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1120                                 wp = w.wp15;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp15mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1121                                 wp = w.wp16;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp16mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1122                                 wp = w.wp17;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp17mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1123                                 wp = w.wp18;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp18mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1124                                 wp = w.wp19;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp19mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1125                                 wp = w.wp20;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp20mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1126                                 wp = w.wp21;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp21mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1127                                 wp = w.wp22;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp22mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1128                                 wp = w.wp23;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp23mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1129                                 wp = w.wp24;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp24mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1130                                 wp = w.wp25;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp25mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1131                                 wp = w.wp26;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp26mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1132                                 wp = w.wp27;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp27mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1133                                 wp = w.wp28;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp28mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1134                                 wp = w.wp29;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp29mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1135                                 wp = w.wp30;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp30mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1136                                 wp = w.wp31;if (wp){cost2 = cost + wp.dmg;if (wp.wpcost > cost2 + wp.wp31mincost) navigation_markroutes_checkwaypoint(w, wp, cost2, p);
1137                                 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
1138                         }
1139                         w = w.chain;
1140                 }
1141         }
1142 };
1143
1144 // updates the best goal according to a weighted calculation of travel cost and item value of a new proposed item
1145 void(entity e, float f) navigation_routerating =
1146 {
1147         if (!e)
1148                 return;
1149         //te_wizspike(e.origin);
1150         //bprint(etos(e));
1151         //bprint("\n");
1152         // update the cached waypoint link on a dynamic item entity
1153         if (time > e.nearestwaypointtimeout)
1154         {
1155                 e.nearestwaypoint = navigation_findnearestwaypoint(e, TRUE);
1156                 e.nearestwaypointtimeout = time + random() * 3 + 5;
1157         }
1158         if (e.nearestwaypoint)
1159         if (e.nearestwaypoint.wpcost < 10000000)
1160         {
1161                 //te_wizspike(e.nearestwaypoint.wpnearestpoint);
1162                 f = f / ((e.nearestwaypoint.wpcost + vlen(e.origin - e.nearestwaypoint.wpnearestpoint)) * 0.001 + 1);
1163                 if (navigation_bestrating < f)
1164                 {
1165                         navigation_bestrating = f;
1166                         navigation_bestgoal = e;
1167                 }
1168         }
1169 };
1170
1171 // replaces the goal stack with the path to a given item
1172 void(entity e) navigation_routetogoal =
1173 {
1174         // if already going to this goal, don't stop
1175         //if (self.goalentity == e)
1176         //      return;
1177         // clear the route and add the new one
1178         navigation_clearroute();
1179         self.goalentity = e;
1180         // if there is no goal, just exit
1181         if (!e)
1182                 return;
1183         // put the entity on the goal stack as the only item
1184         navigation_pushroute(e);
1185         //te_smallflash((e.absmin + e.absmax) * 0.5);
1186         //bprint("navigation_routetogoal(");
1187         //bprint(etos(e));
1188         //bprint(") : ");
1189         //bprint(etos(e));
1190         tracebox(self.origin, '-16 -16 0', '16 16 0', e.origin, TRUE, self);
1191         if (trace_fraction == 1)
1192         if (tracewalk(self, self.origin, PL_MIN, PL_MAX, e.origin))
1193                 return;
1194         // see if there are waypoints describing a path to the item
1195         e = e.nearestwaypoint;
1196         while (e != world)
1197         {
1198                 //bprint(" ");
1199                 //bprint(etos(e));
1200                 //te_smallflash((e.absmin + e.absmax) * 0.5);
1201                 //tracebox(self.origin, '-16 -16 0', '16 16 0', e.origin, TRUE, self);
1202                 //if (trace_fraction == 1)
1203                 //if (tracewalk(self, self.origin, PL_MIN, PL_MAX, e.origin))
1204                 //      break;
1205                 // add the waypoint to the path
1206                 navigation_pushroute(e);
1207                 e = e.enemy;
1208         }
1209         //bprint("\n");
1210 };
1211
1212 // removes any currently touching waypoints from the goal stack
1213 // (this is how bots detect if they have reached a goal)
1214 void() navigation_poptouchedgoals =
1215 {
1216         local vector org, m1, m2;
1217         org = self.origin;// + self.velocity * 0.1;
1218         m1 = org + self.mins;
1219         m2 = org + self.maxs;
1220         while (self.goalcurrent && boxesoverlap(m1, m2, self.goalcurrent.absmin, self.goalcurrent.absmax))
1221                 navigation_poproute();
1222 }
1223
1224 // begin a goal selection session (queries waypoint network)
1225 void() navigation_goalrating_start =
1226 {
1227         navigation_bestrating = -1;
1228         navigation_bestgoal = world;
1229         navigation_markroutes();
1230 };
1231
1232 // ends a goal selection session (updates goal stack to the best goal)
1233 void() navigation_goalrating_end =
1234 {
1235         navigation_routetogoal(navigation_bestgoal);
1236 };
1237
1238
1239
1240
1241
1242 //////////////////////////////////////////////////////////////////////////////
1243 // general bot functions
1244 //////////////////////////////////////////////////////////////////////////////
1245
1246 .float isbot; // true if this client is actually a bot
1247
1248 float skill;
1249 entity bot_list;
1250 .entity nextbot;
1251
1252 float sv_maxspeed;
1253 .float createdtime;
1254
1255 .float bot_attack;
1256 .float bot_dodge;
1257 .float bot_dodgerating;
1258
1259 //.float bot_painintensity;
1260 .float bot_firetimer;
1261 //.float bot_oldhealth;
1262 .void() bot_ai;
1263
1264 .entity bot_aimtarg;
1265 .float bot_aimlatency;
1266 .vector bot_aimselforigin;
1267 .vector bot_aimselfvelocity;
1268 .vector bot_aimtargorigin;
1269 .vector bot_aimtargvelocity;
1270
1271 .float bot_pickup;
1272 .float(entity player, entity item) bot_pickupevalfunc;
1273 .float bot_pickupbasevalue;
1274 .float bot_canfire;
1275
1276 // used for aiming currently
1277 // FIXME: make the weapon code use these and then replace the calculations here with a call to the weapon code
1278 vector shotorg;
1279 vector shotdir;
1280
1281 const float BOTSKINS = 19;
1282 const float BOTNAMES = 32;
1283 string(float r) bot_namefornumber =
1284 {
1285         if (r <  1) return "Thunderstorm";
1286         if (r <  2) return "Darkness";
1287         if (r <  3) return "Scorcher";
1288         if (r <  4) return "Paranoia";
1289         if (r <  5) return "Eureka";
1290         if (r <  6) return "Mystery";
1291         if (r <  7) return "Toxic";
1292         if (r <  8) return "Dominion";
1293         if (r <  9) return "Pegasus";
1294         if (r < 10) return "Sensible";
1295         if (r < 11) return "Gator";
1296         if (r < 12) return "Kangaroo";
1297         if (r < 13) return "Deadline";
1298         if (r < 14) return "Frosty";
1299         if (r < 15) return "Roadkill";
1300         if (r < 16) return "Death";
1301         if (r < 17) return "Panic";
1302         if (r < 18) return "Discovery";
1303         if (r < 19) return "Shadow";
1304         if (r < 20) return "Acidic";
1305         if (r < 21) return "Dominator";
1306         if (r < 22) return "Hellfire";
1307         if (r < 23) return "Necrotic";
1308         if (r < 24) return "Newbie";
1309         if (r < 25) return "Spellbinder";
1310         if (r < 26) return "Lion";
1311         if (r < 27) return "Controlled";
1312         if (r < 28) return "Airhead";
1313         if (r < 29) return "Delirium";
1314         if (r < 30) return "Resurrection";
1315         if (r < 31) return "Danger";
1316         return "Flatline";
1317 };
1318
1319 string(float r) bot_skinfornumber =
1320 {
1321              if (r <  1) {self.playermodel = "models/player/carni.zym"          ;self.playerskin = "0";return "Carni"          ;}
1322         else if (r <  2) {self.playermodel = "models/player/carni.zym"          ;self.playerskin = "1";return "Armored Carni"  ;}
1323         else if (r <  3) {self.playermodel = "models/player/crash.zym"          ;self.playerskin = "0";return "Quark"          ;}
1324         else if (r <  4) {self.playermodel = "models/player/grunt.zym"          ;self.playerskin = "0";return "Grunt"          ;}
1325         else if (r <  5) {self.playermodel = "models/player/headhunter.zym"     ;self.playerskin = "0";return "HeadHunter"     ;}
1326         else if (r <  6) {self.playermodel = "models/player/insurrectionist.zym";self.playerskin = "0";return "Insurrectionist";}
1327         else if (r <  7) {self.playermodel = "models/player/lurk.zym"           ;self.playerskin = "0";return "Lurk"           ;}
1328         else if (r <  8) {self.playermodel = "models/player/lurk.zym"           ;self.playerskin = "1";return "Reptile"        ;}
1329         else if (r <  9) {self.playermodel = "models/player/lycanthrope.zym"    ;self.playerskin = "0";return "Lycanthrope"    ;}
1330         else if (r < 10) {self.playermodel = "models/player/marine.zym"         ;self.playerskin = "0";return "Marine"         ;}
1331         else if (r < 11) {self.playermodel = "models/player/nexus.zym"          ;self.playerskin = "0";return "Nexus"          ;}
1332         else if (r < 12) {self.playermodel = "models/player/nexus.zym"          ;self.playerskin = "1";return "Mulder"         ;}
1333         else if (r < 13) {self.playermodel = "models/player/nexus.zym"          ;self.playerskin = "2";return "Xolar"          ;}
1334         else if (r < 14) {self.playermodel = "models/player/pyria.zym"          ;self.playerskin = "0";return "Pyria"          ;}
1335         else if (r < 15) {self.playermodel = "models/player/shock.zym"          ;self.playerskin = "0";return "Shock"          ;}
1336         else if (r < 16) {self.playermodel = "models/player/skadi.zym"          ;self.playerskin = "0";return "Skadi"          ;}
1337         else if (r < 17) {self.playermodel = "models/player/specop.zym"         ;self.playerskin = "0";return "Specop"         ;}
1338         else             {self.playermodel = "models/player/visitant.zym"       ;self.playerskin = "0";return "Fricka"         ;}
1339 };
1340
1341 void() bot_setnameandstuff =
1342 {
1343         local string name, prefix, suffix;
1344         local float r, b, shirt, pants;
1345
1346         prefix = cvar_string("bot_prefix");
1347         suffix = cvar_string("bot_suffix");
1348
1349         // this is really only a default, JoinBestTeam is called later
1350         pants = bound(0, floor(random() * 13), 13);
1351         //shirt = bound(0, floor(random() * 16), 15);
1352         shirt = pants;
1353         self.clientcolors = shirt * 16 + pants;
1354         self.team = pants + 1;
1355
1356         // now pick a name...
1357
1358         if (cvar("bot_usemodelnames"))
1359         {
1360                 // first see if all skins are taken
1361                 b = 0;
1362                 do
1363                 {
1364                         name = bot_skinfornumber(b);
1365                         b = b + 1;
1366                 }
1367                 while (b < BOTSKINS && find(world, netname, name));
1368
1369                 // randomly pick a skin, if it's taken either repeat until we find one,
1370                 // or give up if we already know all skins are taken
1371                 do
1372                 {
1373                         r = floor(random() * BOTSKINS);
1374                         name = bot_skinfornumber(r);
1375                 }
1376                 while (b < BOTSKINS && find(world, netname, name));
1377         }
1378         else
1379         {
1380                 // first see if all names are taken
1381                 b = 0;
1382                 do
1383                 {
1384                         name = bot_namefornumber(b);
1385                         b = b + 1;
1386                 }
1387                 while (b < BOTNAMES && find(world, netname, name));
1388
1389                 // randomly pick a name, if it's taken either repeat until we find one,
1390                 // or give up if we already know all names are taken
1391                 do
1392                 {
1393                         r = floor(random() * BOTNAMES);
1394                         name = bot_namefornumber(r);
1395                 }
1396                 while (b < BOTNAMES && find(world, netname, name));
1397
1398                 // randomly pick a skin
1399                 bot_skinfornumber(floor(random() * BOTSKINS));
1400         }
1401         if(!cvar("g_campaign"))
1402                 self.netname = strzone(strcat(prefix, name, suffix));
1403         else
1404                 self.netname = name;
1405 };
1406
1407 float bot_ignore_bots; // let bots not attack other bots (only works in non-teamplay)
1408 float(entity e) bot_shouldattack =
1409 {
1410         if (e.team == self.team)
1411         {
1412                 if (e == self)
1413                         return FALSE;
1414                 if (teamplay)
1415                 if (e.team != 0)
1416                         return FALSE;
1417         }
1418         if(!teamplay)
1419                 if(bot_ignore_bots)
1420                         if(clienttype(e) == CLIENTTYPE_BOT)
1421                                 return FALSE;
1422         if (!e.takedamage)
1423                 return FALSE;
1424         if (e.deadflag)
1425                 return FALSE;
1426         if (e.buttonchat)
1427                 return FALSE;
1428         return TRUE;
1429 };
1430
1431 void(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4) bot_lagfunc =
1432 {
1433         self.bot_aimtarg = e1;
1434         self.bot_aimlatency = self.ping; // FIXME?  Shouldn't this be in the lag item?
1435         self.bot_aimselforigin = v1;
1436         self.bot_aimselfvelocity = v2;
1437         self.bot_aimtargorigin = v3;
1438         self.bot_aimtargvelocity = v4;
1439         if(skill <= 0)
1440                 self.bot_canfire = (random() < 0.8);
1441         else if(skill <= 1)
1442                 self.bot_canfire = (random() < 0.9);
1443         else if(skill <= 2)
1444                 self.bot_canfire = (random() < 0.95);
1445         else
1446                 self.bot_canfire = 1;
1447 };
1448
1449 .float bot_badaimtime;
1450 .float bot_aimthinktime;
1451 .vector bot_mouseaim;
1452 .vector bot_badaimoffset;
1453 .vector bot_1st_order_aimfilter;
1454 .vector bot_2nd_order_aimfilter;
1455 .vector bot_3th_order_aimfilter;
1456 .vector bot_4th_order_aimfilter;
1457 .vector bot_5th_order_aimfilter;
1458 .vector bot_olddesiredang;
1459 float(vector v, float maxfiredeviation) bot_aimdir =
1460 {
1461 /*
1462         local float snapcos;
1463         local vector olddir, newdir, desireddir;
1464
1465         makevectors(self.v_angle);
1466         olddir = newdir = v_forward;
1467         desireddir = normalize(v);
1468         snapcos = cos(cvar("bot_ai_aimskill_snapangle") * frametime * (skill + 1) * 0.5);
1469         if (desireddir * olddir < v_forward_x)
1470                 newdir = desireddir;
1471         else
1472         {
1473                 local float blendrate;
1474                 blendrate = cvar("bot_ai_aimskill_blendrate") * frametime * (skill + 1) * 0.5;
1475                 newdir = olddir + (desireddir - olddir) * bound(0, blendrate, 1);
1476         }
1477
1478         // decide whether to fire this time
1479         if ((desireddir * newdir) >= cos(maxfiredeviation))
1480                 self.bot_firetimer = time + 0.3;
1481
1482         self.v_angle = vectoangles(newdir);
1483         self.v_angle_x = self.v_angle_x * -1;
1484
1485         makevectors(self.v_angle);
1486         shotorg = self.origin + self.view_ofs;
1487         shotdir = newdir;
1488
1489         return time < self.bot_firetimer;
1490 */
1491
1492         local float dist;
1493         local vector desiredang, diffang;
1494
1495         //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
1496         // make sure v_angle is sane first
1497         //self.v_angle_x = bound(-180, self.v_angle_x, 180);
1498         //while (self.v_angle_x < -180) self.v_angle_x = self.v_angle_x + 360;
1499         //while (self.v_angle_x >= 180) self.v_angle_x = self.v_angle_x - 360;
1500         while (self.v_angle_y < -180) self.v_angle_y = self.v_angle_y + 360;
1501         while (self.v_angle_y >= 180) self.v_angle_y = self.v_angle_y - 360;
1502         //while (self.v_angle_z < -180) self.v_angle_z = self.v_angle_z + 360;
1503         //while (self.v_angle_z >= 180) self.v_angle_z = self.v_angle_z - 360;
1504         self.v_angle_z = 0;
1505
1506         // get the desired angles to aim at
1507         //dprint(" at:", vtos(v));
1508         v = normalize(v);
1509         //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
1510         if (time >= self.bot_badaimtime)
1511         {
1512                 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
1513                 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5);
1514         }
1515         desiredang = vectoangles(v) + self.bot_badaimoffset*cvar("bot_ai_aimskill_offset");
1516         //dprint(" desired:", vtos(desiredang));
1517         if (desiredang_x >= 180)
1518                 desiredang_x = desiredang_x - 360;
1519         desiredang_x = 0 - desiredang_x;
1520         desiredang_z = self.v_angle_z;
1521         desiredang_x = bound(-90, desiredang_x, 90);
1522         //dprint(" / ", vtos(desiredang));
1523
1524         //// pain throws off aim
1525         //if (self.bot_painintensity)
1526         //{
1527         //      // shake from pain
1528         //      desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
1529         //}
1530
1531         // calculate turn angles
1532         diffang = (desiredang - self.bot_olddesiredang)*(1/frametime);
1533         self.bot_olddesiredang = desiredang;
1534         //dprint(" diff:", vtos(diffang));
1535
1536         // wrap yaw turn
1537         while (diffang_y < -180)
1538                 diffang_y = diffang_y + 360;
1539         while (diffang_y >  180)
1540                 diffang_y = diffang_y - 360;
1541
1542         // Here we will try to anticipate the comming aiming direction
1543         self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
1544                 + (diffang                      - self.bot_1st_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_1st"),1);
1545         self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
1546                 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_2nd"),1);
1547         self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
1548                 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_3th"),1);
1549         self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
1550                 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_4th"),1);
1551         self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
1552                 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, cvar("bot_ai_aimskill_order_filter_5th"),1);
1553
1554         local float blend;
1555         //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
1556         blend = bound(0,skill,10)*0.1;
1557         desiredang = desiredang + blend *
1558         (
1559                   self.bot_1st_order_aimfilter * cvar("bot_ai_aimskill_order_mix_1st")
1560                 + self.bot_2nd_order_aimfilter * cvar("bot_ai_aimskill_order_mix_2nd")
1561                 + self.bot_3th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_3th")
1562                 + self.bot_4th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_4th")
1563                 + self.bot_5th_order_aimfilter * cvar("bot_ai_aimskill_order_mix_5th")
1564         );
1565         while (desiredang_z < -180)
1566                 desiredang_z = desiredang_z + 360;
1567         while (desiredang_z >  180)
1568                 desiredang_z = desiredang_z - 360;
1569         // calculate turn angles
1570         diffang = desiredang - self.bot_mouseaim;
1571         //dprint(" diff:", vtos(diffang));
1572
1573         // wrap yaw turn
1574         while (diffang_y < -180)
1575                 diffang_y = diffang_y + 360;
1576         while (diffang_y >  180)
1577                 diffang_y = diffang_y - 360;
1578
1579         if (time >= self.bot_aimthinktime)
1580         {
1581                 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
1582                 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-skill,10));
1583         }
1584
1585         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
1586
1587         desiredang = self.bot_mouseaim*bound(0,cvar("bot_ai_aimskill_think"),1) + desiredang * bound(0,(1-cvar("bot_ai_aimskill_think")),1);
1588
1589         // calculate turn angles
1590         diffang = desiredang - self.v_angle;
1591         //dprint(" diff:", vtos(diffang));
1592
1593         // wrap yaw turn
1594         while (diffang_y < -180)
1595                 diffang_y = diffang_y + 360;
1596         while (diffang_y >  180)
1597                 diffang_y = diffang_y - 360;
1598         // calculate turn angles
1599         diffang = desiredang - self.v_angle;
1600         //dprint(" diff:", vtos(diffang));
1601
1602         // wrap yaw turn
1603         while (diffang_y < -180)
1604                 diffang_y = diffang_y + 360;
1605         while (diffang_y >  180)
1606                 diffang_y = diffang_y - 360;
1607
1608         // jitter tracking
1609         dist = vlen(diffang);
1610         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
1611
1612         // turn
1613         local float r, fixedrate, blendrate;
1614         fixedrate = cvar("bot_ai_aimskill_fixedrate") / bound(1,dist,1000);
1615         blendrate = cvar("bot_ai_aimskill_blendrate");
1616         r = max(fixedrate, blendrate);
1617         //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
1618         self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
1619         self.v_angle = self.v_angle * bound(0,cvar("bot_ai_aimskill_mouse"),1) + desiredang * bound(0,(1-cvar("bot_ai_aimskill_mouse")),1);
1620         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
1621         //self.v_angle = self.v_angle + diffang * (1/ blendrate);
1622         self.v_angle_z = 0;
1623         while (self.v_angle_y < -180)
1624                 self.v_angle_y = self.v_angle_y + 360;
1625         while (self.v_angle_y >= 180)
1626                 self.v_angle_y = self.v_angle_y - 360;
1627         //dprint(" turn:", vtos(self.v_angle));
1628
1629         makevectors(self.v_angle);
1630         shotorg = self.origin + self.view_ofs;
1631         shotdir = v_forward;
1632
1633         //dprint(" dir:", vtos(v_forward));
1634         //te_lightning2(world, shotorg, shotorg + shotdir * 100);
1635
1636         // calculate turn angles again
1637         diffang = desiredang - self.v_angle;
1638         while (diffang_y < -180)
1639                 diffang_y = diffang_y + 360;
1640         while (diffang_y >  180)
1641                 diffang_y = diffang_y - 360;
1642
1643         //bprint("e ");bprintvector(diffang);bprint(" < ");bprintfloat(maxfiredeviation);bprint("\n");
1644
1645         // decide whether to fire this time
1646         // note the maxfiredeviation is in degrees so this has to convert to radians first
1647         //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
1648         if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
1649                         self.bot_firetimer = time + 0.3/bound(1,skill*0.3,3);
1650         traceline(shotorg,shotorg+shotdir*1000,FALSE,world);
1651         if ( vlen(trace_endpos-shotorg)>bound(0,skill*20,20) && random()<bound(0,skill*0.5,10)*frametime*2 )
1652                 self.bot_canfire = 0;
1653         //dprint(ftos(maxfiredeviation),"\n");
1654         //dprint(" diff:", vtos(diffang), "\n");
1655
1656         return self.bot_canfire && (time < self.bot_firetimer);
1657 };
1658
1659 vector(vector targorigin, vector targvelocity, float shotspeed, float shotdelay) bot_shotlead =
1660 {
1661         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
1662         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
1663 };
1664
1665 float(float shotspeed, float shotspeedupward, float maxshottime, float applygravity) bot_aim =
1666 {
1667         local float f, r;
1668         local vector v;
1669         /*
1670         eprint(self);
1671         dprint("bot_aim(", ftos(shotspeed));
1672         dprint(", ", ftos(shotspeedupward));
1673         dprint(", ", ftos(maxshottime));
1674         dprint(", ", ftos(applygravity));
1675         dprint(");\n");
1676         */
1677         if (!shotspeed)
1678         {
1679                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " shotspeed is zero!\n");
1680                 shotspeed = 1000000;
1681         }
1682         if (!maxshottime)
1683         {
1684                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " maxshottime is zero!\n");
1685                 maxshottime = 1;
1686         }
1687         makevectors(self.v_angle);
1688         shotorg = self.origin + self.view_ofs;
1689         shotdir = v_forward;
1690         v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
1691         local float distanceratio;
1692         distanceratio =sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/cvar("bot_ai_aimskill_firetolerance_distdegrees");
1693         distanceratio = bound(0,distanceratio,1);
1694         r =  (cvar("bot_ai_aimskill_firetolerance_maxdegrees")-cvar("bot_ai_aimskill_firetolerance_mindegrees"))
1695                 * (1-distanceratio) + cvar("bot_ai_aimskill_firetolerance_mindegrees");
1696         if (applygravity && self.bot_aimtarg)
1697         {
1698                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
1699                         return FALSE;
1700                 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
1701         }
1702         else
1703         {
1704                 f = bot_aimdir(v - shotorg, r);
1705                 //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");
1706                 traceline(shotorg, shotorg + shotdir * 10000, FALSE, self);
1707                 if (trace_ent.takedamage)
1708                 if (trace_fraction < 1)
1709                 if (!bot_shouldattack(trace_ent))
1710                         return FALSE;
1711                 traceline(shotorg, self.bot_aimtargorigin, FALSE, self);
1712                 if (trace_fraction < 1)
1713                 if (trace_ent != self.enemy)
1714                 if (!bot_shouldattack(trace_ent))
1715                         return FALSE;
1716         }
1717         if (r > maxshottime * shotspeed)
1718                 return FALSE;
1719         return f;
1720 };
1721
1722 // TODO: move this painintensity code to the player damage code
1723 void() bot_think =
1724 {
1725         //if (self.bot_painintensity > 0)
1726         //      self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
1727
1728         //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
1729         //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
1730
1731         if(cvar("g_campaign") && !campaign_bots_may_start)
1732         {
1733                 self.nextthink = time + 0.5;
1734                 return;
1735         }
1736
1737         if (self.fixangle)
1738         {
1739                 self.v_angle = self.angles;
1740                 self.v_angle_z = 0;
1741                 self.fixangle = FALSE;
1742         }
1743
1744         self.dmg_take = 0;
1745         self.dmg_save = 0;
1746         self.dmg_inflictor = world;
1747
1748         // calculate an aiming latency based on the skill setting
1749         // (simulated network latency + naturally delayed reflexes)
1750         //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
1751         // minimum ping 20+10 random
1752         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
1753         // skill 10 = ping 0.2 (adrenaline)
1754         // skill 0 = ping 0.7 (slightly drunk)
1755
1756         // clear buttons
1757         self.button0 = 0;
1758         self.button1 = 0;
1759         self.button2 = 0;
1760         self.button3 = 0;
1761         self.button4 = 0;
1762         self.button5 = 0;
1763         self.button6 = 0;
1764         self.button7 = 0;
1765         self.button8 = 0;
1766         self.buttonchat = 0;
1767         self.buttonuse = 0;
1768
1769         // if dead, just wait until we can respawn
1770         if (self.deadflag)
1771         {
1772                 if (self.deadflag == DEAD_DEAD)
1773                         self.button2 = 1; // press jump to respawn
1774                 return;
1775         }
1776
1777         // now call the current bot AI (havocbot for example)
1778         self.bot_ai();
1779 };
1780
1781 entity bot_strategytoken;
1782 entity player_list;
1783 .entity nextplayer;
1784 void() bot_relinkplayerlist =
1785 {
1786         local entity e;
1787         local entity prevbot;
1788         player_count = 0;
1789         currentbots = 0;
1790         player_list = e = findchainflags(flags, FL_CLIENT);
1791         bot_list = world;
1792         prevbot = world;
1793         while (e)
1794         {
1795                 player_count = player_count + 1;
1796                 e.nextplayer = e.chain;
1797                 if (clienttype(e) == CLIENTTYPE_BOT)
1798                 {
1799                         if (prevbot)
1800                                 prevbot.nextbot = e;
1801                         else
1802                                 bot_list = e;
1803                         prevbot = e;
1804                         currentbots = currentbots + 1;
1805                 }
1806                 e = e.chain;
1807         }
1808         dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
1809         bot_strategytoken = bot_list;
1810 };
1811
1812 void() havocbot_setupbot;
1813 float JoinBestTeam(entity pl, float only_return_best);
1814
1815 void() bot_clientconnect =
1816 {
1817         if (clienttype(self) != CLIENTTYPE_BOT)
1818                 return;
1819         self.lag_func = bot_lagfunc;
1820         self.isbot = TRUE;
1821         self.createdtime = self.nextthink;
1822         JoinBestTeam(self, FALSE);
1823         havocbot_setupbot();
1824         self.bot_mouseskill=random()-0.5;
1825         self.bot_thinkskill=random()-0.5;
1826         self.bot_predictionskill=random()-0.5;
1827         self.bot_offsetskill=random()-0.5;
1828 };
1829
1830 entity() bot_spawn =
1831 {
1832         local entity oldself, bot;
1833         bot = spawnclient();
1834         if (bot)
1835         {
1836                 currentbots = currentbots + 1;
1837                 oldself = self;
1838                 self = bot;
1839                 bot_setnameandstuff();
1840                 ClientConnect();
1841                 PutClientInServer();
1842                 self = oldself;
1843         }
1844         return bot;
1845 };
1846
1847 void() bot_removenewest =
1848 {
1849         local float besttime;
1850         local entity best, head;
1851         head = findchainfloat(isbot, TRUE);
1852         if (!head)
1853                 return;
1854         best = head;
1855         besttime = head.createdtime;
1856         while (head)
1857         {
1858                 if (besttime < head.createdtime)
1859                 {
1860                         besttime = head.createdtime;
1861                         best = head;
1862                 }
1863                 head = head.chain;
1864         }
1865         currentbots = currentbots - 1;
1866         dropclient(best);
1867 };
1868
1869 float botframe_waypointeditorlightningtime;
1870 void() botframe_showwaypointlinks =
1871 {
1872         local entity player, head, w;
1873         if (time < botframe_waypointeditorlightningtime)
1874                 return;
1875         botframe_waypointeditorlightningtime = time + 0.5;
1876         player = find(world, classname, "player");
1877         while (player)
1878         {
1879                 if (!player.isbot)
1880                 if (player.flags & FL_ONGROUND)
1881                 {
1882                         //navigation_testtracewalk = TRUE;
1883                         head = navigation_findnearestwaypoint(player, FALSE);
1884                         //navigation_testtracewalk = FALSE;
1885                         if (head)
1886                         {
1887                                 w = head     ;if (w) te_lightning2(world, w.origin, player.origin);
1888                                 w = head.wp00;if (w) te_lightning2(world, w.origin, head.origin);
1889                                 w = head.wp01;if (w) te_lightning2(world, w.origin, head.origin);
1890                                 w = head.wp02;if (w) te_lightning2(world, w.origin, head.origin);
1891                                 w = head.wp03;if (w) te_lightning2(world, w.origin, head.origin);
1892                                 w = head.wp04;if (w) te_lightning2(world, w.origin, head.origin);
1893                                 w = head.wp05;if (w) te_lightning2(world, w.origin, head.origin);
1894                                 w = head.wp06;if (w) te_lightning2(world, w.origin, head.origin);
1895                                 w = head.wp07;if (w) te_lightning2(world, w.origin, head.origin);
1896                                 w = head.wp08;if (w) te_lightning2(world, w.origin, head.origin);
1897                                 w = head.wp09;if (w) te_lightning2(world, w.origin, head.origin);
1898                                 w = head.wp10;if (w) te_lightning2(world, w.origin, head.origin);
1899                                 w = head.wp11;if (w) te_lightning2(world, w.origin, head.origin);
1900                                 w = head.wp12;if (w) te_lightning2(world, w.origin, head.origin);
1901                                 w = head.wp13;if (w) te_lightning2(world, w.origin, head.origin);
1902                                 w = head.wp14;if (w) te_lightning2(world, w.origin, head.origin);
1903                                 w = head.wp15;if (w) te_lightning2(world, w.origin, head.origin);
1904                                 w = head.wp16;if (w) te_lightning2(world, w.origin, head.origin);
1905                                 w = head.wp17;if (w) te_lightning2(world, w.origin, head.origin);
1906                                 w = head.wp18;if (w) te_lightning2(world, w.origin, head.origin);
1907                                 w = head.wp19;if (w) te_lightning2(world, w.origin, head.origin);
1908                                 w = head.wp20;if (w) te_lightning2(world, w.origin, head.origin);
1909                                 w = head.wp21;if (w) te_lightning2(world, w.origin, head.origin);
1910                                 w = head.wp22;if (w) te_lightning2(world, w.origin, head.origin);
1911                                 w = head.wp23;if (w) te_lightning2(world, w.origin, head.origin);
1912                                 w = head.wp24;if (w) te_lightning2(world, w.origin, head.origin);
1913                                 w = head.wp25;if (w) te_lightning2(world, w.origin, head.origin);
1914                                 w = head.wp26;if (w) te_lightning2(world, w.origin, head.origin);
1915                                 w = head.wp27;if (w) te_lightning2(world, w.origin, head.origin);
1916                                 w = head.wp28;if (w) te_lightning2(world, w.origin, head.origin);
1917                                 w = head.wp29;if (w) te_lightning2(world, w.origin, head.origin);
1918                                 w = head.wp30;if (w) te_lightning2(world, w.origin, head.origin);
1919                                 w = head.wp31;if (w) te_lightning2(world, w.origin, head.origin);
1920                         }
1921                 }
1922                 player = find(player, classname, "player");
1923         }
1924 };
1925
1926 entity botframe_dangerwaypoint;
1927 void() botframe_updatedangerousobjects =
1928 {
1929         local entity head, bot_dodgelist;
1930         local vector m1, m2, v;
1931         local float c, d, danger;
1932         c = 0;
1933         bot_dodgelist = findchainfloat(bot_dodge, TRUE);
1934         botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1935         while (botframe_dangerwaypoint != world)
1936         {
1937                 danger = 0;
1938                 m1 = botframe_dangerwaypoint.mins;
1939                 m2 = botframe_dangerwaypoint.maxs;
1940                 head = bot_dodgelist;
1941                 while (head)
1942                 {
1943                         v = head.origin;
1944                         v_x = bound(m1_x, v_x, m2_x);
1945                         v_y = bound(m1_y, v_y, m2_y);
1946                         v_z = bound(m1_z, v_z, m2_z);
1947                         d = head.bot_dodgerating - vlen(head.origin - v);
1948                         if (d > 0)
1949                         {
1950                                 traceline(head.origin, v, TRUE, world);
1951                                 if (trace_fraction == 1)
1952                                         danger = danger + d;
1953                         }
1954                         head = head.chain;
1955                 }
1956                 botframe_dangerwaypoint.dmg = danger;
1957                 c = c + 1;
1958                 if (c >= 256)
1959                         break;
1960                 botframe_dangerwaypoint = find(botframe_dangerwaypoint, classname, "waypoint");
1961         }
1962 };
1963
1964
1965 float botframe_spawnedwaypoints;
1966 void() bot_serverframe =
1967 {
1968         float realplayers, bots;
1969
1970         if (intermission_running)
1971                 return;
1972
1973         if (time < 2)
1974                 return;
1975
1976         realplayers = player_count - currentbots;
1977
1978         // add/remove bots if needed to make sure there are at least
1979         // minplayers+bot_number, or remove all bots if no one is playing
1980         // But don't remove bots immediately on level change, as the real players
1981         // usually haven't rejoined yet
1982         if (realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5))
1983         {
1984                 float realminplayers, minplayers;
1985                 realminplayers = cvar("minplayers");
1986                 minplayers = bound(0, floor(realminplayers), maxclients - 1);
1987                 if (realminplayers != minplayers)
1988                         cvar_set("minplayers", ftos(minplayers));
1989
1990                 float realminbots, minbots;
1991                 realminbots = cvar("bot_number");
1992                 minbots = bound(0, floor(realminbots), maxclients - 1);
1993                 if (realminbots != minbots)
1994                         cvar_set("bot_number", ftos(minbots));
1995
1996                 bots = bound(minbots, minplayers - realplayers, maxclients);
1997         }
1998         else
1999         {
2000                 // if there are no players, remove bots
2001                 bots = 0;
2002         }
2003
2004         bot_ignore_bots = cvar("bot_ignore_bots");
2005
2006         // only add one bot per frame to avoid utter chaos
2007         while (currentbots < bots)
2008         {
2009                 if (bot_spawn() == world)
2010                 {
2011                         bprint("Can not add bot, server full.\n");
2012                         cvar_set("bot_number", ftos(currentbots));
2013                         bots = currentbots;
2014                         break;
2015                 }
2016         }
2017         while (currentbots > bots)
2018                 bot_removenewest();
2019
2020         if(botframe_spawnedwaypoints)
2021         {
2022                 if(cvar("waypoint_benchmark"))
2023                         localcmd("quit\n");
2024         }
2025
2026         if (bots > 0 || cvar("g_waypointeditor"))
2027         if (!botframe_spawnedwaypoints)
2028         {
2029                 botframe_spawnedwaypoints = TRUE;
2030                 waypoint_loadall();
2031                 waypoint_schedulerelinkall();
2032         }
2033
2034         if (bot_list)
2035         {
2036                 // cycle the goal token from one bot to the next each frame
2037                 // (this prevents them from all doing waypoint searches on the same
2038                 //  frame, which causes choppy framerates)
2039                 if (bot_strategytoken)
2040                         bot_strategytoken = bot_strategytoken.nextbot;
2041                 if (!bot_strategytoken)
2042                         bot_strategytoken = bot_list;
2043
2044                 botframe_updatedangerousobjects();
2045         }
2046
2047         if (cvar("g_waypointeditor"))
2048                 botframe_showwaypointlinks();
2049 };