﻿#==============================================================================
# ★ ExCommand_BarterShop　～ 以物易物商店 ～
#------------------------------------------------------------------------------
# 　在[商店处理] 使用特定的物品作为货币来购买货物。
#==============================================================================

# 使兑换功能有效的开关 ID。
# 当此号的开关为 ON 时，[商店处理] 实际打开的是物品兑换的商店界面、
# 也可以勾选「只能购买的商店」选项
# 此开关会在离开商店画面时自动设为 OFF
EXCMD_BTRSHOP_SID = 6

# 商店兑换命令窗口的用语。
# 从左数起分别为原本的[购买] [贩卖] [离开]。
EXCMD_BTRSHOP_VOCAB = ["兑换", "贩买", "离开"]

# 兑换物设定。
# 除了交易时使用，兑换物依然当作普通物品处理。
# 依次设定：物品 ID、单位用语、兑换率。
# 商品价格 ÷ 兑换率 自动取整，舍弃小数。
# 范例：兑换率设为 100 时 => 
# 　1650 G 的商品可以用16个兑换物交换，、100 G 以下的商品可以用1个兑换物交换
EXCMD_BTRSHOP_ITEM = [22, "枚", 100]

# 指定商品兑换率的文字。
# 在没有直接指定商品的兑换物价格时，会自动换算
# 但是优先使用[备注]中所指定的价格： 指定文字 + [兑换率]
# 范例：可以用 20 个兑换物兑换的物品 => *BARTER_RATE[20]
EXCMD_BTRSHOP_SIGNATURE = "*BARTER_RATE"

# 识别商店的变量 ID。
# 可以在调用「商店处理」指令前，使用商店识别变量来设定商店编号。
# 也就是说，同一个商店NPC可以因为商店识别变量的数值不同而使用不同的兑换率。
EXCMD_BTRSHOP_VID = 6

# 商店的设定
# 格式：指定文字 ( + [兑换率] ) + [商店识别号:兑换率] 
# 「:」对应每个商店的兑换率，使用「|」来分隔商店。
# 范例1： 1号商店使用 20 个、2号商店使用 15 个、3号商店使用 25 个兑换物
# 来兑换商店内的物品 => *BARTER_RATE[1:20|2:15|3:25]

# 范例2：2号商店使用 10 个、3号商店使用 15 个、其馀的使用 20 个兑换物
# 来兑换商店内的物品 => *BARTER_RATE[20][2:10|3:15]
# 
# 兑换率判断优先度：个别商店的设定 ＞ 商品的设定 ＞ 兑换物的兑换率。

#------------------------------------------------------------------------------

