blucalm

Clean Custom DB 13 Dec 2012 No2

Dec 13th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     #--------------------------------------------------------------------------
  71.     # * Clean up the custom db and normal db
  72.     #--------------------------------------------------------------------------
  73.     def self.clean
  74.       init_vals
  75.       store_all_inv_items
  76.       double_check_nils # Double check all nils have been handled
  77.     end
  78.     #--------------------------------------------------------------------------
  79.     # * Remove any nil value not at pos 0
  80.     #--------------------------------------------------------------------------
  81.     def self.double_check_nils
  82.       dw,da,di = $data_weapons, $data_armors, $data_items
  83.       dw.each_with_index{ |w,i| dw.delete_at(i) if w.nil? && i > 0}
  84.       da.each_with_index{ |a,i| dw.delete_at(i) if a.nil? && i > 0}
  85.       di.each_with_index{ |it,i| dw.delete_at(i) if it.nil? && i > 0}
  86.     end
  87.     #--------------------------------------------------------------------------
  88.     # * Main routine for swapping around id values
  89.     #--------------------------------------------------------------------------
  90.     def self.store_all_inv_items
  91.      
  92.       org_wep_size = Era.db_array_size(:data_weapons)
  93.       org_arm_size = Era.db_array_size(:data_armors)
  94.       org_item_size = Era.db_array_size(:data_items)
  95.      
  96.       wep_size, arm_size = $data_weapons.size, $data_armors.size
  97.       item_size = $data_items.size
  98.  
  99.       @weps = keep_cust_itm(org_wep_size, wep_size, $data_weapons)
  100.       @arms = keep_cust_itm(org_arm_size, arm_size, $data_armors)
  101.       @items = keep_cust_itm(org_item_size, item_size, $data_items)
  102.      
  103.       store_meta_equips # must store before clearing customs
  104.      
  105.       # now clean the custom db and the db and put the kept items back in one by
  106.       # one while updating the parties inventory hashes at the same time.
  107.       new_data_weps = Array.new(org_wep_size)
  108.       new_data_arms = Array.new(org_arm_size)
  109.       new_data_items = Array.new(org_item_size)
  110.      
  111.       $data_weapons = chop_db_arr(org_wep_size, $data_weapons)
  112.       $data_armors = chop_db_arr(org_arm_size, $data_armors)
  113.       $data_items = chop_db_arr(org_item_size, $data_items)
  114.      
  115.       # clean up the db
  116.       $custom_weapons = []; $custom_armors = []; $custom_items = [];
  117.       new_item_vals(@weps, $data_weapons, $custom_weapons, org_wep_size)
  118.       new_item_vals(@arms, $data_armors, $custom_armors, org_arm_size)
  119.       new_item_vals(@items, $data_items, $custom_items, org_item_size)
  120.     end # store_all_inv_items
  121.     #--------------------------------------------------------------------------
  122.     # * Store Meta data about equips
  123.     #--------------------------------------------------------------------------
  124.     def self.store_meta_equips
  125.       party = $game_party
  126.       # Store metadata data about equipped items.
  127.       unqs = $imported["Ra Custom DM add-on"]
  128.       upgs = $imported["Ra Upgradable Equipment"]
  129.      
  130.       $game_actors.data.each do |actor|
  131.         next if !actor
  132.         actor.get_equips_era.each do |e|
  133.           next if !e
  134.           item_class = e.item_class_era
  135.           if item_class == RPG::Weapon
  136.             item = $data_weapons[id = e.item_id]
  137.             @weps[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
  138.               (upgs && item.is_upg_dupe)
  139.           elsif item_class == RPG::Armor
  140.             item = $data_armors[id = e.item_id]
  141.             @arms[id] = [item, 0, :EQUIPPED]  if (unqs&&item.is_unique_rpgbi) ||
  142.               (upgs && item.is_upg_dupe)
  143.           elsif item_class == RPG::Item
  144.             item = $data_items[id = e.item_id]
  145.             @items[id] = [item, 0, :EQUIPPED]  if (unqs&&item.is_unique_rpgbi)||
  146.               (upgs && item.is_upg_dupe)
  147.           end
  148.         end # actor.get_equips_era.each
  149.       end # $game_actors.data.each
  150.     end # store_meta_equips
  151.     #--------------------------------------------------------------------------
  152.     # * New Item IDs
  153.     # sets new ids for items that were kept when cleaning out the database
  154.     # param: metadata is the inclusion information, hashes item.id => array
  155.     #        struct is the db array
  156.     #        cust is the associated custom db array
  157.     #        curr_len is the original length of the db array
  158.     #--------------------------------------------------------------------------
  159.     def self.new_item_vals(metadata, struct, cust, curr_len)
  160.       party = $game_party
  161.       metadata.keys.each do |id|
  162.         md_item = metadata[id][0]
  163.        
  164.         # not using lose item
  165.         container = party.item_container(md_item.class)
  166.         container.delete(md_item.id) unless metadata[id][2] == :EQUIPPED
  167.        
  168.         md_item.set_id_era(curr_len)
  169.         struct.push(md_item)
  170.         cust.push(md_item)
  171.        
  172.         # not using gain item
  173.         equipped = metadata[id][2] == :EQUIPPED && metadata[id][1] == 0
  174.         container[md_item.id] = metadata[id][1] unless equipped
  175.         update_equip_ids(md_item.class, id, curr_len)
  176.        
  177.         @id_mod_log.push("#{md_item.class} #{md_item.name} #{id} -> #{md_item.id}")
  178.         curr_len+=1
  179.       end
  180.     end
  181.    
  182.     #--------------------------------------------------------------------------
  183.     # * Scan through all equipped items and update the ids of those items with
  184.     #     the corresponding new ids.
  185.     #--------------------------------------------------------------------------
  186.     def self.update_equip_ids(item_class, oid, new_id)
  187.       $game_actors.data.each do |a|
  188.         next if !a
  189.         a.get_equips_era.each do |e|
  190.          
  191.           next if !e
  192.           e.item_id=new_id if e.item_id==oid && e.item_class_era==item_class
  193.         end
  194.       end
  195.     end
  196.     #--------------------------------------------------------------------------
  197.     # * Chop off part of an array
  198.     #--------------------------------------------------------------------------
  199.     def self.chop_db_arr(keep_amt, struct)
  200.       new_data = Array.new(keep_amt)
  201.       0.upto(keep_amt-1){ |i| new_data[i] = struct[i] }
  202.       new_data
  203.     end
  204.     #--------------------------------------------------------------------------
  205.     # * Store the custom items that need to be re placed in the custom db arrays
  206.     #   after cleaning.
  207.     #--------------------------------------------------------------------------
  208.     def self.keep_cust_itm(start, fin, struct_pass)
  209.       party = $game_party
  210.       inclusion_hash = {} # items that will be included in db after cleaning.
  211.       start.upto(fin).each do |i|
  212.         if (amt=party.item_number(struct_pass[i])) > 0
  213.           inclusion_hash[struct_pass[i].id] = [struct_pass[i], amt]
  214.         end
  215.       end
  216.       inclusion_hash
  217.     end
  218.     #--------------------------------------------------------------------------
  219.     # * Initialization
  220.     #--------------------------------------------------------------------------
  221.     def self.init_vals
  222.       @id_mod_log ||= []
  223.       @organized_items = {}
  224.     end
  225.   end # CleanCDB
  226. end # Era
  227.  
  228. # End of File
Advertisement
Add Comment
Please, Sign In to add comment