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