Szyu

Item's Usage Redirection

Aug 28th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.23 KB | None | 0 0
  1. #==============================================================================
  2. # Szyu's Item Usage Redirection
  3. # Version 1.0
  4. # By Szyu
  5. #
  6. # About:
  7. # If an item is used, and a condition of the target fits to the notes, the effect
  8. # is redirected to another items usage effect
  9. #
  10. # Instructions:
  11. # - Place below "▼ Materials" but above "▼ Main Process".
  12. #
  13. # How to Use:
  14. # <usage_redirection> and </usage_redirection> mark the condition area
  15. # in this block, you can add redirection with
  16. # "id - condition"
  17. #
  18. # Example:
  19. # <usage_redirection>
  20. # 20 - class_id == 2
  21. # </usage_redirection>
  22. # In this example, the used item's effect is redirected to item[20], if the
  23. # target's class_id is 2
  24. #
  25. # Possible conditions could be everything, that could be done with script call
  26. # or from Game_Battler as class (for example id for actor's id, or $game_variables[x])
  27. #
  28. #
  29. # Requires:
  30. # - RPG Maker VX Ace
  31. #
  32. # Terms of Use:
  33. # - Free for commercal and non-commercial use. Please list me
  34. #   in the credits to support my work.
  35. #
  36. # Pastebin:
  37. # http://adf.ly/4920670/item-usage-redirection
  38. #
  39. #
  40. #==============================================================
  41. #   * Game_Battler
  42. #==============================================================
  43. class Game_Battler < Game_BattlerBase
  44.   alias sz_iurd_use_item use_item
  45.   alias sz_iurd_item_apply item_apply
  46.  
  47.   def use_item(item)
  48.     old_item = item.clone
  49.     sz_iurd_use_item(item)
  50.     consume_item(old_item)   if item.is_a?(RPG::Item) && old_item != item
  51.   end
  52.  
  53.   def item_apply(user, item)
  54.     if item.is_a?(RPG::Item) && item.usage_redirection.size > 0
  55.       item.usage_redirection.each_pair do |cond, nid|
  56.         if evaluate_redir_condition(cond)
  57.           item = $data_items[nid]
  58.           break
  59.         end
  60.       end
  61.     end
  62.     sz_iurd_item_apply(user, item)
  63.   end
  64.  
  65.   def evaluate_redir_condition(cond)
  66.     return eval(cond)
  67.   end
  68. end
  69.  
  70. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71.  
  72. #==============================================================
  73. #   * Initialize BaseItems
  74. #==============================================================
  75. module DataManager
  76.   class << self
  77.     alias load_db_iurd_sz load_database
  78.   end
  79.  
  80.   def self.load_database
  81.     load_db_iurd_sz
  82.     load_iurd_items
  83.   end
  84.  
  85.   def self.load_iurd_items
  86.     groups = [$data_items]
  87.     for group in groups
  88.       for obj in group
  89.         next if obj.nil?
  90.         obj.load_iurd_notetags_sz
  91.       end
  92.     end
  93.   end
  94. end
  95.  
  96. #==============================================================
  97. #   * Content of Recycling Items
  98. #==============================================================
  99. class RPG::UsableItem < RPG::BaseItem
  100.   attr_reader :usage_redirection
  101.  
  102.   def load_iurd_notetags_sz
  103.     @usage_redirection = {}
  104.     @redir = false
  105.     self.note.split(/[\r\n]+/).each do |line|
  106.       case line
  107.       when /<usage_redirection>/i
  108.         @redir = true
  109.       when /<\/usage_redirection>/i
  110.         @redir = false
  111.       else
  112.         scan_sz_redir_notetag_line(line) if @redir
  113.       end
  114.     end
  115.   end  
  116.  
  117.   def scan_sz_redir_notetag_line(line)
  118.     return unless line =~ /(\d+)\s*-(.+)/i ? true : false
  119.     usage_redirection[$2.to_s] = $1.to_i
  120.   end
  121. end
Add Comment
Please, Sign In to add comment