roninator2

Tsukihime Instance Items - Block items

Dec 4th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 30.64 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Instance Items
  4.  Author: Hime
  5.  Date: Jan 29, 2015
  6.  URL: http://himeworks.com/2014/01/07/instance-items/
  7.  Modded by Roninator2
  8. --------------------------------------------------------------------------------
  9.  ** Change log
  10.  April 20, 2023
  11.    - Added feature to block creation of instance items with a note tag on the item
  12.      add <instance_blocked> to the item and it will be treated like a normal item
  13.  Jan 29, 2015
  14.    - requests for weapons, armors, or items will return a clone of the original
  15.      arrays in case you try to operate on it
  16.  Dec 1, 2014
  17.    - moved instance database reloading into "load_game_without_rescue" instead
  18.      of "load_game"
  19.  Aug 30, 2014
  20.    - fixed issue where removing items, including equips, crashed
  21.  May 4, 2014
  22.    - rather than reloading template database completely, simply drop the
  23.      instance objects
  24.  Mar 21, 2014
  25.    - fixed issue where conditional branch for equip checking doesn't work
  26.  Jan 23, 2014
  27.    - added support for refreshing note, description, and icon index
  28.  Jan 16, 2014
  29.    - added support for refreshing price and features
  30.  Jan 14, 2014
  31.    - added support for "refreshing" names and params
  32.  Jan 12, 2014
  33.    - disabling instance mode properly works for equips
  34.    - fixed issue with equipping
  35.  Jan 9, 2014
  36.    - instance counts do not explicitly "gain/lose" a template item
  37.    - fixed issue where changing equips forcibly was not working correctly
  38.    - fixed issue with battle test when item instances were enabled
  39.  Jan 7, 2014
  40.    - added several setup methods to the Instance Manager
  41.  Dec 10, 2013
  42.    - compatibility with Core Equip Slots
  43.    - Initial release
  44. --------------------------------------------------------------------------------  
  45.  ** Terms of Use
  46.  * Free to use in non-commercial projects
  47.  * Contact me for commercial use
  48.  * No real support. The script is provided as-is
  49.  * Will do bug fixes, but no compatibility patches
  50.  * Features may be requested but no guarantees, especially if it is non-trivial
  51.  * Credits to Hime Works in your project
  52.  * Preserve this header
  53. --------------------------------------------------------------------------------
  54.  ** Description
  55.  
  56.  This script introduces the concept of "instance items". In order to understand
  57.  what an instance item is, you need to first understand what a "template item"
  58.  is.
  59.  
  60.  All of your database items, weapons, and armors are called "template items".
  61.  That is, they serve as templates for in-game items.
  62.  
  63.  An instance item is simply an instance of a template. For example, you design
  64.  weapon ID 1 in your database and call it a "Short Sword". This is a template
  65.  of a Short Sword; everytime you receive a Short Sword in-game, you receive
  66.  a unique "instance" of this template. Two Short Swords in your inventory are
  67.  treated as two individual entities that have nothing to do with each other
  68.  besides both being based on the Short Sword template.
  69.  
  70.  It is important to note that this script simply provides instance item
  71.  functionality. Additional add-ons are required to provide various features
  72.  such as equip levels, equip affixes, and so on.
  73.  
  74. --------------------------------------------------------------------------------
  75.  ** Installation
  76.  
  77.  In the script editor, place this script below Materials and above Main
  78.  
  79.  You should place this below any equip-related scripts, such as my
  80.  Core Equip Slots or Yanfly's Ace Equip Engine.
  81.  
  82. --------------------------------------------------------------------------------
  83.  ** Usage
  84.  
  85.  This script is plug and play.
  86.  
  87.  -- Instance Mode --
  88.  
  89.  You can determine what type of objects allow instances or not in the
  90.  configuration by changing its "instance mode". For example, you can disable
  91.  instances for items so that they stack as normal.
  92.  
  93.  These are the default values I have picked:
  94.  
  95.    Items   - false
  96.    Weapons - true
  97.    Armors  - true
  98.    
  99.  Currently, if instance mode is ON for that category of items, all items will
  100.  be treated as instances.
  101.    
  102.  -- Event Changes --
  103.  
  104.  The following event commands behave differently if instance mode is on.
  105.  When I say "item" I refer to weapons, armors, and items.
  106.  
  107.  - When you gain an item using events, new instances will be added to the
  108.  inventory.
  109.  
  110.  - When you lose an item using events, it follows "template discard" rules.
  111.  For example, if your event says to "lose 1 short sword", then the engine will
  112.  simply look for ANY instance item that is based on the short sword. The same
  113.  applies to equips if you include equips.
  114.  
  115.  - When you use the "change equipment" event command, the engine looks for the
  116.  first instance of the specified equip.
  117.  
  118. --------------------------------------------------------------------------------
  119.  ** Developers
  120.  
  121.  This script serves as a framework for all instance items. Currently, it only
  122.  supports item, weapon, and armor instances.
  123.  
  124.  The goal is to allow developers to write their own scripts that require
  125.  "unique" items very easily without having to worry about how to actually
  126.  implement it.
  127.  
  128.  This script is designed so that you only need to focus on two things
  129.  
  130.  1. The RPG module, which contains the template weapons, armors, and items.
  131.  2. the InstanceManager module, which handles everything related to instances.
  132.  
  133.  A simple script would first load note-tags from the RPG objects and store them
  134.  with the templates. For example, suppose we want to give all instance weapons
  135.  a random attack bonus. We start by defining the max possible bonus a weapon
  136.  could receive.
  137.  
  138.    class RPG::Weapon < RPG::EquipItem
  139.      def attack_bonus
  140.        50
  141.      end
  142.    end
  143.    
  144.  Now, we make it so that whenever an instance weapon is created, a random bonus
  145.  will be applied to its attack. The InstanceManager provides several "setup"
  146.  methods available for you, depending on what kind of object you're working
  147.  with:
  148.  
  149.    setup_equip_instance(obj)  - use this for any equips (weapons or armors)
  150.    setup_weapon_instance(obj) - use this only for weapons
  151.    setup_armor_instance(obj)  - use this only for armors
  152.    setup_item_instance(obj)   - use this only for items
  153.    
  154.  Since our example is applying a random flat bonus to an instance weapon, we
  155.  would alias the weapon setup method.
  156.  
  157.    module InstanceManager
  158.      class << self
  159.        alias :th_random_weapon_bonus_setup_weapon_instance :setup_weapon_instance
  160.      end
  161.      
  162.      def self.setup_weapon_instance(obj)
  163.        template_obj = get_template(obj)
  164.        th_random_weapon_bonus_setup_weapon_instance(obj)
  165.        obj.params[2] += rand(template_obj.attack_bonus)
  166.      end
  167.    end
  168.    
  169.  Note the use of the `get_template` method in the setup. `obj` is an instance
  170.  weapon that we are creating.Our data is stored with the template weapon, so we
  171.  need to get it first before we can use it. After you have your template, you
  172.  can easily get the data you need to apply to your instance object.
  173.  
  174.  And that's it! Your instance weapon now has a random attack bonus. You can
  175.  verify this by adding the same weapon to your inventory multiple times and
  176.  checking their parameters in the equip menu.
  177.  
  178.  This is a very simple example, but its goal is to demonstrate how to modify
  179.  your instance objects. The rest of the game will just see it as another item
  180.  or equip.
  181.  
  182.  -- Shared Data Compatibility --
  183.  
  184.  Because many scripts may modify item information, it is important to write
  185.  compatible code.
  186.  
  187.  All RPG items, weapons, and armors support a `refresh` method that will
  188.  re-compute a number of variables such as the name or parameters.
  189.  
  190.  For example, to modify the name of an object, you should alias the
  191.  `make_name` method, which takes a name and returns a new name.
  192.  
  193.  The order in which the modifications are applied is important, and therefore
  194.  you should make it clear what order users should place your script in.
  195.  
  196.  
  197.   -- Modifications --
  198.  
  199.   use note tag <instance_blocked> to prevent item from being created
  200.   as an instance item. Instead it will add the same item to your
  201.   inventory just like normal, giving you multiples of one item.
  202.  
  203. #===============================================================================
  204. =end
  205. $imported = {} if $imported.nil?
  206. $imported["TH_InstanceItems"] = true
  207. #===============================================================================
  208. # ** Configuration
  209. #===============================================================================
  210. module TH
  211.   module Instance_Items
  212.    
  213.     # Enables instance mode for each type of object. When instance mode is
  214.     # enabled, all objects of that type will be treated as instances
  215.     Enable_Items = true
  216.     Enable_Weapons = true
  217.     Enable_Armors = true
  218.   end
  219. end
  220. #===============================================================================
  221. # ** Rest of script
  222. #===============================================================================
  223.  
  224. #-------------------------------------------------------------------------------
  225. # This module manages all instance and template items.
  226. #-------------------------------------------------------------------------------
  227. module InstanceManager
  228.  
  229.   class << self
  230.     attr_accessor :weapons
  231.     attr_accessor :armors
  232.     attr_accessor :items
  233.     attr_reader :template_counts
  234.   end
  235.  
  236.   def self.setup
  237.     @template_counts = {}
  238.     @template_counts[:weapon] = $data_weapons.size
  239.     @template_counts[:armor] = $data_armors.size
  240.     @template_counts[:item] = $data_items.size
  241.   end
  242.  
  243.   #-----------------------------------------------------------------------------
  244.   # Stores the instance objects for game saving purpose
  245.   #-----------------------------------------------------------------------------
  246.   def self.create_game_objects
  247.     @weapons = {}
  248.     @armors = {}
  249.     @items = {}
  250.   end
  251.  
  252.   #-----------------------------------------------------------------------------
  253.   # Full copy of the template object so we don't have any reference issues
  254.   #-----------------------------------------------------------------------------
  255.   def self.make_full_copy(obj)
  256.     return Marshal.load(Marshal.dump(obj))
  257.   end
  258.  
  259.   def self.instance_enabled?(obj)
  260.     return TH::Instance_Items::Enable_Items if obj.is_a?(RPG::Item)
  261.     return TH::Instance_Items::Enable_Weapons if obj.is_a?(RPG::Weapon)
  262.     return TH::Instance_Items::Enable_Armors if obj.is_a?(RPG::Armor)
  263.     return false
  264.   end
  265.  
  266.   def self.is_template?(obj)
  267.     return obj.id >= @template_counts[:item] if obj.is_a?(RPG::Item)
  268.     return obj.id >= @template_counts[:weapon] if obj.is_a?(RPG::Weapon)
  269.     return obj.id >= @template_counts[:armor] if obj.is_a?(RPG::Armor)
  270.   end
  271.  
  272.   #-----------------------------------------------------------------------------
  273.   # create an instance from the template. Basically just a full copy.
  274.   #-----------------------------------------------------------------------------
  275.   def self.make_instance(obj)
  276.     new_obj = make_full_copy(obj)
  277.     new_obj.template_id = new_obj.id
  278.     return new_obj
  279.   end
  280.  
  281.   #-----------------------------------------------------------------------------
  282.   # Return the database table that the obj belongs in
  283.   #-----------------------------------------------------------------------------
  284.   def self.get_tables(obj)
  285.     return @items, $data_items if obj.is_a?(RPG::Item)
  286.     return @weapons, $data_weapons if obj.is_a?(RPG::Weapon)
  287.     return @armors, $data_armors if obj.is_a?(RPG::Armor)
  288.   end
  289.  
  290.   def self.get_template(obj)
  291.     return $data_items[obj.template_id] if obj.is_a?(RPG::Item)
  292.     return $data_weapons[obj.template_id] if obj.is_a?(RPG::Weapon)
  293.     return $data_armors[obj.template_id] if obj.is_a?(RPG::Armor)
  294.   end
  295.  
  296.   #-----------------------------------------------------------------------------
  297.   # Returns an instance of the object, assuming it is a valid object, it
  298.   # supports instances, and it's not a template
  299.   #-----------------------------------------------------------------------------
  300.   def self.get_instance(obj)
  301.     return obj if obj.nil? || !instance_enabled?(obj) || !obj.is_template?
  302.     new_obj = make_instance(obj)
  303.     container, table = get_tables(obj)
  304.     id = table.size
  305.    
  306.     new_obj.id = id
  307.     # Setup the instance object as required
  308.     setup_instance(new_obj)
  309.    
  310.     # Add to database and container
  311.     container[id] = new_obj
  312.     table[id] = new_obj
  313.     return new_obj
  314.   end
  315.  
  316.   #-----------------------------------------------------------------------------
  317.   # Set up our new instance object. This is meant to be aliased
  318.   #-----------------------------------------------------------------------------
  319.   def self.setup_instance(obj)
  320.     setup_equip_instance(obj) if obj.is_a?(RPG::EquipItem)
  321.     setup_item_instance(obj) if obj.is_a?(RPG::Item)
  322.   end
  323.  
  324.   #-----------------------------------------------------------------------------
  325.   # Apply any equip-specific logic. This is meant to be aliased.
  326.   # Note the same object is passed to the appropriate setup method depending
  327.   # on whether it's a weapon or armor, so be careful not to apply the same
  328.   # changes multiple times.
  329.   #-----------------------------------------------------------------------------
  330.   def self.setup_equip_instance(obj)
  331.     setup_weapon_instance(obj) if obj.is_a?(RPG::Weapon)
  332.     setup_armor_instance(obj) if obj.is_a?(RPG::Armor)
  333.   end
  334.  
  335.   #-----------------------------------------------------------------------------
  336.   # Apply any weapon-specific logic. This is meant to be aliased.
  337.   #-----------------------------------------------------------------------------
  338.   def self.setup_weapon_instance(obj)
  339.   end
  340.  
  341.   #-----------------------------------------------------------------------------
  342.   # Apply any armor-specific logic. This is meant to be aliased.
  343.   #-----------------------------------------------------------------------------
  344.   def self.setup_armor_instance(obj)
  345.   end
  346.  
  347.   #-----------------------------------------------------------------------------
  348.   # Apply any item-specific logic. This is meant to be aliased.
  349.   #-----------------------------------------------------------------------------
  350.   def self.setup_item_instance(obj)
  351.   end
  352. end
  353.  
  354. module RPG
  355.  
  356.   class BaseItem
  357.    
  358.     # List of all attributes that are shared instance variable
  359.     _instance_attr = [:name, :params, :price, :features, :note, :icon_index,
  360.                       :description]
  361.    
  362.     #---------------------------------------------------------------------------
  363.     # Define methods for all shared variables
  364.     #---------------------------------------------------------------------------
  365.     _instance_refresh = "def refresh"
  366.     _instance_attr.each do |ivar|
  367.       _instance_refresh << ";refresh_#{ivar}"
  368.      
  369.       eval(
  370.         "def refresh_#{ivar}
  371.          var = InstanceManager.get_template(self).#{ivar}
  372.          @#{ivar} = make_#{ivar}(InstanceManager.make_full_copy(var))
  373.        end
  374.        
  375.        def make_#{ivar}(#{ivar})
  376.          #{ivar}
  377.        end
  378.        "
  379.       )
  380.     end
  381.     _instance_refresh << ";end"
  382.     eval(_instance_refresh)
  383.  
  384.   end
  385.  
  386.   class Item < UsableItem
  387.     attr_accessor :template_id
  388.     attr_accessor :in_hand
  389.    
  390.     def is_template?
  391.       return self.template_id == self.id
  392.     end
  393.     Instance_Blocked      = /<instance_blocked>/i
  394.     def instance_blocked?
  395.       self.note.split(/[\r\n]+/).each do |line|
  396.         case line
  397.         when Instance_Blocked
  398.           return true
  399.         end
  400.       end
  401.       return false
  402.     end
  403.  
  404.     def template_id
  405.       @template_id = @id unless @template_id
  406.       return @template_id
  407.     end
  408.    
  409.     def in_hand
  410.       @in_hand = 0 unless @in_hand
  411.       return @in_hand
  412.     end
  413.   end
  414.  
  415.   class EquipItem < BaseItem    
  416.     attr_accessor :template_id
  417.     attr_accessor :in_hand
  418.    
  419.     Instance_Blocked      = /<instance_blocked>/i
  420.     def instance_blocked?
  421.       self.note.split(/[\r\n]+/).each do |line|
  422.         case line
  423.         when Instance_Blocked
  424.           return true
  425.         end
  426.       end
  427.       return false
  428.     end
  429.    
  430.     def is_template?
  431.       self.template_id == self.id
  432.     end
  433.    
  434.     def template_id
  435.       @template_id = @id unless @template_id
  436.       return @template_id
  437.     end
  438.    
  439.     def in_hand
  440.       @in_hand = 0 unless @in_hand
  441.       return @in_hand
  442.     end
  443.   end
  444. end
  445.  
  446. module DataManager
  447.  
  448.   class << self
  449.     alias :th_instance_items_load_game_without_rescue :load_game_without_rescue
  450.     alias :th_instance_items_create_game_objects :create_game_objects
  451.     alias :th_instance_items_make_save_contents :make_save_contents
  452.     alias :th_instance_items_extract_save_contents :extract_save_contents
  453.   end
  454.  
  455.   def self.create_game_objects
  456.     th_instance_items_create_game_objects
  457.     InstanceManager.create_game_objects
  458.     load_instance_database
  459.   end
  460.  
  461.   def self.make_save_contents
  462.     contents = th_instance_items_make_save_contents
  463.     contents[:instance_weapons] = InstanceManager.weapons
  464.     contents[:instance_armors] = InstanceManager.armors
  465.     contents[:instance_items] = InstanceManager.items
  466.     contents
  467.   end
  468.  
  469.   def self.extract_save_contents(contents)
  470.     th_instance_items_extract_save_contents(contents)
  471.     InstanceManager.weapons = contents[:instance_weapons] || []
  472.     InstanceManager.armors = contents[:instance_armors] || []
  473.     InstanceManager.items = contents[:instance_items] || []
  474.   end
  475.  
  476.   def self.load_game_without_rescue(index)
  477.     res = th_instance_items_load_game_without_rescue(index)
  478.     reload_instance_database
  479.     return res
  480.   end
  481.  
  482.   #-----------------------------------------------------------------------------
  483.   # Merges the instance items into the database
  484.   #-----------------------------------------------------------------------------
  485.   def self.load_instance_database
  486.     InstanceManager.setup
  487.     merge_array_data($data_weapons, InstanceManager.weapons)
  488.     merge_array_data($data_armors, InstanceManager.armors)
  489.     merge_array_data($data_items, InstanceManager.items)
  490.   end
  491.  
  492.   def self.reload_instance_database
  493.     $data_weapons = $data_weapons[0..InstanceManager.template_counts[:weapon]]
  494.     $data_armors = $data_armors[0..InstanceManager.template_counts[:armor]]
  495.     $data_items = $data_items[0..InstanceManager.template_counts[:item]]
  496.     load_instance_database
  497.   end
  498.  
  499.   def self.merge_array_data(arr, hash)
  500.     hash.each {|i, val|
  501.       arr[i] = val
  502.     }
  503.   end
  504. end
  505.  
  506. class Game_Interpreter
  507.  
  508.   alias :th_instance_items_command_111 :command_111
  509.   def command_111
  510.     result = false
  511.     case @params[0]
  512.     when 4
  513.       actor = $game_actors[@params[1]]
  514.       if actor
  515.         case @params[2]
  516.         when 0  # in party
  517.           result = ($game_party.members.include?(actor))
  518.         when 1  # name
  519.           result = (actor.name == @params[3])
  520.         when 2  # Class
  521.           result = (actor.class_id == @params[3])
  522.         when 3  # Skills
  523.           result = (actor.skill_learn?($data_skills[@params[3]]))
  524.         when 4  # Weapons
  525.           result = (actor.instance_weapons_include?(@params[3]))
  526.         when 5  # Armors
  527.           result = (actor.instance_armors_include?(@params[3]))
  528.         when 6  # States
  529.           result = (actor.state?(@params[3]))
  530.         end
  531.       end
  532.     end
  533.     @branch[@indent] = result
  534.  
  535.     # none of them passed, so let's check the other conditions
  536.     th_instance_items_command_111 if !result
  537.   end
  538. end
  539.  
  540. class Game_Actor < Game_Battler
  541.  
  542.   alias :th_instance_items_init_equips :init_equips
  543.   def init_equips(equips)
  544.     @equips = Array.new(equip_slots.size) { Game_BaseItem.new }
  545.     instance_equips = check_instance_equips(equips)
  546.     th_instance_items_init_equips(instance_equips)
  547.   end
  548.  
  549.   #-----------------------------------------------------------------------------
  550.   # Replace all initial equips with instances
  551.   #-----------------------------------------------------------------------------
  552.   def check_instance_equips(equips)
  553.     new_equips = []
  554.     equips.each_with_index do |item_id, i|
  555.       etype_id = index_to_etype_id(i)
  556.       slot_id = empty_slot(etype_id)
  557.       if etype_id == 0
  558.         equip = $data_weapons[item_id]
  559.       else
  560.         equip = $data_armors[item_id]
  561.       end
  562.       new_equips << InstanceManager.get_instance(equip)
  563.     end
  564.     return new_equips.collect {|obj| obj ? obj.id : 0}
  565.   end
  566.  
  567.   alias :th_instance_items_change_equip :change_equip
  568.   def change_equip(slot_id, item)
  569.     new_item = item
  570.     if item && InstanceManager.instance_enabled?(item) && $game_party.has_item?(item) && item.is_template?
  571.       new_item = $game_party.find_instance_item(item)
  572.     end
  573.     th_instance_items_change_equip(slot_id, new_item)
  574.   end
  575.  
  576.   alias :th_instance_items_trade_item_with_party :trade_item_with_party
  577.   def trade_item_with_party(new_item, old_item)    
  578.     if new_item && InstanceManager.instance_enabled?(new_item) && $game_party.has_item?(new_item) && new_item.is_template?
  579.       new_item = $game_party.find_instance_item(new_item)
  580.     end
  581.     th_instance_items_trade_item_with_party(new_item, old_item)
  582.   end
  583.  
  584.   #-----------------------------------------------------------------------------
  585.   # New.
  586.   #-----------------------------------------------------------------------------
  587.   def instance_weapons_include?(id)
  588.     weapons.any? {|obj| obj.template_id == id }
  589.   end
  590.  
  591.   #-----------------------------------------------------------------------------
  592.   # New.
  593.   #-----------------------------------------------------------------------------
  594.   def instance_armors_include?(id)
  595.     armors.any? {|obj| obj.template_id == id }
  596.   end
  597. end
  598.  
  599. class Game_Party < Game_Unit
  600.  
  601.   alias :th_instance_items_init_all_items :init_all_items
  602.   def init_all_items
  603.     th_instance_items_init_all_items
  604.     @item_list = []
  605.     @weapon_list = []
  606.     @armor_list = []
  607.   end
  608.  
  609.   #-----------------------------------------------------------------------------
  610.   # Overwrite. We already keep a list of weapons
  611.   #-----------------------------------------------------------------------------
  612.   alias :th_instance_items_weapons :weapons
  613.   def weapons
  614.     TH::Instance_Items::Enable_Weapons ? @weapon_list.clone : th_instance_items_weapons
  615.   end
  616.  
  617.   #-----------------------------------------------------------------------------
  618.   # Overwrite.
  619.   #-----------------------------------------------------------------------------
  620.   alias :th_instance_items_items :items
  621.   def items
  622.     TH::Instance_Items::Enable_Items ? @item_list.clone : th_instance_items_items
  623.   end
  624.  
  625.   #-----------------------------------------------------------------------------
  626.   # Overwrite.
  627.   #-----------------------------------------------------------------------------
  628.   alias :th_instance_items_armors :armors
  629.   def armors
  630.     TH::Instance_Items::Enable_Armors ? @armor_list.clone : th_instance_items_armors
  631.   end
  632.  
  633.   #-----------------------------------------------------------------------------
  634.   # Returns true if the item type supports instances
  635.   #-----------------------------------------------------------------------------
  636.   def instance_enabled?(item)
  637.     return InstanceManager.instance_enabled?(item)
  638.   end
  639.  
  640.   #-----------------------------------------------------------------------------
  641.   # Returns an instance for the given item. If it is already an instance, then
  642.   # we just return that. If it's a template, we create a new instance.
  643.   #-----------------------------------------------------------------------------
  644.   def get_instance(item)
  645.     return InstanceManager.get_instance(item)
  646.   end
  647.  
  648.   #-----------------------------------------------------------------------------
  649.   # Returns the template for the given item
  650.   #-----------------------------------------------------------------------------
  651.   def get_template(item)
  652.     return InstanceManager.get_template(item)
  653.   end
  654.  
  655.   #-----------------------------------------------------------------------------
  656.   # The gain item method performs various checks on the item that you want to
  657.   # add to the inventory. Namely, it checks whether it is a template item or
  658.   # an instance item, updates the item counts, and so on.
  659.   #-----------------------------------------------------------------------------
  660.   alias :th_instance_items_gain_item :gain_item
  661.   def gain_item(item, amount, include_equip = false)
  662.     # special check for normal items
  663.     if !instance_enabled?(item)
  664.       th_instance_items_gain_item(item, amount, include_equip)
  665.     else
  666.       if item
  667.         if amount > 0
  668.           amount.times do |i|
  669.             new_item = get_instance(item)
  670.             if item.instance_blocked?
  671.               add_instance_blocked_item(item)
  672.             else
  673.               add_instance_item(new_item)
  674.             end
  675.           end
  676.         else
  677.           amount.abs.times do |i|
  678.             item_template = get_template(item)
  679.             if item.is_template?
  680.               # remove using template rules. If an item was lost, then decrease
  681.               # template count by 1.
  682.               lose_template_item(item, include_equip)
  683.             else
  684.               # remove the instance item, and decrease template count by 1
  685.               if item.instance_blocked?
  686.                 lose_instance_blocked_item(item)
  687.               else
  688.                 lose_instance_item(item)
  689.               end
  690.             end
  691.           end
  692.         end
  693.       else
  694.         th_instance_items_gain_item(item, amount, include_equip)
  695.       end
  696.     end
  697.   end
  698.  
  699.   #-----------------------------------------------------------------------------
  700.   # New. Returns the appropriate container list
  701.   #-----------------------------------------------------------------------------
  702.   def item_container_list(item)
  703.     return @item_list if item.is_a?(RPG::Item)
  704.     return @weapon_list if item.is_a?(RPG::Weapon)
  705.     return @armor_list if item.is_a?(RPG::Armor)
  706.   end
  707.  
  708.   #-----------------------------------------------------------------------------
  709.   # New. Adds the instance item to the appropriate list
  710.   #-----------------------------------------------------------------------------
  711.   def add_instance_item(item)
  712.     container = item_container(item.class)
  713.     container[item.template_id] ||= 0
  714.     container[item.template_id] += 1
  715.     container[item.id] = 1
  716.     container_list = item_container_list(item)
  717.     container_list.push(item)
  718.   end
  719.  
  720.   #-----------------------------------------------------------------------------
  721.   # New. Adds the instance item to the appropriate list
  722.   #-----------------------------------------------------------------------------
  723.   def add_instance_blocked_item(item)
  724.     container = item_container(item.class)
  725.     container[item.template_id] ||= 0
  726.     container[item.template_id] += 1
  727.     container[item.in_hand] ||= 0
  728.     container[item.in_hand] += 1 if container[item.in_hand] == 0
  729.     container_list = item_container_list(item)
  730.     if container_list == []
  731.       container_list.push(item)
  732.     else
  733.       item_found = false
  734.       container_list.each do |itm|
  735.         if item.template_id == itm.template_id
  736.           itm.in_hand += 1 if itm.in_hand == 0
  737.           itm.in_hand += 1
  738.           item_found = true
  739.         end
  740.       end
  741.       container_list.push(item) if !item_found
  742.     end
  743.   end
  744.  
  745.   #-----------------------------------------------------------------------------
  746.   # New. Returns an instance item that matches the template. If it doesn't
  747.   # exist, returns nil
  748.   #-----------------------------------------------------------------------------
  749.   def find_instance_item(template_item)
  750.     container_list = item_container_list(template_item)
  751.     return container_list.find {|obj| obj.template_id == template_item.template_id }
  752.   end
  753.  
  754.   #-----------------------------------------------------------------------------
  755.   # New. Lose an instance item. Simply delete it from the appropriate container
  756.   # list
  757.   #-----------------------------------------------------------------------------
  758.   def lose_instance_item(item)
  759.     container = item_container(item.class)
  760.     container[item.template_id] ||= 0
  761.     container[item.template_id] -= 1
  762.     container[item.id] = 0
  763.     container_list = item_container_list(item)
  764.     container_list.delete(item)
  765.   end
  766.  
  767.   #-----------------------------------------------------------------------------
  768.   # New. Lose an instance item. Simply delete it from the appropriate container
  769.   # list
  770.   #-----------------------------------------------------------------------------
  771.   def lose_instance_blocked_item(item)
  772.     container = item_container(item.class)
  773.     container[item.template_id] ||= 0
  774.     container[item.template_id] -= 1
  775.     container[item.in_hand] -= 1
  776.     container_list = item_container_list(item)
  777.     container_list.each do |itm|
  778.       if item.template_id == itm.template_id
  779.         itm.in_hand -= 1
  780.         if itm.in_hand == 0
  781.           container_list.delete(item)
  782.         end
  783.       end
  784.     end
  785.   end
  786.  
  787.   #-----------------------------------------------------------------------------
  788.   # New. Lose a template item. This looks for a
  789.   #-----------------------------------------------------------------------------
  790.   def lose_template_item(item, include_equip)
  791.     container_list = item_container_list(item)
  792.     item_lost = container_list.find {|obj| obj.template_id == item.template_id }
  793.     if item_lost
  794.       container = item_container(item.class)
  795.       container[item.template_id] ||= 0
  796.       container[item.template_id] -= 1
  797.       container_list.delete(item_lost)
  798.     elsif include_equip
  799.       discard_members_template_equip(item, 1)
  800.     end
  801.     return item_lost
  802.   end
  803.  
  804.   #-----------------------------------------------------------------------------
  805.   # New. Same as discarding equips, except we follow template discard rules
  806.   #-----------------------------------------------------------------------------
  807.   def discard_members_template_equip(item, amount)
  808.     n = amount
  809.     members.each do |actor|
  810.       item = actor.equips.find {|obj| obj && obj.template_id == item.template_id }
  811.       while n > 0 && item
  812.         actor.discard_equip(item)
  813.         n -= 1
  814.       end
  815.     end
  816.   end
  817. end
  818.  
  819. class Window_ItemList < Window_Selectable
  820.  
  821.   alias :th_instance_items_draw_item_number :draw_item_number
  822.   def draw_item_number(rect, item)
  823.     th_instance_items_draw_item_number(rect, item) if item.is_template?    
  824.   end
  825. end
  826.  
  827. #===============================================================================
  828. # Compatibility with Core Equip Slots
  829. #===============================================================================
  830. if $imported["TH_CoreEquipSlots"]
  831.   class Game_Actor < Game_Battler
  832.    
  833.     def init_equips(equips)
  834.       @equips = Array.new(initial_slots.size) {|i| Game_EquipSlot.new(initial_slots[i]) }
  835.       instance_equips = check_instance_equips(equips)
  836.       th_instance_items_init_equips(instance_equips)
  837.     end
  838.   end
  839. end
Add Comment
Please, Sign In to add comment