]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/aim.qc
Update default video settings
[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 bool bot_shouldattack(entity this, entity targ)
93 {
94         if (targ.team == this.team)
95         {
96                 if (targ == this)
97                         return false;
98                 if (teamplay)
99                 if (targ.team != 0)
100                         return false;
101         }
102
103         if(STAT(FROZEN, targ))
104                 return false;
105
106         if(teamplay)
107         {
108                 if(targ.team==0)
109                         return false;
110         }
111         else if (autocvar_bot_ignore_bots && IS_BOT_CLIENT(targ))
112                 return false;
113
114         if (!targ.takedamage)
115                 return false;
116         if (IS_DEAD(targ))
117                 return false;
118         if (PHYS_INPUT_BUTTON_CHAT(targ) && !autocvar_bot_typefrag)
119                 return false;
120         if(targ.flags & FL_NOTARGET)
121                 return false;
122         if(targ.alpha <= 0.1 && targ.alpha != 0)
123                 return false; // invisible via alpha
124
125         if(MUTATOR_CALLHOOK(BotShouldAttack, this, targ))
126                 return false;
127
128         return true;
129 }
130
131 // this function should be called after bot_aim so the aim is reset the next frame
132 void bot_aim_reset(entity this)
133 {
134         this.bot_mouseaim = this.v_angle;
135         this.bot_olddesiredang = this.v_angle;
136         this.bot_aimdir_executed = true;
137         this.bot_badaimtime = 0;
138         this.bot_aimthinktime = time;
139         this.bot_prevaimtime = time;
140         this.bot_1st_order_aimfilter = '0 0 0';
141         this.bot_2nd_order_aimfilter = '0 0 0';
142         this.bot_3th_order_aimfilter = '0 0 0';
143         this.bot_4th_order_aimfilter = '0 0 0';
144         this.bot_5th_order_aimfilter = '0 0 0';
145         this.bot_firetimer = 0;
146 }
147
148 void bot_aimdir(entity this, vector v, float maxfiredeviation)
149 {
150         float dist, delta_t, blend;
151         vector desiredang, diffang;
152
153         this.bot_aimdir_executed = true;
154
155         //dprint("aim ", this.netname, ": old:", vtos(this.v_angle));
156         // make sure v_angle is sane first
157         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
158         this.v_angle_z = 0;
159
160         // make work bot_aim_reset even if called before this function
161         if (this.bot_prevaimtime == time)
162                 return;
163
164         // if skill is high enough bots will not have any aim smoothing or aim errors
165         if (SUPERBOT)
166         {
167                 this.v_angle = vectoangles(normalize(v));
168
169                 this.v_angle.x *= -1;
170
171                 makevectors(this.v_angle);
172                 shotorg = this.origin + this.view_ofs;
173                 shotdir = v_forward;
174
175                 // bot will fire on the next tick
176                 this.bot_firetimer = time + 0.001;
177                 return;
178         }
179
180         // invalid aim dir (can happen when bot overlaps target)
181         if(!v) return;
182
183         float skill_save = skill;
184         // allow turning in a more natural way when bot is walking
185         if (!this.enemy)
186                 skill = max(4, skill);
187
188         // get the desired angles to aim at
189         //dprint(" at:", vtos(v));
190         //v = normalize(v);
191         //te_lightning2(NULL, this.origin + this.view_ofs, this.origin + this.view_ofs + v * 200);
192         if (time >= this.bot_badaimtime)
193         {
194                 this.bot_badaimtime = max(this.bot_badaimtime + 0.2 + 0.3 * random(), time);
195                 int f = bound(0, 1 - 0.1 * (skill + this.bot_offsetskill), 1);
196                 this.bot_badaimoffset = randomvec() * f * autocvar_bot_ai_aimskill_offset;
197                 this.bot_badaimoffset.x *= 0.7; // smaller vertical offset
198         }
199         float enemy_factor = ((this.enemy) ? 5 : 2);
200         // apply enemy_factor every frame so that the bigger offset is applied instantly when the bot aims to a new target
201         desiredang = vectoangles(v) + this.bot_badaimoffset * enemy_factor;
202         //dprint(" desired:", vtos(desiredang));
203         if (desiredang.x >= 180)
204                 desiredang.x = desiredang.x - 360;
205         desiredang.x = bound(-90, 0 - desiredang.x, 90);
206         desiredang.z = this.v_angle.z;
207         //dprint(" / ", vtos(desiredang));
208
209         //// pain throws off aim
210         //if (this.bot_painintensity)
211         //{
212         //      // shake from pain
213         //      desiredang = desiredang + randomvec() * this.bot_painintensity * 0.2;
214         //}
215
216         // calculate turn angles
217         diffang = (desiredang - this.bot_olddesiredang);
218         // wrap yaw turn
219         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
220         if (diffang.y >= 180)
221                 diffang.y = diffang.y - 360;
222         this.bot_olddesiredang = desiredang;
223         //dprint(" diff:", vtos(diffang));
224
225         delta_t = time-this.bot_prevaimtime;
226         this.bot_prevaimtime = time;
227         // Here we will try to anticipate the comming aiming direction
228         this.bot_1st_order_aimfilter= this.bot_1st_order_aimfilter
229                 + (diffang * (1 / delta_t)    - this.bot_1st_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_1st,1);
230         this.bot_2nd_order_aimfilter= this.bot_2nd_order_aimfilter
231                 + (this.bot_1st_order_aimfilter - this.bot_2nd_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_2nd,1);
232         this.bot_3th_order_aimfilter= this.bot_3th_order_aimfilter
233                 + (this.bot_2nd_order_aimfilter - this.bot_3th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_3th,1);
234         this.bot_4th_order_aimfilter= this.bot_4th_order_aimfilter
235                 + (this.bot_3th_order_aimfilter - this.bot_4th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_4th,1);
236         this.bot_5th_order_aimfilter= this.bot_5th_order_aimfilter
237                 + (this.bot_4th_order_aimfilter - this.bot_5th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_5th,1);
238
239         //blend = (bound(0,skill,10)*0.1)*((1-bound(0,skill,10)*0.05) ** 2.5)*5.656854249; //Plot formule before changing !
240         blend = bound(0,skill+this.bot_aimskill,10)*0.1;
241         desiredang = desiredang + blend *
242         (
243                   this.bot_1st_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_1st
244                 + this.bot_2nd_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_2nd
245                 + this.bot_3th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_3th
246                 + this.bot_4th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_4th
247                 + this.bot_5th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_5th
248         );
249         desiredang.x = bound(-90, desiredang.x, 90);
250
251         // calculate turn angles
252         diffang = desiredang - this.bot_mouseaim;
253         // wrap yaw turn
254         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
255         if (diffang.y >= 180)
256                 diffang.y = diffang.y - 360;
257         //dprint(" diff:", vtos(diffang));
258
259         if (time >= this.bot_aimthinktime)
260         {
261                 this.bot_aimthinktime = max(this.bot_aimthinktime + 0.5 - 0.05*(skill+this.bot_thinkskill), time);
262                 this.bot_mouseaim = this.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-(skill+this.bot_thinkskill),10));
263         }
264
265         //this.v_angle = this.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
266
267         diffang = this.bot_mouseaim - desiredang;
268         // wrap yaw turn
269         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
270         if (diffang.y >= 180)
271                 diffang.y = diffang.y - 360;
272         desiredang = desiredang + diffang * bound(0,autocvar_bot_ai_aimskill_think,1);
273
274         // calculate turn angles
275         diffang = desiredang - this.v_angle;
276         // wrap yaw turn
277         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
278         if (diffang.y >= 180)
279                 diffang.y = diffang.y - 360;
280         //dprint(" diff:", vtos(diffang));
281
282         // jitter tracking
283         dist = vlen(diffang);
284         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
285
286         // turn
287         float r, fixedrate, blendrate;
288         fixedrate = autocvar_bot_ai_aimskill_fixedrate / bound(1,dist,1000);
289         blendrate = autocvar_bot_ai_aimskill_blendrate;
290         r = max(fixedrate, blendrate);
291         //this.v_angle = this.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
292         r = bound(delta_t, r * delta_t * (2 + ((skill + this.bot_mouseskill) ** 3) * 0.005 - random()), 1);
293         this.v_angle += diffang * (r + (1 - r) * bound(0, 1 - autocvar_bot_ai_aimskill_mouse, 1));
294         this.v_angle_z = 0;
295         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
296         //dprint(" turn:", vtos(this.v_angle));
297
298         skill = skill_save;
299
300         if (maxfiredeviation <= 0)
301                 return;
302
303         if (!autocvar_bot_ai_aimskill_firetolerance)
304         {
305                 this.bot_firetimer = time + 0.2;
306                 return;
307         }
308
309         makevectors(this.v_angle);
310         shotorg = this.origin + this.view_ofs;
311         shotdir = v_forward;
312
313         // decide whether to fire this time
314         // v is the calculated trajectory, shotdir is bot view direction
315         // NOTE: checking if (v * shotdir > cos(maxfiredeviation * DEG2RAD)) would be cheaper
316         // but it gets evaluated to true even if v and shotdir have nearly opposite direction
317         vector deviation = vectoangles(v) - vectoangles(shotdir);
318         while (deviation.x < -180) deviation.x += 360; while (deviation.x > 180) deviation.x -= 360;
319         while (deviation.y < -180) deviation.y += 360; while (deviation.y > 180) deviation.y -= 360;
320         if (fabs(deviation.x) < maxfiredeviation && fabs(deviation.y) < maxfiredeviation)
321         {
322                 traceline(shotorg, shotorg + shotdir * 1000, false, NULL);
323                 if (vdist(trace_endpos - shotorg, <, 500 + 500 * bound(0, skill + this.bot_aggresskill, 10))
324                         || random() * random() > bound(0, (skill + this.bot_aggresskill) * 0.05, 1))
325                 {
326                         this.bot_firetimer = time + bound(0.1, 0.5 - (skill + this.bot_aggresskill) * 0.05, 0.5);
327                 }
328         }
329 }
330
331 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
332 {
333         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
334         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
335 }
336
337 bool bot_aim(entity this, .entity weaponentity, float shotspeed, float shotspeedupward, float maxshottime, bool applygravity, bool shot_accurate)
338 {
339         float hf, distanceratio;
340         vector v;
341         hf = this.dphitcontentsmask;
342         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
343
344         float speed_factor = W_WeaponSpeedFactor(this);
345         shotspeed *= speed_factor;
346         shotspeedupward *= speed_factor;
347         if (!shotspeed)
348         {
349                 LOG_TRACE("bot_aim: WARNING: weapon ", this.(weaponentity).m_weapon.m_name, " shotspeed is zero!");
350                 shotspeed = 1000000;
351         }
352         if (!maxshottime)
353         {
354                 LOG_TRACE("bot_aim: WARNING: weapon ", this.(weaponentity).m_weapon.m_name, " maxshottime is zero!");
355                 maxshottime = 1;
356         }
357         makevectors(this.v_angle);
358         shotorg = this.origin + this.view_ofs;
359         shotdir = v_forward;
360         vector enemy_org = (this.enemy.absmin + this.enemy.absmax) * 0.5;
361         v = bot_shotlead(enemy_org, this.enemy.velocity, shotspeed, this.bot_aimlatency);
362
363         // this formula was created starting from empiric values of distance and max hit angle
364         // with a player as target (32 qu wide) from the center of it right in front of the bot
365         //  distance: 32  50    75   100  150  200  300  400  500
366         //   max ang: 44  24  15.1  10.5  6.5  4.9  3.1  2.3  1.8
367         float dist = max(10, vlen(v - shotorg));
368         float maxfiredeviation = 1000 / (dist - 9) - 0.35;
369
370         float f = (shot_accurate) ? 1 : 1.6;
371         f += bound(0, (10 - (skill + this.bot_aimskill)) * 0.3, 3);
372         maxfiredeviation = min(90, maxfiredeviation * f);
373
374         if (applygravity && this.enemy)
375         {
376                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', this.enemy, shotspeed, shotspeedupward, maxshottime, 0, this))
377                 {
378                         this.dphitcontentsmask = hf;
379                         return false;
380                 }
381
382                 bot_aimdir(this, findtrajectory_velocity - shotspeedupward * '0 0 1', maxfiredeviation);
383         }
384         else
385         {
386                 bot_aimdir(this, v - shotorg, maxfiredeviation);
387                 //dprint("AIM: ");dprint(vtos(enemy_org));dprint(" + ");dprint(vtos(this.enemy.velocity));dprint(" * ");dprint(ftos(this.bot_aimlatency + vlen(this.enemy.origin - shotorg) / shotspeed));dprint(" = ");dprint(vtos(v));dprint(" : aimdir = ");dprint(vtos(normalize(v - shotorg)));dprint(" : ");dprint(vtos(shotdir));dprint("\n");
388                 //traceline(shotorg, shotorg + shotdir * 10000, false, this);
389                 //if (trace_ent.takedamage)
390                 //if (trace_fraction < 1)
391                 //if (!bot_shouldattack(this, trace_ent))
392                 //      return false;
393                 traceline(shotorg, enemy_org, false, this);
394                 if (trace_fraction < 1)
395                 if (trace_ent != this.enemy)
396                 if (!bot_shouldattack(this, trace_ent))
397                 {
398                         this.dphitcontentsmask = hf;
399                         return false;
400                 }
401         }
402
403         if (time > this.bot_firetimer)
404         {
405                 this.dphitcontentsmask = hf;
406                 return false;
407         }
408
409         //if (r > maxshottime * shotspeed)
410         //      return false;
411         this.dphitcontentsmask = hf;
412         return true;
413 }