]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/aim.qc
Start of uncrusticracy script, and kill trailing whitespace in all sources.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / aim.qc
1
2 entity ka_ball;
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)
6
7 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
8 {
9         float c, savesolid, shottime;
10         vector dir, end, v, o;
11         if (shotspeed < 1)
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
15         if (!tracetossent)
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);
25         v = trace_endpos;
26         end = v + (targ.mins + targ.maxs) * 0.5;
27         if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
28         {
29                 // out of range
30                 targ.solid = savesolid;
31                 return FALSE;
32         }
33
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);
43
44         c = 0;
45         dir = normalize(end - org);
46         while (c < 10) // 10 traces
47         {
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
52                 {
53                         targ.solid = savesolid;
54
55                         // make it disappear
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);
62
63                         return TRUE;
64                 }
65                 dir_z = dir_z + 0.1; // aim up a little more
66                 c = c + 1;
67         }
68         targ.solid = savesolid;
69
70         // make it disappear
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);
77
78         // leave a valid one even if it won't reach
79         findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
80         return FALSE;
81 }
82
83 void lag_update()
84 {
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;}
90 }
91
92 float lag_additem(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
93 {
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??)
100         return FALSE;
101 }
102
103 float bot_shouldattack(entity e)
104 {
105         if (e.team == self.team)
106         {
107                 if (e == self)
108                         return FALSE;
109                 if (teamplay)
110                 if (e.team != 0)
111                         return FALSE;
112         }
113
114         if(e.freezetag_frozen)
115                 return FALSE;
116
117         // If neither player has ball then don't attack unless the ball is on the
118         // ground.
119         if (g_keepaway)
120                 if (!e.ballcarried && !self.ballcarried && ka_ball.owner)
121                         return FALSE;
122
123         if(teamplay)
124         {
125                 if(e.team==0)
126                         return FALSE;
127         }
128         else if(bot_ignore_bots)
129                 if(IS_BOT_CLIENT(e))
130                         return FALSE;
131
132         if (!e.takedamage)
133                 return FALSE;
134         if (e.deadflag)
135                 return FALSE;
136         if (e.BUTTON_CHAT)
137                 return FALSE;
138         if(e.flags & FL_NOTARGET)
139                 return FALSE;
140
141         checkentity = e;
142         if(MUTATOR_CALLHOOK(BotShouldAttack))
143                 return FALSE;
144
145         return TRUE;
146 }
147
148 void bot_lagfunc(float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
149 {
150         if(self.flags & FL_INWATER)
151         {
152                 self.bot_aimtarg = world;
153                 return;
154         }
155         self.bot_aimtarg = e1;
156         self.bot_aimlatency = self.ping; // FIXME?  Shouldn't this be in the lag item?
157         self.bot_aimselforigin = v1;
158         self.bot_aimselfvelocity = v2;
159         self.bot_aimtargorigin = v3;
160         self.bot_aimtargvelocity = v4;
161         if(skill <= 0)
162                 self.bot_canfire = (random() < 0.8);
163         else if(skill <= 1)
164                 self.bot_canfire = (random() < 0.9);
165         else if(skill <= 2)
166                 self.bot_canfire = (random() < 0.95);
167         else
168                 self.bot_canfire = 1;
169 }
170
171 float bot_aimdir(vector v, float maxfiredeviation)
172 {
173         float dist, delta_t, blend;
174         vector desiredang, diffang;
175
176         //dprint("aim ", self.netname, ": old:", vtos(self.v_angle));
177         // make sure v_angle is sane first
178         self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
179         self.v_angle_z = 0;
180
181         // get the desired angles to aim at
182         //dprint(" at:", vtos(v));
183         v = normalize(v);
184         //te_lightning2(world, self.origin + self.view_ofs, self.origin + self.view_ofs + v * 200);
185         if (time >= self.bot_badaimtime)
186         {
187                 self.bot_badaimtime = max(self.bot_badaimtime + 0.3, time);
188                 self.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+self.bot_offsetskill), 5) * autocvar_bot_ai_aimskill_offset;
189         }
190         desiredang = vectoangles(v) + self.bot_badaimoffset;
191         //dprint(" desired:", vtos(desiredang));
192         if (desiredang_x >= 180)
193                 desiredang_x = desiredang_x - 360;
194         desiredang_x = bound(-90, 0 - desiredang_x, 90);
195         desiredang_z = self.v_angle_z;
196         //dprint(" / ", vtos(desiredang));
197
198         //// pain throws off aim
199         //if (self.bot_painintensity)
200         //{
201         //      // shake from pain
202         //      desiredang = desiredang + randomvec() * self.bot_painintensity * 0.2;
203         //}
204
205         // calculate turn angles
206         diffang = (desiredang - self.bot_olddesiredang);
207         // wrap yaw turn
208         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
209         if (diffang_y >= 180)
210                 diffang_y = diffang_y - 360;
211         self.bot_olddesiredang = desiredang;
212         //dprint(" diff:", vtos(diffang));
213
214         delta_t = time-self.bot_prevaimtime;
215         self.bot_prevaimtime = time;
216         // Here we will try to anticipate the comming aiming direction
217         self.bot_1st_order_aimfilter= self.bot_1st_order_aimfilter
218                 + (diffang * (1 / delta_t)    - self.bot_1st_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_1st,1);
219         self.bot_2nd_order_aimfilter= self.bot_2nd_order_aimfilter
220                 + (self.bot_1st_order_aimfilter - self.bot_2nd_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_2nd,1);
221         self.bot_3th_order_aimfilter= self.bot_3th_order_aimfilter
222                 + (self.bot_2nd_order_aimfilter - self.bot_3th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_3th,1);
223         self.bot_4th_order_aimfilter= self.bot_4th_order_aimfilter
224                 + (self.bot_3th_order_aimfilter - self.bot_4th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_4th,1);
225         self.bot_5th_order_aimfilter= self.bot_5th_order_aimfilter
226                 + (self.bot_4th_order_aimfilter - self.bot_5th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_5th,1);
227
228         //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
229         blend = bound(0,skill+self.bot_aimskill,10)*0.1;
230         desiredang = desiredang + blend *
231         (
232                   self.bot_1st_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_1st
233                 + self.bot_2nd_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_2nd
234                 + self.bot_3th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_3th
235                 + self.bot_4th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_4th
236                 + self.bot_5th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_5th
237         );
238
239         // calculate turn angles
240         diffang = desiredang - self.bot_mouseaim;
241         // wrap yaw turn
242         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
243         if (diffang_y >= 180)
244                 diffang_y = diffang_y - 360;
245         //dprint(" diff:", vtos(diffang));
246
247         if (time >= self.bot_aimthinktime)
248         {
249                 self.bot_aimthinktime = max(self.bot_aimthinktime + 0.5 - 0.05*(skill+self.bot_thinkskill), time);
250                 self.bot_mouseaim = self.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-(skill+self.bot_thinkskill),10));
251         }
252
253         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
254
255         diffang = self.bot_mouseaim - desiredang;
256         // wrap yaw turn
257         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
258         if (diffang_y >= 180)
259                 diffang_y = diffang_y - 360;
260         desiredang = desiredang + diffang * bound(0,autocvar_bot_ai_aimskill_think,1);
261
262         // calculate turn angles
263         diffang = desiredang - self.v_angle;
264         // wrap yaw turn
265         diffang_y = diffang_y - floor(diffang_y / 360) * 360;
266         if (diffang_y >= 180)
267                 diffang_y = diffang_y - 360;
268         //dprint(" diff:", vtos(diffang));
269
270         // jitter tracking
271         dist = vlen(diffang);
272         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
273
274         // turn
275         float r, fixedrate, blendrate;
276         fixedrate = autocvar_bot_ai_aimskill_fixedrate / bound(1,dist,1000);
277         blendrate = autocvar_bot_ai_aimskill_blendrate;
278         r = max(fixedrate, blendrate);
279         //self.v_angle = self.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
280         self.v_angle = self.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+self.bot_mouseskill,3)*0.005-random()), 1);
281         self.v_angle = self.v_angle * bound(0,autocvar_bot_ai_aimskill_mouse,1) + desiredang * bound(0,(1-autocvar_bot_ai_aimskill_mouse),1);
282         //self.v_angle = self.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
283         //self.v_angle = self.v_angle + diffang * (1/ blendrate);
284         self.v_angle_z = 0;
285         self.v_angle_y = self.v_angle_y - floor(self.v_angle_y / 360) * 360;
286         //dprint(" turn:", vtos(self.v_angle));
287
288         makevectors(self.v_angle);
289         shotorg = self.origin + self.view_ofs;
290         shotdir = v_forward;
291
292         //dprint(" dir:", vtos(v_forward));
293         //te_lightning2(world, shotorg, shotorg + shotdir * 100);
294
295         // calculate turn angles again
296         //diffang = desiredang - self.v_angle;
297         //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
298         //if (diffang_y >= 180)
299         //      diffang_y = diffang_y - 360;
300
301         //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
302
303         // decide whether to fire this time
304         // note the maxfiredeviation is in degrees so this has to convert to radians first
305         //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
306         if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
307         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))
308                 self.bot_firetimer = time + bound(0.1, 0.5-(skill+self.bot_aggresskill)*0.05, 0.5);
309         //traceline(shotorg,shotorg+shotdir*1000,FALSE,world);
310         //dprint(ftos(maxfiredeviation),"\n");
311         //dprint(" diff:", vtos(diffang), "\n");
312
313         return self.bot_canfire && (time < self.bot_firetimer);
314 }
315
316 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
317 {
318         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
319         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
320 }
321
322 float bot_aim(float shotspeed, float shotspeedupward, float maxshottime, float applygravity)
323 {
324         float f, r, hf, distanceratio;
325         vector v;
326         /*
327         eprint(self);
328         dprint("bot_aim(", ftos(shotspeed));
329         dprint(", ", ftos(shotspeedupward));
330         dprint(", ", ftos(maxshottime));
331         dprint(", ", ftos(applygravity));
332         dprint(");\n");
333         */
334
335         hf = self.dphitcontentsmask;
336         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
337
338         shotspeed *= g_weaponspeedfactor;
339         shotspeedupward *= g_weaponspeedfactor;
340         if (!shotspeed)
341         {
342                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " shotspeed is zero!\n");
343                 shotspeed = 1000000;
344         }
345         if (!maxshottime)
346         {
347                 dprint("bot_aim: WARNING: weapon ", W_Name(self.weapon), " maxshottime is zero!\n");
348                 maxshottime = 1;
349         }
350         makevectors(self.v_angle);
351         shotorg = self.origin + self.view_ofs;
352         shotdir = v_forward;
353         v = bot_shotlead(self.bot_aimtargorigin, self.bot_aimtargvelocity, shotspeed, self.bot_aimlatency);
354         distanceratio = sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/autocvar_bot_ai_aimskill_firetolerance_distdegrees;
355         distanceratio = bound(0,distanceratio,1);
356         r =  (autocvar_bot_ai_aimskill_firetolerance_maxdegrees-autocvar_bot_ai_aimskill_firetolerance_mindegrees)
357                 * (1-distanceratio) + autocvar_bot_ai_aimskill_firetolerance_mindegrees;
358         if (applygravity && self.bot_aimtarg)
359         {
360                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', self.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, self))
361                 {
362                         self.dphitcontentsmask = hf;
363                         return FALSE;
364                 }
365
366                 f = bot_aimdir(findtrajectory_velocity - shotspeedupward * '0 0 1', r);
367         }
368         else
369         {
370                 f = bot_aimdir(v - shotorg, r);
371                 //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");
372                 //traceline(shotorg, shotorg + shotdir * 10000, FALSE, self);
373                 //if (trace_ent.takedamage)
374                 //if (trace_fraction < 1)
375                 //if (!bot_shouldattack(trace_ent))
376                 //      return FALSE;
377                 traceline(shotorg, self.bot_aimtargorigin, FALSE, self);
378                 if (trace_fraction < 1)
379                 if (trace_ent != self.enemy)
380                 if (!bot_shouldattack(trace_ent))
381                 {
382                         self.dphitcontentsmask = hf;
383                         return FALSE;
384                 }
385         }
386
387         //if (r > maxshottime * shotspeed)
388         //      return FALSE;
389         self.dphitcontentsmask = hf;
390         return TRUE;
391 }