AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Saving player weapons and ammo (https://forums.alliedmods.net/showthread.php?t=188333)

Iceaac 06-25-2012 07:10

Saving player weapons and ammo
 
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 (Post 817798)
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 );
    }




Backstabnoob 06-25-2012 08:17

Re: Saving player weapons and ammo
 
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.

Iceaac 06-25-2012 10:16

Re: Saving player weapons and ammo
 
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!

Bugsy 06-25-2012 12:53

Re: Saving player weapons and ammo
 
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 ] );
    }



Iceaac 06-25-2012 13:44

Re: Saving player weapons and ammo
 
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!

Backstabnoob 06-25-2012 13:46

Re: Saving player weapons and ammo
 
just change get_user_authid to get_user_name

Iceaac 06-25-2012 13:52

Re: Saving player weapons and ammo
 
I tried it already, and for some reason, it gives me the same weapons with different names. :(

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


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

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