Config/script.js |
"use strict";
this.name = "SolarFlares";
this.author = "phkb";
this.copyright = "2018 phkb";
this.description = "Adds visible solar flares to systems with solar activity";
this.licence = "CC BY-NC-SA 4.0";
//TODO: make ship heat up if inside a flare
// Flaring code borrowed from "oolite-nova-mission"
// Copyright © 2004-2018 Giles C Williams and contributors
this._debug = false;
//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function () {
if (missionVariables.SolarFlares_Hues) {
this._originalHue = missionVariables.SolarFlares_Hues;
delete missionVariables.SolarFlares_Hues;
}
if (missionVariables.SolarFlares_Flare) {
this._originalFlare = missionVariables.SolarFlares_Flare;
delete missionVariables.SolarFlares_Flare;
}
this.shipExitedWitchspace();
}
//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function() {
if (system.sun && (this._debug === true || system.description.indexOf("solar activity") >= 0) && !system.sun.isGoingNova && !system.sun.hasGoneNova) {
missionVariables.SolarFlares_Hues = this._originalHue;
missionVariables.SolarFlares_Flare = this._originalFlare;
}
}
//-------------------------------------------------------------------------------------------------------------
this.shipExitedWitchspace = function () {
if (system.sun) {
// don't do anything in a nova system
if (system.sun.isGoingNova || system.sun.hasGoneNova) return;
// turn on the flare timer in solar activity systems
if (this._debug === true || system.description.indexOf("solar activity") >= 0) {
this._originalHue = system.info.corona_hues;
this._originalFlare = system.info.corona_flare
if (system.info.corona_flare < 0.15) system.info.corona_flare = 0.15;
// The first flare up will begin in between 4 and 8 minutes.
this._flareTimer = new Timer(this, this.$flareUp, Math.random() * 240 + 240);
}
}
};
//-------------------------------------------------------------------------------------------------------------
this.shipWillEnterWitchspace = function() {
if (system.sun) {
// if we were running, stop the timer and frame callback
if (this._flareTimer) {
this._flareTimer.stop();
delete this._flareTimer;
}
if (this._flareCallback) {
removeFrameCallback(this._flareCallback);
delete this._flareCallback;
}
// don't do anything in a nova system
if (system.sun.isGoingNova || system.sun.hasGoneNova) return;
// put everything back the way we found it
if (this._debug === true || system.description.indexOf("solar activity") >= 0) {
system.info.corona_hues = this._originalHue;
system.info.corona_flare = this._originalFlare;
}
}
}
//-------------------------------------------------------------------------------------------------------------
this.$flareUp = function $flareUp() {
system.info.corona_hues = 1;
// This flare up (0.3 to 0.5 flare) will last between 4 and 8 minutes
this._flareTarget = 0.3 + Math.random() * 0.2;
this._flareCallback = addFrameCallback(this.$flareTransition.bind(this));
this.$flareChange(this.$flareDown, Math.random() * 240 + 240);
};
//-------------------------------------------------------------------------------------------------------------
this.$flareDown = function $flareDown() {
system.info.corona_hues = this._originalHue;
// This quiet moment (0.1 to 0.25 flare) will last between 4 and 8 minutes
this._flareTarget = 0.1 + Math.random() * 0.15;
this._flareCallback = addFrameCallback(this.$flareTransition.bind(this));
this.$flareChange(this.$flareUp, Math.random() * 240 + 240);
};
//-------------------------------------------------------------------------------------------------------------
this.$flareChange = function (callFunc, callDelay) {
this._flareTimer.stop();
delete this._flareTimer;
this._flareTimer = new Timer(this, callFunc, callDelay);
};
//-------------------------------------------------------------------------------------------------------------
this.$flareTransition = function $flareTransition(delta) {
var current = system.info.corona_flare;
if (current < this._flareTarget) {
current += delta / 20;
} else {
current -= delta / 20;
}
system.info.corona_flare = current;
if (this._flareTarget > 0.275 && current > this._flareTarget) {
removeFrameCallback(this._flareCallback);
delete this._flareCallback;
} else if (this._flareTarget < 0.275 && current < this._flareTarget) {
removeFrameCallback(this._flareCallback);
delete this._flareCallback;
}
} |