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