Useful Snippets

That can be improved to:
pawn Код:
stock GetVehicleType(model)
{
    if(model < 400 || model > 611) return 0;
    switch(model)
    {
        case 472, 473, 493, 595, 484, 430, 453, 452, 446: return 1; // Water
        case 417, 425, 447, 460, 469, 476, 487, 488, 497, 511, 512, 513, 519, 520,
        548, 553, 563, 577, 592, 593, 464, 465, 501: return 2; // Air
        default: return 3; // Land
    }
    return 0;
}
So invalid models won't return 3.
Reply

Crash a player.
It's usefull.
The command on hisself, isnt usefull.
But the crash is.
(To Ban/kick + Crash hackers etc.) (Hackers can avoid kicks).
Also this snippet can be avoided, but not that easy as Kick.


pawn Код:
CMD:crash(playerid, params[])
{
    new string[128], playerb;
    if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
    if(PlayerInfo[playerid][pAdmin] < 4) return SendClientMessage(playerid, COLOR_GREY, "You are not authorized to use this command.");
    if(!aDuty[playerid]) return SendClientMessage(playerid, COLOR_GREY, "You are not on Admin Duty.");
    if(sscanf(params, "u", playerb)) return SendClientMessage(playerid, COLOR_WHITE, "[Usage]: /crash [playerid]");
    if(PlayerInfo[playerid][pAdmin] < PlayerInfo[playerb][pAdmin]) return SendClientMessage(playerid, COLOR_GREY, "Player has a higher admin level than you.");
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 1000, 0);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 2000, 1);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 3000, 2);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 4000, 3);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 5000, 4);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 6000, 5);
    GameTextForPlayer(playerb, "•¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/", 7000, 6);
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
        SendClientMessage(playerid, COLOR_WHITE, ""&#8226;¤¶§!$$%&'()*+,-./01~!@#$^&*()_-+={[}]:;'<,>.?/");
    format(string, sizeof(string), "You have crashed %s 's game", RPN(playerb));
    SendClientMessage(playerid,COLOR_RED, string);
    format(string, sizeof(string), "AdmWarn: %s has crashed %s his game.", RPN(playerid), RPN(playerb));
    SendAdminMessage(COLOR_DARKRED, 1, string);
    return 1;
}
Reply

Hackers can't avoid kicks.
Reply

