Useful Functions

Quote:
Originally Posted by Grim_
Посмотреть сообщение
Changing weapon preferences
Like the above poster, these functions will allow people to switch weapons but is not limited to the vehicle (in case you don't want certain weapons to be allowed to shoot from drive-by). Also, this gives a more simple, and clean approach. There are three functions included, even though one of them is used internally (I'm sure you could find better uses for it as well). Please note, this will take 0.3c RC4 or later to work correctly.

All descriptions about the functions can be found inside of the comments. If you have questions regarding them, ask.
pawn Код:
// You will need to change the IDs in the array to match with the weapons
// you do not want used inside of the script

/*
Function:
    AdjustWeapons
Params:
    playerid - The ID of the player
Usage:
    Checks for certain weapons, if that weapon is armed
    then it will replace it with a sub-machine gun if
    the player has one. If not, it will set to fist.
Returns:
    N/A
*/


stock AdjustWeapons( playerid )
{
    new
        ForbiddenWeapons[ ] = { 24, 27 }
    ;
   
    for( new i = 0; i < sizeof( ForbiddenWeapons ); i ++ )
    {
        if( GetPlayerWeapon( playerid ) == ForbiddenWeapons[ i ] )
        {
            new
                weapon,
                ammo
            ;
           
            GetPlayerWeaponData( playerid, 4, weapon, ammo );
            if( ammo > 0 )
                SetPlayerArmedWeapon( playerid, weapon );
            else   
                SetPlayerArmedWeapon( playerid, 0 );
        }
    }
}

/*
Function:
    AdjustWeaponsEx
Params:
    playerid - The ID of the player
    slot - The ID of the slot to replace the forbidden weapon with
    weapon = 0 - The ID of the weapon to replace with (if the slot specified is unoccupied)
Usage:
    Checks for certain weapons, if that weapon is armed
    it will replace it with a weapon in the specified
    slot. If there are no weapons in that slot, it will
    replace it with the specified weapon ID (default = 0)
    If that weapon is not found either, it will default
    back to fist.
Returns:
    N/A
*/


stock AdjustWeaponsEx( playerid, slot, weapon = 0 )
{
    new
        ForbiddenWeapons[ ] = { 24, 27 }
    ;
   
    for( new i = 0; i < sizeof( ForbiddenWeapons ); i ++ )
    {
        if( GetPlayerWeapon( playerid ) == ForbiddenWeapons[ i ] )
        {
            new
                weapon_s,
                ammo
            ;
           
            GetPlayerWeaponData( playerid, slot, weapon_s, ammo );
            if( ammo <= 0 )
            {
                GetPlayerWeaponData( playerid, GetWeaponSlot( weapon ), weapon_s, ammo );
                if( ammo <= 0 )
                    SetPlayerArmedWeapon( playerid, 0 );
                else   
                    SetPlayerArmedWeapon( playerid, weapon );
            }
            else   
                SetPlayerArmedWeapon( playerid, weapon_s );
        }
    }
}

/*
Function:
    GetWeaponSlot
Params:
    weaponid - The ID of the weapon
Usage:
    Gets the slot ID for the specified weapon
Returns:
    The returned slot ID. (Default = -1)
*/


stock GetWeaponSlot( weaponid )
{
    switch( weaponid )
    {
        case 0, 1: return 0;
        case 2..9: return 1;
        case 10..15: return 10;
        case 16..18, 39: return 8;
        case 22..24: return 2;
        case 25..27: return 3;
        case 28, 29, 32: return 4;
        case 30, 31: return 5;
        case 33, 34: return 6;
        case 35..38: return 7;
        case 40: return 12;
        case 41..43: return 9;
        case 44..46: return 11;
        default: return -1;
    }
    return -1;
}
As mentioned above, these functions are not limited to vehicles, so you can use them for other things as you please. A few examples are below:
pawn Код:
// Will check if the player has a weapon in slot 5 and put that weapon
// If not, it will switch the player to weapon ID 16
// If none of the above are possible (meaning the player has no weapons in slot 5 and no weapon ID 16), it will default to fist

// That is all considering the player entered the vehicle with a weapon that corresponds to the IDs in the array

public OnPlayerStateChange( playerid, newstate, oldstate )
{
    if( newstate == PLAYER_STATE_PASSENGER )
        AdjustWeaponsEx( playerid, 5, 16 );
   
    return 1;
}
I'm looking forward to releasing more things on the forums again. If you have any suggestions for anything, send me a PM.
Awesome
Reply

