SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 15/08/2019, 02:41 PM   #1
DomagojSellug
Big Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 178
Reputation: 9
Default How to make a server pick a certain percentage of players?

For example, I want the script to pick 40% of random players to be terrorists, and the other 60% to be the police. How can I make that happen so it doesn't glitch out?

I thought of making a loop check how many players are online, and then dividing that number of players by 40% and assign them to one team... but I don't know if it would work out, and how could I make the other 60% be assigned to the other team.
__________________
My work:


Weapon Shop FS
Click on me!
DomagojSellug is offline   Reply With Quote
Old 15/08/2019, 03:12 PM   #2
DomagojSellug
Big Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 178
Reputation: 9
Default Re: How to make a server pick a certain percentage of players?

pawn Code:
new PlayersInServer;
        for(new i=0;i<=MAX_PLAYERS;i++)
        {
            if(IsPlayerConnected(i)) PlayersInServer++;
        }
        new rand;
       
        for(new picker=0;picker<=PlayersInServer;picker++)
        {
            rand = random(100);
            switch(rand)
            {
                case 0..20:
                {
                    if(PlayerTeam[picker] == TEAM_NONE && PlayerTeam[picker] != TEAM_COP)
                    {
                        PlayerTeam[picker] = TEAM_ROB;
                        SendClientMessage(picker, COLOR_WARN, "[!]{ffffff} You have been picked as a {FF0000}CRIMINAL{ffffff} this round.");
                    }
                }
                case 21..100:
                {
                    if(PlayerTeam[picker] == TEAM_NONE && PlayerTeam[picker] != TEAM_ROB)
                    {
                        PlayerTeam[picker] = TEAM_COP;
                        SendClientMessage(picker, COLOR_WARN, "[!]{ffffff} You have been picked as a {0000FF}POLICE OFFICER{ffffff} this round.");
                    }
                }
            }
        }

Would this work? I am not sure myself, and I don't have anyone to test it with at this moment since my friend is busy.
__________________
My work:


Weapon Shop FS
Click on me!
DomagojSellug is offline   Reply With Quote
Old 15/08/2019, 04:43 PM   #3
Robson04
Big Clucker
 
Robson04's Avatar
 
Join Date: Aug 2018
Posts: 54
Reputation: 0
Default Re: How to make a server pick a certain percentage of players?

Create iteretor to reorder players. You can restore players to any iter. Create index variable and add num++; and check if index > (count) add more players to 2nd iter.
Robson04 is offline   Reply With Quote
Old 16/08/2019, 03:12 PM   #4
GTLS
High-roller
 
GTLS's Avatar
 
Join Date: Aug 2014
Location: India
Posts: 1,019
Reputation: 115
Default Re: How to make a server pick a certain percentage of players?

I havent tested this but this will surely work.

PHP Code:
#define RandomEx(%1,%2) (random(%2-%1)+%1) //on top


//Main Logic

for(new i=0j=0<= GetPlayerPoolSize(); i++)
{
    if(!
IsPlayerConnected(i)) continue;
    
    
RandomEx(0,5); //Will return random values from 0 to 4. 
         
    
if(!= 0//Values 0,1,3 will be true here. Means 60%(3 out of 5) chance that this will execute. 
    
{
         
//Your code for TEAM_COP
    

    else 
//Rest 40% automatically 
    
{
        
//Your code for TEAM_TERRORIST
    
}    
                

Try
__________________

I don't help for rep. I help cuz I was helped in the past.


Retired from SAMP Scene. Might open SAMP Forums once in a while.

Some treats for ya. Click em
Basic House System
Simple Speedo
Base Login/Register Script

GTLS is offline   Reply With Quote
Old 16/08/2019, 05:20 PM   #5
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 178
Reputation: 17
Default Re: How to make a server pick a certain percentage of players?

pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0; i < connected_players; i ++)
{
    switch(random(10))
    {
        case 0, 2, 4, 6, 8:
        {
            if((++ l) <= j)
            {
                // in the 40%
            }
            else
            {
                // in the 60%
            }
        }
        default:
        {
            if((++ m) <= k)
            {
                // in the 60%
            }
            else
            {
                // in the 40%
            }
            break;
        }
    }
}

Good luck & have fun!
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!

Last edited by CheezIt; 16/08/2019 at 10:15 PM.
CheezIt is offline   Reply With Quote
Old 16/08/2019, 09:51 PM   #6
DomagojSellug
Big Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 178
Reputation: 9
Default Re: How to make a server pick a certain percentage of players?

Quote:
Originally Posted by GTLS View Post
I havent tested this but this will surely work.

PHP Code:
#define RandomEx(%1,%2) (random(%2-%1)+%1) //on top


//Main Logic

for(new i=0j=0<= GetPlayerPoolSize(); i++)
{
    if(!
IsPlayerConnected(i)) continue;
    
    
RandomEx(0,5); //Will return random values from 0 to 4. 
         
    
if(!= 0//Values 0,1,3 will be true here. Means 60%(3 out of 5) chance that this will execute. 
    
{
         
//Your code for TEAM_COP
    

    else 
//Rest 40% automatically 
    
{
        
//Your code for TEAM_TERRORIST
    
}    
                

Try
This code seems easier for me to understand...

Quote:
Originally Posted by CheezIt View Post
pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0; i < connected_players; i ++)
{
    switch(random(10))
    {
        case 0, 2, 4, 6, 8:
        {
            if(j <= (++ l))
            {
                // in the 40%
            }
            else
            {
                // in the 60%
            }
        }
        default:
        {
            if(k <= (++ m))
            {
                // in the 60%
            }
            else
            {
                // in the 40%
            }
            break;
        }
    }
}

Good luck & have fun!
... but I will try this one too and let you know how it goes once I get someone to test it with.
__________________
My work:


Weapon Shop FS
Click on me!
DomagojSellug is offline   Reply With Quote
Old 16/08/2019, 10:04 PM   #7
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 178
Reputation: 17
Default Re: How to make a server pick a certain percentage of players?

Quote:
Originally Posted by DomagojSellug View Post
This code seems easier for me to understand...


... but I will try this one too and let you know how it goes once I get someone to test it with.
His code doesn't "pick 40% of random players to be terrorists, and the other 60% to be the police", instead it gives users a 40% chance to become a terrorist and 60% chance to become a police officer.

This might be easier to understand/use, also you don't have to have repeated code:
pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0, bool:n = false; i < connected_players; i ++)
{
    switch(random(10))
    {
        case 0, 2, 4, 6, 8:
        {
            n = (((++ l) <= j) ? (true) : (false));
        }
        default:
        {
            n = ((++ m) <= k) ? (false) : (true));
        }
    }

    if(n)
    {
        // in the 40%, put your code here
    }
    else
    {
        // in the 60%, put your code here
    }
}

If you want to go even further, then here you go:
pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0; i < connected_players; i ++)
{
    if(((random(10) % 2) ? ((((++ l) <= j) ? (true) : (false))) : ((((++ m) <= k) ? (false) : (true)))))
    {
        // in the 40%, put your code here
    }
    else
    {
        // in the 60%, put your code here
    }
}

What's not to understand?
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!
CheezIt is offline   Reply With Quote
Old 16/08/2019, 10:50 PM   #8
DomagojSellug
Big Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 178
Reputation: 9
Default Re: How to make a server pick a certain percentage of players?

Quote:
Originally Posted by CheezIt View Post
His code doesn't "pick 40% of random players to be terrorists, and the other 60% to be the police", instead it gives users a 40% chance to become a terrorist and 60% chance to become a police officer.

This might be easier to understand/use, also you don't have to have repeated code:
pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0, bool:n = false; i < connected_players; i ++)
{
    switch(random(10))
    {
        case 0, 2, 4, 6, 8:
        {
            n = (((++ l) <= j) ? (true) : (false));
        }
        default:
        {
            n = ((++ m) <= k) ? (false) : (true));
        }
    }

    if(n)
    {
        // in the 40%, put your code here
    }
    else
    {
        // in the 60%, put your code here
    }
}

