Config/script.js |
"use strict";
this.name = "market_cooldown";
this.author = "spara";
this.description = "Create a fade out effect to the markets";
this.startUp = function() {
//this is the number of jumps needed to fade the effect
this.$jumpsNeeded = 10.0;
//fadecounter is a number between ]0, 10]
//this calculates the step the counter needs to take each time. don't touch
this.$step = 10.0 / this.$jumpsNeeded;
if (missionVariables.marketCooldown) {
this.$storedMarkets = JSON.parse(missionVariables.marketCooldown);
}
else this.$storedMarkets = new Object();
}
//store market on exiting station
this.shipLaunchedFromStation = function(station) {
if (system.isInterstellarSpace) return;
var sMarket = station.market;
var marketObj = new Object();
marketObj.fadeCounter = 0.0;
for (var i in sMarket) {
marketObj[i] = sMarket[i].quantity;
}
//a uniqueish key for the station calculated from station to witchpoint buoy and sun distances
var idKey = Math.round(station.position.magnitude() + station.position.distanceTo(system.sun.position));
this.$storedMarkets[idKey] = marketObj;
}
//advance counters on exiting withcspace. when a counter reaches 10, delete the market entry.
this.shipWillExitWitchspace = function() {
for (var i in this.$storedMarkets) {
//rounding used here for rounding possible rounding error on using fractional step counter.
if (Math.round(this.$storedMarkets[i].fadeCounter) >= 10){
delete this.$storedMarkets[i];
}
else {
this.$storedMarkets[i].fadeCounter += this.$step;
}
}
}
//add cooldown effect to all recently visited markets.
this.shipExitedWitchspace = function() {
var stations = system.stations;
for (var i = 0; i < stations.length; i++) {
var station = stations[i];
var idKey = Math.round(station.position.magnitude() + station.position.distanceTo(system.sun.position));
if (this.$storedMarkets[idKey]) {
var sMarket = station.market;
var oMarket = this.$storedMarkets[idKey];
var percentageMultiplier = oMarket.fadeCounter / 10.0;
for (var j in sMarket) {
var oldQuantity = oMarket[j];
var proposedQuantity = sMarket[j].quantity;
var diff = Math.round((proposedQuantity - oldQuantity) * percentageMultiplier);
station.setMarketQuantity(j, oldQuantity + diff);
}
}
}
}
this.playerWillSaveGame = function() {
missionVariables.marketCooldown = JSON.stringify(this.$storedMarkets);
}
//clear all saved markets on galactic jump.
this.playerEnteredNewGalaxy = function() {
delete this.$storedMarkets;
this.$storedMarkets = new Object();
}
|