scripting/dm_basics.sp
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 23:43:20 2007 +0000 (2007-06-25)
changeset 12 f3fedcdb9bd4
parent 6 a0574cbe9e53
child 13 3f35cdcf6955
permissions -rw-r--r--
one final revision of per-map configs
     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  *
     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 
    23 #pragma semicolon 1
    24 #include <sourcemod>
    25 #include <cssdm>
    26 
    27 /* Plugin stuff */
    28 new Handle:cssdm_respawn_command;
    29 new Handle:cssdm_force_mapchanges;
    30 new Handle:cssdm_mapchange_file;
    31 new Handle:cssdm_refill_ammo;
    32 new Handle:mp_timelimit;
    33 new Handle:g_ChangeMapTimer = INVALID_HANDLE;
    34 new bool:g_AmmoHooks = false;
    35 new g_ActiveWepOffs = -1;
    36 
    37 /* Player stuff */
    38 new Float:g_DeathTimes[MAXPLAYERS+1];
    39 
    40 /** PUBLIC INFO */
    41 public Plugin:myinfo = 
    42 {
    43 	name = "CS:S DM Basics",
    44 	author = "AlliedModders LLC",
    45 	description = "Basic CS:S DM Commands/Features",
    46 	version = CSSDM_VERSION,
    47 	url = "http://www.bailopan.net/cssdm/"
    48 };
    49 
    50 public OnPluginStart()
    51 {
    52 	LoadTranslations("cssdm.phrases");
    53 	
    54 	RegConsoleCmd("say", Command_Say);
    55 	RegConsoleCmd("say_team", Command_Say);
    56 	
    57 	cssdm_respawn_command = CreateConVar("cssdm_respawn_command", "1", "Sets whether clients can manually respawn");
    58 	cssdm_force_mapchanges = CreateConVar("cssdm_force_mapchanges", "0", "Sets whether CS:S DM should force mapchanges");
    59 	cssdm_mapchange_file = CreateConVar("cssdm_mapchange_file", "mapcycle.txt", "Sets the mapchange file for CS:S DM");
    60 	cssdm_refill_ammo = CreateConVar("cssdm_refill_ammo", "1", "Sets whether CS:S DM automatically refills ammo");
    61 	mp_timelimit = FindConVar("mp_timelimit");
    62 	
    63 	HookConVarChange(cssdm_force_mapchanges, CvarChange_RestartMapTimer);
    64 	HookConVarChange(mp_timelimit, CvarChange_RestartMapTimer);	
    65 	HookConVarChange(cssdm_refill_ammo, CvarChange_RefillAmmo);
    66 	
    67 	g_ActiveWepOffs = FindSendPropOffs("CCSPlayer", "m_hActiveWeapon");
    68 }
    69 
    70 public OnConfigsExecuted()
    71 {
    72 	RestartMapTimer();
    73 	
    74 	g_AmmoHooks = GetConVarInt(cssdm_refill_ammo) ? true : false;
    75 	if (g_AmmoHooks && g_ActiveWepOffs > 0)
    76 	{
    77 		HookEvent("weapon_reload", Event_CheckDepleted);
    78 		HookEvent("weapon_fire_on_empty", Event_CheckDepleted);
    79 	}
    80 }
    81 
    82 public OnMapEnd()
    83 {
    84 	if (g_ChangeMapTimer != INVALID_HANDLE)
    85 	{
    86 		CloseHandle(g_ChangeMapTimer);
    87 		g_ChangeMapTimer = INVALID_HANDLE;
    88 	}
    89 	if (g_AmmoHooks)
    90 	{
    91 		UnhookEvent("weapon_reload", Event_CheckDepleted);
    92 		UnhookEvent("weapon_fire_on_reload", Event_CheckDepleted);
    93 	}
    94 }
    95 
    96 public CvarChange_RefillAmmo(Handle:cvar, const String:oldvalue[], const String:newvalue[])
    97 {
    98 	if (GetConVarInt(cvar) && !g_AmmoHooks && g_ActiveWepOffs > 0)
    99 	{
   100 		HookEvent("weapon_reload", Event_CheckDepleted);
   101 		HookEvent("weapon_fire_on_reload", Event_CheckDepleted);
   102 	} else if (!GetConVarInt(cvar) && g_AmmoHooks) {
   103 		UnhookEvent("weapon_reload", Event_CheckDepleted);
   104 		UnhookEvent("weapon_fire_on_reload", Event_CheckDepleted);
   105 	}
   106 }
   107 
   108 public Event_CheckDepleted(Handle:event, const String:name[], bool:dontBroadcast)
   109 {
   110 	new client = GetClientOfUserId(GetEventInt(event, "userid"));
   111 	if (!client || !IsClientInGame(client))
   112 	{
   113 		return;
   114 	}
   115 	
   116 	new entity = GetEntDataEnt(client, g_ActiveWepOffs);
   117 	if (entity < 1)
   118 	{
   119 		return;
   120 	}
   121 	
   122 	new ammoType = GetEntProp(entity, Prop_Send, "m_iPrimaryAmmoType");
   123 	
   124 	/* Give something reasonable -- the game will chop it off */
   125 	DM_GiveClientAmmo(client, ammoType, 30, false);
   126 }
   127 
   128 public CvarChange_RestartMapTimer(Handle:cvar, const String:oldvalue[], const String:newvalue[])
   129 {
   130 	RestartMapTimer();
   131 }
   132 
   133 RestartMapTimer()
   134 {
   135 	if (g_ChangeMapTimer != INVALID_HANDLE)
   136 	{
   137 		CloseHandle(g_ChangeMapTimer);
   138 		g_ChangeMapTimer = INVALID_HANDLE;
   139 	}
   140 	
   141 	if (GetConVarInt(cssdm_force_mapchanges))
   142 	{
   143 		/* Find how much time is left in the map */
   144 		new Float:seconds = (GetConVarInt(mp_timelimit) * 60.0) - GetGameTime();
   145 		g_ChangeMapTimer = CreateTimer(seconds, Timer_ChangeMap);
   146 	}
   147 }
   148 
   149 public Action:Timer_ChangeMap(Handle:timer)
   150 {
   151 	g_ChangeMapTimer = INVALID_HANDLE;
   152 	
   153 	decl String:changefile[64];
   154 	GetConVarString(cssdm_mapchange_file, changefile, sizeof(changefile));
   155 	
   156 	new Handle:file = OpenFile(changefile, "rt");
   157 	if (!file)
   158 	{
   159 		LogError("[CSSDM] Could not open mapchange file \"%s\"", changefile);
   160 		return Plugin_Stop;
   161 	}
   162 	
   163 	decl String:curmap[64];
   164 	GetCurrentMap(curmap, sizeof(curmap));
   165 	
   166 	new String:firstmap[64];
   167 	new String:lastmap[64];
   168 	decl String:buffer[64];
   169 	new bool:matched = false;
   170 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   171 	{
   172 		TrimString(buffer);
   173 		if (buffer[0] == '\0' || (!IsCharAlpha(buffer[0]) && !IsCharNumeric(buffer[0])))
   174 		{
   175 			continue;
   176 		}
   177 		if (!IsMapValid(buffer))
   178 		{
   179 			continue;
   180 		}
   181 		if (firstmap[0] == '\0')
   182 		{
   183 			strcopy(firstmap, sizeof(firstmap), buffer);
   184 		}
   185 		strcopy(lastmap, sizeof(lastmap), buffer);
   186 		if (strcmp(buffer, curmap) == 0)
   187 		{
   188 			matched = true;
   189 		} else if (matched) {
   190 			break;
   191 		}
   192 	}
   193 	CloseHandle(file);
   194 	
   195 	if (!matched || (strcmp(buffer, curmap) == 0))
   196 	{
   197 		strcopy(lastmap, sizeof(lastmap), firstmap);
   198 	}
   199 	
   200 	if (lastmap[0] != '\0')
   201 	{
   202 		ServerCommand("changelevel %s", lastmap);
   203 	}
   204 	
   205 	return Plugin_Stop;
   206 }
   207 
   208 public OnClientPutInServer(client)
   209 {
   210 	g_DeathTimes[client] = 0.0;
   211 }
   212 
   213 public Action:DM_OnClientDeath(client)
   214 {
   215 	g_DeathTimes[client] = GetGameTime();
   216 	return Plugin_Continue;
   217 }
   218 
   219 public Action:Command_Say(client, args)
   220 {
   221 	if (!DM_IsRunning() || !GetConVarInt(cssdm_respawn_command))
   222 	{
   223 		return Plugin_Continue;
   224 	}
   225 	
   226 	decl String:command[32];
   227 	GetCmdArg(1, command, sizeof(command));
   228 	
   229 	if (strcmp(command, "respawn") == 0)
   230 	{
   231 		if (!IsClientInGame(client))
   232 		{
   233 			PrintToChat(client, "[CSSDM] %t", "NoRespawn NotInGame");
   234 			return Plugin_Handled;
   235 		}
   236 		
   237 		if (DM_IsClientAlive(client))
   238 		{
   239 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Alive");
   240 			return Plugin_Handled;
   241 		}
   242 		
   243 		new team = GetClientTeam(client);
   244 		if (team != CSSDM_TEAM_T && team != CSSDM_TEAM_CT)
   245 		{
   246 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Team");
   247 			return Plugin_Handled;
   248 		}
   249 		
   250 		new Float:elapsed = GetGameTime() - g_DeathTimes[client];
   251 		new Float:wait_time = DM_GetSpawnWaitTime();
   252 		if (elapsed < wait_time)
   253 		{
   254 			PrintToChat(client, "[CSSDM] %t", "NoRespawn Wait", wait_time - elapsed);
   255 			return Plugin_Handled;
   256 		}
   257 		
   258 		/* We passed the tests... respawn! */
   259 		DM_RespawnClient(client);
   260 		
   261 		return Plugin_Handled;
   262 	}
   263 	
   264 	return Plugin_Continue;
   265 }
   266