Useful Functions

To create a line with GangZones from A to B (ZoneSize is the size of every little zone AND the distance between each zone):

Код:
CreateGangZoneLine(Float:ZoneSize,Float:X1,Float:Y1,Float:X2,Float:Y2)
{
	// ZoneSize is the size of each "dot" AND the distance between each dot!
	new Float:TotalDistance = GetDistanceBetweenPoints(X1,Y1,0.0,X2,Y2,0.0);
	
	new Dots = floatround(TotalDistance / ZoneSize); // Amount of "dots" to create
	
	for(new i=1; i <= Dots; i++)
	{
	    new Float:X,Float:Y;
	    
	    X = X1 + (((X2 - X1) / Dots) * i ); // Dots is like percentage. If you have 10 dots, the first one is at 1/10, second at 2/10 etc!
	    Y = Y1 + (((Y2 - Y1) / Dots) * i );
	    
	    new Float:Offset = ZoneSize / 2.0;
	    
	    new zone = GangZoneCreate(X-Offset,Y-Offset,X+Offset,Y+Offset);
	    
		// Example showing the zone for all:
		GangZoneShowForAll(zone,0xFF000055);
		// I suggest to save em to an array, to destroy it later :P
	}
}
And I think the easiest way to crash a player is:

Код:
CrashPlayer(playerid)
{
	new Float:X,Float:Y,Float:Z;
	GetPlayerPos(playerid,X,Y,Z);
	CreatePlayerObject(playerid,522,X,Y,Z,0.0,0.0,0.0,1000);
}
522 is the object of a vehicle (NRG-500), this will lead to a crash.
Reply

Quote:
Originally Posted by robintjeh
Посмотреть сообщение
Hmm. That is the code to fix it on vehicle spawn, or? What's the fix?
When you set a vehicle's numberplate, that change won't be seen until the vehicle gets restreamed:
Quote:
Note: The vehicle must be respawned or restreamed for the changes to take effect.
On many servers, this is solved by respawning the car when numplate is set, and they don't think about vehicles' velocity, the players in it, vehicle's health, etc.
When using this function, you don't have to worry about these, because it just sets the vehicle's virtualworld to an other, then changes back, this way the vehicle gets restreamed for the players.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
http://y-less.pastebin.ca/2092126

Fast prime number check. This implementation is specifically tuned to 32bit numbers (i.e. what SA:MP PAWN uses). The function contains a list of all the prime numbers below 65536. Although that seems like a lot, remember that this is less than 0.002% of all the 32bit prime numbers! However, the number 65536 is important here as 65536*65536=4294967296, which is exactly the number of states in 32bits. Why is this significant? Because when checking for prime numbers, you only need to see if it is a product of any number UP TO ITS SQUARE ROOT. What's more, if a number is not a product of 3, it will not be a product of 6 or 9 so we only need to check if prime numbers are factors. If we have a list of all the prime numbers up to the square root of any number we could possibly check, this will all go much faster!

Note that if the number you are checking is less than 65536 the code will simply run a binary search (with an approximated initial upper bound for speed) to see if it is in the array.
Nice piece of code there you created ******, will for sure be useful to me.
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Nice piece of code there you created ******, will for sure be useful to me.
How is this function useful in sa-mp? Just can't think of a situation where you would need to check if the number is a prime.
Reply

Quote:
Originally Posted by wups
Посмотреть сообщение
How is this function useful in sa-mp? Just can't think of a situation where you would need to check if the number is a prime.
Lol, perhaps some ordinary casino games you're about to create?

Objectives like:
Get 3 prime numbers in a row

Damn, I cannot explain this though I'll plan to have something shown when I create a deathmatching tournament server.
Reply

Quote:
Originally Posted by NaS
Посмотреть сообщение
Код:
	    X = X1 + (((X2 - X1) / Dots) * i ) // Dots is like percentage. If you have 10 dots, the first one is at 1/10, second at 2/10 etc!
	    Y = Y1 + (((Y2 - Y1) / Dots) * i )
where are the semicolons?
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Using strfind instead of looping would significantly increase performance!
Depends ─ if you have a string full of the same characters to get replaced, then the loop will be faster I guess.
Reply

OK, it took me a while to calculate all this, but some people might find this function insanely useful.
pawn Код:
/*
GetAttachedObjectPos

objectid - the objectid to which the attached object is attached to. (not the attached objectid)
Float:offset_x - the distance between the main object and the attached object in the X direction.
Float:offset_y - the distance between the main object and the attached object in the Y direction.
Float:offset_z - the distance between the main object and the attached object in the Z direction.
Float:x - the variable to store the X coordinate, passed by reference.
Float:y - the variable to store the Y coordinate, passed by reference.
Float:z - the variable to store the Z coordinate, passed by reference.
*/


stock GetAttachedObjectPos(objectid, Float:offset_x, Float:offset_y, Float:offset_z, &Float:x, &Float:y, &Float:z)
{
    new Float:object_px,
        Float:object_py,
        Float:object_pz,
        Float:object_rx,
        Float:object_ry,
        Float:object_rz;
    GetObjectPos(objectid, object_px, object_py, object_pz);
    GetObjectRot(objectid, object_rx, object_ry, object_rz);
    new Float:cos_x = floatcos(object_rx, degrees),
        Float:cos_y = floatcos(object_ry, degrees),
        Float:cos_z = floatcos(object_rz, degrees),
        Float:sin_x = floatsin(object_rx, degrees),
        Float:sin_y = floatsin(object_ry, degrees),
        Float:sin_z = floatsin(object_rz, degrees);
    x = object_px + offset_x * cos_y * cos_z - offset_x * sin_x * sin_y * sin_z - offset_y * cos_x * sin_z + offset_z * sin_y * cos_z + offset_z * sin_x * cos_y * sin_z;
    y = object_py + offset_x * cos_y * sin_z + offset_x * sin_x * sin_y * cos_z + offset_y * cos_x * cos_z + offset_z * sin_y * sin_z - offset_z * sin_x * cos_y * cos_z;
    z = object_pz - offset_x * cos_x * sin_y + offset_y * sin_x + offset_z * cos_x * cos_y;
}
Reply

Quote:
Originally Posted by YJIET
Посмотреть сообщение
OK, it took me a while to calculate all this, but some people might find this function insanely useful.
pawn Код:
/*
GetAttachedObjectPos

objectid - the objectid to which the attached object is attached to (not the attached object)
Float:offset_x - the distance between the main object and the attached object in the X direction.
Float:offset_y - the distance between the main object and the attached object in the Y direction.
Float:offset_z - the distance between the main object and the attached object in the Z direction.
Float:x - the variable to store the X coordinate, passed by reference.
Float:y - the variable to store the Y coordinate, passed by reference.
Float:z - the variable to store the Z coordinate, passed by reference.
*/


stock GetAttachedObjectPos(objectid, Float:offset_x, Float:offset_y, Float:offset_z, &Float:x, &Float:y, &Float:z)
{
    new Float:object_px,
        Float:object_py,
        Float:object_pz,
        Float:object_rx,
        Float:object_ry,
        Float:object_rz;
    GetObjectPos(objectid, object_px, object_py, object_pz);
    GetObjectRot(objectid, object_rx, object_ry, object_rz);
    new Float:cos_x = floatcos(object_rx, degrees),
        Float:cos_y = floatcos(object_ry, degrees),
        Float:cos_z = floatcos(object_rz, degrees),
        Float:sin_x = floatsin(object_rx, degrees),
        Float:sin_y = floatsin(object_ry, degrees),
        Float:sin_z = floatsin(object_rz, degrees);
    x = object_px + (offset_x * cos_z * cos_y) - (offset_x * sin_x * sin_y * sin_z) - (offset_y * sin_z * cos_x) + (offset_z * cos_z * sin_y) + (offset_z * sin_x * cos_y * sin_z);
    y = object_py + (offset_x * sin_z * cos_y) + (offset_x * sin_x * sin_y * cos_z) + (offset_y * cos_z * cos_x) + (offset_z * sin_z * sin_y) - (offset_z * sin_x * cos_y * cos_z);
    z = object_pz - (offset_x * sin_y * cos_x) + (offset_y * sin_x) + (offset_z * cos_y * cos_x);
}
Is it accurate? Nice job, rep++
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Is it accurate? Nice job, rep++
I'd say it's 99.999% accurate.
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Depends ─ if you have a string full of the same characters to get replaced, then the loop will be faster I guess.
I don't like assumptions! Here are the benchmarks:

