/**
 *	WarmUp lib
 */
#Const	Version		"2017-02-03"
#Const	ScriptName	"WarmUp4.Script.txt"

#Include "Libs/Nadeo/WarmUp2Common.Script.txt" as WarmUp2
#Include "Libs/Nadeo/TrackMania/TM3.Script.txt" as TM

// ---------------------------------- //
// Functions
// ---------------------------------- //
// ---------------------------------- //
// Public
// ---------------------------------- //
// ---------------------------------- //
/** Return the version number of the script
 *
 *	@return		The version number of the script
 */
Text GetScriptVersion() {
	return Version;
}

// ---------------------------------- //
/** Return the name of the script
 *
 *	@return		The name of the script
 */
Text GetScriptName() {
	return ScriptName;
}

// ---------------------------------- //
/// Unload the library
Void Unload() {
	WarmUp2::Unload();
}

// ---------------------------------- //
/// Load the library
Void Load() {
	WarmUp2::Load();
}

// ---------------------------------- //
/** Enable the order selection for a group
 *
 *	@param	_GroupName		The name of the group to enable
 */
Void Enable(Text _GroupName) {
	WarmUp2::Enable(_GroupName);
}

// ---------------------------------- //
/** Disable the order selection for a group
 *
 *	@param	_GroupName		The name of the group to disable
 */
Void Disable(Text _GroupName) {
	WarmUp2::Disable(_GroupName);
}

// ---------------------------------- //
/// Rebuild the warm up UI
Void RebuildUI() {
	WarmUp2::RebuildUI();
}

// ---------------------------------- //
/** Display the clan selection buttons
 *
 *	@param	_Display	The new display status of the clan selection buttons
 */
Void DisplayClanSelection(Boolean _Display) {
	WarmUp2::DisplayClanSelection(_Display);
}

// ---------------------------------- //
/** Set the position of the warm up layer
 *
 *	@param	_Pos	The new position
 */
Void SetLayerPosition(Vec2 _Pos) {
	WarmUp2::SetLayerPosition(_Pos);
}

// ---------------------------------- //
/** Display an icon in one slot of a group
 *
 *	@param	_GroupName		The name of the group to set
 *	@param	_Slot			The number of the slot to set
 *	@param	_Icon			The path to the icon to display
 */
Void SetSlotIcon(Text _GroupName, Integer _Slot, Text _Icon) {
	WarmUp2::SetSlotIcon(_GroupName, _Slot, _Icon);
}

// ---------------------------------- //
/** Display an icon in all the slots of a group
 *
 *	@param	_GroupName		The name of the group to set
 *	@param	_Icon			The path to the icon to display
 */
Void SetAllSlotsIcons(Text _GroupName, Text _Icon) {
	WarmUp2::SetAllSlotsIcons(_GroupName, _Icon);
}

// ---------------------------------- //
/** Check if the order or ready state was updated
 *	since the last time we called thius function
 *
 *	@return		True if it was updated, false otherwise
 */
Boolean Updated() {
	return WarmUp2::Updated();
}

// ---------------------------------- //
/** Check if a player is ready
 *
 *	@param	_Player		The player to check
 *
 *	@return		True if the player is ready, false otherwise
 */
Boolean IsReady(CTmPlayer _Player) {
	return WarmUp2::IsReady(_Player);
}

// ---------------------------------- //
/** Check if a player is ready
 *
 *	@param	_PlayerId		The id of the player to check
 *
 *	@return		True if the player is ready, false otherwise
 */
Boolean IsReady(Ident _PlayerId) {
	return WarmUp2::IsReady(_PlayerId);
}

// ---------------------------------- //
/** Move a player in a group
 *
 *	@param	_Player		The player to remove
 *	@param	_GroupName	The group where to move the player
 */
Void SetPlayerGroup(CTmPlayer _Player, Text _GroupName) {
	WarmUp2::SetPlayerGroup(_Player, _GroupName);
}

