Useful Functions

Quote:
Originally Posted by DarK TeaM PT
Посмотреть сообщение
you sure it is made by you?...

heres a code that i found on my server

pawn Код:
stock
    RGB(red, green, blue, alpha)
{
    /*  Combines a color and returns it, so it can be used in functions.
        @red:           Amount of red color.
        @green:         Amount of green color.
        @blue:          Amount of blue color.
        @alpha:         Amount of alpha transparency.

        -Returns:
        A integer with the combined color.
    */

    return (red * 16777216) + (green * 65536) + (blue * 256) + alpha;
}
i know its slower and all but it calculates the same method as you so...
Yes i am sure, however i know im not the first by any means. There are tons of implementations floating around. Just so you dont think i am trolling, i will explain it. First, i would like to explain that the RGBAToInt1 is pretty much the same as RGBAToInt2 (all of them are really), the only difference is it doesnt use the binary operators.

Lets take a look at both

Код:
R << 24
G << 16
B << 8
A
and

Код:
R * 16777216
G * 65536
B * 256
A
Binary arithmetic shifts are the same as multiplying by the power of the shift. So:

Код:
R << 24 
= 
R * 2^24
=
R * 16777216

//and

G << 16
=
G * 2^16
=
G * 65536

//and

B << 8
=
B * 2^8
=
256
From this you can see how different implementations are possible. Why are we even doing this though, and why are all the powers always incremented by 8? Well, the reason we're doing this is because we want to pretty much acheive the same thing as THIS (Second code block) we're storing four 8-bit integers (0-255) inside of this single variable (we can fit them all because pawn has 32 bits available for storage, and guess what 8*4 is :P). This is why we are multiplying and/or shifting our number over 8 bits. lets take the color "100 0 255" as an example and see how the function handles it/break it down into binary.

Код:
(100 * 16777216) + (0 * 65536) + (255 * 256) + 0 //alpha is out of the picture 

for now

//which equals

1677721600 + 0 + 65280 + 0

//which equals

1677786880  //which is correct
Now lets take a look at the binary side (its a LOT clearer)

Код:
0b01100100000000000000000000000000 //31,30, and 27 turned on (1677721600)
+
0
+
0b00000000000000001111111100000000 //9-16 bits turned on (65280)
+
0
=
0b01100100000000001111111100000000 //which is (1677786880)
Notice how each value (RGBA) has 8bits? lets look at the number 1677786880 with colors

01100100000000001111111100000000

if you convert those binary values, you'll get the original results (red=100,green=0,blue=255,alpha=0).Thats pretty much all there is to this really. If your wondering "why 8bit?" its because the color depth we are using is 8bit. There are higher options for color depth, but we cant store them all in pawn (work out r=255 g=255 b=255 a=255 and you'll see why).

I can go into a bit more detail, but i think its best to keep it simple (dont want a mile long post here).


Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
good but even DracoBlue released a HexToInt function in his DUtils (ofc it contains other useful functions)
But with his function you can insert strings

pawn Код:
/**
 *  Return the value of an hex-string
 *  @param string
 */

stock HexToInt(string[]) {
  if (string[0]==0) return 0;
  new i;
  new cur=1;
  new res=0;
  for (i=strlen(string);i>0;i--) {
    if (string[i-1]<58) res=res+cur*(string[i-1]-48); else res=res+cur*(string[i-1]-65+10);
    cur=cur*16;
  }
  return res;
}
That has nothing to do with my defines at all lol, i guess you dont get it :\.
Reply

Oh lol, sorry about that but you never said that the color is notated in decimal (only that crap with the command)
And such a /color command is still useless, if rly needed than make something like the RGBA CONVERTER you posted in game

