neonblack

Skill Costs V1.1b

Jul 19th, 2012
1,101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###--------------------------------------------------------------------------###
  2. #  Skill Costs script                                                          #
  3. #  Version 1.1b                                                                #
  4. #                                                                              #
  5. #      Credits:                                                                #
  6. #  Original code by: Neonblack                                                 #
  7. #  Modified by:                                                                #
  8. #                                                                              #
  9. #  This work is licensed under the Creative Commons Attribution-NonCommercial  #
  10. #  3.0 Unported License. To view a copy of this license, visit                 #
  11. #  http://creativecommons.org/licenses/by-nc/3.0/.                             #
  12. #  Permissions beyond the scope of this license are available at               #
  13. #  http://cphouseset.wordpress.com/liscense-and-terms-of-use/.                 #
  14. #                                                                              #
  15. #      Contact:                                                                #
  16. #  NeonBlack - neonblack23@live.com (e-mail) or "neonblack23" on skype         #
  17. ###--------------------------------------------------------------------------###
  18.  
  19. ###--------------------------------------------------------------------------###
  20. #      Revision information:                                                   #
  21. #  V1.1 - 7.19.2012                                                            #
  22. #   Added more tags                                                            #
  23. #  V1.0 - 7.15.2012                                                            #
  24. #   Wrote and debugged main script                                             #
  25. ###--------------------------------------------------------------------------###
  26.  
  27. ###--------------------------------------------------------------------------###
  28. #      Compatibility:                                                          #
  29. #  Alias       - Scene_Battle: use_item                                        #
  30. #                DataManager: load_database                                    #
  31. #  Overwrites  - Game_BattlerBase: skill_wtype_ok?, skill_mp_cost,             #
  32. #                                  skill_tp_cost, skill_cost_payable?,         #
  33. #                                  pay_skill_cost                              #
  34. #                Window_SkillList: draw_skill_cost                             #
  35. #  New Objects - Game_BattlerBase: skill_hp_cost, skill_gold_cost,             #
  36. #                                  item_cost_met, item_cost_pay,               #
  37. #                                  state_cost_met, state_cost_pay,             #
  38. #                                  switch_cost_met, variable_cost_met,         #
  39. #                                  actor_cost_met, equip_cost_met              #
  40. #                Scene_Battle: final_cost                                      #
  41. #                RPG::UsableItem: set_new_costs                                #
  42. #                DataManager: create_new_skill_costs                           #
  43. ###--------------------------------------------------------------------------###
  44.  
  45. ###--------------------------------------------------------------------------###
  46. #      Instructions:                                                           #
  47. #  Place this script in the "Materials" section of the scripts above main.     #
  48. #  This script allows you to modify skill costs using not tags added to the    #
  49. #  skills.  You can also define percentage based skill costs.  You may define  #
  50. #  skill costs as seen below.                                                  #
  51. #                                                                              #
  52. #      cost[5 mp] - This is a basic skill cost.  The skill will cost exactly   #
  53. #                   this amount of mp to use.  You can also replace mp with    #
  54. #                   hp, tp, or gold and that much of each will be required     #
  55. #                   and consumed.                                              #
  56. #      cost[10% hp] - This is a percentage based skill cost.  The skill will   #
  57. #                     require this percentage of hp to use.  You can also      #
  58. #                     replace hp with mp or tp.  You can use this at the same  #
  59. #                     time as a standard cost and it will add to the two       #
  60. #                     together, for example, if you use both "cost[20 mp] and  #
  61. #                     cost[15% mp], the skill will cost 20 + 15% of the        #
  62. #                     party member's mp to use.                                #
  63. #      cost[all tp] - This cost will require all of the particular stat to     #
  64. #                     be used up when the skill is used.  There are two        #
  65. #                     differences between using this and using cost[100% tp].  #
  66. #                     first of all, this does not actually "require" the       #
  67. #                     user to have any of the particular stat, meaning the     #
  68. #                     user can have just 20 out of 100 TP currently and can    #
  69. #                     still use the skill.  The second difference is that      #
  70. #                     this cost waits until after ALL damage has been          #
  71. #                     calculated to apply the reduction.  This allows you to   #
  72. #                     use the current value in the damage calculation and      #
  73. #                     still have the stat until after the skill is done.       #
  74. #                     note that unlike the other cost values which cannot      #
  75. #                     make HP go below 1, this cost can actually kill the      #
  76. #                     user.                                                    #
  77. #      cost[5 i4 item] - This will require 5 of item 4 be in the inventory     #
  78. #                        for the skill to be used.  The items will be          #
  79. #                        consumed upon use of the skill.  You can also change  #
  80. #                        "i" to "a" or "w" to consume an armour or weapon      #
  81. #                        instead.  You can use more than one of this tag at a  #
  82. #                        time.                                                 #
  83. #      cost[state 5] - This cost requires the user to be inflicted with a      #
  84. #                      particular state in order to use.  This particular      #
  85. #                      cost will also use up the state, removing it from the   #
  86. #                      actor upon use.  You my have more than one of these on  #
  87. #                      a single skill.                                         #
  88. #      need[state 5] - The same as the one directly above, but does not        #
  89. #                      remove the state on use.                                #
  90. #      need[w 4] - Requires the user to be equipped with weapon number 4 for   #
  91. #                  the skill to be used.  You can also replace "w" with "a"    #
  92. #                  to require an armour instead.  More than one of these can   #
  93. #                  be used at a time.                                          #
  94. #      need[actor 2] - Requires actor 4 to be in the party before the skill    #
  95. #                      can be used.                                            #
  96. #      need[switch 6] - Requires switch 6 to be turned on.                     #
  97. #      need[variable 6 5+] - Requires variable 6 to be greater than or equal   #
  98. #                            to 5.  You can also use 5- to require the value   #
  99. #                            be less than or equal to 5, or just use 5 and it  #
  100. #                            has to be exactly equal to 5.                     #
  101. #      need[type 4] - This one is a little different than the others.  This    #
  102. #                     works exactly like the weapon type drop down boxes.  If  #
  103. #                     you want the skill to be usable by 3 or more weapon      #
  104. #                     types, you can use this tag.  In the example case, if    #
  105. #                     the user is equipped with weapon type 4, the skill can   #
  106. #                     be used.  If more than one of this type of tag is used,  #
  107. #                     the weapon types will only need to match one.            #
  108. ###--------------------------------------------------------------------------###
  109.  
  110. ###--------------------------------------------------------------------------###
  111. #      Config:                                                                 #
  112. #  These are the default values used by several of the functions in the        #
  113. #  script.  You may change these values as you find your game requires in      #
  114. #  order to give the player a better playing experience based on your game.    #
  115. #                                                                              #
  116. module CP         # Do not                                                     #
  117. module SKILLCOSTS #  change these.                                             #
  118. #                                                                              #
  119. ###-----                                                                -----###
  120. # Since only one cost can be shown for a skill by default, this is the order   #
  121. # of priority for costs.  The skill will check the requirements for each value #
  122. # you place in this array in order and draw the first one it finds a           #
  123. # requirement for.  You can use :mp, :hp, :tp, or :gold.                       #
  124. SHOW_PRIORITY = [:mp, :hp, :tp, :gold] # Default = [:mp, :hp, :tp, :gold]      #
  125. #                                                                              #
  126. ###-----                                                                -----###
  127. end                        # Don't edit                                        #
  128. end                        #  these 3                                          #
  129. class Window_Base < Window #   lines.                                          #
  130. #                                                                              #
  131. ###-----                                                                -----###
  132. # Use these two lines to set the text colours used by the script.  You can     #
  133. # change the number to use one of the window skin text colours or you can use  #
  134. # Color.new(r, g, b) to set a custom colour.                                   #
  135. def gold_cost_color;   text_color(17);  end;                                   #
  136. def hp_cost_color;     text_color(2);  end;                                    #
  137. #                                                                              #
  138. ###--------------------------------------------------------------------------###
  139.  
  140.  
  141. ###--------------------------------------------------------------------------###
  142. #  The following lines are the actual core code of the script.  While you are  #
  143. #  certainly invited to look, modifying it may result in undesirable results.  #
  144. #  Modify at your own risk!                                                    #
  145. ###--------------------------------------------------------------------------###
  146.  
  147.  
  148. end
  149.  
  150. module CP
  151. module SKILLCOSTS
  152.  
  153. COST = /cost\[(\d+*)[ ]?(%|i|w|a?)(\d+*)[ ]?(hp|mp|tp|gold|item|state)[ ]?(\d+*)\]/i
  154. NEED = /need\[(w|a|switch|actor|variable|state|type)[ ](\d+)[, ]*(\d*)(\+|-)*\]/i
  155. ALL = /cost\[all[ ](mp|tp|hp)\]/i  ## All the fancy regexps.
  156.  
  157. end
  158. end
  159.  
  160. $imported = {} if $imported.nil?
  161. $imported["CP_SKILLCOSTS"] = 1.1
  162.  
  163. class Game_BattlerBase
  164.   def skill_wtype_ok?(skill)  ## Replaces the weapon type check.
  165.     return true if enemy?
  166.     wtype_id1 = skill.required_wtype_id1
  167.     wtype_id2 = skill.required_wtype_id2
  168.     wtype_ide = skill.req_type  ## Sets up the added weapon types.
  169.     return true if wtype_id1 == 0 && wtype_id2 == 0 && wtype_ide.empty?
  170.     return true if wtype_id1 > 0 && wtype_equipped?(wtype_id1)
  171.     return true if wtype_id2 > 0 && wtype_equipped?(wtype_id2)
  172.     wtype_ide.each do |type|  ## Checks all weapon types.
  173.       return true if wtype_equipped?(type)
  174.     end
  175.     return false
  176.   end
  177.  
  178.   def skill_mp_cost(skill)  ## Skill cost with added percentage support.
  179.     ((skill.mp_cost + (mmp * skill.mpp_cost).to_i) * mcr).to_i
  180.   end
  181.  
  182.   def skill_tp_cost(skill)  ## TP with percentage.
  183.     skill.tp_cost + (max_tp * skill.tpp_cost).to_i
  184.   end
  185.  
  186.   def skill_hp_cost(skill)  ## HP with percentage.
  187.     skill.hp_cost + (mhp * skill.hpp_cost).to_i
  188.   end
  189.  
  190.   def skill_gold_cost(skill)  ## A skill's gold cost.
  191.     skill.gold_cost
  192.   end
  193.  
  194.   def item_cost_met(skill)  ## Check if the inventory contains the items.
  195.     result = true
  196.     return true if enemy?  ## Ignore this if it's an enemy.
  197.     return result if skill.item_cost.empty?
  198.     skill.item_cost.each do |item|
  199.       case item.kind
  200.       when 1
  201.         req = $data_items[item.id]
  202.       when 2
  203.         req = $data_weapons[item.id]
  204.       when 3
  205.         req = $data_armors[item.id]
  206.       end
  207.       next if $game_party.item_number(req) >= item.cost
  208.       result = false
  209.     end
  210.     return result
  211.   end
  212.  
  213.   def item_cost_pay(skill)
  214.     return if enemy?  ## Prevents enemies from using the party's inventory.
  215.     return if skill.item_cost.empty?
  216.     skill.item_cost.each do |item|
  217.       case item.kind
  218.       when 1
  219.         req = $data_items[item.id]
  220.       when 2
  221.         req = $data_weapons[item.id]
  222.       when 3
  223.         req = $data_armors[item.id]
  224.       end
  225.       $game_party.lose_item(req, item.cost)  ## Removes the items.
  226.     end
  227.   end
  228.  
  229.   def state_cost_met(skill)
  230.     result = true  ## Used to check if the player has the conditions.
  231.     return result if skill.req_state.empty?
  232.     skill.req_state.each do |state|
  233.       next if state?(state)
  234.       result = false
  235.     end
  236.     return result
  237.   end
  238.  
  239.   def state_cost_pay(skill)  ## Removes states if needed.
  240.     return if skill.state_cost.empty?
  241.     skill.state_cost.each do |state|
  242.       remove_state(state)
  243.     end
  244.   end
  245.  
  246.   def switch_cost_met(skill)
  247.     result = true  ## Checks switches.
  248.     return result if skill.req_switch.empty?
  249.     skill.req_switch.each do |switch|
  250.       next if $game_switches[switch]
  251.       result = false
  252.     end
  253.     return result
  254.   end
  255.  
  256.   def variable_cost_met(skill)
  257.     result = true  ## Performs the checks for variables.
  258.     return result if skill.req_variable.empty?
  259.     skill.req_variable.each do |variable|
  260.       if variable[2] == '+'
  261.         next if $game_variables[variable[0]] >= variable[1]
  262.       elsif variable[2] == '-'
  263.         next if $game_variables[variable[0]] <= variable[1]
  264.       else
  265.         next if $game_variables[variable[0]] == variable[1]
  266.       end
  267.       result = false
  268.     end
  269.     return result
  270.   end
  271.  
  272.   def actor_cost_met(skill)
  273.     result = true  ## Checks if an actor is in the battle party.
  274.     return result if skill.req_actor.empty?
  275.     skill.req_actor.each do |actor|
  276.       next if $game_party.battle_members.include?($game_actors[actor])
  277.       result = false
  278.     end
  279.     return result
  280.   end
  281.  
  282.   def equip_cost_met(skill)
  283.     result = true  ## Checks if the required items are equipped.
  284.     return result if enemy?
  285.     return result if (skill.req_w.empty? && skill.req_a.empty?)
  286.     skill.req_w.each do |wep|
  287.       next if weapons.include?($data_weapons[wep])
  288.       result = false
  289.     end
  290.     skill.req_a.each do |arm|
  291.       next if armors.include?($data_armors[arm])
  292.       result = false
  293.     end
  294.     return result
  295.   end
  296.  
  297.   def skill_cost_payable?(skill)  ## Adds all the other conditions.
  298.     tp >= skill_tp_cost(skill) && mp >= skill_mp_cost(skill) &&
  299.     hp > skill_hp_cost(skill) && $game_party.gold >= skill_gold_cost(skill) &&
  300.     item_cost_met(skill) && state_cost_met(skill) && switch_cost_met(skill) &&
  301.     variable_cost_met(skill) && actor_cost_met(skill) && equip_cost_met(skill)
  302.   end
  303.  
  304.   def pay_skill_cost(skill)  ## Adds a few more pay costs.
  305.     self.mp -= skill_mp_cost(skill)
  306.     self.tp -= skill_tp_cost(skill)
  307.     self.hp -= skill_hp_cost(skill)
  308.     $game_party.lose_gold(skill_gold_cost(skill))
  309.     item_cost_pay(skill)
  310.     state_cost_pay(skill)
  311.   end
  312. end
  313.  
  314. class Scene_Battle < Scene_Base
  315.   alias cp_sc_use_item use_item unless $@
  316.   def use_item  ## Adds the final cost.
  317.     cp_sc_use_item
  318.     return if @subject.current_action.nil?
  319.     item = @subject.current_action.item
  320.     final_cost(@subject, item)
  321.   end
  322.  
  323.   def final_cost(user, item)  ## The final cost for removing all of a stat.
  324.     user.tp = 0 if item.all_tp
  325.     user.mp = 0 if item.all_mp
  326.     user.hp = 0 if item.all_hp
  327.   end
  328. end
  329.  
  330. class Window_SkillList < Window_Selectable
  331.   def draw_skill_cost(rect, skill)
  332.     CP::SKILLCOSTS::SHOW_PRIORITY.each do |type|
  333.       case type  ## Draws skill type based on priority.
  334.       when :hp
  335.         if @actor.skill_hp_cost(skill) > 0
  336.           change_color(hp_cost_color, enable?(skill))
  337.           draw_text(rect, @actor.skill_hp_cost(skill), 2)
  338.           break
  339.         end
  340.       when :mp
  341.         if @actor.skill_mp_cost(skill) > 0
  342.           change_color(mp_cost_color, enable?(skill))
  343.           draw_text(rect, @actor.skill_mp_cost(skill), 2)
  344.           break
  345.         end
  346.       when :tp
  347.         if @actor.skill_tp_cost(skill) > 0
  348.           change_color(tp_cost_color, enable?(skill))
  349.           draw_text(rect, @actor.skill_tp_cost(skill), 2)
  350.           break
  351.         end
  352.       when :gold
  353.         if @actor.skill_gold_cost(skill) > 0
  354.           change_color(gold_cost_color, enable?(skill))
  355.           draw_text(rect, @actor.skill_gold_cost(skill), 2)
  356.           break
  357.         end
  358.       end
  359.     end
  360.   end
  361. end
  362.  
  363. class Skill_Cost  ## Small class that holds item type and cost.
  364.   attr_accessor :kind
  365.   attr_accessor :id
  366.   attr_accessor :cost
  367.  
  368.   def initialize(kind = 1, id = 1, cost = 0)
  369.     @kind = kind
  370.     @id = id
  371.     @cost = cost
  372.   end
  373. end
  374.  
  375. class RPG::UsableItem < RPG::BaseItem  ## All the new skill costs.
  376.   attr_accessor :gold_cost
  377.   attr_accessor :hp_cost
  378.   attr_accessor :item_cost
  379.   attr_accessor :hpp_cost
  380.   attr_accessor :mpp_cost
  381.   attr_accessor :tpp_cost
  382.   attr_accessor :state_cost
  383.   attr_accessor :all_hp
  384.   attr_accessor :all_mp
  385.   attr_accessor :all_tp
  386.   attr_accessor :req_a
  387.   attr_accessor :req_w
  388.   attr_accessor :req_state
  389.   attr_accessor :req_switch
  390.   attr_accessor :req_variable
  391.   attr_accessor :req_actor
  392.   attr_accessor :req_type
  393.  
  394.   def set_new_costs
  395.     return if @new_cost_set; @new_cost_set = true
  396.     @gold_cost = 0  ## Sets up the basic costs.
  397.     @hp_cost = 0
  398.     @item_cost = []
  399.     @hpp_cost = 0
  400.     @mpp_cost = 0
  401.     @tpp_cost = 0
  402.     @state_cost = []
  403.     @all_hp = false
  404.     @all_mp = false
  405.     @all_tp = false
  406.     @req_a = []
  407.     @req_w = []
  408.     @req_state = []
  409.     @req_switch = []
  410.     @req_variable = []
  411.     @req_actor = []
  412.     @req_type = []
  413.     self.note.split(/[\r\n]+/).each do |line|
  414.       case line  ## Basic costs.
  415.       when CP::SKILLCOSTS::COST
  416.         cost_type = $4.to_s
  417.         case cost_type.downcase
  418.         when 'mp'
  419.           if $2.to_s == '%'  ## Checks percentage or not.
  420.             @mpp_cost = $1.to_f * 0.01
  421.           else
  422.             @mp_cost = $1.to_i
  423.           end
  424.         when 'hp'
  425.           if $2.to_s == '%'
  426.             @hpp_cost = $1.to_f * 0.01
  427.           else
  428.             @hp_cost = $1.to_i
  429.           end
  430.         when 'tp'
  431.           if $2.to_s == '%'
  432.             @tpp_cost = $1.to_f * 0.01
  433.           else
  434.             @tp_cost = $1.to_i
  435.           end
  436.         when 'gold'  ## Adds gold cost.
  437.           @gold_cost = $1.to_i
  438.         when 'item'  ## Item usage check.
  439.           case $2.to_s
  440.           when 'i', 'I'
  441.             kind = 1
  442.           when 'w', 'W'
  443.             kind = 2
  444.           when 'a', 'A'
  445.             kind = 3
  446.           else
  447.             kind = 1
  448.           end  ## Pushes the item cost to an array.
  449.           @item_cost.push(Skill_Cost.new(kind, $3.to_i, $1.to_i))
  450.         when 'state'  ## Pushes a use state to the need array for checking.
  451.           @state_cost.push($5.to_i)
  452.           @req_state.push($5.to_i)
  453.         end
  454.       when CP::SKILLCOSTS::ALL
  455.         cost_type = $1.to_s
  456.         case cost_type.downcase
  457.         when 'mp'  ## Checks if all usage is true.
  458.           @all_mp = true
  459.         when 'hp'
  460.           @all_hp = true
  461.         when 'tp'
  462.           @all_tp = true
  463.         end
  464.       when CP::SKILLCOSTS::NEED
  465.         cost_type = $1.to_s  ## Simple pushes for requirements.
  466.         case cost_type.downcase
  467.         when 'a'
  468.           @req_a.push($2.to_i)
  469.         when 'w'
  470.           @req_w.push($2.to_i)
  471.         when 'state'
  472.           @req_state.push($2.to_i)
  473.         when 'switch'
  474.           @req_switch.push($2.to_i)
  475.         when 'variable'
  476.           @req_variable.push([$2.to_i, $3.to_i, $4.to_s])
  477.         when 'actor'
  478.           @req_actor.push($2.to_i)
  479.         when 'type'
  480.           @req_type.push($2.to_i)
  481.         end
  482.       end #when
  483.     end #split
  484.   end
  485. end
  486.  
  487. module DataManager
  488.   class << self
  489.    
  490.   alias cp_sc_load_database load_database unless $@
  491.   def load_database
  492.     cp_sc_load_database
  493.     create_new_skill_costs
  494.   end
  495.  
  496.   def create_new_skill_costs  ## Adds requirements to all skills.
  497.     groups = [$data_skills]
  498.     for group in groups
  499.       for obj in group
  500.         next if obj == nil
  501.         obj.set_new_costs if obj.is_a?(RPG::Skill)
  502.       end
  503.     end
  504.   end
  505.  
  506.   end
  507. end
RAW Paste Data