code:
//=============================================================================
// Yanfly Engine Plugins - Shop Menu Extension Plugin - More Currencies
// YEP_X_MoreCurrencies.js
//=============================================================================

var Imported = Imported || {};
Imported.YEP_X_MoreCurrencies = true;

var Yanfly = Yanfly || {};
Yanfly.MC = Yanfly.MC || {};
Yanfly.MC.version = 1.05;

//=============================================================================
 /*:
 * @plugindesc v1.05 (Requires YEP_ShopMenuCore.js) This plugin adds
 * functionality for your shops to have multiple currencies.
 * @author Yanfly Engine Plugins
 *
 * @param ---General---
 * @default
 *
 * @param Currency Padding
 * @parent ---General---
 * @type number
 * @min 0
 * @desc The padding used for multiple currencies in pixels.
 * @default 10
 *
 * @param Font Size
 * @parent ---General---
 * @type number
 * @min 1
 * @desc The font size used for currency in shops.
 * Default: 28
 * @default 20
 *
 * @param ---Proxies---
 * @default
 *
 * @param Copy Name
 * @parent ---Proxies---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Copy name of original item for proxy items?
 * NO - false     YES - true
 * @default true
 *
 * @param Copy Icon
 * @parent ---Proxies---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Copy icon of original item for proxy items?
 * NO - false     YES - true
 * @default true
 *
 * @param Copy Help
 * @parent ---Proxies---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Copy help descriptions of original item for proxy items?
 * NO - false     YES - true
 * @default true
 *
 * @param Copy Traits
 * @parent ---Proxies---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Copy traits of original item for proxy items?
 * NO - false     YES - true
 * @default true
 *
 * @param Copy Parameters
 * @parent ---Proxies---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Copy stat parameters of original item for proxy items?
 * NO - false     YES - true
 * @default true
 *
 * @param ---Aftermath---
 * @default
 *
 * @param Gold as Item
 * @parent ---Aftermath---
 * @type boolean
 * @on YES
 * @off NO
 * @desc Do you wish to draw gold as an item drop?
 * NO - false     YES - true
 * @default true
 *
 * @param Gold Icon
 * @parent ---Aftermath---
 * @type number
 * @min 0
 * @desc The icon used for gold. If you are using the Core Engine,
 * this will automatically change to the icon used there.
 * @default 314
 *
 * @help
 * ============================================================================
 * Introduction
 * ============================================================================
 *
 * This plugin requires YEP_ShopMenuCore.
 * Make sure this plugin is located under YEP_ShopMenuCore in the plugin list.
 *
 * As an extension of the Shop Menu Core, this plugin enables you to have items
 * cost multiple currencies or different currencies using variables and/or
 * other items themselves.
 *
 * ============================================================================
 * Instructions - Variables as Currency
 * ============================================================================
 *
 * If you're planning to use variables as currency (and you should!), you can
 * set them up in a way to have them show an icon. Name your currency variable
 * as such:
 *
 * \i[x]Variable Name
 *
 * When displayed in shops, it will show the icon and the name. However, if you
 * want it to display only the icon and/or exclude some text, place the text
 * inside a << and >> bracket like such:
 *
 * \i[x]<<Variable Name>>
 *
 * That way, only the icon will be shown while all the next inside the << >>
 * will be hidden.
 *
 * ============================================================================
 * Instructions - Proxies
 * ============================================================================
 *
 * Perhaps in your game, you would like for the player to have multiple ways of
 * purchasing an item through one currency or another. However, inputting the
 * costs for all of the options makes the item cost every single aspect. But,
 * what you can do is make proxies of these items. For example, in the database
 * will will have the following:
 *
 * Potion           - 50 Gold
 * Potion (Proxy A) - 5 Gems
 * Potion (Proxy B) - 3 Jewels
 *
 * Placing all three items in your shop normally would result in three types of
 * potions that do not stack into the same category. However, by using this
 * proxy notetag, you can make them all attribute to the same Potion item:
 *
 *   <Proxy Buy: x>
 *   <Proxy Buy: name>
 *   If you're using this item as a proxy for another item (so you can set a
 *   different cost for the item), this item will end up representing item x,
 *   weapon x, or armor x of the same type. If you're using a named notetag and
 *   you have multiple entries in your database with the same name, priority
 *   will be given to the item, weapon, or armor with the highest ID.
 *
 * If Proxy A and B link back to the main Potion, then whenever the player buys
 * the Proxies in the shop, they will link back to the main Potion, making it
 * possible to buy the same item using different currencies.
 *
 * ============================================================================
 * Notetags
 * ============================================================================
 *
 * You can use the following notetags to adjust various properties for more
 * currencies for your items, weapons, and armors.
 *
 * Item, Weapon, and Armor Notetags:
 *
 *   <Variable x Buy Price: y>
 *   This sets the currency of this item, weapon, or armor to variable x with
 *   a buy price of y. You can insert multiples of this notetag to have more
 *   than one variable cost for that item, weapon, or armor.
 *
 *   <Variable x Sell Price: y>
 *   This makes it that when selling this item, weapon, or armor, the player
 *   will gain variable x in y amount. You can insert multiples of this
 *   notetag to have more than one variable cost for that item, weapon, or
 *   armor.
 *
 *   <Item x Buy Price: y>
 *   <Item name Buy Price: y>
 *   This sets the currency of this item, weapon, or armor to cost item x (or
 *   the named item) with a buy price of y. You can insert multiples of this
 *   notetag to have more than one item cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Item x Sell Price: y>
 *   <Item name Sell Price: y>
 *   When selling this item, weapon, or armor, the player will get item x (or
 *   the named item) with a quantity of y. You can insert multiples of this
 *   notetag to have more than one item cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Weapon x Buy Price: y>
 *   <Weapon name Buy Price: y>
 *   This sets the currency of this item, weapon, or armor to cost weapon x (or
 *   the named weapon) with a buy price of y. You can insert multiples of this
 *   notetag to have more than one weapon cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Weapon x Sell Price: y>
 *   <Weapon name Sell Price: y>
 *   When selling this item, weapon, or armor, the player will get weapon x (or
 *   the named weapon) with a quantity of y. You can insert multiples of this
 *   notetag to have more than one weapon cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Armor x Buy Price: y>
 *   <Armor name Buy Price: y>
 *   This sets the currency of this item, weapon, or armor to cost armor x (or
 *   the named armor) with a buy price of y. You can insert multiples of this
 *   notetag to have more than one armor cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Armor x Sell Price: y>
 *   <Armor name Sell Price: y>
 *   When selling this item, weapon, or armor, the player will get armor x (or
 *   the named armor) with a quantity of y. You can insert multiples of this
 *   notetag to have more than one armor cost for that item, weapon, or armor.
 *   If you're using Item Core, this will not work on independent items. If you
 *   are using the named version of the notetag and have multiple items in your
 *   database of the same name, priority will be given to the item with the
 *   highest ID.
 *
 *   <Proxy Buy: x>
 *   <Proxy Buy: name>
 *   If you're using this item as a proxy for another item (so you can set a
 *   different cost for the item), this item will end up representing item x,
 *   weapon x, or armor x of the same type. If you're using a named notetag and
 *   you have multiple entries in your database with the same name, priority
 *   will be given to the item, weapon, or armor with the highest ID.
 *
 * ============================================================================
 * Changelog
 * ============================================================================
 *
 * Version 1.05:
 * - Updated for RPG Maker MV version 1.5.0.
 *
 * Version 1.04:
 * - Fixed a typo within the code. Please update Item Core, Item Disassemble,
 * Attachable Augments, and More Currencies if you are using those plugins.
 *
 * Version 1.03:
 * - Updated for RPG Maker MV version 1.1.0.
 *
 * Version 1.02:
 * - Fixed crash bugs from selling independent items from games saved before
 * the plugin was installed. These independent items will not contain any sell
 * data made from variables, items, weapons, or armors, but newly generated
 * ones will.
 *
 * Version 1.01:
 * - Fixed a graphical issue with gold display in the number window.
 *
 * Version 1.00:
 * - Finished Plugin!
 */
//=============================================================================