If you want to go even further, then here you go:
pawn Code:
new connected_players = 0;

for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
{
    if(IsPlayerConnected(i))
    {
        connected_players += 1;
    }
}

for(new i = 0, j = floatround(connected_players * 0.4), k = connected_players - j, l = 0, m = 0; i < connected_players; i ++)
{
    if(((random(10) % 2) ? ((((++ l) <= j) ? (true) : (false))) : ((((++ m) <= k) ? (false) : (true)))))
    {
        // in the 40%, put your code here
    }
    else
    {
        // in the 60%, put your code here
    }
}

What's not to understand?
Yeah, I was looking at that code and it seemed weird, so I will go with this one instead. Once again, when I get at least 3-4 people to test it with, I will make sure to tell you how it goes. I appreciate that you even wrote a 2nd version which is much shorter. I will give REP+

P.S. I ain't much of a maths guy, so can you please just tell me how can I make it so it chooses 30%-70% ?
__________________
My work:


Weapon Shop FS
Click on me!
DomagojSellug is offline   Reply With Quote
Old 17/08/2019, 02:20 AM   #9
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 178
Reputation: 17
Default Re: How to make a server pick a certain percentage of players?

Quote:
Originally Posted by DomagojSellug View Post
Yeah, I was looking at that code and it seemed weird, so I will go with this one instead. Once again, when I get at least 3-4 people to test it with, I will make sure to tell you how it goes. I appreciate that you even wrote a 2nd version which is much shorter. I will give REP+

P.S. I ain't much of a maths guy, so can you please just tell me how can I make it so it chooses 30%-70% ?
Thanks! You should go with the last version, simplified & easy to modify. For 30% just change the 0.4 at "j = floatround(connected_players * 0.4)" to 0.3.
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!
CheezIt is offline   Reply With Quote
Old 18/08/2019, 10:04 AM   #10
DomagojSellug
Big Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 178
Reputation: 9
Default Re: How to make a server pick a certain percentage of players?

Quote:
Originally Posted by CheezIt View Post
Thanks! You should go with the last version, simplified & easy to modify. For 30% just change the 0.4 at "j = floatround(connected_players * 0.4)" to 0.3.
I tested the script with my friends and it seems to work. Thank you so much for the help!!!
__________________
My work:


Weapon Shop FS
Click on me!
DomagojSellug 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
how to pick 3 best players severance Scripting Help 4 15/08/2018 10:28 AM
Question - How to make random based on percentage buburuzu19 Scripting Help 11 18/11/2014 06:21 PM
How to do make it so there is a certain percentage for something to succeed? 101 Scripting Help 2 30/06/2013 07:15 PM
[Tutorial] How to make use of percentage T0pAz Tutorials 10 28/02/2013 04:03 AM
How do I make a pick-up that gives you a car? Andr3y9603 Help Archive 3 08/08/2011 08:07 PM


All times are GMT. The time now is 12:39 PM.


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