plugins/rockthevote.sp
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 13 05:13:37 2008 +0000 (2008-07-13)
changeset 2203 159ab9bad58c
parent 2133 cad0a5ea6742
child 2212 24fcd7ac70f7
permissions -rw-r--r--
- Added ForceChangeLevel and Map History to nextmap api
- Changed base plugins to use new api
- Added sm_maphistory command to nextmap.sp
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Rock The Vote Plugin
ds@1952
     5
 * Creates a map vote when the required number of players have requested one.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#include <sourcemod>
pred@2133
    35
#include <mapchooser>
pred@2133
    36
#include <nextmap>
ds@1952
    37
ds@1952
    38
#pragma semicolon 1
ds@1952
    39
ds@1952
    40
public Plugin:myinfo =
ds@1952
    41
{
ds@1952
    42
	name = "Rock The Vote",
ds@1952
    43
	author = "AlliedModders LLC",
ds@1952
    44
	description = "Provides RTV Map Voting",
ds@1952
    45
	version = SOURCEMOD_VERSION,
ds@1952
    46
	url = "http://www.sourcemod.net/"
ds@1952
    47
};
ds@1952
    48
ds@1952
    49
new Handle:g_Cvar_Needed = INVALID_HANDLE;
ds@1952
    50
new Handle:g_Cvar_MinPlayers = INVALID_HANDLE;
pred@2133
    51
new Handle:g_Cvar_InitialDelay = INVALID_HANDLE;
pred@2133
    52
new Handle:g_Cvar_Interval = INVALID_HANDLE;
pred@2133
    53
new Handle:g_Cvar_ChangeTime = INVALID_HANDLE;
pred@2133
    54
new Handle:g_Cvar_RTVPostVoteAction = INVALID_HANDLE;
ds@1952
    55
ds@1952
    56
new bool:g_CanRTV = false;		// True if RTV loaded maps and is active.
ds@1952
    57
new bool:g_RTVAllowed = false;	// True if RTV is available to players. Used to delay rtv votes.
ds@1952
    58
new g_Voters = 0;				// Total voters connected. Doesn't include fake clients.
ds@1952
    59
new g_Votes = 0;				// Total number of "say rtv" votes
ds@1952
    60
new g_VotesNeeded = 0;			// Necessary votes before map vote begins. (voters * percent_needed)
ds@1952
    61
new bool:g_Voted[MAXPLAYERS+1] = {false, ...};
pred@2133
    62
pred@2133
    63
new bool:g_InChange = false;
ds@1952
    64
ds@1952
    65
public OnPluginStart()
ds@1952
    66
{
ds@1952
    67
	LoadTranslations("common.phrases");
ds@1952
    68
	LoadTranslations("rockthevote.phrases");
ds@1952
    69
	
ds@1952
    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);
ds@1952
    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));
pred@2133
    72
	g_Cvar_InitialDelay = CreateConVar("sm_rtv_initialdelay", "30.0", "Time before first RTV can be held", 0, true, 0.00);
pred@2133
    73
	g_Cvar_Interval = CreateConVar("sm_rtv_interval", "240.0", "Time after a failed RTV before another can be held", 0, true, 0.00);
pred@2133
    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);
pred@2133
    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);
ds@1952
    76
	
ds@1952
    77
	RegConsoleCmd("say", Command_Say);
ds@1952
    78
	RegConsoleCmd("say_team", Command_Say);
ds@1952
    79
	
pred@2133
    80
	RegConsoleCmd("sm_rtv", Command_RTV);
ds@1952
    81
	
ds@1952
    82
	AutoExecConfig(true, "rtv");
ds@1952
    83
}
ds@1952
    84
ds@1952
    85
public OnMapStart()
ds@1952
    86
{
ds@1952
    87
	g_Voters = 0;
ds@1952
    88
	g_Votes = 0;
ds@1952
    89
	g_VotesNeeded = 0;
pred@2133
    90
	g_InChange = false;
pred@2133
    91
	
pred@2133
    92
	/* Handle late load */
pred@2133
    93
	new maxclients = GetMaxClients();
pred@2133
    94
	for (new i=1; i<=maxclients; i++)
pred@2133
    95
	{
pred@2133
    96
		if (IsClientConnected(i))
pred@2133
    97
		{
pred@2133
    98
			OnClientConnect(i, "", 0);	
pred@2133
    99
		}	
pred@2133
   100
	}
ds@1952
   101
}
ds@1952
   102
ds@1952
   103
public OnMapEnd()
ds@1952
   104
{
ds@1952
   105
	g_CanRTV = false;	
ds@1952
   106
	g_RTVAllowed = false;
ds@1952
   107
}
ds@1952
   108
