scripting/dm_basics.sp
author David Anderson <dvander@alliedmods.net>
Sun Jun 26 01:15:30 2011 -0700 (2011-06-26)
changeset 75 3d3d958970eb
parent 43 6793d77652a4
child 79 a5a49887015f
permissions -rw-r--r--
Fix build.
     1 /**
     2  * dm_basics.sp
     3  * Base CS:S DM admin/control functions.
     4  * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
     5  * by David "BAILOPAN" Anderson, http://www.bailopan.net/cssdm/
     6  *
     7  * This program is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2
    10  * of the License, or (at your option) any later version.
    11  *
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  * 
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program; if not, write to the Free Software
    19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    20  *
    21  * Version: $Id$
    22  */
    23 
    24 #pragma semicolon 1
    25 #include <sourcemod>
    26 #include <cssdm>
    27 
    28 /* Plugin stuff */
    29 new Handle:cssdm_respawn_command;
    30 new Handle:cssdm_force_mapchanges;
    31 new Handle:cssdm_mapchange_file;
    32 new Handle:cssdm_refill_ammo;
    33 new Handle:mp_timelimit;
    34 new Handle:g_ChangeMapTimer = INVALID_HANDLE;
    35 new bool:g_AmmoHooks = false;
    36 new g_ActiveWepOffs = -1;
    37 
    38 /* Player stuff */
    39 new Float:g_DeathTimes[MAXPLAYERS+1];
    40 
    41 /** PUBLIC INFO */
    42 public Plugin:myinfo = 
    43 {
    44 	name = "CS:S DM Basics",
    45 	author = "AlliedModders LLC",
    46 	description = "Basic CS:S DM Commands/Features",
    47 	version = CSSDM_VERSION,
    48 	url = "http://www.bailopan.net/cssdm/"
    49 };
    50 
    51 public OnPluginStart()
    52 {
    53 	LoadTranslations("cssdm.phrases");
    54 	
    55 	RegConsoleCmd("say", Command_Say);
    56 	RegConsoleCmd("say_team", Command_Say);
    57 	
    58 	cssdm_respawn_command = CreateConVar("cssdm_respawn_command", "1", "Sets whether clients can manually respawn");
    59 	cssdm_force_mapchanges = CreateConVar("cssdm_force_mapchanges", "0", "Sets whether CS:S DM should force mapchanges");
    60 	cssdm_mapchange_file = CreateConVar("cssdm_mapchange_file", "mapcycle.txt", "Sets the mapchange file for CS:S DM");
    61 	cssdm_refill_ammo = CreateConVar("cssdm_refill_ammo", "1", "Sets whether CS:S DM automatically refills ammo");
    62 	mp_timelimit = FindConVar("mp_timelimit");
    63 	
    64 	HookConVarChange(cssdm_force_mapchanges, CvarChange_RestartMapTimer);
    65 	HookConVarChange(mp_timelimit, CvarChange_RestartMapTimer);	
    66 	HookConVarChange(cssdm_refill_ammo, CvarChange_RefillAmmo);
    67 	
    68 	g_ActiveWepOffs = FindSendPropOffs("CCSPlayer", "m_hActiveWeapon");
    69 }
    70 
    71 public DM_OnStartup()
    72 {
    73 	RestartMapTimer();
    74 	
    75 	g_AmmoHooks = GetConVarInt(cssdm_refill_ammo) ? true : false;
    76 	if (g_AmmoHooks && g_ActiveWepOffs > 0)
    77 	{
    78 		HookEvent("weapon_reload", Event_CheckDepleted);
    79 		HookEvent("weapon_fire_on_empty", Event_CheckDepleted);
    80 	}
    81 }
    82 
    83 public DM_OnShutdown()
    84 {
    85 	if (g_ChangeMapTimer != INVALID_HANDLE)
    86 	{
    87 		CloseHandle(g_ChangeMapTimer);
    88 		g_ChangeMapTimer = INVALID_HANDLE;
    89 	}
    90 	ShutdownAmmoHooks();
    91 }
    92 
    93 ShutdownAmmoHooks()
    94 {
    95 	if (g_AmmoHooks)
    96 	{
    97 		UnhookEvent("weapon_reload", Event_CheckDepleted);
    98 		UnhookEvent("weapon_fire_on_empty", Event_CheckDepleted);
    99 		g_AmmoHooks = false;
   100 	}
   101 }
   102 
   103 public CvarChange_RefillAmmo(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   104 {
   105 	if (GetConVarInt(cvar) && !g_AmmoHooks && g_ActiveWepOffs > 0)
   106 	{
   107 		HookEvent("weapon_reload", Event_CheckDepleted);
   108 		HookEvent("weapon_fire_on_empty", Event_CheckDepleted);
   109 	} else if (!GetConVarInt(cvar) && g_AmmoHooks) {
   110 		UnhookEvent("weapon_reload", Event_CheckDepleted);
   111 		UnhookEvent("weapon_fire_on_empty", Event_CheckDepleted);
   112 	}
   113 }
   114 
   115 public Event_CheckDepleted(Handle:event, const String:name[], bool:dontBroadcast)
   116 {
   117 	new client = GetClientOfUserId(GetEventInt(event, "userid"));
   118 	if (!client || !IsClientInGame(client))
   119 	{
   120 		return;
   121 	}
   122 	
   123 	new entity = GetEntDataEnt(client, g_ActiveWepOffs);
   124 	if (entity < 1)
   125 	{
   126 		return;
   127 	}
   128 	
   129 	new ammoType = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType");
   130 	
   131 	/* Give something reasonable -- the game will chop it off */
   132 	DM_GiveClientAmmo(client, ammoType, 30, false);
   133 }
   134 
   135 public CvarChange_RestartMapTimer(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   136 {
   137 	RestartMapTimer();
   138 }
   139 
   140 RestartMapTimer()
   141 {
   142 	if (g_ChangeMapTimer != INVALID_HANDLE)
   143 	{
   144 		CloseHandle(g_ChangeMapTimer);
   145 		g_ChangeMapTimer = INVALID_HANDLE;
   146 	}
   147 	
   148 	if (GetConVarInt(cssdm_force_mapchanges))
   149 	{
   150 		/* Find how much time is left in the map */
   151 		new Float:seconds = (GetConVarInt(mp_timelimit) * 60.0) - GetGameTime();
   152 		g_ChangeMapTimer = CreateTimer(seconds, Timer_ChangeMap);
   153 	}
   154 }
   155 
   156 public Action:Timer_ChangeMap(Handle:timer)
   157 {
   158 	g_ChangeMapTimer = INVALID_HANDLE;
   159 	
   160 	decl String:changefile[64];
   161 	GetConVarString(cssdm_mapchange_file, changefile, sizeof(changefile));
   162 	
   163 	new Handle:file = OpenFile(changefile, "rt");
   164 	if (!file)
   165 	{
   166 		LogError("[CSSDM] Could not open mapchange file \"%s\"", changefile);
   167 		return Plugin_Stop;
   168 	}
   169 	
   170 	decl String:curmap[64];
   171 	GetCurrentMap(curmap, sizeof(curmap));
   172 	
   173 	new String:firstmap[64];
   174 	new String:lastmap[64];
   175 	decl String:buffer[64];
   176 	new bool:matched = false;
   177 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   178 	{
   179 		TrimString(buffer);
   180 		if (buffer[0] == '\0' || (!IsCharAlpha(buffer[0]) && !IsCharNumeric(buffer[0])))
   181 		{
   182 			continue;
   183 		}
   184 		if (!IsMapValid(buffer))
   185 		{
   186 			continue;
   187 		}
   188 		if (firstmap[0] == '\0')
   189 		{
   190 			strcopy(firstmap, sizeof(firstmap), buffer);
   191 		}
   192 		strcopy(lastmap, sizeof(lastmap), buffer);
   193 		if (strcmp(buffer, curmap) == 0)
   194 		{
   195 			matched = true;
   196 		} else if (matched) {
   197 			break;
   198 		}
   199 	}
   200 	CloseHandle(file);
   201 	
   202 	if (!matched || (strcmp(buffer, curmap) == 0))
   203 	{
   204 		strcopy(lastmap, sizeof(lastmap), firstmap);
   205 	}
   206 	
   207 	if (lastmap[0] != '\0')
   208 	{
   209 		ServerCommand("changelevel %s", lastmap);
   210 	}
   211 	
   212 	return Plugin_Stop;
   213 }
   214 
   215 public Action:Timer_Welcome(Handle:timer, any:userid)
   216 {
   217 	new client = GetClientOfUserId(userid);
   218 	
   219 	if (!client || !IsClientInGame(client))
   220 	{
   221 		return Plugin_Stop;
   222 	}
   223 	
   224 	PrintToChat(client, "[CSSDM] Counter-Strike Source: Deathmatch (version %s)", CSSDM_VERSION);
   225 	PrintToChat(client, "[CSSDM] Visit http://www.bailopan.net/cssdm to download.");
   226 	
   227 	return Plugin_Stop;
   228 }
   229 
   230 public OnClientPutInServer(client)
   231 {
   232 	g_DeathTimes[client] = 0.0;
   233 	CreateTimer(10.0, Timer_Welcome, GetClientUserId(client));
   234 }
   235 
   236 public Action:DM_OnClientDeath(client)
   237 {
   238 	g_DeathTimes[client] = GetGameTime();
   239 	return Plugin_Continue;
   240 }
   241 
   242 public Action:Command_Say(client, args)
   243 {
   244 	if (!DM_IsRunning() || !GetConVarInt(cssdm_respawn_command))
   245 	{
   246 		return Plugin_Continue;
   247 	}
   248 	
   249 	decl String:command[32];
   250 	GetCmdArg(1, command, sizeof(command));
   251 	
   252 	if (strcmp(command, "respawn") == 0)
   253 	{
   254 		if (!IsClientInGame(client))
   255 		{
   256 			PrintToChat(client, "[CSSDM] %t", "NoRespawn NotInGame");
   257 			return Plugin_Handled;
   258 		}
   259 		
   260 		if (DM_IsClientAlive(client))
   261 		{
   262 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Alive");
   263 			return Plugin_Handled;
   264 		}
   265 		
   266 		new team = GetClientTeam(client);
   267 		if (team != CSSDM_TEAM_T && team != CSSDM_TEAM_CT)
   268 		{
   269 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Team");
   270 			return Plugin_Handled;
   271 		}
   272 		
   273 		new Float:elapsed = GetGameTime() - g_DeathTimes[client];
   274 		new Float:wait_time = DM_GetSpawnWaitTime();
   275 		if (elapsed < wait_time)
   276 		{
   277 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Wait", wait_time - elapsed);
   278 			return Plugin_Handled;
   279 		}
   280 		
   281 		/* We passed the tests... respawn! */
   282 		DM_RespawnClient(client);
   283 		
   284 		return Plugin_Handled;
   285 	}
   286 	
   287 	return Plugin_Continue;
   288 }
   289