Useful Functions
#61

ANTI - DRIVE - BY

Код:
public OnPlayerDeath(playerid, killerid, reason)
{
  if(killerid != INVALID_PLAYER_ID && IsPlayerConnected(killerid)) {
  if((IsPlayerInAnyVehicle(killerid)) && (reason==WEAPON_MP5 || reason==WEAPON_UZI || reason==WEAPON_TEC9) &&( !(IsPlayerInAnyVehicle(playerid)))) {
  Kick(killerid);
  }
  }
  return 1;
}
Код:
A Simple code for solving a server owners nightmare of Drive - By's

 This Auto kicks the player if he / she commits a Drive - By.
Reply
#62

Quote:
Originally Posted by Westie
pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
  if(killerid != INVALID_PLAYER_ID && IsPlayerConnected(killerid))
  {
    if(!IsPlayerInAnyVehicle(killerid)) return true;
   
    switch(reason)
    {
      case WEAPON_MP5, WEAPON_UZI, WEAPON_TEC9: Kick(killerid);
    }
  }
  return true;
}
Basically I would do something like that, not sure if it's *more* efficient.
That would work, And Andre, Please by all means post how u would do it.
Reply
#63

So mine is more efficient?
Reply
#64

Quote:
Originally Posted by -Seif-
This is very useful for role playing servers that requires _ in their name. Basically this removes the _ in their chat by using this function:
pawn Код:
stock GetUserName(playerid)
{
  new string[24];
  GetPlayerName(playerid,string,24);
  new str[24];
  strmid(str,string,0,strlen(string),24);
  for(new i = 0; i < MAX_PLAYER_NAME; i++)
  {
    if (str[i] == '_')
    {
      strdel(str,i,i+1);
      strins(str," ",i,24);
    }
  }
  return str;
}
pawn Код:
stock GetUserName(playerid)
{
  new string[24];
  GetPlayerName(playerid,string,24);
  for(new i = 0; i < 24; i++)
  {
    if (string[i] == '_')
    {
      string[i] = ' ';
    }
  }
  return string;
}
Reply
#65

Код:
forward IsInModVehicle(playerid);
public IsInModVehicle(playerid) {
	if(IsPlayerInAnyVehicle(playerid)) {
		new modmodel = GetVehicleModel(GetPlayerVehicleID(playerid));
		switch(modmodel) {
			case 445,602,429,496,422,401,518,402,541,438,527,415,542,589,480,507,585,419,587,533,526,466,492,474,579,545,411,546,400,517,410,551,500,418,516,467,404,603,600,426,436,547,489,479,442,475,405,458,580,439,409,550,506,566,549,420,451,540,491,412,478,421,529,555,477,562,565,559,561,560,558,536,575,534,567,535,576: {
			 	return true; // these are moddable vehicles
			}
		}
	}
	return false;
}
Reply
#66

Nice One Kaiser
Reply
#67

i dont know i make all my functions public too just a habit
Reply
#68

Quote:
Originally Posted by Westie
Question kaisersouse: Why public?
Probably for CallRemoteFunction in another script.
Reply
#69

TeleportPlayer();

it also teleportates the vehicle with the player (if they are inside one)
you can set different positions for when the player is using a vehicle, or not.

Код:
TeleportPlayer(playerid, telename[], Float:Vx, Float:Vy, Float:Vz, Float:Va, Float:Px, Float:Py, Float:Pz, Float:Pa, interior);
- telename = the name of the teleport in: "You have been teleported to <name>"
- Vx = Vehicle x coord
- Vy = Vehicle z ...
- Vz
- Va = Vehicle angle
- Px = player x coord
- ...
- interior = the interior where the player teleports to.

Код:
stock TeleportPlayer(playerid, telename[], Float:Vx, Float:Vy, Float:Vz, Float:Va, Float:Px, Float:Py, Float:Pz, Float:Pa, interior)
{
	new Message[128];
	format(Message, sizeof(Message), "You have been teleported to /%s", telename);
	if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
	{
		SetVehiclePos(GetPlayerVehicleID(playerid),Vx,Vy,Vz);
		SetVehicleZAngle(GetPlayerVehicleID(playerid),Va);
		SetCameraBehindPlayer(playerid);
		LinkVehicleToInterior(GetPlayerVehicleID(playerid),interior);
	}
	else
	{
		SetPlayerPos(playerid,Px,Py,Pz);
		SetPlayerFacingAngle(playerid,Pa);
		SetCameraBehindPlayer(playerid);
	}
	SetPlayerInterior(playerid, interior);
	SendClientMessage(playerid, 0x0066FFAA, Message);
	return 1;
}
usage:
Код:
	if(strcmp(cmdtext, "/Somewhere", true) == 0)
	{
	  TeleportPlayer(playerid, "Somewhere", -2847.1392, 2185.9849, 799.5908, 180.0000, -2838.7273, 2199.9253, 801.7857, 145.0000, 0, 568);
	  return 1;
	}
Reply
#70

ip_strtok

