plugins/funcommands/gravity.sp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3891 5a76641c95e1
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basefuncommands Plugin
     5  * Provides gravity functionality
     6  *
     7  * SourceMod (C)2004-2008 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 new g_GravityTarget[MAXPLAYERS+1];
    35 
    36 PerformGravity(client, target, Float:amount)
    37 {
    38 	SetEntityGravity(target, amount);
    39 	LogAction(client, target, "\"%L\" set gravity on \"%L\" to %d.", client, target, amount);
    40 }
    41 
    42 public AdminMenu_Gravity(Handle:topmenu, 
    43 					  TopMenuAction:action,
    44 					  TopMenuObject:object_id,
    45 					  param,
    46 					  String:buffer[],
    47 					  maxlength)
    48 {
    49 	if (action == TopMenuAction_DisplayOption)
    50 	{
    51 		Format(buffer, maxlength, "%T", "Gravity player", param);
    52 	}
    53 	else if (action == TopMenuAction_SelectOption)
    54 	{
    55 		DisplayGravityMenu(param);
    56 	}
    57 }
    58 
    59 DisplayGravityMenu(client)
    60 {
    61 	new Handle:menu = CreateMenu(MenuHandler_Gravity);
    62 	
    63 	decl String:title[100];
    64 	Format(title, sizeof(title), "%T:", "Gravity player", client);
    65 	SetMenuTitle(menu, title);
    66 	SetMenuExitBackButton(menu, true);
    67 	
    68 	AddTargetsToMenu(menu, client, true, true);
    69 	
    70 	DisplayMenu(menu, client, MENU_TIME_FOREVER);
    71 }
    72 
    73 DisplayGravityAmountMenu(client)
    74 {
    75 	new Handle:menu = CreateMenu(MenuHandler_GravityAmount);
    76 	
    77 	decl String:title[100];
    78 	Format(title, sizeof(title), "%T: %N", "Gravity amount", client, GetClientOfUserId(g_GravityTarget[client]));
    79 	SetMenuTitle(menu, title);
    80 	SetMenuExitBackButton(menu, true);
    81 	
    82 	AddTranslatedMenuItem(menu, "4.0", "Gravity Very High", client);
    83 	AddTranslatedMenuItem(menu, "2.0", "Gravity High", client);
    84 	AddTranslatedMenuItem(menu, "1.0", "Gravity Normal", client);
    85 	AddTranslatedMenuItem(menu, "0.5", "Gravity Low", client);
    86 	AddTranslatedMenuItem(menu, "0.1", "Gravity Very Low", client);
    87 	
    88 	DisplayMenu(menu, client, MENU_TIME_FOREVER);
    89 }
    90 
    91 public MenuHandler_Gravity(Handle:menu, MenuAction:action, param1, param2)
    92 {
    93 	if (action == MenuAction_End)
    94 	{
    95 		CloseHandle(menu);
    96 	}
    97 	else if (action == MenuAction_Cancel)
    98 	{
    99 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   100 		{
   101 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
   102 		}
   103 	}
   104 	else if (action == MenuAction_Select)
   105 	{
   106 		decl String:info[32];
   107 		new userid, target;
   108 		
   109 		GetMenuItem(menu, param2, info, sizeof(info));
   110 		userid = StringToInt(info);
   111 
   112 		if ((target = GetClientOfUserId(userid)) == 0)
   113 		{
   114 			PrintToChat(param1, "[SM] %t", "Player no longer available");
   115 		}
   116 		else if (!CanUserTarget(param1, target))
   117 		{
   118 			PrintToChat(param1, "[SM] %t", "Unable to target");
   119 		}
   120 		else
   121 		{
   122 			g_GravityTarget[param1] = userid;
   123 			DisplayGravityAmountMenu(param1);
   124 			return;	// Return, because we went to a new menu and don't want the re-draw to occur.
   125 		}
   126 		
   127 		/* Re-draw the menu if they're still valid */
   128 		if (IsClientInGame(param1) && !IsClientInKickQueue(param1))
   129 		{
   130 			DisplayGravityMenu(param1);
   131 		}
   132 	}
   133 	
   134 	return;
   135 }
   136 
   137 public MenuHandler_GravityAmount(Handle:menu, MenuAction:action, param1, param2)
   138 {
   139 	if (action == MenuAction_End)
   140 	{
   141 		CloseHandle(menu);
   142 	}
   143 	else if (action == MenuAction_Cancel)
   144 	{
   145 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   146 		{
   147 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
   148 		}
   149 	}
   150 	else if (action == MenuAction_Select)
   151 	{
   152 		decl String:info[32];
   153 		new Float:amount, target;
   154 		
   155 		GetMenuItem(menu, param2, info, sizeof(info));
   156 		amount = StringToFloat(info);
   157 
   158 		if ((target = GetClientOfUserId(g_GravityTarget[param1])) == 0)
   159 		{
   160 			PrintToChat(param1, "[SM] %t", "Player no longer available");
   161 		}
   162 		else if (!CanUserTarget(param1, target))
   163 		{
   164 			PrintToChat(param1, "[SM] %t", "Unable to target");
   165 		}
   166 		else
   167 		{
   168 			new String:name[32];
   169 			GetClientName(target, name, sizeof(name));
   170 			
   171 			PerformGravity(param1, target, amount);
   172 			ShowActivity2(param1, "[SM] ", "%t", "Set gravity on target", "_s", name, amount);
   173 		}
   174 		
   175 		/* Re-draw the menu if they're still valid */
   176 		if (IsClientInGame(param1) && !IsClientInKickQueue(param1))
   177 		{
   178 			DisplayGravityMenu(param1);
   179 		}
   180 	}
   181 }
   182 
   183 public Action:Command_Gravity(client, args)
   184 {
   185 	if (args < 1)
   186 	{
   187 		ReplyToCommand(client, "[SM] Usage: sm_gravity <#userid|name> [amount]");
   188 		return Plugin_Handled;
   189 	}
   190 
   191 	decl String:arg[65];
   192 	GetCmdArg(1, arg, sizeof(arg));
   193 	
   194 	new Float:amount = 1.0;
   195 	if (args > 1)
   196 	{
   197 		decl String:arg2[20];
   198 		GetCmdArg(2, arg2, sizeof(arg2));
   199 		if (StringToFloatEx(arg2, amount) == 0)
   200 		{
   201 			ReplyToCommand(client, "[SM] %t", "Invalid Amount");
   202 			return Plugin_Handled;
   203 		}
   204 		
   205 		if (amount < 0.0)
   206 		{
   207 			amount = 0.0;
   208 		}
   209 	}
   210 
   211 	decl String:target_name[MAX_TARGET_LENGTH];
   212 	decl target_list[MAXPLAYERS], target_count, bool:tn_is_ml;
   213 	
   214 	if ((target_count = ProcessTargetString(
   215 			arg,
   216 			client,
   217 			target_list,
   218 			MAXPLAYERS,
   219 			COMMAND_FILTER_ALIVE,
   220 			target_name,
   221 			sizeof(target_name),
   222 			tn_is_ml)) <= 0)
   223 	{
   224 		ReplyToTargetError(client, target_count);
   225 		return Plugin_Handled;
   226 	}
   227 	
   228 	for (new i = 0; i < target_count; i++)
   229 	{
   230 		PerformGravity(client, target_list[i], amount);
   231 	}
   232 	
   233 	if (tn_is_ml)
   234 	{
   235 		ShowActivity2(client, "[SM] ", "%t", "Set gravity on target", target_name);
   236 	}
   237 	else
   238 	{
   239 		ShowActivity2(client, "[SM] ", "%t", "Set gravity on target", "_s", target_name);
   240 	}
   241 	
   242 	return Plugin_Handled;
   243 }