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