SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 27/06/2017, 03:25 PM   #4651
coool
Huge Clucker
 
Join Date: Feb 2013
Location: Pakistan, Sindh
Posts: 442
Reputation: 23
Default Re: Useful Functions

I just do this
PHP Code:
new randomplayer random(GetPlayerPoolSize);
while(!
IsPlayerConnected(randomplayer))
{
    
randomplayer random(GetPlayerPoolSize);

__________________
My Writing Works
Lost In Future
coool is offline   Reply With Quote
Old 27/06/2017, 04:28 PM   #4652
OneDay
Big Clucker
 
Join Date: Sep 2015
Posts: 198
Reputation: 64
Default Re: Useful Functions

PHP Code:
Iter_Random(Player); 
OneDay is offline   Reply With Quote
Old 27/06/2017, 11:12 PM   #4653
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,047
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, 09:46 AM   #4654
coool
Huge Clucker
 
Join Date: Feb 2013
Location: Pakistan, Sindh
Posts: 442
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, 04:43 PM   #4655
PeterLinkson
Little Clucker
 
PeterLinkson's Avatar
 
Join Date: Jul 2017
Location: Lost in past
Posts: 31
Reputation: 7
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, 05:25 PM   #4656
Meller
Huge Clucker
 
Meller's Avatar
 
Join Date: Dec 2016
Posts: 396
Reputation: 65
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.
__________________
Game Community Website: http://lexus-gaming.pro - Maintenance
Meller is online now   Reply With Quote
Old 12/07/2017, 05:43 PM   #4657
Paulice
Huge Clucker
 
Paulice's Avatar
 
Join Date: Jul 2017
Posts: 491
Reputation: 42
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 online now   Reply With Quote
Old 12/07/2017, 09:23 PM   #4658
Meller
Huge Clucker
 
Meller's Avatar
 
Join Date: Dec 2016
Posts: 396
Reputation: 65
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?
__________________
Game Community Website: http://lexus-gaming.pro - Maintenance
Meller is online now   Reply With Quote
Old 25/07/2017, 02:57 AM   #4659
PeterLinkson
Little Clucker
 
PeterLinkson's Avatar
 
Join Date: Jul 2017
Location: Lost in past
Posts: 31
Reputation: 7
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, 05:26 PM   #4660
Yousha
Big Clucker
 
Join Date: May 2013
Posts: 117
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
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 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 11 03/05/2010 09:47 PM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 07:37 PM.


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