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