ds@1952
   109
public OnConfigsExecuted()
pred@2133
   110
{	
ds@1952
   111
	g_CanRTV = true;
pred@2133
   112
	g_RTVAllowed = false;
pred@2133
   113
	CreateTimer(GetConVarFloat(g_Cvar_InitialDelay), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
ds@1952
   114
}
ds@1952
   115
ds@1952
   116
public bool:OnClientConnect(client, String:rejectmsg[], maxlen)
ds@1952
   117
{
pred@2133
   118
	if(IsFakeClient(client))
ds@1952
   119
		return true;
ds@1952
   120
	
ds@1952
   121
	g_Voted[client] = false;
ds@1952
   122
ds@1952
   123
	g_Voters++;
ds@1952
   124
	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
ds@1952
   125
	
ds@1952
   126
	return true;
ds@1952
   127
}
ds@1952
   128
ds@1952
   129
public OnClientDisconnect(client)
ds@1952
   130
{
ds@1952
   131
	if(!g_CanRTV || IsFakeClient(client))
ds@1952
   132
		return;
ds@1952
   133
	
ds@1952
   134
	if(g_Voted[client])
ds@1952
   135
	{
ds@1952
   136
		g_Votes--;
ds@1952
   137
	}
ds@1952
   138
	
ds@1952
   139
	g_Voters--;
ds@1952
   140
	
ds@1952
   141
	g_VotesNeeded = RoundToFloor(float(g_Voters) * GetConVarFloat(g_Cvar_Needed));
ds@1952
   142
	
pred@2133
   143
	if (g_Votes && 
pred@2133
   144
		g_Voters && 
pred@2133
   145
		g_Votes >= g_VotesNeeded && 
pred@2133
   146
		g_RTVAllowed ) 
ds@1952
   147
	{
pred@2133
   148
		if (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished())
pred@2133
   149
		{
pred@2133
   150
			return;
pred@2133
   151
		}
pred@2133
   152
		
pred@2133
   153
		StartRTV();
ds@1952
   154
	}	
ds@1952
   155
}
ds@1952
   156
pred@2133
   157
public Action:Command_RTV(client, args)
ds@1952
   158
{
pred@2133
   159
	if (!g_CanRTV || !client)
ds@1952
   160
	{
pred@2133
   161
		return Plugin_Continue;
ds@1952
   162
	}
ds@1952
   163
	
pred@2133
   164
	AttemptRTV(client);
ds@1952
   165
	
pred@2133
   166
	return Plugin_Continue;
ds@1952
   167
}
ds@1952
   168
ds@1952
   169
public Action:Command_Say(client, args)
ds@1952
   170
{
ds@1952
   171
	if (!g_CanRTV || !client)
ds@1952
   172
	{
ds@1952
   173
		return Plugin_Continue;
ds@1952
   174
	}
pred@2133
   175
	
ds@1952
   176
	decl String:text[192];
ds@1952
   177
	if (!GetCmdArgString(text, sizeof(text)))
ds@1952
   178
	{
ds@1952
   179
		return Plugin_Continue;
ds@1952
   180
	}
ds@1952
   181
	
ds@1952
   182
	new startidx = 0;
ds@1952
   183
	if(text[strlen(text)-1] == '"')
ds@1952
   184
	{
ds@1952
   185
		text[strlen(text)-1] = '\0';
ds@1952
   186
		startidx = 1;
ds@1952
   187
	}
ds@1952
   188
	
pred@2133
   189
	new ReplySource:old = SetCmdReplySource(SM_REPLY_TO_CHAT);
pred@2133
   190
	
ds@1952
   191
	if (strcmp(text[startidx], "rtv", false) == 0 || strcmp(text[startidx], "rockthevote", false) == 0)
ds@1952
   192
	{
pred@2133
   193
		AttemptRTV(client);
pred@2133
   194
	}
pred@2133
   195
	
pred@2133
   196
	SetCmdReplySource(old);
pred@2133
   197
	
pred@2133
   198
	return Plugin_Continue;	
pred@2133
   199
}
pred@2133
   200
pred@2133
   201
AttemptRTV(client)
pred@2133
   202
{
pred@2133
   203
	if (!g_RTVAllowed  || (GetConVarInt(g_Cvar_RTVPostVoteAction) == 1 && HasEndOfMapVoteFinished()))
pred@2133
   204
	{
pred@2133
   205
		ReplyToCommand(client, "[SM] %t", "RTV Not Allowed");
pred@2133
   206
		return;
pred@2133
   207
	}
ds@1952
   208
		
pred@2133
   209
	if (!CanMapChooserStartVote())
ds@1952
   210
	{
pred@2133
   211
		ReplyToCommand(client, "[SM] %t", "RTV Started");
pred@2133
   212
		return;
ds@1952
   213
	}
ds@1952
   214
	
pred@2133
   215
	if (GetClientCount(true) < GetConVarInt(g_Cvar_MinPlayers))
pred@2133
   216
	{
pred@2133
   217
		ReplyToCommand(client, "[SM] %t", "Minimal Players Not Met");
pred@2133
   218
		return;			
pred@2133
   219
	}
pred@2133
   220
	
pred@2133
   221
	if (g_Voted[client])
pred@2133
   222
	{
pred@2133
   223
		ReplyToCommand(client, "[SM] %t", "Already Voted");
pred@2133
   224
		return;
pred@2133
   225
	}	
pred@2133
   226
	
pred@2133
   227
	new String:name[64];
pred@2133
   228
	GetClientName(client, name, sizeof(name));
pred@2133
   229
	
pred@2133
   230
	g_Votes++;
pred@2133
   231
	g_Voted[client] = true;
pred@2133
   232
	
pred@2133
   233
	PrintToChatAll("[SM] %t", "RTV Requested", name, g_Votes, g_VotesNeeded);
pred@2133
   234
	
pred@2133
   235
	if (g_Votes >= g_VotesNeeded)
pred@2133
   236
	{
pred@2133
   237
		StartRTV();
pred@2133
   238
	}	
ds@1952
   239
}
ds@1952
   240
ds@1952
   241
public Action:Timer_DelayRTV(Handle:timer)
ds@1952
   242
{
ds@1952
   243
	g_RTVAllowed = true;
ds@1952
   244
}
ds@1952
   245
pred@2133
   246
StartRTV()
ds@1952
   247
{
pred@2133
   248
	if (g_InChange)
ds@1952
   249
	{
pred@2133
   250
		return;	
ds@1952
   251
	}
ds@1952
   252
	
pred@2133
   253
	if (HasEndOfMapVoteFinished())
ds@1952
   254
	{
pred@2133
   255
		/* Change right now then */
pred@2133
   256
		new String:map[65];
pred@2133
   257
		if (GetNextMap(map, sizeof(map)))
ds@1952
   258
		{
pred@2133
   259
			PrintToChatAll("[SM] %t", "Changing Maps", map);
pred@2133
   260
			CreateTimer(5.0, Timer_ChangeMap, _, TIMER_FLAG_NO_MAPCHANGE);
pred@2133
   261
			g_InChange = true;
pred@2133
   262
			
pred@2133
   263
			ResetRTV();
ds@1952
   264
			
pred@2133
   265
			g_RTVAllowed = false;
ds@1952
   266
		}
pred@2133
   267
		return;	
pred@2133
   268
	}
pred@2133
   269
	
pred@2133
   270
	if (CanMapChooserStartVote())
pred@2133
   271
	{
pred@2133
   272
		new MapChange:when = MapChange:GetConVarInt(g_Cvar_ChangeTime);
pred@2133
   273
		InitiateMapChooserVote(when);
pred@2133
   274
		
pred@2133
   275
		ResetRTV();
pred@2133
   276
		
pred@2133
   277
		g_RTVAllowed = false;
pred@2133
   278
		CreateTimer(GetConVarFloat(g_Cvar_Interval), Timer_DelayRTV, _, TIMER_FLAG_NO_MAPCHANGE);
ds@1952
   279
	}
ds@1952
   280
}
ds@1952
   281
pred@2133
   282
ResetRTV()
ds@1952
   283
{
pred@2133
   284
	g_Votes = 0;
pred@2133
   285
			
pred@2133
   286
	for (new i=1; i<=MAXPLAYERS; i++)
ds@1952
   287
	{
pred@2133
   288
		g_Voted[i] = false;
pred@2133
   289
	}
pred@2133
   290
}
pred@2133
   291
pred@2133
   292
public Action:Timer_ChangeMap(Handle:hTimer)
pred@2133
   293
{
pred@2133
   294
	g_InChange = false;
pred@2133
   295
	
pred@2133
   296
	LogMessage("RTV changing map manually");
pred@2133
   297
	
pred@2133
   298
	new String:map[65];
pred@2133
   299
	if (GetNextMap(map, sizeof(map)))
pred@2133
   300
	{	
pred@2203
   301
		ForceChangeLevel(map, "RTV after mapvote");
ds@1952
   302
	}
ds@1952
   303
	
pred@2133
   304
	return Plugin_Stop;
ds@1952
   305
}