]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/anticheat.qc
This commit is dedicated to TimePath
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / anticheat.qc
1 #include "anticheat.qh"
2
3 #include "antilag.qh"
4 #include "autocvars.qh"
5 #include "defs.qh"
6 #include "miscfunctions.qh"
7
8
9
10 #include "command/common.qh"
11
12 .float anticheat_jointime;
13
14 .float anticheat_fixangle_endtime;
15
16 float anticheat_div0_evade_evasion_delta;
17 .float anticheat_div0_evade_offset;
18 .vector anticheat_div0_evade_v_angle;
19 .vector anticheat_div0_evade_forward_initial;
20 MEAN_DECLARE(anticheat_div0_evade, 5);
21
22 .vector anticheat_div0_strafebot_movement_prev;
23 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
24
25 .vector anticheat_div0_strafebot_forward_prev;
26 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
27
28 // Snap-aim detection: we track the average angular speed of aiming over time, in "radians per second".
29 // Signal: a high-power mean. Cheaters will have high "signal" here.
30 // Noise: a low-power mean. Active/shivery players will have high "noise" here.
31 // Note one can always artificially add noise - so very high values of both signal and noise need to be checked too.
32 MEAN_DECLARE(anticheat_idle_snapaim_signal, 5);
33 MEAN_DECLARE(anticheat_idle_snapaim_noise, 1);
34
35 // TEMP DEBUG STUFF.
36 MEAN_DECLARE(anticheat_idle_snapaim_m2, 2);
37 MEAN_DECLARE(anticheat_idle_snapaim_m3, 3);
38 MEAN_DECLARE(anticheat_idle_snapaim_m4, 4);
39 MEAN_DECLARE(anticheat_idle_snapaim_m7, 7);
40 MEAN_DECLARE(anticheat_idle_snapaim_m10, 10);
41
42 .float anticheat_speedhack_offset;
43 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
44 MEAN_DECLARE(anticheat_speedhack, 5);
45
46 .float anticheat_speedhack_accu;
47 .float anticheat_speedhack_lasttime;
48 MEAN_DECLARE(anticheat_speedhack_m1, 1);
49 MEAN_DECLARE(anticheat_speedhack_m2, 2);
50 MEAN_DECLARE(anticheat_speedhack_m3, 3);
51 MEAN_DECLARE(anticheat_speedhack_m4, 4);
52 MEAN_DECLARE(anticheat_speedhack_m5, 5);
53
54 float movement_oddity(vector m0, vector m1)
55 {
56         float cosangle = normalize(m0) * normalize(m1);
57         if(cosangle >= 0)
58                 return 0;
59         return 0.5 - 0.5 * cos(cosangle * cosangle * 4 * M_PI);
60                 // returns 0 for: -1, -sqrt(0.5), 0 (angles that commonly happen with kbd)
61 }
62
63 void anticheat_physics(entity this)
64 {
65         float f;
66
67         // div0_evade -> SPECTATORS
68         makevectors(this.v_angle);
69         if(this.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                 this.anticheat_div0_evade_offset = servertime + sys_frametime * (3 * f - 1);
73                 this.anticheat_div0_evade_v_angle = this.v_angle;
74                 this.anticheat_div0_evade_forward_initial = v_forward;
75                 MEAN_ACCUMULATE(this, anticheat_div0_evade, 0, 1);
76         }
77         else
78         {
79                 if(time < this.anticheat_div0_evade_offset)
80                         this.anticheat_div0_evade_v_angle = this.v_angle;
81                 MEAN_ACCUMULATE(this, anticheat_div0_evade, 0.5 - 0.5 * (this.anticheat_div0_evade_forward_initial * v_forward), 1);
82         }
83
84         MEAN_ACCUMULATE(this, anticheat_div0_strafebot_old, movement_oddity(this.movement, this.anticheat_div0_strafebot_movement_prev), 1);
85         this.anticheat_div0_strafebot_movement_prev = this.movement;
86
87         // Note: this actually tries to detect snap-aim.
88         if(vlen(this.anticheat_div0_strafebot_forward_prev) && time > this.anticheat_fixangle_endtime) {
89                 float cosangle = this.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", this.netname, angle * 180 / M_PI, cosangle, time - this.anticheat_fixangle_endtime);
94                 */
95                 MEAN_ACCUMULATE(this, 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(this, anticheat_idle_snapaim_signal, anglespeed, dt);
105                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_noise, anglespeed, dt);
106                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_m2, anglespeed, dt);
107                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_m3, anglespeed, dt);
108                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_m4, anglespeed, dt);
109                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_m7, anglespeed, dt);
110                         MEAN_ACCUMULATE(this, anticheat_idle_snapaim_m10, anglespeed, dt);
111                 }
112         }
113         this.anticheat_div0_strafebot_forward_prev = v_forward;
114
115         // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
116         this.anticheat_speedhack_movetime_frac += frametime;
117         f = floor(this.anticheat_speedhack_movetime_frac);
118         this.anticheat_speedhack_movetime_frac -= f;
119         this.anticheat_speedhack_movetime_count += f;
120         this.anticheat_speedhack_movetime = this.anticheat_speedhack_movetime_frac + this.anticheat_speedhack_movetime_count;
121         f = this.anticheat_speedhack_movetime - servertime;
122         if(this.anticheat_speedhack_offset == 0)
123                 this.anticheat_speedhack_offset = f;
124         else
125         {
126                 MEAN_ACCUMULATE(this, anticheat_speedhack, max(0, f - this.anticheat_speedhack_offset), 1);
127                 this.anticheat_speedhack_offset += (f - this.anticheat_speedhack_offset) * frametime * 0.1;
128         }
129
130         // new generic speedhack detection
131         if (this.anticheat_speedhack_lasttime > 0) {
132                 float dt = servertime - this.anticheat_speedhack_lasttime;
133                 const float falloff = 0.2;
134                 this.anticheat_speedhack_accu *= exp(-dt * falloff);
135                 this.anticheat_speedhack_accu += frametime * falloff;
136                 // NOTE: at cl_netfps x, this actually averages not to 1, but to 1/x * falloff / (1 - exp(-1/x * falloff))
137                 // For 15 netfps (absolute minimum bearable), and 0.2 falloff, this is: 1.0067
138                 this.anticheat_speedhack_lasttime = servertime;
139                 MEAN_ACCUMULATE(this, anticheat_speedhack_m1, this.anticheat_speedhack_accu, frametime);
140                 MEAN_ACCUMULATE(this, anticheat_speedhack_m2, this.anticheat_speedhack_accu, frametime);
141                 MEAN_ACCUMULATE(this, anticheat_speedhack_m3, this.anticheat_speedhack_accu, frametime);
142                 MEAN_ACCUMULATE(this, anticheat_speedhack_m4, this.anticheat_speedhack_accu, frametime);
143                 MEAN_ACCUMULATE(this, anticheat_speedhack_m5, this.anticheat_speedhack_accu, frametime);
144         } else {
145                 this.anticheat_speedhack_accu = 1;
146                 this.anticheat_speedhack_lasttime = servertime;
147         }
148 }
149
150 void anticheat_spectatecopy(entity spectatee)
151 {SELFPARAM();
152         // div0_evade -> SPECTATORS
153         self.angles = spectatee.anticheat_div0_evade_v_angle;
154 }
155
156 void anticheat_prethink()
157 {SELFPARAM();
158         // div0_evade -> SPECTATORS
159         self.anticheat_div0_evade_offset = 0;
160 }
161
162 string anticheat_display(float f, float tmin, float mi, float ma)
163 {
164         string s;
165         s = ftos(f);
166         if(f <= mi)
167                 return strcat(s, ":N");
168         if(f >= ma)
169                 return strcat(s, ":Y");
170         return strcat(s, ":-");
171 }
172
173 void anticheat_report()
174 {SELFPARAM();
175         if(!autocvar_sv_eventlog)
176                 return;
177         // TODO(divVerent): Use xonstat to acquire good thresholds.
178         GameLogEcho(strcat(":anticheat:_time:", ftos(self.playerid), ":", ftos(servertime - self.anticheat_jointime)));
179         GameLogEcho(strcat(":anticheat:speedhack:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack), 240, 0, 9999))); // Actually this one seems broken.
180         GameLogEcho(strcat(":anticheat:speedhack_m1:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack_m1), 240, 1.01, 1.25)));
181         GameLogEcho(strcat(":anticheat:speedhack_m2:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack_m2), 240, 1.01, 1.25)));
182         GameLogEcho(strcat(":anticheat:speedhack_m3:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack_m3), 240, 1.01, 1.25)));
183         GameLogEcho(strcat(":anticheat:speedhack_m4:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack_m4), 240, 1.01, 1.25)));
184         GameLogEcho(strcat(":anticheat:speedhack_m5:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_speedhack_m5), 240, 1.01, 1.25)));
185         GameLogEcho(strcat(":anticheat:div0_strafebot_old:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_div0_strafebot_old), 120, 0.15, 0.4)));
186         GameLogEcho(strcat(":anticheat:div0_strafebot_new:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_div0_strafebot_new), 120, 0.25, 0.8)));
187         GameLogEcho(strcat(":anticheat:div0_evade:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_div0_evade), 120, 0.2, 0.5)));
188         GameLogEcho(strcat(":anticheat:idle_snapaim:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_signal) - MEAN_EVALUATE(self, anticheat_idle_snapaim_noise), 120, 0, 9999)));
189         GameLogEcho(strcat(":anticheat:idle_snapaim_signal:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_signal), 120, 0, 9999)));
190         GameLogEcho(strcat(":anticheat:idle_snapaim_noise:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_noise), 120, 0, 9999)));
191         GameLogEcho(strcat(":anticheat:idle_snapaim_m2:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_m2), 120, 0, 9999)));
192         GameLogEcho(strcat(":anticheat:idle_snapaim_m3:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_m3), 120, 0, 9999)));
193         GameLogEcho(strcat(":anticheat:idle_snapaim_m4:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_m4), 120, 0, 9999)));
194         GameLogEcho(strcat(":anticheat:idle_snapaim_m7:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_m7), 120, 0, 9999)));
195         GameLogEcho(strcat(":anticheat:idle_snapaim_m10:", ftos(self.playerid), ":", anticheat_display(MEAN_EVALUATE(self, anticheat_idle_snapaim_m10), 120, 0, 9999)));
196 }
197
198 float anticheat_getvalue(string id)
199 {SELFPARAM();
200         switch(id) {
201                 case "_time": return servertime - self.anticheat_jointime;
202                 case "speedhack": return MEAN_EVALUATE(self, anticheat_speedhack);
203                 case "speedhack_m1": return MEAN_EVALUATE(self, anticheat_speedhack_m1);
204                 case "speedhack_m2": return MEAN_EVALUATE(self, anticheat_speedhack_m2);
205                 case "speedhack_m3": return MEAN_EVALUATE(self, anticheat_speedhack_m3);
206                 case "speedhack_m4": return MEAN_EVALUATE(self, anticheat_speedhack_m4);
207                 case "speedhack_m5": return MEAN_EVALUATE(self, anticheat_speedhack_m5);
208                 case "div0_strafebot_old": return MEAN_EVALUATE(self, anticheat_div0_strafebot_old);
209                 case "div0_strafebot_new": return MEAN_EVALUATE(self, anticheat_div0_strafebot_new);
210                 case "div0_evade": return MEAN_EVALUATE(self, anticheat_div0_evade);
211                 case "idle_snapaim": return MEAN_EVALUATE(self, anticheat_idle_snapaim_signal) - MEAN_EVALUATE(self, anticheat_idle_snapaim_noise);
212                 case "idle_snapaim_signal": return MEAN_EVALUATE(self, anticheat_idle_snapaim_signal);
213                 case "idle_snapaim_noise": return MEAN_EVALUATE(self, anticheat_idle_snapaim_noise);
214                 case "idle_snapaim_m2": return MEAN_EVALUATE(self, anticheat_idle_snapaim_m2);
215                 case "idle_snapaim_m3": return MEAN_EVALUATE(self, anticheat_idle_snapaim_m3);
216                 case "idle_snapaim_m4": return MEAN_EVALUATE(self, anticheat_idle_snapaim_m4);
217                 case "idle_snapaim_m7": return MEAN_EVALUATE(self, anticheat_idle_snapaim_m7);
218                 case "idle_snapaim_m10": return MEAN_EVALUATE(self, anticheat_idle_snapaim_m10);
219         }
220         return -1;
221 }
222
223 void anticheat_startframe()
224 {
225         anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
226 }
227
228 void anticheat_fixangle()
229 {SELFPARAM();
230         self.anticheat_fixangle_endtime = servertime + ANTILAG_LATENCY(self) + 0.2;
231 }
232
233 void anticheat_endframe()
234 {SELFPARAM();
235         entity e;
236         FOR_EACH_CLIENT(e)
237                 if (e.fixangle) {
238                         WITH(entity, self, e, anticheat_fixangle());
239                 }
240         anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
241 }
242
243 void anticheat_init()
244 {SELFPARAM();
245         self.anticheat_speedhack_offset = 0;
246         self.anticheat_jointime = servertime;
247 }
248
249 void anticheat_shutdown()
250 {
251 }