This repository was archived by the owner on Jan 30, 2021. It is now read-only.
forked from davisempireinc/anti-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanti-cheat.inc
More file actions
366 lines (324 loc) · 8.38 KB
/
Copy pathanti-cheat.inc
File metadata and controls
366 lines (324 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Make functions that sets the values both server side and client side.
Please define the ID and string equivilent of the cheat. E.g
#define AC_CHEAT_HEALTH_ARMOR 0
and the string inside the array, the index of the specified cheat must be the same ID as the defined preprocessor ID.
*/
// Defining cheat ID's
#define AC_CHEAT_FLOOD_1 1
#define AC_CHEAT_FLYING 2
#define AC_CHEAT_SPEED 3
#define FLOOD_LIMIT 27
forward OnAnticheatDetect(playerid, cheatid);
new AC_timer; // Holds the timer for the Anti-cheat checking function.
new AC_names[][] = // Holds names for the Anti-cheat.
{
"Bullet flood hack",
"Flying hack",
"Speed hack"
};
#pragma unused AC_names
// Server-sided variables.
new IsDisableAnticheat[MAX_PLAYERS];
new IsDetected[MAX_PLAYERS];
new WeaponShot [MAX_PLAYERS];
new FloodTimer [MAX_PLAYERS];
new PlayerMoney [MAX_PLAYERS]; // Server-sided player money.
new PlayerWeapons[MAX_PLAYERS][12][2]; // Server-sided player weapons
new Float:PlayerHealth[MAX_PLAYERS]; // Server-sided player weapons
new Float:PlayerArmor[MAX_PLAYERS]; // Server-sided player health.
new Float:PlayerPosition[MAX_PLAYERS][3]; // Server-sided player position.
GetWeaponSlot(weaponid)
{
new slot;
switch(weaponid)
{
case 0,1: slot = 0;
case 2 .. 9: slot = 1;
case 10 .. 15: slot = 10;
case 16 .. 18, 39: slot = 8;
case 22 .. 24: slot =2;
case 25 .. 27: slot = 3;
case 28, 29, 32: slot = 4;
case 30, 31: slot = 5;
case 33, 34: slot = 6;
case 35 .. 38: slot = 7;
case 40: slot = 12;
case 41 .. 43: slot = 9;
case 44 .. 46: slot = 11;
}
return slot;
}
forward AC_OnGameModeInit();
public AC_OnGameModeInit()
{
AC_timer = SetTimer("OnAnticheatCheck", 1000, true); // Starting the timer.
return 1;
}
forward AC_OnGameModeExit();
public AC_OnGameModeExit()
{
KillTimer(AC_timer); // Killing the timer, 'cause why not?
return 1;
}
forward OnAnticheatCheck();
public OnAnticheatCheck()
{
new Float:X, Float:Y, Float:Z, Float:minZ; // Used to get client-side position.
new Float:velX, Float:velY, Float:velZ; // Used to get the speed of the player.
for(new playerid = 0, count = GetPlayerPoolSize(); playerid <= count; playerid++)
{
if(IsDetected[playerid] == 0)
{
if(IsDisableAnticheat[playerid] != 1)
{
GetPlayerPos(playerid, X, Y, Z);
if(GetPlayerVehicleID(playerid) == 0)
{
CA_FindZ_For2DCoord(X, Y, minZ);
GetPlayerVelocity(playerid, velX, velY, velZ);
if(Z - minZ >= 5.0 && GetPlayerInterior(playerid) == 0 && (velX * velX + velY * velY) > 5.0 && velZ >= 0.0)
{
OnAnticheatDetect (playerid, AC_CHEAT_FLYING);
IsDetected[playerid] = 1;
SetTimerEx("UndetectPlayer", 10000, false, "i", playerid);
}
if((velX * velX + velY * velY) >= 20.0)
{
OnAnticheatDetect (playerid, AC_CHEAT_SPEED);
IsDetected[playerid] = 1;
SetTimerEx("UndetectPlayer", 10000, false, "i", playerid);
}
}
}
}
}
return 1;
}
forward AC_OnPlayerUpdate(playerid);
public AC_OnPlayerUpdate(playerid)
{
// Checking happens here.
// Calls the OnAnticheatDetect() with the defined preprocessor ID for the cheat.
new weapon, ammo;// Used to get weapon data.
if(IsDisableAnticheat[playerid] != 1)
{
GivePlayerMoney(playerid, PlayerMoney[playerid] - GetPlayerMoney(playerid));
SetPlayerHealth(playerid, PlayerHealth[playerid]);
SetPlayerArmour(playerid, PlayerArmor[playerid]);
for(new j = 0; j < 12; j++)
{
GetPlayerWeaponData(playerid, j, weapon, ammo); // Gets the client-sided weapon.
if(PlayerWeapons[playerid][j][0] != weapon) // If the client-side and server-side doesn't match...
{
if(PlayerWeapons[playerid][j][0] == 0)
{
RemovePlayerWeapon(playerid, weapon);
}
else
{
GivePlayerWeapon(playerid, PlayerWeapons[playerid][j][0], PlayerWeapons[playerid][j][1]);
}
}
else if(PlayerWeapons[playerid][j][1] > ammo + 4 || PlayerWeapons[playerid][j][1] < ammo - 4)
{
SetPlayerAmmo(playerid, j, PlayerWeapons[playerid][j][1]);
}
}
}
return 1;
}
forward UndetectPlayer(playerid);
public UndetectPlayer(playerid)
{
IsDetected[playerid] = 0;
return 1;
}
/*
Server events.
You're able to create hooks for any event listener. E.g: AC_OnPlayerConnect.
*/
forward AC_OnPlayerConnect(playerid);
public AC_OnPlayerConnect(playerid)
{
// Resetting the Server-sided weapons.
for(new slot = 0; slot < 12; slot++)
{
PlayerWeapons[playerid][slot][0] = 0;
PlayerWeapons[playerid][slot][1] = 0;
}
PlayerMoney[playerid] = 0;
PlayerHealth[playerid] = 100.0;
PlayerArmor[playerid] = 0.0;
IsDetected[playerid] = 0;
TogglePlayerAnticheat(playerid, false);
return 1;
}
forward AC_OnPlayerDisconnect (playerid);
public AC_OnPlayerDisconnect (playerid)
{
if (WeaponShot [playerid])
{
WeaponShot [playerid] = 0;
KillTimer (FloodTimer [playerid]);
}
IsDetected[playerid] = 0;
return 1;
}
forward AC_OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid, bodypart);
public AC_OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid, bodypart)
{
new Float:armor, Float:health;
GetPlayerArmour(playerid, armor);
GetPlayerHealth(playerid, health);
if(weaponid == WEAPON_COLLISION)
{
PlayerHealth[playerid] = health - amount;
}
else
{
if(armor < amount)
{
SetPlayerArmour(playerid, 0.0);
PlayerArmor[playerid] = 0;
new Float:hamount = amount - armor;
PlayerHealth[playerid] = health - hamount;
}
else SetPlayerArmour(playerid, armor - amount);
}
return 1;
}
forward AC_OnPlayerWeaponShot(playerid, weaponid);
public AC_OnPlayerWeaponShot(playerid, weaponid)
{
new slot = GetWeaponSlot(weaponid);
PlayerWeapons[playerid][slot][1]--;
if(PlayerWeapons[playerid][slot][1] == 0)
{
RemovePlayerWeapon(playerid, weaponid);
}
if (weaponid != 38)
{
if (WeaponShot [playerid] >= 1)
{
KillTimer (FloodTimer [playerid]);
FloodTimer [playerid] = SetTimerEx ("ResetFloodCheck", 40, false, "i", playerid);
WeaponShot [playerid]++;
}
else if (!WeaponShot [playerid])
{
WeaponShot [playerid] = 1;
FloodTimer [playerid] = SetTimerEx ("ResetFloodCheck", 40, false, "i", playerid);
}
if (WeaponShot [playerid] >= FLOOD_LIMIT)
{
OnAnticheatDetect (playerid, AC_CHEAT_FLOOD_1);
}
}
return 1;
}
forward ResetFloodCheck (playerid);
public ResetFloodCheck (playerid)
{
WeaponShot[playerid] = 0;
return 1;
}
forward AC_OnPlayerDeath(playerid);
public AC_OnPlayerDeath(playerid)
{
TogglePlayerAnticheat(playerid, false);
return 1;
}
forward AC_OnPlayerSpawn(playerid);
public AC_OnPlayerSpawn(playerid)
{
SetPlayerHealthEx(playerid, 100.0);
SetPlayerArmorEx(playerid, 0.0);
ResetPlayerWeapons(playerid);
IsDetected[playerid] = 0;
return 1;
}
/*
Functions needed for each Anti-cheat.
*/
// General
stock TogglePlayerAnticheat(playerid, bool:toggle)
{
if(toggle) IsDisableAnticheat[playerid] = 0;
else IsDisableAnticheat[playerid] = 1;
IsDetected[playerid] = 0;
return 1;
}
// Money
stock GivePlayerMoneyEx (playerid, money)
{
PlayerMoney[playerid] += money;
return 1;
}
stock SetPlayerMoney(playerid, money)
{
PlayerMoney[playerid] = money;
return 1;
}
// Weapons
stock GivePlayerWeaponEx(playerid, weapon, ammo)
{
new slot = GetWeaponSlot(weapon);
if(PlayerWeapons[playerid][slot][0] == weapon) PlayerWeapons[playerid][slot][1] += ammo;
else
{
PlayerWeapons[playerid][slot][0] = weapon;
PlayerWeapons[playerid][slot][1] = ammo;
}
return 1;
}
stock ResetPlayerWeaponsEx(playerid)
{
for(new slot = 0; slot < 12; slot++)
{
PlayerWeapons[playerid][slot][0] = 0;
PlayerWeapons[playerid][slot][1] = 0;
}
return 1;
}
RemovePlayerWeapon(playerid, weapon)
{
new weapons[13][2];
for (new i = 0; i <= 12; i++)
{
GetPlayerWeaponData(playerid, i, weapons[i][0], weapons[i][1]);
}
ResetPlayerWeapons(playerid);
for(new i = 0; i <= 12; i++)
{
if(weapons[i][0] != weapon) GivePlayerWeapon(playerid, weapons[i][0], weapons[i][1]);
}
return 1;
}
stock RemovePlayerWeaponEx(playerid, weapon)
{
new slot = GetWeaponSlot(weapon);
PlayerWeapons[playerid][slot][0] = 0;
PlayerWeapons[playerid][slot][1] = 0;
return 1;
}
// Health/Armor
stock SetPlayerHealthEx(playerid, Float:health)
{
PlayerHealth[playerid] = health;
return 1;
}
stock SetPlayerArmorEx(playerid, Float:armor)
{
PlayerArmor[playerid] = armor;
return 1;
}
// Position
stock SetPlayerPosEx(playerid, Float:X, Float:Y, Float:Z)
{
PlayerPosition[playerid][0] = X;
PlayerPosition[playerid][1] = Y;
PlayerPosition[playerid][2] = Z;
SetPlayerPos(playerid, X, Y, Z);
return 1;
}