- rename sys_ticrate global to sys_frametime, and premultiply it with slowmo (fixes...
[divverent/nexuiz.git] / data / qcsrc / server / anticheat.qc
1 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
2 {
3         if(weight == 0)
4                 return;
5         if(mean == 0)
6                 e.a *= pow(value, weight);
7         else
8                 e.a += pow(value, mean) * weight;
9         e.c += weight;
10 }
11
12 float mean_evaluate(entity e, .float a, .float c, float mean)
13 {
14         if(e.c == 0)
15                 return 0;
16         if(mean == 0)
17                 return pow(e.a, 1.0 / e.c);
18         else
19                 return pow(e.a / e.c, 1.0 / mean);
20 }
21
22 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
23 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
24 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
25
26 float anticheat_div0_evade_evasion_delta;
27 .float anticheat_div0_evade_offset;
28 .vector anticheat_div0_evade_v_angle;
29 .vector anticheat_div0_evade_forward_initial;
30 MEAN_DECLARE(anticheat_div0_evade, 5);
31
32 .vector anticheat_div0_strafebot_movement_prev;
33 MEAN_DECLARE(anticheat_div0_strafebot_old, 5);
34
35 .vector anticheat_div0_strafebot_forward_prev;
36 MEAN_DECLARE(anticheat_div0_strafebot_new, 5);
37
38 .float anticheat_speedhack_offset;
39 .float anticheat_speedhack_movetime, anticheat_speedhack_movetime_count, anticheat_speedhack_movetime_frac;
40 MEAN_DECLARE(anticheat_speedhack, 5);
41
42 float movement_oddity(vector m0, vector m1)
43 {
44         float cosangle = normalize(m0) * normalize(m1);
45         if(cosangle >= 0)
46                 return 0;
47         return 0.5 - 0.5 * cos(cosangle * cosangle * 4 * M_PI);
48                 // returns 0 for: -1, -sqrt(0.5), 0 (angles that commonly happen with kbd)
49 }
50
51 void anticheat_physics()
52 {
53         float f, wishspeed;
54         vector wishvel;
55
56         // div0_evade -> SPECTATORS
57         makevectors(self.v_angle);
58         if(self.anticheat_div0_evade_offset == 0)
59         {
60                 f = fabs(anticheat_div0_evade_evasion_delta - floor(anticheat_div0_evade_evasion_delta) - 0.5) * 2; // triangle function
61                 self.anticheat_div0_evade_offset = time + sys_frametime * (3 * f - 1);
62                 self.anticheat_div0_evade_v_angle = self.v_angle;
63                 self.anticheat_div0_evade_forward_initial = v_forward;
64                 MEAN_ACCUMULATE(anticheat_div0_evade, 0, 1);
65         }
66         else
67         {
68                 if(time < self.anticheat_div0_evade_offset)
69                         self.anticheat_div0_evade_v_angle = self.v_angle;
70                 MEAN_ACCUMULATE(anticheat_div0_evade, 1 - (self.anticheat_div0_evade_forward_initial * v_forward), 1);
71         }
72
73         MEAN_ACCUMULATE(anticheat_div0_strafebot_old, movement_oddity(self.movement, self.anticheat_div0_strafebot_movement_prev), max(0, 0.05 - frametime));
74         self.anticheat_div0_strafebot_movement_prev = self.movement;
75
76         if(vlen(self.anticheat_div0_strafebot_forward_prev))
77                 MEAN_ACCUMULATE(anticheat_div0_strafebot_new, 1 - (self.anticheat_div0_strafebot_forward_prev * v_forward), max(0, 0.05 - frametime));
78         self.anticheat_div0_strafebot_forward_prev = v_forward;
79
80         // generic speedhack detection: correlate anticheat_speedhack_movetime (UPDATED BEFORE THIS) and server time
81         self.anticheat_speedhack_movetime_frac += frametime;
82         f = floor(self.anticheat_speedhack_movetime_frac);
83         self.anticheat_speedhack_movetime_frac -= f;
84         self.anticheat_speedhack_movetime_count += f;
85         self.anticheat_speedhack_movetime = self.anticheat_speedhack_movetime_frac + self.anticheat_speedhack_movetime_count;
86         f = self.anticheat_speedhack_movetime - servertime;
87         if(self.anticheat_speedhack_offset == 0)
88                 self.anticheat_speedhack_offset = f;
89         else
90         {
91                 MEAN_ACCUMULATE(anticheat_speedhack, fabs(f - self.anticheat_speedhack_offset), 1);
92                 self.anticheat_speedhack_offset += (f - self.anticheat_speedhack_offset) * frametime * 0.1;
93         }
94
95         // race/CTS: force kbd movement for fairness
96         if(g_race || g_cts)
97         {
98                 // if record times matter
99                 // ensure nothing EVIL is being done (i.e. div0_evade)
100                 // this hinders joystick users though
101                 // but it still gives SOME analog control
102                 wishvel_x = fabs(self.movement_x);
103                 wishvel_y = fabs(self.movement_y);
104                 if(wishvel_x != 0 && wishvel_y != 0 && wishvel_x != wishvel_y)
105                 {
106                         wishvel_z = 0;
107                         wishspeed = vlen(wishvel);
108                         if(wishvel_x >= 2 * wishvel_y)
109                         {
110                                 // pure X motion
111                                 if(self.movement_x > 0)
112                                         self.movement_x = wishspeed;
113                                 else
114                                         self.movement_x = -wishspeed;
115                                 self.movement_y = 0;
116                         }
117                         else if(wishvel_y >= 2 * wishvel_x)
118                         {
119                                 // pure Y motion
120                                 self.movement_x = 0;
121                                 if(self.movement_y > 0)
122                                         self.movement_y = wishspeed;
123                                 else
124                                         self.movement_y = -wishspeed;
125                         }
126                         else
127                         {
128                                 // diagonal
129                                 if(self.movement_x > 0)
130                                         self.movement_x = M_SQRT1_2 * wishspeed;
131                                 else
132                                         self.movement_x = -M_SQRT1_2 * wishspeed;
133                                 if(self.movement_y > 0)
134                                         self.movement_y = M_SQRT1_2 * wishspeed;
135                                 else
136                                         self.movement_y = -M_SQRT1_2 * wishspeed;
137                         }
138                 }
139         }
140 }
141
142 void anticheat_spectatecopy(entity spectatee)
143 {
144         // div0_evade -> SPECTATORS
145         self.angles = spectatee.anticheat_div0_evade_v_angle;
146 }
147
148 void anticheat_prethink()
149 {
150         // div0_evade -> SPECTATORS
151         self.anticheat_div0_evade_offset = 0;
152 }
153
154 void anticheat_report()
155 {
156         if(!cvar("sv_eventlog"))
157                 return;
158         GameLogEcho(strcat(":anticheat:speedhack:", ftos(self.playerid), ":", ftos(MEAN_EVALUATE(anticheat_speedhack))));
159         GameLogEcho(strcat(":anticheat:div0_strafebot_old:", ftos(self.playerid), ":", ftos(MEAN_EVALUATE(anticheat_div0_strafebot_old))));
160         GameLogEcho(strcat(":anticheat:div0_strafebot_new:", ftos(self.playerid), ":", ftos(MEAN_EVALUATE(anticheat_div0_strafebot_new))));
161         GameLogEcho(strcat(":anticheat:div0_evade:", ftos(self.playerid), ":", ftos(MEAN_EVALUATE(anticheat_div0_evade))));
162 }
163
164 void anticheat_serverframe()
165 {
166         anticheat_div0_evade_evasion_delta += frametime * (0.5 + random());
167 }
168
169 void anticheat_init()
170 {
171         self.anticheat_speedhack_offset = 0;
172 }
173
174 void anticheat_shutdown()
175 {
176 }