SA-MP Forums Archive
Useful Functions - 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: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+--- Thread: Useful Functions (/showthread.php?tid=38965)



Re: Useful Functions - Kar - 14.01.2014

I believe that should be

pawn Код:
if (1 <= index < 1812)
but nice job, I have a similiar function that fits my script's needs


AW: Re: Useful Functions - Nero_3D - 24.01.2014

I first thought it would create a square around the player in the direction he is facing

But if it is just a square you only need two loops
pawn Code:
stock CreateSquareObjectShape(playerid, Float: fDiag, objectsPerLine = 10) {
    new
        Float: rX[2],
        Float: rY[2],
        Float: fZ
    ;
    if(GetPlayerPos(playerid, fX[0], fY[0], fZ)) {
        new
            Float: inc = (fDiag * 0.707 / objectsPerLine),
            Float: cos = (fDiag * floatcos(45, degrees)),
            Float: sin = (fDiag * floatsin(45, degrees))
        ;
        rX[1] = rX[0] + cos;
        rY[1] = rY[0] + sin;
       
        rX[0] -= cos;
        rY[0] -= sin;

        for(cos = rX[1]; cos > rX[0]; cos -= inc) {
            CreateObject(1598, cos, rY[0], fZ, 0, 0, 0);
            CreateObject(1598, cos, rY[1], fZ, 0, 0, 0);
        }
        for(sin = rY[1]; sin > rY[0]; sin -= inc) {
            CreateObject(1598, rX[0], sin, fZ, 0, 0, 0);
            CreateObject(1598, rX[1], sin, fZ, 0, 0, 0);
        }
        return true;
    }
    return false;
}



Re: AW: Re: Useful Functions - Patrick_ - 24.01.2014

@Nero_3D - Great function! anyways I just added if they use streamer they are going to use CreateDynamicObject or else they will use CreateObject

pawn Code:
stock CreateSquareObjectShape(playerid, Float: fDiag, objectsPerLine = 10)
{
    new
        Float: rX[2],
        Float: rY[2],
        Float: fZ
    ;
    if(GetPlayerPos(playerid, fX[0], fY[0], fZ))
    {
        new
            Float: inc = (fDiag * 0.707 / objectsPerLine),
            Float: cos = (fDiag * floatcos(45, degrees)),
            Float: sin = (fDiag * floatsin(45, degrees))
        ;

        rX[1] = rX[0] + cos;
        rY[1] = rY[0] + sin;
       
        rX[0] -= cos;
        rY[0] -= sin;

        for(cos = rX[1]; cos > rX[0]; cos -= inc)
        {
            #if defined CreateDynamicObject
                CreateDynamicObject(1598, cos, rY[0], fZ, 0, 0, 0);
                CreateDynamicObject(1598, cos, rY[1], fZ, 0, 0, 0);
            #else
                CreateObject(1598, cos, rY[0], fZ, 0, 0, 0);
                CreateObject(1598, cos, rY[1], fZ, 0, 0, 0);
            #endif
        }
        for(sin = rY[1]; sin > rY[0]; sin -= inc)
        {
            #if defined CreateDynamicObject
                CreateDynamicObject(1598, rX[0], sin, fZ, 0, 0, 0);
                CreateDynamicObject(1598, rX[1], sin, fZ, 0, 0, 0);
            #else
                CreateObject(1598, rX[0], sin, fZ, 0, 0, 0);
                CreateObject(1598, rX[1], sin, fZ, 0, 0, 0);
            #endif
        }
        return true;
    }
    return false;
}



Re: Useful Functions - d711728 - 25.01.2014

Text encoding and decoding is a function?
Thank you very much in advance.


Re: Useful Functions - Emmet_ - 25.01.2014

Quote:
Originally Posted by d711728
View Post
Text encoding and decoding is a function?
Thank you very much in advance.
There's "uuencode" and "uudecode".

It depends on what type of encoding you wish to use.


Re: Useful Functions - d711728 - 25.01.2014

For me, the function is somehow going to ask here is why.
Located in a function which I long strings encoding and decoding is a function


Re: Useful Functions - Emmet_ - 26.01.2014

Here's a function that moves an object in the specified time (in milliseconds):

pawn Code:
stock MoveObjectEx(objectid, time, Float:x, Float:y, Float:z, Float:rx = -1000.0, Float:ry = -1000.0, Float:rz = -1000.0)
{
    if (!IsValidObject(objectid))
        return 0;

    static
        Float:fX,
        Float:fY,
        Float:fZ,
        Float:fDist;
       
    GetObjectPos(objectid, fX, fY, fZ);
    fDist = floatsqroot(((fX - x) * (fX - x)) + ((fY - y) * (fY - y)) + ((fZ - z) * (fZ - z)));

    MoveObject(objectid, x, y, z, floatdiv(floatsqroot(x + y + z), time / floatmul(fDist, 15)), rx, ry, rz);
    return 1;
}
General example:

pawn Code:
MoveObjectEx(gGateID, 1000, gMoveX, gMoveY, gMoveZ);



Re: Useful Functions - Emmet_ - 28.01.2014

Here is my INI_ReadData function:

pawn Code:
stock INI_ReadData(filename[], specifiers[], {Float,_}:...)
{
    static
        identifiers[32],
        argument,
        key[32]
    ;
    strcpy(identifiers, specifiers);

    for (new i = 0, len = strlen(identifiers); i != len; i ++)
    {
        if (identifiers[i] != '[')
            continue;

        strdel(identifiers, i, strfind(identifiers, "]", false, i) + 1);
    }
    if ((numargs() - 2) / 2 != strlen(identifiers))
    {
        return printf("** Warning: %d arguments, expecting %d.", numargs() - 2, strlen(identifiers));
    }
    inline ReadData(name[], value[])
    {
        for (new i = 2, args = numargs(); i != args; i += 2)
        {
            va_getstring(key, i);
           
            if (strcmp(name, key, true) != 0)
                continue;

            switch (identifiers[(argument = i / 2 - 1)])
            {        
                case 'f':
                    setarg(i + 1, 0, _:floatstr(value));
                       
                case 's':
                {
                    new size = strval(specifiers[strfind(specifiers, "[", true, argument) + 1]);

                    for (new j = 0, len = strlen(value); j < size && j < len; j ++) {
                        setarg(i + 1, j, value[j]);
                    }
                }
                default: {
                    setarg(i + 1, 0, strval(value));
                }
            }
            break;
        }
    }
    INI_ParseFile(filename, "ReadData");
    return 1;
}
Usage:

Code:
// test.ini

Money = 10000
Health = 50.0
Score = 2176
pawn Code:
new
    money,
    score,
    Float:health;

INI_ReadData("test.ini", "fdd", "Health", health, "Score", score, "Money", money);
It also works for strings, too (but you need to specify the string size).

pawn Code:
new
    money,
    score,
    Float:health,
    Float:armor,
    str[32];

INI_ReadData("test.ini", "dds[32]ff", "Money", money, "Score", score, "Motto", str, "Health", health, "Armor", armor);
Variables and specifiers can be in any order.


Re: Useful Functions - Lordzy - 05.02.2014

I've once posted a function called DamagePlayer, but here's the same function which is improved!
pawn Code:
stock DamagePlayer(playerid, Float:damage)
{
    static
        Float:temp_hp,
        Float:temp_ar;
    GetPlayerHealth(playerid, temp_hp);
    GetPlayerArmour(playerid, temp_ar);
    if(temp_ar > 0)
    {
        if(damage > temp_ar)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, floatsub(damage, temp_ar));
        }
        else if(damage < temp_ar)
        {
            SetPlayerArmour(playerid, floatsub(temp_ar,damage));
        }
    }
    else if(temp_ar <= 0)
    {
        if(damage > temp_hp)
        {
            SetPlayerHealth(playerid, 0.0);
        }
        else if(damage < temp_hp)
        {
            SetPlayerHealth(playerid, floatsub(temp_hp,damage));
        }
    }
    return 1;
}
This functions damages the player just like in-game normal damage. It reduces the armour in case if player got that and if the damage is higher than the armour, it reduces health too.


