Advertisement
Guest User

Untitled

a guest
Sep 4th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. this.name           = "mo-commodity_markets";
  2. this.author         = "spara";
  3. this.copyright      = "2013 Mika SpÃ¥ra";
  4. this.description    = "Different prices for buying and selling in every station.";
  5. this.version        = "1.1-deva";
  6. this.licence        = "CC BY-NC-SA 3.0";
  7.  
  8. //1.1 removed market fluctuating code. Too much complication and player probbly doesn't even notice it.
  9. //1.1 original prices array updated on every change to market screen to give room for price changing oxp's.
  10.  
  11. this.startUp = function() {
  12.    
  13.     //Market Observer is needed for showing buy prices in market.
  14.     if (!worldScripts["market_observer"]) {
  15.         for (prop in this.name) {
  16.             if (prop !== 'name' && prop !== 'version') delete worldScripts[this.name][prop];
  17.         }
  18.         log(this.name, "Install marketObserver for mO-Commodity Markets to work. Exiting");
  19.         player.consoleMessage("Install marketObserver for mO-Commodity Markets to work. Exiting.", 10);
  20.     }
  21.    
  22.     this.$commodities = ["food","textiles","radioactives","slaves","liquor_wines","luxuries","narcotics","computers","machinery","alloys","firearms","furs","minerals","gold","platinum","gem_stones","alien_items"];
  23.     this.$fixedPrices = false;//flag for prices' status
  24.    
  25.     if (missionVariables.commodityMarketsPriceFactors) {
  26.         this.$priceFactor = JSON.parse(missionVariables.commodityMarketsPriceFactors);
  27.     }
  28.     else {
  29.         this.$priceFactor = [];
  30.         var i;
  31.         //init individual pricefactors
  32.         for (i = 0; i < 17; i++) {         
  33.             this.$priceFactor.push(1.02 + 0.03 * Math.random());
  34.         }
  35.     }
  36.    
  37.     this.$createPrices();//init arrays
  38. }
  39.  
  40. //set prices and start timer for fluctuating prices
  41. this.shipDockedWithStation = function(station) {
  42.     this.$renewPriceFactors();
  43.     this.$createPrices();
  44. }
  45.  
  46. this.shipWillLaunchFromStation = function(station) {
  47.     //restore prices to averages when launching
  48.     if (this.$fixedPrices) this.$restorePrices();
  49. }
  50.  
  51. this.guiScreenChanged = function(to, from) {
  52.     if (!player.ship.docked) return;   
  53.     //fix prices when switching to market screen
  54.     if (guiScreen === "GUI_SCREEN_MARKET" && !this.$fixedPrices) {
  55.         for (i = 0; i < 17; i++)
  56.             player.ship.dockedStation.setMarketPrice(this.$commodities[i], this.$sellPrices[i]);
  57.         this.$fixedPrices = true;
  58.         //make a list of scripts to notify about buying and selling
  59.         this.$collectNotifyScripts();
  60.         return;
  61.     }
  62.     //restore median prices when leaving market screen
  63.     if (guiScreen !== "GUI_SCREEN_MARKET" && this.$fixedPrices) {
  64.         this.$restorePrices();
  65.     }  
  66. }
  67.  
  68. //restore original prices
  69. this.$restorePrices = function(){
  70.     for (i = 0; i < 17; i++)
  71.         player.ship.dockedStation.setMarketPrice(this.$commodities[i], this.$originalPrices[i]);
  72.     this.$fixedPrices = false;
  73. }
  74.  
  75. //create arrays for original, buy and sell prices
  76. this.$createPrices = function() {
  77.     var i, newPrice, commodity;
  78.     this.$buyPrices = [];
  79.     this.$sellPrices = [];
  80.     this.$originalPrices = [];
  81.     for (i = 0; i < 17; i ++) {
  82.         commodity = this.$commodities[i];
  83.         var priceFactor = this.$priceFactor[i];
  84.         this.$originalPrices.push(player.ship.dockedStation.market[commodity].price);
  85.         //buy prices
  86.         newPrice = priceFactor * this.$originalPrices[i];
  87.         if (newPrice > 1020) newPrice = 1020;
  88.         this.$buyPrices.push(newPrice);
  89.         //sell prices
  90.         newPrice = this.$originalPrices[i] / priceFactor;
  91.         this.$sellPrices.push(newPrice);
  92.     }
  93. }
  94.  
  95. //5% change for individual commodity prices to fluctuate
  96. this.$renewPriceFactors = function() {
  97.     for (i = 0; i < 17; i ++) {
  98.         if (Math.random() < 0.05)
  99.             this.$priceFactor[i] = 1.02 + 0.03 * Math.random();    
  100.     }
  101. }
  102.  
  103. //handle buying
  104. this.playerBoughtCargo = function(commodity, units, price) {
  105.     //return, if original prices
  106.     if (!this.$fixedPrices) return;
  107.     //return, if price is correct to circumvent continuous looping
  108.     var commIndex = this.$commodities.indexOf(commodity);
  109.     if (price === this.$buyPrices[commIndex]) return;
  110.     //reverse buying with sell prices
  111.     player.ship.dockedStation.setMarketQuantity(commodity, (player.ship.dockedStation.market[commodity].quantity + units));
  112.     player.credits += (units * price) / 10;
  113.     player.ship.manifest[commodity] -= units;
  114.     //notify other scripts about reversing
  115.     this.$notifySell(commodity, units, price);
  116.     //buy again with correct prices
  117.     var unitsBought = 0;
  118.     while (unitsBought < units && player.credits > this.$buyPrices[commIndex]/10) {
  119.         player.ship.dockedStation.setMarketQuantity(commodity, (player.ship.dockedStation.market[commodity].quantity - 1));
  120.         player.credits -= this.$buyPrices[commIndex] / 10;
  121.         player.ship.manifest[commodity] += 1;
  122.         unitsBought++;
  123.     }
  124.     //notify other scripts about buying
  125.     if (unitsBought > 0) {
  126.         this.$notifyBuy(commodity, unitsBought, this.$buyPrices[commIndex]);
  127.     }
  128. }
  129.  
  130. //make a list of scripts to notify about buying and selling
  131. this.$collectNotifyScripts = function() {
  132.     this.$buyScripts = [];
  133.     this.$sellScripts = [];
  134.     for (i = 0; i < worldScriptNames.length; i++) {
  135.         scriptName = worldScriptNames[i];
  136.         if (worldScripts[scriptName] && worldScripts[scriptName].playerSoldCargo)
  137.             this.$sellScripts.push(scriptName);
  138.         if (worldScripts[scriptName] && scriptName !== this.name && worldScripts[scriptName].playerBoughtCargo)
  139.             this.$buyScripts.push(scriptName);         
  140.     }  
  141. }
  142.  
  143. //notify other scripts about selling
  144. this.$notifySell = function(commodity, units, price) {
  145.     var i, scriptName;
  146.     this.$notifyScripts = [];
  147.     for (i = 0; i < this.$sellScripts.length; i++) {
  148.         scriptName = this.$sellScripts[i];
  149.         if (worldScripts[scriptName].playerSoldCargo)
  150.             worldScripts[scriptName].playerSoldCargo(commodity, units, price);
  151.     }  
  152. }
  153.  
  154. //notify other scripts about buying
  155. this.$notifyBuy = function(commodity, units, price) {
  156.     var i, scriptName;
  157.     this.$notifyScripts = [];
  158.     for (i = 0; i < this.$buyScripts.length; i++) {
  159.         scriptName = this.$buyScripts[i];
  160.         if (worldScripts[scriptName].playerBoughtCargo)
  161.             worldScripts[scriptName].playerBoughtCargo(commodity, units, price);
  162.     }  
  163. }
  164.  
  165. this.playerWillSaveGame = function() {
  166.     missionVariables.commodityMarketsPriceFactors = JSON.stringify(this.$priceFactor);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement