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 - Norn - 10.09.2010

sqlite_highest_field.

So basically SQLite has no function similar to the mysql function mysql_insert_id(), sqlite_highest_field will emulate the function returning the latest ID for example which we would assume would be the highest ID.

Example Usage:
pawn Код:
format(query, sizeof(query), "Your character ID = %d.", sqlite_highest_field(USERDB, "id", "Character"));
SendClientMessage(playerid, COLOR_WHITE, query);
pawn Код:
stock sqlite_highest_field(DB:db, fieldname[], tablename[])
{
    new query[128], DBResult:qresult, id = -1, queryresult[128];
    format(query, sizeof(query), "SELECT max( `%s` ) FROM `%s`", fieldname, tablename);
    qresult = db_query(db, query);
    db_get_field(qresult,0,queryresult,128);
    id = strval(queryresult);
    db_free_result(qresult);
    return id;
}



Re: Useful Functions - ziomal432 - 10.09.2010

SQLite has built-in AUTO_INCREMENT. Example creating table:
Код:
CREATE TABLE IF NOT EXISTS `Accounts` (`UID` INTEGER PRIMARY KEY, `Nick` TEXT, `Password` TEXT);



Re: Useful Functions - Norn - 10.09.2010

Quote:
Originally Posted by ziomal432
Посмотреть сообщение
SQLite has built-in AUTO_INCREMENT. Example creating table:
Код:
CREATE TABLE IF NOT EXISTS `Accounts` (`UID` INTEGER PRIMARY KEY, `Nick` TEXT, `Password` TEXT);
Please read my post before posting.


Re : Useful Functions - Peppe95 - 14.09.2010

Код:
#define bit(%1)  (1 << %1)

#define setBit(%1,%2)  %1 |= bit(%2)

#define unsetBit(%1,%2)  %1 ^= bit(%2)

#define getBit(%1,%2)  ((%1 & bit(%2)) != 0)

#define setAllBits(%1)  %1 = 2147483647

#define unsetAllBits(%1)  %1 = 0

stock encodeBits({bool}:...)
{
        new
                result = getarg(0),
                num = numargs(),
                arg;
        while(++arg < num)
        {
                if(getarg(arg))
                {
                        setBit(result, arg);
                }
        }
        return result;
}
bit(bit)
PRIVATE - Converts a bit in a value, for the functions.
Example: bit(2) is 4 and bit(3) is 8.

setBit(var, slot)
Sets the bit 'slot' (from 0 to 31) of a var to true.

unsetBit(var, slot)
Sets the bit 'slot' (from 0 to 31) of a var to false.

getBit(var, slot)
Returns the bit 'slot' (from 0 to 31) of a var.

setAllBits(var)
Sets all bits of a var to true.

unsetAllBits
Sets all bits of a var to false.


Example:
Код:
enum 
{
    Connected,
    Logged,
    Spawned,
    Dead      
}

new 
    pBools[MAX_PLAYERS];

OnPlayerDeath(playerid, killerid, reason)
{
    unsetBit(pBools[playerid], Spawned);
    setBit(pBools[playerid], Dead);
    return 1;
}

OnPlayerRequestSpawn(playerid)
{
    if(!getBit(pBools[playerid], Logged)) return 0;
    return 1;
}

OnPlayerSpawn(playerid)
{
    if(getBit(pBools[playerid], Dead))
    {
        SendClientMessage(playerid, COLOR_WHITE, "You was dead");
        unsetBit(pBools[playerid], Dead);  
    }
    setBit(pBools[playerid], Spawned);
    return 1;
}

OnPlayerConnect(playerid)
{
    setBit(pBools[playerid], Connected);
    return 1;
}

OnPlayerDisconnect(playerid, reason)
{
    unsetAllBits(pBools[playerid]);
    return 1;
}
encodeBits({Bool:}...)
Returns a set of bit of values "bools". It's like encode_tires or encode_lights, so it's "universal"...

Example:
Код:
new 
    panels, 
    doors, 
    lights, 
    tires;       
GetVehicleDamageStatus(vehicleid, panels, doors, lights, tires);
UpdateVehicleDamageStatus(vehicleid, panels, doors, lights, encodeBits(true, true, false, true));



Re: Useful Functions - SFP Owner - 14.09.2010

Good work i love it!


Re: Useful Functions - willsuckformoney - 14.09.2010

I say instead of

pawn Код:
if(!IsPlayerInAnyVehcicle(playerid))
This would work

pawn Код:
stock IsPlayerOnFoot(playerid)
{
    if(GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)
    {
        return 1;
    }
    return 0;
}



Re: Useful Functions - RenisiL - 15.09.2010

TextColorsRandom Macro Define

Hi, this is my new Macro Define created code that changes the text color.

Examples:

Код:
if(!strcmp(cmd, "/test", true))
{
   SendColorsMessage( playerid, "test test test" );
	return 1;
}
Top GameMode:

You can contribute more colors
Код:
new TextColors[ 22 ] =
{
	0xFF8C13FF, 0x8b4513FF,
	0x4949A0FF, 0x148b8bFF,
	0x14ff7fFF, 0x556b2fFF,
	0x0FD9FAFF, 0x10DC29FF,
	0x534081FF, 0x0495CDFF,
	0xEF6CE8FF, 0xBD34DAFF,
	0xC715FFFF, 0xF4A460FF,
	0xEE82EEFF, 0x6152C2FF,
	0xFFD720FF, 0xCF72A9FF,
	0x20B2AAFF, 0x54137DFF,
	0xDC143CFF, 0xB98519FF
};

Top GameMode:


Код:
#define SendColorsMessage(%1,%2) \
    SendClientMessage( %1, TextColors[ random( sizeof( TextColors ) ) ], " " %2 )
Код:
#define SendColorsMessageToAll(%1) \
    SendClientMessageToAll( TextColors[ random( sizeof( TextColors ) ) ) ], " " %1 )
Код:
SendClientMessage(playerid, TEXT_GELKTONA, "Text");
>> change to
Код:
SendColorsMessage( playerid, "Text" );
Код:
SendClientMessageToAll(playerid, TEXT_GELKTONA, "Text");
>> Change to
Код:
SendColorsMessageToAll( playerid, "Text" );



Re: Useful Functions - willsuckformoney - 15.09.2010

Nice, real nice I could had used this 3 days ago D:


Re: Useful Functions - RenisiL - 16.09.2010

Yeah, thank you


Re: Useful Functions - Toni - 17.09.2010

IsPlayerNearPlayer(playerid, targetid, Float:Range)
Purpose:
This is used to see if a player is near another player.

Usage:
pawn Код:
if(IsPlayerNearPlayer(playerid, targetid, 5.0)) //Checks if the player is near the target, with a range of 5.0
Code:
pawn Код:
stock IsPlayerNearPlayer(playerid, targetid, Float:Range)
{
    new
        Float:X,
        Float:Y,
        Float:Z
    ;
    GetPlayerPos(targetid, X, Y, Z);
    if(IsPlayerInRangeOfPoint(playerid, Range, X, Y, Z)) return 1;
    return 0;
}
P.S. I'm not sure if it was already posted, but I just created it real fast and now I am posting it.


Re: Useful Functions - willsuckformoney - 17.09.2010

It was but not posted like you posted, good work.


Re: Useful Functions - RyDeR` - 17.09.2010

Info
Replaces a swear word with a '*' char.

Code
pawn Код:
stock replaceSwear(string[], badWord[], replace = '*')
{
    new
        i
    ;
    while((i = strfind(string, badWord, true)) != -1)
    {
        for(new x = (i + strlen(badWord)); i != x; ++i)
        {
            string[i] = replace;
        }
    }
    return 1;
}
Usage/Example

pawn Код:
public OnPlayerText(playerid, text[])
{
    new
        badWords[][] =
        {
            "bitch",
            "ass",
            "fuck"
        }
    ;
    for(new i; i != sizeof(badWords); ++i)
    {
        replaceSwear(text, badWords[i]);
    }
    return 1;
}
This wil replace for example "bitch" with "*****".


Re: Useful Functions - Kyosaur - 18.09.2010

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Info
Replaces a swear word with a '*' char.

//stuff
This will only replace ONE instant of the swear words, so if you say something like "bitch bitch" only the first bitch will actually be filtered; You can fix this by turning your strfind if statement into a while loop.

With all the loops needed by this function, it would probably be a better idea to just do something like this:

pawn Код:
for(new i=0; i<sizeof(badWords); i++)
    {
        if(strfind(text,badWords[i], true) != -1)
        {
            SendClientMessage(playerid, red, "Do you kiss your mom with that mouth!?");
            return 0;
        }
    }
This is more of a snippet though.


Re: Useful Functions - RyDeR` - 18.09.2010

Indeed. Didn't think about when someone writes it twice. Fixed.
Thanks for warning me.

(Oh and can you delete the quote in your post because the old code is still in there. )


Re: Useful Functions - Basicz - 18.09.2010

SpawnVeh(playerid, IDC)

Code :