class Game_Party
  #--------------------------------------------------------------------------
  # ☆ 获取交换物
  #--------------------------------------------------------------------------
  def barter_item
    return $data_items[EXCMD_BTRSHOP_ITEM[0]]
  end
  #--------------------------------------------------------------------------
  # ☆ 获取交换物的数量
  #--------------------------------------------------------------------------
  def barter_item_number
    return $game_party.item_number(barter_item)
  end
  #--------------------------------------------------------------------------
  # ☆ 获取物品兑换率
  #--------------------------------------------------------------------------
  def barter_rate(item)
    rate = 0
    shop_id = $game_variables[EXCMD_BTRSHOP_VID]
    sig = EXCMD_BTRSHOP_SIGNATURE
    reg = /#{Regexp.quote sig}(?:\[\d+\])?\[((\d+:\d+\|?)+)\]/
    note = item.note.clone
    note.gsub!(/\r\n/, "")
    if shop_id > 0 and note.scan(reg).to_a[0]
      for s in $~[1].split(/\s*\|\s*/)
        r = s.split(/\s*:\s*/)
        rate = r[1].to_i if r[0].to_i == shop_id
        break if rate > 0
      end
    end
    if rate == 0
      if note[/#{Regexp.quote sig}\[(\d+)\]/].to_a[0]
        rate = $1.to_i
      else
        rate = (item.price / EXCMD_BTRSHOP_ITEM[2]).round
      end
    end
    return rate > 0 ? rate : 1
  end
end

class Window_Base
  alias _excbshop_initialize initialize
  #--------------------------------------------------------------------------
  # ● 初始化对像(追加定义)
  #     x      : 窗口 X 坐标
  #     y      : 窗口 Y 坐标
  #     width  : 窗口宽度
  #     height : 窗口高度
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    _excbshop_initialize(x, y, width, height)
    self.currency = Vocab::gold
  end
  #--------------------------------------------------------------------------
  # ☆ 通货单位设定
  #--------------------------------------------------------------------------
  def currency=(currency)
    @currency = currency
  end
  #--------------------------------------------------------------------------
  # ☆ 判断是否使用金钱
  #--------------------------------------------------------------------------
  def gold?
    return @currency == Vocab::gold
  end
  #--------------------------------------------------------------------------
  # ● 绘制金钱单位(重新定义)
  #     value : 数目
  #     x     : 描画目标 X 坐标
  #     y     : 描画目标 Y 坐标
  #     width : 描画目标宽度
  #--------------------------------------------------------------------------
  def draw_currency_value(value, x, y, width)
    cx = contents.text_size(@currency).width
    self.contents.font.color = normal_color
    self.contents.draw_text(x, y, width - cx - 2, WLH, value, 2)
    self.contents.font.color = system_color
    self.contents.draw_text(x, y, width, WLH, @currency, 2)
  end
end

class Window_Gold
  alias _excbshop_refresh refresh
  #--------------------------------------------------------------------------
  # ○ 刷新(追加定义)
  #--------------------------------------------------------------------------
  def refresh
    _excbshop_refresh
    if $game_switches[EXCMD_BTRSHOP_SID]
      self.contents.clear
      value = gold? ? $game_party.gold : $game_party.barter_item_number
      draw_currency_value(value, 4, 0, 120)
    end
  end
end

class Window_ShopBuy
  alias _excbshop_refresh refresh
  alias _excbshop_draw_item draw_item
  #--------------------------------------------------------------------------
  # ○ 刷新 (追加定义)
  #--------------------------------------------------------------------------
  def refresh
    unless $game_switches[EXCMD_BTRSHOP_SID]
      _excbshop_refresh
    else
      @data = []
      for goods_item in @shop_goods
        case goods_item[0]
        when 0
          item = $data_items[goods_item[1]]
        when 1
          item = $data_weapons[goods_item[1]]
        when 2
          item = $data_armors[goods_item[1]]
        end
        if item != nil and item != $game_party.barter_item # 交换アイテムを除外
          @data.push(item)
        end
      end
      @item_max = @data.size
      create_contents
      for i in 0...@item_max
        draw_item(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 绘制商品 (追加定义)
  #     index : 商品索引
  #--------------------------------------------------------------------------
  def draw_item(index)
    unless $game_switches[EXCMD_BTRSHOP_SID]
      _excbshop_draw_item(index)
    else
      item = @data[index]
      number = $game_party.item_number(item)
      barter_number = $game_party.barter_item_number
      barter_rate = $game_party.barter_rate(item)
      enabled = (barter_rate <= barter_number and number < 99)
      rect = item_rect(index)
      self.contents.clear_rect(rect)
      draw_item_name(item, rect.x, rect.y, enabled)
      rect.width -= 4
      self.contents.draw_text(rect, barter_rate, 2)
    end
  end
end

class Scene_Shop
  alias _excbshop_start start
  alias _excbshop_terminate terminate
  alias _excbshop_create_command_window create_command_window
  alias _excbshop_update_command_selection update_command_selection
  alias _excbshop_update_buy_selection update_buy_selection
  alias _excbshop_update_sell_selection update_sell_selection
  alias _excbshop_decide_number_input decide_number_input
  #--------------------------------------------------------------------------
  # ☆ 判断商店是否是「以物易物商店」
  #--------------------------------------------------------------------------
  def barter?
    return $game_switches[EXCMD_BTRSHOP_SID]
  end
  #--------------------------------------------------------------------------
  # ○ 开始处理 (重新定义)
  #--------------------------------------------------------------------------
  def start
    _excbshop_start
    if barter?
      @gold_window.currency = EXCMD_BTRSHOP_ITEM[1]
      @number_window.currency = EXCMD_BTRSHOP_ITEM[1]
      @gold_window.refresh
    end
  end
  #--------------------------------------------------------------------------
  # ○ 结束处理 (追加定义)
  #--------------------------------------------------------------------------
  def terminate
    _excbshop_terminate
    $game_switches[EXCMD_BTRSHOP_SID] = false
  end
  #--------------------------------------------------------------------------
  # ● 生成命令窗口(追加定义)
  #--------------------------------------------------------------------------
  def create_command_window
    unless barter?
      _excbshop_create_command_window
    else
      s1 = EXCMD_BTRSHOP_VOCAB[0]
      s2 = EXCMD_BTRSHOP_VOCAB[1]
      s3 = EXCMD_BTRSHOP_VOCAB[2]
      @command_window = Window_Command.new(384, [s1, s2, s3], 3)
      @command_window.y = 56
      if $game_temp.shop_purchase_only
        @command_window.draw_item(1, false)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 更新命令窗口 (追加定义)
  #--------------------------------------------------------------------------
  def update_command_selection
    _excbshop_update_command_selection
    if Input.trigger?(Input::C) and barter?
      unless $game_temp.shop_purchase_only
        if @command_window.index == 1 # 売却する
          @gold_window.currency = Vocab::gold
          @number_window.currency = Vocab::gold
          @gold_window.refresh
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 更新买入选择(追加定义)
  #--------------------------------------------------------------------------
  def update_buy_selection
    unless barter?
      _excbshop_update_buy_selection
    else
      @status_window.item = @buy_window.item
      if Input.trigger?(Input::B)
        Sound.play_cancel
        @command_window.active = true
        @dummy_window.visible = true
        @buy_window.active = false
        @buy_window.visible = false
        @status_window.visible = false
        @status_window.item = nil
        @help_window.set_text("")
        return
      end
      if Input.trigger?(Input::C)
        @item = @buy_window.item
        barter_number = $game_party.barter_item_number
        barter_rate = $game_party.barter_rate(@item)
        number = $game_party.item_number(@item)
        if @item == nil or barter_rate > barter_number or number == 99
          Sound.play_buzzer
        else
          Sound.play_decision
          max = barter_number / barter_rate
          max = [max, 99 - number].min
          @buy_window.active = false
          @buy_window.visible = false
          @number_window.set(@item, max, barter_rate)
          @number_window.active = true
          @number_window.visible = true
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 更新卖出选择(追加定义)
  #--------------------------------------------------------------------------
  def update_sell_selection
    _excbshop_update_sell_selection
    if Input.trigger?(Input::B) and barter?
      @gold_window.currency = EXCMD_BTRSHOP_ITEM[1]
      @number_window.currency = EXCMD_BTRSHOP_ITEM[1]
      @gold_window.refresh
    end
  end
  #--------------------------------------------------------------------------
  # ● 确认数值输入(追加定义)
  #--------------------------------------------------------------------------
  def decide_number_input
    unless barter?
      _excbshop_decide_number_input
    else
      Sound.play_shop
      @number_window.active = false
      @number_window.visible = false
      barter_rate = $game_party.barter_rate(@item)
      number = @number_window.number
      case @command_window.index
      when 0  # 购买（交换）
        $game_party.lose_item($game_party.barter_item, number * barter_rate)
        $game_party.gain_item(@item, number)
        @gold_window.refresh
        @buy_window.refresh
        @status_window.refresh
        @buy_window.active = true
        @buy_window.visible = true
      when 1  # 贩卖
        $game_party.gain_gold(number * (@item.price / 2))
        $game_party.lose_item(@item, number)
        @gold_window.refresh
        @sell_window.refresh
        @status_window.refresh
        @sell_window.active = true
        @sell_window.visible = true
        @status_window.visible = false
      end
    end
  end
end

