2008-03-30 09:00:22 +02:00
/ * *
* vim : set ts = 4 :
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
* SourceMod Rock The Vote Plugin
* Creates a map vote when the required number of players have requested one .
*
* SourceMod ( C ) 2004 - 2008 AlliedModders LLC . All rights reserved .
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
* This program is free software ; you can redistribute it and / or modify it under
* the terms of the GNU General Public License , version 3.0 , as published by the
* Free Software Foundation .
*
* This program is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE . See the GNU General Public License for more
* details .
*
* You should have received a copy of the GNU General Public License along with
* this program . If not , see < http : //www.gnu.org/licenses/>.
*
* As a special exception , AlliedModders LLC gives you permission to link the
* code of this program ( as well as its derivative works ) to " Half-Life 2, " the
* " Source Engine, " the " SourcePawn JIT, " and any Game MODs that run on software
* by the Valve Corporation . You must obey the GNU General Public License in
* all respects for all other code used . Additionally , AlliedModders LLC grants
* this exception to all derivative works . AlliedModders LLC defines further
* exceptions , found in LICENSE . txt ( as of this writing , version JULY - 31 - 2007 ) ,
* or < http : //www.sourcemod.net/license.php>.
*
* Version : $ Id $
* /
# include <sourcemod>
2008-07-01 03:18:52 +02:00
# include <mapchooser>
# include <nextmap>
2008-03-30 09:00:22 +02:00
# pragma semicolon 1
public Plugin : myinfo =
{
name = " Rock The Vote " ,
author = " AlliedModders LLC " ,
description = " Provides RTV Map Voting " ,
version = SOURCEMOD_VERSION ,
url = " http://www.sourcemod.net/ "
} ;
new Handle : g_Cvar_Needed = INVALID_HANDLE ;
new Handle : g_Cvar_MinPlayers = INVALID_HANDLE ;
2008-07-01 03:18:52 +02:00
new Handle : g_Cvar_InitialDelay = INVALID_HANDLE ;
new Handle : g_Cvar_Interval = INVALID_HANDLE ;
new Handle : g_Cvar_ChangeTime = INVALID_HANDLE ;
new Handle : g_Cvar_RTVPostVoteAction = INVALID_HANDLE ;
2008-03-30 09:00:22 +02:00
new bool : g_CanRTV = false ; // True if RTV loaded maps and is active.
new bool : g_RTVAllowed = false ; // True if RTV is available to players. Used to delay rtv votes.
new g_Voters = 0 ; // Total voters connected. Doesn't include fake clients.
new g_Votes = 0 ; // Total number of "say rtv" votes
new g_VotesNeeded = 0 ; // Necessary votes before map vote begins. (voters * percent_needed)
new bool : g_Voted [ MAXPLAYERS + 1 ] = { false , . . . } ;
2008-07-01 03:18:52 +02:00
new bool : g_InChange = false ;
2008-03-30 09:00:22 +02:00
public OnPluginStart ( )
{
LoadTranslations ( " common.phrases " ) ;
LoadTranslations ( " rockthevote.phrases " ) ;
g_Cvar_Needed = CreateConVar ( " sm_rtv_needed " , " 0.60 " , " Percentage of players needed to rockthevote (Def 60%) " , 0 , true , 0.05 , true , 1.0 ) ;
g_Cvar_MinPlayers = CreateConVar ( " sm_rtv_minplayers " , " 0 " , " Number of players required before RTV will be enabled. " , 0 , true , 0.0 , true , float ( MAXPLAYERS ) ) ;
2008-07-17 09:47:11 +02:00
g_Cvar_InitialDelay = CreateConVar ( " sm_rtv_initialdelay " , " 30.0 " , " Time (in seconds) before first RTV can be held " , 0 , true , 0.00 ) ;
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 ) ;
2008-07-01 03:18:52 +02:00
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 ) ;
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 ) ;
2008-03-30 09:00:22 +02:00
RegConsoleCmd ( " say " , Command_Say ) ;
RegConsoleCmd ( " say_team " , Command_Say ) ;
2008-07-01 03:18:52 +02:00
RegConsoleCmd ( " sm_rtv " , Command_RTV ) ;
2008-03-30 09:00:22 +02:00
AutoExecConfig ( true , " rtv " ) ;
}
public OnMapStart ( )
{
g_Voters = 0 ;
g_Votes = 0 ;
g_VotesNeeded = 0 ;
2008-07-01 03:18:52 +02:00
g_InChange = false ;
/* Handle late load */
2008-10-15 09:42:47 +02:00
for ( new i = 1 ; i < = MaxClients ; i + + )
2008-07-01 03:18:52 +02:00
{
if ( IsClientConnected ( i ) )
{
2008-10-19 06:44:19 +02:00
OnClientConnected ( i ) ;
2008-07-01 03:18:52 +02:00
}
}
2008-03-30 09:00:22 +02:00
}
public OnMapEnd ( )
{
g_CanRTV = false ;
g_RTVAllowed = false ;
}
public OnConfigsExecuted ( )
2008-07-01 03:18:52 +02:00
{
2008-03-30 09:00:22 +02:00
g_CanRTV = true ;
2008-07-01 03:18:52 +02:00
g_RTVAllowed = false ;
CreateTimer ( GetConVarFloat ( g_Cvar_InitialDelay ) , Timer_DelayRTV , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
2008-03-30 09:00:22 +02:00
}
2008-10-19 05:18:22 +02:00
public OnClientConnected ( client )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
if ( IsFakeClient ( client ) )
2008-10-19 05:18:22 +02:00
return ;
2008-03-30 09:00:22 +02:00
g_Voted [ client ] = false ;
g_Voters + + ;
g_VotesNeeded = RoundToFloor ( float ( g_Voters ) * GetConVarFloat ( g_Cvar_Needed ) ) ;
2008-10-19 05:18:22 +02:00
return ;
2008-03-30 09:00:22 +02:00
}
public OnClientDisconnect ( client )
{
2008-10-19 05:18:22 +02:00
if ( IsFakeClient ( client ) )
2008-03-30 09:00:22 +02:00
return ;
if ( g_Voted [ client ] )
{
g_Votes - - ;
}
g_Voters - - ;
g_VotesNeeded = RoundToFloor ( float ( g_Voters ) * GetConVarFloat ( g_Cvar_Needed ) ) ;
2008-10-19 05:18:22 +02:00
if ( ! g_CanRTV )
{
return ;
}
2008-07-01 03:18:52 +02:00
if ( g_Votes & &
g_Voters & &
g_Votes > = g_VotesNeeded & &
g_RTVAllowed )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
if ( GetConVarInt ( g_Cvar_RTVPostVoteAction ) = = 1 & & HasEndOfMapVoteFinished ( ) )
{
return ;
}
StartRTV ( ) ;
2008-03-30 09:00:22 +02:00
}
}
2008-07-01 03:18:52 +02:00
public Action : Command_RTV ( client , args )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
if ( ! g_CanRTV | | ! client )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
return Plugin_Continue ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
AttemptRTV ( client ) ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
return Plugin_Continue ;
2008-03-30 09:00:22 +02:00
}
public Action : Command_Say ( client , args )
{
if ( ! g_CanRTV | | ! client )
{
return Plugin_Continue ;
}
2008-07-01 03:18:52 +02:00
2008-03-30 09:00:22 +02:00
decl String : text [ 192 ] ;
if ( ! GetCmdArgString ( text , sizeof ( text ) ) )
{
return Plugin_Continue ;
}
new startidx = 0 ;
if ( text [ strlen ( text ) - 1 ] = = '"' )
{
text [ strlen ( text ) - 1 ] = '\0' ;
startidx = 1 ;
}
2008-07-01 03:18:52 +02:00
new ReplySource : old = SetCmdReplySource ( SM_REPLY_TO_CHAT ) ;
2008-03-30 09:00:22 +02:00
if ( strcmp ( text [ startidx ] , " rtv " , false ) = = 0 | | strcmp ( text [ startidx ] , " rockthevote " , false ) = = 0 )
{
2008-07-01 03:18:52 +02:00
AttemptRTV ( client ) ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
SetCmdReplySource ( old ) ;
2008-03-30 09:00:22 +02:00
return Plugin_Continue ;
}
2008-07-01 03:18:52 +02:00
AttemptRTV ( client )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
if ( ! g_RTVAllowed | | ( GetConVarInt ( g_Cvar_RTVPostVoteAction ) = = 1 & & HasEndOfMapVoteFinished ( ) ) )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
ReplyToCommand ( client , " [SM] %t " , " RTV Not Allowed " ) ;
2008-03-30 09:00:22 +02:00
return ;
}
2008-07-01 03:18:52 +02:00
if ( ! CanMapChooserStartVote ( ) )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
ReplyToCommand ( client , " [SM] %t " , " RTV Started " ) ;
return ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
if ( GetClientCount ( true ) < GetConVarInt ( g_Cvar_MinPlayers ) )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
ReplyToCommand ( client , " [SM] %t " , " Minimal Players Not Met " ) ;
return ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
if ( g_Voted [ client ] )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
ReplyToCommand ( client , " [SM] %t " , " Already Voted " ) ;
return ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
new String : name [ 64 ] ;
GetClientName ( client , name , sizeof ( name ) ) ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
g_Votes + + ;
g_Voted [ client ] = true ;
PrintToChatAll ( " [SM] %t " , " RTV Requested " , name , g_Votes , g_VotesNeeded ) ;
if ( g_Votes > = g_VotesNeeded )
{
StartRTV ( ) ;
}
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
public Action : Timer_DelayRTV ( Handle : timer )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
g_RTVAllowed = true ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
StartRTV ( )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
if ( g_InChange )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
return ;
}
2008-08-08 02:06:14 +02:00
if ( EndOfMapVoteEnabled ( ) & & HasEndOfMapVoteFinished ( ) )
2008-07-01 03:18:52 +02:00
{
/* Change right now then */
new String : map [ 65 ] ;
if ( GetNextMap ( map , sizeof ( map ) ) )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
PrintToChatAll ( " [SM] %t " , " Changing Maps " , map ) ;
CreateTimer ( 5.0 , Timer_ChangeMap , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
g_InChange = true ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
ResetRTV ( ) ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
g_RTVAllowed = false ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
return ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
if ( CanMapChooserStartVote ( ) )
{
new MapChange : when = MapChange : GetConVarInt ( g_Cvar_ChangeTime ) ;
InitiateMapChooserVote ( when ) ;
ResetRTV ( ) ;
g_RTVAllowed = false ;
CreateTimer ( GetConVarFloat ( g_Cvar_Interval ) , Timer_DelayRTV , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
}
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
ResetRTV ( )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
g_Votes = 0 ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
for ( new i = 1 ; i < = MAXPLAYERS ; i + + )
{
g_Voted [ i ] = false ;
2008-03-30 09:00:22 +02:00
}
}
2008-07-01 03:18:52 +02:00
public Action : Timer_ChangeMap ( Handle : hTimer )
2008-03-30 09:00:22 +02:00
{
2008-07-01 03:18:52 +02:00
g_InChange = false ;
2008-03-30 09:00:22 +02:00
2008-07-01 03:18:52 +02:00
LogMessage ( " RTV changing map manually " ) ;
new String : map [ 65 ] ;
if ( GetNextMap ( map , sizeof ( map ) ) )
{
2008-07-13 07:13:37 +02:00
ForceChangeLevel ( map , " RTV after mapvote " ) ;
2008-03-30 09:00:22 +02:00
}
2008-07-01 03:18:52 +02:00
return Plugin_Stop ;
2008-03-30 09:00:22 +02:00
}