Quote:
Originally Posted by Jakku
Посмотреть сообщение
Switch is faster than if.
Only at a quantity which will never be reached in a SA-MP gamemode.
Reply

Quote:
Originally Posted by jameskmonger
Посмотреть сообщение
A more effective "NumberToMonth":
pawn Код:
stock NumberToMonth(number)
{
    switch(number)
    {
        case 1:month = "January";
        case 2: return "February";
        case 3: return "March";
        case 4: return "April";
        case 5: return "May";
        case 6: return "June";
        case 7: return "July";
        case 8: return "August";
        case 9: return "September";
        case 10: return "October";
        case 11: return "November";
        case 12: return "December";
        case default:return "INVALID_MONTH";
    }
}
you should return janurary too
Reply

My bad :P
Reply

CarColourFromName(const colourName[]);
pawn Код:
stock CarColourFromName(const colourName[]) {
    if(strcmp(colourName, "black", true, 5) == 0) {
        new RandomBlacks[][1] =
        {
            0,
            36,
            40
        };
        new rand = random(sizeof(RandomBlacks));
        return RandomBlacks[rand][1];
    }
    if(strcmp(colourName, "blue", true, 4) == 0) {
        new RandomBlues[][1] =
        {
            2, 7, 10, 12, 20,
            28, 53, 59, 79, 87,
            93, 94, 95, 100, 103,
            106, 108, 125
        };
        new rand = random(sizeof(RandomBlues));
        return RandomBlues[rand][1];
    }
    if(strcmp(colourName, "red", true, 3) == 0) {
        new RandomReds[][1] =
        {
            3, 17, 18, 21, 22, 30, 31, 42, 43, 48, 58, 62, 70, 78, 80, 82, 88, 115, 117, 121, 124
        };
        new rand = random(sizeof(RandomReds));
        return RandomReds[rand][1];
    }
    if(strcmp(colourName, "green", true, 5) == 0) {
        new RandomGreens[][1] =
        {
            4, 16, 37, 44, 51, 86, 114
        };
        new rand = random(sizeof(RandomGreens));
        return RandomGreens[rand][1];
    }
    if(strcmp(colourName, "grey", true, 4) == 0) {
        new RandomGreys[][1] =
        {
            8, 9, 11, 13, 14, 15, 19, 23, 24, 25, 26, 27, 29, 33, 34 35, 50, 60, 64, 90, 92, 96, 105, 109, 122
        };
        new rand = random(sizeof(RandomGreys));
        return RandomGreys[rand][1];
    }
    if(strcmp(colourName, "pink", true, 4) == 0) {
        new RandomPinks[][1] =
        {
            5, 85, 126
        };
        new rand = random(sizeof(RandomGreys));
        return RandomGreys[rand][1];
    }
    if(strcmp(colourName, "white", true, 5) == 0) {
        return 0;
    }
    if(strcmp(colourName, "yellow", true, 6) == 0) {
        return 6;
    }
    else {
        printf("CarColourFromName: %s was not recognised. Available colour names:", colourName);
        print("black, blue, red, green, grey, pink, white, yellow.");
                          return 0;
    }
}
Untested, should work though.

Example of usage:
pawn Код:
printf("Random black car colour: %s", CarColourFromName("black"));
Would return one of the following:
Quote:

Random black car colour: 0

Quote:

Random black car colour: 36

Quote:

Random black car colour: 40

Reply

ProgressiveRconText(const command[], const text[]);
pawn Код:
new timer1, timer2;
stock ProgressiveRconText(const command[], const text[]) {
    if(strcmp(command, "gamemode", true, 8) == 0) {
        if(strlen(text) > 0) {
            timer1 = SetTimer(ProgressiveGameModeChange, 1000, true, "s", text);
        }
    }
    if(strcmp(command, "mapname", true 7) == 0) {
        if(strlen(text) > 0) {
            timer2 = SetTimer(ProgressiveMapNameChange, 1000, true, "s", text);
        }
    }
}

