SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 28/04/2015, 07:42 AM   #3811
[KHK]Khalid
Gangsta
 
[KHK]Khalid's Avatar
 
Join Date: Jan 2012
Location: Alexandria, Egypt
Posts: 854
Reputation: 136
Default Re: Useful Functions

IsSafeGameText: checks if a game-text could crash a player due to bad use of '~' character.

pawn Code:
stock IsEven(integer)
{
    if(integer % 2 == 0)
         return 1; // even
    return 0; // odd
}

stock IsSafeGametext(text[])
{
    new cnt = 0;
    for(new i = 0, j = strlen(text); i < j; i ++)
    {
        if(text[i] == '~')
            cnt ++;
    }
    if(IsEven(cnt) == 0)
        return 0; // unsafe
    return 1; // safe
}

Here's some bent hooking using this function:
pawn Code:
stock _HOOKED_GameTextForPlayer(playerid, string[], time, style)
{
    if(!IsSafeGametext(string))
        return -1;

    return GameTextForPlayer(playerid, string, time, style);
}

#if defined _ALS_GameTextForPlayer
    #undef GameTextForPlayer
#else
    #define _ALS_GameTextForPlayer
#endif

#define GameTextForPlayer _HOOKED_GameTextForPlayer

stock _HOOKED_GameTextForAll(string[], time, style)
{
    if(!IsSafeGametext(string))
        return -1;

    return GameTextForAll(string, time, style);
}

#if defined _ALS_GameTextForAll
    #undef GameTextForAll
#else
    #define _ALS_GameTextForAll
#endif

#define GameTextForAll _HOOKED_GameTextForAll

stock Text:_HOOKED_TextDrawCreate(Float:x, Float:y, text[])
{
    if(!IsSafeGametext(text))
        return Text:INVALID_TEXT_DRAW;

    return TextDrawCreate(x, y, text);
}

#if defined _ALS_TextDrawCreate
    #undef TextDrawCreate
#else
    #define _ALS_TextDrawCreate
#endif

#define TextDrawCreate _HOOKED_TextDrawCreate

stock PlayerText:_HOOKED_CreatePlayerTextDraw(playerid, Float:x, Float:y, text[])
{
    if(!IsSafeGametext(text))
        return PlayerText:INVALID_TEXT_DRAW;

    return CreatePlayerTextDraw(playerid, x, y, text);
}

#if defined _ALS_CreatePlayerTextDraw
    #undef CreatePlayerTextDraw
#else
    #define _ALS_CreatePlayerTextDraw
#endif

#define CreatePlayerTextDraw _HOOKED_CreatePlayerTextDraw

stock _HOOKED_TextDrawSetString(Text:text, string[])
{
    if(!IsSafeGametext(string))
        return -1;

    return TextDrawSetString(text, string);
}

#if defined _ALS_TextDrawSetString
    #undef TextDrawSetString
#else
    #define _ALS_TextDrawSetString
#endif

#define TextDrawSetString _HOOKED_TextDrawSetString

stock _HOOKED_PlayerTextDrawSetString(playerid, PlayerText:text, string[])
{
    if(!IsSafeGametext(string))
        return -1;

    return PlayerTextDrawSetString(playerid, text, string);
}

#if defined _ALS_PlayerTextDrawSetString
    #undef PlayerTextDrawSetString
#else
    #define _ALS_PlayerTextDrawSetString
#endif

#define PlayerTextDrawSetString _HOOKED_PlayerTextDrawSetString

Edit by Yashas.

Last edited by [KHK]Khalid; 02/05/2015 at 07:20 AM.
[KHK]Khalid is offline   Reply With Quote
Old 30/04/2015, 08:03 AM   #3812
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,773
Reputation: 858
Default Re: Useful Functions

Easy functions

Instead of making long functions just for adding arguments into the syntax! Here is a simple way of your_functionf.
  • SendClientMessagef(playerid, color, messagef[], ...);
  • SendClientMessageToAllf(color, messagef[], ...);
  • GameTextForPlayerf(playerid, messagef[], expiretime, style, ...);
  • GameTextForAllf(messagef[], expiretime, style, ...);
  • ShowPlayerDialog(playerid, dialogid, dialogstyle, title[], infof[], button1[], button2[], ...);

