plugins/basetriggers.sp
author David Anderson <dvander@alliedmods.net>
Wed Aug 29 00:28:19 2007 +0000 (2007-08-29)
changeset 1381 a200ec1310f0
parent 1359 c93c61f32568
child 1422 47de36d9386c
permissions -rw-r--r--
added note about putinserver and steamids
     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-2007 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 public Plugin:myinfo = 
    39 {
    40 	name = "Basic Info Triggers",
    41 	author = "AlliedModders LLC",
    42 	description = "Adds ff, timeleft, thetime, and others.",
    43 	version = SOURCEMOD_VERSION,
    44 	url = "http://www.sourcemod.net/"
    45 };
    46 
    47 new Float:g_fStartTime;
    48 new Handle:g_Cvar_TriggerShow = INVALID_HANDLE;
    49 new Handle:g_Cvar_TimeleftInterval = INVALID_HANDLE;
    50 new Handle:g_Cvar_Timelimit = INVALID_HANDLE;
    51 new Handle:g_Cvar_FriendlyFire = INVALID_HANDLE;
    52 
    53 new Handle:g_Timer_TimeShow = INVALID_HANDLE;
    54 
    55 public OnPluginStart()
    56 {
    57 	LoadTranslations("common.phrases");
    58 	LoadTranslations("basetriggers.phrases");
    59 	
    60 	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);	
    61 	g_Cvar_TimeleftInterval = CreateConVar("sm_timeleft_interval", "0.0", "Display timeleft every x seconds. Default 0.", 0, true, 0.0, true, 1800.0);
    62 	g_Cvar_Timelimit = FindConVar("mp_timelimit");
    63 	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
    64 	
    65 	RegConsoleCmd("say", Command_Say);
    66 	RegConsoleCmd("say_team", Command_Say);
    67 	
    68 	HookEvent("round_end", Event_RoundEnd, EventHookMode_Post);
    69 	
    70 	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
    71 }
    72 
    73 public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
    74 {
    75 	new Float:newval = StringToFloat(newValue);
    76 	
    77 	if (newval < 1.0)
    78 	{
    79 		if (g_Timer_TimeShow != INVALID_HANDLE)
    80 		{
    81 			KillTimer(g_Timer_TimeShow);		
    82 		}
    83 		
    84 		return;
    85 	}
    86 	
    87 	if (g_Timer_TimeShow != INVALID_HANDLE)
    88 	{
    89 		KillTimer(g_Timer_TimeShow);
    90 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
    91 	}
    92 	else
    93 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
    94 }
    95 
    96 public Action:Timer_DisplayTimeleft(Handle:timer)
    97 {
    98 	new mins, secs;
    99 	new timeleft = RoundToNearest(GetTimeLeft());
   100 	
   101 	if (timeleft > 0)
   102 	{
   103 		mins = timeleft / 60;
   104 		secs = timeleft % 60;		
   105 	}
   106 
   107 	PrintToChatAll("[SM] %T %d:%02d", "Timeleft", LANG_SERVER, mins, secs);
   108 }
   109 
   110 public Action:Command_Say(client, args)
   111 {
   112 	decl String:text[192], String:command[64];
   113 	new startidx = 0;
   114 	GetCmdArgString(text, sizeof(text));
   115 	
   116 	if (text[strlen(text)-1] == '"')
   117 	{
   118 		text[strlen(text)-1] = '\0';
   119 		startidx = 1;
   120 	}
   121 
   122 	if (strcmp(command, "say2", false) == 0)
   123 		startidx += 4;
   124 
   125 	if (strcmp(text[startidx], "timeleft", false) == 0)
   126 	{
   127 		new mins, secs;
   128 		new timeleft = RoundToNearest(GetTimeLeft());
   129 		
   130 		if (timeleft > 0)
   131 		{
   132 			mins = timeleft / 60;
   133 			secs = timeleft % 60;		
   134 		}	
   135 		
   136 		if(GetConVarInt(g_Cvar_TriggerShow))
   137 		{
   138 			PrintToChatAll("[SM] %t %d:%02d", "Timeleft", mins, secs);
   139 		}
   140 		else
   141 		{
   142 			PrintToChat(client,"[SM] %t %d:%02d", "Timeleft", mins, secs);
   143 		}
   144 		
   145 	}
   146 	else if (strcmp(text[startidx], "thetime", false) == 0)
   147 	{
   148 		decl String:ctime[64];
   149 		FormatTime(ctime, 64, NULL_STRING);
   150 		
   151 		if(GetConVarInt(g_Cvar_TriggerShow))
   152 		{
   153 			PrintToChatAll("[SM] %t", "Thetime", ctime);
   154 		}
   155 		else
   156 		{
   157 			PrintToChat(client,"[SM] %t", "Thetime", ctime);
   158 		}
   159 	}
   160 	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
   161 	{
   162 		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
   163 		{
   164 			decl String:message[64];
   165 			if (GetConVarBool(g_Cvar_FriendlyFire))
   166 			{
   167 				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
   168 			}
   169 			else
   170 			{
   171 				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
   172 			}
   173 		
   174 			if(GetConVarInt(g_Cvar_TriggerShow))
   175 			{
   176 				PrintToChatAll("[SM] %s", message);
   177 			}
   178 			else
   179 			{
   180 				PrintToChat(client,"[SM] %s", message);		
   181 			}
   182 		}
   183 	}
   184 	else if (strcmp(text[startidx], "currentmap", false) == 0)
   185 	{
   186 		decl String:map[64];
   187 		GetCurrentMap(map, sizeof(map));
   188 		
   189 		if(GetConVarInt(g_Cvar_TriggerShow))
   190 		{
   191 			PrintToChatAll("[SM] %t", "Current Map", map);
   192 		}
   193 		else
   194 		{
   195 			PrintToChat(client,"[SM] %t", "Current Map", map);
   196 		}
   197 	}	
   198 	
   199 	return Plugin_Continue;
   200 }
   201 
   202 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   203 {
   204 	new reason = GetEventInt(event, "reason");
   205 	if(reason == 16)
   206 	{
   207 		g_fStartTime = GetEngineTime();		
   208 	}	
   209 }
   210 
   211 public OnMapStart()
   212 {
   213 	g_fStartTime = GetEngineTime();
   214 }
   215 
   216 Float:GetTimeLeft()
   217 {
   218 	new Float:fLimit = GetConVarFloat(g_Cvar_Timelimit);
   219 	new Float:fElapsed = GetEngineTime() - g_fStartTime;
   220 	
   221 	return (fLimit*60.0) - fElapsed;
   222 }