Useful Functions

DarkScripter, remove the space in the macro and consider adding brackets.
Reply

Quote:
Originally Posted by Kar
Посмотреть сообщение
RyDeR` or anyone thinking of improving this function?
I don't think it can be accurate without knowing a player's FOV or Resolution.. :/
Reply

Quote:
Originally Posted by CyNiC
Посмотреть сообщение
Some time ago I was searching for convert timestamp to date and the only way that I found was use a plugin.

I found this on Pawn source, I think that it can be useful to someone else(be aware of your time zone).

pawn Код:
stock timestamptodate(sec1970,&year=0,&month=0,&day=0,&hour=0,&minute=0,&second=0) //By CompuPhase
{
    #define SECONDS_PER_MINUTE  60
    #define SECONDS_PER_HOUR        3600
    #define SECONDS_PER_DAY     86400
    #define SECONDS_PER_YEAR    31556952    /* based on 365.2425 days per year */
    new monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    new days, seconds;

    /* find the year */
    for (year = 1970; ; year += 1) {
    days = 365 + _:((year & 0x03) == 0); /* clumsy "leap-year" routine, fails for 2100 */
    seconds = days * SECONDS_PER_DAY;
    if (seconds > sec1970)
      break;
    sec1970 -= seconds;
    } /* if */

    /* find the month */
    for (month = 1; ; month += 1) {
    days = monthdays[month - 1];
    seconds = days * SECONDS_PER_DAY;
    if (seconds > sec1970)
      break;
    sec1970 -= seconds;
    } /* if */

    /* find the day */
    for (day = 1; sec1970 >= SECONDS_PER_DAY; day += 1)
    sec1970 -= SECONDS_PER_DAY;

    /* find the hour */
    for (hour = 0; sec1970 >= SECONDS_PER_HOUR; hour += 1)
    sec1970 -= SECONDS_PER_HOUR;

    /* find the minute */
    for (minute = 0; sec1970 >= SECONDS_PER_MINUTE; minute += 1)
    sec1970 -= SECONDS_PER_MINUTE;

    /* remainder is the number of seconds */
    second = sec1970;
}
You are just way too stupid to use the search here.
I already found earlier many functions to get the date back. Look into my signature.
Reply

Here's my own version of SendFormatMessage:
pawn Код:
stock SendFormatMessage(const iPlayer, const iColor, const szFormat[], { Float, _ }: ...) {
    new
        iArgs = (numargs() - 3) << 2
    ;
    if(iArgs) {
        static
            s_szBuf[144],
            s_iAddr1,
            s_iAddr2
        ;
        #emit ADDR.PRI szFormat
        #emit STOR.PRI s_iAddr1
       
        for(s_iAddr2 = s_iAddr1 + iArgs, iArgs += 12; s_iAddr2 != s_iAddr1; s_iAddr2 -= 4) {
            #emit LOAD.PRI s_iAddr2
            #emit LOAD.I
            #emit PUSH.PRI
        }
        #emit CONST.PRI s_szBuf
       
        #emit PUSH.S szFormat
        #emit PUSH.C 144
        #emit PUSH.PRI
        #emit PUSH.S iArgs
        #emit SYSREQ.C format
       
        #emit LCTRL 4
        #emit LOAD.S.ALT iArgs
        #emit ADD.C 4
        #emit ADD
        #emit SCTRL 4

        return (iPlayer != -1) ? SendClientMessage(iPlayer, iColor, s_szBuf) : SendClientMessageToAll(iColor, s_szBuf);
    }
    return (iPlayer != -1) ? SendClientMessage(iPlayer, iColor, szFormat) : SendClientMessageToAll(iColor, szFormat);
}
It's slightly faster and shorter than any current versions.
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Here's my own version of SendFormatMessage:
pawn Код:
stock SendFormatMessage(const iPlayer, const iColor, const szFormat[], { Float, _ }: ...) {
    new
        iArgs = (numargs() - 3) << 2
    ;
    if(iArgs) {
        static
            s_szBuf[144],
            s_iAddr1,
            s_iAddr2
        ;
        #emit ADDR.PRI szFormat
        #emit STOR.PRI s_iAddr1
       
        for(s_iAddr2 = s_iAddr1 + iArgs, iArgs += 12; s_iAddr2 != s_iAddr1; s_iAddr2 -= 4) {
            #emit LOAD.PRI s_iAddr2
            #emit LOAD.I
            #emit PUSH.PRI
        }
        #emit CONST.PRI s_szBuf
       
        #emit PUSH.S szFormat
        #emit PUSH.C 144
        #emit PUSH.PRI
        #emit PUSH.S iArgs
        #emit SYSREQ.C format
       
        #emit LCTRL 4
        #emit LOAD.S.ALT iArgs
        #emit ADD.C 4
        #emit ADD
        #emit SCTRL 4

        return (iPlayer != -1) ? SendClientMessage(iPlayer, iColor, s_szBuf) : SendClientMessageToAll(iColor, s_szBuf);
    }
    return (iPlayer != -1) ? SendClientMessage(iPlayer, iColor, szFormat) : SendClientMessageToAll(iColor, szFormat);
}
It's slightly faster and shorter than any current versions.
Really great RyDeR`.The power of #emit
Reply