What it does: strtok's IPs (e.g. "193.11.52.214" => "193" / "11" / "52" / "214")

Note: Minimum result string length must be 4.

Example:
pawn Код:
public OnPlayerConnect(playerid)
{
    new ip[16];
    GetPlayerIp(playerid, ip, sizeof(ip));
    new idx, ip1[4], ip2[4];
    ip1 = ip_strtok(ip, idx);
    ip2 = ip_strtok(ip, idx);
    new str[128];
    format(str, sizeof(str), "*** %s's IP Range: %s.%s.*.*", ip1, ip2);
    SendClientMessageToAll(0xFFFFFFFF, str);
}
Code:
pawn Код:
stock ip_strtok(const string[], &index)
{
    new index2, result[4];
    index2 = strfind(string, ".", false, index);
    if (index2 == -1)
    {
        if (strlen(string) > index)
        {
            strmid(result, string, index, strlen(string), 30);
            index = strlen(string);
        }
        return result;
    }
    if (index2 > (index + 29))
    {
        index2 = index + 29;
        strmid(result, string, index, index2, 30);
        index = index2;
        return result;
    }
    strmid(result, string, index, index2, 30);
    index = index2 + 1;
    return result;
}
Reply
#71

Quote:
Originally Posted by Kapil
pawn Код:
stock GetUserName(playerid)
{
  new string[24];
  GetPlayerName(playerid,string,24);
  for(new i = 0; i < 24; i++)
  {
    if (string[i] == '_')
    {
      string[i] = ' ';
    }
  }
  return string;
}
Even more simpler.
pawn Код:
UnderscoreToSpace(name[])
{
    new pos = strfind(name,"_", true);
    if( pos != -1 )
        name[pos] = ' ';
}
Reply
#72

Код:
stock SetPlayerPosEx(playerid,Float:x,Float:y,Float:z,Float:angle,interior)
{
	SetPlayerPos(playerid,x,y,z);
	SetPlayerFacingAngle(playerid,angle);
	SetPlayerInterior(playerid,interior);
	return 1;
}
Код:
stock SetVehiclePosEx(vehicleid,Float:x,Float:y,Float:z,Float:angle,interior)
{
	SetVehiclePos(vehicleid,x,y,z);
	SetVehicleZAngle(vehicleid,angle);
	LinkVehicleToInterior(vehicleid,interior);
	return 1;
}
Код:
SetPlayerVehiclePos(playerid,Float:x,Float:y,Float:z,Float:angle,interior)
{
	if(IsPlayerInAnyVehicle(playerid))
	{
		SetVehiclePosEx(GetPlayerVehicleID(playerid),x,y,z,angle,interior);
		SetPlayerInterior(playerid,interior);
	}
	else SetPlayerPosEx(playerid,x,y,z,angle,interior);
	return 1;
}
Код:
stock TeleportPlayerToPlayer(playerid1,playerid2)
{
	new Float:x,Float:y,Float:z,interior;
	GetPlayerPos(playerid2,x,y,z);
	interior=GetPlayerInterior(playerid2);
	return SetPlayerVehiclePos(playerid1,x,y,z+3,GetPlayerAngle(playerid2),interior);
}
Код:
stock SlapPlayer(playerid,height)
{
	new Float:x,Float:y,Float:z;
	GetPlayerPos(playerid,x,y,z);
	SetPlayerPos(playerid,x,y,z+height);
	return PlayerPlaySound(playerid,1190,x,y,z+height);
}
Код:
stock GetPlayerAngle(playerid)
{
	new Float:angle;
	if(!IsPlayerInAnyVehicle(playerid)) GetPlayerFacingAngle(playerid,angle);
	else GetVehicleZAngle(GetPlayerVehicleID(playerid),angle)
	return floatround(angle);
}
Reply
#73

Quote:
Originally Posted by Adam_Chaprnka
Quote:
Originally Posted by Kapil
pawn Код:
stock GetUserName(playerid)
{
  new string[24];
  GetPlayerName(playerid,string,24);
  for(new i = 0; i < 24; i++)
  {
    if (string[i] == '_')
    {
      string[i] = ' ';
    }
  }
  return string;
}
Even more simpler.
pawn Код:
UnderscoreToSpace(name[])
{
    new pos = strfind(name,"_", true);
    if( pos != -1 )
        name[pos] = ' ';
}
the point was not about being simpiler, but faster.
Reply
#74

oh i thought those native functions were also made like this,


