Item Crafting System

Item Crafting System Version: 1.00
By: arev

Introduction
Simple and effective crafting system.

Features
  • Mix up to six components.
  • Use items, weapons and armors.
  • Automatic sorting of the components.

Screenshots
Image
http://img148.imageshack.us/img148/5505/screnh.png

Demo
http://www.megaupload.com/?d=CFL41HEH

Script
script Spoiler
  1. #==============================================================================
  2. # ** Item Crafting System
  3. #    version 1.00
  4. #    created by arev (rmxp.pl)
  5. #    released 28-12-2009
  6. #==============================================================================
  7. # Installation & Compatibility:
  8. #
  9. # Copy this tab and paste it above 'Main' in your project.
  10. # The system is compatible with default RMXP system.
  11. # It doesn't override any methods, so it's probably compatible
  12. # with lots of other things.
  13. #
  14. #
  15. #
  16. # Usage:
  17. #
  18. # Below this introduction you'll find an array called RECIPES.
  19. # New recipes are added with new arrays like the following:
  20. # ['i12', 'i78', 'w3', 'a45', 'a45', 'z', 'i-99']
  21. # i stands for items, w for weapons and a for armors.
  22. # Acording to this recipe - to make an item of ID 99 you need
  23. # one item with ID 12, one item with ID 78, one weapon with ID 3,
  24. # two armors with ID 45. There is a maximum number of six components.
  25. # If you want to make a recipe consisting of smaller number of
  26. # components just fill the remaining fields with 'z'.
  27. # The last ithem in the array is the recipe result. The first character
  28. # works just like before, then there's a dash required, followed by ID
  29. # of an item.
  30. #
  31. # To enter the crafting scene call a script like this:
  32. # $scene = Scene_Craft.new
  33. #
  34. # During the crafting the order of items in the component window
  35. # is not important - they are sorted within the script.
  36. #
  37. #
  38. #
  39. # License:
  40. #
  41. # You are free to use this script in your non-commercial project.
  42. # You can redistribute this script (free of charge) only if you provide
  43. # information about its original author.
  44. # If you want to use this script in a commercial project please contact me
  45. # via http://www.rmxp.pl or http://www.hbgames.org/forums private message system.
  46. #==============================================================================
  47.  
  48. RECIPES = [
  49.           ["i1", "i1", "i2", "z", "z", "z", "i-3"],
  50.           ["i3", "i3", "i3", "i3", "i1", "i2", "i-5"],
  51.           ]
  52. #------------------------------------------------------------------------------
  53. class Window_Craft_Components < Window_Selectable
  54.  
  55.   attr_accessor :data
  56.  
  57.   def initialize
  58.     super(0, 64, 320, 224)
  59.     self.contents = Bitmap.new(width - 32, height - 32)
  60.     self.index = 0
  61.     self.active = true
  62.     @data = [nil, nil, nil, nil, nil, nil]
  63.     @item_max = 6
  64.     refresh
  65.   end
  66.  
  67.   def item
  68.     return @data[self.index]
  69.   end
  70.  
  71.   def refresh
  72.     self.contents.clear
  73.     for i in 0..5
  74.       x = 0
  75.       y = i * 32
  76.       if @data[i] != nil
  77.         item = @data[i]
  78.         bitmap = RPG::Cache.icon(item.icon_name)
  79.         self.contents.blt(4 + x, y + 4, bitmap, Rect.new(0, 0, 24, 24))
  80.         self.contents.font.color = normal_color
  81.         self.contents.draw_text(x + 32, y, 160, 32, item.name, 0)
  82.       else
  83.         self.contents.font.color = disabled_color
  84.         self.contents.draw_text(4 + x, y, 160, 32, "Empty", 0)
  85.       end
  86.     end
  87.   end
  88.  
  89.   def update_cursor_rect
  90.     self.cursor_rect.set(0, self.index * 32, 288, 32)
  91.   end
  92.  
  93.   def nullify
  94.     @data = [nil, nil, nil, nil, nil, nil]
  95.   end
  96.  
  97.   def transmutation
  98.     temp = []
  99.     for i in 0..5
  100.       if @data[i] == nil
  101.         temp << "0"
  102.       else
  103.         case @data[i]
  104.         when RPG::Item
  105.           temp << "i" + @data[i].id
  106.         when RPG::Weapon
  107.           temp << "w" + @data[i].id
  108.         when RPG::Armor
  109.           temp << "a" + @data[i].id
  110.         end
  111.       end
  112.     end
  113.     temp = temp.sort
  114.     return temp
  115.   end
  116.  
  117. end
  118. #------------------------------------------------------------------------------
  119. class Window_Craft_Result < Window_Base
  120.  
  121.   attr_accessor :item
  122.  
  123.   def initialize
  124.     super(320,64,320,224)
  125.     self.contents = Bitmap.new(width - 32, height - 32)
  126.     @item = nil
  127.     @item_code = ""
  128.     refresh
  129.   end
  130.  
  131.   def refresh
  132.     self.contents.clear
  133.     if @item == nil
  134.       self.contents.font.color = normal_color
  135.       self.contents.draw_text(4,0,288,32, "Select ingredients.", 0)
  136.     else
  137.       bitmap = RPG::Cache.icon(@item.icon_name)
  138.       self.contents.font.color = text_color(3)
  139.       self.contents.draw_text(4,0,288,32, "Spectacular success!", 0)
  140.       self.contents.blt(4, 65, bitmap, Rect.new(0, 0, 24, 24))
  141.       self.contents.font.color = normal_color
  142.       self.contents.draw_text(32, 65, 288, 32, @item.name, 0)
  143.       self.contents.font.color = disabled_color
  144.       self.contents.draw_text(4,128,288,32, "Press the 'Z' button to finalize", 0)
  145.       self.contents.draw_text(4,160,288,32, "the crafting process!", 0)
  146.     end
  147.   end
  148.  
  149.   def make_item(text="")
  150.     @item_code = text
  151.     if @item_code.include?("-")
  152.       temp = @item_code.split("-")
  153.       @item = (temp[0] == "i" ? $data_items : "w" ? $data_weapons : $data_armors)[temp[1].to_i]
  154.       refresh
  155.     end
  156.   end
  157.  
  158.   def nullify
  159.     @item = nil
  160.     refresh
  161.   end
  162.  
  163. end
  164. #------------------------------------------------------------------------------
  165. class Window_Craft_Item < Window_Selectable
  166.  
  167.   def initialize
  168.     super(0, 288, 640, 192)
  169.     @column_max = 2
  170.     refresh
  171.     self.active = false
  172.     self.index = -1
  173.   end
  174.  
  175.   def item
  176.     return @data[self.index]
  177.   end
  178.  
  179.   def refresh
  180.     if self.contents != nil
  181.       self.contents.dispose
  182.       self.contents = nil
  183.     end
  184.     @data = []
  185.     for i in 1...$data_items.size
  186.       if $game_party.item_number(i) > 0
  187.         @data.push($data_items[i])
  188.       end
  189.     end
  190.     unless $game_temp.in_battle
  191.       for i in 1...$data_weapons.size
  192.         if $game_party.weapon_number(i) > 0
  193.           @data.push($data_weapons[i])
  194.         end
  195.       end
  196.       for i in 1...$data_armors.size
  197.         if $game_party.armor_number(i) > 0
  198.           @data.push($data_armors[i])
  199.         end
  200.       end
  201.     end
  202.     @data.push(nil)
  203.     @item_max = @data.size
  204.     self.contents = Bitmap.new(width - 32, row_max * 32)
  205.     for i in 0...@item_max-1
  206.       draw_item(i)
  207.     end
  208.   end
  209.  
  210.   def draw_item(index)
  211.     item = @data[index]
  212.     x = 4 + index % @column_max * 320
  213.     y = index / @column_max * 32
  214.     case item
  215.     when RPG::Item
  216.       number = $game_party.item_number(item.id)
  217.     when RPG::Weapon
  218.       number = $game_party.weapon_number(item.id)
  219.     when RPG::Armor
  220.       number = $game_party.armor_number(item.id)
  221.     end
  222.     bitmap = RPG::Cache.icon(item.icon_name)
  223.     self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24))
  224.     self.contents.font.color = normal_color
  225.     self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
  226.     self.contents.draw_text(x + 240, y, 16, 32, ":", 1)
  227.     self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)
  228.   end
  229.  
  230.   def update_help
  231.     @help_window.set_text(self.item == nil ? "" : self.item.description)
  232.   end
  233.  
  234.   def update_cursor_rect
  235.     if @index < 0
  236.       self.cursor_rect.empty
  237.       return
  238.     end
  239.     row = @index / @column_max
  240.     if row < self.top_row
  241.       self.top_row = row
  242.     end
  243.     if row > self.top_row + (self.page_row_max - 1)
  244.       self.top_row = row - (self.page_row_max - 1)
  245.     end
  246.     x = index % @column_max * 320
  247.     y = index / @column_max * 32 - self.oy
  248.     self.cursor_rect.set(x, y, 288, 32)
  249.   end
  250.  
  251. end
  252. #------------------------------------------------------------------------------
  253. class Scene_Craft
  254.  
  255.   def main
  256.     @components = Window_Craft_Components.new
  257.     @result = Window_Craft_Result.new
  258.     @item_window = Window_Craft_Item.new
  259.     @help_window = Window_Help.new
  260.     @help_window.set_text("Item Crafting System", 1)
  261.     Graphics.transition
  262.     loop do
  263.       Input.update
  264.       Graphics.update
  265.       update
  266.       if $scene != self
  267.         break
  268.       end
  269.     end
  270.     Graphics.freeze
  271.     @components.dispose
  272.     @result.dispose
  273.     @item_window.dispose
  274.     @help_window.dispose
  275.   end
  276.  
  277.   def update
  278.     @item_window.update
  279.     @components.update    
  280.     if @components.active
  281.       update_comp
  282.       return
  283.     end
  284.     if @item_window.active
  285.       update_item
  286.       return
  287.     end
  288.   end
  289.  
  290.   def update_comp
  291.     if Input.trigger?(Input::A)
  292.       rock_baby
  293.       return
  294.     end
  295.     if Input.trigger?(Input::C)
  296.       $game_system.se_play($data_system.decision_se)
  297.       @components.active = false
  298.       @item_window.active = true
  299.       @item_window.index = 0
  300.     end
  301.     if Input.trigger?(Input::B)
  302.       for i in 0..5
  303.         item = @components.data[i]
  304.         if item != nil
  305.           case item
  306.           when RPG::Item
  307.             $game_party.gain_item(item.id, 1)
  308.           when RPG::Weapon
  309.             $game_party.gain_weapon(item.id, 1)
  310.           when RPG::Armor
  311.             $game_party.gain_armor(item.id, 1)
  312.           end
  313.         end
  314.       end          
  315.       $game_system.se_play($data_system.cancel_se)
  316.       $scene = Scene_Map.new
  317.     end
  318.   end
  319.  
  320.   def update_item
  321.     if Input.trigger?(Input::C)
  322.       $game_system.se_play($data_system.decision_se)
  323.       component = @components.data[@components.index]
  324.       if component != nil
  325.         case component
  326.         when RPG::Item
  327.           $game_party.gain_item(component.id, 1)
  328.         when RPG::Weapon
  329.           $game_party.gain_weapon(component.id, 1)
  330.         when RPG::Armor
  331.           $game_party.gain_armor(component.id, 1)
  332.         end
  333.       end
  334.       item = @item_window.item
  335.       @components.data[@components.index] = item
  336.       case item
  337.       when RPG::Item
  338.         $game_party.lose_item(item.id, 1)
  339.       when RPG::Weapon
  340.         $game_party.lose_weapon(item.id, 1)
  341.       when RPG::Armor
  342.         $game_party.lose_armor(item.id, 1)
  343.       end
  344.       @item_window.refresh
  345.       @components.refresh
  346.       check_recipes
  347.      
  348.       @item_window.active = false
  349.       @item_window.index = -1
  350.       @components.active = true
  351.     end
  352.     if Input.trigger?(Input::B)
  353.       $game_system.se_play($data_system.cancel_se)
  354.       @item_window.active = false
  355.       @item_window.index = -1
  356.       @components.active = true
  357.     end
  358.   end
  359.  
  360.   def check_recipes
  361.     things_in_the_pot = []
  362.     for i in 0..5
  363.       case @components.data[i]
  364.       when nil
  365.         things_in_the_pot << "z"
  366.       when RPG::Item
  367.         things_in_the_pot << "i#{@components.data[i].id}"
  368.       when RPG::Weapon
  369.         things_in_the_pot << "w#{@components.data[i].id}"
  370.       when RPG::Armor
  371.         things_in_the_pot << "a#{@components.data[i].id}"
  372.       end
  373.     end
  374.    
  375.     things_in_the_pot = things_in_the_pot.sort
  376.     for j in 0...RECIPES.size
  377.       temp = RECIPES[j].dup
  378.       temp[6] = nil
  379.       if things_in_the_pot.size == 6
  380.         things_in_the_pot << nil
  381.       else
  382.         things_in_the_pot[6] = nil
  383.       end
  384.       if things_in_the_pot == temp
  385.         @result.make_item(RECIPES[j][6])
  386.         return
  387.       else
  388.         @result.nullify
  389.       end
  390.     end
  391.   end
  392.  
  393.   def rock_baby
  394.     case @result.item
  395.     when nil
  396.       $game_system.se_play($data_system.buzzer_se)
  397.     when RPG::Item
  398.       $game_party.gain_item(@result.item.id, 1)
  399.       finalize_crafting
  400.     when RPG::Weapon
  401.       $game_party.gain_weapon(@result.item.id, 1)
  402.       finalize_crafting
  403.     when RPG::Armor
  404.       $game_party.gain_armor(@result.item.id, 1)
  405.       finalize_crafting
  406.     end
  407.   end
  408.  
  409.   def finalize_crafting
  410.     $game_system.se_play($data_system.save_se)
  411.     @components.nullify
  412.     @components.refresh
  413.     @item_window.refresh
  414.     @result.nullify
  415.   end
  416.  
  417. end
