Enelvon

SES - Instance Items

Dec 25th, 2012
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 23.27 KB | None | 0 0
  1. #═╦═════════════════════════════════════════════════════════════════════════════
  2. # ║ § Instance Items (v1.4) by Enelvon                   [License: CC BY-SA 3.0]
  3. # ║                                                            <RMVX Ace>
  4. #═╬═════════════════════════════════════════════════════════════════════════════
  5. # ║ § Change Log
  6. #─╫─────────────────────────────────────────────────────────────────────────────
  7. # ║ v1.0 (December 25th, 2012) - Released the script for the first time
  8. # ║ v1.1 (December 26th, 2012) - Fixed a single-word error that could cause
  9. # ║                              issues with non-unique armor
  10. # ║ v1.2 (February 16th, 2013) - Added the ability to check if an item is
  11. # ║                              instanced
  12. # ║ v1.3 (February 17th, 2013) - Fixed a bug with equipping items
  13. # ║ v1.4 (May 26th, 2013)      - Fixed a bug with the Remove X event commands
  14. # ║
  15. #═╬═════════════════════════════════════════════════════════════════════════════
  16. # ║ § Summary
  17. #─╫─────────────────────────────────────────────────────────────────────────────
  18. # ║ This script adds the ability to have 'instances' of items. Each instance may
  19. # ║ have slightly different stats/effects than the base item. This script
  20. # ║ contains only the ability to have stat variance on equip items - it is
  21. # ║ intended to be extended by others in order to add more features.
  22. #═╬═════════════════════════════════════════════════════════════════════════════
  23. # ║ § Required Scripts
  24. #─╫─────────────────────────────────────────────────────────────────────────────
  25. # ║ SES Core
  26. # ║
  27. #═╬═════════════════════════════════════════════════════════════════════════════
  28. # ║ § Known Incompatibilities
  29. #─╫─────────────────────────────────────────────────────────────────────────────
  30. # ║ None.
  31. # ║
  32. #═╬═════════════════════════════════════════════════════════════════════════════
  33. # ║ § Installation
  34. #─╫─────────────────────────────────────────────────────────────────────────────
  35. # ║ Place this below Materials and the SES Script Core, and above all other
  36. # ║ non-SES custom scripts (though make sure it's above any Instance Item
  37. # ║ extensions).
  38. # ║
  39. #═╬═════════════════════════════════════════════════════════════════════════════
  40. # ║ § Configuration
  41. #─╫─────────────────────────────────────────────────────────────────────────────
  42. # ║ There is some simple configuration required to use this script. Locate
  43. # ║ All_Unique in the SES::InstanceItems module (that should not be hard - it's
  44. # ║ at the top of the script). Change the value of each key to change whether or
  45. # ║ not items of that type will be treated as unique by default.
  46. # ║
  47. #═╬═════════════════════════════════════════════════════════════════════════════
  48. # ║ § Tags
  49. #─╫─────────────────────────────────────────────────────────────────────────────
  50. # ║ ● Weapon/Armor
  51. # ║   <Unique>
  52. # ║     Placing this in the Notes box of an item will toggle uniqueness. If its
  53. # ║     item type defaults to being unique, the item will be nonunique. If the
  54. # ║     item type defaults to nonunique, the item will be unique.
  55. # ║
  56. # ║   <Vary !Stat!: !Value!, !Chance!>
  57. # ║     Place this in a Notes box to set a variance of !Value! for !Stat!.
  58. # ║     !Chance is the likelihood (as a percentage) that variance will occur.
  59. # ║     This is a very basic implementation of variance - created items of the
  60. # ║     type will have a value of up to the given number added or subtracted
  61. # ║     from their base value for the stat. Don't expect it to work miracles,
  62. # ║     just for it to work.
  63. # ║     ► Replacements:
  64. # ║       !Stat! with hp, mp, atk, def, mat, mdf, agi, or luk.
  65. # ║       !Value! with an integer for the maximum variance of the stat.
  66. # ║       !Chance! with a percentage chance of variance occurring.
  67. # ║
  68. # ║ ● Item
  69. # ║   <Unique>
  70. # ║     Placing this in the Notes box of an item will toggle uniqueness. If its
  71. # ║     item type defaults to being unique, the item will be nonunique. If the
  72. # ║     item type defaults to nonunique, the item will be unique.
  73. # ║
  74. #═╬═════════════════════════════════════════════════════════════════════════════
  75. # ║ § Aliased Methods
  76. #─╫─────────────────────────────────────────────────────────────────────────────
  77. # ║ ● module DataManager
  78. # ║     self.create_game_objects
  79. # ║     self.make_save_contents
  80. # ║     self.extract_save_contents
  81. # ║
  82. # ║ ● class Game_BaseItem
  83. # ║     initialize
  84. # ║
  85. # ║ ● class Game_Party
  86. # ║     gain_item - as trade_item so instancing can be avoided
  87. # ║
  88. #═╬═════════════════════════════════════════════════════════════════════════════
  89. # ║ § Redefined Methods
  90. #─╫─────────────────────────────────────────────────────────────────────────────
  91. # ║ ● class Game_BaseItem
  92. # ║     object
  93. # ║     set_equip(is_weapon, item_id, unique)
  94. # ║
  95. # ║ ● class Game_Actor
  96. # ║     equips_include?(item)
  97. # ║     init_equips(equips)
  98. # ║     trade_item_with_party(new_item, old_item)
  99. # ║     change_equip_by_id(slot_id, item_id)
  100. # ║
  101. # ║ ● class Game_Party
  102. # ║     gain_item(item, amount, include_equip = false)
  103. # ║     lose_item(item, amount, include_equip = false)
  104. # ║
  105. # ║ ● class Window_ItemList
  106. # ║     draw_item(index)
  107. # ║
  108. #═╬═════════════════════════════════════════════════════════════════════════════
  109. # ║ § New Methods
  110. #─╫─────────────────────────────────────────────────────────────────────────────
  111. # ║ ● class Game_Party
  112. # ║     new_item(item, type)
  113. # ║
  114. #═╬═════════════════════════════════════════════════════════════════════════════
  115. # ║ ▼ module SES::InstanceItems
  116. #═╩═════════════════════════════════════════════════════════════════════════════
  117. module SES module InstanceItems
  118.   #═╦═══════════════════════════════════════════════════════════════════════════
  119.   # ║ α BEGIN CONFIGURATION
  120.   #═╩═══════════════════════════════════════════════════════════════════════════
  121.     # The hash defining the base uniqueness value of each type of item. Setting
  122.     # one of these to true makes items of the given type be true by default.
  123.     # Setting it to false will have them default to nonunique, as in base VXAce.
  124.     All_Unique = {
  125.  
  126.       RPG::Weapon => true,
  127.  
  128.       RPG::Armor => true,
  129.  
  130.       RPG::Item => false,
  131.  
  132.     }
  133.   #═╦═══════════════════════════════════════════════════════════════════════════
  134.   # ║ Ω END CONFIGURATION
  135.   #═╩═══════════════════════════════════════════════════════════════════════════
  136.  
  137.   # RegExp for toggling uniqueness of an item.
  138.   Unique = /^<Unique>/i
  139.   # RegExp for stat variance.
  140.   StatVariance = /^<Vary (\w+):\s*(\d+),\s*(\d+)%?>/i
  141.  
  142.   # List of stat indices. You can add custom stats to this to allow them to
  143.   # be varied, but the custom stats must be added to the game as part of the
  144.   # params array for this to have any effect.
  145.   StatIndex = ['hp', 'mp', 'atk', 'def', 'mat', 'mdf', 'agi', 'luk']
  146. end end
  147.  
  148.                 ($imported ||= {})["SES - Instance Items"] = 1.0
  149. #═╦═════════════════════════════════════════════════════════════════════════════
  150. # ║ ▲ module SES::InstanceItems
  151. #─╫─────────────────────────────────────────────────────────────────────────────
  152. # ║ ▼ class RPG::BaseItem
  153. #═╩═════════════════════════════════════════════════════════════════════════════
  154. class RPG::BaseItem
  155.   attr_accessor :old_id; attr_accessor :instanced
  156.   def unique; scan_ses_notes if !@unique; return @unique end
  157. end
  158. #═╦═════════════════════════════════════════════════════════════════════════════
  159. # ║ ▲ class RPG::BaseItem
  160. #─╫─────────────────────────────────────────────────────────────────────────────
  161. # ║ ▼ class RPG::EquipItem
  162. #═╩═════════════════════════════════════════════════════════════════════════════
  163. class RPG::EquipItem < RPG::BaseItem
  164.   # Scans the notes box of the item
  165.   alias en_ii_ei_ssn scan_ses_notes
  166.   def scan_ses_notes(tags = {})
  167.     @variances = [[0,100]] * 8
  168.     @unique = SES::InstanceItems::All_Unique[self.class]
  169.     tags[SES::InstanceItems::StatVariance] =
  170.         "stat = SES::InstanceItems::StatIndex.index($1.to_s.downcase)
  171.        @variances[stat] = [$2.to_i, $3.to_i]"
  172.     tags[SES::InstanceItems::Unique] = "@unique = !@unique"
  173.     en_ii_ei_ssn(tags)
  174.   end
  175.  
  176.   def variances; scan_ses_notes if !@variances; return @variances end
  177.    
  178.   # Allows basic variance
  179.   def vary
  180.     unless @varied
  181.       (0...@params.size).each do |i|
  182.         v = rand(variances[i][0]).to_i; v *= -1 if rand(8) <= 3
  183.         @params[i] += v if rand(100) < variances[i][1]
  184.       end
  185.       @varied = true
  186.     end
  187.   end
  188. end
  189. #═╦═════════════════════════════════════════════════════════════════════════════
  190. # ║ ▲ class RPG::EquipItem
  191. #─╫─────────────────────────────────────────────────────────────────────────────
  192. # ║ ▼ class RPG::Item
  193. #═╩═════════════════════════════════════════════════════════════════════════════
  194. class RPG::Item < RPG::UsableItem
  195.   # Scans the Notes box of the item
  196.   alias en_ii_i_ssn scan_ses_notes
  197.   def scan_ses_notes(tags = {})
  198.     @unique = SES::InstanceItems::All_Unique[self.class]
  199.     tags[SES::InstanceItems::Unique] = "@unique = !@unique"
  200.     en_ii_i_ssn(tags)
  201.   end
  202. end
  203. #═╦═════════════════════════════════════════════════════════════════════════════
  204. # ║ ▲ class RPG::Item
  205. #─╫─────────────────────────────────────────────────────────────────────────────
  206. # ║ ▼ module DataManager
  207. #═╩═════════════════════════════════════════════════════════════════════════════
  208. module DataManager
  209.   class << self
  210.     alias en_fs_dm_cgo create_game_objects
  211.     alias en_fs_dm_msc make_save_contents
  212.     alias en_fs_dm_esc extract_save_contents
  213.   end
  214.  
  215.   def self.create_game_objects
  216.     en_fs_dm_cgo
  217.     $game_armors, $game_weapons, $game_items =
  218.     $data_armors, $data_weapons, $data_items
  219.   end
  220.  
  221.   def self.make_save_contents
  222.     contents = en_fs_dm_msc
  223.     contents[:armors], contents[:weapons], contents[:items] =
  224.     $game_armors,      $game_weapons,      $game_items
  225.     contents
  226.   end
  227.  
  228.   def self.extract_save_contents(contents)
  229.     en_fs_dm_esc(contents)
  230.     $game_armors,      $game_weapons,      $game_items =
  231.     contents[:armors], contents[:weapons], contents[:items]
  232.   end
  233. end
  234. #═╦═════════════════════════════════════════════════════════════════════════════
  235. # ║ ▲ module DataManager
  236. #─╫─────────────────────────────────────────────────────────────────────────────
  237. # ║ ▼ class Game_BaseItem
  238. #═╩═════════════════════════════════════════════════════════════════════════════
  239. class Game_BaseItem
  240.  
  241.   alias en_ii_gbi_i initialize
  242.   def initialize
  243.     en_ii_gbi_i
  244.     @unique = false
  245.   end
  246.  
  247.   def object
  248.     id = @item_id
  249.     return $data_skills[@item_id] if is_skill?
  250.     if is_item? then return $game_items[id]
  251.     elsif is_weapon? then return $game_weapons[id]
  252.     elsif is_armor? then return $game_armors[id] end
  253.     return nil
  254.   end
  255.  
  256.   def set_equip(is_weapon, item_id, unique = false)
  257.     @class = is_weapon ? RPG::Weapon : RPG::Armor
  258.     @item_id = item_id
  259.     @unique = unique
  260.   end
  261. end
  262. #═╦═════════════════════════════════════════════════════════════════════════════
  263. # ║ ▲ class Game_BaseItem
  264. #─╫─────────────────────────────────────────────────────────────────────────────
  265. # ║ ▼ class Game_Actor
  266. #═╩═════════════════════════════════════════════════════════════════════════════
  267. class Game_Actor < Game_Battler
  268.  
  269.   def equips_include?(item)
  270.     equips.compact.each do |e|
  271.       return true if e.old_id == item.id && e.class == item.class
  272.     end
  273.     return false
  274.   end
  275.  
  276.   def init_equips(equips)
  277.     @equips = Array.new(equip_slots.size) { Game_BaseItem.new }
  278.     equips.each_with_index do |item_id, i|
  279.       etype_id = index_to_etype_id(i)
  280.       slot_id = empty_slot(etype_id)
  281.       if etype_id == 0 then i, t = $data_weapons[item_id], :weapon
  282.       else i, t = $data_armors[item_id], :armor end
  283.       if !i.nil? && i.unique then id, u = $game_party.new_item(i, t).id, true
  284.       elsif i.nil? then id, u = 0, false
  285.       else id, u = i.id, false end
  286.       @equips[slot_id].set_equip(etype_id == 0, id, u) if slot_id
  287.     end
  288.     refresh
  289.   end
  290.  
  291.   def trade_item_with_party(new_item, old_item)
  292.     return false if new_item && !$game_party.has_item?(new_item)
  293.     $game_party.trade_item(old_item, 1)
  294.     $game_party.lose_item(new_item, 1)
  295.     return true
  296.   end
  297.  
  298.   def change_equip_by_id(slot_id, item_id)
  299.     if equip_slots[slot_id] == 0
  300.       change_equip(slot_id, $game_weapons[item_id])
  301.     else change_equip(slot_id, $game_armors[item_id]) end
  302.   end
  303. end
  304. #═╦═════════════════════════════════════════════════════════════════════════════
  305. # ║ ▲ class Game_Actor
  306. #─╫─────────────────────────────────────────────────────────────────────────────
  307. # ║ ▼ class Game_Party
  308. #═╩═════════════════════════════════════════════════════════════════════════════
  309. class Game_Party < Game_Unit
  310.  
  311.   alias trade_item gain_item
  312.  
  313.   def new_item(item, type)
  314.     newi = Marshal.load(Marshal.dump(item))
  315.     newi.old_id = item.id
  316.     newi.instanced = true
  317.     newi.id = eval("$game_#{type}s.size")
  318.     newi = process_new_item(newi)
  319.     eval("$game_#{type}s.push(newi)
  320.   $game_#{type}s[newi.id].vary unless type == :item
  321.   return $game_#{type}s[newi.id]")
  322.   end
  323.  
  324.   def process_new_item(item)
  325.     return item
  326.   end
  327.  
  328.   [:items, :weapons, :armors].each do |i|
  329.     define_method(i) do
  330.       eval("@#{i}.keys.sort.collect { |id| $game_#{i}[id] }")
  331.     end
  332.   end
  333.  
  334.   def gain_item(*args)
  335.     oitem = args[0] if !args[0].nil?
  336.     if args[1] > 0
  337.       args[1].times do
  338.         item = oitem
  339.         if item && item.unique
  340.           args[0] = if item.is_a?(RPG::Weapon) then new_item(item, :weapon)
  341.           elsif item.is_a?(RPG::Armor) then args[0] = new_item(item, :armor)
  342.           elsif item.is_a?(RPG::Item) then args[0] = new_item(item, :item) end
  343.         end
  344.         trade_item(args[0], 1)
  345.       end
  346.     elsif args[1] < 0
  347.       item = args[0]
  348.       if item.unique
  349.         container = if item.is_a?(RPG::Weapon) then weapons
  350.                     elsif item.is_a?(RPG::Armor) then armors
  351.                     elsif item.is_a?(RPG::Item) then items end
  352.         args[1].abs.times do
  353.           trade_item(container.select {|i| i.old_id == item.id }[0], -1)
  354.         end
  355.       else
  356.         trade_item(item, args[1])
  357.       end
  358.     end
  359.   end
  360.  
  361.   def lose_item(*args)
  362.     args[1] *= -1
  363.     trade_item(*args)
  364.   end
  365. end
  366. #═╦═════════════════════════════════════════════════════════════════════════════
  367. # ║ ▲ class Game_Party
  368. #─╫─────────────────────────────────────────────────────────────────────────────
  369. # ║ ▼ class Window_ItemList
  370. #═╩═════════════════════════════════════════════════════════════════════════════
  371. class Window_ItemList < Window_Selectable
  372.  
  373.   def draw_item(index)
  374.     item = @data[index]
  375.     if item
  376.       rect = item_rect(index)
  377.       rect.width -= 4
  378.       draw_item_name(item, rect.x, rect.y, enable?(item))
  379.       draw_item_number(rect, item) unless item.unique
  380.     end
  381.   end
  382. end
  383. #═╦═════════════════════════════════════════════════════════════════════════════
  384. # ║ ▲ class Window_ItemList
  385. #═╩═════════════════════════════════════════════════════════════════════════════
Advertisement
Add Comment
Please, Sign In to add comment