]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/aim.qc
s/world/NULL/
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / aim.qc
1 #include "aim.qh"
2
3 #include "bot.qh"
4
5 #include <common/physics/player.qh>
6 #include <common/state.qh>
7
8 #include "../weapons/weaponsystem.qh"
9
10 #include "../mutators/all.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         tracetossfaketarget.movetype = targ.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                         tracetossfaketarget.movetype = 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         tracetossfaketarget.movetype = 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) 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;}
95         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;}
96         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;}
97         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;}
98         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;}
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(bot_ignore_bots)
132                 if(IS_BOT_CLIENT(targ))
133                         return false;
134
135         if (!targ.takedamage)
136                 return false;
137         if (IS_DEAD(targ))
138                 return false;
139         if (PHYS_INPUT_BUTTON_CHAT(targ))
140                 return false;
141         if(targ.flags & FL_NOTARGET)
142                 return false;
143
144         if(MUTATOR_CALLHOOK(BotShouldAttack, this, targ))
145                 return false;
146
147         return true;
148 }
149
150 void bot_lagfunc(entity this, float t, float f1, float f2, entity e1, vector v1, vector v2, vector v3, vector v4)
151 {
152         if(this.flags & FL_INWATER)
153         {
154                 this.bot_aimtarg = NULL;
155                 return;
156         }
157         this.bot_aimtarg = e1;
158         this.bot_aimlatency = 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 float bot_aimdir(entity this, vector v, float maxfiredeviation)
174 {
175         float dist, delta_t, blend;
176         vector desiredang, diffang;
177
178         //dprint("aim ", this.netname, ": old:", vtos(this.v_angle));
179         // make sure v_angle is sane first
180         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
181         this.v_angle_z = 0;
182
183         // get the desired angles to aim at
184         //dprint(" at:", vtos(v));
185         v = normalize(v);
186         //te_lightning2(NULL, this.origin + this.view_ofs, this.origin + this.view_ofs + v * 200);
187         if (time >= this.bot_badaimtime)
188         {
189                 this.bot_badaimtime = max(this.bot_badaimtime + 0.3, time);
190                 this.bot_badaimoffset = randomvec() * bound(0, 5 - 0.5 * (skill+this.bot_offsetskill), 5) * autocvar_bot_ai_aimskill_offset;
191         }
192         desiredang = vectoangles(v) + this.bot_badaimoffset;
193         //dprint(" desired:", vtos(desiredang));
194         if (desiredang.x >= 180)
195                 desiredang.x = desiredang.x - 360;
196         desiredang.x = bound(-90, 0 - desiredang.x, 90);
197         desiredang.z = this.v_angle.z;
198         //dprint(" / ", vtos(desiredang));
199
200         //// pain throws off aim
201         //if (this.bot_painintensity)
202         //{
203         //      // shake from pain
204         //      desiredang = desiredang + randomvec() * this.bot_painintensity * 0.2;
205         //}
206
207         // calculate turn angles
208         diffang = (desiredang - this.bot_olddesiredang);
209         // wrap yaw turn
210         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
211         if (diffang.y >= 180)
212                 diffang.y = diffang.y - 360;
213         this.bot_olddesiredang = desiredang;
214         //dprint(" diff:", vtos(diffang));
215
216         delta_t = time-this.bot_prevaimtime;
217         this.bot_prevaimtime = time;
218         // Here we will try to anticipate the comming aiming direction
219         this.bot_1st_order_aimfilter= this.bot_1st_order_aimfilter
220                 + (diffang * (1 / delta_t)    - this.bot_1st_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_1st,1);
221         this.bot_2nd_order_aimfilter= this.bot_2nd_order_aimfilter
222                 + (this.bot_1st_order_aimfilter - this.bot_2nd_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_2nd,1);
223         this.bot_3th_order_aimfilter= this.bot_3th_order_aimfilter
224                 + (this.bot_2nd_order_aimfilter - this.bot_3th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_3th,1);
225         this.bot_4th_order_aimfilter= this.bot_4th_order_aimfilter
226                 + (this.bot_3th_order_aimfilter - this.bot_4th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_4th,1);
227         this.bot_5th_order_aimfilter= this.bot_5th_order_aimfilter
228                 + (this.bot_4th_order_aimfilter - this.bot_5th_order_aimfilter) * bound(0, autocvar_bot_ai_aimskill_order_filter_5th,1);
229
230         //blend = (bound(0,skill,10)*0.1)*pow(1-bound(0,skill,10)*0.05,2.5)*5.656854249; //Plot formule before changing !
231         blend = bound(0,skill+this.bot_aimskill,10)*0.1;
232         desiredang = desiredang + blend *
233         (
234                   this.bot_1st_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_1st
235                 + this.bot_2nd_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_2nd
236                 + this.bot_3th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_3th
237                 + this.bot_4th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_4th
238                 + this.bot_5th_order_aimfilter * autocvar_bot_ai_aimskill_order_mix_5th
239         );
240
241         // calculate turn angles
242         diffang = desiredang - this.bot_mouseaim;
243         // wrap yaw turn
244         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
245         if (diffang.y >= 180)
246                 diffang.y = diffang.y - 360;
247         //dprint(" diff:", vtos(diffang));
248
249         if (time >= this.bot_aimthinktime)
250         {
251                 this.bot_aimthinktime = max(this.bot_aimthinktime + 0.5 - 0.05*(skill+this.bot_thinkskill), time);
252                 this.bot_mouseaim = this.bot_mouseaim + diffang * (1-random()*0.1*bound(1,10-(skill+this.bot_thinkskill),10));
253         }
254
255         //this.v_angle = this.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
256
257         diffang = this.bot_mouseaim - desiredang;
258         // wrap yaw turn
259         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
260         if (diffang.y >= 180)
261                 diffang.y = diffang.y - 360;
262         desiredang = desiredang + diffang * bound(0,autocvar_bot_ai_aimskill_think,1);
263
264         // calculate turn angles
265         diffang = desiredang - this.v_angle;
266         // wrap yaw turn
267         diffang.y = diffang.y - floor(diffang.y / 360) * 360;
268         if (diffang.y >= 180)
269                 diffang.y = diffang.y - 360;
270         //dprint(" diff:", vtos(diffang));
271
272         // jitter tracking
273         dist = vlen(diffang);
274         //diffang = diffang + randomvec() * (dist * 0.05 * (3.5 - bound(0, skill, 3)));
275
276         // turn
277         float r, fixedrate, blendrate;
278         fixedrate = autocvar_bot_ai_aimskill_fixedrate / bound(1,dist,1000);
279         blendrate = autocvar_bot_ai_aimskill_blendrate;
280         r = max(fixedrate, blendrate);
281         //this.v_angle = this.v_angle + diffang * bound(frametime, r * frametime * (2+skill*skill*0.05-random()*0.05*(10-skill)), 1);
282         this.v_angle = this.v_angle + diffang * bound(delta_t, r * delta_t * (2+pow(skill+this.bot_mouseskill,3)*0.005-random()), 1);
283         this.v_angle = this.v_angle * bound(0,autocvar_bot_ai_aimskill_mouse,1) + desiredang * bound(0,(1-autocvar_bot_ai_aimskill_mouse),1);
284         //this.v_angle = this.v_angle + diffang * bound(0, r * frametime * (skill * 0.5 + 2), 1);
285         //this.v_angle = this.v_angle + diffang * (1/ blendrate);
286         this.v_angle_z = 0;
287         this.v_angle_y = this.v_angle.y - floor(this.v_angle.y / 360) * 360;
288         //dprint(" turn:", vtos(this.v_angle));
289
290         makevectors(this.v_angle);
291         shotorg = this.origin + this.view_ofs;
292         shotdir = v_forward;
293
294         //dprint(" dir:", vtos(v_forward));
295         //te_lightning2(NULL, shotorg, shotorg + shotdir * 100);
296
297         // calculate turn angles again
298         //diffang = desiredang - this.v_angle;
299         //diffang_y = diffang_y - floor(diffang_y / 360) * 360;
300         //if (diffang_y >= 180)
301         //      diffang_y = diffang_y - 360;
302
303         //dprint("e ", vtos(diffang), " < ", ftos(maxfiredeviation), "\n");
304
305         // decide whether to fire this time
306         // note the maxfiredeviation is in degrees so this has to convert to radians first
307         //if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
308         if ((normalize(v) * shotdir) >= cos(maxfiredeviation * (3.14159265358979323846 / 180)))
309         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))
310                 this.bot_firetimer = time + bound(0.1, 0.5-(skill+this.bot_aggresskill)*0.05, 0.5);
311         //traceline(shotorg,shotorg+shotdir*1000,false,NULL);
312         //dprint(ftos(maxfiredeviation),"\n");
313         //dprint(" diff:", vtos(diffang), "\n");
314
315         return this.bot_canfire && (time < this.bot_firetimer);
316 }
317
318 vector bot_shotlead(vector targorigin, vector targvelocity, float shotspeed, float shotdelay)
319 {
320         // Try to add code here that predicts gravity effect here, no clue HOW to though ... well not yet atleast...
321         return targorigin + targvelocity * (shotdelay + vlen(targorigin - shotorg) / shotspeed);
322 }
323
324 bool bot_aim(entity this, float shotspeed, float shotspeedupward, float maxshottime, bool applygravity)
325 {
326         float f, r, hf, distanceratio;
327         vector v;
328         /*
329         eprint(this);
330         dprint("bot_aim(", ftos(shotspeed));
331         dprint(", ", ftos(shotspeedupward));
332         dprint(", ", ftos(maxshottime));
333         dprint(", ", ftos(applygravity));
334         dprint(");\n");
335         */
336
337         hf = this.dphitcontentsmask;
338         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
339
340         shotspeed *= W_WeaponSpeedFactor(this);
341         shotspeedupward *= W_WeaponSpeedFactor(this);
342         if (!shotspeed)
343         {
344                 LOG_TRACE("bot_aim: WARNING: weapon ", PS(this).m_weapon.m_name, " shotspeed is zero!\n");
345                 shotspeed = 1000000;
346         }
347         if (!maxshottime)
348         {
349                 LOG_TRACE("bot_aim: WARNING: weapon ", PS(this).m_weapon.m_name, " maxshottime is zero!\n");
350                 maxshottime = 1;
351         }
352         makevectors(this.v_angle);
353         shotorg = this.origin + this.view_ofs;
354         shotdir = v_forward;
355         v = bot_shotlead(this.bot_aimtargorigin, this.bot_aimtargvelocity, shotspeed, this.bot_aimlatency);
356         distanceratio = sqrt(bound(0,skill,10000))*0.3*(vlen(v-shotorg)-100)/autocvar_bot_ai_aimskill_firetolerance_distdegrees;
357         distanceratio = bound(0,distanceratio,1);
358         r =  (autocvar_bot_ai_aimskill_firetolerance_maxdegrees-autocvar_bot_ai_aimskill_firetolerance_mindegrees)
359                 * (1-distanceratio) + autocvar_bot_ai_aimskill_firetolerance_mindegrees;
360         if (applygravity && this.bot_aimtarg)
361         {
362                 if (!findtrajectorywithleading(shotorg, '0 0 0', '0 0 0', this.bot_aimtarg, shotspeed, shotspeedupward, maxshottime, 0, this))
363                 {
364                         this.dphitcontentsmask = hf;
365                         return false;
366                 }
367
368                 f = bot_aimdir(this, findtrajectory_velocity - shotspeedupward * '0 0 1', r);
369         }
370         else
371         {
372                 f = bot_aimdir(this, v - shotorg, r);
373                 //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");
374                 //traceline(shotorg, shotorg + shotdir * 10000, false, this);
375                 //if (trace_ent.takedamage)
376                 //if (trace_fraction < 1)
377                 //if (!bot_shouldattack(this, trace_ent))
378                 //      return false;
379                 traceline(shotorg, this.bot_aimtargorigin, false, this);
380                 if (trace_fraction < 1)
381                 if (trace_ent != this.enemy)
382                 if (!bot_shouldattack(this, trace_ent))
383                 {
384                         this.dphitcontentsmask = hf;
385                         return false;
386                 }
387         }
388
389         //if (r > maxshottime * shotspeed)
390         //      return false;
391         this.dphitcontentsmask = hf;
392         return true;
393 }