24.01.2010, 21:34
Here is the final code.
NOTE:
This is a rather simplified version of a teams system made for a specific purpose. Generally team systems can be made very efficiently using a binary system which merges all team and status data into one variable. There are many premade team systems out there, this one was just to help this person with what they wanted, and learning pawn code without going through binary team systems.
NOTE:
This is a rather simplified version of a teams system made for a specific purpose. Generally team systems can be made very efficiently using a binary system which merges all team and status data into one variable. There are many premade team systems out there, this one was just to help this person with what they wanted, and learning pawn code without going through binary team systems.
pawn Code:
#include <a_samp>
#define teamA 0
#define teamB 1
pawn Code:
new playerTeam[MAX_PLAYERS];//0 teamA , 1 teamB
new sargeants[2]={-1,-1};
//You need to set the players team on spawn or something however you set teams
//use playerTeam[playerid] = //team that you want;
public OnPlayerSpawn(playerid)
{
//Assuming team has already been chosen
if(sargeants[playerTeam[playerid]]==-1)
{
//Only get here if there is no sargeant yet
sargeants[playerTeam[playerid]]=playerid;
}
}
public OnPlayerDisconnect(playerid, reason)
{
if(sargeants[playerTeam[playerid]]==playerid)
{
SelectNewSargent(playerid, "disconnected");
}
}
public OnPlayerDeath(playerid, killerid, reason)
{
if(sargeants[playerTeam[playerid]]==playerid)
{
SelectNewSargent(playerid, "died");
}
}
stock SelectNewSargent(previousSarg, reason[])
{
new playerid, bestPID=-1, bestPScore=-1, team = playerTeam[previousSarg];
for(playerid=0; playerid<GetMaxPlayers(); playerid++)
{
if(IsPlayerConnected(playerid) && playerid != previousSarg &&
playerTeam[playerid]==team && GetPlayerScore(playerid) > bestPScore)
{
bestPID = playerid;
bestPScore = GetPlayerScore(playerid);
}
}
if(bestPID!=-1)
{
new msg[128], prevName[24], newName[24];
GetPlayerName(previousSarg, prevName, sizeof(prevName));
GetPlayerName(bestPID, newName, sizeof(newName));
format(msg, sizeof(msg), "Sargent %s has %s, %s has become Sargent", prevName, reason, newName);
for(playerid=0; playerid<GetMaxPlayers(); playerid++)
{
if(IsPlayerConnected(playerid) && playerTeam[playerid]==team)
SendClientMessage(playerid, 0xDEEE20FF, msg);
}
sargeants[team]=bestPID;
}//else Sargent stays Sargent as there are no other members
}