ddnet/src/game/server/teams.cpp

123 lines
2.9 KiB
C++
Raw Normal View History

2010-08-28 13:47:52 +00:00
#include "teams.h"
CGameTeams::CGameTeams(CGameContext *pGameContext) : m_pGameContext(pGameContext) {
2010-08-28 13:47:52 +00:00
for(int i = 0; i < MAX_CLIENTS; ++i) {
m_TeamState[i] = EMPTY;
m_TeeFinished[i] = false;
}
}
void CGameTeams::OnCharacterStart(int id) {
int Tick = Server()->Tick();
if(m_Core.Team(id) == 0) {
CCharacter* Char = Character(id);
Char->m_RaceState = RACE_STARTED;
Char->m_StartTime = Tick;
Char->m_RefreshTime = Tick;
} else {
if(m_TeamState[m_Core.Team(id)] <= CLOSED) {
ChangeTeamState(m_Core.Team(id), STARTED);
for(int i = 0; i < MAX_CLIENTS; ++i) {
if(m_Core.SameTeam(i, id)) {
CCharacter* Char = Character(i);
2010-08-28 13:47:52 +00:00
Char->m_RaceState = RACE_STARTED;
Char->m_StartTime = Tick;
Char->m_RefreshTime = Tick;
}
2010-08-28 13:47:52 +00:00
}
}
}
}
void CGameTeams::OnCharacterFinish(int id) {
if(m_Core.Team(id) == 0) {
Character(id)->OnFinish();
2010-08-28 13:47:52 +00:00
} else {
m_TeeFinished[id] = true;
if(TeamFinished(m_Core.Team(id))) {
ChangeTeamState(m_Core.Team(id), FINISHED);//TODO: Make it better
for(int i = 0; i < MAX_CLIENTS; ++i) {
if(m_Core.SameTeam(i, id)) {
CCharacter * Char = Character(i);
if(Char != 0) {
Char->OnFinish();
m_TeeFinished[i] = false;
} //else {
// m_Core.Team(id) = 0; //i saw zomby =)
//}
}
}
2010-08-28 13:47:52 +00:00
}
}
}
bool CGameTeams::SetCharacterTeam(int id, int Team) {
2010-08-28 13:47:52 +00:00
//TODO: Send error message
if(id < 0 || id >= MAX_CLIENTS || Team < 0 || Team >= MAX_CLIENTS) {
return false;
}
if(m_TeamState[Team] >= CLOSED) {
return false;
}
if(Character(id)->m_RaceState != RACE_NONE) return false;
if(m_Core.Team(id) != 0 && m_TeamState[m_Core.Team(id)] != EMPTY) {
bool NoOneInOldTeam = true;
for(int i = 0; i < MAX_CLIENTS; ++i) {
if(i != id && m_Core.SameTeam(i, id)) {
NoOneInOldTeam = false;//all good exists someone in old team
break;
}
}
if(NoOneInOldTeam) {
m_TeamState[m_Core.Team(id)] = EMPTY;
}
2010-08-28 13:47:52 +00:00
}
m_Core.Team(id, Team);
2010-08-28 13:47:52 +00:00
if(m_TeamState[Team] == EMPTY) {
ChangeTeamState(Team, OPEN);
}
//GameServer()->CreatePlayerSpawn(Character(id)->m_Core.m_Pos, TeamMask());
if(Character(id)->GetPlayer()->m_IsUsingRaceClient)
{
CNetMsg_Sv_PlayerTeam Msg;
Msg.m_Team = Team;
Msg.m_Cid = id;
Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, -1);
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "Id = %d Team = %d", id, Team);
dbg_msg("Teams", aBuf);
}
2010-08-28 13:47:52 +00:00
return true;
}
void CGameTeams::ChangeTeamState(int Team, int State) {
2010-08-28 13:47:52 +00:00
m_TeamState[Team] = State;
}
bool CGameTeams::TeamFinished(int Team) {
2010-08-28 13:47:52 +00:00
for(int i = 0; i < MAX_CLIENTS; ++i) {
if(m_Core.Team(i) == Team && !m_TeeFinished[i]) {
2010-08-28 13:47:52 +00:00
return false;
}
}
return true;
}
int CGameTeams::TeamMask(int Team) {
int Mask = 0;
for(int i = 0; i < MAX_CLIENTS; ++i) {
2010-09-12 19:09:19 +00:00
if(m_Core.Team(i) == Team || (Character(i)
&& Character(i)->GetPlayer()
&& Character(i)->GetPlayer()->m_ShowOthers)) {
Mask |= 1 << i;
}
}
return Mask;
}