Raised This Month: $51 Target: $400
 12% 

RemovePlayerItem for specific grenade?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 08-27-2009 , 22:39   RemovePlayerItem for specific grenade?
Reply With Quote #1

Sorry, still learning the language . Is it possible to remove a specific weapon from a player, rather then removing by the weapon's index. For example, I'm writing an anti-nade-spam plugin and I want to remove a player's smoke grenade if they use more then one. However, because I have to use RemovePlayerItem and CS_SLOT_GRENADE, it removes my HEGrenades if I have any first.

Thanks,
Panda
thetwistedpanda is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-27-2009 , 22:50   Re: RemovePlayerItem for specific grenade?
Reply With Quote #2

From Zombie:Reloaded

Code:
/**
 * Grenade slots.
 */
enum WeaponAmmoGrenadeType
{
    GrenadeType_Invalid         = -1,   /** Invalid grenade slot. */
    GrenadeType_HEGrenade       = 11,   /** HEGrenade slot */
    GrenadeType_Flashbang       = 12,   /** Flashbang slot. */
    GrenadeType_Smokegrenade    = 13,   /** Smokegrenade slot. */
}

/**
 * Set the count of any grenade-type a client has.
 * 
 * @param client    The client index.
 * @param slot      The type of
 * @param value     The amount of ammo to set to.
 * @param add       (Optional) If true, the value is added to the grenades' current ammo count. 
 */
stock WeaponAmmoSetGrenadeCount(client, WeaponAmmoGrenadeType:type, value, bool:add)
{
    // Initialize variable (value is 0)
    new ammovalue;
    
    // If we are adding, then update variable with current ammo value.
    if (add)
    {
        ammovalue = WeaponAmmoGetGrenadeCount(client, type);
    }
    
    SetEntData(client, g_iToolsAmmo + (_:type * 4), ammovalue + value, _, true);
}

/**
 * Get the count of any grenade-type a client has.
 * 
 * @param client    The client index.
 * @param slot      The type of
 */
stock WeaponAmmoGetGrenadeCount(client, WeaponAmmoGrenadeType:type)
{
    return GetEntData(client, g_iToolsAmmo + (_:type * 4));
}
Just set the count to 0 for smoke grenades.


In the context of the plugin using these functions, the enum has other uses.
Here you might be better off putting those 3 offsets into defines and using that instead if the enum.
__________________
Greyscale is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 08-27-2009 , 23:01   Re: RemovePlayerItem for specific grenade?
Reply With Quote #3

Whoops. My previous post here sounded completely ungrateful haha. Thank you for the snippet Gray, but I am still learning the language so I need a wee bit more assistance. After converting over to #define, the compiler is complaining about g_iToolsAmmo, and I see no where where the variable is defined. Also, after getting rid of WeaponAmmoGrenadeType, is there anything I need to change in the GetEntData / SetEntData calls?

Last edited by thetwistedpanda; 08-27-2009 at 23:09.
thetwistedpanda is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-28-2009 , 00:04   Re: RemovePlayerItem for specific grenade?
Reply With Quote #4

Ah, I see.

I actually forgot to include g_iToolsAmmo in the example:

Declare it somewhere at the top of your source code (refered to as the global scope) like so:

Code:
new g_iToolsAmmo;
Then using OnPluginStart:

Code:
public OnPluginStart()
{
    // If offset "m_iAmmo" can't be found, then stop the plugin.
    g_iToolsAmmo = FindSendPropInfo("CBasePlayer", "m_iAmmo");
    if (g_iToolsAmmo == -1)
    {
        SetFailState("Offset \"CBasePlayer::m_iAmmo\" was not found.");
    }
}
That fixes that problem.

For converting to define, try:

Code:
#define SLOT_HEGRENADE      11
#define SLOT_FLASHBANG      12
#define SLOT_SMOKEGRENADE   13

And you need to modify the get/set functions by removing "WeaponAmmoGrenadeType:" from the definitions.

Last step is to remove the "_:" where you see it prefixed in front of variable 'type'

_: basically "casts" the variable into the number assigned to it in the enum. Casting is a term that means you're changing the type of the data. For example casting a float (3.789) to an int (3).

I'm not sure how seasoned you are in other languages, so if this is old news to you then ignore it. And if you're just jumping into the word of coding then take a mental note, but don't worry about it too much if it doesn't click immediately.

You can post back here what you got and I'll help with any more questions you got.
__________________

Last edited by Greyscale; 08-28-2009 at 00:07.
Greyscale is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 08-28-2009 , 00:16   Re: RemovePlayerItem for specific grenade?
Reply With Quote #5

Thanks, that makes a bit more sense. Here's what I have so far. I just tested the plugin, it doesn't seem to set the player's grenade value to 0.

PHP Code:
#pragma semicolon 1

#include <sourcemod>
#include <cstrike>
#include <sdktools>
#include <colors>

