Useful Functions

Tiny function, i originally couldn't think of a use for it but southclaw suggested it could be useful for errors. This function will make the machine it is run on beep x (iTimes) amount of times.


pawn Код:
stock PcBeep( iTimes )
{
    new i=0;
    while( i < iTimes )
    {
        print("\7");
        i++;
    }
}
Or

pawn Код:
#define PC_BEEP(%0); new __itr=0; while( __itr < %0 ) print("\7"), __itr++;
Reply

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
Why not use a for loop to constrain the scope of your "__iter" variable and better lay out the code:

Also, I used "__target" instead of "%0" so that the parameter is only evaluated once.
I need to start thinking like that! Not quite there yet
Reply

I think it will only beep if they have speakers connected to the root server.
Reply

Code:
/*
Float:offset_x - the distance between the Vehicle  and the attached object in the X direction.
Float:offset_y - the distance between the Vehicle  and the attached object in the Y direction.
Float:offset_z - the distance between the Vehicle  and the attached object in the Z direction.
Float:x - the variable to store the X coordinate
Float:y - the variable to store the Y coordinate
Float:z - the variable to store the Z coordinate
*/
stock GetAttachedObjectPosFromVehicle(vehicleid, Float:offset_x, Float:offset_y, Float:offset_z, &Float:x, &Float:y, &Float:z)
{
        new Float:deg;
        new Float:s;
        new Float:objectangle;
        new Float:pos[3];
        GetVehicleZAngle(vehicleid, deg);
        GetVehiclePos(vehicleid, pos[0], pos[1], pos[2]);
        s = floatsqroot(offset_x * offset_x + offset_y * offset_y);
        objectangle = atan(offset_y / offset_x);
        deg += 90.0;
        deg += objectangle;
	if(offset_x < 0 )
        {
                x = pos[0] + s  * floatsin(-deg, degrees); 
	       	y = pos[1] + s  * floatcos(-deg, degrees);
	        z = pos[2] + offset_z;
	}
	else
	{
                x = pos[0] + s  * floatsin(180.0-deg, degrees);
	       	y = pos[1] + s  * floatcos(180.0-deg, degrees);
	        z = pos[2] + offset_z;
       	}
}
Reply

Do you not need the full quaternion rotation of the vehicle to calculate that, for example of the vehicle is upside down.
Reply

Quote:
Originally Posted by Y_Less
View Post
Do you not need the full quaternion rotation of the vehicle to calculate that, for example of the vehicle is upside down.
I edit code on a pastebin of someone because it's only right to an object.
If you attach multiple objects, the positions will be wrong
Example: 3 object on the left and right vehicle, change the angle of the vehicle.

Can you give me ideas on how to optimize code ?
Reply

I think this would be a working RGBA to ARGB converter, although I didn't fully test it in-game.
pawn Code:
stock RGBAToARGB(rgba)
{
    return (rgba << 24) | ((rgba & 0xFF000000) >>> 8) | ((rgba & 0xFF0000) >>> 8) | ((rgba & 0xFF00) >>> 8);
}
Reply

Or this:
pawn Code:
stock RGBAToARGB(rgba)
    return rgba >>> 8 | rgba << 24;
Reply

Quote:
Originally Posted by Slice
View Post
Or this:
pawn Code:
stock RGBAToARGB(rgba)
    return rgba >>> 8 | rgba << 24;
At first I also made something very similiar to that function, however I noticed that with the new SA-MP 0.3 RC7-2 functions it doesn't work very well when it comes to convert a color which has alpha channels in it.
So I made this function instead and it works very well:
pawn Code:
RGBAtoARGB( rgba )
{
    return
          ((rgba & 0xFF) << 24) |   /* move A up */
          ((rgba >> 8) & 0xFFFFFF); /* move RGB channels down */
}
Reply

The reason your function didn't work well is you're doing an arithmetic shift (>>) instead of a logical shift (>>>).

An arithmetic will preserve the leftmost bit and fill ones to the right of it, but the logical shift will simply shift bits to the right and put 0.

For example, 0xFF000000 >>> 8 yields 0x00FF0000, but 0xFF000000 >> 8 yields 0xFFFF0000.
Reply

Get a digit in number, according to position

