scripting/dm_basics.sp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Jul 02 13:57:28 2014 -0400 (2014-07-02)
changeset 185 813f027b06cb
parent 80 2cbda5738ddd
permissions -rw-r--r--
Add missing comas to ExtensionBuilder file list
     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 <sdktools>
    27 #include <cssdm>
    28 
    29 /* Plugin stuff */
    30 new Handle:cssdm_respawn_command;
    31 new Handle:cssdm_force_mapchanges;
    32 new Handle:cssdm_mapchange_file;
    33 new Handle:cssdm_refill_ammo;
    34 new Handle:mp_timelimit;
    35 new Handle:g_ChangeMapTimer = INVALID_HANDLE;
    36 new bool:g_AmmoHooks = false;
    37 new g_ActiveWepOffs = -1;
    38 
    39 /* Player stuff */
    40 new Float:g_DeathTimes[MAXPLAYERS+1];
    41 new bool:g_bHasNoAmmoInClip1[MAXPLAYERS+1] = {false,...};
    42 
    43 /** PUBLIC INFO */
    44 public Plugin:myinfo = 
    45 {
    46 	name = "CS:S DM Basics",
    47 	author = "AlliedModders LLC",
    48 	description = "Basic CS:S DM Commands/Features",
    49 	version = CSSDM_VERSION,
    50 	url = "http://www.bailopan.net/cssdm/"
    51 };
    52 
    53 public OnPluginStart()
    54 {
    55 	LoadTranslations("cssdm.phrases");
    56 	
    57 	RegConsoleCmd("say", Command_Say);
    58 	RegConsoleCmd("say_team", Command_Say);
    59 	
    60 	cssdm_respawn_command = CreateConVar("cssdm_respawn_command", "1", "Sets whether clients can manually respawn");
    61 	cssdm_force_mapchanges = CreateConVar("cssdm_force_mapchanges", "0", "Sets whether CS:S DM should force mapchanges");
    62 	cssdm_mapchange_file = CreateConVar("cssdm_mapchange_file", "mapcycle.txt", "Sets the mapchange file for CS:S DM");
    63 	cssdm_refill_ammo = CreateConVar("cssdm_refill_ammo", "1", "Sets whether CS:S DM automatically refills ammo");
    64 	mp_timelimit = FindConVar("mp_timelimit");
    65 	
    66 	HookConVarChange(cssdm_force_mapchanges, CvarChange_RestartMapTimer);
    67 	HookConVarChange(mp_timelimit, CvarChange_RestartMapTimer);	
    68 	HookConVarChange(cssdm_refill_ammo, CvarChange_RefillAmmo);
    69 	
    70 	g_ActiveWepOffs = FindSendPropOffs("CCSPlayer", "m_hActiveWeapon");
    71 }
    72 
    73 public DM_OnStartup()
    74 {
    75 	RestartMapTimer();
    76 	
    77 	g_AmmoHooks = GetConVarInt(cssdm_refill_ammo) ? true : false;
    78 	if (g_AmmoHooks && g_ActiveWepOffs > 0)
    79 	{
    80 		HookEvent("weapon_reload", Event_CheckDepleted);
    81 		HookEvent("weapon_fire_on_empty", Event_CheckDepleted);
    82 	}
    83 }
    84 
    85 public DM_OnShutdown()
    86 {
    87 	if (g_ChangeMapTimer != INVALID_HANDLE)
    88 	{
    89 		CloseHandle(g_ChangeMapTimer);
    90 		g_ChangeMapTimer = INVALID_HANDLE;
    91 	}
    92 	ShutdownAmmoHooks();
    93 }
    94 
    95 ShutdownAmmoHooks()
    96 {
    97 	if (g_AmmoHooks)
    98 	{
    99 		UnhookEvent("weapon_reload", Event_CheckDepleted);
   100 		UnhookEvent("weapon_fire_on_empty", Event_CheckDepleted);
   101 		g_AmmoHooks = false;
   102 	}
   103 }
   104 
   105 public CvarChange_RefillAmmo(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   106 {
   107 	if (GetConVarInt(cvar) && !g_AmmoHooks && g_ActiveWepOffs > 0)
   108 	{
   109 		HookEvent("weapon_reload", Event_CheckDepleted);
   110 		HookEvent("weapon_fire_on_empty", Event_CheckDepleted);
   111 	} else if (!GetConVarInt(cvar) && g_AmmoHooks) {
   112 		UnhookEvent("weapon_reload", Event_CheckDepleted);
   113 		UnhookEvent("weapon_fire_on_empty", Event_CheckDepleted);
   114 	}
   115 }
   116 
   117 public Event_CheckDepleted(Handle:event, const String:name[], bool:dontBroadcast)
   118 {
   119 	new client = GetClientOfUserId(GetEventInt(event, "userid"));
   120 	if (!client || !IsClientInGame(client))
   121 	{
   122 		return;
   123 	}
   124 	
   125 	new entity = GetEntDataEnt2(client, g_ActiveWepOffs);
   126 	if (entity < 1)
   127 	{
   128 		return;
   129 	}
   130 	
   131 	new ammoType = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType");
   132 	
   133 	/* Give something reasonable -- the game will chop it off */
   134 	DM_GiveClientAmmo(client, ammoType, 200, false);
   135 }
   136 
   137 public CvarChange_RestartMapTimer(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   138 {
   139 	RestartMapTimer();
   140 }
   141 
   142 RestartMapTimer()
   143 {
   144 	if (g_ChangeMapTimer != INVALID_HANDLE)
   145 	{
   146 		CloseHandle(g_ChangeMapTimer);
   147 		g_ChangeMapTimer = INVALID_HANDLE;
   148 	}
   149 	
   150 	if (GetConVarInt(cssdm_force_mapchanges))
   151 	{
   152 		/* Find how much time is left in the map */
   153 		new Float:seconds = (GetConVarInt(mp_timelimit) * 60.0) - GetGameTime();
   154 		g_ChangeMapTimer = CreateTimer(seconds, Timer_ChangeMap);
   155 	}
   156 }
   157 
   158 public Action:Timer_ChangeMap(Handle:timer)
   159 {
   160 	g_ChangeMapTimer = INVALID_HANDLE;
   161 	
   162 	decl String:changefile[64];
   163 	GetConVarString(cssdm_mapchange_file, changefile, sizeof(changefile));
   164 	
   165 	new Handle:file = OpenFile(changefile, "rt");
   166 	if (!file)
   167 	{
   168 		LogError("[CSSDM] Could not open mapchange file \"%s\"", changefile);
   169 		return Plugin_Stop;
   170 	}
   171 	
   172 	decl String:curmap[64];
   173 	GetCurrentMap(curmap, sizeof(curmap));
   174 	
   175 	new String:firstmap[64];
   176 	new String:lastmap[64];
   177 	decl String:buffer[64];
   178 	new bool:matched = false;
   179 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   180 	{
   181 		TrimString(buffer);
   182 		if (buffer[0] == '\0' || (!IsCharAlpha(buffer[0]) && !IsCharNumeric(buffer[0])))
   183 		{
   184 			continue;
   185 		}
   186 		if (!IsMapValid(buffer))
   187 		{
   188 			continue;
   189 		}
   190 		if (firstmap[0] == '\0')
   191 		{
   192 			strcopy(firstmap, sizeof(firstmap), buffer);
   193 		}
   194 		strcopy(lastmap, sizeof(lastmap), buffer);
   195 		if (strcmp(buffer, curmap) == 0)
   196 		{
   197 			matched = true;
   198 		} else if (matched) {
   199 			break;
   200 		}
   201 	}
   202 	CloseHandle(file);
   203 	
   204 	if (!matched || (strcmp(buffer, curmap) == 0))
   205 	{
   206 		strcopy(lastmap, sizeof(lastmap), firstmap);
   207 	}
   208 	
   209 	if (lastmap[0] != '\0')
   210 	{
   211 		ServerCommand("changelevel %s", lastmap);
   212 	}
   213 	
   214 	return Plugin_Stop;
   215 }
   216 
   217 public Action:Timer_Welcome(Handle:timer, any:userid)
   218 {
   219 	new client = GetClientOfUserId(userid);
   220 	
   221 	if (!client || !IsClientInGame(client))
   222 	{
   223 		return Plugin_Stop;
   224 	}
   225 	
   226 	PrintToChat(client, "[CSSDM] Counter-Strike Source: Deathmatch (version %s)", CSSDM_VERSION);
   227 	PrintToChat(client, "[CSSDM] Visit http://www.bailopan.net/cssdm to download.");
   228 	
   229 	return Plugin_Stop;
   230 }
   231 
   232 public OnClientPutInServer(client)
   233 {
   234 	g_DeathTimes[client] = 0.0;
   235 	CreateTimer(10.0, Timer_Welcome, GetClientUserId(client));
   236 	
   237 	g_bHasNoAmmoInClip1[client] = false;
   238 }
   239 
   240 public Action:DM_OnClientDeath(client)
   241 {
   242 	g_DeathTimes[client] = GetGameTime();
   243 	return Plugin_Continue;
   244 }
   245 
   246 public Action:Command_Say(client, args)
   247 {
   248 	if (!DM_IsRunning() || !GetConVarInt(cssdm_respawn_command))
   249 	{
   250 		return Plugin_Continue;
   251 	}
   252 	
   253 	decl String:command[32];
   254 	GetCmdArg(1, command, sizeof(command));
   255 	
   256 	if (strcmp(command, "respawn") == 0)
   257 	{
   258 		if (!IsClientInGame(client))
   259 		{
   260 			PrintToChat(client, "[CSSDM] %t", "NoRespawn NotInGame");
   261 			return Plugin_Handled;
   262 		}
   263 		
   264 		if (DM_IsClientAlive(client))
   265 		{
   266 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Alive");
   267 			return Plugin_Handled;
   268 		}
   269 		
   270 		new team = GetClientTeam(client);
   271 		if (team != CSSDM_TEAM_T && team != CSSDM_TEAM_CT)
   272 		{
   273 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Team");
   274 			return Plugin_Handled;
   275 		}
   276 		
   277 		new Float:elapsed = GetGameTime() - g_DeathTimes[client];
   278 		new Float:wait_time = DM_GetSpawnWaitTime();
   279 		if (elapsed < wait_time)
   280 		{
   281 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Wait", wait_time - elapsed);
   282 			return Plugin_Handled;
   283 		}
   284 		
   285 		/* We passed the tests... respawn! */
   286 		DM_RespawnClient(client);
   287 		
   288 		return Plugin_Handled;
   289 	}
   290 	
   291 	return Plugin_Continue;
   292 }
   293 
   294 public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
   295 {
   296     if(!g_AmmoHooks)
   297         return Plugin_Continue;
   298     
   299     new entity = GetEntDataEnt2(client, g_ActiveWepOffs);
   300     if (entity < 1)
   301     {
   302         g_bHasNoAmmoInClip1[client] = false;
   303         return Plugin_Continue;
   304     }
   305     
   306     // He's out of ammo -> reloading
   307     new iClip1 = GetEntProp(entity, Prop_Send, "m_iClip1");
   308     if(iClip1 == 0)
   309     {
   310         g_bHasNoAmmoInClip1[client] = true;
   311     }
   312     // He got ammo in clip1 again but hadn't before -> reloaded.
   313     else if(g_bHasNoAmmoInClip1[client])
   314     {
   315         g_bHasNoAmmoInClip1[client] = false;
   316         
   317         new ammoType = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType");
   318         
   319         if(ammoType <= 0)
   320         {
   321             return Plugin_Continue;
   322         }
   323         
   324         // Give some ammo.
   325         DM_GiveClientAmmo(client, ammoType, 200, false);
   326     }
   327     
   328     return Plugin_Continue;
   329 }