scripting/dm_basics.sp
changeset 1 a23c4930415e
child 6 a0574cbe9e53
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scripting/dm_basics.sp	Sun Jun 24 21:59:51 2007 +0000
     1.3 @@ -0,0 +1,266 @@
     1.4 +/**
     1.5 + * dm_basics.sp
     1.6 + * Base CS:S DM admin/control functions.
     1.7 + * This file is part of CS:S DM, Copyright (C) 2005-2007 AlliedModders LLC
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or
    1.10 + * modify it under the terms of the GNU General Public License
    1.11 + * as published by the Free Software Foundation; either version 2
    1.12 + * of the License, or (at your option) any later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful,
    1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + * 
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with this program; if not, write to the Free Software
    1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.22 + *
    1.23 + * Version: $Id$
    1.24 + */
    1.25 +
    1.26 +#pragma semicolon 1
    1.27 +#include <sourcemod>
    1.28 +#include <cssdm>
    1.29 +
    1.30 +/* Plugin stuff */
    1.31 +new Handle:cssdm_respawn_command;
    1.32 +new Handle:cssdm_force_mapchanges;
    1.33 +new Handle:cssdm_mapchange_file;
    1.34 +new Handle:cssdm_refill_ammo;
    1.35 +new Handle:mp_timelimit;
    1.36 +new Handle:g_ChangeMapTimer = INVALID_HANDLE;
    1.37 +new bool:g_AmmoHooks = false;
    1.38 +new g_ActiveWepOffs = -1;
    1.39 +
    1.40 +/* Player stuff */
    1.41 +new Float:g_DeathTimes[MAXPLAYERS+1];
    1.42 +
    1.43 +/** PUBLIC INFO */
    1.44 +public Plugin:myinfo = 
    1.45 +{
    1.46 +	name = "CS:S DM Basics",
    1.47 +	author = "AlliedModders LLC",
    1.48 +	description = "Basic CS:S DM Commands/Features",
    1.49 +	version = CSSDM_VERSION,
    1.50 +	url = "http://www.bailopan.net/cssdm/"
    1.51 +};
    1.52 +
    1.53 +public OnPluginStart()
    1.54 +{
    1.55 +	LoadTranslations("cssdm.phrases");
    1.56 +	
    1.57 +	RegConsoleCmd("say", Command_Say);
    1.58 +	RegConsoleCmd("say_team", Command_Say);
    1.59 +	
    1.60 +	cssdm_respawn_command = CreateConVar("cssdm_respawn_command", "1", "Sets whether clients can manually respawn");
    1.61 +	cssdm_force_mapchanges = CreateConVar("cssdm_force_mapchanges", "0", "Sets whether CS:S DM should force mapchanges");
    1.62 +	cssdm_mapchange_file = CreateConVar("cssdm_mapchange_file", "mapcycle.txt", "Sets the mapchange file for CS:S DM");
    1.63 +	cssdm_refill_ammo = CreateConVar("cssdm_refill_ammo", "1", "Sets whether CS:S DM automatically refills ammo");
    1.64 +	mp_timelimit = FindConVar("mp_timelimit");
    1.65 +	
    1.66 +	HookConVarChange(cssdm_force_mapchanges, CvarChange_RestartMapTimer);
    1.67 +	HookConVarChange(mp_timelimit, CvarChange_RestartMapTimer);	
    1.68 +	HookConVarChange(cssdm_refill_ammo, CvarChange_RefillAmmo);
    1.69 +	
    1.70 +	g_ActiveWepOffs = FindSendPropOffs("CCSPlayer", "m_hActiveWeapon");
    1.71 +}
    1.72 +
    1.73 +public OnConfigsExecuted()
    1.74 +{
    1.75 +	RestartMapTimer();
    1.76 +	
    1.77 +	g_AmmoHooks = GetConVarInt(cssdm_refill_ammo) ? true : false;
    1.78 +	if (g_AmmoHooks && g_ActiveWepOffs > 0)
    1.79 +	{
    1.80 +		HookEvent("weapon_reload", Event_CheckDepleted);
    1.81 +		HookEvent("weapon_fire_on_reload", Event_CheckDepleted);
    1.82 +	}
    1.83 +}
    1.84 +
    1.85 +public OnMapEnd()
    1.86 +{
    1.87 +	if (g_ChangeMapTimer != INVALID_HANDLE)
    1.88 +	{
    1.89 +		CloseHandle(g_ChangeMapTimer);
    1.90 +		g_ChangeMapTimer = INVALID_HANDLE;
    1.91 +	}
    1.92 +	if (g_AmmoHooks)
    1.93 +	{
    1.94 +		UnhookEvent("weapon_reload", Event_CheckDepleted);
    1.95 +		UnhookEvent("weapon_fire_on_reload", Event_CheckDepleted);
    1.96 +	}
    1.97 +}
    1.98 +
    1.99 +public CvarChange_RefillAmmo(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   1.100 +{
   1.101 +	if (GetConVarInt(cvar) && !g_AmmoHooks && g_ActiveWepOffs > 0)
   1.102 +	{
   1.103 +		HookEvent("weapon_reload", Event_CheckDepleted);
   1.104 +		HookEvent("weapon_fire_on_reload", Event_CheckDepleted);
   1.105 +	} else if (!GetConVarInt(cvar) && g_AmmoHooks) {
   1.106 +		UnhookEvent("weapon_reload", Event_CheckDepleted);
   1.107 +		UnhookEvent("weapon_fire_on_reload", Event_CheckDepleted);
   1.108 +	}
   1.109 +}
   1.110 +
   1.111 +public Event_CheckDepleted(Handle:event, const String:name[], bool:dontBroadcast)
   1.112 +{
   1.113 +	new client = GetClientOfUserId(GetEventInt(event, "userid"));
   1.114 +	if (!client || !IsClientInGame(client))
   1.115 +	{
   1.116 +		return;
   1.117 +	}
   1.118 +	
   1.119 +	new entity = GetEntDataEnt(client, g_ActiveWepOffs);
   1.120 +	if (entity < 1)
   1.121 +	{
   1.122 +		return;
   1.123 +	}
   1.124 +	
   1.125 +	new ammoType = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType");
   1.126 +	
   1.127 +	/* Give something reasonable -- the game will chop it off */
   1.128 +	DM_GiveClientAmmo(client, ammoType, 30, false);
   1.129 +}
   1.130 +
   1.131 +public CvarChange_RestartMapTimer(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   1.132 +{
   1.133 +	RestartMapTimer();
   1.134 +}
   1.135 +
   1.136 +RestartMapTimer()
   1.137 +{
   1.138 +	if (g_ChangeMapTimer != INVALID_HANDLE)
   1.139 +	{
   1.140 +		CloseHandle(g_ChangeMapTimer);
   1.141 +		g_ChangeMapTimer = INVALID_HANDLE;
   1.142 +	}
   1.143 +	
   1.144 +	if (GetConVarInt(cssdm_force_mapchanges))
   1.145 +	{
   1.146 +		/* Find how much time is left in the map */
   1.147 +		new Float:seconds = (GetConVarInt(mp_timelimit) * 60.0) - GetGameTime();
   1.148 +		g_ChangeMapTimer = CreateTimer(seconds, Timer_ChangeMap);
   1.149 +	}
   1.150 +}
   1.151 +
   1.152 +public Action:Timer_ChangeMap(Handle:timer)
   1.153 +{
   1.154 +	g_ChangeMapTimer = INVALID_HANDLE;
   1.155 +	
   1.156 +	decl String:changefile[64];
   1.157 +	GetConVarString(cssdm_mapchange_file, changefile, sizeof(changefile));
   1.158 +	
   1.159 +	new Handle:file = OpenFile(changefile, "rt");
   1.160 +	if (!file)
   1.161 +	{
   1.162 +		LogError("[CSSDM] Could not open mapchange file \"%s\"", changefile);
   1.163 +		return Plugin_Stop;
   1.164 +	}
   1.165 +	
   1.166 +	decl String:curmap[64];
   1.167 +	GetCurrentMap(curmap, sizeof(curmap));
   1.168 +	
   1.169 +	new String:firstmap[64];
   1.170 +	new String:lastmap[64];
   1.171 +	decl String:buffer[64];
   1.172 +	new bool:matched = false;
   1.173 +	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   1.174 +	{
   1.175 +		TrimString(buffer);
   1.176 +		if (buffer[0] == '\0' || (!IsCharAlpha(buffer[0]) && !IsCharNumeric(buffer[0])))
   1.177 +		{
   1.178 +			continue;
   1.179 +		}
   1.180 +		if (!IsMapValid(buffer))
   1.181 +		{
   1.182 +			continue;
   1.183 +		}
   1.184 +		if (firstmap[0] == '\0')
   1.185 +		{
   1.186 +			strcopy(firstmap, sizeof(firstmap), buffer);
   1.187 +		}
   1.188 +		strcopy(lastmap, sizeof(lastmap), buffer);
   1.189 +		if (strcmp(buffer, curmap) == 0)
   1.190 +		{
   1.191 +			matched = true;
   1.192 +		} else if (matched) {
   1.193 +			break;
   1.194 +		}
   1.195 +	}
   1.196 +	CloseHandle(file);
   1.197 +	
   1.198 +	if (!matched || (strcmp(buffer, curmap) == 0))
   1.199 +	{
   1.200 +		strcopy(lastmap, sizeof(lastmap), firstmap);
   1.201 +	}
   1.202 +	
   1.203 +	if (lastmap[0] != '\0')
   1.204 +	{
   1.205 +		ServerCommand("changelevel %s", lastmap);
   1.206 +	}
   1.207 +	
   1.208 +	return Plugin_Stop;
   1.209 +}
   1.210 +
   1.211 +public OnClientPutInServer(client)
   1.212 +{
   1.213 +	g_DeathTimes[client] = 0.0;
   1.214 +}
   1.215 +
   1.216 +public Action:DM_OnClientDeath(client)
   1.217 +{
   1.218 +	g_DeathTimes[client] = GetGameTime();
   1.219 +	return Plugin_Continue;
   1.220 +}
   1.221 +
   1.222 +public Action:Command_Say(client, args)
   1.223 +{
   1.224 +	if (!DM_IsRunning() || !GetConVarInt(cssdm_respawn_command))
   1.225 +	{
   1.226 +		return Plugin_Continue;
   1.227 +	}
   1.228 +	
   1.229 +	decl String:command[32];
   1.230 +	GetCmdArg(1, command, sizeof(command));
   1.231 +	
   1.232 +	if (strcmp(command, "respawn") == 0)
   1.233 +	{
   1.234 +		if (!IsClientInGame(client))
   1.235 +		{
   1.236 +			PrintToChat(client, "[CSSDM] %t", "NoRespawn NotInGame");
   1.237 +			return Plugin_Handled;
   1.238 +		}
   1.239 +		
   1.240 +		if (DM_IsClientAlive(client))
   1.241 +		{
   1.242 +			PrintToChat(client, "[CSSDM] %t", "NoRespawn Alive");
   1.243 +			return Plugin_Handled;
   1.244 +		}
   1.245 +		
   1.246 +		new team = GetClientTeam(client);
   1.247 +		if (team != CSSDM_TEAM_T && team != CSSDM_TEAM_CT)
   1.248 +		{
   1.249 +			PrintToChat(client, "[CSSDM] %t", "NoRespawn Team");
   1.250 +			return Plugin_Handled;
   1.251 +		}
   1.252 +		
   1.253 +		new Float:elapsed = GetGameTime() - g_DeathTimes[client];
   1.254 +		new Float:wait_time = DM_GetSpawnWaitTime();
   1.255 +		if (elapsed < wait_time)
   1.256 +		{
   1.257 +			PrintToChat(client, "[CSSDM] %t", "NoRespawn Wait", wait_time - elapsed);
   1.258 +			return Plugin_Handled;
   1.259 +		}
   1.260 +		
   1.261 +		/* We passed the tests... respawn! */
   1.262 +		DM_RespawnClient(client);
   1.263 +		
   1.264 +		return Plugin_Handled;
   1.265 +	}
   1.266 +	
   1.267 +	return Plugin_Continue;
   1.268 +}
   1.269 +