Fixed build for real.
3 * =============================================================================
4 * SourceMod Reserved Slots Plugin
5 * Provides basic reserved slots.
7 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
38 public Plugin:myinfo =
40 name = "Reserved Slots",
41 author = "AlliedModders LLC",
42 description = "Provides basic reserved slots",
43 version = SOURCEMOD_VERSION,
44 url = "http://www.sourcemod.net/"
48 new bool:g_isAdmin[MAXPLAYERS+1];
50 /* Handles to convars used by plugin */
51 new Handle:sm_reserved_slots;
52 new Handle:sm_hide_slots;
53 new Handle:sv_visiblemaxplayers;
54 new Handle:sm_reserve_type;
55 new Handle:sm_reserve_maxadmins;
56 new Handle:sm_reserve_kicktype;
69 public OnPluginStart()
71 LoadTranslations("reservedslots.phrases");
73 sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
74 sm_hide_slots = CreateConVar("sm_hide_slots", "0", "If set to 1, reserved slots will hidden (subtracted from the max slot count)", 0, true, 0.0, true, 1.0);
75 sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
76 sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
77 sm_reserve_maxadmins = CreateConVar("sm_reserve_maxadmins", "1", "Maximum amount of admins to let in the server with reserve type 2", 0, true, 0.0);
78 sm_reserve_kicktype = CreateConVar("sm_reserve_kicktype", "0", "How to select a client to kick (if appropriate)", 0, true, 0.0, true, 2.0);
80 HookConVarChange(sm_reserved_slots, SlotCountChanged);
81 HookConVarChange(sm_hide_slots, SlotHideChanged);
83 g_SDKVersion = GuessSDKVersion();
85 if (g_SDKVersion == SOURCE_SDK_EPISODE2VALVE)
87 for (new i = 1; i <= MaxClients; i++)
89 if (!IsClientConnected(i))
92 if (IsClientSourceTV(i))
96 else if (IsClientReplay(i))
106 /* If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
112 if (GetConVarBool(sm_hide_slots))
114 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
118 public OnConfigsExecuted()
120 if (GetConVarBool(sm_hide_slots))
122 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
126 public Action:OnTimedKick(Handle:timer, any:client)
128 if (!client || !IsClientInGame(client))
130 return Plugin_Handled;
133 KickClient(client, "%T", "Slot reserved", client);
135 if (GetConVarBool(sm_hide_slots))
137 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
140 return Plugin_Handled;
143 public OnClientPostAdminCheck(client)
145 if (g_SDKVersion == SOURCE_SDK_EPISODE2VALVE)
147 if (IsClientSourceTV(client))
151 else if (IsClientReplay(client))
157 new reserved = GetConVarInt(sm_reserved_slots);
161 new clients = GetClientCount(false);
162 new limit = GetMaxHumanPlayers() - reserved;
163 new flags = GetUserFlagBits(client);
165 new type = GetConVarInt(sm_reserve_type);
169 if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
171 if (GetConVarBool(sm_hide_slots))
173 SetVisibleMaxSlots(clients, limit);
179 /* Kick player because there are no public slots left */
180 CreateTimer(0.1, OnTimedKick, client);
186 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
188 new target = SelectKickClient();
192 /* Kick public player to free the reserved slot again */
193 CreateTimer(0.1, OnTimedKick, target);
198 /* Kick player because there are no public slots left */
199 CreateTimer(0.1, OnTimedKick, client);
205 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
208 g_isAdmin[client] = true;
211 if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
213 /* Server is full, reserved slots aren't and client doesn't have reserved slots access */
215 if (g_isAdmin[client])
217 new target = SelectKickClient();
221 /* Kick public player to free the reserved slot again */
222 CreateTimer(0.1, OnTimedKick, target);
227 /* Kick player because there are no public slots left */
228 CreateTimer(0.1, OnTimedKick, client);
235 public OnClientDisconnect_Post(client)
237 if (g_SDKVersion == SOURCE_SDK_EPISODE2VALVE)
239 if (client == g_SourceTV)
243 else if (client == g_Replay)
249 if (GetConVarBool(sm_hide_slots))
251 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
254 if (g_isAdmin[client])
257 g_isAdmin[client] = false;
261 public SlotCountChanged(Handle:convar, const String:oldValue[], const String:newValue[])
263 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
264 new slotcount = GetConVarInt(convar);
269 else if (GetConVarBool(sm_hide_slots))
271 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - slotcount);
275 public SlotHideChanged(Handle:convar, const String:oldValue[], const String:newValue[])
277 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
278 if (!GetConVarBool(convar))
284 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
288 SetVisibleMaxSlots(clients, limit)
292 if (clients == GetMaxHumanPlayers())
294 num = GetMaxHumanPlayers();
295 } else if (clients < limit) {
299 if (g_SDKVersion == SOURCE_SDK_EPISODE2VALVE)
312 SetConVarInt(sv_visiblemaxplayers, num);
317 SetConVarInt(sv_visiblemaxplayers, -1);
322 new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
324 new Float:highestValue;
327 new Float:highestSpecValue;
328 new highestSpecValueId;
334 for (new i=1; i<=MaxClients; i++)
336 if (!IsClientConnected(i))
341 new flags = GetUserFlagBits(i);
343 if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
350 if (IsClientInGame(i))
352 if (type == Kick_HighestPing)
354 value = GetClientAvgLatency(i, NetFlow_Outgoing);
356 else if (type == Kick_HighestTime)
358 value = GetClientTime(i);
362 value = GetRandomFloat(0.0, 100.0);
365 if (IsClientObserver(i))
369 if (value > highestSpecValue)
371 highestSpecValue = value;
372 highestSpecValueId = i;
377 if (value >= highestValue)
379 highestValue = value;
386 return highestSpecValueId;
389 return highestValueId;