plugins/basetriggers.sp
author Liam <rbutler14@yahoo.com>
Sat Mar 07 23:27:10 2009 -0500 (2009-03-07)
changeset 2681 3243c1b63c4c
parent 2552 30bad9a12191
child 2779 45b3d5d9b873
permissions -rw-r--r--
Fixed typo in code that broke buildbot. (Bug 3698 r=Liam)
     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 new g_TotalRounds;
    68 
    69 public OnPluginStart()
    70 {
    71 	LoadTranslations("common.phrases");
    72 	LoadTranslations("basetriggers.phrases");
    73 	
    74 	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);	
    75 	g_Cvar_TimeleftInterval = CreateConVar("sm_timeleft_interval", "0.0", "Display timeleft every x seconds. Default 0.", 0, true, 0.0, true, 1800.0);
    76 	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
    77 	
    78 	RegConsoleCmd("say", Command_Say);
    79 	RegConsoleCmd("say_team", Command_Say);
    80 	
    81 	RegConsoleCmd("timeleft", Command_Timeleft);
    82 	RegConsoleCmd("nextmap", Command_Nextmap);
    83 	
    84 	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
    85 
    86 	decl String:folder[64];   	 
    87    	GetGameFolderName(folder, sizeof(folder));
    88 
    89    	if (strcmp(folder, "insurgency") == 0)
    90    	{
    91    		HookEvent("game_newmap", Event_GameStart);
    92    	}
    93    	else
    94    	{
    95    		HookEvent("game_start", Event_GameStart);
    96    	}
    97 	
    98 	HookEvent("round_end", Event_RoundEnd);
    99 	HookEventEx("teamplay_win_panel", Event_TeamPlayWinPanel);
   100 	HookEventEx("teamplay_restart_round", Event_TFRestartRound);
   101 	HookEventEx("arena_win_panel", Event_TeamPlayWinPanel);
   102 	
   103 	g_Cvar_WinLimit = FindConVar("mp_winlimit");
   104 	g_Cvar_FragLimit = FindConVar("mp_fraglimit");
   105 	g_Cvar_MaxRounds = FindConVar("mp_maxrounds");
   106 	
   107 	mapchooser = LibraryExists("mapchooser");
   108 }
   109 
   110 public OnMapStart()
   111 {
   112 	g_TotalRounds = 0;	
   113 }
   114 
   115 /* Round count tracking */
   116 public Event_TFRestartRound(Handle:event, const String:name[], bool:dontBroadcast)
   117 {
   118 	/* Game got restarted - reset our round count tracking */
   119 	g_TotalRounds = 0;	
   120 }
   121 
   122 public Event_GameStart(Handle:event, const String:name[], bool:dontBroadcast)
   123 {
   124 	/* Game got restarted - reset our round count tracking */
   125 	g_TotalRounds = 0;	
   126 }
   127 
   128 public Event_TeamPlayWinPanel(Handle:event, const String:name[], bool:dontBroadcast)
   129 {
   130 	if(GetEventInt(event, "round_complete") == 1 || StrEqual(name, "arena_win_panel"))
   131 	{
   132 		g_TotalRounds++;
   133 	}
   134 }
   135 /* You ask, why don't you just use team_score event? And I answer... Because CSS doesn't. */
   136 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   137 {
   138 	g_TotalRounds++;
   139 }
   140 
   141 public OnLibraryRemoved(const String:name[])
   142 {
   143 	if (StrEqual(name, "mapchooser"))
   144 	{
   145 		mapchooser = false;
   146 	}
   147 }
   148  
   149 public OnLibraryAdded(const String:name[])
   150 {
   151 	if (StrEqual(name, "mapchooser"))
   152 	{
   153 		mapchooser = true;
   154 	}
   155 }
   156 
   157 public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
   158 {
   159 	new Float:newval = StringToFloat(newValue);
   160 	
   161 	if (newval < 1.0)
   162 	{
   163 		if (g_Timer_TimeShow != INVALID_HANDLE)
   164 		{
   165 			KillTimer(g_Timer_TimeShow);		
   166 		}
   167 		
   168 		return;
   169 	}
   170 	
   171 	if (g_Timer_TimeShow != INVALID_HANDLE)
   172 	{
   173 		KillTimer(g_Timer_TimeShow);
   174 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
   175 	}
   176 	else
   177 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
   178 }
   179 
   180 public Action:Timer_DisplayTimeleft(Handle:timer)
   181 {
   182 	ShowTimeLeft(0, TIMELEFT_ALL_ALWAYS);	
   183 }
   184 
   185 public Action:Command_Timeleft(client, args)
   186 {
   187 	ShowTimeLeft(client, TIMELEFT_ONE);
   188 	
   189 	return Plugin_Handled;
   190 }
   191 
   192 public Action:Command_Nextmap(client, args)
   193 {
   194 	decl String:map[64];
   195 	
   196 	GetNextMap(map, sizeof(map));
   197 	
   198 	if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   199 	{
   200 		ReplyToCommand(client, "[SM] %t", "Pending Vote");			
   201 	}
   202 	else
   203 	{
   204 		ReplyToCommand(client, "[SM] %t", "Next Map", map);
   205 	}
   206 	
   207 	return Plugin_Handled;
   208 }
   209 
   210 public Action:Command_Say(client, args)
   211 {
   212 	decl String:text[192], String:command[64];
   213 	new startidx = 0;
   214 	if (GetCmdArgString(text, sizeof(text)) < 1)
   215 	{
   216 		return Plugin_Continue;
   217 	}
   218 	
   219 	if (text[strlen(text)-1] == '"')
   220 	{
   221 		text[strlen(text)-1] = '\0';
   222 		startidx = 1;
   223 	}
   224 
   225 	if (strcmp(command, "say2", false) == 0)
   226 		startidx += 4;
   227 
   228 	if (strcmp(text[startidx], "timeleft", false) == 0)
   229 	{
   230 		ShowTimeLeft(client, TIMELEFT_ALL_MAYBE);
   231 	}
   232 	else if (strcmp(text[startidx], "thetime", false) == 0)
   233 	{
   234 		decl String:ctime[64];
   235 		FormatTime(ctime, 64, NULL_STRING);
   236 		
   237 		if(GetConVarInt(g_Cvar_TriggerShow))
   238 		{
   239 			PrintToChatAll("[SM] %t", "Thetime", ctime);
   240 		}
   241 		else
   242 		{
   243 			PrintToChat(client,"[SM] %t", "Thetime", ctime);
   244 		}
   245 	}
   246 	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
   247 	{
   248 		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
   249 		{
   250 			decl String:message[64];
   251 			if (GetConVarBool(g_Cvar_FriendlyFire))
   252 			{
   253 				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
   254 			}
   255 			else
   256 			{
   257 				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
   258 			}
   259 		
   260 			if(GetConVarInt(g_Cvar_TriggerShow))
   261 			{
   262 				PrintToChatAll("[SM] %s", message);
   263 			}
   264 			else
   265 			{
   266 				PrintToChat(client,"[SM] %s", message);		
   267 			}
   268 		}
   269 	}
   270 	else if (strcmp(text[startidx], "currentmap", false) == 0)
   271 	{
   272 		decl String:map[64];
   273 		GetCurrentMap(map, sizeof(map));
   274 		
   275 		if(GetConVarInt(g_Cvar_TriggerShow))
   276 		{
   277 			PrintToChatAll("[SM] %t", "Current Map", map);
   278 		}
   279 		else
   280 		{
   281 			PrintToChat(client,"[SM] %t", "Current Map", map);
   282 		}
   283 	}
   284 	else if (strcmp(text[startidx], "nextmap", false) == 0)
   285 	{
   286 		decl String:map[32];
   287 		GetNextMap(map, sizeof(map));
   288 			
   289 		if(GetConVarInt(g_Cvar_TriggerShow))
   290 		{
   291 			if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   292 			{
   293 				PrintToChatAll("[SM] %t", "Pending Vote");			
   294 			}
   295 			else
   296 			{
   297 				PrintToChatAll("[SM] %t", "Next Map", map);
   298 			}
   299 		}
   300 		else
   301 		{
   302 			if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   303 			{
   304 				PrintToChat(client, "[SM] %t", "Pending Vote");			
   305 			}
   306 			else
   307 			{
   308 				PrintToChat(client, "[SM] %t", "Next Map", map);
   309 			}
   310 		}
   311 	}
   312 	
   313 	return Plugin_Continue;
   314 }
   315 
   316 ShowTimeLeft(client, who)
   317 {
   318 	new bool:lastround = false;
   319 	new bool:written = false;
   320 	new bool:notimelimit = false;
   321 	
   322 	decl String:finalOutput[1024];
   323 	finalOutput[0] = 0;
   324 	
   325 	if (who == TIMELEFT_ALL_ALWAYS
   326 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   327 	{
   328 		client = 0;	
   329 	}
   330 	
   331 	new timeleft;
   332 	if (GetMapTimeLeft(timeleft))
   333 	{
   334 		new mins, secs;
   335 		new timelimit;
   336 		
   337 		if (timeleft > 0)
   338 		{
   339 			mins = timeleft / 60;
   340 			secs = timeleft % 60;
   341 			written = true;
   342 			FormatEx(finalOutput, sizeof(finalOutput), "%T %d:%02d", "Timeleft", client, mins, secs);
   343 		}
   344 		else if (GetMapTimeLimit(timelimit) && timelimit == 0)
   345 		{
   346 			notimelimit = true;
   347 		}
   348 		else
   349 		{
   350 			/* 0 timeleft so this must be the last round */
   351 			lastround=true;
   352 		}
   353 	}
   354 	
   355 	if (!lastround)
   356 	{
   357 		if (g_Cvar_WinLimit != INVALID_HANDLE)
   358 		{
   359 			new winlimit = GetConVarInt(g_Cvar_WinLimit);
   360 			
   361 			if (winlimit > 0)
   362 			{
   363 				if (written)
   364 				{
   365 					new len = strlen(finalOutput);
   366 					if (len < sizeof(finalOutput))
   367 					{
   368 						if (winlimit > 1)
   369 						{
   370 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppendPlural" ,client, winlimit);
   371 						}
   372 						else
   373 						{
   374 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppend" ,client);
   375 						}
   376 					}
   377 				}
   378 				else
   379 				{
   380 					if (winlimit > 1)
   381 					{
   382 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimitPlural", client, winlimit);
   383 					}
   384 					else
   385 					{
   386 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimit", client);
   387 					}
   388 					
   389 					written = true;
   390 				}
   391 			}
   392 		}
   393 		
   394 		if (g_Cvar_FragLimit != INVALID_HANDLE)
   395 		{
   396 			new fraglimit = GetConVarInt(g_Cvar_FragLimit);
   397 			
   398 			if (fraglimit > 0)
   399 			{
   400 				if (written)
   401 				{
   402 					new len = strlen(finalOutput);
   403 					if (len < sizeof(finalOutput))
   404 					{
   405 						if (fraglimit > 1)
   406 						{
   407 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppendPlural", client, fraglimit);
   408 						}
   409 						else
   410 						{
   411 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppend", client);
   412 						}
   413 					}	
   414 				}
   415 				else
   416 				{
   417 					if (fraglimit > 1)
   418 					{
   419 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimitPlural", client, fraglimit);
   420 					}
   421 					else
   422 					{
   423 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimit", client);
   424 					}
   425 					
   426 					written = true;
   427 				}			
   428 			}
   429 		}
   430 		
   431 		if (g_Cvar_MaxRounds != INVALID_HANDLE)
   432 		{
   433 			new maxrounds = GetConVarInt(g_Cvar_MaxRounds);
   434 			
   435 			if (maxrounds > 0)
   436 			{
   437 				new remaining = maxrounds - g_TotalRounds;
   438 				
   439 				if (written)
   440 				{
   441 					new len = strlen(finalOutput);
   442 					if (len < sizeof(finalOutput))
   443 					{
   444 						if (remaining > 1)
   445 						{
   446 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppendPlural", client, remaining);
   447 						}
   448 						else
   449 						{
   450 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppend", client);
   451 						}
   452 					}
   453 				}
   454 				else
   455 				{
   456 					if (remaining > 1)
   457 					{
   458 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRoundsPlural", client, remaining);
   459 					}
   460 					else
   461 					{
   462 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRounds", client);
   463 					}
   464 					
   465 					written = true;
   466 				}			
   467 			}		
   468 		}
   469 	}
   470 	
   471 	if (lastround)
   472 	{
   473 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "LastRound", client);
   474 	}
   475 	else if (notimelimit && !written)
   476 	{
   477 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "NoTimelimit", client);
   478 	}
   479 
   480 	if (who == TIMELEFT_ALL_ALWAYS
   481 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   482 	{
   483 		PrintToChatAll("[SM] %s", finalOutput);
   484 	}
   485 	else if (client != 0)
   486 	{
   487 		PrintToChat(client, "[SM] %s", finalOutput);
   488 	}
   489 	
   490 	if (client == 0)
   491 	{
   492 		PrintToServer("[SM] %s", finalOutput);
   493 	}
   494 }