if (Imported.YEP_ShopMenuCore) {

//=============================================================================
// Parameter Variables
//=============================================================================

Yanfly.Parameters = PluginManager.parameters('YEP_X_MoreCurrencies');
Yanfly.Param = Yanfly.Param || {};

Yanfly.Param.MCCurrencyPadding = Number(Yanfly.Parameters['Currency Padding']);
Yanfly.Param.MCCurrencyFontSize = Number(Yanfly.Parameters['Font Size']);

Yanfly.Param.MCCopyName = eval(String(Yanfly.Parameters['Copy Name']));
Yanfly.Param.MCCopyIcon = eval(String(Yanfly.Parameters['Copy Icon']));
Yanfly.Param.MCCopyHelp = eval(String(Yanfly.Parameters['Copy Help']));
Yanfly.Param.MCCopyTraits = eval(String(Yanfly.Parameters['Copy Traits']));
Yanfly.Param.MCCopyParams = eval(String(Yanfly.Parameters['Copy Parameters']));

Yanfly.Param.MCGoldItem = eval(String(Yanfly.Parameters['Gold as Item']));
Yanfly.Param.MCGoldIcon = Number(Yanfly.Parameters['Gold Icon']);
if (Yanfly.Icon.Gold) Yanfly.Param.MCGoldIcon = Yanfly.Icon.Gold;

//=============================================================================
// DataManager
//=============================================================================

Yanfly.MC.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
  if (!Yanfly.MC.DataManager_isDatabaseLoaded.call(this)) return false;
  if (!Yanfly._loaded_YEP_X_MoreCurrencies) {
    this.processMCNotetagsI($dataItems);
    this.processMCNotetagsW($dataWeapons);
    this.processMCNotetagsA($dataArmors);
    this.processMCNotetags1($dataItems, 0);
    this.processMCNotetags1($dataWeapons, 1);
    this.processMCNotetags1($dataArmors, 2);
    Yanfly._loaded_YEP_X_MoreCurrencies = true;
  }
  return true;
};

DataManager.processMCNotetagsI = function(group) {
  if (Yanfly.ItemIdRef) return;
  Yanfly.ItemIdRef = {};
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    if (obj.name.length <= 0) continue;
    Yanfly.ItemIdRef[obj.name.toUpperCase()] = n;
  }
};

DataManager.processMCNotetagsW = function(group) {
  if (Yanfly.WeaponIdRef) return;
  Yanfly.WeaponIdRef = {};
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    if (obj.name.length <= 0) continue;
    Yanfly.WeaponIdRef[obj.name.toUpperCase()] = n;
  }
};

DataManager.processMCNotetagsA = function(group) {
  if (Yanfly.ArmorIdRef) return;
  Yanfly.ArmorIdRef = {};
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    if (obj.name.length <= 0) continue;
    Yanfly.ArmorIdRef[obj.name.toUpperCase()] = n;
  }
};

DataManager.processMCNotetags1 = function(group, itemType) {
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    var notedata = obj.note.split(/[\r\n]+/);

    obj.proxyBuy = 0;
    obj.variableBuyPrice = {};
    obj.variableBuyPrices = [];
    obj.variableSellPrice = {};
    obj.variableSellPrices = [];
    obj.itemBuyPrice = {};
    obj.itemBuyPrices = [];
    obj.itemSellPrice = {};
    obj.itemSellPrices = [];
    obj.weaponBuyPrice = {};
    obj.weaponBuyPrices = [];
    obj.weaponSellPrice = {};
    obj.weaponSellPrices = [];
    obj.armorBuyPrice = {};
    obj.armorBuyPrices = [];
    obj.armorSellPrice = {};
    obj.armorSellPrices = [];

    for (var i = 0; i < notedata.length; i++) {
      var line = notedata[i];
      if (line.match(/<PROXY BUY:[ ](\d+)>/i)) {
        obj.proxyBuy = parseInt(RegExp.$1);
        this.adjustProxyBuy(obj, itemType);
      } else if (line.match(/<PROXY BUY:[ ](.*)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        if (itemType === 0 && Yanfly.ItemIdRef[name]) {
          obj.proxyBuy = Yanfly.ItemIdRef[name];
        } else if (itemType === 1 && Yanfly.WeaponIdRef[name]) {
          obj.proxyBuy = Yanfly.WeaponIdRef[name];
        } else if (itemType === 2 && Yanfly.ArmorIdRef[name]) {
          obj.proxyBuy = Yanfly.ArmorIdRef[name];
        }
        this.adjustProxyBuy(obj, itemType);
      } else if (line.match(/<VARIABLE[ ](\d+)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.variableBuyPrices.contains(varId)) continue;
        obj.variableBuyPrices.unshift(varId);
        obj.variableBuyPrice[varId] = value;
      } else if (line.match(/<VARIABLE[ ](\d+)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.variableSellPrices.contains(varId)) continue;
        obj.variableSellPrices.unshift(varId);
        obj.variableSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<ITEM[ ](\d+)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.itemBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataItems[varId])) continue;
        }
        obj.itemBuyPrices.unshift(varId);
        obj.itemBuyPrice[varId] = value;
      } else if (line.match(/<ITEM[ ](.*)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.ItemIdRef[name];
        if (!varId) continue;
        if (obj.itemBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataItems[varId])) continue;
        }
        obj.itemBuyPrices.unshift(varId);
        obj.itemBuyPrice[varId] = value;
      } else if (line.match(/<ITEM[ ](\d+)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.itemSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataItems[varId])) continue;
        }
        obj.itemSellPrices.unshift(varId);
        obj.itemSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<ITEM[ ](.*)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.ItemIdRef[name];
        if (!varId) continue;
        if (obj.itemSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataItems[varId])) continue;
        }
        obj.itemSellPrices.unshift(varId);
        obj.itemSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<WEAPON[ ](\d+)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.weaponBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataWeapons[varId])) continue;
        }
        obj.weaponBuyPrices.unshift(varId);
        obj.weaponBuyPrice[varId] = value;
      } else if (line.match(/<WEAPON[ ](.*)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.WeaponIdRef[name];
        if (!varId) continue;
        if (obj.weaponBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataWeapons[varId])) continue;
        }
        obj.weaponBuyPrices.unshift(varId);
        obj.weaponBuyPrice[varId] = value;
      } else if (line.match(/<WEAPON[ ](\d+)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.weaponSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataWeapons[varId])) continue;
        }
        obj.weaponSellPrices.unshift(varId);
        obj.weaponSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<WEAPON[ ](.*)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.WeaponIdRef[name];
        if (!varId) continue;
        if (obj.weaponSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataWeapons[varId])) continue;
        }
        obj.weaponSellPrices.unshift(varId);
        obj.weaponSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<ARMOR[ ](\d+)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.armorBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataArmors[varId])) continue;
        }
        obj.armorBuyPrices.unshift(varId);
        obj.armorBuyPrice[varId] = value;
      } else if (line.match(/<ARMOR[ ](.*)[ ]BUY PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.ArmorIdRef[name];
        if (!varId) continue;
        if (obj.armorBuyPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataArmors[varId])) continue;
        }
        obj.armorBuyPrices.unshift(varId);
        obj.armorBuyPrice[varId] = value;
      } else if (line.match(/<ARMOR[ ](\d+)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var varId = parseInt(RegExp.$1);
        var value = parseInt(RegExp.$2);
        if (obj.armorSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataArmors[varId])) continue;
        }
        obj.armorSellPrices.unshift(varId);
        obj.armorSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      } else if (line.match(/<ARMOR[ ](.*)[ ]SELL PRICE:[ ](\d+)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var value = parseInt(RegExp.$2);
        var varId = Yanfly.ArmorIdRef[name];
        if (!varId) continue;
        if (obj.armorSellPrices.contains(varId)) continue;
        if (Imported.YEP_ItemCore) {
          if (DataManager.isIndependent($dataArmors[varId])) continue;
        }
        obj.armorSellPrices.unshift(varId);
        obj.armorSellPrice[varId] = value;
        obj.canSell = true;
        obj.cannotSell = false;
      }
    }
  }
};

DataManager.adjustProxyBuy = function(obj, itemType) {
    if (obj.proxyBuy <= 0) return;
    obj.nonIndependent = true;
    var id = obj.proxyBuy;
    var item = null;
    if (itemType === 0) item = $dataItems[id];
    if (itemType === 1) item = $dataWeapons[id];
    if (itemType === 2) item = $dataArmors[id];
    if (Yanfly.Param.MCCopyName) obj.name = item.name;
    if (Yanfly.Param.MCCopyIcon) obj.iconIndex = item.iconIndex;
    if (Yanfly.Param.MCCopyHelp) obj.description = item.description;
    if (Yanfly.Param.MCCopyParams && itemType === 0) {
      obj.effects = item.effects;
    }
    if (Yanfly.Param.MCCopyParams && itemType !== 0) {
      obj.params = item.params.slice();
    }
};

