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