"..." means the arguments to be handled.

pawn Code:
//some easy functions using macros
new easy_FALSE = false;

#define SendClientMessagef(%0,%1,%2,%3) \
            do \
            {   new STRING[145]; \
                format(STRING, sizeof(STRING), %2, %3); \
                SendClientMessage(%0, %1, STRING); \
            } \
            while(easy_FALSE)

#define SendClientMessageToAllf(%0,%1,%2) \
            do \
            {   new STRING[145]; \
                format(STRING, sizeof(STRING), %1, %2); \
                SendClientMessageToAll(%0, STRING); \
            } \
            while(easy_FALSE)

#define GameTextForPlayerf(%0,%1,%2,%3,%4) \
            do \
            {   new STRING[145]; \
                format(STRING, sizeof(STRING), %1, %4); \
                GameTextForPlayer(%0, STRING, %2, %3); \
            } \
            while(easy_FALSE)

#define GameTextForAllf(%0,%1,%2,%3) \
            do \
            {   new STRING[145]; \
                format(STRING, sizeof(STRING), %0, %3); \
                GameTextForAll(STRING, %1, %2); \
            } \
            while(easy_FALSE)
           
#define ShowPlayerDialogf(%0,%1,%2,%3,%4,%5,%6,%7) \
            do \
            {   new STRING[145]; \
                format(STRING, sizeof(STRING), %4, %7); \
                ShowPlayerDialog(%0, %1, %2, %3, STRING, %5, %6); \
            } \
            while(easy_FALSE)

Macros does the magic in few lines of code.

Example:
pawn Code:
public OnPlayerConnect(playerid)
{
    new player_name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, player_name, MAX_PLAYER_NAME);
    SendClientMessageToAllf(-1, "** %s(%i) have joined the server", player_name, playerid);
}
__________________

Follow me on Github: Donate on Paypal:


Last edited by Gammix; 30/04/2015 at 09:54 AM.
Gammix is offline   Reply With Quote
Old 30/04/2015, 10:22 AM   #3813
Niko_boy
High-roller
 
Niko_boy's Avatar
 
Join Date: Aug 2010
Location: Somewhere i belong
Posts: 1,336
Reputation: 138
Default Re: Useful Functions

Hmm 128 is client message character limit so you dont need 145 ish
or is there something hidden behind this (145 size)?
__________________
nope[IMG]http://*******/1r0SOkH_[/IMG]
•••[CLOSED]LCS•Freeroam•DM•Stunts•••AutoArena [0.3z][No SkinShot][sixtytiger.com]Want a decent Attack Defend Gamemode?
N/A176.31.229.148:7830Get This! Attack-Defend(v2.3.1)
Niko_boy is offline   Reply With Quote
Old 30/04/2015, 10:29 AM   #3814
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,773
Reputation: 858
Default Re: Useful Functions

Quote:
Originally Posted by Niko_boy View Post
Hmm 128 is client message character limit so you dont need 145 ish
or is there something hidden behind this (145 size)?
No, its 145 (******)!
__________________

Follow me on Github: Donate on Paypal:

Gammix is offline   Reply With Quote
Old 30/04/2015, 10:30 AM   #3815
Emmet_
Guest
 
Posts: n/a
Default Re: Useful Functions

What about:

pawn Code:
return SendClientMessagef(playerid, COLOR_WHITE, "You have $%d in your bank account.", g_BankAccount[playerid]);

pawn Code:
if (IsPlayerAdmin(playerid))
    SendClientMessageToAllf(COLOR_WHITE, "%s has logged in as an admin.", GetName(playerid));
else
    SendClientMessageToAllf(COLOR_WHITE, "%s has logged in as a regular player.", GetName(playerid));

pawn Code:
SendClientMessagef(playerid, COLOR_WHITE, "Name: %s - Money: $%d - Skin: %d - Score: %d",
    GetName(playerid),
    GetPlayerMoney(playerid),
    GetPlayerSkin(playerid),
    GetPlayerScore(playerid)
);

The trick is to use format and SendClientMessage in the same line, e.g:

