Useful Functions

Thuper-duper-fast memset!

It modifies the FILL instruction's parameter on-the-fly. Linux compatible.

Demo:
pawn Код:
new test[20] = {4, ...};

memset(test, sizeof(test), 1234);

for (new i = 0; i < sizeof(test); i++) {
    printf("test[%d] = %d", i, test[i]);
}
pawn Код:
stock memset(variable[], cells, value) {
    new address;
   
    #emit LOAD.S.pri  variable
    #emit STOR.S.pri  address
   
    raw_memset(address, cells, value);
}

stock raw_memset(address, cells, value) {
    new param_adr;
   
    #emit LCTRL       6
    #emit MOVE.alt
    #emit LCTRL       0
    #emit ADD
    #emit MOVE.alt
    #emit LCTRL       1
    #emit SUB.alt
    #emit ADD.C       92
    #emit STOR.S.pri  param_adr
    #emit LOAD.S.pri  cells
    #emit SHL.C.pri   2
    #emit SREF.S.pri  param_adr
   
    #emit LOAD.S.alt  address
    #emit LOAD.S.pri  value
    #emit FILL        0
}
Reply

That's amazing! Also worked on this a while ago to do it without loops, but I failed. Good job.
Reply

@Ryder or ****** or anyone who knows >.<

Is there anyway to "select" all the string, till a certain character?
For example:
"Hello, my name is firecat."
Is want to copy all the string from "Hello" till "firecat" without the .
Is there any function for that?
Reply

What about negative numbers, that would mess it completely up. Also why converting first and then check if the money is less then thousand?

http://forum.sa-mp.com/showthread.ph...95#post1938895
Reply

pawn Код:
islower(character)
{
    if(character >= 'A' && character <= 'Z')
        return 0;

    return 1;
}

// returns 0 if character is in uppercase, else returns 1
// Example:
new c[4] = "Abc";
printf("%d", islower(c[0]));
// Output: 0
pawn Код:
isupper(character)
{
    if(character >= 'a' && character <= 'z')
        return 0;

    return 1;
}
// returns 0 if character is in lowercase, else returns 1
// Example:
new c[4] = "Abc";
printf("%d", islupper(c[0]));
// Output: 1
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
That's a handy function, however the problem is it would only put them in a line on the X axis, that is East to West. Sure if you wanted North to South you could change it to the Y axis but what if you wanted it at a 47.6 degree angle?

To do that, you use the trigonometric functions Sine and Cosine (Or in Pawn: floatsin and floatcos)