#define SLOT_HEGRENADE 11
#define SLOT_FBGRENADE 12
#define SLOT_SMGRENADE 13

public Plugin:myinfo 
{
  
name "{HoM} Anti Nade Spam",
  
author "Panda",
  
description "Nade spam protection for {HoM} servers.",
  
version "1.0",
  
url "www.heirsofmortality.com"
}

enum GrenadeData
{
  
numHE,
  
numSG,
  
numFB
};

new 
p_Clients[MAXPLAYERS+1][GrenadeData];
new 
g_iToolsAmmo;

new 
Handle:p_Enabled;
new 
Handle:p_TotalGrenade;
new 
Handle:p_TotalSmoke;
new 
Handle:p_TotalFlash;

public 
OnPluginStart()
{
  
g_iToolsAmmo FindSendPropInfo("CBasePlayer""m_iAmmo");
  if (
g_iToolsAmmo == -1)
  {
      
SetFailState("Offset \"CBasePlayer::m_iAmmo\" was not found.");
  }
    
  
p_Enabled    CreateConVar("sm_nadespam_enable""1""Determines whether or not Anti-Nade-Spam protection is active.");
  
p_TotalGrenade  CreateConVar("sm_nadespam_numHE""1""The number of High-Explosive Grenades players are allowed to use in total.");
  
p_TotalSmoke    CreateConVar("sm_nadespam_numSG""1""The number of Flash Bangs players are allowed to use.");
  
p_TotalFlash    CreateConVar("sm_nadespam_numFB""0""The number of Smoke Grenades players are allowed to use.");
  
AutoExecConfig(true);
  
  
HookEvent("player_spawn"OnPlayerSpawn);
  
HookEvent("item_pickup"OnItemPickup);
}

public 
Action:OnItemPickup(Handle:event, const String:name[], bool:dontBroadcast)
{
  if(
GetConVarInt(p_Enabled) == 1)
  {
    
decl String:p_tempWeapon[64];
    
GetEventString(event"item"p_tempWeaponsizeof(p_tempWeapon));
  
    if(
strcmp(p_tempWeapon"hegrenade") == 0)
    {
      new 
p_Client GetClientOfUserId(GetEventInt(event,"userid"));

      if(
GetConVarInt(p_TotalGrenade) > 0)
      {
        if(
p_Clients[p_Client][numHE] >= GetConVarInt(p_TotalGrenade))
        {
          if(
GetConVarInt(p_TotalGrenade) == 1)
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d HE Grenade per round!"GetConVarInt(p_TotalGrenade));
          else
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d HE Grenades per round!"GetConVarInt(p_TotalGrenade));

          
WeaponAmmoSetGrenadeCount(p_ClientSLOT_HEGRENADE0false);
          return 
Plugin_Handled;
        }
        else
        {
          ++
p_Clients[p_Client][numHE];
          return 
Plugin_Continue;
        }
      }
      else
      {
        
WeaponAmmoSetGrenadeCount(p_ClientSLOT_HEGRENADE0false);
        return 
Plugin_Handled;
      }  
    }
    if(
strcmp(p_tempWeapon"smokegrenade") == 0)
    {
      new 
p_Client GetClientOfUserId(GetEventInt(event,"userid"));    
  
      if(
GetConVarInt(p_TotalSmoke) > 0)
      {
        if(
p_Clients[p_Client][numSG] >= GetConVarInt(p_TotalSmoke))
        {
          if(
GetConVarInt(p_TotalSmoke) == 1)
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d Smoke Grenade per round!"GetConVarInt(p_TotalSmoke));
          else
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d Smoke Grenades per round!"GetConVarInt(p_TotalSmoke));
          
          
WeaponAmmoSetGrenadeCount(p_ClientSLOT_SMGRENADE0false);
          return 
Plugin_Handled;
        }
        else
        {
          ++
p_Clients[p_Client][numSG];
          return 
Plugin_Continue;
        }
      }
      else
      {
        
WeaponAmmoSetGrenadeCount(p_ClientSLOT_SMGRENADE0false);
        return 
Plugin_Handled;
      }      
    }
    if(
strcmp(p_tempWeapon,"flashbang") == 0)
    {
      new 
p_Client GetClientOfUserId(GetEventInt(event,"userid"));
      
      if(
GetConVarInt(p_TotalFlash) > 0)
      {
        if(
p_Clients[p_Client][numFB] >= GetConVarInt(p_TotalFlash))
        {
          if(
GetConVarInt(p_TotalFlash) == 1)
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d Flash Bang per round!"GetConVarInt(p_TotalFlash));
          else
            
CPrintToChat(p_Client"{lightgreen}{HoM}: {default}You are only allowed to use %d Flash Bangs per round!"GetConVarInt(p_TotalFlash));
          
          
WeaponAmmoSetGrenadeCount(p_ClientSLOT_FBGRENADE0false);            
          return 
Plugin_Handled;
        }
        else
        {
          ++
p_Clients[p_Client][numFB];
          return 
Plugin_Continue;
        }
      }
      else
      {
        
WeaponAmmoSetGrenadeCount(p_ClientSLOT_FBGRENADE0false);
        return 
Plugin_Handled;
      }
    }
  }
  return 
Plugin_Continue;
}

