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