pawn Код:
stock SpawnVeh(playerid, IDC)
{
    new Float:X, Float:Y, Float:Z, Float:Ang;
    GetPlayerPos(playerid, X, Y, Z);
    GetPlayerFacingAngle(playerid, Ang);
    CreateVehicle(IDC, X + 3.0, Y, Z + 1.0, Ang, -1, -1, 5000);
    return 1;
}
Example :
SpawnVeh(playerid, 411);


Re: Useful Functions - RyDeR` - 18.09.2010

@Basicz:

Don't forget to delete one vehicle if you create another. Otherwise you will exceed the vehicle limit very quick.


Re: Useful Functions - RyDeR` - 18.09.2010

Nevermind.


Re: Useful Functions - Double-O-Seven - 18.09.2010

pawn Код:
stock CallVehicle(playerid,vehicleid)
{
    if(IsPlayerConnected(playerid) && GetPlayerState(playerid)==PLAYER_STATE_ONFOOT && vehicleid!=INVALID_VEHICLE_ID)
    {
        new Float:x,Float:y,Float:z,Float:a;
        GetPlayerPos(playerid,x,y,z);
        GetPlayerFacingAngle(playerid,a);
       
        LinkVehicleToInterior(vehicleid,GetPlayerInterior(playerid));
        SetVehicleVirtualWorld(vehicleid,GetPlayerVirtualWorld(playerid));
        SetVehiclePos(vehicleid,x,y,z);
        PutPlayerInVehicle(playerid,vehicleid,0);
        SetVehicleZAngle(vehicleid,a);
        SetCameraBehindPlayer(playerid);
        return 1;
    }
    return 0;
}



Re: Useful Functions - [03]Garsino - 18.09.2010

NOTE: sscanf is needed
NOTE 2: I didn't make a version wich supports "," yet, will do later
pawn Код:
public OnGameModeInit()
{
// Example:
    LoadDynamicObjectsFromFile("MyObjectFile1.txt");
    LoadStaticObjectsFromFile("MyObjectFile2.txt");
// You may also load from folders
    LoadDynamicObjectsFromFile("/Objects/MyObjectFile3.txt"); // Will load objects from /scriptfiles/Objects/MyObjectFile3.txt
    LoadStaticObjectsFromFile("/Objects/MyObjectFile4.txt"); // Will load objects from /scriptfiles/Objects/MyObjectFile4.txt
    return 1;
}
LoadDynamicObjectsFromFile - By [03]Garsino
Код:
modelid float:SpawnX float:SpawnY float:SpawnZ float:SpawnRotX float:SpawnRotY SpawnRotZ worldid interiorid playerid float:distance
pawn Код:
stock LoadDynamicObjectsFromFile(filename[]) // For Incognito's streamer plugin
{
    new File:file_ptr, line[256], modelid, Float:SpawnX, Float:SpawnY, Float:SpawnZ, Float:SpawnRotX, Float:SpawnRotY, Float:SpawnRotZ, worldid, interiorid, playerid, Float:distance, objects_loaded;
    file_ptr = fopen(filename, io_read);
    if(!file_ptr) return printf("ERROR! Failed To Load Objects From The File %s (File Doesn't Exist In Scriptfiles Directory)!", filename);
    while(fread(file_ptr, line) > 0)
    {
        sscanf(line, "dffffffdddf", modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ, worldid, interiorid, playerid, distance);
        CreateDynamicObject(modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ, worldid, interiorid, playerid, distance);
        objects_loaded++;
    }
    fclose(file_ptr);
    printf("Loaded %d objects from: %s", objects_loaded, filename);
    return objects_loaded;
}
LoadStaticObjectsFromFile - By [03]Garsino
Код:
modelid float:SpawnX float:SpawnY float:SpawnZ float:SpawnRotX float:SpawnRotY SpawnRotZ
pawn Код:
stock LoadStaticObjectsFromFile(filename[])
{
    new File:file_ptr, line[256], modelid, Float:SpawnX, Float:SpawnY, Float:SpawnZ, Float:SpawnRotX, Float:SpawnRotY, Float:SpawnRotZ, objects_loaded;
    file_ptr = fopen(filename, io_read);
    if(!file_ptr) return printf("ERROR! Failed To Load Objects From The File %s (File Doesn't Exist In Scriptfiles Directory)!", filename);
    while(fread(file_ptr, line) > 0)
    {
        sscanf(line, "dffffff", modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ);
        CreateObject(modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ);
        objects_loaded++;
    }
    fclose(file_ptr);
    printf("Loaded %d objects from: %s", objects_loaded, filename);
    return objects_loaded;
}



Re: Useful Functions - RenisiL - 18.09.2010

not bad , nice