1 .float anticheat_jointime;
3 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
8 e.a *= pow(value, weight);
10 e.a += pow(value, mean) * weight;
14 float mean_evaluate(entity e, .float a, .float c, float mean)
19 return pow(e.a, 1.0 / e.c);
21 return pow(e.a / e.c, 1.0 / mean);
24 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
25 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
26 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
28 .float anticheat_fixangle_endtime;
30 float anticheat_div0_evade_evasion_delta;
31 .float anticheat_div0_evade_offset;
32 .vector anticheat_div0_evade_v_angle;
33 .vector anticheat_div0_evade_forward_initial;
34 MEAN_DECLARE(anticheat_div0_evade, 5);
36 .vector anticheat_div0_strafebot_movement_prev;
37 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
39 .vector anticheat_div0_strafebot_forward_prev;
40 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
42 // Snap-aim detection: we track the average angular speed of aiming over time, in "radians per second".
43 // Signal: a high-power mean. Cheaters will have high "signal" here.
44 // Noise: a low-power mean. Active/shivery players will have high "noise" here.
45 // Note one can always artificially add noise - so very high values of both signal and noise need to be checked too.
46 MEAN_DECLARE(anticheat_idle_snapaim_signal, 5);
47 MEAN_DECLARE(anticheat_idle_snapaim_noise, 1);
49 .float anticheat_speedhack_offset;
50 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
51 MEAN_DECLARE(anticheat_speedhack, 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()
67 // div0_evade -> SPECTATORS
68 makevectors(self.v_angle);
69 if(self.anticheat_div0_evade_offset == 0)
71 f = fabs(anticheat_div0_evade_evasion_delta - floor(anticheat_div0_evade_evasion_delta) - 0.5) * 2; // triangle function
72 self.anticheat_div0_evade_offset = time + sys_frametime * (3 * f - 1);
73 self.anticheat_div0_evade_v_angle = self.v_angle;
74 self.anticheat_div0_evade_forward_initial = v_forward;
75 MEAN_ACCUMULATE(anticheat_div0_evade, 0, 1);
79 if(time < self.anticheat_div0_evade_offset)
80 self.anticheat_div0_evade_v_angle = self.v_angle;
81 MEAN_ACCUMULATE(anticheat_div0_evade, 0.5 - 0.5 * (self.anticheat_div0_evade_forward_initial * v_forward), 1);
84 MEAN_ACCUMULATE(anticheat_div0_strafebot_old, movement_oddity(self.movement, self.anticheat_div0_strafebot_movement_prev), 1);
85 self.anticheat_div0_strafebot_movement_prev = self.movement;
87 // Note: this actually tries to detect snap-aim.
88 if(vlen(self.anticheat_div0_strafebot_forward_prev) && time > self.anticheat_fixangle_endtime) {
89 float cosangle = self.anticheat_div0_strafebot_forward_prev * v_forward;
90 float angle = cosangle < -1 ? M_PI : cosangle > 1 ? 0 : acos(cosangle);
92 if (angle >= 10 * M_PI / 180)
93 printf("SNAP %s: %f for %f, %f since fixangle\n", self.netname, angle * 180 / M_PI, cosangle, time - self.anticheat_fixangle_endtime);
95 MEAN_ACCUMULATE(anticheat_div0_strafebot_new, angle / M_PI, 1);
97 if (autocvar_slowmo > 0) {
98 // Technically this is a NOP, as the engine should be ensuring
99 // this in the first place. Let's guard against dividing by
101 float dt = max(0.001, frametime) / autocvar_slowmo;
103 float anglespeed = angle / dt;
104 MEAN_ACCUMULATE(anticheat_idle_snapaim_signal, anglespeed, dt);
105 MEAN_ACCUMULATE(anticheat_idle_snapaim_noise, anglespeed, dt);
108 self.anticheat_div0_strafebot_forward_prev = v_forward;
110 // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
111 self.anticheat_speedhack_movetime_frac += frametime;
112 f = floor(self.anticheat_speedhack_movetime_frac);
113 self.anticheat_speedhack_movetime_frac -= f;
114 self.anticheat_speedhack_movetime_count += f;
115 self.anticheat_speedhack_movetime = self.anticheat_speedhack_movetime_frac + self.anticheat_speedhack_movetime_count;
116 f = self.anticheat_speedhack_movetime - servertime;
117 if(self.anticheat_speedhack_offset == 0)
118 self.anticheat_speedhack_offset = f;
121 MEAN_ACCUMULATE(anticheat_speedhack, max(0, f - self.anticheat_speedhack_offset), 1);
122 self.anticheat_speedhack_offset += (f - self.anticheat_speedhack_offset) * frametime * 0.1;
125 // race/CTS: force kbd movement for fairness
128 // if record times matter
129 // ensure nothing EVIL is being done (i.e. div0_evade)
130 // this hinders joystick users though
131 // but it still gives SOME analog control
132 wishvel_x = fabs(self.movement_x);
133 wishvel_y = fabs(self.movement_y);
134 if(wishvel_x != 0 && wishvel_y != 0 && wishvel_x != wishvel_y)
137 wishspeed = vlen(wishvel);
138 if(wishvel_x >= 2 * wishvel_y)
141 if(self.movement_x > 0)
142 self.movement_x = wishspeed;
144 self.movement_x = -wishspeed;
147 else if(wishvel_y >= 2 * wishvel_x)
151 if(self.movement_y > 0)
152 self.movement_y = wishspeed;
154 self.movement_y = -wishspeed;
159 if(self.movement_x > 0)
160 self.movement_x = M_SQRT1_2 * wishspeed;
162 self.movement_x = -M_SQRT1_2 * wishspeed;
163 if(self.movement_y > 0)
164 self.movement_y = M_SQRT1_2 * wishspeed;
166 self.movement_y = -M_SQRT1_2 * wishspeed;
172 void anticheat_spectatecopy(entity spectatee)
174 // div0_evade -> SPECTATORS
175 self.angles = spectatee.anticheat_div0_evade_v_angle;
178 void anticheat_prethink()
180 // div0_evade -> SPECTATORS
181 self.anticheat_div0_evade_offset = 0;
184 string anticheat_display(float f, float tmin, float mi, float ma)
189 return strcat(s, ":N");
191 return strcat(s, ":Y");
192 return strcat(s, ":-");
195 void anticheat_report()
197 if(!autocvar_sv_eventlog)
199 // TODO(divVerent): Use xonstat to acquire good thresholds.
200 GameLogEcho(strcat(":anticheat:_time:", ftos(self.playerid), ":", ftos(servertime - self.anticheat_jointime)));
201 GameLogEcho(strcat(":anticheat:speedhack:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_speedhack), 240, 0.1, 0.15)));
202 GameLogEcho(strcat(":anticheat:div0_strafebot_old:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_strafebot_old), 120, 0.3, 0.4)));
203 GameLogEcho(strcat(":anticheat:div0_strafebot_new:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_strafebot_new), 120, 0.3, 0.4)));
204 GameLogEcho(strcat(":anticheat:div0_evade:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_div0_evade), 120, 0.1, 0.2)));
205 GameLogEcho(strcat(":anticheat:idle_snapaim:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_signal) - MEAN_EVALUATE(anticheat_idle_snapaim_noise), 120, 0.1, 0.2)));
206 GameLogEcho(strcat(":anticheat:idle_snapaim_signal:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_signal), 120, 0.1, 0.2)));
207 GameLogEcho(strcat(":anticheat:idle_snapaim_noise:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(anticheat_idle_snapaim_noise), 120, 0.1, 0.2)));
210 float anticheat_getvalue(string id)
213 case "_time": return servertime - self.anticheat_jointime;
214 case "speedhack": return MEAN_EVALUATE(anticheat_speedhack);
215 case "div0_strafebot_old": return MEAN_EVALUATE(anticheat_div0_strafebot_old);
216 case "div0_strafebot_new": return MEAN_EVALUATE(anticheat_div0_strafebot_new);
217 case "div0_evade": return MEAN_EVALUATE(anticheat_div0_evade);
218 case "idle_snapaim": return MEAN_EVALUATE(anticheat_idle_snapaim_signal) - MEAN_EVALUATE(anticheat_idle_snapaim_noise);
219 case "idle_snapaim_signal": return MEAN_EVALUATE(anticheat_idle_snapaim_signal);
220 case "idle_snapaim_noise": return MEAN_EVALUATE(anticheat_idle_snapaim_noise);
225 void anticheat_startframe()
227 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
230 void anticheat_fixangle()
232 self.anticheat_fixangle_endtime = servertime + ANTILAG_LATENCY(self) + 0.2;
235 void anticheat_endframe()
237 entity oldself = self;
238 FOR_EACH_CLIENT(self)
240 anticheat_fixangle();
242 anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
245 void anticheat_init()
247 self.anticheat_speedhack_offset = 0;
248 self.anticheat_jointime = servertime;
251 void anticheat_shutdown()