Raised This Month: $ Target: $400
 0% 

Optimize code, menu and nvault


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ualasxd
New Member
Join Date: Dec 2011
Old 05-05-2012 , 18:45   Optimize code, menu and nvault
Reply With Quote #1

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <nvault>

new g_iSave[33][33]

enum _:Data
{
    
_Nome[30],
    
_Valor_CSW // CSW_*
}
new 
iVault
new Weapons[][Data] = {
    {
"Knife"29},
    {
"Glock 18"17 },
    {
"USP .45 ACP Tactical",16},
    {
"P228 Compact"1},
    {
"FiveseveN"11}, // 5
    
{"Desert Eagle .50 AE"26},
    {
"Dual Elite Berettas"10},
    {
"Schmidt TMP"23},
    {
"Ingram MAC-10"7},
    {
"UMP 45"12},
    {
"MP5 Navy",  19},
    {
"ES P90"30},
    {
"M3 Super"21},
    {
"XM1014 M4"5},
    {
"Famas"15},
    {
"IMI Galil"14},
    {
"AK-47"28},
    {
"M4A1 Carbine"22},
    {
"Scout"3},
    {
"Steyr AUG A1"8}, // 20
    
{"SG-552 Commando",27},
    {
"AWP Magnum Sniper"18},
    {
"G3SG1 Auto-Sniper"24},
    {
"SG-550 Auto-Sniper"13},
    {
"M249 Para Machinegun" 20}
}
new 
g_iLevels[33][32// 4 = CSW_GRENADE, 6 = CSW_C4, 9 = CSW_SMOKEGRENADE
new g_iKills[33][32]
new 
g_pcvar_kills_level
public plugin_init()
{
    
register_plugin("ZM WEAPONS : Levels""0.01""jz")
    
    
register_clcmd("say /menu","cmd_armas_menu")
    
    
RegisterHam(Ham_TakeDamage"player""fwd_TakeDamage_Pre"0)
    
RegisterHam(Ham_Killed"player""fwd_Killed_Post"1)
    
    
g_pcvar_kills_level register_cvar("zm_kills_levels""10")
    
    
iVault nvault_open("weapon_levels")
    if ( 
iVault == INVALID_HANDLE )
        
set_fail_state"Error" );
    
}

public 
plugin_end()
{
    
nvault_closeiVault );
}
public 
cmd_armas_menu(id)
{
    new 
menu menu_create("menu - test""menu_handler")
    
    new 
szOpcao[128]
    new 
csw_id
    
for(new 0sizeof Weapons i++)
    {
        
csw_id Weapons[i][_Valor_CSW]
        
formatex(szOpcaosizeof szOpcao -1"\w%s \d-\y Level: \r %d" Weapons[i][_Nome], g_iLevels[id][csw_id]) //show name and level weapon of player
        
menu_additem(menuszOpcao)
    }

    
menu_display(idmenu)
    return 
PLUGIN_HANDLED;
}
public 
menu_handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    }
    
    
client_print(idprint_chat"Weapon %s - Level %d "Weapons[item][_Nome] , g_iLevels[id][item])
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED;
}

public 
fwd_TakeDamage_Pre(victiminflictorattackerFloat:damagedamage_bits)
{
    if(!
is_user_connected(attacker))
    return 
HAM_IGNORED

    
new weapon get_user_weapon(attacker)
    if (
weapon == CSW_HEGRENADE)
    return 
HAM_IGNORED

    
new level g_iLevels[attacker][weapon]
    
SetHamParamFloat(4level 10.0)
    return 
HAM_IGNORED;
}
public 
fwd_Killed_Post(victimidwtf)
{
    if(!
is_user_connected(id))
    return 
HAM_IGNORED;
    
    new 
weapon get_user_weapon(id_,_)
    if(++
g_iKills[id][weapon] >= get_pcvar_num(g_pcvar_kills_level))
    {
        
g_iLevels[id][weapon]++
        
g_iKills[id][weapon] = 0
        Save
(id);
    }
    return 
HAM_IGNORED;
}
public 
client_putinserver(id)
{
    
get_user_authid(idg_iSave[id], 32)
    
Load(id)
}
public 
Load(id)
{
    
    new 
szData[120];
    
    if ( 
nvault_getiVault g_iSave[id], szData charsmaxszData ) ) )
    {
        new 
space
        
for(new sizeof Weapons i++)
        {
            if(
szData[i] == ' ')
            
space 1
            
            g_iLevels
[id][i] =  szData[space i
            
        }
    }
}
public 
Save(id)
{
    new 
szData[128],temp[40]
    
    for(new 
isizeof Weaponsi++)
    {
        
formatex(tempsizeof temp -1" %d",  g_iLevels[id][i])
        
add(szData,sizeof szData -1temp)
    }
    
//server_print(szData) //debug
    
nvault_setiVault g_iSave[id] , szData );

Don't load correctly .
And the menu sometimes shows the wrong level of the weapon. What can be?


Any suggestions to optimize the code?

Last edited by Ualasxd; 05-05-2012 at 18:46. Reason: [code]
Ualasxd is offline
Old 05-05-2012, 19:36
Bilal Pro
This message has been deleted by Exolent[jNr]. Reason: Sigh. Bilal Pro stop helping because you don't have a clue of what you are doing. Also, don't flame.
Old 05-05-2012, 19:45
Ualasxd
This message has been deleted by Exolent[jNr]. Reason: Sigh. Bilal Pro stop helping because you don't have a clue of what you are doing. Also, don't flame.
Old 05-05-2012, 19:49
Bilal Pro
This message has been deleted by Exolent[jNr]. Reason: Sigh. Bilal Pro stop helping because you don't have a clue of what you are doing. Also, don't flame.
Old 05-05-2012, 20:59
kramesa
This message has been deleted by Exolent[jNr]. Reason: Sigh. Bilal Pro stop helping because you don't have a clue of what you are doing. Also, don't flame.
Ualasxd
New Member
Join Date: Dec 2011
Old 06-09-2012 , 20:09   Re: Optimize code, menu and nvault
Reply With Quote #6

