Useful Snippets

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
where
pawn Код:
PlayerInfo[playerid][pAdmin] >= changeme
comes from? o.o
I used it in my GF Edit...nuff said
Reply

Quote:
Originally Posted by DonWade
Посмотреть сообщение
Don't know if anyone posted something like this but just created it for myself , could be used by someone:

Код:
stock FindFreeObjectSlot(playerid)
{
    for(new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
    {
        if(!IsPlayerAttachedObjectSlotUsed(playerid, i))
        {
            return i;
        }
    }
    return 1;
}
Return (-1) or something 10+ if there are no free slots, otherwise this function is useless.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
pawn Код:
// Weapon Knockback

public OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid)
{
    new
        Float:px,
        Float:py,
        Float:pz,
        Float:ix,
        Float:iy,
        Float:iz,
        Float:angle;

    GetPlayerPos(playerid, px, py, pz);
    GetPlayerPos(issuerid, ix, iy, iz);

    angle = absoluteangle(-(90-(atan2((fDestY - fPointY), (fDestX - fPointX)))));

    SetPlayerVelocity(playerid, 0.35 * floatsin(-angle, degrees), 0.35 * floatcos(-angle, degrees), 0.1);
    ApplyAnimation(playerid, "PED", "FALL_back", 4.0, 0, 1, 1, 1, 0, 1);
    defer GetUp(playerid);
}
timer GetUp[1000](playerid)
{
    ApplyAnimation(playerid, "PED", "getup", 4.0, 0, 1, 1, 0, 0, 1);
}

stock Float:absoluteangle(Float:angle)
{
    while(angle < 0.0)angle += 360.0;
    while(angle > 360.0)angle -= 360.0;
    return angle;
}
You never declared fDestY, fPointY, fDestX, fPointX.
Reply

Quote:
Originally Posted by JordanMaddox
Посмотреть сообщение
It's not much...made it in like 5 minutes...but I guess it can be quite useful

Код:
#define COLOR_YELLOW 0xFFFF00AA //<--Incase you don't have yellow defined, add it to the top of your script with all your other definitions
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_GRAD1 0xB4B5B7FF

CMD:arims(playerid, params[])
{
        if(IsPlayerAdmin(playerid))
	{
		if(isnull(params))
		{
			SendClientMessage(playerid, COLOR_WHITE, "USAGE: /arims [rim model]");
			SendClientMessage(playerid, COLOR_GRAD1, "Rim Models: Cutter, Switch, OffRoad, Shadow, Mega, Rimshine, Wire");
			return 1;
		}
		if(strcmp(params,"cutter",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Cutter Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1079);
		}

		else if(strcmp(params,"switch",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Switch Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1080);
		}

		else if(strcmp(params,"offroad",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed OffRoad Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1026);
		}
		
		else if(strcmp(params,"shadow",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Shadow Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1073);
		}
		
		else if(strcmp(params,"mega",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Mega Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1074);
		}
		
		else if(strcmp(params,"rimshine",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Rimshine Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1075);
		}
		
		else if(strcmp(params,"wire",true) == 0)
		{
			SendClientMessage(playerid, COLOR_YELLOW, "You have placed Wire Rims on this vehicle.");
			AddVehicleComponent(GetPlayerVehicleID(playerid), 1076);
		}
	}
	else
	{
		SendClientMessage(playerid, COLOR_GRAD1, "You're not authorized to use that command!");
	}
	return 1;
}
It's fixed now...
Reply

