]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/aim.qc
96de17a747172a93202db1e09053cec518ad94df
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / aim.qc
1 #include "aim.qh"
2
3 #include <common/physics/player.qh>
4 #include <common/state.qh>
5 #include <common/stats.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/bot/default/bot.qh>
8 #include <server/bot/default/cvars.qh>
9 #include <server/mutators/_mod.qh>
10 #include <server/weapons/weaponsystem.qh>
11
12 // traces multiple trajectories to find one that will impact the target
13 // 'end' vector is the place it aims for,
14 // returns true only if it hit targ (don't target non-solid entities)
15
16 float findtrajectorywithleading(vector org, vector m1, vector m2, entity targ, float shotspeed, float shotspeedupward, float maxtime, float shotdelay, entity ignore)
17 {
18         float c, savesolid, shottime;
19         vector dir, end, v, o;
20         if (shotspeed < 1)
21                 return false; // could cause division by zero if calculated
22         if (targ.solid < SOLID_BBOX) // SOLID_NOT and SOLID_TRIGGER
23                 return false; // could never hit it
24         if (!tracetossent)
25                 tracetossent = new(tracetossent);
26         tracetossent.owner = ignore;
27         setsize(tracetossent, m1, m2);
28         savesolid = targ.solid;
29         targ.solid = SOLID_NOT;
30         o = (targ.absmin + targ.absmax) * 0.5;
31         shottime = ((vlen(o - org) / shotspeed) + shotdelay);
32         v = targ.velocity * shottime + o;
33         tracebox(o, targ.mins, targ.maxs, v, false, targ);
34         v = trace_endpos;
35         end = v + (targ.mins + targ.maxs) * 0.5;
36         if ((vlen(end - org) / shotspeed + 0.2) > maxtime)
37         {
38                 // out of range
39                 targ.solid = savesolid;
40                 return false;
41         }
42
43         if (!tracetossfaketarget)
44                 tracetossfaketarget = new(tracetossfaketarget);
45         tracetossfaketarget.solid = savesolid;
46         set_movetype(tracetossfaketarget, targ.move_movetype);
47         _setmodel(tracetossfaketarget, targ.model); // no low precision
48         tracetossfaketarget.model = targ.model;
49         tracetossfaketarget.modelindex = targ.modelindex;
50         setsize(tracetossfaketarget, targ.mins, targ.maxs);
51         setorigin(tracetossfaketarget, v);
52
53         c = 0;
54         dir = normalize(end - org);
55         while (c < 10) // 10 traces
56         {
57                 setorigin(tracetossent, org); // reset
58                 tracetossent.velocity = findtrajectory_velocity = normalize(dir) * shotspeed + shotspeedupward * '0 0 1';
59                 tracetoss(tracetossent, ignore); // love builtin functions...
60                 if (trace_ent == tracetossfaketarget) // done
61                 {
62                         targ.solid = savesolid;
63
64                         // make it disappear
65                         tracetossfaketarget.solid = SOLID_NOT;
66                         set_movetype(tracetossfaketarget, MOVETYPE_NONE);
67                         tracetossfaketarget.model = "";
68                         tracetossfaketarget.modelindex = 0;
69                         // relink to remove it from physics considerations
70                         setorigin(tracetossfaketarget, v);
71
72                         return true;
73                 }
74                 dir.z = dir.z + 0.1; // aim up a little more
75                 c = c + 1;
76         }
77         targ.solid = savesolid;
78
79         // make it disappear
80         tracetossfaketarget.solid = SOLID_NOT;
81         set_movetype(tracetossfaketarget, MOVETYPE_NONE);
82         tracetossfaketarget.model = "";
83         tracetossfaketarget.modelindex = 0;
84         // relink to remove it from physics considerations
85         setorigin(tracetossfaketarget, v);
86
87         // leave a valid one even if it won't reach
88         findtrajectory_velocity = normalize(end - org) * shotspeed + shotspeedupward * '0 0 1';
89         return false;
90 }
91
92 void lag_update(entity this)
93 {
94         if (this.lag1_time && time > this.lag1_time) { this.lag_func(this, this.lag1_time, this.lag1_float1, this.lag1_float2, this.lag1_entity1, this.lag1_vec1, this.lag1_vec2, this.lag1_vec3, this.lag1_vec4); this.lag1_time = 0; }
95         if (this.lag2_time && time > this.lag2_time) { this.lag_func(this, this.lag2_time, this.lag2_float1, this.lag2_float2, this.lag2_entity1, this.lag2_vec1, this.lag2_vec2, this.lag2_vec3, this.lag2_vec4); this.lag2_time = 0; }
96         if (this.lag3_time && time > this.lag3_time) { this.lag_func(this, this.lag3_time, this.lag3_float1, this.lag3_float2, this.lag3_entity1, this.lag3_vec1, this.lag3_vec2, this.lag3_vec3, this.lag3_vec4); this.lag3_time = 0; }
97         if (this.lag4_time && time > this.lag4_time) { this.lag_func(this, this.lag4_time, this.lag4_float1, this.lag4_float2, this.lag4_entity1, this.lag4_vec1, this.lag4_vec2, this.lag4_vec3, this.lag4_vec4); this.lag4_time = 0; }
98         if (this.lag5_time && time > this.lag5_time) { this.lag_func(this, this.lag5_time, this.lag5_float1, this.lag5_float2, this.lag5_entity1, this.lag5_vec1, this.lag5_vec2, this.lag5_vec3, this.lag5_vec4); this.lag5_time = 0; }
99 }
100
101 float lag_additem(entity this, float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
102 {
103         if (this.lag1_time == 0) {this.lag1_time = t;this.lag1_float1 = f1;this.lag1_float2 = f2;this.lag1_entity1 = e1;this.lag1_vec1 = v1;this.lag1_vec2 = v2;this.lag1_vec3 = v3;this.lag1_vec4 = v4;return true;}
104         if (this.lag2_time == 0) {this.lag2_time = t;this.lag2_float1 = f1;this.lag2_float2 = f2;this.lag2_entity1 = e1;this.lag2_vec1 = v1;this.lag2_vec2 = v2;this.lag2_vec3 = v3;this.lag2_vec4 = v4;return true;}
105         if (this.lag3_time == 0) {this.lag3_time = t;this.lag3_float1 = f1;this.lag3_float2 = f2;this.lag3_entity1 = e1;this.lag3_vec1 = v1;this.lag3_vec2 = v2;this.lag3_vec3 = v3;this.lag3_vec4 = v4;return true;}
106         if (this.lag4_time == 0) {this.lag4_time = t;this.lag4_float1 = f1;this.lag4_float2 = f2;this.lag4_entity1 = e1;this.lag4_vec1 = v1;this.lag4_vec2 = v2;this.lag4_vec3 = v3;this.lag4_vec4 = v4;return true;}
107         if (this.lag5_time == 0) {this.lag5_time = t;this.lag5_float1 = f1;this.lag5_float2 = f2;this.lag5_entity1 = e1;this.lag5_vec1 = v1;this.lag5_vec2 = v2;this.lag5_vec3 = v3;this.lag5_vec4 = v4;return true;}
108         // no room for it (what is the best thing to do here??)
109         return false;
110 }
111
112 bool bot_shouldattack(entity this, entity targ)
113 {
114         if (targ.team == this.team)
115         {
116                 if (targ == this)
117                         return false;
118                 if (teamplay)
119                 if (targ.team != 0)
120                         return false;
121         }
122
123         if(STAT(FROZEN, targ))
124                 return false;
125
126         if(teamplay)
127         {
128                 if(targ.team==0)
129                         return false;
130         }
131         else if (autocvar_bot_ignore_bots && IS_BOT_CLIENT(targ))
132                 return false;
133
134         if (!targ.takedamage)
135                 return false;
136         if (IS_DEAD(targ))
137                 return false;
138         if (PHYS_INPUT_BUTTON_CHAT(targ) && !autocvar_bot_typefrag)
139                 return false;
140         if(targ.flags & FL_NOTARGET)
141                 return false;
142         if(targ.alpha <= 0.1 && targ.alpha != 0)
143                 return false; // invisible via alpha
144
145         if(MUTATOR_CALLHOOK(BotShouldAttack, this, targ))
146                 return false;
147
148         return true;
149 }
150
151 void bot_lagfunc(entity this, float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
152 {
153         this.bot_aimtarg = e1;
154         this.bot_aimlatency = CS(this).ping; // FIXME?  Shouldn't this be in the lag item?
155         //this.bot_aimorigin = v1;
156         //this.bot_aimvelocity = v2;
157         this.bot_aimtargorigin = v3;
158         this.bot_aimtargvelocity = v4;
159 }
160
161 // this function should be called after bot_aim so the aim is reset the next frame
162 void bot_aim_reset(entity this)
163 {
164         this.bot_mouseaim = this.v_angle;
165         this.bot_olddesiredang = this.v_angle;
166         this.bot_aimdir_executed = true;
167         this.bot_badaimtime = 0;
168         this.bot_aimthinktime = time;
169         this.bot_prevaimtime = time;
170         this.bot_1st_order_aimfilter = '0 0 0';
171         this.bot_2nd_order_aimfilter = '0 0 0';
172         this.bot_3th_order_aimfilter = '0 0 0';
173         this.bot_4th_order_aimfilter = '0 0 0';
174         this.bot_5th_order_aimfilter = '0 0 0';
175         this.bot_firetimer = 0;
176 }
177
178 void bot_aimdir(entity this, vector v, float maxfiredeviation)
179 {
180         float dist, delta_t, blend;
181         vector desiredang, diffang;
182
183         this.bot_aimdir_executed = true;
184
185         //dprint("aim ", this.netname, ": old:", vtos(this.v_angle));
186         // make sure v_angle is sane first
187         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
188         this.v_angle_z = 0;
189
190         // make work bot_aim_reset even if called before this function
191         if (this.bot_prevaimtime == time)
192                 return;
193
194         // invalid aim dir (can happen when bot overlaps target)
195         if(!v) return;
196
197         float skill_save = skill;
198         // allow turning in a more natural way when bot is walking
199         if (!this.bot_aimtarg)
200                 skill = max(4, skill);
201
202         // get the desired angles to aim at
203         //dprint(" at:", vtos(v));
204         v = normalize(v);
205         //te_lightning2(NULL, this.origin + this.view_ofs, this.origin + this.view_ofs + v * 200);
206         if (time >= this.bot_badaimtime)
207         {
208                 this.bot_badaimtime = max(this.bot_badaimtime + 0.3, time);
209                 this.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+this.bot_offsetskill), 5) * autocvar_bot_ai_aimskill_offset;
210         }
211         desiredang = vectoangles(v) + this.bot_badaimoffset;
212         //dprint(" desired:", vtos(desiredang));
213         if (desiredang.x >= 180)
214                 desiredang.x = desiredang.x - 360;
215         desiredang.x = bound(-90, 0 - desiredang.x, 90);
216         desiredang.z = this.v_angle.z;
217         //dprint(" / ", vtos(desiredang));
218
219         //// pain throws off aim
220         //if (this.bot_painintensity)
221         //{
222         //      // shake from pain
223         //      desiredang = desiredang + randomvec() * this.bot_painintensity * 0.2;
224         //}
225
226         // calculate turn angles
227         diffang = (desiredang - this.bot_olddesiredang);
228         // wrap yaw turn
229         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
230         if (diffang.y >= 180)
231                 diffang.y = diffang.y - 360;
232         this.bot_olddesiredang = desiredang;
233         //dprint(" diff:", vtos(diffang));
234
235         delta_t = time-this.bot_prevaimtime;
236         this.bot_prevaimtime = time;
237         // Here we will try to anticipate the comming aiming direction
238         this.bot_1st_order_aimfilter= this.bot_1st_order_aimfilter
239                 + (diffang * (1 / delta_t)    - this.bot_1st_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_1st,1);
240         this.bot_2nd_order_aimfilter= this.bot_2nd_order_aimfilter
241                 + (this.bot_1st_order_aimfilter - this.bot_2nd_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_2nd,1);
242         this.bot_3th_order_aimfilter= this.bot_3th_order_aimfilter
243                 + (this.bot_2nd_order_aimfilter - this.bot_3th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_3th,1);
244         this.bot_4th_order_aimfilter= this.bot_4th_order_aimfilter
245                 + (this.bot_3th_order_aimfilter - this.bot_4th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_4th,1);
246         this.bot_5th_order_aimfilter= this.bot_5th_order_aimfilter
247                 + (this.bot_4th_order_aimfilter - this.bot_5th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_5th,1);
248
249         //blend = (bound(0,skill,10)*0.1)*((1-bound(0,skill,10)*0.05) ** 2.5)*5.656854249; //Plot formule before changing !
250         blend = bound(0,skill+this.bot_aimskill,10)*0.1;
251         desiredang = desiredang + blend *
252         (
253                   this.bot_1st_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_1st
254                 + this.bot_2nd_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_2nd
255                 + this.bot_3th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_3th
256                 + this.bot_4th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_4th
257                 + this.bot_5th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_5th
258         );
259         desiredang.x = bound(-90, desiredang.x, 90);
260
261         // calculate turn angles
262         diffang = desiredang - this.bot_mouseaim;
263         // wrap yaw turn
264         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
265         if (diffang.y >= 180)
266                 diffang.y = diffang.y - 360;
267         //dprint(" diff:", vtos(diffang));
268
269         if (time >= this.bot_aimthinktime)
270         {
271                 this.bot_aimthinktime = max(this.bot_aimthinktime + 0.5 - 0.05*(skill+this.bot_thinkskill), time);
272                 this.bot_mouseaim = this.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-(skill+this.bot_thinkskill),10));
273         }
274
275         //this.v_angle = this.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
276
277         diffang = this.bot_mouseaim - desiredang;
278         // wrap yaw turn
279         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
280         if (diffang.y >= 180)
281                 diffang.y = diffang.y - 360;
282         desiredang = desiredang + diffang * bound(0,autocvar_bot_ai_aimskill_think,1);
283
284         // calculate turn angles
285         diffang = desiredang - this.v_angle;
286         // wrap yaw turn
287         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
288         if (diffang.y >= 180)
289                 diffang.y = diffang.y - 360;
290         //dprint(" diff:", vtos(diffang));
291
292         // jitter tracking
293         dist = vlen(diffang);
294         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
295
296         // turn
297         float r, fixedrate, blendrate;
298         fixedrate = autocvar_bot_ai_aimskill_fixedrate / bound(1,dist,1000);
299         blendrate = autocvar_bot_ai_aimskill_blendrate;
300         r = max(fixedrate, blendrate);
301         //this.v_angle = this.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
302         r = bound(delta_t, r * delta_t * (2 + ((skill + this.bot_mouseskill) ** 3) * 0.005 - random()), 1);
303         this.v_angle += diffang * (r + (1 - r) * bound(0, 1 - autocvar_bot_ai_aimskill_mouse, 1));
304         this.v_angle_z = 0;
305         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
306         //dprint(" turn:", vtos(this.v_angle));
307
308         skill = skill_save;
309
310         if (maxfiredeviation <= 0)
311                 return;
312
313         if (!autocvar_bot_ai_aimskill_firetolerance)
314         {
315                 this.bot_firetimer = time + 0.2;
316                 return;
317         }
318
319         makevectors(this.v_angle);
320         shotorg = this.origin + this.view_ofs;
321         shotdir = v_forward;
322
323         // decide whether to fire this time
324         // v is the calculated trajectory, shotdir is bot view direction
325         // NOTE: checking if (v * shotdir > cos(maxfiredeviation * DEG2RAD)) would be cheaper
326         // but it gets evaluated to true even if v and shotdir have nearly opposite direction
327         vector deviation = vectoangles(v) - vectoangles(shotdir);
328         while (deviation.x < -180) deviation.x += 360; while (deviation.x > 180) deviation.x -= 360;
329         while (deviation.y < -180) deviation.y += 360; while (deviation.y > 180) deviation.y -= 360;
330         if (fabs(deviation.x) < maxfiredeviation && fabs(deviation.y) < maxfiredeviation)
331         {
332                 traceline(shotorg, shotorg + shotdir * 1000, false, NULL);
333                 if (vdist(trace_endpos - shotorg, <, 500 + 500 * bound(0, skill + this.bot_aggresskill, 10))
334                         || random() * random() > bound(0, (skill + this.bot_aggresskill) * 0.05, 1))
335                 {
336                         this.bot_firetimer = time + bound(0.1, 0.5 - (skill + this.bot_aggresskill) * 0.05, 0.5);
337                 }
338         }
339 }
340
341 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
342 {
343         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
344         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
345 }
346
347 bool bot_aim(entity this, .entity weaponentity, float shotspeed, float shotspeedupward, float maxshottime, bool applygravity, bool shot_accurate)
348 {
349         float hf, distanceratio;
350         vector v;
351         hf = this.dphitcontentsmask;
352         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
353
354         float speed_factor = W_WeaponSpeedFactor(this);
355         shotspeed *= speed_factor;
356         shotspeedupward *= speed_factor;
357         if (!shotspeed)
358         {
359                 LOG_TRACE("bot_aim: WARNING: weapon ", this.(weaponentity).m_weapon.m_name, " shotspeed is zero!");
360                 shotspeed = 1000000;
361         }
362         if (!maxshottime)
363         {
364                 LOG_TRACE("bot_aim: WARNING: weapon ", this.(weaponentity).m_weapon.m_name, " maxshottime is zero!");
365                 maxshottime = 1;
366         }
367         makevectors(this.v_angle);
368         shotorg = this.origin + this.view_ofs;
369         shotdir = v_forward;
370         v = bot_shotlead(this.bot_aimtargorigin, this.bot_aimtargvelocity, shotspeed, this.bot_aimlatency);
371
372         // this formula was created starting from empiric values of distance and max hit angle
373         // with a player as target (32 qu wide) from the center of it right in front of the bot
374         //  distance: 32  50    75   100  150  200  300  400  500
375         //   max ang: 44  24  15.1  10.5  6.5  4.9  3.1  2.3  1.8
376         float dist = max(10, vlen(v - shotorg));
377         float maxfiredeviation = 1000 / (dist - 9) - 0.35;
378
379         float f = (shot_accurate) ? 1 : 1.6;
380         f += bound(0, (10 - (skill + this.bot_aimskill)) * 0.3, 3);
381         maxfiredeviation = min(90, maxfiredeviation * f);
382
383         if (applygravity && this.bot_aimtarg)
384         {
385                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', this.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, this))
386                 {
387                         this.dphitcontentsmask = hf;
388                         return false;
389                 }
390
391                 bot_aimdir(this, findtrajectory_velocity - shotspeedupward * '0 0 1', maxfiredeviation);
392         }
393         else
394         {
395                 bot_aimdir(this, v - shotorg, maxfiredeviation);
396                 //dprint("AIM: ");dprint(vtos(this.bot_aimtargorigin));dprint(" + ");dprint(vtos(this.bot_aimtargvelocity));dprint(" * ");dprint(ftos(this.bot_aimlatency + vlen(this.bot_aimtargorigin - shotorg) / shotspeed));dprint(" = ");dprint(vtos(v));dprint(" : aimdir = ");dprint(vtos(normalize(v - shotorg)));dprint(" : ");dprint(vtos(shotdir));dprint("\n");
397                 //traceline(shotorg, shotorg + shotdir * 10000, false, this);
398                 //if (trace_ent.takedamage)
399                 //if (trace_fraction < 1)
400                 //if (!bot_shouldattack(this, trace_ent))
401                 //      return false;
402                 traceline(shotorg, this.bot_aimtargorigin, false, this);
403                 if (trace_fraction < 1)
404                 if (trace_ent != this.enemy)
405                 if (!bot_shouldattack(this, trace_ent))
406                 {
407                         this.dphitcontentsmask = hf;
408                         return false;
409                 }
410         }
411
412         if (time > this.bot_firetimer)
413         {
414                 this.dphitcontentsmask = hf;
415                 return false;
416         }
417
418         //if (r > maxshottime * shotspeed)
419         //      return false;
420         this.dphitcontentsmask = hf;
421         return true;
422 }