//=============================================================================
// Window_Base
//=============================================================================

Window_Base.prototype.getcurrencyGoldWidth = function(value) {
    if (Imported.YEP_CoreEngine) {
      return this.getCoreGoldWidth(value);
    } else {
      return this.getDefaultGoldWidth(value);
    }
};

Window_Base.prototype.getCoreGoldWidth = function(value) {
    this.resetTextColor();
    this.contents.fontSize = Yanfly.Param.GoldFontSize;
    if (this.usingGoldIcon(TextManager.currencyUnit)) {
      var ww = Window_Base._iconWidth;
    } else {
      var ww = this.textWidth(TextManager.currencyUnit);
    }
    ww += this.textWidth(Yanfly.Util.toGroup(value));
    ww += 4;
    if (this.usingGoldIcon(TextManager.currencyUnit)) ww += 2;
    return ww;
};

Window_Base.prototype.getDefaultGoldWidth = function(value) {
    this.resetTextColor();
    var ww = Math.min(80, this.textWidth(TextManager.currencyUnit));
    ww += this.textWidth(Yanfly.Util.toGroup(value));
    ww += 6;
    return ww;
};

Yanfly.MC.Window_Base_resetFontSettings =
    Window_Base.prototype.resetFontSettings;
Window_Base.prototype.resetFontSettings = function() {
    if (this._bypassResetText) return;
    Yanfly.MC.Window_Base_resetFontSettings.call(this);
};

Yanfly.MC.Window_Base_drawCurrencyValue =
    Window_Base.prototype.drawCurrencyValue;
Window_Base.prototype.drawCurrencyValue = function(value, unit, wx, wy, ww) {
    if (unit !== TextManager.currencyUnit) {
      this.drawAltCurrency(value, unit, wx, wy, ww);
    }
    Yanfly.MC.Window_Base_drawCurrencyValue.call(this, value, unit, wx, wy, ww);
};

Window_Base.prototype.drawAltCurrency = function(value, unit, wx, wy, ww) {
    this.resetTextColor();
    var iconIndex = 0;
    var unitText = '';
    if (DataManager.isItem(unit) || DataManager.isWeapon(unit) ||
    DataManager.isArmor(unit)) {
      var iconIndex = unit.iconIndex;
    } else if (unit.match(/VARIABLE[ ](\d+)/i)) {
      var name = $dataSystem.variables[parseInt(RegExp.$1)];
      if (name.match(/\\I\[(\d+)\]/i)) {
        var iconIndex = parseInt(RegExp.$1);
      }
      name = name.replace(/\\I\[(\d+)\]/gi, '');
      unitText = name.replace(/<<(.*?)>>/gi, '');
    }
    // Draw Text
    this.contents.fontSize = Yanfly.Param.MCCurrencyFontSize;
    if (unitText !== '') {
      this.changeTextColor(this.systemColor());
      this.drawText(unitText, wx, wy, ww, 'right');
      ww -= this.textWidth(unitText);
    }
    // Draw Icon
    if (iconIndex > 0) {
      if (unitText !== '') ww -= 6;
      ww -= Window_Base._iconWidth;
      this.drawIcon(iconIndex, wx + ww, wy + 2);
    }
    this.resetTextColor();
    this.drawText(Yanfly.Util.toGroup(value), wx, wy, ww, 'right');
    ww -= this.textWidth(Yanfly.Util.toGroup(value));
    this.resetFontSettings();
    return ww;
};

//=============================================================================
// Window_Gold
//=============================================================================

Yanfly.MC.Window_Gold_refresh = Window_Gold.prototype.refresh;
Window_Gold.prototype.refresh = function() {
    var x = this.textPadding();
    var ww = this.contents.width - this.textPadding() * 2;
    this.contents.clear();
    if (this.isDrawGoldCurrency()) {
      Yanfly.MC.Window_Gold_refresh.call(this);
      ww -= this.getcurrencyGoldWidth(this.value());
      ww -= Yanfly.Param.MCCurrencyPadding;
    }
    if (this._item) this.drawItemCurrencies(x, ww);
    this.resetFontSettings();
};

Window_Gold.prototype.isDrawGoldCurrency = function() {
    if (this._item) {
      var item = this._item;
      if (this._buyMode) {
        if (item.variableBuyPrices && item.variableBuyPrices.length > 0) {
          return item.price > 0;
        }
        if (item.itemBuyPrices && item.itemBuyPrices.length > 0) {
          return item.price > 0;
        }
        if (item.weaponBuyPrices && item.weaponBuyPrices.length > 0) {
          return item.price > 0;
        }
        if (item.armorBuyPrices && item.armorBuyPrices.length > 0) {
          return item.price > 0;
        }
      } else {
        if (item.variableSellPrices && item.variableSellPrices.length > 0) {
          return item.price > 0;
        }
        if (item.itemSellPrices && item.itemSellPrices.length > 0) {
          return item.price > 0;
        }
        if (item.weaponSellPrices && item.weaponSellPrices.length > 0) {
          return item.price > 0;
        }
        if (item.armorSellPrices && item.armorSellPrices.length > 0) {
          return item.price > 0;
        }
      }
    }
    return true;
};

Window_Gold.prototype.setItemBuy = function(item) {
    if (this._item === item) return;
    this._item = item;
    this._buyMode = true;
    this.refresh();
};

Window_Gold.prototype.setItemSell = function(item) {
    if (this._item === item) return;
    this._item = item;
    this._buyMode = false;
    this.refresh();
};

Window_Gold.prototype.drawItemCurrencies = function(wx, ww) {
    var item = this._item;
    var wy = 0;
    // Variables
    var currencies = this._buyMode ?
      item.variableBuyPrices : item.variableSellPrices;
    if (currencies) {
      var length = currencies.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var varId = currencies[i];
          var value = $gameVariables.value(varId);
          var unit = 'VARIABLE ' + varId;
          ww = this.drawAltCurrency(value, unit, wx, wy, ww);
          ww -= Yanfly.Param.MCCurrencyPadding;
        }
      }
    }
    // Armors
    currencies = this._buyMode ? item.armorBuyPrices : item.armorSellPrices;
    if (currencies) {
      length = currencies.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var costItem = $dataArmors[currencies[i]];
          var value = $gameParty.numItems(costItem);
          ww = this.drawAltCurrency(value, costItem, wx, wy, ww);
          ww -= Yanfly.Param.MCCurrencyPadding;
        }
      }
    }
    // Weapons
    currencies = this._buyMode ? item.weaponBuyPrices : item.weaponSellPrices;
    if (currencies) {
      length = currencies.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var costItem = $dataWeapons[currencies[i]];
          var value = $gameParty.numItems(costItem);
          ww = this.drawAltCurrency(value, costItem, wx, wy, ww);
          ww -= Yanfly.Param.MCCurrencyPadding;
        }
      }
    }
    // Items
    currencies = this._buyMode ? item.itemBuyPrices : item.itemSellPrices;
    if (currencies) {
      length = currencies.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var costItem = $dataItems[currencies[i]];
          var value = $gameParty.numItems(costItem);
          ww = this.drawAltCurrency(value, costItem, wx, wy, ww);
          ww -= Yanfly.Param.MCCurrencyPadding;
        }
      }
    }
};

//=============================================================================
// Window_ShopStatus
//=============================================================================

Yanfly.MC.Window_ShopStatus_drawPossession =
    Window_ShopStatus.prototype.drawPossession;
Window_ShopStatus.prototype.drawPossession = function(x, y) {
    var oldItem = this._item;
    if (this._item.proxyBuy) {
      var id = this._item.proxyBuy;
      if (DataManager.isItem(this._item)) this._item = $dataItems[id];
      if (DataManager.isWeapon(this._item)) this._item = $dataWeapons[id];
      if (DataManager.isArmor(this._item)) this._item = $dataArmors[id];
    }
    Yanfly.MC.Window_ShopStatus_drawPossession.call(this, x, y);
    this._item = oldItem;
};

Yanfly.MC.Window_ShopStatus_setItem = Window_ShopStatus.prototype.setItem;
Window_ShopStatus.prototype.setItem = function(item) {
    if (item && item.proxyBuy) {
      var id = item.proxyBuy;
      if (DataManager.isItem(item)) item = $dataItems[id];
      if (DataManager.isWeapon(item)) item = $dataWeapons[id];
      if (DataManager.isArmor(item)) item = $dataArmors[id];
    }
    Yanfly.MC.Window_ShopStatus_setItem.call(this, item);
};

//=============================================================================
// Window_ShopBuy
//=============================================================================

