Hi! First-time post from someone who's read a lot of threads here without an account.
There appears to be a long-standing issue with having multiple of one grenade and nothing else (see CS:S thread
https://forums.alliedmods.net/showthread.php?t=280143 ). Normally not an issue, since you'll have at least a knife, but if you create a game mode with knives disabled (setting mp_[c]t_default_melee to ""), you can get "stuck". Simplest way to demonstrate this: Buy two flashbangs and nothing else, throw one, and you can't throw the other.
Here's my partial solution. See also
https://github.com/Rosuav/TF2BuffBot...ede41bee873a3b (don't mind the "TF2" part in there, this is a CS:GO plugin). MIT licensed.
Code:
public void OnPluginStart()
{
HookEvent("weapon_fire", Event_weapon_fire);
}
//If you throw a grenade and it's the only thing you have, unselect.
public void Event_weapon_fire(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if (GetPlayerWeaponSlot(client, 2) != -1) return; //Normally you'll have a knife, and things are fine.
char weapon[64]; event.GetString("weapon", weapon, sizeof(weapon));
int ammo_offset = 0;
if (!strcmp(weapon, "weapon_hegrenade")) ammo_offset = 14;
else if (!strcmp(weapon, "weapon_flashbang")) ammo_offset = 15;
else if (!strcmp(weapon, "weapon_smokegrenade")) ammo_offset = 16;
else if (!strcmp(weapon, "weapon_molotov") || !strcmp(weapon, "weapon_incgrenade")) ammo_offset = 17;
else if (!strcmp(weapon, "weapon_decoy")) ammo_offset = 18;
else return; //Wasn't a grenade you just threw.
//Okay, you threw a grenade, and we know where to check its ammo.
//Let's see if you have stock of anything else.
if (GetPlayerWeaponSlot(client, 0) != -1) return; //Got a primary? All good.
if (GetPlayerWeaponSlot(client, 1) != -1) return; //Got a pistol? All good.
//Already checked knife above as our fast-abort.
//Checking for a 'nade gives false positives.
if (GetPlayerWeaponSlot(client, 5) != -1) return; //Got the C4? All good.
//Do you have ammo of any other type of grenade?
for (int offset = 14; offset <= 18; ++offset)
if (offset != ammo_offset && GetEntProp(client, Prop_Data, "m_iAmmo", _, offset) > 0)
//You have some other 'nade. Default behaviour is fine.
return;
//You don't have anything else. Unselect the current weapon, allowing you
//to reselect your one and only grenade.
CreateTimer(0.25, deselect_weapon, client, TIMER_FLAG_NO_MAPCHANGE);
}
Action deselect_weapon(Handle timer, any client)
{
ignore(timer);
SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", -1);
//Ideally, I would like to now say "and select slot 4", but that doesn't seem
//to work. It might also be possible to pick by a different slot (eg "slot7"
//for flashbang), but I can't get that to work either.
}
Question: How can I make the plugin automatically reselect the grenade? I've tried EquipPlayerWeapon(client, GetPlayerWeaponSlot(client, 4)) and SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", GetPlayerWeaponSlot(client, 4)) and a variety of other variants. Would be grateful for any suggestions. (Including "you're going about this all wrong, here, do this tiny fix and it'll be solved in core".)