Useful Functions

@Ryder
Try:
pawn Код:
stock
    filterColorTags_teszt(string[])
{
    new
        start,
        end;
    while((start = strfind(string, "{", false, start)) != -1)
    {
        if((end = strfind(string, "}", false, start + 1)) != -1)
        {
            strdel(string, start, end + 1);
            start -= end - start;
        } else start++;
    }
}
Reply

Why would I if my code works already? Nice anyway.
Reply

pawn Код:
#if !defined isnull
    #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
//Credits for the isnull macro goes to ******, I just use it in the function below.

stock strcmpex(const string1[],const string2[],bool:ignorecase = false,length = cellmax)
{
    if(!isnull(string1) && !isnull(string2))
    {
        new len1 = strlen(string1),len2 = strlen(string2);
        if(len1 == len2 || length != cellmax)
        {
            if(length > len1) length = len1;
            if(!ignorecase)
            {
                for(new i = 0;i < length;i++)
                    if(string1[i] != string2[i]) return 1;
            }
            else
            {
                for(new i = 0;i < length;i++)
                    if(string1[i] != string2[i])
                    {
                        switch(string1[i])
                        {
                            case 'A'..'Z','a'..'z':
                            {
                                switch(string2[i])
                                {
                                    case 'A'..'Z','a'..'z': if((string1[i] | 0x20) != (string2[i] | 0x20)) return 1;
                                    default: return 1;
                                }
                            }
                            default: return 1;
                        }
                    }
            }
            return 0;
        }
    }
    else if(isnull(string1) && isnull(string2)) return 0;
    return 1;
}

#if defined strcmp
    #undef strcmp
#endif
#define strcmp strcmpex
Features:
  1. Faster than the original strcmp when ignoring case in a longer string length.
  2. Fixed a bug a strcmp that null string will be equal to anything.
  3. Fixed a crashing bug in strcmp but I forgot the code(May be fixed already)
It may be buggy(but stable for me) and slow

EDIT:
Now fixed a bug on ignoring case and the fix improved the speed a bit(by around 5%).
Reply

Quote:
Originally Posted by pmkrz
Посмотреть сообщение
system:trCheck (stupid name) fixed ! (I do think so xd)

