2016-01-19 23:57:32 +01:00
/ * *
* vim : set ts = 4 :
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2017-01-06 19:16:02 +01:00
* SourceMod Rock The Vote Plugin
2016-01-19 23:57:32 +01:00
* Creates a map vote when the required number of players have requested one .
*
2017-01-06 19:16:02 +01:00
* SourceMod ( C ) 2004 - 2008 AlliedModders LLC . All rights reserved .
2016-01-19 23:57:32 +01:00
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
* 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 $
* /
2017-02-26 00:16:12 +01:00
# pragma semicolon 1
# pragma newdecls required
2016-01-19 23:57:32 +01:00
# include <sourcemod>
2017-01-06 19:16:02 +01:00
# include <sdktools_functions>
2016-01-19 23:57:32 +01:00
# include <mapchooser>
# include <nextmap>
2017-01-06 19:16:02 +01:00
# define MCE_VERSION "1.13.0"
2016-01-19 23:57:32 +01:00
2016-12-19 08:34:52 +01:00
public Plugin myinfo =
2016-01-19 23:57:32 +01:00
{
name = " Rock The Vote Extended " ,
author = " Powerlord and AlliedModders LLC " ,
description = " Provides RTV Map Voting " ,
version = MCE_VERSION ,
url = " https://forums.alliedmods.net/showthread.php?t=156974 "
} ;
2017-01-06 19:16:02 +01:00
ConVar g_Cvar_Needed ;
ConVar g_Cvar_MinPlayers ;
ConVar g_Cvar_InitialDelay ;
ConVar g_Cvar_Interval ;
ConVar g_Cvar_ChangeTime ;
ConVar g_Cvar_RTVPostVoteAction ;
2016-01-19 23:57:32 +01:00
2016-12-19 08:34:52 +01:00
bool g_CanRTV = false ; // True if RTV loaded maps and is active.
bool g_RTVAllowed = false ; // True if RTV is available to players. Used to delay rtv votes.
int g_Voters = 0 ; // Total voters connected. Doesn't include fake clients.
int g_Votes = 0 ; // Total number of "say rtv" votes
int g_VotesNeeded = 0 ; // Necessary votes before map vote begins. (voters * percent_needed)
bool g_Voted [ MAXPLAYERS + 1 ] = { false , . . . } ;
2016-01-19 23:57:32 +01:00
2016-12-19 08:34:52 +01:00
bool g_InChange = false ;
2016-01-19 23:57:32 +01:00
2016-12-19 08:34:52 +01:00
public void OnPluginStart ( )
2016-01-19 23:57:32 +01:00
{
LoadTranslations ( " common.phrases " ) ;
LoadTranslations ( " rockthevote.phrases " ) ;
LoadTranslations ( " basevotes.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 ) ) ;
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 ) ;
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 ) ;
RegConsoleCmd ( " sm_rtv " , Command_RTV ) ;
RegAdminCmd ( " sm_forcertv " , Command_ForceRTV , ADMFLAG_CHANGEMAP , " Force an RTV vote " ) ;
2017-01-06 19:16:02 +01:00
RegAdminCmd ( " sm_disablertv " , Command_DisableRTV , ADMFLAG_CHANGEMAP , " Disable the RTV command " ) ;
RegAdminCmd ( " sm_enablertv " , Command_EnableRTV , ADMFLAG_CHANGEMAP , " Enable the RTV command " ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
HookEvent ( " player_team " , OnPlayerChangedTeam , EventHookMode_PostNoCopy ) ;
2016-01-19 23:57:32 +01:00
AutoExecConfig ( true , " rtv " ) ;
}
2016-12-19 08:34:52 +01:00
public void OnMapStart ( )
2016-01-19 23:57:32 +01:00
{
g_Voters = 0 ;
g_Votes = 0 ;
g_VotesNeeded = 0 ;
g_InChange = false ;
2017-01-06 19:16:02 +01:00
/* Handle late load */
for ( int i = 1 ; i < = MaxClients ; i + + )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( IsClientConnected ( i ) )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
OnClientConnected ( i ) ;
2016-01-19 23:57:32 +01:00
}
}
}
2016-12-19 08:34:52 +01:00
public void OnMapEnd ( )
2016-01-19 23:57:32 +01:00
{
g_CanRTV = false ;
g_RTVAllowed = false ;
}
2016-12-19 08:34:52 +01:00
public void OnConfigsExecuted ( )
2016-01-19 23:57:32 +01:00
{
g_CanRTV = true ;
g_RTVAllowed = false ;
2017-01-06 19:16:02 +01:00
CreateTimer ( g_Cvar_InitialDelay . FloatValue , Timer_DelayRTV , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
2016-01-19 23:57:32 +01:00
}
2016-12-19 08:34:52 +01:00
public void OnClientConnected ( int client )
2016-01-19 23:57:32 +01:00
{
if ( IsFakeClient ( client ) )
return ;
g_Voted [ client ] = false ;
g_Voters = GetTeamClientCount ( 2 ) + GetTeamClientCount ( 3 ) ;
2017-01-06 19:16:02 +01:00
g_VotesNeeded = RoundToFloor ( float ( g_Voters ) * g_Cvar_Needed . FloatValue ) ;
2016-01-19 23:57:32 +01:00
return ;
}
2016-12-19 08:34:52 +01:00
public void OnClientDisconnect ( int client )
2016-01-19 23:57:32 +01:00
{
if ( IsFakeClient ( client ) )
return ;
if ( g_Voted [ client ] )
2017-01-06 19:16:02 +01:00
{
2016-01-19 23:57:32 +01:00
g_Votes - - ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
g_Voters = GetTeamClientCount ( 2 ) + GetTeamClientCount ( 3 ) ;
2017-01-06 19:16:02 +01:00
g_VotesNeeded = RoundToFloor ( float ( g_Voters ) * g_Cvar_Needed . FloatValue ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( ! g_CanRTV )
{
2016-01-19 23:57:32 +01:00
return ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( g_Votes & &
2016-01-19 23:57:32 +01:00
g_Voters & &
g_Votes > = g_VotesNeeded & &
g_RTVAllowed )
{
2017-01-06 19:16:02 +01:00
if ( g_Cvar_RTVPostVoteAction . IntValue = = 1 & & HasEndOfMapVoteFinished ( ) )
{
2016-01-19 23:57:32 +01:00
return ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
StartRTV ( ) ;
}
}
2016-12-19 08:34:52 +01:00
public void OnPlayerChangedTeam ( Handle event , const char [ ] name , bool dontBroadcast )
2016-01-19 23:57:32 +01:00
{
2016-12-19 08:34:52 +01:00
int client = GetClientOfUserId ( GetEventInt ( event , " userid " ) ) ;
if ( client = = 0 | | ! IsClientConnected ( client ) | | ! IsClientInGame ( client ) | | IsFakeClient ( client ) | |
GetClientTeam ( client ) = = 0 | | GetClientTeam ( client ) = = 1 )
2016-01-19 23:57:32 +01:00
return ;
2016-12-19 08:34:52 +01:00
g_Voters = GetTeamClientCount ( 2 ) + GetTeamClientCount ( 3 ) ;
g_VotesNeeded = RoundToFloor ( float ( g_Voters ) * GetConVarFloat ( g_Cvar_Needed ) ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( g_Votes & &
2016-12-19 08:34:52 +01:00
g_Voters & &
g_Votes > = g_VotesNeeded & &
g_RTVAllowed )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( g_Cvar_RTVPostVoteAction . IntValue = = 1 & & HasEndOfMapVoteFinished ( ) )
{
2016-12-19 08:34:52 +01:00
return ;
2017-01-06 19:16:02 +01:00
}
2016-12-19 08:34:52 +01:00
StartRTV ( ) ;
2016-01-19 23:57:32 +01:00
}
}
2017-01-06 19:16:02 +01:00
public void OnClientSayCommand_Post ( int client , const char [ ] command , const char [ ] sArgs )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( ! g_CanRTV | | ! client )
{
return ;
}
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( strcmp ( sArgs , " rtv " , false ) = = 0 | | strcmp ( sArgs , " rockthevote " , false ) = = 0 )
{
ReplySource old = SetCmdReplySource ( SM_REPLY_TO_CHAT ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
AttemptRTV ( client ) ;
SetCmdReplySource ( old ) ;
}
2016-01-19 23:57:32 +01:00
}
2017-01-06 19:16:02 +01:00
public Action Command_RTV ( int client , int args )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( ! g_CanRTV | | ! client )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
return Plugin_Handled ;
2016-01-19 23:57:32 +01:00
}
2017-01-06 19:16:02 +01:00
AttemptRTV ( client ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
return Plugin_Handled ;
2016-01-19 23:57:32 +01:00
}
2016-12-19 08:34:52 +01:00
void AttemptRTV ( int client )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( ! g_RTVAllowed | | ( g_Cvar_RTVPostVoteAction . IntValue = = 1 & & HasEndOfMapVoteFinished ( ) ) )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
ReplyToCommand ( client , " [SM] %t " , " RTV Not Allowed " ) ;
2016-01-19 23:57:32 +01:00
return ;
}
2017-01-06 19:16:02 +01:00
if ( ! CanMapChooserStartVote ( ) )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
ReplyToCommand ( client , " [SM] %t " , " RTV Started " ) ;
2016-01-19 23:57:32 +01:00
return ;
}
2017-01-06 19:16:02 +01:00
if ( GetClientCount ( true ) < g_Cvar_MinPlayers . IntValue )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
ReplyToCommand ( client , " [SM] %t " , " Minimal Players Not Met " ) ;
2016-01-19 23:57:32 +01:00
return ;
}
2017-01-06 19:16:02 +01:00
if ( g_Voted [ client ] )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
ReplyToCommand ( client , " [SM] %t " , " Already Voted " , g_Votes , g_VotesNeeded ) ;
2016-01-19 23:57:32 +01:00
return ;
}
2016-12-19 08:34:52 +01:00
char name [ MAX_NAME_LENGTH ] ;
2016-01-19 23:57:32 +01:00
GetClientName ( client , name , sizeof ( name ) ) ;
g_Votes + + ;
g_Voted [ client ] = true ;
2017-01-06 19:16:02 +01:00
PrintToChatAll ( " [SM] %t " , " RTV Requested " , name , g_Votes , g_VotesNeeded ) ;
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( g_Votes > = g_VotesNeeded )
{
2016-01-19 23:57:32 +01:00
StartRTV ( ) ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
}
2016-12-19 08:34:52 +01:00
public Action Timer_DelayRTV ( Handle timer )
2016-01-19 23:57:32 +01:00
{
g_RTVAllowed = true ;
}
2016-12-19 08:34:52 +01:00
void StartRTV ( )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( g_InChange )
{
2016-01-19 23:57:32 +01:00
return ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
2017-01-06 19:16:02 +01:00
if ( EndOfMapVoteEnabled ( ) & & HasEndOfMapVoteFinished ( ) )
2016-01-19 23:57:32 +01:00
{
/* Change right now then */
2016-12-19 08:34:52 +01:00
char map [ PLATFORM_MAX_PATH ] ;
2017-01-06 19:16:02 +01:00
if ( GetNextMap ( map , sizeof ( map ) ) )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
GetMapDisplayName ( map , map , sizeof ( map ) ) ;
PrintToChatAll ( " [SM] %t " , " Changing Maps " , map ) ;
2016-01-19 23:57:32 +01:00
CreateTimer ( 5.0 , Timer_ChangeMap , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
g_InChange = true ;
ResetRTV ( ) ;
g_RTVAllowed = false ;
}
return ;
}
2017-01-06 19:16:02 +01:00
if ( CanMapChooserStartVote ( ) )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
MapChange when = view_as < MapChange > ( g_Cvar_ChangeTime . IntValue ) ;
2016-01-19 23:57:32 +01:00
InitiateMapChooserVote ( when ) ;
ResetRTV ( ) ;
g_RTVAllowed = false ;
2017-01-06 19:16:02 +01:00
CreateTimer ( g_Cvar_Interval . FloatValue , Timer_DelayRTV , _ , TIMER_FLAG_NO_MAPCHANGE ) ;
2016-01-19 23:57:32 +01:00
}
}
2016-12-19 08:34:52 +01:00
void ResetRTV ( )
2016-01-19 23:57:32 +01:00
{
g_Votes = 0 ;
2017-01-06 19:16:02 +01:00
for ( int i = 1 ; i < = MAXPLAYERS ; i + + )
{
2016-01-19 23:57:32 +01:00
g_Voted [ i ] = false ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
}
2016-12-19 08:34:52 +01:00
public Action Timer_ChangeMap ( Handle hTimer )
2016-01-19 23:57:32 +01:00
{
g_InChange = false ;
LogMessage ( " RTV changing map manually " ) ;
2016-12-19 08:34:52 +01:00
char map [ PLATFORM_MAX_PATH ] ;
2017-01-06 19:16:02 +01:00
if ( GetNextMap ( map , sizeof ( map ) ) )
{
2016-01-19 23:57:32 +01:00
ForceChangeLevel ( map , " RTV after mapvote " ) ;
2017-01-06 19:16:02 +01:00
}
2016-01-19 23:57:32 +01:00
return Plugin_Stop ;
}
2016-12-19 08:34:52 +01:00
public Action Command_ForceRTV ( int client , int args )
2016-01-19 23:57:32 +01:00
{
2017-01-06 19:16:02 +01:00
if ( ! g_CanRTV )
2016-01-19 23:57:32 +01:00
return Plugin_Handled ;
ShowActivity2 ( client , " [RTVE] " , " %t " , " Initiated Vote Map " ) ;
StartRTV ( ) ;
return Plugin_Handled ;
}
2017-01-06 19:16:02 +01:00
public Action Command_DisableRTV ( int client , int args )
{
if ( ! g_RTVAllowed )
return Plugin_Handled ;
ShowActivity2 ( client , " [RTVE] " , " disabled RockTheVote. " ) ;
g_RTVAllowed = false ;
return Plugin_Handled ;
}
public Action Command_EnableRTV ( int client , int args )
{
if ( g_RTVAllowed )
return Plugin_Handled ;
ShowActivity2 ( client , " [RTVE] " , " enabled RockTheVote " ) ;
g_RTVAllowed = true ;
return Plugin_Handled ;
}