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

Optimize Code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-24-2020 , 08:04   Optimize Code
Reply With Quote #1

someone optimize code for me

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fakemeta>
#include <hamsandwich>
#include <csx>

#define TASK_PRESENTS 1337
#define TASK_SHOWHUD 2674

#define ID_PRESENTS (TaskIndex - TASK_PRESENTS)
#define ID_SHOWHUD (TaskIndex - TASK_SHOWHUD)


// Variabile
new MesajSync;


// Cvaruri
new cvar_hudenable

public plugin_init()
{
   
register_plugin("Hud Info""1.1""")
   
   
// Register Cvars
   
cvar_hudenable register_cvar("credits_hudstats""1")
   
   
// Register Messages
   
MesajSync CreateHudSyncObj()
   
   
register_message(get_user_msgid("SayText"), "message")
   
register_logevent("round_start"2"1=Round_Start")

   
}

public 
client_putinserver(id)
{
   
set_task(1.0"MesajHUD"id+TASK_SHOWHUD, .flags "b")
}


public 
MesajHUD(TaskIndex)
{
   static 
id
   id 
ID_SHOWHUD;
   
   if (!
is_user_alive(id))
   {
      
id pev(idpev_iuser2)
      
      if(!
is_user_alive(id)) return;
   }
   
   if (
get_pcvar_num(cvar_hudenable))
   {
      if(
id != ID_SHOWHUD)
      {
        new 
name[32];
        
get_user_name(idname31)

        new 
stats], body];

           new 
rank_pos get_user_statsidstatsbody )
        new 
rank_max get_statsnum( )

        
set_hudmessage(0100250, -1.00.7806.01.10.00.0, -1)
        
ShowSyncHudMsg(ID_SHOWHUDMesajSync"[ Spectating %s | Rank: %d/%d ]"namerank_posrank_max)
     }
   }

__________________

Last edited by Sanjay Singh; 01-24-2020 at 08:05.
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Old 01-24-2020, 08:12
Napoleon_be
This message has been deleted by Napoleon_be. Reason: nvm,i'll repost when it's efficient enough.
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-24-2020 , 08:29   Re: Optimize Code
Reply With Quote #3

Untested. Could probably be done more efficient using Ham_Killed etc.

PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <csstats>
#include <fakemeta_util>

#define PLUGIN "ShowSpecInfo"
#define VERSION "1.0"
#define AUTHOR "NapoleoN#"

#define TASKID 546873

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR);
}

public 
client_putinserver(id)
{
    
set_task(1.0"ShowSpecInfo"id TASKID__"b");
}

public 
client_disconnect(id)
{
    if(
task_exists(id TASKID))
        
remove_task(id TASKID)
}

public 
ShowSpecInfo(id)
{
    
id -= TASKID
    
if(!is_user_alive(id))
    {
        new 
iPlayers[32], iNumiSpecPlayerszName[33], iRankiMaxRankiStats[8], iBodyHits[8];
        
get_players(iPlayersiNum"ch");
        
        
iMaxRank get_statsnum();
        
        for(new 
iiNumi++)
        {
            
iSpecPlayer pev(iPlayers[i], pev_iuser2);
            
            if(
is_user_alive(iSpecPlayer))
            {
                
get_user_name(iSpecPlayerszNamecharsmax(szName));
                
                
iRank get_user_stats(iSpecPlayeriStatsiBodyHits);
            }
        }
        
set_hudmessage(180180180, -1.00.006.01.10.0,0.0, -1);
        
show_hudmessage(id"Spectating [%s] | [Rank: %i / %i]"szNameiRankiMaxRank);
    }

__________________

Last edited by Napoleon_be; 01-24-2020 at 08:30.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-24-2020 , 11:49   Re: Optimize Code
Reply With Quote #4

Quote:
Originally Posted by Napoleon_be View Post
Untested. Could probably be done more efficient using Ham_Killed etc.

PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <csstats>
#include <fakemeta_util>

#define PLUGIN "ShowSpecInfo"
#define VERSION "1.0"
#define AUTHOR "NapoleoN#"

#define TASKID 546873

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR);
}

public 
client_putinserver(id)
{
    
set_task(1.0"ShowSpecInfo"id TASKID__"b");
}

public 
client_disconnect(id)
{
    if(
task_exists(id TASKID))
        
remove_task(id TASKID)
}

public 
ShowSpecInfo(id)
{
    
id -= TASKID
    
if(!is_user_alive(id))
    {
        new 
iPlayers[32], iNumiSpecPlayerszName[33], iRankiMaxRankiStats[8], iBodyHits[8];
        
get_players(iPlayersiNum"ch");
        
        
iMaxRank get_statsnum();
        
        for(new 
iiNumi++)
        {
            
iSpecPlayer pev(iPlayers[i], pev_iuser2);
            
            if(
is_user_alive(iSpecPlayer))
            {
                
get_user_name(iSpecPlayerszNamecharsmax(szName));
                
                
iRank get_user_stats(iSpecPlayeriStatsiBodyHits);
            }
        }
        
set_hudmessage(180180180, -1.00.006.01.10.0,0.0, -1);
        
show_hudmessage(id"Spectating [%s] | [Rank: %i / %i]"szNameiRankiMaxRank);
    }

Why is that get_players loop in there? iRank and szName will contain the values for the last player that's being spectated, not the one the current player(with id "id") is currently spectating. There's no need to loop.

This is enough(basically what op had before):
PHP Code:
iSpecPlayer pev(idpev_iuser2);
get_user_name(iSpecPlayerszNamecharsmax(szName));
iRank get_user_stats(iSpecPlayeriStatsiBodyHits);

set_hudmessage(180180180, -1.00.006.01.10.0,0.0, -1);
show_hudmessage(id"Spectating [%s] | [Rank: %i / %i]"szNameiRankiMaxRank); 
The only thing that's worth "optimizing" is to create only one task in plugin_init instead of creating a task for each player. Then inside the global task you would use get_players as you tried to, get all dead players(use flag b, don't check with is_user_alive), retrieve iuser2 and show the hud.
__________________

