eugene222

"Do It Yourself" - Crafting (DEU)

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