Useful Functions

Or perhaps just..

pawn Код:
if (string[strlen(string)-1] == 's')
{
  // 's' character at the end
}
?
Reply

Just a little tip - GetPlayerName returns name length, so you don't need the strlen()

pawn Код:
new len = GetPlayerName(playerid, playername, sizeof(playername));
if (name[len-1] == 's')
{
  // ....
}
Reply

IntToFormattedStr(integer):[*] Returns a formatted string adding commas to sepparate every 3 digits. (For example, 1000 will return "1,000", 53426244, will return "53,426,244", and 245 will return "245".

pawn Код:
stock IntToFormattedStr(integer)
{
    // By Zamaroht
    new value[20], string[20];

    valstr(value, integer);

    new charcount;
    for(new i = strlen(value); i >= 0; i --)
    {
        format(string, sizeof(string), "%c%s", value[i], string);
        if(charcount == 3)
        {
          if((integer >= 0 && i != 0) || (integer < 0 && i > 1))
                format(string, sizeof(string), ",%s", string);
               
            charcount = 0;
        }
        charcount ++;
    }

    return string;
}
PS: Performance can be improved.

*edit 11 april 2010:
Fixed a problem with negative integers.
Reply

@Seif:

printf( "%d", CreateObject( 1989, 0, 0, 0, 0, 0, 0 ) );

Test that in a blank script and then think about the structure of your loop, the condition it will continue to run under (while(true)).
Reply

pawn Код:
stock Cenzura(string[],word[],destch='*')
{
    new start_index=(-1),
      end_index=(-1);
    start_index=strfind(string,word,true);
    if(start_index==(-1)) return false;
    end_index=(start_index+strlen(word));
    for( ; start_index<end_index; start_index++) string[start_index]=destch;
    return true;
}
pawn Код:
new string[32]="testing the function";
Cenzura(string,"testing"); -> output: ******* the function
Cenzura(string,"testing",'Ч'); -> output: ЧЧЧЧЧЧЧ the function

Filterscript
pawn Код:
#include <a_samp>

#define MAX_LEN 32
#define MAX_ENTRY 120
#define SOURCE "cenzura.txt"

static Cenzuralt_Szo[MAX_ENTRY][MAX_LEN];

public OnFilterScriptInit() {
    if(!fexist(SOURCE)) return false;
    new File:myFile,
        line[MAX_LEN],
        index=0;
    myFile=fopen(SOURCE,filemode:io_read);
    while(fread(myFile,line,sizeof line) && (index != MAX_ENTRY)) {
        if(strlen(line)>MAX_LEN) continue;
        StripNewLine(line);
        strmid(Cenzuralt_Szo[index],line,0,strlen(line),sizeof line);
        index++;
    }
    return true;
}

public OnPlayerText(playerid,text[])
{
    for(new i=0; i<MAX_ENTRY; i++) if(!Cenzuralt_Szo[i][0]) continue;else Cenzura(text,Cenzuralt_Szo[i]);
    return true;
}

stock StripNewLine(str[]) // ysi-misc.own
{
    new
        l = strlen(str);
    while (l-- && str[l] <= ' ') str[l] = '\0';
}

stock Cenzura(string[],word[],destch='*')
{
    new start_index=(-1),
      end_index=(-1);
    start_index=strfind(string,word,true);
    if(start_index==(-1)) return false;
    end_index=(start_index+strlen(word));
    for( ; start_index<end_index; start_index++) string[start_index]=destch;
    return true;
}
cenzura.txt (Example)
Код:
gay
shit
fuck
Reply

if(a>316&&a<45)

How can a be over 316 and under 45 at the same time?
Reply

Quote:
Originally Posted by Finn
if(a>316&&a<45)

How can a be over 316 and under 45 at the same time?
cirlce ... degrees, 0° - 360°

North (0°)
/\
West (270°) <-|-> East (90°)
\/
South (180°)

and there arent that much checks needed, 4 would be enough
pawn Код:
//for easier usage
#define North (1)
#define East  (2)
#define South (3)
#define West  (4)
pawn Код:
stock GetNESW(playerid)
{
    new Float:angle;
    if(!GetPlayerFacingAngle(playerid, angle)) return 0;
    else if(angle >= 315.0)          return North;
    else if(angle >= 225.0)          return West;
    else if(angle >= 125.0)          return South;
    else if(angle >= 45.0)          return East;
    else                    return North;
}
Reply

Quote:
Originally Posted by ♣ Joker ♠
Quote:
Originally Posted by Finn
if(a>316&&a<45)

How can a be over 316 and under 45 at the same time?
cirlce ... degrees, 0° - 360°

North (0°)
/\
West (270°) <-|-> East (90°)
\/
South (180°)

and there arent that much checks needed, 4 would be enough
pawn Код:
//for easier usage
#define North (1)
#define East  (2)
#define South (3)
#define West  (4)
pawn Код:
stock GetNESW(playerid)
{
    new Float:angle;
    if(!GetPlayerFacingAngle(playerid, angle)) return 0;
    else if(angle >= 315.0)          return North;
    else if(angle >= 225.0)          return West;
    else if(angle >= 125.0)          return South;
    else if(angle >= 45.0)          return East;
    else                    return North;
}
You're mistaken, the angle can be 0, so do
pawn Код:
if(!GetPlayerFacingAngle(playerid, angle)) return North;
And you also forgot using the variable.
Reply

Код:
if(!GetPlayerFacingAngle(playerid, angle)) return North;
won't work because GetPlayerFacingAngle will always return 0, it doesn't return the angle.
use
Код:
if(angle == 0)
Reply

GetPlayerFacingAngle: This function does not return a specific value, it's best to simply ignore it.

It only returns 0 if it fails and 1 if it succeeds, so if it fails (which can happen when the player isnt connected) it returns 0

There wouldnt be any reason to check if the angle is exact 0..., would it ?
Reply

Actually, it can just be
pawn Код:
GetVehicleMaxSeats(vehicleid)
{
    if(GetVehicleModel(vehicleid)) return VehicleSeats[modelid - 400];
    else return 0;
}
since GetVehicleModel returns 0 on invalid vehicles
And isn't the passenger limit for the Bus and Coach 7 ?
Reply

Код:
GetVehicleMaxSeats(vehicleid)return (GetVehicleModel(vehicleid)) ? (VehicleSeats[modelid - 400]) : (0);
Reply

You guys are missing something important - the declaration of modelid

Quote:
Originally Posted by dice7
GetVehicleMaxSeats(vehicleid)
{
if(GetVehicleModel(vehicleid)) return VehicleSeats[modelid - 400];
else return 0;
}
Quote:
Originally Posted by BlackFoX_UD_
GetVehicleMaxSeats(vehicleid) return (GetVehicleModel(vehicleid)) ? (VehicleSeats[modelid - 400]) : (0);
pawn Код:
stock GetVehicleMaxSeats(vehicleid)
    return ((vehicleid = GetVehicleModel(vehicleid)) ? VehicleSeats[vehicleid - 400] : (0));
Reply

vehicleid isnt the Model of Vehicle

Код:
(vehicleid = GetVehicleModel(vehicleid)
(vehicleid == GetVehicleModel(vehicleid)
Reply

Quote:
Originally Posted by BlackFoX_UD_
vehicleid isnt the Model of Vehicle

Код:
(vehicleid = GetVehicleModel(vehicleid)
(vehicleid == GetVehicleModel(vehicleid)
yes indeed but "vehicleid" is just another variable

Another version which does the same thing
pawn Код:
stock GetVehicleMaxSeats(vehicleid)
    if((vehicleid = GetVehicleModel(vehicleid))) return VehicleSeats[vehicleid - 400]; else return 0;
And now we just put the assignment out of the statment
pawn Код:
stock GetVehicleMaxSeats(vehicleid) {
    vehicleid = GetVehicleModel(vehicleid);
    if(vehicleid) return VehicleSeats[vehicleid - 400];
    else return 0;
}
1. Line: We assign the modelid of the vehicle to the variable vehicleid
2. Line: We check if the saved modelid is logical true (means != 0) and return the seats number from (vehicleid[which stores the modelid] - 400)
3. Line: If the check fails (also vehicleid is logical false ( == 0)) we return 0;

Vehicleid is just a variable name, nothing else - you can store any number in it
pawn Код:
stock GetVehicleMaxSeats(var)
    if((var = GetVehicleModel(var))) return VehicleSeats[var - 400]; else return 0;
Reply

Alternate of Playername(playerid);
pawn Код:
new PlayerName[MAX_PLAYERS][MAX_PLAYER_NAME]; //To upper

GetPlayerName(playerid, PlayerName[playerid], MAX_PLAYER_NAME); //to OnPlayerConnect
Usage example:
format(string, sizeof(string), "%s has blowed up", PlayerName[playerid]);
Reply

Quote:
Originally Posted by S2D.Alone_ (RU)
Alternate of Playername(playerid);
pawn Код:
new PlayerName[MAX_PLAYERS][MAX_PLAYER_NAME]; //To upper

GetPlayerName(playerid, PlayerName[playerid], MAX_PLAYER_NAME); //to OnPlayerConnect
Usage example:
format(string, sizeof(string), "%s has blowed up", PlayerName[playerid]);
Useless..

pawn Код:
stock PlayerName(playerid)
{
  new
      name[MAX_PLAYER_NAME];
  GetPlayerName(playerid, name, sizeof(name));
  return name;
}

format(string, sizeof(string), "%s", PlayerName(playerid));
Reply

Quote:
Originally Posted by Don Correlli
Quote:
Originally Posted by S2D.Alone_ (RU)
Alternate of Playername(playerid);
pawn Код:
new PlayerName[MAX_PLAYERS][MAX_PLAYER_NAME]; //To upper

GetPlayerName(playerid, PlayerName[playerid], MAX_PLAYER_NAME); //to OnPlayerConnect
Usage example:
format(string, sizeof(string), "%s has blowed up", PlayerName[playerid]);
Useless..

pawn Код:
stock PlayerName(playerid)
{
  new
      name[MAX_PLAYER_NAME];
  GetPlayerName(playerid, name, sizeof(name));
  return name;
}

format(string, sizeof(string), "%s", PlayerName(playerid));
There is always the memory wasting way which is usually faster and the normal way

And he said "Alternate of PlayerName(playerid);"
Reply

Quote:
Originally Posted by ♣ Joker ♠
There is always the memory wasting way which is usually faster and the normal way

And he said "Alternate of PlayerName(playerid);"
His way won't work if player changes his name (SetPlayerName).
Reply

Quote:
Originally Posted by Don Correlli
Quote:
Originally Posted by ♣ Joker ♠
There is always the memory wasting way which is usually faster and the normal way

And he said "Alternate of PlayerName(playerid);"
His way won't work if player changes his name (SetPlayerName).
Using your words.
Quote:
Originally Posted by Don Correlli
You can script that in a less than a minute.
Quote:
Originally Posted by Don Correlli
You can script that in a less than a minute.
Quote:
Originally Posted by Don Correlli
You can script that in a less than a minute.
-----------------------
Quote:
Originally Posted by S2D.Alone_ (RU)
Alternate of Playername(playerid);
pawn Код:
new PlayerName[MAX_PLAYERS][MAX_PLAYER_NAME]; //To upper

GetPlayerName(playerid, PlayerName[playerid], MAX_PLAYER_NAME); //to OnPlayerConnect
Usage example:
format(string, sizeof(string), "%s has blowed up", PlayerName[playerid]);
It would be useful to make define, so people don't need to edit their scripts.
pawn Код:
#define PlayerName(%1) PlayerName[%1]
Reply


Forum Jump:


Users browsing this thread: 22 Guest(s)