plugins/basetriggers.sp
author David Anderson <dvander@alliedmods.net>
Wed Jul 16 03:46:10 2008 +0000 (2008-07-16)
changeset 2211 be9f9063b727
parent 2206 6b946d8524d8
child 2212 24fcd7ac70f7
permissions -rw-r--r--
Fixed a bug where natives with no owner could be bound (crashing).
Fixed a bug where FakeNative entries were leaked.
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
pred@2203
    57
#define TIMELEFT_ALL_ALWAYS		0		/* Print to all players */
pred@2203
    58
#define TIMELEFT_ALL_MAYBE		1		/* Print to all players if sm_trigger_show allows */
pred@2203
    59
#define TIMELEFT_ONE			2		/* Print to a single player */
dvander@2160
    60
ds@1952
    61
public OnPluginStart()
ds@1952
    62
{
ds@1952
    63
	LoadTranslations("common.phrases");
ds@1952
    64
	LoadTranslations("basetriggers.phrases");
ds@1952
    65
	
ds@1952
    66
	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
    67
	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
    68
	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
ds@1952
    69
	
ds@1952
    70
	RegConsoleCmd("say", Command_Say);
ds@1952
    71
	RegConsoleCmd("say_team", Command_Say);
ds@1952
    72
	
pred@2083
    73
	RegConsoleCmd("timeleft", Command_Timeleft);
pred@2206
    74
	RegConsoleCmd("nextmap", Command_Nextmap);
pred@2083
    75
	
ds@1952
    76
	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
pred@2083
    77
	
pred@2083
    78
	
pred@2083
    79
	g_Cvar_WinLimit = FindConVar("mp_winlimit");
pred@2083
    80
	g_Cvar_FragLimit = FindConVar("mp_fraglimit");
pred@2083
    81
	g_Cvar_MaxRounds = FindConVar("mp_maxrounds");
ds@1952
    82
}
ds@1952
    83
ds@1952
    84
public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
ds@1952
    85
{
ds@1952
    86
	new Float:newval = StringToFloat(newValue);
ds@1952
    87
	
ds@1952
    88
	if (newval < 1.0)
ds@1952
    89
	{
ds@1952
    90
		if (g_Timer_TimeShow != INVALID_HANDLE)
ds@1952
    91
		{
ds@1952
    92
			KillTimer(g_Timer_TimeShow);		
ds@1952
    93
		}
ds@1952
    94
		
ds@1952
    95
		return;
ds@1952
    96
	}
ds@1952
    97
	
ds@1952
    98
	if (g_Timer_TimeShow != INVALID_HANDLE)
ds@1952
    99
	{
ds@1952
   100
		KillTimer(g_Timer_TimeShow);
ds@1952
   101
		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
ds@1952
   102
	}
ds@1952
   103
	else
ds@1952
   104
		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
ds@1952
   105
}
ds@1952
   106
ds@1952
   107
public Action:Timer_DisplayTimeleft(Handle:timer)
ds@1952
   108
{
dvander@2160
   109
	ShowTimeLeft(0, TIMELEFT_ALL_ALWAYS);	
pred@2083
   110
}
pred@2083
   111
pred@2083
   112
public Action:Command_Timeleft(client, args)
pred@2083
   113
{
dvander@2160
   114
	ShowTimeLeft(client, TIMELEFT_ONE);
pred@2083
   115
	
pred@2083
   116
	return Plugin_Handled;
ds@1952
   117
}
ds@1952
   118
pred@2206
   119
public Action:Command_Nextmap(client, args)
pred@2206
   120
{
pred@2206
   121
	decl String:map[64];
pred@2206
   122
	
pred@2206
   123
	GetNextMap(map, sizeof(map));
pred@2206
   124
	
pred@2206
   125
	if(GetConVarInt(g_Cvar_TriggerShow))
pred@2206
   126
	{
pred@2206
   127
		PrintToChatAll("[SM] %t", "Next Map", map);
pred@2206
   128
	}
pred@2206
   129
	else
pred@2206
   130
	{
pred@2206
   131
		ReplyToCommand(client, "[SM] %t", "Next Map", map);
pred@2206
   132
	}
pred@2206
   133
	
pred@2206
   134
	return Plugin_Handled;
pred@2206
   135
}
pred@2206
   136
ds@1952
   137
