Useful Functions

Quote:
Originally Posted by Nubotron
QuatToEuler(Float:qx, Float:qy, Float:qz, Float:qw, &Float:ex, &Float:ey, &Float:ez, bool:normalize = false)

edit: bug, work 50%..
rofl , for what is this ?
Reply

Quote:
Originally Posted by MoroJr™
Quote:
Originally Posted by Nubotron
QuatToEuler(Float:qx, Float:qy, Float:qz, Float:qw, &Float:ex, &Float:ey, &Float:ez, bool:normalize = false)

edit: bug, work 50%..
rofl , for what is this ?
Re-edited, i was wrong, it working 100% i think!

I thinked it was wrong because i tested with normal objects models, not LOD models (i thinked normal and LOD objects was same rotation, but it is not always true!)
Reply

Quote:
Originally Posted by zozo
Re-edited, i was wrong, it working 100% i think!
No it doesn't work 100%.. in some cases it will return totally wrong angles. I will try to fix that some time.. and i told you to not share it anyway, a bugged function is all but useful..
Reply

Quote:
Originally Posted by 0rb
Quote:
Originally Posted by zozo
Re-edited, i was wrong, it working 100% i think!
No it doesn't work 100%.. in some cases it will return totally wrong angles. I will try to fix that some time.. and i told you to not share it anyway, a bugged function is all but useful..
Indeed it is cake!
Reply


• PlayerToPlayer •

From the PlayerToPoint Function


pawn Код:
stock PlayerToPlayer(playerid, destid, Float:radius)
{
    if(!IsPlayerConnected(playerid)) return -1; // Checks if player is connected
    {
        new Float:X, Float:Y, Float:Z; // Base x, y, z values
        new Float:px, Float:py, Float:pz; //px, py, pz used to express
        GetPlayerPos(playerid, X, Y, Z); //
        GetPlayerPos(destid, px, py, pz); //
        X = floatsub(X, px);
        Y = floatsub(Y, py);
        Z = floatsub(Z, pz);
        if( ( radius > X && X > -radius ) &&
        ( radius > Y && Y > -radius ) &&
        ( radius > Z && Z > -radius ) ) return 1;
    }
    return 0;
}
Reply

TurnPlayerFaceToPlayer
Turn another player's facing angle to a player.
pawn Код:
stock TurnPlayerFaceToPlayer(playerid, facingtoid)
{
    new Float:angle;
    new Float:misc = 5.0;
    new Float:x, Float:y, Float:z;
    new Float:ix, Float:iy, Float:iz;
    GetPlayerPos(facingtoid, x, y, z);
    GetPlayerPos(playerid, ix, iy, iz);
    angle = 180.0-atan2(ix-x,iy-y);
    angle += misc;
    misc *= -1;
    SetPlayerFacingAngle(playerid, angle+misc);
}
Leopard
Reply

Maybe add a SetPlayerCameraLookAt too ?
Reply

Quote:
Originally Posted by lrZ^ aka LarzI
Maybe add a SetPlayerCameraLookAt too ?
Nah just use PutCameraBehindPlayer or whatever the function is called.
Reply

Ah ofc :P
Reply

PlayerVehicleToPoint
By Pyrokid

Credits go to whoever made PlayerToPoint. All I did was modify it to work for vehicles so you can use it for pickups.
pawn Код:
stock PlayerVehicleToPoint(playerid,Float:x,Float:y,Float:z)
{
  if(IsPlayerConnected(playerid))
    {
        new Float:oldposx, Float:oldposy, Float:oldposz;
        new Float:tempposx, Float:tempposy, Float:tempposz;
        tempposx = (oldposx -x);
        tempposy = (oldposy -y);
        tempposz = (oldposz -z);
        if(IsPlayerInAnyVehicle(playerid))
        {
            GetVehiclePos(GetPlayerVehicleID(playerid), oldposx, oldposy, oldposz);
            if (((tempposx < 3) && (tempposx > -3)) && ((tempposy < 3) && (tempposy > -3)) && ((tempposz < 3) && (tempposz > -3)))
            {
                return 1;
            }
        } else {
            if (((tempposx < 3) && (tempposx > -3)) && ((tempposy < 3) && (tempposy > -3)) && ((tempposz < 3) && (tempposz > -3)))
            {
                return 1;
            }
        }
    }
    return false;
}
Usage
I did this basically for picking up pickups if you're in a vehicle.
pawn Код:
//Top of script
forward timer1();

//OnGameModeInit
SetTimer("timer1", 1000, 1); //Every second
CreatePickup(1550,23,2302.2800,1387.9668,42.4453); //Example pickup

//Anywhere
public timer1()
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
      {
            if(PlayerVehicleToPoint(i,2302.2800,1387.9668,42.4453)) //Is the same coordinates of the example pickup
            {
              new vehid = GetPlayerVehicleID(i);
              SetVehiclePos(vehid,5555,5555,5555);
              PutPlayerInVehicle(i,vehid,0);
            }
        }
    }
    return 1;
}
That's it. Now you can do vehicle pickups. Tested and works. This is my first attempt at making a function so if there is a more efficient or better way to do it, please do it! Again credits go to whoever made PlayerToPoint because all I did was modify it a tiny bit.

