Useful Functions
#81

Quote:
Originally Posted by cosmy
Code:
stock isspace©
{
	if(c == ' ') return 1;
	else return 0;
}
native isspace©;
Pointless function

Quote:
Originally Posted by cosmy
Code:
stock CelsiusToFahrenheit(degrees)
{
	new f;
	f = degrees + 32;
	return f;
}
native CelsiusToFahrenheit(degrees);
Code:
stock FahrenheitToCelsius(degrees)
{
	new c;
	c = degrees - 32;
	return c;
}
native FahrenheitToCelsius(degrees);
These are highly inaccurate
Reply
#82

Code:
stock SMessage(playerid,color,str[],len=128,lines=20){
	if(!len||!str[0])return true;
	new c=0,x=0;if(len>128||len<0)len=128;
	for(new i=0,j,y=0;i<lines;y=0,i++){
		for(j=x+_:!i;j<x+len&&str[j];j++){
			if(str[j]<33){if(str[j-1]>32)y=j;}
		}if(!str[j])j=-1;else{
			if(!y){
				if(str[j]>32||str[j-1]<33)c=str[j];else c=0;y=j;
			}else c=0;str[y]=0;
		}if(playerid<0)SendClientMessageToAll(color,str[x]);
		else SendClientMessage(playerid,color,str[x]);
		if(j<0)break;str[y]=c;x=y+(c?0:1);
	}return true;
}
Sends a client messages to player or all players. The message is divided to reach maximum 'len' characters in one line. That will send ass many lines as needed between 0 and 'lines' parameter.
'playerid' is a player ID, which receive the 'str[]' message. Use -1 for all players.
'color' is color of the message. 'len' : max length of one line , 'lines' : the maximum of allowed lines.
This function should send words separated as their lenght are worse than 'len' parameter.
That's all.

By the way,
Quote:
Originally Posted by Vetle
Get Player ID:
pawn Code:
stock GetPlayerID(playerid)
{
    return playerid;
}
Yeah! That's very useful. Why didn't I use it so far?
Reply
#83

That's very inefficient.
Reply
#84

u're having a phobia about spaces and indentation or something?
Reply
#85

Quote:
Originally Posted by Pixels^
That's very inefficient.
It seems that this function does exactly, what it was suppose to do.
Look at the example:
Code:
#include <a_samp.inc>
public OnPlayerCommandText(playerid, cmdtext[]) {
	if(strcmp(cmdtext[1],"test",true)>2)
		return SMessage(playerid,0xFFFFFFFF,cmdtext[6],25);
	return 1;
}
Max length is 25.

in game:
Reply
#86

The encryption/decryption line by key
Code:
stock StringCoded(string[],key[],bool:method=true)
{
new keylen=strlen(key),
strl=strlen(string);
if(keylen==0) return printf("Error string coded (%s),incorrect key len",string);
for(new i=0;i<strl;i++){
if(method)string[i]+=keylen+key[i];else string[i]-=keylen+key[i];
}
return 1;
}
Example:


Code:
new str[]="coded_string";
new keystr[]="coded_key";


printf("%s <original string",str);
StringCoded(str,keystr,true); // or StringCoded(str,keystr);
printf("%s <encoded string",str);
StringCoded(str,keystr,false);
printf("%s <decoded string",str);
p.s Sorry bad english
Reply
#87

