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