SA-MP Forums Archive
Useful Functions - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+--- Thread: Useful Functions (/showthread.php?tid=38965)



Re: Useful Functions - TheArcher - 27.12.2012

@PhillipS You made my day LMAO. Thanks for making me laughing as hell...


Re: Useful Functions - Dragonborn - 29.12.2012

ContainInvalidCharacters
I created this function on any time ago... Good to check and create "normal" passwords and to set player name.

Check if have invalid characters in one string.

pawn Код:
#define function%0(%1) stock %0(%1)

function ContainInvalidCharacters(string[])
{
    // Add more if you need/want. (if you add new characters, you should add the quantity of characters that you put to the variable. ("19"))
    new InvalidCharacters[19][] =
    {
        "&","$","^","?","_",".","*","#","(",")",
        "[","]","{","}","=","+","-","!","@"    
    };
   
    for(new checkloop; checkloop<sizeof(InvalidCharacters); checkloop++)
    {
        if(strfind(string, InvalidCharacters[checkloop]) != -1) return true;
    }
   
    return false;
}
An example :
pawn Код:
public OnGameModeInit()
{
    new varstring[15] = "mystringwith@";
    if(ContainInvalidCharacters(varstring)) return print("- 'varstring' contains invalid characters !");
   
    return 1;
}
Output :
Quote:

[04:42:32] Filterscripts
[04:42:32] ---------------
[04:42:32] Loading filterscript 'gl_actions.amx'...
[04:42:32] Loading filterscript 'gl_realtime.amx'...
[04:42:32] Loaded 2 filterscripts.

[04:42:32] - 'varstring' contains invalid characters !
[04:42:32] Number of vehicle models: 0

Enjoy and i hope it is useful !


