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

[TF2] Gamedata: GetMaxAmmo / GetMaxClip


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 05-10-2015 , 11:38   [TF2] Gamedata: GetMaxAmmo / GetMaxClip
Reply With Quote #1

Useful for anyone modding weapons or anything related to them.

This should also work for weapons with custom attributes and custom spawned weapons.

Anyway, TF2 doesn't seem to have a netprop or datamap for a weapon's max clip/ammo, meaning you have to use an SDKCall to GetMaxAmmo / GetMaxClip1 to get it. Maybe there's hidden ones really, but yeah.

It also properly accounts for mult_clip_size. I believe it returns -1 for energy weapons though. m_iClip1 will be -1 for energy weapons anyway. (Such as the Cow Mangler).

I also threw in GetDefaultClip (whatever that is) and GetSlot.

And well... see the attached files!

Oh, I only set it up for Linux/Mac, because I'm lazy.
Attached Files
File Type: txt weapon.data.txt (1,004 Bytes, 430 views)
File Type: inc weapondata.inc (4.4 KB, 382 views)
__________________

Last edited by Chdata; 06-08-2015 at 11:21.
Chdata is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-11-2015 , 17:47   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #2

Weapons have a virtual function that can be called to get this information, but Bacardi was saying it doesn't work on TF2 or CS:GO... maybe because they're CEconItems and not standard weapons.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-11-2015 at 17:48. Reason: they're, not their
Powerlord is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 05-12-2015 , 10:31   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #3

I thought of that too, but this is less work and easier.

Also now that I think of it... wow those GetWeaponX functions must be old.

But I'm not sure, how would one rewrite those exactly?

I think GetWeaponClip would just be

PHP Code:
stock GetWeaponClip(weapon)
{
    if (
IsValidEntity(weapon))
    {
        return 
GetEntProp(weaponProp_Send"m_iClip1");
    }
    return 
0;

__________________

Last edited by Chdata; 05-12-2015 at 10:36.
Chdata is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-12-2015 , 12:03   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #4

To be honest, after doing a bit of research, it's a bit of a pain to check ammo levels using methods. Hell, you'd need a signature to call CAmmDef::MaxCarry as it has no entity.

Incidentally, why would you need a TF2Items callback to get accurate numbers? Doesn't the post_inventory_application event fire after a player is given a new inventory? You could just iterate over their item slots then.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-12-2015 at 12:03.
Powerlord is offline
psychonic

BAFFLED
Join Date: May 2008
Old 05-12-2015 , 13:35   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #5

Quote:
Originally Posted by Chdata View Post
Anyway, TF2 doesn't seem to have a netprop or datamap for a weapon's max clip/ammo, meaning you have to get their 'current' clip/ammo...
As Powerlord mentioned, there are virtual methods for this on the weapon itself. They exist in every game and it would be a good candidate of something to be added to SDKTools as it also handles any game-specific logic rather than just reading the weapon script. Additionally, default ammo/clip is not guaranteed to be the max.

There are multiple other issues here as well.

Quote:
Originally Posted by Chdata View Post
#define TF_MAX_PLAYERS 34
There isn't much of a reason to not uses (MAXPLAYERS + 1) here instead of making your own define. Srcds already has a much higher memory variance than the 1.5kb you're saving with this. Additionally, if other people try to do the same, the define could conflict with another, or even be defined differently in the case of someone doing the same, but having it as 33 and then doing a +1.

Quote:
Originally Posted by Chdata View Post
new iOffset = GetEntProp(weapon, Prop_Send, "m_iPrimaryAmmoType", 1)*4;
new iAmmoTable = FindSendPropInfo("CTFPlayer", "m_iAmmo");
return GetEntData(owner, iAmmoTable+iOffset, 4);
Quote:
Originally Posted by Chdata View Post
new AmmoClipTable = FindSendPropInfo("CTFWeaponBase", "m_iClip1");
return GetEntData(weapon, AmmoClipTable);
GetEntProp should be used, else cache is being bypassed and for extra complication of code.

Quote:
Originally Posted by Chdata View Post
if (!IsValidEnt(iWeapon) || !GetEdictClassname(iWeapon, szClassname, sizeof(szClassname)))
This is checking whether it's a valid entity, but then getting the edict classname. Not all entities have edicts. GetEntityClassname or IsValidEdict would be more appropriate.

Quote:
Originally Posted by Chdata View Post
stock bool:IsValidEnt(iEnt)
This is ambiguously named and requires extra effort to figure out what the difference between IsValidEntity and IsValidEnt are.
psychonic is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-12-2015 , 14:02   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #6

Quote:
Originally Posted by psychonic View Post
GetEntProp should be used, else cache is being bypassed and for extra complication of code.
To add to this, GetEntProp's size and element values for m_iAmmo would be 4 and iOffset respectively. The 4 doesn't even need to be set as GetEntProp can figure it out on its own.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 05-15-2015 , 12:31   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #7

Quote:
Originally Posted by Powerlord View Post
To be honest, after doing a bit of research, it's a bit of a pain to check ammo levels using methods. Hell, you'd need a signature to call CAmmDef::MaxCarry as it has no entity.

Incidentally, why would you need a TF2Items callback to get accurate numbers? Doesn't the post_inventory_application event fire after a player is given a new inventory? You could just iterate over their item slots then.
Hm, I used to use post_inventory_application but had problems. I ought to test it again.

and isvalident is something I mostly use privately, usually I edit that out of public things, dunno why I didn't here

So...

PHP Code:
stock GetWeaponAmmo(weapon)
{
    new 
owner GetEntPropEnt(weaponProp_Send"m_hOwnerEntity");
    if (
owner == -1) return 0;
    if (
IsValidEntity(weapon))
    {
        new 
iOffset GetEntProp(weaponProp_Send"m_iPrimaryAmmoType"1);
        return 
GetEntProp(ownerProp_Send"m_iAmmo"_iOffset);
    }
    return 
0;
}] 
I'd make an sdkcall but i'm too lazy.