Yanfly.MC.Window_ShopBuy_drawBuyPrice = Window_ShopBuy.prototype.drawBuyPrice;
Window_ShopBuy.prototype.drawBuyPrice = function(item, rect) {
    if (!item) return;
    if (item.price > 0) {
      Yanfly.MC.Window_ShopBuy_drawBuyPrice.call(this, item, rect);
    }
    var ww = this.calculatePriceWidth(item, rect);
    ww = this.drawVariableBuyPrices(item, rect, ww);
    ww = this.drawArmorBuyPrices(item, rect, ww);
    ww = this.drawWeaponBuyPrices(item, rect, ww);
    ww = this.drawItemBuyPrices(item, rect, ww);
};

Window_ShopBuy.prototype.calculatePriceWidth = function(item, rect) {
    if (item.price <= 0) return rect.width;
    var ww = rect.width;
    ww -= this.getcurrencyGoldWidth(item.price);
    ww -= Yanfly.Param.MCCurrencyPadding;
    return ww;
};

Window_ShopBuy.prototype.drawVariableBuyPrices = function(item, rect, ww) {
    if (!item.variableBuyPrices) return ww;
    if (item.variableBuyPrices.length <= 0) return ww;
    var length = item.variableBuyPrices.length;
    for (var i = 0; i < length; ++i) {
      var varId = item.variableBuyPrices[i];
      ww = this.drawVariablePrice(item, varId, rect, ww);
    }
    return ww;
};

Window_ShopBuy.prototype.drawVariablePrice = function(item, varId, rect, ww) {
    var value = item.variableBuyPrice[varId];
    var unit = 'VARIABLE ' + varId;
    ww = this.drawAltCurrency(value, unit, rect.x, rect.y, ww);
    ww -= Yanfly.Param.MCCurrencyPadding;
    return ww;
};

Window_ShopBuy.prototype.drawItemBuyPrices = function(item, rect, ww) {
    if (!item.itemBuyPrices) return ww;
    if (item.itemBuyPrices.length <= 0) return ww;
    var length = item.itemBuyPrices.length;
    for (var i = 0; i < length; ++i) {
      var varId = item.itemBuyPrices[i];
      ww = this.drawItemBuyPrice(item, varId, rect, ww);
    }
    return ww;
};

Window_ShopBuy.prototype.drawItemBuyPrice = function(item, varId, rect, ww) {
    var value = item.itemBuyPrice[varId];
    var unit = $dataItems[varId];
    ww = this.drawAltCurrency(value, unit, rect.x, rect.y, ww);
    ww -= Yanfly.Param.MCCurrencyPadding;
    return ww;
};

Window_ShopBuy.prototype.drawWeaponBuyPrices = function(item, rect, ww) {
    if (!item.weaponBuyPrices) return ww;
    if (item.weaponBuyPrices.length <= 0) return ww;
    var length = item.weaponBuyPrices.length;
    for (var i = 0; i < length; ++i) {
      var varId = item.weaponBuyPrices[i];
      ww = this.drawWeaponBuyPrice(item, varId, rect, ww);
    }
    return ww;
};

Window_ShopBuy.prototype.drawWeaponBuyPrice = function(item, varId, rect, ww) {
    var value = item.weaponBuyPrice[varId];
    var unit = $dataWeapons[varId];
    ww = this.drawAltCurrency(value, unit, rect.x, rect.y, ww);
    ww -= Yanfly.Param.MCCurrencyPadding;
    return ww;
};

Window_ShopBuy.prototype.drawArmorBuyPrices = function(item, rect, ww) {
    if (!item.armorBuyPrices) return ww;
    if (item.armorBuyPrices.length <= 0) return ww;
    var length = item.armorBuyPrices.length;
    for (var i = 0; i < length; ++i) {
      var varId = item.armorBuyPrices[i];
      ww = this.drawArmorBuyPrice(item, varId, rect, ww);
    }
    return ww;
};

Window_ShopBuy.prototype.drawArmorBuyPrice = function(item, varId, rect, ww) {
    var value = item.armorBuyPrice[varId];
    var unit = $dataArmors[varId];
    ww = this.drawAltCurrency(value, unit, rect.x, rect.y, ww);
    ww -= Yanfly.Param.MCCurrencyPadding;
    return ww;
};

Window_ShopBuy.prototype.textWidthEx = function(text) {
    return this.drawTextEx(text, 0, this.contents.height);
};

Yanfly.MC.Window_ShopBuy_isEnabled = Window_ShopBuy.prototype.isEnabled;
Window_ShopBuy.prototype.isEnabled = function(item) {
    // Variables
    if (item && $gamePlayer.isDebugThrough()) return true;
    if (item && item.variableBuyPrices.length > 0) {
      var length = item.variableBuyPrices.length;
      for (var i = 0; i < length; ++i) {
        var varId = item.variableBuyPrices[i];
        var price = item.variableBuyPrice[varId];
        var value = $gameVariables.value(varId);
        if (value < price) return false;
      }
    }
    // Items
    if (item && item.itemBuyPrices.length > 0) {
      var length = item.itemBuyPrices.length;
      for (var i = 0; i < length; ++i) {
        var varId = item.itemBuyPrices[i];
        var price = item.itemBuyPrice[varId];
        var value = $gameParty.numItems($dataItems[varId]);
        if (value < price) return false;
      }
    }
    // Weapons
    if (item && item.weaponBuyPrices.length > 0) {
      var length = item.weaponBuyPrices.length;
      for (var i = 0; i < length; ++i) {
        var varId = item.weaponBuyPrices[i];
        var price = item.weaponBuyPrice[varId];
        var value = $gameParty.numItems($dataWeapons[varId]);
        if (value < price) return false;
      }
    }
    // Armors
    if (item && item.armorBuyPrices.length > 0) {
      var length = item.armorBuyPrices.length;
      for (var i = 0; i < length; ++i) {
        var varId = item.armorBuyPrices[i];
        var price = item.armorBuyPrice[varId];
        var value = $gameParty.numItems($dataArmors[varId]);
        if (value < price) return false;
      }
    }
    return Yanfly.MC.Window_ShopBuy_isEnabled.call(this, item);
};

Yanfly.MC.Window_ShopBuy_updateHelp = Window_ShopBuy.prototype.updateHelp;
Window_ShopBuy.prototype.updateHelp = function() {
    Yanfly.MC.Window_ShopBuy_updateHelp.call(this);
    if (SceneManager._scene._goldWindow) {
      SceneManager._scene._goldWindow.setItemBuy(this.item())
    }
};

//=============================================================================
// Window_ShopSell
//=============================================================================

Yanfly.MC.Window_ShopSell_updateHelp = Window_ShopSell.prototype.updateHelp;
Window_ShopSell.prototype.updateHelp = function() {
    Yanfly.MC.Window_ShopSell_updateHelp.call(this);
    if (SceneManager._scene._goldWindow) {
      SceneManager._scene._goldWindow.setItemSell(this.item())
    }
};

//=============================================================================
// Window_ShopNumber
//=============================================================================

Window_ShopNumber.prototype.isDrawGoldCurrency = function() {
    var item = this._item;
    if (item.variableBuyPrices && item.variableBuyPrices.length > 0) {
      return this._item.price > 0;
    }
    if (item.itemBuyPrices && item.itemBuyPrices.length > 0) {
      return this._item.price > 0;
    }
    return true;
};

Yanfly.MC.Window_ShopNumber_drawTotalCurrency =
    Window_ShopNumber.prototype.drawTotalCurrency;
Window_ShopNumber.prototype.drawTotalCurrency = function(ww, wy) {
    if (!this.isDrawGoldCurrency()) return;
    Yanfly.MC.Window_ShopNumber_drawTotalCurrency.call(this, ww, wy);
};

Yanfly.MC.Window_ShopNumber_drawTotalCost =
    Window_ShopNumber.prototype.drawTotalCost;
Window_ShopNumber.prototype.drawTotalCost = function(ww, wy) {
    if (!this.isDrawGoldCurrency()) return;
    Yanfly.MC.Window_ShopNumber_drawTotalCost.call(this, ww, wy);
};

Yanfly.MC.Window_ShopNumber_drawTotalAfter =
    Window_ShopNumber.prototype.drawTotalAfter;
Window_ShopNumber.prototype.drawTotalAfter = function(ww, wy) {
    if (!this.isDrawGoldCurrency()) return;
    Yanfly.MC.Window_ShopNumber_drawTotalAfter.call(this, ww, wy);
};

