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 - iPLEOMAX - 05.04.2012

pawn Код:
stock randomColor(Alpha = -1) //iPLEOMAX
return ((random(256) * 16777216) + (random(256) * 65536) + (random(256) * 256) + ((Alpha == -1) ? random(256) : Alpha));

//USAGE:

SendClientMessage(playerid, randomColor(255), "What color is this?");
And something really awkward.. 800 different colors (RGB Wise, not your vision)!
http://pastebin.com/6TD6sXMN

EDIT: When I used [pawn][/pawn] tags for the color variable.. the page never loaded for me.. WTF.


Re: Useful Functions - iPLEOMAX - 05.04.2012

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Anyone else getting an error?
I had to post this from page 314

PM me if the topic needs spamming to get it to the next page (This happened with the Script Request once, someone spammed it and it was fixed)
Guess I was the culprit..

EDIT: I wonder what "EXACTLY" causes this.. xD

And btw, a warning I never got before:
pawn Код:
colors.pwn(100) : warning 232: output file is written, but with compact encoding disabled
o_O


Re: Useful Functions - iPLEOMAX - 06.04.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
Do you have examples of the colours? Or names for them? If you want loads of colours, you might want to check out this topic:

https://sampforum.blast.hk/showthread.php?tid=319606

(Sorry, another plug for YSI, but if you WILL post things that have been done before... )
Ah, didn't see that before


Re: Useful Functions - smeti - 06.04.2012

pawn Код:
stock
    rgbatoabgr(color)
{
    new
        abgr[1],
        rgba[1];
    rgba[0] = color;
    abgr{3} = rgba{0};
    abgr{2} = rgba{1};
    abgr{1} = rgba{2};
    abgr{0} = rgba{3};
    return abgr[0];
}
0.3e RC6
pawn Код:
#define orange                  ( 0xFF9900AA )
public OnPlayerCommandText(playerid,cmdtext[])
{
    if(!strcmp(cmdtext,"/mycommand",true))
    {
        new Float:X, Float:Y, Float:Z;
        new myobject;
        GetPlayerPos(playerid, X, Y, Z);
        myobject = CreateObject(19371, X, Y, Z+0.5, 0.0, 0.0, 0.0, 300.0);
        SetObjectMaterial(myobject, 0, 19341, "egg_texts", "easter_egg01", rgbatoabgr(orange));
        //Replaces the texture of our object with the texture of object 19341
        return 1;
    }
    return 0;
}



Re: Useful Functions - Kaczmi - 07.04.2012

