Useful Functions

I believe that should be

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

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;
}
Reply

@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;
}
Reply

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

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.
Reply

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
Reply

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);
Reply

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.
Reply

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.
Reply

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.
Reply

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!");
//...
Reply

Use foreach
Reply

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.
Reply

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.
Reply

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;
}
Reply

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

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.
Reply

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);

  • Notes:
    • To avoid problems, you must use this:
    pawn Код:
    public OnPlayerSpawn(playerid)
    {
        // Here if you assign weapon...
        LastWeaponPlayer[playerid] = lastweapon; // 'lastweapon' Would refer to the last one ID of weapon that we assign in this callback.
        return 1;
    }
    • In the parts where it assigns weapon or money to the player, you must replace this function and add a '@' at the end of the name of the function.
    • Only use 'GivePlayerMoney@' if it wants to increase the money of the player.
Reply

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
Reply

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.
Reply


Forum Jump:


Users browsing this thread: 10 Guest(s)