开关变量钩子
# @taroxd metadata 1.0
# @id game_variable_hook
# @display 开关变量钩子
# @help 
#   可以用于事件页的出现条件,
#   可以用于其他以开关作为条件的脚本。
#
#   设置区域在下方,设置范例:
#
#   变量1 固定为队伍的金钱
#     variable(1) { $game_party.gold }
#
#   变量2 固定为队伍中第i+1号队员的体力值,其中i为变量2原本的值
#     variable(2) do |i|
#       actor = $game_party.members[i]
#       actor ? actor.hp : 0
#     end
#
#   开关1 取反
#     switch(1, &:!)

module Taroxd end

module Taroxd::GameVariableHook

  # 是否更改F9调试窗口的显示。如无冲突建议为 true。
  DEBUG_WINDOW = true

  @list = {}  # 保存了监控设置的哈希表

  # 获取变量的值
  def self.operate(id, value)
    proc = @list[id]
    proc ? proc.call(value) : value
  end

  # 增加开关监控(不存档)
  def self.switch(id, &proc)
    @list[-id] = proc
  end

  # 增加变量监控(不存档)
  def self.variable(id, &proc)
    @list[id] = proc
  end

  # 是否正在监控。若列表不存在对应的项或值为 nil,则返回 nil。
  def self.include?(id)
    @list[id]
  end

  # --- 设置区域在此 ---

  # --- 设置区域结束 ---
end

class Game_Switches
  alias_method :value_without_hook, :[]
  def [](id)
    Taroxd::GameVariableHook.operate(-id, value_without_hook(id))
  end
end

class Game_Variables
  alias_method :value_without_hook, :[]
  def [](id)
    Taroxd::GameVariableHook.operate(id, value_without_hook(id))
  end
end

class Game_Interpreter
  def operate_variable(id, type, value)
    $game_variables[id] = case type
    when 0  # 代入
      value
    when 1  # 加法
      $game_variables.value_without_hook(id) + value
    when 2  # 减法
      $game_variables.value_without_hook(id) - value
    when 3  # 乘法
      $game_variables.value_without_hook(id) * value
    when 4  # 除法
      value.zero? ? 0 : $game_variables.value_without_hook(id) / value
    when 5  # 取余
      value.zero? ? 0 : $game_variables.value_without_hook(id) % value
    end
  end
end

class Window_DebugRight < Window_Selectable
  def update_switch_mode
    return unless Input.trigger?(:C)
    id = current_id
    $game_switches[id] = !$game_switches.value_without_hook(id)
    Sound.play_ok
    redraw_current_item
  end

  def update_variable_mode
    id = current_id
    value = $game_variables.value_without_hook(id)
    return unless value.is_a?(Numeric)
    value += 1 if Input.repeat?(:RIGHT)
    value -= 1 if Input.repeat?(:LEFT)
    value += 10 if Input.repeat?(:R)
    value -= 10 if Input.repeat?(:L)
    if $game_variables.value_without_hook(current_id) != value
      $game_variables[id] = value
      Sound.play_cursor
      redraw_current_item
    end
  end

  def draw_item(index)
    data_id = @top_id + index
    id_text = sprintf("%04d:", data_id)
    id_width = text_size(id_text).width
    if @mode == :switch
      name = $data_system.switches[data_id]
      status = $game_switches.value_without_hook(data_id) ? '[ON]' : '[OFF]'
      if Taroxd::GameVariableHook.include?(-data_id)
        status.concat($game_switches[data_id] ? ' ->  [ON]' : ' -> [OFF]')
      end
    else
      name = $data_system.variables[data_id]
      status = $game_variables.value_without_hook(data_id).to_s
      if Taroxd::GameVariableHook.include?(data_id)
        status << ' -> ' << $game_variables[data_id].to_s
      end
    end
    name = "" unless name
    rect = item_rect_for_text(index)
    change_color(normal_color)
    draw_text(rect, id_text)
    rect.x += id_width
    rect.width -= id_width + 60
    draw_text(rect, name)
    rect.width += 60
    draw_text(rect, status, 2)
  end
end if Taroxd::GameVariableHook::DEBUG_WINDOW
得失物品提示
# @taroxd metadata 1.0
# @id gain_message
# @require taroxd_core
# @display 得失物品提示
module Taroxd::GainMessage

  # 信息格式

  # 转义符:
  # \name    代表物品名称 / 金钱单位
  # \value   代表获得 / 失去的物品 / 金钱数量
  # \icon    绘制物品 / 金钱的图标
  # \action  代表“获得”或者“失去”。可在下面修改。
  # 支持“显示文字”中的所有转义符。
  ITEM_FORMAT  = '\action了 \name * \value'
  GOLD_FORMAT  = '\action了 \value \name'
  ACTION_GAIN  = '获得'
  ACTION_LOSE  = '失去'
  GOLD_ICON_INDEX = 361           # 金钱图标的索引

  BACKGROUND   = 1                # 窗口背景(0/1/2)
  POSITION     = 1                # 显示位置(0/1/2)

  # 音效(不需要的话可以直接删去对应的行)
  GAIN_GOLD_SE = 'Shop'           # 获得金钱
  LOSE_GOLD_SE = 'Blow2'          # 失去金钱
  GAIN_ITEM_SE = 'Item1'          # 获得物品
  LOSE_ITEM_SE = LOSE_GOLD_SE     # 失去物品

  # 功能是否启用。
  def self.enabled?
    true
  end

  # 显示提示信息。获得金钱时将 item 设为 nil。
  def self.show(value, item)
    @item = item
    @value = value
    $game_message.background = BACKGROUND
    $game_message.position = POSITION
    $game_message.add(message)
    play_se
  end

  private

  # 获取提示的消息
  def self.message
    if @item
      format = ITEM_FORMAT
      icon_index = @item.icon_index
      name = @item.name
    else
      format = GOLD_FORMAT
      icon_index = GOLD_ICON_INDEX
      name = Vocab.currency_unit
    end

    gsub = {
      '\action' => @value > 0 ? ACTION_GAIN : ACTION_LOSE,
      '\value'  => @value.abs,
      '\icon'   => "\\I[#{icon_index}]",
      '\name'   => name
    }

    format.gsub(Regexp.union(gsub.keys), gsub)
  end

  def self.play_se
    const = :"#{@value > 0 ? 'GAIN' : 'LOSE'}_#{@item ? 'ITEM' : 'GOLD'}_SE"
    se = const_defined?(const) && const_get(const)
    Audio.se_play('Audio/SE/' + se) if se
  end