Stock function that will find if a string contains an IP address. Useful for anti advertising.
pawn Code:
#define MAX_NUM 10
#define MY_SERVER_IP "255.255.255.255:7777"
stock ContainsIP(const string[])
{
    new num[MAX_NUM][128];
    new numpos[MAX_NUM];
    new dotcount,i;
    if(strfind(string,MY_SERVER_IP)!=-1)return 0;
    for(new npos=0;npos<=MAX_NUM;npos++)
    {
        new c;
        for(c=0;i<strlen(string);i++)
        {

            new ch = string[i];

            if((string[i] == '.' || string[i] == ':') && ((i>0 && '0' <= string[i-1] <= '9') || (i>1 && '0' <= string[i-2] <= '9') ))
            {
                dotcount++;
                i++;
                break;
            }
            if ((npos==0 || i - numpos[npos - 1] < strlen(num[npos-1])+4) && '0' <= ch <= '9' || (c == 0 && ch == '-'))
            {
                num[npos][c] = string[i];
                numpos[npos] = i;
                c++;
            }


        }

    }
    new found=0;
    for(new npos=0;npos<MAX_NUM;npos++)
    {
        if( strlen(num[npos]) > 0 && 0 < strval(num[npos]) <= 225)
        {
            found++;
        }
    }
    if(found >= 3 && dotcount >= 3)return 1;
    return 0;
}
Example:

pawn Code:
public OnPlayerText(playerid, text[])
{
    if(ContainsIP(text))
    {
        BanEx(playerid,"Advertising");
        return 0;
    }
    return 0;
}
Reply

Quote:
Originally Posted by vvhy
View Post
Stock function that will find if a string contains an IP address. Useful for anti advertising.
pawn Code:
#define MAX_NUM 10
#define MY_SERVER_IP "255.255.255.255:7777"
stock ContainsIP(const string[])
{
    new num[MAX_NUM][128];
    new numpos[MAX_NUM];
    new dotcount,i;
    if(strfind(string,MY_SERVER_IP)!=-1)return 0;
    for(new npos=0;npos<=MAX_NUM;npos++)
    {
        new c;
        for(c=0;i<strlen(string);i++)
        {

            new ch = string[i];

            if((string[i] == '.' || string[i] == ':') && ((i>0 && '0' <= string[i-1] <= '9') || (i>1 && '0' <= string[i-2] <= '9') ))
            {
                dotcount++;
                i++;
                break;
            }
            if ((npos==0 || i - numpos[npos - 1] < strlen(num[npos-1])+4) && '0' <= ch <= '9' || (c == 0 && ch == '-'))
            {
                num[npos][c] = string[i];
                numpos[npos] = i;
                c++;
            }


        }

    }
    new found=0;
    for(new npos=0;npos<MAX_NUM;npos++)
    {
        if( strlen(num[npos]) > 0 && 0 < strval(num[npos]) <= 225)
        {
            found++;
        }
    }
    if(found >= 3 && dotcount >= 3)return 1;
    return 0;
}
Example:

pawn Code:
public OnPlayerText(playerid, text[])
{
    if(ContainsIP(text))
    {
        BanEx(playerid,"Advertising");
        return 0;
    }
    return 0;
}
A more proper name for that function is IsServerAdvertisement or something along those lines.
Reply

Quote:
Originally Posted by SuperViper
View Post
A more proper name for that function is IsServerAdvertisement or something along those lines.
But then the function name would be falsely describing what the function does.
Reply

pawn Code:
stock GetPlayerPacketloss(playerid,&Float:packetloss)
{
    /* Returns the packetloss percentage of the given playerid - Made by Fusez */

    if(!IsPlayerConnected(playerid)) return 0;

    new nstats[400+1], nstats_loss[20], start, end;
    GetPlayerNetworkStats(playerid, nstats, sizeof(nstats));

    start = strfind(nstats,"packetloss",true);
    end = strfind(nstats,"%",true,start);

    strmid(nstats_loss, nstats, start+12, end, sizeof(nstats_loss));
    packetloss = floatstr(nstats_loss);
    return 1;
}
Reply

Quote:
Originally Posted by Stylock
View Post
But then the function name would be falsely describing what the function does.
The function checks for server advertisement, not just any IP.
Reply

Quote:
Originally Posted by RedFusion
View Post
pawn Code:
stock GetPlayerPacketloss(playerid,&Float:packetloss)
{
    /* Returns the packetloss percentage of the given playerid - Made by Fusez */

    if(!IsPlayerConnected(playerid)) return 0;

    new nstats[400+1], nstats_loss[20], start, end;
    GetPlayerNetworkStats(playerid, nstats, sizeof(nstats));

    start = strfind(nstats,"packetloss",true);
    end = strfind(nstats,"%",true,start);

    strmid(nstats_loss, nstats, start+12, end, sizeof(nstats_loss));
    packetloss = floatstr(nstats_loss);
    return 1;
}
Nice function!!!

