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