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