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