plugins/basetriggers.sp
author Michael McKoy <ferret99gt@gmail.com>
Thu Aug 09 00:31:45 2007 +0000 (2007-08-09)
changeset 1285 a83f449ab2f8
parent 1253 82d3637e3dbf
child 1330 ab94316bb52b
permissions -rw-r--r--
Basevote changes based on testing and BAILOFEEDBACK
     1 /**
     2  * basetriggers.sp
     3  * Implements basic information chat triggers like ff and timeleft
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22 #include <sourcemod>
    23 
    24 #pragma semicolon 1
    25 
    26 public Plugin:myinfo = 
    27 {
    28 	name = "Basic Info Triggers",
    29 	author = "AlliedModders LLC",
    30 	description = "Adds ff, timeleft, thetime, and others.",
    31 	version = SOURCEMOD_VERSION,
    32 	url = "http://www.sourcemod.net/"
    33 };
    34 
    35 new Float:g_fStartTime;
    36 new Handle:g_Cvar_TriggerShow = INVALID_HANDLE;
    37 new Handle:g_Cvar_TimeleftInterval = INVALID_HANDLE;
    38 new Handle:g_Cvar_Timelimit = INVALID_HANDLE;
    39 new Handle:g_Cvar_FriendlyFire = INVALID_HANDLE;
    40 
    41 new Handle:g_Timer_TimeShow = INVALID_HANDLE;
    42 
    43 public OnPluginStart()
    44 {
    45 	LoadTranslations("common.phrases");
    46 	LoadTranslations("basetriggers.phrases");
    47 	
    48 	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);	
    49 	g_Cvar_TimeleftInterval = CreateConVar("sm_timeleft_interval", "0.0", "Display timeleft every x seconds. Default 0.", 0, true, 0.0, true, 1800.0);
    50 	g_Cvar_Timelimit = FindConVar("mp_timelimit");
    51 	g_Cvar_FriendlyFire = FindConVar("mp_friendlyfire");
    52 	
    53 	RegConsoleCmd("say", Command_Say);
    54 	RegConsoleCmd("say_team", Command_Say);
    55 	
    56 	HookEvent("round_end", Event_RoundEnd, EventHookMode_Post);
    57 	
    58 	HookConVarChange(g_Cvar_TimeleftInterval, ConVarChange_TimeleftInterval);
    59 }
    60 
    61 public ConVarChange_TimeleftInterval(Handle:convar, const String:oldValue[], const String:newValue[])
    62 {
    63 	new Float:newval = StringToFloat(newValue);
    64 	
    65 	if (newval < 1.0)
    66 	{
    67 		if (g_Timer_TimeShow != INVALID_HANDLE)
    68 		{
    69 			KillTimer(g_Timer_TimeShow);		
    70 		}
    71 		
    72 		return;
    73 	}
    74 	
    75 	if (g_Timer_TimeShow != INVALID_HANDLE)
    76 	{
    77 		KillTimer(g_Timer_TimeShow);
    78 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
    79 	}
    80 	else
    81 		g_Timer_TimeShow = CreateTimer(newval, Timer_DisplayTimeleft, _, TIMER_REPEAT);
    82 }
    83 
    84 public Action:Timer_DisplayTimeleft(Handle:timer)
    85 {
    86 	new mins, secs;
    87 	new timeleft = RoundToNearest(GetTimeLeft());
    88 	
    89 	if (timeleft > 0)
    90 	{
    91 		mins = timeleft / 60;
    92 		secs = timeleft % 60;		
    93 	}
    94 
    95 	PrintToChatAll("[SM] %T %d:%02d", "Timeleft", LANG_SERVER, mins, secs);
    96 }
    97 
    98 public Action:Command_Say(client, args)
    99 {
   100 	decl String:text[192], String:command[64];
   101 	new startidx = 0;
   102 	GetCmdArgString(text, sizeof(text));
   103 	
   104 	if (text[strlen(text)-1] == '"')
   105 	{
   106 		text[strlen(text)-1] = '\0';
   107 		startidx = 1;
   108 	}
   109 
   110 	if (strcmp(command, "say2", false) == 0)
   111 		startidx += 4;
   112 
   113 	if (strcmp(text[startidx], "timeleft", false) == 0)
   114 	{
   115 		new mins, secs;
   116 		new timeleft = RoundToNearest(GetTimeLeft());
   117 		
   118 		if (timeleft > 0)
   119 		{
   120 			mins = timeleft / 60;
   121 			secs = timeleft % 60;		
   122 		}	
   123 		
   124 		if(GetConVarInt(g_Cvar_TriggerShow))
   125 		{
   126 			PrintToChatAll("[SM] %t %d:%02d", "Timeleft", mins, secs);
   127 		}
   128 		else
   129 		{
   130 			PrintToChat(client,"[SM] %t %d:%02d", "Timeleft", mins, secs);
   131 		}
   132 		
   133 	}
   134 	else if (strcmp(text[startidx], "thetime", false) == 0)
   135 	{
   136 		decl String:ctime[64];
   137 		FormatTime(ctime, 64, "%m/%d/%Y - %H:%M:%S");
   138 		
   139 		if(GetConVarInt(g_Cvar_TriggerShow))
   140 		{
   141 			PrintToChatAll("[SM] %t", "Thetime", ctime);
   142 		}
   143 		else
   144 		{
   145 			PrintToChat(client,"[SM] %t", "Thetime", ctime);
   146 		}
   147 	}
   148 	else if (strcmp(text[startidx], "ff", false) == 0 || strcmp(text[startidx], "/ff", false) == 0)
   149 	{
   150 		if (g_Cvar_FriendlyFire != INVALID_HANDLE)
   151 		{
   152 			decl String:message[64];
   153 			if (GetConVarBool(g_Cvar_FriendlyFire))
   154 			{
   155 				Format(message, sizeof(message), "%T", "Friendly Fire On", client);
   156 			}
   157 			else
   158 			{
   159 				Format(message, sizeof(message), "%T", "Friendly Fire Off", client);
   160 			}
   161 		
   162 			if(GetConVarInt(g_Cvar_TriggerShow))
   163 			{
   164 				PrintToChatAll("[SM] %s", message);
   165 			}
   166 			else
   167 			{
   168 				PrintToChat(client,"[SM] %s", message);		
   169 			}
   170 		}
   171 	}
   172 	else if (strcmp(text[startidx], "currentmap", false) == 0)
   173 	{
   174 		decl String:map[64];
   175 		GetCurrentMap(map, sizeof(map));
   176 		
   177 		if(GetConVarInt(g_Cvar_TriggerShow))
   178 		{
   179 			PrintToChatAll("[SM] %t", "Current Map", map);
   180 		}
   181 		else
   182 		{
   183 			PrintToChat(client,"[SM] %t", "Current Map", map);
   184 		}
   185 	}	
   186 	
   187 	return Plugin_Continue;
   188 }
   189 
   190 public Event_RoundEnd(Handle:event, const String:name[], bool:dontBroadcast)
   191 {
   192 	new reason = GetEventInt(event, "reason");
   193 	if(reason == 16)
   194 	{
   195 		g_fStartTime = GetEngineTime();		
   196 	}	
   197 }
   198 
   199 public OnMapStart()
   200 {
   201 	g_fStartTime = GetEngineTime();
   202 }
   203 
   204 Float:GetTimeLeft()
   205 {
   206 	new Float:fLimit = GetConVarFloat(g_Cvar_Timelimit);
   207 	new Float:fElapsed = GetEngineTime() - g_fStartTime;
   208 	
   209 	return (fLimit*60.0) - fElapsed;
   210 }