Config/script.js |
this.name = "mo-commodity_markets";
this.author = "spara";
this.copyright = "2013-2014 Mika SpÄra";
this.description = "Different prices for buying and selling in every station.";
this.version = "1.2.3";
this.licence = "CC BY-NC-SA 3.0";
this.startUp = function() {
//Market Observer is needed for showing buy prices in market.
if (!worldScripts["market_observer"]) {
for (prop in this.name) {
if (prop !== 'name' && prop !== 'version') delete worldScripts[this.name][prop];
}
log(this.name, "Install marketObserver for mO-Commodity Markets to work. Exiting");
player.consoleMessage("Install marketObserver for mO-Commodity Markets to work. Exiting.", 10);
}
this.$commodities = ["food","textiles","radioactives","slaves","liquor_wines","luxuries","narcotics","computers","machinery","alloys","firearms","furs","minerals","gold","platinum","gem_stones","alien_items"];
this.$fixedPrices = false;//flag for prices' status
//restore pricefactors
if (missionVariables.commodityMarketsPriceFactors) {
this.$priceFactor = JSON.parse(missionVariables.commodityMarketsPriceFactors);
}
else {
//init individual pricefactors
this.$priceFactor = [];
var i;
for (i = 0; i < 17; i++) {
this.$priceFactor.push(1.02 + 0.03 * Math.random());
}
}
this.$createPrices();//init arrays
}
//set prices and start timer for fluctuating prices
this.shipDockedWithStation = function(station) {
this.$renewPriceFactors();
this.$createPrices();
}
this.shipWillLaunchFromStation = function(station) {
//restore prices to averages when launching
if (this.$fixedPrices) this.$restorePrices();
}
this.guiScreenChanged = function(to, from) {
if (!player.ship.docked) return;
//fix prices when switching to market screen
if (guiScreen === "GUI_SCREEN_MARKET" && !this.$fixedPrices) {
for (i = 0; i < 17; i++)
player.ship.dockedStation.setMarketPrice(this.$commodities[i], this.$sellPrices[i]);
this.$fixedPrices = true;
//make a list of scripts to notify about buying and selling
this.$collectNotifyScripts();
return;
}
//restore median prices when leaving market screen
if (guiScreen !== "GUI_SCREEN_MARKET" && this.$fixedPrices) {
this.$restorePrices();
}
}
//restore original prices
this.$restorePrices = function(){
for (i = 0; i < 17; i++)
player.ship.dockedStation.setMarketPrice(this.$commodities[i], this.$originalPrices[i]);
this.$fixedPrices = false;
}
//create arrays for original, buy and sell prices
this.$createPrices = function() {
var i, newPrice, commodity;
this.$buyPrices = [];
this.$sellPrices = [];
this.$originalPrices = [];
for (i = 0; i < 17; i ++) {
commodity = this.$commodities[i];
var priceFactor = this.$priceFactor[i];
this.$originalPrices.push(player.ship.dockedStation.market[commodity].price);
//buy prices
newPrice = priceFactor * this.$originalPrices[i];
if (newPrice > 1020) newPrice = 1020;
this.$buyPrices.push(newPrice);
//sell prices
newPrice = this.$originalPrices[i] / priceFactor;
this.$sellPrices.push(newPrice);
}
}
//5% change for individual commodity prices to fluctuate
this.$renewPriceFactors = function() {
for (i = 0; i < 17; i ++) {
if (Math.random() < 0.05)
this.$priceFactor[i] = 1.02 + 0.03 * Math.random();
}
}
//handle buying
this.$playerBoughtCargo = function(commodity, units, price) {
//return, if original prices
if (!this.$fixedPrices) return [units, price];
//return, if price is correct to circumvent continuous looping
var commIndex = this.$commodities.indexOf(commodity);
if (price === this.$buyPrices[commIndex]) return [units, price];
//reverse buying with sell prices
player.ship.dockedStation.setMarketQuantity(commodity, (player.ship.dockedStation.market[commodity].quantity + units));
player.credits += (units * price) / 10;
player.ship.manifest[commodity] -= units;
//notify other scripts about reversing
//this.$notifySell(commodity, units, price);
//buy again with correct prices
var unitsBought = 0;
while (unitsBought < units && player.credits >= Math.round(this.$buyPrices[commIndex])/10) {
player.ship.dockedStation.setMarketQuantity(commodity, (player.ship.dockedStation.market[commodity].quantity - 1));
player.credits -= this.$buyPrices[commIndex] / 10;
player.ship.manifest[commodity] += 1;
unitsBought++;
}
//notify other scripts about buying
/*
if (unitsBought > 0) {
this.$notifyBuy(commodity, unitsBought, this.$buyPrices[commIndex]);
}
*/
return [unitsBought, this.$buyPrices[commIndex]];
}
//make a list of scripts to notify about buying and selling
this.$collectNotifyScripts = function() {
this.$buyScripts = [];
this.$sellScripts = [];
for (i = 0; i < worldScriptNames.length; i++) {
scriptName = worldScriptNames[i];
if (worldScripts[scriptName] && scriptName !== "market_observer" && worldScripts[scriptName].playerSoldCargo)
this.$sellScripts.push(scriptName);
if (worldScripts[scriptName] && scriptName !== this.name && scriptName !== "market_observer" && worldScripts[scriptName].playerBoughtCargo)
this.$buyScripts.push(scriptName);
}
}
//notify other scripts about selling
this.$notifySell = function(commodity, units, price) {
var i, scriptName;
this.$notifyScripts = [];
for (i = 0; i < this.$sellScripts.length; i++) {
scriptName = this.$sellScripts[i];
if (worldScripts[scriptName].playerSoldCargo)
worldScripts[scriptName].playerSoldCargo(commodity, units, price);
}
}
//notify other scripts about buying
this.$notifyBuy = function(commodity, units, price) {
var i, scriptName;
this.$notifyScripts = [];
for (i = 0; i < this.$buyScripts.length; i++) {
scriptName = this.$buyScripts[i];
if (worldScripts[scriptName].playerBoughtCargo)
worldScripts[scriptName].playerBoughtCargo(commodity, units, price);
}
}
//save pricefactors
this.playerWillSaveGame = function() {
missionVariables.commodityMarketsPriceFactors = JSON.stringify(this.$priceFactor);
}
|