pawn Код:
stock
        SendFormattedMessage(playerid,color,msg[],{Float,_}:...)
{
    if(!(IsPlayerConnected(playerid))) return 0;
    if(!(msg[0]) || (msg[0] == 1) && (msg[1] == 0)) return 0;
    else
    {
        new
            pos,
            delim = ' ',
            num = numargs(),
            start = 3,
            strr[128]
        ;

        strmid(strr,msg,0,128);
        while((pos = strfind(strr,"%",true)) != -1)
        {
            if(start > num) break;
            new
                chars,
                count = pos+1,
                changed_str[128]
            ;
            while((chars = strr[count]) && chars != delim)
            {
                switch(chars)
                {
                    case 'd','D': // integer
                    {
                        new
                            value = getarg(start,0)
                        ;
                        strdel(strr,pos,pos+2);
                        format(changed_str,sizeof(changed_str),"%d",value);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    case 's','S':
                    {
                        new
                            index,
                            test
                        ;

                        for( ; ; )
                        {
                            test = getarg(start,index);
                            if(test == 0) break;
                            changed_str[index] = test;
                            index++;
                        }
                        strdel(strr,pos,pos+2);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    case 'f','F':
                    {
                        new
                            value = getarg(start,0)
                        ;
                        strdel(strr,pos,pos+2);
                        format(changed_str,sizeof(changed_str),"%0.3f",value);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    default: // nalezen neznбmэ charakter
                    {
                        strdel(strr,pos,pos+2);
                        break;
                    }
                }
            }

        }
        SendClientMessage(playerid,color,strr);
    }
    return 1;
}

stock
        SendFormattedMessageToAll(color,msg[],{Float,_}:...)
{
    if(!(msg[0]) || (msg[0] == 1) && (msg[1] == 0)) return 0;
    else
    {
        new
            pos,
            delim = ' ',
            num = numargs(),
            start = 2,
            strr[128]
        ;

        strmid(strr,msg,0,128);
        while((pos = strfind(strr,"%",true)) != -1)
        {
            if(start > num) break;
            new
                chars,
                count = pos+1,
                changed_str[128]
            ;
            while((chars = strr[count]) && chars != delim)
            {
                switch(chars)
                {
                    case 'd','D': // integer
                    {
                        new
                            value = getarg(start,0)
                        ;
                        strdel(strr,pos,pos+2);
                        format(changed_str,sizeof(changed_str),"%d",value);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    case 's','S':
                    {
                        new
                            index,
                            test
                        ;

                        for( ; ; )
                        {
                            test = getarg(start,index);
                            if(test == 0) break;
                            changed_str[index] = test;
                            index++;
                        }
                        strdel(strr,pos,pos+2);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    case 'f','F':
                    {
                        new
                            value = getarg(start,0)
                        ;
                        strdel(strr,pos,pos+2);
                        format(changed_str,sizeof(changed_str),"%0.3f",value);
                        strins(strr,changed_str,pos,128);
                        start++;
                        break;
                    }
                    default: // nalezen neznбmэ charakter
                    {
                        strdel(strr,pos,pos+2);
                        break;
                    }
                }
            }

        }
        SendClientMessageToAll(color,strr);
    }
    return 1;
}
I don't say it's fast but i say: It's my!


Re: Useful Functions - organe. - 07.04.2012

pawn Код:
#define LOOK 15.0

stock CreatePickupEx(pickupID,pickupStyle,Float:pX,Float:pY,Float:pZ,pVW,text[])
{
    CreatePickup(pickupID,pickupStyle,pX,pY,pZ,pVW);
    Create3DTextLabel(text,0xFFBF00FF,pX,pY,pZ,LOOK,0,0);
}
Use example:
pawn Код:
CreatePickupEx(1239, 2, 1172.1051, -1321.0652, 15.3980, -1,"Pickup x,y,z: 1172.1051, -1321.0652, 15.3980");



Re: Useful Functions - KoczkaHUN - 07.04.2012

Quote:
Originally Posted by Kaczmi
Посмотреть сообщение
pawn Код:
stock
        SendFormattedMessage(playerid,color,msg[],{Float,_}:...)
I don't say it's fast but i say: It's my!
Quote:
Originally Posted by KoczkaHUN
Посмотреть сообщение
pawn Код:
new szSFM[144]; // or 128 for that matter
#define SendFormatMessage(%0,%1,%2) \
format(szSFM, 144, %2), SendClientMessage(%0, %1, szSFM)
Isn't it much cooler?


Re: Useful Functions - Luka P. - 10.04.2012

about this "negativepositive" funcition, why don't you just multiply it by -1?
like

#define negativepositive(%0) ((%0)*(-1))


Re: Useful Functions - Slice - 10.04.2012

Or just put a minus sign in front of whatever it is you want negative..


Re: Useful Functions - RyDeR` - 10.04.2012

Exactly! Something I was trying to explain in one of my previous posts.


Re: Useful Functions - clavador - 10.04.2012

I discover something recently, don't know if it's been posted, but some people complain that when you attach the camera to an object, the player can move it freely. To avoid this, first set the camera-look-at position to the angle you wish and then attach the object. The camera will continuously aim at the angle you set from the position attached and the player won't be able to move it with the mouse.


Re: Useful Functions - Skream - 12.04.2012

pawn Код:
Call_GetPlayerCity(playerid)
{
    new ip[16];
   
    GetPlayerIp(playerid, ip, sizeof(ip));
   
    new URL[200] = "geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=";
   
    strcat(URL, ip);
   
    HTTP(playerid, HTTP_GET, URL, "", "MyHttpResponse");
}

forward MyHttpResponse(index, response_code, data[]);
public MyHttpResponse(index, response_code, data[])
{
    if(response_code != 200) return;

    new start = strfind(data, "\"city\" content=");
    start += 16;

    strdel(data, 0, start);

    new end = strfind(data, "\"");

    data[end] = EOS;
   
    print(data); // City here...
}



Re: Useful Functions - iggy1 - 12.04.2012

Code was bugged...


Re: Useful Functions - Slice - 12.04.2012

No need to use a buffer there. strval will read until it reaches anything else than digits (a dot in this case).

Ex.:
pawn Код:
// Modified version from YSI/y_utils
stock GetIPFromString(const ip[])
{
    new
        ipv = strval(ip) << 24,
        pos = 0;
    while (pos < 15 && ip[pos++] != '.') {}
    ipv += strval(ip[pos]) << 16;
    while (pos < 15 && ip[pos++] != '.') {}
    ipv += strval(ip[pos]) << 8;
    while (pos < 15 && ip[pos++] != '.') {}
    ipv += strval(ip[pos]);
    return ipv;
}



Re: Useful Functions - iggy1 - 12.04.2012

@Slice Nice ty i didn't know that's how strval works. Tested both versions mine is slightly faster, but i suppose that doesn't matter with this type of func.

Код:
1000000 iter  = IpToInt - 3215ms
1000000 iter  = GetIPFromString - 3333ms
@ southclaw i was using it for "AKA" script. Better looking up a number in a DB than string.


Re: Useful Functions - rjjj - 12.04.2012

Quote:
Originally Posted by Kar
Посмотреть сообщение
pawn Код:
stock ShiftRGBAToABGR(&color)
{
    new r, g, b, a;
    r = (color >>> 24);
    g = (color >>> 16 & 0xFF);
    b = (color >>> 8 & 0xFF);
    a = (color  & 0xFF);
    color = (a & 0xFF) | ((b & 0xFF) << 8) | ((g & 0xFF) << 16) | (r << 24);
    return color;
}
Quote:
Originally Posted by ******
Посмотреть сообщение
From YSI y_cell:

pawn Код:
stock Cell_ReverseBytes(data)
{
    // Swap all bytes.
    return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
}

Or just use the swapchars function of core.inc .



I hope that i have helped .


Re: Useful Functions - iggy1 - 12.04.2012

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
Yours might be faster, but it's also wrong! The number "255255255255", which is what your version calculates, is different to "255.255.255.255", which is actually the number "0xFFFFFFFF" or "4294967295".
Guess i'll have to switch to your version then. Thanks for the heads up.


Re: Useful Functions - RyDeR` - 12.04.2012

This is another possibility by me (used in my FloodControl.inc):
pawn Код:
stock IpToInt(const szIP[]) {
    new
        aiBytes[1],
        iPos = 0
    ;
    aiBytes{0} = strval(szIP[iPos]);
    while(szIP[iPos] != EOS && szIP[iPos++] != '.') {}
    aiBytes{1} = strval(szIP[iPos]);
    while(szIP[iPos] != EOS && szIP[iPos++] != '.') {}
    aiBytes{2} = strval(szIP[iPos]);
    while(szIP[iPos] != EOS && szIP[iPos++] != '.') {}
    aiBytes{3} = strval(szIP[iPos]);
   
    return aiBytes[0];
}
Not sure about the performance, but I guess it's slightly faster and if not, the same.

EDIT:
pawn Код:
stock shuffleString(szStr[]) {
    for(new i = 0, iLen = strlen(szStr), iRand = 0, iTemp = 0; i < iLen; ++i) {
        iRand = i + (random(0) % (iLen - i));
        iTemp = szStr[i], szStr[i] = szStr[iRand], szStr[iRand] = iTemp;
    }
}
Small function that randomly shuffles a string. For example:
pawn Код:
new
    szStr[] = "RyDeR`"
;
shuffleString(szStr);
printf("%s", szStr); // Possible output: "yReR`D"



Re: Useful Functions - iPLEOMAX - 15.04.2012

Creates a box at point X,Y and extends as Width and Height.

pawn Код:
forward Text:TextDrawCreateBox(Float:X, Float:Y, Float:Width, Float:Height, Color);
Text:TextDrawCreateBox(Float:X, Float:Y, Float:Width, Float:Height, Color) //pleo
{
    new Text:TD_Box =
    TextDrawCreate((X + Width), Y, "_");
    TextDrawUseBox(TD_Box, 1);
    TextDrawLetterSize(TD_Box, 0.0, (Height/9.6));
    TextDrawTextSize(TD_Box, X, 0.0);
    TextDrawBoxColor(TD_Box, Color);

    return TD_Box;
}
EDIT: Thnx Southclaw


AW: Useful Functions - olaf137 - 16.04.2012

These functions replace the modified texture of an object ( Set(Player)ObjectMaterial ) with the original one.
Код:
stock ResetObjectMaterial(objectid, materialindex)
{
	SetObjectMaterial(objectid, materialindex, 19341, "invalid", "invalid", 0);
}
stock ResetPlayerObjectMaterial(playerid, objectid, materialindex)
{
	SetPlayerObjectMaterial(playerid, objectid, materialindex, 19341, "invalid", "invalid", 0);
}
To use this code, you need 0.3e RC6 or higher.