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

Expansion Commodity Markets

Content

Warnings

  1. Required Expansions mismatch between OXP Manifest and Expansion Manager at character position 0066 (DIGIT ZERO vs LATIN SMALL LETTER N)
  2. No version in dependency reference to oolite.oxp.spara.market_observer:null

Manifest

from Expansion Manager's OXP list from Expansion Manifest
Description Divides the Market Observer's market screen's 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 Market Observer's market screen's 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.mo_commodity_markets oolite.oxp.spara.mo_commodity_markets
Title Commodity Markets Commodity Markets
Category Mechanics Mechanics
Author spara spara
Version 1.2.3 1.2.3
Tags
Required Oolite Version
Maximum Oolite Version
Required Expansions
  • oolite.oxp.spara.market_observer:0
  • oolite.oxp.spara.market_observer:
  • Optional Expansions
    Conflict Expansions
    Information URL http://wiki.alioth.net/index.php/MarketObserver n/a
    Download URL https://wiki.alioth.net/img_auth.php/8/8a/Mo_commodity_markets_1.2.3.oxz n/a
    License CC-BY-NC-SA 3.0 CC-BY-NC-SA 3.0
    File Size n/a
    Upload date 1610873355

    Documentation

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

    Equipment

    This expansion declares no equipment. This may be related to warnings.

    Ships

    This expansion declares no ships. This may be related to warnings.

    Models

    This expansion declares no models. This may be related to warnings.

    Scripts

    Path
    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);
    }