SA-MP Forums Archive
Unlimited Nitros....Is this possible? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Unlimited Nitros....Is this possible? (/showthread.php?tid=173666)



Unlimited Nitros....Is this possible? - r3k1lLz_ - 03.09.2010

Is this possible?


Re: Unlimited Nitros....Is this possible? - SpaZ (Ed) - 03.09.2010

I don't think it's possible but this should do.

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{

  if ((newkeys & KEY_FIRE) && !(oldkeys & KEY_FIRE))
  {
    if(IsPlayerInAnyVehicle(playerid))
    {
      AddVehicleComponent(GetPlayerVehicleID(playerid), 1010);
    }
  }
}
Adds a new NOS bottle every time a player uses the NOS button and it'll work (I think).


Re: Unlimited Nitros....Is this possible? - Cank - 03.09.2010

it's possible with a timer:

pawn Код:
forward nitro();
public nitro()
{
       for(new i=0; i<MAX_PLAYERS; i++)
       {
            if(IsPlayerConnected(playerid))
            {
                  if(IsPlayerInAnyVehicle(playerid))
                  {
                         AddVehicleComponent(GetPlayerVehicleID(playerid), 1010); //nitro
                  }
             }
        }
        return 1;
}

//OnGameModeInit

SetTimer("nitro", 2000, 1);
//or you do it like spaZ


Re: Unlimited Nitros....Is this possible? - Kyosaur - 03.09.2010

Quote:
Originally Posted by Cank
Посмотреть сообщение
it's possible with a timer:

pawn Код:
forward nitro();
public nitro()
{
       for(new i=0; i<MAX_PLAYERS; i++)
       {
            if(IsPlayerConnected(playerid))
            {
                  if(IsPlayerInAnyVehicle(playerid))
                  {
                         AddVehicleComponent(GetPlayerVehicleID(playerid), 1010); //nitro
                  }
             }
        }
        return 1;
}

//OnGameModeInit

SetTimer("nitro", 2000, 1);
//or you do it like spaZ
You're pretty much doing 3 connection checks + 2 vehicle checks right there lol.

Having it on a timer would be the worst way, as every time you use it a new bottle would be applied to your vehicle. Which would cut the nos off.

You'd have to press fire every 2 seconds to use it lol.


EDIT: As for the other poster, its not AS bad i guess. But you're still doing 2 connection checks, Not checking if the player is the driver, and not seeing if its a safe vehicle to apply nos on (Is this still a problem in 0.3b? Or will it through a warning?). I have no idea why you're even checking the oldkeys at all .


Re: Unlimited Nitros....Is this possible? - [HiC]TheKiller - 03.09.2010

I'd just do it like this
pawn Код:
//Func to check if it's not something like a bike
static invalid_nos_vehicle[] =
{
    430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
    493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};


public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
        new Vid = GetPlayerVehicleId(playerid);
        new model = GetVehicleModel(playerid);
        new invalid_model;
        for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
        {
            if(model == invalid_nos_vehicle[i]) invalid_model = 1;
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && newkeys & KEY_FIRE)
        {
            if(!invalid_model) AddVehicleComponent(Vid,1010);
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && oldkeys & KEY_FIRE)
        {
            if(!invalid_model) RemoveVehicleComponent(Vid,1010);
        }
        return 1;
}



Re: Unlimited Nitros....Is this possible? - Kyosaur - 03.09.2010

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
I'd just do it like this
pawn Код:
//Func to check if it's not something like a bike
static invalid_nos_vehicle[] =
{
    430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
    493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};


public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
        new Vid = GetPlayerVehicleId(playerid);
        new model = GetVehicleModel(playerid);
        new invalid_model;
        for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
        {
            if(model == invalid_nos_vehicle[i]) invalid_model = 1;
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && newkeys & KEY_FIRE)
        {
            if(!invalid_model) AddVehicleComponent(Vid,1010);
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && oldkeys & KEY_FIRE)
        {
            if(!invalid_model) RemoveVehicleComponent(Vid,1010);
        }
        return 1;
}
Why add the remove o.O ? Once they press the nos button again, it'll add a new container stopping the current nos. Also every time a player pressed a button you are using 3 functions and looping, you should check their state and keys FIRST, then do that.


Re: Unlimited Nitros....Is this possible? - [HiC]TheKiller - 03.09.2010

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
Why add the remove o.O ? Once they press the nos button again, it'll add a new container stopping the current nos. Also every time a player pressed a button you are using 3 functions and looping, you should check their state and keys FIRST, then do that.
It looks cooler when you release the key if it disappears :P. Also for the loop, I really CBF making it efficient because I wrote it up in a short period of time. Feel free to fix it .


Re: Unlimited Nitros....Is this possible? - Kyosaur - 03.09.2010

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
It looks cooler when you release the key if it disappears :P. Also for the loop, I really CBF making it efficient because I wrote it up in a short period of time. Feel free to fix it .
Ok fair game lol, never seen that before :P.


Here's how i would do it

pawn Код:
new InvalidNosVehicles[] =
{
    430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
    493,509,510,520,521,522,523,537,538,569,570,581,586,590,595
};

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & 4)
    {
        if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
        {
            new
                VehcileID,
                VehicleModelID,
                bool:Invalid;

            VehicleID = GetPlayerVehicleID(playerid);
            VehicleModelID = GetVehicleModel(VehicleID);

            for(new a=0; a<sizeof(InvalidNosVehicles); a++)
            {
                if(VehicleModelID == InvalidNosVehicles[a])
                {
                    Invalid = true;
                    break;
                }
            }
            if(!Invalid) AddVehicleComponent(VehicleID, 1010);
        }
    }
    return 1;
}

