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