Raised This Month: $ Target: $400
 0% 

Optimizing code.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 16:14   Re: Optimizing code.
Reply With Quote #1

Quote:
Originally Posted by danielkza View Post
From what I understood, it's a very simple snippet, what it does is check if an entity is a 'func_button'. If positive, loop through two string sets, checking if the target name matches. If it does, store the entity index on gButtons[START], if it's the first match, or gButtons[START2] if one was already found. Do the same with the stop buttons.

My attempt:
Code:
new iStartCount = 0, iStopCount = 0

if(equal( szClassname, "func_button"))
{
    new szTarget[32], i;
    pev(iEntity, pev_target, szTarget, charsmax(szTarget));
    
    if(!iStartCount || (iStartCount == 1 && iEntity != gButtons[START]))
    {
        for( i = 0; i < sizeof(szCounterStart); i++)
        {
            if(equal(szTarget, szCounterStart[i]))
                gButtons[!(iStartCount++) ? START : START2] = iEntity;
        }
    }
    else if(!iStopCount || (iStopCount == 1 && iEntity != gButtons[STOP]))
    {
        for( i = 0; i < sizeof(szCounterStop); i++)
        {
            if(equal(szTarget, szCounterStop[i]))
                gButtons[!(iStopCount++) ? STOP : STOP2] = iEntity;
        }
    }
}
PHP Code:
      if(!iStartCount || (iStartCount == && iEntity != gButtons[START])) 
and

PHP Code:
     else if(!iStopCount || (iStopCount == && iEntity != gButtons[STOP])) 
Don't make sense because iStartCount and iStopCount will be 0.
__________________

Last edited by joaquimandrade; 04-10-2009 at 16:18.
joaquimandrade is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 17:03   Re: Optimizing code.
Reply With Quote #2

Quote:
Originally Posted by joaquimandrade View Post
PHP Code:
      if(!iStartCount || (iStartCount == && iEntity != gButtons[START])) 
and

PHP Code:
     else if(!iStopCount || (iStopCount == && iEntity != gButtons[STOP])) 
Don't make sense because iStartCount and iStopCount will be 0.
In the first run they'll be 0, but they'll be incremented inside the loop if a match is found.

And i'm not sure if it should be an if or an elseif in the second part, xPaw should know and can tell us.
__________________

Community / No support through PM
danielkza is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:10   Re: Optimizing code.
Reply With Quote #3

Quote:
Originally Posted by danielkza View Post
In the first run they'll be 0, but they'll be incremented inside the loop if a match is found.

And i'm not sure if it should be an if or an elseif in the second part, xPaw should know and can tell us.
They are not inside a loop.
__________________
joaquimandrade is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:23   Re: Optimizing code.
Reply With Quote #4

I don't know if you already understood what i'm trying to tell you:

PHP Code:
if(!iStartCount || (iStartCount == && iEntity != gButtons[START]))
{
    for( 
0sizeof(szCounterStart); i++)
        { 
What happens inside the for, will not affect the if.
__________________
joaquimandrade is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 17:25   Re: Optimizing code.
Reply With Quote #5

Quote:
Originally Posted by joaquimandrade View Post
I don't know if you already understood what i'm trying to tell you:

PHP Code:
if(!iStartCount || (iStartCount == && iEntity != gButtons[START]))
{
    for( 
0sizeof(szCounterStart); i++)
        { 
What happens inside the for, will not affect the if.
It will affect the next call, this code is just a part of a forward or another loop that will be called for multiple entities. When the next entity is passed to the function/whatever this code is, it will work like intended.

@xPaw:
Just come and explain us what you want lol
__________________

Community / No support through PM
danielkza is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:27   Re: Optimizing code.
Reply With Quote #6

Quote:
Originally Posted by danielkza View Post
It will affect the next call, this code is just a part of a forward or another loop that will be called for multiple entities. When the next entity is passed to the function/whatever this code is, it will work like intended.

@xPaw:
Just come and explain us what you want lol
So you can't have

PHP Code:
new iStartCount 0iStopCount 
before the if
__________________
joaquimandrade is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 17:29   Re: Optimizing code.
Reply With Quote #7

Quote:
Originally Posted by joaquimandrade View Post
So you can't have

PHP Code:
new iStartCount 0iStopCount 
before the if
No, they should be global vars or something, I forgot to mention it.
__________________

Community / No support through PM
danielkza is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 17:32   Re: Optimizing code.
Reply With Quote #8

Quote:
Originally Posted by danielkza View Post
No, they should be global vars or something, I forgot to mention it.
Ok. xPaw we're partying without you
__________________
joaquimandrade is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-11-2009 , 03:31   Re: Optimizing code.
Reply With Quote #9

Guys having fun...
Okay i'm doing Kreedz plugin, and i'm searching for Start & Stop buttons, since some maps have 2 start or stop buttons, thats why i'm searching for 2nd button, then later i'm using them to hook starting/stopping timer... I'm not sure is it best way to search them in FM_Spawn

PHP Code:
enum _:iButtons {
    
START 0,
    
STOP,
    
START2,
    
STOP2
};

new 
gButtons[iButtons];

// Timer entitys
new szCounterStart[][]    = { "counter_start""clockstartbutton""firsttimerelay""multi_start""counter_start_button" };
new 
szCounterStop[][]    = { "counter_off""clockstop""clockstopbutton""multi_stop""stop_counter" };


public 
fwdSpawniEntity ) {
    if( 
pev_valid(iEntity) ) {
        new 
szClassname[32];
        
peviEntitypev_classnameszClassname31 );
        
        if( 
equalszClassname"func_water" ) ) {
            if( !
gInvisWaterFound )
                
gInvisWaterFound true;
            
            
gInvisWaterEntityiEntity ] = true;
        }
        
        if( 
equalszClassname"func_button" ) ) {
            new 
szTarget[32], i;
            
peviEntitypev_targetszTarget31 );
            
            if( !
gButtons[START] )
                for( 
0sizeof szCounterStarti++ )
                    if( 
equalszTargetszCounterStart[i] ) )
                        
gButtons[START] = iEntity;
            
            if( !
gButtons[START2] && gButtons[START] )
                for( 
0sizeof szCounterStarti++ )
                    if( 
iEntity != gButtons[START] && equalszTargetszCounterStart[i] ) )
                        
gButtons[START2] = iEntity;
            
            if( !
gButtons[STOP] )
                for( 
0sizeof szCounterStopi++ )
                    if( 
equalszTargetszCounterStop[i] ) )
                        
gButtons[STOP] = iEntity;
            
            if( !
gButtons[STOP2] && gButtons[STOP] )
                for( 
0sizeof szCounterStopi++ )
                    if( 
iEntity != gButtons[STOP] && equalszTargetszCounterStop[i] ) )
                        
gButtons[STOP2] = iEntity;
        }
        
        for( new 
0sizeof gRemoveEntitiesi++ ) {
            if( 
equal(szClassnamegRemoveEntities[i]) ) {
                
engfuncEngFunc_RemoveEntityiEntity );
                break;
            }
        }
    }
    
    return 
FMRES_IGNORED;

__________________

Last edited by xPaw; 04-11-2009 at 03:38.
xPaw is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-11-2009 , 03:49   Re: Optimizing code.
Reply With Quote #10

This is an old, partial, script I started doing a while ago, for a kreedz plugin, I'm sure there's something done for the start/end counters, but I'm too lazy to search for it.

If you want it:
http://ampaste.net/f4cf0fd74
__________________

Community / No support through PM
danielkza 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 02:19.


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