1 #include "anticheat.qh"
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/autocvars.qh>
9 #include <server/client.qh>
10 #include <server/command/common.qh>
11 #include <server/gamelog.qh>
12 #include <server/main.qh>
14 .float anticheat_jointime;
16 .float anticheat_fixangle_endtime;
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);
24 .vector anticheat_div0_strafebot_movement_prev;
25 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
27 .vector anticheat_div0_strafebot_forward_prev;
28 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
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);
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);
44 .float anticheat_speedhack_offset;
45 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
46 MEAN_DECLARE(anticheat_speedhack, 5);
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);
56 float movement_oddity(vector m0, vector m1)
58 float cosangle = normalize(m0) * normalize(m1);
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)
65 void anticheat_physics(entity this)
69 // div0_evade -> SPECTATORS
70 makevectors(this.v_angle);
71 if(CS(this).anticheat_div0_evade_offset == 0)
73 f = fabs(anticheat_div0_evade_evasion_delta - floor(anticheat_div0_evade_evasion_delta) - 0.5) * 2; // triangle function
74 CS(this).anticheat_div0_evade_offset = servertime + sys_frametime * (3 * f - 1);
75 CS(this).anticheat_div0_evade_v_angle = this.v_angle;
76 CS(this).anticheat_div0_evade_forward_initial = v_forward;
77 MEAN_ACCUMULATE(CS(this), anticheat_div0_evade, 0, 1);
81 if(time < CS(this).anticheat_div0_evade_offset)
82 CS(this).anticheat_div0_evade_v_angle = this.v_angle;
83 MEAN_ACCUMULATE(CS(this), anticheat_div0_evade, 0.5 - 0.5 * (CS(this).anticheat_div0_evade_forward_initial * v_forward), 1);
86 MEAN_ACCUMULATE(CS(this), anticheat_div0_strafebot_old, movement_oddity(CS(this).movement, CS(this).anticheat_div0_strafebot_movement_prev), 1);
87 CS(this).anticheat_div0_strafebot_movement_prev = CS(this).movement;
89 // Note: this actually tries to detect snap-aim.
90 if(CS(this).anticheat_div0_strafebot_forward_prev && time > CS(this).anticheat_fixangle_endtime) {
91 float cosangle = CS(this).anticheat_div0_strafebot_forward_prev * v_forward;
92 float angle = cosangle < -1 ? M_PI : cosangle > 1 ? 0 : acos(cosangle);
94 if (angle >= 10 * M_PI / 180)
95 printf("SNAP %s: %f for %f, %f since fixangle\n", this.netname, angle * 180 / M_PI, cosangle, time - CS(this).anticheat_fixangle_endtime);
97 MEAN_ACCUMULATE(CS(this), anticheat_div0_strafebot_new, angle / M_PI, 1);
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
103 float dt = max(0.001, frametime) / autocvar_slowmo;
105 float anglespeed = angle / dt;
106 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_signal, anglespeed, dt);
107 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_noise, anglespeed, dt);
108 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m2, anglespeed, dt);
109 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m3, anglespeed, dt);
110 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m4, anglespeed, dt);
111 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m7, anglespeed, dt);
112 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m10, anglespeed, dt);
115 CS(this).anticheat_div0_strafebot_forward_prev = v_forward;
117 // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
118 CS(this).anticheat_speedhack_movetime_frac += frametime;
119 f = floor(CS(this).anticheat_speedhack_movetime_frac);
120 CS(this).anticheat_speedhack_movetime_frac -= f;
121 CS(this).anticheat_speedhack_movetime_count += f;
122 CS(this).anticheat_speedhack_movetime = CS(this).anticheat_speedhack_movetime_frac + CS(this).anticheat_speedhack_movetime_count;
123 f = CS(this).anticheat_speedhack_movetime - servertime;
124 if(CS(this).anticheat_speedhack_offset == 0)
125 CS(this).anticheat_speedhack_offset = f;
128 MEAN_ACCUMULATE(CS(this), anticheat_speedhack, max(0, f - CS(this).anticheat_speedhack_offset), 1);
129 CS(this).anticheat_speedhack_offset += (f - CS(this).anticheat_speedhack_offset) * frametime * 0.1;
132 // new generic speedhack detection
133 if (CS(this).anticheat_speedhack_lasttime > 0) {
134 float dt = servertime - CS(this).anticheat_speedhack_lasttime;
135 const float falloff = 0.2;
136 CS(this).anticheat_speedhack_accu *= exp(-dt * falloff);
137 CS(this).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 CS(this).anticheat_speedhack_lasttime = servertime;
141 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m1, CS(this).anticheat_speedhack_accu, frametime);
142 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m2, CS(this).anticheat_speedhack_accu, frametime);
143 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m3, CS(this).anticheat_speedhack_accu, frametime);
144 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m4, CS(this).anticheat_speedhack_accu, frametime);
145 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m5, CS(this).anticheat_speedhack_accu, frametime);
147 CS(this).anticheat_speedhack_accu = 1;
148 CS(this).anticheat_speedhack_lasttime = servertime;
152 void anticheat_spectatecopy(entity this, entity spectatee)
154 // div0_evade -> SPECTATORS
155 this.angles = CS(spectatee).anticheat_div0_evade_v_angle;
158 void anticheat_prethink(entity this)
160 // div0_evade -> SPECTATORS
161 CS(this).anticheat_div0_evade_offset = 0;
164 string anticheat_display(float f, float t, float tmin, float mi, float ma)
170 return strcat(s, ":N");
172 return strcat(s, ":Y");
174 return strcat(s, ":-");
177 #define ANTICHEATS(ANTICHEAT) \
178 ANTICHEAT("speedhack", MEAN_EVALUATE(CS(this), anticheat_speedhack), 240, 0, 9999); /* Actually this one seems broken. */ \
179 ANTICHEAT("speedhack_m1", MEAN_EVALUATE(CS(this), anticheat_speedhack_m1), 240, 1.01, 1.25); \
180 ANTICHEAT("speedhack_m2", MEAN_EVALUATE(CS(this), anticheat_speedhack_m2), 240, 1.01, 1.25); \
181 ANTICHEAT("speedhack_m3", MEAN_EVALUATE(CS(this), anticheat_speedhack_m3), 240, 1.01, 1.25); \
182 ANTICHEAT("speedhack_m4", MEAN_EVALUATE(CS(this), anticheat_speedhack_m4), 240, 1.01, 1.25); \
183 ANTICHEAT("speedhack_m5", MEAN_EVALUATE(CS(this), anticheat_speedhack_m5), 240, 1.01, 1.25); \
184 ANTICHEAT("div0_strafebot_old", MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_old), 120, 0.15, 0.4); \
185 ANTICHEAT("div0_strafebot_new", MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_new), 120, 0.25, 0.8); \
186 ANTICHEAT("div0_evade", MEAN_EVALUATE(CS(this), anticheat_div0_evade), 120, 0.2, 0.5); \
187 ANTICHEAT("idle_snapaim", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal) - MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise), 120, 0, 9999); \
188 ANTICHEAT("idle_snapaim_signal", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal), 120, 0, 9999); \
189 ANTICHEAT("idle_snapaim_noise", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise), 120, 0, 9999); \
190 ANTICHEAT("idle_snapaim_m2", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m2), 120, 0, 9999); \
191 ANTICHEAT("idle_snapaim_m3", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m3), 120, 0, 9999); \
192 ANTICHEAT("idle_snapaim_m4", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m4), 120, 0, 9999); \
193 ANTICHEAT("idle_snapaim_m7", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m7), 120, 0, 9999); \
194 ANTICHEAT("idle_snapaim_m10", MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m10), 120, 0, 9999)
196 void anticheat_report_to_eventlog(entity this) {
197 if(!autocvar_sv_eventlog)
199 GameLogEcho(strcat(":anticheat:_time:", ftos(this.playerid), ":", ftos(servertime - CS(this).anticheat_jointime)));
200 #define ANTICHEAT_REPORT_ONE(name, f, tmin, mi, ma) \
201 GameLogEcho(strcat(":anticheat:", name, ":", anticheat_display(f, servertime - CS(this).anticheat_jointime, tmin, mi, ma)))
202 ANTICHEATS(ANTICHEAT_REPORT_ONE);
203 #undef ANTICHEAT_REPORT_ONE
206 void anticheat_report_to_playerstats(entity this) {
207 PlayerStats_GameReport_Event_Player(this,
208 strcat(PLAYERSTATS_ANTICHEAT, "_time"), servertime - CS(this).anticheat_jointime);
209 #define ANTICHEAT_REPORT_ONE(name, f, tmin, mi, ma) \
210 PlayerStats_GameReport_Event_Player(this, strcat(PLAYERSTATS_ANTICHEAT, name), f)
211 ANTICHEATS(ANTICHEAT_REPORT_ONE);
212 #undef ANTICHEAT_REPORT_ONE
215 void anticheat_register_to_playerstats() {
216 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_ANTICHEAT, "_time"));
217 #define ANTICHEAT_REGISTER_ONE(name, unused_f, unused_tmin, unused_mi, unused_ma) \
218 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_ANTICHEAT, name))
219 ANTICHEATS(ANTICHEAT_REGISTER_ONE);
220 #undef ANTICHEAT_REGISTER_ONE
225 void anticheat_startframe()
227 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
230 void anticheat_fixangle(entity this)
232 CS(this).anticheat_fixangle_endtime = servertime + ANTILAG_LATENCY(this) + 0.2;
235 void anticheat_endframe()
237 FOREACH_CLIENT(it.fixangle, anticheat_fixangle(it));
238 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
241 void anticheat_init(entity this)
243 CS(this).anticheat_speedhack_offset = 0;
244 CS(this).anticheat_jointime = servertime;