* Revision 1 on 4/23/09
Reply

Код:
stock GetFileDataFromSelectLine(filename[], line, result[])
{
	new File:FileHandle;
	new LineData[1024 char];
	new LoadCount = 0;

	if(!fexist(filename))
	{
		printf("Could not open \"%s\" ",filename);

		return false;
	}

	FileHandle = fopen(filename, io_read);

	while(fread(FileHandle, LineData))
	{
		if(LoadCount == (line - 1))
		{
			if(LineData[0] == 0)
			{
				continue;
			}
			
			if((LineData[strlen(LineData) - 1] == '\n') || (LineData[strlen(LineData) - 1] == '\r'))
			{
				LineData[strlen(LineData) - 1] = 0;

				if((LineData[strlen(LineData) - 2] == '\n') || (LineData[strlen(LineData) - 2] == '\r'))
				{
					LineData[strlen(LineData) - 2] = 0;
				}
				
				if(LineData[0] == 0)
				{
					continue;
				}
			}
			format(result, sizeof(LineData), "%s", LineData);
			
			fclose(FileHandle);
			return true;
		}
		LoadCount += 1;
	}
	fclose(FileHandle);
	return false;
}
you can get strings from one line of file to a variable.

please write like this code when you want to get string from line 4 of settings.txt to variable "str".
Код:
GetFileDataFromSelectLine("settings.txt", 4, str)
Reply

RGB to hex converter. RGBToHex(RValue,GValue,BValue,dest[]) Here's the function and usage.

http://samp.pastebin.com/mc389038
Reply

Clear A File
pawn Код:
stock fclear(File[])
{
    new File:FileC;
    FileC = fopen(File, io_write);
    fwrite(File, " ");
    fclose(File);
    return 1;
}
Reply

Quote:
Originally Posted by ► James_Alex
Clear A File
pawn Код:
stock fclear(File[])
{
    new File:FileC;
    FileC = fopen(File, io_write);
    fwrite(File, " ");
    fclose(File);
    return 1;
}
You should (IMO) check the file exists and was created so no bugs can occur. It would also be good to have a return value so you can check if the file was cleared or not.

pawn Код:
fclear( file[] )
{
  if ( file[ 0 ] && fexists( file ) )
  {
    fremove( file );
    new File: f = fopen( file, io_write );
    if ( f )
    {
      fclose( f );
      return true;
    }
  }
  return false;
}
Example:

pawn Код:
if ( fclear( "some_file.txt" ) ) print( "'some_file.txt' was cleared" );
else print( "failed to clear 'some_file.txt'" );
Reply

I guess its about time I contributed something, to bad its just an optimized versions of someone else's function

The good thing is, its optimized according to ******'s optimization code thread. I have tested this with GetClosestPlayer and it worked fine, I tested it on my new version of LSA with only one person but it worked, can't tell you how much it improved processing tho as I haven't tested that just if it worked, I assumed going by what ****** said that it should be faster either way.

Credits to the original creator. (Sorry don't know who it was)

pawn Код:
public Float:GetDistanceBetweenPlayers(p1,p2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.00;
    }
    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
    return floatsqroot((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
}
The line improved is this
pawn Код:
return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
Reply

pawn Код:
floatsqroot((x2-=x1)*x2 + (y2-=y1)*y2 + (z2-=z1)*z2);
Reply

Quote:
Originally Posted by paytas
pawn Код:
floatsqroot((x2-=x1)*x2 + (y2-=y1)*y2 + (z2-=z1)*z2);
Thanks
Reply

Clean a mysql table.
pawn Код:
stock TruncateTable(table[])
{
  format(string,sizeof(string),"TRUNCATE TABLE `%s`",table);
  samp_mysql_real_escape_string(table,table);
  samp_mysql_query(string);
  return 1;
}
Reply

A safe query.
pawn Код:
stock query(string[])
{
  if(samp_mysql_ping>=1)
  {
    samp_mysql_connect("host","user","password");
    samp_mysql_select_db("database");
    print("Attempting to re-connect due to losing connection.");
  }
  else
  {
    samp_mysql_real_escape_string(string,string); // To escape invalid characters.
    samp_mysql_query(string);
  }
}
Reply

Quote:
Originally Posted by ssǝן‾ʎ
That code makes no sense and frankly won't work! You can't escape ALL of a query else it won't work - those characters are special for a reason. Also, I don't understand why you check for "INSERT", "SELECT" or "UPDATE" and assume they've lost connection if they didn't enter one of them, what if they've lost connection but did enter one of them? Or what if they're doing a "DROP" or "DELETE" or one of the many other types of query? If it was that simple to make queries secure, don't you think it would be built into SQL?

Also, why MySQL and not SQLite, the default official database?
Well, I don't know much about SQLite and for now I'm ok with mysql .
Also, I could add more but I guess everyone has understood what the strcmp is supposed to do as it's a quick check for sql characters. You could just do ||strcmp(str,"DELETE")) or drop, w/e.
Reply


Forum Jump:


Users browsing this thread: 30 Guest(s)