pawn Код:
//CODE
@leong124: Could you make those your tests again, please ? ( :
Why do you always use binar code ? I only looks more complicated as it rly is
At the end its only harder to read and not understandable if you have no clue about the ascii code for the letters
And is not any faster than a "normal" version

Than the switch(l) is just a waste, use if(l ==
After the ic you should use an else and return the result instead of GOTO
Remove the b and return there the result, too
And remove one of the c's because the other is (c + 1) or (c - 1)

@******
I was only wondering
Because there werent any documentation
Nor any definitions in the includes
But it should be clear now, thx
Reply

Yeah, indeed. Also, why do you have such an overdone indentitation (at some parts)? That looks actually ugly (not that it matters)

EDIT: Which one's faster now?
Reply

@Nero_3D: I use binary code because it is just the way I have to practice that type of coding. Nothing more.

@Nero_3D, ******, Ryder: About the indentation, I think that everyone have yours so..

Thanks anyway. ( :
Reply

Quote:
Originally Posted by pmkrz
I use binary code because it is just the way I have to practice that type of coding. Nothing more.
What?
Reply

Didn't you understand ? : o

I mean, there is a simple reason for my use of binary:
- It's a way, for me, to practice those binary techniques. Understood ? ( :
Reply

Copy that. ( :
Reply

RemovePlayerWeapon(playerid, {_}...)
USAGE: RemovePlayerWeapon(playerid, weaponid);
You can use multilple weapons, eg.
RemovePlayerWeapon(playerid, 38, 29, 4, 36, 35);
The code:
pawn Код:
stock RemovePlayerWeapon(playerid, {_}:...)
{
    new pWepInfo[13][2], pCurWep, WepID,
        ammount = numargs()
    ;
    pCurWep = GetPlayerWeapon(playerid);
    for(new i = 0; i < 13; i++)
        GetPlayerWeaponData(playerid, i, pWepInfo[i][0], pWepInfo[i][1]);
    ResetPlayerWeapons(playerid);
    for(new i = 1; i < ammount; i++){
        WepID = getarg(i);
        for(new w = 0; w < 13; w++)
            if(pWepInfo[w][0] == WepID)
                pWepInfo[w][0] = 0;
    }
    for(new i = 0; i < 13; i++)
        GivePlayerWeapon(playerid, pWepInfo[i][0], pWepInfo[i][1]);
    SetPlayerArmedWeapon(playerid, pCurWep);
}
Tests with ZCMD:
pawn Код:
CMD:wep(playerid, params[])
{
    ResetPlayerWeapons(playerid);
    GivePlayerWeapon(playerid, 4, 1);
    GivePlayerWeapon(playerid, 29, 500);
    GivePlayerWeapon(playerid, 31, 500);
    GivePlayerWeapon(playerid, 38, 500);
    return 1;
}

CMD:testrpw(playerid, params[])
{
    RemovePlayerWeapon(playerid, 29, 38);
    return 1;
}
Reply

That would cause weapons with ammo above 30,000 to get negative values mostly. You could just use SetPlayerAmmo!
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
You could just use SetPlayerAmmo!
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
I'm not quite sure why you said that tbh, unless GetPlayerWeaponData ammo is only 16 bit, which I'm not sure about now.
The ammo values are 15 bits.
Reply

Quote:
Originally Posted by LarzI
View Post
We had soo many useful functions, why did it have to fuck up??

Thanks for the link WeeDarr
new g_szString[ MAX_WHORES ]; - i loled soooooo hard!!!!!
Reply

Mod 2 and Mod 4 ? Yeah sure
pawn Код:
if( !(iValor % 2) || !(iValor % 3) || !(iValor % 4) || (iValor == 1)) return false;
Can iValor ever be iCount if iCount needs to be smaller than the square root of iValor ? Of course
pawn Код:
while(iCount++ < floatsqroot(iValor))  
    if( !(iValor % iCount) && (iValor != iCount) ) return false;
So lets present my version
pawn Код:
stock IsPrime(value)
{
    if((value < 4) || (value & 0b1) == 0) {
        if((value == 2) || (value == 3)) {
            return true;
        }
        return false;
    }
    new
        i = floatround(floatpower(value, 0.5), floatround_floor);
    if((i & 0b1) == 0) {
        i--;
    }
    while(i > 2) {
        if((value % i) == 0) {
            return false;
        }
        i -= 2;
    }
    return true;
}
Criticism is allowed


I dont know if thats faster but
pawn Код:
stock DecimalToBinary(const iNum)
{
    new string[cellbits + 1];
    format(string, sizeof string, "%b", iNum);
    return strval(iNum);
}
Reply

@Nero_3D:
Yes, both of your functions are a lot faster.

@[FeK]DraKiNs:
Here's my version of GetWeekOfDate:
pawn Код:
stock GetWeekOfDate(day, month, year)
{
    if((0 < day <= 31) && (0 < month <= 12))
    {
        new
            days_In_Month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
            total_Days
        ;
        for(new i; i < month - 1; ++i)
        {
            total_Days += days_In_Month[i];
        }
        return (total_Days + ((((!(year % 4) && (year % 100) != 0)) || !(year % 400)) ? (1) : (0)) + day) / 7;
    }
    return -1;
}
EDIT: Seems like I misunderstood your function. My functions gets the week index from a year (1 to 52) using a date and yours gives the index of the day. Please name your functions better...
Reply

Why using "static" in this case? And why using hexadecimal forms?
Reply

@[FeK]DraKiNs

Just a question, why do you use hex codes all the time? They don't make your code faster either better looking. The only thing you do using them is losing time and make your code hard-readable for people that wants to know the values.
Reply

Quote:
Originally Posted by Toribio
Посмотреть сообщение
Why using "static" in this case? And why using hexadecimal forms?
Perhaps hex numbers shall only be useful if used with Enums or something like this.
Reply

pawn Код:
stock strtok_two(szParser[], &iPos) {

    new
        szResult[20];
   
    while(szParser[iPos] == ' ') ++iPos;
    strmid(szResult, szParser, iPos, (strfind(szParser, " ", false, iPos + 1) != -1) ? (strfind(szParser, " ", false, iPos + 1)) : (strlen(szParser)), sizeof(szResult));
    iPos = (strfind(szParser, " ", false, iPos + 1) != -1) ? (strfind(szParser, " ", false, iPos + 1)) : (strlen(szParser));
    return szResult;
}
For the lazy people who don't want to replace their extensive strtok-based code with sscanf (lol GF), the benchmarks don't lie. I chose 20 as a size for all functions involved as rarely anything longer is used as a parameter (unless at the end of an input).

Код:
[20:25:08] GetParam - 24194ms
[20:25:34] strtok v1 - 26096ms
[20:25:44] strtok v2 - 9715ms
Reply


Forum Jump:


Users browsing this thread: 30 Guest(s)