查看: 2022|回复: 3

[已经解决] 物品合成脚本第224行出错

[复制链接]
梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
回帖
13
发表于 2016-1-26 16:49:08 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 RyanBern 于 2016-1-26 21:33 编辑

代码如下
[pre lang="Ruby"]#=============================================================================
# 物品合成简易优化版 Ver 1.0
#-----------------------------------------------------------------------------
# By : RyanBern
#-----------------------------------------------------------------------------
# 功能介绍:
#     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
#     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
#     改成了外部文件的形式,下面就说明一下如何使用。
#-----------------------------------------------------------------------------
# 使用方法:
#     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
#       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
#       <recipe=1>
#         <product>kind=0,id=3</product>
#         <ingredients>
#           kind=0,id=1,number=2
#           kind=0,id=2,number=2
#         </ingredients>
#       </recipe>
#       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
#       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
#       就是对应的ID
#       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
#       每一行代表一种原料的分类,id,和数量,注意不要写重复了。
#       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
#     2.有关学习配方:
#       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
#       $game_party.learn_recipe(配方ID) 学习该ID的配方
#       $game_party.forget_recipe(配方ID) 失去该ID的配方
#     3.有关场景调用:
#       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
#       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
#       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
#-----------------------------------------------------------------------------
# 注意事项:
#     1.Recipes.txt里面的缩进不是必须的。
#     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
#       以便日后做出更改。
#-----------------------------------------------------------------------------
# 更新记录:
#=============================================================================
module RB
  Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m
  Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m
  Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m
  Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
end
module RPG
  class Recipe
    attr_accessor :id
    attr_accessor :product
    attr_accessor :ingredients
    def initialize
      @id = 0
      @product = Ingredient.new
      @ingredients = []
    end
    class Ingredient
      attr_accessor :kind
      attr_accessor :id
      attr_accessor :number
      def initialize(kind = 0, id = 0, number = 0)
        @kind = kind
        @id = id
        @number = number
      end
    end
  end
  def self.extract_recipe
    filename = "Recipes.txt"
    outfile_name = "Data/Recipes.rxdata"
    data = []
    begin
      file = File.open(filename, "r")
      str = file.read
      file.close
      while str.slice!(RB::Regex_Recipe)
        id = $1.to_i
        contents = $2
        recipe = Recipe.new
        recipe.id = id
        if RB::Regex_Product =~ contents
          recipe.product.kind = $1.to_i
          recipe.product.id = $2.to_i
        end
        if RB::Regex_Ingredient =~ contents
          ingredient_contents = $1
          ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|
            recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
          end
        end
        data[id] = recipe
      end
      outfile = File.open(outfile_name, "wb")
      Marshal.dump(data, outfile)
      outfile.close
    rescue
      p 1
    end
  end
end

class Game_Party
  alias rb_initialize_20141226 initialize
  def initialize
    rb_initialize_20141226
    @recipes = {}
  end
  def recipe_learn?(recipe_id)
    @recipes ||= {}
    return @recipes[recipe_id]
  end
  def learn_recipe(recipe_id)
    @recipes ||= {}
    @recipes[recipe_id] = true
  end
  def forget_recipe(recipe_id)
    @recipes ||= {}
    @recipes[recipe_id] = nil
  end
  def product_max_num(recipe_id)
    max = -1
    recipe = $data_recipes[recipe_id]
    return 0 if recipe.nil?
    recipe.ingredients.each do |ingredient|
      num_need = ingredient.number
      case ingredient.kind
      when 0
        num_current = item_number(ingredient.id)
      when 1
        num_current = weapon_number(ingredient.id)
      when 2
        num_current = armor_number(ingredient.id)
      else
        num_current = 0
      end
      max = num_current / num_need if max == -1 || num_current / num_need < max
    end
    return max == -1 ? 0 : max
  end
end

