]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/anticheat.qc
Anticheat improvements.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / anticheat.qc
1 .float anticheat_jointime;
2
3 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
4 {
5         if(weight == 0)
6                 return;
7         if(mean == 0)
8                 e.a *= pow(value, weight);
9         else
10                 e.a += pow(value, mean) * weight;
11         e.c += weight;
12 }
13
14 float mean_evaluate(entity e, .float a, .float c, float mean)
15 {
16         if(e.c == 0)
17                 return 0;
18         if(mean == 0)
19                 return pow(e.a, 1.0 / e.c);
20         else
21                 return pow(e.a / e.c, 1.0 / mean);
22 }
23
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
27
28 .float anticheat_fixangle_endtime;
29
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);
35
36 .vector anticheat_div0_strafebot_movement_prev;
37 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
38
39 .vector anticheat_div0_strafebot_forward_prev;
40 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
41
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);
48
49 .float anticheat_speedhack_offset;
50 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
51 MEAN_DECLARE(anticheat_speedhack, 5);
52
53 float movement_oddity(vector m0, vector m1)
54 {
55         float cosangle = normalize(m0) * normalize(m1);
56         if(cosangle >= 0)
57                 return 0;
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)
60 }
61
62 void anticheat_physics()
63 {
64         float f, wishspeed;
65         vector wishvel;
66
67         // div0_evade -> SPECTATORS
68         makevectors(self.v_angle);
69         if(self.anticheat_div0_evade_offset == 0)
70         {
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);
76         }
77         else
78         {
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);
82         }
83
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;
86
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);
91                 /*
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);
94                 */
95                 MEAN_ACCUMULATE(anticheat_div0_strafebot_new, angle / M_PI, 1);
96
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
100                         // zero anyway.
101                         float dt = max(0.001, frametime) / autocvar_slowmo;
102
103                         float anglespeed = angle / dt;
104                         MEAN_ACCUMULATE(anticheat_idle_snapaim_signal, anglespeed, dt);
105                         MEAN_ACCUMULATE(anticheat_idle_snapaim_noise, anglespeed, dt);
106                 }
107         }
108         self.anticheat_div0_strafebot_forward_prev = v_forward;
109
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;
119         else
120         {
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;
123         }
124
125         // race/CTS: force kbd movement for fairness
126         if(g_race || g_cts)
127         {
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)
135                 {
136                         wishvel_z = 0;
137                         wishspeed = vlen(wishvel);
138                         if(wishvel_x >= 2 * wishvel_y)
139                         {
140                                 // pure X motion
141                                 if(self.movement_x > 0)
142                                         self.movement_x = wishspeed;
143                                 else
144                                         self.movement_x = -wishspeed;
145                                 self.movement_y = 0;
146                         }
147                         else if(wishvel_y >= 2 * wishvel_x)
148                         {
149                                 // pure Y motion
150                                 self.movement_x = 0;
151                                 if(self.movement_y > 0)
152                                         self.movement_y = wishspeed;
153                                 else
154                                         self.movement_y = -wishspeed;
155                         }
156                         else
157                         {
158                                 // diagonal
159                                 if(self.movement_x > 0)
160                                         self.movement_x = M_SQRT1_2 * wishspeed;
161                                 else
162                                         self.movement_x = -M_SQRT1_2 * wishspeed;
163                                 if(self.movement_y > 0)
164                                         self.movement_y = M_SQRT1_2 * wishspeed;
165                                 else
166                                         self.movement_y = -M_SQRT1_2 * wishspeed;
167                         }
168                 }
169         }
170 }
171
172 void anticheat_spectatecopy(entity spectatee)
173 {
174         // div0_evade -> SPECTATORS
175         self.angles = spectatee.anticheat_div0_evade_v_angle;
176 }
177
178 void anticheat_prethink()
179 {
180         // div0_evade -> SPECTATORS
181         self.anticheat_div0_evade_offset = 0;
182 }
183
184 string anticheat_display(float f, float tmin, float mi, float ma)
185 {
186         string s;
187         s = ftos(f);
188         if(f <= mi)
189                 return strcat(s, ":N");
190         if(f >= ma)
191                 return strcat(s, ":Y");
192         return strcat(s, ":-");
193 }
194
195 void anticheat_report()
196 {
197         if(!autocvar_sv_eventlog)
198                 return;
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)));
208 }
209
210 float anticheat_getvalue(string id)
211 {
212         switch(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);
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 }