Useful Functions

Quote:
Originally Posted by cj101
Посмотреть сообщение
This will return in what range a file size is in.. supports Bytes, KB, MB, and GB...

Код:
GetFileSizeInBytes(file[])
{
	if(!fexist(file)) return 0;
	new t;
	new kb,mb,fl;
	new File:raw;
	raw = fopen(file);
	fl = flength(raw);
	mb = 1073741824/fl;
	kb = 1024/fl;
	if(fl > 1024 && fl < 1048576)
	{
		return 1; // 1 = file is in KB
	}
	if(fl >= 1048576)
	{
		return 2; // 2 = file is in MB
	}
	if(fl >= 1073741824)
	{
	    return 3; // 3 = file is in GB
	}
	return 4;
}
You didn't close the file, is that on purpose or a mistake?
Reply

Formatting and getting the player name with one parameter less to write(format is for use of one parameter, shift it, if you want):

pawn Code:
#define cformat(%0,%1,%2); format(%0,sizeof %0,%1,%2); //used for formats with one parameter
#define cGetPlayerName(%0,%1); GetPlayerName(%0,%1,MAX_PLAYER_NAME);

Example of standart use:

new Name[MAX_PLAYER_NAME], string[121];
GetPlayerName(playerid,Name,MAX_PLAYER_NAME);
format(string,sizeof (string),"%s was comited suicide",Name);
printf("%s",string);

Example of use without standart parameters :

new Name[MAX_PLAYER_NAME], string[121];
cGetPlayerName(playerid,Name);
cformat(string,"%s was comited suicide",Name);
printf("%s",string);

