Advertisement
Maliki79

MalVariableBuySellRates

Jun 14th, 2016
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Maliki's Variable Buy and Sell Rates
  3. // MalVariableBuySellRates.js
  4. // version 1.2a
  5. //=============================================================================
  6. /*:  
  7.  * @plugindesc ver1.2a - Allows developers to set group item buy and sell costs.  
  8.  * @author Maliki79
  9.  *
  10.  * @help Allows developers to set group item buy and sell costs.  
  11.  *
  12.  * To use item group rates, item group tags must be created in your item notetags.
  13.  * <itemGroup: x>
  14.  * with x being the number of the group you wish to add this item to.
  15.  * (This works for items, weapons, and armors)
  16.  *
  17.  * To set group rates, use the Script Call:
  18.  * $gameParty.setShopRates(x, y, z);
  19.  * With x being the group number, y being the buying amount and z being the cost amount.
  20.  * y and z will be a percentage integer.
  21.  * (0 is the group default and cannot be set.)
  22.  *
  23.  * Example: $gameParty.setShopRates(1, 120, 50);
  24.  * This will set all items in group one to be bought from shops at 120% regular cost and sold at 50% regular cost.
  25.  * (You can use any call that will return a number, including variables!)
  26.  * Using a setShopRate call with the same group number will overwrite the older one.
  27.  *
  28.  * You can also set regional buy/sell rates which will be applied to all items.
  29.  * $gameParty.setRegionRate(x, y);
  30.  * With x being the buying rate and y being the selling rate.
  31.  * Note that this setting will after ALL stores the player encounters untill a new region rate is set.
  32.  * Also note that both rates can be active at any time.
  33.  * Group rates are multiplied first, followed by the regional rate.
  34.  *
  35.  * You can reset ALL Shop Rates by using the Script Call:
  36.  * $gameParty.resetShopRates();
  37.  * (You can also use this call if using this Plugin from an old save to initialize it.)
  38.  *
  39.  * When in the Shop Scene, the Names and prices have been given colors to help determine positive and
  40.  * negative rates relative to the player.
  41.  *
  42.  * When buying, the cost of items recieving a discount will appear in green text.
  43.  * Items higher than the regular cost will have that value appear in red.
  44.  * Similarly, when selling, the item name will appear green if the player is getting a good deal selling
  45.  * above the normal selling rate and red if below.
  46.  *
  47.  * The rate considered "normal" can be changed with the script call:
  48.  * $gameParty.setNormalSellRate(x);
  49.  * where x is the rate by which sold items are considered to be normal.
  50.  * (It defaults to 50.)
  51.  *
  52.  * If changed, this rate will NOT return to default when reseting other rates.
  53.  * Because of this, you must make at least one normal sell rate setting call when using this with old saves.
  54.  *
  55.  * Note that if an item's buying price is set to 0, it will NOT show up in the shops list for purchase.
  56.  * Also, if an item's sell price is 0, it will not be sellable to that shop either.
  57.  */
  58.  
  59.  Window_Base.prototype.price = function(item) {
  60.     var price = item.price || 0;
  61.     if (item.meta.itemGroup) {
  62.     price = price * this.setMulti($gameParty._shopNote, item.meta.itemGroup, false) / 100;
  63.     } else {
  64.     price = price * this.setMulti($gameParty._shopNote, 0, false) / 100;
  65.     }
  66.     price = price * $gameParty._regionSellRate / 100;
  67.     if (price < 0) price = 0;
  68.     return Math.floor(price);
  69. };
  70.  
  71.  Window_ShopBuy.prototype.price = function(item) {
  72.     var price = this._price[this._data.indexOf(item)] || 0;
  73.     if (item.meta.itemGroup) {
  74.     price = price * this.setMulti($gameParty._shopNote, item.meta.itemGroup, true) / 100;
  75.     } else {
  76.     price = price * this.setMulti($gameParty._shopNote, 0, true) / 100;
  77.     }
  78.     price = price * $gameParty._regionBuyRate / 100;
  79.     if (price < 0) price = 0;
  80.     return Math.floor(price);
  81. };
  82.  
  83.  Window_ShopSell.prototype.price = function(item) {
  84.     if (item) {
  85.     var price = item.price || 0;
  86.     if (item.meta.itemGroup) {
  87.     price = price * this.setMulti($gameParty._shopNote, item.meta.itemGroup, false) / 100;
  88.     } else {
  89.     price = price * this.setMulti($gameParty._shopNote, 0, false) / 100;
  90.     }
  91.     price = price * $gameParty._regionSellRate / 100;
  92.     if (price < 0) price = 0;
  93.     return Math.floor(price);
  94.     }
  95. };
  96.  
  97.  Window_ShopBuy.prototype.protoPrice = function(item) {
  98.     var price = item.price || 0;
  99.     if (item.meta.itemGroup) {
  100.     price = price * this.setMulti($gameParty._shopNote, item.meta.itemGroup, true) / 100;
  101.     } else {
  102.     price = price * this.setMulti($gameParty._shopNote, 0, true) / 100;
  103.     }
  104.     price = price * $gameParty._regionBuyRate / 100;
  105.     if (price < 0) price = 0;
  106.     return Math.floor(price);
  107. };
  108.  
  109. Window_ShopSell.prototype.isEnabled = function(item) {
  110.     return item && this.price(item) > 0;
  111. };
  112.  
  113. Window_ShopBuy.prototype.makeItemList = function() {
  114.     this._data = [];
  115.     this._price = [];
  116.     this._shopGoods.forEach(function(goods) {
  117.         var item = null;
  118.         switch (goods[0]) {
  119.         case 0:
  120.             item = $dataItems[goods[1]];
  121.             break;
  122.         case 1:
  123.             item = $dataWeapons[goods[1]];
  124.             break;
  125.         case 2:
  126.             item = $dataArmors[goods[1]];
  127.             break;
  128.         }
  129.         if (item && this.protoPrice(item) > 0) {
  130.             this._data.push(item);
  131.             this._price.push(goods[2] === 0 ? item.price : goods[3]);
  132.         }
  133.     }, this);
  134. };
  135.  
  136. var MalShopPartyInit = Game_Party.prototype.initialize
  137. Game_Party.prototype.initialize = function() {
  138.     MalShopPartyInit.call(this);
  139.     this._shopNote = [];
  140.     this._regionBuyRate  = 100;
  141.     this._regionSellRate = 50;
  142.     this._normalSellRate = 50;
  143.     this._shopNote.push('<buysell: ' + 0 + ',' + 100 + ',' + 100 + ',>');
  144. };
  145.  
  146. Game_Party.prototype.setShopRates = function(x, y, z) {
  147. if (x == 0) return;
  148. for(var i = 0; i < this._shopNote.length; ++i){
  149.         if (this._shopNote[i].indexOf('<buysell: ' + x) > -1) {
  150.         this._shopNote[i] = '<buysell: ' + x + ',' + y + ',' + z + ',>';
  151.         return;
  152.         }
  153.         }
  154. this._shopNote.push('<buysell: ' + x + ',' + y + ',' + z + ',>');
  155. }
  156.  
  157. Game_Party.prototype.setNormalSellRate = function(x) {
  158. this._normalSellRate = x;
  159. }
  160.  
  161. Game_Party.prototype.resetShopRates = function() {
  162. this._shopNote = [];
  163. this._shopNote.push('<buysell: ' + 0 + ',' + 100 + ',' + 100 + ',>');
  164. this._regionBuyRate  = 100;
  165. this._regionSellRate = this._normalSellRate;
  166. }
  167.  
  168. Game_Party.prototype.setRegionRate = function(x, y) {
  169. this._regionBuyRate  = x;
  170. this._regionSellRate = y;
  171. }
  172.  
  173. Window_ShopBuy.prototype.setMulti = function(note, set, buy){
  174. //for buying price
  175.     var num = 100;
  176.     var buy = buy;
  177.     var objele = Number(set);
  178.     var noteread = note;
  179.     for(var i = 0; i < noteread.length; ++i){
  180.         var notereg = noteread[i].split("<buysell: ");
  181.         var match = notereg[1].split(",");
  182.         var bonuselem = Number(match[0]);
  183.         var bonusvalue1 = Number(match[1]);
  184.         if (isNaN(bonusvalue1)) bonusvalue1 = 100;
  185.         var bonusvalue2 = Number(match[2]);
  186.         if (objele == bonuselem && buy == true) num = bonusvalue1;
  187.     }
  188.     return num;
  189. }
  190.  
  191. Window_ShopSell.prototype.setMulti = function(note, set, buy){
  192. //for selling price
  193. var num = 100;
  194.     var buy = buy;
  195.     var objele = Number(set);
  196.     var noteread = note;
  197.     for(var i = 0; i < noteread.length; ++i){
  198.         var notereg = noteread[i].split("<buysell: ");
  199.         var match = notereg[1].split(",");
  200.         var bonuselem = Number(match[0]);
  201.         var bonusvalue1 = Number(match[1]);
  202.         var bonusvalue2 = Number(match[2]);
  203.         if (isNaN(bonusvalue2)) bonusvalue2 = 50;
  204.         if (objele == bonuselem && buy == false) num = bonusvalue2;
  205.     }
  206.     return num;
  207. }
  208.  
  209. Scene_Shop.prototype.setMulti = function(note, set, buy){
  210. //for selling price
  211. var num = 100;
  212.     var buy = buy;
  213.     var objele = Number(set);
  214.     var noteread = note;
  215.     for(var i = 0; i < noteread.length; ++i){
  216.         var notereg = noteread[i].split("<buysell: ");
  217.         var match = notereg[1].split(",");
  218.         var bonuselem = Number(match[0]);
  219.         var bonusvalue1 = Number(match[1]);
  220.         var bonusvalue2 = Number(match[2]);
  221.         if (isNaN(bonusvalue2)) bonusvalue2 = 50;
  222.         if (objele == bonuselem && buy == false) num = bonusvalue2;
  223.     }
  224.     return num;
  225. }
  226.  
  227. Window_Base.prototype.setMulti = function(note, set, buy){
  228. //for selling price
  229. var num = 100;
  230.     var buy = buy;
  231.     var objele = Number(set);
  232.     var noteread = note;
  233.     for(var i = 0; i < noteread.length; ++i){
  234.         var notereg = noteread[i].split("<buysell: ");
  235.         var match = notereg[1].split(",");
  236.         var bonuselem = Number(match[0]);
  237.         var bonusvalue1 = Number(match[1]);
  238.         var bonusvalue2 = Number(match[2]);
  239.         if (isNaN(bonusvalue2)) bonusvalue2 = 50;
  240.         if (objele == bonuselem && buy == false) num = bonusvalue2;
  241.     }
  242.     return num;
  243. }
  244.  
  245. Scene_Shop.prototype.sellingPrice = function() {
  246.     if (this._item.meta.itemGroup){
  247.     return Math.floor(this._item.price * this.setMulti($gameParty._shopNote, this._item.meta.itemGroup, false) / 100 * $gameParty._regionSellRate / 100);
  248.     } else {
  249.         return Math.floor(this._item.price * this.setMulti($gameParty._shopNote, 0, false) / 100 * $gameParty._regionSellRate / 100);
  250.    }
  251. };
  252.  
  253. Window_Base.prototype.sellingPrice = function(item) {
  254.     if (item.meta.itemGroup) return Math.floor(item.price * this.setMulti($gameParty._shopNote, item.meta.itemGroup, false) / 100 * $gameParty._regionSellRate / 100);
  255.     return Math.floor(item.price * this.setMulti($gameParty._shopNote, 0, false) / 100 * $gameParty._regionSellRate / 100);  
  256. };
  257.  
  258. Window_ShopBuy.prototype.drawItem = function(index) {
  259.     var item = this._data[index];
  260.     var rect = this.itemRect(index);
  261.     var priceWidth = 96;
  262.     rect.width -= this.textPadding();
  263.     this.changePaintOpacity(this.isEnabled(item));
  264.     this.resetTextColor();
  265.     this.drawItemName(item, rect.x, rect.y, rect.width - priceWidth);
  266.     if (this.price(item) > this._price[this._data.indexOf(item)]) {
  267.     this.changeTextColor(this.powerDownColor());
  268.     }
  269.     if (this.price(item) < this._price[this._data.indexOf(item)]) {
  270.     this.changeTextColor(this.powerUpColor());
  271.     }
  272.     this.drawText(this.price(item), rect.x + rect.width - priceWidth,
  273.                   rect.y, priceWidth, 'right');
  274.     this.changePaintOpacity(true);
  275. };
  276.  
  277. var MavSceneShopinit = Scene_Shop.prototype.initialize
  278. Scene_Shop.prototype.initialize = function() {
  279.     MavSceneShopinit.call(this);
  280.     this._buysellmode = 0;
  281. };
  282.  
  283. Window_Base.prototype.drawItemName = function(item, x, y, width) {
  284.     width = width || 312;
  285.     if (item) {
  286.         var iconBoxWidth = Window_Base._iconWidth + 4;
  287.         this.resetTextColor();
  288.         this.drawIcon(item.iconIndex, x + 2, y + 2);
  289.         if (SceneManager._scene instanceof Scene_Shop){
  290.         if(SceneManager._scene._buysellmode && SceneManager._scene._buysellmode == 2) {
  291.         if (this.sellingPrice(item) > item.price * $gameParty._normalSellRate / 100) this.changeTextColor(this.powerUpColor());
  292.         if (this.sellingPrice(item) < item.price * $gameParty._normalSellRate / 100) this.changeTextColor(this.powerDownColor());
  293.         }
  294.         }
  295.         this.drawText(item.name, x + iconBoxWidth, y, width - iconBoxWidth);
  296.         this.resetTextColor();
  297.     }
  298. };
  299.  
  300. var MalShopBuy = Scene_Shop.prototype.commandBuy;
  301. Scene_Shop.prototype.commandBuy = function() {
  302.     this._buysellmode = 1;
  303.     MalShopBuy.call(this);
  304. };
  305.  
  306. var MalShopSell = Scene_Shop.prototype.commandSell;
  307. Scene_Shop.prototype.commandSell = function() {
  308.     this._buysellmode = 2;
  309.     MalShopSell.call(this);
  310. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement