| Config/script.js |
"use strict";
this.name = "station_validator";
this.author = "spara";
this.copyright = "2013 - 2014 Mika Spara";
this.licence = "CC BY-NC-SA 3.0";
this.description = "Centralized service to keep track of destroyed stations";
this.version = "1.0";
//This oxp keeps track of destroyed stations. When a station gets destroyed the destruction is noted. The primary role of the destroyed station is saved along with the destruction time, systemID and place of death. Oxps can then ask this oxp for an array or death times and places when they want to spawn a station with a specific role. If a station with that role has been destroyed in current system and the rebuild time has not been reached, then the death details are given. In case of multiple stations, the array can have multiple items.
//Usage 1: Simple check before spawning a singular station.
// * Ask for spawning permission by putting worldScripts.station_validator.$deathTime("your_station_role", rebuildTime) into your spawning script and receive an array of deaths in return.
// * An empty array means that the station was not found from the destroyed stations array and can be spawned.
//Example: SothisTC uses the following code before spawning the station:
//if (worldScripts.station_validator && (worldScripts.station_validator.$deaths("sothis").length !== 0))
// return;
//Usage 2: Case of multiple stations of the same role.
// * Ask for spawning permission by putting worldScripts.station_validator.$deaths("your_station_role", rebuildTime) into your spawning script and receive an array of deaths in return.
// * Cycle through the list of death times and spawn places and act accordingly.
//Usage 3: Use for showing different stages of construct when rebuilding a station
// * Ask for spawning permission by putting worldScripts.station_validator.$deaths("your_station_role", rebuildTime) into your spawning script and receive an array of deaths in return.
// * Spawn and unfinished construct to the place of death. Construct must have "construct" among it's roles and constructRole = "role_of_the_station_to_be_built" in its script_info
// * If the unfinished station gets destroyed, the original deathTime is reset.
this.$runOnce = 1;
this.startUp = function() {
//Make sure startUp is ran exactly once.
if (this.$runOnce) {
//destroyed stations array is used for storing all destroyed stations in a galaxy.
if (missionVariables.stationValidator)
this.$destroyedStations = JSON.parse(missionVariables.stationValidator);
else this.$destroyedStations = new Array();
this.$blockedHermits = 0;
this.$runOnce = 0;
}
}
//A public function for oxps to use for asking a spawning permit. Returns an array of death times and spawn places.
this.$deaths = function(role, rebuildTime) {
if (typeof rebuildTime === 'undefined') rebuildTime = 30;
//Make sure startUp is ran before this function returns anything
//This might happen if a scripts calls this function from it's startUp
if (this.$runOnce) this.startUp();
var i;
var systemID = system.ID;
var time = clock.days;
var deaths = new Array();
//Go through the destroyed stations array and when a match is found, add it to the deaths array
for (i = 0; i < this.$destroyedStations.length; i++) {
if (this.$destroyedStations[i][0] === systemID && role === this.$destroyedStations[i][1] && time < this.$destroyedStations[i][2] + rebuildTime)
//station found from the array is not yet rebuilt and spawning is not permitted
deaths.push({time: this.$destroyedStations[i][2], place: this.$destroyedStations[i][3]});
}
return deaths;
}
this.playerWillSaveGame = function() {
if (this.$destroyedStations.length > 0)
missionVariables.stationValidator = JSON.stringify(this.$destroyedStations);
else delete missionVariables.stationValidator;
}
//When a stationary station or a construct is spawned, add death note to it's shipDied script.
//Handle main stations and hermits
this.shipSpawned = function(ship) {
//handle main station
if (ship.isMainStation && this.$deaths(ship.primaryRole).length !== 0) {
ship.remove();
log("stat","Main station removed");
return;
}
//handle hermits
if (ship.primaryRole === "rockhermit" && this.$deaths("rockhermit").length > this.$blockedHermits) {
//remove miners around the destroyed hermit
var miners = system.shipsWithPrimaryRole("miner",ship,25000);
var i;
for (i = 0; i < miners.length; i++)
miners[i].remove();
//remove hermit
ship.remove();
this.$blockedHermits++;
return;
}
//note only stationary stations in normal space
if (!system.isInterstellarSpace && ship.isStation && !ship.maxSpeed || ship.hasRole("construct")) {
//add shipscript to notify on death event
if (ship.script.shipDied)
ship.script.$sparaShipDied = ship.script.shipDied;
else ship.script.$sparaShipDied = function() {};
ship.sparaSpawnPosition = ship.position;
ship.script.shipDied = function(who, why) {
worldScripts.station_validator.$stationDied(ship);
ship.script.$sparaShipDied(who, why);
delete ship.script.shipDied;
ship.script.shipDied = ship.script.$sparaShipDied;
delete ship.script.$sparaShipDied;
}
}
}
//stations call this when they die.
this.$stationDied = function(shipObj) {
//if the destroyed station is an unfinished construct, alter the death time of the original instead of adding a new destroyed station to the array.
if (shipObj.hasRole("construct") && shipObj.scriptInfo.constructRole) {
var i;
var role = shipObj.scriptInfo.constructRole;
var spawnPosition = shipObj.sparaSpawnPosition;
for (i = 0; i < this.$destroyedStations.length; i++) {
if (role === this.$destroyedStations[i][1] && spawnPosition === this.$destroyedStations[i][3]) {
this.$destroyedStations[i][2] = clock.days;
return;
}
}
//if something went wrong and the original station was not found, return silently.
return;
}
//add the destroyed station to the array
this.$destroyedStations.push([system.ID, shipObj.primaryRole, clock.days, shipObj.sparaSpawnPosition]);
}
//clean destroyed stations array when witchspacing or docking
this.shipWillEnterWitchspace = function() {
this.$cleanDestroyedStations();
this.$blockedHermits = 0;
}
this.shipWillDockWithStation = function() {
this.$cleanDestroyedStations();
}
this.$cleanDestroyedStations = function() {
var i;
var time = clock.days;
//remove stations that have been destroyed for more than 60 days from the destroyed stations array
for (i = this.$destroyedStations.length - 1; i >= 0 ; i--) {
if (time >= this.$destroyedStations[i][2] + 60)
this.$destroyedStations.splice(i, 1);
}
}
//forget everything when entering a new galaxy
this.playerEnteredNewGalaxy = function(galaxyNumber) {
this.$destroyedStations = new Array();
}
|