Advertisement
eugene222

"Do It Yourself" - Crafting

Jul 24th, 2014
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 40.66 KB | None | 0 0
  1. #===============================================================================
  2. # ** "Do It Yourself" - Crafting **
  3. #
  4. # Author:   Evgenij
  5. # Version:  1.1a
  6. # Date:     08.08.2014
  7. #
  8. # Terms of Use: http://evgenij-scripts.org/?page_id=34
  9. #
  10. #===============================================================================
  11. # Changelog:
  12. #
  13. # V. 1.0 - 24.07.2014   - Script created
  14. # V. 1.1 - 25.07.2014   - fixed some bugs, added new recipe scene
  15. # V. 1.1a- 08.08.2014   - fixed small bug
  16. #
  17. #===============================================================================
  18. # Description:
  19. #   This script adds a crafting scene to your game.
  20. #   It is a bit different from the other crafting scripts, cause with this script
  21. #   the player dont choose the recipes but need to choose the ingredients.
  22. #
  23. # Instruction:
  24. #
  25. #   To call the crafting scene make this scriptcall:
  26. #   SceneManager.call(Scene_ICrafting)
  27. #
  28. #   To call the recipe scene make this scriptcall:
  29. #   SceneManager.call(Scene_ICRecipes)
  30. #
  31. #
  32. #   These calls are only relevant if SHOW_RECIPES_ONLY_WHEN_LEARNED = true:
  33. #
  34. #     To make recipes visible to the player:
  35. #     CraftingHandler.add_recipe(:recipe_name)
  36. #  
  37. #
  38. #     to make all recipes visible to the player:
  39. #     CraftingHandler.add_all_recipes
  40. #===============================================================================
  41. module EVG
  42.   module CRAFTING
  43.     module CONFIG
  44.       #-------------------------------------------------------------------------
  45.       # Here you can define your recipes.
  46.       # The template looks like this:
  47.       #   :recipe_name => {:recipe => [:item_codes], :result => :item_code},
  48.       # The item codes look like this:
  49.       #   For weapon:
  50.       #     :w+database_id
  51.       #   For armor:
  52.       #     :a+database_id
  53.       #   For item:
  54.       #     :i+database_id
  55.       #
  56.       # Example:
  57.       #   w21 - This would be a weapon with the database_id 21
  58.       #
  59.       # Look at the predefined recipes for more examples
  60.       #
  61.       # You dont need to care for the ingredient order.
  62.       #-------------------------------------------------------------------------
  63.       RECIPES = {
  64.        
  65.         :potion2 => {:recipe => [:i1, :i1, :i1], :result => :i2},
  66.         :potion3 => {:recipe => [:i2, :i2],      :result => :i3},
  67.         :cool_weapon => {:recipe => [:w1, :w2, :i17], :result => :w61},
  68.         :cool_armor => {:recipe => [:a1, :i3], :result => :a61}
  69.        
  70.       }
  71.      
  72.       #-------------------------------------------------------------------------
  73.       # Welche Kategorien sollen angezeigt werden?
  74.       # Standartkategorien:  :item, :weapon, :armor, :key_item
  75.       #-------------------------------------------------------------------------
  76.       CATEGORIES = [:item, :weapon, :armor]
  77.  
  78.       #-------------------------------------------------------------------------
  79.       # If this is set to true the player wont be able to see the result item
  80.       # before he have crafted it once
  81.       #-------------------------------------------------------------------------
  82.       SHOW_RESULT_ONLY_WHEN_LEARNED = false
  83.      
  84.       #-------------------------------------------------------------------------
  85.       # Give items back when crafting fails ?
  86.       #-------------------------------------------------------------------------
  87.       GIVE_ITEMS_BACK_WHEN_FAILURE = true
  88.      
  89.       #-------------------------------------------------------------------------
  90.       # Show the crafting command in the main menu ?
  91.       # This may only work for the vanilla menu.
  92.       #-------------------------------------------------------------------------
  93.       MAIN_MENU_COMMAND = true
  94.  
  95.   #-----------------------------------------------------------------------------
  96.   # Recipe Scene:
  97.   #-----------------------------------------------------------------------------
  98.       SHOW_RECIPES_ONLY_WHEN_LEARNED = false
  99.      
  100.       #-------------------------------------------------------------------------
  101.       # If the the option above is true, you can choose if you want to show icons
  102.       # only or show nothing.
  103.       #-------------------------------------------------------------------------
  104.       SHOW_ICONS = true
  105.      
  106.       #-------------------------------------------------------------------------
  107.       # Show the recipe command in the main menu ?
  108.       # This may only work for the vanilla menu.
  109.       #-------------------------------------------------------------------------
  110.       RECIPE_MAIN_MENU_COMMAND = true
  111.       #-------------------------------------------------------------------------
  112.     end
  113.     module VOCAB
  114.       #-------------------------------------------------------------------------
  115.       # Main Menu Command
  116.       #-------------------------------------------------------------------------
  117.       COMMAND = "Crafting"
  118.      
  119.       UNKNOWN_OUTPUT = "???"
  120.       ALREADY_FAILED = "This won't work!"
  121.       FAILED = "This has gone wrong!"
  122.       CRAFT = "Start Crafting!"
  123.       RESULT = "Result:"
  124.  
  125.       #-------------------------------------------------------------------------
  126.       # Rezept Scene Vokabular:
  127.       #-------------------------------------------------------------------------
  128.       RECIPE_COMMAND = "Recipes"
  129.       RECIPE = "Recipe:"
  130.       NOT_LEARNED = "You haven't learned the recipe yet!"
  131.     end
  132.   end
  133. end
  134. #===============================================================================
  135. module CraftingHandler
  136.   #--------------------------------------------------------------------------
  137.   def self.get_recipe_for_list(list)
  138.     return if list.compact.size < 2
  139.     converted_list = code_array(list).compact.sort
  140.     EVG::CRAFTING::CONFIG::RECIPES.each_with_index do |(name, setting), i|
  141.       next if setting[:recipe].compact.size != list.compact.size
  142.       if setting[:recipe].compact.sort == converted_list
  143.         return name
  144.       end
  145.     end
  146.     return nil
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   def self.add_failed_recipe(list)
  150.     converted_list = code_array(list).compact.sort
  151.     $game_system.add_failed_recipe(converted_list)
  152.   end
  153.   #--------------------------------------------------------------------------
  154.   def self.already_failed?(list)
  155.     return false unless $game_system.failed_recipes
  156.     converted_list = code_array(list).compact.sort
  157.     $game_system.failed_recipes.each do |recipe_list|
  158.       if recipe_list == converted_list
  159.         return true
  160.       end
  161.     end
  162.     return false
  163.   end
  164.   #--------------------------------------------------------------------------
  165.   def self.get_object_for_code(code)
  166.     case code.to_s[0]
  167.     when "w"
  168.       $data_weapons[code.to_s.delete("w").to_i]
  169.     when "a"
  170.       $data_armors[code.to_s.delete("a").to_i]
  171.     when "i"
  172.       $data_items[code.to_s.delete("i").to_i]
  173.     else
  174.       return nil
  175.     end
  176.   end
  177.   #--------------------------------------------------------------------------
  178.   def self.get_code_for_object(object)
  179.     return "w#{object.id}".to_sym if object.is_a?(RPG::Weapon)
  180.     return "i#{object.id}".to_sym if object.is_a?(RPG::Item)
  181.     return "a#{object.id}".to_sym if object.is_a?(RPG::Armor)
  182.   end
  183.   #--------------------------------------------------------------------------
  184.   def self.code_array(object_array)
  185.     object_array.map{|obj| get_code_for_object(obj)}
  186.   end
  187.   #--------------------------------------------------------------------------
  188.   def self.get_object_for_recipe(recipe)
  189.     return nil unless recipe
  190.     return get_object_for_code(EVG::CRAFTING::CONFIG::RECIPES[recipe][:result])
  191.   end
  192.   #--------------------------------------------------------------------------
  193.   def self.recipes
  194.     return @recipes if @recipes
  195.     @recipes = {}
  196.     recipe_codes.each do |recipe_name, recipes|
  197.       @recipes[recipe_name] = recipes.map {|code| get_object_for_code(code)}
  198.     end
  199.     return @recipes
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   def self.recipe_codes
  203.     return @recipe_codes if @recipe_codes
  204.     @recipe_codes = {}
  205.     EVG::CRAFTING::CONFIG::RECIPES.each do |(recipe, setting)|
  206.       @recipe_codes[recipe] = setting[:recipe]
  207.     end
  208.     return @recipe_codes
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   def self.results
  212.     return @results if @results
  213.     @results = {}
  214.     result_codes.each do |recipe, code|
  215.       @results[recipe] = get_object_for_code(code)
  216.     end
  217.     return @results
  218.   end
  219.   #--------------------------------------------------------------------------
  220.   def self.result_codes
  221.     return @result_codes if @result_codes
  222.     @result_codes = {}
  223.     EVG::CRAFTING::CONFIG::RECIPES.each do |(recipe, setting)|
  224.       @result_codes[recipe] = setting[:result]
  225.     end
  226.     return @result_codes
  227.   end
  228.   #--------------------------------------------------------------------------
  229.   def self.add_recipe(key)
  230.     $game_system.add_crafting_recipe(key)
  231.   end
  232.   #--------------------------------------------------------------------------
  233.   def self.add_all_recipes
  234.     $game_system.crafting_recipes = recipes.keys
  235.   end
  236.   #--------------------------------------------------------------------------
  237. end
  238. #===============================================================================
  239. class RPG::BaseItem
  240.   def hide_craft?
  241.     @note.include?("<hide_craft>")
  242.   end
  243. end
  244. #===============================================================================
  245. class Game_System
  246.   #--------------------------------------------------------------------------
  247.   attr_accessor     :crafting_recipes
  248.   attr_reader       :failed_recipes
  249.   #--------------------------------------------------------------------------
  250.   alias :evg_gs_initialize_ic   :initialize
  251.   def initialize
  252.     evg_gs_initialize_ic
  253.     @crafting_recipes = []
  254.   end
  255.   #--------------------------------------------------------------------------
  256.    def add_crafting_recipe(recipe)
  257.     return if @crafting_recipes && @crafting_recipes.include?(recipe)
  258.     @crafting_recipes.push(recipe)
  259.   end
  260.   #--------------------------------------------------------------------------
  261.   def remove_crafting_recipe(recipe)
  262.     return if @crafting_recipes && !@crafting_recipes.include?(recipe)
  263.     @crafting_recipes.delete(recipe)
  264.   end
  265.  
  266.   def add_failed_recipe(list)
  267.     @failed_recipes ||= []
  268.     return if @failed_recipes.include?(list)
  269.     @failed_recipes.push(list)
  270.   end
  271.   #--------------------------------------------------------------------------
  272. end
  273. #===============================================================================
  274. class Window_ICCategory < Window_ItemCategory
  275.   #--------------------------------------------------------------------------
  276.   def col_max
  277.     return [@list.size, 4].min
  278.   end
  279.   #--------------------------------------------------------------------------
  280.   def make_command_list
  281.     EVG::CRAFTING::CONFIG::CATEGORIES.each do |cat|
  282.       add_command(Vocab.send(cat),     cat)
  283.     end
  284.   end
  285.   #--------------------------------------------------------------------------
  286.   def ok_enabled?
  287.     return false
  288.   end
  289.   #--------------------------------------------------------------------------
  290. end
  291. #===============================================================================
  292. class Window_ICraftingList < Window_Selectable
  293.   include EVG::CRAFTING::VOCAB
  294.   #--------------------------------------------------------------------------
  295.   attr_reader   :items
  296.   #--------------------------------------------------------------------------
  297.   def initialize
  298.     @crafting_slots = 4
  299.     @items = [nil] * @crafting_slots
  300.     super(window_x, window_y, window_width, window_height)
  301.     activate
  302.     select(0)
  303.     refresh
  304.   end
  305.   #--------------------------------------------------------------------------
  306.   def window_x
  307.     Graphics.width / 2
  308.   end
  309.   #--------------------------------------------------------------------------
  310.   def window_y
  311.     fitting_height(4)
  312.   end
  313.   #--------------------------------------------------------------------------
  314.   def window_width
  315.     return Graphics.width / 2
  316.   end
  317.   #--------------------------------------------------------------------------
  318.   def window_height
  319.     return Graphics.height - fitting_height(4)
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   def item_max
  323.     return @crafting_slots + 1
  324.   end
  325.   #--------------------------------------------------------------------------
  326.   def current_slot
  327.     return @items[@index]
  328.   end
  329.   #--------------------------------------------------------------------------
  330.   def current_slot=(item)
  331.     @items[@index] = item
  332.   end
  333.   #--------------------------------------------------------------------------
  334.   def result=(result)
  335.     @result = result
  336.     refresh
  337.   end
  338.   #--------------------------------------------------------------------------
  339.   def recipe=(recipe)
  340.     @recipe = recipe
  341.   end
  342.   #--------------------------------------------------------------------------
  343.   def current_item_enabled?
  344.     return true if index < 4
  345.     return true if @items.compact.size > 1
  346.     return false
  347.   end
  348.   #--------------------------------------------------------------------------
  349.   def craft_command?(index)
  350.     return index >= @crafting_slots
  351.   end
  352.   #--------------------------------------------------------------------------
  353.   def current_craft_command?
  354.     return craft_command?(@index)
  355.   end
  356.   #--------------------------------------------------------------------------
  357.   def text_enabled?
  358.     return @items.compact.size > 1
  359.   end
  360.   #--------------------------------------------------------------------------
  361.   def recipe_learned?
  362.     return true unless recipe_need_to_learn?
  363.     return $game_system.crafting_recipes.include?(@recipe)
  364.   end
  365.  
  366.   def recipe_need_to_learn?
  367.     EVG::CRAFTING::CONFIG::SHOW_RESULT_ONLY_WHEN_LEARNED
  368.   end
  369.   #--------------------------------------------------------------------------
  370.   def item_rect(index)
  371.     rect = super(index)
  372.     rect.y += line_height * top_lines
  373.     rect.y += line_height if craft_command?(index)
  374.     rect
  375.   end
  376.   #--------------------------------------------------------------------------
  377.   def top_lines
  378.     return 4
  379.   end
  380.   #--------------------------------------------------------------------------
  381.   def draw_item(index)
  382.     if !craft_command?(index)
  383.       draw_bg(index, item_rect(index), Color.new(0,0,0, 128))
  384.       draw_item_name(index) if @items[index]
  385.     else
  386.       draw_craft_command(index)
  387.     end
  388.   end
  389.   #--------------------------------------------------------------------------
  390.   def draw_bg(index, rect, color)
  391.     contents.fill_rect(rect.x+1, rect.y+1, rect.width-2, rect.height-2, color)
  392.   end
  393.   #--------------------------------------------------------------------------
  394.   def draw_item_name(index)
  395.     change_color(normal_color)
  396.     draw_icon(@items[index].icon_index, item_rect(index).x + 2, item_rect(index).y)
  397.     x = item_rect(index).x + 28
  398.     y = item_rect(index).y
  399.     width = item_rect(index).width - 28
  400.     draw_text(x, y, width, line_height, @items[index].name)
  401.   end
  402.   #--------------------------------------------------------------------------
  403.   def draw_craft_command(index)
  404.     change_color(normal_color, text_enabled?)
  405.     draw_text(item_rect(index), CRAFT, 1)
  406.   end
  407.   #--------------------------------------------------------------------------
  408.   def draw_horz_line(y)
  409.     line_y = y + line_height / 2 - 1
  410.     contents.fill_rect(0, line_y, contents_width, 2, line_color)
  411.   end
  412.   #--------------------------------------------------------------------------
  413.   def refresh
  414.     super
  415.     draw_result
  416.     draw_result_item
  417.     draw_horizontal_lines
  418.   end
  419.   #--------------------------------------------------------------------------
  420.   def draw_result
  421.     change_color(system_color)
  422.     draw_text(0,0,contents.width, line_height, RESULT, 1)
  423.   end
  424.   #--------------------------------------------------------------------------
  425.   def draw_result_item
  426.     return unless items.compact.size > 1
  427.     change_color(normal_color)
  428.     if @result && recipe_learned? && @recipe != :failed
  429.       draw_icon(@result.icon_index, 0, line_height * 2)
  430.       draw_text(24, line_height * 2, contents.width, line_height, @result.name)
  431.       @result = nil
  432.     elsif @recipe == :failed || !recipe_need_to_learn? && recipe_learned?
  433.       draw_text(0,line_height * 2,contents.width, line_height, ALREADY_FAILED, 1)
  434.     else
  435.       draw_text(0,line_height * 2,contents.width, line_height, UNKNOWN_OUTPUT, 1)
  436.     end
  437.   end
  438.   #--------------------------------------------------------------------------
  439.   def draw_horizontal_lines
  440.     draw_horz_line(line_height)
  441.     draw_horz_line(line_height * (top_lines - 1))
  442.     draw_horz_line(line_height * (top_lines + @crafting_slots))
  443.     draw_horz_line(line_height * (top_lines + @crafting_slots + 2))
  444.   end
  445.   #--------------------------------------------------------------------------
  446.   def clear
  447.     @items = [nil] * @crafting_slots
  448.     refresh
  449.   end
  450.   #--------------------------------------------------------------------------
  451.   def update_help
  452.     @help_window.set_item(current_slot)
  453.   end
  454.   #--------------------------------------------------------------------------
  455.   def line_color
  456.     color = normal_color
  457.     color.alpha = 48
  458.     color
  459.   end
  460.   #--------------------------------------------------------------------------
  461.   def process_handling
  462.     return unless open? && active
  463.     return process_ok       if ok_enabled?        && Input.trigger?(:C)
  464.     return process_cancel   if cancel_enabled?    && Input.trigger?(:B)
  465.     return process_pagedown if handle?(:pagedown) && Input.trigger?(:R)
  466.     return process_pageup   if handle?(:pageup)   && Input.trigger?(:L)
  467.     return process_shift    if handle?(:shift)    && Input.trigger?(:A)
  468.   end
  469.   #--------------------------------------------------------------------------
  470.   def process_shift
  471.     Sound.play_cursor
  472.     Input.update
  473.     call_handler(:shift)
  474.   end
  475.   #--------------------------------------------------------------------------
  476. end
  477. #===============================================================================
  478. class Window_ICItemList < Window_Selectable
  479.   #--------------------------------------------------------------------------
  480.   def initialize(x, y)
  481.     super(x, y, window_width, window_height)
  482.     @data = []
  483.     @category = :none
  484.     select(0)
  485.   end
  486.   #--------------------------------------------------------------------------
  487.   def category=(category)
  488.     return if @category == category
  489.     @category = category
  490.     select(0)
  491.     refresh
  492.     self.oy = 0
  493.   end
  494.   #--------------------------------------------------------------------------
  495.   def col_max
  496.     return 1
  497.   end
  498.   #--------------------------------------------------------------------------
  499.   def window_width
  500.     return Graphics.width  / 2
  501.   end
  502.   #--------------------------------------------------------------------------
  503.   def window_height
  504.     Graphics.height - fitting_height(4)
  505.   end
  506.   #--------------------------------------------------------------------------
  507.   def item_max
  508.     @data ? @data.size : 1
  509.   end
  510.   #--------------------------------------------------------------------------
  511.   def item
  512.     @data && index >= 0 ? @data[index] : nil
  513.   end
  514.   def current_item
  515.     return @data[@index]
  516.   end
  517.   #--------------------------------------------------------------------------
  518.   def current_item_enabled?
  519.     return true
  520.   end
  521.   #--------------------------------------------------------------------------
  522.   def enable?(item)
  523.     return true
  524.   end
  525.   #--------------------------------------------------------------------------
  526.   def include?(item)
  527.     return false if item.hide_craft?
  528.     case @category
  529.     when :item
  530.       item.is_a?(RPG::Item)
  531.     when :weapon
  532.       item.is_a?(RPG::Weapon)
  533.     when :armor
  534.       item.is_a?(RPG::Armor)
  535.     else
  536.       false
  537.     end
  538.   end
  539.   #--------------------------------------------------------------------------
  540.   def make_item_list
  541.     @data = $game_party.all_items.select {|item| include?(item) }
  542.     @data.push(nil)
  543.   end
  544.   #--------------------------------------------------------------------------
  545.   def draw_item(index)
  546.     item = @data[index]
  547.     if item
  548.       rect = item_rect(index)
  549.       rect.width -= 4
  550.       draw_item_name(item, rect.x, rect.y, enable?(item))
  551.       draw_item_number(rect, item)
  552.     end
  553.   end
  554.   #--------------------------------------------------------------------------
  555.   def draw_item_number(rect, item)
  556.     draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)
  557.   end
  558.   #--------------------------------------------------------------------------
  559.   def update_help
  560.     @help_window.set_item(item)
  561.   end
  562.   #--------------------------------------------------------------------------
  563.   def refresh
  564.     make_item_list
  565.     create_contents
  566.     draw_all_items
  567.   end
  568.   #--------------------------------------------------------------------------
  569. end
  570. #===============================================================================
  571. class Window_ICResult < Window_Base
  572.   include EVG::CRAFTING::VOCAB
  573.   #--------------------------------------------------------------------------
  574.   def initialize
  575.     super(window_x, window_y, window_width, window_height)
  576.     self.visible = false
  577.     self.opacity = 0
  578.     @frame_counter = 0
  579.   end
  580.   #--------------------------------------------------------------------------
  581.   def window_x
  582.     window_width
  583.   end
  584.   #--------------------------------------------------------------------------
  585.   def window_y
  586.     fitting_height(6)
  587.   end
  588.   #--------------------------------------------------------------------------
  589.   def window_width
  590.     Graphics.width / 2
  591.   end
  592.   #--------------------------------------------------------------------------
  593.   def window_height
  594.     fitting_height(1)
  595.   end
  596.   #--------------------------------------------------------------------------
  597.   def open(item)
  598.     self.visible = true
  599.     if item
  600.       draw_result(item)
  601.     else
  602.       draw_no_result
  603.     end
  604.     @frame_counter = 60
  605.   end
  606.   #--------------------------------------------------------------------------
  607.   def draw_result(item)
  608.     change_color(power_up_color)
  609.     draw_icon(item.icon_index, 0, 0)
  610.     draw_text(24, 0, contents.width - 26, line_height, "#{item.name} +", 0)
  611.   end
  612.   #--------------------------------------------------------------------------
  613.   def draw_no_result
  614.     change_color(power_down_color)
  615.     draw_text(4, 0, contents.width - 4, contents.height, FAILED, 1)
  616.   end
  617.   #--------------------------------------------------------------------------
  618.   def update
  619.     super
  620.     if @frame_counter > 0
  621.       @frame_counter -= 1
  622.       if @frame_counter <= 0
  623.         contents.clear
  624.         self.visible = false
  625.       end
  626.     end
  627.   end
  628.   #--------------------------------------------------------------------------
  629. end
  630. #===============================================================================
  631. class Scene_ICrafting < Scene_MenuBase
  632.   #--------------------------------------------------------------------------
  633.   def start
  634.     super
  635.     @items_back = {}
  636.     @current_recipe = nil
  637.     create_help_window
  638.     create_crafting_window
  639.     create_category_window
  640.     create_item_window
  641.     create_result_window
  642.   end
  643.   #--------------------------------------------------------------------------
  644.   def create_crafting_window
  645.     @crafting_window = Window_ICraftingList.new
  646.     @crafting_window.set_handler(:cancel, method(:return_scene))
  647.     @crafting_window.set_handler(:ok, method(:on_crafting_ok))
  648.     @crafting_window.set_handler(:shift, method(:on_crafting_shift))
  649.     @crafting_window.help_window = @help_window
  650.   end
  651.   #--------------------------------------------------------------------------
  652.   def create_category_window
  653.     @category_window = Window_ICCategory.new
  654.     @category_window.y = @help_window.height
  655.   end
  656.   #--------------------------------------------------------------------------
  657.   def create_item_window
  658.     y = @help_window.height + @category_window.height
  659.     @item_window = Window_ICItemList.new(0, y)
  660.     @item_window.help_window = @help_window
  661.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  662.     @item_window.set_handler(:ok, method(:on_item_ok))
  663.     @category_window.item_window = @item_window
  664.   end
  665.   #--------------------------------------------------------------------------
  666.   def create_result_window
  667.     @result_window = Window_ICResult.new
  668.   end
  669.   #--------------------------------------------------------------------------
  670.   def on_crafting_ok
  671.     if !@crafting_window.current_craft_command?
  672.       @crafting_window.deactivate
  673.       @item_window.activate
  674.     else
  675.       @result_window.open(item)
  676.       process_old_items
  677.       gain_item if item
  678.       CraftingHandler.add_failed_recipe(@crafting_window.items) unless item
  679.       @crafting_window.clear
  680.       @crafting_window.activate
  681.       @crafting_window.select(0)
  682.       @item_window.refresh
  683.     end
  684.   end
  685.   #--------------------------------------------------------------------------
  686.   def on_crafting_shift
  687.     switch_items(@crafting_window.current_slot, nil)
  688.     #check_recipe
  689.   end
  690.   #--------------------------------------------------------------------------
  691.   def on_item_cancel
  692.     @item_window.deactivate
  693.     @crafting_window.activate
  694.   end
  695.   #--------------------------------------------------------------------------
  696.   def on_item_ok
  697.     switch_items(@crafting_window.current_slot, @item_window.current_item)
  698.     on_item_cancel
  699.     @crafting_window.select(@crafting_window.index + 1)
  700.     #check_recipe
  701.   end
  702.   #--------------------------------------------------------------------------
  703.   def check_recipe
  704.     @current_recipe = CraftingHandler.get_recipe_for_list(@crafting_window.items)
  705.     if !@current_recipe
  706.       @crafting_window.recipe = :failed if failed?
  707.     else
  708.       @crafting_window.recipe = @current_recipe
  709.     end
  710.     @crafting_window.result = item
  711.   end
  712.   #--------------------------------------------------------------------------
  713.   def failed?
  714.     CraftingHandler.already_failed?(@crafting_window.items)
  715.   end
  716.   #--------------------------------------------------------------------------
  717.   def switch_items(old_item, new_item)
  718.     @crafting_window.current_slot = new_item
  719.     @items_back[@crafting_window.index] = new_item
  720.     $game_party.lose_item(new_item, 1)
  721.     $game_party.gain_item(old_item, 1) if old_item
  722.     @item_window.refresh
  723.     check_recipe
  724.   end
  725.   #--------------------------------------------------------------------------
  726.   def gain_item
  727.     $game_party.gain_item(item, 1)
  728.     $game_system.add_crafting_recipe(@current_recipe)
  729.   end
  730.   #--------------------------------------------------------------------------
  731.   def process_old_items
  732.     return_items if return_items?
  733.     @items_back = {}
  734.   end
  735.   #--------------------------------------------------------------------------
  736.   def return_items?
  737.     EVG::CRAFTING::CONFIG::GIVE_ITEMS_BACK_WHEN_FAILURE && !item
  738.   end
  739.   #--------------------------------------------------------------------------
  740.   def item
  741.     CraftingHandler.get_object_for_recipe(@current_recipe)
  742.   end
  743.   #--------------------------------------------------------------------------
  744.   def return_scene
  745.     return_items
  746.     super
  747.   end
  748.   #--------------------------------------------------------------------------
  749.   def return_items
  750.     @items_back.values.compact.each{|item| $game_party.gain_item(item, 1)}
  751.   end
  752.   #--------------------------------------------------------------------------
  753. end
  754. #===============================================================================
  755. # RECIPE SCENE
  756. #===============================================================================
  757. if EVG::CRAFTING::CONFIG::MAIN_MENU_COMMAND
  758.   class Window_MenuCommand
  759.     #--------------------------------------------------------------------------
  760.     alias :evg_wmc_aoc_ic   :add_original_commands
  761.     def add_original_commands
  762.       evg_wmc_aoc_ic
  763.       add_command(EVG::CRAFTING::VOCAB::COMMAND, :ic_crafting)
  764.     end
  765.   end
  766.   class Scene_Menu
  767.   #--------------------------------------------------------------------------
  768.   alias :evg_sm_ccw_ic      :create_command_window
  769.     def create_command_window
  770.       evg_sm_ccw_ic
  771.       @command_window.set_handler(:ic_crafting, method(:command_ic_crafting))
  772.     end
  773.     def command_ic_crafting
  774.       SceneManager.call(Scene_ICrafting)
  775.     end
  776.   end
  777.   #--------------------------------------------------------------------------
  778. end
  779. #===============================================================================
  780. class Window_ICRCategory  < Window_ItemCategory
  781.   #--------------------------------------------------------------------------
  782.   def window_width
  783.     Graphics.width - 24
  784.   end
  785.   #--------------------------------------------------------------------------
  786.   def col_max
  787.     return [@list.size, 4].min
  788.   end
  789.   #--------------------------------------------------------------------------
  790.   def make_command_list
  791.     EVG::CRAFTING::CONFIG::CATEGORIES.each do |cat|
  792.       add_command(Vocab.send(cat.to_s),     cat)
  793.     end
  794.   end
  795.   #--------------------------------------------------------------------------
  796. end
  797. #===============================================================================
  798. class Window_ICRecipe_Recipe < Window_Base
  799.   #--------------------------------------------------------------------------
  800.   def initialize(x, y)
  801.     super(x, y, window_width, window_height)
  802.   end
  803.   #--------------------------------------------------------------------------
  804.   def window_width
  805.     Graphics.width / 2 - 18
  806.   end
  807.   #--------------------------------------------------------------------------
  808.   def window_height
  809.     Graphics.height - fitting_height(5) - 24
  810.   end
  811.   #--------------------------------------------------------------------------
  812.   def set_recipe(recipe)
  813.     @recipe = recipe
  814.     refresh
  815.   end
  816.   #--------------------------------------------------------------------------
  817.   def clear
  818.     @recipe = nil
  819.     refresh
  820.   end
  821.   #--------------------------------------------------------------------------
  822.   def refresh
  823.     contents.clear
  824.     return unless @recipe
  825.     change_color(system_color)
  826.     draw_text(0,0, contents.width, line_height, EVG::CRAFTING::VOCAB::RECIPE, 1)
  827.     draw_horz_line(line_height)
  828.     draw_horz_line(line_height * (5 + top_lines))
  829.     draw_backgrounds
  830.     draw_ingredients
  831.   end
  832.   #--------------------------------------------------------------------------
  833.   def draw_horz_line(y)
  834.     line_y = y + line_height / 2 - 1
  835.     contents.fill_rect(0, line_y, contents_width, 2, line_color)
  836.   end
  837.   #--------------------------------------------------------------------------
  838.   def draw_bg(x, y, color)
  839.     contents.fill_rect(x+1, y+1, contents.width-2, line_height-2, color)
  840.   end
  841.   #--------------------------------------------------------------------------
  842.   def draw_backgrounds
  843.     color = Color.new(0,0,0, 128)
  844.     4.times do |i|
  845.       y = (i + top_lines) * line_height
  846.       draw_bg(0, y, color)
  847.     end
  848.   end
  849.   #--------------------------------------------------------------------------
  850.   def draw_ingredients
  851.     return unless @recipe
  852.     change_color(normal_color)
  853.     CraftingHandler.recipes[@recipe].compact.each_with_index do |ingredient, i|
  854.       y = (i + top_lines) * line_height
  855.       draw_icon(ingredient.icon_index, 2, y) if show_icon?
  856.       text = learned? ? ingredient.name : EVG::CRAFTING::VOCAB::UNKNOWN_OUTPUT
  857.       draw_text(28, y, contents.width - 28, line_height, text)
  858.     end
  859.   end
  860.   #--------------------------------------------------------------------------
  861.   def learned?
  862.     return true unless EVG::CRAFTING::CONFIG::SHOW_RECIPES_ONLY_WHEN_LEARNED
  863.     return $game_system.crafting_recipes.include?(@recipe)
  864.   end
  865.   #--------------------------------------------------------------------------
  866.   def show_icon?
  867.     return (learned? || EVG::CRAFTING::CONFIG::SHOW_ICONS)
  868.   end
  869.   #--------------------------------------------------------------------------
  870.   def top_lines
  871.     return 3
  872.   end
  873.   #--------------------------------------------------------------------------
  874.   def line_color
  875.     color = normal_color
  876.     color.alpha = 48
  877.     color
  878.   end
  879.   #--------------------------------------------------------------------------
  880. end
  881. #===============================================================================
  882. class Window_ICR_Help < Window_Base
  883.   #--------------------------------------------------------------------------
  884.   def initialize(line_number = 2)
  885.     super(0, 0, Graphics.width - 24, fitting_height(line_number))
  886.   end
  887.   #--------------------------------------------------------------------------
  888.   def set_text(text)
  889.     if text != @text
  890.       @text = text
  891.       refresh
  892.     end
  893.   end
  894.   #--------------------------------------------------------------------------
  895.   def clear
  896.     set_text("")
  897.   end
  898.   #--------------------------------------------------------------------------
  899.   def set_item(item)
  900.     set_text(item ? item.description : "")
  901.   end
  902.   #--------------------------------------------------------------------------
  903.   def refresh
  904.     contents.clear
  905.     draw_text_ex(4, 0, @text)
  906.   end
  907.   #--------------------------------------------------------------------------
  908. end
  909. #===============================================================================
  910. class Window_ICRecipe_List < Window_Selectable
  911.   #--------------------------------------------------------------------------
  912.   attr_reader     :recipe_window
  913.   #--------------------------------------------------------------------------
  914.   def initialize
  915.     super
  916.   end
  917.   #--------------------------------------------------------------------------
  918.   def initialize(x, y)
  919.     super(x, y, window_width, window_height)
  920.     @category = :none
  921.     @data = []
  922.   end
  923.   #--------------------------------------------------------------------------
  924.   def recipe_window=(window)
  925.     @recipe_window = window
  926.     @recipe_window.set_recipe(current_key)
  927.   end
  928.   #--------------------------------------------------------------------------
  929.   def window_width
  930.     Graphics.width / 2 - 18
  931.   end
  932.   #--------------------------------------------------------------------------
  933.   def window_height
  934.     Graphics.height - fitting_height(5) - 24
  935.   end
  936.   #--------------------------------------------------------------------------
  937.   def category=(category)
  938.     return if @category == category
  939.     @category = category
  940.     refresh
  941.     self.oy = 0
  942.   end
  943.   #--------------------------------------------------------------------------
  944.   def col_max
  945.     return 1
  946.   end
  947.   #--------------------------------------------------------------------------
  948.   def item_max
  949.     @data ? @data.size : 1
  950.   end
  951.   #--------------------------------------------------------------------------
  952.   def item
  953.     @data && index >= 0 ? @data[index] : nil
  954.   end
  955.   #--------------------------------------------------------------------------
  956.   def current_item_enabled?
  957.     return true
  958.   end
  959.   #--------------------------------------------------------------------------
  960.   def include?(recipe, item)
  961.     case @category
  962.     when :item
  963.       item.is_a?(RPG::Item) && !item.key_item?
  964.     when :weapon
  965.       item.is_a?(RPG::Weapon)
  966.     when :armor
  967.       item.is_a?(RPG::Armor)
  968.     when :key_item
  969.       item.is_a?(RPG::Item) && item.key_item?
  970.     else
  971.       false
  972.     end
  973.   end
  974.   #--------------------------------------------------------------------------
  975.   def enable?(item)
  976.     return true
  977.   end
  978.   #--------------------------------------------------------------------------
  979.   def make_item_list
  980.     results = CraftingHandler.results
  981.     @keys = results.map {|recipe, item| recipe if include?(recipe, item) }.compact
  982.     @data = results.map {|recipe, item| item if include?(recipe, item) }.compact
  983.   end
  984.   #--------------------------------------------------------------------------
  985.   def current_key
  986.     @keys[@index] if @keys && @index >= 0
  987.   end
  988.   #--------------------------------------------------------------------------
  989.   def draw_item_name(item, x, y, index)
  990.     return unless item
  991.     draw_icon(item.icon_index, x, y) if show_icon?(index)
  992.     change_color(normal_color)
  993.     text = learned?(index) ? item.name : EVG::CRAFTING::VOCAB::UNKNOWN_OUTPUT
  994.     draw_text(x + 24, y, width, line_height, text)
  995.   end
  996.   #--------------------------------------------------------------------------
  997.   def draw_item(index)
  998.     item = @data[index]
  999.     if item
  1000.       rect = item_rect(index)
  1001.       rect.width -= 4
  1002.       draw_item_name(item, rect.x, rect.y, index)
  1003.     end
  1004.   end
  1005.   #--------------------------------------------------------------------------
  1006.   def update_help
  1007.     if learned?(@index)
  1008.       @help_window.set_item(item)
  1009.     else
  1010.       @help_window.set_text(EVG::CRAFTING::VOCAB::NOT_LEARNED) if item
  1011.     end
  1012.     return unless @recipe_window
  1013.     @recipe_window.set_recipe(current_key)
  1014.   end
  1015.   #--------------------------------------------------------------------------
  1016.   def refresh
  1017.     make_item_list
  1018.     create_contents
  1019.     draw_all_items
  1020.     @recipe_window.refresh if @recipe_window
  1021.   end
  1022.   #--------------------------------------------------------------------------
  1023.   def learned?(index)
  1024.     return true unless EVG::CRAFTING::CONFIG::SHOW_RECIPES_ONLY_WHEN_LEARNED
  1025.     return $game_system.crafting_recipes.include?(@keys[index])
  1026.   end
  1027.   #--------------------------------------------------------------------------
  1028.   def show_icon?(index)
  1029.     return (learned?(index) || EVG::CRAFTING::CONFIG::SHOW_ICONS)
  1030.   end
  1031.   #--------------------------------------------------------------------------
  1032. end
  1033. #===============================================================================
  1034. class Scene_ICRecipes < Scene_MenuBase
  1035.   #--------------------------------------------------------------------------
  1036.   def start
  1037.     super
  1038.     create_help_window
  1039.     create_category_window
  1040.     create_item_window
  1041.     create_recipe_window
  1042.     @help_window.y = 36 + @category_window.height + @item_window.height
  1043.     @help_window.x = 12
  1044.   end
  1045.   #--------------------------------------------------------------------------
  1046.   def create_help_window
  1047.     @help_window = Window_ICR_Help.new
  1048.   end
  1049.   #--------------------------------------------------------------------------
  1050.   def create_category_window
  1051.     @category_window = Window_ICRCategory.new
  1052.     @category_window.x = 12
  1053.     @category_window.y = 12
  1054.     @category_window.set_handler(:ok,     method(:on_category_ok))
  1055.     @category_window.set_handler(:cancel, method(:return_scene))
  1056.   end
  1057.   #--------------------------------------------------------------------------
  1058.   def create_item_window
  1059.     @item_window = Window_ICRecipe_List.new(12, @category_window.height + 24)
  1060.     @item_window.help_window = @help_window
  1061.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  1062.     @category_window.item_window = @item_window
  1063.   end
  1064.   #--------------------------------------------------------------------------
  1065.   def create_recipe_window
  1066.     y = @category_window.height + 24
  1067.     @recipe_window = Window_ICRecipe_Recipe.new(@item_window.width + 24, y)
  1068.     @item_window.recipe_window = @recipe_window
  1069.   end
  1070.   #--------------------------------------------------------------------------
  1071.   def on_category_ok
  1072.     @item_window.activate
  1073.     @item_window.select(0)
  1074.   end
  1075.   #--------------------------------------------------------------------------
  1076.   def on_item_cancel
  1077.     @item_window.unselect
  1078.     @recipe_window.clear
  1079.     @help_window.clear
  1080.     @category_window.activate
  1081.   end
  1082.   #--------------------------------------------------------------------------
  1083. end
  1084. #===============================================================================
  1085. if EVG::CRAFTING::CONFIG::RECIPE_MAIN_MENU_COMMAND
  1086.   class Window_MenuCommand
  1087.     #--------------------------------------------------------------------------
  1088.     alias :evg_wmc_aoc_icr   :add_original_commands
  1089.     def add_original_commands
  1090.       evg_wmc_aoc_icr
  1091.       add_command(EVG::CRAFTING::VOCAB::RECIPE_COMMAND, :ic_recipes)
  1092.     end
  1093.     #--------------------------------------------------------------------------
  1094.   end
  1095.   class Scene_Menu
  1096.   #--------------------------------------------------------------------------
  1097.     alias :evg_sm_ccw_icr      :create_command_window
  1098.     #--------------------------------------------------------------------------
  1099.     def create_command_window
  1100.       evg_sm_ccw_icr
  1101.       @command_window.set_handler(:ic_recipes, method(:command_ic_recipes))
  1102.     end
  1103.     def command_ic_recipes
  1104.       SceneManager.call(Scene_ICRecipes)
  1105.     end
  1106.     #--------------------------------------------------------------------------
  1107.   end
  1108.   #--------------------------------------------------------------------------
  1109. end
  1110. #===============================================================================
  1111. # SCRIPT END
  1112. #===============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement