3 // traces multiple trajectories to find one that will impact the target
4 // 'end' vector is the place it aims for,
5 // returns true only if it hit targ (don't target non-solid entities)
7 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
9 float c, savesolid, shottime;
10 vector dir, end, v, o;
12 return false; // could cause division by zero if calculated
13 if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
14 return false; // could never hit it
16 tracetossent = spawn();
17 tracetossent.owner = ignore;
18 setsize(tracetossent, m1, m2);
19 savesolid = targ.solid;
20 targ.solid = SOLID_NOT;
21 o = (targ.absmin + targ.absmax) * 0.5;
22 shottime = ((vlen(o - org) / shotspeed) + shotdelay);
23 v = targ.velocity * shottime + o;
24 tracebox(o, targ.mins, targ.maxs, v, false, targ);
26 end = v + (targ.mins + targ.maxs) * 0.5;
27 if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
30 targ.solid = savesolid;
34 if (!tracetossfaketarget)
35 tracetossfaketarget = spawn();
36 tracetossfaketarget.solid = savesolid;
37 tracetossfaketarget.movetype = targ.movetype;
38 _setmodel(tracetossfaketarget, targ.model); // no low precision
39 tracetossfaketarget.model = targ.model;
40 tracetossfaketarget.modelindex = targ.modelindex;
41 setsize(tracetossfaketarget, targ.mins, targ.maxs);
42 setorigin(tracetossfaketarget, v);
45 dir = normalize(end - org);
46 while (c < 10) // 10 traces
48 setorigin(tracetossent, org); // reset
49 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
50 tracetoss(tracetossent, ignore); // love builtin functions...
51 if (trace_ent == tracetossfaketarget) // done
53 targ.solid = savesolid;
56 tracetossfaketarget.solid = SOLID_NOT;
57 tracetossfaketarget.movetype = MOVETYPE_NONE;
58 tracetossfaketarget.model = "";
59 tracetossfaketarget.modelindex = 0;
60 // relink to remove it from physics considerations
61 setorigin(tracetossfaketarget, v);
65 dir.z = dir.z + 0.1; // aim up a little more
68 targ.solid = savesolid;
71 tracetossfaketarget.solid = SOLID_NOT;
72 tracetossfaketarget.movetype = MOVETYPE_NONE;
73 tracetossfaketarget.model = "";
74 tracetossfaketarget.modelindex = 0;
75 // relink to remove it from physics considerations
76 setorigin(tracetossfaketarget, v);
78 // leave a valid one even if it won't reach
79 findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
85 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;}
86 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;}
87 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;}
88 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;}
89 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;}
92 float lag_additem(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
94 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;}
95 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;}
96 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;}
97 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;}
98 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;}
99 // no room for it (what is the best thing to do here??)
103 float bot_shouldattack(entity e)
105 if (e.team == self.team)
122 else if(bot_ignore_bots)
132 if(e.flags & FL_NOTARGET)
135 if(MUTATOR_CALLHOOK(BotShouldAttack, e))
141 void bot_lagfunc(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
143 if(self.flags & FL_INWATER)
145 self.bot_aimtarg = world;
148 self.bot_aimtarg = e1;
149 self.bot_aimlatency = self.ping; // FIXME? Shouldn't this be in the lag item?
150 self.bot_aimselforigin = v1;
151 self.bot_aimselfvelocity = v2;
152 self.bot_aimtargorigin = v3;
153 self.bot_aimtargvelocity = v4;
155 self.bot_canfire = (random() < 0.8);
157 self.bot_canfire = (random() < 0.9);
159 self.bot_canfire = (random() < 0.95);
161 self.bot_canfire = 1;
164 float bot_aimdir(vector v, float maxfiredeviation)
166 float dist, delta_t, blend;
167 vector desiredang, diffang;
169 //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
170 // make sure v_angle is sane first
171 self.v_angle_y = self.v_angle.y - floor(self.v_angle.y / 360) * 360;
174 // get the desired angles to aim at
175 //dprint(" at:", vtos(v));
177 //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
178 if (time >= self.bot_badaimtime)
180 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
181 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5) * autocvar_bot_ai_aimskill_offset;
183 desiredang = vectoangles(v) + self.bot_badaimoffset;
184 //dprint(" desired:", vtos(desiredang));
185 if (desiredang.x >= 180)
186 desiredang.x = desiredang.x - 360;
187 desiredang.x = bound(-90, 0 - desiredang.x, 90);
188 desiredang.z = self.v_angle.z;
189 //dprint(" / ", vtos(desiredang));
191 //// pain throws off aim
192 //if (self.bot_painintensity)
194 // // shake from pain
195 // desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
198 // calculate turn angles
199 diffang = (desiredang - self.bot_olddesiredang);
201 diffang.y = diffang.y - floor(diffang.y / 360) * 360;
202 if (diffang.y >= 180)
203 diffang.y = diffang.y - 360;
204 self.bot_olddesiredang = desiredang;
205 //dprint(" diff:", vtos(diffang));
207 delta_t = time-self.bot_prevaimtime;
208 self.bot_prevaimtime = time;
209 // Here we will try to anticipate the comming aiming direction
210 self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
211 + (diffang * (1 / delta_t) - self.bot_1st_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_1st,1);
212 self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
213 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_2nd,1);
214 self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
215 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_3th,1);
216 self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
217 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_4th,1);
218 self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
219 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_5th,1);
221 //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
222 blend = bound(0,skill+self.bot_aimskill,10)*0.1;
223 desiredang = desiredang + blend *
225 self.bot_1st_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_1st
226 + self.bot_2nd_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_2nd
227 + self.bot_3th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_3th
228 + self.bot_4th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_4th
229 + self.bot_5th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_5th
232 // calculate turn angles
233 diffang = desiredang - self.bot_mouseaim;
235 diffang.y = diffang.y - floor(diffang.y / 360) * 360;
236 if (diffang.y >= 180)
237 diffang.y = diffang.y - 360;
238 //dprint(" diff:", vtos(diffang));
240 if (time >= self.bot_aimthinktime)
242 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
243 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-(skill+self.bot_thinkskill),10));
246 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
248 diffang = self.bot_mouseaim - desiredang;
250 diffang.y = diffang.y - floor(diffang.y / 360) * 360;
251 if (diffang.y >= 180)
252 diffang.y = diffang.y - 360;
253 desiredang = desiredang + diffang * bound(0,autocvar_bot_ai_aimskill_think,1);
255 // calculate turn angles
256 diffang = desiredang - self.v_angle;
258 diffang.y = diffang.y - floor(diffang.y / 360) * 360;
259 if (diffang.y >= 180)
260 diffang.y = diffang.y - 360;
261 //dprint(" diff:", vtos(diffang));
264 dist = vlen(diffang);
265 //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
268 float r, fixedrate, blendrate;
269 fixedrate = autocvar_bot_ai_aimskill_fixedrate / bound(1,dist,1000);
270 blendrate = autocvar_bot_ai_aimskill_blendrate;
271 r = max(fixedrate, blendrate);
272 //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
273 self.v_angle = self.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
274 self.v_angle = self.v_angle * bound(0,autocvar_bot_ai_aimskill_mouse,1) + desiredang * bound(0,(1-autocvar_bot_ai_aimskill_mouse),1);
275 //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
276 //self.v_angle = self.v_angle + diffang * (1/ blendrate);
278 self.v_angle_y = self.v_angle.y - floor(self.v_angle.y / 360) * 360;
279 //dprint(" turn:", vtos(self.v_angle));
281 makevectors(self.v_angle);
282 shotorg = self.origin + self.view_ofs;
285 //dprint(" dir:", vtos(v_forward));
286 //te_lightning2(world, shotorg, shotorg + shotdir * 100);
288 // calculate turn angles again
289 //diffang = desiredang - self.v_angle;
290 //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
291 //if (diffang_y >= 180)
292 // diffang_y = diffang_y - 360;
294 //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
296 // decide whether to fire this time
297 // note the maxfiredeviation is in degrees so this has to convert to radians first
298 //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
299 if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
300 if (vlen(trace_endpos-shotorg) < 500+500*bound(0, skill+self.bot_aggresskill, 10) || random()*random()>bound(0,(skill+self.bot_aggresskill)*0.05,1))
301 self.bot_firetimer = time + bound(0.1, 0.5-(skill+self.bot_aggresskill)*0.05, 0.5);
302 //traceline(shotorg,shotorg+shotdir*1000,false,world);
303 //dprint(ftos(maxfiredeviation),"\n");
304 //dprint(" diff:", vtos(diffang), "\n");
306 return self.bot_canfire && (time < self.bot_firetimer);
309 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
311 // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
312 return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
315 float bot_aim(float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
317 float f, r, hf, distanceratio;
321 dprint("bot_aim(", ftos(shotspeed));
322 dprint(", ", ftos(shotspeedupward));
323 dprint(", ", ftos(maxshottime));
324 dprint(", ", ftos(applygravity));
328 hf = self.dphitcontentsmask;
329 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
331 shotspeed *= W_WeaponSpeedFactor();
332 shotspeedupward *= W_WeaponSpeedFactor();
335 LOG_TRACE("bot_aim: WARNING: weapon ", WEP_NAME(self.weapon), " shotspeed is zero!\n");
340 LOG_TRACE("bot_aim: WARNING: weapon ", WEP_NAME(self.weapon), " maxshottime is zero!\n");
343 makevectors(self.v_angle);
344 shotorg = self.origin + self.view_ofs;
346 v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
347 distanceratio = sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/autocvar_bot_ai_aimskill_firetolerance_distdegrees;
348 distanceratio = bound(0,distanceratio,1);
349 r = (autocvar_bot_ai_aimskill_firetolerance_maxdegrees-autocvar_bot_ai_aimskill_firetolerance_mindegrees)
350 * (1-distanceratio) + autocvar_bot_ai_aimskill_firetolerance_mindegrees;
351 if (applygravity && self.bot_aimtarg)
353 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
355 self.dphitcontentsmask = hf;
359 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
363 f = bot_aimdir(v - shotorg, r);
364 //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");
365 //traceline(shotorg, shotorg + shotdir * 10000, false, self);
366 //if (trace_ent.takedamage)
367 //if (trace_fraction < 1)
368 //if (!bot_shouldattack(trace_ent))
370 traceline(shotorg, self.bot_aimtargorigin, false, self);
371 if (trace_fraction < 1)
372 if (trace_ent != self.enemy)
373 if (!bot_shouldattack(trace_ent))
375 self.dphitcontentsmask = hf;
380 //if (r > maxshottime * shotspeed)
382 self.dphitcontentsmask = hf;