plugins/basetriggers.sp
author Scott Ehlert <ds@alliedmods.net>
Wed Jun 11 08:07:17 2008 +0000 (2008-06-11)
changeset 2112 2a83a521287c
parent 2083 3dcbef056e44
child 2160 b37c444b0a84
permissions -rw-r--r--
All natives that use a recipient filter now actually validate the clients passed to them.
This includes StartMessage(), StartMessageEx() from Core as well as EmitSound(), EmitSentence(), and TE_Send() from SDKTools

(Warning: This may potentially cause a minor compatibility problem with plugins that don't check client validity before passing to these natives)
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Basic Info Triggers Plugin
ds@1952
     5
 * Implements basic information chat triggers like ff and timeleft.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
#pragma semicolon 1
ds@1952
    35
ds@1952
    36
#include <sourcemod>
ds@1952
    37
ds@1952
    38
public Plugin:myinfo = 
ds@1952
    39
{
ds@1952
    40
	name = "Basic Info Triggers",
ds@1952
    41
	author = "AlliedModders LLC",
ds@1952
    42
	description = "Adds ff, timeleft, thetime, and others.",
ds@1952
    43
	version = SOURCEMOD_VERSION,
ds@1952
    44
	url = "http://www.sourcemod.net/"
ds@1952
    45
};
ds@1952
    46
ds@1952
    47
new Handle:g_Cvar_TriggerShow = INVALID_HANDLE;
ds@1952
    48
new Handle:g_Cvar_TimeleftInterval = INVALID_HANDLE;
ds@1952
    49
new Handle:g_Cvar_FriendlyFire = INVALID_HANDLE;
ds@1952
    50
ds@1952
    51
new Handle:g_Timer_TimeShow = INVALID_HANDLE;
ds@1952
    52
pred@2083
    53
new Handle:g_Cvar_WinLimit = INVALID_HANDLE;
pred@2083
    54
new Handle:g_Cvar_FragLimit = INVALID_HANDLE;
pred@2083
    55
new Handle:g_Cvar_MaxRounds = INVALID_HANDLE;
pred@2083
    56
ds@1952
    57
public OnPluginStart()
ds@1952
    58
{
ds@1952
    59
	LoadTranslations("common.phrases");
ds@1952
    60
	LoadTranslations("basetriggers.phrases");
ds@1952
    61
	
ds@1952
    62
	g_Cvar_TriggerShow = CreateConVar("sm_trigger_show", "1", "Display triggers message to all players? (0 off, 1 on, def. 1)", 0, true, 0.0, true, 1.0);	
ds@1952
    63
	g_Cvar_TimeleftInterval = CreateConVar("sm_timeleft_interval", "0.0", "Display timeleft every x seconds. Default 0.", 0, true, 0.0, true, 1800.0);
ds@1952
    64
	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
ds@1952
    65
	
ds@1952
    66
	RegConsoleCmd("say", Command_Say);
ds@1952
    67
	RegConsoleCmd("say_team", Command_Say);
ds@1952
    68
	
pred@2083
    69
	RegConsoleCmd("timeleft", Command_Timeleft);
pred@2083
    70
	
ds@1952
    71
	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
pred@2083
    72
	
pred@2083
    73
	
pred@2083
    74
	g_Cvar_WinLimit = FindConVar("mp_winlimit");
pred@2083
    75
	g_Cvar_FragLimit = FindConVar("mp_fraglimit");
pred@2083
    76
	g_Cvar_MaxRounds = FindConVar("mp_maxrounds");
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
ds@1952
    80
{
ds@1952
    81
	new Float:newval = StringToFloat(newValue);
ds@1952
    82
	
ds@1952
    83
	if (newval < 1.0)
ds@1952
    84
	{
ds@1952
    85
		if (g_Timer_TimeShow != INVALID_HANDLE)
ds@1952
    86
		{
ds@1952
    87
			KillTimer(g_Timer_TimeShow);		
ds@1952
    88
		}
ds@1952
    89
		
ds@1952
    90
		return;
ds@1952
    91
	}
ds@1952
    92
	
ds@1952
    93
	if (g_Timer_TimeShow != INVALID_HANDLE)
ds@1952
    94
	{
ds@1952
    95
		KillTimer(g_Timer_TimeShow);
ds@1952
    96
		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
ds@1952
    97
	}
ds@1952
    98
	else
ds@1952
    99
		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
ds@1952
   100
}
ds@1952
   101
ds@1952
   102
public Action:Timer_DisplayTimeleft(Handle:timer)
ds@1952
   103
{
pred@2083
   104
	ShowTimeLeft(0, true);	
pred@2083
   105
}
pred@2083
   106
pred@2083
   107
public Action:Command_Timeleft(client, args)
pred@2083
   108
{
pred@2083
   109
	ShowTimeLeft(client);
pred@2083
   110
	
pred@2083
   111
	return Plugin_Handled;
ds@1952
   112
}
ds@1952
   113
ds@1952
   114
public Action:Command_Say(client, args)
ds@1952
   115
{
ds@1952
   116
	decl String:text[192], String:command[64];
ds@1952
   117
	new startidx = 0;
ds@1952
   118
	if (GetCmdArgString(text, sizeof(text)) < 1)
ds@1952
   119
	{
ds@1952
   120
		return Plugin_Continue;
ds@1952
   121
	}
ds@1952
   122
	
ds@1952
   123
	if (text[strlen(text)-1] == '"')
ds@1952
   124
	{
ds@1952
   125
		text[strlen(text)-1] = '\0';
ds@1952
   126
		startidx = 1;
ds@1952
   127
	}
ds@1952
   128
ds@1952
   129
	if (strcmp(command, "say2", false) == 0)
ds@1952
   130
		startidx += 4;
ds@1952
   131
ds@1952
   132
	if (strcmp(text[startidx], "timeleft", false) == 0)
ds@1952
   133
	{
pred@2083
   134
		ShowTimeLeft(client);
ds@1952
   135
	}
ds@1952
   136
	else if (strcmp(text[startidx], "thetime", false) == 0)
ds@1952
   137
	{
ds@1952
   138
		decl String:ctime[64];
ds@1952
   139
		FormatTime(ctime, 64, NULL_STRING);
ds@1952
   140
		
ds@1952
   141
		if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   142
		{
ds@1952
   143
			PrintToChatAll("[SM] %t", "Thetime", ctime);
ds@1952
   144
		}
ds@1952
   145
		else
ds@1952
   146
		{
ds@1952
   147
			PrintToChat(client,"[SM] %t", "Thetime", ctime);
ds@1952
   148
		}
ds@1952
   149
	}
ds@1952
   150
	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
ds@1952
   151
	{
ds@1952
   152
		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
ds@1952
   153
		{
ds@1952
   154
			decl String:message[64];
ds@1952
   155
			if (GetConVarBool(g_Cvar_FriendlyFire))
ds@1952
   156
			{
ds@1952
   157
				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
ds@1952
   158
			}
ds@1952
   159
			else
ds@1952
   160
			{
ds@1952
   161
				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
ds@1952
   162
			}
ds@1952
   163
		
ds@1952
   164
			if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   165
			{
ds@1952
   166
				PrintToChatAll("[SM] %s", message);
ds@1952
   167
			}
ds@1952
   168
			else
ds@1952
   169
			{
ds@1952
   170
				PrintToChat(client,"[SM] %s", message);		
ds@1952
   171
			}
ds@1952
   172
		}
ds@1952
   173
	}
ds@1952
   174
	else if (strcmp(text[startidx], "currentmap", false) == 0)
ds@1952
   175
	{
ds@1952
   176
		decl String:map[64];
ds@1952
   177
		GetCurrentMap(map, sizeof(map));
ds@1952
   178
		
ds@1952
   179
		if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   180
		{
ds@1952
   181
			PrintToChatAll("[SM] %t", "Current Map", map);
ds@1952
   182
		}
ds@1952
   183
		else
ds@1952
   184
		{
ds@1952
   185
			PrintToChat(client,"[SM] %t", "Current Map", map);
ds@1952
   186
		}
ds@1952
   187
	}	
ds@1952
   188
	
ds@1952
   189
	return Plugin_Continue;
pred@2083
   190
}
pred@2083
   191
pred@2083
   192
ShowTimeLeft(client, bool:all=false)
pred@2083
   193
{
pred@2083
   194
	new bool:lastround = false;
pred@2083
   195
	new bool:written = false;
pred@2083
   196
	new bool:notimelimit = false;
pred@2083
   197
	
pred@2083
   198
	decl String:finalOutput[1024];
pred@2083
   199
	finalOutput[0] = 0;
pred@2083
   200
	
pred@2083
   201
	if(GetConVarInt(g_Cvar_TriggerShow) || all)
pred@2083
   202
	{
pred@2083
   203
		client = 0;	
pred@2083
   204
	}
pred@2083
   205
	
pred@2083
   206
	new timeleft;
pred@2083
   207
	if (GetMapTimeLeft(timeleft))
pred@2083
   208
	{
pred@2083
   209
		new mins, secs;
pred@2083
   210
		new timelimit;
pred@2083
   211
		
pred@2083
   212
		if (timeleft > 0)
pred@2083
   213
		{
pred@2083
   214
			mins = timeleft / 60;
pred@2083
   215
			secs = timeleft % 60;
pred@2083
   216
			written = true;
pred@2083
   217
			FormatEx(finalOutput, sizeof(finalOutput), "%T %d:%02d", "Timeleft", client, mins, secs);
pred@2083
   218
		}
pred@2083
   219
		else if (GetMapTimeLimit(timelimit) && timelimit == 0)
pred@2083
   220
		{
pred@2083
   221
			notimelimit = true;
pred@2083
   222
		}
pred@2083
   223
		else
pred@2083
   224
		{
pred@2083
   225
			/* 0 timeleft so this must be the last round */
pred@2083
   226
			lastround=true;
pred@2083
   227
		}
pred@2083
   228
	}
pred@2083
   229
	
pred@2083
   230
	if (!lastround)
pred@2083
   231
	{
pred@2083
   232
		if (g_Cvar_WinLimit != INVALID_HANDLE)
pred@2083
   233
		{
pred@2083
   234
			new winlimit = GetConVarInt(g_Cvar_WinLimit);
pred@2083
   235
			
pred@2083
   236
			if (winlimit > 0)
pred@2083
   237
			{
pred@2083
   238
				if (written)
pred@2083
   239
				{
pred@2083
   240
					new len = strlen(finalOutput);
pred@2083
   241
					if (len < sizeof(finalOutput))
pred@2083
   242
					{
pred@2083
   243
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppend" ,client, winlimit, (winlimit == 1)? "":"s");
pred@2083
   244
					}
pred@2083
   245
				}
pred@2083
   246
				else
pred@2083
   247
				{
pred@2083
   248
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimit", client, winlimit, (winlimit == 1)? "":"s");
pred@2083
   249
					written = true;
pred@2083
   250
				}
pred@2083
   251
			}
pred@2083
   252
		}
pred@2083
   253
		
pred@2083
   254
		if (g_Cvar_FragLimit != INVALID_HANDLE)
pred@2083
   255
		{
pred@2083
   256
			new fraglimit = GetConVarInt(g_Cvar_FragLimit);
pred@2083
   257
			
pred@2083
   258
			if (fraglimit > 0)
pred@2083
   259
			{
pred@2083
   260
				if (written)
pred@2083
   261
				{
pred@2083
   262
					new len = strlen(finalOutput);
pred@2083
   263
					if (len < sizeof(finalOutput))
pred@2083
   264
					{
pred@2083
   265
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppend", client, fraglimit, (fraglimit == 1)? "":"s");
pred@2083
   266
					}	
pred@2083
   267
				}
pred@2083
   268
				else
pred@2083
   269
				{
pred@2083
   270
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimit", client, fraglimit, (fraglimit == 1)? "":"s");
pred@2083
   271
					written = true;
pred@2083
   272
				}			
pred@2083
   273
			}
pred@2083
   274
		}
pred@2083
   275
		
pred@2083
   276
		if (g_Cvar_MaxRounds != INVALID_HANDLE)
pred@2083
   277
		{
pred@2083
   278
			new maxrounds = GetConVarInt(g_Cvar_MaxRounds);
pred@2083
   279
			
pred@2083
   280
			if (maxrounds > 0)
pred@2083
   281
			{
pred@2083
   282
				if (written)
pred@2083
   283
				{
pred@2083
   284
					new len = strlen(finalOutput);
pred@2083
   285
					if (len < sizeof(finalOutput))
pred@2083
   286
					{
pred@2083
   287
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppend", client, maxrounds, (maxrounds == 1)? "":"s");
pred@2083
   288
					}
pred@2083
   289
				}
pred@2083
   290
				else
pred@2083
   291
				{
pred@2083
   292
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRounds", client, maxrounds, (maxrounds == 1)? "":"s");
pred@2083
   293
					written = true;
pred@2083
   294
				}			
pred@2083
   295
			}		
pred@2083
   296
		}
pred@2083
   297
	}
pred@2083
   298
	
pred@2083
   299
	if (lastround)
pred@2083
   300
	{
pred@2083
   301
		FormatEx(finalOutput, sizeof(finalOutput), "%T", "LastRound", client);
pred@2083
   302
	}
pred@2083
   303
	else if (notimelimit && !written)
pred@2083
   304
	{
pred@2083
   305
		FormatEx(finalOutput, sizeof(finalOutput), "%T", "NoTimelimit", client);
pred@2083
   306
	}
pred@2083
   307
pred@2083
   308
	if(GetConVarInt(g_Cvar_TriggerShow) || all)
pred@2083
   309
	{
pred@2083
   310
		PrintToChatAll("[SM] %s", finalOutput);
pred@2083
   311
	}
pred@2083
   312
	else if (client != 0)
pred@2083
   313
	{
pred@2083
   314
		PrintToChat(client, "[SM] %s", finalOutput);
pred@2083
   315
	}
pred@2083
   316
	
pred@2083
   317
	if (client == 0)
pred@2083
   318
	{
pred@2083
   319
		PrintToServer("[SM] %s", finalOutput);
pred@2083
   320
	}
ds@1952
   321
}