plugins/rockthevote.sp
author David Anderson <dvander@alliedmods.net>
Thu Mar 05 02:05:56 2009 -0500 (2009-03-05)
changeset 2652 01152829b375
parent 2370 43b134b35542
child 2802 7ff4cda0b570
permissions -rw-r--r--
Updated changelog.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Rock The Vote Plugin
     5  * Creates a map vote when the required number of players have requested one.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    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.
    13  * 
    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
    17  * details.
    18  *
    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/>.
    21  *
    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>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #include <sourcemod>
    35 #include <mapchooser>
    36 #include <nextmap>
    37 
    38 #pragma semicolon 1
    39 
    40 public Plugin:myinfo =
    41 {
    42 	name = "Rock The Vote",
    43 	author = "AlliedModders LLC",
    44 	description = "Provides RTV Map Voting",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49 new Handle:g_Cvar_Needed = INVALID_HANDLE;
    50 new Handle:g_Cvar_MinPlayers = INVALID_HANDLE;
    51 new Handle:g_Cvar_InitialDelay = INVALID_HANDLE;
    52 new Handle:g_Cvar_Interval = INVALID_HANDLE;
    53 new Handle:g_Cvar_ChangeTime = INVALID_HANDLE;
    54 new Handle:g_Cvar_RTVPostVoteAction = INVALID_HANDLE;
    55 
    56 new bool:g_CanRTV = false;		// True if RTV loaded maps and is active.
    57 new bool:g_RTVAllowed = false;	// True if RTV is available to players. Used to delay rtv votes.
    58 new g_Voters = 0;				// Total voters connected. Doesn't include fake clients.
    59 new g_Votes = 0;				// Total number of "say rtv" votes
    60 new g_VotesNeeded = 0;			// Necessary votes before map vote begins. (voters * percent_needed)
    61 new bool:g_Voted[MAXPLAYERS+1] = {false, ...};
    62 
    63 new bool:g_InChange = false;
    64 
    65 public OnPluginStart()
    66 {
    67 	LoadTranslations("common.phrases");
    68 	LoadTranslations("rockthevote.phrases");
    69 	
    70 	g_Cvar_Needed = CreateConVar("sm_rtv_needed", "0.60", "Percentage of players needed to rockthevote (Def 60%)", 0, true, 0.05, true, 1.0);
    71 	g_Cvar_MinPlayers = CreateConVar("sm_rtv_minplayers", "0", "Number of players required before RTV will be enabled.", 0, true, 0.0, true, float(MAXPLAYERS));
    72 	g_Cvar_InitialDelay = CreateConVar("sm_rtv_initialdelay", "30.0", "Time (in seconds) before first RTV can be held", 0, true, 0.00);
    73 	g_Cvar_Interval = CreateConVar("sm_rtv_interval", "240.0", "Time (in seconds) after a failed RTV before another can be held", 0, true, 0.00);
    74 	g_Cvar_ChangeTime = CreateConVar("sm_rtv_changetime", "0", "When to change the map after a succesful RTV: 0 - Instant, 1 - RoundEnd, 2 - MapEnd", _, true, 0.0, true, 2.0);
    75 	g_Cvar_RTVPostVoteAction = CreateConVar("sm_rtv_postvoteaction", "0", "What to do with RTV's after a mapvote has completed. 0 - Allow, success = instant change, 1 - Deny", _, true, 0.0, true, 1.0);
    76 	
    77 	RegConsoleCmd("say", Command_Say);
    78 	RegConsoleCmd("say_team", Command_Say);
    79 	
    80 	RegConsoleCmd("sm_rtv", Command_RTV);
    81 	
    82 	AutoExecConfig(true, "rtv");
    83 }
    84 
    85 public OnMapStart()
    86 {
    87 	g_Voters = 0;
    88 	g_Votes = 0;
    89 	g_VotesNeeded = 0;
    90 	g_InChange = false;
    91 	
    92 	/* Handle late load */
    93 	for (new i=1; i<=MaxClients; i++)
    94 	{
    95 		if (IsClientConnected(i))
    96 		{
    97 			OnClientConnected(i);	
    98 		}	
    99 	}
   100 }
   101 
   102 public OnMapEnd()
   103 {
   104 	g_CanRTV = false;	
   105 	g_RTVAllowed = false;
   106 }
   107 
   108 public OnConfigsExecuted()
   109 {	
   110 	g_CanRTV = true;
   111 	g_RTVAllowed = false;
   112 	CreateTimer(GetConVarFloat(g_Cvar_InitialDelay), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
   113 }
   114 
   115 public OnClientConnected(client)
   116 {
   117 	if(IsFakeClient(client))
   118 		return;
   119 	
   120 	g_Voted[client] = false;
   121 
   122 	g_Voters++;
   123 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   124 	
   125 	return;
   126 }
   127 
   128 public OnClientDisconnect(client)
   129 {
   130 	if(IsFakeClient(client))
   131 		return;
   132 	
   133 	if(g_Voted[client])
   134 	{
   135 		g_Votes--;
   136 	}
   137 	
   138 	g_Voters--;
   139 	
   140 	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
   141 	
   142 	if (!g_CanRTV)
   143 	{
   144 		return;	
   145 	}
   146 	
   147 	if (g_Votes && 
   148 		g_Voters && 
   149 		g_Votes >= g_VotesNeeded && 
   150 		g_RTVAllowed ) 
   151 	{
   152 		if (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished())
   153 		{
   154 			return;
   155 		}
   156 		
   157 		StartRTV();
   158 	}	
   159 }
   160 
   161 public Action:Command_RTV(client, args)
   162 {
   163 	if (!g_CanRTV || !client)
   164 	{
   165 		return Plugin_Continue;
   166 	}
   167 	
   168 	AttemptRTV(client);
   169 	
   170 	return Plugin_Continue;
   171 }
   172 
   173 public Action:Command_Say(client, args)
   174 {
   175 	if (!g_CanRTV || !client)
   176 	{
   177 		return Plugin_Continue;
   178 	}
   179 	
   180 	decl String:text[192];
   181 	if (!GetCmdArgString(text, sizeof(text)))
   182 	{
   183 		return Plugin_Continue;
   184 	}
   185 	
   186 	new startidx = 0;
   187 	if(text[strlen(text)-1] == '"')
   188 	{
   189 		text[strlen(text)-1] = '\0';
   190 		startidx = 1;
   191 	}
   192 	
   193 	new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
   194 	
   195 	if (strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0)
   196 	{
   197 		AttemptRTV(client);
   198 	}
   199 	
   200 	SetCmdReplySource(old);
   201 	
   202 	return Plugin_Continue;	
   203 }
   204 
   205 AttemptRTV(client)
   206 {
   207 	if (!g_RTVAllowed  || (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished()))
   208 	{
   209 		ReplyToCommand(client, "[SM] %t", "RTV Not Allowed");
   210 		return;
   211 	}
   212 		
   213 	if (!CanMapChooserStartVote())
   214 	{
   215 		ReplyToCommand(client, "[SM] %t", "RTV Started");
   216 		return;
   217 	}
   218 	
   219 	if (GetClientCount(true) < GetConVarInt(g_Cvar_MinPlayers))
   220 	{
   221 		ReplyToCommand(client, "[SM] %t", "Minimal Players Not Met");
   222 		return;			
   223 	}
   224 	
   225 	if (g_Voted[client])
   226 	{
   227 		ReplyToCommand(client, "[SM] %t", "Already Voted");
   228 		return;
   229 	}	
   230 	
   231 	new String:name[64];
   232 	GetClientName(client, name, sizeof(name));
   233 	
   234 	g_Votes++;
   235 	g_Voted[client] = true;
   236 	
   237 	PrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
   238 	
   239 	if (g_Votes >= g_VotesNeeded)
   240 	{
   241 		StartRTV();
   242 	}	
   243 }
   244 
   245 public Action:Timer_DelayRTV(Handle:timer)
   246 {
   247 	g_RTVAllowed = true;
   248 }
   249 
   250 StartRTV()
   251 {
   252 	if (g_InChange)
   253 	{
   254 		return;	
   255 	}
   256 	
   257 	if (EndOfMapVoteEnabled() && HasEndOfMapVoteFinished())
   258 	{
   259 		/* Change right now then */
   260 		new String:map[65];
   261 		if (GetNextMap(map, sizeof(map)))
   262 		{
   263 			PrintToChatAll("[SM] %t", "Changing Maps", map);
   264 			CreateTimer(5.0, Timer_ChangeMap, _, TIMER_FLAG_NO_MAPCHANGE);
   265 			g_InChange = true;
   266 			
   267 			ResetRTV();
   268 			
   269 			g_RTVAllowed = false;
   270 		}
   271 		return;	
   272 	}
   273 	
   274 	if (CanMapChooserStartVote())
   275 	{
   276 		new MapChange:when = MapChange:GetConVarInt(g_Cvar_ChangeTime);
   277 		InitiateMapChooserVote(when);
   278 		
   279 		ResetRTV();
   280 		
   281 		g_RTVAllowed = false;
   282 		CreateTimer(GetConVarFloat(g_Cvar_Interval), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
   283 	}
   284 }
   285 
   286 ResetRTV()
   287 {
   288 	g_Votes = 0;
   289 			
   290 	for (new i=1; i<=MAXPLAYERS; i++)
   291 	{
   292 		g_Voted[i] = false;
   293 	}
   294 }
   295 
   296 public Action:Timer_ChangeMap(Handle:hTimer)
   297 {
   298 	g_InChange = false;
   299 	
   300 	LogMessage("RTV changing map manually");
   301 	
   302 	new String:map[65];
   303 	if (GetNextMap(map, sizeof(map)))
   304 	{	
   305 		ForceChangeLevel(map, "RTV after mapvote");
   306 	}
   307 	
   308 	return Plugin_Stop;
   309 }