blucalm

DB Cleaner 19 Jan 2013

Jan 19th, 2013
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.45 KB | None | 0 0
  1. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  2. # Clean Custom DB and Party Inventory Cleaner
  3. # Author: Eshra
  4. # First Version: 13 Dec. 2012
  5. # Compatibility: RPG Maker VX Ace
  6. # Dependencies:
  7. #     1. Tsuki_CustomDataManager
  8. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  9. # * This script is meant to be used with the upgradable equipment script or the
  10. #   Unique Items script
  11. #
  12. # Custom Database and Default Database are now managed properly, before
  13. # items were not being removed from it properly and so strange things such
  14. # as items disappearing, or new items could seemingly magically appear in
  15. # your inventory. It was a result of the ids in the parties inventory
  16. # no longer matching with the ids of the items (b/c database was changed).
  17. #
  18. # The Upg Equip Script did not work properly when saving and reloading a file in
  19. # which actors had upgraded/unique item equipped, this is now fixed. The item
  20. # would seem to disappear.
  21. #
  22. # This script provides a method call to be used internally to fix those issues.
  23. #------------------------------------------------------------------------------
  24. # Update Log
  25. # 13 Dec 2012 - Init Release
  26. #------------------------------------------------------------------------------
  27. ($imported||={})["Ra CleanCDB"] = true
  28. raise "CCDBPI Requires Tsuki Custom DM" if !$imported["Tsuki_CustomDataManager"]
  29. raise "CCDBPI Requires Snippet" if !$imported["Ra Store DB Arr Sizes"]
  30.  
  31.  
  32. #==============================================================================
  33. # ** Game_Party
  34. #==============================================================================
  35. class Game_Party < Game_Unit
  36.   def get_item_era; @items; end
  37.   def get_armors_era; @armors; end
  38.   def get_weapons_era; @weapons; end
  39. end
  40. #==============================================================================
  41. # * Game_Actor
  42. #==============================================================================
  43. class Game_Actor < Game_Battler
  44.  def get_equips_era; @equips; end
  45. end
  46. #==============================================================================
  47. # * Game_Actors
  48. #==============================================================================
  49. class Game_Actors
  50.   attr_reader :data
  51. end
  52. #==============================================================================
  53. # * RPG::BaseItem
  54. #==============================================================================
  55. class RPG::BaseItem
  56.   def set_id_era(nid); @id = nid; end
  57. end
  58. #==============================================================================
  59. # * Game_BaseItem
  60. #==============================================================================
  61. class Game_BaseItem
  62.   attr_accessor :item_id
  63.   def item_class_era; @class;  end
  64. end
  65. #==============================================================================
  66. # * Era
  67. #==============================================================================
  68. module Era
  69.   module CleanCDB
  70.     # If there is a compatibilty problem between this script and
  71.     # another script, try setting this value to false. This will cause
  72.     # the database to retain a large amount of references to objects that
  73.     # are no longer actually being used by the game, but, it prevents the
  74.     # database from being cleaned up which can cause major compatibility
  75.     # problems since it removes items from the database.
  76.     #
  77.     # I recommend keeping this option true unless you run into a compatibility
  78.     # issue. In that case setting it to false may solve the problem.
  79.     Clean = false
  80.     #--------------------------------------------------------------------------
  81.     # * Clean up the custom db and normal db
  82.     #--------------------------------------------------------------------------
  83.     def self.clean
  84.       init_vals
  85.       store_all_inv_items
  86.       double_check_nils # Double check all nils have been handled
  87.     end
  88.     #--------------------------------------------------------------------------
  89.     # * Remove any nil value not at pos 0
  90.     #--------------------------------------------------------------------------
  91.     def self.double_check_nils
  92.       dw,da,di = $data_weapons, $data_armors, $data_items
  93.       dw.each_with_index{ |w,i| dw.delete_at(i) if w.nil? && i > 0}
  94.       da.each_with_index{ |a,i| dw.delete_at(i) if a.nil? && i > 0}
  95.       di.each_with_index{ |it,i| dw.delete_at(i) if it.nil? && i > 0}
  96.     end
  97.     #--------------------------------------------------------------------------
  98.     # * Main routine for swapping around id values
  99.     #--------------------------------------------------------------------------
  100.     def self.store_all_inv_items
  101.      
  102.       org_wep_size = Era.db_array_size(:data_weapons)
  103.       org_arm_size = Era.db_array_size(:data_armors)
  104.       org_item_size = Era.db_array_size(:data_items)
  105.      
  106.       wep_size, arm_size = $data_weapons.size, $data_armors.size
  107.       item_size = $data_items.size
  108.  
  109.       @weps = keep_cust_itm(org_wep_size, wep_size, $data_weapons)
  110.       @arms = keep_cust_itm(org_arm_size, arm_size, $data_armors)
  111.       @items = keep_cust_itm(org_item_size, item_size, $data_items)
  112.      
  113.       store_meta_equips # must store before clearing customs
  114.      
  115.       # now clean the custom db and the db and put the kept items back in one by
  116.       # one while updating the parties inventory hashes at the same time.
  117.       new_data_weps = Array.new(org_wep_size)
  118.       new_data_arms = Array.new(org_arm_size)
  119.       new_data_items = Array.new(org_item_size)
  120.      
  121.       $data_weapons = chop_db_arr(org_wep_size, $data_weapons)
  122.       $data_armors = chop_db_arr(org_arm_size, $data_armors)
  123.       $data_items = chop_db_arr(org_item_size, $data_items)
  124.      
  125.       # clean up the db
  126.       $custom_weapons = []; $custom_armors = []; $custom_items = [];
  127.       new_item_vals(@weps, $data_weapons, $custom_weapons, org_wep_size)
  128.       new_item_vals(@arms, $data_armors, $custom_armors, org_arm_size)
  129.       new_item_vals(@items, $data_items, $custom_items, org_item_size)
  130.     end # store_all_inv_items
  131.     #--------------------------------------------------------------------------
  132.     # * Store Meta data about equips
  133.     #--------------------------------------------------------------------------
  134.     def self.store_meta_equips
  135.       party = $game_party
  136.       # Store metadata data about equipped items.
  137.       unqs = $imported["Ra Custom DM add-on"]
  138.       upgs = $imported["Ra Upgradable Equipment"]
  139.      
  140.       $game_actors.data.each do |actor|
  141.         next if !actor
  142.         actor.get_equips_era.each do |e|
  143.           next if !e
  144.           item_class = e.item_class_era
  145.           if item_class == RPG::Weapon
  146.             item = $data_weapons[id = e.item_id]
  147.             @weps[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
  148.               (upgs && item.is_upg_dupe)
  149.           elsif item_class == RPG::Armor
  150.             item = $data_armors[id = e.item_id]
  151.             @arms[id] = [item, 0, :EQUIPPED]  if (unqs&&item.is_unique_rpgbi) ||
  152.               (upgs && item.is_upg_dupe)
  153.           elsif item_class == RPG::Item
  154.             item = $data_items[id = e.item_id]
  155.             @items[id] = [item, 0, :EQUIPPED]  if (unqs&&item.is_unique_rpgbi)||
  156.               (upgs && item.is_upg_dupe)
  157.           end
  158.         end # actor.get_equips_era.each
  159.       end # $game_actors.data.each
  160.     end # store_meta_equips
  161.     #--------------------------------------------------------------------------
  162.     # * New Item IDs
  163.     # sets new ids for items that were kept when cleaning out the database
  164.     # param: metadata is the inclusion information, hashes item.id => array
  165.     #        struct is the db array
  166.     #        cust is the associated custom db array
  167.     #        curr_len is the original length of the db array
  168.     #--------------------------------------------------------------------------
  169.     def self.new_item_vals(metadata, struct, cust, curr_len)
  170.       party = $game_party
  171.       metadata.keys.each do |id|
  172.         md_item = metadata[id][0]
  173.        
  174.         # not using lose item
  175.         container = party.item_container(md_item.class)
  176.         container.delete(md_item.id) unless metadata[id][2] == :EQUIPPED
  177.        
  178.         md_item.set_id_era(curr_len)
  179.         struct.push(md_item)
  180.         cust.push(md_item)
  181.        
  182.         # not using gain item
  183.         equipped = metadata[id][2] == :EQUIPPED && metadata[id][1] == 0
  184.         container[md_item.id] = metadata[id][1] unless equipped
  185.         update_equip_ids(md_item.class, id, curr_len)
  186.        
  187.         @id_mod_log.push("#{md_item.class} #{md_item.name} #{id} -> #{md_item.id}")
  188.         curr_len+=1
  189.       end
  190.     end
  191.    
  192.     #--------------------------------------------------------------------------
  193.     # * Scan through all equipped items and update the ids of those items with
  194.     #     the corresponding new ids.
  195.     #--------------------------------------------------------------------------
  196.     def self.update_equip_ids(item_class, oid, new_id)
  197.       $game_actors.data.each do |a|
  198.         next if !a
  199.         a.get_equips_era.each do |e|
  200.          
  201.           next if !e
  202.           e.item_id=new_id if e.item_id==oid && e.item_class_era==item_class
  203.         end
  204.       end
  205.     end
  206.     #--------------------------------------------------------------------------
  207.     # * Chop off part of an array
  208.     #--------------------------------------------------------------------------
  209.     def self.chop_db_arr(keep_amt, struct)
  210.       new_data = Array.new(keep_amt)
  211.       0.upto(keep_amt-1){ |i| new_data[i] = struct[i] }
  212.       new_data
  213.     end
  214.     #--------------------------------------------------------------------------
  215.     # * Store the custom items that need to be re placed in the custom db arrays
  216.     #   after cleaning.
  217.     #--------------------------------------------------------------------------
  218.     def self.keep_cust_itm(start, fin, struct_pass)
  219.       party = $game_party
  220.       inclusion_hash = {} # items that will be included in db after cleaning.
  221.       start.upto(fin).each do |i|
  222.         if (amt=party.item_number(struct_pass[i])) > 0
  223.           inclusion_hash[struct_pass[i].id] = [struct_pass[i], amt]
  224.         end
  225.       end
  226.       inclusion_hash
  227.     end
  228.     #--------------------------------------------------------------------------
  229.     # * Initialization
  230.     #--------------------------------------------------------------------------
  231.     def self.init_vals
  232.       @id_mod_log ||= []
  233.       @organized_items = {}
  234.     end
  235.   end # CleanCDB
  236. end # Era
  237.  
  238. # End of File
Advertisement
Add Comment
Please, Sign In to add comment