I used Lorenc's function (strreplacechar_1), and compared it to my own one (strreplacechar_2).

Код:
[21:22:18]  Bench for strreplacechar_1 - Long string.: executes, by average, 65.62 times/ms.
[21:22:22]  Bench for strreplacechar_2 - Long string.: executes, by average, 125.78 times/ms.
[21:22:26]  Bench for strreplacechar_1 - Medium-long string.: executes, by average, 170.01 times/ms.
[21:22:30]  Bench for strreplacechar_2 - Medium-long string.: executes, by average, 352.09 times/ms.
[21:22:34]  Bench for strreplacechar_1 - Short string.: executes, by average, 616.62 times/ms.
[21:22:38]  Bench for strreplacechar_2 - Short string.: executes, by average, 833.00 times/ms.
Of course these results are different for different strings, but I can't be bothered testing a bunch of strings.

Code used: http://pastebay.com/142996
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Did you test with anything but z rotation?
It works with all rotations.
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
I don't like assumptions! Here are the benchmarks:

I used Lorenc's function (strreplacechar_1), and compared it to my own one (strreplacechar_2).

<results>

Of course these results are different for different strings, but I can't be bothered testing a bunch of strings.

Code used: http://pastebay.com/142996
That was actually not my point. This was:

http://pastebin.com/j5XfZ0E0
Reply

Oh, I didn't catch that for some reason. Either way, I'd say that's pretty much a worst-case scenario.
Reply

Is there not a way of making a 3D version of SetObjectToFacePoint? I don't know trigonometry very well at all so I wouldn't know where to begin. It just seems like someone would have something like that by now. I have a function which does this but it's not simply an equation. It's also very old. Take a look.

pawn Код:
SetObjectToFaceCords(objectid, Float:x1,Float:y1,Float:z1)
{
    //   SetObjectToFaceCords() By LucifeR   //
    //                LucifeR@vgames.co.il   //

    // setting the objects cords
    new Float:x2,Float:y2,Float:z2;
    GetObjectPos(objectid, x2,y2,z2);

    // setting the distance values
    new Float:DX = floatabs(x2-x1);
    new Float:DY = floatabs(y2-y1);
    new Float:DZ = floatabs(z2-z1);

    // defining the angles and setting them to 0
    new Float:yaw = 0;
    new Float:pitch = 0;

        // check that there isnt any 0 in one of the distances,
    // if there is any  use the given parameters:
    if(DY == 0 || DX == 0)
    {
        if(DY == 0 && DX > 0)
        {
            yaw = 0;
            pitch = 0;
        }
        else if(DY == 0 && DX < 0)
        {
            yaw = 180;
            pitch = 180;
        }
        else if(DY > 0 && DX == 0)
        {
            yaw = 90;
            pitch = 90;
        }
        else if(DY < 0 && DX == 0)
        {
            yaw = 270;
            pitch = 270;
        }
        else if(DY == 0 && DX == 0)
        {
            yaw = 0;
            pitch = 0;
        }
    }
    // calculating the angale using atan
    else // non of the distances is 0.
    {
            // calculatin the angles
        yaw = atan(DX/DY);
        pitch = atan(floatsqroot(DX*DX + DZ*DZ) / DY);

        // there are three quarters in a circle, now i will
        // check wich circle this is and change the angles
        // according to it.
        if(x1 > x2 && y1 <= y2)
        {
            yaw = yaw + 90;
            pitch = pitch - 45;
        }
        else if(x1 <= x2 && y1 < y2)
        {
            yaw = 90 - yaw;
            pitch = pitch - 45;
        }
        else if(x1 < x2 && y1 >= y2)
        {
            yaw = yaw - 90;
            pitch = pitch - 45;
        }
        else if(x1 >= x2 && y1 > y2)
        {
            yaw = 270 - yaw;
            pitch = pitch + 315;
        }

        if(z1 < z2)
            pitch = 360-pitch;
    }

    // setting the object rotation (should be twice cuz of lame GTA rotation system)
    SetObjectRot(objectid, 0, 0, yaw);
    SetObjectRot(objectid, 0, pitch, yaw);
    return 1;
}
Reply