PHP код:
#define getDigit(%0,%1) (((%0 / (floatround(floatpower(10, %1)))) % 10)) 
Ex
Код:
 * f(123458459, 2) = 4
 * f(123458459, 1) = 5
 * f(123458459, 0) = 9
 * f(123458459, 7) = 2
Reply

What..? That makes no sense, looks like random numbers. Care to explain?
Reply

Quote:
Originally Posted by MP2
Посмотреть сообщение
What..? That makes no sense, looks like random numbers. Care to explain?
the function name makes sense.. but his function doesn't really

it's to get the digit of the index in a number

I think his function is actually reversed... though weird.

RyDeR` made something similiar


Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Aw, I searched too far for something so easy... Thanks for clearing that up, I knew the pack argument but never thought about using it for this.
pawn Код:
stock getDigits(const value, strDig[])
{
    valstr(strDig, value, true);
   
    for(new i; strDig{i} != EOS; ++i)
        strDig{i} -= '0';
}
Example(s):

It's the same (see on the previous page).
Reply

I chose to use mathematical calculations (without strings). The function returns the smallest to the largest equivalent digit.
Reply

I've never seen this, so pardon me if it's already been posted:

Converting a price number (e.g. "$1000") into a price string (e.g. "$1,000"):

How this works: When you format a string or use SendClientMessageEx, you normally use "$%d" for a price. With this stock, you just use "%s" and it will convert it into currency format.

Example:
pawn Код:
format(string, sizeof(string), "This property is for sale!~n~Price: %s", ConvertPrice(HouseInfo[idx][Price]))");
Stock:
pawn Код:
stock ConvertPrice(price)
{
    new pricestring[32];
    new j = valstr(pricestring,price);
    while(j >= 4) { j -= 3; strins(pricestring,",",j); }
    strins(pricestring,"$",0);
    return pricestring;
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
That looks a lot different from the stock I just provided, plus the stock I provided only converts the number into actual price format, I'd expect someone like you to realize that ******.
Reply

Well it's also been done here by me again.

Also, don't use valstr - it freezes the server on certain values (unless you're using fixes.inc).
Reply

Hello SA-MP scripters.

I'm releasing a function that has been VERY useful for me, for all kinds of scripts:
pawn Код:
GetPlayerLagPos(playerid,&Float:x,&Float:y,&Float:z)
{
    new Float:vx,Float:vy,Float:vz;
    new Float:ping=float(GetPlayerPing(playerid)),vehid=GetPlayerVehicleID(playerid);
    if (!vehid)
    {
        GetPlayerPos(playerid,x,y,z);
        GetPlayerVelocity(playerid,vx,vy,vz);
    }
    else
    {
        GetVehiclePos(vehid,x,y,z);
        GetVehicleVelocity(vehid,vx,vy,vz);
    }
    x+=vx*ping/25.0;
    y+=vy*ping/25.0;
    z+=vz*ping/25.0;
}
What does this function, exactly? Well, it calculates the position where the player WILL be when he receives instructions from the server. Basically, if that player is using a vehicle, and has a high ping, use the CreateExplosion function at the coordinates given by this function so the explosion will occur ON the player on his screen (it may not look like being on him on your screen though).

Keep in mind that you should use this function ONLY if you're having issues with high pingers (or high speed vehicles) in your server functionalities, as it is more CPU consuming than GetPlayerPos. Be VERY careful when using in loops also...
Reply

pawn Код:
/*
 * GetMaxVehicles
 * Get numbers of vehicles created
*/


new
    totalVeh  = -1;

#define CreateVehicle   totalVeh ++, InternalCVeh
#define DestroyVehicle  totalVeh  --,  InternalDVeh

#define GetMaxVehicles() totalVeh

forward InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
public InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay) {
    return CallRemoteFunction("CreateVehicle", "dffffiid", modelid, x, y, z, angle, color1, color2, respawn_delay);
}

forward InternalDVeh(id);
public  InternalDVeh(id) {
    return CallRemoteFunction("DestroyVehicle", "i", id);
}

// Now, make to CreateVehicleEx. etc
Get the numbers of vehicles created
Reply

I am a long time without programming pawn, I forgot something so basic. Totally confused with Callbacks Hook. I'm sorry. Thanks for warning.

^^ Edited to Public Var ..
Reply


Forum Jump:


Users browsing this thread: 44 Guest(s)