adiktuzmiko

Item Refund V. 1.20

Apr 30th, 2019
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.00 KB | None | 0 0
  1. =begin
  2. ====================================================================
  3.   GENERAL INFORMATION
  4. --------------------------------------------------------------------
  5.   Title: Item Refund System
  6.   Author: Adiktuzmiko
  7.   Version: 1.20
  8.   Requirements: None
  9.  
  10.   This script allows you to use notetags to have a chance of refunding
  11.   the cost of an item when you use it.
  12.  
  13. ====================================================================
  14.  
  15. ====================================================================
  16.   FEATURES
  17. --------------------------------------------------------------------
  18.  
  19.   Refund item cost when used
  20.  
  21.   Customizable
  22.   - Set the percent chance of the refund to happen
  23.   - Set what percentage of the cost to refund
  24.   - Use either fixed values or a method
  25.   - Three different stacking modes for multiple notetags
  26.     -> Additive, Highest value, Individual refunds
  27.   - Set a maximum chance for the additive mode and maximum cost
  28.     percentage for all modes
  29.  
  30. ====================================================================
  31.  
  32. ====================================================================
  33.   INSTRUCTIONS
  34. --------------------------------------------------------------------
  35.  
  36.   After importing this script to your project, go to the config part
  37.   below (after TERMS AND CONDITIONS) and edit to suit your needs.
  38.  
  39.   Instructions on the notetag usage also in the config part.
  40.  
  41.   Notetags are usable for Actors, Class, Equipment, Items, States
  42.  
  43. ====================================================================
  44.  
  45. ====================================================================
  46.   TERMS AND CONDITIONS
  47. --------------------------------------------------------------------
  48.  
  49.   View it here: http://lescripts.wordpress.com/terms-and-conditions/
  50.  
  51. ====================================================================
  52.  
  53. =end
  54.  
  55. module ADIK
  56.   module REFUND_ITEM
  57.     #For simple, fixed value usage
  58.     #Refund Chance, Refund Percentage
  59.     TAG = /<refund:(.*):(.*)>/i
  60.    
  61.     #For more advanced usage
  62.     #Takes method_name
  63.     #Methods must be created inside Class Game_Battler
  64.     #Methods must take parameters (user,item)
  65.     #Methods must return array [Chance,Percentage]
  66.     #See sample method named sample in the class Game_Battler below
  67.     TAG_M = /<refund_method:(.*)>/i
  68.  
  69.     #Text shown on the battle log
  70.     #Usable text codes
  71.     #<item> => item name
  72.     #<value> => refund value
  73.     #<user> => name of user
  74.     TEXT = "<user> has been refunded <value>!"
  75.    
  76.     #:additive, :highest, :individual
  77.     #if :additive, adds all chances and refund percentages of user
  78.     #then evaluates it as a single chance and refund line
  79.     #if :highest, evaluates each refund notetag of user and returns the
  80.     #successful evaluation with the highest value
  81.     #if :individual, evaluates each refund notetag of user and returns the
  82.     #total value of all successful evaluation
  83.  
  84.     MODE = :individual
  85.  
  86.     #Max Chance (for additive mode)
  87.     def self.max_chance
  88.       return 100
  89.     end
  90.  
  91.     #Max Refund (for all modes)
  92.     def self.max_refund
  93.       return 100
  94.     end
  95.   end
  96. end
  97.  
  98. # DO NOT EDIT BELOW THIS LINE
  99. # DO NOT EDIT BELOW THIS LINE
  100. # DO NOT EDIT BELOW THIS LINE
  101.  
  102. class Game_Actor
  103.   def note
  104.     return $data_actors[@actor_id].note
  105.   end
  106. end
  107.  
  108. class Game_Enemy
  109.   def note
  110.     return $data_enemies[@enemy_id].note
  111.   end
  112. end
  113.  
  114. class Scene_Battle
  115.   def log_window
  116.     return @log_window
  117.   end
  118. end
  119.  
  120. class Game_Battler
  121.  
  122.   def sample(user,item)
  123.     return [50,30]
  124.   end
  125.  
  126.   def process_refund_text(user,item,value)
  127.     text = ADIK::REFUND_ITEM::TEXT
  128.     text = text.gsub(/<user>/) {user.name}
  129.     text = text.gsub(/<value>/) {value.to_s}
  130.     text = text.gsub(/<item>/) {item.name}
  131.     return text
  132.   end
  133.  
  134.   def refund(user,item,value)
  135.     value *= item.price
  136.     value = value.to_i
  137.     $game_party.gain_gold(value)
  138.     return unless SceneManager.scene_is?(Scene_Battle)
  139.     return if value <= 0
  140.     text = process_refund_text(user,item,value)
  141.     SceneManager.scene.log_window.add_text(text)
  142.   end
  143.  
  144.   def process_refund_add(user,item)
  145.     chance = 0
  146.     percentage = 0
  147.     if item.note =~ ADIK::REFUND_ITEM::TAG
  148.       chance += $1.to_i
  149.       percentage += $2.to_i  
  150.     end
  151.     if item.note =~ ADIK::REFUND_ITEM::TAG_M
  152.       result = self.method($1.to_sym).call(user,item)
  153.       chance += result[0]
  154.       percentage += result[1]
  155.     end
  156.     user.feature_objects.each do |obj|
  157.       next if obj.nil?
  158.       if obj.note =~ ADIK::REFUND_ITEM::TAG
  159.         chance += $1.to_i
  160.         percentage += $2.to_i  
  161.       end
  162.       if obj.note =~ ADIK::REFUND_ITEM::TAG_M
  163.         result = self.method($1.to_sym).call(user,item)
  164.         chance += result[0]
  165.         percentage += result[1]
  166.       end
  167.     end
  168.     chance = [chance,ADIK::REFUND_ITEM.max_chance].min
  169.     if rand(100) < chance
  170.       return percentage
  171.     end
  172.   end
  173.  
  174.   def process_refund_high(user,item)
  175.     percentage = []
  176.     if item.note =~ ADIK::REFUND_ITEM::TAG
  177.       percentage.push($2.to_i) if rand(100) <= $1.to_i
  178.     end
  179.     if item.note =~ ADIK::REFUND_ITEM::TAG_M
  180.       result = self.method($1.to_sym).call(user,item)
  181.       percentage.push(result[1].to_i) if rand(100) <= result[0].to_i
  182.     end
  183.     user.feature_objects.each do |obj|
  184.       next if obj.nil?
  185.       if obj.note =~ ADIK::REFUND_ITEM::TAG
  186.         percentage.push($2.to_i) if rand(100) <= $1.to_i  
  187.       end
  188.       if obj.note =~ ADIK::REFUND_ITEM::TAG_M
  189.         result = self.method($1.to_sym).call(user,item)
  190.         percentage.push(result[1].to_i) if rand(100) <= result[0].to_i
  191.       end
  192.     end
  193.     return percentage.max
  194.   end
  195.  
  196.   def process_refund_indi(user,item)
  197.     percentage = 0
  198.     if item.note =~ ADIK::REFUND_ITEM::TAG
  199.       percentage += $2.to_i if rand(100) <= $1.to_i
  200.     end
  201.     if item.note =~ ADIK::REFUND_ITEM::TAG_M
  202.       result = self.method($1.to_sym).call(user,item)
  203.       percentage += result[1].to_i if rand(100) <= result[0].to_i
  204.     end
  205.     user.feature_objects.each do |obj|
  206.       next if obj.nil?
  207.       if obj.note =~ ADIK::REFUND_ITEM::TAG
  208.         percentage += $2.to_i if rand(100) <= $1.to_i  
  209.       end
  210.       if obj.note =~ ADIK::REFUND_ITEM::TAG_M
  211.         result = self.method($1.to_sym).call(user,item)
  212.         percentage += result[1].to_i if rand(100) <= result[0].to_i
  213.       end
  214.     end
  215.     return percentage
  216.   end
  217.  
  218.   def process_refund(user,item)
  219.     refund_value = 0
  220.     case ADIK::REFUND_ITEM::MODE
  221.     when :additive
  222.       refund_value = process_refund_add(user,item)
  223.     when :highest
  224.       refund_value = process_refund_high(user,item)
  225.     when :individual
  226.       refund_value = process_refund_indi(user,item)
  227.     end
  228.     refund_value = 0 if refund_value.nil?
  229.     refund_value = [refund_value,ADIK::REFUND_ITEM.max_refund].min
  230.     refund(user,item,refund_value.to_f/100)
  231.   end
  232.  
  233.   alias use_item_refund use_item
  234.   def use_item(item)
  235.     use_item_refund(item)
  236.     process_refund(self,item) if (item.is_a?(RPG::Item) && item.consumable)
  237.   end
  238.  
  239. end
Advertisement