Raised This Month: $ Target: $400
 0% 

Saving player weapons and ammo


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Iceaac
Junior Member
Join Date: Oct 2009
Old 06-25-2012 , 07:10   Saving player weapons and ammo
Reply With Quote #1

Hey!

I wish to save players' weapons and ammo when they disconnect and give the weapons and ammo back correctly when they come online again. I've found a very useful post, which saves the weapons and gives them back later, I managed to implement it in my script properly and it works flawlessly, but I don't know how can I save the weapons' ammo in the backpack AND in the clip of the weapons.
A small note: My script enables having multiple primary and secondary weapons, if this changes anything.

Any help would be appreciated!

Edit: I forgot to quote the post about weapon saving (if this provides any help):


Quote:
Originally Posted by Bugsy View Post
To block weapon purchase you could destroy the buyzone: http://forums.alliedmods.net/showthread.php?t=89676

Not sure if this is exactly what you need but this is a quick and easy way to save\restore weapons.

Save weapons:
PHP Code:
g_PlayerWeapons[id] = pevid pev_weapons ) &~ ( << 31 ); 
Restore weapons:
PHP Code:
//To restore grenades you will need to set bpammo accordingly.
new iValue g_PlayerWeapons[id];

for( new 
<= 30 j++ )
{
    if ( (
& (iValue >> j)) && ( != ) && ( != CSW_C4) && ( != CSW_KNIFE ) )
    {
        
get_weaponnameszWeapon 31 );
        
fm_give_itemid szWeapon );
    }


Last edited by Iceaac; 06-25-2012 at 07:16. Reason: Added weapon saving.
Iceaac is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-25-2012 , 08:17   Re: Saving player weapons and ammo
Reply With Quote #2

I think you won't be able to do this with bitsums, which means you would have to create a more dimensional variable, like:

PHP Code:
enum AmmoType
{
     
ClipAmmo,
     
BpAmmo
}

new 
g_Weapons33 ][ CSW_P90 /*I think?*/ ][ AmmoType 
Then you will have to set the variable for each player like

PHP Code:
new weapon get_user_weaponid )
g_Weaponsid ][ weapon ][ BpAmmo  ] = cs_get_user_bpammoidweapon 
g_Weaponsid ][ weapon ][ ClipAmmo ] = /* you'll need to get the weapon's entity index to get clip ammo here */ 
You can save some memory and set for example bpammo to -1 if the user doesn't have the specific weapon.

Last edited by Backstabnoob; 06-25-2012 at 08:19.
Backstabnoob is offline
Iceaac
Junior Member
Join Date: Oct 2009
Old 06-25-2012 , 10:16   Re: Saving player weapons and ammo
Reply With Quote #3

Thank you for your reply!

I guess I understand how can I get the ammo in the backpack with a loop, but the clip ammo is not really clear! Also, get_user_weapon returns the id of the weapon currently being held by the player, isn't it? Can you please write an example how should it be used?
Thank you!
Iceaac is offline
Old 06-25-2012, 12:37
Bugsy
This message has been deleted by Bugsy.
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-25-2012 , 12:53   Re: Saving player weapons and ammo
Reply With Quote #4

Bitsums can be useful for storing what weapons a player has, if you want to store clip\bp-ammo or any other info then it will take some more coding, as backstabnoob said.

This will save\restore all primary\secondary weapons as well as grenades. Let me know if you have any problems.
PHP Code:
#include <amxmodx>
#include <fakemeta>
#include <fakemeta_util>
#include <hamsandwich>
#include <fun>
#include <cstrike>
#include <nvault>

new const Version[] = "0.1";

new const 
VaultName[] = "weapondata";

const 
NoClipAmmo = ( ( << CSW_FLASHBANG ) | ( << CSW_HEGRENADE ) | ( << CSW_SMOKEGRENADE ) );
const 
IgnoreWeapons = ( ( << CSW_C4 ) | ( << CSW_KNIFE ) );

new 
g_Vault g_szAuthID33 ][ 35 ] , g_FirstSpawn;

public 
plugin_init() 
{
    
register_plugin"Save and Restore Weapons" Version "bugsy" );
    
    
RegisterHamHam_Spawn "player" "fw_HamSpawn_Post" );
    
    if ( ( 
g_Vault nvault_openVaultName ) ) == -)
        
log_amx"Error opening vault" );
}

public 
plugin_end()
{
    
nvault_closeg_Vault );
}

public 
client_putinserverid 
{
    
get_user_authidid g_szAuthIDid ] , charsmaxg_szAuthID[] ) );
    
