Enelvon

SES - Affix System

Jan 17th, 2013
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.83 KB | None | 0 0
  1. #═╦═════════════════════════════════════════════════════════════════════════════
  2. # ║ § Affix System (v1.0) by Enelvon                     [License: CC BY-SA 3.0]
  3. # ║                                                            <RMVX Ace>
  4. #═╬═════════════════════════════════════════════════════════════════════════════
  5. # ║ § Change Log
  6. #─╫─────────────────────────────────────────────────────────────────────────────
  7. # ║ v1.0 (January 17th, 2013) - Script released.
  8. # ║
  9. #═╬═════════════════════════════════════════════════════════════════════════════
  10. # ║ § Summary
  11. #─╫─────────────────────────────────────────────────────────────────────────────
  12. # ║ This script adds the ability for equipment to have affixes. Affixes can
  13. # ║ alter the stats of an item or provide new features.
  14. # ║
  15. #═╬═════════════════════════════════════════════════════════════════════════════
  16. # ║ § Required Scripts
  17. #─╫─────────────────────────────────────────────────────────────────────────────
  18. # ║ SES - Instance Items v1.0 or higher
  19. # ║ SES - Parse Features v1.0 or higher
  20. # ║
  21. #═╬═════════════════════════════════════════════════════════════════════════════
  22. # ║ § Known Incompatibilities
  23. #─╫─────────────────────────────────────────────────────────────────────────────
  24. # ║ None.
  25. # ║
  26. #═╬═════════════════════════════════════════════════════════════════════════════
  27. # ║ § Installation
  28. #─╫─────────────────────────────────────────────────────────────────────────────
  29. # ║ Place this below SES - Instance Items.
  30. # ║
  31. #═╬═════════════════════════════════════════════════════════════════════════════
  32. # ║ § Configuration
  33. #─╫─────────────────────────────────────────────────────────────────────────────
  34. # ║ There's no configuration within the script. All of it occurs in a text file
  35. # ║ that should be placed in the Data folder. Name the file Affixes.txt.
  36. # ║
  37. # ║ There are several important tags that you'll be using in this file - it's a
  38. # ║ lot like the one from External Text.
  39. # ║
  40. # ║ First of all there are the tags to determine whether what we're making is
  41. # ║ a prefix or a suffix, as well as its name. They're simple:
  42. # ║
  43. # ║     [!Affix!] !Name!
  44. # ║
  45. # ║ Replace !Affix! with either Prefix or Suffix and !Name! with what should be
  46. # ║ added to the weapon's name. It will be added before the base for prefixes
  47. # ║ and after it for suffixes (obviously).
  48. # ║
  49. # ║ Next there's the tag to determine whether it should be applied to weapons,
  50. # ║ armor, or both:
  51. # ║
  52. # ║     [Type] !Type!
  53. # ║
  54. # ║ Type can be Weapon, Armor, or All.
  55. # ║
  56. # ║ The last tag is to determine the rarity of the affix. It won't appear on
  57. # ║ equipment with a lower rarity level than the affix itself, and will appear
  58. # ║ more frequently on equipment of a higher rarity.
  59. # ║
  60. # ║     [Rarity] !Rarity!
  61. # ║
  62. # ║ Replace !Rarity! with an integer value representing the affix's rarity.
  63. # ║
  64. # ║ To give an affix effects, just add them on lines below the rarity tag.
  65. # ║ Effects are given with the tags described in the instructions for
  66. # ║ SES - Parse Features.
  67. # ║
  68. #═╬═════════════════════════════════════════════════════════════════════════════
  69. # ║ § Tags
  70. #─╫─────────────────────────────────────────────────────────────────────────────
  71. # ║ ● Weapon/Armor
  72. # ║   <Rarity: !Num!>
  73. # ║     Use this to indicate the rarity level of a piece of equipment. The
  74. # ║     rarity determines which affixes the item can have.
  75. # ║     ► Replacements:
  76. # ║       !Num! with the rarity level of the item.
  77. # ║
  78. # ║   <Disable: !Type!>
  79. # ║     Use this to prevent an item from having a certain type of affix.
  80. # ║     ► Replacements:
  81. # ║       !Type! with either Pre to disable prefixes or Suf to disable suffixes.
  82. # ║
  83. # ║   <Max !Type!: !Num!>
  84. # ║     Use this to determine the maximum number of prefixes or suffixes an item
  85. # ║     can have. The default is 1 of each.
  86. # ║     ► Replacements:
  87. # ║       !Type! with either Pre to choose the maximum number of prefixes or Suf
  88. # ║       to choose the number of suffixes.
  89. # ║       !Num! with the desired maximum.
  90. # ║
  91. # ║   <!Type! Chance: !Num!>
  92. # ║     Use this to determine the chance of an item having prefixes or suffixes.
  93. # ║     ► Replacements:
  94. # ║       !Type! with either Pre to choose the likelihood of prefixes or Suf
  95. # ║       to choose the likelihood of suffixes.
  96. # ║       !Num! with the desired likelihood (as a percentage).
  97. # ║
  98. #═╬═════════════════════════════════════════════════════════════════════════════
  99. # ║ § Aliased Methods
  100. #─╫─────────────────────────────────────────────────────────────────────────────
  101. # ║ ● module Data_Manager
  102. # ║     self.load_normal_database
  103. # ║     self.load_battle_test_database
  104. # ║
  105. #═╬═════════════════════════════════════════════════════════════════════════════
  106. # ║ ▼ module SES::Affixes
  107. #═╩═════════════════════════════════════════════════════════════════════════════
  108. module SES
  109.   module Affixes
  110.    
  111.     # Tags for Affixes.txt
  112.     Tags = {
  113.    
  114.       /^\[Prefix\]\s*(.+)/i =>
  115.           %Q{ affix = :pre; key = $1; specific = nil },
  116.          
  117.       /^\[Suffix\]\s*(.+)/i =>
  118.           %Q{ affix = :suf; key = $1; specific = nil },
  119.                                          
  120.       /^\[Type\]\s*(.+)/i =>
  121.           %Q{ type = $1.downcase.to_sym
  122.               affixes[type][key] = ["", 0, affix] },
  123.          
  124.       /^\[Rarity\]\s*(\d+)/i =>
  125.           %Q{ affixes[type][key][1] = $1.to_i },
  126.     }
  127.    
  128.     # Tag for rarity of equipment
  129.     RarityTag = /^<Rarity:\s*(\d+)>/i
  130.    
  131.     # Tag to disable prefixes or suffixes
  132.     DisableTag = /^<Disable (Pre|Suf)>/i
  133.    
  134.     # Tag to determine maximum number of prefixes/suffixes
  135.     MaxTag = /^<Max (Pre|Suf):\s*(\d+)>/i
  136.    
  137.     # Tag to determine chance of having a prefix or suffix
  138.     ChanceTag = /^<(Pre|Suf) Chance:\s*(\d+)>/i
  139.  
  140.     # Gets all available prefixes for an item
  141.     def self.collectPrefixes(etype, rarity)
  142.       type = etype == RPG::Weapon ? :weapon : :armor
  143.       $game_affixes[type].merge($game_affixes[:all]).select do |k,v|
  144.         v[2] == :pre && v[1] <= rarity
  145.       end
  146.     end
  147.    
  148.     # Gets all available suffixes for an item
  149.     def self.collectSuffixes(etype, rarity)
  150.       type = etype == RPG::Weapon ? :weapon : :armor
  151.       $game_affixes[type].merge($game_affixes[:all]).select do |k,v|
  152.         v[2] == :suf && v[1] <= rarity
  153.       end
  154.     end
  155.   end
  156. end
  157.  
  158.                               $imported ||= {}
  159.                   if $imported["SES - Instance Items"] < 1.0
  160. raise("You need SES - Instance Items v1.0 or higher to use SES - Affix System.")
  161.                                   end
  162.                  $imported["SES - Affix System"] = 1.0
  163. #═╦═════════════════════════════════════════════════════════════════════════════
  164. # ║ ▲ module SES::Affixes
  165. #─╫─────────────────────────────────────────────────────────────────────────────
  166. # ║ ▼ class RPG::EquipItem < RPG::BaseItem
  167. #═╩═════════════════════════════════════════════════════════════════════════════
  168. class RPG::EquipItem < RPG::BaseItem
  169.  
  170.   alias en_af_ei_sn scan_ses_notes
  171.   def scan_ses_notes(tags = {})
  172.     @rarity = 1
  173.     @canHave = {:pre => true, :suf => true}
  174.     @maxAffixes = {:pre => 1, :suf => 1}
  175.     @chanceOfAffix = {:pre => 25, :suf => 25}
  176.     @prefixes, @suffixes = [], []
  177.     tags[SES::Affixes::RarityTag] =
  178.       %Q{@rarity = $1.to_i}
  179.     tags[SES::Affixes::DisableTag] =
  180.       %Q{@canHave[$1.downcase.to_sym] = false}
  181.     tags[SES::Affixes::MaxTag] =
  182.       %Q{@maxAffixes[$1.downcase.to_sym] = $2.to_i}
  183.     tags[SES::Affixes::ChanceTag] =
  184.       %Q{@chanceOfAffix[$1.downcase.to_sym] = $2.to_i}
  185.     en_af_ei_sn(tags)
  186.   end
  187.  
  188.   [:rarity, :canHave, :maxAffixes, :chanceOfAffix, :prefixes,
  189.    :suffixes].each do |i|
  190.     define_method(i) do
  191.       eval("scan_ses_notes unless @#{i}
  192.      @#{i}")
  193.     end
  194.   end
  195.  
  196.   def affixAffix
  197.     pres, sufs = {}, {}
  198.     if canHave[:pre]
  199.       pres = SES::Affixes.collectPrefixes(self.class, @rarity)
  200.     end
  201.     if canHave[:suf]
  202.       sufs = SES::Affixes.collectSuffixes(self.class, @rarity)
  203.     end
  204.     return if pres.empty? && sufs.empty?
  205.     prelist,                           suflist =
  206.     pres.keys.shuffle.shuffle.shuffle, sufs.keys.shuffle.shuffle.shuffle
  207.     [:pre, :suf].each do |i|
  208.       next if eval("#{i}list").empty?
  209.       eval(%Q{if rand(100)+1 <= @chanceOfAffix[:#{i}]
  210.         while #{i}list.size > 0 && @#{i}fixes.size < maxAffixes[:#{i}]
  211.           cur#{i} = #{i}list.pop
  212.           next if cur#{i}.nil? || cur#{i}.empty?
  213.           if (@rarity - #{i}s[cur#{i}][1]) > rand(@rarity) &&
  214.               rand(100)+1 <= @chanceOfAffix[:#{i}]
  215.             @#{i}fixes << cur#{i}
  216.           end
  217.         end
  218.       end})
  219.     end
  220.     prefix, suffix = "", ""
  221.     @prefixes.each do |i| prefix << i << " " end
  222.     @suffixes.each do |i| suffix << " " << i end
  223.     @name = prefix << @name << suffix
  224.     affixEffects = ""
  225.     affixes = pres.merge(sufs)
  226.     (@prefixes + @suffixes).each do |i|
  227.       affixEffects << affixes[i][0] + "\n"
  228.     end
  229.     add_features(affixEffects, "Affixes")
  230.   end
  231. end
  232. #═╦═════════════════════════════════════════════════════════════════════════════
  233. # ║ ▲ class RPG::EquipItem < RPG::BaseItem
  234. #─╫─────────────────────────────────────────────────────────────────────────────
  235. # ║ ▼ module DataManager
  236. #═╩═════════════════════════════════════════════════════════════════════════════
  237. module DataManager
  238.  
  239.   class << self
  240.     alias en_af_dm_lnd load_normal_database
  241.     alias en_af_dm_lbd load_battle_test_database
  242.   end
  243.  
  244.   def self.create_affixes
  245.     affixes = { :all => {}, :weapon => {}, :armor => {}, }
  246.     [:weapon, :armor].each do |i| affixes[i].default = {} end
  247.     affix, type, key = :pre, :all, ""
  248.     File.open("Data/Affixes.txt", "r") do |file|
  249.       file.readlines.each_with_index do |v, i|
  250.         next if v =~ /(^\s*(#|\/\/).*|^\s*$)/
  251.         SES::Affixes::Tags.each_pair do |k,p|
  252.           (instance_eval(p); v = "") if v =~ k
  253.         end
  254.         affixes[type][key][0] << v unless v.empty?
  255.       end
  256.     end
  257.     File.open("Data/Affixes.rvdata2", "w") do |file|
  258.       Marshal.dump(affixes, file)
  259.     end
  260.   end
  261.  
  262.   def self.load_normal_database
  263.     en_af_dm_lnd
  264.     create_affixes if FileTest.exist?("Data/Affixes.txt")
  265.     $game_affixes = load_data("Data/Affixes.rvdata2")
  266.   end
  267.  
  268.   def self.load_battle_test_database
  269.     en_af_dm_lbd
  270.     create_affixes if FileTest.exist?("Data/Affixes.txt")
  271.     $game_affixes = load_data("Data/Affixes.rvdata2")
  272.   end
  273. end
  274. #═╦═════════════════════════════════════════════════════════════════════════════
  275. # ║ ▲ module DataManager
  276. #─╫─────────────────────────────────────────────────────────────────────────────
  277. # ║ ▼ class Game_Party < Game_Unit
  278. #═╩═════════════════════════════════════════════════════════════════════════════
  279. class Game_Party < Game_Unit
  280.  
  281.   alias en_af_gp_pni process_new_item
  282.   def process_new_item(item)
  283.     item.affixAffix
  284.     en_af_gp_pni(item)
  285.   end
  286. end
  287. #═╦═════════════════════════════════════════════════════════════════════════════
  288. # ║ ▲ class Game_Party < Game_Unit
  289. #═╩═════════════════════════════════════════════════════════════════════════════
Advertisement
Add Comment
Please, Sign In to add comment