public Action:Command_Say(client, args)
ds@1952
   138
{
ds@1952
   139
	decl String:text[192], String:command[64];
ds@1952
   140
	new startidx = 0;
ds@1952
   141
	if (GetCmdArgString(text, sizeof(text)) < 1)
ds@1952
   142
	{
ds@1952
   143
		return Plugin_Continue;
ds@1952
   144
	}
ds@1952
   145
	
ds@1952
   146
	if (text[strlen(text)-1] == '"')
ds@1952
   147
	{
ds@1952
   148
		text[strlen(text)-1] = '\0';
ds@1952
   149
		startidx = 1;
ds@1952
   150
	}
ds@1952
   151
ds@1952
   152
	if (strcmp(command, "say2", false) == 0)
ds@1952
   153
		startidx += 4;
ds@1952
   154
ds@1952
   155
	if (strcmp(text[startidx], "timeleft", false) == 0)
ds@1952
   156
	{
dvander@2160
   157
		ShowTimeLeft(client, TIMELEFT_ALL_MAYBE);
ds@1952
   158
	}
ds@1952
   159
	else if (strcmp(text[startidx], "thetime", false) == 0)
ds@1952
   160
	{
ds@1952
   161
		decl String:ctime[64];
ds@1952
   162
		FormatTime(ctime, 64, NULL_STRING);
ds@1952
   163
		
ds@1952
   164
		if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   165
		{
ds@1952
   166
			PrintToChatAll("[SM] %t", "Thetime", ctime);
ds@1952
   167
		}
ds@1952
   168
		else
ds@1952
   169
		{
ds@1952
   170
			PrintToChat(client,"[SM] %t", "Thetime", ctime);
ds@1952
   171
		}
ds@1952
   172
	}
ds@1952
   173
	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
ds@1952
   174
	{
ds@1952
   175
		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
ds@1952
   176
		{
ds@1952
   177
			decl String:message[64];
ds@1952
   178
			if (GetConVarBool(g_Cvar_FriendlyFire))
ds@1952
   179
			{
ds@1952
   180
				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
ds@1952
   181
			}
ds@1952
   182
			else
ds@1952
   183
			{
ds@1952
   184
				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
ds@1952
   185
			}
ds@1952
   186
		
ds@1952
   187
			if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   188
			{
ds@1952
   189
				PrintToChatAll("[SM] %s", message);
ds@1952
   190
			}
ds@1952
   191
			else
ds@1952
   192
			{
ds@1952
   193
				PrintToChat(client,"[SM] %s", message);		
ds@1952
   194
			}
ds@1952
   195
		}
ds@1952
   196
	}
ds@1952
   197
	else if (strcmp(text[startidx], "currentmap", false) == 0)
ds@1952
   198
	{
ds@1952
   199
		decl String:map[64];
ds@1952
   200
		GetCurrentMap(map, sizeof(map));
ds@1952
   201
		
ds@1952
   202
		if(GetConVarInt(g_Cvar_TriggerShow))
ds@1952
   203
		{
ds@1952
   204
			PrintToChatAll("[SM] %t", "Current Map", map);
ds@1952
   205
		}
ds@1952
   206
		else
ds@1952
   207
		{
ds@1952
   208
			PrintToChat(client,"[SM] %t", "Current Map", map);
ds@1952
   209
		}
pred@2206
   210
	}
pred@2206
   211
	else if (strcmp(text[startidx], "nextmap", false) == 0)
pred@2206
   212
	{
pred@2206
   213
		decl String:map[32];
pred@2206
   214
		GetNextMap(map, sizeof(map));
pred@2206
   215
			
pred@2206
   216
		if(GetConVarInt(g_Cvar_TriggerShow))
pred@2206
   217
		{
pred@2206
   218
			PrintToChatAll("[SM] %t", "Next Map", map);
pred@2206
   219
		}
pred@2206
   220
		else
pred@2206
   221
		{
pred@2206
   222
			PrintToChat(client, "[SM] %t", "Next Map", map);
pred@2206
   223
		}
pred@2206
   224
	}
ds@1952
   225
	
ds@1952
   226
	return Plugin_Continue;
pred@2083
   227
}
pred@2083
   228
dvander@2160
   229