And simplify your RGBAToInt function, calculation with large numbers takes longer than with small numbers (although its not much)
Reply

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Oh lol, sorry about that but you never said that the color is notated in decimal (only that crap with the command)
I did in my first reply to you, while describing the online tool. Sorry if i worded it poorly, was in a bit of a hurry.

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
And such a /color command is still useless, if rly needed than make something like the RGBA CONVERTER you posted in game
Thats what this is lol :P, it takes RGBA and turned it into an integer. Sorry, still seems to be confusion between is D:

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
And simplify your RGBAToInt function, calculation with large numbers takes longer than with small numbers (although its not much)
Unfortunately its at the lowest level it can be (afaik), if you see something i dont, im all ears. Im all for improvements.
Reply

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
Yes i am sure, however i know im not the first by any means. There are tons of implementations floating around. Just so you dont think i am trolling, i will explain it. First, i would like to explain that the RGBAToInt1 is pretty much the same as RGBAToInt2 (all of them are really), the only difference is it doesnt use the binary operators.

Lets take a look at both

Код:
R << 24
G << 16
B << 8
A
and

Код:
R * 16777216
G * 65536
B * 256
A
Binary arithmetic shifts are the same as multiplying by the power of the shift. So:

Код:
R << 24 
= 
R * 2^24
=
R * 16777216

//and

G << 16
=
G * 2^16
=
G * 65536

//and

B << 8
=
B * 2^8
=
256
From this you can see how different implementations are possible. Why are we even doing this though, and why are all the powers always incremented by 8? Well, the reason we're doing this is because we want to pretty much acheive the same thing as THIS (Second code block) we're storing four 8-bit integers (0-255) inside of this single variable (we can fit them all because pawn has 32 bits available for storage, and guess what 8*4 is :P). This is why we are multiplying and/or shifting our number over 8 bits. lets take the color "100 0 255" as an example and see how the function handles it/break it down into binary.

Код:
(100 * 16777216) + (0 * 65536) + (255 * 256) + 0 //alpha is out of the picture 

for now

//which equals

1677721600 + 0 + 65280 + 0

//which equals

1677786880  //which is correct
Now lets take a look at the binary side (its a LOT clearer)

Код:
0b01100100000000000000000000000000 //31,30, and 27 turned on (1677721600)
+
0
+
0b00000000000000001111111100000000 //9-16 bits turned on (65280)
+
0
=
0b01100100000000001111111100000000 //which is (1677786880)
Notice how each value (RGBA) has 8bits? lets look at the number 1677786880 with colors

01100100000000001111111100000000

if you convert those binary values, you'll get the original results (red=100,green=0,blue=255,alpha=0).Thats pretty much all there is to this really. If your wondering "why 8bit?" its because the color depth we are using is 8bit. There are higher options for color depth, but we cant store them all in pawn (work out r=255 g=255 b=255 a=255 and you'll see why).

I can go into a bit more detail, but i think its best to keep it simple (dont want a mile long post here).




That has nothing to do with my defines at all lol, i guess you dont get it :\.

okay bro you command
Reply

Easiest, shortest and fastest ticks converter that I was able to make, for races of whatever else you need.

Код:
stock TicksConvert(ticks) {
	new d, h, m, s, ms;
	d = ticks/86400000;
	h = ticks/3600000 % 24;
	m = ticks/60000 % 60;
	s = ticks/1000 % 60;
	ms = ticks % 1000;
	new str[128];
	format(str, 128, "%d days, %d hours, %d minutes, %d seconds, %d milliseconds", d, h, m, s, ms);
	// format(str, 128, "%d:%02d:%03d", m, s, ms);
	// format(str, 128, "%d mins, %d secs, %d msecs", m, s, ms);
	return str;
}
and super short version for races usage only:

Код:
stock TicksConvert(ticks) {
	new str[128];
	format(str, 128, "%d:%02d:%03d", ticks/60000 % 60, ticks/1000 % 60, ticks % 1000);
	return str;
}
Reply

