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