| Scripts/ringRacer_systemScript.js | this.name					= "RingRacer";
this.author					= "Thargoid";
this.copyright				= "Creative Commons Attribution - Non-Commercial - Share Alike 3.0 license with clauses - see readme.txt.";
this.description			= "Script for setting up Ring Racer Courses and running them.";
this.version				= "1.20";
this.startUp = function()
	{
	if(missionVariables.ringRacer == null)
		{ missionVariables.ringRacer = 0; }
	if(missionVariables.ringRacer_location == null)
		{ missionVariables.ringRacer_location = 0; }
	if(missionVariables.ringRacer_raceCounter == null)
		{ missionVariables.ringRacer_raceCounter = 0; }
	if(missionVariables.ringRacer_bestTimeM == null)
		{ missionVariables.ringRacer_bestTimeM = 0; }
	if(missionVariables.ringRacer_bestTimeS == null)
		{ missionVariables.ringRacer_bestTimeS = 0; }
	this.courseLocations = [129, 178, 151, 65, 87, 119, 33, 72, 168, 104];
	this.racing = false;
	}
this.playerBoughtEquipment = function(equipment)
	{
	if(equipment == "EQ_RINGRACER")
		{
		player.ship.removeEquipment("EQ_RINGRACER");
		missionVariables.ringRacer = 1;
		mission.markSystem(this.courseLocations[0]);
		mission.setInstructionsKey("RingRacer_" + this.courseLocations[0]);
		mission.runScreen({title:"Ring Racer League", messageKey:"ringRacer_infoScreen", model:"ringRacer_greenRing"});
		}
	}
