i would prefer using foreach include. it just help shrink the code and work even better!
Lets start with defines, variables and iterators.
Код:
#include a_samp
#include foreach
#include izcmd
new Iterator:TeamRedPlayer<MAX_PLAYERS>;
new Iterator:TeamBluePlayer<MAX_PLAYERS>;
new bool:IsInTDM[MAX_PLAYERS];
#define Team_Red 1
#define Team_Blue 2
Well now lets add a command so that we enter a team that can auto balance itself
Код:
CMD:tdm(playerid)
{
if(Iter_Count(TeamRedPlayer) > Iter_Count(TeamBluePlayer)) // easy
SetPlayerTeamEx(playerid, Team_Blue);
else
SetPlayerTeamEx(playerid, Team_Red);
return 1;
}
while working with foreach we have made an extended stock for setting a player's team
Код:
stock SetPlayerTeamEx(playerid, teamid)
{
switch(teamid)
{
case Team_Red:
{
SetPlayerTeam(playerid, Team_Red);///// Setting team id
SetPlayerColor(playerid, 0xFF0000FF);///// Color Red
Iter_Add(TeamRedPlayer, playerid);///// Adding in iterator
}
case Team_Blue:
{
SetPlayerTeam(playerid, Team_Blue); ///// Setting team id
SetPlayerColor(playerid, 0x0000FFFF); ///// Color blue
Iter_Add(TeamBluePlayer, playerid); ///// Adding in iterator
}
}
IsInTDM[playerid] = true; // Confirm that player is in TDM minigame now
return 1;
}
Well what if a player disconnects? we must exclude him to avoid false counting obviously
Код:
public OnPlayerDisconnect(playerid, reason)
{
if(IsInTDM[playerid] == true) // checking if true
ExcludePlayerFromTeam(playerid); //then exclude player
return 1;
}
stock ExcludePlayerFromTeam(playerid)
{
switch(GetPlayerTeam(playerid))
{
case Team_Red:
{
Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
}
case Team_Blue:
{
Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
}
default:
return 0; // in case player was not in the specified team
}
IsInTDM[playerid] = false; // ensure that player is no more in this TDM minigame
return 1;
}