Lol u mad ryder? I mean, this is amazing I even didn't know I can use so much params in for loop, really awesome!
Reply

pawn Код:
stock DoesPlayerHaveWeapon(playerid, weaponid)
{
    new Gun[13];
    new Ammo[13];
    for(new i=0; i<13; i++)
    {
        GetPlayerWeaponData(playerid, i, Gun[i], Ammo[i]);
        if(Gun[i] == weaponid)
        {
            return true;
        }
    }
    return false;
}
Reply

Your function only accepts one param, and if you set to accept more don't will work with just one.
Reply

Quote:
Originally Posted by CyNiC
Посмотреть сообщение
Your function only accepts one param, and if you set to accept more don't will work with just one.
I don't really get what you wanted to say, but I have tested it and it works fine for multiple parameters.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Actually, that code uses a little trick in that the pre-processor doesn't limit the number of parameters:

pawn Код:
#define A(%0,%1)

// With:
A(0, 1);
// %0 = 0
// %1 = 1

// With:
A(0, 1, 2, 3)
// %0 = 0
// %1 = 1, 2, 3
The pre-processor is purely a text-based matcher - things like "parameters" and "commas" mean nothing to it.
Interesting. Will the result be accurate afterwards?
Reply

ryder already made better function for that

pawn Код:
stock RemovePlayerWeapon(playerid, ...)
{
    new iArgs = numargs();
    while(--iArgs)
    {
        SetPlayerAmmo(playerid, getarg(iArgs), 0);
    }
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
The pre-processor is purely a text-based matcher - things like "parameters" and "commas" mean nothing to it.
It does treat brackets/parenthesis specially, though!
Reply

@KoczkaHUN I remember Ryder said somewhere that that function needs to be called somewhere after OnGameModeInit or OnFilterScriptinit
Reply

Код:
stock OddOrEven( _num_ )
{
	if( ( 1 & ( _num_ ) ) == 1 ) return 1;
	else return 0;
}
Usage:

Код:
if(OddOrEven(17) == 1)
{
	printf("17 is an odd number");
}
else printf("Number 17 is even");
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
pawn Код:
#define OddOrEven(%0) (1&(%0))
Yes, i tested it, it works but I dont like code with warnings/errors.

Код:
redundant test constant expression is non-zero
Reply

Quote:
Originally Posted by aNdReSk
Посмотреть сообщение
can this be used on chat messages kaczmi?
Yes, easy use:

pawn Код:
public OnPlayerText(playerid,text[])
{
DisableBadWord(text,"_SOME WORD_");
DisableBadWord(.....);
SendPlayerMessageToAll(playerid,text);
return 0;
}
Reply

Updating TextDraw Function

pawn Код:
stock UpdateTextDraw(Text: textid, str[], player = -1)
{
    TextDrawSetString(textid, str);
    return player != -1 ? TextDrawShowForPlayer(player, textid) : TextDrawShowForAll(textid);
}
made me.
Reply

pawn Код:
stock ShiftRGBAToABGR(&color)
{
    new r, g, b, a;
    r = (color >>> 24);
    g = (color >>> 16 & 0xFF);
    b = (color >>> 8 & 0xFF);
    a = (color  & 0xFF);
    color = (a & 0xFF) | ((b & 0xFF) << 8) | ((g & 0xFF) << 16) | (r << 24);
    return color;
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
From YSI y_cell:

pawn Код:
stock Cell_ReverseBytes(data)
{
    // Swap all bytes.
    return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
}
it shows you the bytes from a cell?
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
From YSI y_cell:

pawn Код:
stock Cell_ReverseBytes(data)
{
    // Swap all bytes.
    return (data >>> 24) | ((data & 0x00FF0000) >> 8) | ((data & 0x0000FF00) << 8) | (data << 24);
}
Nice Job.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)