Yanfly.MC.Window_ShopNumber_drawTotalPrice =
    Window_ShopNumber.prototype.drawTotalPrice;
Window_ShopNumber.prototype.drawTotalPrice = function() {
    Yanfly.MC.Window_ShopNumber_drawTotalPrice.call(this);
    var ww = this.contents.width - this.textPadding();
    var wy = this.itemY();
    if (this.isDrawGoldCurrency()) {
      var max = $gameParty.gold();
      max = Math.max(max, this._price * this._number);
      max = Math.max(max, this._price * this._number + $gameParty.gold());
      ww -= this.getcurrencyGoldWidth(max * 100);
    }
    ww = this.drawVariablePrices(ww, wy);
    ww = this.drawArmorCostPrices(ww, wy);
    ww = this.drawWeaponCostPrices(ww, wy);
    ww = this.drawItemCostPrices(ww, wy);
};

Window_ShopNumber.prototype.getVariableCurrency = function() {
    if (this.isSelling()) {
      var currencies = this._item.variableSellPrices;
    } else {
      var currencies = this._item.variableBuyPrices;
    }
    return currencies;
};

Window_ShopNumber.prototype.drawVariablePrices = function(ww, wy) {
    var currencies = this.getVariableCurrency();
    if (currencies) {
      var length = currencies.length;
      if (length <= 0) return ww;
      for (var i = 0; i < length; ++i) {
        var varId = currencies[i];
        ww = this.drawVariablePrice(ww, wy, varId);
        ww -= Yanfly.Param.MCCurrencyPadding;
      }
      this.resetFontSettings();
    }
    return ww;
};

Window_ShopNumber.prototype.drawVariablePrice = function(ww, wy, varId) {
    var fw = ww;
    var fy = wy + this.lineHeight() * 1;
    var value = $gameVariables.value(varId);
    var unit = 'VARIABLE ' + varId;
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    if (this.isSelling()) {
      var price = this._item.variableSellPrice[varId];
      value = this._number * price;
      value = '+' + value;
    } else {
      var price = this._item.variableBuyPrice[varId];
      value = this._number * price;
      value = Yanfly.Util.toGroup(value * -1);
    }
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    value = $gameVariables.value(varId);
    value += price * this._number * (this.isSelling() ? 1 : -1);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    return fw;
};

Window_ShopNumber.prototype.getItemCostCurrency = function() {
    if (this.isSelling()) {
      var currencies = this._item.itemSellPrices;
    } else {
      var currencies = this._item.itemBuyPrices;
    }
    return currencies;
};

Window_ShopNumber.prototype.drawItemCostPrices = function(ww, wy) {
    var currencies = this.getItemCostCurrency();
    if (currencies) {
      var length = currencies.length;
      if (length <= 0) return ww;
      for (var i = 0; i < length; ++i) {
        var varId = currencies[i];
        ww = this.drawItemCostPrice(ww, wy, varId);
        ww -= Yanfly.Param.MCCurrencyPadding;
      }
      this.resetFontSettings();
    }
    return ww;
};

Window_ShopNumber.prototype.drawItemCostPrice = function(ww, wy, varId) {
    var fw = ww;
    var fy = wy + this.lineHeight() * 1;
    var unit = $dataItems[varId];
    var value = $gameParty.numItems(unit);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    if (this.isSelling()) {
      var price = this._item.itemSellPrice[varId];
      value = this._number * price;
      value = '+' + value;
    } else {
      var price = this._item.itemBuyPrice[varId];
      value = this._number * price;
      value = Yanfly.Util.toGroup(value * -1);
    }
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    value = $gameParty.numItems(unit);
    value += price * this._number * (this.isSelling() ? 1 : -1);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    return fw;
};

Window_ShopNumber.prototype.getWeaponCostCurrency = function() {
    if (this.isSelling()) {
      var currencies = this._item.weaponSellPrices;
    } else {
      var currencies = this._item.weaponBuyPrices;
    }
    return currencies;
};

Window_ShopNumber.prototype.drawWeaponCostPrices = function(ww, wy) {
    var currencies = this.getWeaponCostCurrency();
    if (currencies) {
      var length = currencies.length;
      if (length <= 0) return ww;
      for (var i = 0; i < length; ++i) {
        var varId = currencies[i];
        ww = this.drawWeaponCostPrice(ww, wy, varId);
        ww -= Yanfly.Param.MCCurrencyPadding;
      }
      this.resetFontSettings();
    }
    return ww;
};

Window_ShopNumber.prototype.drawWeaponCostPrice = function(ww, wy, varId) {
    var fw = ww;
    var fy = wy + this.lineHeight() * 1;
    var unit = $dataWeapons[varId];
    var value = $gameParty.numItems(unit);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    if (this.isSelling()) {
      var price = this._item.weaponSellPrice[varId];
      value = this._number * price;
      value = '+' + value;
    } else {
      var price = this._item.weaponBuyPrice[varId];
      value = this._number * price;
      value = Yanfly.Util.toGroup(value * -1);
    }
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    value = $gameParty.numItems(unit);
    value += price * this._number * (this.isSelling() ? 1 : -1);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    return fw;
};

Window_ShopNumber.prototype.getArmorCostCurrency = function() {
    if (this.isSelling()) {
      var currencies = this._item.armorSellPrices;
    } else {
      var currencies = this._item.armorBuyPrices;
    }
    return currencies;
};

Window_ShopNumber.prototype.drawArmorCostPrices = function(ww, wy) {
    var currencies = this.getArmorCostCurrency();
    if (currencies) {
      var length = currencies.length;
      if (length <= 0) return ww;
      for (var i = 0; i < length; ++i) {
        var varId = currencies[i];
        ww = this.drawArmorCostPrice(ww, wy, varId);
        ww -= Yanfly.Param.MCCurrencyPadding;
      }
      this.resetFontSettings();
    }
    return ww;
};

Window_ShopNumber.prototype.drawArmorCostPrice = function(ww, wy, varId) {
    var fw = ww;
    var fy = wy + this.lineHeight() * 1;
    var unit = $dataArmors[varId];
    var value = $gameParty.numItems(unit);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    if (this.isSelling()) {
      var price = this._item.armorSellPrice[varId];
      value = this._number * price;
      value = '+' + value;
    } else {
      var price = this._item.armorBuyPrice[varId];
      value = this._number * price;
      value = Yanfly.Util.toGroup(value * -1);
    }
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    fy += this.lineHeight();
    value = $gameParty.numItems(unit);
    value += price * this._number * (this.isSelling() ? 1 : -1);
    fw = Math.min(fw, this.drawAltCurrency(value, unit, 0, fy, ww));
    return fw;
};

//=============================================================================
// Scene_Shop
//=============================================================================

Yanfly.MC.Scene_Shop_maxBuy = Scene_Shop.prototype.maxBuy;
Scene_Shop.prototype.maxBuy = function() {
    var max = Yanfly.MC.Scene_Shop_maxBuy.call(this);
    // Variables
    var length = this._item.variableBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.variableBuyPrices[i];
        var value = $gameVariables.value(varId);
        var price = this._item.variableBuyPrice[varId];
        max = Math.min(max, Math.floor(value / price));
      }
    }
    // Items
    var length = this._item.itemBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.itemBuyPrices[i];
        var value = $gameParty.numItems($dataItems[varId]);
        var price = this._item.itemBuyPrice[varId];
        max = Math.min(max, Math.floor(value / price));
      }
    }
    // Weapons
    var length = this._item.weaponBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.weaponBuyPrices[i];
        var value = $gameParty.numItems($dataWeapons[varId]);
        var price = this._item.weaponBuyPrice[varId];
        max = Math.min(max, Math.floor(value / price));
      }
    }
    // Armors
    var length = this._item.armorBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.armorBuyPrices[i];
        var value = $gameParty.numItems($dataArmors[varId]);
        var price = this._item.armorBuyPrice[varId];
        max = Math.min(max, Math.floor(value / price));
      }
    }
    return max;
};

