]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/anticheat.qc
Merge remote-tracking branch 'origin/terencehill/menu_langwarning'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / anticheat.qc
1 .float anticheat_jointime;
2
3 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
4 {
5         if(weight == 0)
6                 return;
7         if(mean == 0)
8                 e.a *= pow(value, weight);
9         else
10                 e.a += pow(value, mean) * weight;
11         e.c += weight;
12 }
13
14 float mean_evaluate(entity e, .float a, .float c, float mean)
15 {
16         if(e.c == 0)
17                 return 0;
18         if(mean == 0)
19                 return pow(e.a, 1.0 / e.c);
20         else
21                 return pow(e.a / e.c, 1.0 / mean);
22 }
23
24 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
25 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
26 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
27
28 .float anticheat_fixangle_endtime;
29
30 float anticheat_div0_evade_evasion_delta;
31 .float anticheat_div0_evade_offset;
32 .vector anticheat_div0_evade_v_angle;
33 .vector anticheat_div0_evade_forward_initial;
34 MEAN_DECLARE(anticheat_div0_evade, 5);
35
36 .vector anticheat_div0_strafebot_movement_prev;
37 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
38
39 .vector anticheat_div0_strafebot_forward_prev;
40 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
41
42 // Snap-aim detection: we track the average angular speed of aiming over time, in "radians per second".
43 // Signal: a high-power mean. Cheaters will have high "signal" here.
44 // Noise: a low-power mean. Active/shivery players will have high "noise" here.
45 // Note one can always artificially add noise - so very high values of both signal and noise need to be checked too.
46 MEAN_DECLARE(anticheat_idle_snapaim_signal, 5);
47 MEAN_DECLARE(anticheat_idle_snapaim_noise, 1);
48
49 // TEMP DEBUG STUFF.
50 MEAN_DECLARE(anticheat_idle_snapaim_m2, 2);
51 MEAN_DECLARE(anticheat_idle_snapaim_m3, 3);
52 MEAN_DECLARE(anticheat_idle_snapaim_m4, 4);
53 MEAN_DECLARE(anticheat_idle_snapaim_m7, 7);
54 MEAN_DECLARE(anticheat_idle_snapaim_m10, 10);
55
56 .float anticheat_speedhack_offset;
57 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
58 MEAN_DECLARE(anticheat_speedhack, 5);
59
60 .float anticheat_speedhack_accu;
61 .float anticheat_speedhack_lasttime;
62 MEAN_DECLARE(anticheat_speedhack_m1, 1);
63 MEAN_DECLARE(anticheat_speedhack_m2, 2);
64 MEAN_DECLARE(anticheat_speedhack_m3, 3);
65 MEAN_DECLARE(anticheat_speedhack_m4, 4);
66 MEAN_DECLARE(anticheat_speedhack_m5, 5);
67
68 float movement_oddity(vector m0, vector m1)
69 {
70         float cosangle = normalize(m0) * normalize(m1);
71         if(cosangle >= 0)
72                 return 0;
73         return 0.5 - 0.5 * cos(cosangle * cosangle * 4 * M_PI);
74                 // returns 0 for: -1, -sqrt(0.5), 0 (angles that commonly happen with kbd)
75 }
76
77 void anticheat_physics()
78 {
79         float f, wishspeed;
80         vector wishvel;
81
82         // div0_evade -> SPECTATORS
83         makevectors(self.v_angle);
84         if(self.anticheat_div0_evade_offset == 0)
85         {
86                 f = fabs(anticheat_div0_evade_evasion_delta - floor(anticheat_div0_evade_evasion_delta) - 0.5) * 2; // triangle function
87                 self.anticheat_div0_evade_offset = servertime + sys_frametime * (3 * f - 1);
88                 self.anticheat_div0_evade_v_angle = self.v_angle;
89                 self.anticheat_div0_evade_forward_initial = v_forward;
90                 MEAN_ACCUMULATE(anticheat_div0_evade, 0, 1);
91         }
92         else
93         {
94                 if(time < self.anticheat_div0_evade_offset)
95                         self.anticheat_div0_evade_v_angle = self.v_angle;
96                 MEAN_ACCUMULATE(anticheat_div0_evade, 0.5 - 0.5 * (self.anticheat_div0_evade_forward_initial * v_forward), 1);
97         }
98
99         MEAN_ACCUMULATE(anticheat_div0_strafebot_old, movement_oddity(self.movement, self.anticheat_div0_strafebot_movement_prev), 1);
100         self.anticheat_div0_strafebot_movement_prev = self.movement;
101
102         // Note: this actually tries to detect snap-aim.
103         if(vlen(self.anticheat_div0_strafebot_forward_prev) && time > self.anticheat_fixangle_endtime) {
104                 float cosangle = self.anticheat_div0_strafebot_forward_prev * v_forward;
105                 float angle = cosangle < -1 ? M_PI : cosangle > 1 ? 0 : acos(cosangle);
106                 /*
107                 if (angle >= 10 * M_PI / 180)
108                         printf("SNAP %s: %f for %f, %f since fixangle\n", self.netname, angle * 180 / M_PI, cosangle, time - self.anticheat_fixangle_endtime);
109                 */
110                 MEAN_ACCUMULATE(anticheat_div0_strafebot_new, angle / M_PI, 1);
111
112                 if (autocvar_slowmo > 0) {
113                         // Technically this is a NOP, as the engine should be ensuring
114                         // this in the first place. Let's guard against dividing by
115                         // zero anyway.
116                         float dt = max(0.001, frametime) / autocvar_slowmo;
117
118                         float anglespeed = angle / dt;
119                         MEAN_ACCUMULATE(anticheat_idle_snapaim_signal, anglespeed, dt);
120                         MEAN_ACCUMULATE(anticheat_idle_snapaim_noise, anglespeed, dt);
121                         MEAN_ACCUMULATE(anticheat_idle_snapaim_m2, anglespeed, dt);
122                         MEAN_ACCUMULATE(anticheat_idle_snapaim_m3, anglespeed, dt);
123                         MEAN_ACCUMULATE(anticheat_idle_snapaim_m4, anglespeed, dt);
124                         MEAN_ACCUMULATE(anticheat_idle_snapaim_m7, anglespeed, dt);
125                         MEAN_ACCUMULATE(anticheat_idle_snapaim_m10, anglespeed, dt);
126                 }
127         }
128         self.anticheat_div0_strafebot_forward_prev = v_forward;
129
130         // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
131         self.anticheat_speedhack_movetime_frac += frametime;
132         f = floor(self.anticheat_speedhack_movetime_frac);
133         self.anticheat_speedhack_movetime_frac -= f;
134         self.anticheat_speedhack_movetime_count += f;
135         self.anticheat_speedhack_movetime = self.anticheat_speedhack_movetime_frac + self.anticheat_speedhack_movetime_count;
136         f = self.anticheat_speedhack_movetime - servertime;
137         if(self.anticheat_speedhack_offset == 0)
138                 self.anticheat_speedhack_offset = f;
139         else
140         {
141                 MEAN_ACCUMULATE(anticheat_speedhack, max(0, f - self.anticheat_speedhack_offset), 1);
142                 self.anticheat_speedhack_offset += (f - self.anticheat_speedhack_offset) * frametime * 0.1;
143         }
144
145         // new generic speedhack detection
146         if (self.anticheat_speedhack_lasttime > 0) {
147                 float dt = servertime - self.anticheat_speedhack_lasttime;
148                 const float falloff = 0.2;
149                 self.anticheat_speedhack_accu *= exp(-dt * falloff);
150                 self.anticheat_speedhack_accu += frametime * falloff;
151                 // NOTE: at cl_netfps x, this actually averages not to 1, but to 1/x * falloff / (1 - exp(-1/x * falloff))
152                 // For 15 netfps (absolute minimum bearable), and 0.2 falloff, this is: 1.0067
153                 self.anticheat_speedhack_lasttime = servertime;
154                 MEAN_ACCUMULATE(anticheat_speedhack_m1, self.anticheat_speedhack_accu, frametime);
155                 MEAN_ACCUMULATE(anticheat_speedhack_m2, self.anticheat_speedhack_accu, frametime);
156                 MEAN_ACCUMULATE(anticheat_speedhack_m3, self.anticheat_speedhack_accu, frametime);
157                 MEAN_ACCUMULATE(anticheat_speedhack_m4, self.anticheat_speedhack_accu, frametime);
158                 MEAN_ACCUMULATE(anticheat_speedhack_m5, self.anticheat_speedhack_accu, frametime);
159         } else {
160                 self.anticheat_speedhack_accu = 1;
161                 self.anticheat_speedhack_lasttime = servertime;
162         }
163
164         // race/CTS: force kbd movement for fairness
165         if(g_race || g_cts)
166         {
167                 // if record times matter
168                 // ensure nothing EVIL is being done (i.e. div0_evade)
169                 // this hinders joystick users though
170                 // but it still gives SOME analog control
171                 wishvel_x = fabs(self.movement_x);
172                 wishvel_y = fabs(self.movement_y);
173                 if(wishvel_x != 0 && wishvel_y != 0 && wishvel_x != wishvel_y)
174                 {
175                         wishvel_z = 0;
176                         wishspeed = vlen(wishvel);
177                         if(wishvel_x >= 2 * wishvel_y)
178                         {
179                                 // pure X motion
180                                 if(self.movement_x > 0)
181                                         self.movement_x = wishspeed;
182                                 else
183                                         self.movement_x = -wishspeed;
184                                 self.movement_y = 0;
185                         }
186                         else if(wishvel_y >= 2 * wishvel_x)
187                         {
188                                 // pure Y motion
189                                 self.movement_x = 0;
190                                 if(self.movement_y > 0)
191                                         self.movement_y = wishspeed;
192                                 else
193                                         self.movement_y = -wishspeed;
194                         }
195                         else
196                         {
197                                 // diagonal
198                                 if(self.movement_x > 0)
199                                         self.movement_x = M_SQRT1_2 * wishspeed;
200                                 else
201                                         self.movement_x = -M_SQRT1_2 * wishspeed;
202                                 if(self.movement_y > 0)
203                                         self.movement_y = M_SQRT1_2 * wishspeed;
204                                 else
205                                         self.movement_y = -M_SQRT1_2 * wishspeed;
206                         }
207                 }
208         }
209 }
210
211 void anticheat_spectatecopy(entity spectatee)
212 {
213         // div0_evade -> SPECTATORS
214         self.angles = spectatee.anticheat_div0_evade_v_angle;
215 }
216
217 void anticheat_prethink()
218 {
219         // div0_evade -> SPECTATORS
220         self.anticheat_div0_evade_offset = 0;
221 }
222
223 string anticheat_display(float f, float tmin, float mi, float ma)
224 {
225         string s;
226         s = ftos(f);
227         if(f <= mi)
228                 return strcat(s, ":N");
229         if(f >= ma)
230                 return strcat(s, ":Y");
231         return strcat(s, ":-");
232 }
233
234 void anticheat_report()
235 {
236         if(!autocvar_sv_eventlog)
237                 return;
238         // TODO(divVerent): Use xonstat to acquire good thresholds.
239         GameLogEcho(strcat(":anticheat:_time:", ftos(self.playerid), ":", ftos(servertime - self.anticheat_jointime)));
240         GameLogEcho(strcat(":anticheat:speedhack:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack), 240, 0.1, 0.15)));
241         GameLogEcho(strcat(":anticheat:speedhack_m1:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack_m1), 240, 0.1, 0.15)));
242         GameLogEcho(strcat(":anticheat:speedhack_m2:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack_m2), 240, 0.1, 0.15)));
243         GameLogEcho(strcat(":anticheat:speedhack_m3:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack_m3), 240, 0.1, 0.15)));
244         GameLogEcho(strcat(":anticheat:speedhack_m4:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack_m4), 240, 0.1, 0.15)));
245         GameLogEcho(strcat(":anticheat:speedhack_m5:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack_m5), 240, 0.1, 0.15)));
246         GameLogEcho(strcat(":anticheat:div0_strafebot_old:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_strafebot_old), 120, 0.3, 0.4)));
247         GameLogEcho(strcat(":anticheat:div0_strafebot_new:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_strafebot_new), 120, 0.3, 0.4)));
248         GameLogEcho(strcat(":anticheat:div0_evade:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_evade), 120, 0.1, 0.2)));
249         GameLogEcho(strcat(":anticheat:idle_snapaim:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_signal) - MEAN_EVALUATE(anticheat_idle_snapaim_noise), 120, 0.1, 0.2)));
250         GameLogEcho(strcat(":anticheat:idle_snapaim_signal:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_signal), 120, 0.1, 0.2)));
251         GameLogEcho(strcat(":anticheat:idle_snapaim_noise:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_noise), 120, 0.1, 0.2)));
252         GameLogEcho(strcat(":anticheat:idle_snapaim_m2:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_m2), 120, 0.1, 0.2)));
253         GameLogEcho(strcat(":anticheat:idle_snapaim_m3:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_m3), 120, 0.1, 0.2)));
254         GameLogEcho(strcat(":anticheat:idle_snapaim_m4:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_m4), 120, 0.1, 0.2)));
255         GameLogEcho(strcat(":anticheat:idle_snapaim_m7:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_m7), 120, 0.1, 0.2)));
256         GameLogEcho(strcat(":anticheat:idle_snapaim_m10:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_m10), 120, 0.1, 0.2)));
257 }
258
259 float anticheat_getvalue(string id)
260 {
261         switch(id) {
262                 case "_time": return servertime - self.anticheat_jointime;
263                 case "speedhack": return MEAN_EVALUATE(anticheat_speedhack);
264                 case "speedhack_m1": return MEAN_EVALUATE(anticheat_speedhack_m1);
265                 case "speedhack_m2": return MEAN_EVALUATE(anticheat_speedhack_m2);
266                 case "speedhack_m3": return MEAN_EVALUATE(anticheat_speedhack_m3);
267                 case "speedhack_m4": return MEAN_EVALUATE(anticheat_speedhack_m4);
268                 case "speedhack_m5": return MEAN_EVALUATE(anticheat_speedhack_m5);
269                 case "div0_strafebot_old": return MEAN_EVALUATE(anticheat_div0_strafebot_old);
270                 case "div0_strafebot_new": return MEAN_EVALUATE(anticheat_div0_strafebot_new);
271                 case "div0_evade": return MEAN_EVALUATE(anticheat_div0_evade);
272                 case "idle_snapaim": return MEAN_EVALUATE(anticheat_idle_snapaim_signal) - MEAN_EVALUATE(anticheat_idle_snapaim_noise);
273                 case "idle_snapaim_signal": return MEAN_EVALUATE(anticheat_idle_snapaim_signal);
274                 case "idle_snapaim_noise": return MEAN_EVALUATE(anticheat_idle_snapaim_noise);
275                 case "idle_snapaim_m2": return MEAN_EVALUATE(anticheat_idle_snapaim_m2);
276                 case "idle_snapaim_m3": return MEAN_EVALUATE(anticheat_idle_snapaim_m3);
277                 case "idle_snapaim_m4": return MEAN_EVALUATE(anticheat_idle_snapaim_m4);
278                 case "idle_snapaim_m7": return MEAN_EVALUATE(anticheat_idle_snapaim_m7);
279                 case "idle_snapaim_m10": return MEAN_EVALUATE(anticheat_idle_snapaim_m10);
280         }
281         return -1;
282 }
283
284 void anticheat_startframe()
285 {
286         anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
287 }
288
289 void anticheat_fixangle()
290 {
291         self.anticheat_fixangle_endtime = servertime + ANTILAG_LATENCY(self) + 0.2;
292 }
293
294 void anticheat_endframe()
295 {
296         entity oldself = self;
297         FOR_EACH_CLIENT(self)
298                 if (self.fixangle)
299                         anticheat_fixangle();
300         self = oldself;
301         anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
302 }
303
304 void anticheat_init()
305 {
306         self.anticheat_speedhack_offset = 0;
307         self.anticheat_jointime = servertime;
308 }
309
310 void anticheat_shutdown()
311 {
312 }