AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimize Code (https://forums.alliedmods.net/showthread.php?t=321079)

Sanjay Singh 01-24-2020 08:04

Optimize Code
 
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)
     }
   }



Napoleon_be 01-24-2020 08:29

Re: Optimize Code
 
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);
    }



Sanjay Singh 01-24-2020 08:32

Re: Optimize Code
 
bdw why TASKID is used ?

Napoleon_be 01-24-2020 08:51

Re: Optimize Code
 
https://forums.alliedmods.net/showthread.php?t=125638

Sanjay Singh 01-24-2020 09:10

Re: Optimize Code
 
so if i remove taskid it wont affect any thing and work fine .

Napoleon_be 01-24-2020 09:21

Re: Optimize Code
 
Quote:

Originally Posted by Sanjay Singh (Post 2681450)
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?

georgik57 01-24-2020 10:24

Re: Optimize Code
 
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])



HamletEagle 01-24-2020 11:49

Re: Optimize Code
 
Quote:

Originally Posted by Napoleon_be (Post 2681445)
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.

Napoleon_be 01-24-2020 13:45

Re: Optimize Code
 
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);
            
    }



HamletEagle 01-24-2020 14:49

Re: Optimize Code
 
That's much better.
Just a few more things:
1.Remove !is_user_alive(iPlayers[i]) and add flag "b" to get_players instead.
2.I don't think you have to check if iSpecPlayer is alive.
3. iMaxRank = get_statsnum(); can be moved outside the loop, no need to retireve the value more than once.


All times are GMT -4. The time now is 02:44.

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