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