//You can make this for any functions, it is only a example.
Reply

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
pawn Код:
stock SpawnVehicleForPlayer(playerid,modelid,color1,color2,bool:PutIn = false,...)
{
    new num = numargs(),beg = 1,playerforv,Float:x,Float:y,Float:z,Float:fa;
    GetPlayerPos(playerid,x,y,z);
    GetPlayerFacingAngle(playerid,fa);
    if(PutIn == false)
    {
        x += (10.4632134 * floatsin(-fa,degrees));
        y += (10.4632134 * floatsin(-fa,degrees));
        playerforv = CreateVehicle(modelid,x,y,z,fa+90,color1,color2,-1);
        for(new i = beg; i < num; i++)
        {
            new tune = getarg(i,0);
            AddVehicleComponent(playerforv,tune);
        }
    }
    else
    {
        playerforv = CreateVehicle(modelid,x,y,z,fa,color1,color2,-1);
        PutPlayerInVehicle(playerid,playerforv,0);
        for(new i = beg; i < num; i++)
        {
            new tune = getarg(i,0);
            AddVehicleComponent(playerforv,tune);
        }
    }
    return 1;
}
Just a simple function to spawn a vehicle for a specified player. (last '...' are arguments for tuning parts,I'm not sure about them though)
Nice done, but you have a typo:
Quote:

y += (10.4632134 * floatcos(-fa,degrees));

Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Nice done, but you have a typo:
Oops! Thanks for correcting me
Reply

This is simple macro function for economy on your server

pawn Код:
new VALUE = 5;
#define returnValue(%0) %0 * VALUE

stock SetNewValue(value) return VALUE = value;

//example

SetNewValue(8);
printf("%i", returnValue(6)); // back 48 (because 8 * 6 = 48)
I wanna give thanks to Don Correlli, he helps me out
Reply

* AddLineBreaks(input[], chars=80)
This function is meant to be used by DIALOG_STYLE_MSGBOX dialogs, when it is desired to show a large string, and this string to be split into several lines. It adds the line breaks just if it finds a ' ' (space) character.

- Parameters:
-- input[]: The string to be split.
-- chars[]: How many minimum characters should show per line (default: 80).
- Returns:
-- The formatted string.

pawn Code:
AddLineBreaks(input[], chars=80)
{
    new output[512];

    format(output, sizeof(output), "%s", input);

    for(new i, j, o = strlen(output); i < o; i++, j++)
    {
        if(j >= chars && output[i] == ' ')
        {
            output[i] = '\n';
            j = 0;
        }
    }

    return output;
}
Reply

Nice one Zamaroht, this is very useful!
Reply

IsPlayerOutOfArea
Function:
Detects if a player is OUT of an area.

Code:
pawn Код:
stock IsPlayerOutOfArea(playerid, Float:MinX, Float:MinY, Float:MaxX, Float:MaxY)
{
    new
        Float:X,
        Float:Y,
        Float:Z
    ;
    GetPlayerPos(playerid, X, Y, Z);
    if(X >= MaxX && Y >= MaxY && X <= MinX && Y <= MinY) return 1;
    return 0;
}
This is like IsPlayerInArea, but with reversed coding.
Reply

Quote:
Originally Posted by The Toni
Посмотреть сообщение
IsPlayerOutOfArea
pawn Код:
stock IsPlayerOutOfArea(playerid, Float:MinX, Float:MinY, Float:MaxX, Float:MaxY)
This is like IsPlayerInArea, but with reversed coding.
No offense, but that's just dumb xD

if(!IsPlayerInArea(...))
Reply

@The Toni: Unless my mind deceives me, your code won't work. Your && checks in the if statement should be or (||). And it's just as easy to check if somebody is !not in an area.

@******: I'll get the code when I return to my PC, and I'll check out that SYSREQ.C stuff.
Reply

Quote:
Originally Posted by Zamaroht
View Post
* AddLineBreaks(input[], chars=80)
This function is meant to be used by DIALOG_STYLE_MSGBOX dialogs, when it is desired to show a large string, and this string to be split into several lines. It adds the line breaks just if it finds a ' ' (space) character.

- Parameters:
-- input[]: The string to be split.
-- chars[]: How many minimum characters should show per line (default: 80).
- Returns:
-- The formatted string.

pawn Code:
AddLineBreaks(input[], chars=80)
{
    new output[512];

    format(output, sizeof(output), "%s", input);

    for(new i, j, o = strlen(output); i < o; i++, j++)
    {
        if(j >= chars && output[i] == ' ')
        {
            output[i] = '\n';
            j = 0;
        }
    }

    return output;
}
Nice.

Try:
pawn Code:
stock
    AddLineBreaks(input[], chars = 80)
{
    new
        Count = 1,
        i = chars,
        output[512];
    format(output, sizeof(output), "%s", input);
    while(output[i])
    {
//      if(i % chars) { printf("Loop: %d", i); } else { printf("    Start chars Index: %d", i); } // DEBUG
        if(output[i] == ' ')
        {
            output[i] = '\n';
            i = chars * (++Count);
        } else i--;
    }
    return output;
}
pawn Code:
public
    OnFilterScriptInit()
{
    printf("%s", AddLineBreaks("This function is meant to be used by DIALOG_STYLE_MSGBOX dialogs, when it is desired to show a large string, and this string to be split into several lines. It adds the line breaks just if it finds a ' ' (space) character. - Parameters: -- input[]: The string to be split. -- chars[]: How many minimum characters should show per line (default: 80). - Returns: -- The formatted string"));


    printf("%s", AddLineBreaks("This function is meant to be used by DIALOG_STYLE_MSGBOX dialogs, when it is desired to show a large string, and this string to be split into several lines. It adds the line breaks just if it finds a ' ' (space) character. - Parameters: -- input[]: The string to be split. -- chars[]: How many minimum characters should show per line (default: 80). - Returns: -- The formatted string", 20));
    return 1;
}
Output:
Code:
[11:21:18]  Start chars Index 80
[11:21:18] Loop: 79
[11:21:19] Loop: 78
[11:21:19] Loop: 77
[11:21:19] Loop: 76
[11:21:19]  Start chars Index 160
[11:21:19] Loop: 159
[11:21:19]  Start chars Index 240
[11:21:19] Loop: 239
[11:21:19]  Start chars Index 320
[11:21:19] Loop: 319
[11:21:19] This function is meant to be used by DIALOG_STYLE_MSGBOX dialogs, when it is
desired to show a large string, and this string to be split into several lines. It
adds the line breaks just if it finds a ' ' (space) character. - Parameters: --
input[]: The string to be split. -- chars[]: How many minimum characters should
show per line (default: 80). - Returns: -- The formatted string
[11:21:19]  Start chars Index 20
[11:21:19] Loop: 19
[11:21:19] Loop: 18
[11:21:19] Loop: 17
[11:21:19] Loop: 16
[11:21:19]  Start chars Index 40
[11:21:19] Loop: 39
[11:21:19] Loop: 38
[11:21:19] Loop: 37
[11:21:19] Loop: 36
[11:21:19]  Start chars Index 60
[11:21:19] Loop: 59
[11:21:19] Loop: 58
[11:21:19] Loop: 57
[11:21:19] Loop: 56
[11:21:19]  Start chars Index 80
[11:21:19] Loop: 79
[11:21:19] Loop: 78
[11:21:19] Loop: 77
[11:21:19] Loop: 76
[11:21:19]  Start chars Index 100
[11:21:19]  Start chars Index 120
[11:21:19] Loop: 119
[11:21:19] Loop: 118
[11:21:19] Loop: 117
[11:21:19]  Start chars Index 140
[11:21:19] Loop: 139
[11:21:19] Loop: 138
[11:21:19] Loop: 137
[11:21:19] Loop: 136
[11:21:19]  Start chars Index 160
[11:21:19] Loop: 159
[11:21:19]  Start chars Index 180
[11:21:19]  Start chars Index 200
[11:21:19] Loop: 199
[11:21:19]  Start chars Index 220
[11:21:19] Loop: 219
[11:21:19] Loop: 218
[11:21:19] Loop: 217
[11:21:19] Loop: 216
[11:21:19] Loop: 215
[11:21:19] Loop: 214
[11:21:19] Loop: 213
[11:21:19] Loop: 212
[11:21:19] Loop: 211
[11:21:19]  Start chars Index 240
[11:21:19] Loop: 239
[11:21:19]  Start chars Index 260
[11:21:19] Loop: 259
[11:21:19]  Start chars Index 280
[11:21:19] Loop: 279
[11:21:19] Loop: 278
[11:21:19] Loop: 277
[11:21:19] Loop: 276
[11:21:19] Loop: 275
[11:21:19]  Start chars Index 300
[11:21:19] Loop: 299
[11:21:19] Loop: 298
[11:21:19] Loop: 297
[11:21:19] Loop: 296
[11:21:19] Loop: 295
[11:21:19] Loop: 294
[11:21:19] Loop: 293
[11:21:19]  Start chars Index 320
[11:21:19] Loop: 319
[11:21:19]  Start chars Index 340
[11:21:19] Loop: 339
[11:21:19] Loop: 338
[11:21:19] Loop: 337
[11:21:19] Loop: 336
[11:21:19] Loop: 335
[11:21:19] Loop: 334
[11:21:19] Loop: 333
[11:21:19]  Start chars Index 360
[11:21:19] Loop: 359
[11:21:19]  Start chars Index 380
[11:21:19] Loop: 379
[11:21:19] Loop: 378
[11:21:19] Loop: 377
[11:21:19] Loop: 376
[11:21:19] This function is
meant to be used by
DIALOG_STYLE_MSGBOX
dialogs, when it is
desired to show a large
string, and this
string to be split
into several lines. It
adds the line breaks
just if it finds a
' ' (space)
character. - Parameters: --
input[]: The string
to be split. --
chars[]: How many
minimum characters should
show per line
(default: 80). - Returns:
-- The formatted
string
Reply

not really useful but will be for the lazy people

pawn Код:
stock SendERROR(playerid, error)
{
    if(error == 1)  return SendClientMessage(playerid,red,"ERROR: You are not a high enough level to use this command");
    if(error == 2)  return SendClientMessage(playerid,red,"ERROR: Player is not connected");
    if(error == 3)  return SendClientMessage(playerid,red,"ERROR: Player is not connected or is yourself or is highest level admin");
    if(error == 4)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 4 to use this Command");
    if(error == 5)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 3 to use this Command");
    if(error == 6)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 2 to use this Command");
    if(error == 7)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 1 to use this Command");
    if(error == 8)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 5 to use this Command");
    if(error == 9)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 6 to use this Command");
    if(error == 10)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 7 to use this Command");
    if(error == 11)  return SendClientMessage(playerid,red,"ERROR: You need to be Level 8 to use this Command");
    if(error == 12) return SendClientMessage(playerid,red,"ERROR: You are not in a vehicle");
    return 1;
}
The text can be changed easily and to use just do

SendERROR(playerid, 1); - SendClientMessage(playerid,red,"ERROR: You are not a high enough level to use this command");

just SendERROR(playerid, error); choose the error id from the stock
Reply

Not really useful, unless you want to do something to certain vehicles on death:

DestroyVehicle2(vehicleid)

pawn Код:
stock DestroyVehicle2(vehicleid)
{
    OnVehicleDeath(vehicleid, INVALID_PLAYER_ID);
    return DestroyVehicle(vehicleid);
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Makes it a lot clearer which error you're sending - yours were in an almost random order, especially the levels, which went 4, 3, 2, 1, 5, 6, 7, 8... Now you can do:

pawn Код:
SendError(playerid, E_ERROR_YOU);
And it's fairly obvious the error is something to do with you. Or:

pawn Код:
SendError(playerid, E_ERROR_LEVEL, 27);
To make it known that you need to be level 27.
Or messages could be simply stored in a mysql table and a threaded lookup could fetch the message based on index and sendclientmessage it to a playerid.
Reply

This function saves a string to a file.
Very simple, but it can be very handy!

Код:
stock SaveStringToFile(content[], filename[])
{
     new File:usfl = fopen(filename, io_append);
     fwrite(usfl, content);
     fclose(usfl);
}
I am using this much for logs
For example:

Код:
}
else //A login code above this, don't need it here ;) So this 'else' means: WRONG PASSWORD
{
     new year,month,day,hours,minutes,seconds; getdate(year,month,day);gettime(hours,minutes,seconds);
     Kick(playerid);
     format(string, sizeof(string), "%s [%d-%d-%d %d:%d:%d], Kicked by: ADMIN BOT\r\n", PlayerName[playerid], day, month, year, hours, minutes, seconds);
     SaveStringToFile(string, "LOGS/KickLog.log");
}
Very easy
This is better then use the fopen, fwrite etc again and again....
Reply

pawn Код:
LineCount(filepath[])
{
    new idx = 0;
    if(fexist(filepath))
    {
        new File:Handler, string[1028];
        Handler = fopen(filepath, io_read);
        while(fread(Handler, string)){idx++;}
        fclose(Handler);
    }
    return idx;
}
Example:

pawn Код:
printf("There are currently %d players banned.",LineCount("List.ban"));
Reply

Nice, could be useful to most Norn
Reply

Removed.
Reply

Nice function, but why don't you escape them instead of adding whitespace?

This is an escape function made by ******:

Код:
#define MAX_INI_ENTRY_TEXT 80

stock DB_Escape(text[])
{
	new
		ret[MAX_INI_ENTRY_TEXT * 2],
		ch,
		i,
		j;
	while ((ch = text[i++]) && j < sizeof (ret))
	{
		if (ch == '\'')
		{
			if (j < sizeof (ret) - 2)
			{
				ret[j++] = '\'';
				ret[j++] = '\'';
			}
		}
		else if (j < sizeof (ret))
		{
			ret[j++] = ch;
		}
		else
		{
			j++;
		}
	}
	ret[sizeof (ret) - 1] = '\0';
	return ret;
}
Reply

Quote:
Originally Posted by Remis
Посмотреть сообщение
Nice function, but why don't you escape them instead of adding whitespace?

This is an escape function made by ******:

Код:
#define MAX_INI_ENTRY_TEXT 80

stock DB_Escape(text[])
{
	new
		ret[MAX_INI_ENTRY_TEXT * 2],
		ch,
		i,
		j;
	while ((ch = text[i++]) && j < sizeof (ret))
	{
		if (ch == '\'')
		{
			if (j < sizeof (ret) - 2)
			{
				ret[j++] = '\'';
				ret[j++] = '\'';
			}
		}
		else if (j < sizeof (ret))
		{
			ret[j++] = ch;
		}
		else
		{
			j++;
		}
	}
	ret[sizeof (ret) - 1] = '\0';
	return ret;
}
Ah that version one was the wrong one I posted, it was meant to escape the string indeed, I've removed it now as I only created it because I wasn't aware such code existed.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)