pawn Code:
static
    s_FormatString[128];

#define SendFormatMessage(%0,%1,%2) \
    (format(s_FormatString, 128, %2), SendClientMessage((%0), %1, s_FormatString))

Only problem is that you can't split it into multiple lines.

Another trick is to use #emit but that's too complex.
  Reply With Quote
Old 30/04/2015, 12:17 PM   #3816
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,938
Reputation: 538
Default Re: Useful Functions

Quote:
Originally Posted by Niko_boy View Post
Hmm 128 is client message character limit so you dont need 145 ish
or is there something hidden behind this (145 size)?
Quote:
Originally Posted by Gammix View Post
No, its 145 (******)!
It's 144.

You can also use extra characters for colours.
Kar is offline   Reply With Quote
Old 30/04/2015, 03:14 PM   #3817
trablon
Big Clucker
 
trablon's Avatar
 
Join Date: Jan 2010
Location: Turkish Republic
Posts: 120
Reputation: 23
Default Re: Useful Functions

pawn Code:
stock GetPlayerID(const name[])
{
    new pName[MAX_PLAYER_NAME];
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(!IsPlayerConnected(i)) continue;
        GetPlayerName(i, pName, MAX_PLAYER_NAME);
        if(!strcmp(pName, name))
            return i;
    }
    return INVALID_PLAYER_ID;
}
trablon is offline   Reply With Quote
Old 30/04/2015, 04:03 PM   #3818
Niko_boy
High-roller
 
Niko_boy's Avatar
 
Join Date: Aug 2010
Location: Somewhere i belong
Posts: 1,336
Reputation: 138
Default Re: Useful Functions

oh shit 144.. i never knew
__________________
nope[IMG]http://*******/1r0SOkH_[/IMG]
•••[CLOSED]LCS•Freeroam•DM•Stunts•••AutoArena [0.3z][No SkinShot][sixtytiger.com]Want a decent Attack Defend Gamemode?
N/A176.31.229.148:7830Get This! Attack-Defend(v2.3.1)
Niko_boy is offline   Reply With Quote
Old 30/04/2015, 04:30 PM   #3819
Yashas
Gangsta
 
Join Date: Jun 2012
Location: India
Posts: 882
Reputation: 317
Default Re: Useful Functions

Quote:
Originally Posted by [KHK]Khalid View Post
IsSafeGameText: checks if a game-text could crash a player due to bad use of '~' character.

pawn Code:
stock IsEven(integer)
{
    if(integer % 2 == 0)
         return 1; // even
    return 0; // odd
}

stock IsSafeGametext(text[])
{
    new cnt = 0;
    for(new i = 0; i < strlen(text); i ++)
    {
        if(text[i] == '~')
            cnt ++;
    }
    if(IsEven(cnt) == 0)
        return 0; // unsafe
    return 1; // safe
}
You want the server to execute strlen strlen(text) times??

This is better
Code:
for(new i = 0,j = strlen(text);i < j;i++)

Last edited by Yashas; 03/05/2015 at 02:58 AM.
Yashas is offline   Reply With Quote
Old 01/05/2015, 03:38 PM   #3820
Emmet_
Guest
 
Posts: n/a
Default Re: Useful Functions

Function to check if a skin is valid. Updated for 0.3.7, of course:

pawn Code:
IsValidSkin(skinid)
{
    return (0 <= skinid <= 311) && skinid != 74;
}

IsPlayerAiming, much more accurate and detects all weapons and skills:

pawn Code:
IsPlayerAiming(playerid)
{
    new keys, ud, lr;

    GetPlayerKeys(playerid, keys, ud, lr);

    if ((keys & KEY_HANDBRAKE) & KEY_HANDBRAKE && (22 <= GetPlayerWeapon(playerid) <= 38))
    {
        new
            index = GetPlayerAnimationIndex(playerid);

        switch (index)
        {
            case 1160..1167, 360..363, 220, 640, 1189, 1331, 1365, 1453, 1449, 1643:
            {
                return 1;
            }
        }
    }
    return 0;
}
  Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 14 21/04/2018 04:06 AM
Functions? sciman001 Help Archive 7 19/03/2011 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 07:38 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.