Re: Useful Functions - RyDeR` - 05.02.2014

That' not a function, but it can aswell be shortened:
pawn Code:
stock damagePlayer(playerid, Float: fDamage) {
    new
        Float: fHealth,
        Float: fArmour
    ;
    GetPlayerHealth(playerid, fHealth);
    GetPlayerArmour(playerid, fArmour);
   
    fHealth += fArmour - fDamage;
   
    SetPlayerArmour(playerid, (fHealth > 100.0) ? (fHealth - 100.0) : (0.0));
    SetPlayerHealth(playerid, (fHealth > 100.0) ? (100.0) : (fHealth));
}
I think this should do pretty much the same.


AW: Useful Functions - BigETI - 10.02.2014

Test for, if a floating point number is not a real number (including NaN and infinity):
pawn Code:
#define isnrn(%0) (((_:(%0))&0x7F800000) == 0x7F800000)
Example:
pawn Code:
//...
if(isnrn(1.0/0.0)) printf("ERROR!");
if(isnrn(floatsqroot(-1.0))) printf("ERROR!");
//...



Re: Useful Functions - Guest4390857394857 - 29.04.2014

Use foreach


Re: Useful Functions - Meyokie - 05.05.2014

Originally Posted by =>Sandra<=
SendClientMessageToAllEx(exception, color, const message[])
This function sends a message to all players, except for 1 player. (exception)

Code:
stock SendClientMessageToAllEx(exception, color, const message[])
{
for(new i; i<MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(i != exeption)
{
SendClientMessage(i, color, message);
}
}
}
}
Need a 'c' in "if(i != ex©eption)"

EDIT: Please delete this post when noted.


Re: Useful Functions - RajatPawar - 05.05.2014

Quote:
Originally Posted by Meyokie
Посмотреть сообщение
Originally Posted by =>Sandra<=
SendClientMessageToAllEx(exception, color, const message[])
This function sends a message to all players, except for 1 player. (exception)

Code:
stock SendClientMessageToAllEx(exception, color, const message[])
{
for(new i; i<MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(i != exeption)
{
SendClientMessage(i, color, message);
}
}
}
}
Need a 'c' in "if(i != ex©eption)"

EDIT: Please delete this post when noted.
Literally, it is the first function to be posted on this topic. No need to repost it, you only get reputation when you genuinely help people.


Re: Useful Functions - Lordzy - 15.05.2014

pawn Код:
stock SetPlayerRandomPos(playerid, Float:position[][3], size = sizeof(position))
{
    new rand = random(size);
    SetPlayerPos(playerid, position[rand][0], position[rand][1], position[rand][2]);
    return rand;
}

stock SetVehicleRandomPos(vehicleid, Float:position[][3], size = sizeof(position))
{
    new rand = random(size);
    SetVehiclePos(vehicleid, position[rand][0], position[rand][1], position[rand][2]);
    return rand;
}

//Returning the randomized value because it can be used for anything else later.
About : Using these, random position for a player or vehicle can be set easily by just using the array which contains the position data over the function.

An example of using it:
pawn Код:
new
    Float:RandomPositions[5][3] = { //5 random XYZ positions.
    {1614.7770,1162.2877,14.2188},
    {2515.5234,-1675.7893,13.8168},
    {-1723.2974,-89.6194,3.5547},
    {-1980.5894,656.0847,46.5683},
    {-1502.7856,824.2550,7.1875}
};

ExampleOnSomething(playerid, vehicleid)
{
    SetPlayerRandomPos(playerid, RandomPositions);
    SetVehicleRandomPos(vehicleid, RandomPositions);
    return 1;
}



Re: Useful Functions - Vince - 15.05.2014

pawn Код:
SetPlayerMarkerVisibility(playerid, alpha = 0xFF)
    return (GetPlayerColor(playerid) & ~0xFF) | clamp(alpha, 0x00, 0xFF);



Respuesta: Useful Functions - JustBored - 15.05.2014

IsNumberOdd:

It checks whether a number is odd or not.

pawn Код:
#define IsNumberOdd(%0) ((%0&0x01):(false)?(true))
It used the parity bit http://en.wikipedia.org/wiki/Parity_bit

Credits to DesignMcCry that posted it on the spanish section.


Respuesta: Useful Functions - Swedky - 15.05.2014

pawn Код:
#include <a_samp>

new LastWeaponPlayer[MAX_PLAYERS];
new MoneyPlayer[MAX_PLAYERS];
new WeaponPlayer[MAX_PLAYERS][13];


#define GivePlayerWeapon@(%1,%2,%3) WeaponPlayer[%1][GetWeaponSlot(%2)] = %2, GivePlayerWeapon(%1, %2, %3)
#define GivePlayerMoney@(%1,%2) MoneyPlayer[%1] = (GetPlayerMoney(%1)+%2), GivePlayerMoney(%1, %2)


public OnPlayerUpdate(playerid)
{
    if(GetPlayerWeapon(playerid) != LastWeaponPlayer[playerid])
    {
        CallLocalFunction("OnPlayerChangeWeapon", "dii", playerid, GetPlayerWeapon(playerid), LastWeaponPlayer[playerid]);
        LastWeaponPlayer[playerid] = GetPlayerWeapon(playerid);
    }
    if(GetPlayerMoney(playerid) > MoneyPlayer[playerid]) CallLocalFunction("OnPlayerMoneyHack", "dii", playerid, GetPlayerMoney(playerid), MoneyPlayer[playerid]);
    return 1;
}

forward OnPlayerChangeWeapon(playerid, newweapon, oldweapon);
public OnPlayerChangeWeapon(playerid, newweapon, oldweapon)
{
    static data[2];
    GetPlayerWeaponData(playerid, GetWeaponSlot(newweapon), data[0], data[1]);

    if(data[0] != WeaponPlayer[playerid][GetWeaponSlot(newweapon)])
    {
        CallLocalFunction("OnPlayerWeaponHack", "dii", playerid, data[0], WeaponPlayer[playerid][GetWeaponSlot(newweapon)]);
    }
    return 1;
}


stock GetWeaponSlot(weaponid)
{
    switch(weaponid)
    {
        case 0, 1: return 0;
        case 2 .. 9: return 1;
        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 16 .. 19, 39: return 8;
        case 41 .. 43: return 9;
        case 10 .. 15: return 10;
        case 44 .. 46: return 11;
        case 40: return 12;
        default: return -1;
    }
    return -1;
}

forward OnPlayerMoneyHack(playerid, hackmoney, oldmoney);
forward OnPlayerWeaponHack(playerid, hackweapon, realweapon);




Re: Useful Functions - Hwang - 17.05.2014

IsPlayerPingKick

pawn Code:
stock IsPlayerPingKick(playerid,maxping){
if(GetPlayerPing(playerid) > maxping){
Kick(playerid);
}return true;}
Example:
pawn Code:
IsPlayerPingKick(playerid, 300);
> 300 Ping = Kick, public: OnPlayerUpdate


Re: Useful Functions - Patrick - 17.05.2014

Quote:
Originally Posted by Hwang
View Post
IsPlayerPingKick

pawn Code:
stock IsPlayerPingKick(playerid,maxping){
new
    pngString[256];
if(GetPlayerPing(playerid) > maxping){
format(pngString,256,"{ffffff}Server closed the connection, Your Ping: {00FF00}%d {ffffff}- Max Ping {00FF00}%d",GetPlayerPing(playerid),maxping);
SendClientMessage(playerid,-1,pngString);
Kick(playerid);
}return true;}
Example:
pawn Code:
IsPlayerPingKick(playerid, 300);
> 300 Ping = Kick, public: OnPlayerUpdate
This will kick a player who just connected because the ping you get on OnPlayerConnect is 6355, so this will kick a player who just connected. I suggest you check if the player is spawned.