public 
OnPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
  if(
GetConVarInt(p_Enabled) == 1)
  {
    new 
p_Client GetClientOfUserId(GetEventInt(event,"userid"));
    
p_Clients[p_Client][numHE] = 0;
    
p_Clients[p_Client][numSG] = 0;
    
p_Clients[p_Client][numFB] = 0;
  }
}

/** Credits to Greyscale
 * Set the count of any grenade-type a client has.
 * 
 * @param client    The client index.
 * @param slot      The type of
 * @param value     The amount of ammo to set to.
 * @param add       (Optional) If true, the value is added to the grenades' current ammo count. 
 */
stock WeaponAmmoSetGrenadeCount(clientany:typevaluebool:add)
{
    
// Initialize variable (value is 0)
    
new ammovalue;
    
    
// If we are adding, then update variable with current ammo value.
    
if (add)
    {
      
ammovalue WeaponAmmoGetGrenadeCount(clienttype);
    }
    
    
SetEntData(clientg_iToolsAmmo + (type 4), ammovalue value_true);
}

/** Credits to Greyscale
 * Get the count of any grenade-type a client has.
 * 
 * @param client    The client index.
 * @param slot      The type of
 */
stock WeaponAmmoGetGrenadeCount(clientany:type)
{
    return 
GetEntData(clientg_iToolsAmmo + (type 4));

thetwistedpanda is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-28-2009 , 01:00   Re: RemovePlayerItem for specific grenade?
Reply With Quote #6

Hm the offsets and usage look right to me. Are you sure it's calling WeaponAmmoSetGrenadeCount?
__________________
Greyscale is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 08-28-2009 , 13:16   Re: RemovePlayerItem for specific grenade?
Reply With Quote #7

Yep I'm getting the chat messages.

Quote:
Originally Posted by status
# userid name uniqueid connected ping loss state
# 2 "{HoMev} Opie" BOT active
# 3 "{HoMev} Bill" BOT active
# 4 "{HoMev} Martin" BOT active
# 5 "Panda | Development" STEAM_0:0:12812584 00:26 150 0 active
Code: CPrintToChatAll("WeaponAmmoSetGrenadeCount - Client: %d,Type: %d, Value: %d, Add: %d", client, type, value, add);
Prints: WeaponAmmoSetGrenadeCount - Client: 4,Type: 13, Value: 0, Add: 0
g_iToolsAmmo: 1576

Last edited by thetwistedpanda; 08-28-2009 at 13:34.
thetwistedpanda is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-28-2009 , 13:48   Re: RemovePlayerItem for specific grenade?
Reply With Quote #8

Instead of using g_iToolsAmmo (I need to check my own code for this problem)

Try this:

Code:
new offsAmmo = FindDataMapOffs(client, "m_iAmmo") + (type * 4);
That will be your new g_iToolsAmmo, so don't use that variable anymore, and if this above works then just erase it from your plugin.

Put the line I posted above at the top of WeaponAmmoSetGrenadeCount and WeaponAmmoGetGrenadeCount

Change g_iToolsAmmo to offsAmmo.
__________________
Greyscale is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 08-28-2009 , 13:57   Re: RemovePlayerItem for specific grenade?
Reply With Quote #9

PHP Code:
stock WeaponAmmoSetGrenadeCount(clienttypevaluebool:add)
{
    
// Initialize variable (value is 0)
    
new ammovalue 0;
    
    
// If we are adding, then update variable with current ammo value.
    
if (add)
      
ammovalue WeaponAmmoGetGrenadeCount(clienttype);
    
    new 
offsAmmo FindDataMapOffs(client"m_iAmmo") + (type 4);
    
    
CPrintToChat(client"%d"offsAmmo);
    
SetEntData(clientoffsAmmoammovalue value_true);
}

/** Credits to Greyscale
 * Get the count of any grenade-type a client has.
 * 
 * @param client    The client index.
 * @param slot      The type of
 */
stock WeaponAmmoGetGrenadeCount(clienttype)
{
    new 
offsAmmo FindDataMapOffs(client"m_iAmmo") + (type 4);
    return 
GetEntData(clientoffsAmmo);

offsAmmo is returning 1620 for HE, 1628 for SM. Still doesn't seem to set the grenade value though .
thetwistedpanda is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 08-28-2009 , 15:53   Re: RemovePlayerItem for specific grenade?
Reply With Quote #10

My grenade pack plugin still works fine, that's the same way it sets grenades..

http://forums.alliedmods.net/showthr...t=grenade+pack


Maybe try:

Code:
SetEntData(client, offsAmmo, ammovalue + value);
The difference is the change won't be sent over the network.
__________________
Greyscale is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode