Re: Useful Functions -
Luka P. - 30.03.2011
pawn Код:
stock GetTextFileLine(buffer[], file[], line, size = sizeof(buffer)) // by Luka P.
{
new File:stream = fopen(file, io_read), index = -1;
if(!stream) return 0;
while(fread(stream, buffer, size))
if(++index == line) break;
return fclose(stream);
}
pawn Код:
static stock GetKey(file[], buffer[], key[], len = sizeof(buffer)) // by Luka P.
{
new File:stream = fopen(file, io_read), line = -1, index = 0;
if(!stream) return 0;
while(fread(stream, buffer, len))
{
++line;
if(strfind(buffer, key, true) == 0)
{
break;
}
}
for(new x=0; x < len; ++x)
{
if(buffer[x] == '=')
{
index = x + 1;
}
}
strdel(buffer, 0, index);
return fclose(stream);
}
Re: Useful Functions -
$t3ve - 01.04.2011
Quote:
Originally Posted by Slice
The reason it's so fast is it's a C-function. When doing string manipulation in PAWN, it's best to use the native string functions as much as possible.
Anyways, I made this just now:
GetParam
Note: I have not fully tested this - only like 80% of the possible different inputs.
Am I the only one pissed off at strtok? Not sure why but it just annoys me.
Anyways, I made this function as a replacement. It's slightly faster than strtok, and easier to use.
Usage:
pawn Код:
GetParam( string[], param_num );
string is the string you want to look for params in, and param_num is the index of the param you want, starting at 1.
The function will seamlessly trim the string and return it.
Example:
pawn Код:
new params[] = " I would like to \t\t\nhave\n 50 bags of cheetos.\r\n"; printf( "1: \"%s\"", GetParam( params, 1 ) ); // Prints "I" printf( "2: \"%s\"", GetParam( params, 2 ) ); // Prints "would" printf( "3: \"%s\"", GetParam( params, 3 ) ); // Prints "like" printf( "4: \"%s\"", GetParam( params, 4 ) ); // Prints "to" printf( "5: \"%s\"", GetParam( params, 5 ) ); // Prints "have" printf( "6: \"%s\"", GetParam( params, 6 ) ); // Prints "50" printf( "7: \"%s\"", GetParam( params, 7 ) ); // Prints "bags" printf( "8: \"%s\"", GetParam( params, 8 ) ); // Prints "of" printf( "9: \"%s\"", GetParam( params, 9 ) ); // Prints "cheetos."
Code:
pawn Код:
stock GetParam( const szString[ ], iParam ) { new iSearchPos, iPos, iHoles, szReturn[ 128 ], iFoundParam = -1; if ( szString[ 0 ] <= 1 ) // Treat \1 as a null character for ZCMD/YCMD compatibility. return szReturn; // If the string is empty there's no point in looking for stuff. iPos = -1; while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Trim the beginning of the string. if ( --iParam == 0 ) // Wether or not we're looking for the first parameter. { if ( ( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab || ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1 ) strcat( szReturn, szString[ iPos ], iSearchPos + 1 - iPos ); // Put the found part into the return. else strcat( szReturn, szString[ iPos ] ); // Put the whole thing (except for the first tabs/spaces, if any) into the return. } else { while ( ( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab || ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1 ) { back_in_action: iPos = iSearchPos; while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Skip any whitespace. if ( iFoundParam != -1 ) // If the previous iteration found a param.. { strcat( szReturn, szString[ iFoundParam ], iPos - iFoundParam ); // Put it into the return string. iFoundParam = -1; break; // Then BREAK YOSELF. } if ( ++iHoles == iParam ) // If this current "hole" is in front of the param we're looking for.. iFoundParam = iPos; // ..let the script know that. } if ( iFoundParam != -1 ) // If a param was found in the last iteration.. (end of the string) { iSearchPos = strlen( szString ); // Set the next position to the end of the string. goto back_in_action; } } if ( szReturn[ 0 ] ) { // Trim any whitespace on the right side. for ( iPos = strlen( szReturn ) - 1; iPos >= 0 && szReturn[ iPos ] <= ' '; iPos-- ) szReturn[ iPos ] = 0; } return szReturn; }
|
oh nice its crashing my server
Код:
CMD:test(playerid,params[])
{
if((sscanf(params, "s", params[1])) || (sscanf(params, "s", params[2]))) return SendClientMessage(playerid, ADMINRED, "(!)"HEX_WHITE" Usage is /Test <Something>");
SendClientMessagef(playerid, ADMINRED, "(!)"HEX_WHITE" The first param is %s and the second one is %s.", GetParam(params, 1), GetParam(params, 2));
return 1;
}
strtok ftw
Re: Useful Functions -
Hoxxy - 01.04.2011
....
Re: Useful Functions -
RyDeR` - 01.04.2011
Why do you actually re-release something that is not made by you without even giving credits?
Re: Useful Functions -
Luka P. - 01.04.2011
Isn't it easier to just write credits than deleting the whole function? :O
Re: Useful Functions -
General Abe - 02.04.2011
Wonder if anyone's tried mine yet
Re: Useful Functions -
Luka P. - 02.04.2011
pawn Код:
#define findNextPositive(%0,%1,%2) findNext(%0,%1,1,%2)
#define findNextNegative(%0,%1,%2) findNext(%0,%1,0,%2)
static stock findNext(const array[], index, type, len = sizeof(array))
{
new _next = -1;
for(new x = index + 1; x < len; ++x)
{
if((type) ? (array[x]) : (!array[x]))
{
_next = x;
break;
}
}
return _next;
}
Re: Useful Functions -
sciman001 - 16.04.2011
Quote:
Originally Posted by Rafelder_GRF
DisableBadWord(word[])
word[] - word which will be disabled
pawn Code:
#define DisableBadword(%1) for(new i=0; i<strlen(text); i++) if(strfind(text[i], %1, true) == 0) for(new a=0; a<256; a++) if (a >= i && a < i+strlen(%1)) text[a]=\'*\'
|
how can i use this?
Re: Useful Functions -
sciman001 - 16.04.2011
Quote:
Originally Posted by Yaheli_Faro
TeleportPlayer(playerid, X, Y, Z); - Teleports a player to a certain location. (Useful for noobs or saving time. :P)
pawn Код:
stock TeleportPlayer(playerid, Float:X, Float:Y, Float:Z) { if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER) { new car = GetPlayerVehicleID(playerid); SetPlayerPos(playerid, X, Y, Z); SetVehiclePos(car, X, Y, Z); PutPlayerInVehicle(playerid, car, 0); } else SetPlayerPos(playerid, X, Y, Z); return 1; }
|
i got this:
pawn Код:
stock TeleportPlayer(playerid, Float:X, Float:Y, Float:Z)
{
if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
new car = GetPlayerVehicleID(playerid);
SetPlayerPos(playerid, X, Y, Z);
SetVehiclePos(car, X, Y, Z);
PutPlayerInVehicle(playerid, car, 0);
}
else
{
SetPlayerPos(playerid, X, Y, Z);
}
return 1;
}
since i like suuper clean code, but pawno crashes. the compiler anyway.
Re: Respuesta: Useful Functions -
sciman001 - 18.04.2011
Quote:
Originally Posted by [FeK]DraKiNs
@sciman001 - No
@StrickenKid
Lol man,it no is faster,Only is a definition of for and no navigate in variable multi...
pawn Код:
#define foreach(%0,%1) for(new %0,_%0; _%0 != sizeof %1; %0 = %1[_%0], ++_%0) /* - foreach ~ Simple ~ No Fast..
Note: post has edited :d
___
pawn Код:
public OnFilterScriptInit() { new var[10]; new Tick = GetTickCount(); for(new a; a < 99000; a++) { for(new i; i < sizeof(var); ++i) { new b = var[i]; } } printf("[FOR] %d ms",GetTickCount() - Tick); Tick = GetTickCount(); for(new a; a < 99000; a++) { foreach(i,var) { } } printf("[FOREACH] %d ms",GetTickCount() - Tick); return true; }
Код:
[02:45:17] [FOR] 60 ms
[02:45:17] [FOREACH] 49 ms
|
that doesnt work for me?..... hmmm... i dont get the code... a nbit too complex for me... can yuo explain please?
Re: Respuesta: Useful Functions -
Kwarde - 18.04.2011
Quote:
Originally Posted by sciman001
that doesnt work for me?..... hmmm... i dont get the code... a nbit too complex for me... can yuo explain please?
|
That was a benchmark -> A script that compares two scripts / functions or something smilar like that.
Re: Respuesta: Useful Functions -
Slice - 18.04.2011
Quote:
Originally Posted by Kwarde
That was a benchmark -> A script that compares two scripts / functions or something smilar like that.
|
I might add that it was a horrible benchmark.
Re: Useful Functions -
admantis - 19.04.2011
A couple months ago whilst testing the new 0.3c objects, I found this rather interestant and tried to make something fun off it.
Just place the ammount of stairs after 'Desired = ..' and it will create alot of loop stairs to the heaven with the new 0.3c object at the positions :P
p.s: Sorry if it looks abit cheesy, this code is like a couple months old.
pawn Код:
stock StairsToHeaven(Float:X, Float:Y, Float:Z);
{
new Z_Increase, Quant, Desired;
Desired = 400; // We're creating 400 stairs!
do
{
Z_Increase += 20;
Quant += 1;
CreateObject(18771, X, Y, Z+Z_Increase, 0, 0, 0, 250);
}
while (Desired != Quant);
return 1;
}
Re: Useful Functions -
Slice - 06.05.2011
For swapping, a simple XOR swap could be done as a macro instead.
Re: Useful Functions -
RyDeR` - 06.05.2011
XOR swap is slow (as far as I can remember).
Re: Useful Functions -
Slice - 06.05.2011
Quote:
Originally Posted by RyDeR`
XOR swap is slow (as far as I can remember).
|
I compared the two:
pawn Код:
Bench for swap with variable: executes, by average, 2536.06 times/ms.
Bench for swap with xor: executes, by average, 3052.78 times/ms.
Using
my benchmarking macros.
Re: Useful Functions -
RyDeR` - 06.05.2011
Indeed. Well, I probably miss-remembered again. Thanks for correcting.
Re: Useful Functions -
Kwarde - 07.05.2011
getNumCountFromAlfabet(input[]);
This function returns the count from the first letter of the input string. If I put 'Hi there' in it, it will get from 'H' the value, which is '8':
A=1
B=2
C=3
etc.
The function:
pawn Код:
stock getNumCountFromAlfabet(input[])
{
switch(input[0])
{
case 'a', 'A': return 1;
case 'b', 'B': return 2;
case 'c', 'C': return 3;
case 'd', 'D': return 4;
case 'e', 'E': return 5;
case 'f', 'F': return 6;
case 'g', 'G': return 7;
case 'h', 'H': return 8;
case 'i', 'I': return 9;
case 'j', 'J': return 10;
case 'k', 'K': return 11;
case 'l', 'L': return 12;
case 'm', 'M': return 13;
case 'n', 'N': return 14;
case 'o', 'O': return 15;
case 'p', 'P': return 16;
case 'q', 'Q': return 17;
case 'r', 'R': return 18;
case 's', 'S': return 19;
case 't', 'T': return 20;
case 'u', 'U': return 21;
case 'v', 'V': return 22;
case 'w', 'W': return 23;
case 'x', 'X': return 24;
case 'y', 'Y': return 25;
case 'z', 'Z': return 26;
default: return strval(input[0]);
}
return -1;
}
If it's an integer, it will return that. ('9hi there' will return 9).
I am by the way sure it can be made much easier.
Why did I made this?
I am using djson and Whirlpool. After gmx or something else, any password is good to login, so I made a hash function:
pawn Код:
stock hash(input[])
{
new tmp[2], tmppass[129];
WP_Hash(tmppass, 129, input);
for(new i = 0; i < strlen(tmppass); i++){
switch(tmppass[i]){
case 'a'..'z', 0..9: tmp[0] = ((tmp[0] + getNumCountFromAlfabet(tmppass[i]) [CENSORED])) * getNumCountFromAlfabet(tmppass[i]);
case 'A'..'Z': tmp[0] = ((tmp[0] + getNumCountFromAlfabet(tmppass[i]) [CENSORED])) * (100 + getNumCountFromAlfabet(tmppass[i]));
}
tmp[1] = ([CENSORED]);
}
return [CENSORED];
}
I've censored it so that decrypt is harder. And as you can see, it first encrypts the string to whirlpool, and then I use getNumCountFromAlfabet to get an integer (I could've used that num_hash from dracoblue, but I made my own, I am not sure why).
With this hash,
Hi there! You are very smart if you can decrypt this text. will be:
9371648.
However, I don't know why I posted this, because it's the getNumCountFromAlfabet.
And, please tell it if that function can be improved (something like "return 'a'..'z' = 1..26" or something :P)
Re: Useful Functions -
RyDeR` - 07.05.2011
Yes, it can be improved:
pawn Код:
stock GetAlphabetIndex(const ch)
{
return ('a' <= ch <= 'z') ? (ch - 96) : ('A' <= ch <= 'Z') ? (ch - 64) : '\0';
}
Or:
pawn Код:
stock GetAlphabetIndex(const ch)
{
if('a' <= ch <= 'z')
return ch - 96;
else if('A' <= ch <= 'Z')
return ch - 64;
else return '\0';
}
Or:
pawn Код:
stock GetAlphabetIndex(const ch)
{
switch(ch)
{
case 'a'..'z': return ch - 96;
case 'A'..'Z': return ch - 64;
}
return '\0';
}
Re: Useful Functions -
Kwarde - 07.05.2011
I knew it was possible :') - Thanks.
By the way, I forgot that 'const' (that's why I'd use input
[])