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

Solved need help in optimization [weird coding]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 01-20-2022 , 09:34   need help in optimization [weird coding]
Reply With Quote #1

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

__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951

Last edited by kww; 01-20-2022 at 12:00.
kww is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-20-2022 , 09:57   Re: need help in optimization [weird coding]
Reply With Quote #2

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.
__________________
HamletEagle is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-20-2022 , 09:59   Re: need help in optimization [weird coding]
Reply With Quote #3

Pass the string directly no need for reformatting...
PHP Code:
                console_print(players[i], "%s"szOutput

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

__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-20-2022 at 10:12.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 01-20-2022 , 10:57   Re: need help in optimization [weird coding]
Reply With Quote #4

Quote:
Originally Posted by HamletEagle View Post
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 View Post
Pass the string directly no need for reformatting...
PHP Code:
                console_print(players[i], "%s"szOutput

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 View Post
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 View Post
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?
__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951

Last edited by kww; 01-20-2022 at 11:03.
kww is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-20-2022 , 11:11   Re: need help in optimization [weird coding]
Reply With Quote #5

Maximum SayText message is 192 characters, so yes.

And the last character ( 191 ) must be EOS ( end of string ) which is equal to 0.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-20-2022 at 11:12.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-20-2022 , 14:02   Re: need help in optimization [weird coding]
Reply With Quote #6

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)
__________________

Last edited by OciXCrom; 01-20-2022 at 14:02.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 01-20-2022 , 16:22   Re: need help in optimization [weird coding]
Reply With Quote #7

Quote:
Originally Posted by OciXCrom View Post
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 View Post
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?
__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951
kww is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2022 , 22:14   Re: need help in optimization [weird coding]
Reply With Quote #8

Quote:
Originally Posted by kww View Post
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 View Post
At what moment we got this feature?
He literally just told you in that same post.
__________________

Last edited by fysiks; 01-22-2022 at 14:45.
fysiks is offline
kww
Senior Member
Join Date: Feb 2021
Location: Russia
Old 01-22-2022 , 05:31   Re: need help in optimization [weird coding]
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
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), " ")
}
__________________
Now working on: Side Weapons (Very lazy, tbh)
Avatar source: https://bit.ly/3BAk19g
Discord: kww#9951

Last edited by kww; 01-22-2022 at 05:35.
kww is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-22-2022 , 05:38   Re: need help in optimization [weird coding]
Reply With Quote #10

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
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-22-2022 at 05:47.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Reply


Thread Tools
Display Modes

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:45.


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