pawn Код:
forward IsPlayerInAnyPlane(playerid);
    public IsPlayerInAnyPlane(playerid) {
        if(IsPlayerInAnyVehicle(playerid)) {
            new FXF_vehicle = GetVehicleModel(GetPlayerVehicleID(playerid));
            if(FXF_vehicle == 460 || FXF_vehicle == 476 || FXF_vehicle == 511 || FXF_vehicle == 512 || FXF_vehicle == 513 || FXF_vehicle == 519 || FXF_vehicle == 520 || FXF_vehicle == 553 || FXF_vehicle == 577 || FXF_vehicle == 592 || FXF_vehicle == 593) {
                return 1; } }
        return 0; }
    forward IsPlayerInAnyHelicopter(playerid);
    public IsPlayerInAnyHelicopter(playerid) {
        if(IsPlayerInAnyVehicle(playerid)) {
            new FXF_vehicle = GetVehicleModel(GetPlayerVehicleID(playerid));
            if(FXF_vehicle == 417 || FXF_vehicle == 425 || FXF_vehicle == 447 || FXF_vehicle == 469 || FXF_vehicle == 487 || FXF_vehicle == 488 || FXF_vehicle == 497 || FXF_vehicle == 548 || FXF_vehicle == 563) {
                return 1; } }
        return 0; }
    forward IsPlayerOnAnyBoat(playerid);
    public IsPlayerOnAnyBoat(playerid) {
        if(IsPlayerInAnyVehicle(playerid)) {
            new FXF_vehicle = GetVehicleModel(GetPlayerVehicleID(playerid));
            if(FXF_vehicle == 430 || FXF_vehicle == 446 || FXF_vehicle == 452 || FXF_vehicle == 453 || FXF_vehicle == 454 || FXF_vehicle == 472 || FXF_vehicle == 473 || FXF_vehicle == 484 || FXF_vehicle == 493 || FXF_vehicle == 595) {
                return 1; } }
        return 0; }
    forward IsPlayerOnAnyBike(playerid);
    public IsPlayerOnAnyBike(playerid) {
        if(IsPlayerInAnyVehicle(playerid)) {
            new FXF_vehicle = GetVehicleModel(GetPlayerVehicleID(playerid));
            if(FXF_vehicle == 448 || FXF_vehicle == 461 || FXF_vehicle == 462 || FXF_vehicle == 463 || FXF_vehicle == 468 || FXF_vehicle == 471 || FXF_vehicle == 481 || FXF_vehicle == 409 || FXF_vehicle == 510 || FXF_vehicle == 521 || FXF_vehicle == 522 || FXF_vehicle == 523 || FXF_vehicle == 581 || FXF_vehicle == 586) {
                return 1; } }
        return 0; }
Reply

oh my god, indentation please
Reply

oh my god, don't post random shit... Those was old.

EDIT: And it has indention.
Reply

Quote:
Originally Posted by Skiaffo
Посмотреть сообщение
Easiest, shortest and fastest ticks converter that I was able to make, for races of whatever else you need.

Код:
stock TicksConvert(ticks) {
	new d, h, m, s, ms;
	d = ticks/86400000;
	h = ticks/3600000 % 24;
	m = ticks/60000 % 60;
	s = ticks/1000 % 60;
	ms = ticks % 1000;
	new str[128];
	format(str, 128, "%d days, %d hours, %d minutes, %d seconds, %d milliseconds", d, h, m, s, ms);
	// format(str, 128, "%d:%02d:%03d", m, s, ms);
	// format(str, 128, "%d mins, %d secs, %d msecs", m, s, ms);
	return str;
}
and super short version for races usage only:

Код:
stock TicksConvert(ticks) {
	new str[128];
	format(str, 128, "%d:%02d:%03d", ticks/60000 % 60, ticks/1000 % 60, ticks % 1000);
	return str;
}
Thanks, pretty useful.
Reply

Why use publics, and why not use switch?
Reply

Here are a few simple functions I want to share:

toUpper
This function already exist but if someone wants the function here you go:
pawn Код:
stock toUpper(c)
{
    return ('a' <= c <= 'z') ? (c += 'A' - 'a') : (c);
}
isCharUpper
Returns true if the char is in upper case; otherwise false.
pawn Код:
stock isCharUpper(c)
{
    return ('A' <= c <= 'Z');
}
toLower
Same as toUpper.
pawn Код:
stock toLower(c)
{
    return ('A' <= c <= 'Z') ? (c += 'a' - 'A') : (c);
}
isCharLower
Returns true if the char is in lower case; otherwise false.
pawn Код:
stock isCharLower(c)
{
    return ('a' <= c <= 'z');
}
Reply