g_FirstSpawn |= ( << ( id 31 ) );
}

public 
fw_HamSpawn_Postid )
{
    if ( 
is_user_aliveid ) )
    {
        if ( 
g_FirstSpawn & ( << ( id 31 ) ) )
        {
            new 
szData256 ] , iTS;
    
            if ( 
nvault_lookupg_Vault g_szAuthIDid ] , szData charsmaxszData ) , iTS ) )
            {
                new 
bool:bHasC4 bool:user_has_weaponid CSW_C4 );
                    
                
strip_user_weaponsid );
                
                
give_itemid "weapon_knife" );
                
                if ( 
bHasC4 )
                    
give_itemid "weapon_c4" );
                    
                new 
iWeapon szWeapon20 ] , szWeaponData14 ] , szClip] , szBP] , iClip iBP;
                
                for ( new 
<= 30 i++ )
                {
                    if ( !( 
IgnoreWeapons & ( << ) ) )
                    {
                        
strbreakszData szWeaponData charsmaxszWeaponData ) , szData charsmaxszData ) );
                        
strbreakszWeaponData szClip charsmaxszClip ) , szBP charsmaxszBP ) );
                        
                        
iClip str_to_numszClip );
                        
iBP str_to_numszBP );
                        
                        if ( 
iClip > -)
                        {
                            
get_weaponnameszWeapon charsmaxszWeapon ) );
                            
                            if ( ( 
iWeapon give_itemid szWeapon ) ) > -
                            {
                                if ( !( 
NoClipAmmo & ( << ) ) && ( iClip ) )
                                    
cs_set_weapon_ammoiWeapon iClip );
                                
                                if ( 
iBP )
                                    
cs_set_user_bpammoid iBP );
                            }
                        }
                    }
                }
            }
        
            
g_FirstSpawn &= ~( << ( id 31 ) );
        }
    }
}

public 
client_disconnectid )

    if ( 
is_user_aliveid ) ) 
    {
        new 
iWeapons pevid pev_weapons ) , szData256 ] , iPos szWeapon20 ] , iEntity iWeaponAmmo;
        
        for ( new 
<= 30 i++ )
        {
            if ( !( 
IgnoreWeapons & ( << ) ) )
            {
                if ( 
iWeapons & ( << ) )
                {
                    
get_weaponnameszWeapon charsmaxszWeapon ) );
                    
                    if ( 
NoClipAmmo & ( << ) ) 
                    {
                        
iWeaponAmmo 0;
                    }
                    else
                    {
                        
iEntity fm_find_ent_by_owner( -szWeapon id );
                        
iWeaponAmmo cs_get_weapon_ammoiEntity );
                    }
                    
                    
iPos += formatexszDataiPos ] , charsmaxszData ) - iPos "^"%%d^" " iWeaponAmmo cs_get_user_bpammoid ) );
                }
                else
                {
                    
iPos += copyszDataiPos ] , charsmaxszData ) - iPos "^"--1^" " );
                }
            }
        }
    
        
nvault_setg_Vault g_szAuthIDid ] , szData );
    }
    else
    {
        
nvault_removeg_Vault g_szAuthIDid ] );
    }

__________________

Last edited by Bugsy; 06-25-2012 at 12:53.
Bugsy is offline
Old 06-25-2012, 13:39
Iceaac
This message has been deleted by Iceaac. Reason: -
Iceaac
Junior Member
Join Date: Oct 2009
Old 06-25-2012 , 13:44   Re: Saving player weapons and ammo
Reply With Quote #5

It works flawlessly! Thank you very much, Bugsy!

I have got a last request, if you can make it. I'd like to enable people to have "multiple accounts" on my server for their different characters, therefore it would be awesome if you could edit it to save by name instead of Steam ID. Thank you!
Iceaac is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-25-2012 , 13:46   Re: Saving player weapons and ammo
Reply With Quote #6

just change get_user_authid to get_user_name
Backstabnoob is offline
Iceaac
Junior Member
Join Date: Oct 2009
Old 06-25-2012 , 13:52   Re: Saving player weapons and ammo
Reply With Quote #7

I tried it already, and for some reason, it gives me the same weapons with different names.

Edit: I'm completely retarded. It DOES work! Don't know what was the problem before... Thank you, both of you! ^^

Last edited by Iceaac; 06-25-2012 at 14:53. Reason: I'm retarded
Iceaac is offline
Reply


Thread Tools
Display Modes

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:14.


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