-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwarden.sp
More file actions
345 lines (277 loc) · 7.88 KB
/
warden.sp
File metadata and controls
345 lines (277 loc) · 7.88 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
#include <sourcemod>
#include <cstrike>
//#include <emitsoundany>
#include <cnf_core>
#include <warden>
#pragma newdecls required
#pragma semicolon 1
int g_iWarden = -1;
bool g_bRoundStartQueue = false;
ArrayList g_aWardenQueue = null;
//ArrayList g_aPreviousWarden = null;
Handle g_hPickWardenTimer = null;
// ConVars
ConVar g_hWardenPickTime = null;
// Forwards
Handle g_hOnWardenClaimed = null;
Handle g_hOnWardenDisappeared = null;
Handle g_hOnTryingToBecomeWarden = null;
public void OnPluginStart()
{
g_aWardenQueue = new ArrayList();
//g_aPreviousWarden = new ArrayList();
RegisterConvars();
RegisterConsoleCmds();
RegisterForwards();
HookEvents();
}
void RegisterForwards()
{
g_hOnWardenClaimed = CreateGlobalForward("OnWardenClaimed", ET_Ignore, Param_Cell);
g_hOnWardenDisappeared = CreateGlobalForward("OnWardenDisappeared", ET_Ignore);
g_hOnTryingToBecomeWarden = CreateGlobalForward("OnTryingToBecomeWarden", ET_Event, Param_Cell);
}
public void OnMapStart()
{
//PrecacheSoundAny
//AddFileToDownloadstable
}
void SetWarden(int client)
{
g_iWarden = client;
// Play sound
PrintToChatAll(" \x0C[WARDEN] \x01%N has become the new warden!");
// Notify other plugins about our new warden
Call_StartForward(g_hOnWardenClaimed);
Call_PushCell(client);
Call_Finish();
}
void RemoveWarden()
{
g_iWarden = -1;
// Play sound
// Let other plugins know the warden is gone
Call_StartForward(g_hOnWardenDisappeared);
Call_Finish();
}
public Action OnClientCommandKeyValues(int client, KeyValues kv)
{
char sCmd[64];
if (kv.GetSectionName(sCmd, sizeof(sCmd)) && StrEqual(sCmd, "ClanTagChanged", false))
{
// Might have to watch out we don't get into an infinte loop changing clantags here
SetClanTag(client);
return Plugin_Handled;
}
return Plugin_Continue;
}
void SetClanTag(int client)
{
char tag[32];
if (IsWarden(client)) {
tag = "[Warden]";
}
// When we add T Warden, we can also add that here
CS_SetClientClanTag(client, tag);
}
/********************************* COMMANDS ***********************************/
void RegisterConsoleCmds()
{
RegConsoleCmd("sm_warden", Command_ClaimWarden);
RegConsoleCmd("sm_w", Command_ClaimWarden);
RegConsoleCmd("sm_uw", Command_Unwarden);
RegConsoleCmd("sm_unwarden", Command_Unwarden);
RegAdminCmd("sm_rw", Command_RemoveWarden, ADMFLAG_SLAY);
RegAdminCmd("sm_removewarden", Command_RemoveWarden, ADMFLAG_SLAY);
//RegConsoleCmd("mark", Command_ShowMarker);
}
public Action Command_ClaimWarden(int client, int args)
{
if (g_iWarden != -1)
{
ReplyToCommand(client, "The current warden is %N", g_iWarden);
return Plugin_Handled;
}
if (GetClientTeam(client) != CS_TEAM_CT)
{
ReplyToCommand(client, "There is currently no warden!");
return Plugin_Handled;
}
// Let other people know someone is trying to become warden so they can block it or fire other actions
Action result = Plugin_Continue;
Call_StartForward(g_hOnTryingToBecomeWarden);
Call_PushCell(client);
Call_Finish(result);
if (result > Plugin_Continue)
{
// A plugin blocked this player from becoming warden
return Plugin_Handled;
}
if (!g_bRoundStartQueue)
{
SetWarden(client);
return Plugin_Handled;
}
int iUserId = GetClientUserId(client);
if (g_aWardenQueue.FindValue(iUserId) != -1)
{
ReplyToCommand(client, "You and %d other guards are currently preparing to become warden.", g_aWardenQueue.Length - 1);
return Plugin_Handled;
}
// Add to people trying to claim
g_aWardenQueue.Push(client);
return Plugin_Handled;
}
public Action Command_Unwarden(int client, int args)
{
if (IsWarden(client)) {
RemoveWarden();
}
return Plugin_Handled;
}
public Action Command_RemoveWarden(int client, int args)
{
if (g_iWarden == -1)
{
ReplyToCommand(client, "We don't currently have a warden!");
return Plugin_Handled;
}
RemoveWarden();
ShowActivity2(client, "[Warden]", "Removed the active warden");
return Plugin_Handled;
}
bool IsWarden(int client)
{
return g_iWarden == client;
}
/********************************** EVENTS *******************************/
void HookEvents()
{
HookEvent("player_team", Event_LeavingActiveTeam); // This also gets called when leaving the game
HookEvent("player_death", Event_LeavingActiveTeam);
HookEvent("round_start", Event_RoundStart);
//HookEvent("round_end", Event_RoundEnd);
}
public Action Event_LeavingActiveTeam(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if (client == -1)
{
// We have no idea who this is, make sure this wasn't our warden!
if (g_iWarden != -1 && !IsClientInGame(g_iWarden))
{
RemoveWarden();
}
return Plugin_Continue;
}
// If the player is currently warden, he should no longer be it
if (IsWarden(client))
{
RemoveWarden();
return Plugin_Continue;
}
// Remove the player from the warden queue if he is in it
int iUserId = GetClientUserId(client);
int iQueuePos = g_aWardenQueue.FindValue(iUserId);
if (iQueuePos != -1)
{
g_aWardenQueue.Erase(iQueuePos);
}
return Plugin_Continue;
}
public Action Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
g_iWarden = -1;
g_bRoundStartQueue = true;
g_hPickWardenTimer = CreateTimer(g_hWardenPickTime.FloatValue, Timer_PickWarden, TIMER_FLAG_NO_MAPCHANGE);
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i))
{
// Make sure everyone has their clan tag reset
SetClanTag(i);
}
}
}
public Action Timer_PickWarden(Handle timer)
{
if (timer != g_hPickWardenTimer)
{
// This is a timer from another round?
return Plugin_Stop;
}
g_bRoundStartQueue = false;
int iWardenCandidates = g_aWardenQueue.Length;
if (iWardenCandidates > 0)
{
int iRandomWardenPos = GetRandomInt(0, g_aWardenQueue.Length - 1);
int iWardenCandidate = GetClientOfUserId(g_aWardenQueue.Get(iRandomWardenPos));
if (iWardenCandidate == -1)
{
LogError("For some reason we have a warden candidate with client id -1 in our PickWarden timer callback. This isn't supposed to be possible.");
}
SetWarden(iWardenCandidate);
g_aWardenQueue.Clear();
return Plugin_Stop;
}
// There were no candidates, prepare to choose one randomly
int iCTCount = GetTeamAliveClientCount(CS_TEAM_CT);
if (iCTCount == 0)
{
// There are no CT's
return Plugin_Stop;
}
// Choose a random CT and make him warden
int iRandomCTPos = GetRandomInt(0, iCTCount - 1);
int count;
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i) && GetClientTeam(i) == CS_TEAM_CT)
{
if (count == iRandomCTPos)
{
SetWarden(i);
break;
}
count++;
}
}
return Plugin_Stop;
}
/******************************** NATIVES ********************************/
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
CreateNative("Warden_IsWarden", Native_IsWarden);
CreateNative("Warden_Set", Native_SetWarden);
CreateNative("Warden_Remove", Native_RemoveWarden);
CreateNative("Warden_GetWarden", Native_GetWarden);
CreateNative("Warden_Exists", Native_WardenExists);
return APLRes_Success;
}
public int Native_IsWarden(Handle plugin, int args)
{
int client = GetNativeCell(1);
return view_as<int>(IsWarden(client));
}
public int Native_SetWarden(Handle plugin, int args)
{
int client = GetNativeCell(1);
SetWarden(client);
}
public int Native_RemoveWarden(Handle plugin, int args)
{
RemoveWarden();
}
public int Native_GetWarden(Handle plugin, int args)
{
return g_iWarden;
}
public int Native_WardenExists(Handle plugin, int args)
{
return view_as<int>(g_iWarden != -1);
}
/******************************** CONVARS ********************************/
void RegisterConvars()
{
g_hWardenPickTime = CreateConVar("warden_picktime", "5.0", "Time (starting from round start) during which guards are able to become warden candidates", FCVAR_NONE, true, 0.0, false);
}