Expand to see the code.


Instructions
Copy this tab and paste it above 'Main' in your project.

In the script you'll find an array called RECIPES. New recipes are added with new arrays like the following:
['i12', 'i78', 'w3', 'a45', 'a45', 'z', 'i-99']
i stands for items, w for weapons and a for armors.
Acording to this recipe - to make an item of ID 99 you need one item with ID 12, one item with ID 78, one weapon with ID 3, two armors with ID 45. There is a maximum number of six components. If you want to make a recipe consisting of smaller number of components just fill the remaining fields with 'z'. The last ithem in the array is the recipe result. The first character works just like before, then there's a dash required, followed by ID of an item.

To enter the crafting scene call a script like this: $scene = Scene_Craft.new

During the crafting the order of items in the component window is not important - they are sorted within the script.

Compatibility
The system is compatible with default RMXP system. It doesn't override any methods, so it's probably compatible with lots of other things.

Credits and Thanks
arev of rmxp.pl :]

Terms and Conditions
You are free to use this script in your non-commercial project. You can redistribute this script (free of charge) only if you provide information about its original author. If you want to use this script in a commercial project please contact me via http://www.rmxp.pl or http://www.hbgames.org/forums private message system.
Great addition Arev - I'm currently working on a XAS game with Gameface (Gameface101.com) I was just wondering whether it would be possible to edit the script so that it isn't limited to 7 items - for example:

You could take:

50 Blueberries
10 Raspberries
5 Dot Leafs

To create a healing potion.

That'd really make the script something special 😀
Hi Calvin. I'm glad you like the script. While it is totally possible to do what you've described, I'm afraid I won't have the time to alter the script that much.
Take care.
No worries Arev, thanks for the quick reply.
I'm adding this system to my game. Great work!

A little suggestion:

When player have enough components to mass produce some thing like potions, the repetitive progress of selecting components is quite annoying.

Maybe add something like ask the player "How many?" or simply let the seleted components stay in the list if available.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=69104