Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
- # Clean Custom DB and Party Inventory Cleaner
- # Author: Eshra
- # First Version: 13 Dec. 2012
- # Compatibility: RPG Maker VX Ace
- # Dependencies:
- # 1. Tsuki_CustomDataManager
- #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
- # * This script is meant to be used with the upgradable equipment script or the
- # Unique Items script
- #
- # Custom Database and Default Database are now managed properly, before
- # items were not being removed from it properly and so strange things such
- # as items disappearing, or new items could seemingly magically appear in
- # your inventory. It was a result of the ids in the parties inventory
- # no longer matching with the ids of the items (b/c database was changed).
- #
- # The Upg Equip Script did not work properly when saving and reloading a file in
- # which actors had upgraded/unique item equipped, this is now fixed. The item
- # would seem to disappear.
- #
- # This script provides a method call to be used internally to fix those issues.
- #------------------------------------------------------------------------------
- # Update Log
- # 13 Dec 2012 - Init Release
- #------------------------------------------------------------------------------
- ($imported||={})["Ra CleanCDB"] = true
- raise "CCDBPI Requires Tsuki Custom DM" if !$imported["Tsuki_CustomDataManager"]
- raise "CCDBPI Requires Snippet" if !$imported["Ra Store DB Arr Sizes"]
- #==============================================================================
- # ** Game_Party
- #==============================================================================
- class Game_Party < Game_Unit
- def get_item_era; @items; end
- def get_armors_era; @armors; end
- def get_weapons_era; @weapons; end
- end
- #==============================================================================
- # * Game_Actor
- #==============================================================================
- class Game_Actor < Game_Battler
- def get_equips_era; @equips; end
- end
- #==============================================================================
- # * Game_Actors
- #==============================================================================
- class Game_Actors
- attr_reader :data
- end
- #==============================================================================
- # * RPG::BaseItem
- #==============================================================================
- class RPG::BaseItem
- def set_id_era(nid); @id = nid; end
- end
- #==============================================================================
- # * Game_BaseItem
- #==============================================================================
- class Game_BaseItem
- attr_accessor :item_id
- def item_class_era; @class; end
- end
- #==============================================================================
- # * Era
- #==============================================================================
- module Era
- module CleanCDB
- # If there is a compatibilty problem between this script and
- # another script, try setting this value to false. This will cause
- # the database to retain a large amount of references to objects that
- # are no longer actually being used by the game, but, it prevents the
- # database from being cleaned up which can cause major compatibility
- # problems since it removes items from the database.
- #
- # I recommend keeping this option true unless you run into a compatibility
- # issue. In that case setting it to false may solve the problem.
- Clean = false
- #--------------------------------------------------------------------------
- # * Clean up the custom db and normal db
- #--------------------------------------------------------------------------
- def self.clean
- init_vals
- store_all_inv_items
- double_check_nils # Double check all nils have been handled
- end
- #--------------------------------------------------------------------------
- # * Remove any nil value not at pos 0
- #--------------------------------------------------------------------------
- def self.double_check_nils
- dw,da,di = $data_weapons, $data_armors, $data_items
- dw.each_with_index{ |w,i| dw.delete_at(i) if w.nil? && i > 0}
- da.each_with_index{ |a,i| dw.delete_at(i) if a.nil? && i > 0}
- di.each_with_index{ |it,i| dw.delete_at(i) if it.nil? && i > 0}
- end
- #--------------------------------------------------------------------------
- # * Main routine for swapping around id values
- #--------------------------------------------------------------------------
- def self.store_all_inv_items
- org_wep_size = Era.db_array_size(:data_weapons)
- org_arm_size = Era.db_array_size(:data_armors)
- org_item_size = Era.db_array_size(:data_items)
- wep_size, arm_size = $data_weapons.size, $data_armors.size
- item_size = $data_items.size
- @weps = keep_cust_itm(org_wep_size, wep_size, $data_weapons)
- @arms = keep_cust_itm(org_arm_size, arm_size, $data_armors)
- @items = keep_cust_itm(org_item_size, item_size, $data_items)
- store_meta_equips # must store before clearing customs
- # now clean the custom db and the db and put the kept items back in one by
- # one while updating the parties inventory hashes at the same time.
- new_data_weps = Array.new(org_wep_size)
- new_data_arms = Array.new(org_arm_size)
- new_data_items = Array.new(org_item_size)
- $data_weapons = chop_db_arr(org_wep_size, $data_weapons)
- $data_armors = chop_db_arr(org_arm_size, $data_armors)
- $data_items = chop_db_arr(org_item_size, $data_items)
- # clean up the db
- $custom_weapons = []; $custom_armors = []; $custom_items = [];
- new_item_vals(@weps, $data_weapons, $custom_weapons, org_wep_size)
- new_item_vals(@arms, $data_armors, $custom_armors, org_arm_size)
- new_item_vals(@items, $data_items, $custom_items, org_item_size)
- end # store_all_inv_items
- #--------------------------------------------------------------------------
- # * Store Meta data about equips
- #--------------------------------------------------------------------------
- def self.store_meta_equips
- party = $game_party
- # Store metadata data about equipped items.
- unqs = $imported["Ra Custom DM add-on"]
- upgs = $imported["Ra Upgradable Equipment"]
- $game_actors.data.each do |actor|
- next if !actor
- actor.get_equips_era.each do |e|
- next if !e
- item_class = e.item_class_era
- if item_class == RPG::Weapon
- item = $data_weapons[id = e.item_id]
- @weps[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
- (upgs && item.is_upg_dupe)
- elsif item_class == RPG::Armor
- item = $data_armors[id = e.item_id]
- @arms[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi) ||
- (upgs && item.is_upg_dupe)
- elsif item_class == RPG::Item
- item = $data_items[id = e.item_id]
- @items[id] = [item, 0, :EQUIPPED] if (unqs&&item.is_unique_rpgbi)||
- (upgs && item.is_upg_dupe)
- end
- end # actor.get_equips_era.each
- end # $game_actors.data.each
- end # store_meta_equips
- #--------------------------------------------------------------------------
- # * New Item IDs
- # sets new ids for items that were kept when cleaning out the database
- # param: metadata is the inclusion information, hashes item.id => array
- # struct is the db array
- # cust is the associated custom db array
- # curr_len is the original length of the db array
- #--------------------------------------------------------------------------
- def self.new_item_vals(metadata, struct, cust, curr_len)
- party = $game_party
- metadata.keys.each do |id|
- md_item = metadata[id][0]
- # not using lose item
- container = party.item_container(md_item.class)
- container.delete(md_item.id) unless metadata[id][2] == :EQUIPPED
- md_item.set_id_era(curr_len)
- struct.push(md_item)
- cust.push(md_item)
- # not using gain item
- equipped = metadata[id][2] == :EQUIPPED && metadata[id][1] == 0
- container[md_item.id] = metadata[id][1] unless equipped
- update_equip_ids(md_item.class, id, curr_len)
- @id_mod_log.push("#{md_item.class} #{md_item.name} #{id} -> #{md_item.id}")
- curr_len+=1
- end
- end
- #--------------------------------------------------------------------------
- # * Scan through all equipped items and update the ids of those items with
- # the corresponding new ids.
- #--------------------------------------------------------------------------
- def self.update_equip_ids(item_class, oid, new_id)
- $game_actors.data.each do |a|
- next if !a
- a.get_equips_era.each do |e|
- next if !e
- e.item_id=new_id if e.item_id==oid && e.item_class_era==item_class
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Chop off part of an array
- #--------------------------------------------------------------------------
- def self.chop_db_arr(keep_amt, struct)
- new_data = Array.new(keep_amt)
- 0.upto(keep_amt-1){ |i| new_data[i] = struct[i] }
- new_data
- end
- #--------------------------------------------------------------------------
- # * Store the custom items that need to be re placed in the custom db arrays
- # after cleaning.
- #--------------------------------------------------------------------------
- def self.keep_cust_itm(start, fin, struct_pass)
- party = $game_party
- inclusion_hash = {} # items that will be included in db after cleaning.
- start.upto(fin).each do |i|
- if (amt=party.item_number(struct_pass[i])) > 0
- inclusion_hash[struct_pass[i].id] = [struct_pass[i], amt]
- end
- end
- inclusion_hash
- end
- #--------------------------------------------------------------------------
- # * Initialization
- #--------------------------------------------------------------------------
- def self.init_vals
- @id_mod_log ||= []
- @organized_items = {}
- end
- end # CleanCDB
- end # Era
- # End of File
Advertisement
Add Comment
Please, Sign In to add comment