Back to Index Page generated: Nov 12, 2024, 11:02:04 PM

Expansion Commodity Markets

Content

Manifest

from Expansion Manager's OXP list from Expansion Manifest
Description Divides the price column to sell price and buy price. Buy price is always a little higher than sell price. This adds a little bit of difficulty and realism to the game. Divides the price column to sell price and buy price. Buy price is always a little higher than sell price. This adds a little bit of difficulty and realism to the game.
Identifier oolite.oxp.spara.commodity_markets oolite.oxp.spara.commodity_markets
Title Commodity Markets Commodity Markets
Category Mechanics Mechanics
Author spara, Thargoid spara, Thargoid
Version 2.0.1 2.0.1
Tags
Required Oolite Version
Maximum Oolite Version
Required Expansions
Optional Expansions
Conflict Expansions
Information URL http://wiki.alioth.net/index.php/Commodity_Markets_OXP n/a
Download URL https://wiki.alioth.net/img_auth.php/7/79/Commodity_markets_2.0.1.oxz n/a
License CC-BY-NC-SA 4.0 CC-BY-NC-SA 4.0
File Size n/a
Upload date 1610873342

Documentation

Also read http://wiki.alioth.net/index.php/Commodity%20Markets

Equipment

This expansion declares no equipment.

Ships

This expansion declares no ships.

Models

This expansion declares no models.

Scripts

Path
Config/script.js
"use strict";

this.name           = "commodity_markets";
this.author         = "spara";
this.copyright      = "2013-2015 Mika SpÄra";
this.description    = "Different prices for buying and selling at every station.";
this.licence    	= "CC BY-NC-SA 4.0";

this.startUp = function() {
	
	//flag for prices' status
	this.$fixedPrices = false;
	
	//restore pricefactors
	if (missionVariables.commodityMarketsPriceFactors2) {
		this.$priceFactors = JSON.parse(missionVariables.commodityMarketsPriceFactors2);
	} 
	else {
		//init individual pricefactors
		this.$priceFactors = new Object();
		var i;
		var sMarket = system.mainStation.market;
		for (var i in sMarket)
			this.$priceFactors[i] = 1.02 + 0.03 * Math.random();
	}
	
	//these are for more efficient tabbing
	this.$tabs = [""];
	for (var i = 1; i < 10; i++) {
		var tab = " ";
		//keep on adding spaces until we go one space over.
		while (defaultFont.measureString(tab + " ") < i)
			tab += " ";
		this.$tabs.push(tab);
	}
	
	//calculate new prices
	this.$createPrices();
	
	//take control of playerBoughtCargo handlers from other scripts
	this.$injectOXPs();
}

//set prices on docking
this.shipDockedWithStation = function(station) {
	
	//5% change for an individual commodity price to fluctuate
	for (var i in station.market) {
		if (Math.random() < 0.05)
			this.$priceFactors[i] = 1.02 + 0.03 * Math.random();		
	}
	
	this.$createPrices();
}

//restore prices on launch
this.shipWillLaunchFromStation = function(station) {
	if (this.$fixedPrices) this.$restorePrices();
}

this.guiScreenWillChange = function(to, from) {
	//only touch prices when docked
	if (!player.ship.docked) return;
	
	//fix prices when switching to market screen
	if (to === "GUI_SCREEN_MARKET" && !this.$fixedPrices) {
		var dMarket = player.ship.dockedStation.market;
		for (var i in dMarket) {
			player.ship.dockedStation.setMarketPrice(i, this.$commodities[i][2]);
			if (!worldScripts["market_observer3"])
				manifest.setShortComment(i, this.$tabulate("", 7)+this.$formatCredits(this.$commodities[i][1]));
		}
		this.$fixedPrices = true;
	}
}

this.guiScreenChanged = function(to, from) {
	//restore original prices when leaving market screen
	if (guiScreen.indexOf("GUI_SCREEN_MARKET") === -1 && this.$fixedPrices)
		this.$restorePrices();
}

//restore original prices
this.$restorePrices = function(){
	var dMarket = player.ship.dockedStation.market;
	for (var i in dMarket) {
		if (!worldScripts["market_observer3"])
			manifest.setShortComment(i, "");
		player.ship.dockedStation.setMarketPrice(i, this.$commodities[i][0]);
	}
	this.$fixedPrices = false;
}

//create an object holding the prices
this.$createPrices = function() {
	this.$commodities = new Object();
	var dMarket = player.ship.dockedStation.market;
	for (var i in dMarket) {
		var priceFactor = this.$priceFactors[i];
		var originalPrice = player.ship.dockedStation.market[i].price;
		var buyPrice = Math.round(priceFactor * originalPrice);
		//if (buyPrice > 1020) buyPrice = 1020;//not needed anymore?
		var sellPrice = Math.round(originalPrice / priceFactor);
		this.$commodities[i] = [originalPrice, buyPrice, sellPrice];
	}
}

this.playerBoughtCargo = function(commodity, units, price) {

	var correctPrice = price;
	var correctUnits = units;

	//don't touch, if for some mysterious reason original prices
	if (this.$fixedPrices) {
		
		//calculate the correct values
		correctPrice = this.$commodities[commodity][1];
		var creditsBefore = player.credits + Math.round(units * price) / 10;
		while (correctPrice * correctUnits > creditsBefore * 10)
			correctUnits--;
		
		//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;
		
		//buy again with correct prices
		if (correctUnits > 0) {
			player.ship.dockedStation.setMarketQuantity(commodity, (player.ship.dockedStation.market[commodity].quantity - correctUnits));
			player.credits -= correctUnits * correctPrice / 10;
			player.ship.manifest[commodity] += correctUnits;
		}
	}
	
	//activate handlers in other scripts
	for (var i in this.$buyScripts) {
		var scriptName = this.$buyScripts[i];
		if (worldScripts[scriptName].$cmPlayerBoughtCargo) 
			worldScripts[scriptName].$cmPlayerBoughtCargo(commodity, correctUnits, correctPrice);
	}
}

//rename playerBoughtCargo handlers so that they won't be called by the game. calling is handled from this script
this.$injectOXPs = function() {
	this.$buyScripts = new Array();
	for (var i in worldScriptNames) {
		var scriptName = worldScriptNames[i];
		if (worldScripts[scriptName] && scriptName !== this.name  && worldScripts[scriptName].playerBoughtCargo) {
			this.$buyScripts.push(scriptName);
			worldScripts[scriptName].$cmPlayerBoughtCargo = worldScripts[scriptName].playerBoughtCargo;
			delete worldScripts[scriptName].playerBoughtCargo;
		}
	}
}

//save pricefactors
this.playerWillSaveGame = function() {
	missionVariables.commodityMarketsPriceFactors2 = JSON.stringify(this.$priceFactors);
}

//tabbing function
this.$tabulate = function(string, col) {

	//start with spaces
	var tab = this.$tabs[Math.floor(col - defaultFont.measureString(string))];
	while (defaultFont.measureString(string + tab + " ") < col) {
		tab += " ";
	}
	
	//fine tune with hair spaces
	var hairSpace = String.fromCharCode(31);
	while (defaultFont.measureString(string + tab + hairSpace) < col) {
		tab += hairSpace;
	}
	
	return(string + tab);
}

//helper to format credits on screen
this.$formatCredits = function(credits) {
	if (credits < 99.5) 
		return "    " + formatCredits(credits/10, true, false);
	else if (credits < 999.5)
		return "  " + formatCredits(credits/10, true, false);
	else return formatCredits(credits/10, true, false);
}