plugins/include/tf2_stocks.inc
author Scott Ehlert <ds@alliedmods.net>
Thu Apr 17 09:28:46 2008 +0000 (2008-04-17)
changeset 2013 5cf82fea0177
parent 2005 92033efef85f
child 2021 02c435635bd7
permissions -rw-r--r--
Gah, finalized the revised ILibrarySys::GetFileExtension() code
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _tf2_stocks_included
    34  #endinput
    35 #endif
    36 #define _tf2_stocks_included
    37 
    38 #include <tf2>
    39 #include <sdktools>
    40 
    41 enum TFResourceType
    42 {
    43 	TFResource_Ping,
    44 	TFResource_Score,
    45 	TFResource_Deaths,
    46 	TFResource_TotalScore, 
    47 	TFResource_Captures,
    48 	TFResource_Defenses,
    49 	TFResource_Dominations,
    50 	TFResource_Revenge,
    51 	TFResource_BuildingsDestroyed, 
    52 	TFResource_Headshots,
    53 	TFResource_Backstabs,
    54 	TFResource_HealPoints,
    55 	TFResource_Invulns,
    56 	TFResource_Teleports,
    57 	TFResource_ResupplyPoints,
    58 	TFResource_KillAssists,
    59 	TFResource_MaxHealth,
    60 	TFResource_PlayerClass
    61 };
    62 
    63 static const String:TFResourceNames[TFResourceType][] =
    64 {
    65 	"m_iPing",
    66 	"m_iScore",
    67 	"m_iDeaths",
    68 	"m_iTotalScore",
    69 	"m_iCaptures",
    70 	"m_iDefenses",
    71 	"m_iDominations",
    72 	"m_iRevenge",
    73 	"m_iBuildingsDestroyed",
    74 	"m_iHeadshots",
    75 	"m_iBackstabs",
    76 	"m_iHealPoints",
    77 	"m_iInvulns",
    78 	"m_iTeleports",
    79 	"m_iResupplyPoints",
    80 	"m_iKillAssists",
    81 	"m_iMaxHealth",
    82 	"m_iPlayerClass"
    83 };
    84 
    85 /**
    86  * Get's a Clients current class.
    87  *
    88  * @param client		Player's index.
    89  * @param class			TFClassType to change to.
    90  * @noreturn
    91  * @error				Invalid client index.
    92  */
    93 stock TFClassType:TF2_GetPlayerClass(client)
    94 {
    95 	return TFClassType:GetEntProp(client, Prop_Send, "m_iClass");
    96 }
    97 
    98 /**
    99  * Set's a Clients class.
   100  *
   101  * Note: If setting player class in a player spawn hook weapons should be set to false.
   102  *
   103  * @param client		Player's index.
   104  * @param class			TFClassType class symbol.
   105  * @param weapons		If true, changes the players weapon set to that of the new class.
   106  * @param persistant	If true changes the players desired class so the change stays after death.
   107  * @noreturn
   108  * @error				Invalid client index.
   109  */
   110 stock TF2_SetPlayerClass(client, TFClassType:class, bool:weapons=true, bool:persistant=true)
   111 {
   112 	SetEntProp(client, Prop_Send, "m_iClass", _:class);
   113 	TF2_SetPlayerResourceData(client, TFResource_PlayerClass, class);
   114 	
   115 	if (persistant)
   116 	{
   117 		SetEntProp(client, Prop_Send, "m_iDesiredPlayerClass", _:class);
   118 	}
   119 	
   120 	if (weapons && IsPlayerAlive(client))
   121 	{
   122 		TF2_RemoveAllWeapons(client);
   123 		TF2_EquipPlayerClassWeapons(client, class);
   124 	}
   125 }
   126 
   127 /**
   128  * Retrieves client data from the resource entity
   129  *
   130  * @param client		Player's index.
   131  * @param type			ResourceType constant
   132  * @return				Value or -1 on failure.
   133  * @error				Invalid client index, client not in game or failed to find resource entity.
   134  */
   135 stock TF2_GetPlayerResourceData(client, TFResourceType:type)
   136 {
   137 	if (!IsClientConnected(client))
   138 	{
   139 		return -1;	
   140 	}
   141 	
   142 	static offset;
   143 	static set = false;
   144 	
   145 	if (!set)
   146 	{
   147 		offset = FindSendPropInfo("CTFPlayerResource", TFResourceNames[type]);
   148 		set = true;
   149 	}
   150 		
   151 	if (offset < 1)
   152 	{
   153 		return -1;	
   154 	}
   155 				
   156 	new entity = TF2_GetResourceEntity();
   157 	
   158 	if (entity == -1)
   159 	{
   160 		return -1;
   161 	}
   162 	
   163 	return GetEntData(entity, offset + (client*4));
   164 }
   165 
   166 /**
   167  * Sets client data in the resource entity
   168  *
   169  * @param client		Player's index.
   170  * @param type			ResourceType constant
   171  * @param value			Value to set.
   172  * @return				Value or -1 on failure.
   173  * @error				Invalid client index, client not in game or failed to find resource entity.
   174  */
   175 stock bool:TF2_SetPlayerResourceData(client, TFResourceType:type, any:value)
   176 {
   177 	if (!IsClientConnected(client))
   178 	{
   179 		return false;	
   180 	}
   181 	
   182 	static offset;
   183 	static set = false;
   184 	
   185 	if (!set)
   186 	{
   187 		offset = FindSendPropInfo("CTFPlayerResource", TFResourceNames[type]);
   188 		set = true;
   189 	}
   190 						
   191 	if (offset < 1)
   192 	{
   193 		return false;	
   194 	}
   195 				
   196 	new entity = TF2_GetResourceEntity();
   197 	
   198 	if (entity == -1)
   199 	{
   200 		return false;	
   201 	}
   202 	
   203 	SetEntData(entity, offset + (client*4), value);
   204 	
   205 	return true;	
   206 }
   207 
   208 /**
   209  * Removes all weapons from a client's weapon slot
   210  *
   211  * @param client		Player's index.
   212  * @param slot			Slot index (0-5)
   213  * @noreturn
   214  * @error				Invalid client, invalid slot or lack of mod support
   215  */
   216 stock TF2_RemoveWeaponSlot(client, slot)
   217 {
   218 	new weaponIndex;
   219 	while ((weaponIndex = GetPlayerWeaponSlot(client, slot)) != -1)
   220 	{
   221 		RemovePlayerItem(client, weaponIndex);
   222 		RemoveEdict(weaponIndex);
   223 	}
   224 }
   225 
   226 /**
   227  * Removes all weapons from a client
   228  *
   229  * @param client		Player's index.
   230  * @noreturn
   231  */
   232 stock TF2_RemoveAllWeapons(client)
   233 {
   234 	for (new i = 0; i <= 5; i++)
   235 	{
   236 		TF2_RemoveWeaponSlot(client, i);
   237 	}
   238 }
   239 
   240 /**
   241  * Gives a named weapon to a client
   242  *
   243  * @param client		Player's index.
   244  * @param weapon		Weapon name
   245  * @return				False if weapon could not be created, true on success
   246  * @error				Invalid client index or lack of mod support
   247  */
   248 stock bool:TF2_GivePlayerWeapon(client, const String:weapon[])
   249 {
   250 	new weaponIndex = GivePlayerItem(client, weapon);
   251 	if (weaponIndex == -1)
   252 	{
   253 		return false;	
   254 	}
   255 	
   256 	EquipPlayerWeapon(client, weaponIndex);
   257 	
   258 	return true;
   259 }
   260 
   261 /**
   262  * Equips a client with a class's weapons. This does not remove existing weapons.
   263  *
   264  * Note: 	Some class specific items such tf_weapon_pda_engineer_build are only given 
   265  *			if the client is the correct class.
   266  *
   267  * @param client		Player's index.
   268  * @param class			TFClasssType class symbol.
   269  * @noreturn
   270  */
   271 stock TF2_EquipPlayerClassWeapons(client, TFClassType:class)
   272 {
   273 	switch(class)
   274 	{
   275 		case TFClass_Scout:
   276 		{
   277 			TF2_GivePlayerWeapon(client, "tf_weapon_scattergun");
   278 			TF2_GivePlayerWeapon(client, "tf_weapon_pistol_scout");
   279 			TF2_GivePlayerWeapon(client, "tf_weapon_bat");
   280 		}
   281 		case TFClass_Sniper:
   282 		{
   283 			TF2_GivePlayerWeapon(client, "tf_weapon_sniperrifle");
   284 			TF2_GivePlayerWeapon(client, "tf_weapon_smg");
   285 			TF2_GivePlayerWeapon(client, "tf_weapon_club");
   286 		}
   287 		case TFClass_Soldier:
   288 		{
   289 			TF2_GivePlayerWeapon(client, "tf_weapon_rocketlauncher");
   290 			TF2_GivePlayerWeapon(client, "tf_weapon_shotgun_soldier");
   291 			TF2_GivePlayerWeapon(client, "tf_weapon_shovel");
   292 		}
   293 		case TFClass_DemoMan:
   294 		{
   295 			TF2_GivePlayerWeapon(client, "tf_weapon_pipebomblauncher");
   296 			TF2_GivePlayerWeapon(client, "tf_weapon_grenadelauncher");
   297 			TF2_GivePlayerWeapon(client, "tf_weapon_bottle");
   298 		}
   299 		case TFClass_Medic:
   300 		{
   301 			TF2_GivePlayerWeapon(client, "tf_weapon_syringegun_medic");
   302 			TF2_GivePlayerWeapon(client, "tf_weapon_medigun");
   303 			TF2_GivePlayerWeapon(client, "tf_weapon_bonesaw");
   304 		}
   305 		case TFClass_Heavy:
   306 		{
   307 			TF2_GivePlayerWeapon(client, "tf_weapon_minigun");
   308 			TF2_GivePlayerWeapon(client, "tf_weapon_shotgun_hwg");
   309 			TF2_GivePlayerWeapon(client, "tf_weapon_fists");
   310 		}
   311 		case TFClass_Pyro:
   312 		{
   313 			TF2_GivePlayerWeapon(client, "tf_weapon_flamethrower");
   314 			TF2_GivePlayerWeapon(client, "tf_weapon_shotgun_pyro");
   315 			TF2_GivePlayerWeapon(client, "tf_weapon_fireaxe");
   316 		}
   317 		case TFClass_Spy:
   318 		{
   319 			TF2_GivePlayerWeapon(client, "tf_weapon_revolver");
   320 			TF2_GivePlayerWeapon(client, "tf_weapon_knife");
   321 
   322 			if (TF2_GetPlayerClass(client) != TFClass_Spy)
   323 				return;
   324 
   325 			TF2_GivePlayerWeapon(client, "tf_weapon_pda_spy");
   326 		}
   327 		case TFClass_Engineer:
   328 		{
   329 			TF2_GivePlayerWeapon(client, "tf_weapon_shotgun_primary");
   330 			TF2_GivePlayerWeapon(client, "tf_weapon_pistol");
   331 			TF2_GivePlayerWeapon(client, "tf_weapon_wrench");
   332 
   333 			if (TF2_GetPlayerClass(client) != TFClass_Engineer)
   334 				return;
   335 
   336 			TF2_GivePlayerWeapon(client, "tf_weapon_pda_engineer_build");
   337 			TF2_GivePlayerWeapon(client, "tf_weapon_pda_engineer_destroy");
   338 		}
   339 	}
   340 }