Someone?
Ualasxd is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 06-10-2012 , 00:38   Re: Optimize code, menu and nvault
Reply With Quote #7

There's quite a number of issues here.

First of all, you can't level up, because you start at level 0 and level * 10.0 = 0.0 .

( Just note that I've only added and edited parts to make your plugin work, not to make it more efficient )

So change your Ham_TakeDamage hook to:

Code:
public fwd_TakeDamage_Pre(victim, inflictor, attacker, Float:damage, damage_bits) 
{ 
	if(!is_user_connected(attacker)) 
		return HAM_IGNORED 
	
	new weapon = get_user_weapon(attacker) 
	if (weapon == CSW_HEGRENADE) 
		return HAM_IGNORED 
	
	new level = g_iLevels[attacker][weapon] 
	SetHamParamFloat(4, ( level + 1 ) * 10.0) 
	return HAM_IGNORED; 
}
This will display the message properly when you select a menu item:

Code:
public menu_handler(id, menu, item) 
{ 
	if( item == MENU_EXIT ) 
	{ 
		menu_destroy(menu); 
		return PLUGIN_HANDLED; 
	} 
	
	client_print(id, print_chat, "Weapon %s - Level %d ", Weapons[item][_Nome] , g_iLevels[ id ][ Weapons[ item ][ _Valor_CSW ] ] ) 
	menu_destroy(menu) 
	
	return PLUGIN_HANDLED; 
}
The reason you can't save or load properly is because your level variable isn't saving with the same alignment to your data structure. You can't save them in order of weapon constant, because your data structure isn't in weapon constant order.

So instead you can save and load according to the structure:
Code:
public Load(id) {     new szData[128];         nvault_get( iVault , g_iSave[id], szData , charsmax( szData ) )     new szPiece[ 32 ], i;         /*I wont lie ;) Exolent taught me the use of strtok() */         while( contain( szData, " " ) >= 0 )     {               strtok( szData, szPiece, charsmax( szPiece ), szData, charsmax( szData ), ' ' )                         g_iLevels[ id ][ Weapons[ i ][ _Valor_CSW ] ] = str_to_num( szPiece );                 if( szPiece[ 0 ] )             i ++;     }         if( !i )     {         Save( id );         Load( id );     } } public Save(id) {     new szData[128],temp[40]         for(new i; i < sizeof Weapons; i++)     {         formatex(temp, charsmax( temp ), " %d",  g_iLevels[ id ][ Weapons[ i ][ _Valor_CSW ] ] )         add(szData, charsmax( szData ), temp)     }         nvault_set( iVault , g_iSave[id] , szData ); }

And change client_putinserver() to this:
Code:
public client_connect(id) {     get_user_authid(id, g_iSave[id], charsmax( g_iSave[] ))     Load(id) }

See how it goes.

Also, I'd recommend that you don't use is_user_connected() to ensure that the attacker is indeed a player in your Ham_TakeDamage and Ham_Killed hooks.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.

Last edited by hornet; 06-10-2012 at 00:55.
hornet is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-10-2012 , 01:44   Re: Optimize code, menu and nvault
Reply With Quote #8

try this.... in the Load() function
Code:
static space = 0
//for loop
if(szData[i] == ' ') 
            space++
This is assuming you actually want to count the spaces from the retrieved data....

also
g_iSave[33][33]

g_iSave[33][32]
SteamIDs are only 32, you don't need to set it to 33, its one extra bit you don't need. Relatively negligible in the long run, but an optimization, if you care about it.

EDIT: looking at how hornet fixed your code.....the first half of this is obsolete. The second half is still just a little something.

Oh...and instead of using client_putinserver() use client_authorized(id) because this is based completely on the authorization of the client (using their SteamID) you want to make sure they actually have one when you execute the body of it. doing it this way will ensure that.
client_putinserver(id) is called when the client gets prompted the MOTD, while client_authorized() is called when the client has a proper SteamID placed to his credentials.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-10-2012 at 01:51.
Liverwiz is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 06-10-2012 , 02:20   Re: Optimize code, menu and nvault
Reply With Quote #9

He doesn't want to count the spaces. It looks as though he thought that he could retrieve the level by getting the character after the space.

Quote:
Originally Posted by Liverwiz View Post
Oh...and instead of using client_putinserver() use client_authorized(id) because this is based completely on the authorization of the client (using their SteamID) you want to make sure they actually have one when you execute the body of it. doing it this way will ensure that.
client_putinserver(id) is called when the client gets prompted the MOTD, while client_authorized() is called when the client has a proper SteamID placed to his credentials.
Not necessarily:
http://www.amxmodx.org/funcwiki.php?...rver&go=search
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-10-2012 , 11:54   Re: Optimize code, menu and nvault
Reply With Quote #10

Quote:
Originally Posted by hornet View Post
He doesn't want to count the spaces. It looks as though he thought that he could retrieve the level by getting the character after the space.



Not necessarily:
http://www.amxmodx.org/funcwiki.php?...rver&go=search
yeah....i wasn't fully sure what he was actually trying to do.

and i suppose your right, but i still believe that in order to make sure its proper (because it doesn't call any private data other than SteamID) i'd use it from client_authorized(). But from what i hear typically authorization is done before putinserver. But i have seen people have STEAM_ID_PENDING when they are spawned. It all depends on his level of acceptable malfunction.
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz 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 00:21.


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