Quote:
Originally Posted by Powerlord View Post
The 4 doesn't even need to be set as GetEntProp can figure it out on its own.
What exactly does "GetEntProp" do to "figure it out"? I see it has a default value of 4 for the size, but I don't see anything like sizeof(int) or something.
__________________

Last edited by Chdata; 05-17-2015 at 06:52.
Chdata is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 06-07-2015 , 05:05   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #8

edit: Gonna update this soon with some sdkcalls
__________________

Last edited by Chdata; 06-07-2015 at 05:37.
Chdata is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 06-08-2015 , 16:47   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #9

Quote:
Originally Posted by Chdata View Post
What exactly does "GetEntProp" do to "figure it out"? I see it has a default value of 4 for the size, but I don't see anything like sizeof(int) or something.
Whoops, I missed this last month.

If you check a netprops / datamaps list, you'll notice that it includes the number of bits each thing takes up. That's because the SendProp class (which is what netprops are) stores this information1. I'm assuming the DataMap equivalent does as well.

1Unless it's a CUtlVector SendProp or one of its direct children. These are really a wrapper around a CUtlVector object on the server side and not an actual number/string property.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 06-08-2015 at 16:51.
Powerlord is offline
psychonic

BAFFLED
Join Date: May 2008
Old 06-08-2015 , 17:11   Re: [TF2] Accurately get a weapon's max clip/ammo.
Reply With Quote #10

Quote:
Originally Posted by Powerlord View Post
If you check a netprops / datamaps list, you'll notice that it includes the number of bits each thing takes up. That's because the SendProp class (which is what netprops are) stores this information1. I'm assuming the DataMap equivalent does as well.
Both store a data type (int, float, etc.), and SendProps also store a count of bits to be networked.
psychonic 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 19:33.


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