Advertisement
Szyu

Crafting System

Aug 26th, 2013
9,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 27.79 KB | None | 0 0
  1. #==============================================================================
  2. # Crafting System
  3. # Version 1.9
  4. # By Szyu
  5. #
  6. # About:
  7. # Craft items, weapons and armors.
  8. #
  9. # Instructions:
  10. # - Place below "? Materials" but above "? Main Process".
  11. # - Call global Crafting List by "SceneManager.call(Scene_Crafting)" or
  12. #       "SceneManager.call(Scene_Crafting, -1)".
  13. #   For categorized Crafting Lists use "SceneManager.call(Scene_Crafting, x)"
  14. #
  15. # How to Use:
  16. # - "<ingredients> content </ingredients>" marks the area for ingredients
  17. # example:
  18. #   <ingredients>
  19. #   i: 3x 5   => 3 items of item_id 5
  20. #   w: 2x 7   => 2 weapons of weapon_id 7
  21. #   a: 1x 2   => 1 armor of armor_id 2
  22. #   </ingredients>
  23. #
  24. # - "<recipe book>" marks the item as a recipe book, able to hold recipes
  25. # - "<category: x>" marks a recipe book as category x. You can call seperate
  26. #               category crafting lists by SceneManager.call(Scene_Crafting, x)
  27. #
  28. # - "<recipes> content </recipes>" marks the area for recipes if the
  29. #                                  item is a crafting book
  30. # example:
  31. #   <recipes>
  32. #   i: 5   => ability to craft the item with id 5
  33. #   w: 7   => ability to craft the weapon with id 7
  34. #   a: 2 - 20   => ability to craft armors from id 2 to id 20
  35. #   </recipes>
  36. #
  37. # - "$data_items[book_id].add_recipe("i/w/a: id")" adds a new recipe to a book
  38. # - "$data_items[book_id].add_recipe("i/w/a: id1 - id2")" adds a new recipe to a book
  39. # - "$data_items[book_id].remove_recipe("i/w/a: id")" removes a recipe from a book
  40. # - "$data_items[book_id].remove_recipe("i/w/a: id1 - id2")" removes a recipe from a book
  41. #
  42. # Requires:
  43. # - RPG Maker VX Ace
  44. #
  45. # Terms of Use:
  46. # - Free for commercal and non-commercial use. Please list me
  47. #   in the credits to support my work.
  48. #
  49. # Pastebin:
  50. # http://pastebin.com/CxB8F8T5
  51. #
  52. #==============================================================
  53. #   * Configuration
  54. #==============================================================
  55.  
  56. # Term used for crafting from recipe books
  57. INGREDIENTS_TERM = "Ingredientes"
  58. CR_WEAPON_TYPE_TERM = "Clase de arma"
  59. CR_ARMOR_TYPE_TERM = "Clase de armadura"
  60.  
  61. CRAFTING_CATEGORIES = ["Alchemy","Blacksmithing"]
  62.  
  63. # Custom crafting sounds by category
  64. CUSTOM_CRAFT_SOUNDS_BY_CAT = ["Saint5", "Bell2"]
  65. # Custom crafting sounds for RPG::Item, RPG::Weapon, RPG::Armor if no special cat
  66. CUSTOM_CRAFT_SOUNDS_BY_TYPE = ["Saint5", "Bell2", "Bell3"]
  67.  
  68. # If you want to use crafting from the menu, set this to true, else false
  69. CRAFTING_IN_MENU = true
  70. MENU_CRAFTING_VOCAB = "Crafting"
  71.  
  72.  
  73. # Vocabs used in status section for crafted items
  74. CRAFTING_ITEM_STATUS ={
  75.       :empty      => "-",     # Text used when nothing is shown.
  76.       :hp_recover => "HP",      # Text used for HP Recovery.
  77.       :mp_recover => "MP",      # Text used for MP Recovery.
  78.       :tp_recover => "TP",      # Text used for TP Recovery.
  79.       :tp_gain    => "TP gain", # Text used for TP Gain.
  80.       :applies    => "applies", # Text used for applied states and buffs.
  81.       :removes    => "removes", # Text used for removed states and buffs.
  82.     }
  83.  
  84. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85. #==============================================================
  86. #   * Scene_Crafting
  87. #==============================================================
  88. class Scene_Crafting < Scene_ItemBase
  89.  
  90.   def initialize()
  91.     @cr_category = SceneManager.scene_param[0] ? SceneManager.scene_param[0] : -1
  92.   end
  93.  
  94.   def start
  95.     super
  96.     create_help_window
  97.     create_category_window
  98.     create_ingredients_window
  99.     create_item_window
  100.   end
  101.  
  102.   def create_category_window
  103.     @category_window = Window_CraftingCategory.new
  104.     @category_window.viewport = @viewport
  105.     @category_window.help_window = @help_window
  106.     @category_window.y = @help_window.height
  107.     @category_window.set_handler(:ok,     method(:on_category_ok))
  108.     @category_window.set_handler(:cancel, method(:return_scene))
  109.   end
  110.  
  111.   def create_ingredients_window
  112.     wx = 240
  113.     wy = @category_window.y + @category_window.height
  114.     ww = Graphics.width - wx
  115.     wh = Graphics.height - wy
  116.     @ingredients_window = Window_CraftingIngredients.new(wx,wy,ww,wh)
  117.     @ingredients_window.viewport = @viewport
  118.   end
  119.  
  120.   def create_item_window
  121.     wy = @category_window.y + @category_window.height
  122.     wh = Graphics.height - wy
  123.     @item_window = Window_CraftingItemList.new(0, wy, 240, wh)
  124.     @item_window.cr_category = @cr_category
  125.     @item_window.viewport = @viewport
  126.     @item_window.help_window = @help_window
  127.     @item_window.set_handler(:ok,     method(:on_item_ok))
  128.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  129.     @item_window.set_handler(:right, method(:ingredients_show_stats))
  130.     @item_window.set_handler(:left, method(:ingredients_show_ingredients))
  131.     @item_window.ingredients_window = @ingredients_window
  132.     @category_window.item_window = @item_window
  133.   end
  134.  
  135.   def on_category_ok
  136.     @item_window.activate
  137.     @item_window.select_last
  138.   end
  139.  
  140.   def on_item_ok
  141.     determine_crafting
  142.   end
  143.  
  144.   def on_item_cancel
  145.     @item_window.unselect
  146.     @category_window.activate
  147.   end
  148.  
  149.   def determine_crafting
  150.     craft_item if @item_window.item.match_ingredients?
  151.   end
  152.  
  153.   def craft_item
  154.     item = @item_window.item
  155.     item.ingredients.each do |ing|
  156.       if ing[0].is_a?(RPG::BaseItem)
  157.         $game_party.lose_item(ing[0],ing[1])
  158.       else
  159.         $game_party.lose_gold(ing[1])
  160.       end
  161.     end
  162.     $game_party.gain_item(item,1)
  163.    
  164.     if @cr_category != -1
  165.       csf = CUSTOM_CRAFT_SOUNDS_BY_CAT[@cr_category]
  166.     else
  167.       if item.is_a?(RPG::Item)
  168.         csf = CUSTOM_CRAFT_SOUNDS_BY_TYPE[0]
  169.       elsif item.is_a?(RPG::Weapon)
  170.         csf = CUSTOM_CRAFT_SOUNDS_BY_TYPE[1]
  171.       elsif item.is_a?(RPG::Armor)
  172.         csf = CUSTOM_CRAFT_SOUNDS_BY_TYPE[2]
  173.       end
  174.     end
  175.     RPG::SE.new(csf, 100, 50).play
  176.    
  177.     @item_window.refresh
  178.     @item_window.activate
  179.   end
  180.  
  181.   def ingredients_show_ingredients
  182.     @ingredients_window.showtype = 0
  183.   end
  184.  
  185.   def ingredients_show_stats
  186.     @ingredients_window.showtype = 1
  187.   end
  188. end
  189.  
  190. #==============================================================
  191. #   * Scene_Menu
  192. #==============================================================
  193. class Scene_Menu < Scene_MenuBase
  194.   alias add_crafting_menu_entry create_command_window
  195.  
  196.   def create_command_window
  197.     add_crafting_menu_entry
  198.     @command_window.set_handler(:crafting,      method(:open_crafting)) if CRAFTING_IN_MENU
  199.   end
  200.  
  201.   def open_crafting
  202.     SceneManager.call(Scene_Crafting, -1)
  203.   end
  204. end
  205.  
  206. #==============================================================
  207. #   * Window_CraftingCategory
  208. #==============================================================
  209. class Window_CraftingCategory < Window_HorzCommand
  210.   attr_reader   :item_window
  211.  
  212.   def initialize
  213.     super(0, 0)
  214.   end
  215.  
  216.   def window_width
  217.     Graphics.width
  218.   end
  219.  
  220.   def col_max
  221.     return 3
  222.   end
  223.  
  224.   def update
  225.     super
  226.     @item_window.category = current_symbol if @item_window
  227.   end
  228.  
  229.   def make_command_list
  230.     add_command(Vocab::item,     :item)
  231.     add_command(Vocab::weapon,   :weapon)
  232.     add_command(Vocab::armor,    :armor)
  233.   end
  234.  
  235.   def item_window=(item_window)
  236.     @item_window = item_window
  237.     update
  238.   end
  239. end
  240.  
  241. #==============================================================
  242. #   * Window_CraftingCategory
  243. #==============================================================
  244. class Window_CraftingItemList < Window_Selectable
  245.   attr_reader :ingredients_window
  246.   attr_accessor :cr_category
  247.  
  248.   def initialize(x, y, width, height)
  249.     super
  250.     @category = :none
  251.     @data = []
  252.   end
  253.  
  254.   def category=(category)
  255.     return if @category == category
  256.     @category = category
  257.     refresh
  258.     self.oy = 0
  259.   end
  260.  
  261.   def col_max
  262.     return 1
  263.   end
  264.  
  265.   def item_max
  266.     @data ? @data.size : 1
  267.   end
  268.  
  269.   def item
  270.     @data && index >= 0 ? @data[index] : nil
  271.   end
  272.  
  273.   def current_item_enabled?
  274.     return false unless @data[index]
  275.     @data[index].match_ingredients?
  276.   end
  277.  
  278.   def include?(item)
  279.     case @category
  280.     when :item
  281.       item.is_a?(RPG::Item)
  282.     when :weapon
  283.       item.is_a?(RPG::Weapon)
  284.     when :armor
  285.       item.is_a?(RPG::Armor)
  286.     else
  287.       false
  288.     end
  289.   end
  290.  
  291.   def enable?(item)
  292.     $game_party.usable?(item)
  293.   end
  294.  
  295.   def make_item_list
  296.     rbooks = $game_party.all_items.select {|item| item.recipe_book}
  297.     rbooks.delete_if {|x| x.cr_category != @cr_category} if @cr_category != -1
  298.     @data = []
  299.     rbooks.each do |book|
  300.       sdata = book.recipes.select {|recipe| include?(recipe) }
  301.       @data.concat sdata
  302.     end
  303.     @data.push(nil) if include?(nil)
  304.   end
  305.  
  306.   def select_last
  307.     select(@data.index($game_party.last_item.object) || 0)
  308.   end
  309.  
  310.   def draw_item(index)
  311.     item = @data[index]
  312.     if item
  313.       rect = item_rect(index)
  314.       rect.width -= 4
  315.       draw_item_name(item, rect.x, rect.y, item.match_ingredients?, width-70)
  316.       draw_item_number(rect, item)
  317.     end
  318.   end
  319.  
  320.   def draw_item_number(rect, item)
  321.     draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)
  322.   end
  323.  
  324.   def update_help
  325.     @help_window.set_item(item)
  326.     @ingredients_window.item = item if @ingredients_window
  327.   end
  328.  
  329.   def refresh
  330.     make_item_list
  331.     create_contents
  332.     draw_all_items
  333.   end
  334.  
  335.   def ingredients_window=(ingredients_window)
  336.     @ingredients_window = ingredients_window
  337.     update
  338.   end
  339.  
  340.   def process_ok
  341.     if current_item_enabled?
  342.       Input.update
  343.       deactivate
  344.       call_ok_handler
  345.     else
  346.       Sound.play_buzzer
  347.     end
  348.   end
  349.  
  350.   alias cr_us_win unselect
  351.   def unselect
  352.     cr_us_win
  353.     @ingredients_window.contents.clear
  354.   end
  355. end
  356.  
  357. #==============================================================
  358. #   * Window_MenuCraftingList
  359. #==============================================================
  360. class Window_MenuCraftingList < Window_Selectable
  361.  
  362.   attr_reader :ingredients_window
  363.   attr_reader :book
  364.  
  365.   alias cr_ing_help call_update_help
  366.   alias cr_ing_hide hide
  367.  
  368.   def initialize(book,y)
  369.     @book = book
  370.     super(0,y, 240, Graphics.height-y)
  371.     self.visible = false
  372.     refresh
  373.   end
  374.  
  375.   def window_height
  376.     Graphics.height-self.y
  377.   end
  378.  
  379.   def item_max
  380.     @book.recipes.size
  381.   end
  382.  
  383.   def item_height
  384.     line_height+4
  385.   end
  386.  
  387.   def draw_item(index)
  388.     recipe = @book.recipes[index]
  389.     rect = item_rect(index)
  390.     draw_icon(recipe.icon_index, rect.x+2, rect.y+2)
  391.     draw_text(rect.x+30,rect.y+2,width-75, line_height, recipe.name)
  392.     draw_text(rect.x-30,rect.y+2,width, line_height, sprintf(":%2d", $game_party.item_number(@item)), 2)
  393.   end
  394.  
  395.   def process_ok
  396.     super
  397.     $game_party.menu_actor = $game_party.members[index]
  398.   end
  399.  
  400.   def select_last
  401.     select($game_party.menu_actor.index || 0)
  402.   end
  403.  
  404.   def select_for_item(item)
  405.     select(0)
  406.     @book = item
  407.   end
  408.  
  409.   def ingredients_window=(ingredients_window)
  410.     @ingredients_window = ingredients_window
  411.     update
  412.   end
  413.  
  414.   def call_update_help
  415.     cr_ing_help
  416.     @ingredients_window.item = @book.recipes[@index] if @ingredients_window && @index >= 0
  417.   end
  418.  
  419.   def hide
  420.     cr_ing_hide
  421.     @ingredients_window.hide.deactivate
  422.   end
  423. end
  424.  
  425. #==============================================================
  426. #   * Window_CraftingIngredients
  427. #==============================================================
  428. class Window_CraftingIngredients < Window_Selectable
  429.   def initialize(x,y,w,h)
  430.     super(x,y,w,h)
  431.     @item = nil
  432.     @showtype=0
  433.   end
  434.  
  435.   def item=(item)
  436.     @item = item
  437.     refresh
  438.   end
  439.  
  440.   def refresh
  441.     contents.clear
  442.     return if !@item
  443.     case @showtype
  444.     when 1
  445.       #draw_stats_item if @item.is_a?(RPG::Item)
  446.       draw_stats
  447.     else
  448.       draw_ingredients
  449.     end
  450.   end
  451.  
  452.   def draw_ingredients
  453.     change_color(system_color)
  454.     draw_text(0,line_height*0,width,line_height, INGREDIENTS_TERM)
  455.     i = 1
  456.     @item.ingredients.each do |ing|
  457.       change_color(normal_color)
  458.       change_color(normal_color)
  459.       if ing[0].is_a?(String)
  460.         draw_icon(361,0,line_height*i)
  461.         draw_text(24,line_height*i, width,line_height, ing[0])
  462.         inumber = $game_party.gold
  463.       else
  464.         draw_icon(ing[0].icon_index,0,line_height*i)
  465.         draw_text(24,line_height*i, width,line_height, ing[0].name)
  466.         inumber = $game_party.item_number(ing[0])
  467.       end
  468.      
  469.       change_color(crisis_color) if inumber < ing[1]
  470.       change_color(hp_gauge_color1) if inumber == 0
  471.       change_color(tp_gauge_color2) if inumber >= ing[1]
  472.      
  473.       txt = sprintf("%d/%d",inumber, ing[1])
  474.       draw_text(-24,line_height*i,width-4,line_height,txt,2)
  475.       i += 1
  476.     end
  477.     change_color(normal_color)
  478.   end
  479.  
  480.   def draw_stats
  481.     draw_item_stats
  482.     draw_item_effects
  483.   end
  484.  
  485.   def showtype=(st)
  486.     @showtype = st
  487.     refresh
  488.   end
  489.  
  490.   def draw_background_box(dx, dy, dw)
  491.     colour = Color.new(0, 0, 0, translucent_alpha/2)
  492.     rect = Rect.new(dx+1, dy+1, dw-2, line_height-2)
  493.     contents.fill_rect(rect, colour)
  494.   end
  495.      
  496.   def draw_item_stats
  497.     return unless @item.is_a?(RPG::Weapon) || @item.is_a?(RPG::Armor)
  498.     dx = 0; dy = 0
  499.     dw = (contents.width) / 2
  500.     for i in 0...8
  501.       draw_equip_param(i, dx, dy, dw)
  502.       dx = dx >= dw ? 0 : dw
  503.       dy += line_height if dx == 0
  504.     end
  505.   end
  506.      
  507.   def draw_equip_param(param_id, dx, dy, dw)
  508.     draw_background_box(dx, dy, dw)
  509.     change_color(system_color)
  510.     draw_text(dx+4, dy, dw-8, line_height, Vocab::param(param_id))
  511.     draw_set_param(param_id, dx, dy, dw)
  512.   end
  513.      
  514.   def draw_set_param(param_id, dx, dy, dw)
  515.     value = @item.params[param_id]
  516.     change_color(param_change_color(value), value != 0)
  517.     text = value.to_s
  518.     text = "+" + text if value > 0
  519.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  520.     return text
  521.   end
  522.  
  523.   def draw_percent_param(param_id, dx, dy, dw)
  524.     value = @item.per_params[param_id]
  525.     change_color(param_change_color(value))
  526.     text = (@item.per_params[param_id] * 100).to_i.to_s + "%"
  527.     text = "+" + text if @item.per_params[param_id] > 0
  528.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  529.     return text
  530.   end
  531.      
  532.   def draw_item_effects
  533.     return unless @item.is_a?(RPG::Item)
  534.     dx = 0; dy = 0
  535.     dw = (contents.width) / 2
  536.     draw_hp_recover(dx, dy + line_height * 0, dw)
  537.     draw_mp_recover(dx, dy + line_height * 1, dw)
  538.     draw_tp_recover(dx + dw, dy + line_height * 0, dw)
  539.     draw_tp_gain(dx + dw, dy + line_height * 1, dw)
  540.     dw = contents.width
  541.     draw_applies(dx, dy + line_height * 2, dw)
  542.     draw_removes(dx, dy + line_height * 3, dw)
  543.   end
  544.  
  545.   def draw_hp_recover(dx, dy, dw)
  546.     draw_background_box(dx, dy, dw)
  547.     change_color(system_color)
  548.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:hp_recover])
  549.     per = 0
  550.     set = 0
  551.     for effect in @item.effects
  552.       next unless effect.code == 11
  553.       per += (effect.value1 * 100).to_i
  554.       set += effect.value2.to_i
  555.     end
  556.     if per != 0 && set != 0
  557.       change_color(param_change_color(set))
  558.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  559.       draw_text(dx+4, dy, dw-8, line_height, text, 2)
  560.       dw -= text_size(text).width
  561.       change_color(param_change_color(per))
  562.       text = per > 0 ? sprintf("+%s%%", per.to_s) : sprintf("%s%%", per.to_s)
  563.       draw_text(dx+4, dy, dw-8, line_height, text, 2)
  564.       return
  565.     elsif per != 0
  566.       change_color(param_change_color(per))
  567.       text = per > 0 ? sprintf("+%s%%", per.to_s) : sprintf("%s%%", per.to_s)
  568.     elsif set != 0
  569.       change_color(param_change_color(set))
  570.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  571.     else
  572.       change_color(normal_color, false)
  573.       text = CRAFTING_ITEM_STATUS[:empty]
  574.     end
  575.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  576.   end
  577.  
  578.   def draw_mp_recover(dx, dy, dw)
  579.     draw_background_box(dx, dy, dw)
  580.     change_color(system_color)
  581.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:mp_recover])
  582.     per = 0
  583.     set = 0
  584.     for effect in @item.effects
  585.       next unless effect.code == 12
  586.       per += (effect.value1 * 100).to_i
  587.       set += effect.value2.to_i
  588.     end
  589.     if per != 0 && set != 0
  590.       change_color(param_change_color(set))
  591.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  592.       draw_text(dx+4, dy, dw-8, line_height, text, 2)
  593.       dw -= text_size(text).width
  594.       change_color(param_change_color(per))
  595.       text = per > 0 ? sprintf("+%s%%", per.to_s) : sprintf("%s%%", per.to_s)
  596.       draw_text(dx+4, dy, dw-8, line_height, text, 2)
  597.       return
  598.     elsif per != 0
  599.       change_color(param_change_color(per))
  600.       text = per > 0 ? sprintf("+%s%%", per.to_s) : sprintf("%s%%", per.to_s)
  601.     elsif set != 0
  602.       change_color(param_change_color(set))
  603.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  604.     else
  605.       change_color(normal_color, false)
  606.       text = CRAFTING_ITEM_STATUS[:empty]
  607.     end
  608.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  609.   end
  610.  
  611.   def draw_tp_recover(dx, dy, dw)
  612.     draw_background_box(dx, dy, dw)
  613.     change_color(system_color)
  614.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:tp_recover])
  615.     set = 0
  616.     for effect in @item.effects
  617.       next unless effect.code == 13
  618.       set += effect.value1.to_i
  619.     end
  620.     if set != 0
  621.       change_color(param_change_color(set))
  622.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  623.     else
  624.       change_color(normal_color, false)
  625.       text = CRAFTING_ITEM_STATUS[:empty]
  626.     end
  627.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  628.   end
  629.  
  630.   def draw_tp_gain(dx, dy, dw)
  631.     draw_background_box(dx, dy, dw)
  632.     change_color(system_color)
  633.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:tp_gain])
  634.     set = @item.tp_gain
  635.     if set != 0
  636.       change_color(param_change_color(set))
  637.       text = set > 0 ? sprintf("+%s", set.to_s) : set.to_s
  638.     else
  639.       change_color(normal_color, false)
  640.       text = CRAFTING_ITEM_STATUS[:empty]
  641.     end
  642.     draw_text(dx+4, dy, dw-8, line_height, text, 2)
  643.   end
  644.  
  645.   def draw_applies(dx, dy, dw)
  646.     draw_background_box(dx, dy, dw)
  647.     change_color(system_color)
  648.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:applies])
  649.     icons = []
  650.     for effect in @item.effects
  651.       case effect.code
  652.       when 21
  653.         next unless effect.value1 > 0
  654.         next if $data_states[effect.value1].nil?
  655.         icons.push($data_states[effect.data_id].icon_index)
  656.       when 31
  657.         icons.push($game_actors[1].buff_icon_index(1, effect.data_id))
  658.       when 32
  659.         icons.push($game_actors[1].buff_icon_index(-1, effect.data_id))
  660.       end
  661.       icons.delete(0)
  662.       break if icons.size >= 10
  663.     end
  664.     draw_icons(dx, dy, dw, icons)
  665.   end
  666.  
  667.   def draw_removes(dx, dy, dw)
  668.     draw_background_box(dx, dy, dw)
  669.     change_color(system_color)
  670.     draw_text(dx+4, dy, dw-8, line_height, CRAFTING_ITEM_STATUS[:removes])
  671.     icons = []
  672.     for effect in @item.effects
  673.       case effect.code
  674.       when 22
  675.         next unless effect.value1 > 0
  676.         next if $data_states[effect.value1].nil?
  677.         icons.push($data_states[effect.data_id].icon_index)
  678.       when 33
  679.         icons.push($game_actors[1].buff_icon_index(1, effect.data_id))
  680.       when 34
  681.         icons.push($game_actors[1].buff_icon_index(-1, effect.data_id))
  682.       end
  683.       icons.delete(0)
  684.       break if icons.size >= 10
  685.     end
  686.     draw_icons(dx, dy, dw, icons)
  687.   end
  688.  
  689.   def draw_icons(dx, dy, dw, icons)
  690.     dx += dw - 4
  691.     dx -= icons.size * 24
  692.     for icon_id in icons
  693.       draw_icon(icon_id, dx, dy)
  694.       dx += 24
  695.     end
  696.     if icons.size == 0
  697.       change_color(normal_color, false)
  698.       text = CRAFTING_ITEM_STATUS[:empty]
  699.       draw_text(4, dy, contents.width-8, line_height, text, 2)
  700.     end
  701.   end
  702. end
  703.  
  704. #==============================================================
  705. #   * Window_MenuCommand
  706. #==============================================================
  707. class Window_MenuCommand < Window_Command
  708.   alias add_crafting_menu_entry add_main_commands
  709.  
  710.   def add_main_commands
  711.     add_crafting_menu_entry
  712.     add_command(MENU_CRAFTING_VOCAB, :crafting) if CRAFTING_IN_MENU
  713.   end
  714. end
  715.  
  716. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  717. #==============================================================
  718. #   * Initialize BaseItems
  719. #==============================================================
  720. module DataManager
  721.   class << self
  722.     alias load_db_sz_crafting load_database
  723.     alias save_crafting_recipe_books make_save_contents
  724.     alias load_crafting_recipe_books extract_save_contents
  725.   end
  726.  
  727.   def self.load_database
  728.     load_db_sz_crafting
  729.     load_crafting_item_notetags
  730.   end
  731.  
  732.   def self.load_crafting_item_notetags
  733.     groups = [$data_items, $data_weapons, $data_armors]
  734.     for group in groups
  735.       for obj in group
  736.         next if obj.nil?
  737.         obj.load_crafting_notetags_sz
  738.       end
  739.     end
  740.   end
  741.  
  742.  
  743.   def self.make_save_contents
  744.     contents = save_crafting_recipe_books
  745.     recipe_books = {}
  746.     for item in $data_items
  747.       next if item.nil?
  748.       next if not item.recipe_book
  749.       recipe_books[item.id] = item.recipes
  750.     end
  751.    
  752.     contents[:recipe_books] = recipe_books
  753.     contents
  754.   end
  755.  
  756.   def self.extract_save_contents(contents)
  757.     load_crafting_recipe_books(contents)
  758.     recipe_books = contents[:recipe_books]
  759.    
  760.     recipe_books.each do |id, recipes|
  761.       $data_items[id].recipes = recipes
  762.     end
  763.   end
  764. end
  765.  
  766. #==============================================================
  767. #   * Call Scene with Parameters
  768. #==============================================================
  769. class << SceneManager
  770.   alias call_scene_crafting call
  771.  
  772.   attr_accessor :scene_param
  773.  
  774.   def call(scene_class, *param)
  775.     @scene_param = param
  776.     call_scene_crafting(scene_class)
  777.   end
  778. end
  779.  
  780. #==============================================================
  781. #   * List Recipes of a Book
  782. #==============================================================
  783. class Scene_Item < Scene_ItemBase
  784.   alias sz_crafting_determitem determine_item
  785.  
  786.   def determine_item
  787.     if item.recipe_book
  788.       create_crafting_item_window
  789.       show_crafting_sub_window(@crafting_item_window)
  790.     else
  791.       sz_crafting_determitem
  792.     end
  793.   end
  794.  
  795.   def create_crafting_item_window
  796.     wy = @help_window.height+@category_window.height
  797.     @crafting_item_window = Window_MenuCraftingList.new(item,wy)
  798.     @crafting_item_window.set_handler(:cancel, method(:on_sz_item_cancel))
  799.     @crafting_item_window.set_handler(:left, method(:ingredients_show_ingredients))
  800.     @crafting_item_window.set_handler(:right, method(:ingredients_show_stats))
  801.    
  802.     ww = Graphics.width - @crafting_item_window.width
  803.     wh = Graphics.height - wy
  804.     @ingredients_window = Window_CraftingIngredients.new(240, wy,ww,wh)
  805.     @ingredients_window.viewport
  806.     @crafting_item_window.ingredients_window = @ingredients_window
  807.   end
  808.  
  809.   def on_sz_item_cancel
  810.     hide_crafting_sub_window(@crafting_item_window)
  811.   end
  812.  
  813.  
  814.   def show_crafting_sub_window(window)
  815.     height_remain = @help_window.height+@category_window.height
  816.     @viewport.rect.height = height_remain
  817.     window.show.activate
  818.   end
  819.  
  820.   def hide_crafting_sub_window(window)
  821.     @viewport.rect.y = @viewport.oy = 0
  822.     @viewport.rect.height = Graphics.height
  823.     window.hide.deactivate
  824.     activate_item_window
  825.   end
  826.  
  827.   def ingredients_show_ingredients
  828.     @ingredients_window.showtype = 0
  829.   end
  830.  
  831.   def ingredients_show_stats
  832.     @ingredients_window.showtype = 1
  833.   end
  834. end
  835.  
  836.  
  837. class Window_Selectable < Window_Base
  838.   alias :sz_cr_input_handler_process_handling :process_handling
  839.  
  840.   def process_handling
  841.     return unless open? && active
  842.     sz_cr_input_handler_process_handling
  843.     return call_handler(:left) if handle?(:left) && Input.trigger?(:LEFT)
  844.     return call_handler(:right) if handle?(:right) && Input.trigger?(:RIGHT)
  845.   end
  846. end
  847.  
  848. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  849. #==============================================================
  850. #   * Content of Crafting Items
  851. #==============================================================
  852. class RPG::BaseItem
  853.   attr_accessor :ingredients
  854.   attr_accessor :recipe_book
  855.   attr_accessor :recipes
  856.   attr_accessor :cr_category
  857.  
  858.  
  859.   def load_crafting_notetags_sz
  860.     @ingredients = []
  861.     @recipe_book = false
  862.     @recipes = []
  863.     @cr_category = -1
  864.    
  865.     @scan_ingredients = false
  866.     @scan_recipes = false
  867.    
  868.     self.note.split(/[\r\n]+/).each do |line|
  869.       case line.downcase
  870.       # Ingredients
  871.       when /<(?:ingredients?)>/i
  872.         @scan_ingredients = true
  873.       when /<\/(?:ingredients?)>/i
  874.         @scan_ingredients = false
  875.       # Recipes
  876.       when /<(?:recipes?)>/i
  877.         @scan_recipes = true
  878.       when /<\/(?:recipes?)>/i
  879.         @scan_recipes = false
  880.       # Crafting Book
  881.       when /<(?:recipe book)>/i
  882.         @recipe_book = true
  883.         @itype_id = 2
  884.       when /<category:\s*?(\d+)>/i
  885.         @cr_category = $1.to_i if @recipe_book
  886.       else
  887.         scan_ingredients(line) if @scan_ingredients
  888.         scan_recipes(line) if @scan_recipes
  889.       end
  890.     end
  891.   end
  892.  
  893.   def scan_ingredients(line)
  894.     return if @crafting_book
  895.     return unless line =~ /(\w+):\s*?(\d+)[x]?\s*(\d+)?/i ? true : false
  896.     case $1
  897.     when "c"
  898.       @ingredients.push([Vocab::currency_unit,$2.to_i])
  899.     when "i"
  900.       @ingredients.push([$data_items[$3.to_i], $2.to_i])
  901.     when "w"
  902.       @ingredients.push([$data_weapons[$3.to_i], $2.to_i])
  903.     when "a"
  904.       @ingredients.push([$data_armors[$3.to_i], $2.to_i])
  905.     end
  906.   end
  907.  
  908.   def scan_recipes(line)
  909.     return unless line =~ /(\w+):\s*(\d+)\s*-?\s*(\d+)?/i ? true : false
  910.     from = $2.to_i
  911.     if $3 == nil
  912.       til = from
  913.     else
  914.       til = $3.to_i
  915.     end
  916.    
  917.     for i in from..til
  918.       case $1
  919.       when "i"
  920.         @recipes.push($data_items[i])
  921.       when "w"
  922.         @recipes.push($data_weapons[i])
  923.       when "a"
  924.         @recipes.push($data_armors[i])
  925.       end
  926.     end
  927.     @recipes = @recipes.sort_by {|x| [x.class.to_s, x.id]}
  928.   end
  929.  
  930.  
  931.   def match_ingredients?
  932.     @ingredients.each do |ing|
  933.       icount = ing[0].is_a?(RPG::BaseItem) ? $game_party.item_number(ing[0]) : $game_party.gold
  934.       return false if icount < ing[1]
  935.     end
  936.     return true
  937.   end
  938.  
  939.   def add_recipe(type)
  940.     return unless @recipe_book
  941.     return unless type =~ /(\w+):\s*(\d+)\s*-?\s*(\d+)?/i ? true : false
  942.     from = $2.to_i
  943.     if $3 == nil
  944.       til = from
  945.     else
  946.       til = $3.to_i
  947.     end
  948.    
  949.     for i in from..til
  950.       case $1
  951.       when "i"
  952.         return if @recipes.include?($data_items[i])
  953.         @recipes.push($data_items[i])
  954.       when "w"
  955.         return if @recipes.include?($data_weapons[i])
  956.         @recipes.push($data_weapons[i])
  957.       when "a"
  958.         return if @recipes.include?($data_armors[i])
  959.         @recipes.push($data_armors[i])
  960.       end
  961.     end
  962.    
  963.     @recipes = @recipes.sort_by {|x| [x.class.to_s, x.id]}
  964.   end
  965.  
  966.   def remove_recipe(type)
  967.     return unless @recipe_book
  968.     return unless type =~ /(\w+):\s*(\d+)\s*-?\s*(\d+)?/i ? true : false
  969.     from = $2.to_i
  970.     if $3 == nil
  971.       til = from
  972.     else
  973.       til = $3.to_i
  974.     end
  975.    
  976.     for i in from..til
  977.       case $1
  978.       when "i"
  979.         return if not @recipes.include?($data_items[i])
  980.         @recipes.delete($data_items[i])
  981.       when "w"
  982.         return if not @recipes.include?($data_weapons[i])
  983.         @recipes.delete($data_weapons[i])
  984.       when "a"
  985.         return if not @recipes.include?($data_armors[i])
  986.         @recipes.delete($data_armors[i])
  987.       end
  988.     end
  989.    
  990.     @recipes = @recipes.sort_by {|x| [x.class.to_s, x.id]}
  991.   end
  992. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement