Useful Functions

Not good at all...
You didn't even post the SetPlayerBlind(playerid,toggle); function...
Reply

Mis-read.
Reply

Quote:
Originally Posted by MenaceX^
Посмотреть сообщение
Not good at all...
You didn't even post the SetPlayerBlind(playerid,toggle); function...
Ahmm... What?
Reply

Yeah, my bad, my internet kindda died and that's why I double posted, it didn't receive all web info either so I saw the command only, without the other stuff.
Reply

DoesPlayerHaveAnyWeapon(playerid)

pawn Код:
stock DoesPlayerHaveAnyWeapon(playerid)
{
    new
        iWeaponID,
        iAmmo;

    for(new i =0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, iWeaponID, iAmmo);
   
        if(iWeaponID > 0 && iAmmo > 0)
        {
            return 1;
        }
    }
    return 0;
}

DoesPlayerHaveWeapon(playerid, weaponid, slot)

I could've discluded the slot parameter to make this function much easier to use, however for it's initial use it required it. I could remove it and have it check that itself (with ease) but I'm too lazy.

pawn Код:
stock DoesPlayerHaveWeapon(playerid, weaponid, slot)
{
    if(weaponid < 0 || weaponid > 50)
        return 0;
       
    static iWeaponData[2];
    // 0 = weapon
    // 1 = ammo
       
    GetPlayerWeaponData(playerid, slot, iWeaponData[0], iWeaponData[1]);
   
    if(iWeaponData[0] == weaponid && iWeaponData[1] > 0)
        {
        return 1;
    }
    return 0;
}
RemovePlayerWeapon(playerid, weaponid)

Note that this doesn't have a return value.

pawn Код:
stock RemovePlayerWeapon(playerid, iWeaponID)
{
    if(iWeaponID < 0 || iWeaponID > 50)
        {
        return;
        }

    new
        iWeapon[13],
        iAmmo[13];
       
    for(new iSlot; iSlot < 13; iSlot++)
        {
        GetPlayerWeaponData(playerid, iSlot, iWeapon[iSlot], iAmmo[iSlot]);
        }

    ResetPlayerWeapons(playerid);
   
    for(new iSlot; iSlot < 13; iSlot++)
    {
        if(iAmmo[iSlot] == 0 || iWeapon[iSlot] == iWeaponID)
            continue;

        GivePlayerWeapon(playerid, iWeapon[iSlot], iAmmo[iSlot]);
    }
}

From one of my old projects. Will post more later on, I'm not going to use them so I might as well post some here.
Reply

Headshot detection function - for sniper rifle

it's very accurate, can't be more precise
and it only uses 1 loop (for looping trough all players)

Код:
#include <a_samp>

new bool:ShowHitCoords = false; //Set to 'true' to show where the bullet hits. (red ball is the center if the enemy's head)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

