Advertisement
roninator2

Yanfly Item Limit Uses for instance items

Dec 8th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 14.19 KB | None | 0 0
  1. #==============================================================================
  2. #
  3. # ▼ Yanfly Engine Ace - Item Limited Uses v1.00
  4. # -- Last Updated: 2023.09.01
  5. # -- Level: Easy
  6. # -- Requires: n/a
  7. # -- Modded by Roninator2 - Support Instance Items
  8. #==============================================================================
  9.  
  10. $imported = {} if $imported.nil?
  11. $imported["YEA-ItemLimitUse"] = true
  12.  
  13. #==============================================================================
  14. # ▼ Updates
  15. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  16. # 2023.09.01 - Started Script and Finished.
  17. #
  18. #==============================================================================
  19. # ▼ Introduction
  20. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  21. # Sometimes good game balance depends on restriction mechanics. One of these
  22. # mechanics include the amount of times an item can be used (limited uses)
  23. # This is intented to be used with items that you only get one of.
  24. # Compatible with Instance Items (preferred)
  25. #==============================================================================
  26. # ▼ Instructions
  27. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  28. # To install this script, open up your script editor and copy/paste this script
  29. # to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.
  30. #
  31. # -----------------------------------------------------------------------------
  32. # Item Notetags - These notetags go in the items notebox in the database.
  33. # -----------------------------------------------------------------------------
  34. # <limit use: x>
  35. # This will allow the skill to only be usable x times throughout the course of
  36. # battle. Once the skill is used x times, it is disabled until the battle is
  37. # over. This effect only takes place during battle.
  38. #
  39. # <discard_zero>
  40. # Tells the system to discard teh item when zero uses remaining
  41. # Useful when needing to change the conditions
  42. #
  43. #==============================================================================
  44. # ▼ Script Calls
  45. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  46. # Used to modify the items limited uses values
  47. #
  48. # script: alter_item_limit_use(id, X)
  49. #   x equals the number of uses you are setting
  50. #   used to recharge an item or lower number of uses
  51. #   you can event this with a script condition to check if the
  52. #   items uses remaining is greater than X, then lower to Y.
  53. #
  54. # script: alter_item_limited(id, bool) # item.limited
  55. #   turns limited use on or off (true/false)
  56. #   useful if you need to change an item from limited use to permanently available
  57. #   true = limited use; false = permanent use
  58. #
  59. # script: alter_item_discard_zero(id, bool)
  60. #   turns the items discard setting true or false for limited uses
  61. #   similar to above but will not cause an item to be useable if 0 uses left
  62. #   true = remove item when 0 uses left; false = do not remove when 0 uses left
  63. #
  64. #==============================================================================
  65. # ▼ Compatibility
  66. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  67. # This script is made strictly for RPG Maker VX Ace. It is highly unlikely that
  68. # it will run with RPG Maker VX without adjusting.
  69. #
  70. #==============================================================================
  71.  
  72. module YEA
  73.   module ITEM_RESTRICT
  74.    
  75.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  76.     # - Limited Use Settings -
  77.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  78.     # Some skills have limited uses per battle. These limited uses are reset
  79.     # at the start and end of each battle and do not apply when used outside of
  80.     # battle. There are no effects that can affect limited uses.
  81.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  82.     LIMIT_COLOUR  = 8          # Colour used from "Window" skin.
  83.     LIMIT_SIZE    = 18         # Font size used
  84.     LIMIT_TEXT    = "%s uses"  # Text used
  85.     LIMIT_ICON    = 0          # Icon used. Set 0 to disable.
  86.    
  87.   end # SKILL_RESTRICT
  88. end # YEA
  89.  
  90. #==============================================================================
  91. # ▼ Editting anything past this point may potentially result in causing
  92. # computer damage, incontinence, explosion of user's head, coma, death, and/or
  93. # halitosis so edit at your own risk.
  94. #==============================================================================
  95.  
  96. module YEA
  97.   module REGEXP
  98.     module ITEM
  99.       LIMIT_USE = /<(?:LIMIT_USE|limit use):[ ](\d+)>/i
  100.       DISCARD_ZERO = /<(?:DISCARD ZERO|discard zero)>/i
  101.     end # ITEM
  102.   end # REGEXP
  103. end # YEA
  104.  
  105. #==============================================================================
  106. # ■ Icon
  107. #==============================================================================
  108.  
  109. module Icon
  110.  
  111.   #--------------------------------------------------------------------------
  112.   # self.itemlimit
  113.   #--------------------------------------------------------------------------
  114.   def self.itemlimit; return YEA::ITEM_RESTRICT::LIMIT_ICON; end
  115.    
  116. end # Icon
  117.  
  118. #==============================================================================
  119. # ■ DataManager
  120. #==============================================================================
  121.  
  122. module DataManager
  123.  
  124.   #--------------------------------------------------------------------------
  125.   # alias method: load_database
  126.   #--------------------------------------------------------------------------
  127.   class <<self; alias load_database_liu load_database; end
  128.   def self.load_database
  129.     load_database_liu
  130.     load_notetags_liu
  131.   end
  132.  
  133.   #--------------------------------------------------------------------------
  134.   # new method: load_notetags_srs
  135.   #--------------------------------------------------------------------------
  136.   def self.load_notetags_liu
  137.     for obj in $data_items
  138.       next if obj.nil?
  139.       obj.load_notetags_liu
  140.     end
  141.   end
  142.  
  143. end # DataManager
  144.  
  145. #==============================================================================
  146. # ■ RPG::Item
  147. #==============================================================================
  148.  
  149. class RPG::UsableItem
  150.  
  151.   #--------------------------------------------------------------------------
  152.   # public instance variables
  153.   #--------------------------------------------------------------------------
  154.   attr_accessor :limit_use
  155.   attr_accessor :limited
  156.   attr_accessor :discard_zero
  157.  
  158.   #--------------------------------------------------------------------------
  159.   # common cache: load_notetags_liu
  160.   #--------------------------------------------------------------------------
  161.   def load_notetags_liu
  162.     @limit_use = 0
  163.     @limited = false
  164.     @discard_zero = false
  165.     #---
  166.     self.note.split(/[\r\n]+/).each { |line|
  167.       case line
  168.       #---
  169.       when YEA::REGEXP::ITEM::LIMIT_USE
  170.         @limit_use = $1.to_i
  171.         @limited = true
  172.       when YEA::REGEXP::ITEM::DISCARD_ZERO
  173.         @discard_zero = true
  174.       end
  175.     } # self.note.split
  176.     #---
  177.   end
  178. end # RPG::Items
  179.  
  180. #==============================================================================
  181. # ** Game_Interpreter
  182. #==============================================================================
  183.  
  184. class Game_Interpreter
  185.   #--------------------------------------------------------------------------
  186.   # * alter_limit_use(id, X)
  187.   #--------------------------------------------------------------------------
  188.   def alter_item_limit_use(id, i)
  189.     $game_party.items.each do |item|
  190.       if $imported["TH_InstanceItems"]
  191.         if item.template_id == id
  192.           item.limit_use = i
  193.         end
  194.       else
  195.         if item.id == id
  196.           item.limit_use = i
  197.         end
  198.       end
  199.     end
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   # * alter_item_limited(id, bool)
  203.   #--------------------------------------------------------------------------
  204.   def alter_item_limited(id, boo)
  205.     $game_party.items.each do |item|
  206.       if $imported["TH_InstanceItems"]
  207.         if item.template_id == id
  208.           item.limited = boo
  209.         end
  210.       else
  211.         if item.id == id
  212.           item.limited = boo
  213.         end
  214.       end
  215.     end
  216.   end
  217.   #--------------------------------------------------------------------------
  218.   # * alter_discard_zero(id, bool)
  219.   #--------------------------------------------------------------------------
  220.   def alter_item_discard_zero(id, boo)
  221.     $game_party.items.each do |item|
  222.       if $imported["TH_InstanceItems"]
  223.         if item.template_id == id
  224.           item.discard_zero = boo
  225.           $game_party.gain_item($data_items[item.id], -1) if (boo == true) && (item.limit_use == 0)
  226.         end
  227.       else
  228.         if item.id == id
  229.           item.discard_zero = boo
  230.           $game_party.gain_item($data_items[id], -1) if (boo == true) && (item.limit_use == 0)
  231.         end
  232.       end
  233.     end
  234.   end
  235.  
  236. end
  237.  
  238. #==============================================================================
  239. # ■ Game_BattlerBase
  240. #==============================================================================
  241.  
  242. class Game_Battler
  243.  
  244.   #--------------------------------------------------------------------------
  245.   # * Consume Items
  246.   #--------------------------------------------------------------------------
  247.   def consume_item(item)
  248.     if item.limit_use >= 1
  249.       item.limit_use -= 1
  250.     end
  251.     $game_party.consume_item(item) if item.limit_use == 0
  252.   end
  253.    
  254. end # Game_BattlerBase
  255.  
  256. #==============================================================================
  257. # ■ Game_Party
  258. #==============================================================================
  259.  
  260. class Game_Party < Game_Unit
  261.   #--------------------------------------------------------------------------
  262.   # * Consume Items
  263.   #    If the specified object is a consumable item, the number in investory
  264.   #    will be reduced by 1.
  265.   #--------------------------------------------------------------------------
  266.   alias r2_party_consume_discard_zero consume_item
  267.   def consume_item(item)
  268.     r2_party_consume_discard_zero(item)
  269.     lose_item(item, 1) if !item.consumable && item.discard_zero
  270.   end
  271. end
  272.  
  273. #==============================================================================
  274. # ■ Window_Base
  275. #==============================================================================
  276.  
  277. class Window_Base < Window
  278.  
  279.   def limit_colour; text_color(YEA::ITEM_RESTRICT::LIMIT_COLOUR); end;
  280.  
  281. end # Window_Base
  282.  
  283. #==============================================================================
  284. # ■ Window_SkillList
  285. #==============================================================================
  286.  
  287. class Window_ItemList < Window_Selectable
  288.  
  289.   #--------------------------------------------------------------------------
  290.   # alias method: draw_item
  291.   #--------------------------------------------------------------------------
  292.   alias window_itemlist_draw_item_liu draw_item
  293.   def draw_item(index)
  294.     if item_limit_restrict?(index)
  295.       draw_item_restriction(index)
  296.     else
  297.       window_itemlist_draw_item_liu(index)
  298.     end
  299.   end
  300.  
  301.   #--------------------------------------------------------------------------
  302.   # new method: limit_restricted?
  303.   #--------------------------------------------------------------------------
  304.   def item_limit_restrict?(index)
  305.     item = @data[index]
  306.     return true if item.limit_use >= 1
  307.   end
  308.  
  309.   #--------------------------------------------------------------------------
  310.   # new method: limit_restricted?
  311.   #--------------------------------------------------------------------------
  312.   def item_limited?(index)
  313.     item = @data[index]
  314.     return item.limited
  315.   end
  316.  
  317.   #--------------------------------------------------------------------------
  318.   # new method: draw_item
  319.   #--------------------------------------------------------------------------
  320.   def draw_item_restriction(index)
  321.     item = @data[index]
  322.     rect = item_rect(index)
  323.     rect.width -= 4
  324.     draw_item_name(item, rect.x, rect.y, enable?(item))
  325.     draw_item_limited(rect, item)
  326.   end
  327.  
  328.   #--------------------------------------------------------------------------
  329.   # * Display in Enabled State?
  330.   #--------------------------------------------------------------------------
  331.   def enable?(item)
  332.     if item.limited
  333.       return false if item.limit_use == 0
  334.     end
  335.     $game_party.usable?(item)
  336.   end
  337.  
  338.   #--------------------------------------------------------------------------
  339.   # new method: draw_skill_limited
  340.   #--------------------------------------------------------------------------
  341.   def draw_item_limited(rect, item)
  342.     change_color(limit_colour, enable?(item))
  343.     icon = Icon.itemlimit
  344.     if icon > 0
  345.       draw_icon(icon, rect.x + rect.width-24, rect.y, enable?(item))
  346.       rect.width -= 24
  347.     end
  348.     contents.font.size = YEA::ITEM_RESTRICT::LIMIT_SIZE
  349.     text = sprintf(YEA::ITEM_RESTRICT::LIMIT_TEXT, item.limit_use)
  350.     draw_text(rect, text, 2)
  351.     reset_font_settings
  352.   end
  353.  
  354. end # Window_SkillList
  355.  
  356. #==============================================================================
  357. # ■ Scene_ItemBase
  358. #==============================================================================
  359.  
  360. class Scene_ItemBase < Scene_MenuBase
  361.   #--------------------------------------------------------------------------
  362.   # * Use Item
  363.   #--------------------------------------------------------------------------
  364.   def use_item
  365.     if item.limited
  366.       return if item.limit_use == 0
  367.     end
  368.     play_se_for_item
  369.     user.use_item(item)
  370.     use_item_to_actors
  371.     check_common_event
  372.     check_gameover
  373.     @actor_window.refresh
  374.   end
  375.   #--------------------------------------------------------------------------
  376.   # * Determine if Item is Usable
  377.   #--------------------------------------------------------------------------
  378.   def item_usable?
  379.     if item.limited
  380.       return false if item.limit_use == 0
  381.     end
  382.     user.usable?(item) && item_effects_valid?
  383.   end
  384. end
  385.  
  386. #==============================================================================
  387. #
  388. # ▼ End of File
  389. #
  390. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement