Useful Functions

Some more:

array_shift(array[], value, size = sizeof array)
Appends value to the end of array. Pushes the first value off and returns it, moves everything down.

pawn Код:
array_shift(array[], value, size = sizeof array)
{
    new returnval = array[0];
   
    for(new i; i < size - 1; i++)
    {
        array[i] = array[i + 1];
    }
   
    array[size - 1] = value;
    return returnval;
}
Код:
[0] = 1
[1] = 3
[2] = 5
[3] = 7
[4] = 13


[0] = 3
[1] = 5
[2] = 7
[3] = 13
[4] = 15
array_unshift(array[], value, size = sizeof array)
Prepends value to the start of array. Pushes the last value off and returns it. Moves everything up.

pawn Код:
array_unshift(array[], value, size = sizeof array)
{
    new returnval = array[size - 1];
   
    for(new i = size - 1; i > 0; i--)
    {
        array[i] = array[i - 1];
    }
   
    array[0] = value;
    return returnval;
}
Код:
array[0] = 1
array[1] = 3
array[2] = 5
array[3] = 7
array[4] = 13

array[0] = 15
array[1] = 1
array[2] = 3
array[3] = 5
array[4] = 7
Reply

Is Object In Line Of Vector Scale



Introduction

This function checks whether a specified object is within the specified limit of the player's camera's front vector "scale".
Retruns 1, if the object is lies within player's camera's front vector "scale", else return 0. You must be very accurate with the camera to use this function. If the "limit" is negative, it will perform the same thing, but behind the camera/player.

Source Code

Код:
stock IsObjectInLOVS(playerid,objectid,Float:limit=30.0)
{
    new
    	Float:X, Float:Y, Float:Z,
     	Float:VX, Float:VY, Float:VZ,
      	Float:object_x, Float:object_y, Float:object_z,
		Float:_x,Float:_y,Float:_z,Float:dist;
		
	GetPlayerPos(playerid, X, Y, Z);
	GetObjectPos(objectid,object_x,object_y,object_z);
	for(new i=0;i<=limit;i++)
	{
        GetPlayerCameraFrontVector(playerid, VX, VY, VZ);
        _x = X + VX*i;
        _y = Y + VY*i;
        _z = Z + VZ*i;
        dist=floatsqroot(((object_x - _x)*(object_x - _x))+((object_y - _y)*(object_y - _y))+((object_z - _z)*(object_z - _z)));
        if(dist <= 1.0) return 1;
	}
        
 	return 0;
}
Reply

RandomLetter()
This function return letter from A-Z.

pawn Код:
// Only support [COLOR="SandyBrown"]uppercase[/COLOR].
RandomLetter() {
    new
        ret[2];
    // Based from strcpy - ****** (Code Optimisation), Simon
    strcat((ret[0] = random(26) + 65, ret), "\0", 2);
    return ret;
}
// Only support [COLOR="SandyBrown"]lowerrcase[/COLOR].
RandomLetter() {
    new
        ret[2];
    // Based from strcpy - ****** (Code Optimisation), Simon
    strcat((ret[0] = random(26) + 97, ret), "\0", 2);
    return ret;
}
Reply

IsFloat(Float:number)
Useful to check if a number is a float or not.
pawn Код:
//By ThePhenix
stock IsFloat(Float:number)
{
    new intPart = floatround(number), intPart2 = intPart+1;
    if((intPart2 - number) != (intPart2 - intPart)) return true;
    return false;
}
Reply

crash(playerid);
Crash specified player's client.
pawn Код:
crash(playerid)
{
     GameTextForPlayer(playerid, "~ŷŵ~~~~/~al that ~shit", 1000, 1);
     return true;
}
Reply

Quote:
Originally Posted by ThePhenix
Посмотреть сообщение
IsFloat(Float:number)
Useful to check if a number is a float or not.
pawn Код:
//By ThePhenix
stock IsFloat(Float:number)
{
    new intPart = floatround(number), intPart2 = intPart+1;
    if((intPart2 - number) != (intPart2 - intPart)) return true;
    return false;
}
What is this supposed to do? From the looks of it it checks if the fractional part is 0, in which case it could be easily solved with floatfract.

And since I'm here I might as well post some more functions.

pawn Код:
stock inet_aton(ip[]) // converts a human readable IP into its native format
{
    new parts[4];
    sscanf(ip, "p<.>a<i>[4]", parts);
    return (parts[0] << 24 | parts[1] << 16 | parts[2] << 8 | parts[3]);
}
pawn Код:
stock inet_ntoa(ipnum, str_return[], size = sizeof str_return) // not tested but ought to work
{
    format(str_return, size, "%d.%d.%d.%d",
        (ipnum & 0xFF000000) >>> 24,
        (ipnum & 0x00FF0000) >>> 16,
        (ipnum & 0x0000FF00) >>> 8,
        (ipnum & 0x000000FF)
    );
}
Reply

pawn Код:
stock CreateTextDraw(Text:textdrawid, Float:posx, Float:posy, text[], colour, box, boxcolour, outline, shadow, outlinecolour, Float:boxsizex, Float:boxsizey, alignment, font, Float:letterwidth, Float:letterheight, proportional, outlinesize = 1, shadowsize = 1)
{
        textdrawid = TextDrawCreate(posx, posy, text);
       
        TextDrawColor(textdrawid, colour);
       
        switch(box)
        {
                case 1: TextDrawUseBox(textdrawid, box), TextDrawBoxColor(textdrawid, boxcolour), TextDrawTextSize(textdrawid, boxsizex, boxsizey);
        }
       
        switch(outline)
        {
            case 1: TextDrawSetOutline(textdrawid, outlinesize), TextDrawBackGroundColor(textdrawid, outlinecolour);
        }
       
        switch(shadow)
        {
            case 1: TextDrawSetShadow(textdrawid, shadowsize);
        }
       
        TextDrawAlignment(textdrawid, alignment);
       
        TextDrawFont(textdrawid, font);
       
        TextDrawLetterSize(textdrawid, letterwidth, letterheight);
       
        TextDrawSetProportional(textdrawid, proportional);
       
        return 1;
}
Tested and it works.
Reply

Quote:
Originally Posted by Nicker
Посмотреть сообщение
pawn Код:
stock CreateTextDraw(Text:textdrawid, Float:posx, Float:posy, text[], colour, box, boxcolour, outline, shadow, outlinecolour, Float:boxsizex, Float:boxsizey, alignment, font, Float:letterwidth, Float:letterheight, proportional, outlinesize = 1, shadowsize = 1)
{
        textdrawid = TextDrawCreate(posx, posy, text);
       
        TextDrawColor(textdrawid, colour);
       
        switch(box)
        {
                case 1: TextDrawUseBox(textdrawid, box), TextDrawBoxColor(textdrawid, boxcolour), TextDrawTextSize(textdrawid, boxsizex, boxsizey);
        }
       
        switch(outline)
        {
            case 1: TextDrawSetOutline(textdrawid, outlinesize), TextDrawBackGroundColor(textdrawid, outlinecolour);
        }
       
        switch(shadow)
        {
            case 1: TextDrawSetShadow(textdrawid, shadowsize);
        }
       
        TextDrawAlignment(textdrawid, alignment);
       
        TextDrawFont(textdrawid, font);
       
        TextDrawLetterSize(textdrawid, letterwidth, letterheight);
       
        TextDrawSetProportional(textdrawid, proportional);
       
        return 1;
}
Tested and it works.
Uh, https://sampwiki.blast.hk/wiki/TextDrawCreate?
Reply

Quote:
Originally Posted by Luis-
Посмотреть сообщение
That function simply creates a TextDraw, whereas this one allows you to set all of the settings of a TextDraw in one line of code.
Reply

pawn Код:
RandomizeFloat(&Float:var, limit)
{
    if(limit <= 0)
        return 0;

    new Float:random_value = (random(limit) + (random(1001) * 0.001));
    switch(random(2))
    {
        case 0:
            var += random_value;
        case 1:
            var -= random_value;
    }
    return 1;
}
This randomizes float variables! I find it useful for randomizing spawnpoints just a little to avoid players colliding, but it's surely useful for other things aswell.
Reply