stock Float:Distance(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
	new Float:result = floatsqroot(floatpower(floatsub(x2,x1),2)+floatpower(floatsub(y2,y1),2)+floatpower(floatsub(z2,z1),2));
	return result;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
	if(newkeys == 132 && GetPlayerWeapon(playerid) == 34)
	{
		new Float:Cvx, Float:Cvy, Float:Cvz;
		new Float:Cvx2, Float:Cvy2, Float:Cvz2;
		new Float:distance;
		new Float:P2x, Float:P2y, Float: P2z;
		new Float:Cx, Float:Cy, Float:Cz;

		GetPlayerCameraPos(playerid, Cx, Cy, Cz);
		GetPlayerCameraFrontVector(playerid, Cvx, Cvy, Cvz);

		for(new i; i<MAX_PLAYERS; i++)
		{
			if((IsPlayerConnected(i) && i != playerid))
			{
				GetPlayerPos(i, P2x, P2y, P2z);

				if(GetPlayerSpecialAction(i) == SPECIAL_ACTION_DUCK)
				{
					new Float:P2a;
					GetPlayerFacingAngle(i, P2a);
					P2a += 330;
					P2x += 0.27 * floatsin(-P2a, degrees);
					P2y += 0.27 * floatcos(-P2a, degrees);
					P2z -= 0.08;
				}
				else P2z += 0.76;

				distance = Distance(Cx, Cy, Cz, P2x, P2y, P2z);

				Cvx2 = Cvx * distance + Cx;
				Cvy2 = Cvy * distance + Cy;
				Cvz2 = Cvz * distance + Cz;

				if(ShowHitCoords == true)
				{
					CreateObject(2996, Cvx2, Cvy2, Cvz2, 0.0, 0.0, 0.0);
					CreateObject(2997, P2x, P2y, P2z, 0.0, 0.0, 0.0);
				}

				if(Distance(Cvx2, Cvy2, Cvz2, P2x, P2y, P2z) < 0.18)
				{
					SetPlayerHealth(i, 0.0);
					break;
				}
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Reply

Nice, could you post script from these functions, pls

GetPlayerCameraPos(playerid, Cx, Cy, Cz);
GetPlayerCameraFrontVector(playerid, Cvx, Cvy, Cvz);

@Jay_, thx now I can make to gift flowers with RemovePlayerWeapon , I wanted to make it too, but if there is already this one, I will use this
Reply

Код:
PlayerID(pName[],bool:namemode)
{
	for(new i = 0; i < GetMaxPlayers(); i++)
	{
	    if(!IsPlayerConnected(i)) continue;
		new name[MAX_PLAYER_NAME];
		GetPlayerName(i, name, sizeof(name));
		if(namemode==true)
		{
			if(strcmp(name, pName, false) == 0)
			{
		    return i;
			}
		}
		else
		{
			if(strfind(name,pName,true,0))
			{
		    return i;
			}
		}
	}
	return -1;
}
edited PlayerID function to my needs and thought release it
namemode true means exact, false means part of it(ignorecase is on)
Reply

Make your money strings neater!
will change 1000000 to "1,000,000" etc.

Код:
NiceMoney(amount,sep[]=",")
{
	new str[16],count=-1;
	format(str,16,"%i",amount);
	if(strlen(str)<4)return str;
	for(new i=strlen(str);i>0;i--)//backwards loop ftw!
	{
	    count++;
	    if(count==3)
	    {
			strins(str,sep,i);
			count=0;
		}
	}
	return str;
}
also if you don't mind i'd like to suggest something to the previous post:

Код:
PlayerID(pName[],bool:namemode)
{
	for(new i = 0; i < GetMaxPlayers(); i++)
	{
	    if(!IsPlayerConnected(i)) continue;
		new name[MAX_PLAYER_NAME];
		GetPlayerName(i, name, sizeof(name));
		if(strcmp(name, pName, namemode) == 0)
		{
		    return i;
		}
	}
	return -1;
}
Reply

Quote:
Originally Posted by ikey07
Посмотреть сообщение
Nice, could you post script from these functions, pls

GetPlayerCameraPos(playerid, Cx, Cy, Cz);
GetPlayerCameraFrontVector(playerid, Cvx, Cvy, Cvz);

@Jay_, thx now I can make to gift flowers with RemovePlayerWeapon , I wanted to make it too, but if there is already this one, I will use this
download the newest sa-mp version, it's a default function
Reply

Quote:
Originally Posted by ivex
Посмотреть сообщение
Код:
stock NameFromFullRPname(playerid)
{
	new ImeEx[MAX_PLAYER_NAME];
	new destinacija[64];
	destinacija[0] = 0;
	GetPlayerName(playerid, ImeEx, sizeof(ImeEx));
	if(strfind(ImeEx,"_") != -1)
	for(new x =0; x < strlen(ImeEx); x++)
	if(ImeEx[x] == '_')
	 strmid(destinacija, ImeEx, 0, x,sizeof(destinacija));
	return destinacija;
}
Its function that extract just first name from RolePlay nickname .. let say from nick "Justin_Beckes" this function will extract just "Justin" ..

Use like this :
Код:
new Name[64];
Name = NameFromFullRPname(playerid);
printf("Your first name is %s", Name);
THis is just example .. Enjoy by Ivex

Here's how i'd personally do it:

pawn Код:
stock GetRolePlayName(playerid, first[24], last[24] = "N/A")
{
    new trpn[MAX_PLAYER_NAME];
   
    if(GetPlayerName(playerid,trpn,sizeof(trpn)))
    {
        new strd = strfind(trpn, "_", false);
       
        if(strd != -1)
        {
            strmid(first,trpn,0,strd,sizeof(first));
            strmid(last,trpn,strd+1,sizeof(trpn),sizeof(last));
            return 1;
        }
        return 0;
    }
    return 0;
}

It has a connected check, no loops, and an optional last name parameter.


usage:

Код:
new FirstName[MAX_PLAYER_NAME, LastName[Max_PLAYER_NAME];

GetRolePlayName(playerid,FirstName,LastName); //dont need the LastName (optional)

printf("First name = %s, and last name = %s", FirstName, LastName);
Reply

IsRolePlayName(playerid, bool:alphaonly = true)
by Kyoshiro aka Kyosaur
Since i made the GetRolePlayName, i decided to make this as well. Basically it just tells you if the person is using an rp name (example: Josh_Smith). This function has an optional parameter called alphaonly which if enable will check for invalid characters and numbers (most functions like these will say 'kyo_test_lol' is valid, and even 'ke0h_t3st'...when they aren't).

Usage:

Код:
if(!IsRolePlayName(playerid))
{
    SendClientMessage(playerid,0xFF0000FF, "You have to have an rp name to play here ('firstname_lastname')!");
    Kick(playerid);
}

Code:

pawn Код:
stock IsRolePlayName(playerid, bool:alphaonly = true)
{
    new trpn[MAX_PLAYER_NAME];
   
    if(GetPlayerName(playerid,trpn,sizeof(trpn)))
    {
        new strd = strfind(trpn, "_", false);

        if(strfind(trpn,"_",false,strd+1) == -1)
        {
            if(strd > 0)
            {
                if(trpn[strd-1] && trpn[strd+1])
                {
                    if(alphaonly)
                    {
                        for(new a = 0, l = strlen(trpn); a < l; a++)
                        {
                            switch(trpn[a])
                            {
                                case '0' .. '9': return 0;

                                case 'a' .. 'z': continue;
                                case 'A' .. 'Z': continue;
                                case '_': continue; // easier than specifying every invalid char

                                default: return 0;
                            }
                        }
                    }
                    return 1;
                }
            }
        }
    }
    return 0;
}
Reply

http://forum.sa-mp.com/showpost.php?...6&postcount=81 =)
Reply

@Blantas, http://forum.sa-mp.com/showpost.php?...6&postcount=81
Reply

Quote:
Originally Posted by [TDM]Relax
Посмотреть сообщение
Sry. My bad. Didn't saw that. =)
Reply

ConvertTime(&seconds, &minutes=-1, &hours=-1, &days=-1, &weeks=-1, &months=-1, &years=-1);

By Kyoshiro aka Kyosaur


What is this exactly?
This is a function that converts seconds into seconds,minutes,hours,days,weeks,months, and years. ConvertTime has multiple usages since the function returns a string formatted with the time and also passes all the variables by reference. This means that the function is capable of not only formatting a string with the newly created time for you, but also means that if you wanted to you could format your own (or do anything with the data you wished- ie save it, or use it for something else) you could very well do so.


How do i use it?
Well the first parameter is for the seconds you wish to convert, and all the other parameters are for holding the data and specifying how far you want the conversion to actually go. If you specify variables up to weeks, this function will convert your seconds into the number of minutes,hours,days, and weeks. If you specify up to the years, you'll get the complete time stamp.

Since this function has multiple uses and possibilities, i think showing examples is the best way to show you how to use it. First lets look at a simple example and convert seconds into days.

Example:
pawn Код:
new
        Seconds = 1234567,
        Minutes,
        Hours,
        Days;
       
    printf("[USE 1]: Return string from ConvertTime: %s",ConvertTime(Seconds,Minutes,Hours,Days));
    printf("[USE 2]: The data from ConvertTime (Vars): %d, %d, %d, %d", Days, Hours, Minutes, Seconds);
   
    //NOTE: Since ConvertTime was used in the first example, the variables already have the data inside.
    //ConvertTime has to be used first before you can use the second usage! Its not magic :P.
Results:
Quote:

[23:18:01] [USE 1]: Return string from ConvertTime: 14 days, 6 hours, 56 minutes, and 7 seconds
[23:18:01] [USE 2]: The data from ConvertTime (Vars): 14, 6, 56, 7

In the above example "USE 1" uses the functions return string, which uses correct grammar (You wont see "1 seconds") and also only uses whats actually there. So if you specify up to years, and the time only goes to weeks, the string will only go up to weeks, so you wont see anything like "0 years, 0 months, 4 weeks, 2 days, 3 hours, 0 minutes, and 34 seconds".

The second usage simply uses the variables that ConvertTime put the data into. If you want to format your own string or do something with the data from the variables, you can!


Ok, but how is this useful? Can i see a practical example?
Well not at this moment. Im going to be making a tutorial on how to use this very soon, so i will just link it in this section when it is completed. Its going to focus on what a LOT of people have problems with, time management. This includes: Saving/showing how long a user has played, count downs, anti-spams, and things of that nature.



Code:
pawn Код:
stock ConvertTime(&cts, &ctm=-1,&cth=-1,&ctd=-1,&ctw=-1,&ctmo=-1,&cty=-1)
{
    #define PLUR(%0,%1,%2) (%0),((%0) == 1)?((#%1)):((#%2))

    #define CTM_cty 31536000
    #define CTM_ctmo 2628000
    #define CTM_ctw 604800
    #define CTM_ctd 86400
    #define CTM_cth 3600
    #define CTM_ctm 60

    #define CT(%0) %0 = cts / CTM_%0; cts %= CTM_%0

    new strii[128];

    if(cty != -1 && (cts/CTM_cty))
    {
        CT(cty); CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(cty,"year","years"),PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctmo != -1 && (cts/CTM_ctmo))
    {
        cty = 0; CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctw != -1 && (cts/CTM_ctw))
    {
        cty = 0; ctmo = 0; CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctd != -1 && (cts/CTM_ctd))
    {
        cty = 0; ctmo = 0; ctw = 0; CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, and %d %s",PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(cth != -1 && (cts/CTM_cth))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, and %d %s",PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctm != -1 && (cts/CTM_ctm))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; CT(ctm);
        format(strii, sizeof(strii), "%d %s, and %d %s",PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; ctm = 0;
    format(strii, sizeof(strii), "%d %s", PLUR(cts,"second","seconds"));
    return strii;
}
Reply

pawn Код:
#define mysql_create_db(%0) mysql_query("CREATE DATABASE %0")
pawn Код:
#define mysql_drop_db(%0) mysql_query("DROP DATABASE %0")
pawn Код:
#define mysql_auto_increment(%0, %1) mysql_query("ALTER TABLE  `%0` AUTO_INCREMENT =%1")
Reply

Info
Just a simple piece of code which you can use to replace a char in a text.

Code
pawn Код:
replaceChar(string[128], findchar, replacechar)
{
    for(new i; string[i] != 0; ++i)
    {
        if(string[i] == findchar) string[i] = replacechar;
    }
    return string;
}
Post edited. Thanks to ******.

Usage
Код:
printf("%s", replaceChar("hi i'm just testing my function", ' ', '+'));
This will output:
Код:
hi+i'm+just+testing+my+function
Reply

pawn Код:
forward SendTeamMessage(color,string[],team);

public SendTeamMessage(color,string[],team)
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            if (GetPlayerTeam(i) == team)
            {
                SendClientMessage(i, color, string);
            }
        }
    }
    return 1;
}
For example:
pawn Код:
format(string, 128, "Grove team managed to own the Ballas");
SendTeamMessage(COLOR_GREEN, string, Grove);
Reply

Info
Just a simple piece of code which you can to detect IP adresses.
It returns true/1 if it detects a ip otherwise false/0.

Code
pawn Код:
stock stringContainsIP(const szStr[])
{
    new
        iDots,
        i
    ;
    while(szStr[i] != EOS)
    {
        if('0' <= szStr[i] <= '9')
        {
            do
            {
                if(szStr[i] == '.')
                    iDots++;
               
                i++;
            }
            while(('0' <= szStr[i] <= '9') || szStr[i] == '.' || szStr[i] == ':');
        }
        if(iDots > 2)
            return 1;
        else
            iDots = 0;
       
        i++;
    }
    return 0;
}
Usage
Block advertisements:
Under OnPlayerText:
pawn Код:
if(stringContainsIP(text))
{
    new
        szMsg[128]
    ;
    GetPlayerName(playerid, szMsg, MAX_PLAYER_NAME);
   
    format(szMsg, sizeof(szMsg), "%s has been banned due advertising!", szMsg);
    SendClientMessageToAll(0xFF0000FF, szMsg);
    Ban(playerid);
   
    return 0;
}
This will ban the player if he writes an IP adress like 78.22.66.22:7777 (doesn't matter which IP)
Reply


Forum Jump:


Users browsing this thread: 40 Guest(s)