end

class Game_Party < Game_Unit
  # 获取道具总数(包括装备)
  def item_number_with_equip(item)
    members.inject(item_number(item)) { |a, e| a + e.equips.count(item) }
  end
end

class Game_Interpreter

  GainMessage = Taroxd::GainMessage

  # 显示提示窗口
  def show_gain_message(value, item = nil)
    return if value.zero?
    GainMessage.show(value, item)
    wait_for_message
  end

  # 增减金钱
  def_chain :command_125 do |old|
    return old.call unless GainMessage.enabled?
    last_gold = $game_party.gold
    old.call
    show_gain_message($game_party.gold - last_gold)
  end

  # 增减物品
  def_chain :command_126 do |old|
    return old.call unless GainMessage.enabled?
    item = $data_items[@params[0]]
    last_num = $game_party.item_number(item)
    old.call
    show_gain_message($game_party.item_number(item) - last_num, item)
  end

  # 增减武器
  def_chain :command_127 do |old|
    return old.call unless GainMessage.enabled?
    item = $data_weapons[@params[0]]
    last_num = $game_party.item_number_with_equip(item)
    old.call
    value = $game_party.item_number_with_equip(item) - last_num
    show_gain_message(value, item)
  end

  # 增减护甲
  def_chain :command_128 do |old|
    return old.call unless GainMessage.enabled?
    item = $data_armors[@params[0]]
    last_num = $game_party.item_number_with_equip(item)
    old.call
    value = $game_party.item_number_with_equip(item) - last_num
    show_gain_message(value, item)
  end
end

class Taroxd::EventTranscompiler
  def command_125
    value = operate_value(@params[1], @params[2], @params[3])
    "if GainMessage.enabled?
      @params = $game_party.item_number(item)
      $game_party.gain_gold(#{value})
      show_gain_message($game_party.gold - @params)
    else
      $game_party.gain_gold(#{value})
    end"
  end

  # database: :items, :weapons, :equips
  def show_message_gain_item(database)
    item_number = database == :item ? :item_number : :item_number_with_equip
    value = operate_value(@params[1], @params[2], @params[3])
    item = "$data_#{database}[#{@params[0]}]"
    "if GainMessage.enabled?
      @params = $game_party.#{item_number}(#{item})
      $game_party.gain_item(#{item}, #{value}, #{@params[4]})
      show_gain_message($game_party.#{item_number}(#{item}) - @params, #{item})
    else
      $game_party.gain_item(#{item}, #{value}, #{@params[4]})
    end"
  end

  def command_126
    show_message_gain_item(:items)
  end

  def command_127
    show_message_gain_item(:weapons)
  end

  def command_128
    show_message_gain_item(:armors)
  end
end if Taroxd.const_defined?(:EventTranscompiler)
快进对话
# @taroxd metadata 1.0
# @id fast_message
# @display 快进对话
module Taroxd
  module FastMessage
    KEY = :CTRL      # 按此键快进对话
    ENABLED = $TEST  # true: 启用; $TEST: 仅测试模式; false: 不启用
    SPEED = 3        # 文字快进速度。可以为小数,但不能小于 1。

    @counter = 0

    # 是否在输出一个字符后等待。show_fast: 是否快进
    def self.wait?(show_fast)
      !show_fast && !Input.press?(KEY) || (@counter += 1) % SPEED < 1
    end

    # 已经进入等待输入的情况下,是否继续等待输入
    def self.keep_pause?
      [:B, :C, KEY].none? { |k| Input.trigger?(k) }
    end

    # 尚未进入等待输入的情况下,是否跳过等待输入
    def self.skip_pause?
      Input.press?(KEY)
    end
  end
end

class Window_Message < Window_Base

  FastMessage = Taroxd::FastMessage

  def input_pause
    return if FastMessage.skip_pause?
    self.pause = true
    wait(10)
    Fiber.yield while FastMessage.keep_pause?
    Input.update
    self.pause = false
  end

  def wait_for_one_character
    update_show_fast
    Fiber.yield if FastMessage.wait?(@show_fast || @line_show_fast)
  end
end if Taroxd::FastMessage::ENABLED