____

And guys, use the Regex plugin. No need for some complex code when you can just make an expression:

pawn Code:
stock textContainsIP(const string[])
{
    static
        RegEx:rCIP
    ;

    if ( !rCIP )
    {
        rCIP = regex_build("(.*?)([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})(.*?)");
    }

    return regex_match_exid(string, rCIP);
}
Reply

Hey I wanted to make this into a filterscript with a warning system but I didn't really feel like it, it's a sound file that plays "Hello user, you have been warned by an admin. Please stop breaking the rules" I think you guys can do something fun with this. Enjoy!
https://dl.dropboxusercontent.com/u/...hello_user.wav
Reply

Quote:
Originally Posted by NicholasA
View Post
Hey I wanted to make this into a filterscript with a warning system but I didn't really feel like it, it's a sound file that plays "Hello user, you have been warned by an admin. Please stop breaking the rules" I think you guys can do something fun with this. Enjoy!
https://dl.dropboxusercontent.com/u/...hello_user.wav
Hahaha are you supposed to use this?
Reply

A command which creates gang zones quickly on the map. The default range specified here is 100, which creates a square model gang zone. You can change it though. It creates square model zones only as it's all set to a specified value, you can change it though.

pawn Code:
CMD:gangzone(playerid, params[])
{
 new Float:xp, Float:yp, Float:zp;
 GetPlayerPos(playerid, xp, yp, zp);
 #pragma unused zp
 new range = 100;
 new Gangzone = GangZoneCreate(xp-range, yp-range, xp+range, yp+range);
 GangZoneShowForAll(Gangzone, 0xFF0000FF);
 return 1;
}
I use this when I'm lazy to get all the coords.
Reply

a better one here
http://forum.sa-mp.com/showpost.php?...postcount=3990
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
21th?
If the number ends with "1" and is not 11, it's "st"

pawn Код:
printf("%s", AddNumberSuffix(21));
// From serverlog: [15:45:15] 21st
Reply

I use something like this (RyDeR's race system):
pawn Код:
switch(Position)
{
      case 1,21,31,41,51,61,71,81,91: pos = "st";
      case 2,22,32,42,52,62,72,82,92: pos = "nd";
      case 3,23,33,43,53,63,73,83,93: pos = "rd";
      default: pos = "th";
}
Only if max players is 100.
Easy...
EDIT: I saw that, of course. But mine is simple, no need for any calculation.
I am aditing just for not posting again
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
You're right, but then there's 111, which should be "th" but is "st" (unless you say "eleventy-first").
I just looked at a table of number 1 - 100 and made a code according to it. But you're right about this.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
Even that can be improved slightly.
True that:
pawn Код:
stock getOrdinal(iNum) {
    static const
        s_aszOrdinal[4][3] = { "st", "nd", "rd", "th" }
    ;
    iNum = (iNum < 0) ? -iNum : iNum;

    return ((10 < (iNum % 100) < 14) || !(0 < (iNum = (iNum % 10)) < 4)) ? s_aszOrdinal[3] : s_aszOrdinal[iNum - 1];
}
Reply

EDIT:
pawn Код:
stock GetNumberSuffix(number)
{
    new value[20], string[3], length;
    format(value, sizeof(value), "%i", number);
    length = strlen(value);

    if(value[length-1] == '1' && (length == 1 || (length > 1 && value[length-2] != '1'))) string = "st";
    else if(value[length-1] == '2' && (length == 1 || (length > 1 && value[length-2] != '1'))) string = "nd";
    else if(value[length-1] == '3' && (length == 1 || (length > 1 && value[length-2] != '1'))) string = "rd";
    else string = "th";
    return string;
}
Reply

pawn Код:
stock GetVehicleType(model)
{
    switch(model)
    {
        case 0: return 0; // None
        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;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)