Very simple but useful:
PHP код:
stock GetMinute()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Minute;
}
stock GetDay()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Day;
}
stock GetMonth()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Month;
}
stock GetYear()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Year;
}
stock GetSecond()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Second;
}
stock GetHour()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Hour;
}
stock IsLeapYear(year)
{
    switch(
year)
    {
        case 
2012: return 1;
        case 
2016: return 1;
        case 
2020: return 1;
        case 
2024: return 1;
        case 
2028: return 1;
        case 
2032: return 1;
        case 
2036: return 1;
    }
    return 
0;
}
stock GetDaysInMonth(month)
{
    new 
Year,Month,Day,days;
    
getdate(YearMonthDay);
    switch(
month)
    {
        case 
1days 31;
        case 
2:
        {
            if(
IsLeapYear(Year)) days 29;
            else 
days 28;
        }
        case 
3days 31;
        case 
4days 30;
        case 
5days 31;
        case 
6days 30;
        case 
7days 31;
        case 
8days 31;
        case 
9days 30;
        case 
10days 31;
        case 
11days 30;
        case 
12days 31;
    }
    return 
days;

Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Hackers can't avoid kicks.
They can.
NOP KickPlayer;
Reply

The server still closes the connection, so I don't see how it would change anything.
Reply

Plus the crashing code wouldn't work on most players codes, you gotta include the stock codes too.
Reply

Make a color darker\lighter

pawn Код:
stock ColorLighten(colour,Float:Amount)
{
    new a = colour & 0xFF;
    new r=min(255, ((colour >> 24) & 0xFF)+ floatround(255 * Amount));
    new g=min(255, ((colour >> 16) & 0xFF)+ floatround(255 * Amount));
    new b=min(255, ((colour >> 8) & 0xFF)+ floatround(255 * Amount));
    return (r<<24 | g<<16 | b<<8 | a);
}

stock ColorDarken(colour,Float:Amount)
{
    new a = colour & 0xFF;
    new r=max(0, ((colour >> 24) & 0xFF)- floatround(255 * Amount));
    new g=max(0, ((colour >> 16) & 0xFF)- floatround(255 * Amount));
    new b=max(0, ((colour >> 8) & 0xFF)- floatround(255 * Amount));
    return (r<<24 | g<<16 | b<<8 | a);
}
(Amount used is 0.3)

input Hello this is red
LighterHello this is red
Darker Hello this is red


input Hello this is blue
LighterHello this is blue
Darker Hello this is blue

input Hello this is green
LighterHello this is green
Darker Hello this is green

input Hello this is yellow
LighterHello this is yellow
Darker Hello this is yellow
Reply

Not trying to be a dick here, but you guys are posting functions. This thread is for snippets.

https://sampforum.blast.hk/showthread.php?tid=38965
Reply

Quote:
Originally Posted by bensmart469
Посмотреть сообщение
Very simple but useful:
PHP код:
stock GetMinute()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Minute;
}
stock GetDay()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Day;
}
stock GetMonth()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Month;
}
stock GetYear()
{
    new 
Year,Month,Day;
    
getdate(YearMonthDay);
    return 
Year;
}
stock GetSecond()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Second;
}
stock GetHour()
{
    new 
Hour,Minute,Second;
    
gettime(Hour,Minute,Second);
    return 
Hour;
}
stock IsLeapYear(year)
{
    switch(
year)
    {
        case 
2012: return 1;
        case 
2016: return 1;
        case 
2020: return 1;
        case 
2024: return 1;
        case 
2028: return 1;
        case 
2032: return 1;
        case 
2036: return 1;
    }
    return 
0;
}
stock GetDaysInMonth(month)
{
    new 
Year,Month,Day,days;
    
getdate(YearMonthDay);
    switch(
month)
    {
        case 
1days 31;
        case 
2:
        {
            if(
IsLeapYear(Year)) days 29;
            else 
days 28;
        }
        case 
3days 31;
        case 
4days 30;
        case 
5days 31;
        case 
6days 30;
        case 
7days 31;
        case 
8days 31;
        case 
9days 30;
        case 
10days 31;
        case 
11days 30;
        case 
12days 31;
    }
    return 
days;

thanks, using this.
Reply

Haven't posted in this thread in a while..

The script below was just something simple I made. It's a 100% accurate bunnyhop detection script. Most scripts just detect KEY_JUMP being pressed, but my script actually checks if the player is indeed jumping.

The more you know: bunnyhopping is spamming the jump key to get somewhere faster.

pawn Код:
// Bunnyhop Detection
#include <a_samp>

new g_JumpCount[MAX_PLAYERS char];
new g_JumpTime[MAX_PLAYERS];

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if (GetPlayerState(playerid) == PLAYER_STATE_ONFOOT && GetPlayerAnimationIndex(playerid) == 1198)
    {
        if (GetTickCount() - g_JumpTime[playerid] >= 500)
        {
            if (++g_JumpCount{playerid} > 4)
            {
                // Your code here!
                g_JumpCount{playerid} = 0;
            }
            g_JumpTime[playerid] = GetTickCount() + 500;
        }
    }
    return 1;
}
Reply

What's the difference between

pawn Код:
g_JumpCount[playerid] = 0; or g_JumpCount{playerid} = 0;
and

pawn Код:
new g_JumpCount[MAX_PLAYERS]; or new g_JumpCount[MAX_PLAYERS char];
Reply

Quote:
Originally Posted by ThomasTailor93
Посмотреть сообщение
What's the difference between

