| Scripts/spawn_moon.js | // Standard attributes 
"use strict";
this.name           = "SpawnMoons"; 
this.author         = "Stranger"; 
this.copyright      = "This script is hereby placed in the public domain."; 
this.version        = "1.1"; 
this.description    = "Spawn moons around planets";
// Configuration
this.$planetRadius = 6400;
this.$planetDensity = 5.525;
this.shipWillExitWitchspace = function()
    {
    //Terran moons
    this.$orbitPeriod_SeleneBase = 4 * 24 * 60;    //  4 days 00 hours 00 mins
    this.$distance_SeleneShift = system.scrambledPseudoRandomNumber(143);
    this.$distance_LunaShift = system.scrambledPseudoRandomNumber(147);
    this.$offsetMoon = system.scrambledPseudoRandomNumber(151);
    this.$selectMoon_Terran = system.scrambledPseudoRandomNumber(159);
    this.$targetFlag_Terran = 0;
    this.$seedMoon_Terran();
    //Jovian moons
    this.$orbitMoon_Inner = system.scrambledPseudoRandomNumber(227);
    this.$orbitMoon_Outer = system.scrambledPseudoRandomNumber(235);
    this.$targetFlag_Jovian = 0;
    this.$targetPlanet_Jovian = -1;
    this.$seedingDelay = new Timer(this, this.$seedMoon_Jovian, 0.20);    // delay to set planet orbits
    }
this.$seedMoon_Terran = function()
    {
    if (system.isInterstellarSpace) return;
    if (this.$targetFlag_Terran > 0) return;  // moons already exist - prevents multiple seeding
    // calculate main planet density
    var w = worldScripts.AstroLibrary;
	var info = system.info;
    if (system.info.sun_radius)
		{
        var sunRadius = system.sun.radius;
		}
    var sunTemperature = w.$astroLib_sunTemperature(sunRadius);
    var sunLuminosity = w.$astroLib_sunLuminosity(sunTemperature,sunRadius);
    var mainOrbitVector = new Vector3D(system.sun.position.subtract(system.mainPlanet.position));
    var ouScale = mainOrbitVector.magnitude();
    var mainPlanetRadius = system.mainPlanet.radius;
    var mainPlanetInsolation = sunLuminosity * Math.pow((50000000/ouScale),2);
    var mainPlanetTemperature = w.$astroLib_equilibriumTemperature(mainPlanetInsolation);
    var mainPlanetDensity = w.$astroLib_planetDensity(mainPlanetRadius, mainPlanetTemperature);
    
    // set moon orbit
    var localTime = clock.minutes;
    var distance;
    var polar = Math.acos(1.4 * this.$offsetMoon - 0.7);
    var azimuth;
    var directionV;
    if (this.$selectMoon_Terran < 0.25) return;
    if (this.$selectMoon_Terran > 0.5)
        {
        distance = system.mainPlanet.radius * (16 + 8 * this.$distance_SeleneShift);
        this.$orbitRadius_Selene = distance / system.mainPlanet.radius;
        this.$orbitPeriod_Selene = this.$orbitPeriod_SeleneBase * Math.pow((this.$orbitRadius_Selene/16),1.5) * Math.pow((this.$planetDensity/mainPlanetDensity),0.5);
        var phaseSelene = 2.0 * Math.PI * (localTime % this.$orbitPeriod_Selene) / this.$orbitPeriod_Selene;
        azimuth = 2.0 * Math.PI * this.$offsetMoon + phaseSelene;
        directionV = Vector3D(Math.sin(polar) * Math.cos(azimuth), Math.sin(polar) * Math.sin(azimuth), Math.cos(polar));
        var tagSelene = Math.floor(this.$selectMoon_Terran * 10);
        var addedMoon_Selene = system.addMoon("omoon_Selene" + tagSelene);
        addedMoon_Selene.position = system.mainPlanet.position.toCoordinateSystem("pwm").add(directionV.multiply(distance)).fromCoordinateSystem("pwm");
        }
    if (this.$selectMoon_Terran < 0.75)
        {
        distance = system.mainPlanet.radius * (5 + 2.5 * this.$distance_LunaShift);
        this.$orbitRadius_Luna = distance / system.mainPlanet.radius;
        this.$orbitPeriod_Luna = this.$orbitPeriod_SeleneBase * Math.pow((this.$orbitRadius_Luna/16),1.5) * Math.pow((this.$planetDensity/mainPlanetDensity),0.5);
        var phaseLuna = 2.0 * Math.PI * (localTime % this.$orbitPeriod_Luna) / this.$orbitPeriod_Luna;
        azimuth = 2.0 * Math.PI * this.$offsetMoon + phaseLuna;
        directionV = Vector3D(Math.sin(polar) * Math.cos(azimuth), Math.sin(polar) * Math.sin(azimuth), Math.cos(polar));
        var tagLuna = Math.floor(this.$selectMoon_Terran * 10);
        var addedMoon_Luna = system.addMoon("omoon_Luna" + tagLuna);
        addedMoon_Luna.position = system.mainPlanet.position.toCoordinateSystem("pwm").add(directionV.multiply(distance)).fromCoordinateSystem("pwm");
        }
    this.$targetFlag_Terran = 1;    // Terran moons seeded - set flag to prevent multiple seeding
    }