Yanfly.MC.Scene_Shop_doBuyGold = Scene_Shop.prototype.doBuyGold;
Scene_Shop.prototype.doBuyGold = function(number) {
    Yanfly.MC.Scene_Shop_doBuyGold.call(this, number);
    // Variables
    var length = this._item.variableBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.variableBuyPrices[i];
        var value = $gameVariables.value(varId);
        var price = this._item.variableBuyPrice[varId];
        $gameVariables.setValue(varId, value - price * number);
      }
    }
    // Items
    var length = this._item.itemBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.itemBuyPrices[i];
        var value = $gameParty.numItems($dataItems[varId]);
        var price = this._item.itemBuyPrice[varId];
        $gameParty.gainItem($dataItems[varId], -price * number);
      }
    }
    // Weapons
    var length = this._item.weaponBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.weaponBuyPrices[i];
        var value = $gameParty.numItems($dataWeapons[varId]);
        var price = this._item.weaponBuyPrice[varId];
        $gameParty.gainItem($dataWeapons[varId], -price * number);
      }
    }
    // Armors
    var length = this._item.armorBuyPrices.length;
    if (length > 0) {
      for (var i = 0; i < length; ++i) {
        var varId = this._item.armorBuyPrices[i];
        var value = $gameParty.numItems($dataArmors[varId]);
        var price = this._item.armorBuyPrice[varId];
        $gameParty.gainItem($dataArmors[varId], -price * number);
      }
    }
};

Yanfly.MC.Scene_Shop_doBuyItem = Scene_Shop.prototype.doBuyItem;
Scene_Shop.prototype.doBuyItem = function(number) {
    var oldItem = this._item;
    if (this._item.proxyBuy) {
      var id = this._item.proxyBuy;
      if (DataManager.isItem(this._item)) this._item = $dataItems[id];
      if (DataManager.isWeapon(this._item)) this._item = $dataWeapons[id];
      if (DataManager.isArmor(this._item)) this._item = $dataArmors[id];
    }
    Yanfly.MC.Scene_Shop_doBuyItem.call(this, number);
    this._item = oldItem;
};

Yanfly.MC.Scene_Shop_onBuyCancel = Scene_Shop.prototype.onBuyCancel;
Scene_Shop.prototype.onBuyCancel = function() {
    Yanfly.MC.Scene_Shop_onBuyCancel.call(this);
    this._goldWindow.setItemBuy(null);
};

Yanfly.MC.Scene_Shop_doSellGold = Scene_Shop.prototype.doSellGold;
Scene_Shop.prototype.doSellGold = function(number) {
    Yanfly.MC.Scene_Shop_doSellGold.call(this, number);
    // Variables
    if (this._item.variableSellPrices) {
      var length = this._item.variableSellPrices.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var varId = this._item.variableSellPrices[i];
          var value = $gameVariables.value(varId);
          var price = this._item.variableSellPrice[varId];
          $gameVariables.setValue(varId, value + price * number);
        }
      }
    }
    // Items
    if (this._item.itemSellPrices) {
      var length = this._item.itemSellPrices.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var varId = this._item.itemSellPrices[i];
          var value = $gameParty.numItems($dataItems[varId]);
          var price = this._item.itemSellPrice[varId];
          $gameParty.gainItem($dataItems[varId], price * number);
        }
      }
    }
    // Weapons
    if (this._item.weaponSellPrices) {
      var length = this._item.weaponSellPrices.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var varId = this._item.weaponSellPrices[i];
          var value = $gameParty.numItems($dataWeapons[varId]);
          var price = this._item.weaponSellPrice[varId];
          $gameParty.gainItem($dataWeapons[varId], price * number);
        }
      }
    }
    // Armors
    if (this._item.armorSellPrices) {
      var length = this._item.armorSellPrices.length;
      if (length > 0) {
        for (var i = 0; i < length; ++i) {
          var varId = this._item.armorSellPrices[i];
          var value = $gameParty.numItems($dataArmors[varId]);
          var price = this._item.armorSellPrice[varId];
          $gameParty.gainItem($dataArmors[varId], price * number);
        }
      }
    }
};

Yanfly.MC.Scene_Shop_onSellCancel = Scene_Shop.prototype.onSellCancel;
Scene_Shop.prototype.onSellCancel = function() {
    Yanfly.MC.Scene_Shop_onSellCancel.call(this);
    this._goldWindow.setItemSell(null);
};

//=============================================================================
// Victory Aftermath Changes
//=============================================================================

if (Imported.YEP_VictoryAftermath) {

//=============================================================================
// Window_VictoryDrop
//=============================================================================

if (Yanfly.Param.MCGoldItem) {

Window_VictoryDrop.prototype.drawGold = function(item, index) {
    if (item !== 'gold') return;
    this.resetFontSettings();
    var rect = this.itemRect(index);
    rect.width -= this.textPadding();
    var value = BattleManager._rewards.gold;
    var currency = TextManager.currencyUnit;
    this.drawIcon(Yanfly.Param.MCGoldIcon, rect.x + 2, rect.y);
    var wx = rect.x + Window_Base._iconWidth + 4;
    var ww = rect.width - Window_Base._iconWidth + 4;
    this.drawText(currency, wx, rect.y, ww);
    var size = Yanfly.Param.ItemQuantitySize || 28;
    this.contents.fontSize = size;
    var text = '\u00d7' + Yanfly.Util.toGroup(value);
    this.drawText(text, rect.x, rect.y, rect.width, 'right');
    this.resetFontSettings();
};

}; // Yanfly.Param.MCGoldItem

//=============================================================================
// End Victory Aftermath Changes
//=============================================================================

} // Imported.YEP_VictoryAftermath

//=============================================================================
// Utilities
//=============================================================================

Yanfly.Util = Yanfly.Util || {};

if (!Yanfly.Util.toGroup) {
    Yanfly.Util.toGroup = function(inVal) {
        return inVal;
    }
};

//=============================================================================
// End of File
//=============================================================================
}; //


code:
//=============================================================================
// Yanfly Engine Plugins - Passive Aura Effects
// YEP_X_PassiveAuras.js
//=============================================================================

var Imported = Imported || {};
Imported.YEP_X_PassiveAuras = true;

var Yanfly = Yanfly || {};
Yanfly.Aura = Yanfly.Aura || {};
Yanfly.Aura.version = 1.05;