ShowTimeLeft(client, who)
pred@2083
   230
{
pred@2083
   231
	new bool:lastround = false;
pred@2083
   232
	new bool:written = false;
pred@2083
   233
	new bool:notimelimit = false;
pred@2083
   234
	
pred@2083
   235
	decl String:finalOutput[1024];
pred@2083
   236
	finalOutput[0] = 0;
pred@2083
   237
	
dvander@2160
   238
	if (who == TIMELEFT_ALL_ALWAYS
dvander@2160
   239
		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
pred@2083
   240
	{
pred@2083
   241
		client = 0;	
pred@2083
   242
	}
pred@2083
   243
	
pred@2083
   244
	new timeleft;
pred@2083
   245
	if (GetMapTimeLeft(timeleft))
pred@2083
   246
	{
pred@2083
   247
		new mins, secs;
pred@2083
   248
		new timelimit;
pred@2083
   249
		
pred@2083
   250
		if (timeleft > 0)
pred@2083
   251
		{
pred@2083
   252
			mins = timeleft / 60;
pred@2083
   253
			secs = timeleft % 60;
pred@2083
   254
			written = true;
pred@2083
   255
			FormatEx(finalOutput, sizeof(finalOutput), "%T %d:%02d", "Timeleft", client, mins, secs);
pred@2083
   256
		}
pred@2083
   257
		else if (GetMapTimeLimit(timelimit) && timelimit == 0)
pred@2083
   258
		{
pred@2083
   259
			notimelimit = true;
pred@2083
   260
		}
pred@2083
   261
		else
pred@2083
   262
		{
pred@2083
   263
			/* 0 timeleft so this must be the last round */
pred@2083
   264
			lastround=true;
pred@2083
   265
		}
pred@2083
   266
	}
pred@2083
   267
	
pred@2083
   268
	if (!lastround)
pred@2083
   269
	{
pred@2083
   270
		if (g_Cvar_WinLimit != INVALID_HANDLE)
pred@2083
   271
		{
pred@2083
   272
			new winlimit = GetConVarInt(g_Cvar_WinLimit);
pred@2083
   273
			
pred@2083
   274
			if (winlimit > 0)
pred@2083
   275
			{
pred@2083
   276
				if (written)
pred@2083
   277
				{
pred@2083
   278
					new len = strlen(finalOutput);
pred@2083
   279
					if (len < sizeof(finalOutput))
pred@2083
   280
					{
pred@2083
   281
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppend" ,client, winlimit, (winlimit == 1)? "":"s");
pred@2083
   282
					}
pred@2083
   283
				}
pred@2083
   284
				else
pred@2083
   285
				{
pred@2083
   286
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimit", client, winlimit, (winlimit == 1)? "":"s");
pred@2083
   287
					written = true;
pred@2083
   288
				}
pred@2083
   289
			}
pred@2083
   290
		}
pred@2083
   291
		
pred@2083
   292
		if (g_Cvar_FragLimit != INVALID_HANDLE)
pred@2083
   293
		{
pred@2083
   294
			new fraglimit = GetConVarInt(g_Cvar_FragLimit);
pred@2083
   295
			
pred@2083
   296
			if (fraglimit > 0)
pred@2083
   297
			{
pred@2083
   298
				if (written)
pred@2083
   299
				{
pred@2083
   300
					new len = strlen(finalOutput);
pred@2083
   301
					if (len < sizeof(finalOutput))
pred@2083
   302
					{
pred@2083
   303
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppend", client, fraglimit, (fraglimit == 1)? "":"s");
pred@2083
   304
					}	
pred@2083
   305
				}
pred@2083
   306
				else
pred@2083
   307
				{
pred@2083
   308
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimit", client, fraglimit, (fraglimit == 1)? "":"s");
pred@2083
   309
					written = true;
pred@2083
   310
				}			
pred@2083
   311
			}
pred@2083
   312
		}
pred@2083
   313
		
pred@2083
   314
		if (g_Cvar_MaxRounds != INVALID_HANDLE)
pred@2083
   315
		{
pred@2083
   316
			new maxrounds = GetConVarInt(g_Cvar_MaxRounds);
pred@2083
   317
			
pred@2083
   318
			if (maxrounds > 0)
pred@2083
   319
			{
pred@2083
   320
				if (written)
pred@2083
   321
				{
pred@2083
   322
					new len = strlen(finalOutput);
pred@2083
   323
					if (len < sizeof(finalOutput))
pred@2083
   324
					{
pred@2083
   325
						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppend", client, maxrounds, (maxrounds == 1)? "":"s");
pred@2083
   326
					}
pred@2083
   327
				}
pred@2083
   328
				else
pred@2083
   329
				{
pred@2083
   330
					FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRounds", client, maxrounds, (maxrounds == 1)? "":"s");
pred@2083
   331
					written = true;
pred@2083
   332
				}			
pred@2083
   333
			}		
pred@2083
   334
		}
pred@2083
   335
	}
pred@2083
   336
	
pred@2083
   337
	if (lastround)
pred@2083
   338
	{
pred@2083
   339
		FormatEx(finalOutput, sizeof(finalOutput), "%T", "LastRound", client);
pred@2083
   340
	}
pred@2083
   341
	else if (notimelimit && !written)
pred@2083
   342
	{
pred@2083
   343
		FormatEx(finalOutput, sizeof(finalOutput), "%T", "NoTimelimit", client);
pred@2083
   344
	}
pred@2083
   345
dvander@2160
   346
	if (who == TIMELEFT_ALL_ALWAYS
dvander@2160
   347
		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
pred@2083
   348
	{
pred@2083
   349
		PrintToChatAll("[SM] %s", finalOutput);
pred@2083
   350
	}
pred@2083
   351
	else if (client != 0)
pred@2083
   352
	{
pred@2083
   353
		PrintToChat(client, "[SM] %s", finalOutput);
pred@2083
   354
	}
pred@2083
   355
	
pred@2083
   356
	if (client == 0)
pred@2083
   357
	{
pred@2083
   358
		PrintToServer("[SM] %s", finalOutput);
pred@2083
   359
	}
ds@1952
   360
}