NOTE: UN-TESTED, but I think it might work (can't test it myself)

pawn Code:
stock GetPlayerFullname(partofname[])
{
    for(new i=0, m=GetMaxPlayers(); i<m; i++)
    {
        new pname[24];
        GetPlayerName(i, pname, 24);
        if(strfind(pname, partofname, true))
        {
            return pname;
        }
    }
    return -1;
}
pawn Code:
stock GetPlayerID(partofname[])
{
    for(new i=0, m=GetMaxPlayers(); i<m; i++)
    {
        new pname[24];
        GetPlayerName(i, pname, 24);
        if(strfind(pname, partofname, true))
        {
            return i;
        }
    }
    return -1;
}
Reply
#88

LarzI aka. D'x_Orzizt ye it surely should work I just wanted mention that you create the array 'pname' inside the loop (thats inefficient)

pawn Code:
stock GetPlayerID(partofname[])
{
    for(new i, m = GetMaxPlayers(), pname[MAX_PLAER_NAME]; i < m; i++)
    {
        GetPlayerName(i, pname, MAX_PLAYER_NAME);
        if(strfind(pname, partofname, true)) return i;
    }
    return INVALID_PLAYER_ID;
}
Ah and changed GetPlayerFullname, its indirect the same
pawn Code:
stock GetPlayerFullname(partofname[])
{
    new id = GetPlayerID(partofname), pname[MAX_PLAYER_NAME];
    if(id != INVALID_PLAYER_ID) GetPlayerName(id, pname, MAX_PLAYER_NAME);
    return pname;
}
Reply
#89

Quote:
Originally Posted by gesior7
Quote:
Originally Posted by Pixels^
That's very inefficient.
It seems that this function does exactly, what it was suppose to do.
Look at the example:
Код:
#include <a_samp.inc>
public OnPlayerCommandText(playerid, cmdtext[]) {
	if(strcmp(cmdtext[1],"test",true)>2)
		return SMessage(playerid,0xFFFFFFFF,cmdtext[6],25);
	return 1;
}
Max length is 25.

in game:
pawn Код:
stock pMessage(string[],maxlen)
{
  new str[128];
  format(str,maxlen,string);
  print(str);
  return 1;
}
pawn Код:
pMessage("HELLLO",2);
//prints "HE"
Reply
#90

Quote:
Originally Posted by Nero_3D
LarzI aka. D'x_Orzizt ye it surely should work I just wanted mention that you create the array 'pname' inside the loop (thats inefficient)

pawn Код:
stock GetPlayerID(partofname[])
{
    for(new i, m = GetMaxPlayers(), pname[MAX_PLAER_NAME]; i < m; i++)
    {
        GetPlayerName(i, pname, MAX_PLAYER_NAME);
        if(strfind(pname, partofname, true)) return i;
    }
    return INVALID_PLAYER_ID;
}
Ah and changed GetPlayerFullname, its indirect the same
pawn Код:
stock GetPlayerFullname(partofname[])
{
    new id = GetPlayerID(partofname), pname[MAX_PLAYER_NAME];
    if(id != INVALID_PLAYER_ID) GetPlayerName(id, pname, MAX_PLAYER_NAME);
    return pname;
}
OK, thanks for making it work (better)
Reply
#91

Quote:
Originally Posted by Pixels^
Quote:
Originally Posted by gesior7
Quote:
Originally Posted by Pixels^
That's very inefficient.
It seems that this function does exactly, what it was suppose to do.
Look at the example:
Код:
#include <a_samp.inc>
public OnPlayerCommandText(playerid, cmdtext[]) {
	if(strcmp(cmdtext[1],"test",true)>2)
		return SMessage(playerid,0xFFFFFFFF,cmdtext[6],25);
	return 1;
}
Max length is 25.

in game:
pawn Код:
stock pMessage(string[],maxlen)
{
  new str[128];
  format(str,maxlen,string);
  print(str);
  return 1;
}
pawn Код:
pMessage("HELLLO",2);
//prints "HE"
Do you understand what the SMessage function does? It's word wrap, not string cutting...
Reply
#92

Woops. , It wasn't my fault.
Reply
#93

Nevermind.
Reply
#94

Make a RP name for a Player (Removes the "_")


pawn Код:
GetPlayerNameEx(playerid)
{
  new name[MAX_PLAYER_NAME];
  GetPlayerName(playerid,name,MAX_PLAYER_NAME);
  for(new i=0;i<strlen(name);i++)
  if(name[i]=='_')
  name[i]=' ';
  return name;
}
Example how work with.
GetPlayerName(playerid, sendername, sizeof(sendername));
Replace it with
sendername=GetPlayerNameEx(playerid);



GetPlayerName(giveplayerid, giveplayer, sizeof(giveplayer));
Replace it with
giveplayer=GetPlayerNameEx(giveplayerid);

Easy right ?
Reply
#95

pawn Код:
for(new i=0;i<strlen(name);i++)
  if(name[i]=='_')
  name[i]=' ';
No brackets there. It will only process if(name[i ]=='_') for the loop.

pawn Код:
stock GetPlayerNameEx(playerid)
{
  new name[24];
  GetPlayerName(playerid,name,24);
  for(new i=0;i<strlen(name);i++) if(name[i]=='_')
  {
     name[i]=' ';
    return name;
  }
}
Reply
#96

Quote:
Originally Posted by Seif_
Quote:
Originally Posted by Norn
First you need:

pawn Код:
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;
}
GetPlayerRpName(playerid); Will remove the "_" and make it a space, for roleplay. Will only work with Firstname_Lastname format.

