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