good morning, evenings, nights
I'm using this add-on which assigns a number to the player by joining the team until that point everything works fine.
but I want to improve it because I've noticed that when a player disconnects and connects another player assigns him a number that another player already has
example:
if 5 players join the CT team, it assigns a unique number to each player without repeating
PLAYER 1: NUMBER - 4
PLAYER 2: NUMBER - 5
PLAYER 3: NUMBER - 2
PLAYER 4: NUMBER - 3
PLAYER 5: NUMBER - 1
if player 4 is disconnected with the number 3, when another player joins, a random number is assigned, what I want is for him to assign the number of player 4 that was disconnected
PHP Code:
/* Plugin generated by AMXX-Studio */
#include <amxmodx>
#include <amxmisc>
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"
new NUM_PLAYER[32]
const MaxRandom = 5;
new AllNumbers[ ( MaxRandom >> 5 ) + 1 ];
new bool:g_bJoined[33];
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event( "TeamInfo", "join_team", "a")
}
PRE_RANDOM_TT( iRanMin , iRanMax )
{
for ( new i = iRanMin ; i <= iRanMax ; i++ )
AllNumbers[ i >> 5 ] |= ( 1 << i );
}
public GET_RANDOM_TT( iRanMin , iRanMax )
{
static UsedNumbers[ ( MaxRandom >> 5 ) + 1 ];
new iRandom , bool:bAllUsedTT=true;
if ( iRanMax > MaxRandom )
set_fail_state( "Must increase MaxRandom value: %d > %d" , iRanMax , MaxRandom );
//Check if all numbers have already bee nused, if so this will reset the 'used' variable
for ( new i = 0 ; i < sizeof( UsedNumbers ) ; i++ )
{
if ( AllNumbers[ i ] && ( AllNumbers[ i ] != UsedNumbers[ i ] ) )
{
bAllUsedTT = false;
break;
}
}
//If all random numbers have already been used, reset variable so they can be re-used
if( bAllUsedTT )
arrayset( UsedNumbers , 0 , sizeof( UsedNumbers ) );
//Find random number
do
{
iRandom = random_num( iRanMin , iRanMax );
}
while ( ( UsedNumbers[ iRandom >> 5 ] & ( 1 << iRandom ) ) )
//Set current random number as used
UsedNumbers[ iRandom >> 5 ] |= ( 1 << iRandom );
//Return random number
return iRandom;
}
PRE_RANDOM_CT( iRanMin , iRanMax )
{
for ( new i = iRanMin ; i <= iRanMax ; i++ )
AllNumbers[ i >> 5 ] |= ( 1 << i );
}
public GET_RANDOM_CT( iRanMin , iRanMax )
{
static UsedNumbers[ ( MaxRandom >> 5 ) + 1 ];
new iRandom , bool:bAllUsedCT=true;
if ( iRanMax > MaxRandom )
set_fail_state( "Must increase MaxRandom value: %d > %d" , iRanMax , MaxRandom );
//Check if all numbers have already bee nused, if so this will reset the 'used' variable
for ( new i = 0 ; i < sizeof( UsedNumbers ) ; i++ )
{
if ( AllNumbers[ i ] && ( AllNumbers[ i ] != UsedNumbers[ i ] ) )
{
bAllUsedCT = false;
break;
}
}
//If all random numbers have already been used, reset variable so they can be re-used
if( bAllUsedCT )
arrayset( UsedNumbers , 0 , sizeof( UsedNumbers ) );
//Find random number
do
{
iRandom = random_num( iRanMin , iRanMax );
}
while ( ( UsedNumbers[ iRandom >> 5 ] & ( 1 << iRandom ) ) )
//Set current random number as used
UsedNumbers[ iRandom >> 5 ] |= ( 1 << iRandom );
//Return random number
return iRandom;
}
public client_authorized(id)
{
g_bJoined[id] = false;
}
public join_team()
{
new id = read_data(1);
if(!is_user_connected(id) || g_bJoined[id]) return PLUGIN_CONTINUE;
static user_team[32];
read_data(2, user_team, 31);
new szName[32];
get_user_name(id,szName,charsmax(szName));
switch(user_team[0])
{
case 'C':
{
g_bJoined[id] = true;
PRE_RANDOM_CT( 1 , 5 );
NUM_PLAYER[id] = GET_RANDOM_CT(1, 5);
server_print("CT: %d - player: %s", NUM_PLAYER[id],szName);
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]);
}
case 'T':
{
g_bJoined[id] = true;
PRE_RANDOM_TT( 1 , 5 );
NUM_PLAYER[id] = GET_RANDOM_TT(1, 5);
server_print("TT: %d - player: %s", NUM_PLAYER[id],szName);
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]);
}
}
return PLUGIN_CONTINUE;
}