this.$planetLock_Jovian = function()
    {
    var iplanetRadius = 150000;
    for (let i=0; i<system.planets.length; i++) 
        { 
        if(system.planets[i].radius >= iplanetRadius) 
            {
            this.$targetPlanet_Jovian = i;
            iplanetRadius = system.planets[i].radius;
            }
        }
    }
this.$seedMoon_Jovian = function()
    {
    this.shipDied();
    if (system.isInterstellarSpace) return;
    if (this.$targetFlag_Jovian > 0) return;    // moons already exist - prevents multiple seeding
    this.$planetLock_Jovian();
    if (this.$targetPlanet_Jovian < 0) return;   // no giant planet in system detected
    // seed jovian moons
    var p = this.$targetPlanet_Jovian;
    this.$giantRadius = system.planets[p].radius;
    var selectMoon_Jovian = system.scrambledPseudoRandomNumber(223);
    var paintMoon_Inner = system.scrambledPseudoRandomNumber(231);
    var paintMoon_Outer = system.scrambledPseudoRandomNumber(239);
        {
        if (selectMoon_Jovian > 0.25)
           {
            var orbitVector_ZeusInner;
            orbitVector_ZeusInner = Vector3D.randomDirection(this.$giantRadius * (5 + 2.5 * this.$orbitMoon_Inner));
            this.$orbitRadius_ZeusInner = orbitVector_ZeusInner.magnitude() / this.$giantRadius;
            var tagZeusInner = Math.floor(selectMoon_Jovian * 10); 
            var addedMoon_ZeusInner = system.addMoon("omoon_ZeusInner" + tagZeusInner);
            addedMoon_ZeusInner.position = system.planets[p].position.add(orbitVector_ZeusInner);
            if (paintMoon_Inner < 0.5)
                {
                addedMoon_ZeusInner.texture = "omoon_Io.png";
                }
            else
                {
                addedMoon_ZeusInner.texture = "omoon_Europa.png";
                }
            }
        if (selectMoon_Jovian < 0.75)
            {
            var orbitVector_ZeusOuter;
            orbitVector_ZeusOuter = Vector3D.randomDirection(this.$giantRadius * (10 + 5 * this.$orbitMoon_Outer));
            this.$orbitRadius_ZeusOuter = orbitVector_ZeusOuter.magnitude() / this.$giantRadius;
            var tagZeusOuter = Math.floor(selectMoon_Jovian * 10);
            var addedMoon_ZeusOuter = system.addMoon("omoon_ZeusOuter" + tagZeusOuter);
            addedMoon_ZeusOuter.position = system.planets[p].position.add(orbitVector_ZeusOuter);        
            if (paintMoon_Outer < 0.5)
                {
                addedMoon_ZeusOuter.texture = "omoon_Ganymede.png";
                }
            else
                {
                addedMoon_ZeusOuter.texture = "omoon_Callisto.png";
                }
            }
        }
    this.$targetFlag_Jovian = 1;    // moons seeded - set flag to prevent multiple seeding
    };
this.shipWillLaunchFromStation = function()
    {
    this.shipWillExitWitchspace();
    delete this.shipWillLaunchFromStation;
    }
this.shipWillDockWithStation = this.shipWillEnterWitchspace = function()
	{
    this.shipDied();
    }
this.shipDied = function()  // kill timer after moon seeding
	{
	if(this.$seedingDelay) 
		{
		this.$seedingDelay.stop();
		delete this.$seedingDelay;
		}
    } |