Advertisement
Vlue

Basic Recipe Crafting

Feb 25th, 2014
2,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.30 KB | None | 0 0
  1. #Basic Recipe Crafting v1.1a
  2. #----------#
  3. #Features: Recipe crafting! Yeesh.
  4. #
  5. #Usage:    Set up your recipes, learn recipes, make items! Yay!
  6. #       SceneManager.call(Scene_Crafting)   - opens the Crafting meny
  7. #       learn_recipe(id)                    - teaches that recipe
  8. #       forget_recipe(id)                   - forgets that recipe
  9. #
  10. #----------#
  11. #-- Script by: V.M of D.T
  12. #
  13. #- Questions or comments can be:
  14. #    given by email: sumptuaryspade@live.ca
  15. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  16. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  17. #
  18. #- Free to use in any project with credit given, donations always welcome!
  19.  
  20. #How the recipes work:
  21. #   ID = > [ result , [ materials ] ],
  22. #  Result = one item set up like... [item_type,item_id,amount]
  23. #  Materials are set up the same way but in an array to account for multiples:
  24. #   [item_type,item_id,amount],[item_type,item_id,amount],etc...
  25. #  Item_type is 0 for items, 1 for weapons, 2 for armors
  26. #
  27. # Example for making 1 Weapon 3 using 2 Item 2's, 1 Weapon 1, and 1 Armor 1:
  28. # 30 => [[1,3,1],[[0,2,2],[1,1,1],[2,1,1]]],
  29.  
  30. USE_WA_RANDOMIZATION = false
  31.  
  32. RECIPES = {
  33.   0 => [ [0,1,1] , [ [0,17,2] ] ],
  34.   1 => [ [0,2,1] , [ [0,17,1],[0,18,2] ] ],
  35.   2 => [ [0,3,1] , [ [0,17,1],[0,18,1],[0,19,2] ] ],
  36.   3 => [ [0,1,1] , [ [0,17,2] ] ],
  37.   4 => [ [0,2,1] , [ [0,17,1],[0,18,2] ] ],
  38.   5 => [ [0,3,1] , [ [0,17,1],[0,18,1],[0,19,2] ] ],
  39.   6 => [ [0,1,1] , [ [0,17,2] ] ],
  40.   7 => [ [0,2,1] , [ [0,17,1],[0,18,2] ] ],
  41.   8 => [ [0,3,1] , [ [0,17,1],[0,18,1],[0,19,2] ] ],
  42.   9 => [ [0,1,1] , [ [0,17,2] ] ],
  43.   10 => [ [0,2,1] , [ [0,17,1],[0,18,2] ] ],
  44.   11 => [ [0,3,1] , [ [0,17,1],[0,18,1],[0,19,2] ] ],
  45.   12 => [ [0,1,1] , [ [0,17,2] ] ],
  46.   13 => [ [0,2,1] , [ [0,17,1],[0,18,2] ] ],
  47.   14 => [ [0,3,1] , [ [0,17,1],[0,18,1],[0,19,2] ] ],
  48.  
  49.  
  50.   }
  51.  
  52.  
  53. class Recipe
  54.   attr_accessor :result
  55.   attr_accessor :materials
  56.   attr_accessor :known
  57.   attr_accessor :id
  58.   def initialize(id,res,mat)
  59.     @id = id
  60.     @result = Material.new(res[0],res[1],res[2])
  61.     @materials = []
  62.     for item in mat
  63.       @materials.push(Material.new(item[0],item[1],item[2]))
  64.     end
  65.     @known = false
  66.   end
  67.   def name
  68.     return @result.item.name
  69.   end
  70.   def has_materials?
  71.     for item in @materials
  72.       return false unless $game_party.item_number(item.item) >= item.amount
  73.     end
  74.     return true
  75.   end
  76.   def craft
  77.     remove_materials
  78.     add_result
  79.   end
  80.   def remove_materials
  81.     for item in @materials
  82.       $game_party.gain_item(item.item,-item.amount)
  83.     end
  84.   end
  85.   def add_result
  86.     if USE_WA_RANDOMIZATION
  87.       $game_party.add_weapon(@result.item.id,@result.amount) if @result.item.is_a?(RPG::Weapon)
  88.       $game_party.add_armor(@result.item.id,@result.amount) if @result.item.is_a?(RPG::Armor)
  89.       $game_party.add_item(@result.item.id,@result.amount) if @result.item.is_a?(RPG::Item)
  90.     else
  91.       $game_party.gain_item(@result.item,@result.amount)
  92.     end
  93.   end
  94. end
  95.  
  96. class Material
  97.   attr_accessor :item
  98.   attr_accessor :amount
  99.   def initialize(type, id, amount)
  100.     @item = $data_items[id] if type == 0
  101.     @item = $data_weapons[id] if type == 1
  102.     @item = $data_armors[id] if type == 2
  103.     @amount = amount
  104.   end
  105. end
  106.  
  107. module DataManager
  108.   class << self
  109.     alias rec_cgo create_game_objects
  110.     alias rec_msc make_save_contents
  111.     alias rec_esc extract_save_contents
  112.   end
  113.   def self.create_game_objects
  114.     rec_cgo
  115.     $game_recipes = create_recipes
  116.   end
  117.   def self.make_save_contents
  118.     contents = rec_msc
  119.     contents[:recipe] = $game_recipes
  120.     contents
  121.   end
  122.   def self.extract_save_contents(contents)
  123.     rec_esc(contents)
  124.     $game_recipes = contents[:recipe]
  125.   end
  126.   def self.create_recipes
  127.     recipes = {}
  128.     RECIPES.each_pair do |key, val|
  129.       recipes[key] = Recipe.new(key,val[0],val[1])
  130.     end
  131.     recipes
  132.   end
  133. end
  134.  
  135. class Game_Interpreter
  136.   def learn_recipe(id)
  137.     return if $game_recipes[id].nil?
  138.     $game_recipes[id].known = true
  139.   end
  140.   def forget_recipe(id)
  141.     return if $game_recipes[id].nil?
  142.     $game_recipes[id].known = false
  143.   end
  144. end
  145.  
  146. class Window_RecipeList < Window_Selectable
  147.   def initialize(x,y,w,h)
  148.     super
  149.     @data = $game_recipes.values.select {|recipe| include?(recipe)}
  150.     self.contents = Bitmap.new(self.contents.width,@data.size*24)
  151.     select(0)
  152.     activate
  153.     refresh
  154.   end
  155.   def item_max
  156.     @data ? @data.size : 1
  157.   end
  158.   def item
  159.     @data && index >= 0 ? @data[index] : nil
  160.   end
  161.   def current_item_enabled?
  162.     enable?(@data[index])
  163.   end
  164.   def include?(item)
  165.     item.known
  166.   end
  167.   def enable?(item)
  168.     return false if item.nil?
  169.     item.has_materials?
  170.   end
  171.   def draw_item(index)
  172.     item = @data[index]
  173.     if item
  174.       rect = item_rect(index)
  175.       rect.width -= 4
  176.       draw_item_name(item.result.item, rect.x, rect.y, enable?(item))
  177.     end
  178.   end
  179.   def current_item
  180.     @data[index]
  181.   end
  182.   def process_ok
  183.     if current_item_enabled?
  184.       Sound.play_ok
  185.       Input.update
  186.       call_ok_handler
  187.     else
  188.       Sound.play_buzzer
  189.     end
  190.   end
  191. end
  192.  
  193. class Window_RecipeDetail < Window_Base
  194.   def initialize(x,y,w,h)
  195.     super
  196.     @recipe = nil
  197.   end
  198.   def set_recipe(recipe)
  199.     @recipe = recipe
  200.     refresh
  201.   end
  202.   def refresh
  203.     contents.clear
  204.     return if @recipe.nil?
  205.     change_color(normal_color)
  206.     draw_text(0,0,self.width,line_height,"Required:")
  207.     yy = line_height
  208.     for item in @recipe.materials
  209.       change_color(normal_color, $game_party.item_number(item.item) >= item.amount)
  210.       draw_icon(item.item.icon_index,0,yy)
  211.       draw_text(24,yy,self.width,line_height,item.item.name)
  212.       string = $game_party.item_number(item.item).to_s + "/" + item.amount.to_s
  213.       draw_text(0,yy,self.contents.width,line_height,string,2)
  214.       yy += line_height
  215.     end
  216.   end
  217. end
  218.  
  219. class Window_RecipeConfirm < Window_Selectable
  220.   def initialize(x,y,w,h)
  221.     super
  222.     self.opacity = 0
  223.     refresh
  224.   end
  225.   def item_max; 1; end;
  226.   def enable?(item); true; end;
  227.   def refresh
  228.     super
  229.     draw_text(0,0,self.contents.width,line_height,"Craft",1)
  230.   end
  231.   def activate
  232.     super
  233.     select(0)
  234.   end
  235.   def deactivate
  236.     super
  237.     select(-1)
  238.   end
  239. end
  240.  
  241. class Scene_Crafting < Scene_Base
  242.   def start
  243.     super
  244.     @top_help_window = Window_Help.new(1)
  245.     @top_help_window.set_text("Select recipe to craft:")
  246.     @bottom_help_window = Window_Help.new
  247.     @bottom_help_window.y = Graphics.height - @bottom_help_window.height
  248.     width = Graphics.width / 2
  249.     height = Graphics.height - @top_help_window.height - @bottom_help_window.height
  250.     @list_window = Window_RecipeList.new(0,@top_help_window.height,width,height)
  251.     @list_window.set_handler(:ok, method(:list_success))
  252.     @list_window.set_handler(:cancel, method(:cancel))
  253.     @detail_window = Window_RecipeDetail.new(width,@list_window.y,width,height)
  254.     height = @list_window.y + @list_window.height - @top_help_window.height
  255.     @confirm_window = Window_RecipeConfirm.new(width,height,width,@top_help_window.height)
  256.     @confirm_window.set_handler(:ok, method(:craft_success))
  257.     @confirm_window.set_handler(:cancel, method(:confirm_cancel))
  258.   end
  259.   def update
  260.     super
  261.     @bottom_help_window.set_text(@list_window.current_item.result.item.description) if !@list_window.current_item.nil?
  262.     @detail_window.set_recipe(@list_window.current_item)
  263.   end
  264.   def list_success
  265.     @list_window.deactivate
  266.     @confirm_window.activate
  267.   end
  268.   def craft_success
  269.     @list_window.current_item.craft
  270.     @list_window.refresh
  271.     @list_window.activate
  272.   end
  273.   def confirm_cancel
  274.     @confirm_window.deactivate
  275.     @list_window.activate
  276.   end
  277.   def cancel
  278.     SceneManager.return
  279.   end
  280. end
  281.  
  282. class Window_MenuCommand < Window_Command
  283.   alias recipe_aoc add_original_commands
  284.   def add_original_commands
  285.     recipe_aoc
  286.     rec = $game_recipes.values.select {|recipe| recipe.known}
  287.     add_command("Crafting", :recipe, rec.size > 0)
  288.   end
  289. end
  290.  
  291. class Scene_Menu < Scene_MenuBase
  292.   alias recipe_create_command_window create_command_window
  293.   def create_command_window
  294.     recipe_create_command_window
  295.     @command_window.set_handler(:recipe,   method(:command_recipe))
  296.   end
  297.   def command_recipe
  298.     SceneManager.call(Scene_Crafting)
  299.   end
  300. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement