Advertisement
IceDragon200

IEI020-CraftSystem (WIP)

Mar 12th, 2012
1,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.93 KB | None | 0 0
  1. #==============================================================================#
  2. # ♥ Craft System
  3. #==============================================================================#
  4. # // • Created By    : IceDragon (rpgmakervxace.com)
  5. # // • Modified By   : IceDragon (rpgmakervxace.com)
  6. # // • Data Created  : 03/12/2012
  7. # // • Data Modified : 03/12/2012
  8. # // • Version       : 0.9
  9. # // • IEI ID        : 20
  10. #==============================================================================#
  11. # ● Change Log
  12. #     ♣ 03/12/2012 V0.9
  13. #         Started & Finished
  14. #         The reason this isnt 1.0, is that some methods need to be moved
  15. #         to the IEI Core, but until then, this is what its gonna be.
  16. #
  17. #==============================================================================#
  18. # ● How To Use
  19. #     In an Item, Weapon, or Armor notebox use this tag:
  20. #     <crafting: Nid>
  21. #     <crafting: Nid, Nid, Nid..>
  22. #     Replace N with:
  23. #       A - For Armor
  24. #       W - For Weapon
  25. #       I - For Item
  26. #     Replace id with well the object's Id
  27. #
  28. #     EG:
  29. #     <crafting: W1, I1>
  30. #        That creates a healing axe, using Weapon 1 and Item 1
  31. #     You can add as many crafting tags as you like, so you can have
  32. #     several recipes and 1 outcome
  33. #
  34. #     Pressing L/R in the input window will switch to the output window
  35. #    
  36. #==============================================================================#
  37. module IEI
  38.   module CraftSystem  
  39.     # // (JUMP:MaterialCount) find that if you need to change the material count
  40.     # // Craft functions
  41.     def self.item2craft(item)
  42.       return [IEI.item_sym(item), item ? item.id : nil]
  43.     end
  44.     def self.craft2item(craft)
  45.       return nil unless(craft)
  46.       sym, id = craft
  47.       case(sym)
  48.       when :item   ; $data_items[id]
  49.       when :skill  ; $data_skills[id]  # // Still not used
  50.       when :weapon ; $data_weapons[id]
  51.       when :armor  ; $data_armors[id]
  52.       else         ; nil  
  53.       end  
  54.     end  
  55.     def self.sort_recipe(recipe)
  56.       return recipe.sort_by{|a|craft2item(a).db_id}
  57.     end    
  58.     def self.mk_recipe_list!(items)
  59.       @recipe_list = mk_recipe_list_abs(items) unless(@recipe_list)
  60.     end  
  61.     def self.mk_recipe_list_abs(items)
  62.       recipe_list = {}
  63.       pc = nil # // Forward Dec.
  64.       items.each do |item| pc = item2craft(item) # // Convert2Craft
  65.         item.craft_recipes.each do |r|recipe_list[sort_recipe(r)]=pc ; end
  66.       end  
  67.       return recipe_list
  68.     end # // mk_recipe_list
  69.     def self.item_from_recipe(recipe)
  70.       return craft2item(@recipe_list[sort_recipe(recipe)])
  71.     end  
  72.     def self.items2recipe(items)
  73.       return items.collect{|i|item2craft(i)}
  74.     end  
  75.   end # // Craft System
  76.   module REGEXP
  77.     module BaseItem
  78.       # // <crafting: i1, w2, a7>
  79.       CRAFT_MATERIAL = /<CRAFTING:[ ]*((?:I|W|A)\d+(?:\s*,\s*(?:I|W|A)\d+)*)>/i
  80.       CRAFT_ID       = /(I|W|A)(\d+)/i
  81.     end  
  82.   end
  83.   # // Core
  84.   def self.item_sym(item)
  85.     case(item)
  86.     when RPG::Item  ; :item
  87.     when RPG::Skill ; :skill # // Not used for craft
  88.     when RPG::Weapon; :weapon
  89.     when RPG::Armor ; :armor
  90.     else            ; :nil
  91.     end  
  92.   end
  93.   # // Array to Count Hash
  94.   def self.a2count_hash(array)
  95.     array.inject(Hash.new){|hash,element|hash[element]=(hash[element]||0)+1;hash}
  96.   end  
  97. end  
  98. module DataManager
  99.   class << self
  100.     alias :iei020_load_database :load_database
  101.     def load_database(*args,&block)
  102.       iei020_load_database(*args,&block)
  103.       IEI::CraftSystem.mk_recipe_list!(
  104.         (
  105.         $data_items   + # // Support Items
  106.         $data_weapons + # // Support Weapons
  107.         $data_armors    # // Support Armors
  108.         ).compact
  109.       )
  110.     end  
  111.   end  
  112. end  
  113. class RPG::BaseItem
  114.   # // Core
  115.   def db_id()
  116.     # // Database ID - Used to sort items
  117.     unless(@db_id)
  118.       @db_id = case(self)
  119.       # // Since the database has a limit of 999 items, you can use 1000 intervals
  120.       when RPG::Item   ; 0    
  121.       when RPG::Skill  ; 1000
  122.       when RPG::Weapon ; 2000
  123.       when RPG::Armor  ; 3000
  124.       else             ; 9000 # // Massive Offset D:
  125.       end  
  126.       @db_id += self.id # // Add the item's id to the offset
  127.     end
  128.     return @db_id
  129.   end  
  130.   attr_writer :db_id
  131.   # // Craft
  132.   def craft_recipes()
  133.     unless(@craft_recipes)
  134.       @craft_recipes = []
  135.       reg = IEI::REGEXP::BaseItem
  136.       # // Forward Declaration
  137.       recipe = []
  138.       crft = nil
  139.       self.note.scan(reg::CRAFT_MATERIAL).each do |n|
  140.         # // I should have made an recipe object instead of nested arrays...
  141.         recipe = [] # // Make a new recipe
  142.         n[0].scan(reg::CRAFT_ID).each do |cid|
  143.           l, id = cid.join("").match(reg::CRAFT_ID)[1,2]
  144.           id = id.to_i
  145.           # // The regex patterns(nID) are converted to craft arrays [:symbol, id]
  146.           crft = case(l.upcase)
  147.           when "I" ; [:item  ,id] # // Item
  148.           when "W" ; [:weapon,id] # // Weapon
  149.           when "A" ; [:armor ,id] # // Armor
  150.           when "S" ; [:skill ,id] # // Skill # // Not supported but whatever.
  151.           else     ; raise "Unknown type #{l}" # // Now how the hell did you do that?
  152.           end  
  153.           recipe << crft # // Push this craft unto the list
  154.         end
  155.         @craft_recipes << recipe # // Add the new recipe to the list
  156.       end  
  157.     end  
  158.     # // Array[Recipe[Crafts[:symbol, id]..]..]
  159.     return @craft_recipes
  160.   end  
  161.   attr_writer :craft_recipes
  162. end  
  163. class Game_Party
  164.   def recipe2items(recipe)
  165.     recipe.collect{|c|IEI::CraftSystem.craft2item(c)}
  166.   end  
  167.   def items2recipe(items)
  168.     IEI::CraftSystem.items2recipe(items)
  169.   end  
  170.   def item_from_recipe(recipe)
  171.     IEI::CraftSystem.item_from_recipe(recipe)
  172.   end  
  173.   def has_these?(*items)
  174.     hsh = IEI.a2count_hash(items)
  175.     return hsh.keys.all?{|i|hsh[i]<=item_number(i)}
  176.   end
  177.   def crafted?(item)
  178.     return (@crafted ||= {})[IEI::CraftSystem.item2craft(item)] == true
  179.   end  
  180.   def correct_recipe?(target_item,recipe)
  181.     item_from_recipe(recipe) == target_item
  182.   end  
  183.   def craft(recipe,item=item_from_recipe(recipe))
  184.     return false unless(item)
  185.     return false unless(item_number(item)<max_item_number(item))
  186.     return false unless(has_these?(*recipe2items(recipe)))
  187.     craft!(recipe,item)
  188.   end  
  189.   # // Dont care if its impossible, just DO IT
  190.   def craft!(recipe,item=item_from_recipe(recipe))
  191.     @crafted[IEI::CraftSystem.item2craft(item)] = true
  192.     gain_item(item,1)
  193.     recipe2items(recipe).each{|m|lose_item(m,1)}
  194.     return true
  195.   end  
  196. end  
  197. class Window::CraftItemList < Window_ItemList
  198.   def col_max()
  199.     1
  200.   end
  201.   def item_max()
  202.     super + 1
  203.   end  
  204. end  
  205. class Window::CraftOutput < Window_Selectable
  206.   def initialize(x,y,w,h)
  207.     super(x,y,w,h)
  208.     select(0)
  209.   end  
  210.   def craft!()
  211.     return Sound.play_buzzer unless($game_party.craft(@recipe))
  212.     @input_window.craft!() if(@input_window)
  213.   end  
  214.   attr_reader :input_window
  215.   def input_window=(n)
  216.     @input_window = n
  217.     refresh()
  218.   end
  219.   attr_reader :item
  220.   def change_recipe(recipe)
  221.     @recipe = recipe.compact
  222.     @item   = IEI::CraftSystem.item_from_recipe(@recipe)
  223.     refresh()
  224.   end  
  225.   def refresh()
  226.     contents.clear()
  227.     return unless(@item)
  228.     if($game_party.crafted?(@item))
  229.       draw_item_name(@item,0,0)
  230.     else
  231.       rect = item_rect_for_text(0); rect.x += 24;rect.width-=24
  232.       draw_text(rect,"?"*10)
  233.     end  
  234.   end  
  235.   def item_max
  236.     1
  237.   end
  238.   def col_max
  239.     1
  240.   end  
  241.   def update_help
  242.     @help_window.set_item(item)
  243.   end
  244. end  
  245. class Window::CraftInput < Window_Selectable
  246.   attr_reader :items
  247.   def initialize(x,y,w,h)
  248.     @items = []
  249.     super(x,y,w,h)
  250.     refresh()
  251.     select(0)
  252.   end
  253.   def craft!()
  254.     adjust_items()
  255.   end  
  256.   attr_reader :output_window
  257.   def output_window=(n)
  258.     @output_window = n
  259.     refresh()
  260.   end
  261.   def item(index=self.index)
  262.     @items[index]
  263.   end  
  264.   def change_item(index,n)
  265.     @items[index] = n
  266.     redraw_item(index)
  267.     update_recipe()
  268.   end  
  269.   def change_current_item(n)
  270.     change_item(self.index,n)
  271.     call_update_help()
  272.   end
  273.   def clear_items()
  274.     clear_items!()
  275.     update_recipe()
  276.   end  
  277.   def clear_items!()
  278.     @items.clear()
  279.     refresh()
  280.     call_update_help()
  281.   end  
  282.   def draw_item(index)
  283.     rect = item_rect(index)
  284.     item = @items[index]
  285.     if(item)
  286.       draw_item_name(item,rect.x,rect.y)
  287.     else
  288.       rect = item_rect_for_text(index)
  289.       draw_text(rect.x+24,rect.y,rect.width-24,rect.height,"-"*10)
  290.     end  
  291.   end  
  292.   def adjust_items()
  293.     hsh = IEI.a2count_hash(@items)
  294.     @items.clear()
  295.     hsh.each_pair do |key,value|
  296.       @items += [key] * [[0,value].max,$game_party.item_number(key)].min
  297.     end  
  298.     update_recipe()
  299.   end  
  300.   def update_recipe()
  301.     if(@output_window)
  302.       @output_window.change_recipe(IEI::CraftSystem.items2recipe(@items))
  303.     end  
  304.   end  
  305.   def item_max()
  306.     5 # // (JUMP:MaterialCount)
  307.   end
  308.   def col_max()
  309.     1
  310.   end
  311.   def update_help()
  312.     @help_window.set_item(item)
  313.   end
  314. end
  315. class Scene_Craft < Scene_MenuBase
  316.   def start()
  317.     super()
  318.     create_all_windows()
  319.   end  
  320.   def create_all_windows()
  321.     create_help_window()
  322.     create_category_window()
  323.     create_item_window()
  324.     create_output_window()
  325.     create_input_window()
  326.   end  
  327.   def create_category_window
  328.     @category_window = Window_ItemCategory.new
  329.     @category_window.viewport = @viewport
  330.     @category_window.help_window = @help_window
  331.     @category_window.y = @help_window.height
  332.     @category_window.set_handler(:ok,     method(:on_category_ok))
  333.     #@category_window.set_handler(:cancel, method(:on_category_cancel))
  334.     #@category_window.deactivate()
  335.   end
  336.   def create_item_window()
  337.     wx = @category_window.width / 2
  338.     wy = @category_window.y+@category_window.height
  339.     ww = @category_window.width / 2
  340.     wh = Graphics.height - wy - 48
  341.     @item_window             = Window::CraftItemList.new(wx,wy,ww,wh)
  342.     @item_window.viewport    = @viewport
  343.     @item_window.help_window = @help_window
  344.     @item_window.set_handler(:ok      , method(:on_item_ok))
  345.     @item_window.set_handler(:cancel  , method(:on_item_cancel))
  346.     @item_window.set_handler(:pageup  , method(:change_to_input))
  347.     @item_window.set_handler(:pagedown, method(:change_to_input))
  348.     @category_window.item_window = @item_window
  349.   end  
  350.   def create_output_window()
  351.     wx = 0
  352.     wy = @item_window.y + @item_window.height
  353.     ww = @category_window.width
  354.     wh = 24+24
  355.     @coutput_window = Window::CraftOutput.new(wx,wy,ww,wh)
  356.     @coutput_window.viewport    = @viewport
  357.     @coutput_window.help_window = @help_window
  358.     @coutput_window.set_handler(:ok      , method(:on_coutput_ok))
  359.     @coutput_window.set_handler(:cancel  , method(:on_coutput_cancel))
  360.     @coutput_window.set_handler(:pageup  , method(:change_to_input))
  361.     @coutput_window.set_handler(:pagedown, method(:change_to_input))
  362.   end  
  363.   def create_input_window()
  364.     wx = 0
  365.     wy = @category_window.y+@category_window.height
  366.     ww = @category_window.width / 2
  367.     wh = @item_window.height
  368.     @cinput_window               = Window::CraftInput.new(wx,wy,ww,wh)
  369.     @cinput_window.viewport      = @viewport
  370.     @cinput_window.help_window   = @help_window
  371.     #@cinput_window.set_handler(:ok      , method(:on_cinput_ok))
  372.     @cinput_window.set_handler(:cancel  , method(:on_cinput_cancel))
  373.     @cinput_window.set_handler(:pageup  , method(:change_to_output))
  374.     @cinput_window.set_handler(:pagedown, method(:change_to_output))
  375.     @cinput_window.output_window = @coutput_window
  376.     @coutput_window.input_window = @cinput_window
  377.     @cinput_window.activate()
  378.   end
  379.   def change_to_item()
  380.     @category_window.activate()
  381.   end  
  382.   def change_to_output()
  383.     @coutput_window.activate()
  384.     @category_window.deactivate()
  385.   end
  386.   def change_to_input()
  387.     @cinput_window.activate()
  388.     @category_window.activate()
  389.   end
  390.   def on_category_ok()
  391.     @cinput_window.deactivate()
  392.     @item_window.activate()
  393.     @item_window.select_last()
  394.   end  
  395.   def on_category_cancel()
  396.     @cinput_window.activate()
  397.   end  
  398.   def on_item_ok()
  399.     @cinput_window.change_current_item(@item_window.item)
  400.     on_item_cancel()
  401.   end  
  402.   def on_item_cancel()
  403.     @cinput_window.activate()
  404.     @category_window.activate()
  405.   end
  406.   def on_coutput_ok()
  407.     @coutput_window.craft!()
  408.     @cinput_window.refresh()
  409.     @coutput_window.refresh()
  410.     @item_window.refresh()
  411.     @coutput_window.activate()
  412.   end  
  413.   def on_coutput_cancel()
  414.     change_to_input()
  415.     #@cinput_window.activate()
  416.   end
  417.   def on_cinput_ok()
  418.     @coutput_window.activate()
  419.   end  
  420.   def on_cinput_cancel()
  421.     return_scene()
  422.   end
  423. end  
  424. #=■==========================================================================■=#
  425. #                           // ● End of File ● //                              #
  426. #=■==========================================================================■=#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement