plugins/funvotes.sp
branchrefac-jit
changeset 2168 c24a6fa957db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/funvotes.sp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,355 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Fun Votes Plugin
     1.8 + * Implements extra fun vote commands.
     1.9 + *
    1.10 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
    1.11 + * =============================================================================
    1.12 + *
    1.13 + * This program is free software; you can redistribute it and/or modify it under
    1.14 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.15 + * Free Software Foundation.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.18 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.19 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.20 + * details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License along with
    1.23 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.24 + *
    1.25 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.26 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.27 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.28 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.29 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.30 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.31 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.32 + * or <http://www.sourcemod.net/license.php>.
    1.33 + *
    1.34 + * Version: $Id$
    1.35 + */
    1.36 +
    1.37 +#pragma semicolon 1
    1.38 +
    1.39 +#include <sourcemod>
    1.40 +#include <sdktools>
    1.41 +
    1.42 +#undef REQUIRE_PLUGIN
    1.43 +#include <adminmenu>
    1.44 +
    1.45 +public Plugin:myinfo =
    1.46 +{
    1.47 +	name = "Fun Votes",
    1.48 +	author = "AlliedModders LLC",
    1.49 +	description = "Fun Vote Commands",
    1.50 +	version = SOURCEMOD_VERSION,
    1.51 +	url = "http://www.sourcemod.net/"
    1.52 +};
    1.53 +
    1.54 +#define VOTE_NO "###no###"
    1.55 +#define VOTE_YES "###yes###"
    1.56 +
    1.57 +new Handle:g_hVoteMenu = INVALID_HANDLE;
    1.58 +
    1.59 +new Handle:g_Cvar_Limits[5] = {INVALID_HANDLE, ...};
    1.60 +new Handle:g_Cvar_Gravity = INVALID_HANDLE;
    1.61 +new Handle:g_Cvar_Alltalk = INVALID_HANDLE;
    1.62 +new Handle:g_Cvar_FF = INVALID_HANDLE;
    1.63 +
    1.64 +// new Handle:g_Cvar_Show = INVALID_HANDLE;
    1.65 +
    1.66 +enum voteType
    1.67 +{
    1.68 +	gravity = 0,
    1.69 +	burn,
    1.70 +	slay,
    1.71 +	alltalk,
    1.72 +	ff
    1.73 +};
    1.74 +
    1.75 +new voteType:g_voteType = voteType:gravity;
    1.76 +
    1.77 +// Menu API does not provide us with a way to pass multiple peices of data with a single
    1.78 +// choice, so some globals are used to hold stuff.
    1.79 +//
    1.80 +#define VOTE_CLIENTID	0
    1.81 +#define VOTE_USERID	1
    1.82 +new g_voteClient[2];		/* Holds the target's client id and user id */
    1.83 +
    1.84 +#define VOTE_NAME	0
    1.85 +#define VOTE_AUTHID	1
    1.86 +#define	VOTE_IP		2
    1.87 +new String:g_voteInfo[3][65];	/* Holds the target's name, authid, and IP */
    1.88 +
    1.89 +new Handle:hTopMenu = INVALID_HANDLE;
    1.90 +
    1.91 +#include "funvotes/votegravity.sp"
    1.92 +#include "funvotes/voteburn.sp"
    1.93 +#include "funvotes/voteslay.sp"
    1.94 +#include "funvotes/votealltalk.sp"
    1.95 +#include "funvotes/voteff.sp"
    1.96 +
    1.97 +public OnPluginStart()
    1.98 +{
    1.99 +	if (FindPluginByFile("basefunvotes.smx") != INVALID_HANDLE)
   1.100 +	{
   1.101 +		ThrowError("This plugin replaces basefuncommands.  You cannot run both at once.");
   1.102 +	}
   1.103 +	
   1.104 +	LoadTranslations("common.phrases");
   1.105 +	LoadTranslations("basevotes.phrases");
   1.106 +	LoadTranslations("funvotes.phrases");
   1.107 +	LoadTranslations("funcommands.phrases");
   1.108 +	
   1.109 +	RegAdminCmd("sm_votegravity", Command_VoteGravity, ADMFLAG_VOTE, "sm_votegravity <amount> [amount2] ... [amount5]");
   1.110 +	RegAdminCmd("sm_voteburn", Command_VoteBurn, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteburn <player>");
   1.111 +	RegAdminCmd("sm_voteslay", Command_VoteSlay, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteslay <player>");
   1.112 +	RegAdminCmd("sm_votealltalk", Command_VoteAlltalk, ADMFLAG_VOTE, "sm_votealltalk");
   1.113 +	RegAdminCmd("sm_voteff", Command_VoteFF, ADMFLAG_VOTE, "sm_voteff");
   1.114 +
   1.115 +	g_Cvar_Limits[0] = CreateConVar("sm_vote_gravity", "0.60", "percent required for successful gravity vote.", 0, true, 0.05, true, 1.0);
   1.116 +	g_Cvar_Limits[1] = CreateConVar("sm_vote_burn", "0.60", "percent required for successful burn vote.", 0, true, 0.05, true, 1.0);
   1.117 +	g_Cvar_Limits[2] = CreateConVar("sm_vote_slay", "0.60", "percent required for successful slay vote.", 0, true, 0.05, true, 1.0);
   1.118 +	g_Cvar_Limits[3] = CreateConVar("sm_vote_alltalk", "0.60", "percent required for successful alltalk vote.", 0, true, 0.05, true, 1.0);
   1.119 +	g_Cvar_Limits[4] = CreateConVar("sm_vote_ff", "0.60", "percent required for successful friendly fire vote.", 0, true, 0.05, true, 1.0);
   1.120 +	
   1.121 +	g_Cvar_Gravity = FindConVar("sv_gravity");
   1.122 +	g_Cvar_Alltalk = FindConVar("sv_alltalk");
   1.123 +	g_Cvar_FF = FindConVar("mp_friendlyfire");
   1.124 +	
   1.125 +	/*
   1.126 +	g_Cvar_Show = FindConVar("sm_vote_show");
   1.127 +	if (g_Cvar_Show == INVALID_HANDLE)
   1.128 +	{
   1.129 +		g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
   1.130 +	}
   1.131 +	*/
   1.132 +	
   1.133 +	/* Account for late loading */
   1.134 +	new Handle:topmenu;
   1.135 +	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
   1.136 +	{
   1.137 +		OnAdminMenuReady(topmenu);
   1.138 +	}
   1.139 +}
   1.140 +
   1.141 +public OnAdminMenuReady(Handle:topmenu)
   1.142 +{
   1.143 +	/* Block us from being called twice */
   1.144 +	if (topmenu == hTopMenu)
   1.145 +	{
   1.146 +		return;
   1.147 +	}
   1.148 +	
   1.149 +	/* Save the Handle */
   1.150 +	hTopMenu = topmenu;
   1.151 +	
   1.152 +	/* Build the "Voting Commands" category */
   1.153 +	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   1.154 +
   1.155 +	if (voting_commands != INVALID_TOPMENUOBJECT)
   1.156 +	{
   1.157 +		AddToTopMenu(hTopMenu,
   1.158 +			"sm_votegravity",
   1.159 +			TopMenuObject_Item,
   1.160 +			AdminMenu_VoteGravity,
   1.161 +			voting_commands,
   1.162 +			"sm_votegravity",
   1.163 +			ADMFLAG_VOTE);
   1.164 +			
   1.165 +		AddToTopMenu(hTopMenu,
   1.166 +			"sm_voteburn",
   1.167 +			TopMenuObject_Item,
   1.168 +			AdminMenu_VoteBurn,
   1.169 +			voting_commands,
   1.170 +			"sm_voteburn",
   1.171 +			ADMFLAG_VOTE|ADMFLAG_SLAY);
   1.172 +			
   1.173 +		AddToTopMenu(hTopMenu,
   1.174 +			"sm_voteslay",
   1.175 +			TopMenuObject_Item,
   1.176 +			AdminMenu_VoteSlay,
   1.177 +			voting_commands,
   1.178 +			"sm_voteslay",
   1.179 +			ADMFLAG_VOTE|ADMFLAG_SLAY);
   1.180 +			
   1.181 +		AddToTopMenu(hTopMenu,
   1.182 +			"sm_votealltalk",
   1.183 +			TopMenuObject_Item,
   1.184 +			AdminMenu_VoteAllTalk,
   1.185 +			voting_commands,
   1.186 +			"sm_votealltalk",
   1.187 +			ADMFLAG_VOTE);
   1.188 +			
   1.189 +		AddToTopMenu(hTopMenu,
   1.190 +			"sm_voteff",
   1.191 +			TopMenuObject_Item,
   1.192 +			AdminMenu_VoteFF,
   1.193 +			voting_commands,
   1.194 +			"sm_voteff",
   1.195 +			ADMFLAG_VOTE);
   1.196 +	}
   1.197 +}
   1.198 +
   1.199 +public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
   1.200 +{
   1.201 +	if (action == MenuAction_End)
   1.202 +	{
   1.203 +		VoteMenuClose();
   1.204 +	}
   1.205 +	else if (action == MenuAction_Display)
   1.206 +	{
   1.207 +	 	decl String:title[64];
   1.208 +		GetMenuTitle(menu, title, sizeof(title));
   1.209 +
   1.210 +	 	decl String:buffer[255];
   1.211 +		Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
   1.212 +
   1.213 +		new Handle:panel = Handle:param2;
   1.214 +		SetPanelTitle(panel, buffer);
   1.215 +	}
   1.216 +	else if (action == MenuAction_DisplayItem)
   1.217 +	{
   1.218 +		decl String:display[64];
   1.219 +		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
   1.220 +	 
   1.221 +	 	if (strcmp(display, VOTE_NO) == 0 || strcmp(display, VOTE_YES) == 0)
   1.222 +	 	{
   1.223 +			decl String:buffer[255];
   1.224 +			Format(buffer, sizeof(buffer), "%T", display, param1);
   1.225 +
   1.226 +			return RedrawMenuItem(buffer);
   1.227 +		}
   1.228 +	}
   1.229 +	/* else if (action == MenuAction_Select)
   1.230 +	{
   1.231 +		VoteSelect(menu, param1, param2);
   1.232 +	}*/
   1.233 +	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
   1.234 +	{
   1.235 +		PrintToChatAll("[SM] %t", "No Votes Cast");
   1.236 +	}
   1.237 +	else if (action == MenuAction_VoteEnd)
   1.238 +	{
   1.239 +		decl String:item[64];
   1.240 +		new Float:percent, Float:limit, votes, totalVotes;
   1.241 +
   1.242 +		GetMenuVoteInfo(param2, votes, totalVotes);
   1.243 +		GetMenuItem(menu, param1, item, sizeof(item));
   1.244 +		
   1.245 +		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
   1.246 +		{
   1.247 +			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
   1.248 +		}
   1.249 +		
   1.250 +		percent = GetVotePercent(votes, totalVotes);
   1.251 +		
   1.252 +		limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
   1.253 +		
   1.254 +		/* :TODO: g_voteClient[userid] needs to be checked.
   1.255 +		 */
   1.256 +
   1.257 +		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
   1.258 +		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
   1.259 +		{
   1.260 +			/* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
   1.261 +			 */
   1.262 +			LogAction(-1, -1, "Vote failed.");
   1.263 +			PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
   1.264 +		}
   1.265 +		else
   1.266 +		{
   1.267 +			PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
   1.268 +			
   1.269 +			switch (g_voteType)
   1.270 +			{
   1.271 +				case (voteType:gravity):
   1.272 +				{
   1.273 +					PrintToChatAll("[SM] %t", "Cvar changed", "sv_gravity", item);					
   1.274 +					LogAction(-1, -1, "Changing gravity to %s due to vote.", item);
   1.275 +					SetConVarInt(g_Cvar_Gravity, StringToInt(item));
   1.276 +				}
   1.277 +				
   1.278 +				case (voteType:burn):
   1.279 +				{
   1.280 +					PrintToChatAll("[SM] %t", "Set target on fire", "_s", g_voteInfo[VOTE_NAME]);					
   1.281 +					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote burn successful, igniting \"%L\"", g_voteClient[VOTE_CLIENTID]);
   1.282 +					
   1.283 +					IgniteEntity(g_voteClient[VOTE_CLIENTID], 19.8);	
   1.284 +				}
   1.285 +				
   1.286 +				case (voteType:slay):
   1.287 +				{
   1.288 +					PrintToChatAll("[SM] %t", "Slayed player", g_voteInfo[VOTE_NAME]);					
   1.289 +					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote slay successful, slaying \"%L\"", g_voteClient[VOTE_CLIENTID]);
   1.290 +					
   1.291 +					ExtinguishEntity(g_voteClient[VOTE_CLIENTID]);
   1.292 +					ForcePlayerSuicide(g_voteClient[VOTE_CLIENTID]);
   1.293 +				}
   1.294 +				
   1.295 +				case (voteType:alltalk):
   1.296 +				{
   1.297 +					PrintToChatAll("[SM] %t", "Cvar changed", "sv_alltalk", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
   1.298 +					LogAction(-1, -1, "Changing alltalk to %s due to vote.", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
   1.299 +					SetConVarBool(g_Cvar_Alltalk, !GetConVarBool(g_Cvar_Alltalk));
   1.300 +				}
   1.301 +				
   1.302 +				case (voteType:ff):
   1.303 +				{
   1.304 +					PrintToChatAll("[SM] %t", "Cvar changed", "mp_friendlyfire", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
   1.305 +					LogAction(-1, -1, "Changing friendly fire to %s due to vote.", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
   1.306 +					SetConVarBool(g_Cvar_FF, !GetConVarBool(g_Cvar_FF));
   1.307 +				}				
   1.308 +			}
   1.309 +		}
   1.310 +	}
   1.311 +	
   1.312 +	return 0;
   1.313 +}
   1.314 +
   1.315 +/*
   1.316 +VoteSelect(Handle:menu, param1, param2 = 0)
   1.317 +{
   1.318 +	if (GetConVarInt(g_Cvar_VoteShow) == 1)
   1.319 +	{
   1.320 +		decl String:voter[64], String:junk[64], String:choice[64];
   1.321 +		GetClientName(param1, voter, sizeof(voter));
   1.322 +		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
   1.323 +		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
   1.324 +	}
   1.325 +}
   1.326 +*/
   1.327 +
   1.328 +VoteMenuClose()
   1.329 +{
   1.330 +	CloseHandle(g_hVoteMenu);
   1.331 +	g_hVoteMenu = INVALID_HANDLE;
   1.332 +}
   1.333 +
   1.334 +Float:GetVotePercent(votes, totalVotes)
   1.335 +{
   1.336 +	return FloatDiv(float(votes),float(totalVotes));
   1.337 +}
   1.338 +
   1.339 +bool:TestVoteDelay(client)
   1.340 +{
   1.341 + 	new delay = CheckVoteDelay();
   1.342 + 	
   1.343 + 	if (delay > 0)
   1.344 + 	{
   1.345 + 		if (delay > 60)
   1.346 + 		{
   1.347 + 			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
   1.348 + 		}
   1.349 + 		else
   1.350 + 		{
   1.351 + 			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
   1.352 + 		}
   1.353 + 		
   1.354 + 		return false;
   1.355 + 	}
   1.356 + 	
   1.357 +	return true;
   1.358 +}