// ---------------------------------- //
/** Remove a player from his current group
 *
 *	@param	The player to remove
 */
Void UnsetPlayerGroup(CTmPlayer _Player) {
	WarmUp2::UnsetPlayerGroup(_Player);
}

// ---------------------------------- //
/** Get in which group a player is
 *
 *	@param	_Player		The player to check
 *
 *	@return		The name of the group if the player has one, an empty Text otherwise
 */
Text GetPlayerGroup(CTmPlayer _Player) {
	return WarmUp2::GetPlayerGroup(_Player);
}

// ---------------------------------- //
/** Set a player in a slot of his group
 *	If the set is not forced the player can take the slot
 *	only if it's empty or if the player in the slot is not ready.
 *	If the player in the slot is ready the other player must already
 *	have a slot in the group to wamp with him.
 *
 *	@param	_Player		The player to set
 *	@param	_Slot		The slot for this player
 *	@param	_Forced		If False, the slot must meet certain conditions before the player can take it
 */
Void SetPlayerSlot(CTmPlayer _Player, Integer _Slot, Boolean _Forced) {
	WarmUp2::SetPlayerSlot(_Player, _Slot, _Forced);
}

// ---------------------------------- //
// Overload of the SetPlayerSlot function
Void SetPlayerSlot(CTmPlayer _Player, Integer _Slot) {
	WarmUp2::SetPlayerSlot(_Player, _Slot, False);
}

// ---------------------------------- //
/** Get the current slot of a player
 *
 *	@param	_Player		The player to check
 *
 *	@return		The slot of the player if he has one, -1 otherwise
 */
Integer GetPlayerSlot(CTmPlayer _Player) {
	return WarmUp2::GetPlayerSlot(_Player);
}

// ---------------------------------- //
/** Unset the slot of a player
 *
 *	@param	_Player		The player to unset
 */
Void UnsetPlayerSlot(CTmPlayer _Player) {
	WarmUp2::UnsetPlayerSlot(_Player);
}

// ---------------------------------- //
/** Get the content of a slot
 *
 *	@param	_GroupName		The group to check
 *	@param	_Slot			The slot to check
 *
 *	@return		The id of the player in the slot if there is one, NullId otherwise
 */
Ident GetSlot(Text _GroupName, Integer _Slot) {
	return WarmUp2::GetSlot(_GroupName, _Slot);
}

// ---------------------------------- //
/** Unset a player from a slot
 *
 *	@param	_GroupName	The name of the group where the slot must be unset
 *	@param	_Slot		The slot to unset
 */
Void UnsetSlot(Text _GroupName, Integer _Slot) {
	WarmUp2::UnsetSlot(_GroupName, _Slot);
}

// ---------------------------------- //
/** Check if a group exists
 *
 *	@param	_GroupName		The name of the group
 *
 *	@return		True if the gorup exists, False otherwise
 */
Boolean GroupExists(Text _GroupName) {
	return WarmUp2::GroupExists(_GroupName);
}

// ---------------------------------- //
/** Create a new warm up group
 *
 *	@param	_GroupName		The name of the group
 *	@param	_SlotsNb		The number of slots in the group
 */
Void CreateGroup(Text _GroupName, Integer _SlotsNb) {
	WarmUp2::CreateGroup(_GroupName, _SlotsNb);
}

// ---------------------------------- //
/** Destroy a warm up group
 *
 *	@param	_GroupName		The name of the group to destroy
 */
Void DestroyGroup(Text _GroupName) {
	WarmUp2::DestroyGroup(_GroupName);
}

// ---------------------------------- //
/** Manually set the players ids in a group
 *
 *	@param	_GroupName		The name of the group to set
 *	@param	_PlayersIds		The players ids to add
 */
Void SetGroup(Text _GroupName, Ident[Integer] _PlayersIds) {
	WarmUp2::SetGroup(_GroupName, _PlayersIds);
}

