Useful Functions

^ could you give an example?
Reply

http://en.wikipedia.org/wiki/Fibonacci_number

Maybe use fibonacci(random(47)) for reaction tests?
Reply

I still don't understand where you can use the fibonacci function. Anyone examples or usage somewhere?
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
I still don't understand where you can use the fibonacci function. Anyone examples or usage somewhere?
+1 to that one.
Reply

Quote:
Originally Posted by Y_Less
View Post
You have to be VERY careful when using macros - please go read my topic on the preprocessor! In cases like that the overhead of calling a function is minimal compared to the overhead of the loop itself. What's more, that code will give warnings if they've already used "i" in their current function and will not work if you try use it as the statement it looks like:

pawn Code:
return ClearChatForPlayer(playerid), printf("done");
In general, unless you have a VERY good reason for making something a macro, stick to functions.
That's the reason I was "unsure" to use it in a macro format. I saw it some where on the forums one time that you said to try to use functions over macros. (@ Your macro topic).
Reply

Code:
GetPlayerIDFromIP(ip[])
{
    new IP[16], playerid;
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
	    if(IsPlayerConnected(i))
		{
		    GetPlayerIp(i, IP, sizeof(IP));
			if(strcmp(ip, IP, true)==0) return i = playerid;
        }
        else return INVALID_PLAYER_ID;
	}
	return playerid;
}
It can be used with the callback "OnRconLoginAttempt"
Code:
public OnRconLoginAttempt(ip[], password[], success)
{
    new nome[24], str[100], playerid;
    if(!success)
    {
        playerid = GetPlayerIDFromIP(ip);
        GetPlayerName(playerid, nome, sizeof(nome));
        format(str, 100, "%s has entered a wrong password", nome);
        SendClientMessageToAll(COLOR_WHITE, str);
    }
    return 1;
}
Sorry for my bad english :P
Reply

Code can be optimized. Just return if it's find if not just INVALID_PLAYER_ID also use !IsPlayerNPC(playerid) to skip bot checks.
pawn Code:
stock GetPlayerIDFromIP(ip[])
{
    new
        pIp[16]
    ;
    for(new i; i != MAX_PLAYERS; ++i)
    {
        if(IsPlayerConnected(i) && !IsPlayerNPC(i))
        {
            GetPlayerIP(i, pIp, sizeof(pIp));
           
            if(!strcmp(ip, pIp, true)) return i;
        }
    }
    return INVALID_PLAYER_ID;
}
Hope you don't mind. Just trying to help.
Reply