class Window_Product < Window_Selectable
  def initialize(mode = -1)
    super(0, 64, 240, 480 - 64)
    self.index = 0
    @mode = mode
    refresh
  end
  def recipe
    return @data[self.index]
  end
  def item
    recipe = self.recipe
    return nil if recipe.nil?
    product = recipe.product
    case product.kind
    when 0
      item = $data_items[product.id]
    when 1
      item = $data_weapons[product.id]
    when 2
      item = $data_armors[product.id]
    else
      item = nil
    end
    return item
  end
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    (1...$data_recipes.size).each do |i|
      if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes.product.kind == @mode || @mode == 3
        @data << $data_recipes if $data_recipes != nil
      end
    end
    @item_max = @data.size
    if @item_max > 0
      self.contents = Bitmap.new(width - 32, @item_max * 32)
      @data.each_index{|i| draw_item(i)}
    else
      self.contents = Bitmap.new(width - 32, 32)
      self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
    end
  end
  def draw_item(index)
    x = 4
    y = 32 * index
    product = @data[index].product
    case product.kind
    when 0
      item = $data_items[product.id]
    when 1
      item = $data_weapons[product.id]
    when 2
      item = $data_armors[product.id]
    else
      item = nil
    end
    if item == nil
      return
    end
    disabled = $game_party.product_max_num(@data[index].id) == 0
    bitmap = RPG::Cache.icon(item.icon_name)
    opacity = disabled ? 128 : 255
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.font.color = disabled ? disabled_color : normal_color
    self.contents.draw_text(x + 28, y, 176, 32, item.name)
  end
  def update_help
    @help_window.set_text(self.item == nil ? "" : self.item.description)
  end
end

class Window_Ingredients < Window_Base
  def initialize
    super(240, 288, 400, 192)
    @waiting = 0
    @dy = 1
    @recipe = nil
    refresh
  end
  def recipe=(recipe)
    if @recipe != recipe
      @recipe = recipe
      @waiting = 40
      @dy = 1
      self.oy = 0
      refresh
    end
  end
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    if @recipe == nil
      return
    end
    h = 32 * @recipe.ingredients.size
    if h > 0
      self.contents = Bitmap.new(width - 32, h)
      @recipe.ingredients.each_with_index do |ingredient, index|
        draw_item(ingredient, index)
      end
    end
  end
  def draw_item(ingredient, index)
    x = 4
    y = index * 32
    case ingredient.kind
    when 0
      item = $data_items[ingredient.id]
      num = $game_party.item_number(ingredient.id)
    when 1
      item = $data_weapons[ingredient.id]
      num = $game_party.weapon_number(ingredient.id)
    when 2
      item = $data_armors[ingredient.id]
      num = $game_party.armor_number(ingredient.id)
    else
      item = nil
    end
    if item == nil
      return
    end
    bitmap = RPG::Cache.icon(item.icon_name)
    disabled = num < ingredient.number
    opacity = disabled ? 128 : 255
    self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.font.color = disabled ? disabled_color : normal_color
    self.contents.draw_text(x + 28, y, 192, 32, item.name)
    self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
    self.contents.font.color = normal_color
    self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
  end
  def update
    super
    if self.contents != nil && self.contents.height > self.height - 32
      if @waiting > 0
        @waiting -= 1
        return
      end
      self.oy += @dy
      if self.oy + self.height - 32 >= self.contents.height
        @dy = -1
        @waiting = 60
      end
      if self.oy <= 0
        @dy = 1
        @waiting = 60
      end
    end
  end
end

class Window_Craft_Number < Window_Base
  def initialize
    super(240, 64, 400, 160)
    self.contents = Bitmap.new(width - 32, height - 32)
    @recipe = nil
    @Max = 1
    @number = 1
    @number_current = 0
  end
  def recipe=(recipe)
    if @recipe != recipe
      @recipe = recipe
      reset
      refresh
    end
  end
  def reset
    if @recipe == nil
      @number = 0
      @number_current = 0
      return
    end
    product = @recipe.product
    case product.kind
    when 0
      @number_current = $game_party.item_number(product.id)
    when 1
      @number_current = $game_party.weapon_number(product.id)
    when 2
      @number_current = $game_party.armor_number(product.id)
    else
      @number_current = 0
    end
    @Max = $game_party.product_max_num(@recipe.id)
    @max = [@max, 99 - @number_current].min
    @number = @max == 0 ? 0 : 1
  end
  def number
    return @number
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 364, 32, "所持数")
    self.contents.draw_text(4, 32, 364, 32, "最大可合成数")
    self.contents.draw_text(4, 64, 364, 32, "当前合成数量")
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
    self.contents.draw_text(4, 32, 364, 32, @max.to_s, 2)
    self.contents.draw_text(252, 96, 32, 32, "×")
    self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
  end
  def update
    super
    if self.active
      # 光标右 (+1)
      if Input.repeat?(Input::RIGHT) and @number < @max
        $game_system.se_play($data_system.cursor_se)
        @number += 1
        refresh
      end
      # 光标左 (-1)
      if Input.repeat?(Input::LEFT) and @number > 1
        $game_system.se_play($data_system.cursor_se)
        @number -= 1
        refresh
      end
      # 光标上 (+10)
      if Input.repeat?(Input::UP) and @number < @max
        $game_system.se_play($data_system.cursor_se)
        @number = [@number + 10, @max].min
        refresh
      end
      # 光标下 (-10)
      if Input.repeat?(Input::DOWN) and @number > 1
        $game_system.se_play($data_system.cursor_se)
        @number = [@number - 10, 1].max
        refresh
      end
    end
  end