Quote:
Originally Posted by Backwardsman97
Посмотреть сообщение
Is there not a way of making a 3D version of SetObjectToFacePoint? I don't know trigonometry very well at all so I wouldn't know where to begin. It just seems like someone would have something like that by now. I have a function which does this but it's not simply an equation. It's also very old. Take a look.

<code>
Sure, here is a snippet I wrote a while ago:
pawn Код:
stock SetObjectFaceCoords3D(iObject, Float: fX, Float: fY, Float: fZ, Float: fRollOffset = 0.0, Float: fPitchOffset = 0.0, Float: fYawOffset = 0.0) {
    new
        Float: fOX,
        Float: fOY,
        Float: fOZ,
        Float: fPitch
    ;
    GetObjectPos(iObject, fOX, fOY, fOZ);
   
    fPitch = floatsqroot(floatpower(fX - fOX, 2.0) + floatpower(fY - fOY, 2.0));
    fPitch = floatabs(atan2(fPitch, fZ - fOZ));
   
    fZ = atan2(fY - fOY, fX - fOX) - 90.0; // Yaw
   
    SetObjectRot(iObject, fRollOffset, fPitch + fPitchOffset, fZ + fYawOffset);
}
Example:
pawn Код:
iArrowObject = CreateObject(1318, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0);

public OnPlayerUpdate(playerid) {
    new
        Float: fX,
        Float: fY,
        Float: fZ
    ;
    GetPlayerPos(playerid, fX, fY, fZ);
    SetObjectFaceCoords3D(iArrowObject, fX, fY, fZ, 0.0, 180.0, 90.0);
    return 1;
}
This will make the arrow object have a face to the player in 3D.
Reply

Quote:
Originally Posted by wups
Посмотреть сообщение
I did some tests earlier and it works for me without encoding.
Still safer to urlencode.
Reply

Quote:
Originally Posted by KoczkaHUN
Посмотреть сообщение
Why wouldn't it work?
pawn Код:
enum E_MY_ENUM
{
    Float:E_SOMETHING,
    bool:E_BOOL_SOMEHING,
    E_E_STR[64]
}
new aStrings[14][E_MY_ENUM];
// ...
strnull(aStrings[13][E_E_STR]); // becames aStrings[13][E_E_STR][0] = 0;
Sorry, I got that all wrong. I remembered having issues with a macro like that one, and making it into a function solved it because I wanted to modify a fragment of a string.

i.e.

pawn Код:
new szTest[100], iPos = strfind(szTest, ...); nullstr(szTest[iPos]);
Reply

I was searching for PlayAudioStreamForAll and StopAudioStreamForAll functions and I didn't found them , so I made them by myself.

pawn Код:
stock PlayAudioStreamForAll(link[])
{
  foreach(Player,i)
  {
    PlayAudioStreamForPlayer(i,link);
    for(new c; c < 20; c++) SendClientMessage(i,-1,"  ");
  }
  return true;
}
Ex. of using it :

pawn Код:
CMD:play(playerid,params[])
{
  PlayAudioStreamForAll("http://users2.ml.mindenkilapja.hu/users/fuchida/uploads/Green_Day_-_Holiday.mp3");
  return true;
}
And StopAudioStreamForAll function :

pawn Код:
stock StopAudioStreamForAll()
{
  foreach(Player,i)
  {
    StopAudioStreamForPlayer(i);
  }
  return true;
}
Ex. of using it :

pawn Код:
CMD:stop(playerid,params[])
{
  StopAudioStreamForAll();
  return true;
}
Reply

@STMatt it's pretty good. But tif someone writes something and uses that command/function the abover text will hide too.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)