Nicely done, though I think it's already made.
But oh well, good effort anyway
Reply

Quote:
Originally Posted by LarzI
Посмотреть сообщение
Nicely done, though I think it's already made.
But oh well, good effort anyway
Thanks, makes me always happy when I see comments like this. (:
Reply

I prefer to keep up the good mood while I can ^^
Reply

IntToRGBA(Number, &Red, &Green, &Blue, &Alpha); by kyoshiro aka kyosaur

What is this?

This define is the inverse to THIS. It takes a color and returns its RGBA values. Its probably not as useful as the RGBAToInt function, but i decided to make it anyways for shits and giggles. Once again i made multiple versions and ran speed tests, the results are at the bottom.


Note: This can not be used inside of an if statement. I have no idea why someone would want to put it in one, i just thought i would say so for the n00bs.


Code (for those who dont care about the tests):

pawn Код:
#define IntToRGBA(%0,%1,%2,%3,%4) \
(%1) = ((%0) >>> 24); (%2) = (((%0) >>> 16) & 0xFF); (%3) = (((%0) >>> 8) & 0xFF); (%4) = ((%0) & 0xFF)

Speed results (10,000,000 loops)

Unfortunately i couldnt think of a way to break it down any further. If anyone can think of an improvement or another method, im all ears.

pawn Код:
#define IntToRGBA(%0,%1,%2,%3,%4) \
(%1) = ((%0) >>> 24); (%2) = (((%0) >>> 16) & 0xFF); (%3) = (((%0) >>> 8) & 0xFF); (%4) = ((%0) & 0xFF)

#define IntToRGBA2(%0,%1,%2,%3,%4) \
(%1) = ((%0) >>> 24); (%2) = (((%0) << 8) >>> 24); (%3) = (((%0) << 16) >>> 24); (%4) = (((%0) << 24) >>> 24)

#define IntToRGBA3(%0,%1,%2,%3,%4) \
(%1) = ((%0) >>> 24); (%2) = (((%0) * 256) >>> 24); (%3) = (((%0) * 65536) >>> 24); (%4) = (((%0) * 16777216) >>> 24)


IntToRGBA(1): R:28 || G:28 || B:81 || A:6 || Time: 4199ms
IntToRGBA(2): R:28 || G:28 || B:81 || A:6 || Time: 4332ms
IntToRGBA(3): R:28 || G:28 || B:81 || A:6 || Time: 4357ms

IntToRGBA(1): R:93 || G:173 || B:182 || A:3 || Time: 4147ms
IntToRGBA(2): R:93 || G:173 || B:182 || A:3 || Time: 4300ms
IntToRGBA(3): R:93 || G:173 || B:182 || A:3 || Time: 4310ms
Reply

nice one ryder but im going to kill your party :P

by ******

pawn Код:
#define toupper(%0) \
    (((%0) >= 'a' && (%0) <= 'z') ? ((%0) & ~0x20) : (%0))

#define tolower(%0) \
    (((%0) >= 'A' && (%0) <= 'Z') ? ((%0) | 0x20) : (%0))
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Here are a few simple functions I want to share:

toUpper
This function already exist but if someone wants the function here you go:
pawn Код:
stock toUpper(c)
{
    return ('a' <= c <= 'z') ? (c += 'A' - 'a') : (c);
}
isCharUpper
Returns true if the char is in upper case; otherwise false.
pawn Код:
stock isCharUpper(c)
{
    return ('A' <= c <= 'Z') ? (1) : (0);
}
toLower
Same as toUpper.
pawn Код:
stock toLower(c)
{
    return ('A' <= c <= 'Z') ? (c += 'a' - 'A') : (c);
}
isCharLower
Returns true if the char is in lower case; otherwise false.
pawn Код:
stock isCharLower(c)
{
    return ('a' <= c <= 'z') ? (1) : (0);
}
Quote:
Originally posted by core.inc
native tolower©;
native toupper©;

You can improve your isCharUpper/Lower by making it a define and also removing the triadic operators.
Reply

I know, I know. But I leave it as it is.
Reply

Smooth object rotation

RotateObject(objectid, axis, Float:extent, Float:rspeed)
Top:

Код:
#define rotx 1
#define roty 2
#define rotz 3

forward RotateObject(objectid, axis, Float:extent, Float:rspeed);
Bottom:

Код:
public RotateObject(objectid, axis, Float:extent, Float:rspeed) {
	if (rspeed <= 0.0) return 1;
	if ((extent > 0.0) && (extent < rspeed/10)) rspeed = extent*10;
	if ((extent < 0.0) && (extent > -rspeed/10)) rspeed = -extent*10;
	if (extent == 0.0) return 1;
	new Float:rx, Float:ry, Float:rz;
	GetObjectRot(objectid, rx, ry, rz);
	switch (axis) {
	    case rotx: SetObjectRot(objectid, rx + ((extent > 0.0) ? (rspeed/10) : (-rspeed/10)), ry, rz);
	    case roty: SetObjectRot(objectid, rx, ry + ((extent > 0.0) ? (rspeed/10) : (-rspeed/10)), rz);
	    case rotz: SetObjectRot(objectid, rx, ry, rz + ((extent > 0.0) ? (rspeed/10) : (-rspeed/10)));
	}
	SetTimerEx("RotateObject", 10, 0, "ddff", objectid, axis, ((extent > 0.0) ? (extent - rspeed/10) : (extent + rspeed/10)), rspeed);
	return 1;
}
How to use this?:
Example:

Top:

Код:
new policebar;
OnGameModeInit:

Код:
policebar = CreateObject(...);
Everywhere, for example OnPlayerCommandText:

Код:
if (!strcmp(cmdtext, "/openbar", true)) {
    RotateObject(policebar, rotx, 90.0, 0.5); // Just an example, not sure this values will work for the object u putted, you need to find out the correct values depending on where you placed the object.
    return 1;
}
Possibilities of usage:
- It's possible to use negative values for the degrees if you want to rotate the object clockwise (positive values will rotate the object anticlockwise).

Example:

Код:
if (!strcmp(cmdtext, "/rot", true)) {
    RotateObject(objectid, rotz, -90.0, 1.0);
    return 1;
}
- It's possible to rotate an object on more axis at the same time, using also different speed and different degrees if u wish.

Example:

Код:
if (!strcmp(cmdtext, "/rot", true)) {
    RotateObject(objectid, rotz, -90.0, 1.0);
    RotateObject(objectid, rotx, 270.0, 5.0);
    RotateObject(objectid, roty, -180.0, 3.5);
    return 1;
}
- It's possible to put a > 360 value if you want to rotate the object more then once.

Example:

Код:
if (!strcmp(cmdtext, "/rot", true)) {
    RotateObject(objectid, rotz, 1440.0, 5.0); // Object will spin 4 times and then stop (1440/360 = 4)
    return 1;
}
Known bugs:
- No one so far.

-----------------

Hope you apprecciate
Reply

pawn Код:
stock fibonacci(n)
{
    new fn;
    if(n<1)
        fn=0;
    else if(n==1)
        fn=1;
    else
    {
        new f0=0,f1=1;
        for(new i=2;i<=n;i++)
        {
            fn=f1+f0;
            f0=f1;
            f1=fn;
        }
    }
    return fn;
}
Maybe useful, but probably not. Function will not return correct values after n=46.

pawn Код:
stock wait(time)
{
    new t1=GetTickCount();
    do {}
    while((GetTickCount()-t1)<time);
}
This may already exist, but w/e.
Reply


Forum Jump:


Users browsing this thread: 31 Guest(s)