1 #include "anticheat.qh"
4 #include "autocvars.qh"
6 #include "miscfunctions.qh"
8 #include "command/common.qh"
9 #include <common/state.qh>
11 .float anticheat_jointime;
13 .float anticheat_fixangle_endtime;
15 float anticheat_div0_evade_evasion_delta;
16 .float anticheat_div0_evade_offset;
17 .vector anticheat_div0_evade_v_angle;
18 .vector anticheat_div0_evade_forward_initial;
19 MEAN_DECLARE(anticheat_div0_evade, 5);
21 .vector anticheat_div0_strafebot_movement_prev;
22 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
24 .vector anticheat_div0_strafebot_forward_prev;
25 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
27 // Snap-aim detection: we track the average angular speed of aiming over time, in "radians per second".
28 // Signal: a high-power mean. Cheaters will have high "signal" here.
29 // Noise: a low-power mean. Active/shivery players will have high "noise" here.
30 // Note one can always artificially add noise - so very high values of both signal and noise need to be checked too.
31 MEAN_DECLARE(anticheat_idle_snapaim_signal, 5);
32 MEAN_DECLARE(anticheat_idle_snapaim_noise, 1);
35 MEAN_DECLARE(anticheat_idle_snapaim_m2, 2);
36 MEAN_DECLARE(anticheat_idle_snapaim_m3, 3);
37 MEAN_DECLARE(anticheat_idle_snapaim_m4, 4);
38 MEAN_DECLARE(anticheat_idle_snapaim_m7, 7);
39 MEAN_DECLARE(anticheat_idle_snapaim_m10, 10);
41 .float anticheat_speedhack_offset;
42 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
43 MEAN_DECLARE(anticheat_speedhack, 5);
45 .float anticheat_speedhack_accu;
46 .float anticheat_speedhack_lasttime;
47 MEAN_DECLARE(anticheat_speedhack_m1, 1);
48 MEAN_DECLARE(anticheat_speedhack_m2, 2);
49 MEAN_DECLARE(anticheat_speedhack_m3, 3);
50 MEAN_DECLARE(anticheat_speedhack_m4, 4);
51 MEAN_DECLARE(anticheat_speedhack_m5, 5);
53 float movement_oddity(vector m0, vector m1)
55 float cosangle = normalize(m0) * normalize(m1);
58 return 0.5 - 0.5 * cos(cosangle * cosangle * 4 * M_PI);
59 // returns 0 for: -1, -sqrt(0.5), 0 (angles that commonly happen with kbd)
62 void anticheat_physics(entity this)
66 // div0_evade -> SPECTATORS
67 makevectors(this.v_angle);
68 if(CS(this).anticheat_div0_evade_offset == 0)
70 f = fabs(anticheat_div0_evade_evasion_delta - floor(anticheat_div0_evade_evasion_delta) - 0.5) * 2; // triangle function
71 CS(this).anticheat_div0_evade_offset = servertime + sys_frametime * (3 * f - 1);
72 CS(this).anticheat_div0_evade_v_angle = this.v_angle;
73 CS(this).anticheat_div0_evade_forward_initial = v_forward;
74 MEAN_ACCUMULATE(CS(this), anticheat_div0_evade, 0, 1);
78 if(time < CS(this).anticheat_div0_evade_offset)
79 CS(this).anticheat_div0_evade_v_angle = this.v_angle;
80 MEAN_ACCUMULATE(CS(this), anticheat_div0_evade, 0.5 - 0.5 * (CS(this).anticheat_div0_evade_forward_initial * v_forward), 1);
83 MEAN_ACCUMULATE(CS(this), anticheat_div0_strafebot_old, movement_oddity(this.movement, CS(this).anticheat_div0_strafebot_movement_prev), 1);
84 CS(this).anticheat_div0_strafebot_movement_prev = this.movement;
86 // Note: this actually tries to detect snap-aim.
87 if(CS(this).anticheat_div0_strafebot_forward_prev && time > CS(this).anticheat_fixangle_endtime) {
88 float cosangle = CS(this).anticheat_div0_strafebot_forward_prev * v_forward;
89 float angle = cosangle < -1 ? M_PI : cosangle > 1 ? 0 : acos(cosangle);
91 if (angle >= 10 * M_PI / 180)
92 printf("SNAP %s: %f for %f, %f since fixangle\n", this.netname, angle * 180 / M_PI, cosangle, time - CS(this).anticheat_fixangle_endtime);
94 MEAN_ACCUMULATE(CS(this), anticheat_div0_strafebot_new, angle / M_PI, 1);
96 if (autocvar_slowmo > 0) {
97 // Technically this is a NOP, as the engine should be ensuring
98 // this in the first place. Let's guard against dividing by
100 float dt = max(0.001, frametime) / autocvar_slowmo;
102 float anglespeed = angle / dt;
103 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_signal, anglespeed, dt);
104 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_noise, anglespeed, dt);
105 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m2, anglespeed, dt);
106 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m3, anglespeed, dt);
107 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m4, anglespeed, dt);
108 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m7, anglespeed, dt);
109 MEAN_ACCUMULATE(CS(this), anticheat_idle_snapaim_m10, anglespeed, dt);
112 CS(this).anticheat_div0_strafebot_forward_prev = v_forward;
114 // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
115 CS(this).anticheat_speedhack_movetime_frac += frametime;
116 f = floor(CS(this).anticheat_speedhack_movetime_frac);
117 CS(this).anticheat_speedhack_movetime_frac -= f;
118 CS(this).anticheat_speedhack_movetime_count += f;
119 CS(this).anticheat_speedhack_movetime = CS(this).anticheat_speedhack_movetime_frac + CS(this).anticheat_speedhack_movetime_count;
120 f = CS(this).anticheat_speedhack_movetime - servertime;
121 if(CS(this).anticheat_speedhack_offset == 0)
122 CS(this).anticheat_speedhack_offset = f;
125 MEAN_ACCUMULATE(CS(this), anticheat_speedhack, max(0, f - CS(this).anticheat_speedhack_offset), 1);
126 CS(this).anticheat_speedhack_offset += (f - CS(this).anticheat_speedhack_offset) * frametime * 0.1;
129 // new generic speedhack detection
130 if (CS(this).anticheat_speedhack_lasttime > 0) {
131 float dt = servertime - CS(this).anticheat_speedhack_lasttime;
132 const float falloff = 0.2;
133 CS(this).anticheat_speedhack_accu *= exp(-dt * falloff);
134 CS(this).anticheat_speedhack_accu += frametime * falloff;
135 // NOTE: at cl_netfps x, this actually averages not to 1, but to 1/x * falloff / (1 - exp(-1/x * falloff))
136 // For 15 netfps (absolute minimum bearable), and 0.2 falloff, this is: 1.0067
137 CS(this).anticheat_speedhack_lasttime = servertime;
138 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m1, CS(this).anticheat_speedhack_accu, frametime);
139 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m2, CS(this).anticheat_speedhack_accu, frametime);
140 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m3, CS(this).anticheat_speedhack_accu, frametime);
141 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m4, CS(this).anticheat_speedhack_accu, frametime);
142 MEAN_ACCUMULATE(CS(this), anticheat_speedhack_m5, CS(this).anticheat_speedhack_accu, frametime);
144 CS(this).anticheat_speedhack_accu = 1;
145 CS(this).anticheat_speedhack_lasttime = servertime;
149 void anticheat_spectatecopy(entity this, entity spectatee)
151 // div0_evade -> SPECTATORS
152 this.angles = CS(spectatee).anticheat_div0_evade_v_angle;
155 void anticheat_prethink(entity this)
157 // div0_evade -> SPECTATORS
158 CS(this).anticheat_div0_evade_offset = 0;
161 string anticheat_display(float f, float tmin, float mi, float ma)
166 return strcat(s, ":N");
168 return strcat(s, ":Y");
169 return strcat(s, ":-");
172 void anticheat_report(entity this)
174 if(!autocvar_sv_eventlog)
176 // TODO(divVerent): Use xonstat to acquire good thresholds.
177 GameLogEcho(strcat(":anticheat:_time:", ftos(this.playerid), ":", ftos(servertime - CS(this).anticheat_jointime)));
178 GameLogEcho(strcat(":anticheat:speedhack:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack), 240, 0, 9999))); // Actually this one seems broken.
179 GameLogEcho(strcat(":anticheat:speedhack_m1:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack_m1), 240, 1.01, 1.25)));
180 GameLogEcho(strcat(":anticheat:speedhack_m2:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack_m2), 240, 1.01, 1.25)));
181 GameLogEcho(strcat(":anticheat:speedhack_m3:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack_m3), 240, 1.01, 1.25)));
182 GameLogEcho(strcat(":anticheat:speedhack_m4:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack_m4), 240, 1.01, 1.25)));
183 GameLogEcho(strcat(":anticheat:speedhack_m5:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_speedhack_m5), 240, 1.01, 1.25)));
184 GameLogEcho(strcat(":anticheat:div0_strafebot_old:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_old), 120, 0.15, 0.4)));
185 GameLogEcho(strcat(":anticheat:div0_strafebot_new:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_new), 120, 0.25, 0.8)));
186 GameLogEcho(strcat(":anticheat:div0_evade:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_div0_evade), 120, 0.2, 0.5)));
187 GameLogEcho(strcat(":anticheat:idle_snapaim:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal) - MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise), 120, 0, 9999)));
188 GameLogEcho(strcat(":anticheat:idle_snapaim_signal:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal), 120, 0, 9999)));
189 GameLogEcho(strcat(":anticheat:idle_snapaim_noise:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise), 120, 0, 9999)));
190 GameLogEcho(strcat(":anticheat:idle_snapaim_m2:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m2), 120, 0, 9999)));
191 GameLogEcho(strcat(":anticheat:idle_snapaim_m3:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m3), 120, 0, 9999)));
192 GameLogEcho(strcat(":anticheat:idle_snapaim_m4:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m4), 120, 0, 9999)));
193 GameLogEcho(strcat(":anticheat:idle_snapaim_m7:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m7), 120, 0, 9999)));
194 GameLogEcho(strcat(":anticheat:idle_snapaim_m10:", ftos(this.playerid), ":", anticheat_display(MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m10), 120, 0, 9999)));
197 float anticheat_getvalue(entity this, string id)
200 case "_time": return servertime - CS(this).anticheat_jointime;
201 case "speedhack": return MEAN_EVALUATE(CS(this), anticheat_speedhack);
202 case "speedhack_m1": return MEAN_EVALUATE(CS(this), anticheat_speedhack_m1);
203 case "speedhack_m2": return MEAN_EVALUATE(CS(this), anticheat_speedhack_m2);
204 case "speedhack_m3": return MEAN_EVALUATE(CS(this), anticheat_speedhack_m3);
205 case "speedhack_m4": return MEAN_EVALUATE(CS(this), anticheat_speedhack_m4);
206 case "speedhack_m5": return MEAN_EVALUATE(CS(this), anticheat_speedhack_m5);
207 case "div0_strafebot_old": return MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_old);
208 case "div0_strafebot_new": return MEAN_EVALUATE(CS(this), anticheat_div0_strafebot_new);
209 case "div0_evade": return MEAN_EVALUATE(CS(this), anticheat_div0_evade);
210 case "idle_snapaim": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal) - MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise);
211 case "idle_snapaim_signal": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_signal);
212 case "idle_snapaim_noise": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_noise);
213 case "idle_snapaim_m2": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m2);
214 case "idle_snapaim_m3": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m3);
215 case "idle_snapaim_m4": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m4);
216 case "idle_snapaim_m7": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m7);
217 case "idle_snapaim_m10": return MEAN_EVALUATE(CS(this), anticheat_idle_snapaim_m10);
222 void anticheat_startframe()
224 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
227 void anticheat_fixangle(entity this)
229 CS(this).anticheat_fixangle_endtime = servertime + ANTILAG_LATENCY(this) + 0.2;
232 void anticheat_endframe()
234 FOREACH_CLIENT(it.fixangle, anticheat_fixangle(it));
235 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
238 void anticheat_init(entity this)
240 CS(this).anticheat_speedhack_offset = 0;
241 CS(this).anticheat_jointime = servertime;