-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcrouchboostfix.sp
More file actions
260 lines (218 loc) · 8.75 KB
/
Copy pathcrouchboostfix.sp
File metadata and controls
260 lines (218 loc) · 8.75 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
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#pragma semicolon 1
#pragma newdecls required
public Plugin myinfo = {
name = "crouchboostfix",
version = "2.0.2",
author = "https://github.com/t5mat",
description = "Prevents crouchboosting",
url = "https://github.com/t5mat/crouchboostfix",
};
#define CROUCHBOOST_TIME (0.25)
#define MAX_ENTITIES (4096)
enum struct Engine
{
int m_vecMins;
int m_vecMaxs;
int m_flLaggedMovementValue;
int m_vecAbsOrigin;
int m_vecAbsVelocity;
void Initialize(int entity = -1, const char[] classname = "")
{
static bool start = false;
if (!start) {
start = true;
(this.m_vecMins = FindSendPropInfo("CBaseEntity", "m_vecMins")) == -1 && SetFailState("CBaseEntity::m_vecMins");
(this.m_vecMaxs = FindSendPropInfo("CBaseEntity", "m_vecMaxs")) == -1 && SetFailState("CBaseEntity::m_vecMaxs");
(this.m_flLaggedMovementValue = FindSendPropInfo("CBasePlayer", "m_flLaggedMovementValue")) == -1 && SetFailState("CBasePlayer::m_flLaggedMovementValue");
}
static bool CBaseEntity = false;
if (!CBaseEntity && entity != -1) {
CBaseEntity = true;
(this.m_vecAbsOrigin = FindDataMapInfo(entity, "m_vecAbsOrigin")) == -1 && SetFailState("CBaseEntity::m_vecAbsOrigin");
(this.m_vecAbsVelocity = FindDataMapInfo(entity, "m_vecAbsVelocity")) == -1 && SetFailState("CBaseEntity::m_vecAbsVelocity");
}
}
}
enum struct Client
{
float origin[3];
float velocity[3];
float mins[3];
float maxs[3];
int flags;
float frame;
bool validTouch[MAX_ENTITIES];
float endTouchFrame[MAX_ENTITIES];
bool endTouchDuck[MAX_ENTITIES];
}
bool g_late;
ConVar g_crouchboostfix_enabled;
Engine g_engine;
Client g_clients[MAXPLAYERS + 1];
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
RegPluginLibrary("crouchboostfix");
g_late = late;
return APLRes_Success;
}
public void OnPluginStart()
{
g_engine.Initialize();
g_crouchboostfix_enabled = CreateConVar("crouchboostfix_enabled", "1", "Enable crouchboost prevention", FCVAR_NOTIFY, true, 0.0, true, 1.0);
AutoExecConfig();
HookEntityOutput("trigger_multiple", "OnStartTouch", Hook_EntityOutput);
HookEntityOutput("trigger_multiple", "OnEndTouch", Hook_EntityOutput);
HookEntityOutput("trigger_push", "OnStartTouch", Hook_EntityOutput);
HookEntityOutput("trigger_push", "OnEndTouch", Hook_EntityOutput);
HookEntityOutput("trigger_gravity", "OnStartTouch", Hook_EntityOutput);
HookEntityOutput("trigger_gravity", "OnEndTouch", Hook_EntityOutput);
if (g_late) {
for (int e = 0; e < sizeof(Client::validTouch); ++e) {
if (IsValidEntity(e)) {
char classname[64];
GetEntityClassname(e, classname, sizeof(classname));
OnEntityCreated(e, classname);
}
}
for (int c = 1; c <= MaxClients; ++c) {
if (IsClientInGame(c)) {
OnClientPutInServer(c);
}
}
}
}
public void OnEntityCreated(int entity, const char[] classname)
{
g_engine.Initialize(entity, classname);
bool push = StrEqual(classname, "trigger_push");
if (StrEqual(classname, "trigger_multiple") || push || StrEqual(classname, "trigger_gravity")) {
for (int i = 0; i < sizeof(g_clients); ++i) {
g_clients[i].validTouch[entity] = false;
g_clients[i].endTouchFrame[entity] = -1.0;
}
SDKHook(entity, SDKHook_StartTouch, Hook_TriggerStartTouch);
SDKHook(entity, SDKHook_EndTouchPost, Hook_TriggerEndTouchPost);
if (push) {
SDKHook(entity, SDKHook_Touch, Hook_TriggerPushTouch);
}
}
}
public void OnClientPutInServer(int client)
{
g_clients[client].frame = 0.0;
for (int i = 0; i < sizeof(Client::endTouchFrame); ++i) {
g_clients[client].endTouchFrame[i] = -1.0;
}
SDKHook(client, SDKHook_PreThinkPost, Hook_ClientPreThinkPost);
}
Action Hook_EntityOutput(const char[] output, int caller, int activator, float delay)
{
caller = EntRefToEntIndex(caller);
if (caller == -1 || caller > sizeof(Client::validTouch) - 1) {
return Plugin_Continue;
}
activator = EntRefToEntIndex(activator);
if (activator < 1 || activator > sizeof(g_clients) - 1) {
return Plugin_Continue;
}
if (g_crouchboostfix_enabled.BoolValue && !g_clients[activator].validTouch[caller]) {
return Plugin_Handled;
}
return Plugin_Continue;
}
void Hook_ClientPreThinkPost(int client)
{
GetEntDataVector(client, g_engine.m_vecAbsOrigin, g_clients[client].origin);
GetEntDataVector(client, g_engine.m_vecAbsVelocity, g_clients[client].velocity);
GetEntDataVector(client, g_engine.m_vecMins, g_clients[client].mins);
GetEntDataVector(client, g_engine.m_vecMaxs, g_clients[client].maxs);
g_clients[client].flags = GetEntityFlags(client);
g_clients[client].frame += GetEntDataFloat(client, g_engine.m_flLaggedMovementValue);
}
Action Hook_TriggerStartTouch(int entity, int other)
{
if (other > sizeof(g_clients) - 1) {
return Plugin_Continue;
}
if (g_clients[other].endTouchFrame[entity] != -1.0 && g_clients[other].frame - g_clients[other].endTouchFrame[entity] < CROUCHBOOST_TIME / GetTickInterval()) {
bool startTouchUnduck = false;
if (!g_clients[other].endTouchDuck[entity]) {
// Were we mid-air last tick?
if (!(g_clients[other].flags & FL_ONGROUND)) {
// Did we unduck?
if ((g_clients[other].flags & FL_DUCKING) && !(GetEntityFlags(other) & FL_DUCKING)) {
// Had we not unducked, would we still be not touching the trigger?
float origin[3];
origin = g_clients[other].velocity;
ScaleVector(origin, GetTickInterval() * GetEntDataFloat(other, g_engine.m_flLaggedMovementValue));
AddVectors(origin, g_clients[other].origin, origin);
if (!DoHullEntityIntersect(origin, g_clients[other].mins, g_clients[other].maxs, entity)) {
// This StartTouch was caused by a mid-air unduck
startTouchUnduck = true;
}
}
}
}
// If this StartTouch happened too soon after the last EndTouch, and either:
// - the last EndTouch was caused by a mid-air duck, or
// - this StartTouch was caused by a mid-air unduck
// then this StartTouch is considered "invalid", disable pushing so we don't get boosted again
g_clients[other].validTouch[entity] = !(g_clients[other].endTouchDuck[entity] || startTouchUnduck);
} else {
g_clients[other].validTouch[entity] = true;
}
return Plugin_Continue;
}
void Hook_TriggerEndTouchPost(int entity, int other)
{
if (other > sizeof(g_clients) - 1) {
return;
}
g_clients[other].validTouch[entity] = false;
g_clients[other].endTouchFrame[entity] = g_clients[other].frame;
g_clients[other].endTouchDuck[entity] = false;
// Were we mid-air last tick?
if (!(g_clients[other].flags & FL_ONGROUND)) {
// Did we duck?
if (!(g_clients[other].flags & FL_DUCKING) && (GetEntityFlags(other) & FL_DUCKING)) {
// Had we not ducked, would we still be touching the trigger?
float origin[3];
origin = g_clients[other].velocity;
ScaleVector(origin, GetTickInterval() * GetEntDataFloat(other, g_engine.m_flLaggedMovementValue));
AddVectors(origin, g_clients[other].origin, origin);
if (DoHullEntityIntersect(origin, g_clients[other].mins, g_clients[other].maxs, entity)) {
// This EndTouch was caused by a mid-air duck
g_clients[other].endTouchDuck[entity] = true;
}
}
}
}
Action Hook_TriggerPushTouch(int entity, int other)
{
if (other > sizeof(g_clients) - 1) {
return Plugin_Continue;
}
if (g_crouchboostfix_enabled.BoolValue && !g_clients[other].validTouch[entity]) {
return Plugin_Handled;
}
return Plugin_Continue;
}
bool g_DoHullEntityIntersect_hit;
bool DoHullEntityIntersect(const float origin[3], const float mins[3], const float maxs[3], int entity, int mask = PARTITION_TRIGGER_EDICTS)
{
g_DoHullEntityIntersect_hit = false;
TR_EnumerateEntitiesHull(origin, origin, mins, maxs, mask, Trace_DoHullEntityIntersect, entity);
return g_DoHullEntityIntersect_hit;
}
bool Trace_DoHullEntityIntersect(int entity, any data)
{
if (entity == data) {
TR_ClipCurrentRayToEntity(MASK_ALL, entity);
g_DoHullEntityIntersect_hit = TR_DidHit();
return false;
}
return true;
}