end
class Window_Craft_Label < Window_Base
  def initialize
    super(240, 224, 400, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  end
end

class Scene_Title
  alias rb_main_20141226 main
  def main
    if $DEBUG
      RPG.extract_recipe
    end
    $data_recipes = load_data("Data/Recipes.rxdata")
    rb_main_20141226
  end
end

class Scene_Craft
  def initialize(mode = -1)
    @mode = mode
  end
  def main
    @product_window = Window_Product.new(@mode)
    @help_window = Window_Help.new
    @product_window.help_window = @help_window
    @ingredient_window = Window_Ingredients.new
    @number_window = Window_Craft_Number.new
    @label_window = Window_Craft_Label.new
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @product_window.dispose
    @help_window.dispose
    @ingredient_window.dispose
    @number_window.dispose
    @label_window.dispose
  end
  def update
    @product_window.update
    @ingredient_window.update
    @ingredient_window.recipe = @product_window.recipe
    @number_window.recipe = @product_window.recipe
    if @product_window.active
      update_product
      return
    end
    if @number_window.active
      update_number
    end
  end
  def update_product
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
    if Input.trigger?(Input::C)
      recipe = @product_window.recipe
      if recipe == nil || $game_party.product_max_num(recipe.id) == 0
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      $game_system.se_play($data_system.decision_se)
      @number_window.cursor_rect.set(304, 96, 32, 32)
      @product_window.active = false
      @number_window.active = true
      return
    end
  end
  def update_number
    @number_window.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      @number_window.active = false
      @product_window.active = true
      @number_window.cursor_rect.empty
      return
    end
    if Input.trigger?(Input::C)
      $game_system.se_play($data_system.shop_se)
      recipe = @product_window.recipe
      case recipe.product.kind
      when 0
        $game_party.gain_item(recipe.product.id, @number_window.number)
      when 1
        $game_party.gain_weapon(recipe.product.id, @number_window.number)
      when 2
        $game_party.gain_armor(recipe.product.id, @number_window.number)
      end
      recipe.ingredients.each do |ingredient|
        case ingredient.kind
        when 0
          $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
        when 1
          $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
        when 2
          $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
        end
      end
      @product_window.refresh
      @ingredient_window.refresh
      @number_window.reset
      @number_window.refresh
      @product_window.active = true
      @number_window.active = false
      @number_window.cursor_rect.empty
      return
    end
  end
end [/pre]

评分

参与人数 1星屑 +35 收起 理由
RyanBern + 35 手动认可奖励

查看全部评分

梦石
0
星屑
9557
在线时间
5074 小时
注册时间
2013-6-21
回帖
3459

开拓者贵宾剧作品鉴家

发表于 2016-1-26 21:35:32 | 显示全部楼层
代码被论坛错误解析。
代码中有多处
  1. [url=home....]xxxx[/url]
复制代码
请将含url的部分(包括方括号)去掉。

或者是直接下载范例工程。
回复

使用道具 举报

梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
回帖
13
 楼主| 发表于 2016-1-27 19:41:56 | 显示全部楼层
那第193行错误是怎么回事?

点评

我这边测试没有发现问题,请前往http://rm.66rpg.com/thread-374803-1-1.html发布帖下载范例工程查看一下?  发表于 2016-1-27 21:16
回复

使用道具 举报

梦石
0
星屑
215
在线时间
51 小时
注册时间
2015-11-8
回帖
13
 楼主| 发表于 2016-1-28 15:37:30 | 显示全部楼层
灰色的 发表于 2016-1-27 19:41
那第193行错误是怎么回事?

谢谢您,问题已经解决
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部