Last edited by HamletEagle; 01-24-2020 at 11:51.
HamletEagle is online now
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-24-2020 , 08:32   Re: Optimize Code
Reply With Quote #5

bdw why TASKID is used ?
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-24-2020 , 08:51   Re: Optimize Code
Reply With Quote #6

https://forums.alliedmods.net/showthread.php?t=125638
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Sanjay Singh
Veteran Member
Join Date: Sep 2016
Old 01-24-2020 , 09:10   Re: Optimize Code
Reply With Quote #7

so if i remove taskid it wont affect any thing and work fine .
__________________
Sanjay Singh is offline
Send a message via AIM to Sanjay Singh
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-24-2020 , 09:21   Re: Optimize Code
Reply With Quote #8

Quote:
Originally Posted by Sanjay Singh View Post
so if i remove taskid it wont affect any thing and work fine .
In this case yes, cause you're only using 1 task. If you're using multiple tasks in a plugin you should use TASKID's to seperate each task from the player.

Also, is the plugin doing the job or is it not?
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 01-24-2020 , 10:24   Re: Optimize Code
Reply With Quote #9

Wanted to make it better but then I remembered you could just add this line in your addons/amxmodx/configs/stats.ini file and have the same functionality so I dropped it:
PHP Code:
SpecRankInfo             ;SpecRank Info 
PHP Code:
#include <amxmodx>
#include <csstats>
#include <fakemeta>
#include <hamsandwich>

// Cvaruri
new g_iIDPCVarHudEnable;

public 
plugin_init()
{
    
register_plugin("Hud Info""1.1""")
    
register_dictionary("statscfg.txt")
    
    
// Register Cvars
    
g_iIDPCVarHudEnable register_cvar("credits_hudstats""1");
    
    
RegisterHam(Ham_Spawn"player""fwHamSpawnPlayer"1)
    
RegisterHam(Ham_Killed"player""fwHamKilledPlayer"1)
}

public 
client_putinserver(iID)
{
    
set_task(1.0"fwTaskShowHud"iID, .flags "b")
}

public 
fwHamSpawnPlayer(const iID)
{
    if (!
is_user_alive(iID))
        return;
    
    
remove_task(iID)
}

public 
fwHamKilledPlayer(const iID)
{
    
set_task(1.0"fwTaskShowHud"iID, .flags "b")
}

#if AMXX_VERSION_NUM <= 182
public client_disconnect(iID)
#else
public client_disconnected(iID)
#endif
{
    
remove_task(iID)
}

public 
fwTaskShowHud(const iID)
{
    if (!
get_pcvar_num(g_iIDPCVarHudEnable))
        return;
    
    
// AMXX 182 and lower have issues and client disconnect forwards don't trigger for
    // all the cases so we need to "manually" check if client is still connected too
    #if AMXX_VERSION_NUM <= 182
    
if (!is_user_connected(iID))
    {
        
remove_task(iID)
        
        return;
    }
    
#endif
    
    
new iIDSpec pev(iIDpev_iuser2);
    
    if(!
iIDSpec || iIDSpec == iID)
        return;
    
    
// Player max name length is 31
    
new szName[31];
    
get_user_name(iIDszNamecharsmax(szName))
    
    new 
iTemp[8], iRankSpec;
    
iRankSpec get_user_stats(iIDSpeciTempiTemp);
    
    
iTemp[0] = get_statsnum();
    
    
// Avoid using automatic channel and/or hud sync because they create hud flicker issues
    
set_hudmessage(0100250, -1.00.7800.01.050.00.02)
    
show_hudmessage(iIDSpec"[ %L ^"%s^": %d/%d ]"iID"ST_SPEC_RANK"szNameiRankSpeciTemp[0])

__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-24-2020 , 13:45   Re: Optimize Code
Reply With Quote #10

Well this was my original code, so i guess i fucked it up even more than trying to optimize it lol
PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <csstats>
#include <fakemeta_util>
#include <hamsandwich>

#define PLUGIN "ShowSpecInfo"
#define VERSION "1.0"
#define AUTHOR "NapoleoN#"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR);
    
    
set_task(1.0"ShowSpecInfo", .flags "b");
}

public 
ShowSpecInfo()
{
    new 
iPlayers[32], iNumiSpecPlayerszName[33], iRankiMaxRankiStats[8], iBodyhits[8];
    
get_players(iPlayersiNum"bch");
    
    
iMaxRank get_statsnum();
    
    for(new 
iiNumi++)
    {
        
iSpecPlayer pev(iPlayers[i], pev_iuser2);
        
        
get_user_name(iSpecPlayerszNamecharsmax(szName));
            
        
iRank get_user_stats(iSpecPlayeriStatsiBodyhits);
            
        
set_hudmessage(180180180, -1.00.006.01.10.0,0.0, -1);
        
show_hudmessage(iPlayers[i], "Spectating [%s] | [Rank: %i / %i]"szNameiRankiMaxRank);
            
    }

__________________

Last edited by Napoleon_be; 01-25-2020 at 06:49.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
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 09:20.


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