pawn Код:
stock GetPlayerRpName(playerid)
{
    new namestring[2][MAX_PLAYER_NAME];
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid,name,MAX_PLAYER_NAME);
    split(name, namestring, '_');
    new wstring[25];
    format(wstring, sizeof(wstring), "%s %s", namestring[0],namestring[1]);
    return wstring;
}
Example:

pawn Код:
if(strcmp(cmd, "/test", true) == 0)
    {
        new pName[MAX_PLAYER_NAME];
        new wstring[128];
        pName=GetPlayerFirstName(playerid);
        format(wstring, sizeof(wstring), "Hello %s!", GetPlayerRpName(playerid));
        SendClientMessage(playerid,COLOR_WHITE, wstring);
        return 1;
    }
Why do that if you can just use a loop in a string to get the _ and replace it by a space like this:
http://forum.sa-mp.com/index.php?top...7932#msg457932
Didn't find this when i searched, thanks.
Reply
#97

[size=20pt]zcmd beta v2
Now moved to the Wiki.

https://sampwiki.blast.hk/wiki/Zcmd
Reply
#98

Quote:
Originally Posted by cosmy
pawn Код:
stock CelsiusToFahrenheit(degrees)
{
    new f;
    f = degrees + 32;
    return f;
}
native CelsiusToFahrenheit(degrees);
pawn Код:
stock FahrenheitToCelsius(degrees)
{
    new c;
    c = degrees - 32;
    return c;
}
native FahrenheitToCelsius(degrees);
pawn Код:
stock CelsiusToFahrenheit(degrees)
{
    new f;
    f = degrees*1.8;
    f += 32;
    return f;
}
native CelsiusToFahrenheit(degrees);
pawn Код:
stock FahrenheitToCelsius(degrees)
{
    new c;
    c = degrees * 1.8;
    c -= 32;
    return c;
}
native FahrenheitToCelsius(degrees);
Reply
#99

Quote:
Originally Posted by [K4L
Leopard ]
Quote:
Originally Posted by cosmy
pawn Код:
stock CelsiusToFahrenheit(degrees)
{
    new f;
    f = degrees + 32;
    return f;
}
native CelsiusToFahrenheit(degrees);
pawn Код:
stock FahrenheitToCelsius(degrees)
{
    new c;
    c = degrees - 32;
    return c;
}
native FahrenheitToCelsius(degrees);
pawn Код:
stock CelsiusToFahrenheit(degrees)
{
    new f;
    f = degrees*1.8;
    f += 32;
    return f;
}
native CelsiusToFahrenheit(degrees);
pawn Код:
stock FahrenheitToCelsius(degrees)
{
    new c;
    c = degrees * 1.8;
    c -= 32;
    return c;
}
native FahrenheitToCelsius(degrees);
you can just take that function out of the pawn-lang.pdf
Reply

do i put this anywhere? or is this a usage of it?
i am confused on how to use any of these commands

Quote:
Originally Posted by ******
Код:
GetPlayerId(playername[])
{
	for (new i = 0; i < MAX_PLAYERS; i++)
	{
		if ((IsPlayerConnected(i)) && (strcmp(playername, gPlayerName[i], true) == 0))
		{
            return i;
     	}
	}
	return INVALID_PLAYER_ID;
}
Edit: added a ';' only just spotted as missing by DonnyK.
Reply


Forum Jump:


Users browsing this thread: 8 Guest(s)