Advertisement
trevync

Trvc Stat Manager

May 28th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.79 KB | None | 0 0
  1. $imported_trvc_params = true # make this false to disable this script
  2. if $imported_trvc_params
  3. # =============================================================================
  4. # ======== INTRODUCTION =======================================================
  5. # =============================================================================
  6. #   Name:    Stat Manager
  7. #   Author:  Trevyn MC (http://trevync.wordpress.com/)
  8. #   Version: 1.2 (5/28/14)
  9. #  
  10. #   Purpose: Allows for the easy creation of custom stats. Features a number of
  11. #            "plugins" that cause custom stats to effect the game in various
  12. #            ways
  13. #
  14. #   Dependencies:  None
  15. #
  16. #   Terms Of Use:
  17. #     1 Always give credit
  18. #     2 Let me know when you release a game (a response to the forum post about
  19. #       this script is enough)
  20. #     3 Always include the full introduction section with the script
  21. #    
  22. #        Otherwise you are free to use and modify as you see fit for both
  23. #        commercial or non_commercial projects
  24. #
  25. #   Compatibility:
  26. #      Overwrites: None
  27. #      Alias:      1 Method
  28. #         Data_Manager::load_database
  29. #
  30. #      Mostly This script just adds new stats. since by itself it doesn't
  31. #      actually do anything with them there should not be any compatibility
  32. #      issues
  33. #  
  34. #   Change_log:
  35. #      5/28/14:  1.0  Initial Release
  36. #      5/28/14:  1.1  Bug fix. Changed script calls
  37. #      5/28/14:  1.2  Conditional and Formula Modifiers
  38. #
  39. # =============================================================================
  40. # ========= INSTRUCTIONS ======================================================
  41. # =============================================================================
  42. #   Place this script Above Materials and below main
  43. #
  44. #   To add a new stat to the game simply put the name of the stat in the
  45. #   Add_New_Params_Here array
  46. #
  47. #   After that the new stat will be added to the game! it is that simple
  48. #
  49. #   To retrieve the stats value with a simple call
  50. #
  51. #   $game_actors[id].t_stat(param_name)
  52. #       Returns the combined value of all objects that modify that stat
  53. #       for the specified actor.
  54. #
  55. #       You can also use these script calls to modify custom paramaters of
  56. #       an actor
  57. #   $game_actors[id].t_stat_rate(rate, param_name)
  58. #       Or
  59. #   $game_actors[id].t_stat_plus(plus, param_name)
  60. #       Or
  61. #   $game_actors[id].t_stat_reset(param_name)
  62. #
  63. #   Notetags:
  64. #   <stat_name: (+,-,*) value>
  65. #   Put this notetag on Actors, Enemies, States, Classes, Equips, Or Skills
  66. #
  67. #   example
  68. #   <str: - 6>
  69. #   <cha: + 5>
  70. #   <dex: * 2.5>
  71. #   etc
  72. #
  73. #   In addition there are two more note_tag types.
  74. #   conditions and formulas. Use these wisely! as making conditions/formulas
  75. #   based on other t_stats will potentially crash the game...
  76. #       Avoid recursive calls!
  77. #  
  78. #   <int: condition>
  79. #   actor? ? true : false
  80. #   </int>
  81. #   <wis: formula>
  82. #   level * 12
  83. #   </wis>
  84. #  
  85. #   multitple conditions or multiple formulas on the same object affecting the
  86. #   same stat will be ignored.
  87. #      
  88. #  
  89. #
  90. #
  91. #   One more note is if you put
  92. #   a.t_stat(param_name)
  93. #         or
  94. #   b.t_stat(param_name)
  95. #
  96. #   in a skill damage formula then you can modify skill damage base on these
  97. #   parameters.
  98. #
  99. #
  100. # =============================================================================
  101. # ========= CUSTOMIZATION =====================================================
  102. # =============================================================================
  103.  
  104. module Trvc_Params
  105.   Add_New_Params_Here = [
  106.     # [name , formula],
  107.     "str",
  108.     "wis",
  109.     "dex",
  110.     "con",
  111.     "int",
  112.     "cha",
  113.   ] # do not touch
  114.  
  115.  
  116.  
  117.  
  118.   Params = [# they can also be added here if you understand how this script works
  119.   # [[:catagory_name, Id],["notetag value", Notetag_Id(optional)]]
  120.   # if Notetag_Id is specified then the notetage will be
  121.   # <stat_catagory stat_id: (+,-,*) value> and the calls to access a stat become
  122.   # t_param(name, id)
  123.  
  124.  
  125.   ]
  126. end
  127. # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  128. # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  129. # =============================================================================
  130. # ========= TECHNICAL STUFF ===================================================
  131. # =============================================================================
  132.  
  133. module Trvc_Params
  134.   # ===========================================================================
  135.   # ======================== Read Notetags on database load ===================
  136.   # ===========================================================================
  137.   def self.init_params
  138.     Add_New_Params_Here.each do |n|
  139.       Params.push([[n.to_sym, 0],[n]])
  140.    
  141.     end
  142.     for o in $data_skills | $data_classes | $data_enemies | $data_states | $data_actors | $data_weapons | $data_armors
  143.       next if o.nil?
  144.       next unless o.note.match(/<.*>/)# skipps objects that have not notetags
  145.       for p in Params
  146.         next if p.nil?
  147.         read_param_note(o, p)
  148.       end
  149.     end
  150.   end
  151.   # ======================== Read an individual notetag =======================
  152.   def self. read_param_note(o, p)
  153.     return if p.nil?
  154.     key = p[1][0]
  155.     return unless o.note.match(/<(#{key})/)# Skips if key isn't found
  156.     id  = p[1][1]
  157.     id  = id.to_s if id
  158.     id  = "" if id.nil?
  159.     ary = o.note.scan(/<(#{key})[ _\-]?(#{id}):\s*([\+\*\-]?)\s*(\d*\.*\d+)>/)
  160.     return unless ary
  161.     for a in ary
  162.       next if a.nil?
  163.       rate = 1.0
  164.       plus = 0
  165.       case a[2]
  166.       when "*"
  167.         rate = a[3].to_f
  168.       when "-"
  169.         plus = -(a[3].to_f)
  170.       else
  171.         plus = a[3].to_f
  172.       end
  173.       o.add_t_param(p[0][0],p[0][1], Param.new(rate,plus))
  174.     end
  175.     options = o.note.scan(/<#{key}[ _\-]?#{id}:\s*(condition|formula)>(.*)<\/#{key}/m)
  176.     for opt in options
  177.       next if opt.nil?
  178.       case opt[0]
  179.       when "formula"
  180.         formula = opt[1]
  181.       when "condition"
  182.         condition = opt[1]
  183.       end
  184.       par = Param.new
  185.       par.formula = formula if formula
  186.       par.condition = condition if condition
  187.       o.add_t_param(p[0][0],p[0][1], par)
  188.     end
  189.    
  190.    
  191.   end
  192.   # ===========================================================================
  193.   # =========================== Param Class Definition ========================
  194.   # ===========================================================================
  195.   class Param
  196.     # ========================= Initialize members ============================
  197.     def initialize(r = 1.0, p = 0)
  198.       @rate = r
  199.       @plus = p
  200.       @formula = "0"
  201.       @condition = "true"
  202.     end
  203.     attr_accessor :condition
  204.     attr_accessor :formula
  205.     attr_accessor :rate
  206.     attr_accessor :plus
  207.    
  208.     # ========================= Public Method for combining parameter =========
  209.     def combine(p)
  210.       return self unless p.is_a? Param
  211.       @rate *= p.rate
  212.       @plus += p.plus
  213.       @condition = p.condition if @condition == "true"
  214.       @formula   = p.formula   if @formula   == "0"
  215.       return self
  216.     end
  217.  
  218.     # ========================== Calculates and returns the value =============
  219.     def value
  220.       result = @plus * @rate
  221.       result
  222.     end
  223.    
  224.     # ========================== Resets the value to default ==================
  225.     def reset
  226.       @rate = 1.0
  227.       @plus = 0
  228.     end
  229.   end
  230.   # ================================= End Class Param =========================
  231. end
  232. # =================================== End Module Trvc_Params ==================
  233.  
  234. # =============================================================================
  235. # ======================= Add or retrieve Params from database objects
  236. # =============================================================================
  237. class RPG::BaseItem
  238.   # =========================== To retrieve ===================================
  239.   def get_t_param(type, id)
  240.     return nil if @trvc_params.nil?
  241.     return nil if @trvc_params[type].nil?
  242.     return @trvc_params[type][id]
  243.   end
  244.   # =========================== To Add ========================================
  245.   def add_t_param(type, id, p)
  246.     @trvc_params = {} if @trvc_params.nil?
  247.     @trvc_params[type] = [] if @trvc_params[type].nil?
  248.     if @trvc_params[type][id].nil?
  249.       @trvc_params[type][id] = p
  250.     else
  251.       @trvc_params[type][id].combine(p)
  252.     end
  253.   end
  254. end
  255. # ====================================== End RPG::Base_Item ===================
  256.  
  257.  
  258. # =============================================================================
  259. # ===================================== Access Parameters by actor ============
  260. # =============================================================================
  261. class Game_Battler < Game_BattlerBase
  262.   def t_stat(type, id = 0)
  263.     t_param(type, id).value
  264.   end
  265.  
  266.  
  267.   # ===================================== Retrieve param object ===============
  268.   def t_param(type, id = 0)
  269.     p = Trvc_Params::Param.new
  270.     ary = collect_t_feature_objs.compact.push( self)
  271.     for o in ary
  272.       temp = o.get_t_param(type, id)
  273.       next if temp.nil?
  274.       next unless eval(temp.condition) rescue false
  275.       p.plus += eval(temp.formula) rescue 0
  276.       p.combine(temp)
  277.     end
  278.     p
  279.   end
  280.  
  281.   # ===================================== Add a bonus value ===================
  282.   def t_stat_plus(value, type, id = 0)
  283.     p = Trvc_Params::Param.new(1.0, value)
  284.     add_t_param(type, id, p)
  285.   end
  286.   # ===================================== Add a rate ==========================
  287.   def t_stat_rate(value, type, id = 0)
  288.     p = Trvc_Params::Param.new(value, 0)
  289.     add_t_param(type, id, p)
  290.   end
  291.   # ===================================== Reset local modifiers ===============
  292.   def t_stat_reset(type, id = 0)
  293.     get_t_param(type, id).reset unless get_t_param(type, id).nil?
  294.   end
  295.  
  296.   # ===========================================================================
  297.   # ======================= Add or retrieve local Params from battlers
  298.   # ===========================================================================
  299.  
  300.   # =========================== To retrieve ===================================
  301.   def get_t_param(type, id)
  302.     return nil if @trvc_params.nil?
  303.     return nil if @trvc_params[type].nil?
  304.     return @trvc_params[type][id]
  305.   end
  306.   # =========================== To Add ========================================
  307.   def add_t_param(type, id, p)
  308.     @trvc_params = {} if @trvc_params.nil?
  309.     @trvc_params[type] = [] if @trvc_params[type].nil?
  310.     if @trvc_params[type][id].nil?
  311.       @trvc_params[type][id] = p
  312.     else
  313.       @trvc_params[type][id].combine(p)
  314.     end
  315.   end
  316.  
  317.  
  318.   def debug_test
  319.     print t_stat(:cha)
  320.    
  321.   end
  322.  
  323.  
  324. end
  325. # ====================================== End Game_Battler ===================
  326.  
  327.  
  328. # ===========================================================================
  329. # ===================== Get Lists of objects with params ====================
  330. # ===========================================================================
  331. class Game_Battler < Game_BattlerBase
  332.   def collect_t_feature_objs
  333.   end
  334. end
  335. class Game_Enemy < Game_Battler
  336.   def collect_t_feature_objs
  337.     ary = [enemy] + states
  338.     ary.compact
  339.   end
  340. end
  341. class Game_Actor < Game_Battler
  342.   def collect_t_feature_objs
  343.     ary = skills + equips + states + [self.class] + [actor]
  344.     ary.compact
  345.   end
  346. end
  347. # ===========================================================================
  348. # ======================= Set up parameters on application start ============
  349. # ===========================================================================
  350. module DataManager
  351.   class << self
  352.     alias trvc_load_database load_database
  353.     alias trvc_create_objects create_game_objects
  354.   end
  355.   def self.load_database
  356.     trvc_load_database
  357.     Trvc_Params::init_params
  358.   end
  359. end
  360.  
  361. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement