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