/**
 *	Team mode
 */

#Extends "Modes/TrackMania/RoundsBase.Script.txt"

#Const  CompatibleMapTypes  "Race"
#Const	Version		"2014-12-31"
#Const	ScriptName	"Team.Script.txt"

#Include "TextLib" as TL

// ---------------------------------- //
// Settings
// ---------------------------------- //
#Setting S_PointsLimit 			5
#Setting S_MaxPointsPerRound	6		as _("Max points :")	///< The maxium number of points attirbuted to the first player to cross the finish line
#Setting S_PointsGap			1		as _("Points gap :")	///< The number of points lead a team must have to win the map
#Setting S_UsePlayerClublinks	False	as _("Use Clublinks :")	///< Use the players clublinks, or otherwise use the default teams
// @mm start
#Setting S_NbPlayersPerTeamMax	5		as _("Maximum number of players per team in matchmaking")
#Setting S_NbPlayersPerTeamMin	2		as _("Minimum number of players per team in matchmaking")
// @mm end

// ---------------------------------- //
// Constants
// ---------------------------------- //
#Const C_BlueBotsNb	0
#Const C_RedBotsNb	0
#Const Description _("""$fffIn $f00Team$fff mode, you have to choose a team : $f00Red$fff or $55fBlue$fff.
(you can select a team in the in-game menu, 
accessible via the 'Escape' key)

The team mode mode consists of $f00a series of races$fff.
The goal for your team is to win a maximum number of $f00points$fff.