pawn Код:
g_JumpCount[playerid] = 0; or g_JumpCount{playerid} = 0;
and

pawn Код:
new g_JumpCount[MAX_PLAYERS]; or new g_JumpCount[MAX_PLAYERS char];
"Char-arrays"
https://sampforum.blast.hk/showthread.php?tid=216730
Reply

pawn Код:
math(x, y, op) {

    if(!(0 < op < 5))
        return 0;
       
    switch(op) {
        case 1: {
            // ADD
            #emit LOAD.S.PRI x
            #emit LOAD.S.ALT y
           
            #emit ADD
        }
        case 2: {
            // SUB
            #emit LOAD.S.PRI x
            #emit LOAD.S.ALT y
           
            #emit SUB
        }
        case 3: {
            // MUL
            #emit LOAD.S.PRI x
            #emit LOAD.S.ALT y
           
            #emit SMUL
        }
        case 4: {
            // DIV
            #emit LOAD.S.PRI x
            #emit LOAD.S.ALT y
           
            #emit SDIV
        }
    }
   
    #emit RETN
    return 0;
}

Small code developed while studying #emit.
Reply

Quote:
Originally Posted by zPain
Посмотреть сообщение
Small code developed while studying #emit.
But not quite as useful ^^"
Reply

My own way of inline formatting:

pawn Код:
stock gFormatString[2048 char];

#define _format(%0,%1) \
    (format(!gFormatString, sizeof(gFormatString), %0, %1), !gFormatString)
Got the idea one day when RealCop228 replied to the Useful Functions thread about my long SendClientMessageEx function, and I also got the prefix idea from Slice's translate.inc include (thanks to them).

So basically, with a little tweaking, you can now format strings very easily:

pawn Код:
SendClientMessage(playerid, COLOR_WHITE, _format("You have $%d. You are very rich!", GetPlayerMoney(playerid));
Last year, I made a non-macro version of the same function, but it's very inefficient since I wasn't actually very good at efficiency-wise scripting last year.

EDIT: I should probably clarify. The reason why it's 512 cells (no, not 2048, it's a packed array) is because you might want to use the function for long inputs, such as MySQL queries and such.
Reply

SendClientMessage can use packed strings? TIL, thanks

#e: Ah, mea culpa, ! is negation and only indicates packing before string constant
Reply

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
My own way of inline formatting:

pawn Код:
stock gFormatString[2048 char];

#define _format(%0,%1) \
    (format(!gFormatString, sizeof(gFormatString), %0, %1), !gFormatString)
Got the idea one day when RealCop228 replied to the Useful Functions thread about my long SendClientMessageEx function, and I also got the prefix idea from Slice's translate.inc include (thanks to them).

So basically, with a little tweaking, you can now format strings very easily:

pawn Код:
SendClientMessage(playerid, COLOR_WHITE, _format("You have $%d. You are very rich!", GetPlayerMoney(playerid));
Last year, I made a non-macro version of the same function, but it's very inefficient since I wasn't actually very good at efficiency-wise scripting last year.

EDIT: I should probably clarify. The reason why it's 512 cells (no, not 2048, it's a packed array) is because you might want to use the function for long inputs, such as MySQL queries and such.
"format" doesn't support packed strings, you're corrupting data at address 0 (exclamation mark in front of the array) and it can't be split into multiple lines!
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
"format" doesn't support packed strings, you're corrupting data at address 0 (exclamation mark in front of the array) and it can't be split into multiple lines!
Well, I've only tested it with client messages with success, but I didn't manage to test it thoroughly with things like larger outputs. Stupid me, thanks for noticing that ;d

EDIT: Maybe I should stop trying to make low-leveled shit. I fail miserably at it.
Reply

You didn't fail miserably, you just mixed a few things up! It happens.

You also helped finding a new way of making pointers, which is much faster compared to the previous version!

http://forum.sa-mp.com/showthread.ph...49#post2738949
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)