// ---------------------------------- //
/** Get the players ids of a group
 *
 *	@param	_GroupName		The name of the group to get
 *
 *	@return		The ordered players ids
 */
Ident[Integer] GetGroup(Text _GroupName) {
	return WarmUp2::GetGroup(_GroupName);
}

// ---------------------------------- //
/** Update the number of slots available in a group
 *
 *	@param	_GroupName		The name of the group to update
 *	@param	_SlotsNb		The new number  of slots
 */
Void SetSlotsNb(Text _GroupName, Integer _SlotsNb) {
	WarmUp2::SetSlotsNb(_GroupName, _SlotsNb);
}

// ---------------------------------- //
/** Get the number of slots in a group
 *
 *	@param	_GroupName		The name of the group to check
 *
 *	@return		The number of slot in the group if this group exist, 0 otherwise
 */
Integer GetSlotsNb(Text _GroupName) {
	return WarmUp2::GetSlotsNb(_GroupName);
}

// ---------------------------------- //
/** Get the number of ready players in a group
 *
 *	@param	_GroupName		The name of the group to check
 *
 *	@return		The number of ready players
 */
Integer GetReadyPlayersNb(Text _GroupName) {
	return WarmUp2::GetReadyPlayersNb(_GroupName);
}

// ---------------------------------- //
/** Get the number of players in a group
 *
 *	@param	_GroupName		The name of the group to check
 *
 *	@return		The number of players
 */
Integer GetPlayersNb(Text _GroupName) {
	return WarmUp2::GetPlayersNb(_GroupName);
}

// ---------------------------------- //
// Automatically fill the orders with players
Void Fill() {
	WarmUp2::Fill();
}

// ---------------------------------- //
/// Remove the players from the order when they stop playing
Void Clean() {
	WarmUp2::Clean();
}

// ---------------------------------- //
/** Set a group timers
 *	@param	_GroupName		The name of the group to update
 *	@param	_Timers			The new timers with this format :
 *							[Timer => [MinPlayerReady, MinPlayer]]
 *							If MinPlayerReady < 0 then the value will be replaced
 *							by the current number of players in the group
 *							(meaning that all the players of the group must be ready)
 */
Void SetGroupTimers(Text _GroupName, Integer[][Integer] _Timers) {
	WarmUp2::SetGroupTimers(_GroupName, _Timers);
}

// ---------------------------------- //
/// Attach the warm up layer
Void Attach() {
	WarmUp2::Attach();
}

// ---------------------------------- //
/// Detach the warm up layer
Void Detach() {
	WarmUp2::Detach();
}

// ---------------------------------- //
/// Initialize the warm up
Void Begin() {
	WarmUp2::Begin();
	
	CutOffTimeLimit = -1;
}

// ---------------------------------- //
/// Clean after the warm up
Void End() {	
	WarmUp2::End();
	
	CutOffTimeLimit = -1;
}

// ---------------------------------- //
/// Warm up loop
Void Loop() {
	CutOffTimeLimit = WarmUp2::Loop(CutOffTimeLimit);
}

// ---------------------------------- //
/// Default events management
Void ManageEvents() {
	foreach (Event in PendingEvents) {
		PassOn(Event);
		
		// ---------------------------------- //
		// Waypoint
		if (Event.Type == CTmModeEvent::EType::WayPoint) {
			if (Event.IsEndRace) {
				TM::EndRace(Event.Player);
			}
		}
		// ---------------------------------- //
		// GiveUp
		else if (Event.Type == CTmModeEvent::EType::GiveUp) {
			TM::WaitRace(Event.Player);
		}
	}
}

// ---------------------------------- //
/** Say if the warm up must stop or not
 *
 *	@return		True if the warm up must stop, false if it cans continue
 */
Boolean Stop() {
	return WarmUp2::Stop();
}