blucalm

Clean Custom DB 13 Dec 2012

Dec 13th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 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. #--------------------------------------------------------------------------
  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. unis =
  136. if item_class == RPG::Weapon
  137. item = $data_weapons[id = e.item_id]
  138. @weps[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
  139. (upgs && item.is_upg_dupe)
  140. elsif item_class == RPG::Armor
  141. item = $data_armors[id = e.item_id]
  142. @arms[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
  143. (upgs && item.is_upg_dupe)
  144. elsif item_class == RPG::Item
  145. item = $data_items[id = e.item_id]
  146. @items[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi)||
  147. (upgs && item.is_upg_dupe)
  148. end
  149. end # actor.get_equips_era.each
  150. end # $game_actors.data.each
  151. end # store_meta_equips
  152. #--------------------------------------------------------------------------
  153. # * New Item IDs
  154. # sets new ids for items that were kept when cleaning out the database
  155. # param: metadata is the inclusion information, hashes item.id => array
  156. # struct is the db array
  157. # cust is the associated custom db array
  158. # curr_len is the original length of the db array
  159. #--------------------------------------------------------------------------
  160. def self.new_item_vals(metadata, struct, cust, curr_len)
  161. party = $game_party
  162. metadata.keys.each do |id|
  163. md_item = metadata[id][0]
  164.  
  165. # not using lose item
  166. container = party.item_container(md_item.class)
  167. container.delete(md_item.id) unless metadata[id][2] == :EQUIPPED
  168.  
  169. md_item.set_id_era(curr_len)
  170. struct.push(md_item)
  171. cust.push(md_item)
  172.  
  173. # not using gain item
  174. equipped = metadata[id][2] == :EQUIPPED && metadata[id][1] == 0
  175. container[md_item.id] = metadata[id][1] unless equipped
  176. update_equip_ids(md_item.class, id, curr_len)
  177.  
  178. @id_mod_log.push("#{md_item.class} #{md_item.name} #{id} -> #{md_item.id}")
  179. curr_len+=1
  180. end
  181. end
  182.  
  183. #--------------------------------------------------------------------------
  184. # * Scan through all equipped items and update the ids of those items with
  185. # the corresponding new ids.
  186. #--------------------------------------------------------------------------
  187. def self.update_equip_ids(item_class, oid, new_id)
  188. $game_actors.data.each do |a|
  189. next if !a
  190. a.get_equips_era.each do |e|
  191.  
  192. next if !e
  193. e.item_id=new_id if e.item_id==oid && e.item_class_era==item_class
  194. end
  195. end
  196. end
  197. #--------------------------------------------------------------------------
  198. # * Chop off part of an array
  199. #--------------------------------------------------------------------------
  200. def self.chop_db_arr(keep_amt, struct)
  201. new_data = Array.new(keep_amt)
  202. 0.upto(keep_amt-1){ |i| new_data[i] = struct[i] }
  203. new_data
  204. end
  205. #--------------------------------------------------------------------------
  206. # * Store the custom items that need to be re placed in the custom db arrays
  207. # after cleaning.
  208. #--------------------------------------------------------------------------
  209. def self.keep_cust_itm(start, fin, struct_pass)
  210. party = $game_party
  211. inclusion_hash = {} # items that will be included in db after cleaning.
  212. start.upto(fin).each do |i|
  213. if (amt=party.item_number(struct_pass[i])) > 0
  214. inclusion_hash[struct_pass[i].id] = [struct_pass[i], amt]
  215. end
  216. end
  217. inclusion_hash
  218. end
  219. #--------------------------------------------------------------------------
  220. # * Initialization
  221. #--------------------------------------------------------------------------
  222. def self.init_vals
  223. @id_mod_log ||= []
  224. @organized_items = {}
  225. end
  226. end # CleanCDB
  227. end # Era
  228.  
  229. # End of File
Advertisement
Add Comment
Please, Sign In to add comment