/** 
 *	MapType library
 */

#Const Version		"2016-01-13"
#Const ScriptName	"MapType.Script.txt"

// ---------------------------------- //
// 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;
}

// ---------------------------------- //
/** Start map test
 *
 *	@param	_NbPlayers		Number of players testing
 */
Void EnterPlayground(Integer _NbPlayers) {
	assert(_NbPlayers == 1);
	if (!IsSwitchedToPlayground) {
		RequestEnterPlayground();
	}
	wait(IsSwitchedToPlayground);
	wait(Mode != Null && Mode.AllPlayers.count >= 1);
	assert(Mode.AllPlayers.count == _NbPlayers);
}

// ---------------------------------- //
/// Stop map test
Void LeavePlayground() {
	if (IsSwitchedToPlayground) {
		RequestLeavePlayground();
	}
	wait(!IsSwitchedToPlayground);
}

// ---------------------------------- //
/** Save the map type script version in the metadata of the map
 *
 *	@param	_Version	The version number of the maptype
 */
Void SetVersion(Integer _Version) {
	declare metadata LibMapType_MapTypeVersion for Map = 0;
	LibMapType_MapTypeVersion = _Version;
}

// ---------------------------------- //
/** Get the map type script version in the metadata of the map
 *
 *	@return	The version number of the maptype
 */
Integer GetVersion() {
	declare metadata LibMapType_MapTypeVersion for Map = 0;
	return LibMapType_MapTypeVersion;
}

// ---------------------------------- //
/** Set the time objectives of a map based on the author time
 *
 *	@param	_AuthorTime		The time of the author of the map
 */
Void SetObjectivesFromAuthorTime(Integer _AuthorTime) {
	Map.TMObjective_AuthorTime = _AuthorTime;
	
	// The gold, silver and bronze times are automatically overwritten by new values 
	// based on the author time when the author time changes
	// So its not needed to do it manually
	/*declare BronzeTime = _AuthorTime + ML::NearestInteger(_AuthorTime * 0.5);
	Map.TMObjective_BronzeTime = BronzeTime - (BronzeTime % 1000);
	
	declare SilverTime = _AuthorTime + ML::NearestInteger(_AuthorTime * 0.2);
	Map.TMObjective_SilverTime = SilverTime - (SilverTime % 1000);
	
	declare GoldTime = _AuthorTime + ML::NearestInteger(_AuthorTime * 0.05);
	Map.TMObjective_GoldTime = GoldTime - (GoldTime % 1000);*/
}

// ---------------------------------- //
/** Set the time objectives of a map based on the author score
 *
 *	@param	_AuthorTime		The score of the author of the map
 */
Void SetObjectivesFromAuthorScore(CTmScore _Score) {
	TMObjective_SetFromBestRace(_Score);
}

// ---------------------------------- //
// Reset the objectives of the map
Void ResetObjectives() {
	Map.ObjectiveTextAuthor = "";
	Map.ObjectiveTextGold = "";
	Map.ObjectiveTextSilver = "";
	Map.ObjectiveTextBronze = "";

	Map.TMObjective_AuthorTime = -1;
	Map.TMObjective_BronzeTime = -1;
	Map.TMObjective_SilverTime = -1;
	Map.TMObjective_GoldTime = -1;
}

// ---------------------------------- //
/** Check if the objectives times are valid
 *
 *	For the time being this function shoud always return True
 *	because the values set in the TMObjectives variables
 *	are automatically checked by the game and sanitized
 *	when necessary.
 *
 *	@return		True if the objectives are valid, false otherwise
 */
Boolean ObjectivesAreValid() {
	if (
		Map.TMObjective_BronzeTime <= 0
		|| Map.TMObjective_SilverTime > Map.TMObjective_BronzeTime
		|| Map.TMObjective_GoldTime > Map.TMObjective_SilverTime
		|| Map.TMObjective_AuthorTime > Map.TMObjective_GoldTime
	) {
		return False;
	}
	
	return True;
}