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;
58 new bool:g_BotsSubtractFromMax;
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 new EngineVersion:engineVersion = GetEngineVersion();
84 g_BotsSubtractFromMax = engineVersion == Engine_TF2 || engineVersion == Engine_CSS || engineVersion == Engine_DODS || engineVersion == Engine_HL2DM;
86 if (g_BotsSubtractFromMax)
88 for (new i = 1; i <= MaxClients; i++)
90 if (!IsClientConnected(i))
93 if (IsClientSourceTV(i))
97 else if (IsClientReplay(i))
107 /* If the plugin has been unloaded, reset visiblemaxplayers. In the case of the server shutting down this effect will not be visible */
113 if (GetConVarBool(sm_hide_slots))
115 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
119 public OnConfigsExecuted()
121 if (GetConVarBool(sm_hide_slots))
123 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
127 public Action:OnTimedKick(Handle:timer, any:client)
129 if (!client || !IsClientInGame(client))
131 return Plugin_Handled;
134 KickClient(client, "%T", "Slot reserved", client);
136 if (GetConVarBool(sm_hide_slots))
138 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
141 return Plugin_Handled;
144 public OnClientPostAdminCheck(client)
146 if (g_BotsSubtractFromMax)
148 if (IsClientSourceTV(client))
152 else if (IsClientReplay(client))
158 new reserved = GetConVarInt(sm_reserved_slots);
162 new clients = GetClientCount(false);
163 new limit = GetMaxHumanPlayers() - reserved;
164 new flags = GetUserFlagBits(client);
166 new type = GetConVarInt(sm_reserve_type);
170 if (clients <= limit || IsFakeClient(client) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
172 if (GetConVarBool(sm_hide_slots))
174 SetVisibleMaxSlots(clients, limit);
180 /* Kick player because there are no public slots left */
181 CreateTimer(0.1, OnTimedKick, client);
187 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
189 new target = SelectKickClient();
193 /* Kick public player to free the reserved slot again */
194 CreateTimer(0.1, OnTimedKick, target);
199 /* Kick player because there are no public slots left */
200 CreateTimer(0.1, OnTimedKick, client);
206 if (flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION)
209 g_isAdmin[client] = true;
212 if (clients > limit && g_adminCount < GetConVarInt(sm_reserve_maxadmins))
214 /* Server is full, reserved slots aren't and client doesn't have reserved slots access */
216 if (g_isAdmin[client])
218 new target = SelectKickClient();
222 /* Kick public player to free the reserved slot again */
223 CreateTimer(0.1, OnTimedKick, target);
228 /* Kick player because there are no public slots left */
229 CreateTimer(0.1, OnTimedKick, client);
236 public OnClientDisconnect_Post(client)
238 if (g_BotsSubtractFromMax)
240 if (client == g_SourceTV)
244 else if (client == g_Replay)
250 if (GetConVarBool(sm_hide_slots))
252 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
255 if (g_isAdmin[client])
258 g_isAdmin[client] = false;
262 public SlotCountChanged(Handle:convar, const String:oldValue[], const String:newValue[])
264 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
265 new slotcount = GetConVarInt(convar);
270 else if (GetConVarBool(sm_hide_slots))
272 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - slotcount);
276 public SlotHideChanged(Handle:convar, const String:oldValue[], const String:newValue[])
278 /* Reserved slots or hidden slots have been disabled - reset sv_visiblemaxplayers */
279 if (!GetConVarBool(convar))
285 SetVisibleMaxSlots(GetClientCount(false), GetMaxHumanPlayers() - GetConVarInt(sm_reserved_slots));
289 SetVisibleMaxSlots(clients, limit)
293 if (clients == GetMaxHumanPlayers())
295 num = GetMaxHumanPlayers();
296 } else if (clients < limit) {
300 if (g_BotsSubtractFromMax)
313 SetConVarInt(sv_visiblemaxplayers, num);
318 SetConVarInt(sv_visiblemaxplayers, -1);
323 new KickType:type = KickType:GetConVarInt(sm_reserve_kicktype);
325 new Float:highestValue;
328 new Float:highestSpecValue;
329 new highestSpecValueId;
335 for (new i=1; i<=MaxClients; i++)
337 if (!IsClientConnected(i))
342 new flags = GetUserFlagBits(i);
344 if (IsFakeClient(i) || flags & ADMFLAG_ROOT || flags & ADMFLAG_RESERVATION || CheckCommandAccess(i, "sm_reskick_immunity", ADMFLAG_RESERVATION, false))
351 if (IsClientInGame(i))
353 if (type == Kick_HighestPing)
355 value = GetClientAvgLatency(i, NetFlow_Outgoing);
357 else if (type == Kick_HighestTime)
359 value = GetClientTime(i);
363 value = GetRandomFloat(0.0, 100.0);
366 if (IsClientObserver(i))
370 if (value > highestSpecValue)
372 highestSpecValue = value;
373 highestSpecValueId = i;
378 if (value >= highestValue)
380 highestValue = value;
387 return highestSpecValueId;
390 return highestValueId;