Raised This Month: $51 Target: $400
 12% 

Randomization player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 08-04-2021 , 04:32   Randomization player
Reply With Quote #1

Hello everyone, I apologize for the English, please tell me how can I make a random distribution of roles?

I need to call the function, implement the issuance of roles for the players.

There is a certain but, the role is assigned g_iRole [iClient] = Role_Name

There is a counter of how many roles need to be given to players, that is, based on the number of roles, it is necessary to randomly assign them to the players.

my counter roles:
Code:
void CalculateRoles()
{
	int iTotalPlayers = 0;
	
	// roleOne, roleTwo, roleThree, roleFour // global int
	
	for(int i = 1; i <= MaxClients; ++i)
	{
		if(IsClientInGame(i) && IsPlayerAlive(i))
		{
			iTotalPlayers++;
		}
	}

	roleOne = iTotalPlayers / 6;
	roleTwo = iTotalPlayers / 10;
	roleThree = (iTotalPlayers > 2) ? (iTotalPlayers / 5) :  (iTotalPlayers > 1 ? 1 : 0);
	roleFour = iTotalPlayers - roleOne - roleTwo - roleThree;
}
I have a timer at the end of which I need to assign roles. By calling a function such as InitRoles (), I'm not so much a pro yet, I can't figure out how to better implement it. I ask for help

Thank you very much for those who can help

Last edited by Hennesy; 08-04-2021 at 04:38.
Hennesy is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 08-04-2021 , 11:21   Re: Randomization player
Reply With Quote #2

I tried to do this, but I'm not sure if this is a normal solution. Someone does not give a role at all.

Code:
void InitRoles()
{
	LoopClients(i) g_iRole[i] = Role_None;
	
	for (int i = 1; i <= RolesCounter[Role_One]; i++) {	
		if(g_iRole[i] == Role_None) g_iRole[GetRandomClient()] = Role_One;
	}
	for (int i = 1; i <= RolesCounter[Role_Two]; i++) {	
		if(g_iRole[i] == Role_None) g_iRole[GetRandomClient()] = Role_Two;
	}
	for (int i = 1; i <= RolesCounter[Role_Three]; i++) {	
		if(g_iRole[i] == Role_None) g_iRole[GetRandomClient()] = Role_Three;
	}
	for (int i = 1; i <= RolesCounter[Role_Four]; i++) {	
		if(g_iRole[i] == Role_None) g_iRole[GetRandomClient()] = Role_Four;
	}
	
	LoopClients(iClient) {
		
		switch(g_iRole[iClient])
		{
			case Role_One:
			{
				//
			}
			case Role_Two:
			{
				//
			}
			case Role_Three:
			{
				//
			}
			case Role_Four:
			{
				//
			}
		}
	}
}

int GetRandomClient() 
{
    int[] clients = new int[MaxClients];
    int clientCount;

    LoopClients(i)
    {
        if (IsPlayerAlive(i))
        {
            clients[clientCount++] = i;
        }
    }

    return (clientCount == 0) ? -1 : clients[GetRandomInt(0, clientCount-1)];
}
Hennesy is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 08-06-2021 , 02:49   Re: Randomization player
Reply With Quote #3

I wanted to show one "random" way shuffle players, using adt_array with custom sort.

But I'm bit lost about "roles", how those need work... I let that part out for now.

You see in this Console output how it does.
Code:
map     : de_dust2 at: 0 x, 0 y, 0 z
tags    : bots,compspec,nostats,startmoney
players : 0 humans, 11 bots (32 max)
edicts  : 273 used of 2048 max
# userid name                uniqueid            connected ping loss state  adr
#     55 "Alfred"            BOT                                     active
#     56 "Doug"              BOT                                     active
#     57 "Pat"               BOT                                     active
#     58 "Jon"               BOT                                     active
#     59 "Grant"             BOT                                     active
#     60 "Nick"              BOT                                     active
#     61 "Martin"            BOT                                     active
#     62 "Norm"              BOT                                     active
#     63 "Dustin"            BOT                                     active
#     64 "Henry"             BOT                                     active
#     65 "Andy"              BOT                                     active

sm_test
Before Sort 1 (Alfred)
Before Sort 2 (Doug)
Before Sort 3 (Pat)
Before Sort 4 (Jon)
Before Sort 5 (Grant)
Before Sort 6 (Nick)
Before Sort 7 (Martin)
Before Sort 8 (Norm)
Before Sort 9 (Dustin)
Before Sort 10 (Henry)
Before Sort 11 (Andy)


After Sort 9 (Dustin)
After Sort 11 (Andy)
After Sort 4 (Jon)
After Sort 2 (Doug)
After Sort 10 (Henry)
After Sort 6 (Nick)
After Sort 7 (Martin)
After Sort 8 (Norm)
After Sort 5 (Grant)
After Sort 3 (Pat)
After Sort 1 (Alfred)



Code sample is here.
PHP Code:
....
    
    if( !
InitRoles() )
    {
        
PrintToServer("InitRoles() failed");
    }

}

// code sample start here

enum {
    
Role_None,
    
Role_One,
    
Role_Two,
    
Role_Three,
    
Role_Four,
    
Role_Max
};

enum {
    
Values_Player,
    
Values_Role,
    
Values_Max
};

bool InitRoles()
{
    
// Create adt_array
    
ArrayList RandomList = new ArrayList(Values_Max);

    
// array
    
int Values[Values_Max];

    
// collect players from server
    
for(int i 1<= MaxClientsi++)
    {
        if(!
IsClientInGame(i) || !IsPlayerAlive(i)) continue;
        
        
Values[Values_Player] = i;
        
Values[Values_Role] = Role_None;
        
RandomList.PushArray(Values);
    }



    
bool RandomSuccess;

    
// If array contain even one player, return callback as true
    
if(RandomList.Length 0)
        
RandomSuccess true;



    
// Print output of adt_array list before sort
    
for(int x 0RandomList.Lengthx++)
    {
        
RandomList.GetArray(xValuessizeof(Values));
        
PrintToServer("Before Sort %i (%N)"Values[Values_Player], Values[Values_Player]);
    }
    
PrintToServer("\n");


    
// Sort adt_array list
    
RandomList.SortCustom(SortRandomList);




    
// Print output of adt_array list after sort
    
for(int x 0RandomList.Lengthx++)
    {
        
RandomList.GetArray(xValuessizeof(Values));
        
PrintToServer("After Sort %i (%N)"Values[Values_Player], Values[Values_Player]);
    }



    
// Remember delete adt_array handle at the end
    
delete RandomList;

    return 
RandomSuccess;
}






public 
int SortRandomList(int index1int index2Handle array, Handle hndl)
{
    return 
GetRandomInt(-11);

__________________
Do not Private Message @me

Last edited by Bacardi; 08-06-2021 at 02:52.
Bacardi is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 08-06-2021 , 08:05   Re: Randomization player
Reply With Quote #4

Quote:
Originally Posted by Bacardi View Post
I wanted to show one "random" way shuffle players, using adt_array with
[/PHP]
It is necessary to issue the roles of the player, the roles have restrictions. Let's say role1 - 1 player, role2 - 2 player

and let's say for 30 players you need to distribute 2 roles to 3 players out of 30 players
Hennesy is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 08-06-2021 , 21:02   Re: Randomization player
Reply With Quote #5

Check out how it's done in TTT
Detective, Traitor, Innoncent
https://forums.alliedmods.net/showthread.php?p=2357981
__________________
8guawong is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 10:16.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode