//
// Author: Qam1
// Name: Mercury Triple Sunrise and Sunset
// Version: 2.1
// License: Public Domain
// Description: Due to the quirks in Mercury's orbit and rotation at certain spots the sun will rise & set 3 different times in one Mercury day.
//

include("i18n.inc");

var r = 1
var long = 90;
var ds = 2455361;

core.setObserverLocation(90.0, 0.00, 1, 1, "", "Mercury");

var locTxt = LabelMgr.labelScreen(tr("Location: Mercury, 0N, 90E"), 10, 10, false, 15, "#F9E397");
LabelMgr.setLabelShow(locTxt, true);

LandscapeMgr.setFlagAtmosphere(false);
LandscapeMgr.setFlagFog(false);
LandscapeMgr.setCurrentLandscapeID("ocean");
core.setGuiVisible(false);

var y = core.getScreenHeight()/768;
var x = core.getScreenWidth()/1024;

core.selectObjectByName("Sun", pointer = true);
StelMovementMgr.setFlagTracking(true);
StelMovementMgr.zoomTo(50, 1);

var d = 0;
while (r == 1)
{
	d = d + 1;
	var j = core.getJDay();
	if (j > 2561117)
	{
		j = 2455361;
		ds = 455461;
		d = 1;
	}

	while (j > ds)
	{
		ds = ds + 176;
	}

	core.setJDay(ds);

	var txt1 = LabelMgr.labelScreen(tr("Just Before Sunrise....."), 620*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	txt1 = LabelMgr.labelScreen(tr("Surface of Mercury"), 640*x, 35, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	txt1 = LabelMgr.labelScreen(tr("Day"), 660*x, 60, false, 20, "#F0F0F0");
	LabelMgr.setLabelShow(txt1, true);

	txt1 = LabelMgr.labelScreen(d, 720*x, 60, false, 20, "#F0F0F0");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(0);
	core.wait(1);

	var SunData = core.getObjectInfo("Sun");
	var SunAlt = SunData["altitude"];

	while (SunAlt > -2)
	{
		core.setDate("-1 day");
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
		core.wait(0.01);
	}

	npcc = 0;

	var ctr = core.getTimeRate();
	core.setTimeRate(30000);
	while (SunAlt < 0)
	{
		core.wait(0.01);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
	}
	core.setTimeRate(ctr);

	StelMovementMgr.zoomTo(10, 1);
	LabelMgr.deleteAllLabels();

	txt1 = LabelMgr.labelScreen(tr("Sunrise #1"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(0);
	core.wait(3);
	core.setTimeRate(30000);

	var ls = SunAlt - 10;
	var lt = 0;

	LabelMgr.setLabelShow(txt1, false);

	while (lt == 0)
	{
		core.wait(0.01);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];

		if (SunAlt < ls)
		{
			lt = 1;
		}
		ls = SunAlt;
	}

	core.setTimeRate(30000);
	LabelMgr.deleteAllLabels();

	txt1 = LabelMgr.labelScreen(tr("Retro"), 600*x, 10, false, 15, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	lt = 0;
	ls = SunAlt + 10;

	while (lt == 0)
	{
		core.wait(0.01);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];

		if (ls < SunAlt)
		{
			lt = 1;
		}
		ls = SunAlt;
	}

	LabelMgr.deleteAllLabels();

	txt1 = LabelMgr.labelScreen(tr("Sunset #1"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(10);
	core.wait(5);
	core.setTimeRate(30000);
	LabelMgr.deleteAllLabels();

	var ctr = core.getTimeRate();
	core.setTimeRate(30000);
	while (SunAlt < 0)
	{
		core.wait(0.01);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
	}
	core.setTimeRate(ctr);

	txt1 = LabelMgr.labelScreen(tr("Sunrise #2"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(30000);

	while (SunAlt < 5)
	{
		core.wait(0.01);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
	}

	LabelMgr.deleteAllLabels();

	txt1 = LabelMgr.labelScreen(tr("Later in the afternoon"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	StelMovementMgr.zoomTo(120, 1);
	core.setTimeRate(30000);

	while (SunAlt > 5)
	{
		core.wait(0.1);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
		core.setDate("+1 day");
	}

	if (SunAlt < 2)
	{
		core.setDate("-1 days");
	}

	StelMovementMgr.zoomTo(10, 1);

	ctr = core.getTimeRate();
	core.setTimeRate(20000);
	while (SunAlt > 0)
	{
		core.wait(0.1);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"] - 0.5;
	}
	core.setTimeRate(ctr);
	LabelMgr.deleteAllLabels();

	txt1 = LabelMgr.labelScreen(tr("Sunset #2"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(10);
	core.wait(5);
	core.setTimeRate(20000);
	LabelMgr.deleteAllLabels();

	npcc = 0;

	ctr = core.getTimeRate();
	core.setTimeRate(20000);
	while (SunAlt <0.5 )
	{
		core.wait(0.1);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
	}
	core.setTimeRate(ctr);

	txt1 = LabelMgr.labelScreen(tr("Sunrise #3"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(10);
	core.wait(5);
	core.setTimeRate(20000);
	LabelMgr.deleteAllLabels();

	ctr = core.getTimeRate();
	core.setTimeRate(30000);
	while (SunAlt > -0.2)
	{
		
		core.wait(0.1);
		SunData = core.getObjectInfo("Sun");
		SunAlt = SunData["altitude"];
	}
	core.setTimeRate(ctr);

	txt1 = LabelMgr.labelScreen(tr("Sunset #3"), 600*x, 10, false, 20, "#F9E397");
	LabelMgr.setLabelShow(txt1, true);

	core.setTimeRate(10);
	core.wait(5);
	core.setTimeRate(20000);
	LabelMgr.deleteAllLabels();

	StelMovementMgr.zoomTo(120, 1);

	for(c=1; c<66; c++)
	{
		core.setDate("+1 day");
		core.wait(0.1);
	}

	core.setTimeRate(1);

	LabelMgr.deleteAllLabels();
	
	if (d == 3)
	{
		r = 0;
	}
}

core.setGuiVisible(true);
core.goHome();