this.shipWillExitWitchspace = this.shipLaunchedFromStation = function()
	{
	if(!missionVariables.ringRacer == 1) // if the ring racer pass hasn't been purchased
		{return;}
	this.raceLocation = this.courseLocations[missionVariables.ringRacer_raceCounter];
	if(galaxyNumber == 0 && system.ID == this.raceLocation)
		{
		if(system.countShipsWithRole("ringRacer_greenRing") == 0) // if the course isn't there yet
			{
			player.commsMessage("Welcome Ring Racer! Follow Q on your compass to the course.", 6);
			switch(this.raceLocation)
				{
				case 178:
				case 119:
				case 168:
					{	// sphere race
					log("Ring Racer", "Sphere race started near " + system.name);
					system.legacy_addShipsAtPrecisely("ringRacer_courseBuoy", 1, "abs", [0, 0, -50000]);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 10, "abs", [0, 0, -50000], 20000);
					this.startRace();
					break;
					}
				case 129: 
				case 151:
				case 65:
				case 87:
				case 33:
				case 72:
				case 104:
					{	// linear race
					log("Ring Racer", "Linear race started near " + system.name);
					system.legacy_addShipsAtPrecisely("ringRacer_courseBuoy", 1, "abs", [0, 0, -40000]);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [0, 0, -60000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [0, 0, -80000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [0, 20000, -80000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [0, 40000, -80000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [20000, 40000, -80000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [40000, 40000, -80000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [50000, 40000, -90000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [50000, 50000, -100000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [60000, 60000, -100000], 2500);
					system.legacy_addShipsWithinRadius("ringRacer_greenRing", 1, "abs", [60000, 75000, -110000], 2500);
					this.startRace();
					break;
					}
				}
			}
		}
	}
this.startRace = function()
	{
	missionVariables.ringRacer_ringCount = 0; // reset the count for a new race
	this.startTime = 0; // reset the time
	if(worldScripts["Welcome Information Script"]) // if Welcome Mat is loaded & running, disable it to stop data messages with the course buoys
		{
		if(worldScripts["Welcome Information Script"].welcomeTimer.isRunning)
			{
			worldScripts["Welcome Information Script"].welcomeTimer.stop();
			player.consoleMessage("Planetary Information System deactivated.",6);
			}
		}
	if(this.raceTimer)
		{
		this.raceTimer.start();
		}
	else
		{
		this.raceTimer = new Timer(this, this.raceCheck, 0, 1);
		}
	}
this.raceCheck = function()
	{
	if(missionVariables.ringRacer_ringCount == 1 && this.startTime == 0)
		{
		this.startTime = clock.absoluteSeconds;
		player.commsMessage("Ring race clock started.", 6);
		this.racing = true;
		}
	if(missionVariables.ringRacer_ringCount == 10) // if we're at the end of the race, having done all 10 rings
		{
		this.raceTimer.stop();
		this.raceEnd();
		}
	this.raceElapsedTime = Math.floor(clock.absoluteSeconds - this.startTime); // absolute time since the race began
	if(this.racing && this.raceElapsedTime > 900) // if we've been racing for over 15 minutes
		{
		player.commsMessage("Race time limit exceeded, clock stopped.",6);
		this.raceTimer.stop();
		missionVariables.ringRacer_ringCount = 0;
		}
	}
this.raceEnd = function()
	{
	this.racing = false;
	this.endTime = clock.absoluteSeconds;
	player.commsMessage("Ring race clock stopped.", 6);
	missionVariables.ringRacer_ringCount = 0;
	this.raceTime = Math.floor(this.endTime - this.startTime);
	this.raceTimeMinutes = Math.floor(this.raceTime / 60);
	this.raceTimeSeconds = this.raceTime - (60 * this.raceTimeMinutes);
	player.commsMessage("You completed the race in " + this.raceTimeMinutes + "m " + this.raceTimeSeconds + "s.", 6);
	this.bestTime = (missionVariables.ringRacer_bestTimeM * 60) + missionVariables.ringRacer_bestTimeS;
	if(this.bestTime == 0 && this.raceTime > 0)
		{
		player.commsMessage("We have registered your new best time.", 6);
		missionVariables.ringRacer_bestTimeM = this.raceTimeMinutes;
		missionVariables.ringRacer_bestTimeS = this.raceTimeSeconds;
		}
	if(this.bestTime > this.raceTime)
		{
		player.commsMessage("Congratulations, you've improved your best time!", 6);
		missionVariables.ringRacer_bestTimeM = this.raceTimeMinutes;
		missionVariables.ringRacer_bestTimeS = this.raceTimeSeconds;		
		}
	mission.unmarkSystem(this.courseLocations[missionVariables.ringRacer_raceCounter]);
	mission.setInstructionsKey(null);
	if(worldScripts["Welcome Information Script"]) // if Welcome Mat has been disabled earlier, restart it
		{
		if(!worldScripts["Welcome Information Script"].welcomeTimer.isRunning)
			{
			worldScripts["Welcome Information Script"].welcomeTimer.start();
			player.consoleMessage("Planetary Information System restarted.",6);
			}
		}
	if(missionVariables.ringRacer_raceCounter == 9) // if all 10 races have been run
		{
		missionVariables.ringRacer = 0;
		missionVariables.ringRacer_raceCounter = 0;
		player.commsMessage("And that concludes the Ring Racing league for this season.", 10);
		player.commsMessage("Your best time for the season was " + missionVariables.ringRacer_bestTimeM + "m " + missionVariables.ringRacer_bestTimeS + "s.", 10); 
		return;
		}
	missionVariables.ringRacer_raceCounter += 1;
	mission.markSystem(this.courseLocations[missionVariables.ringRacer_raceCounter]);
	mission.setInstructionsKey("RingRacer_" + this.courseLocations[missionVariables.ringRacer_raceCounter]);
	}
this.shipDied = this.shipWillEnterWitchspace = this.shipWillDockWithStation = function()
	{
	if(missionVariables.ringRacer == 1)
		{
		this.racing = false;
		if(this.raceTimer)
			{
			this.raceTimer.stop();
			missionVariables.ringRacer_ringCount = 0;
			}
		this.courseList = system.shipsWithRole("ringRacer_ringCourse"); // find all course entities so we can remove them
		if(this.courseList.length > 0) // if there are parts of the course present, loop through them and remove
			{
			let loopCounter = 0; // reset the counter
			for(loopCounter = 0; loopCounter < this.courseList.length; loopCounter++)
				{
				this.courseList[loopCounter].remove();
				}
			}
			if(worldScripts["Welcome Information Script"]) // if Welcome Mat has been disabled earlier, restart it
				{
				if(!worldScripts["Welcome Information Script"].welcomeTimer.isRunning)
					{
					worldScripts["Welcome Information Script"].welcomeTimer.start();
					}
				}
		}
	}
this.playerEnteredNewGalaxy = function(galaxyNumber)
	{
	if(missionVariables.ringRacer == 1)
		{
		missionVariables.ringRacer = 0;
		missionVariables.ringRacer_raceCounter = 0;
		missionVariables.ringRacer_location = 0;
		missionVariables.ringRacer_bestTimeM = 0;
		missionVariables.ringRacer_bestTimeS = 0;
		this.racing = false;
		}
	} |