| Config/script.js | "use strict";
this.name = "Satellite";
this.author = "Rorschachhamster";
this.copyright = "Creative Commons Attribution - Non-Commercial - Share Alike 3.0 license";
this.description = "Populator for Satellites around Main Planet";
this._mainBeacons = true;
this._maxSatsMain = 6;
this._epBeacons = true;
this._maxSatsEP = 2;
this._trueValues = ["yes", "1", 1, "true", true];
this._extraPlanetSatellites = null;
this._lastID = 0;
this._destroyed = [];
// configuration settings for use in Lib_Config
this._satConfig = {
    Name: this.name, Alias: expandDescription("[rssat_config_alias]"), Display: expandDescription("[rssat_config_display]"), Alive: "_satConfig",
    Bool: {
        B0: { Name: "_mainBeacons", Def: true, Desc: expandDescription("[rssat_main_beacons]") },
        B1: { Name: "_epBeacons", Def: true, Desc: expandDescription("[rssat_extra_beacons]") },
        Info: expandDescription("[rssat_config_bool_info]")
    },
    SInt: {
        S0: { Name: "_maxSatsMain", Def: 6, Min: 0, Max: 15, Desc: expandDescription("[rssat_main_sats]") },
        S1: { Name: "_maxSatsEP", Def: 2, Min: 0, Max: 5, Desc: expandDescription("[rssat_extra_sats]") },
        Info: expandDescription("[rssat_config_sint_info]")
    }
};
//-------------------------------------------------------------------------------------------------------------
this.startUp = function () {
    if (missionVariables.Satellites_MainBeacons) this._mainBeacons = (this._trueValues.indexOf(missionVariables.Satellites_MainBeacons) >= 0 ? true : false);
    if (missionVariables.Satellites_ExtraPlanetBeacons) this._epBeacons = (this._trueValues.indexOf(missionVariables.Satellites_ExtraPlanetBeacons) >= 0 ? true : false);
    if (missionVariables.Satellites_MaxMain) this._maxSatsMain = parseInt(missionVariables.Satellites_MaxMain);
    if (missionVariables.Satellites_MaxEP) this._maxSatsEP = parseInt(missionVariables.Satellites_MaxEP);
    if (missionVariables.Satellites_Destroyed) this._destroyed = JSON.parse(missionVariables.Satellites_Destroyed);
}
this.startUpComplete = function() {
    // register our settings, if Lib_Config is present
    if (worldScripts.Lib_Config) worldScripts.Lib_Config._registerSet(this._satConfig);
}
//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function () {
    missionVariables.Satellites_MainBeacons = this._mainBeacons;
    missionVariables.Satellites_ExtraPlanetBeacons = this._epBeacons;
    missionVariables.Satellites_MaxMain = this._maxSatsMain;
    missionVariables.Satellites_MaxEP = this._maxSatsEP;
    missionVariables.Satellites_Destroyed = JSON.stringify(this._destroyed);
}
//-------------------------------------------------------------------------------------------------------------
this.shipWillEnterWitchspace = function () {
    this._destroyed = [];
}
//-------------------------------------------------------------------------------------------------------------
this.systemWillPopulate = function () {
    this._lastID = 0;
    if (system.isInterstellarSpace) return;
    if (!system.mainPlanet) return;
    // random number of sats, but consistent per system
    var RaSaCount = system.scrambledPseudoRandomNumber(3873 + system.ID) * (system.techLevel + 1) * ((system.population + 33) / 100);
    if (RaSaCount > this._maxSatsMain) RaSaCount = this._maxSatsMain;
    for (var i = 0; i <= RaSaCount; i++) {
        // skip any satellites that might have been destroyed
        if (Array.isArray(this._destroyed) && this._destroyed.indexOf(i + 1) >= 0) continue;
        // random initial position of sats, but consistent per system
        var Schalter = Math.round(system.scrambledPseudoRandomNumber(11212 + system.ID + i) * 6);
        switch (Schalter) {
            case 1:
                var pos = Vector3D((Math.random() - 0.5), (Math.random() - 0.5), 1.6).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [(Math.random() - 0.5), (Math.random() - 0.5), 1.6]);
                break;
            case 2:
                var pos = Vector3D((Math.random() - 0.5), 1.6, (Math.random() - 0.5)).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [(Math.random() - 0.5), 1.6, (Math.random() - 0.5)]);
                break;
            case 3:
                var pos = Vector3D(1.6, (Math.random() - 0.5), (Math.random() - 0.5)).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [1.6, (Math.random() - 0.5), (Math.random() - 0.5)]);
                break;
            case 4:
                var pos = Vector3D((Math.random() - 0.5), (Math.random() - 0.5), - 1.6).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [(Math.random() - 0.5), (Math.random() - 0.5), - 1.6]);
                break;
            case 5:
                var pos = Vector3D((Math.random() - 0.5), - 1.6, (Math.random() - 0.5)).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [(Math.random() - 0.5), - 1.6, (Math.random() - 0.5)]);
                break;
            case 6:
                var pos = Vector3D(-1.6, (Math.random() - 0.5), (Math.random() - 0.5)).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [ - 1.6, (Math.random() - 0.5), (Math.random() - 0.5)]);
                break;
            default:
                var pos = Vector3D((Math.random() - 0.5), (Math.random() - 0.5), 1.6).fromCoordinateSystem("pwp");
                //system.legacy_addShipsAtPrecisely(this.role1, 1, "pwp", [(Math.random() - 0.5), (Math.random() - 0.5), 1.6]);
                break;
        }
        // random types, but consistent in system
        var satType = i % 4; //Math.floor(system.scrambledPseudoRandomNumber(983 + system.ID + i) * 4);
        switch (satType) {
            case 0:
                system.setPopulator("rs_satellite_" + i, {
                    location: "COORDINATES",
                    coordinates: pos,
                    callback: function (pos) {
                        var sat = system.addShips("[RSSatellite_1]", 1, pos, 0);
                        if (sat && sat.length > 0) {
                            sat[0].primaryRole = "RSsatellite";
                            sat[0].script._ID = worldScripts.Satellite.$getID();
                            sat[0].script._orbiting = system.mainPlanet;
                            if (worldScripts.Satellite._mainBeacons === false) {
                                sat[0].beaconCode = "";
                                sat[0].beaconLabel = "";
                            }
                        }
                    }
                }
                );
                break;
            case 1:
                system.setPopulator("rs_satellite_" + i, {
                    location: "COORDINATES",
                    coordinates: pos,
                    callback: function (pos) {
                        var sat = system.addShips("[RSSatellite_2]", 1, pos, 0);
                        if (sat && sat.length > 0) {
                            sat[0].primaryRole = "RSsatellite";
                            sat[0].script._ID = worldScripts.Satellite.$getID();
                            sat[0].script._orbiting = system.mainPlanet;
                            if (worldScripts.Satellite._mainBeacons === false) {
                                sat[0].beaconCode = "";
                                sat[0].beaconLabel = "";
                            }
                        }
                    }
                }
                );
                break;
            case 2:
                system.setPopulator("rs_satellite_" + i, {
                    location: "COORDINATES",
                    coordinates: pos,
                    callback: function (pos) {
                        var sat = system.addShips("[RSSatellite_3]", 1, pos, 0);
                        if (sat && sat.length > 0) {
                            sat[0].primaryRole = "RSsatellite";
                            sat[0].script._ID = worldScripts.Satellite.$getID();
                            sat[0].script._orbiting = system.mainPlanet;
                            if (worldScripts.Satellite._mainBeacons === false) {
                                sat[0].beaconCode = "";
                                sat[0].beaconLabel = "";
                            }
                        }
                    }
                }
                );
                break;
            case 3:
                system.setPopulator("rs_satellite_" + i, {
                    location: "COORDINATES",
                    coordinates: pos,
                    callback: function (pos) {
                        var sat = system.addShips("[RSSatellite_4]", 1, pos, 0);
                        if (sat && sat.length > 0) {
                            sat[0].primaryRole = "RSsatellite";
                            sat[0].script._ID = worldScripts.Satellite.$getID();
                            sat[0].script._orbiting = system.mainPlanet;
                            if (worldScripts.Satellite._mainBeacons === false) {
                                sat[0].beaconCode = "";
                                sat[0].beaconLabel = "";
                            }
                        }
                    }
                }
                );
                break;
        }
    }
    // we'll add extra planet satellites via a timer, because we can't be sure when the planets will actually be created and ready to use
    this._extraPlanetSatellites = new Timer(this, this.$addExtraPlanetSatellites, 2, 0);
}
//-------------------------------------------------------------------------------------------------------------
this.$addExtraPlanetSatellites = function $addExtraPlanetSatellites() {
    if (system.planets.length > 1) {
        var satCount = system.countShipsWithRole("RSsatellite");
        var idCount = 0;
        var list = system.planets;
        for (var i = 0; i < list.length; i++) {
            // only add to non-main planets and to planets with atmosphere
            var pl = list[i];
            if (pl.isMainPlanet === false && pl.hasAtmosphere === true) {
                // random number, but consistent per system
                var s = Math.floor(system.scrambledPseudoRandomNumber(1245 + system.ID) * (this._maxSatsEP + 1));
                for (var j = 1; j <= s; j++) {
                    idCount += 1;
                    // skip any that have been destroyed
                    if (Array.isArray(this._destroyed) && this._destroyed.indexOf(satCount + idCount) >= 0) continue;
                    // random position
                    var eppos = pl.position.add(new Vector3D.randomDirection(pl.radius * (1 + (Math.random() * 0.4) + 0.2)));
                    // random types, but consistent in system
                    var satType = Math.floor(system.scrambledPseudoRandomNumber(785 + system.ID + i) * 4);
                    switch (satType) {
                        case 0:
                            var sat = system.addShips("[RSSatellite_1]", 1, eppos, 0);
                            if (sat && sat.length > 0) {
                                sat[0].primaryRole = "RSsatellite";
                                sat[0].script._ID = worldScripts.Satellite.$getID();
                                sat[0].script._orbiting = pl;
                                if (this._epBeacons === false) {
                                    sat[0].beaconCode = "";
                                    sat[0].beaconLabel = "";
                                }
                            }
                            break;
                        case 1:
                            var sat = system.addShips("[RSSatellite_2]", 1, eppos, 0);
                            if (sat && sat.length > 0) {
                                sat[0].primaryRole = "RSsatellite";
                                sat[0].script._ID = worldScripts.Satellite.$getID();
                                sat[0].script._orbiting = pl;
                                if (this._epBeacons === false) {
                                    sat[0].beaconCode = "";
                                    sat[0].beaconLabel = "";
                                }
                            }
                            break;
                        case 2:
                            var sat = system.addShips("[RSSatellite_3]", 1, eppos, 0);
                            if (sat && sat.length > 0) {
                                sat[0].primaryRole = "RSsatellite";
                                sat[0].script._ID = worldScripts.Satellite.$getID();
                                sat[0].script._orbiting = pl;
                                if (this._epBeacons === false) {
                                    sat[0].beaconCode = "";
                                    sat[0].beaconLabel = "";
                                }
                            }
                            break;
                        case 3:
                            var sat = system.addShips("[RSSatellite_4]", 1, eppos, 0);
                            if (sat && sat.length > 0) {
                                sat[0].primaryRole = "RSsatellite";
                                sat[0].script._ID = worldScripts.Satellite.$getID();
                                sat[0].script._orbiting = pl;
                                if (this._epBeacons === false) {
                                    sat[0].beaconCode = "";
                                    sat[0].beaconLabel = "";
                                }
                            }
                            break;
                    }
                }
            }
        }
    }
}
//-------------------------------------------------------------------------------------------------------------
this.$getID = function () {
    this._lastID += 1;
    return this._lastID;
} |