You simply add the sine of the angle to the X, and the cosine of the angle to the Y, like this:
(Don't forget to use 'degrees' in the functions!)

pawn Код:
SetPlayerPos(playerid, x + floatsin(angle, degrees), y + floatcos(angle, degrees), z);
We're not done yet though, you can probably work out that this will just put all the players in one spot as we haven't used 'count' or 'distance'.
Well, fortunately, if you multiply the Sine and Cosine functions by a distance, the "projected" position (Projected from the origin point) will be that distance from the origin. You can simply use your "count * distance" equasion:

pawn Код:
SetPlayerPos(playerid, x + ((count * p2pdistance) * floatsin(angle, degrees)), y + ((count * p2pdistance) * floatcos(angle, degrees)), z);
And you will need to add the 'angle' parameter to the function header, the final result is:

pawn Код:
TeleportAllToALine(Float: x, Float: y, Float: z, Float: p2pdistance, Float:angle, interior = 0, virtualworld = 0)
{
    new
        count = 0;

    for(new playerid = 0; playerid < MAX_PLAYERS; playerid ++)
    {
        if(IsPlayerConnected(playerid))
        {
            SetPlayerPos(playerid, x + ((count * p2pdistance) * floatsin(angle, degrees)), y + ((count * p2pdistance) * floatcos(angle, degrees)), z);
            SetPlayerFacingAngle(playerid, angle+90.0); // Set the players to face forwards, can't remember if it's + or - ...
            SetPlayerInterior(playerid, interior);
            SetPlayerVirtualWorld(playerid, virtualworld);
            count ++;
        }
    }
}
I haven't tested this, but I hope you get the idea!
You could also add a player angle parameter that changes the direction of the players, in case you want them to all face back or forward or at each other's backs!
Great addition, Southclaw!
Reply

Quote:
Originally Posted by FireCat
Посмотреть сообщение
If you're storing the value in "output" don't you mean
No, I don't.
Код:
Error 067: variable cannot be both a reference and an array (variable name)
A function argument may be denoted as a “reference” or as an array, but not as both.
Reply

Arrays are always passed by reference.
Reply

Yeah, it will.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Well I never knew that...

pawn Код:
main()
{
    new str[32] = {"hello"};
    func(str);
    print(str);
}

func(arr[])
{
    arr[0] = 'j';
}
Will that code print "jello" then?
How those two are even connected ?
Reply

Quote:
Originally Posted by The__
Посмотреть сообщение
How those two are even connected ?
He sends the string to func as a parameter.
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Arrays are always passed by reference.
yes, unless the array is constant:
pawn Код:
main()
{
    new str[32] = {"hello"};
    func(str);
    print(str);
}

func(const arr[])//<--here
{
    arr[0] = 'j';
}
that would still print "hello".
Reply

Quote:
Originally Posted by MeDaKewlDude
Посмотреть сообщение
yes, unless the array is constant:
pawn Код:
main()
{
    new str[32] = {"hello"};
    func(str);
    print(str);
}

func(const arr[])//<--here
{
    arr[0] = 'j';
}
that would still print "hello".
No, that wouldn't compile.
Reply

Quote:
Originally Posted by System64
Посмотреть сообщение
ryder already made better function for that

pawn Код:
stock RemovePlayerWeapon(playerid, ...)
{
    new iArgs = numargs();
    while(--iArgs)
    {
        SetPlayerAmmo(playerid, getarg(iArgs), 0);
    }
}
Is this working just like that?
Reply

Quote:
Originally Posted by Emmet_
View Post
I don't know lol, I just started getting addicted to putting an i in front of my variables, followed by a capital letter after the i.
For those who style variables like that, the lowercase first letter generally refers to the type of variable - 'i' for integer in this case. If that's what you were trying to get at, your variable is oddly named "Ter". Otherwise I see you were trying to use a shortened version of 'iterator' and just had a seizure with the capitalisation.
Either way, the simple name i is usually best for loops.
Reply

ev0lution$YOLO$

Your name and your signature... oh god why.
Reply

Two functions I've made some time ago, they work perfectly:

pawn Code:
native IsValidVehicle(vehicleid); // YOU NEED TO PUT THIS IN YOUR SCRIPT!

stock RandCar()
{
    new rand = random(MAX_VEHICLES);
    while (!IsValidVehicle(rand)) rand = random(MAX_VEHICLES);
    return rand;
}

stock RandPlayer()
{
    new rand = random(MAX_PLAYERS);
    while (!IsPlayerConnected(rand)) rand = random(MAX_PLAYERS);
    return rand;
}
Reply

Emmet for this function
pawn Code:
stock Float:getdistance(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
    new Float:distance[3];
    distance[0] = (x1 < x2) ? x1 - x2 : x2 - x1;
    distance[1] = (y1 < y2) ? y1 - y2 : y2 - y1;
    distance[2] = (z1 < z2) ? z1 - z2 : z2 - z1;
    return floatsqroot(floatpower(distance[0], 2.0) + floatpower(distance[1], 2.0) + floatpower(distance[2], 2.0));
}
You could use the absolute power function...
Like this:
pawn Code:
Distance = floatsqroot(floatpower(floatabs(floatsub(X1, X2)), 2.0) + floatpower(floatabs(floatsub(Y1, Y2)), 2.0) + floatpower(floatabs(floatsub(Z1, Z2)), 2.0));
Reply

Hey, dudes, multiplying two negatives results in a positive value. The simplest way would be:
Code:
floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
Reply

Quote:
Originally Posted by YJIET
View Post
Hey, dudes, multiplying two negatives results in a positive value. The simplest way would be:
Code:
floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
What if one turns out to be a positive value and the other a negative?

The absolute value is fool-proof.
Reply


Forum Jump:


Users browsing this thread: 27 Guest(s)