pawn Код:
strfind(const string[],const sub[],bool:ignorecase=false,pos=0)
{
    if(ignorecase == false)
    {
        for(new i = pos;i < 256; i++)
        {
            if(string[i] == sub[0])
            {
              new j;
              for(j = 0; j < 256;j++)
              {
                if(string[i+j] != sub[j]) break;
                continue;
              }
              if(j == strlen(sub))return i;
            }
        }
    }
    else
    {
        for(new i = pos;i < 256; i++)
        {
            if((string[i] == toupper(sub[0])) || (string[i] == tolower(sub[0])))
            {
              new j;
              for(j = 0; j < 256;j++)
              {
                if((string[i+j] != toupper(sub[j])) || (string[i+j] != tolower(sub[j]))) break;
                continue;
              }
              if(j == strlen(sub))return i;
            }
        }
    }
    return -1;
}
pawn Код:
strlen(const string[])
{
    for(new i = 0;i < 256;i++) if(string[i] == '\0') return i;
    return 0;
}
pawn Код:
tolower(c)
{
    if((c >= 65) && (c <= 90))  return c + ( ('Z' - c) + 7 + (c - 'A') );
    return c;
}
pawn Код:
toupper(c)
{
    if((c >= 97) && (c <= 122)) return c - ( ('z' - c) + 7 + (c - 'a') );
    return c;
}
Reply
#75

I did some short function today. It works like strfind(). That function uses '*' and '?' characters to find any character ('?') or string ('*'), like in MS Windows explorer.

Код:
stock FindStr(const str[],const sub[],bool:ignorecase=false,pos=0){
	new i=pos,j=1,x=-1,y,l=strlen(sub),bool:c;
	while(str[i]){
		if(sub[y]==42){
			if(!c)x=i;while(str[i+j-1]&&str[i+y-1]!=sub[y+1])j++;y++,c=true;
			if(y>=l)break;continue;
		}else{
			new bool:d;
			while(j){
				if(sub[y]==str[i]||sub[y]==63||(str[i]>=65&&str[i]<=122&&str[i]%32==sub[y]%32&&ignorecase)){
					if(!c)x=i;c=true,d=true,y++;if(sub[y-1]!=63){i++;break;}
				}i++,j--;
			}if(!d)y=0,x=-1,c=false;
		}j=1;if(y>=l)break;
	}return (y>=l?x:-1);
}
Made few tries today and I think, it works preety fine. If anyone found some gliches, please let me know.
Reply
#76

1. Hmm but still there cant be any change in the code even it is written in C or pawn.
2. But pawn doesnt recognize a string larger than 256, than wats the use( maybe untill its a packed stirng ).
3. oh

And also one more question( maybe offtopic )

< nvm moved to http://forum.sa-mp.com/index.php?top...3704#msg473704 >


Reply
#77

Regex:
Its Splits A String In a Array

Sample:
Код:
new str[25] = "hello how;are=you.doing?";
new out[6][20];
Regex(str, out, " ;=.");
for(new i=0; i<5; i++) printf("Regex %d: %s", i, out[i]);
Код:
Regex(const src[], dest[][], const seperators[])
{
	new aNum = 0, bNum = 0, eNum=0;
	do
	{
		for(new dNum=0; dNum<strlen(seperators); dNum++)
		{
		  if(src[eNum] == seperators[dNum]) { aNum++; bNum=0; eNum++; }
		}
		dest[aNum][bNum] = src[eNum];
		bNum++;
		eNum++;
	}
	while(eNum < strlen(src));
}
Reply
#78

New better random system

Код:
forward RandNumb(minamount, maxamount);
public RandNumb(minamount, maxamount)
{
	new Total = minamount+(maxamount/minamount)+random(maxamount-minamount);
    if(Total < minamount) Total = minamount;
    if(Total > maxamount) Total = maxamount;
	return Total;
}
Reply
#79

Oh sorry, didn't think about that
Reply
#80

You need this:
pawn Code:
forward split(const strsrc[], strdest[][], delimiter);
public split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc)){
      if(strsrc[i]==delimiter || i==strlen(strsrc)){
        len = strmid(strdest[aNum], strsrc, li, i, 128);
        strdest[aNum][len] = 0;
        li = i+1;
        aNum++;
        }
        i++;
    }
    return 1;
}


Useful for roleplay scripts, will only work in "Firstname_Lastname" format.

Example of usage:
pawn Code:
if(strcmp(cmd, "/firstname", true) == 0)
    {
        new wstring[128];
        format(wstring, sizeof(wstring), "Hello, %s.", GetPlayerFirstName(playerid));
        SendClientMessage(playerid,0xFFFFFFAA, wstring);
        return 1;
    }
pawn Code:
if(strcmp(cmd, "/lastname", true) == 0)
    {
        new wstring[128];
        format(wstring, sizeof(wstring), "Hello, Mr %s.", GetPlayerLastName(playerid));
        SendClientMessage(playerid,0xFFFFFFAA, wstring);
        return 1;
    }

GetPlayerFirstName(playerid);
GetPlayerLastName(playerid);

pawn Code:
stock GetPlayerFirstName(playerid)
{
    new namestring[2][MAX_PLAYER_NAME];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,MAX_PLAYER_NAME);
    split(name, namestring, '_');
    return namestring[0];
}
pawn Code:
stock GetPlayerLastName(playerid)
{
    new namestring[2][MAX_PLAYER_NAME];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,MAX_PLAYER_NAME);
    split(name, namestring, '_');
    return namestring[1];
}
Enjoy.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)