Advertisement
Szyu

Recycling System

Aug 19th, 2014
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.38 KB | None | 0 0
  1. #==============================================================================
  2. # Recycling System
  3. # Version 1.0
  4. # By Szyu
  5. #
  6. # About:
  7. # Recycle your items to regain material items.
  8. #
  9. # Instructions:
  10. # - Place below "Materials" but above "Main Process".
  11. # - Access the recycling menu from game menu, or if disabled, by calling
  12. #   "SceneManager.call(Scene_Recycling)"
  13. #
  14. # How to Use:
  15. # - "<recycling> content </recycling>" marks the area for recycling
  16. # example:
  17. #   <recycling>
  18. #   i: 2, 50%, 4 => Recycling gives a 50% chance to obtain 1-4 items of id 2
  19. #   w: 13, 70%, 8 => Recycling gives a 70% chance to obtain 1-8 weapons of id 13
  20. #   a: 7, 10%, 1 => Recycling gives a 10% chance to obtain 1 armor of id 7
  21. #   </recycling>
  22. #
  23. # Requires:
  24. # - RPG Maker VX Ace
  25. #
  26. # Terms of Use:
  27. # - Free for commercal and non-commercial use. Please list me
  28. #   in the credits to support my work.
  29. #
  30. # Pastebin:
  31. # http://pastebin.com/0gRUSmcg
  32. #
  33. #==============================================================
  34. #   * Configuration
  35. #==============================================================
  36.  
  37. # If you want to use recycling from the menu, set this to true, else false
  38. MENU_RECYCLING_VOCAB = "Recycling"
  39. RECYCLING_IN_MENU = true
  40.  
  41. # These phrases are shown when you recycle an item, based on its success
  42. RECYCLING_RESULT_PHRASE = {
  43.   :success => "Success!",
  44.   :failure => "Recycling process failed!"
  45. }
  46.  
  47. # These sounds are played when you recycle an item, based on its success
  48. # (Taken from SE)
  49. RECYCLING_SOUNDS = {
  50.   :success => "Item1",
  51.   :failure => "Water1"
  52. }
  53.  
  54. #==============================================================
  55. #   * Scene_Recycling
  56. #==============================================================
  57. class Scene_Recycling < Scene_ItemBase
  58.   def start
  59.     super
  60.     create_help_window
  61.     create_category_window
  62.     create_recycling_window
  63.     create_item_window
  64.   end
  65.  
  66.   def create_category_window
  67.     @category_window = Window_RecyclingCategory.new
  68.     @category_window.viewport = @viewport
  69.     @category_window.help_window = @help_window
  70.     @category_window.y = @help_window.height
  71.     @category_window.set_handler(:ok,     method(:on_category_ok))
  72.     @category_window.set_handler(:cancel, method(:return_scene))
  73.   end
  74.  
  75.   def create_recycling_window
  76.     wx = 240
  77.     wy = @category_window.y + @category_window.height
  78.     ww = Graphics.width - wx
  79.     wh = Graphics.height - wy
  80.     @recycling_window = Window_RecyclingOutput.new(wx,wy,ww,wh)
  81.     @recycling_window.viewport = @viewport
  82.   end
  83.  
  84.   def create_item_window
  85.     wy = @category_window.y + @category_window.height
  86.     wh = Graphics.height - wy
  87.     @item_window = Window_RecyclingItemList.new(0, wy, 240, wh)
  88.     @item_window.viewport = @viewport
  89.     @item_window.help_window = @help_window
  90.     @item_window.set_handler(:ok,     method(:on_item_ok))
  91.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  92.     @item_window.recycling_window = @recycling_window
  93.     @category_window.item_window = @item_window
  94.   end
  95.  
  96.   def show_result_window(recycled)
  97.     wdth = Graphics.width / 2
  98.     @result_window = Window_RecyclingResult.new((Graphics.width - wdth) / 2, wdth, recycled)
  99.     @result_window.set_handler(:ok,     method(:on_result_seen))
  100.     @result_window.set_handler(:cancel, method(:on_result_seen))
  101.     @result_window.show.activate
  102.   end
  103.  
  104.   def on_category_ok
  105.     @item_window.activate
  106.     @item_window.select_last
  107.   end
  108.  
  109.   def on_item_ok
  110.     recycle_item
  111.   end
  112.  
  113.   def on_item_cancel
  114.     @item_window.unselect
  115.     @category_window.activate
  116.   end
  117.  
  118.   def on_result_seen
  119.     @result_window.hide.deactivate
  120.     @result_window = nil
  121.     @item_window.refresh
  122.     @item_window.activate
  123.   end
  124.  
  125.   def recycle_item
  126.     item = @item_window.item
  127.    
  128.     recycled = []
  129.     item.recycling.each do |rec|
  130.       chance = rec[2]
  131.       amount = rand(rec[3]) + 1
  132.       gamble = rand(101)
  133.       if gamble <= chance
  134.         case rec[0]
  135.         when "i"; ri = $data_items[rec[1]]
  136.         when "w"; ri = $data_weapons[rec[1]]
  137.         when "a"; ri = $data_armors[rec[1]]
  138.         end
  139.         $game_party.gain_item(ri, amount)
  140.         recycled.push([ri, amount])
  141.       end
  142.     end
  143.    
  144.     $game_party.gain_item(item, -1, false)
  145.    
  146.     if recycled.size > 0
  147.       rsf = RECYCLING_SOUNDS[:success]
  148.     else
  149.       rsf = RECYCLING_SOUNDS[:failure]
  150.     end
  151.     RPG::SE.new(rsf, 100, 50).play
  152.    
  153.     show_result_window(recycled)
  154.   end
  155. end
  156.  
  157. #==============================================================
  158. #   * Scene_Menu
  159. #==============================================================
  160. class Scene_Menu < Scene_MenuBase
  161.   alias add_recycling_menu_entry create_command_window
  162.  
  163.   def create_command_window
  164.     add_recycling_menu_entry
  165.     @command_window.set_handler(:recycling,      method(:open_recycling)) if RECYCLING_IN_MENU
  166.   end
  167.  
  168.   def open_recycling
  169.     SceneManager.call(Scene_Recycling)
  170.   end
  171. end
  172.  
  173. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174.  
  175. #==============================================================
  176. #   * Window_RecyclingCategory
  177. #==============================================================
  178. class Window_RecyclingCategory < Window_HorzCommand
  179.   attr_reader   :item_window
  180.  
  181.   def initialize
  182.     super(0, 0)
  183.   end
  184.  
  185.   def window_width
  186.     Graphics.width
  187.   end
  188.  
  189.   def col_max
  190.     return 3
  191.   end
  192.  
  193.   def update
  194.     super
  195.     @item_window.category = current_symbol if @item_window
  196.   end
  197.  
  198.   def make_command_list
  199.     add_command(Vocab::item,     :item)
  200.     add_command(Vocab::weapon,   :weapon)
  201.     add_command(Vocab::armor,    :armor)
  202.   end
  203.  
  204.   def item_window=(item_window)
  205.     @item_window = item_window
  206.     update
  207.   end
  208. end
  209.  
  210. #==============================================================
  211. #   * Window_RecyclingItemList
  212. #==============================================================
  213. class Window_RecyclingItemList < Window_Selectable
  214.   attr_reader :recycling_window
  215.  
  216.   def initialize(x, y, width, height)
  217.     super
  218.     @category = :none
  219.     @data = []
  220.   end
  221.  
  222.   def category=(category)
  223.     return if @category == category
  224.     @category = category
  225.     refresh
  226.     self.oy = 0
  227.   end
  228.  
  229.   def col_max
  230.     return 1
  231.   end
  232.  
  233.   def item_max
  234.     @data ? @data.size : 1
  235.   end
  236.  
  237.   def item
  238.     @data && index >= 0 ? @data[index] : nil
  239.   end
  240.  
  241.   def current_item_enabled?
  242.     enable?(@data[index])
  243.   end
  244.  
  245.   def include?(item)
  246.     case @category
  247.     when :item
  248.       item.is_a?(RPG::Item)
  249.     when :weapon
  250.       item.is_a?(RPG::Weapon)
  251.     when :armor
  252.       item.is_a?(RPG::Armor)
  253.     else
  254.       false
  255.     end
  256.   end
  257.  
  258.   def enable?(item)
  259.     return item != nil
  260.   end
  261.  
  262.   def make_item_list
  263.     @data = $game_party.all_items.select {|item| include?(item) && item.recycling != nil && item.recycling.size > 0 }
  264.     @data.push(nil) if include?(nil)
  265.   end
  266.  
  267.   def select_last
  268.     select(@data.index($game_party.last_item.object) || 0)
  269.   end
  270.  
  271.   def draw_item(index)
  272.     item = @data[index]
  273.     if item
  274.       rect = item_rect(index)
  275.       rect.width -= 4
  276.       draw_item_name(item, rect.x, rect.y, true, width-70)
  277.       draw_item_number(rect, item)
  278.     end
  279.   end
  280.  
  281.   def draw_item_number(rect, item)
  282.     draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)
  283.   end
  284.  
  285.   def update_help
  286.     @help_window.set_item(item)
  287.     @recycling_window.item = item if @recycling_window
  288.   end
  289.  
  290.   def refresh
  291.     make_item_list
  292.     create_contents
  293.     draw_all_items
  294.   end
  295.  
  296.   def recycling_window=(recycling_window)
  297.     @recycling_window = recycling_window
  298.     update
  299.   end
  300.  
  301.   def process_ok
  302.     if current_item_enabled?
  303.       Input.update
  304.       deactivate
  305.       call_ok_handler
  306.     else
  307.       Sound.play_buzzer
  308.     end
  309.   end
  310.  
  311.   alias rec_us_win unselect
  312.   def unselect
  313.     rec_us_win
  314.     @recycling_window.contents.clear
  315.   end
  316. end
  317.  
  318. #==============================================================
  319. #   * Window_RecyclingOutput
  320. #==============================================================
  321. class Window_RecyclingOutput < Window_Selectable
  322.   def initialize(x,y,w,h)
  323.     super(x,y,w,h)
  324.     @item = nil
  325.   end
  326.  
  327.   def item=(item)
  328.     @item = item
  329.     refresh
  330.   end
  331.  
  332.   def refresh
  333.     contents.clear
  334.     return if !@item
  335.     draw_recycling_output
  336.   end
  337.  
  338.   def draw_recycling_output
  339.     dx = 0; dy = 0
  340.     dw = (contents.width)
  341.    
  342.     @item.recycling.each do |rec_item|
  343.       draw_recycling_item(rec_item, dx, dy, dw)
  344.       dy += line_height
  345.     end
  346.   end
  347.  
  348.   def draw_recycling_item(rec_item, dx, dy, dw)
  349.     draw_background_box(dx, dy, dw)
  350.     change_color(system_color)
  351.    
  352.     case rec_item[0]
  353.     when "i"; draw_text(dx+4, dy, dw-8, line_height, $data_items[rec_item[1]].name)
  354.     when "w"; draw_text(dx+4, dy, dw-8, line_height, $data_weapons[rec_item[1]].name)
  355.     when "a"; draw_text(dx+4, dy, dw-8, line_height, $data_armors[rec_item[1]].name)
  356.     end
  357.    
  358.     change_color(normal_color)
  359.     draw_text(dx+4, dy, dw-8, line_height, rec_item[2].to_s + "%" , 2)
  360.   end
  361.      
  362.   def draw_background_box(dx, dy, dw)
  363.     colour = Color.new(0, 0, 0, translucent_alpha/2)
  364.     rect = Rect.new(dx+1, dy+1, dw-2, line_height-2)
  365.     contents.fill_rect(rect, colour)
  366.   end
  367.      
  368. end
  369.  
  370. #==============================================================
  371. #   * Window_RecyclingResult
  372. #==============================================================
  373. class Window_RecyclingResult < Window_Selectable
  374.   def initialize(x, w, recycled)
  375.     @items = recycled
  376.     h = line_height * (@items.size + 2)
  377.     super(x, (Graphics.height - h) / 2, w, h)
  378.     self.z = 999
  379.     refresh
  380.   end
  381.  
  382.   def refresh
  383.     contents.clear
  384.     return if !@items
  385.     draw_recycling_output
  386.   end
  387.  
  388.   def draw_recycling_output
  389.     if @items.size == 0
  390.       draw_text(4, 0, contents.width - 8, line_height, RECYCLING_RESULT_PHRASE[:failure])
  391.     else
  392.      
  393.       draw_text(4, 0, contents.width - 8, line_height, RECYCLING_RESULT_PHRASE[:success])
  394.       dx = 0
  395.       dy = line_height
  396.       dw = contents.width
  397.       @items.each do |rec_item|
  398.         draw_recycling_item(rec_item, dx, dy, dw)
  399.         dy += line_height
  400.       end
  401.     end
  402.   end
  403.  
  404.   def draw_recycling_item(rec_item, dx, dy, dw)
  405.     change_color(system_color)
  406.     draw_text(dx+4, dy, dw-8, line_height, rec_item[0].name)
  407.     change_color(normal_color)
  408.     draw_text(dx+4, dy, dw-8, line_height, "x" + rec_item[1].to_s , 2)
  409.   end
  410. end
  411.  
  412. #==============================================================
  413. #   * Window_MenuCommand
  414. #==============================================================
  415. class Window_MenuCommand < Window_Command
  416.   alias add_recycling_menu_entry add_main_commands
  417.  
  418.   def add_main_commands
  419.     add_recycling_menu_entry
  420.     add_command(MENU_RECYCLING_VOCAB, :recycling) if RECYCLING_IN_MENU
  421.   end
  422. end
  423.  
  424. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  425.  
  426. #==============================================================
  427. #   * Initialize BaseItems
  428. #==============================================================
  429. module DataManager
  430.   class << self
  431.     alias load_db_recycling_sz load_database
  432.   end
  433.  
  434.   def self.load_database
  435.     load_db_recycling_sz
  436.     load_recycling_items
  437.   end
  438.  
  439.   def self.load_recycling_items
  440.     groups = [$data_items, $data_weapons, $data_armors]
  441.     for group in groups
  442.       for obj in group
  443.         next if obj.nil?
  444.         obj.load_recycling_notetags_sz
  445.       end
  446.     end
  447.   end
  448. end
  449.  
  450. #==============================================================
  451. #   * Content of Recycling Items
  452. #==============================================================
  453. class RPG::BaseItem
  454.   attr_accessor :recycling
  455.  
  456.   def load_recycling_notetags_sz
  457.     @recycling = []
  458.     @scan_recycling = false
  459.    
  460.     self.note.split(/[\r\n]+/).each do |line|
  461.       case line.downcase
  462.       when /<(?:recycling?)>/i
  463.         @scan_recycling = true
  464.       when /<\/(?:recycling?)>/i
  465.         @scan_recycling = false
  466.       else
  467.         if @scan_recycling == true
  468.           scan_recycling_data(line)
  469.         end
  470.       end
  471.     end
  472.   end
  473.  
  474.   def scan_recycling_data(line)
  475.     return unless line =~ /(\w+):\s*(\d+)\s*,?\s*(\d+)?%?\s*,\s*(\d+)?/i ? true : false
  476.     type = $1.to_s
  477.     item_id = $2.to_i
  478.     chance = $3.to_i || 100
  479.     amount = $4.to_i || 1
  480.     @recycling.push([type, item_id, chance, amount])
  481.   end
  482.  
  483. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement