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

Solved It's good code?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Diversity
AlliedModders Donor
Join Date: Jan 2018
Location: Constanta, Romania
Old 01-06-2018 , 00:13   It's good code?
Reply With Quote #1

it's ok?

Code:
/*

V 1.0:

    * Now if you have the knife in your hand you are invisible



*/

#include < amxmodx >
#include < amxmisc >
#include < cstrike >
#include < hamsandwich >
#include < fun >
#include < engine >

new const   PLUGIN[ ] = "Nightcrawler Main",
            VERSION[ ] = "1.0",
            AUTHOR[ ] = "Diversity"

new g_MaxClients

new NoKnife[ 33 ]

#define NIGHT_TEAM CS_TEAM_T
#define HUMAN_TEAM CS_TEAM_CT

public plugin_init( ) {

    register_plugin( PLUGIN, VERSION, AUTHOR )

    register_event( "CurWeapon", "_CurWeapon", "be", "1=1" )

    set_task( 1.0, "_MakeInvis", 0, "", 0, "b", 0 )

    g_MaxClients = get_global_int( GL_maxClients )
}

public client_putinserver( id )
{
    NoKnife[ id ] = 1
}

public _MakeInvis( id )
{
    for( new i = 1; i <= g_MaxClients; i++ )
        if( cs_get_user_team( i ) == NIGHT_TEAM && NoKnife[ id ] == 0 && is_user_alive( i ) )
            return set_user_rendering( i, kRenderFxNone, 0, 0, 0, kRenderTransAlpha , 0 )

    return PLUGIN_HANDLED
}

public _CurWeapon( id )
{
    if( is_user_alive( id ) && get_user_weapon( id ) != CSW_KNIFE )
        return NoKnife[ id ] = 1

    if( is_user_alive( id ) && get_user_weapon( id ) == CSW_KNIFE )
        return NoKnife[ id ] = 0

    return PLUGIN_HANDLED
}

Last edited by Diversity; 01-06-2018 at 02:03.
Diversity is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2018 , 00:15   Re: It's good code?
Reply With Quote #2

Why do you ask? Does it work?
__________________
fysiks is offline
Diversity
AlliedModders Donor
Join Date: Jan 2018
Location: Constanta, Romania
Old 01-06-2018 , 00:20   Re: It's good code?
Reply With Quote #3

I want to know if there is another faster option
Diversity is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2018 , 00:32   Re: It's good code?
Reply With Quote #4

There are a couple simple optimizations that can be done but they won't likely speed it up all that much, probably negligibly.

In _MakeInvis(), make the entire code conditional based on NoKnife[id] and use get_players(). So, if NoKnife[ id ] == 0 then use get_players() to get only alive Terrorist and then set the rendering on the first player in the array.

In _CurWeapon(), you can refactor it to use the conditionals more efficiently by only calling is_user_alive() and get_user_weapon() once each. Like so:

Code:
if player is alive
    if user's weapon is knife
        NoKnife[ id ] = 0
    else
        NoKnife[ id ] = 1

return
__________________
fysiks is offline
Diversity
AlliedModders Donor
Join Date: Jan 2018
Location: Constanta, Romania
Old 01-06-2018 , 00:40   Re: It's good code?
Reply With Quote #5

Is good now?
Code:
public _MakeInvis( id )
{
    new players[32], asd
    get_players( players, asd, "ae", "TERRORIST" )

    if( asd == 1 )
        if( NoKnife[ players[ 0 ] ] && is_user_alive( players[ 0 ] ) )
            return set_user_rendering( players[ 0 ], kRenderFxNone, 0, 0, 0, kRenderTransAlpha , 0 )

    return PLUGIN_HANDLED
}

public _CurWeapon( id )
{
    if( is_user_alive( id ) && get_user_weapon( id ) == CSW_KNIFE )
        return NoKnife[ id ] = 0
    else
        return NoKnife[ id ] = 1

    return PLUGIN_HANDLED
}
Diversity is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2018 , 01:03   Re: It's good code?
Reply With Quote #6

Your implementation of _CurWeapon() looks like it should work.

Your implementation of _MakeInvis() is not equivalent to the original code and does not match what I said. However, I just noticed that even the original implementation of _MakeInvis() is not correct because there is no "id" passed to the function. Also, you return the set_user_rendering() which means that it will only set rendering for one player. If you did not mean to do this then you should remove the return there.


So, it likely should be like this:

Code:
get alive terrorist players
loop through players[]
    if NoKnife[ id ] == 0
        set rendering
__________________

Last edited by fysiks; 01-06-2018 at 01:04.
fysiks is offline
Diversity
AlliedModders Donor
Join Date: Jan 2018
Location: Constanta, Romania
Old 01-06-2018 , 01:14   Re: It's good code?
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
Your implementation of _CurWeapon() looks like it should work.

Your implementation of _MakeInvis() is not equivalent to the original code and does not match what I said. However, I just noticed that even the original implementation of _MakeInvis() is not correct because there is no "id" passed to the function. Also, you return the set_user_rendering() which means that it will only set rendering for one player. If you did not mean to do this then you should remove the return there.


So, it likely should be like this:

Code:
get alive terrorist players
loop through players[]
    if NoKnife[ id ] == 0
        set rendering
now?

Code:
public _MakeInvis( id )
{
    new players[32], asd
    get_players( players, asd, "ae", "TERRORIST" )

    if( asd == 1 )
        for(new i = 0; i < players[ 0 ] i++)
            if( NoKnife[ i ] == 0 && is_user_alive( i ) )
                set_user_rendering( i, kRenderFxNone, 0, 0, 0, kRenderTransAlpha , 0 )

    return PLUGIN_HANDLED
}
Diversity is offline
Diversity
AlliedModders Donor
Join Date: Jan 2018
Location: Constanta, Romania
Old 01-06-2018 , 02:03   Re: It's good code?
Reply With Quote #8

Solved.
Diversity is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-06-2018 , 03:39   Re: It's good code?
Reply With Quote #9

Quote:
Originally Posted by Diversity View Post
now?

Code:
public _MakeInvis( id )
{
    new players[32], asd
    get_players( players, asd, "ae", "TERRORIST" )

    if( asd == 1 )
        for(new i = 0; i < players[ 0 ] i++)
            if( NoKnife[ i ] == 0 && is_user_alive( i ) )
                set_user_rendering( i, kRenderFxNone, 0, 0, 0, kRenderTransAlpha , 0 )

    return PLUGIN_HANDLED
}
You say it's solve but I just want to clarify to anybody reading this that this code will not work and is not what I suggested.
__________________
fysiks is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 01-08-2018 , 17:01   Re: It's good code?
Reply With Quote #10

PHP Code:
public _MakeInvisid )
{
    new 
players[32], asd
    get_players
playersasd"ae""TERRORIST" )

    if( 
asd == )
        for(new 
0playersi++)
            if( 
NoKnife] == && is_user_alive) )
                
set_user_renderingikRenderFxNone000kRenderTransAlpha )

    return 
PLUGIN_HANDLED 
---->

PHP Code:
public _MakeInvisid )
{
    new 
players[32], asd
    get_players
playersasd"ae""TERRORIST" )

    if( 
asd )
    {
        for(new 
0asdi++)
        {  
            new 
client players[i];
            if( 
NoKnifeclient ] == && is_user_aliveclient ) )
                
set_user_renderingclientkRenderFxNone000kRenderTransAlpha )
        }
    }

    return 
PLUGIN_HANDLED 
I quickly wrote it so it might be wrong / not work / not even compile but it should solve your issue which you falsely claimed to be solved.
eyal282 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 01:53.


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