SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 28/06/2017, 12:12 AM   #4651
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,044
Reputation: 519
Default Re: Useful Functions

Quote:
Originally Posted by coool View Post
I just do this
PHP Code:
new randomplayer random(GetPlayerPoolSize);
while(!
IsPlayerConnected(randomplayer))
{
    
randomplayer random(GetPlayerPoolSize);

That's a terrible method and incredibly inefficient. GetPlayerPoolSize gets the highest connected player ID, meaning if you have 1 player in your server, and they have player ID 100, it could take thousands of iterations before they're even selected. Not to mention your code is wrong, so that player would never be selected. The 'random' function returns a value between 0 and input minus 1, so the highest player ID is unachievable.
__________________

SERVER.LVCNR.NET:7777
Threshold is offline   Reply With Quote
Old 28/06/2017, 10:46 AM   #4652
coool
Huge Clucker
 
Join Date: Feb 2013
Location: Pakistan, Sindh
Posts: 449
Reputation: 23
Default Re: Useful Functions

Quote:
Originally Posted by Threshold View Post
...
Thanks, for telling it.
__________________
My Writing Works
Lost In Future

coool is offline   Reply With Quote
Old 12/07/2017, 05:43 PM   #4653
PeterLinkson
Big Clucker
 
PeterLinkson's Avatar
 
Join Date: Jul 2017
Location: Rio de janeiro, Brasil.
Posts: 77
Reputation: 14
Default Re: Useful Functions

List of admin commands

Log in rcon
( /rcon login [password] )
  • Pictures




  • Script

PHP Code:
enum EnumAdminCommands {command[45],information[80]};
new const 
AdminCommands[][EnumAdminCommands] = //{"command","information"}
{
    {
"/Admincmd","List of commands for admin"},
    {
"/Example","This command does not exist!"},
    {
"/Example2","..."}
};


CMD:admincmd(playerid//using include izcmd
{
    if(!
IsPlayerAdmin(playerid)) //change
        
return 0;

    new 
ss[50],s[500];
    for(new 
isizeof(AdminCommands); i++)
    {
        
format(ss50"%s\n"AdminCommands[i][command]);
        
strcat(sss);
    }

    
ShowPlayerDialog(playerid888DIALOG_STYLE_LIST"Admin Commands"s"more""close");
    return 
1;
}

public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[ ])
{
    if(
dialogid == 888)
    {
        if(!
response)
            return 
0;
        
        new 
s[80];
        for(new 
isizeof(AdminCommands); i++)
        if(
listitem == iformat(s80"%s"AdminCommands[i][information]);
        
        return 
ShowPlayerDialog(playerid889DIALOG_STYLE_MSGBOX"Admin Commands"s,"close","");
    }
    return 
0;


I was going to throw it in the trash, but it's a job.
Sorry for my english and if I posted on the wrong topic.
PeterLinkson is offline   Reply With Quote
Old 12/07/2017, 06:25 PM   #4654
Meller
Gangsta
 
Meller's Avatar
 
Join Date: Dec 2016
Posts: 550
Reputation: 90
Default Re: Useful Functions

Quote:
Originally Posted by zLink View Post
List of admin commands

Log in rcon
( /rcon login [password] )
  • Pictures




  • Script

PHP Code:
enum EnumAdminCommands {command[45],information[80]};
new const 
AdminCommands[][EnumAdminCommands] = //{"command","information"}
{
    {
"/Admincmd","List of commands for admin"},
    {
"/Example","This command does not exist!"},
    {
"/Example2","..."}
};


CMD:admincmd(playerid//using include izcmd
{
    if(!
IsPlayerAdmin(playerid)) //change
        
return 0;

    new 
ss[50],s[500];
    for(new 
isizeof(AdminCommands); i++)
    {
        
format(ss50"%s\n"AdminCommands[i][command]);
        
strcat(sss);
    }

    
ShowPlayerDialog(playerid888DIALOG_STYLE_LIST"Admin Commands"s"more""close");
    return 
1;
}

public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[ ])
{
    if(
dialogid == 888)
    {
        if(!
response)
            return 
0;
        
        new 
s[80];
        for(new 
isizeof(AdminCommands); i++)
        if(
listitem == iformat(s80"%s"AdminCommands[i][information]);
        
        return 
ShowPlayerDialog(playerid889DIALOG_STYLE_MSGBOX"Admin Commands"s,"close","");
    }
    return 
0;


I was going to throw it in the trash, but it's a job.
Sorry for my english and if I posted on the wrong topic.
Oooor just:
PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[ ]) 

    if(
dialogid == 888
        if(
response
              
ShowPlayerDialog(playerid889DIALOG_STYLE_MSGBOX"Admin Commands"AdminCommands[i][information], "Close",""); 

Don't return false if OnDialogResponse may be used somewhere else again.
Meller is offline   Reply With Quote
Old 12/07/2017, 06:43 PM   #4655
Paulice
Gangsta
 
Join Date: Jul 2017
Posts: 497
Reputation: 44
Default Re: Useful Functions

Quote:
Originally Posted by Eoussama View Post
Select Random Player

PHP Code:
stock SelectRandomPlayer()
{
    new 
GetPlayerPoolSize(),i,j,T[k];
    for(
i=0i<ki++){
        if(
IsPlayerConnected(i)){
            
T[j] = i;
            
j++;
        }
    }
    return 
T[random(j)];

PHP Code:
SelectRandomPlayer()
{
    new 
players[MAX_PLAYERS], count;
    for(new 
0GetPlayerPoolSize(); <= j++)
    {
        
IsPlayerConnected(i) && (players[count ++] = i);
    }
    return ((!
count) ? (-1) : (players[random(count)]));

Returns -1 if no players are connected.

Alternatively (for a tad better performance):
Quote:
Originally Posted by OneDay View Post
PHP Code:
Iter_Random(Player); 
or

Have a global array store connected players and remove the ones that leave, filling the slots in a continuous order.
Paulice is offline   Reply With Quote
Old 12/07/2017, 10:23 PM   #4656
Meller
Gangsta
 
Meller's Avatar
 
Join Date: Dec 2016
Posts: 550
Reputation: 90
Default Re: Useful Functions

Quote:
Originally Posted by zLink View Post
no bump please.
Do you know what you're talking about?

@Edit I'm not a machine, just because you do it one way, and you think you're better.
This does not mean that you should force people to do the same.

http://wiki.sa-mp.com/wiki/OnDialogResponse
Just.. kys?
Meller is offline   Reply With Quote
Old 25/07/2017, 03:57 AM   #4657
PeterLinkson
Big Clucker
 
PeterLinkson's Avatar
 
Join Date: Jul 2017
Location: Rio de janeiro, Brasil.
Posts: 77
Reputation: 14
Default Re: Useful Functions

Useful
PHP Code:
stock IsValidRPName(name[]) //credits: zLink and base Larceny
{
    if((
'A' <= name[0] <= 'Z') && ('a' <= name[1] <= 'z'))
    {
        if(
IsValidRPInput(name))
        {
            if(
strfind(name"_") != -1)
            {
                new 
underline strfind(name"_"), pos underline +1;
                if((
'A' <= name[pos] <= 'Z') && ('a' <= name[pos +1] <= 'z'))
                {
                    for(new 
istrlen(name); ++i)
                    {
                        if((
'A' <= name[i] <= 'Z') && (!= 0) && (!= pos)) return 0;
                        else if((
name[i] == '_') && (!= underline)) return 0;
                        else continue;
                    }
                    return 
1;
                }
            }
        }
    }
    return 
0;
}

IsValidRPInput(const ipstr[]) //credits: zLink and base PT
{
    for(new 
0ipstr[i] != EOS; ++i)
    {
        switch(
ipstr[i])
        {
            case 
'A'..'Z''a'..'z','_': continue;
            default: return 
0;
        }
    }
    return 
1;

Example
Code:
CMD:test(playerid, params[])
{
    if(isnull(params))
	return SendClientMessage(playerid, 0xFF0000FF,"use: /test [your name]");

    if(IsValidRPName(params))
    {
        //is valid
    }
    else
    {
        //not is valid
    }
    return 1;
}
PeterLinkson is offline   Reply With Quote
Old 12/09/2017, 06:26 PM   #4658
Yousha
Big Clucker
 
Join Date: May 2013
Posts: 115
Reputation: 5
Default Re: Useful Functions

Quote:
Originally Posted by Lokii View Post
Get weapon slot
Code:
stock GetWeaponSlot(weaponid)
{
    switch(weaponid)
    {
        case 0,1: return 0;
        case 2,3,4,5,6,7,8,9: return 1;
        case 10,11,12,13,14,15: return 10;
        case 16,17,18: return 8;
        case 22,23,24: return 2;
        case 25,26,27: return 3;
        case 28,29,32: return 4;
        case 30,31: return 5;
        case 33,34: return 6;
        case 35,36,37,38: return 7;
        case 39: return 8;
        case 40: return 12;
        case 41,42,43: return 9;
        case 44,45,46: return 11;
    }
    return -1;
}
Just some minor changes:

Code:
// stock GetWeaponSlotID(const weaponid)
stock GetWeaponSlot(const weaponid)
{
   switch(weaponid)
   {
      case 0,1: return 0;
      case 2 .. 9: return 1;
      case 10 .. 15: return 10;
      case 16 .. 18: return 8;
      case 22 .. 24: return 2;
      case 25 .. 27: return 3;
      case 28,29,32: return 4;
      case 30,31: return 5;
      case 33,34: return 6;
      case 35 .. 38: return 7;
      case 39: return 8;
      case 40: return 12;
      case 41 .. 43: return 9;
      case 44 .. 46: return 11;
   }

   return -1;
}
__________________

^ OLDEST and MOST Stable Linux distro! ^
Yousha is offline   Reply With Quote
Old 13/09/2017, 09:24 PM   #4659
Bren
Big Clucker
 
Bren's Avatar
 
Join Date: Jul 2014
Location: Russia Orenburg
Posts: 66
Reputation: 0
Default Re: Useful Functions

Quote:
Originally Posted by Lokii View Post
Get weapon slot

Code:
stock GetWeaponSlot(weaponid)
{
    switch(weaponid)
    {
        case 0,1: return 0;
        case 2,3,4,5,6,7,8,9: return 1;
        case 10,11,12,13,14,15: return 10;
        case 16,17,18: return 8;
        case 22,23,24: return 2;
        case 25,26,27: return 3;
        case 28,29,32: return 4;
        case 30,31: return 5;
        case 33,34: return 6;
        case 35,36,37,38: return 7;
        case 39: return 8;
        case 40: return 12;
        case 41,42,43: return 9;
        case 44,45,46: return 11;
    }
    return -1;
}
example 1:

Code:
CMD:test(playerid, params[])
{
     new x[15];
     format(x, sizeof(x), "Minigun slot: %d",  GetWeaponSlot(38));
     printf(x);
     return 1;
}
example 2:

Code:
CMD:test2(playerid, params[])
{
     new x[23];
     format(x, sizeof(x), "Your weapon slot is: %d",  GetWeaponSlot(GetPlayerWeapon(playerid)));
     printf(x);
     return 1;
}
by: Nexius
PHP Code:
static const WSlot[] = {0011111111101010101010888, -1, -1, -122233344554667777812999111111};


CMD:test(playeridparams[]) return printf("Minigun slot: %d"WSlot[38]); 
Bren is offline   Reply With Quote
Old 05/10/2017, 05:58 PM   #4660
RoW001
Little Clucker
 
Join Date: Jul 2012
Posts: 42
Reputation: 4
Default Re: Useful Functions

About that RandomPlayer function you can use this.
Code:
GetRandomPlayer()
{
    new iPlayer = -1, iID = random(GetPlayerPoolSize());
    for(new i = 0; i < GetPlayerPoolSize(); i++){
        if(!IsPlayerConnected(i)) continue;
        if(iID == i){
            iPlayer = i;
        } else {
            CallLocalFunction("GetRandomPlayer", "");
        }
    }
    return iPlayer;
}
__________________
RoW001 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions? sciman001 Help Archive 7 19/03/2011 04:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 08:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 09:38 AM
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 11 03/05/2010 10:47 PM
What this functions do? harrold Help Archive 2 09/05/2009 12:30 AM


All times are GMT. The time now is 03:48 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.