Useful Functions

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Bad joke, just do 5 - 1 if you are serious
Actually it wasn't a joke. I wanted to know just a function like, that if it was possible.
Reply

This is actually quite silly if you know that you can just do the thing that Nero_3D said.
pawn Код:
#define differenceBetweenNumbers(%0,%1) \
        floatround(floatabs((%0) - (%1)))
Reply

Код:
stock GetDifferenceBetweenNumbers(Float:num1, Float:num2) return printf("%f",((num1) >= (num2)) ? ((num1) - (num2)) : ((num2) - (num1)));
@RyDeR` idk for floatabs function ty for it...
Reply

you define return tags as following:
pawn Код:
stock bool:FUnkshion()
stock Float:FUnkshion()
stock bool:FUnkshion()
The only useful macro like you posted that I've ever used was:
pawn Код:
#define int _
That allowed me to do this:
pawn Код:
stock int:FUnkshion()
When you put the underscore (_) tag, it's the same as not putting a tag at all.
Reply

pawn Код:
#define DOB:: \
    DOB_
   
#define DOB_GetCells(%0) \
    (((%0) + cellbits) / cellbits) // Round up
   
#define BitArray:%0[%1] \
    Bits:%0[DOB_GetCells(%1)]
   
stock DOB::GetBit (Bits: bitarray [], index)
    return _: ((bitarray [index / cellbits] & Bits: (1 << (index % cellbits))) != Bits: 0);

stock DOB::SetBit (Bits: bitarray [], index, bool: set)
{
    if (set)
        bitarray [index / cellbits] |= Bits: (1 << (index % cellbits));
    else
        bitarray [index / cellbits] &= Bits: ~(1 << (index % cellbits));
}

stock DOB::SetAllBits (Bits: bitarray [], bool: set, cells = sizeof (bitarray))
{
    if (set)
        for (new i = 0; i < cells; ++i)
            bitarray [i] = Bit: 0xFFFFFFFF; // bitarray [i] |= Bit: 0xFFFFFFFF;
    else
        for (new i = 0; i < cells; ++i)
            bitarray [i] = Bit: 0; // bitarray [i] &= Bit: 0;
}
Reply

Quote:
Originally Posted by Kwarde
Посмотреть сообщение
I'm not sure, but if it's an invalid skin ID, won't it just set it to CJ?
I'm thinking the same thing...
Reply

Nope. It will crash (tested a while back with skin 300 I guess).
Reply

I'm not sure is this actually useful but I was bored lol

pawn Код:
stock PrintStack()
{
    new stp, stk;
    new row[4];
    new rowidx = 0;
    new stackitem;

    // Get the registers
    #emit lctrl 3
    #emit stor.s.pri stp
    #emit lctrl 4
    #emit stor.s.pri stk

    // Walk through the stack print 4 elements in each row
    for (new i = stk; i < stp; i += 4) {
        #emit lref.s.pri i
        #emit stor.s.pri stackitem
        row[rowidx++] = stackitem;
        if ((i / 4) % 4 == 0) {
            printf("[%04x]: %04x%04x %04x%04x %04x%04x %04x%04x", i - stk,
                   (row[0] & 0xFFFF0000) >> 16, row[0] & 0x0000FFFF,
                   (row[1] & 0xFFFF0000) >> 16, row[1] & 0x0000FFFF,
                   (row[2] & 0xFFFF0000) >> 16, row[2] & 0x0000FFFF,
                   (row[3] & 0xFFFF0000) >> 16, row[3] & 0x0000FFFF
            );
            rowidx = 0;
        }
    }

    return (stp - stk) / 4; // Number of items (cells) printed
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
You could do:

pawn Код:
#define int stock _:
so you can just do:

pawn Код:
int Func()
{
}
But that does make your code very ambiguous and obfuscated because you're making it look like something else for no real gain.
This works ?

#define int(%0) stock(%0)
Reply

pawn Код:
#define SetAllBits(%1,%2,%3); \
    for(new %1aset=(%2),Bit:bbt%1=(!%1aset)?(Bit:0):(Bit:-1),%1i;%1i!=%3;%1i++){%1[%1i]=bbt%1;}
credits to Drakins he asked me to post this here...D:
Reply

Quote:
Originally Posted by SlashPT
Посмотреть сообщение
pawn Код:
#define SetAllBits(%1,%2,%3); \
    for(new %1aset=(%2),Bit:bbt%1=(!%1aset)?(Bit:0):(Bit:-1),%1i;%1i!=%3;%1i++){%1[%1i]=bbt%1;}
credits to Drakins he asked me to post this here...D:
Damn, he's here, even while banned.
Reply

Quote:
Originally Posted by MrDeath537
Посмотреть сообщение
Damn, he's here, even while banned.
problem? i think there isn't
Reply

Quote:
Originally Posted by SlashPT
Посмотреть сообщение
problem? i think there isn't
What about Double-O-Seven's post?
Reply

Both wrong. If you saw his post, it's better to set the health to -1 so the player immediatly dies.
Reply

pawn Code:
stock strreplace (newstr [], oldstr [], srcstr [], deststr [], bool: ignorecase = false, size = sizeof (deststr))
{
    new
        newlen = strlen (newstr),
        oldlen = strlen (oldstr),
        srclen = strlen (srcstr),
        idx,
        rep;

    for (new i = 0; i < srclen; ++i)
    {
        if ((i + oldlen) < srclen)
        {
            if (!strcmp (srcstr [i], oldstr, ignorecase, oldlen))
            {
                deststr [idx] = '\0';
                strcat (deststr, newstr, size);
                ++rep;
                idx += newlen;
                i += oldlen - 1;
            }
            else
            {
                if (idx < size)
                    deststr [idx++] = srcstr [i];
                else
                    return rep;
            }
        }
        else
        {
            if (idx < size)
                deststr [idx++] = srcstr [i];
            else
                return rep;
        }
    }
    return rep;
}
Reply

What about this, Double-O-Seven?

Code:
stock strreplace2(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string))
{
    if(isnull(find) || isnull(string)) return 0;
    new matches;
    for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx))
    {
        strdel(string, pos, pos + flen);
        if(rlen) strins(string, replace, pos, maxlength);
        idx = pos + rlen;
        matches++;
    }
    return matches;
}
Reply

pawn Code:
stock str_replace (newstr [], oldstr [], srcstr [], deststr [], bool: ignorecase = false, size = sizeof (deststr))
{
    new
        newlen = strlen (newstr),
        oldlen = strlen (oldstr),
        srclen = strlen (srcstr),
        idx,
        rep;

    for (new i = 0; i < srclen; ++i)
    {
        if ((i + oldlen) <= srclen)
        {
            if (!strcmp (srcstr [i], oldstr, ignorecase, oldlen))
            {
                deststr [idx] = '\0';
                strcat (deststr, newstr, size);
                ++rep;
                idx += newlen;
                i += oldlen - 1;
            }
            else
            {
                if (idx < (size - 1))
                    deststr [idx++] = srcstr [i];
                else
                    return rep;
            }
        }
        else
        {
            if (idx < (size - 1))
                deststr [idx++] = srcstr [i];
            else
                return rep;
        }
    }
    deststr [idx] = '\0';
    return rep;
}
Here's a fixed version, I've detected some bugs.
I've also compared your version (strreplace2) and mine (str_replace) and the strreplace from DUtils.

pawn Code:
for (new i = 0; i < 500; ++i)
    {
        new
            t,
            string [256] = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe",
            string2 [256];
       
        const MAX_I = 20000;
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            string2 = strreplace ("Affe", "Kuh", string);
        }
        t = GetTickCount () - t;
        printf ("strreplace: %d ms (%s)", t, string2);
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            str_replace ("Kuh", "Affe", string, string2);
        }
        t = GetTickCount () - t;
        printf ("str_replace: %d ms (%s)", t, string2);
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            strreplace2 (string, "Affe", "Kuh");
        }
        t = GetTickCount () - t;
        printf ("strreplace2: %d ms (%s)", t, string);
    }
The result is (all work correctly):
Code:
[18:09:33] strreplace: 8101 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:34] str_replace: 379 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:34] strreplace2: 547 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:42] strreplace: 7986 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:43] str_replace: 378 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:43] strreplace2: 550 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:51] strreplace: 8017 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:52] str_replace: 376 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:09:52] strreplace2: 549 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:10:00] strreplace: 8003 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:10:00] str_replace: 382 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:10:01] strreplace2: 546 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
Mine is the fastest, the one from dutils is terrible slow and yours is a little bit slower than mine.
The reason why mine is the fastest is:
My str_replace iterates only once through the whole string because it doesn't use strfind.
strfind is a bad choice here because it also iterates through the whole string in the worst case and for every match it has the iterates once more.
For many matches this is not a good choice.
Reply

What about this one?
pawn Code:
stock strReplace(strWhat[], strWith[], strSrc[], strDest[], bool: ignoreCase = false, destSize = sizeof(strDest))
{
    new
        strPos = -1,
        whatLen = strlen(strWhat)
    ;
    memcpy(strDest, strSrc, _, 1 + strlen(strSrc) << 2, destSize);
   
    while((strPos = strfind(strDest, strWhat, ignoreCase, strPos + 1)) != -1)
    {
        strdel(strDest, strPos, strPos + whatLen);
        strins(strDest, strWith, strPos, destSize);
    }
    return ;
}
Reply

pawn Code:
for (new i = 0; i < 500; ++i)
    {
        new
            t,
            string [256] = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe",
            string2 [256];
       
        const MAX_I = 20000;
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            string2 = strreplace ("Affe", "Kuh", string);
        }
        t = GetTickCount () - t;
        printf ("strreplace: %d ms (%s)", t, string2);
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            str_replace ("Kuh", "Affe", string, string2);
        }
        t = GetTickCount () - t;
        printf ("str_replace: %d ms (%s)", t, string2);
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            strreplace2 (string, "Affe", "Kuh");
        }
        t = GetTickCount () - t;
        printf ("strreplace2: %d ms (%s)", t, string);
       
        t = GetTickCount ();
        for (new k = 0; k < MAX_I; ++k)
        {
            string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
            strReplace ("Affe", "Kuh", string, string2);
        }
        t = GetTickCount () - t;
        printf ("strReplace: %d ms (%s)", t, string2);
    }
Code:
[18:27:23] strreplace: 8077 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:24] str_replace: 382 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:24] strreplace2: 543 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:25] strReplace: 484 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:33] strreplace: 8032 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:33] str_replace: 386 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:34] strreplace2: 543 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:34] strReplace: 483 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:42] strreplace: 8023 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:43] str_replace: 381 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:43] strreplace2: 544 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
[18:27:44] strReplace: 489 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
You are also using strfind, this is never a good idea when there are many matches...
Reply

Yes, indeed. I see. Nice function though - Pretty fast.
_______
Here's a VERY fast strCopy (compared to existing ones as far as I know) if someone's interested:
pawn Code:
stock strCopy(strSrc[], strDest[], maxLen = -1, destSize = sizeof(strDest))
{
    if(maxLen == -1)
        maxLen = strlen(strSrc);
    return memcpy(strDest, strSrc, _, maxLen << 2, destSize);
}
Reply


Forum Jump:


Users browsing this thread: 8 Guest(s)