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