public ProgressiveGameModeChange(name[])  {
    new count;
    strdel(name, count, sizeof(name));
    if(count > strlen(name)) {
        new string[128];
        format(string, sizeof(string), "gamemode %s", name);
        SendRconCommand(string);
        count++;
    } else {
        KillTimer(timer1);
    }
}
public ProgressiveMapNameChange(name[])  {
    new count2;
    strdel(name, count, sizeof(name));
    if(count2 > strlen(name)) {
        new string[128];
        format(string, sizeof(string), "mapname %s", name);
        SendRconCommand(string);
        count2++;
    } else {
        KillTimer(timer2);
    }
}
Again, untested.
Reply

Pretty Sexy code there...

VehicleHaveNitros(vehicleid)
Returns True if has Nos.
Returns False if No Nos.

pawn Код:
stock VehicleHaveNitros(vehicleid)
{
    if(GetVehicleComponentInSlot(vehicleid, CARMODTYPE_NITRO)) return true;
    else return false;
}
IsPlayerVehicleModded(playerid, vehicleid)
Returns True if Players Vehicle is Modded.
Returns False if Players Vehicle isn't Modded.


pawn Код:
stock IsPlayerVehicleModded(playerid, vehicleid)
{
    if(!IsPlayerInAnyVehicle(playerid)) return false;
    for(new slot = 0; slot < 13; slot++)
    {
        if(GetVehicleComponentInSlot(vehicleid, slot))
        {
            return 1;
        }
    }
    return 0;
}
Will post example usages later.
Reply

@willsuckformoney,
the function is called "IsPlayerVehicleModded" why do you also use the var "vehicleid"?
maybe you could use GetPlayerVehicleId(playerid), that whould be more obvious
nice function though
Reply

There is maybe other cars with boot, so you need to add them yourself..
pawn Код:
stock
    IsBootCar(vehicleid)
{
    new
        car = GetVehicleModel(vehicleid);
    switch(car)
    {
        case 401..402, 405, 409, 410, 412, 419..421, 426, 429, 436, 438, 439, 442, 445, 466, 467, 474, 475, 477, 480, 491, 492, 496, 507, 516..518, 526, 527, 529, 533, 534, 536, 540, 542, 548..551, 545, 555, 558, 559, 560, 562, 566, 567, 575, 576, 580, 585, 587, 596..598, 600, 602, 603: return car;
        default: return 0;
    }
}
Reply

[FeK]DraKiNs, as long as the format variable and the arguments correspond to eachother it works just fine. I've tested it alot and I'm currently using it a lot in a gamemode - not a single crash/faulty message.

Also, opening a file with io_write will empty the file so most of that code is just redundant.

I don't see why you're using static in there; unless you're going to use that function in OnPlayerUpdate or something it won't matter.

Here's a proper freset:
pawn Код:
freset( const szFilename[ ] )
{
    new
        File:fpFile = fopen( szFilename, io_write )
    ;
   
    if ( fpFile )
        fclose( fpFile );
}
As for the player loop, I'd suggest:
pawn Код:
static iSlots;

if ( !iSlots )
    iSlots = GetMaxPlayers( );

for ( new i = 0; i < iSlots; i++ )
{
    if ( !IsPlayerConnected( i ) )
        continue;
}
Reply

@Slice:
Just create the code, do not do it especially for someone.

Who should care for those who use it.
If you really want to optimize something loop, use any linked list
Simple

@Garsino:
Speak regarding the messages with parameters (define not use returns)


Hugs Friends.
Reply

hugs, mi amore.
Reply

You can just add a return 0; after sending the message, it's not harder than that.
Reply

[03]Garsino,return false not block the print in samp-server.exe

Hugs.
Reply

What print, lol. You don't get a print while using SendClientMessage(ToAll) ?
Reply

[03]Garsino

' • ' = Bug the samp-server.exe

It is an invalid character, so no point return false;

Thanks
Reply

Please post the function not your opinion about sh*ts, fukin anoying. There are scripting discussions forum, post in that!
Reply

[FeK]DraKiNs: I still don't get what you're talking about? :S Are you talking about ******'s macros wich I posted? But where does "•" come in the picture...?
GaGlets®: If you don't like us discussing functions here, then go somewhere else.
PS. Stop flaming in your posts and improve your english.
Reply

I was speaking in relation to another post just to fool me. (oh, i am an idiot)

Even so macros are not used in returns.

Hugs and Sorry GaGlets®.
Reply

Oh, lol.

Yes, you can't use return with macros, but you can just put return 0; afterwards, lol
Reply


Forum Jump:


Users browsing this thread: 40 Guest(s)