When you finish a race with a good $f00position$fff, you give $f00points$fff to your team.
The $f00winning team$fff is the first team whose total reaches the $f00point limit$fff (5 for example).""")

// ---------------------------------- //
// Globales
// ---------------------------------- //

// ---------------------------------- //
// Extend
// ---------------------------------- //
***LogVersion***
***
MB_LogVersion(ScriptName, Version);
***

// @mm start
***LobbyStartServer***
***
MM_SetFormat([S_NbPlayersPerTeamMax, S_NbPlayersPerTeamMax]);
if (S_NbPlayersPerTeamMax > S_NbPlayersPerTeamMin) {
	declare Formats = Integer[][];
	for (I, S_NbPlayersPerTeamMin, S_NbPlayersPerTeamMax-1) {
		if (I > 0) Formats.add([I, I]);
	}
	MM_SetProgressiveFormats(Formats);
}
***
// @mm end

// @mm start
***ManialinkRules***
***
ManialinkRules = """<label posn="-62.5 25" sizen="125 50" autonewline="1" maxline="10" textemboss="1" textsize="2" text="{{{Description}}}" />""";
***
// @mm end

// @mm start
***InitMatchmaking***
***
// ---------------------------------- //
// Matchmaking mode
if (MM_IsMatchServer()) {
	MM_Init([S_NbPlayersPerTeamMax, S_NbPlayersPerTeamMax]);
}
***
// @mm end

***StartServer***
***
// ---------------------------------- //
// Initialize mode
UseClans = True;
MB_UsePlayerClublinks = S_UsePlayerClublinks;

// ---------------------------------- //
// Create scores table
ST2::SetStyle("LibST_TMBaseTeams");
ST2::SetModeIcon("Icons128x32_1|RT_Team");
MB_SetScoresTableStyleFromXml(S_ScoresTableStylePath);
***

***InitMap***
***
// ---------------------------------- //
// Initialize scores
Scores_Clear();
ST2::ClearScores();

// Skip intro in matchmaking
// @mm start
if (MM_IsMatchServer()) MB_UseIntro = False;
else MB_UseIntro = True;
// @mm end

// ---------------------------------- //
// Set scores table for race
ST2::DestroyCol("LibST_TMBestTime");
ST2::CreateCol("LibST_TMPrevRaceDeltaPoints", "", "", 3., 60.);
ST2::SetColTextSize("LibST_TMPrevRaceDeltaPoints", 1.5);
ST2::CreateCol("LibST_TMPrevTime", "", "--:--.---", 8., 70.);
ST2::SetColTextAlign("LibST_TMPrevTime", CMlControl::AlignHorizontal::Right);
ST2::Build("TM");
***

***StartMap***
***
// ---------------------------------- //
// Initialize map
Users_SetNbFakeUsers(C_BlueBotsNb, C_RedBotsNb);
// @mm start
/*
MB_WarmUp(-1);
SetFooterText();

Clublink::DefineTeamAuto();
*/
// @mm end

// @mm start
if (MM_IsMatchServer()) {
	// ---------------------------------- //
	// Set matchmaking scores
	MM_SetScores([ClanScores[1], ClanScores[2]]);
	
	// ---------------------------------- //
	// Wait players when using matchmaking
	if (!IsRematch) {
		MM_MatchWait();
		MM_VoteForNextMap(True);
	} else {
		MM_WaitPlayers(15000);
	}
} else {
	// ---------------------------------- //
	// Standard warm up
	MB_WarmUp(-1);
}

SetFooterText();

Clublink::DefineTeamAuto();

IsRematch = False;

// ---------------------------------- //
// Matchmaking : allow substitutes
if (MM_IsMatchServer()) MM_AllowSubstitutes(True);
// @mm end
***

***StartRound***
***
ST2::ClearScores();
CutOffTimeLimit = -1;
Clublink::DefineTeamAuto();
***

***PlayLoop***
***
// ---------------------------------- //
// Manage events
foreach (Event in PendingEvents) {
	PassOn(Event);
	XmlRpc::PassOn(Event);
	
	// ---------------------------------- //
	// Waypoint
	if (Event.Type == CTmModeEvent::EType::WayPoint) {
		if (Event.IsEndRace) {
			if (Event.Player.Score != Null) {
				if (Event.Player.Score.BestRace.Compare(Event.Player.CurRace, CTmResult::ETmRaceResultCriteria::Time) <= 0) {
					Event.Player.Score.BestRace = Event.Player.CurRace;
				}
			}
			Event.Player.Score.PrevRace = Event.Player.CurRace;
			ComputeLatestRaceScores();
			Scores_Sort(CTmMode::ETmScoreSortOrder::PrevRace_Time);
			TM2::EndRace(Event.Player);
			
			// ---------------------------------- //
			// Start the countdown if it's the first player to finish
			if (CutOffTimeLimit <= 0) {
				CutOffTimeLimit = GetFinishTimeout();
			}
		}
		if (Event.IsEndLap) {
			if (Event.Player.Score != Null) {
				if (Event.Player.Score.BestLap.Compare(Event.Player.CurLap, CTmResult::ETmRaceResultCriteria::Time) <= 0) {
					Event.Player.Score.BestLap = Event.Player.CurLap;
				}
			}
		}
	}
	// ---------------------------------- //
	// GiveUp
	else if (Event.Type == CTmModeEvent::EType::GiveUp) {
		TM2::WaitRace(Event.Player);
	}
}
***

***EndRound***
***
TM2::WaitRaceAll();
CutOffTimeLimit = -1;

if (ForceEndRound) {
	ForcedEndRoundSequence();
} else {
	// ---------------------------------- //
	// Get the last round points
	ComputeLatestRaceScores();
	Scores_Sort(CTmMode::ETmScoreSortOrder::PrevRace_Time);
	UIManager.UIAll.ScoreTableVisibility = CUIConfig::EVisibility::ForcedVisible;
	UIManager.UIAll.UISequence = CUIConfig::EUISequence::EndRound;
	MB_Sleep(3000);
	// ---------------------------------- //
	// Add them to the total scores
	ComputeScores();
	Scores_Sort(CTmMode::ETmScoreSortOrder::PrevRace_Time);
	MB_Sleep(3000);
	UIManager.UIAll.ScoreTableVisibility = CUIConfig::EVisibility::Normal;
	UIManager.UIAll.UISequence = CUIConfig::EUISequence::Playing;
	
	SetFooterText();
	if (MapIsOver()) {
		MB_StopMap = True;
		MB_StopMatch = True;
	}
}

// @mm start
// ---------------------------------- //
// Set matchmaking scores
if (MM_IsMatchServer()) {
	MM_SetScores([ClanScores[1], ClanScores[2]]);
}
// @mm end
***

***EndMap***
***
// ---------------------------------- //
// Ranking
declare WinningTeam = -1;
if (Clan1Score > Clan2Score) {
	WinningTeam = 1;
	MB_VictoryMessage = TL::Compose(_("$<%1$> wins the map!"), Teams[0].ColorizedName);
} else if (Clan2Score > Clan1Score) {
	WinningTeam = 2;
	MB_VictoryMessage = TL::Compose(_("$<%1$> wins the map!"), Teams[1].ColorizedName);
} else {
	MB_VictoryMessage = _("This map is a draw.");
}
foreach (Score in Scores) {
	// @mm start
	if (Score.Points > MasterPoints) {
		MasterLogin = Score.User.Login;
		MasterPoints = Score.Points;
	}
	// @mm end
	Score.Points = 0;
	if (WinningTeam == 1 || WinningTeam == 2) {
		if (Score.LadderClan == WinningTeam) {
			Score.Points = 2;
		} else if (Score.LadderClan == 3 - WinningTeam) {
			Score.Points = 1;
		} else {
			Score.Points = 0;
		}
	} else {
		if (Score.LadderClan == 1 || Score.LadderClan == 2) {
			Score.Points = 1;
		} else {
			Score.Points = 0;
		}
	}
	Score.PrevRaceDeltaPoints = 0;
}

// ---------------------------------- //
// Close ladder
// @mm start
//Ladder_ComputeRank(CTmMode::ETmScoreSortOrder::TotalPoints);
//MB_Ladder_CloseMatch();
if (MM_IsMatchServer() && (TM2::GetClanNbPlayers(1) == 0 || TM2::GetClanNbPlayers(2) == 0 || MM_RestartMatchmaking) && MB_SectionRoundNb <= 1) {
	MM_SetLadderMatchId();
	MB_Ladder_CancelMatch();
	MB_Log("Cancel match and don't give LP because one of the team left the match.");
} else {
	if (MM_IsMatchServer()) MM_SetLadderMatchId();
	Ladder_ComputeRank(CTmMode::ETmScoreSortOrder::TotalPoints);
	MB_Ladder_CloseMatch();
}
// @mm end

// ---------------------------------- //
// Set scores table for podium
ST2::DestroyCol("LibST_TMPrevRaceDeltaPoints");
ST2::DestroyCol("LibST_TMPrevTime");
ST2::CreateCol("LibST_TMBestTime", "", "--:--.---", 10., 70.);
ST2::SetColTextAlign("LibST_TMBestTime", CMlControl::AlignHorizontal::Right);
ST2::Build("TM");
SetFooterText();
Scores_Sort(CTmMode::ETmScoreSortOrder::BestRace_Time);
***

// ---------------------------------- //
// Functions
// ---------------------------------- //
// ---------------------------------- //
/** Get the time left to the players to finish the round after the first player
 *
 *	@return 		The time left in ms
 */
Integer GetFinishTimeout() {
	declare FinishTimeout = 0;
	
	if (S_FinishTimeout >= 0) {
		FinishTimeout = S_FinishTimeout * 1000;
	} else {
		FinishTimeout = 5000;
		if (Map.TMObjective_IsLapRace && NbLaps > 0 && Map.TMObjective_NbLaps > 0) {
			FinishTimeout += ((Map.TMObjective_AuthorTime / Map.TMObjective_NbLaps) * NbLaps) / 6;
		} else {
			FinishTimeout += Map.TMObjective_AuthorTime / 6;
		}
	}
	
	return Now + FinishTimeout;
}

// ---------------------------------- //
/// Update the text in the scores table footer
Void SetFooterText() {
	if (S_PointsGap > 1) {
		ST2::SetFooterText(TL::Compose("%1 "^S_PointsLimit^"  |  %2 "^S_PointsGap, _("Points limit : "), _("Points gap :")));
	} else {
		ST2::SetFooterText(TL::Compose("%1 "^S_PointsLimit, _("Points limit : ")));
	}
}

// ---------------------------------- //
/** Announce the round winner in the chat
 *
 *	@param	_TeamNum	The number of the team who won the round
 */
Void AnnounceWinner(Integer _TeamNum) {
	if (!Teams.existskey(_TeamNum)) {
		UIManager.UIAll.SendChat(_("This round is a draw."));
	} else {
		UIManager.UIAll.SendChat(TL::Compose(_("$<%1$> wins the round!"), Teams[_TeamNum].ColorizedName));
	}
}

// ---------------------------------- //
/// Compute the latest race scores
Void ComputeLatestRaceScores() {
	if (Scores.count <= 0) return;
	Scores_Sort(CTmMode::ETmScoreSortOrder::PrevRace_Time);
	
	// Each player finishing the race scores (MaxPoints - (Rank - 1))
	if (S_UseAlternateRules) {
		declare Points = Scores.count;
		foreach (Score in Scores) {
			if (Score.User.RequestsSpectate && Score.PrevRace.Time <= 0) {
				Points -= 1;
			}
		}
		if (Points > S_MaxPointsPerRound) Points = S_MaxPointsPerRound;
		
		declare TeamsScores = [0, 0];
		declare WinningTeam = -1;
		foreach (Score in Scores) {
			if (Score.TeamNum != 1 && Score.TeamNum != 2) continue;
			
			if (Score.PrevRace.Time > 0) {
				TeamsScores[Score.TeamNum - 1] += Points;
				Score.PrevRaceDeltaPoints = Points;
				if (Points > 0) Points -= 1;
			} else {
				Score.PrevRaceDeltaPoints = 0;
			}
		}
		
		if (TeamsScores[0] != TeamsScores[1]) {
			if (TeamsScores[0] > TeamsScores[1]) {
				WinningTeam = 0;
			} else {
				WinningTeam = 1;
			}
		}
		
		foreach (TeamIndex => Team in Teams) {
			declare PrevRaceDeltaPoints for Team = 0;
			if (TeamIndex == WinningTeam) {
				PrevRaceDeltaPoints = 1;
			} else {
				PrevRaceDeltaPoints = 0;
			}
		}
	} 
	// Give one points to all the players of a team from first place to the first player of the opposing team
	// eg: R, R, B, R, B, B -> 1, 1, 0, 0, 0, 0 -> 2 points for Red
	else {
		declare WinningTeam = -1;
		declare Points = 0;
		declare AheadOfOpponents = True;
		
		foreach (Score in Scores) {
			if (Score.TeamNum != 1 && Score.TeamNum != 2) continue;
			if (Score.PrevRace.Time <= 0) break;
			if (WinningTeam == -1) WinningTeam = Score.TeamNum - 1;
			
			if (Score.TeamNum - 1 == WinningTeam) {
				if (AheadOfOpponents) {
					Points += 1;
					Score.PrevRaceDeltaPoints = 1;
				}
			} else {
				AheadOfOpponents = False;
			}
		}
		
		if (Points > S_MaxPointsPerRound) Points = S_MaxPointsPerRound;
		
		foreach (TeamIndex => Team in Teams) {
			declare PrevRaceDeltaPoints for Team = 0;
			if (TeamIndex == WinningTeam) {
				PrevRaceDeltaPoints = Points;
			} else {
				PrevRaceDeltaPoints = 0;
			}
		}
	}
}

// ---------------------------------- //
/// Compute the map scores
Void ComputeScores() {
	declare PrevRaceDeltaPoints as ScoreTeam0 for Teams[0] = 0;
	declare PrevRaceDeltaPoints as ScoreTeam1 for Teams[1] = 0;
	
	declare WinningTeam = -1;
	if (ScoreTeam0 > 0 && ScoreTeam0 > ScoreTeam1) {
		WinningTeam = 0;
	} else if (ScoreTeam1 > 0 && ScoreTeam1 > ScoreTeam0) {
		WinningTeam = 1;
	}
	
	AnnounceWinner(WinningTeam);
	
	foreach (TeamIndex => Team in Teams) {
		declare PrevRaceDeltaPoints for Team = 0;	
		if (ClanScores.existskey(TeamIndex + 1)) {
			ClanScores[TeamIndex + 1] += PrevRaceDeltaPoints;
		}
	}
	
	foreach (Score in Scores) {
		Score.Points += Score.PrevRaceDeltaPoints;
		Score.PrevRaceDeltaPoints = 0;
		if (Score.User.RequestsSpectate) {
			Score.LadderClan = -1;
		} else {
			Score.LadderClan = Score.TeamNum;
		}
	}
}

// ---------------------------------- //
/** Check if we should go to the next map
 *
 *	@return		True if it is the case, false otherwise
 */
Boolean MapIsOver() {
	declare PointsGap = S_PointsGap;
	if (PointsGap < 0) PointsGap = 0;
	
	if (Clan1Score >= S_PointsLimit && Clan1Score - Clan2Score >= PointsGap) {
		return True;
	} else if (Clan2Score >= S_PointsLimit && Clan2Score - Clan1Score >= PointsGap) {
		return True;
	}
	
	return False;
}