查看: 3802|回复: 3

[已经解决] 【搬运+改进】怎么改进“物品合成简易优化版 Ver 1.1”?

[复制链接]
梦石
24
星屑
8942
在线时间
248 小时
注册时间
2020-12-4
回帖
259

极短24获奖极短23获奖极短22获奖

发表于 2021-6-18 21:07:32 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 纯属小虫 于 2021-6-18 22:11 编辑

一直用公共事件做物品合成,觉得不够华丽,而且很难对比目标素材需求量和已有素材数量
于是决定把合成系统大换血,就找到了版主发的早期的一个物品合成脚本——

https://rpg.blue/thread-374803-1-1.html

这是版主所做的合成系统脚本的窗口效果:
234329ejt3uw30333c6z3n.png

由于之前用公共事件做物品合成的时候,还加入了大量对合成目标的说明,这里不希望因为更新合成系统而把这些说明阉割了!!!
所以自己稍微修改了一下脚本,将布局修改成这样:
NTSH])}IJP@Q@4K)$`9]0SR.png
到这一步为止,出现了两个问题,脚本小白研究了三天的RUBY和正则表达式还是没有弄清版主的脚本中的奥秘——
1、该合成系统是通过在根目录建记事本,然后通过脚本读取的合成列表信息的。
      记事本中原存在合成目标物的【种类】和【编号】,合成素材的【种类】、【编号】和【需求量】这两个信息,那么,我要如何其中添入【合成说明】的信息?

2、右下角窗口中的需求量是固定的,如何添加变量算法使得【素材需求量】会随着【当前合成数量】变化?
附上原版脚本:
点击展开/收起
  1. #=============================================================================
  2. # 物品合成简易优化版 Ver 1.1
  3. #-----------------------------------------------------------------------------
  4. # By : RyanBern
  5. #-----------------------------------------------------------------------------
  6. # 功能介绍:
  7. #     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
  8. #     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
  9. #     改成了外部文件的形式,下面就说明一下如何使用。
  10. #-----------------------------------------------------------------------------
  11. # 使用方法:
  12. #     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
  13. #       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
  14. #       <recipe=1>
  15. #         <product>kind=0,id=3</product>
  16. #         <ingredients>
  17. #           kind=0,id=1,number=2
  18. #           kind=0,id=2,number=2
  19. #         </ingredients>
  20. #       </recipe>
  21. #       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
  22. #       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
  23. #       就是对应的ID,最终合成物品在<product></product>之间
  24. #       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
  25. #       每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
  26. #       的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
  27. #       变量的ID。
  28. #       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
  29. #     2.有关学习配方:
  30. #       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
  31. #       $game_party.learn_recipe(配方ID) 学习该ID的配方
  32. #       $game_party.forget_recipe(配方ID) 失去该ID的配方
  33. #     3.有关场景调用:
  34. #       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
  35. #       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
  36. #       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
  37. #-----------------------------------------------------------------------------
  38. # 注意事项:
  39. #     1.Recipes.txt里面的缩进不是必须的。
  40. #     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
  41. #       以便日后做出更改。
  42. #-----------------------------------------------------------------------------
  43. # 更新记录:
  44. #     Date       |  Version    | Details
  45. #     2014.12.26 |   1.0       | 初版
  46. #     2016.04.30 |   1.1       | 范例优化,增加变量消耗判定
  47. #=============================================================================
  48. module RB
  49.   Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m
  50.   Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m
  51.   Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m
  52.   Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
  53. end
  54. module RPG
  55.   class Recipe
  56.     attr_accessor :id
  57.     attr_accessor :product
  58.     attr_accessor :ingredients
  59.     def initialize
  60.       @id = 0
  61.       @product = Ingredient.new
  62.       @ingredients = []
  63.     end
  64.     class Ingredient
  65.       attr_accessor :kind
  66.       attr_accessor :id
  67.       attr_accessor :number
  68.       def initialize(kind = 0, id = 0, number = 0)
  69.         @kind = kind
  70.         @id = id
  71.         @number = number
  72.       end
  73.     end
  74.   end
  75.   class Variable_Wrapper
  76.     attr_accessor :id
  77.     attr_accessor :name
  78.     attr_accessor :icon_name
  79.     def initialize(id)
  80.       @id = id
  81.       @name = $data_system.variables[id]
  82.       @icon_name = ""
  83.     end
  84.   end
  85.   def self.extract_recipe
  86.     filename = "Recipes.txt"
  87.     outfile_name = "Data/Recipes.rxdata"
  88.     data = []
  89.     begin
  90.       file = File.open(filename, "r")
  91.       str = file.read
  92.       file.close
  93.       while str.slice!(RB::Regex_Recipe)
  94.         id = $1.to_i
  95.         contents = $2
  96.         recipe = Recipe.new
  97.         recipe.id = id
  98.         if RB::Regex_Product =~ contents
  99.           recipe.product.kind = $1.to_i
  100.           recipe.product.id = $2.to_i
  101.         end
  102.         if RB::Regex_Ingredient =~ contents
  103.           ingredient_contents = $1
  104.           ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|
  105.             recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
  106.           end
  107.         end
  108.         data[id] = recipe
  109.       end
  110.       outfile = File.open(outfile_name, "wb")
  111.       Marshal.dump(data, outfile)
  112.       outfile.close
  113.     rescue
  114.       print "Recipes.txt中的数据可能含有无效格式"
  115.       exit
  116.     end
  117.   end
  118. end

  119. class Game_Party
  120.   alias rb_initialize_20141226 initialize
  121.   def initialize
  122.     rb_initialize_20141226
  123.     @recipes = {}
  124.   end
  125.   def recipe_learn?(recipe_id)
  126.     @recipes ||= {}
  127.     return @recipes[recipe_id]
  128.   end
  129.   def learn_recipe(recipe_id)
  130.     @recipes ||= {}
  131.     @recipes[recipe_id] = true
  132.   end
  133.   def forget_recipe(recipe_id)
  134.     @recipes ||= {}
  135.     @recipes[recipe_id] = nil
  136.   end
  137.   def product_max_num(recipe_id)
  138.     max = -1
  139.     recipe = $data_recipes[recipe_id]
  140.     return 0 if recipe.nil?
  141.     recipe.ingredients.each do |ingredient|
  142.       num_need = ingredient.number
  143.       case ingredient.kind
  144.       when 0
  145.         num_current = item_number(ingredient.id)
  146.       when 1
  147.         num_current = weapon_number(ingredient.id)
  148.       when 2
  149.         num_current = armor_number(ingredient.id)
  150.       when 3
  151.         num_current = $game_variables[ingredient.id]
  152.       else
  153.         num_current = 0
  154.       end
  155.       max = num_current / num_need if max == -1 || num_current / num_need < max
  156.     end
  157.     return max == -1 ? 0 : max
  158.   end
  159. end

  160. class Window_Product < Window_Selectable
  161.   def initialize(mode = -1)
  162.     super(0, 64, 240, 480 - 64)
  163.     self.index = 0
  164.     @mode = mode
  165.     refresh
  166.   end
  167.   def recipe
  168.     return @data[self.index]
  169.   end
  170.   def item
  171.     recipe = self.recipe
  172.     return nil if recipe.nil?
  173.     product = recipe.product
  174.     case product.kind
  175.     when 0
  176.       item = $data_items[product.id]
  177.     when 1
  178.       item = $data_weapons[product.id]
  179.     when 2
  180.       item = $data_armors[product.id]
  181.     else
  182.       item = nil
  183.     end
  184.     return item
  185.   end
  186.   def refresh
  187.     if self.contents != nil
  188.       self.contents.dispose
  189.       self.contents = nil
  190.     end
  191.     @data = []
  192.     (1...$data_recipes.size).each do |i|
  193.       if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
  194.         @data << $data_recipes[i] if $data_recipes[i] != nil
  195.       end
  196.     end
  197.     @item_max = @data.size
  198.     if @item_max > 0
  199.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  200.       @data.each_index{|i| draw_item(i)}
  201.     else
  202.       self.contents = Bitmap.new(width - 32, 32)
  203.       self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
  204.     end
  205.   end
  206.   def draw_item(index)
  207.     x = 4
  208.     y = 32 * index
  209.     product = @data[index].product
  210.     case product.kind
  211.     when 0
  212.       item = $data_items[product.id]
  213.     when 1
  214.       item = $data_weapons[product.id]
  215.     when 2
  216.       item = $data_armors[product.id]
  217.     else
  218.       item = nil
  219.     end
  220.     if item == nil
  221.       return
  222.     end
  223.     disabled = $game_party.product_max_num(@data[index].id) == 0
  224.     bitmap = RPG::Cache.icon(item.icon_name)
  225.     opacity = disabled ? 128 : 255
  226.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  227.     self.contents.font.color = disabled ? disabled_color : normal_color
  228.     self.contents.draw_text(x + 28, y, 176, 32, item.name)
  229.   end
  230.   def update_help
  231.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  232.   end
  233. end

  234. class Window_Ingredients < Window_Base
  235.   def initialize
  236.     super(240, 288, 400, 192)
  237.     @waiting = 0
  238.     @dy = 1
  239.     @recipe = nil
  240.     refresh
  241.   end
  242.   def recipe=(recipe)
  243.     if @recipe != recipe
  244.       @recipe = recipe
  245.       @waiting = 40
  246.       @dy = 1
  247.       self.oy = 0
  248.       refresh
  249.     end
  250.   end
  251.   def refresh
  252.     if self.contents != nil
  253.       self.contents.dispose
  254.       self.contents = nil
  255.     end
  256.     if @recipe == nil
  257.       return
  258.     end
  259.     h = 32 * @recipe.ingredients.size
  260.     if h > 0
  261.       self.contents = Bitmap.new(width - 32, h)
  262.       @recipe.ingredients.each_with_index do |ingredient, index|
  263.         draw_item(ingredient, index)
  264.       end
  265.     end
  266.   end
  267.   def draw_item(ingredient, index)
  268.     x = 4
  269.     y = index * 32
  270.     case ingredient.kind
  271.     when 0
  272.       item = $data_items[ingredient.id]
  273.       num = $game_party.item_number(ingredient.id)
  274.     when 1
  275.       item = $data_weapons[ingredient.id]
  276.       num = $game_party.weapon_number(ingredient.id)
  277.     when 2
  278.       item = $data_armors[ingredient.id]
  279.       num = $game_party.armor_number(ingredient.id)
  280.     when 3
  281.       item = RPG::Variable_Wrapper.new(ingredient.id)
  282.       num = $game_variables[ingredient.id]
  283.     else
  284.       item = nil
  285.     end
  286.     if item == nil
  287.       return
  288.     end
  289.     bitmap = RPG::Cache.icon(item.icon_name)
  290.     disabled = num < ingredient.number
  291.     opacity = disabled ? 128 : 255
  292.     self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  293.     self.contents.font.color = disabled ? disabled_color : normal_color
  294.     self.contents.draw_text(x + 28, y, 192, 32, item.name)
  295.     self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
  296.     self.contents.font.color = normal_color
  297.     self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
  298.   end
  299.   def update
  300.     super
  301.     if self.contents != nil && self.contents.height > self.height - 32
  302.       if @waiting > 0
  303.         @waiting -= 1
  304.         return
  305.       end
  306.       self.oy += @dy
  307.       if self.oy + self.height - 32 >= self.contents.height
  308.         @dy = -1
  309.         @waiting = 60
  310.       end
  311.       if self.oy <= 0
  312.         @dy = 1
  313.         @waiting = 60
  314.       end
  315.     end
  316.   end
  317. end

  318. class Window_Craft_Number < Window_Base
  319.   def initialize
  320.     super(240, 64, 400, 160)
  321.     self.contents = Bitmap.new(width - 32, height - 32)
  322.     @recipe = nil
  323.     @max = 1
  324.     @number = 1
  325.     @number_current = 0
  326.   end
  327.   def recipe=(recipe)
  328.     if @recipe != recipe
  329.       @recipe = recipe
  330.       reset
  331.       refresh
  332.     end
  333.   end
  334.   def reset
  335.     if @recipe == nil
  336.       @number = 0
  337.       @number_current = 0
  338.       return
  339.     end
  340.     product = @recipe.product
  341.     case product.kind
  342.     when 0
  343.       @number_current = $game_party.item_number(product.id)
  344.     when 1
  345.       @number_current = $game_party.weapon_number(product.id)
  346.     when 2
  347.       @number_current = $game_party.armor_number(product.id)
  348.     else
  349.       @number_current = 0
  350.     end
  351.     @max = $game_party.product_max_num(@recipe.id)
  352.     @max = [@max, 99 - @number_current].min
  353.     @number = @max == 0 ? 0 : 1
  354.   end
  355.   def number
  356.     return @number
  357.   end
  358.   #--------------------------------------------------------------------------
  359.   # ● 刷新
  360.   #--------------------------------------------------------------------------
  361.   def refresh
  362.     self.contents.clear
  363.     self.contents.font.color = system_color
  364.     self.contents.draw_text(4, 0, 364, 32, "所持数")
  365.     self.contents.draw_text(4, 32, 364, 32, "最大可合成数")
  366.     self.contents.draw_text(4, 64, 364, 32, "当前合成数量")
  367.     self.contents.font.color = normal_color
  368.     self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
  369.     self.contents.draw_text(4, 32, 364, 32, @max.to_s, 2)
  370.     self.contents.draw_text(252, 96, 32, 32, "×")
  371.     self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
  372.   end
  373.   def update
  374.     super
  375.     if self.active
  376.       # 光标右 (+1)
  377.       if Input.repeat?(Input::RIGHT) and @number < @max
  378.         $game_system.se_play($data_system.cursor_se)
  379.         @number += 1
  380.         refresh
  381.       end
  382.       # 光标左 (-1)
  383.       if Input.repeat?(Input::LEFT) and @number > 1
  384.         $game_system.se_play($data_system.cursor_se)
  385.         @number -= 1
  386.         refresh
  387.       end
  388.       # 光标上 (+10)
  389.       if Input.repeat?(Input::UP) and @number < @max
  390.         $game_system.se_play($data_system.cursor_se)
  391.         @number = [@number + 10, @max].min
  392.         refresh
  393.       end
  394.       # 光标下 (-10)
  395.       if Input.repeat?(Input::DOWN) and @number > 1
  396.         $game_system.se_play($data_system.cursor_se)
  397.         @number = [@number - 10, 1].max
  398.         refresh
  399.       end
  400.     end
  401.   end
  402. end
  403. class Window_Craft_Label < Window_Base
  404.   def initialize
  405.     super(240, 224, 400, 64)
  406.     self.contents = Bitmap.new(width - 32, height - 32)
  407.     refresh
  408.   end
  409.   def refresh
  410.     self.contents.clear
  411.     self.contents.font.color = system_color
  412.     self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  413.   end
  414. end

  415. class Scene_Title
  416.   alias rb_main_20141226 main
  417.   def main
  418.     if $DEBUG
  419.       RPG.extract_recipe
  420.     end
  421.     $data_recipes = load_data("Data/Recipes.rxdata")
  422.     rb_main_20141226
  423.   end
  424. end

  425. class Scene_Craft
  426.   def initialize(mode = -1)
  427.     @mode = mode
  428.   end
  429.   def main
  430.     @product_window = Window_Product.new(@mode)
  431.     @help_window = Window_Help.new
  432.     @product_window.help_window = @help_window
  433.     @ingredient_window = Window_Ingredients.new
  434.     @number_window = Window_Craft_Number.new
  435.     @label_window = Window_Craft_Label.new
  436.     Graphics.transition
  437.     loop do
  438.       Graphics.update
  439.       Input.update
  440.       update
  441.       if $scene != self
  442.         break
  443.       end
  444.     end
  445.     Graphics.freeze
  446.     @product_window.dispose
  447.     @help_window.dispose
  448.     @ingredient_window.dispose
  449.     @number_window.dispose
  450.     @label_window.dispose
  451.   end
  452.   def update
  453.     @product_window.update
  454.     @ingredient_window.update
  455.     @ingredient_window.recipe = @product_window.recipe
  456.     @number_window.recipe = @product_window.recipe
  457.     if @product_window.active
  458.       update_product
  459.       return
  460.     end
  461.     if @number_window.active
  462.       update_number
  463.     end
  464.   end
  465.   def update_product
  466.     if Input.trigger?(Input::B)
  467.       $game_system.se_play($data_system.cancel_se)
  468.       $scene = Scene_Map.new
  469.       return
  470.     end
  471.     if Input.trigger?(Input::C)
  472.       recipe = @product_window.recipe
  473.       if recipe == nil || $game_party.product_max_num(recipe.id) == 0
  474.         $game_system.se_play($data_system.buzzer_se)
  475.         return
  476.       end
  477.       $game_system.se_play($data_system.decision_se)
  478.       @number_window.cursor_rect.set(304, 96, 32, 32)
  479.       @product_window.active = false
  480.       @number_window.active = true
  481.       return
  482.     end
  483.   end
  484.   def update_number
  485.     @number_window.update
  486.     if Input.trigger?(Input::B)
  487.       $game_system.se_play($data_system.cancel_se)
  488.       @number_window.active = false
  489.       @product_window.active = true
  490.       @number_window.cursor_rect.empty
  491.       return
  492.     end
  493.     if Input.trigger?(Input::C)
  494.       $game_system.se_play($data_system.shop_se)
  495.       recipe = @product_window.recipe
  496.       case recipe.product.kind
  497.       when 0
  498.         $game_party.gain_item(recipe.product.id, @number_window.number)
  499.       when 1
  500.         $game_party.gain_weapon(recipe.product.id, @number_window.number)
  501.       when 2
  502.         $game_party.gain_armor(recipe.product.id, @number_window.number)
  503.       end
  504.       recipe.ingredients.each do |ingredient|
  505.         case ingredient.kind
  506.         when 0
  507.           $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
  508.         when 1
  509.           $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
  510.         when 2
  511.           $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
  512.         when 3
  513.           $game_variables[ingredient.id] -= ingredient.number * @number_window.number
  514.         end
  515.       end
  516.       @product_window.refresh
  517.       @ingredient_window.refresh
  518.       @number_window.reset
  519.       @number_window.refresh
  520.       @product_window.active = true
  521.       @number_window.active = false
  522.       @number_window.cursor_rect.empty
  523.       return
  524.     end
  525.   end
  526. end
复制代码

附上目前对问题1.的尝试结果的改动脚本:
点击展开/收起

  1. #=============================================================================
  2. # 物品合成简易优化版 Ver 1.1
  3. #-----------------------------------------------------------------------------
  4. # By : RyanBern
  5. #-----------------------------------------------------------------------------
  6. # 功能介绍:
  7. #     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
  8. #     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
  9. #     改成了外部文件的形式,下面就说明一下如何使用。
  10. #-----------------------------------------------------------------------------
  11. # 使用方法:
  12. #     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
  13. #       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
  14. #       <recipe=1>
  15. #         <product>kind=0,id=3</product>
  16. #         <ingredients>
  17. #           kind=0,id=1,number=2
  18. #           kind=0,id=2,number=2
  19. #         </ingredients>
  20. #       </recipe>
  21. #       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
  22. #       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
  23. #       就是对应的ID,最终合成物品在<product></product>之间
  24. #       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
  25. #       每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
  26. #       的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
  27. #       变量的ID。
  28. #       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
  29. #     2.有关学习配方:
  30. #       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
  31. #       $game_party.learn_recipe(配方ID) 学习该ID的配方
  32. #       $game_party.forget_recipe(配方ID) 失去该ID的配方
  33. #     3.有关场景调用:
  34. #       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
  35. #       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
  36. #       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
  37. #-----------------------------------------------------------------------------
  38. # 注意事项:
  39. #     1.Recipes.txt里面的缩进不是必须的。
  40. #     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
  41. #       以便日后做出更改。
  42. #-----------------------------------------------------------------------------
  43. # 更新记录:
  44. #     Date       |  Version    | Details
  45. #     2014.12.26 |   1.0       | 初版
  46. #     2016.04.30 |   1.1       | 范例优化,增加变量消耗判定
  47. #=============================================================================
  48. module RB
  49.   Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m #整的合成内容
  50.   Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m #目标物
  51.   #添加
  52.   Regex_Txt = /<txt>\s*txt\s*=(.*?)\s*<\/txt>/m
  53.   #——————————
  54.   Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m #需要素材
  55.   Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
  56. end
  57. module RPG
  58.   class Recipe# 配方 大类
  59.     attr_accessor :id
  60.     attr_accessor :product
  61.     attr_accessor :ingredients
  62.     def initialize
  63.       @id = 0
  64.       @product = Ingredient.new
  65.       @ingredients = []
  66.     end
  67.     # ● 新加入类——txt●●●●●●●●●●●
  68.     class Txt
  69.       attr_accessor :txt
  70.       def initialize(txt = "")
  71.         @txt = txt
  72.       end
  73.     end
  74.     # 素材类
  75.     class Ingredient
  76.       attr_accessor :kind
  77.       attr_accessor :id
  78.       attr_accessor :number
  79.       def initialize(kind = 0, id = 0, number = 0)
  80.         @kind = kind
  81.         @id = id
  82.         @number = number
  83.       end
  84.     end
  85.   end
  86.   # 变量大类
  87.   class Variable_Wrapper
  88.     attr_accessor :id
  89.     attr_accessor :name
  90.     attr_accessor :icon_name
  91.     def initialize(id)
  92.       @id = id
  93.       @name = $data_system.variables[id]
  94.       @icon_name = ""
  95.     end
  96.   end
  97.   # 读取文本————并给文本分类型●●●●●●●
  98.   def self.extract_recipe
  99.     filename = "Recipes.txt"#读取文本的名字
  100.     outfile_name = "Data/Recipes.rxdata"#输出转换文本的路径和名字
  101.     data = []
  102.     begin#每次测试开始都会进行这一读取程序
  103.       file = File.open(filename, "r")#以读取模式打开文件"Recipes.txt"
  104.       str = file.read#将字符串导入str
  105.       file.close  #关闭文件
  106.       while str.slice!(RB::Regex_Recipe)  #slice!的意思是:从字符串中删除指定的范围,然后返回被删除的子字符串
  107.         id = $1.to_i  #返回某符号所对应的整数,就是ID号,$1指的是正则表达式里第一个括号内容
  108.         contents = $2   #$2指的是正则表达式里第二个括号里的内容,contents是合成物品总目录
  109.         recipe = Recipe.new
  110.         recipe.id = id
  111.         if RB::Regex_Product =~ contents  #导入制作目标的内容
  112.           recipe.product.kind = $1.to_i   #导入物品种类号(.to_i是整数)
  113.           recipe.product.id = $2.to_i   #导入物品id号
  114.         end
  115.         #●●●●添加txt字段●●●●
  116.         if RB::Regex_Txt =~ contents
  117.           txt_contents = $1.to_s
  118.           #txt_contents.scan(RB::Regex_Txt) do |b| #依葫芦画瓢
  119.             #recipe.txt <<Recipe::Txt.new(b[0].to_s)
  120.           #end
  121.         end
  122.         if RB::Regex_Ingredient =~ contents #导入制作材料
  123.           ingredient_contents = $1 #这里导入的是ingredients总成,未分类
  124.           ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|#这里是将ingredients里的数据分类
  125.             recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
  126.                                                           #a[2]是合成素材需要数
  127.           end
  128.         end
  129.         data[id] = recipe #data的排列id就是合成物的id
  130.       end
  131.       outfile = File.open(outfile_name, "wb") #输出文件
  132.       Marshal.dump(data, outfile)  #把 obj 递归地写入文件。
  133.       outfile.close #关闭文件
  134.     rescue #发生异常时就会执行这一个内容
  135.       print "Recipes.txt中的数据可能含有无效格式"
  136.       exit
  137.     end
  138.   end
  139. end

  140. class Game_Party  #在Game_Party类里的再定义
  141.   alias rb_initialize_20141226 initialize   #为initialize 作别名
  142.   def initialize  #重新定义initialize(或者说,是在定义中添加新的东西!)
  143.     rb_initialize_20141226  #别名,把原有的弄进来
  144.     @recipes = {}   #加入新的实例变量recipes,哈希值,也就是组好的合成表
  145.   end
  146.   def recipe_learn?(recipe_id) #判断配方是否学习的 def
  147.     @recipes ||= {}
  148.     return @recipes[recipe_id]
  149.   end
  150.   def learn_recipe(recipe_id) #学习配方的 def
  151.     @recipes ||= {}
  152.     @recipes[recipe_id] = true
  153.   end
  154.   def forget_recipe(recipe_id) #忘记配方的 def
  155.     @recipes ||= {}
  156.     @recipes[recipe_id] = nil
  157.   end
  158.   def product_max_num(recipe_id) #计算当前可以的最大合成数
  159.     max = -1
  160.     recipe = $data_recipes[recipe_id]
  161.     return 0 if recipe.nil? #如果没有合成表,返回0为最大合成数
  162.     recipe.ingredients.each do |ingredient|
  163.       num_need = ingredient.number
  164.       case ingredient.kind
  165.       when 0
  166.         num_current = item_number(ingredient.id)
  167.       when 1
  168.         num_current = weapon_number(ingredient.id)
  169.       when 2
  170.         num_current = armor_number(ingredient.id)
  171.       when 3
  172.         num_current = $game_variables[ingredient.id]
  173.       else
  174.         num_current = 0
  175.       end
  176.       max = num_current / num_need if max == -1 || num_current / num_need < max
  177.     end
  178.     return max == -1 ? 0 : max
  179.   end
  180. end

  181. #————合成窗口左栏————
  182. class Window_Product < Window_Selectable
  183.   def initialize(mode = -1)
  184.     super(0, 64, 240, 480 - 64)
  185.     self.index = 0
  186.     @mode = mode
  187.     refresh
  188.   end
  189.   def recipe
  190.     return @data[self.index]
  191.   end
  192.   def item
  193.     recipe = self.recipe
  194.     return nil if recipe.nil?
  195.     product = recipe.product
  196.     case product.kind
  197.     when 0
  198.       item = $data_items[product.id]
  199.     when 1
  200.       item = $data_weapons[product.id]
  201.     when 2
  202.       item = $data_armors[product.id]
  203.     else
  204.       item = nil
  205.     end
  206.     return item
  207.   end
  208.   def refresh
  209.     if self.contents != nil
  210.       self.contents.dispose
  211.       self.contents = nil
  212.     end
  213.     @data = []
  214.     (1...$data_recipes.size).each do |i|
  215.       if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
  216.         @data << $data_recipes[i] if $data_recipes[i] != nil
  217.       end
  218.     end
  219.     @item_max = @data.size
  220.     if @item_max > 0
  221.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  222.       @data.each_index{|i| draw_item(i)}
  223.     else
  224.       self.contents = Bitmap.new(width - 32, 32)
  225.       self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
  226.     end
  227.   end
  228.   def draw_item(index)
  229.     x = 4
  230.     y = 32 * index
  231.     product = @data[index].product
  232.     case product.kind
  233.     when 0
  234.       item = $data_items[product.id]
  235.     when 1
  236.       item = $data_weapons[product.id]
  237.     when 2
  238.       item = $data_armors[product.id]
  239.     else
  240.       item = nil
  241.     end
  242.     if item == nil
  243.       return
  244.     end
  245.     disabled = $game_party.product_max_num(@data[index].id) == 0
  246.     bitmap = RPG::Cache.icon(item.icon_name)
  247.     opacity = disabled ? 128 : 255
  248.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  249.     self.contents.font.color = disabled ? disabled_color : normal_color
  250.     self.contents.draw_text(x + 28, y, 176, 32, item.name)
  251.   end
  252.   def update_help
  253.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  254.   end
  255. end
  256. #————合成窗口右下————
  257. class Window_Ingredients < Window_Base
  258.   def initialize
  259.     super(240, 288, 400, 192)
  260.     @waiting = 0
  261.     @dy = 1
  262.     @recipe = nil
  263.     refresh
  264.   end
  265.   def recipe=(recipe)
  266.     if @recipe != recipe
  267.       @recipe = recipe
  268.       @waiting = 40
  269.       @dy = 1
  270.       self.oy = 0
  271.       refresh
  272.     end
  273.   end
  274.   def refresh
  275.     if self.contents != nil
  276.       self.contents.dispose
  277.       self.contents = nil
  278.     end
  279.     if @recipe == nil
  280.       return
  281.     end
  282.     h = 32 * @recipe.ingredients.size
  283.     if h > 0
  284.       self.contents = Bitmap.new(width - 32, h)
  285.       @recipe.ingredients.each_with_index do |ingredient, index|
  286.         draw_item(ingredient, index)
  287.       end
  288.     end
  289.   end
  290.   def draw_item(ingredient, index)
  291.     x = 4
  292.     y = index * 32
  293.     case ingredient.kind
  294.     when 0
  295.       item = $data_items[ingredient.id]
  296.       num = $game_party.item_number(ingredient.id)
  297.     when 1
  298.       item = $data_weapons[ingredient.id]
  299.       num = $game_party.weapon_number(ingredient.id)
  300.     when 2
  301.       item = $data_armors[ingredient.id]
  302.       num = $game_party.armor_number(ingredient.id)
  303.     when 3
  304.       item = RPG::Variable_Wrapper.new(ingredient.id)
  305.       num = $game_variables[ingredient.id]
  306.     else
  307.       item = nil
  308.     end
  309.     if item == nil
  310.       return
  311.     end
  312.     bitmap = RPG::Cache.icon(item.icon_name)
  313.     disabled = num < ingredient.number
  314.     opacity = disabled ? 128 : 255
  315.     self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  316.     self.contents.font.color = disabled ? disabled_color : normal_color
  317.     self.contents.draw_text(x + 28, y, 192, 32, item.name)
  318.     self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
  319.     self.contents.font.color = normal_color
  320.     self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
  321.   end
  322.   def update
  323.     super
  324.     if self.contents != nil && self.contents.height > self.height - 32
  325.       if @waiting > 0
  326.         @waiting -= 1
  327.         return
  328.       end
  329.       self.oy += @dy
  330.       if self.oy + self.height - 32 >= self.contents.height
  331.         @dy = -1
  332.         @waiting = 60
  333.       end
  334.       if self.oy <= 0
  335.         @dy = 1
  336.         @waiting = 60
  337.       end
  338.     end
  339.   end
  340. end
  341. #————合成窗口右上————●●●●●●●●●●●●
  342. class Window_Craft_Number < Window_Base
  343.   def initialize
  344.     super(240, 64, 400, 160)
  345.     self.contents = Bitmap.new(width - 32, height - 32)
  346.     @recipe = nil
  347.     @max = 1
  348.     @txt = ""
  349.     @number = 1
  350.     @number_current = 0
  351.   end
  352.   def txt=(txt_contents) #●●●●txt在这里!
  353.     if @txt != txt_contents
  354.       @txt = txt_contents
  355.       reset
  356.       refresh
  357.     end
  358.   end
  359.   def recipe=(recipe)
  360.     if @recipe != recipe
  361.       @recipe = recipe
  362.       reset
  363.       refresh
  364.     end
  365.   end
  366.   def reset
  367.     if @recipe == nil
  368.       @number = 0
  369.       @number_current = 0
  370.       return
  371.     end
  372.     product = @recipe.product
  373.     case product.kind
  374.     when 0
  375.       @number_current = $game_party.item_number(product.id)
  376.     when 1
  377.       @number_current = $game_party.weapon_number(product.id)
  378.     when 2
  379.       @number_current = $game_party.armor_number(product.id)
  380.     else
  381.       @number_current = 0
  382.     end
  383.     @max = $game_party.product_max_num(@recipe.id)
  384.     @max = [@max, 99 - @number_current].min
  385.     @number = @max == 0 ? 0 : 1
  386.   end
  387.   def number
  388.     return @number
  389.   end
  390.   #--------------------------------------------------------------------------
  391.   # ● 刷新
  392.   #--------------------------------------------------------------------------
  393.   def refresh
  394.     self.contents.clear
  395.     self.contents.font.color = system_color
  396.     self.contents.draw_text(4, 0, 364, 32, "所持数")
  397.     self.contents.draw_text(4, 32, 364, 32, "说明:")
  398.     self.contents.draw_text(4, 64+32, 364, 32, "当前合成数量")
  399.     self.contents.font.color = normal_color
  400.     self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
  401.     self.contents.draw_text(4, 32, 364, 32, @txt.to_s, 2)
  402.     self.contents.draw_text(252, 96, 32, 32, "×")
  403.     self.contents.draw_text(308, 96, 24, 32, @number.to_s, 2)
  404.   end
  405.   def update
  406.     super
  407.     if self.active
  408.       # 光标右 (+1)
  409.       if Input.repeat?(Input::RIGHT) and @number < @max
  410.         $game_system.se_play($data_system.cursor_se)
  411.         @number += 1
  412.         refresh
  413.       end
  414.       # 光标左 (-1)
  415.       if Input.repeat?(Input::LEFT) and @number > 1
  416.         $game_system.se_play($data_system.cursor_se)
  417.         @number -= 1
  418.         refresh
  419.       end
  420.       # 光标上 (+10)
  421.       if Input.repeat?(Input::UP) and @number < @max
  422.         $game_system.se_play($data_system.cursor_se)
  423.         @number = [@number + 10, @max].min
  424.         refresh
  425.       end
  426.       # 光标下 (-10)
  427.       if Input.repeat?(Input::DOWN) and @number > 1
  428.         $game_system.se_play($data_system.cursor_se)
  429.         @number = [@number - 10, 1].max
  430.         refresh
  431.       end
  432.     end
  433.   end
  434. end
  435. class Window_Craft_Label < Window_Base
  436.   def initialize
  437.     super(240, 224, 400, 64)
  438.     self.contents = Bitmap.new(width - 32, height - 32)
  439.     refresh
  440.   end
  441.   def refresh
  442.     self.contents.clear
  443.     self.contents.font.color = system_color
  444.     self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  445.   end
  446. end

  447. class Scene_Title
  448.   alias rb_main_20141226 main
  449.   def main
  450.     if $DEBUG
  451.       RPG.extract_recipe
  452.     end
  453.     $data_recipes = load_data("Data/Recipes.rxdata")
  454.     rb_main_20141226
  455.   end
  456. end

  457. class Scene_Craft
  458.   def initialize(mode = -1)
  459.     @mode = mode
  460.   end
  461.   def main
  462.     @product_window = Window_Product.new(@mode)
  463.     @help_window = Window_Help.new
  464.     @product_window.help_window = @help_window
  465.     @ingredient_window = Window_Ingredients.new
  466.     @number_window = Window_Craft_Number.new
  467.     @label_window = Window_Craft_Label.new
  468.     Graphics.transition
  469.     loop do
  470.       Graphics.update
  471.       Input.update
  472.       update
  473.       if $scene != self
  474.         break
  475.       end
  476.     end
  477.     Graphics.freeze
  478.     @product_window.dispose
  479.     @help_window.dispose
  480.     @ingredient_window.dispose
  481.     @number_window.dispose
  482.     @label_window.dispose
  483.   end
  484.   def update
  485.     @product_window.update
  486.     @ingredient_window.update
  487.     @ingredient_window.recipe = @product_window.recipe
  488.     @number_window.recipe = @product_window.recipe
  489.     if @product_window.active
  490.       update_product
  491.       return
  492.     end
  493.     if @number_window.active
  494.       update_number
  495.     end
  496.   end
  497.   def update_product
  498.     if Input.trigger?(Input::B)
  499.       $game_system.se_play($data_system.cancel_se)
  500.       $scene = Scene_Map.new
  501.       return
  502.     end
  503.     if Input.trigger?(Input::C)
  504.       recipe = @product_window.recipe
  505.       if recipe == nil || $game_party.product_max_num(recipe.id) == 0
  506.         $game_system.se_play($data_system.buzzer_se)
  507.         return
  508.       end
  509.       $game_system.se_play($data_system.decision_se)
  510.       @number_window.cursor_rect.set(304, 96, 32, 32)
  511.       @product_window.active = false
  512.       @number_window.active = true
  513.       return
  514.     end
  515.   end
  516.   def update_number
  517.     @number_window.update
  518.     if Input.trigger?(Input::B)
  519.       $game_system.se_play($data_system.cancel_se)
  520.       @number_window.active = false
  521.       @product_window.active = true
  522.       @number_window.cursor_rect.empty
  523.       return
  524.     end
  525.     if Input.trigger?(Input::C)
  526.       $game_system.se_play($data_system.shop_se)
  527.       recipe = @product_window.recipe
  528.       case recipe.product.kind
  529.       when 0
  530.         $game_party.gain_item(recipe.product.id, @number_window.number)
  531.       when 1
  532.         $game_party.gain_weapon(recipe.product.id, @number_window.number)
  533.       when 2
  534.         $game_party.gain_armor(recipe.product.id, @number_window.number)
  535.       end
  536.       recipe.ingredients.each do |ingredient|
  537.         case ingredient.kind
  538.         when 0
  539.           $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
  540.         when 1
  541.           $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
  542.         when 2
  543.           $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
  544.         when 3
  545.           $game_variables[ingredient.id] -= ingredient.number * @number_window.number
  546.         end
  547.       end
  548.       @product_window.refresh
  549.       @ingredient_window.refresh
  550.       @number_window.reset
  551.       @number_window.refresh
  552.       @product_window.active = true
  553.       @number_window.active = false
  554.       @number_window.cursor_rect.empty
  555.       return
  556.     end
  557.   end
  558. end
复制代码


附上被引用的TXT
点击展开/收起
  1. <recipe=1>
  2.   <product>kind=0,id=3</product>
  3.   <txt>
  4.    txt = "说明5165162621"
  5.   </txt>
  6.   <ingredients>
  7.     kind = 0,id = 1,number = 2
  8.     kind = 0,id = 2,number = 2
  9.   </ingredients>
  10. </recipe>

  11. <recipe=2>
  12.   <product>kind=1,id=4</product>
  13.   <txt>
  14.    txt = "说明1515616516"
  15.   </txt>
  16.   <ingredients>
  17.     kind = 1,id = 1,number = 1
  18.     kind = 0,id = 2,number = 2
  19.     kind = 0, id = 4, number = 8
  20.     kind = 0,id = 5, number = 2
  21.     kind = 0,id = 6, number = 7
  22.     kind = 1,id = 3, number = 1
  23.   </ingredients>
  24. </recipe>

  25. <recipe=3>
  26.   <product>kind=0,id=6</product>
  27.   <txt>
  28.    txt = "说明1651"
  29.   </txt>
  30.   <ingredients>
  31.     kind = 0,id = 4, number = 2
  32.     kind = 0,id = 5, number = 2
  33.   </ingredients>
  34. </recipe>

  35. <recipe=4>
  36.   <product>kind=2,id=4</product>
  37.   <txt>
  38.    txt = "说明"
  39.   </txt>
  40.   <ingredients>
  41.     kind = 2,id = 3, number = 1
  42.     kind = 3,id = 1, number = 2
  43.   </ingredients>
  44. </recipe>
复制代码


脚本已经被我改得乱七八糟了,备注都用麻了......
但依然没有成功,所以在此向大侠们江湖救急~~~










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

开拓者贵宾剧作品鉴家

发表于 2021-6-19 00:32:38 | 显示全部楼层
本帖最后由 RyanBern 于 2021-6-19 00:33 编辑

现在居然还有人在用这么简陋的脚本么……

提示几点吧:
1. 配方(Recipe)中因为说明文字只有一组,所以不必新建一个 class 来表示说明文字,用一个字符串就好。Recipe.txt 中可以直接写
<txt>说明文字</txt>,读取后直接令 recipe.txt = $1 即可
2. 因为 txt 已经在 @recipe对象里面了,每次左侧光标移动会重新设置 @recipe的值,因此你的 355-361 行就不要了,404 行直接对 @recipe.txt 进行绘制
3. 如果需要根据合成数量变化,得想办法上右下面板获取到右上面板的 @number,并且在 @number 改动的时候同时刷新右侧两个窗口
回复 1 0

使用道具 举报

梦石
24
星屑
8942
在线时间
248 小时
注册时间
2020-12-4
回帖
259

极短24获奖极短23获奖极短22获奖

 楼主| 发表于 2021-6-19 03:14:58 | 显示全部楼层
本帖最后由 纯属小虫 于 2021-6-19 14:41 编辑
RyanBern 发表于 2021-6-19 00:32
现在居然还有人在用这么简陋的脚本么……

提示几点吧:


感谢版主这么晚了还回复得这么快,能得到作者的亲自回复真是受宠若惊~~
合成说明已经成功加入了,虽然方法比较笨,但实现了编辑起来比较便捷的分行说明:(图片在附件)


以下是修正后的脚本
增添了说明的合成脚本
  1. #=============================================================================
  2. # 物品合成简易优化版 Ver 1.1
  3. #-----------------------------------------------------------------------------
  4. # By : RyanBern
  5. #-----------------------------------------------------------------------------
  6. # 功能介绍:
  7. #     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
  8. #     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
  9. #     改成了外部文件的形式,下面就说明一下如何使用。
  10. #-----------------------------------------------------------------------------
  11. # 使用方法:
  12. #     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
  13. #       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
  14. #       <recipe=1>
  15. #         <product>kind=0,id=3</product>
  16. #         <ingredients>
  17. #           kind=0,id=1,number=2
  18. #           kind=0,id=2,number=2
  19. #         </ingredients>
  20. #       </recipe>
  21. #       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
  22. #       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
  23. #       就是对应的ID,最终合成物品在<product></product>之间
  24. #       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
  25. #       每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
  26. #       的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
  27. #       变量的ID。
  28. #       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
  29. #     2.有关学习配方:
  30. #       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
  31. #       $game_party.learn_recipe(配方ID) 学习该ID的配方
  32. #       $game_party.forget_recipe(配方ID) 失去该ID的配方
  33. #     3.有关场景调用:
  34. #       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
  35. #       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
  36. #       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
  37. #-----------------------------------------------------------------------------
  38. # 注意事项:
  39. #     1.Recipes.txt里面的缩进不是必须的。
  40. #     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
  41. #       以便日后做出更改。
  42. #-----------------------------------------------------------------------------
  43. # 更新记录:
  44. #     Date       |  Version    | Details
  45. #     2014.12.26 |   1.0       | 初版
  46. #     2016.04.30 |   1.1       | 范例优化,增加变量消耗判定
  47. #     2021.06.19 |   ???       | 将最大合成数量改为了合成说明!感谢版主教导!
  48. #=============================================================================
  49. module RB
  50.   Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m #整的合成内容
  51.   Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m #目标物
  52.   #添加
  53.   Regex_Txt1 = /<txt1>(.*?)<\/txt1>/m
  54.   Regex_Txt2 = /<txt2>(.*?)<\/txt2>/m
  55.   Regex_Txt3 = /<txt3>(.*?)<\/txt3>/m
  56.   Regex_Txt4 = /<txt4>(.*?)<\/txt4>/m
  57.   #——————————
  58.   Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m #需要素材
  59.   Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
  60. end
  61. module RPG
  62.   class Recipe# 配方 大类
  63.     attr_accessor :id
  64.     attr_accessor :product
  65.     attr_accessor :txt1
  66.     attr_accessor :txt2
  67.     attr_accessor :txt3
  68.     attr_accessor :txt4
  69.     attr_accessor :ingredients
  70.     def initialize
  71.       @id = 0
  72.       @product = Ingredient.new
  73.       @txt1 = ""
  74.       @txt2 = ""
  75.       @txt3 = ""
  76.       @txt4 = ""
  77.       @ingredients = []
  78.     end
  79.     # 素材类
  80.     class Ingredient
  81.       attr_accessor :kind
  82.       attr_accessor :id
  83.       attr_accessor :number
  84.       def initialize(kind = 0, id = 0, number = 0)
  85.         @kind = kind
  86.         @id = id
  87.         @number = number
  88.       end
  89.     end
  90.   end
  91.   # 变量大类
  92.   class Variable_Wrapper
  93.     attr_accessor :id
  94.     attr_accessor :name
  95.     attr_accessor :icon_name
  96.     def initialize(id)
  97.       @id = id
  98.       @name = $data_system.variables[id]
  99.       @icon_name = ""
  100.     end
  101.   end
  102.   # 读取文本————并给文本分类型●●●●●●●
  103.   def self.extract_recipe
  104.     filename = "Recipes.txt"#读取文本的名字
  105.     outfile_name = "Data/Recipes.rxdata"#输出转换文本的路径和名字
  106.     data = []
  107.     begin#每次测试开始都会进行这一读取程序
  108.       file = File.open(filename, "r")#以读取模式打开文件"Recipes.txt"
  109.       str = file.read#将字符串导入str
  110.       file.close  #关闭文件
  111.       while str.slice!(RB::Regex_Recipe)  #slice!的意思是:从字符串中删除指定的范围,然后返回被删除的子字符串
  112.         id = $1.to_i  #返回某符号所对应的整数,就是ID号,$1指的是正则表达式里第一个括号内容
  113.         contents = $2   #$2指的是正则表达式里第二个括号里的内容,contents是合成物品总目录
  114.         recipe = Recipe.new
  115.         recipe.id = id

  116.         if RB::Regex_Product =~ contents  #导入制作目标的内容
  117.           recipe.product.kind = $1.to_i   #导入物品种类号(.to_i是整数)
  118.           recipe.product.id = $2.to_i   #导入物品id号
  119.         end
  120.         if RB::Regex_Txt1 =~ contents #●●●●添加txt字段●●●●
  121.           recipe.txt1 = $1   
  122.         end
  123.         if RB::Regex_Txt2 =~ contents #●●●●添加txt字段●●●●
  124.           recipe.txt2 = $1   
  125.         end
  126.         if RB::Regex_Txt3 =~ contents #●●●●添加txt字段●●●●
  127.           recipe.txt3 = $1   
  128.         end
  129.         if RB::Regex_Txt4 =~ contents #●●●●添加txt字段●●●●
  130.           recipe.txt4 = $1   
  131.         end
  132.         if RB::Regex_Ingredient =~ contents #导入制作材料
  133.           ingredient_contents = $1 #这里导入的是ingredients总成,未分类
  134.           ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|#这里是将ingredients里的数据分类
  135.             recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
  136.                                                           #a[2]是合成素材需要数
  137.           end
  138.         end
  139.         data[id] = recipe #data的排列id就是合成物的id
  140.       end
  141.       outfile = File.open(outfile_name, "wb") #输出文件
  142.       Marshal.dump(data, outfile)  #把 obj 递归地写入文件。
  143.       outfile.close #关闭文件
  144.     rescue #发生异常时就会执行这一个内容
  145.       print "Recipes.txt中的数据可能含有无效格式"
  146.       exit
  147.     end
  148.   end
  149. end

  150. class Game_Party  #在Game_Party类里的再定义
  151.   alias rb_initialize_20141226 initialize   #为initialize 作别名
  152.   def initialize  #重新定义initialize(或者说,是在定义中添加新的东西!)
  153.     rb_initialize_20141226  #别名,把原有的弄进来
  154.     @recipes = {}   #加入新的实例变量recipes,哈希值,也就是组好的合成表
  155.   end
  156.   def recipe_learn?(recipe_id) #判断配方是否学习的 def
  157.     @recipes ||= {}
  158.     return @recipes[recipe_id]
  159.   end
  160.   def learn_recipe(recipe_id) #学习配方的 def
  161.     @recipes ||= {}
  162.     @recipes[recipe_id] = true
  163.   end
  164.   def forget_recipe(recipe_id) #忘记配方的 def
  165.     @recipes ||= {}
  166.     @recipes[recipe_id] = nil
  167.   end
  168.   def product_max_num(recipe_id) #计算当前可以的最大合成数
  169.     max = -1
  170.     recipe = $data_recipes[recipe_id]
  171.     return 0 if recipe.nil? #如果没有合成表,返回0为最大合成数
  172.     recipe.ingredients.each do |ingredient| #循环,代入需要素材以及数量的数据
  173.       num_need = ingredient.number  #需求量等于素材需求量●●这里是不是可以做手脚)
  174.       case ingredient.kind #判断物品种类
  175.       when 0
  176.         num_current = item_number(ingredient.id) #现有的物品数量
  177.       when 1
  178.         num_current = weapon_number(ingredient.id) #现有的武器数量
  179.       when 2
  180.         num_current = armor_number(ingredient.id) #现有的防具数量
  181.       when 3
  182.         num_current = $game_variables[ingredient.id] #现有的变量数量
  183.       else
  184.         num_current = 0
  185.       end
  186.       #计算最大可合成的数量的公式
  187.       max = num_current / num_need if max == -1 || num_current / num_need < max
  188.     end
  189.     return max == -1 ? 0 : max #如果max为-1 啧最大合成数为0 ;如果不是则为max本身
  190.   end
  191. end

  192. #————合成窗口左栏————
  193. class Window_Product < Window_Selectable
  194.   def initialize(mode = -1) #初始化,mode默认值为-1
  195.     super(0, 64, 240, 480 - 64) #super将调用被当前方法覆盖的父类中的同名方法,所以这里是创造窗口!
  196.     self.index = 0 #默认的光标选择位置为0
  197.     @mode = mode
  198.     refresh
  199.   end
  200.   def recipe
  201.     return @data[self.index] #输出自己序号的 配方
  202.   end
  203.   def item #获取描绘左侧窗口的项目
  204.     recipe = self.recipe #recipe值为自己 的配方
  205.     return nil if recipe.nil? #如果配方空则为空
  206.     product = recipe.product
  207.     case product.kind
  208.     when 0
  209.       item = $data_items[product.id]
  210.     when 1
  211.       item = $data_weapons[product.id]
  212.     when 2
  213.       item = $data_armors[product.id]
  214.     else
  215.       item = nil
  216.     end
  217.     return item
  218.   end
  219.   def refresh #闪烁?刷新?
  220.     if self.contents != nil
  221.       self.contents.dispose
  222.       self.contents = nil
  223.     end
  224.     @data = []
  225.     (1...$data_recipes.size).each do |i|
  226.       if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
  227.         @data << $data_recipes[i] if $data_recipes[i] != nil
  228.       end
  229.     end
  230.     @item_max = @data.size
  231.     if @item_max > 0
  232.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  233.       @data.each_index{|i| draw_item(i)}
  234.     else
  235.       self.contents = Bitmap.new(width - 32, 32)
  236.       self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
  237.     end
  238.   end
  239.   
  240.   def draw_item(index) #描绘项目
  241.     x = 4
  242.     y = 32 * index
  243.     product = @data[index].product
  244.     case product.kind
  245.     when 0
  246.       item = $data_items[product.id]
  247.     when 1
  248.       item = $data_weapons[product.id]
  249.     when 2
  250.       item = $data_armors[product.id]
  251.     else
  252.       item = nil
  253.     end
  254.     if item == nil
  255.       return
  256.     end
  257.     disabled = $game_party.product_max_num(@data[index].id) == 0
  258.     bitmap = RPG::Cache.icon(item.icon_name)
  259.     opacity = disabled ? 128 : 255
  260.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  261.     self.contents.font.color = disabled ? disabled_color : normal_color
  262.     self.contents.draw_text(x + 28, y, 176, 32, item.name)
  263.   end
  264.   def update_help
  265.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  266.   end
  267. end
  268. #————合成窗口右下——————————————————————————
  269. class Window_Ingredients < Window_Base
  270.   def initialize
  271.     super(240, 288, 400, 192)
  272.     @waiting = 0
  273.     @dy = 1
  274.     @recipe = nil
  275.     refresh
  276.   end
  277.   def recipe=(recipe)
  278.     if @recipe != recipe
  279.       @recipe = recipe
  280.       @waiting = 40
  281.       @dy = 1
  282.       self.oy = 0
  283.       refresh
  284.     end
  285.   end
  286.   def refresh
  287.     if self.contents != nil
  288.       self.contents.dispose
  289.       self.contents = nil
  290.     end
  291.     if @recipe == nil
  292.       return
  293.     end
  294.     h = 32 * @recipe.ingredients.size
  295.     if h > 0
  296.       self.contents = Bitmap.new(width - 32, h)
  297.       @recipe.ingredients.each_with_index do |ingredient, index|
  298.         draw_item(ingredient, index)
  299.       end
  300.     end
  301.   end
  302.   def draw_item(ingredient, index)
  303.     x = 4
  304.     y = index * 32
  305.     case ingredient.kind  #带入需要的素材量,●●在这里可以改成随合成数量变化!
  306.     when 0
  307.       item = $data_items[ingredient.id]
  308.       num = $game_party.item_number(ingredient.id) #*
  309.     when 1
  310.       item = $data_weapons[ingredient.id]
  311.       num = $game_party.weapon_number(ingredient.id)
  312.     when 2
  313.       item = $data_armors[ingredient.id]
  314.       num = $game_party.armor_number(ingredient.id)
  315.     when 3
  316.       item = RPG::Variable_Wrapper.new(ingredient.id)
  317.       num = $game_variables[ingredient.id]
  318.     else
  319.       item = nil
  320.     end
  321.     if item == nil
  322.       return
  323.     end
  324.     bitmap = RPG::Cache.icon(item.icon_name)
  325.     disabled = num < ingredient.number
  326.     opacity = disabled ? 128 : 255 #通过判定是否满足条件来展示不同透明度
  327.     self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  328.     self.contents.font.color = disabled ? disabled_color : normal_color
  329.     self.contents.draw_text(x + 28, y, 192, 32, item.name)
  330.     self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
  331.     self.contents.font.color = normal_color#这里可以描绘颜色!
  332.     #下面这句是所需要素材量的描述,如果没法改就去前面改!
  333.     self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
  334.   end
  335.   def update
  336.     super
  337.     if self.contents != nil && self.contents.height > self.height - 32
  338.       if @waiting > 0
  339.         @waiting -= 1
  340.         return
  341.       end
  342.       self.oy += @dy
  343.       if self.oy + self.height - 32 >= self.contents.height
  344.         @dy = -1
  345.         @waiting = 60
  346.       end
  347.       if self.oy <= 0
  348.         @dy = 1
  349.         @waiting = 60
  350.       end
  351.     end
  352.   end
  353. end
  354. #————合成窗口右上————●●●●●●●●●●●●
  355. class Window_Craft_Number < Window_Base
  356.   def initialize #初始化
  357.     super(240, 64, 400, 160)
  358.     self.contents = Bitmap.new(width - 32, height - 32)
  359.     @recipe = nil
  360.     @max = 1
  361.     @number = 1
  362.     @number_current = 0
  363.   end
  364.   def recipe=(recipe) #带入一个实例变量(整个recipe的值,里头包含全部了)
  365.     if @recipe != recipe
  366.       @recipe = recipe
  367.       reset
  368.       refresh
  369.     end
  370.   end
  371.   def reset #重置数据(刷新)
  372.     if @recipe == nil
  373.       @number = 0
  374.       @number_current = 0
  375.       return
  376.     end
  377.     product = @recipe.product
  378.     case product.kind
  379.     when 0
  380.       @number_current = $game_party.item_number(product.id)
  381.     when 1
  382.       @number_current = $game_party.weapon_number(product.id)
  383.     when 2
  384.       @number_current = $game_party.armor_number(product.id)
  385.     else
  386.       @number_current = 0
  387.     end
  388.     @max = $game_party.product_max_num(@recipe.id)
  389.     @max = [@max, 99 - @number_current].min
  390.     @number = @max == 0 ? 0 : 1
  391.   end
  392.   def number
  393.     return @number
  394.   end
  395.   #--------------------------------------------------------------------------
  396.   # ● 刷新
  397.   #--------------------------------------------------------------------------
  398.   def refresh #描绘右上角的各个项目的【方法】
  399.     self.contents.clear
  400.     self.contents.font.color = system_color
  401.     self.contents.draw_text(4, 0, 364, 32, "所持数")
  402.    
  403.     a=17
  404.     b=18
  405.     self.contents.font.size =8
  406.     self.contents.draw_text(6, 32-9+b*0, 364, 32, "■") #文字前缀
  407.     self.contents.draw_text(6, 32-9+b*1, 364, 32, "■") #文字前缀
  408.     self.contents.draw_text(6, 32-9+b*2, 364, 32, "■") #文字前缀
  409.     self.contents.draw_text(6, 32-9+b*3, 364, 32, "■") #文字前缀
  410.     self.contents.font.size =20
  411.    
  412.     self.contents.draw_text(4, 96+3, 364, 32, "当前合成数量")
  413.     self.contents.font.color = normal_color
  414.     self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
  415.     #说明文字描绘●●●●●●●●
  416.     self.contents.font.size =16
  417.     self.contents.draw_text(a, 23+b*0, 364, 32, @recipe.txt1.to_s)
  418.     self.contents.draw_text(a, 23+b*1, 364, 32, @recipe.txt2.to_s)
  419.     self.contents.draw_text(a, 23+b*2, 364, 32, @recipe.txt3.to_s)
  420.     self.contents.draw_text(a, 23+b*3, 364, 32, @recipe.txt4.to_s)
  421.     self.contents.font.size =20
  422.    
  423.     self.contents.draw_text(252, 96+3, 32, 32, "×")
  424.     self.contents.draw_text(308, 96+3, 24, 32, @number.to_s, 2)
  425.   end
  426.   def update
  427.     super
  428.     if self.active
  429.       # 光标右 (+1)
  430.       if Input.repeat?(Input::RIGHT) and @number < @max
  431.         $game_system.se_play($data_system.cursor_se)
  432.         @number += 1
  433.         refresh
  434.       end
  435.       # 光标左 (-1)
  436.       if Input.repeat?(Input::LEFT) and @number > 1
  437.         $game_system.se_play($data_system.cursor_se)
  438.         @number -= 1
  439.         refresh
  440.       end
  441.       # 光标上 (+10)
  442.       if Input.repeat?(Input::UP) and @number < @max
  443.         $game_system.se_play($data_system.cursor_se)
  444.         @number = [@number + 10, @max].min
  445.         refresh
  446.       end
  447.       # 光标下 (-10)
  448.       if Input.repeat?(Input::DOWN) and @number > 1
  449.         $game_system.se_play($data_system.cursor_se)
  450.         @number = [@number - 10, 1].max
  451.         refresh
  452.       end
  453.     end
  454.   end
  455. end
  456. #————————————描绘中间框——————————————
  457. class Window_Craft_Label < Window_Base
  458.   def initialize
  459.     super(240, 224, 400, 64)
  460.     self.contents = Bitmap.new(width - 32, height - 32)
  461.     refresh
  462.   end
  463.   def refresh
  464.     self.contents.clear
  465.     self.contents.font.color = system_color
  466.     self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  467.   end
  468. end

  469. class Scene_Title
  470.   alias rb_main_20141226 main
  471.   def main
  472.     if $DEBUG
  473.       RPG.extract_recipe
  474.     end
  475.     $data_recipes = load_data("Data/Recipes.rxdata")
  476.     rb_main_20141226
  477.   end
  478. end

  479. #——————————显示窗口 的最终显示处理————————————————
  480. class Scene_Craft
  481.   def initialize(mode = -1)
  482.     @mode = mode
  483.   end
  484.   def main
  485.     @product_window = Window_Product.new(@mode)
  486.     @help_window = Window_Help.new
  487.     @product_window.help_window = @help_window
  488.     @ingredient_window = Window_Ingredients.new
  489.     @number_window = Window_Craft_Number.new  #●●●右上角
  490.     @label_window = Window_Craft_Label.new
  491.     Graphics.transition
  492.     loop do
  493.       Graphics.update
  494.       Input.update
  495.       update
  496.       if $scene != self
  497.         break
  498.       end
  499.     end
  500.     Graphics.freeze
  501.     @product_window.dispose
  502.     @help_window.dispose
  503.     @ingredient_window.dispose
  504.     @number_window.dispose
  505.     @label_window.dispose
  506.   end
  507.   def update #显示各种数字等信息●●●
  508.     @product_window.update
  509.     @ingredient_window.update
  510.     @ingredient_window.recipe = @product_window.recipe
  511.     @number_window.recipe = @product_window.recipe #●●●右上角
  512.     if @product_window.active
  513.       update_product
  514.       return
  515.     end
  516.     if @number_window.active
  517.       update_number
  518.     end
  519.   end
  520.   def update_product
  521.     if Input.trigger?(Input::B)
  522.       $game_system.se_play($data_system.cancel_se)
  523.       $scene = Scene_Map.new
  524.       return
  525.     end
  526.     if Input.trigger?(Input::C)
  527.       recipe = @product_window.recipe
  528.       if recipe == nil || $game_party.product_max_num(recipe.id) == 0
  529.         $game_system.se_play($data_system.buzzer_se)
  530.         return
  531.       end
  532.       $game_system.se_play($data_system.decision_se)
  533.       @number_window.cursor_rect.set(304, 96, 32, 32)
  534.       @product_window.active = false
  535.       @number_window.active = true
  536.       return
  537.     end
  538.   end
  539.   def update_number
  540.     @number_window.update
  541.     if Input.trigger?(Input::B)
  542.       $game_system.se_play($data_system.cancel_se)
  543.       @number_window.active = false
  544.       @product_window.active = true
  545.       @number_window.cursor_rect.empty
  546.       return
  547.     end
  548.     if Input.trigger?(Input::C)
  549.       $game_system.se_play($data_system.shop_se)
  550.       recipe = @product_window.recipe
  551.       case recipe.product.kind
  552.       when 0
  553.         $game_party.gain_item(recipe.product.id, @number_window.number)
  554.       when 1
  555.         $game_party.gain_weapon(recipe.product.id, @number_window.number)
  556.       when 2
  557.         $game_party.gain_armor(recipe.product.id, @number_window.number)
  558.       end
  559.       recipe.ingredients.each do |ingredient|
  560.         case ingredient.kind
  561.         when 0
  562.           $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
  563.         when 1
  564.           $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
  565.         when 2
  566.           $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
  567.         when 3
  568.           $game_variables[ingredient.id] -= ingredient.number * @number_window.number
  569.         end
  570.       end
  571.       @product_window.refresh
  572.       @ingredient_window.refresh
  573.       @number_window.reset
  574.       @number_window.refresh
  575.       @product_window.active = true
  576.       @number_window.active = false
  577.       @number_window.cursor_rect.empty
  578.       return
  579.     end
  580.   end
  581. end
复制代码



【素材需求量变动】还没有尝试,接下来打算拼入主工程中进行合成系统换血~~~
再次感谢版主大大~~~


==========================================================================================
这里尝试解决第二个问题:让需求量随着合成数量变化,但是失败了:
添加的脚本为 336行-341行 400行-405行
但是运行时会报错:
339行发生了 TypeError, nil can't be coerced into Fixnum
有点诡异,我明明判断了nil,可是为什么还会出错
还有 ingredient.number.to_i 这一句是不是有问题,ingredient的数据结构有点弄不清楚
附上脚本 20210619
  1. #=============================================================================
  2. # 物品合成简易优化版 Ver 1.1
  3. #-----------------------------------------------------------------------------
  4. # By : RyanBern
  5. #-----------------------------------------------------------------------------
  6. # 功能介绍:
  7. #     这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
  8. #     通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
  9. #     改成了外部文件的形式,下面就说明一下如何使用。
  10. #-----------------------------------------------------------------------------
  11. # 使用方法:
  12. #     1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
  13. #       再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
  14. #       <recipe=1>
  15. #         <product>kind=0,id=3</product>
  16. #         <ingredients>
  17. #           kind=0,id=1,number=2
  18. #           kind=0,id=2,number=2
  19. #         </ingredients>
  20. #       </recipe>
  21. #       其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
  22. #       第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
  23. #       就是对应的ID,最终合成物品在<product></product>之间
  24. #       第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
  25. #       每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
  26. #       的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
  27. #       变量的ID。
  28. #       如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
  29. #     2.有关学习配方:
  30. #       $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
  31. #       $game_party.learn_recipe(配方ID) 学习该ID的配方
  32. #       $game_party.forget_recipe(配方ID) 失去该ID的配方
  33. #     3.有关场景调用:
  34. #       $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
  35. #       $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
  36. #       $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
  37. #-----------------------------------------------------------------------------
  38. # 注意事项:
  39. #     1.Recipes.txt里面的缩进不是必须的。
  40. #     2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
  41. #       以便日后做出更改。
  42. #-----------------------------------------------------------------------------
  43. # 更新记录:
  44. #     Date       |  Version    | Details
  45. #     2014.12.26 |   1.0       | 初版
  46. #     2016.04.30 |   1.1       | 范例优化,增加变量消耗判定
  47. #     2021.06.19 |   ???       | 将最大合成数量改为了合成说明!感谢版主教导!
  48. #=============================================================================
  49. module RB
  50.   Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m #整的合成内容
  51.   Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m #目标物
  52.   #添加
  53.   Regex_Txt1 = /<txt1>(.*?)<\/txt1>/m
  54.   Regex_Txt2 = /<txt2>(.*?)<\/txt2>/m
  55.   Regex_Txt3 = /<txt3>(.*?)<\/txt3>/m
  56.   Regex_Txt4 = /<txt4>(.*?)<\/txt4>/m
  57.   #——————————
  58.   Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m #需要素材
  59.   Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
  60. end
  61. module RPG
  62.   class Recipe# 配方 大类
  63.     attr_accessor :id
  64.     attr_accessor :product
  65.     attr_accessor :txt1
  66.     attr_accessor :txt2
  67.     attr_accessor :txt3
  68.     attr_accessor :txt4
  69.     attr_accessor :ingredients
  70.     def initialize
  71.       @id = 0
  72.       @product = Ingredient.new
  73.       @txt1 = ""
  74.       @txt2 = ""
  75.       @txt3 = ""
  76.       @txt4 = ""
  77.       @ingredients = []
  78.     end
  79.     # 素材类
  80.     class Ingredient
  81.       attr_accessor :kind
  82.       attr_accessor :id
  83.       attr_accessor :number
  84.       def initialize(kind = 0, id = 0, number = 0)
  85.         @kind = kind
  86.         @id = id
  87.         @number = number
  88.       end
  89.     end
  90.   end
  91.   # 变量大类
  92.   class Variable_Wrapper
  93.     attr_accessor :id
  94.     attr_accessor :name
  95.     attr_accessor :icon_name
  96.     def initialize(id)
  97.       @id = id
  98.       @name = $data_system.variables[id]
  99.       @icon_name = ""
  100.     end
  101.   end
  102.   # 读取文本————并给文本分类型●●●●●●●
  103.   def self.extract_recipe
  104.     filename = "Recipes.txt"#读取文本的名字
  105.     outfile_name = "Data/Recipes.rxdata"#输出转换文本的路径和名字
  106.     data = []
  107.     begin#每次测试开始都会进行这一读取程序
  108.       file = File.open(filename, "r")#以读取模式打开文件"Recipes.txt"
  109.       str = file.read#将字符串导入str
  110.       file.close  #关闭文件
  111.       while str.slice!(RB::Regex_Recipe)  #slice!的意思是:从字符串中删除指定的范围,然后返回被删除的子字符串
  112.         id = $1.to_i  #返回某符号所对应的整数,就是ID号,$1指的是正则表达式里第一个括号内容
  113.         contents = $2   #$2指的是正则表达式里第二个括号里的内容,contents是合成物品总目录
  114.         recipe = Recipe.new
  115.         recipe.id = id

  116.         if RB::Regex_Product =~ contents  #导入制作目标的内容
  117.           recipe.product.kind = $1.to_i   #导入物品种类号(.to_i是整数)
  118.           recipe.product.id = $2.to_i   #导入物品id号
  119.         end
  120.         if RB::Regex_Txt1 =~ contents #●●●●添加txt字段●●●●
  121.           recipe.txt1 = $1   
  122.         end
  123.         if RB::Regex_Txt2 =~ contents #●●●●添加txt字段●●●●
  124.           recipe.txt2 = $1   
  125.         end
  126.         if RB::Regex_Txt3 =~ contents #●●●●添加txt字段●●●●
  127.           recipe.txt3 = $1   
  128.         end
  129.         if RB::Regex_Txt4 =~ contents #●●●●添加txt字段●●●●
  130.           recipe.txt4 = $1   
  131.         end
  132.         if RB::Regex_Ingredient =~ contents #导入制作材料
  133.           ingredient_contents = $1 #这里导入的是ingredients总成,未分类
  134.           ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|#这里是将ingredients里的数据分类
  135.             recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
  136.                                                           #a[2]是合成素材需要数
  137.           end
  138.         end
  139.         data[id] = recipe #data的排列id就是合成物的id
  140.       end
  141.       outfile = File.open(outfile_name, "wb") #输出文件
  142.       Marshal.dump(data, outfile)  #把 obj 递归地写入文件。
  143.       outfile.close #关闭文件
  144.     rescue #发生异常时就会执行这一个内容
  145.       print "Recipes.txt中的数据可能含有无效格式"
  146.       exit
  147.     end
  148.   end
  149. end

  150. class Game_Party  #在Game_Party类里的再定义
  151.   alias rb_initialize_20141226 initialize   #为initialize 作别名
  152.   def initialize  #重新定义initialize(或者说,是在定义中添加新的东西!)
  153.     rb_initialize_20141226  #别名,把原有的弄进来
  154.     @recipes = {}   #加入新的实例变量recipes,哈希值,也就是组好的合成表
  155.   end
  156.   def recipe_learn?(recipe_id) #判断配方是否学习的 def
  157.     @recipes ||= {}
  158.     return @recipes[recipe_id]
  159.   end
  160.   def learn_recipe(recipe_id) #学习配方的 def
  161.     @recipes ||= {}
  162.     @recipes[recipe_id] = true
  163.   end
  164.   def forget_recipe(recipe_id) #忘记配方的 def
  165.     @recipes ||= {}
  166.     @recipes[recipe_id] = nil
  167.   end
  168.   def product_max_num(recipe_id) #计算当前可以的最大合成数
  169.     max = -1
  170.     recipe = $data_recipes[recipe_id]
  171.     return 0 if recipe.nil? #如果没有合成表,返回0为最大合成数
  172.     recipe.ingredients.each do |ingredient| #循环,代入需要素材以及数量的数据
  173.       num_need = ingredient.number  #需求量等于素材需求量●●这里是不是可以做手脚)
  174.       case ingredient.kind #判断物品种类
  175.       when 0
  176.         num_current = item_number(ingredient.id) #现有的物品数量
  177.       when 1
  178.         num_current = weapon_number(ingredient.id) #现有的武器数量
  179.       when 2
  180.         num_current = armor_number(ingredient.id) #现有的防具数量
  181.       when 3
  182.         num_current = $game_variables[ingredient.id] #现有的变量数量
  183.       else
  184.         num_current = 0
  185.       end
  186.       #计算最大可合成的数量的公式
  187.       max = num_current / num_need if max == -1 || num_current / num_need < max
  188.     end
  189.     return max == -1 ? 0 : max #如果max为-1 啧最大合成数为0 ;如果不是则为max本身
  190.   end
  191. end

  192. #————合成窗口左栏——————————————————————————
  193. class Window_Product < Window_Selectable
  194.   def initialize(mode = -1) #初始化,mode默认值为-1
  195.     super(0, 64, 240, 480 - 64) #super将调用被当前方法覆盖的父类中的同名方法,所以这里是创造窗口!
  196.     self.index = 0 #默认的光标选择位置为0
  197.     @mode = mode
  198.     refresh
  199.   end
  200.   def recipe
  201.     return @data[self.index] #输出自己序号的 配方
  202.   end
  203.   def item #获取描绘左侧窗口的项目
  204.     recipe = self.recipe #recipe值为自己 的配方
  205.     return nil if recipe.nil? #如果配方空则为空
  206.     product = recipe.product
  207.     case product.kind
  208.     when 0
  209.       item = $data_items[product.id]
  210.     when 1
  211.       item = $data_weapons[product.id]
  212.     when 2
  213.       item = $data_armors[product.id]
  214.     else
  215.       item = nil
  216.     end
  217.     return item
  218.   end
  219.   def refresh #闪烁?刷新?
  220.     if self.contents != nil
  221.       self.contents.dispose
  222.       self.contents = nil
  223.     end
  224.     @data = []
  225.     (1...$data_recipes.size).each do |i|
  226.       if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
  227.         @data << $data_recipes[i] if $data_recipes[i] != nil
  228.       end
  229.     end
  230.     @item_max = @data.size
  231.     if @item_max > 0
  232.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  233.       @data.each_index{|i| draw_item(i)}
  234.     else
  235.       self.contents = Bitmap.new(width - 32, 32)
  236.       self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
  237.     end
  238.   end
  239.   
  240.   def draw_item(index) #描绘项目
  241.     x = 4
  242.     y = 32 * index
  243.     product = @data[index].product
  244.     case product.kind
  245.     when 0
  246.       item = $data_items[product.id]
  247.     when 1
  248.       item = $data_weapons[product.id]
  249.     when 2
  250.       item = $data_armors[product.id]
  251.     else
  252.       item = nil
  253.     end
  254.     if item == nil
  255.       return
  256.     end
  257.     disabled = $game_party.product_max_num(@data[index].id) == 0
  258.     bitmap = RPG::Cache.icon(item.icon_name)
  259.     opacity = disabled ? 128 : 255
  260.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  261.     self.contents.font.color = disabled ? disabled_color : normal_color
  262.     self.contents.draw_text(x + 28, y, 176, 32, item.name)
  263.   end
  264.   def update_help
  265.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  266.   end
  267. end
  268. #————————————●●●●右下●●●●需求量 ————————————
  269. class Window_Ingredients < Window_Base
  270.   def initialize
  271.     super(240, 288, 400, 192)
  272.     @waiting = 0
  273.     @dy = 1
  274.     @recipe = nil
  275.     refresh
  276.   end
  277.   def recipe=(recipe)
  278.     if @recipe != recipe
  279.       @recipe = recipe
  280.       @waiting = 40
  281.       @dy = 1
  282.       self.oy = 0
  283.       refresh
  284.     end
  285.   end
  286.   def refresh
  287.     if self.contents != nil
  288.       self.contents.dispose
  289.       self.contents = nil
  290.     end
  291.     if @recipe == nil
  292.       return
  293.     end
  294.     h = 32 * @recipe.ingredients.size
  295.     if h > 0
  296.       self.contents = Bitmap.new(width - 32, h)
  297.       @recipe.ingredients.each_with_index do |ingredient, index|
  298.         draw_item(ingredient, index)
  299.       end
  300.     end
  301.   end

  302.   def draw_item(ingredient, index)
  303.     x = 4
  304.     y = index * 32
  305.     case ingredient.kind
  306.     when 0
  307.       item = $data_items[ingredient.id]
  308.       num = $game_party.item_number(ingredient.id) # num是持有量
  309.     when 1
  310.       item = $data_weapons[ingredient.id]
  311.       num = $game_party.weapon_number(ingredient.id)
  312.     when 2
  313.       item = $data_armors[ingredient.id]
  314.       num = $game_party.armor_number(ingredient.id)
  315.     when 3
  316.       item = RPG::Variable_Wrapper.new(ingredient.id)
  317.       num = $game_variables[ingredient.id]
  318.     else
  319.       item = nil
  320.     end
  321.     if item == nil
  322.       return
  323.     end
  324.     bitmap = RPG::Cache.icon(item.icon_name)
  325.     disabled = num < ingredient.number
  326.     opacity = disabled ? 128 : 255 #通过判定是否满足条件来展示不同透明度
  327.     self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
  328.     self.contents.font.color = disabled ? disabled_color : normal_color
  329.     self.contents.draw_text(x + 28, y, 192, 32, item.name)
  330.     self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
  331.     self.contents.font.color = normal_color#这里可以描绘颜色!
  332.     #合成素材的需求量!
  333.     @需求量 = ingredient.number.to_i
  334.     if @需求量 != nil
  335.       @需求量 *= $number_合成_translate
  336.     end
  337.     self.contents.draw_text(x + 264, y, 64, 32,@需求量, 2)
  338.     #self.contents.draw_text(x + 264, y, 64, 32,ingredient.number.to_s, 2)
  339.   end
  340.   def update
  341.     super
  342.     if self.contents != nil && self.contents.height > self.height - 32
  343.       if @waiting > 0
  344.         @waiting -= 1
  345.         return
  346.       end
  347.       self.oy += @dy
  348.       if self.oy + self.height - 32 >= self.contents.height
  349.         @dy = -1
  350.         @waiting = 60
  351.       end
  352.       if self.oy <= 0
  353.         @dy = 1
  354.         @waiting = 60
  355.       end
  356.     end
  357.   end
  358. end
  359. #——————————■■■■合成窗口右上■■■■————
  360. class Window_Craft_Number < Window_Base
  361.   def initialize #初始化
  362.     super(240, 64, 400, 160)
  363.     self.contents = Bitmap.new(width - 32, height - 32)
  364.     @recipe = nil
  365.     @max = 1
  366.     @number = 1
  367.     @number_current = 0
  368.   end
  369.   def recipe=(recipe) #带入一个实例变量(整个recipe的值,里头包含全部了)
  370.     if @recipe != recipe
  371.       @recipe = recipe
  372.       reset
  373.       refresh
  374.     end
  375.   end
  376.   def reset #重置数据(刷新)
  377.     if @recipe == nil
  378.       @number = 0
  379.       @number_current = 0
  380.       return
  381.     end
  382.     product = @recipe.product
  383.     case product.kind
  384.     when 0
  385.       @number_current = $game_party.item_number(product.id)
  386.     when 1
  387.       @number_current = $game_party.weapon_number(product.id)
  388.     when 2
  389.       @number_current = $game_party.armor_number(product.id)
  390.     else
  391.       @number_current = 0
  392.     end
  393.     @max = $game_party.product_max_num(@recipe.id)
  394.     @max = [@max, 99 - @number_current].min
  395.     @number = @max == 0 ? 0 : 1
  396.     #中转数
  397.     if @number < 1
  398.       $number_合成_translate = 1
  399.     else
  400.       $number_合成_translate = @number
  401.     end
  402.   end
  403.   def number
  404.     return @number
  405.   end
  406.   #--------------------------------------------------------------------------
  407.   # ● 刷新
  408.   #--------------------------------------------------------------------------
  409.   def refresh #描绘右上角的各个项目的【方法】
  410.     self.contents.clear
  411.     self.contents.font.color = system_color
  412.     self.contents.draw_text(4, 0, 364, 32, "所持数")
  413.     self.contents.font.color = normal_color
  414.     self.contents.draw_text(4+100, 0, 364, 32, @number_current.to_s)

  415.     #●说明文字描绘
  416.     a=17 #说明文字:左右偏移
  417.     b=18 #说明文字:行间距
  418.     self.contents.font.size =8
  419.     self.contents.draw_text(6, 32-9+b*0, 364, 32, "·") #说明文字前缀1-4
  420.     self.contents.draw_text(6, 32-9+b*1, 364, 32, "·")
  421.     self.contents.draw_text(6, 32-9+b*2, 364, 32, "·")
  422.     self.contents.draw_text(6, 32-9+b*3, 364, 32, "·")
  423.     self.contents.font.size =16
  424.     self.contents.font.color = system_color
  425.     self.contents.draw_text(a, 23+b*0, 364, 32, @recipe.txt1.to_s)
  426.     self.contents.draw_text(a, 23+b*1, 364, 32, @recipe.txt2.to_s)
  427.     self.contents.draw_text(a, 23+b*2, 364, 32, @recipe.txt3.to_s)
  428.     self.contents.draw_text(a, 23+b*3, 364, 32, @recipe.txt4.to_s)
  429.     self.contents.font.size =20
  430.     self.contents.font.color = normal_color
  431.    
  432.     self.contents.font.color = system_color
  433.     self.contents.draw_text(4+150, 96+3, 364, 32, "当前合成数量")
  434.     self.contents.draw_text(252+30, 96+3, 32, 32, "×")
  435.     self.contents.font.color = normal_color
  436.     self.contents.draw_text(308, 96+3, 24, 32, @number.to_s, 2)#合成数量
  437.   end
  438.   def update
  439.     super
  440.     if self.active
  441.       # 光标右 (+1)
  442.       if Input.repeat?(Input::RIGHT) and @number < @max
  443.         $game_system.se_play($data_system.cursor_se)
  444.         @number += 1
  445.         refresh
  446.       end
  447.       # 光标左 (-1)
  448.       if Input.repeat?(Input::LEFT) and @number > 1
  449.         $game_system.se_play($data_system.cursor_se)
  450.         @number -= 1
  451.         refresh
  452.       end
  453.       # 光标上 (+10)
  454.       if Input.repeat?(Input::UP) and @number < @max
  455.         $game_system.se_play($data_system.cursor_se)
  456.         @number = [@number + 10, @max].min
  457.         refresh
  458.       end
  459.       # 光标下 (-10)
  460.       if Input.repeat?(Input::DOWN) and @number > 1
  461.         $game_system.se_play($data_system.cursor_se)
  462.         @number = [@number - 10, 1].max
  463.         refresh
  464.       end
  465.     end
  466.   end
  467. end
  468. #————————————描绘中间框——————————————
  469. class Window_Craft_Label < Window_Base
  470.   def initialize
  471.     super(240, 224, 400, 64)
  472.     self.contents = Bitmap.new(width - 32, height - 32)
  473.     refresh
  474.   end
  475.   def refresh
  476.     self.contents.clear
  477.     self.contents.font.color = system_color
  478.     self.contents.draw_text(0, 0, width - 32, 32, "材料名称               现有  需要")
  479.   end
  480. end

  481. class Scene_Title
  482.   alias rb_main_20141226 main
  483.   def main
  484.     if $DEBUG
  485.       RPG.extract_recipe
  486.     end
  487.     $data_recipes = load_data("Data/Recipes.rxdata")
  488.     rb_main_20141226
  489.   end
  490. end

  491. #——————————显示窗口 的最终显示处理————————————————
  492. class Scene_Craft
  493.   def initialize(mode = -1)
  494.     @mode = mode
  495.   end
  496.   def main
  497.     @product_window = Window_Product.new(@mode)
  498.     @help_window = Window_Help.new
  499.     @product_window.help_window = @help_window
  500.     @ingredient_window = Window_Ingredients.new
  501.     @number_window = Window_Craft_Number.new  #●●●右上角
  502.     @label_window = Window_Craft_Label.new
  503.     Graphics.transition
  504.     loop do
  505.       Graphics.update
  506.       Input.update
  507.       update
  508.       if $scene != self
  509.         break
  510.       end
  511.     end
  512.     Graphics.freeze
  513.     @product_window.dispose
  514.     @help_window.dispose
  515.     @ingredient_window.dispose
  516.     @number_window.dispose
  517.     @label_window.dispose
  518.   end
  519.   def update #显示各种数字等信息●●●
  520.     @product_window.update
  521.     @ingredient_window.update
  522.     @ingredient_window.recipe = @product_window.recipe
  523.     @number_window.recipe = @product_window.recipe #●●●右上角
  524.     if @product_window.active
  525.       update_product
  526.       return
  527.     end
  528.     if @number_window.active
  529.       update_number
  530.     end
  531.   end
  532.   def update_product
  533.     if Input.trigger?(Input::B)
  534.       $game_system.se_play($data_system.cancel_se)
  535.       $scene = Scene_Map.new
  536.       return
  537.     end
  538.     if Input.trigger?(Input::C)
  539.       recipe = @product_window.recipe
  540.       if recipe == nil || $game_party.product_max_num(recipe.id) == 0
  541.         $game_system.se_play($data_system.buzzer_se)
  542.         return
  543.       end
  544.       $game_system.se_play($data_system.decision_se)
  545.       @number_window.cursor_rect.set(304, 96, 32, 32)
  546.       @product_window.active = false
  547.       @number_window.active = true
  548.       return
  549.     end
  550.   end
  551.   def update_number
  552.     @number_window.update
  553.     if Input.trigger?(Input::B)
  554.       $game_system.se_play($data_system.cancel_se)
  555.       @number_window.active = false
  556.       @product_window.active = true
  557.       @number_window.cursor_rect.empty
  558.       return
  559.     end
  560.     if Input.trigger?(Input::C)
  561.       $game_system.se_play($data_system.shop_se)
  562.       recipe = @product_window.recipe
  563.       case recipe.product.kind
  564.       when 0
  565.         $game_party.gain_item(recipe.product.id, @number_window.number)
  566.       when 1
  567.         $game_party.gain_weapon(recipe.product.id, @number_window.number)
  568.       when 2
  569.         $game_party.gain_armor(recipe.product.id, @number_window.number)
  570.       end
  571.       recipe.ingredients.each do |ingredient|
  572.         case ingredient.kind
  573.         when 0
  574.           $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
  575.         when 1
  576.           $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
  577.         when 2
  578.           $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
  579.         when 3
  580.           $game_variables[ingredient.id] -= ingredient.number * @number_window.number
  581.         end
  582.       end
  583.       @product_window.refresh
  584.       @ingredient_window.refresh
  585.       @number_window.reset
  586.       @number_window.refresh
  587.       @product_window.active = true
  588.       @number_window.active = false
  589.       @number_window.cursor_rect.empty
  590.       return
  591.     end
  592.   end
  593. end
复制代码

E{G0MQKU5IK2B}EVOQ)3WBE.png
回复

使用道具 举报

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

开拓者贵宾剧作品鉴家

发表于 2021-6-21 23:36:25 | 显示全部楼层
纯属小虫 发表于 2021-6-19 03:14
感谢版主这么晚了还回复得这么快,能得到作者的亲自回复真是受宠若惊~~
合成说明已经成功加入了,虽然方 ...

值为 nil 的不是被判断的变量,而是那个用于传递的全局变量。因为只有右上窗口先生成的时候才会设置这个值,如果生成顺序不对的话就会造成没有初始化的问题。

其实有关传递变量,更常见的做法是直接将两个窗口互相关联,这样更新右上窗口的时候顺带刷新右下窗口。需要改动右上窗口的 update 方法。
回复 1 0

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部