//=============================================================================
 /*:
 * @plugindesc v1.05 (Requires YEP_AutoPassiveStates.js) Add aura effects
 * to various database objects.
 * @author Yanfly Engine Plugins
 *
 * @help
 * ============================================================================
 * Introduction
 * ============================================================================
 *
 * This plugin requires YEP_AutoPassiveStates. Make sure this plugin is located
 * under YEP_AutoPassiveStates in the plugin list.
 *
 * Passive Aura Effects are commonly found in many online multiplayer games
 * with RPG elements. When a battler can give out an aura, it will affect other
 * nearby battlers, too, either ally and/or foe. This plugin will allow states
 * to generate aura effects for other party members, opponents, or specifically
 * for actor and/or enemy parties.
 *
 * ============================================================================
 * Notetags
 * ============================================================================
 *
 * Use the following notetags to make a state generate auras.
 *
 * *NOTE* The notetags that affect alive members will affect members that have
 * at least 1 HP and not affected by the dead state. Even if they are immortal,
 * if they have 0 HP, they will not be affected by the alive aura effects as to
 * not conflict with the dead aura effects.
 *
 * *NOTE* The notetags that affect dead members will affect members that are
 * either affected by the death state or if their HP is at 0. The moment the
 * dead member has 1 HP or is no longer affected by the dead state, they will
 * no longer be affected by dead-only aura effects.
 *
 * State Notetags:
 *
 *   <Ally Aura: x>
 *   <Ally Aura: x, x, x>
 *   <Ally Aura: x through y>
 *   - This will cause the battler's allies to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Alive Ally Aura: x>
 *   <Alive Ally Aura: x, x, x>
 *   <Alive Ally Aura: x through y>
 *   - This will cause the battler's alive allies to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Dead Ally Aura: x>
 *   <Dead Ally Aura: x, x, x>
 *   <Dead Ally Aura: x through y>
 *   - This will cause the battler's dead allies to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Foe Aura: x>
 *   <Foe Aura: x, x, x>
 *   <Foe Aura: x through y>
 *   - This will cause the battler's foes to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Alive Foe Aura: x>
 *   <Alive Foe Aura: x, x, x>
 *   <Alive Foe Aura: x through y>
 *   - This will cause the battler's alive foes to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Dead Foe Aura: x>
 *   <Dead Foe Aura: x, x, x>
 *   <Dead Foe Aura: x through y>
 *   - This will cause the battler's dead foes to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Party Aura: x>
 *   <Party Aura: x, x, x>
 *   <Party Aura: x through y>
 *   - This will cause the Actor Party to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Alive Party Aura: x>
 *   <Alive Party Aura: x, x, x>
 *   <Alive Party Aura: x through y>
 *   - This will cause the battler's alive Actor Party to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Dead Party Aura: x>
 *   <Dead Party Aura: x, x, x>
 *   <Dead Party Aura: x through y>
 *   - This will cause the battler's dead Actor Party to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Troop Aura: x>
 *   <Troop Aura: x, x, x>
 *   <Troop Aura: x through y>
 *   - This will cause the Enemy Troop to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Alive Troop Aura: x>
 *   <Alive Troop Aura: x, x, x>
 *   <Alive Troop Aura: x through y>
 *   - This will cause the battler's alive Enemy Troop to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Dead Troop Aura: x>
 *   <Dead Troop Aura: x, x, x>
 *   <Dead Troop Aura: x through y>
 *   - This will cause the battler's dead Enemy Troop to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Everybody Aura: x>
 *   <Everybody Aura: x, x, x>
 *   <Everybody Aura: x through y>
 *   - This will cause the all active battlers to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Alive Aura: x>
 *   <Alive Aura: x, x, x>
 *   <Alive Aura: x through y>
 *   - This will cause the all alive battlers to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 *   <Dead Aura: x>
 *   <Dead Aura: x, x, x>
 *   <Dead Aura: x through y>
 *   - This will cause the all alive battlers to gain state(s) x (to y)
 *   while the battler is affected by the current state.
 *   *Note: A state cannot use itself in an aura effect.
 *
 * ============================================================================
 * Lunatic Mode - Custom Aura Conditions
 * ============================================================================
 *
 * For those with JavaScript experience and would like to make conditional aura
 * effects, you can use these notetags. Keep in mind, this conditional effect
 * is for the target delivered state and not the origin aura itself.
 *
 * State Notetags:
 *
 *   <Custom Aura Condition>
 *    if (user.hpRate() > 0.50) {
 *      condition = true;
 *    } else {
 *      condition = false;
 *    }
 *   </Custom Aura Condition>
 *   - The 'condition' variable will determine whether or not the target aura
 *   state will appear. If the 'condition' variable is 'true', then it will
 *   appear. If the 'condition' variable is 'false', then it will not appear.
 *   Remember, this notetag has to be placed in the target delivered state and
 *   not the origin aura itself.
 *
 * ============================================================================
 * Changelog
 * ============================================================================
 *
 * Version 1.06:
 * - Updated for RPG Maker MV version 1.6.1.
 *
 * Version 1.05:
 * - Updated for RPG Maker MV version 1.5.0.
 *
 * Version 1.04:
 * - Bug fixed where if an aura is applied as the last action of a turn, it
 * wouldn't take effect until the following turn.
 *
 * Version 1.03:
 * - Lunatic Mode fail safes added.
 *
 * Version 1.02:
 * - Added new notetags that affect specifically alive or dead allies. Alive
 * aura effects will affect only members with at least 1 HP and not affected by
 * the dead state. Dead aura effects will affect members with either 0 HP or
 * have the dead state.
 *
 * Version 1.01:
 * - Fixed a bug that would conflict with Taunt and Selection Core making some
 * aura effects randomly disappear.
 *
 * Version 1.00:
 * - Finished Plugin!
 */
//=============================================================================

if (Imported.YEP_AutoPassiveStates) {

//=============================================================================
// DataManager
//=============================================================================

Yanfly.Aura.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
  if (!Yanfly.Aura.DataManager_isDatabaseLoaded.call(this)) return false;

  if (!Yanfly._loaded_YEP_X_PassiveAuras) {
    this.processAuraNotetags1($dataStates);
    Yanfly._loaded_YEP_X_PassiveAuras = true;
  }

  return true;
};

Yanfly.Aura.Types = [
  'friends', 'aliveFriends', 'deadFriends',
  'opponents', 'aliveOpponents', 'deadOpponents',
  'party', 'aliveParty', 'deadParty',
  'troop', 'aliveTroop', 'deadTroop',
  'all', 'aliveAll', 'deadAll'
];

DataManager.processAuraNotetags1 = function(group) {
  var note1 = /<(.*)[ ](?:AURA|STATE AURA):[ ]*(\d+(?:\s*,\s*\d+)*)>/i;
  var note2 = /<(.*)[ ](?:AURA|STATE AURA):[ ](\d+)[ ](?:THROUGH|to)[ ](\d+)>/i;
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    var notedata = obj.note.split(/[\r\n]+/);

    obj.aura = {};
    for (a = 0; a < Yanfly.Aura.Types.length; ++a) {
      obj.aura[Yanfly.Aura.Types[a]] = [];
    }
    var evalMode = 'none';
    obj.auraConditionEval = '';

    for (var i = 0; i < notedata.length; i++) {
      var line = notedata[i];
      if (line.match(note1)) {
        var type = this.getNotetagAuraType(String(RegExp.$1));
        var array = JSON.parse('[' + RegExp.$2.match(/\d+/g) + ']');
        obj.aura[type] = obj.aura[type].concat(array);
        Yanfly.Util.removeArrayElement(obj.aura[type], obj.id);
      } else if (line.match(note2)) {
        var type = this.getNotetagAuraType(String(RegExp.$1));
        var range = Yanfly.Util.getRange(parseInt(RegExp.$2),
          parseInt(RegExp.$3));
        obj.aura[type] = obj.aura[type].concat(range);
        Yanfly.Util.removeArrayElement(obj.aura[type], obj.id);
      } else if (line.match(/<(?:CUSTOM AURA CONDITION)>/i)) {
        var evalMode = 'custom aura condition';
      } else if (line.match(/<\/(?:CUSTOM AURA CONDITION)>/i)) {
        var evalMode = 'none';
      } else if (evalMode === 'custom aura condition') {
        obj.auraConditionEval = obj.auraConditionEval + line + '\n';
      }
    }
  }
};

DataManager.getNotetagAuraType = function(str) {
  switch (str.toUpperCase()) {
  case 'ALLY':
  case 'ALLIES':
  case 'FRIEND':
  case 'FRIENDS':
  case 'FRIENDLY':
    return 'friends';
    break;

  case 'ALIVE ALLY':
  case 'ALIVE ALLIES':
  case 'ALIVE FRIEND':
  case 'ALIVE FRIENDS':
  case 'ALIVE FRIENDLY':
    return 'aliveFriends';
    break;

  case 'DEAD ALLY':
  case 'DEAD ALLIES':
  case 'DEAD FRIEND':
  case 'DEAD FRIENDS':
  case 'DEAD FRIENDLY':
    return 'deadFriends';
    break;

  case 'FOE':
  case 'FOES':
  case 'OPPONENT':
  case 'OPPONENTS':
  case 'OPPOSING':
    return 'opponents';
    break;

  case 'ALIVE FOE':
  case 'ALIVE FOES':
  case 'ALIVE OPPONENT':
  case 'ALIVE OPPONENTS':
  case 'ALIVE OPPOSING':
    return 'aliveOpponents';
    break;

  case 'DEAD FOE':
  case 'DEAD FOES':
  case 'DEAD OPPONENT':
  case 'DEAD OPPONENTS':
  case 'DEAD OPPOSING':
    return 'deadOpponents';
    break;

  case 'ACTOR':
  case 'ACTORS':
  case 'PARTY':
  case 'PARTIES':
    return 'party';
    break;

  case 'ALIVE ACTOR':
  case 'ALIVE ACTORS':
  case 'ALIVE PARTY':
  case 'ALIVE PARTIES':
    return 'aliveParty';
    break;

  case 'DEAD ACTOR':
  case 'DEAD ACTORS':
  case 'DEAD PARTY':
  case 'DEAD PARTIES':
    return 'deadParty';
    break;

  case 'ENEMY':
  case 'ENEMIES':
  case 'TROOP':
  case 'TROOPS':
    return 'troop';
    break;

  case 'ALIVE ENEMY':
  case 'ALIVE ENEMIES':
  case 'ALIVE TROOP':
  case 'ALIVE TROOPS':
    return 'aliveTroop';
    break;

  case 'DEAD ENEMY':
  case 'DEAD ENEMIES':
  case 'DEAD TROOP':
  case 'DEAD TROOPS':
    return 'deadTroop';
    break;

  default:
    return 'all';
  }
};

DataManager.isAuraState = function(state) {
  if (!state) return false;
  if (!state.aura) return false;
  var array = Yanfly.Aura.Types;
  var length = array.length;
  for (var i = 0; i < length; ++i) {
    if (state.aura[array[i]].length > 0) return true;
  }
  return false;
};

//=============================================================================
// BattleManager
//=============================================================================

BattleManager.refreshAllBattlers = function() {
  var members = $gameParty.members().concat($gameTroop.members());
  var length = members.length;
  for (var i = 0; i < length; ++i) {
    var member = members[i];
    if (member) member.refresh();
  }
};

