scripting/dm_spawn_protection.sp
author Dr!fter
Sat Jun 30 00:38:22 2012 -0400 (2012-06-30)
changeset 81 d46a5ba80277
parent 18 c7ab45011dfc
permissions -rw-r--r--
Build release
     1 /**
     2  * dm_spawn_protection.sp
     3  * Adds spawn protection to CS:S DM.
     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 #define K_RENDER_NORMAL			0
    30 #define K_RENDER_TRANS_COLOR	1
    31 
    32 /* Plugin stuff */
    33 new Handle:g_ProtVar;
    34 new Handle:g_TColorVar;
    35 new Handle:g_CTColorVar;
    36 new Handle:g_ProtTimeVar;
    37 new g_NormColor[4] = {255, 255, 255, 255};
    38 new g_TColor[4] = {255, 0, 0, 128};
    39 new g_CTColor[4] = {0, 0, 255, 128};
    40 new g_HealthOffset;
    41 new g_RenderModeOffset;
    42 new g_RenderClrOffset;
    43 
    44 /* Player stuff */
    45 new Handle:g_PlayerTimers[MAXPLAYERS+1];
    46 new g_PlayerHealth[MAXPLAYERS+1];
    47 
    48 /** PUBLIC INFO */
    49 public Plugin:myinfo = 
    50 {
    51 	name = "CS:S DM Spawn Protection",
    52 	author = "AlliedModders LLC",
    53 	description = "Spawn protection for CS:S DM",
    54 	version = CSSDM_VERSION,
    55 	url = "http://www.bailopan.net/cssdm/"
    56 };
    57 
    58 public OnPluginStart()
    59 {
    60 	g_ProtVar = CreateConVar("cssdm_spawn_protection", "1", "Sets whether spawn protection is enabled");
    61 	g_CTColorVar = CreateConVar("cssdm_prot_ctcolor", "0 0 255 128", "Sets the spawn color of CTs");
    62 	g_TColorVar = CreateConVar("cssdm_prot_tcolor", "255 0 0 128", "Sets the spawn color of Ts");
    63 	g_ProtTimeVar = CreateConVar("cssdm_prot_time", "2", "Sets time in seconds players are protected for");
    64 	
    65 	HookConVarChange(g_CTColorVar, OnColorChange);
    66 	HookConVarChange(g_TColorVar, OnColorChange);
    67 	
    68 	g_HealthOffset = FindSendPropOffs("CCSPlayer", "m_iHealth");
    69 	g_RenderModeOffset = FindSendPropOffs("CCSPlayer", "m_nRenderMode");
    70 	g_RenderClrOffset = FindSendPropOffs("CCSPlayer", "m_clrRender");
    71 }
    72 
    73 public OnColorChange(Handle:convar, const String:oldValue[], const String:newValue[])
    74 {
    75 	decl String:colors[4][12];
    76 	decl clr[4];
    77 	new num_strings = ExplodeString(newValue, " ", colors, 4, 12);
    78 	
    79 	for (new i=0; i<num_strings; i++)
    80 	{
    81 		clr[i] = StringToInt(colors[i]);
    82 	}
    83 	
    84 	if (convar == g_CTColorVar)
    85 	{
    86 		g_CTColor = clr;
    87 	} else if (convar == g_TColorVar) {
    88 		g_TColor = clr;
    89 	}
    90 }
    91 
    92 public OnClientDisconnect(client)
    93 {
    94 	if (g_PlayerTimers[client] != INVALID_HANDLE)
    95 	{
    96 		CloseHandle(g_PlayerTimers[client]);
    97 		g_PlayerTimers[client] = INVALID_HANDLE;
    98 	}
    99 }
   100 
   101 public Action:DM_OnClientDeath(client)
   102 {
   103 	if (g_PlayerTimers[client] != INVALID_HANDLE)
   104 	{
   105 		CloseHandle(g_PlayerTimers[client]);
   106 		g_PlayerTimers[client] = INVALID_HANDLE;
   107 	}
   108 	
   109 	return Plugin_Continue;
   110 }
   111 
   112 public Action:StopProtection(Handle:timer, any:client)
   113 {
   114 	g_PlayerTimers[client] = INVALID_HANDLE;
   115 	
   116 	SetEntData(client, g_HealthOffset, g_PlayerHealth[client]);
   117 	UTIL_Render(client, g_NormColor);
   118 	
   119 	return Plugin_Stop;
   120 }
   121 
   122 public DM_OnClientPostSpawned(client)
   123 {
   124 	if (!GetConVarInt(g_ProtVar) 
   125 		|| !IsClientInGame(client)
   126 		|| g_PlayerTimers[client] != INVALID_HANDLE)
   127 	{
   128 		return;
   129 	}
   130 	
   131 	g_PlayerTimers[client] = CreateTimer(GetConVarFloat(g_ProtTimeVar), StopProtection, client);
   132 	
   133 	/* Start protection */
   134 	g_PlayerHealth[client] = GetEntData(client, g_HealthOffset);
   135 	SetEntData(client, g_HealthOffset, 1012);	/* This overflows to show "500" */
   136 	
   137 	new team = GetClientTeam(client);
   138 	if (team == CSSDM_TEAM_T)
   139 	{
   140 		UTIL_Render(client, g_TColor);
   141 	} else if (team == CSSDM_TEAM_CT) {
   142 		UTIL_Render(client, g_CTColor);
   143 	}
   144 }
   145 
   146 UTIL_Render(client, const color[4])
   147 {
   148 	new mode = (color[3] == 255) ? K_RENDER_NORMAL : K_RENDER_TRANS_COLOR;
   149 	
   150 	SetEntData(client, g_RenderModeOffset, mode, 1);
   151 	SetEntDataArray(client, g_RenderClrOffset, color, 4, 1);
   152 	ChangeEdictState(client);
   153 }