Config/script.js |
"use strict";
this.author = "spara";
this.copyright = "2015 spara";
this.description = "asteroid respawner and multiplier";
this.name = "asteroid_tweaks";
this.startUp = function() {
//asteroid respawn
if (!missionVariables.asteroid_tweaks_respawn) {
this.$respawn = false;
}
else this.$respawn = true;
//asteroid multiplier
if (!missionVariables.asteroid_tweaks_multiplier) {
missionVariables.asteroid_tweaks_multiplier = 1;
}
this.$asteroidMultiplier = missionVariables.asteroid_tweaks_multiplier;
//main station configuration interface
if (!missionVariables.asteroid_tweaks_config_main) {
this.$configMain = true;
}
else this.$configMain = false;
}
this.missionScreenOpportunity= function() {
if (!missionVariables.asteroid_tweaks_run_once) {
this._showAsteroidConfig();
missionVariables.asteroid_tweaks_run_once = true;
}
delete this.missionScreenOpportunity;
}
//multiply the number of asteroids when populating
this.systemWillPopulate = function (){
var addition = this.$asteroidMultiplier - 1;
if (addition > 0) {
system.setPopulator("moar_rocks", {
callback: function(pos) {
var asteroids = system.shipsWithPrimaryRole("asteroid");
for (var i = 0; i < asteroids.length; i++) {
var position = asteroids[i].position;
system.addShips("asteroid", addition, position, 25E3);
};
}.bind(this),
location: "INNER_SYSTEM_OFFPLANE"
});
}
}
this.shipExitedWitchspace = function() {
function $findHermits(entity) {
if (entity.primaryRole && entity.primaryRole.indexOf("rockhermit") !== -1) {
return true;
}
return false;
}
if (this.$respawn) {
//count the total number of asteroids
this.$asteroidsTotal = system.countShipsWithPrimaryRole("asteroid");
//define upper and lower limits for asteroid spawning around hermits.
var hermits = system.filteredEntities(this, $findHermits, player.ship);
if (hermits.length > 0) {
var asteroidsAroundHermits = 0;
for (var i = 0; i < hermits.length; i++) {
var hermit = hermits[i];
asteroidsAroundHermits += system.countShipsWithPrimaryRole("asteroid", hermit, 25E3);
}
var average = asteroidsAroundHermits / hermits.length;
this.$upperHermitLimit = Math.ceil(average / 2);
this.$lowerHermitLimit = Math.floor(average / 4);
}
}
}
this.startUpComplete = function() {
this.shipWillDockWithStation(player.ship.dockedStation);
this.shipExitedWitchspace();
}
//replace destroyed asteroids when docking.
this.shipDockedWithStation = function(station) {
function $findHermits(entity) {
if (entity.primaryRole && entity.primaryRole.indexOf("rockhermit") !== -1) {
return true;
}
return false;
}
function $findFarawayAsteroids(entity){
if (entity.primaryRole && entity.primaryRole === "asteroid" && entity.position.distanceTo(player.ship.position) > 500E3) {
return true;
}
return false;
}
if (this.$respawn) {
//how many asteroids are we missing?
//4 boulders count as one asteroid and 4 splinters count as one boulder
//these are averages. an asteroid spawns 2-6 boulders and a boulder 2-6 splinters
var reSpawnCount = Math.floor(this.$asteroidsTotal - system.countShipsWithPrimaryRole("asteroid") - (system.countShipsWithPrimaryRole("boulder") + system.countShipsWithPrimaryRole("GriffBoulderBrown") + system.countShipsWithPrimaryRole("GriffBoulderBlue") + system.countShipsWithPrimaryRole("GriffBoulderGrey")) / 4 - system.countShipsWithPrimaryRole("splinter") / 16);
//log(this.name, "missing " + reSpawnCount + " asteroids");
//check hermits
if (reSpawnCount > 0) {
var hermits = system.filteredEntities(this, $findHermits);
if (hermits.length > 0) {
for (var i = 0; i < hermits.length; i++) {
var hermit = hermits[i];
var asteroids = Math.floor(system.countShipsWithPrimaryRole("asteroid", hermit, 25E3) - (system.countShipsWithPrimaryRole("boulder", hermit, 25E3) + system.countShipsWithPrimaryRole("GriffBoulderBrown", hermit, 25E3) + system.countShipsWithPrimaryRole("GriffBoulderBlue", hermit, 25E3) + system.countShipsWithPrimaryRole("GriffBoulderGrey", hermit, 25E3)) / 4 - system.countShipsWithPrimaryRole("splinter", hermit, 25E3) / 16);
//check if we're between the spawn limits
if (asteroids < this.$upperHermitLimit && asteroids > this.$lowerHermitLimit) {
var difference = this.$upperHermitLimit - asteroids;
while (difference > 0 && reSpawnCount > 0) {
system.addShips("asteroid", 1, hermit.position, 25E3);
//log(this.name, "spawned a hermit asteroid");
difference--;
reSpawnCount--;
}
}
}
}
};
//Do we still have more asteroids to respawn? If so, try to create a new field into space
if (reSpawnCount > 0) {
//report asteroids over min distance away from player
var asteroids = system.filteredEntities(this, $findFarawayAsteroids);
//Add field only if it can be added around an asteroid far away enough
if (asteroids.length > 0) {
var spawnPosition = asteroids[Math.floor(Math.random(asteroids.length))].position;
//add asteroids as a cloud
system.addShips("asteroid", reSpawnCount, spawnPosition, 25E3);
//log(this.name, "spawned a field of " +reSpawnCount + " asteroids");
}
//In case there's no suitable asteroid to be used as a spawn position, spread 'em around the player
else {
for (var i = 0; i < reSpawnCount; i++) {
var spawnPosition = player.ship.position.add(Vector3D.randomDirection(500E3)).multiply(1 + Math.random());
system.addShips("asteroid", 1, spawnPosition, 25E3);
//log(this.name, "spawned a scattered asteroid");
}
}
};
}
}
this.shipWillDockWithStation = function(station) {
if (station.primaryRole.indexOf("rockhermit") !== -1 || (station.isMainStation && this.$configMain)) {
station.setInterface("asteroid_tweaks",{
title: "Asteroid tweaks",
category: "Config",
summary: "Configure asteroid tweaks.",
callback: this._showAsteroidConfig.bind(this)
});
}
}
this._showAsteroidConfig = function() {
var options = {
"1_X" : "1x",
"2_X" : "2x",
"3_X" : "3x",
"4_X" : "4x",
"5_X" : "5x"
};
mission.runScreen({
title: "Asteroid Tweaks",
message: "Do you want moar asteroids? Multiply the number of asteroids the game spawns when a system is being created. Note though, more asteroids means more stress for your computer. Current multiplier is " + this.$asteroidMultiplier + ".",
choices: options
},
function (choice) {
this.$asteroidMultiplier = choice.split('_')[0];
missionVariables.asteroid_tweaks_multiplier = this.$asteroidMultiplier;
this._showRespawnConfig();
});
}
this._showRespawnConfig = function() {
var options = {
"1_NO" : "No, don't respawn asteroids",
"2_YES" : "Yes, respawn asteroids"
};
mission.runScreen({
title: "Asteroid Tweaks",
message: "In the default game, it's possible to empty a system from asteroids by destroying them all. This setting makes the game respawn destroyed asteroids as a new asteroid field somewhere in system when the player docks. Current asteroid respawn setting is: " + this.$respawn + ".",
choices: options
},
function (choice) {
if (choice === "1_NO" && this.$respawn) {
this.$respawn = false;
delete missionVariables.asteroid_tweaks_respawn;
}
else if (choice === "2_YES" && !this.$respawn) {
this.$respawn = true;
this.shipExitedWitchspace();
missionVariables.asteroid_tweaks_respawn = 1;
}
this._showMainConfig();
});
}
this._showMainConfig = function() {
var options = {
"1_YES" : "Yes, show it at the main station",
"2_NO" : "No, don't show it at the main station"
};
mission.runScreen({
title: "Asteroid Tweaks",
message: "Show this config interface at the main station interfaces. Interface is always available at any rock hermit.\n\nDon't worry, the settings you have chosen will remain active even if you decide to hide the interface from the main station.",
choices: options
},
function (choice) {
if (choice === "1_YES" && !this.$configMain) {
this.$configMain = true;
delete missionVariables.asteroid_tweaks_config_main;
}
else if (choice === "2_NO" && this.$configMain){
this.$configMain = false;
missionVariables.asteroid_tweaks_config_main = 1;
system.mainStation.setInterface("asteroid_tweaks",null);
}
});
}
|