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