Useful Functions

Quote:
Originally Posted by [03]Garsino
Посмотреть сообщение
Why not use the macros posted in this topic? 1 line for each function :/
Functions are more neat though I'm still not aware if defines are faster in code performance.
Reply

Here's a tiny function in case someone is interested in exploring yellow markers all over San Andreas!!

pawn Код:
CMD:showmeyellowmarkers(playerid, params[])
{
    YellowMarkersForPlayer(playerid, 0);
    return 1;
}

forward YellowMarkersForPlayer(playerid, pos);
public YellowMarkersForPlayer(playerid, pos)
{
    switch(pos)
    {
        case 0, 2, 4:
        {
            SetPlayerPos(playerid, 2449.17, 689.83, 10.97);
        }
        case 1, 3:
        {
            SetPlayerPos(playerid, 2543.15, 1025.16, 10.32);
        }
        case 5:
        {
            SendClientMessage(playerid, 0xAA3333AA, "Go and explore!");
        }
    }
    if(pos < 5)
    {
        SetTimerEx("YellowMarkersForPlayer", 1500, 0, "dd", playerid, ++pos);
    }
}
Yellow markers will randomly appear, disappear and reappear in random places. Screenies:
Reply

intLen

Returns the length of an integer:
pawn Код:
stock intLen(iNum)
    return floatround(floatlog(floatabs(iNum) + 1), floatround_ceil);
Examples:
pawn Код:
printf("%d", intLen(235)); // Prints 3
printf("%d", intLen(123456)); // Prints 6
intCat

Returns a value of two concatenated integers:
pawn Код:
stock intCat(iNum1, iNum2)
    return iNum1 * floatround(floatpower(10.0, intLen(iNum2))) + iNum2;
or in recursive way (most probably slower):
pawn Код:
stock intCat(iNum1, iNum2)
{
    if(iNum2)
        iNum1 = intCat(iNum1 * 10, iNum2 / 10);
    return iNum1 - (iNum2 / 10) + iNum2;
}
Examples:
pawn Код:
printf("%d", intCat(3, 1456)); // Prints 31456
printf("%d", intCat(54654, 9984)); // Prints 546549984
Reply

Quote:
Originally Posted by YJIET
Посмотреть сообщение
Here's a tiny function in case someone is interested in exploring yellow markers all over San Andreas!!

pawn Код:
CMD:showmeyellowmarkers(playerid, params[])
{
    YellowMarkersForPlayer(playerid, 0);
    return 1;
}

forward YellowMarkersForPlayer(playerid, pos);
public YellowMarkersForPlayer(playerid, pos)
{
    switch(pos)
    {
        case 0, 2, 4:
        {
            SetPlayerPos(playerid, 2449.17, 689.83, 10.97);
        }
        case 1, 3:
        {
            SetPlayerPos(playerid, 2543.15, 1025.16, 10.32);
        }
        case 5:
        {
            SendClientMessage(playerid, 0xAA3333AA, "Go and explore!");
        }
    }
    if(pos < 5)
    {
        SetTimerEx("YellowMarkersForPlayer", 1500, 0, "dd", playerid, ++pos);
    }
}
Yellow markers will randomly appear, disappear and reappear in random places. Screenies:
cool beans
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Have you tried this on "10", "100", "1000" etc?
Didn't think about that. Corrected, thanks.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
That won't work either - you do need "+ 1", but not there...
It is working fine but not for 0. I deleted it anyway and changed the code now.
Reply

Quote:
Originally Posted by YJIET
Посмотреть сообщение
Here's a tiny function in case someone is interested in exploring yellow markers all over San Andreas!!

pawn Код:
CMD:showmeyellowmarkers(playerid, params[])
{
    YellowMarkersForPlayer(playerid, 0);
    return 1;
}

forward YellowMarkersForPlayer(playerid, pos);
public YellowMarkersForPlayer(playerid, pos)
{
    switch(pos)
    {
        case 0, 2, 4:
        {
            SetPlayerPos(playerid, 2449.17, 689.83, 10.97);
        }
        case 1, 3:
        {
            SetPlayerPos(playerid, 2543.15, 1025.16, 10.32);
        }
        case 5:
        {
            SendClientMessage(playerid, 0xAA3333AA, "Go and explore!");
        }
    }
    if(pos < 5)
    {
        SetTimerEx("YellowMarkersForPlayer", 1500, 0, "dd", playerid, ++pos);
    }
}
Yellow markers will randomly appear, disappear and reappear in random places. Screenies:
https://sampforum.blast.hk/showthread.php?tid=180389
Reply

Using y_va from YSI. This function returns the formatted string!

pawn Код:
stock sprintf(szFormat[], va_args<>) {
    static
        s_szBuffer[512]
    ;
   
    va_format(s_szBuffer, sizeof (s_szBuffer), szFormat, va_start<1>);

    return s_szBuffer;
}
Example:
pawn Код:
SendRconCommand(sprintf("weather %d", weather));
Another thing I just made is strcatf, it appends a formatted string to a string, without using a buffer in-between!

pawn Код:
stock strcatf(szDest[], szFormat[], iMaxLength = sizeof(szDest), va_args<>) {
    new
        iLength = strlen(szDest)
    ;
   
    va_format(szDest[iLength], iMaxLength - iLength, szFormat, va_start<3>);
}
Reply

@Slice so it'll change the wather ID as you wish?
Reply

Well, yea. The weather thing was just an example, though.
Reply

NEW FUNCTION
pawn Код:
stock Float: Pythagoras2(Float: val1, Float: val2, bool: shortside = false)
    return shortside == true ? (floatsqroot(floatsub(floatmul(val1, val1), floatmul(val2, val2)))) :  (floatsqroot(floatadd(floatmul(val1, val1), floatmul(val2, val2))));
