-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathspecrates.sp
More file actions
238 lines (198 loc) · 5.47 KB
/
Copy pathspecrates.sp
File metadata and controls
238 lines (198 loc) · 5.47 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
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#undef REQUIRE_PLUGIN
#include <caster_system>
#define REQUIRE_PLUGIN
enum L4DTeam
{
L4DTeam_Unassigned = 0,
L4DTeam_Spectator = 1,
L4DTeam_Survivor = 2,
L4DTeam_Infected = 3
}
enum StatusRates
{
RatesLimit = 0,
RatesFree = 1,
}
enum struct Player
{
float LastAdjusted;
StatusRates Status;
}
bool
g_bCasterSystem,
g_bLateload;
ConVar
sv_mincmdrate,
sv_maxcmdrate,
sv_minupdaterate,
sv_maxupdaterate,
sv_minrate,
sv_maxrate,
sv_client_min_interp_ratio,
sv_client_max_interp_ratio;
char
g_sNetVars[8][8];
Player
g_Players[MAXPLAYERS + 1];
public Plugin myinfo =
{
name = "Lightweight Spectating",
author = "Visor, lechuga",
description = "Forces low rates on spectators",
version = "1.3",
url = "https://github.com/SirPlease/L4D2-Competitive-Rework"
};
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
CreateNative("SetStatusRates", Native_SetStatusRates);
CreateNative("GetStatusRates", Native_GetStatusRates);
g_bLateload = late;
RegPluginLibrary("specrates");
return APLRes_Success;
}
public void OnAllPluginsLoaded()
{
g_bCasterSystem = LibraryExists("caster_system");
}
public void OnLibraryRemoved(const char[] name)
{
if (StrEqual(name, "caster_system", true))
g_bCasterSystem = false;
}
public void OnLibraryAdded(const char[] name)
{
if (StrEqual(name, "caster_system", true))
g_bCasterSystem = true;
}
public void OnPluginStart()
{
sv_mincmdrate = FindConVar("sv_mincmdrate");
sv_maxcmdrate = FindConVar("sv_maxcmdrate");
sv_minupdaterate = FindConVar("sv_minupdaterate");
sv_maxupdaterate = FindConVar("sv_maxupdaterate");
sv_minrate = FindConVar("sv_minrate");
sv_maxrate = FindConVar("sv_maxrate");
sv_client_min_interp_ratio = FindConVar("sv_client_min_interp_ratio");
sv_client_max_interp_ratio = FindConVar("sv_client_max_interp_ratio");
HookEvent("player_team", OnTeamChange);
if (!g_bLateload)
return;
g_bCasterSystem = LibraryExists("caster_system");
}
public void OnPluginEnd()
{
sv_minupdaterate.SetString(g_sNetVars[2]);
sv_mincmdrate.SetString(g_sNetVars[0]);
}
public void OnConfigsExecuted()
{
sv_mincmdrate.GetString(g_sNetVars[0], 8);
sv_maxcmdrate.GetString(g_sNetVars[1], 8);
sv_minupdaterate.GetString(g_sNetVars[2], 8);
sv_maxupdaterate.GetString(g_sNetVars[3], 8);
sv_minrate.GetString(g_sNetVars[4], 8);
sv_maxrate.GetString(g_sNetVars[5], 8);
sv_client_min_interp_ratio.GetString(g_sNetVars[6], 8);
sv_client_max_interp_ratio.GetString(g_sNetVars[7], 8);
sv_minupdaterate.SetInt(30);
sv_mincmdrate.SetInt(30);
}
public void OnClientPutInServer(int client)
{
g_Players[client].LastAdjusted = 0.0;
g_Players[client].Status = RatesLimit;
}
void OnTeamChange(Event event, const char[] name, bool dontBroadcast)
{
if (event.GetBool("disconnect"))
return;
CreateTimer(10.0, TimerAdjustRates, event.GetInt("userid"), TIMER_FLAG_NO_MAPCHANGE);
}
Action TimerAdjustRates(Handle timer, any userid)
{
int client = GetClientOfUserId(userid);
if (!IsValidClient(client))
return Plugin_Handled;
AdjustRates(client);
return Plugin_Handled;
}
public void OnClientSettingsChanged(int client)
{
AdjustRates(client);
}
void AdjustRates(int client)
{
if (!IsValidClient(client))
return;
if (g_Players[client].LastAdjusted < GetEngineTime() - 1.0)
{
g_Players[client].LastAdjusted = GetEngineTime();
L4DTeam team = L4D_GetClientTeam(client);
if (team == L4DTeam_Survivor || team == L4DTeam_Infected || (g_bCasterSystem && IsClientCaster(client)))
ResetRates(client);
else if (team == L4DTeam_Spectator)
{
if (g_Players[client].Status == RatesLimit)
SetSpectatorRates(client);
else
ResetRates(client);
}
}
}
void SetSpectatorRates(int client)
{
sv_mincmdrate.ReplicateToClient(client, "30");
sv_maxcmdrate.ReplicateToClient(client, "30");
sv_minupdaterate.ReplicateToClient(client, "30");
sv_maxupdaterate.ReplicateToClient(client, "30");
sv_minrate.ReplicateToClient(client, "10000");
sv_maxrate.ReplicateToClient(client, "10000");
SetClientInfo(client, "cl_updaterate", "30");
SetClientInfo(client, "cl_cmdrate", "30");
}
void ResetRates(int client)
{
sv_mincmdrate.ReplicateToClient(client, g_sNetVars[0]);
sv_maxcmdrate.ReplicateToClient(client, g_sNetVars[1]);
sv_minupdaterate.ReplicateToClient(client, g_sNetVars[2]);
sv_maxupdaterate.ReplicateToClient(client, g_sNetVars[3]);
sv_minrate.ReplicateToClient(client, g_sNetVars[4]);
sv_maxrate.ReplicateToClient(client, g_sNetVars[5]);
SetClientInfo(client, "cl_updaterate", g_sNetVars[3]);
SetClientInfo(client, "cl_cmdrate", g_sNetVars[1]);
}
// void SetStatusRates(int client, StatusRates Status);
int Native_SetStatusRates(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
StatusRates status = view_as<StatusRates>(GetNativeCell(2));
g_Players[client].Status = status;
AdjustRates(client);
return 0;
}
// StatusRates GetStatusRates(int client);
any Native_GetStatusRates(Handle plugin, int numParams)
{
int client = GetNativeCell(1);
return g_Players[client].Status;
}
bool IsValidClient(int client)
{
return client > 0 && client <= MaxClients && IsClientInGame(client) && !IsFakeClient(client);
}
/**
* Returns the clients team using L4DTeam.
*
* @param client Player's index.
* @return Current L4DTeam of player.
* @error Invalid client index.
*/
stock L4DTeam L4D_GetClientTeam(int client)
{
int team = GetClientTeam(client);
return view_as<L4DTeam>(team);
}