Re: Useful Functions - RyDeR` - 29.12.2012

You don't need an array for that. I guess you're just looking for numbers and alphabetic characters:
pawn Код:
stock isValidPassword(const szPass[]) {
    for(new i = 0; szPass[i] != EOS; ++i) {
        switch(szPass[i]) {
            case '0'..'9', 'A'..'Z', 'a'..'z':
                continue;
            default:
                return 0;
        }
    }
    return 1;
}



AW: Useful Functions - IPrototypeI - 02.01.2013

PHP код:
stock MoveCameraInCircelRotation(playerid,Float:winkel2Float:x_koordinateFloat:y_koordinateFloat:z_koordinateFloat:radius1Float:winkel)
{
    
PlayerCamInfo[playerid][player_degree1] =(winkel2 360)?(0):((winkel 0)?(360):(PlayerCamInfo[playerid][player_degree1]));
    
PlayerCamInfo[playerid][player_degree2] =(winkel 360)?(0):((winkel 0)?(360):(PlayerCamInfo[playerid][player_degree2]));
    
SetPlayerCameraLookAt(playerid,x_koordinate,y_koordinate,z_koordinate);
    
x_koordinate += (radius1 floatcos(winkeldegrees) * floatcos(winkel2degrees));
    
y_koordinate += (radius1 floatcos(winkeldegrees) * floatsin(winkel2degrees));
    
z_koordinate += (radius1 floatsin(winkeldegrees));
    
SetPlayerCameraPos(playerid,x_koordinate,y_koordinate,z_koordinate);
    return 
1;

with this calculation you get the koordination from a globe to rotate you camera around it
but you have to chance some variabel for example PlayerCamInfo[playerid][player_degree2]


AW: Useful Functions - IPrototypeI - 02.01.2013

PHP код:
stock SetPlayerFacingAngleToPoint(playerid,Float:XFloat:Y){
   new 
FloatPos[2];
   
GetPlayerPos(playeridPos[0], Pos[1], 0);
   return 
SetPlayerFacingAngle(playerid,180.0-atan2(Pos[0]-X,Pos[1]-Y));

This code set your FacingAnglethe the direction of a point it can be useful to rotate a bot(using RNPC) that he can shot at you.


Re: Useful Functions - smeti - 02.01.2013

@[HLF]Southclaw
This makes it easier:
pawn Код:
stock
    Float:absoluteangle2(Float:angle)
{
    while(angle < 0.0) angle += 360.0
    while(angle > 360.0) angle -= 360.0;
    return angle;
}



AW: Useful Functions - IPrototypeI - 02.01.2013

PHP код:
stock GetAge(string[])
{
       new 
var0var1var2Date[3], age;
       
sscanf(string"p<.>iii"var0var1var2);
    if(
var0 31 || var0 <= 0)return 0;
    if(
var1 12 || var1 <= 0)return 0;
    
getdate(Date[0],Date[1],Date[2]);
    if(
var2 Date[0] || var2 1900)return 0;
       
age Date[0] - var2 - ((Date[1] < var1)? : ((Date[1] == var1 && Date[2] < var0)? 0));
       return 
age;

with this code it is possibel to calculate the age with a date as a string


Re: Useful Functions - Sasino97 - 06.01.2013

Today I was looking at the PAWN example scripts (downloaded from CompuPhase's website some time ago) and I noticed a function that can be useful also in SA-MP. So I decided to edit it to make it avaiable in PAWN 3.0 and to paste it here
pawn Код:
rot13(string[])
{
    for(new index = 0; string[index]; index ++)
    {
        if ('a' <= string[index] <= 'z')
            string[index] = (string[index] - 'a' + 13) % 26 + 'a';
        else if ('A' <= string[index] <= 'Z')
            string[index] = (string[index] - 'A' + 13) % 26 + 'A';
    }
    return 0;
}
// By CompuPhase
That function will mangle the string you enter.

The original was this
pawn Код:
rot13(string[])
    {
    for (new index = 0; string[index]; index++)
        if ('a' <= string[index] <= 'z')
            string[index] = (string[index] - 'a' + 13) % 26 + 'a'
        else if ('A' <= string[index] <= 'Z')
            string[index] = (string[index] - 'A' + 13) % 26 + 'A'
    }
but it will work only in pawn 4, and SA-MP uses pawn 3.


Respuesta: Useful Functions - [DOG]irinel1996 - 08.01.2013

Someone suggested this for SA-MP 0.3x, so I tried to make it:
pawn Код:
stock GetVehiclePlayerInSeat(vehicleid, seatid) {
    if (!GetVehicleModel(vehicleid)) return INVALID_VEHICLE_ID;
    new
        total = GetMaxPlayers()
    ;
    for (new x; x < total; x++) {
        if (IsPlayerConnected(x)) {
            if (IsPlayerInAnyVehicle(x) && GetPlayerVehicleSeat(x) == seatid) {
                return x; //Thanks ****** for making me notice this. (player id 0 issue)
                break;
            }
        }
    }
    return INVALID_PLAYER_ID; //Thanks Slice
}
Returns:
x -> player's id who is seated on the seatid.
INVALID_VEHICLE_ID -> the vehicle doesn't exist.
INVALID_PLAYER_ID -> the seatid is empty.


PD: I didn't test it, but I guess it should work.

Best regards!

EDIT: Tested.


Respuesta: Re: Useful Functions - [DOG]irinel1996 - 08.01.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
What if player 0 is in that seat? Also, the rules explicitly state only posting TESTED code.
To be honest, I can't see the error, when x is declared its value should be 0, or not?

EDIT: Ohh, I'm so stup*d, I just saw that.

PD: I'm going to test it right now.


Re: Respuesta: Re: Useful Functions - Grim_ - 08.01.2013

Quote:
Originally Posted by [DOG]irinel1996
Посмотреть сообщение
To be honest, I can't see the error, when x is declared its value should be 0, or not?

EDIT: Ohh, I'm so stup*d, I just saw that.

PD: I'm going to test it right now.
If it were player 0 in that seat, it would return 0, saying that the seat is actually empty.

Your signature is also very immature.


Respuesta: Re: Respuesta: Re: Useful Functions - [DOG]irinel1996 - 08.01.2013

Quote:
Originally Posted by Grim_
Посмотреть сообщение
If it were player 0 in that seat, it would return 0, saying that the seat is actually empty.

Your signature is also very immature.
Yes, after ****** answer I saw that, I didn't think about it.

PD: unfortunately, there are many important things that are more immature than my signature. As they say, YOLO.


Re: Useful Functions - Slice - 08.01.2013

Use INVALID_PLAYER_ID, not -1. Please.


Respuesta: Re: Useful Functions - [DOG]irinel1996 - 08.01.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
To be fair, I can see the logic in using different values for non-existant and empty seat.
I guess so.
Maybe, different conditions with other messages, as scripter wants. (?)


AW: Useful Functions - IPrototypeI - 09.01.2013

with SecToMin you can convert seconds to minutes
for example

119 secounds are 1:59 minutes
PHP код:
stock SecToMin(sec) {
    return 
printf("%02d:%02d", (sec/60), sec 60);

and with this code you can convert milliseconds to minutes
example:
182412 ms are 3:02:412 minutes

PHP код:
stock MsToMin (sec) {
    return 
printf("%02d:%02d:%02d", ((sec / (1000*60)) % 60), (sec 1000) % 60,sec %1000);




Re: Respuesta: Useful Functions - Emmet_ - 09.01.2013

Quote:
Originally Posted by [DOG]irinel1996
Посмотреть сообщение
Someone suggested this for SA-MP 0.3x, so I tried to make it:
pawn Код:
stock GetVehiclePlayerInSeat(vehicleid, seatid) {
    if (!GetVehicleModel(vehicleid)) return INVALID_VEHICLE_ID;
    new
        total = GetMaxPlayers()
    ;
    for (new x; x < total; x++) {
        if (IsPlayerConnected(x)) {
            if (IsPlayerInAnyVehicle(x) && GetPlayerVehicleSeat(x) == seatid) {
                return x; //Thanks ****** for making me notice this. (player id 0 issue)
                break;
            }
        }
    }
    return INVALID_PLAYER_ID; //Thanks Slice
}
Returns:
x -> player's id who is seated on the seatid.
INVALID_VEHICLE_ID -> the vehicle doesn't exist.
INVALID_PLAYER_ID -> the seatid is empty.


PD: I didn't test it, but I guess it should work.

Best regards!

EDIT: Tested.
That's going to give a warning. You can't have the break keyword under the return keyword, so you'll have to remove the break;


Re: Useful Functions - LarzI - 16.01.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
Slice used to have a very good website where you could watch macros get expanded slowly to study them, but sadly it seems to be down at the minute. It was VERY useful for looking at macros like this.
I know, I've tried refreshing the page the last 48 hours, but I've never gotten to view it.

Quote:
Originally Posted by ******
Посмотреть сообщение
"|||" is used because it can never appear normally in valid code, so macros can scan for it to find the end of a block of text. Another one I like to use is "Ј" because it's on my keyboard (being British), but I don't tend to release scripts with it due to possible character set issues.
Oh okay, thanks for the info. Appreciate it.


Re: Useful Functions - DarkPower - 20.01.2013

For RP servers

pawn Код:
stock getName(playerid)
{
    new sNick_[MAX_PLAYER_NAME];
    GetPlayerName(playerid, sNick_, MAX_PLAYER_NAME);
    for(new iX__ = (0); iX__ < strlen(sNick_); ++ iX__) if(sNick_[iX__] == '_') sNick_[iX__] = (32);
    return format(sNick_, MAX_PLAYER_NAME, "%s", sNick_), (sNick_);
}
Example, lets say that my nick is Andrew_Kramer:

pawn Код:
new string[64];
format(string, (sizeof string), "My name is: %s", getName(playerid));
SendClientMessage(playerid, -1, string);
Server will print My name is: Andrew Kramer


Re: Useful Functions - TheArcher - 20.01.2013

@DarkPower Why "_" after the variables? E.g "new sNick_" or "new iX__" makes no sense.


Re: Useful Functions - Emmet_ - 20.01.2013

@DarkPower: You shouldn't use strlen in a loop. And the format wasn't necessary.

pawn Код:
stock getName(playerid)
{
    new pos = -1, name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    while ((pos = strfind(name, "_", true)) != -1)
    {
        name[pos] = ' ';
    }
    return name;
}