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