AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved need help in optimization [weird coding] (https://forums.alliedmods.net/showthread.php?t=336002)

kww 01-20-2022 09:34

need help in optimization [weird coding]
 
Can we optimize it? I think I can reduce the code size but unsure. After almost the year of sleeping I forgot most of the amxx scripting :bee:
PHP Code:

// enums could be renamed later (because of my weird fantasy)
enum _:eNotifyType {
    
DONT_SHOW     0,
    
ID_ONLY     1,
    
NAME_ONLY     2,
    
EVERYTHING     3,
}

enum _:ePrintDestination {
    
CHAT_AND_CON     0,
    
CHAT_ONLY         1,
    
CONSOLE_ONLY     2,
}

/**
 * Shows specified text
 * 
 * @param szInput                Text message to show
 * @param iSender                Function executor's ID
 * @param showUserInfo            What info about executor should be shown
 * @param szPluginName            Plugin name (leave blank but then this stock will be useless) // it is useless anyway :)
 * @param print_destination        Where to show this message
 * 
 * @noreturn
 */
stock __notify_everyone(const szInput[] /* 192 max? */iSender = -1eNotifyType:showUserInfo, const szPluginName[], ePrintDestination:print_destination)
{
    new 
szOutput[256], szTemp[MAX_NAME_LENGTH]
    
    if(!
szInput[0])
    {
        
log_amx("Warning! There's nothing to send to players")
        return 
PLUGIN_HANDLED
    
}
    
    if(
szPluginName[0])
        
formatex(szOutputcharsmax(szOutput), "[%s] "szPluginName)
    
    if(!
iSender// if id is not passed then show nothing anyway
        
showUserInfo DONT_SHOW
    
    
switch(showUserInfo)
    {
        case 
DONT_SHOW:
        {
            
formatex(szTempcharsmax(szTemp), "")
        }
        case 
ID_ONLY:
        {
            
formatex(szTempcharsmax(szTemp), "(ID: %i) "iSender)
        }
        case 
NAME_ONLY:
        {
            new 
szName[MAX_NAME_LENGTH]
            
get_user_name(iSenderszNamecharsmax(szName))
            
formatex(szTempcharsmax(szTemp), "%s "szName)
        }
        case 
EVERYTHING:
        {
            new 
szName[MAX_NAME_LENGTH]
            
get_user_name(iSenderszNamecharsmax(szName))
            
formatex(szTempcharsmax(szTemp), "%s (ID: %i) "szNameiSender)
        }
    }
    
    
add(szOutputcharsmax(szOutput), szTemp)
    
add(szOutputcharsmax(szOutput), szInput)
    
    new 
players[MAX_PLAYERS], playerCount
    get_players
(playersplayerCount)
    
    switch(
print_destination)
    {
        case 
CHAT_AND_CON:
        {
            new 
player
            
            
for(new iplayerCounti++)
            {
                
player players[i]
                
console_print(player"%s"szOutput)
                
client_print(playerprint_chat"%s"szOutput)
            }
        }
        case 
CHAT_ONLY:
        {
            for(new 
iplayerCounti++)
            {
                
client_print(players[i], print_chat"%s"szOutput)
            }
        }
        case 
CONSOLE_ONLY:
        {
            for(new 
iplayerCounti++)
            {
                
console_print(players[i], "%s"szOutput)
            }
        }
    }



HamletEagle 01-20-2022 09:57

Re: need help in optimization [weird coding]
 
Reducing the code size shouldn't be your goal. Do you have a valid reason as to why you want to change the code? If not, leave it as it is.

Natsheh 01-20-2022 09:59

Re: need help in optimization [weird coding]
 
Pass the string directly no need for reformatting...
PHP Code:

                console_print(players[i], "%s"szOutput

:arrow:
PHP Code:

                console_print(players[i], szOutput

Also no need for using loop to print for each individual player just use index as 0 for all and remove the loops.

Also you can use bitsum values for print destination will be be more easy and reliable, same goes for notify type.

PHP Code:


enum _
:ePrintDestination (<<=1) {
    
PRINT_CHAT     1,
    
PRINT_CONSOLE



kww 01-20-2022 10:57

Re: need help in optimization [weird coding]
 
Quote:

Originally Posted by HamletEagle (Post 2768902)
Reducing the code size shouldn't be your goal. Do you have a valid reason as to why you want to change the code? If not, leave it as it is.

Maybe you're right.

I had repeating code in some parts of a plugin so i decided to make one stock to call it. Now i just want to make it cleaner to look better. Or I shouldn't aim that?

Quote:

Originally Posted by Natsheh (Post 2768903)
Pass the string directly no need for reformatting...
PHP Code:

                console_print(players[i], "%s"szOutput

:arrow:
PHP Code:

                console_print(players[i], szOutput


Oh, thanks, i didn't notice it. Just ctrl+c then ctrl+v, sorry

Quote:

Originally Posted by Natsheh (Post 2768903)
Also no need for using loop to print for each individual player just use index as 0 for all and remove the loops.

agree

Quote:

Originally Posted by Natsheh (Post 2768903)
Also you can use bitsum values for print destination will be be more easy and reliable, same goes for notify type.

PHP Code:


enum _
:ePrintDestination (<<=1) {
    
PRINT_CHAT     1,
    
PRINT_CONSOLE



Never understood bitsums and related things. Looks like it's worth digging into this thread.


UPD: Is the chat string limit is 192 symbols?

Natsheh 01-20-2022 11:11

Re: need help in optimization [weird coding]
 
Maximum SayText message is 192 characters, so yes.

And the last character ( 191 ) must be EOS ( end of string ) which is equal to 0.

OciXCrom 01-20-2022 14:02

Re: need help in optimization [weird coding]
 
Code:
case NAME_ONLY: {     new szName[MAX_NAME_LENGTH]     get_user_name(iSender, szName, charsmax(szName))     formatex(szTemp, charsmax(szTemp), "%s ", szName) }

=>

Code:
case NAME_ONLY: {     get_user_name(iSender, szTemp, charsmax(szTemp)) }

If you're using AMXX 1.9 or 1.10, you don't even need to use get_user_name():

Code:
formatex(szTemp, charsmax(szTemp), "%n (ID: %i) ", iSender, iSender)

kww 01-20-2022 16:22

Re: need help in optimization [weird coding]
 
Quote:

Originally Posted by OciXCrom (Post 2768923)
Code:
case NAME_ONLY: {     new szName[MAX_NAME_LENGTH]     get_user_name(iSender, szName, charsmax(szName))     formatex(szTemp, charsmax(szTemp), "%s ", szName) }

=>

Code:
case NAME_ONLY: {     get_user_name(iSender, szTemp, charsmax(szTemp)) }

In this case i need that space after name

Quote:

Originally Posted by OciXCrom (Post 2768923)
If you're using AMXX 1.9 or 1.10, you don't even need to use get_user_name():

Code:
formatex(szTemp, charsmax(szTemp), "%n (ID: %i) ", iSender, iSender)

At what moment we got this feature?

fysiks 01-21-2022 22:14

Re: need help in optimization [weird coding]
 
Quote:

Originally Posted by kww (Post 2768935)
In this case i need that space after name

No. The format string tells the code how to format the string so it will only replace the special formatting character sets (i.e. the "%s" or "%n").
(I guess I misinterpreted your post)



Quote:

Originally Posted by kww (Post 2768935)
At what moment we got this feature?

He literally just told you in that same post.

kww 01-22-2022 05:31

Re: need help in optimization [weird coding]
 
Quote:

Originally Posted by fysiks (Post 2769020)
No. The format string tells the code how to format the string so it will only replace the special formatting character sets (i.e. the "%s" or "%n").

What do you mean? I know that. I wrote I need NOT only the name but a space after it too
Code:

formatex(szTemp, charsmax(szTemp), "%s ", szName)
                                      ^

If you know how to do it another way then please tell me

Sure, I can do this to add a space but what's the difference then:
Code:

case NAME_ONLY:
{
        get_user_name(iSender, szTemp, charsmax(szTemp))
        add(szTemp, charsmax(szTemp), " ")
}


Natsheh 01-22-2022 05:38

Re: need help in optimization [weird coding]
 
PHP Code:

#define MAX_NAME_LENGTH 32
new szName[MAX_NAME_LENGTH 2], len;
get_user_name(idszNameMAX_NAME_LENGTH-1);
szName[(len=strlen(szName))] = ' ';
szName[len+1] = 0



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

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