plugins/funcommands/drug.sp
author Nicholas Hastings <skamonkey@gmail.com>
Tue May 14 11:27:06 2013 -0400 (2013-05-14)
changeset 3918 312fa627bf21
parent 3743 a98686a77974
permissions -rw-r--r--
Fixed SDKTools dota build.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basefuncommands Plugin
     5  * Provides drug 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 Handle:g_DrugTimers[MAXPLAYERS+1];
    35 new Float:g_DrugAngles[20] = {0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 20.0, 15.0, 10.0, 5.0, 0.0, -5.0, -10.0, -15.0, -20.0, -25.0, -20.0, -15.0, -10.0, -5.0};
    36 
    37 CreateDrug(client)
    38 {
    39 	g_DrugTimers[client] = CreateTimer(1.0, Timer_Drug, client, TIMER_REPEAT);	
    40 }
    41 
    42 KillDrug(client)
    43 {
    44 	KillDrugTimer(client);
    45 	
    46 	new Float:angs[3];
    47 	GetClientEyeAngles(client, angs);
    48 	
    49 	angs[2] = 0.0;
    50 	
    51 	TeleportEntity(client, NULL_VECTOR, angs, NULL_VECTOR);	
    52 	
    53 	new clients[2];
    54 	clients[0] = client;
    55 
    56 	new duration = 1536;
    57 	new holdtime = 1536;
    58 	new flags = (0x0001 | 0x0010);
    59 	new color[4] = { 0, 0, 0, 0 };
    60 
    61 	new Handle:message = StartMessageEx(g_FadeUserMsgId, clients, 1);
    62 
    63 	if (GetUserMessageType() == UM_Protobuf)
    64 	{
    65 		PbSetInt(message, "duration", duration);
    66 		PbSetInt(message, "hold_time", holdtime);
    67 		PbSetInt(message, "flags", flags);
    68 		PbSetColor(message, "clr", color);
    69 	}
    70 	else
    71 	{	
    72 		BfWriteShort(message, duration);
    73 		BfWriteShort(message, holdtime);
    74 		BfWriteShort(message, flags);
    75 		BfWriteByte(message, color[0]);
    76 		BfWriteByte(message, color[1]);
    77 		BfWriteByte(message, color[2]);
    78 		BfWriteByte(message, color[3]);
    79 	}
    80 	
    81 	EndMessage();
    82 }
    83 
    84 KillDrugTimer(client)
    85 {
    86 	KillTimer(g_DrugTimers[client]);
    87 	g_DrugTimers[client] = INVALID_HANDLE;	
    88 }
    89 
    90 KillAllDrugs()
    91 {
    92 	for (new i = 1; i <= MaxClients; i++)
    93 	{
    94 		if (g_DrugTimers[i] != INVALID_HANDLE)
    95 		{
    96 			if(IsClientInGame(i))
    97 			{
    98 				KillDrug(i);
    99 			}
   100 			else
   101 			{
   102 				KillDrugTimer(i);
   103 			}
   104 		}
   105 	}
   106 }
   107 
   108 PerformDrug(client, target, toggle)
   109 {
   110 	switch (toggle)
   111 	{
   112 		case (2):
   113 		{
   114 			if (g_DrugTimers[target] == INVALID_HANDLE)
   115 			{
   116 				CreateDrug(target);
   117 				LogAction(client, target, "\"%L\" drugged \"%L\"", client, target);
   118 			}
   119 			else
   120 			{
   121 				KillDrug(target);
   122 				LogAction(client, target, "\"%L\" undrugged \"%L\"", client, target);
   123 			}			
   124 		}
   125 
   126 		case (1):
   127 		{
   128 			if (g_DrugTimers[target] == INVALID_HANDLE)
   129 			{
   130 				CreateDrug(target);
   131 				LogAction(client, target, "\"%L\" drugged \"%L\"", client, target);
   132 			}			
   133 		}
   134 		
   135 		case (0):
   136 		{
   137 			if (g_DrugTimers[target] != INVALID_HANDLE)
   138 			{
   139 				KillDrug(target);
   140 				LogAction(client, target, "\"%L\" undrugged \"%L\"", client, target);
   141 			}			
   142 		}
   143 	}
   144 }
   145 
   146 public Action:Timer_Drug(Handle:timer, any:client)
   147 {
   148 	if (!IsClientInGame(client))
   149 	{
   150 		KillDrugTimer(client);
   151 
   152 		return Plugin_Handled;
   153 	}
   154 	
   155 	if (!IsPlayerAlive(client))
   156 	{
   157 		KillDrug(client);
   158 		
   159 		return Plugin_Handled;
   160 	}
   161 	
   162 	new Float:angs[3];
   163 	GetClientEyeAngles(client, angs);
   164 	
   165 	angs[2] = g_DrugAngles[GetRandomInt(0,100) % 20];
   166 	
   167 	TeleportEntity(client, NULL_VECTOR, angs, NULL_VECTOR);
   168 	
   169 	new clients[2];
   170 	clients[0] = client;	
   171 	
   172 	new duration = 255;
   173 	new holdtime = 255;
   174 	new flags = 0x0002;
   175 	new color[4] = { 0, 0, 0, 128 };
   176 	color[0] = GetRandomInt(0,255);
   177 	color[1] = GetRandomInt(0,255);
   178 	color[2] = GetRandomInt(0,255);
   179 
   180 	new Handle:message = StartMessageEx(g_FadeUserMsgId, clients, 1);
   181 	
   182 	if (GetUserMessageType() == UM_Protobuf)
   183 	{
   184 		PbSetInt(message, "duration", duration);
   185 		PbSetInt(message, "hold_time", holdtime);
   186 		PbSetInt(message, "flags", flags);
   187 		PbSetColor(message, "clr", color);
   188 	}
   189 	else
   190 	{
   191 		BfWriteShort(message, duration);
   192 		BfWriteShort(message, holdtime);
   193 		BfWriteShort(message, flags);
   194 		BfWriteByte(message, color[0]);
   195 		BfWriteByte(message, color[1]);
   196 		BfWriteByte(message, color[2]);
   197 		BfWriteByte(message, color[3]);
   198 	}
   199 	
   200 	EndMessage();
   201 		
   202 	return Plugin_Handled;
   203 }
   204 
   205 public AdminMenu_Drug(Handle:topmenu, 
   206 					  TopMenuAction:action,
   207 					  TopMenuObject:object_id,
   208 					  param,
   209 					  String:buffer[],
   210 					  maxlength)
   211 {
   212 	if (action == TopMenuAction_DisplayOption)
   213 	{
   214 		Format(buffer, maxlength, "%T", "Drug player", param);
   215 	}
   216 	else if (action == TopMenuAction_SelectOption)
   217 	{
   218 		DisplayDrugMenu(param);
   219 	}
   220 }
   221 
   222 DisplayDrugMenu(client)
   223 {
   224 	new Handle:menu = CreateMenu(MenuHandler_Drug);
   225 	
   226 	decl String:title[100];
   227 	Format(title, sizeof(title), "%T:", "Drug player", client);
   228 	SetMenuTitle(menu, title);
   229 	SetMenuExitBackButton(menu, true);
   230 	
   231 	AddTargetsToMenu(menu, client, true, true);
   232 	
   233 	DisplayMenu(menu, client, MENU_TIME_FOREVER);
   234 }
   235 
   236 public MenuHandler_Drug(Handle:menu, MenuAction:action, param1, param2)
   237 {
   238 	if (action == MenuAction_End)
   239 	{
   240 		CloseHandle(menu);
   241 	}
   242 	else if (action == MenuAction_Cancel)
   243 	{
   244 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   245 		{
   246 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
   247 		}
   248 	}
   249 	else if (action == MenuAction_Select)
   250 	{
   251 		decl String:info[32];
   252 		new userid, target;
   253 		
   254 		GetMenuItem(menu, param2, info, sizeof(info));
   255 		userid = StringToInt(info);
   256 
   257 		if ((target = GetClientOfUserId(userid)) == 0)
   258 		{
   259 			PrintToChat(param1, "[SM] %t", "Player no longer available");
   260 		}
   261 		else if (!CanUserTarget(param1, target))
   262 		{
   263 			PrintToChat(param1, "[SM] %t", "Unable to target");
   264 		}
   265 		else
   266 		{
   267 			new String:name[32];
   268 			GetClientName(target, name, sizeof(name));
   269 			
   270 			PerformDrug(param1, target, 2);
   271 			ShowActivity2(param1, "[SM] ", "%t", "Toggled drug on target", "_s", name);
   272 		}
   273 		
   274 		/* Re-draw the menu if they're still valid */
   275 		if (IsClientInGame(param1) && !IsClientInKickQueue(param1))
   276 		{
   277 			DisplayDrugMenu(param1);
   278 		}
   279 	}
   280 }
   281 
   282 public Action:Command_Drug(client, args)
   283 {
   284 	if (args < 1)
   285 	{
   286 		ReplyToCommand(client, "[SM] Usage: sm_drug <#userid|name> [0/1]");
   287 		return Plugin_Handled;
   288 	}
   289 
   290 	decl String:arg[65];
   291 	GetCmdArg(1, arg, sizeof(arg));
   292 	
   293 	new toggle = 2;
   294 	if (args > 1)
   295 	{
   296 		decl String:arg2[2];
   297 		GetCmdArg(2, arg2, sizeof(arg2));
   298 		if (StringToInt(arg2))
   299 		{
   300 			toggle = 1;
   301 		}
   302 		else
   303 		{
   304 			toggle = 0;
   305 		}
   306 	}
   307 
   308 	decl String:target_name[MAX_TARGET_LENGTH];
   309 	decl target_list[MAXPLAYERS], target_count, bool:tn_is_ml;
   310 	
   311 	if ((target_count = ProcessTargetString(
   312 			arg,
   313 			client,
   314 			target_list,
   315 			MAXPLAYERS,
   316 			COMMAND_FILTER_ALIVE,
   317 			target_name,
   318 			sizeof(target_name),
   319 			tn_is_ml)) <= 0)
   320 	{
   321 		ReplyToTargetError(client, target_count);
   322 		return Plugin_Handled;
   323 	}
   324 	
   325 	for (new i = 0; i < target_count; i++)
   326 	{
   327 		PerformDrug(client, target_list[i], toggle);
   328 	}
   329 	
   330 	if (tn_is_ml)
   331 	{
   332 		ShowActivity2(client, "[SM] ", "%t", "Toggled drug on target", target_name);
   333 	}
   334 	else
   335 	{
   336 		ShowActivity2(client, "[SM] ", "%t", "Toggled drug on target", "_s", target_name);
   337 	}
   338 	
   339 	return Plugin_Handled;
   340 }