//=============================================================================
// Game_BattlerBase
//=============================================================================

Yanfly.Aura.Game_BattlerBase_passiveStatesRaw =
  Game_BattlerBase.prototype.passiveStatesRaw;
Game_BattlerBase.prototype.passiveStatesRaw = function() {
  var array = Yanfly.Aura.Game_BattlerBase_passiveStatesRaw.call(this);
  if ($gameParty.inBattle()) {
    array = array.concat(this.validAuraStateIds());
  }
  return array.filter(Yanfly.Util.onlyUnique);
};

Game_BattlerBase.prototype.checkAuraAlive = function() {
  return this.hp > 0 && this.isAlive();
};

Game_BattlerBase.prototype.checkAuraDead = function() {
  return this.hp <= 0 || this.isDead();
};

Game_BattlerBase.prototype.validAuraStateIds = function() {
  var array = [];
  var states = this.auraStateIds();
  var length = states.length;
  for (var i = 0; i < length; ++i) {
    var stateId = states[i];
    if (this.meetAuraStateCondition(stateId)) array.push(stateId);
  }
  return array;
};

Game_BattlerBase.prototype.auraStateIds = function() {
  if ($gameTemp._isGatheringAuraData) return [];
  var array = [];
  var friends = this.friendsUnit();
  var opponents = this.opponentsUnit();
  $gameTemp._isGatheringAuraData = true;
  array = array.concat(friends.auraStateTypeIds('all'));
  array = array.concat(opponents.auraStateTypeIds('all'));
  array = array.concat(friends.auraStateTypeIds('friends'));
  array = array.concat(opponents.auraStateTypeIds('opponents'));
  if (this.checkAuraAlive()) {
    array = array.concat(friends.auraStateTypeIds('aliveAll'));
    array = array.concat(opponents.auraStateTypeIds('aliveAll'));
    array = array.concat(friends.auraStateTypeIds('aliveFriends'));
    array = array.concat(opponents.auraStateTypeIds('aliveOpponents'));
  } else if (this.checkAuraDead()) {
    array = array.concat(friends.auraStateTypeIds('deadAll'));
    array = array.concat(opponents.auraStateTypeIds('deadAll'));
    array = array.concat(friends.auraStateTypeIds('deadFriends'));
    array = array.concat(opponents.auraStateTypeIds('deadOpponents'));
  }
  $gameTemp._isGatheringAuraData = false;
  return array;
};

Game_BattlerBase.prototype.getAuraStateTypeId = function(type) {
  var array = [];
  var states = this.states();
  var length = states.length;
  for (var i = 0; i < length; ++i) {
    var state = states[i];
    if (!state) continue;
    if (!DataManager.isAuraState(state)) continue;
    array = array.concat(state.aura[type]);
  }
  return array;
};

Game_BattlerBase.prototype.meetAuraStateCondition = function(stateId) {
  this._checkAuraStateCondition = this._checkAuraStateCondition || [];
  if (this._checkAuraStateCondition.contains(stateId)) return false;
  var state = $dataStates[stateId];
  if (!state) return false;
  if (state.auraConditionEval === '') return true;
  return this.auraStateConditionEval(state);
};

Game_BattlerBase.prototype.auraStateConditionEval = function(state) {
  this._checkAuraStateCondition = this._checkAuraStateCondition || [];
  this._checkAuraStateCondition.push(state.id);
  var condition = true;
  var a = this;
  var user = this;
  var subject = this;
  var b = this;
  var target = this;
  var s = $gameSwitches._data;
  var v = $gameVariables._data;
  var code = state.auraConditionEval;
  try {
      eval(code);
    } catch (e) {
      Yanfly.Util.displayError(e, code, 'PASSIVE AURA CUSTOM CONDITION ERROR');
    }
  var index = this._checkAuraStateCondition.indexOf(state.id);
  this._checkAuraStateCondition.splice(index, 1);
  return condition;
};

Yanfly.Aura.Game_BattlerBase_addNewState =
  Game_BattlerBase.prototype.addNewState;
Game_BattlerBase.prototype.addNewState = function(stateId) {
  Yanfly.Aura.Game_BattlerBase_addNewState.call(this, stateId);
  this.updateAuras(stateId);
};

Yanfly.Aura.Game_BattlerBase_eraseState =
  Game_BattlerBase.prototype.eraseState;
Game_BattlerBase.prototype.eraseState = function(stateId) {
  Yanfly.Aura.Game_BattlerBase_eraseState.call(this, stateId);
  this.updateAuras(stateId);
};

Game_BattlerBase.prototype.updateAuras = function(stateId) {
  var state = $dataStates[stateId];
  if (!state) return;
  aura = state.aura;
  if ((aura.all.length + aura.friends.length > 0) || aura.party.length > 0) {
    $gameParty.refreshMembers();
  }
  if ((aura.all.length + aura.opponents.length > 0) || aura.troop.length > 0) {
    $gameTroop.refreshMembers();
  }
};

//=============================================================================
// Game_Battler
//=============================================================================

Yanfly.Aura.Game_Battler_onTurnEnd = Game_Battler.prototype.onTurnEnd;
Game_Battler.prototype.onTurnEnd = function() {
  if ($gameParty.inBattle()) this.refresh();
  Yanfly.Aura.Game_Battler_onTurnEnd.call(this);
};

//=============================================================================
// Game_Actor
//=============================================================================

Game_Actor.prototype.auraStateIds = function() {
  if ($gameTemp._isGatheringAuraData) return [];
  var array = Game_Battler.prototype.auraStateIds.call(this);
  var friends = this.friendsUnit();
  var opponents = this.opponentsUnit();
  $gameTemp._isGatheringAuraData = true;
  array = array.concat(friends.auraStateTypeIds('party'));
  array = array.concat(opponents.auraStateTypeIds('party'));
  if (this.checkAuraAlive()) {
    array = array.concat(friends.auraStateTypeIds('aliveParty'));
    array = array.concat(opponents.auraStateTypeIds('aliveParty'));
  } else if (this.checkAuraDead()) {
    array = array.concat(friends.auraStateTypeIds('deadParty'));
    array = array.concat(opponents.auraStateTypeIds('deadParty'));
  }
  $gameTemp._isGatheringAuraData = false;
  return array;
};

//=============================================================================
// Game_Enemy
//=============================================================================

Game_Enemy.prototype.auraStateIds = function() {
  if ($gameTemp._isGatheringAuraData) return [];
  var array = Game_Battler.prototype.auraStateIds.call(this);
  var friends = this.friendsUnit();
  var opponents = this.opponentsUnit();
  $gameTemp._isGatheringAuraData = true;
  array = array.concat(friends.auraStateTypeIds('troop'));
  array = array.concat(opponents.auraStateTypeIds('troop'));
  if (this.checkAuraAlive()) {
    array = array.concat(friends.auraStateTypeIds('aliveTroop'));
    array = array.concat(opponents.auraStateTypeIds('aliveTroop'));
  } else if (this.checkAuraDead()) {
    array = array.concat(friends.auraStateTypeIds('deadTroop'));
    array = array.concat(opponents.auraStateTypeIds('deadTroop'));
  }
  $gameTemp._isGatheringAuraData = false;
  return array;
};

//=============================================================================
// Game_Unit
//=============================================================================

Game_Unit.prototype.allAliveMembers = function() {
  return this.members().filter(function(member) {
      return member.isAlive();
  });
};

Game_Unit.prototype.auraStateTypeIds = function(type) {
  var array = [];
  var members = this.allAliveMembers();
  var length = members.length;
  for (var i = 0; i < length; ++i) {
    var member = members[i];
    if (member) {
      array = array.concat(member.getAuraStateTypeId(type));
    }
  }
  return array;
};

//=============================================================================
// Utilities
//=============================================================================

Yanfly.Util = Yanfly.Util || {};

Yanfly.Util.displayError = function(e, code, message) {
  console.log(message);
  console.log(code || 'NON-EXISTENT');
  console.error(e);
  if (Utils.isNwjs() && Utils.isOptionValid('test')) {
    if (!require('nw.gui').Window.get().isDevToolsOpen()) {
      require('nw.gui').Window.get().showDevTools();
    }
  }
};

Yanfly.Util.removeArrayElement = function(array, element) {
  while (array.indexOf(element) >= 0) {
    array.splice(array.indexOf(element), 1);
  }
};

//=============================================================================
// End of File
//=============================================================================
}; // Imported.YEP_AutoPassiveStates

repo_name: voidljb/karryn

license: unknown