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;
65 public OnPluginStart()
67 LoadTranslations("reservedslots.phrases");
69 sm_reserved_slots = CreateConVar("sm_reserved_slots", "0", "Number of reserved player slots", 0, true, 0.0);
70 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);
71 sv_visiblemaxplayers = FindConVar("sv_visiblemaxplayers");
72 sm_reserve_type = CreateConVar("sm_reserve_type", "0", "Method of reserving slots", 0, true, 0.0, true, 2.0);
73 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);
74 sm_reserve_kicktype = CreateConVar("sm_reserve_kicktype", "0", "How to select a client to kick (if appropriate)", 0, true, 0.0, true, 2.0);
76 HookConVarChange(sm_reserved_slots, SlotsChanged);
77 HookConVarChange(sm_hide_slots, SlotsChanged);
82 /* If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
83 SetConVarInt(sv_visiblemaxplayers, MaxClients);
88 if (GetConVarBool(sm_hide_slots))
90 SetVisibleMaxSlots(GetClientCount(false), MaxClients - GetConVarInt(sm_reserved_slots));
94 public OnConfigsExecuted()
96 if (GetConVarBool(sm_hide_slots))
98 SetVisibleMaxSlots(GetClientCount(false), MaxClients - GetConVarInt(sm_reserved_slots));
102 public Action:OnTimedKick(Handle:timer, any:client)
104 if (!client || !IsClientInGame(client))
106 return Plugin_Handled;
109 KickClient(client, "%T", "Slot reserved", client);
111 if (GetConVarBool(sm_hide_slots))
113 SetVisibleMaxSlots(GetClientCount(false), MaxClients - GetConVarInt(sm_reserved_slots));
116 return Plugin_Handled;
119 public OnClientPostAdminCheck(client)
121 new reserved = GetConVarInt(sm_reserved_slots);
125 new clients = GetClientCount(false);
126 new limit = MaxClients - reserved;
127 new flags = GetUserFlagBits(client);
129 new type = GetConVarInt(sm_reserve_type);
133 if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
135 if (GetConVarBool(sm_hide_slots))
137 SetVisibleMaxSlots(clients, limit);
143 /* Kick player because there are no public slots left */
144 CreateTimer(0.1, OnTimedKick, client);
150 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
152 new target = SelectKickClient();
156 /* Kick public player to free the reserved slot again */
157 CreateTimer(0.1, OnTimedKick, target);
162 /* Kick player because there are no public slots left */
163 CreateTimer(0.1, OnTimedKick, client);
169 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
172 g_isAdmin[client] = true;
175 if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
177 /* Server is full, reserved slots aren't and client doesn't have reserved slots access */
179 if (g_isAdmin[client])
181 new target = SelectKickClient();
185 /* Kick public player to free the reserved slot again */
186 CreateTimer(0.1, OnTimedKick, target);
191 /* Kick player because there are no public slots left */
192 CreateTimer(0.1, OnTimedKick, client);
199 public OnClientDisconnect_Post(client)
201 if (GetConVarBool(sm_hide_slots))
203 SetVisibleMaxSlots(GetClientCount(false), MaxClients - GetConVarInt(sm_reserved_slots));
206 if (g_isAdmin[client])
209 g_isAdmin[client] = false;
213 public SlotsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
215 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
216 if (StringToInt(newValue) == 0)
218 SetConVarInt(sv_visiblemaxplayers, MaxClients);
222 SetVisibleMaxSlots(clients, limit)
226 if (clients == MaxClients)
229 } else if (clients < limit) {
233 SetConVarInt(sv_visiblemaxplayers, num);
238 new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
240 new Float:highestValue;
243 new Float:highestSpecValue;
244 new highestSpecValueId;
250 for (new i=1; i<=MaxClients; i++)
252 if (!IsClientConnected(i))
257 new flags = GetUserFlagBits(i);
259 if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
266 if (IsClientInGame(i))
268 if (type == Kick_HighestPing)
270 value = GetClientAvgLatency(i, NetFlow_Outgoing);
272 else if (type == Kick_HighestTime)
274 value = GetClientTime(i);
278 value = GetRandomFloat(0.0, 100.0);
281 if (IsClientObserver(i))
285 if (value > highestSpecValue)
287 highestSpecValue = value;
288 highestSpecValueId = i;
293 if (value >= highestValue)
295 highestValue = value;
302 return highestSpecValueId;
305 return highestValueId;