My plugin needs to check a survivor's primary weapon, if they have one, for any upgrades (laser, incendiary ammo or explosive ammo) and then decide if an upgrade should be given or not.
Peace-Maker posted some code that I modified to find the current upgrades, if any, and return a value that corresponds to the appropriate cvar values for the same upgrades.
There are
two challenges at this point:
1. The GetUpgradeType function successfully determines the client's primary weapon upgrades, but it needs to return multiple values and I'm not sure how best to handle it.
2. Because there are five unique non-repeating combinations of possible upgrades (incendiary and explosive ammo are mutually exclusive) the logic to check them for regular and vip players gets a little complicated.
The code below works for one upgrade, but not more. I understand why but am not sure where to go from here. Seems like an array is needed but my array experience is limited:
Note: Survivors are handled individually based on player_spawn event, so this codes runs for one client at a time.
PHP Code:
#define L4D2_WEPUPGFLAG_NONE (0 << 0)
#define L4D2_WEPUPGFLAG_INCENDIARY (1 << 0)
#define L4D2_WEPUPGFLAG_EXPLOSIVE (1 << 1)
#define L4D2_WEPUPGFLAG_LASER (1 << 2)
g_cvUpgradeChoice = CreateConVar("l4d2_sis_upgrade_choice", "0", "Give an Upgrade to the survivors? 0=disable, 1=laser, 2=incendiary ammo, 3=explosive ammo", FCVAR_NOTIFY);
g_cvRandomUpgrade = CreateConVar("l4d2_sis_random_upgrade", "0", "Give a random Upgrade item to the survivors? Overrides previous upgrade settings. 0=disable, 1=enable.", FCVAR_NOTIFY);
g_cvVipUpgradeChoice = CreateConVar("l4d2_sis_vip_upgrade_choice", "0", "Give an Upgrade to the VIPs? Overrides ALL other upgrade settings. 0=disable, 1=laser, 2=incendiary ammo, 3=explosive ammo", FCVAR_NOTIFY);
/* ============= Upgrade Item ============= */
if (g_cvUpgradeChoice.IntValue > 0 || g_cvRandomUpgrade.BoolValue || g_cvVipUpgradeChoice.IntValue > 0) // check that at least one upgrade cvar is > 0
{
int iEntPrimaryWeapon = GetPlayerWeaponSlot(client, view_as<int>(L4D2WeaponSlot_Primary)); // check for a primary weapon
if (iEntPrimaryWeapon == -1 && !IsValidEntity(iEntPrimaryWeapon))
{
return; // client doesn't have a primary weapon, so no upgrade is given.
}
else // client has a primary weapon.
{
int iExistingUpgrade = GetUpgradeType(client); // what is the existing upgrade, if any? Need to return and manage multiple values here!
if (iExistingUpgrade != 0) //client has an upgrade. Should they receive another?
{
if (bIsVip && g_cvVipUpgradeChoice.IntValue > 0 && g_cvVipUpgradeChoice.IntValue != iExistingUpgrade ) //check for vip/admin status and the vip upgrade cvar
{
SelectUpgrade(client, bIsVip);
}
else if (g_cvUpgradeChoice.IntValue > 0 && g_cvUpgradeChoice.IntValue != iExistingUpgrade) // client is not vip, check for base upgrade cvar.
{
SelectUpgrade(client, bIsVip);
}
}
else // client does not have an existing upgrade. Should they receive one?
{
if (bIsVip && g_cvVipUpgradeChoice.IntValue > 0) // check vip/admin status and vip upgrade cvar.
{
SelectUpgrade(client, bIsVip);
}
else if (g_cvUpgradeChoice.IntValue > 0) // client is not vip, check base upgrade cvar.
{
SelectUpgrade(client, bIsVip);
}
}
}
}
stock int GetUpgradeType(int client)
{
int iEntPrimaryWeapon = GetPlayerWeaponSlot(client, view_as<int>(L4D2WeaponSlot_Primary));
if (iEntPrimaryWeapon != -1 && IsValidEdict(iEntPrimaryWeapon) && HasEntProp(iEntPrimaryWeapon, Prop_Send, "m_upgradeBitVec")) // check if client has a valid primary weapon with an upgrade(s)
{
int iUpgradeType = GetEntProp(iEntPrimaryWeapon, Prop_Send, "m_upgradeBitVec"); // get the upgrade type
if (iUpgradeType & L4D2_WEPUPGFLAG_LASER)
{
return 1; // laser upgrade cvar value
}
else if (iUpgradeType & L4D2_WEPUPGFLAG_INCENDIARY)
{
return 2; // indendiary upgrade cvar value
}
else if (iUpgradeType & L4D2_WEPUPGFLAG_EXPLOSIVE)
{
return 3; // explosive upgrade cvar value
}
}
return 0; // no upgrades
}
Appreciate any help,
PG