plugins/basetriggers.sp
author David Anderson <dvander@alliedmods.net>
Sun Oct 05 04:19:39 2008 -0700 (2008-10-05)
changeset 2341 4bb035f9750f
parent 2289 44045ead61cd
child 2362 9d2543813060
permissions -rw-r--r--
Corrected case in a nominations phrase.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Info Triggers Plugin
     5  * Implements basic information chat triggers like ff and timeleft.
     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 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 
    38 #undef REQUIRE_PLUGIN
    39 #include <mapchooser>
    40 #define REQUIRE_PLUGIN
    41 
    42 public Plugin:myinfo = 
    43 {
    44 	name = "Basic Info Triggers",
    45 	author = "AlliedModders LLC",
    46 	description = "Adds ff, timeleft, thetime, and others.",
    47 	version = SOURCEMOD_VERSION,
    48 	url = "http://www.sourcemod.net/"
    49 };
    50 
    51 new Handle:g_Cvar_TriggerShow = INVALID_HANDLE;
    52 new Handle:g_Cvar_TimeleftInterval = INVALID_HANDLE;
    53 new Handle:g_Cvar_FriendlyFire = INVALID_HANDLE;
    54 
    55 new Handle:g_Timer_TimeShow = INVALID_HANDLE;
    56 
    57 new Handle:g_Cvar_WinLimit = INVALID_HANDLE;
    58 new Handle:g_Cvar_FragLimit = INVALID_HANDLE;
    59 new Handle:g_Cvar_MaxRounds = INVALID_HANDLE;
    60 
    61 #define TIMELEFT_ALL_ALWAYS		0		/* Print to all players */
    62 #define TIMELEFT_ALL_MAYBE		1		/* Print to all players if sm_trigger_show allows */
    63 #define TIMELEFT_ONE			2		/* Print to a single player */
    64 
    65 new bool:mapchooser;
    66 
    67 public OnPluginStart()
    68 {
    69 	LoadTranslations("common.phrases");
    70 	LoadTranslations("basetriggers.phrases");
    71 	
    72 	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);	
    73 	g_Cvar_TimeleftInterval = CreateConVar("sm_timeleft_interval", "0.0", "Display timeleft every x seconds. Default 0.", 0, true, 0.0, true, 1800.0);
    74 	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
    75 	
    76 	RegConsoleCmd("say", Command_Say);
    77 	RegConsoleCmd("say_team", Command_Say);
    78 	
    79 	RegConsoleCmd("timeleft", Command_Timeleft);
    80 	RegConsoleCmd("nextmap", Command_Nextmap);
    81 	
    82 	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
    83 	
    84 	
    85 	g_Cvar_WinLimit = FindConVar("mp_winlimit");
    86 	g_Cvar_FragLimit = FindConVar("mp_fraglimit");
    87 	g_Cvar_MaxRounds = FindConVar("mp_maxrounds");
    88 	
    89 	mapchooser = LibraryExists("mapchooser");
    90 }
    91 
    92 public OnLibraryRemoved(const String:name[])
    93 {
    94 	if (StrEqual(name, "mapchooser"))
    95 	{
    96 		mapchooser = false;
    97 	}
    98 }
    99  
   100 public OnLibraryAdded(const String:name[])
   101 {
   102 	if (StrEqual(name, "mapchooser"))
   103 	{
   104 		mapchooser = true;
   105 	}
   106 }
   107 
   108 public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
   109 {
   110 	new Float:newval = StringToFloat(newValue);
   111 	
   112 	if (newval < 1.0)
   113 	{
   114 		if (g_Timer_TimeShow != INVALID_HANDLE)
   115 		{
   116 			KillTimer(g_Timer_TimeShow);		
   117 		}
   118 		
   119 		return;
   120 	}
   121 	
   122 	if (g_Timer_TimeShow != INVALID_HANDLE)
   123 	{
   124 		KillTimer(g_Timer_TimeShow);
   125 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
   126 	}
   127 	else
   128 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
   129 }
   130 
   131 public Action:Timer_DisplayTimeleft(Handle:timer)
   132 {
   133 	ShowTimeLeft(0, TIMELEFT_ALL_ALWAYS);	
   134 }
   135 
   136 public Action:Command_Timeleft(client, args)
   137 {
   138 	ShowTimeLeft(client, TIMELEFT_ONE);
   139 	
   140 	return Plugin_Handled;
   141 }
   142 
   143 public Action:Command_Nextmap(client, args)
   144 {
   145 	decl String:map[64];
   146 	
   147 	GetNextMap(map, sizeof(map));
   148 	
   149 	if(GetConVarInt(g_Cvar_TriggerShow))
   150 	{
   151 		if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   152 		{
   153 			PrintToChatAll("[SM] %t", "Pending Vote");			
   154 		}
   155 		else
   156 		{
   157 			PrintToChatAll("[SM] %t", "Next Map", map);
   158 		}
   159 	}
   160 	else
   161 	{
   162 		if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   163 		{
   164 			ReplyToCommand(client, "[SM] %t", "Pending Vote");			
   165 		}
   166 		else
   167 		{
   168 			ReplyToCommand(client, "[SM] %t", "Next Map", map);
   169 		}
   170 	}
   171 	
   172 	return Plugin_Handled;
   173 }
   174 
   175 public Action:Command_Say(client, args)
   176 {
   177 	decl String:text[192], String:command[64];
   178 	new startidx = 0;
   179 	if (GetCmdArgString(text, sizeof(text)) < 1)
   180 	{
   181 		return Plugin_Continue;
   182 	}
   183 	
   184 	if (text[strlen(text)-1] == '"')
   185 	{
   186 		text[strlen(text)-1] = '\0';
   187 		startidx = 1;
   188 	}
   189 
   190 	if (strcmp(command, "say2", false) == 0)
   191 		startidx += 4;
   192 
   193 	if (strcmp(text[startidx], "timeleft", false) == 0)
   194 	{
   195 		ShowTimeLeft(client, TIMELEFT_ALL_MAYBE);
   196 	}
   197 	else if (strcmp(text[startidx], "thetime", false) == 0)
   198 	{
   199 		decl String:ctime[64];
   200 		FormatTime(ctime, 64, NULL_STRING);
   201 		
   202 		if(GetConVarInt(g_Cvar_TriggerShow))
   203 		{
   204 			PrintToChatAll("[SM] %t", "Thetime", ctime);
   205 		}
   206 		else
   207 		{
   208 			PrintToChat(client,"[SM] %t", "Thetime", ctime);
   209 		}
   210 	}
   211 	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
   212 	{
   213 		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
   214 		{
   215 			decl String:message[64];
   216 			if (GetConVarBool(g_Cvar_FriendlyFire))
   217 			{
   218 				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
   219 			}
   220 			else
   221 			{
   222 				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
   223 			}
   224 		
   225 			if(GetConVarInt(g_Cvar_TriggerShow))
   226 			{
   227 				PrintToChatAll("[SM] %s", message);
   228 			}
   229 			else
   230 			{
   231 				PrintToChat(client,"[SM] %s", message);		
   232 			}
   233 		}
   234 	}
   235 	else if (strcmp(text[startidx], "currentmap", false) == 0)
   236 	{
   237 		decl String:map[64];
   238 		GetCurrentMap(map, sizeof(map));
   239 		
   240 		if(GetConVarInt(g_Cvar_TriggerShow))
   241 		{
   242 			PrintToChatAll("[SM] %t", "Current Map", map);
   243 		}
   244 		else
   245 		{
   246 			PrintToChat(client,"[SM] %t", "Current Map", map);
   247 		}
   248 	}
   249 	else if (strcmp(text[startidx], "nextmap", false) == 0)
   250 	{
   251 		decl String:map[32];
   252 		GetNextMap(map, sizeof(map));
   253 			
   254 		if(GetConVarInt(g_Cvar_TriggerShow))
   255 		{
   256 			if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   257 			{
   258 				PrintToChatAll("[SM] %t", "Pending Vote");			
   259 			}
   260 			else
   261 			{
   262 				PrintToChatAll("[SM] %t", "Next Map", map);
   263 			}
   264 		}
   265 		else
   266 		{
   267 			if (mapchooser && !HasEndOfMapVoteFinished())
   268 			{
   269 				PrintToChat(client, "[SM] %t", "Pending Vote");			
   270 			}
   271 			else
   272 			{
   273 				PrintToChat(client, "[SM] %t", "Next Map", map);
   274 			}
   275 		}
   276 	}
   277 	
   278 	return Plugin_Continue;
   279 }
   280 
   281 ShowTimeLeft(client, who)
   282 {
   283 	new bool:lastround = false;
   284 	new bool:written = false;
   285 	new bool:notimelimit = false;
   286 	
   287 	decl String:finalOutput[1024];
   288 	finalOutput[0] = 0;
   289 	
   290 	if (who == TIMELEFT_ALL_ALWAYS
   291 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   292 	{
   293 		client = 0;	
   294 	}
   295 	
   296 	new timeleft;
   297 	if (GetMapTimeLeft(timeleft))
   298 	{
   299 		new mins, secs;
   300 		new timelimit;
   301 		
   302 		if (timeleft > 0)
   303 		{
   304 			mins = timeleft / 60;
   305 			secs = timeleft % 60;
   306 			written = true;
   307 			FormatEx(finalOutput, sizeof(finalOutput), "%T %d:%02d", "Timeleft", client, mins, secs);
   308 		}
   309 		else if (GetMapTimeLimit(timelimit) && timelimit == 0)
   310 		{
   311 			notimelimit = true;
   312 		}
   313 		else
   314 		{
   315 			/* 0 timeleft so this must be the last round */
   316 			lastround=true;
   317 		}
   318 	}
   319 	
   320 	if (!lastround)
   321 	{
   322 		if (g_Cvar_WinLimit != INVALID_HANDLE)
   323 		{
   324 			new winlimit = GetConVarInt(g_Cvar_WinLimit);
   325 			
   326 			if (winlimit > 0)
   327 			{
   328 				if (written)
   329 				{
   330 					new len = strlen(finalOutput);
   331 					if (len < sizeof(finalOutput))
   332 					{
   333 						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppend" ,client, winlimit, (winlimit == 1)? "":"s");
   334 					}
   335 				}
   336 				else
   337 				{
   338 					FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimit", client, winlimit, (winlimit == 1)? "":"s");
   339 					written = true;
   340 				}
   341 			}
   342 		}
   343 		
   344 		if (g_Cvar_FragLimit != INVALID_HANDLE)
   345 		{
   346 			new fraglimit = GetConVarInt(g_Cvar_FragLimit);
   347 			
   348 			if (fraglimit > 0)
   349 			{
   350 				if (written)
   351 				{
   352 					new len = strlen(finalOutput);
   353 					if (len < sizeof(finalOutput))
   354 					{
   355 						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppend", client, fraglimit, (fraglimit == 1)? "":"s");
   356 					}	
   357 				}
   358 				else
   359 				{
   360 					FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimit", client, fraglimit, (fraglimit == 1)? "":"s");
   361 					written = true;
   362 				}			
   363 			}
   364 		}
   365 		
   366 		if (g_Cvar_MaxRounds != INVALID_HANDLE)
   367 		{
   368 			new maxrounds = GetConVarInt(g_Cvar_MaxRounds);
   369 			
   370 			if (maxrounds > 0)
   371 			{
   372 				if (written)
   373 				{
   374 					new len = strlen(finalOutput);
   375 					if (len < sizeof(finalOutput))
   376 					{
   377 						FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppend", client, maxrounds, (maxrounds == 1)? "":"s");
   378 					}
   379 				}
   380 				else
   381 				{
   382 					FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRounds", client, maxrounds, (maxrounds == 1)? "":"s");
   383 					written = true;
   384 				}			
   385 			}		
   386 		}
   387 	}
   388 	
   389 	if (lastround)
   390 	{
   391 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "LastRound", client);
   392 	}
   393 	else if (notimelimit && !written)
   394 	{
   395 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "NoTimelimit", client);
   396 	}
   397 
   398 	if (who == TIMELEFT_ALL_ALWAYS
   399 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   400 	{
   401 		PrintToChatAll("[SM] %s", finalOutput);
   402 	}
   403 	else if (client != 0)
   404 	{
   405 		PrintToChat(client, "[SM] %s", finalOutput);
   406 	}
   407 	
   408 	if (client == 0)
   409 	{
   410 		PrintToServer("[SM] %s", finalOutput);
   411 	}
   412 }