Im not at a place where i can compile / test :P, but should be fine. Someone could take it further and convert the array into a switch... but im to lazy :P.


Re: Unlimited Nitros....Is this possible? - dax123 - 03.09.2010

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
I'd just do it like this
pawn Код:
//Func to check if it's not something like a bike
static invalid_nos_vehicle[] =
{
    430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
    493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};


public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
        new Vid = GetPlayerVehicleId(playerid);
        new model = GetVehicleModel(playerid);
        new invalid_model;
        for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
        {
            if(model == invalid_nos_vehicle[i]) invalid_model = 1;
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && newkeys & KEY_FIRE)
        {
            if(!invalid_model) AddVehicleComponent(Vid,1010);
        }
        if (GetPlayerState(playerid) == PLAYER_STATE_DRIVER && oldkeys & KEY_FIRE)
        if(!invalid_model) RemoveVehicleComponent(Vid,1010);
        return 1;
}
code optimized + uses a timer to keep nitro working

pawn Код:
//Func to check if it's not something like a bike
new invalid_nos_vehicle[] =
{
    430,446,448,449,452,453,454,461,462,463,468,472,473,481,484,
    493,509,510,520,521,522,523,537,538,569,570,581,586,590,595,429,461
};

#define NITRO 1010
#define NITRO_RECHARGE_TICK 12000
#define NITRO_TIMER_WORKING (NITRO_TIMER != -1)
#define IsPlayerConnectedEx(%1) (iter_index[(%1)] != -1)
//checks if a timer is working
new NITRO_TIMER = -1;
//optimised iteration
new
    iter[MAX_PLAYERS] = { INVALID_PLAYER_ID, ... },
    iter_index[MAX_PLAYERS] = { -1, ... },
    NUM_PLAYERS;
new CHECK_RECHARGE_NITRO[ MAX_PLAYERS ];
new CHECK_KEY_NITRO[ MAX_PLAYERS ];
 
public OnPlayerConnect( playerid )
{
    //optimised iteration
    iter[ NUM_PLAYERS ] = playerid;
    iter_index[ playerid ] = NUM_PLAYERS;
    NUM_PLAYERS ++;
    //reset the nitro keycheck value
    CHECK_KEY_NITRO[ playerid ] = 0;
    return 1;
}
 
public OnPlayerDisconnect( playerid, reason )
{
    NUM_PLAYERS --;
    // trims the iteration list
    iter[ iter_index[ playerid ] ] = iter[ NUM_PLAYERS ];
    iter_index[ iter[ NUM_PLAYERS ] ] = iter_index[ playerid ];
    // reset the variable for next use
    iter_index[ playerid ] = -1;
    iter[ NUM_PLAYERS ] = INVALID_PLAYER_ID;
    //reset the nitro_recheck value
    CHECK_RECHARGE_NITRO[ playerid ] = 0;
}
 
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{

    // do something
    Something();


    if( !CHECK_KEY_NITRO[ playerid ] ) return 1;
    //check it with nitro-compatible autos
    if( (newkeys & KEY_FIRE) )
    {
        CHECK_RECHARGE_NITRO[ playerid ] = 1;
        AddVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
        if( !NITRO_TIMER_WORKING )
        {
            //enables a timer to keep nitro working
            NITRO_TIMER = SetTimer( "KeepNitro", NITRO_RECHARGE_TICK, 1 );
        }
    }
    else if( (oldkeys & KEY_FIRE) )
    {
        CHECK_RECHARGE_NITRO[ playerid ] = 0;
        RemoveVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
    }
        return 1;
}
 
public OnPlayerStateChange( playerid, newstate, oldstate )
{
    //invalid model check
    if ( newstate == PLAYER_STATE_DRIVER )
    {
        new model = GetVehicleModel( GetPlayerVehicleID(playerid) );
        CHECK_KEY_NITRO[ playerid ] = 1;
        for (new i = 0; i < sizeof(invalid_nos_vehicle); i++)
        {
            if( model == invalid_nos_vehicle[i] )
            {
                CHECK_KEY_NITRO[ playerid ] = 0;
                break;
            }
        }
    }
    else if( oldstate == PLAYER_STATE_DRIVER )
    {
        CHECK_KEY_NITRO[ playerid ] = 0;
        CHECK_RECHARGE_NITRO[ playerid ] = 0;
    }
}
 
forward public KeepNitro();
public KeepNitro()
{
    new KEEP_TIMER;

    for( new i = 0 ; i < NUM_PLAYERS ; i ++ )
    {
        #define playerid iter[i]
        if( CHECK_RECHARGE_NITRO[ playerid ] )
        {
            new key, updown, leftright;
            GetPlayerKeys( playerid, key, updown, leftright );
            if( ( key & KEY_FIRE ) )
            {
                KEEP_TIMER = 1;
                AddVehicleComponent( GetPlayerVehicleID(playerid), NITRO );
            }
        }
        #undef playerid
    }

    if( !KEEP_TIMER )
    {
        KillTimer( NITRO_TIMER );
        NITRO_TIMER = -1;
    }
}
and please, i would not modify it for your slight compile error.
regards


Re: Unlimited Nitros....Is this possible? - Mike Garber - 03.09.2010

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
You're pretty much doing 3 connection checks + 2 vehicle checks right there lol.

Having it on a timer would be the worst way, as every time you use it a new bottle would be applied to your vehicle. Which would cut the nos off.

You'd have to press fire every 2 seconds to use it lol.


EDIT: As for the other poster, its not AS bad i guess. But you're still doing 2 connection checks, Not checking if the player is the driver, and not seeing if its a safe vehicle to apply nos on (Is this still a problem in 0.3b? Or will it through a warning?). I have no idea why you're even checking the oldkeys at all .
The errors is not a SA:MP bug. It is caused by vehicle mods.