Код:
GetPlayerVehicleAngle(playerid, &Float:x, &Float:y, &Float:z);
QuatToEulers(Float:qw, Float:qx, Float:qy, Float:qz, &Float:rX, &Float:rY, &Float: rZ);
Seen a post about this the other day searched a few sites and put this together, only to see that ev0lutionnn and betamaster already had something similar, thing is I couldn't even find betamasters on this site and ev0lutionnn's is an include and sort of hard to find (wasn't for me after I scripted this though xD ). I haven't looked at ev0lutionnn's but I seen betamasters and what (I think) is different about my function is it returns 0.0 to 360.0 for the Eulers not 180.0 to -180.0 altho thats completely valid I prefer the other way... Thanks to Tannz0rz for that magic line lol.


pawn Код:
stock GetPlayerVehicleAngle(playerid, &Float:x, &Float:y, &Float:z)
{
    if(IsPlayerInAnyVehicle(playerid))
    {
        new
            Float:qw,
            Float:qx,
            Float:qy,
            Float:qz;

        GetVehicleRotationQuat(GetPlayerVehicleID(playerid), qw, qx, qy, qz);
        QuatToEulers(qw, qx, qy, qz, x, y, z);
        return 1;
    }
    else return 0;
}


stock QuatToEulers(Float:qw, Float:qx, Float:qy, Float:qz, &Float:rX, &Float:rY, &Float: rZ)
{
/**Can be non-normalised/normalised Quaternion will return Eulers Angle*/
    new Float:sqw = qw*qw;
    new Float:sqx = qx*qx;
    new Float:sqy = qy*qy;
    new Float:sqz = qz*qz;
    new Float:unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
    new Float:test = qx*qy + qz*qw;


//Check for Gimbal Lock
    if (test > 0.499*unit) { // singularity at north pole
        rX = 2 * atan2(qx,qw);
        rZ = 3.1415926535/2;
        rY = 0;
        while(rX < 0.0) rX += 360.0; while(rX > 360.0) rX -= 360.0;
        while(rZ < 0.0) rZ += 360.0; while(rZ > 360.0) rZ -= 360.0;
        return 1;
    }

    if (test < -0.499*unit) { // singularity at south pole
        rX = -2 * atan2(qx,qw);
        rZ = -3.1415926535/2;
        rY = 0;
        while(rX < 0.0) rX += 360.0; while(rX > 360.0) rX -= 360.0;
        while(rZ < 0.0) rZ += 360.0; while(rZ > 360.0) rZ -= 360.0;
        return 1;
    }

//No Gimbal Lock
    rX = atan2(2*qy*qw-2*qx*qz , sqx - sqy - sqz + sqw);
    rZ = asin(2*test/unit);
    rY = atan2(2*(qx*qw-2)*(qy*qz) , -sqx + sqy - sqz + sqw);
    while(rX < 0.0) rX += 360.0; while(rX > 360.0) rX -= 360.0;
    while(rY < 0.0) rY += 360.0; while(rY > 360.0) rY -= 360.0;
    while(rZ < 0.0) rZ += 360.0; while(rZ > 360.0) rZ -= 360.0;
    return 1;
}
If this is the same as all the others feel free to delete it.
Reply

Код:
stock ResetNormalGravity()
{
	SetGravity(0.008);
}

stock GetPlayerIDFromName(const name[])
{
	new pname[MAX_PLAYER_NAME];
	for(new i = 0;i<MAX_PLAYERS;i++)
	{
		GetPlayerName(playerid, pname, sizeof(pname));
		if(pname == name) return i;
	}
	return -1;
}
Reply

It should be like this.

pawn Код:
stock GetPlayerIDFromName(const name[])
{
    new playerName[MAX_PLAYER_NAME];
        for(new x = 0; x < MAX_PLAYERS; x++)
        {
                GetPlayerName(x, playerName, sizeof(playerName));
                if(strcmp(playerName, name) == 0) return x;
        }
        return -1;
}
Reply

You better do if(GetPlayerName(...)) { } because it has an inbuild IsPlayerConnected() check. You never check if the player is connected,which will make the code useless at all, because it will always return 0.
Besides I would recommend using INVALID_PLAYER_ID instead of -1.
Reply

pawn Код:
#define INVALID_PLAYER_ID (0xFFFF)
0xFFFF equals to -1
Reply

Quote:
Originally Posted by Luka P.
Посмотреть сообщение
pawn Код:
#define INVALID_PLAYER_ID (0xFFFF)
0xFFFF equals to -1
No, INVALID_PLAYER_ID equals 65535 (2^16).


https://sampwiki.blast.hk/wiki/Hex


It's a little short, but it gets the idea across. I'll be expanding it some time.
Reply

@Kyosaur:
It's ((256 ^ 2) - 1).
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
@Kyosaur:
It's ((256 ^ 2) - 1).
True, but actually

15 * (16^3) + 15 * (16^2) + 15 * (16^1) + 15
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
@Kyosaur:
It's ((256 ^ 2) - 1).
I can provide hundreds of ways to get the number 65535. There is no SINGLE correct way to represent the value because in the end it is the SAME number lol. Kind of a silly post :P.


EDIT:

Quote:
Originally Posted by LarzI
Посмотреть сообщение
True, but actually

15 * (16^3) + 15 * (16^2) + 15 * (16^1) + 15
Someone's been reading up on hex, nice .


Its like i said before though, there isnt a completely correct way to represent the value. I chose 2^16 for 2 reasons. The first is that its simple, and it makes complete sense (if you know anything about binary its clear how i got a base of 2 ... no idea where ryders 256 came from, but it does equal the right value). The second reason is that im thinking Kalcor uses an unsigned short (16 bit integer) in C(++) to store playerids. Since that's likely the case i thought the binary representation would be best.
Reply

Quote:
Originally Posted by Kyosaur
Посмотреть сообщение
I can provide hundreds of ways to get the number 65535. There is no SINGLE correct way to represent the value because in the end it is the SAME number lol. Kind of a silly post :P.


EDIT:



Someone's been reading up on hex, nice .


Its like i said before though, there isnt a completely correct way to represent the value. I chose 2^16 for 2 reasons. The first is that its simple, and it makes complete sense (if you know anything about binary its clear how i got a base of 2 ... no idea where ryders 256 came from, but it does equal the right value). The second reason is that im thinking Kalcor uses an unsigned short (16 bit integer) in C(++) to store playerids. Since that's likely the case i thought the binary representation would be best.
Yep i've readed too good post thanks!

Quote:
Originally Posted by nemesis-
Посмотреть сообщение
*facepalm*

Dumbass.
Dumbass? what did he do? and aren't you too?
Reply

Quote:
Originally Posted by DarK TeaM PT
Посмотреть сообщение
Yep i've readed too good post thanks!



Dumbass? what did he do? and aren't you too?
Can you honestly not read?
Reply

Yes i read...

its because this

It's ((256 ^ 2) - 1).

I thought it was right and btw why the hell are you always rude to ppl?...
Reply

Quote:
Originally Posted by DarK TeaM PT
Посмотреть сообщение
Yes i read...

its because this

It's ((256 ^ 2) - 1).

I thought it was right and btw why the hell are you always rude to ppl?...
RyDeR` tried to correct him where there's more than 1 method to get to the number 65535 - can you not read?

And I'm rude to people who are retarded, stupid or just generally incompetent. Pick what you think matches you the closest.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)