Useful Functions

Quote:
Originally Posted by Djole1337
Посмотреть сообщение
pawn Код:
// CODE
Quote:
Originally Posted by Djole1337
Can be made more general like this.
ah i made a very terrible function to do that lol, thanks for it!
Reply

This is for all old strtok uses.

I build this function (strtok) from the beginning and i could optimize it by more than one and a half the original function:

PHP код:
stock strtok(str[], &index, array[20] = ' 'dilimiter[2] = ' ') {
    
format(array, 20str);
    
strdel(array, 0index);
    new 
pos strfind(array, dilimiterfalse);
    if(
pos == -1) array[index] = EOS;
    else {
        array[
pos] = EOS;
        
index += pos;
    }
    
index++;
    return array;

In addition there's the option to insert the result into an array and not just return it.

Speed results:

Код:
< speed > Ron's strtok: 1100
< speed > Original strtok: 1791
Enjoy
Reply

Few ms of difference, almost everyone use sscanf instead.
Reply

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
Few ms of difference, almost everyone use sscanf instead.
Almost.
This is for the people that not use sscanf...
Reply

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
Few ms of difference, almost everyone use sscanf instead.
dunno but some people stick to no plugin version gamemodes/scripts :/
Reply

Thought I'd share even though I wouldn't put my hand in fire that it couldn't be made simpler.
Код:
stock IsRPName(name[])
{
	new upos=strfind(name, "_");
	if(isnull(name)) return false;
	if(strlen(name)-2<upos<2) return false;
	for(new i = 0; i < 24; i++)
	{
		if(!name[i]) break;
		if(!i && 65 > name[i] > 90) return false;
		if(name[i] == 95 && i!=upos) return false;
		if(upos && i-upos == 1)
		{
			if(65 > name[i] > 90) return false;
		}
		if(65 <= name[i] <= 90)
		{
			if(!(!i || i==upos+1 || (i==2 && upos>5) || (i==upos+3 && strlen(name)-upos>5))) return false;
		}
		if(97 > name[i] > 122) return false;
	}
	if(upos==-1) return false;
	if(strlen(name)-upos<3) return false;
	return true;
}
Reply

This function checks if two line segments intersect.
pawn Код:
/*
Function:
SegmentsIntersect

Parameters:
Float:x1 - The X start point of the first line segment.
Float:y1 - The Y start point of the first line segment.
Float:x2 - The X end point of the first line segment.
Float:y2 - The Y end point of the first line segment.
Float:x3 - The X start point of the second line segment.
Float:y3 - The Y start point of the second line segment.
Float:x4 - The X end point of the second line segment.
Float:y4 - The Y end point of the second line segment.
Float:x - A float to store the X intersection point in, passed by reference.
Float:y - A float to store the Y intersection point in, passed by reference.

Returns:
0 - line segments do not intersect
1 - line segments intersect
*/

stock SegmentsIntersect(Float:x1, Float:y1, Float:x2, Float:y2, Float:x3, Float:y3, Float:x4, Float:y4, &Float:x, &Float:y)
{
    new
        Float:xA = x2 - x1,
        Float:yA = y2 - y1,
        Float:xB = x4 - x3,
        Float:yB = y4 - y3,
        Float:d  = xA * yB - yA * xB;
    if (!d)
    {
        // Lines are parallel, or one or both segments are zero-length
        return 0;
    }
    new
        Float:xC = x3 - x1,
        Float:yC = y3 - y1,
        Float:pA = (xC * yB - yC * xB) / d,
        Float:pB = (xC * yA - yC * xA) / d;
    if (pA < 0 || pA > 1 || pB < 0 || pB > 1)
    {
        return 0;
    }
    // Compute the intersection point
    x = x1 + pA * xA
    y = y1 + pA * yA
    return 1;
}
Reply

Stylock are you able to make a 3D version of that function?

It will be very useful, seeing as this is a 3D game.
Reply

Quote:
Originally Posted by EviLpRo
Посмотреть сообщение
Almost.
This is for the people that not use sscanf...
You shouldn't enable people to use bad scripting methods.
Reply

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
You shouldn't enable people to use bad scripting methods.
Right, but if they already used this bad methods, why not optimize that?
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Because then they'll never learn and move on.
It's true, but people that worked already with strtok, in most situation they have to work a lot to change it to sscanf.

Anyway i builded it as a challenge optimize it for me (not for use), i just wanted to see how much hard it to do that.
Reply

Quote:
Originally Posted by fiki574
Посмотреть сообщение
For example, using "random(100)" you can get a number ranging from 0-100(max, parameter in random), but with "randomNumber()" you choose how many digits you want your number to have.

pawn Код:
public OnFilterScriptInit()
{
    printf("random(100): %d", random(100));
    return 1;
}
Possible outputs:
Код:
random(100): 1
random(100): 5
random(100): 57
random(100): 98
And if you set "RND_NUM_DIG" to "3" for my function "randomNumber()":
pawn Код:
public OnFilterScriptInit()
{
    printf("Random number: %d", randomNumber());
    return 1;
}
Possible outputs:
Код:
Random number: 123
Random number: 359
Random number: 811
Random number: 925
NOTE: I may have not explained this properly, sorry, im not functioning okay today.
Not true, because with your function may happen that 0 is the first digit.
Reply

Quote:
Originally Posted by fiki574
Посмотреть сообщение
pawn Код:
// Code
pawn Код:
//Code
Possible outputs:
Код:
Random string: aTzuiJKLoFdacVb
Random number: 665
or
pawn Код:
stock randomString()
{
    new str[RND_STR_SIZE];
    for(new i = 0; i != RND_STR_SIZE; i++) str[i] = random(2) ? random(26) + 65 : random(26) + 97;
    return str;
}
Reply

Quote:
Originally Posted by OrMisicL
Посмотреть сообщение
or
pawn Код:
stock randomString()
{
    new str[RND_STR_SIZE];
    for(new i = 0; i != RND_STR_SIZE; i++) str[i] = random(2) ? random(26) + 65 : random(26) + 97;
    return str;
}
Where are the letters here?
Reply

Quote:
Originally Posted by fiki574
Посмотреть сообщение
Where are the letters here?
it generate letters from its ASCII code, just test it
Reply

pawn Код:
decode_doors(src, &bonnet, &boot, &driver_door, &passenger_door)
{
    new int[1];
    int[0] = src;
    bonnet = int{3};
    boot = int{2};
    driver_door = int{1};
    passenger_door = int{0};
    return 1;
}

EXAMPLE:
pawn Код:
encode_doors(bonnet, boot, driver_door, passenger_door, behind_driver_door, behind_passenger_door)
{
    #pragma unused behind_driver_door
    #pragma unused behind_passenger_door
    return bonnet | (boot << 8) | (driver_door << 16) | (passenger_door << 24);
}
decode_doors(src, &bonnet, &boot, &driver_door, &passenger_door)
{
    new int[1];
    int[0] = src;
    bonnet = int{3};
    boot = int{2};
    driver_door = int{1};
    passenger_door = int{0};
    return 1;
}

main()
{
 new bonnet, boot, driver_door, passenger_door;
 decode_doors(encode_doors(1, 2, 3, 4, 0, 0), bonnet, boot, driver_door, passenger_door);
 printf("%d | %d | %d | %d", bonnet, boot, driver_door, passenger_door); // Result: "1 | 2 | 3 | 4"
}
Any idea how to implement decode_panels?
Reply

whoever -repped me
shall be sent to jail for life
Reply

[] GetPlayerSex

[] Information:
Character sex verify the server.

[] function:
pawn Код:
native GetPlayerSex(playerid, bool:sex);;
pawn Код:
stock GetPlayerSex(playerid, bool:sex) {
    if(sex==true) { //man
    switch(GetPlayerSkin(playerid)) {
        case 0..8,14..30,32..38,42..52,57..62,66..68,70..73,78..84,86,94..128,132..137,142..144: return true;
        case 146..147,149,153..156,158..168,170..177,179..189,200,202..204,206,208..209,210,212,213,217: return true;
        case 220..223,227..230,234..236,239..242,247..250,252..255,264..297,299,258..262: return true; }
    } else { //woman
    switch(GetPlayerSkin(playerid)) {
        case 9..13,31,39..41,53..56,63..65,69,74..77,85,87..93,129..131,138..141: return true;
        case 145,148,150..152,157,169,172,178,190..199,201,205,207,211,214..216,218,219,224..226,231..233: return true;
        case 237..238,243..246,251,256,257,263,298: return true; }
    } return false;
}
[] example:
pawn Код:
COMMAND:test(playerid, params[]) {
        if(GetPlayerSex(playerid, false)) { //Man
            SendClientMessage(playerid, -1, "[TEST] You Man");
        }
        else if(GetPlayerSex(playerid, true) { //woman
            SendClientMessage(playerid, -1, "[TEST] You woman");
        }
        return true;
    }
[] Credits:
OTACON
Reply

Quote:
Originally Posted by OTACON
Посмотреть сообщение
[] GetPlayerSex
Well, guess what! There are only two possibilities (AFAIK) which means you can, for example, check for male and just return false for the others (to improve the function).
Reply

[] GetPlayerSex v2

[] Information:
Character sex verify the server.

[] function:
pawn Код:
native GetPlayerSex(playerid);
pawn Код:
stock GetPlayerSex(playerid) {
    switch(GetPlayerSkin(playerid)) { //man
        case 0..8,14..30,32..38,42..52,57..62,66..68,70..73,78..84,86,94..128,132..137,142..144: return true;
        case 146..147,149,153..156,158..168,170..177,179..189,200,202..204,206,208..209,210,212,213,217: return true;
        case 220..223,227..230,234..236,239..242,247..250,252..255,264..297,299,258..262: return true;
    } return false;
}
[] example:
pawn Код:
COMMAND:test(playerid, params[]) {
    if(GetPlayerSex(playerid)) { //Man
        SendClientMessage(playerid, -1, "[TEST] You Man");
    } else { //woman
        SendClientMessage(playerid, -1, "[TEST] You woman");
    } return true;
}
[] Credits:
OTACON
RyDeR (suggestion)
__________________________________________________ ______________

[] GetPlayerSkinColour

[] Information:
Check the Character Skin color on the Server.

[] Function:
pawn Код:
native GetPlayerSkinColour(playerid);
pawn Код:
stock GetPlayerSkinColour(playerid) {
    switch(!GetPlayerSkin(playerid)) { //black
        case 0,4..22,24,25,28,30,35,36,40,46..48,50,51,54,55,58,63..67,69,74,76,78..80,83,84,86: return true;
        case 91,98,102..107,131,134,136,139,140,142..146,149..150,156,163,166,168,176,180,182,183: return true;
        case 185,187,190,193,195,207,211,214,215,218..223,230,233,238,239,243..245,249,253,256,260: return true;
        case 262,263,265,267,269,270,271,274,275,278,279,284,293,296..298: return true;
    } return false;
}
[] example::
pawn Код:
COMMAND:test(playerid, params[]) {
    if(GetPlayerSkinColour(playerid)) { //white
        SendClientMessage(playerid, -1, "You white");
    } else { //black
        SendClientMessage(playerid, -1, "You black");
    } return true;
}
[] Creditos:
OTACON
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)