OLD FUNCTION
pawn Код:
stock Pythagoras(Float: val1, Float: val2, bool: shortside = false)
{
        new
                Float: cal
        ;
        if(shortside) cal = ((val1 * val1) - (val2 * val2));
        else cal = ((val1 * val1) + (val2 * val2));
        cal = floatsqroot(cal);
        return cal;
}

Example 1


pawn Код:
new Float: X = Pythagoras(90, 20, false);
printf("The hypotenuse of this is %f", X);
Untested this little example though should work!

Example 1


pawn Код:
new Float: X = Pythagoras(56, 20, true);
printf("The height of this is %f", X);
Untested this little example though should work!

If I did something wrong, tell me

PS.

We're on
pawn Код:
new page[256];
Reply

Quote:
Originally Posted by OKStyle
Посмотреть сообщение
What does that have to do with my post? My function triggers some glitch in GTA and enexes start to appear.
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
NEW FUNCTION
pawn Код:
stock Float: Pythagoras2(Float: val1, Float: val2, bool: shortside = false)
    return shortside == true ? (floatsqroot(floatsub(floatmul(val1, val1), floatmul(val2, val2)))) :  (floatsqroot(floatadd(floatmul(val1, val1), floatmul(val2, val2))));
OLD FUNCTION
pawn Код:
stock Pythagoras(Float: val1, Float: val2, bool: shortside = false)
{
        new
                Float: cal
        ;
        if(shortside) cal = ((val1 * val1) - (val2 * val2));
        else cal = ((val1 * val1) + (val2 * val2));
        cal = floatsqroot(cal);
        return cal;
}

Example 1


pawn Код:
new Float: X = Pythagoras(90, 20, false);
printf("The hypotenuse of this is %f", X);
Untested this little example though should work!

Example 1


pawn Код:
new Float: X = Pythagoras(56, 20, true);
printf("The height of this is %f", X);
Untested this little example though should work!

If I did something wrong, tell me

PS.

We're on
pawn Код:
new page[256];
hmm,

Pytahoras says:

if we put in some numbers:

a = 6
b = 3
------
c=?

c=15 cm


now your formula:

cal = ((val1 * val1) - (val2 * val2));

val1 = 6
val2 = 3
--------
cal=?
cal=((val1 * val1) - (val2 * val2))
cal=27 cm

we get difreent result, something is wrong :S
Reply

Quote:
Originally Posted by Speed
Посмотреть сообщение
hmm,

Pytahoras says:

if we put in some numbers:

a = 6
b = 3
------
c=?

c=15 cm
What the hell are you doing?
The end result of that will be square root of 45, which is by far from being 15.
The form you're using is only one of many. Lorenc_ is using this:
Reply

i did not see that lorenc put floatsqroot(cal); sorry i fajl :S
Reply

intIns

Inserts an integer to another integer and returns the final value:
pawn Код:
stock intIns(iNum1, iNum2, iPos)
{
    static
        iLength[2]
    ;
    iLength[0] = intLen(iNum1);
    iLength[1] = intLen(iNum2);
   
    if((0 <= (iLength[0] + iLength[1]) <= 9) && (0 <= (iLength[0] -= iPos) <= 9))
    {
        iLength[0] = floatround(floatpower(10.0, iLength[0]));
        iLength[1] = floatround(floatpower(10.0, iLength[1]));
           
        return ((((iNum1 / iLength[0]) * iLength[1]) + iNum2) * iLength[0]) + (iNum1 % iLength[0]);
    }
    return 0;
}
Examples:
pawn Код:
printf("%d", intIns(1245, 3, 2)); // Prints 12345
printf("%d", intIns(666666, 999, 3)); // Prints 666999666
NOTE: You also need intLen which I posted here.
Reply

@Lorenc_:

I meant something more like this:
pawn Код:
stock SetObjectFacePoint(iObjectID, Float: fX, Float: fY, Float: fOffset = 0.0)
{
    new
        Float: fOX,
        Float: fOY,
        Float: fRZ
    ;
    if(GetObjectPos(iObjectID, fOX, fOY, fRZ))
    {
        fRZ = atan2(fY - fOY, fX - fOX) - 90.0;
       
        GetObjectRot(iObjectID, fX, fY, fOX);
        SetObjectRot(iObjectID, fX, fY, fRZ + fOffset);
    }
}
Reply

here is a little function for MySQL
pawn Код:
stock mysql_get_name(playerid)
{
    new mysqlname[MAX_PLAYER_NAME];
    mysql_real_escape_string(GetName(playerid), mysqlname);
    return mysqlname;
}
It just make easier than doing mysql_real_escape_string etc. also you will need GetName function !
pawn Код:
stock GetName(playerid)
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
    return pName;
}
Reply

Quote:
Originally Posted by System64
Посмотреть сообщение
here is a little function for MySQL
pawn Код:
stock mysql_get_name(playerid)
{
    new mysqlname[MAX_PLAYER_NAME];
    mysql_real_escape_string(GetName(playerid), mysqlname);
    return mysqlname;
}
It just make easier than doing mysql_real_escape_string etc. also you will need GetName function !
pawn Код:
stock GetName(playerid)
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
    return pName;
}
I can't say it's not useful, but there's no point in escaping characters from the player's name.
On the other hand, if you're using BlueG's MySQL plugin, mysql_format can be used.
Reply

Quote:
Originally Posted by [NoV]LaZ
Посмотреть сообщение
I can't say it's not useful, but there's no point in escaping characters from the player's name.
On the other hand, if you're using BlueG's MySQL plugin, mysql_format can be used.
Why not?
I think that BlueG is G-Stylezz?
Reply


Forum Jump:


Users browsing this thread: 39 Guest(s)