PDA

View Full Version : [L4D2] Keep Upgraded Ammo


RavenDan29
12-25-2011, 03:33
How can i alter this code for [L4D2] keep laser sights to include keeping incendary and explosive ammo

#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#define PLUGIN_VERSION "1.1"
#define L4D2_WEPUPGFLAG_LASER (1 << 2)
public Plugin:myinfo =
{
name = "L4D2 Keep Lasers",
author = "dcx2 (assist: Mr. Zero)",
description = "Keep existing laser sights when picking up a new gun",
version = PLUGIN_VERSION,
url = "www.AlliedMods.net (http://www.AlliedMods.net)"
};
new Handle:g_cvarEnable;
new Handle:g_cvarNoHint;
new g_droppedWeapons[MAXPLAYERS+1];
public OnPluginStart()
{
g_cvarEnable = CreateConVar("sm_keeplasers_enable", "1", "Enables this plugin.", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FC VAR_NOTIFY);
g_cvarNoHint = CreateConVar("sm_keeplasers_nohint", "1", "Disable laser sight upgrade hint.", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FC VAR_NOTIFY);
CreateConVar("sm_keeplasers_version", PLUGIN_VERSION, "L4D2 Keep Lasers", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FC VAR_NOTIFY);

for (new i = 1; i <= MAXPLAYERS; i++)
{
g_droppedWeapons[i] = -1;
}
AutoExecConfig(true, "L4D2_KeepLasers");

HookEvent("weapon_drop", Event_WeaponDrop);
}
public Action:Event_WeaponDrop(Handle:event, const String:name[], bool:dontBroadcast)
{
if (!GetConVarBool(g_cvarEnable)) return Plugin_Handled; // Not enabled? Do nothing
new targetClient = GetClientOfUserId(GetEventInt(event, "userid"));

// client 0, or not connected, or not in game, or not survivor? Done
if (targetClient == 0 || !IsClientConnected(targetClient) || !IsClientInGame(targetClient) || GetClientTeam(targetClient)!= 2) return Plugin_Handled;

new droppedEnt = GetEventInt(event, "propid"); // Grab the dropped weapon

// WeaponDrop is called for all kinds of stuff (pills, melee, etc)
// Can this dropped weapon have a laser sight?
// If it can have a laser sight, does it have one?
if (L4D2_CanWeaponUpgrades(droppedEnt))
{
new droppedUpgrades = L4D2_GetWeaponUpgrades(droppedEnt);
if (droppedUpgrades & L4D2_WEPUPGFLAG_LASER)
{
// Remember that this client has had a laser sight, in case of mode 1
g_droppedWeapons[targetClient] = droppedEnt;

// If the client dies, don't remove laser sights or the gun could disappear
if (GetClientHealth(targetClient) > 0)
{
L4D2_SetWeaponUpgrades(droppedEnt, droppedUpgrades & ~L4D2_WEPUPGFLAG_LASER);
CreateTimer(0.2, LaserDelay, targetClient); //add a delay
}
}
}
return Plugin_Handled;
}
// We can't give the upgrade immediately because you don't quite have the new weapon yet
// So we use a small delay to make sure that UpgradeLaserSight is called after you have your new weapon
public Action:LaserDelay(Handle:timer, any:targetClient)
{
new bool:oldGun = false;
new CurrentWeapon = -1;
if (!IsClientInGame(targetClient) || !IsPlayerAlive(targetClient))
{
oldGun = true; // Not in game or alive? Put laser sights back on the old gun
}

if (!oldGun)
{
// If not the old gun, check current weapon
CurrentWeapon = GetPlayerWeaponSlot(targetClient, 0);
if (CurrentWeapon < 0)
{
oldGun = true; // No gun? Put laser sights back on the old one
}
}

if (oldGun)
{
// If old gun, replace CurrentWeapon pointer with dropped weapon pointer
CurrentWeapon = g_droppedWeapons[targetClient];
}

// TODO: how to spawn a gun with laser sights? can we just give and force drop?
if (!IsValidEntity(CurrentWeapon)) return;

new CurrentUpgrades = L4D2_GetWeaponUpgrades(CurrentWeapon);
if (!oldGun && (CurrentUpgrades & L4D2_WEPUPGFLAG_LASER)) // Already have laser sight on new gun? Give it back to the old gun
{
oldGun = true;
CurrentWeapon = g_droppedWeapons[targetClient];
CurrentUpgrades = L4D2_GetWeaponUpgrades(CurrentWeapon);
}
// Give laser sight
if (!oldGun && GetConVarInt(g_cvarNoHint) < 1)
{
// No hint *disabled* and not old gun? Then show hint with upgrade_add, unless it's a gun on the ground
CheatCommand(targetClient, "upgrade_add", "LASER_SIGHT");
}
else
{
// No hint *enabled*, or old gun, set the upgrade quietly
L4D2_SetWeaponUpgrades(CurrentWeapon, CurrentUpgrades | L4D2_WEPUPGFLAG_LASER);
}
}
// Made this myself, based on the two codes below
stock bool:L4D2_CanWeaponUpgrades(weapon)
{
return (GetEntSendPropOffs(weapon, "m_upgradeBitVec") > -1);
}
// Thanks Mr. Zero!
stock L4D2_GetWeaponUpgrades(weapon)
{
return GetEntProp(weapon, Prop_Send, "m_upgradeBitVec");
}
// Thanks Mr. Zero!
stock L4D2_SetWeaponUpgrades(weapon, upgrades)
{
if (weapon < 0) return;
SetEntProp(weapon, Prop_Send, "m_upgradeBitVec", upgrades);
}
// Not sure where I grabbed this, it's pretty common
stock CheatCommand(client, const String:command[], const String:arguments[])
{
new flags = GetCommandFlags(command);
SetCommandFlags(command, flags & ~FCVAR_CHEAT);
FakeClientCommand(client, "%s %s", command, arguments);
SetCommandFlags(command, flags);
}