plugins/basefunvotes.sp
author David Anderson <dvander@alliedmods.net>
Sun Dec 02 05:33:57 2007 +0000 (2007-12-02)
changeset 1742 efe29eb4200a
parent 1625 8e4a6a8c3256
permissions -rw-r--r--
committed vcproj changes left around
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Fun Votes Plugin
     5  * Implements extra fun vote commands.
     6  *
     7  * SourceMod (C)2004-2007 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 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 #include <sdktools>
    38 
    39 #undef REQUIRE_PLUGIN
    40 #include <adminmenu>
    41 
    42 public Plugin:myinfo =
    43 {
    44 	name = "Basic Fun Votes",
    45 	author = "AlliedModders LLC",
    46 	description = "Fun Vote Commands",
    47 	version = SOURCEMOD_VERSION,
    48 	url = "http://www.sourcemod.net/"
    49 };
    50 
    51 #define VOTE_NO "###no###"
    52 #define VOTE_YES "###yes###"
    53 
    54 new Handle:g_hVoteMenu = INVALID_HANDLE;
    55 
    56 new Handle:g_Cvar_Limits[5] = {INVALID_HANDLE, ...};
    57 new Handle:g_Cvar_Gravity = INVALID_HANDLE;
    58 new Handle:g_Cvar_Alltalk = INVALID_HANDLE;
    59 new Handle:g_Cvar_FF = INVALID_HANDLE;
    60 
    61 // new Handle:g_Cvar_Show = INVALID_HANDLE;
    62 
    63 enum voteType
    64 {
    65 	gravity = 0,
    66 	burn,
    67 	slay,
    68 	alltalk,
    69 	ff
    70 };
    71 
    72 new voteType:g_voteType = voteType:gravity;
    73 
    74 // Menu API does not provide us with a way to pass multiple peices of data with a single
    75 // choice, so some globals are used to hold stuff.
    76 //
    77 #define VOTE_CLIENTID	0
    78 #define VOTE_USERID	1
    79 new g_voteClient[2];		/* Holds the target's client id and user id */
    80 
    81 #define VOTE_NAME	0
    82 #define VOTE_AUTHID	1
    83 #define	VOTE_IP		2
    84 new String:g_voteInfo[3][65];	/* Holds the target's name, authid, and IP */
    85 
    86 new Handle:hTopMenu = INVALID_HANDLE;
    87 
    88 #include "basefunvotes/votegravity.sp"
    89 #include "basefunvotes/voteburn.sp"
    90 #include "basefunvotes/voteslay.sp"
    91 #include "basefunvotes/votealltalk.sp"
    92 #include "basefunvotes/voteff.sp"
    93 
    94 public OnPluginStart()
    95 {
    96 	LoadTranslations("common.phrases");
    97 	LoadTranslations("basevotes.phrases");
    98 	LoadTranslations("basefunvotes.phrases");
    99 	LoadTranslations("basefuncommands.phrases");
   100 	
   101 	RegAdminCmd("sm_votegravity", Command_VoteGravity, ADMFLAG_VOTE, "sm_votegravity <amount> [amount2] ... [amount5]");
   102 	RegAdminCmd("sm_voteburn", Command_VoteBurn, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteburn <player>");
   103 	RegAdminCmd("sm_voteslay", Command_VoteSlay, ADMFLAG_VOTE|ADMFLAG_SLAY, "sm_voteslay <player>");
   104 	RegAdminCmd("sm_votealltalk", Command_VoteAlltalk, ADMFLAG_VOTE, "sm_votealltalk");
   105 	RegAdminCmd("sm_voteff", Command_VoteFF, ADMFLAG_VOTE, "sm_voteff");
   106 
   107 	g_Cvar_Limits[0] = CreateConVar("sm_vote_gravity", "0.60", "percent required for successful gravity vote.", 0, true, 0.05, true, 1.0);
   108 	g_Cvar_Limits[1] = CreateConVar("sm_vote_burn", "0.60", "percent required for successful burn vote.", 0, true, 0.05, true, 1.0);
   109 	g_Cvar_Limits[2] = CreateConVar("sm_vote_slay", "0.60", "percent required for successful slay vote.", 0, true, 0.05, true, 1.0);
   110 	g_Cvar_Limits[3] = CreateConVar("sm_vote_alltalk", "0.60", "percent required for successful alltalk vote.", 0, true, 0.05, true, 1.0);
   111 	g_Cvar_Limits[4] = CreateConVar("sm_vote_ff", "0.60", "percent required for successful friendly fire vote.", 0, true, 0.05, true, 1.0);
   112 	
   113 	g_Cvar_Gravity = FindConVar("sv_gravity");
   114 	g_Cvar_Alltalk = FindConVar("sv_alltalk");
   115 	g_Cvar_FF = FindConVar("mp_friendlyfire");
   116 	
   117 	/*
   118 	g_Cvar_Show = FindConVar("sm_vote_show");
   119 	if (g_Cvar_Show == INVALID_HANDLE)
   120 	{
   121 		g_Cvar_Show = CreateConVar("sm_vote_show", "1", "Show player's votes? Default on.", 0, true, 0.0, true, 1.0);
   122 	}
   123 	*/
   124 	
   125 	/* Account for late loading */
   126 	new Handle:topmenu;
   127 	if (LibraryExists("adminmenu") && ((topmenu = GetAdminTopMenu()) != INVALID_HANDLE))
   128 	{
   129 		OnAdminMenuReady(topmenu);
   130 	}
   131 }
   132 
   133 public OnAdminMenuReady(Handle:topmenu)
   134 {
   135 	/* Block us from being called twice */
   136 	if (topmenu == hTopMenu)
   137 	{
   138 		return;
   139 	}
   140 	
   141 	/* Save the Handle */
   142 	hTopMenu = topmenu;
   143 	
   144 	/* Build the "Voting Commands" category */
   145 	new TopMenuObject:voting_commands = FindTopMenuCategory(hTopMenu, ADMINMENU_VOTINGCOMMANDS);
   146 
   147 	if (voting_commands != INVALID_TOPMENUOBJECT)
   148 	{
   149 		AddToTopMenu(hTopMenu,
   150 			"sm_votegravity",
   151 			TopMenuObject_Item,
   152 			AdminMenu_VoteGravity,
   153 			voting_commands,
   154 			"sm_votegravity",
   155 			ADMFLAG_VOTE);
   156 			
   157 		AddToTopMenu(hTopMenu,
   158 			"sm_voteburn",
   159 			TopMenuObject_Item,
   160 			AdminMenu_VoteBurn,
   161 			voting_commands,
   162 			"sm_voteburn",
   163 			ADMFLAG_VOTE|ADMFLAG_SLAY);
   164 			
   165 		AddToTopMenu(hTopMenu,
   166 			"sm_voteslay",
   167 			TopMenuObject_Item,
   168 			AdminMenu_VoteSlay,
   169 			voting_commands,
   170 			"sm_voteslay",
   171 			ADMFLAG_VOTE|ADMFLAG_SLAY);
   172 			
   173 		AddToTopMenu(hTopMenu,
   174 			"sm_votealltalk",
   175 			TopMenuObject_Item,
   176 			AdminMenu_VoteAllTalk,
   177 			voting_commands,
   178 			"sm_votealltalk",
   179 			ADMFLAG_VOTE);
   180 			
   181 		AddToTopMenu(hTopMenu,
   182 			"sm_voteff",
   183 			TopMenuObject_Item,
   184 			AdminMenu_VoteFF,
   185 			voting_commands,
   186 			"sm_voteff",
   187 			ADMFLAG_VOTE);
   188 	}
   189 }
   190 
   191 public Handler_VoteCallback(Handle:menu, MenuAction:action, param1, param2)
   192 {
   193 	if (action == MenuAction_End)
   194 	{
   195 		VoteMenuClose();
   196 	}
   197 	else if (action == MenuAction_Display)
   198 	{
   199 	 	decl String:title[64];
   200 		GetMenuTitle(menu, title, sizeof(title));
   201 
   202 	 	decl String:buffer[255];
   203 		Format(buffer, sizeof(buffer), "%T", title, param1, g_voteInfo[VOTE_NAME]);
   204 
   205 		new Handle:panel = Handle:param2;
   206 		SetPanelTitle(panel, buffer);
   207 	}
   208 	else if (action == MenuAction_DisplayItem)
   209 	{
   210 		decl String:display[64];
   211 		GetMenuItem(menu, param2, "", 0, _, display, sizeof(display));
   212 	 
   213 	 	if (strcmp(display, VOTE_NO) == 0 || strcmp(display, VOTE_YES) == 0)
   214 	 	{
   215 			decl String:buffer[255];
   216 			Format(buffer, sizeof(buffer), "%T", display, param1);
   217 
   218 			return RedrawMenuItem(buffer);
   219 		}
   220 	}
   221 	/* else if (action == MenuAction_Select)
   222 	{
   223 		VoteSelect(menu, param1, param2);
   224 	}*/
   225 	else if (action == MenuAction_VoteCancel && param1 == VoteCancel_NoVotes)
   226 	{
   227 		PrintToChatAll("[SM] %t", "No Votes Cast");
   228 	}
   229 	else if (action == MenuAction_VoteEnd)
   230 	{
   231 		decl String:item[64];
   232 		new Float:percent, Float:limit, votes, totalVotes;
   233 
   234 		GetMenuVoteInfo(param2, votes, totalVotes);
   235 		GetMenuItem(menu, param1, item, sizeof(item));
   236 		
   237 		if (strcmp(item, VOTE_NO) == 0 && param1 == 1)
   238 		{
   239 			votes = totalVotes - votes; // Reverse the votes to be in relation to the Yes option.
   240 		}
   241 		
   242 		percent = GetVotePercent(votes, totalVotes);
   243 		
   244 		limit = GetConVarFloat(g_Cvar_Limits[g_voteType]);
   245 		
   246 		/* :TODO: g_voteClient[userid] needs to be checked.
   247 		 */
   248 
   249 		// A multi-argument vote is "always successful", but have to check if its a Yes/No vote.
   250 		if ((strcmp(item, VOTE_YES) == 0 && FloatCompare(percent,limit) < 0 && param1 == 0) || (strcmp(item, VOTE_NO) == 0 && param1 == 1))
   251 		{
   252 			/* :TODO: g_voteClient[userid] should be used here and set to -1 if not applicable.
   253 			 */
   254 			LogAction(-1, -1, "Vote failed.");
   255 			PrintToChatAll("[SM] %t", "Vote Failed", RoundToNearest(100.0*limit), RoundToNearest(100.0*percent), totalVotes);
   256 		}
   257 		else
   258 		{
   259 			PrintToChatAll("[SM] %t", "Vote Successful", RoundToNearest(100.0*percent), totalVotes);
   260 			
   261 			switch (g_voteType)
   262 			{
   263 				case (voteType:gravity):
   264 				{
   265 					PrintToChatAll("[SM] %t", "Cvar changed", "sv_gravity", item);					
   266 					LogAction(-1, -1, "Changing gravity to %s due to vote.", item);
   267 					SetConVarInt(g_Cvar_Gravity, StringToInt(item));
   268 				}
   269 				
   270 				case (voteType:burn):
   271 				{
   272 					PrintToChatAll("[SM] %t", "Set target on fire", "_s", g_voteInfo[VOTE_NAME]);					
   273 					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote burn successful, igniting \"%L\"", g_voteClient[VOTE_CLIENTID]);
   274 					
   275 					IgniteEntity(g_voteClient[VOTE_CLIENTID], 19.8);	
   276 				}
   277 				
   278 				case (voteType:slay):
   279 				{
   280 					PrintToChatAll("[SM] %t", "Slayed player", g_voteInfo[VOTE_NAME]);					
   281 					LogAction(-1, g_voteClient[VOTE_CLIENTID], "Vote slay successful, slaying \"%L\"", g_voteClient[VOTE_CLIENTID]);
   282 					
   283 					ExtinguishEntity(g_voteClient[VOTE_CLIENTID]);
   284 					ForcePlayerSuicide(g_voteClient[VOTE_CLIENTID]);
   285 				}
   286 				
   287 				case (voteType:alltalk):
   288 				{
   289 					PrintToChatAll("[SM] %t", "Cvar changed", "sv_alltalk", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
   290 					LogAction(-1, -1, "Changing alltalk to %s due to vote.", (GetConVarBool(g_Cvar_Alltalk) ? "0" : "1"));
   291 					SetConVarBool(g_Cvar_Alltalk, !GetConVarBool(g_Cvar_Alltalk));
   292 				}
   293 				
   294 				case (voteType:ff):
   295 				{
   296 					PrintToChatAll("[SM] %t", "Cvar changed", "mp_friendlyfire", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
   297 					LogAction(-1, -1, "Changing friendly fire to %s due to vote.", (GetConVarBool(g_Cvar_FF) ? "0" : "1"));
   298 					SetConVarBool(g_Cvar_FF, !GetConVarBool(g_Cvar_FF));
   299 				}				
   300 			}
   301 		}
   302 	}
   303 	
   304 	return 0;
   305 }
   306 
   307 /*
   308 VoteSelect(Handle:menu, param1, param2 = 0)
   309 {
   310 	if (GetConVarInt(g_Cvar_VoteShow) == 1)
   311 	{
   312 		decl String:voter[64], String:junk[64], String:choice[64];
   313 		GetClientName(param1, voter, sizeof(voter));
   314 		GetMenuItem(menu, param2, junk, sizeof(junk), _, choice, sizeof(choice));
   315 		PrintToChatAll("[SM] %T", "Vote Select", LANG_SERVER, voter, choice);
   316 	}
   317 }
   318 */
   319 
   320 VoteMenuClose()
   321 {
   322 	CloseHandle(g_hVoteMenu);
   323 	g_hVoteMenu = INVALID_HANDLE;
   324 }
   325 
   326 Float:GetVotePercent(votes, totalVotes)
   327 {
   328 	return FloatDiv(float(votes),float(totalVotes));
   329 }
   330 
   331 bool:TestVoteDelay(client)
   332 {
   333  	new delay = CheckVoteDelay();
   334  	
   335  	if (delay > 0)
   336  	{
   337  		if (delay > 60)
   338  		{
   339  			ReplyToCommand(client, "[SM] %t", "Vote Delay Minutes", delay % 60);
   340  		}
   341  		else
   342  		{
   343  			ReplyToCommand(client, "[SM] %t", "Vote Delay Seconds", delay);
   344  		}
   345  		
   346  		return false;
   347  	}
   348  	
   349 	return true;
   350 }