plugins/basetriggers.sp
author Scott Ehlert <ds@alliedmods.net>
Tue Jul 06 17:32:58 2010 -0500 (2010-07-06)
changeset 3071 52e383b58df7
parent 3052 a102c645bf4b
child 3294 eb106d53ce40
permissions -rw-r--r--
Added GetConVarDefault() native (bug 4502, r=dvander).
     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 	if (client == 0)
   214 	{
   215 		ReplyToCommand(client, "[SM] %t", "Command is in-game only");
   216 		return Plugin_Handled;
   217 	}
   218 
   219 	ShowMOTDPanel(client, "Message Of The Day", "motd", MOTDPANEL_TYPE_INDEX);
   220 
   221 	return Plugin_Handled;
   222 }
   223 
   224 public Action:Command_Say(client, args)
   225 {
   226 	decl String:text[192], String:command[64];
   227 	new startidx = 0;
   228 	if (GetCmdArgString(text, sizeof(text)) < 1)
   229 	{
   230 		return Plugin_Continue;
   231 	}
   232 	
   233 	if (text[strlen(text)-1] == '"')
   234 	{
   235 		text[strlen(text)-1] = '\0';
   236 		startidx = 1;
   237 	}
   238 
   239 	if (strcmp(command, "say2", false) == 0)
   240 		startidx += 4;
   241 
   242 	if (strcmp(text[startidx], "timeleft", false) == 0)
   243 	{
   244 		ShowTimeLeft(client, TIMELEFT_ALL_MAYBE);
   245 	}
   246 	else if (strcmp(text[startidx], "thetime", false) == 0)
   247 	{
   248 		decl String:ctime[64];
   249 		FormatTime(ctime, 64, NULL_STRING);
   250 		
   251 		if(GetConVarInt(g_Cvar_TriggerShow))
   252 		{
   253 			PrintToChatAll("[SM] %t", "Thetime", ctime);
   254 		}
   255 		else
   256 		{
   257 			PrintToChat(client,"[SM] %t", "Thetime", ctime);
   258 		}
   259 	}
   260 	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
   261 	{
   262 		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
   263 		{
   264 			decl String:message[64];
   265 			if (GetConVarBool(g_Cvar_FriendlyFire))
   266 			{
   267 				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
   268 			}
   269 			else
   270 			{
   271 				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
   272 			}
   273 		
   274 			if(GetConVarInt(g_Cvar_TriggerShow))
   275 			{
   276 				PrintToChatAll("[SM] %s", message);
   277 			}
   278 			else
   279 			{
   280 				PrintToChat(client,"[SM] %s", message);		
   281 			}
   282 		}
   283 	}
   284 	else if (strcmp(text[startidx], "currentmap", false) == 0)
   285 	{
   286 		decl String:map[64];
   287 		GetCurrentMap(map, sizeof(map));
   288 		
   289 		if(GetConVarInt(g_Cvar_TriggerShow))
   290 		{
   291 			PrintToChatAll("[SM] %t", "Current Map", map);
   292 		}
   293 		else
   294 		{
   295 			PrintToChat(client,"[SM] %t", "Current Map", map);
   296 		}
   297 	}
   298 	else if (strcmp(text[startidx], "nextmap", false) == 0)
   299 	{
   300 		decl String:map[32];
   301 		GetNextMap(map, sizeof(map));
   302 			
   303 		if(GetConVarInt(g_Cvar_TriggerShow))
   304 		{
   305 			if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   306 			{
   307 				PrintToChatAll("[SM] %t", "Pending Vote");			
   308 			}
   309 			else
   310 			{
   311 				PrintToChatAll("[SM] %t", "Next Map", map);
   312 			}
   313 		}
   314 		else
   315 		{
   316 			if (mapchooser && EndOfMapVoteEnabled() && !HasEndOfMapVoteFinished())
   317 			{
   318 				PrintToChat(client, "[SM] %t", "Pending Vote");			
   319 			}
   320 			else
   321 			{
   322 				PrintToChat(client, "[SM] %t", "Next Map", map);
   323 			}
   324 		}
   325 	}
   326 	else if (strcmp(text[startidx], "motd", false) == 0)
   327 	{
   328 		ShowMOTDPanel(client, "Message Of The Day", "motd", MOTDPANEL_TYPE_INDEX);
   329 	}
   330 	
   331 	return Plugin_Continue;
   332 }
   333 
   334 ShowTimeLeft(client, who)
   335 {
   336 	new bool:lastround = false;
   337 	new bool:written = false;
   338 	new bool:notimelimit = false;
   339 	
   340 	decl String:finalOutput[1024];
   341 	finalOutput[0] = 0;
   342 	
   343 	if (who == TIMELEFT_ALL_ALWAYS
   344 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   345 	{
   346 		client = 0;	
   347 	}
   348 	
   349 	new timeleft;
   350 	if (GetMapTimeLeft(timeleft))
   351 	{
   352 		new mins, secs;
   353 		new timelimit;
   354 		
   355 		if (timeleft > 0)
   356 		{
   357 			mins = timeleft / 60;
   358 			secs = timeleft % 60;
   359 			written = true;
   360 			FormatEx(finalOutput, sizeof(finalOutput), "%T %d:%02d", "Timeleft", client, mins, secs);
   361 		}
   362 		else if (GetMapTimeLimit(timelimit) && timelimit == 0)
   363 		{
   364 			notimelimit = true;
   365 		}
   366 		else
   367 		{
   368 			/* 0 timeleft so this must be the last round */
   369 			lastround=true;
   370 		}
   371 	}
   372 	
   373 	if (!lastround)
   374 	{
   375 		if (g_Cvar_WinLimit != INVALID_HANDLE)
   376 		{
   377 			new winlimit = GetConVarInt(g_Cvar_WinLimit);
   378 			
   379 			if (winlimit > 0)
   380 			{
   381 				if (written)
   382 				{
   383 					new len = strlen(finalOutput);
   384 					if (len < sizeof(finalOutput))
   385 					{
   386 						if (winlimit > 1)
   387 						{
   388 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppendPlural" ,client, winlimit);
   389 						}
   390 						else
   391 						{
   392 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "WinLimitAppend" ,client);
   393 						}
   394 					}
   395 				}
   396 				else
   397 				{
   398 					if (winlimit > 1)
   399 					{
   400 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimitPlural", client, winlimit);
   401 					}
   402 					else
   403 					{
   404 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "WinLimit", client);
   405 					}
   406 					
   407 					written = true;
   408 				}
   409 			}
   410 		}
   411 		
   412 		if (g_Cvar_FragLimit != INVALID_HANDLE)
   413 		{
   414 			new fraglimit = GetConVarInt(g_Cvar_FragLimit);
   415 			
   416 			if (fraglimit > 0)
   417 			{
   418 				if (written)
   419 				{
   420 					new len = strlen(finalOutput);
   421 					if (len < sizeof(finalOutput))
   422 					{
   423 						if (fraglimit > 1)
   424 						{
   425 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppendPlural", client, fraglimit);
   426 						}
   427 						else
   428 						{
   429 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "FragLimitAppend", client);
   430 						}
   431 					}	
   432 				}
   433 				else
   434 				{
   435 					if (fraglimit > 1)
   436 					{
   437 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimitPlural", client, fraglimit);
   438 					}
   439 					else
   440 					{
   441 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "FragLimit", client);
   442 					}
   443 					
   444 					written = true;
   445 				}			
   446 			}
   447 		}
   448 		
   449 		if (g_Cvar_MaxRounds != INVALID_HANDLE)
   450 		{
   451 			new maxrounds = GetConVarInt(g_Cvar_MaxRounds);
   452 			
   453 			if (maxrounds > 0)
   454 			{
   455 				new remaining = maxrounds - g_TotalRounds;
   456 				
   457 				if (written)
   458 				{
   459 					new len = strlen(finalOutput);
   460 					if (len < sizeof(finalOutput))
   461 					{
   462 						if (remaining > 1)
   463 						{
   464 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppendPlural", client, remaining);
   465 						}
   466 						else
   467 						{
   468 							FormatEx(finalOutput[len], sizeof(finalOutput)-len, "%T", "MaxRoundsAppend", client);
   469 						}
   470 					}
   471 				}
   472 				else
   473 				{
   474 					if (remaining > 1)
   475 					{
   476 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRoundsPlural", client, remaining);
   477 					}
   478 					else
   479 					{
   480 						FormatEx(finalOutput, sizeof(finalOutput), "%T", "MaxRounds", client);
   481 					}
   482 					
   483 					written = true;
   484 				}			
   485 			}		
   486 		}
   487 	}
   488 	
   489 	if (lastround)
   490 	{
   491 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "LastRound", client);
   492 	}
   493 	else if (notimelimit && !written)
   494 	{
   495 		FormatEx(finalOutput, sizeof(finalOutput), "%T", "NoTimelimit", client);
   496 	}
   497 
   498 	if (who == TIMELEFT_ALL_ALWAYS
   499 		|| (who == TIMELEFT_ALL_MAYBE && GetConVarInt(g_Cvar_TriggerShow)))
   500 	{
   501 		PrintToChatAll("[SM] %s", finalOutput);
   502 	}
   503 	else if (client != 0)
   504 	{
   505 		PrintToChat(client, "[SM] %s", finalOutput);
   506 	}
   507 	
   508 	if (client == 0)
   509 	{
   510 		PrintToServer("[SM] %s", finalOutput);
   511 	}
   512 }