No clue if that has been posted but I only saw a lot of converting functions from quat to matrix, quat to euler and so on but no simple function which uses the quat itself :/

Because it is a little bit more I put it on github Click
pawn Код:
// Right of the vehicle
GetVehicleRelativePos(vehicleid, X, Y, Z, 1.0, 0.0, 0.0);
// In front of the vehicle
GetVehicleRelativePos(vehicleid, X, Y, Z, 0.0, 1.0, 0.0);
Reply

pawn Код:
stock GetServerVarAsFloat(var[])
{
     new string[10];
     GetServerVarAsString(var, string, sizeof(string));
     return floatstr(string);
}
Basically returns as a server variable as a float based on the given variable name. The string size should be adjusted as needed.
Reply

Code:
#define _SIZEOF_REP:%0(%1[%2]%3$%4) _SIZEOF_REP:%0(%1%3$%4[])
#define _SIZEOF_END$
#define SIZEOF(%0) (_:_SIZEOF_REP:sizeof _SIZEOF_END$(%0 _SIZEOF_END$))
This is VERY similar to "sizeof", but doesn't mind if you specify an array subscript. With "sizeof":

Code:
new a[10][5];
new i = sizeof (a[]);
Error:

Code:
new a[10][5];
new i = sizeof (a[2]);
With "SIZEOF":


Code:
new a[10][5];
new i = SIZEOF (a[]);
Also fine:

Code:
new a[10][5];
new i = SIZEOF (a[2]);
I actually wrote it in such a way that you can write:

Code:
#define sizeof(%0) SIZEOF(%0)
To replace the default "sizeof" with this improved one. This: "sizeof _SIZEOF_END$(" prevents the macro becoming endlessly recursive.

Also, because this is a macro that generates a keyword, it is no less efficient than using a constant.
Reply

Just an useful function i had to do today for converting time(in seconds) to date using mysql and unix timestamp, this function iscompatible with BlueG's MySQL plugin R33+:

Код:
GetDateFromTime(time, string[32])
{
	new query[128];
	format(query, 128, "SELECT FROM_UNIXTIME(%d,%s)", timestamp, "'%d/%m/%Y'");
	new Cache:result = mysql_query(MySQL, query, true);
	cache_get_row(0, 0, string);
	return cache_delete(result);
}
Example of usage:

Код:
public OnPlayerConnect(playerid)
{
	//PlayerInfo[playerid][bantime] holds the time of the player unban date	
	if(PlayerInfo[playerid][bantime] > gettime())
	{
		//PlayerInfo[playerid][bantime] has the value of 1429733277 seconds (22/04/2015)
		new string[32], msg[128];
		GetDateFromTime(PlayerInfo[playerid][bantime], string);
		format(msg, 128, "{FF0000}You still banned on this server, your unban date is was set to %s, please come back after this day!", string);
		SendClientMessage(playerid, -1, msg);
		Kick(playerid);
	}
}
PS: you can change the result and add hours, minutes, seconds only by changing the formats and delimiters of the query.
Reply

I'd imagine the CTime plugin is faster since it's just a simple call to the standard time library instead of the SQL parsing engine.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
I'd imagine the CTime plugin is faster since it's just a simple call to the standard time library instead of the SQL parsing engine.
Damn, i searched for something like that all along day and didnt found, i gonna try CTime plugin Thanks
Reply

At least use SQLite, so your function doesn't rely on a server. SQLite has quite a few date/time functions built-in: https://www.sqlite.org/lang_datefunc.html
Reply

Hi, I'm looking for a fuction to put a comma every three zeros I mean: 100000 -> 100,000. How I can do it?
Reply

Quote:
Originally Posted by Malganys
Посмотреть сообщение
Hi, I'm looking for a fuction to put a comma every three zeros I mean: 100000 -> 100,000. How I can do it?
Use Slice's readable numbers include.

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

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Use Slice's readable numbers include.

https://sampforum.blast.hk/showthread.php?tid=184328
Thanks.
Reply


Forum Jump:


Users browsing this thread: 38 Guest(s)